xref: /openbmc/qemu/target/arm/helper.c (revision 55abfc1f)
1 /*
2  * ARM generic helpers.
3  *
4  * This code is licensed under the GNU GPL v2 or later.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "trace.h"
12 #include "cpu.h"
13 #include "internals.h"
14 #include "cpu-features.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/main-loop.h"
17 #include "qemu/timer.h"
18 #include "qemu/bitops.h"
19 #include "qemu/crc32c.h"
20 #include "qemu/qemu-print.h"
21 #include "exec/exec-all.h"
22 #include <zlib.h> /* For crc32 */
23 #include "hw/irq.h"
24 #include "sysemu/cpu-timers.h"
25 #include "sysemu/kvm.h"
26 #include "sysemu/tcg.h"
27 #include "qapi/error.h"
28 #include "qemu/guest-random.h"
29 #ifdef CONFIG_TCG
30 #include "semihosting/common-semi.h"
31 #endif
32 #include "cpregs.h"
33 
34 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
35 
36 static void switch_mode(CPUARMState *env, int mode);
37 
38 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
39 {
40     assert(ri->fieldoffset);
41     if (cpreg_field_is_64bit(ri)) {
42         return CPREG_FIELD64(env, ri);
43     } else {
44         return CPREG_FIELD32(env, ri);
45     }
46 }
47 
48 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
49 {
50     assert(ri->fieldoffset);
51     if (cpreg_field_is_64bit(ri)) {
52         CPREG_FIELD64(env, ri) = value;
53     } else {
54         CPREG_FIELD32(env, ri) = value;
55     }
56 }
57 
58 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
59 {
60     return (char *)env + ri->fieldoffset;
61 }
62 
63 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
64 {
65     /* Raw read of a coprocessor register (as needed for migration, etc). */
66     if (ri->type & ARM_CP_CONST) {
67         return ri->resetvalue;
68     } else if (ri->raw_readfn) {
69         return ri->raw_readfn(env, ri);
70     } else if (ri->readfn) {
71         return ri->readfn(env, ri);
72     } else {
73         return raw_read(env, ri);
74     }
75 }
76 
77 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
78                              uint64_t v)
79 {
80     /*
81      * Raw write of a coprocessor register (as needed for migration, etc).
82      * Note that constant registers are treated as write-ignored; the
83      * caller should check for success by whether a readback gives the
84      * value written.
85      */
86     if (ri->type & ARM_CP_CONST) {
87         return;
88     } else if (ri->raw_writefn) {
89         ri->raw_writefn(env, ri, v);
90     } else if (ri->writefn) {
91         ri->writefn(env, ri, v);
92     } else {
93         raw_write(env, ri, v);
94     }
95 }
96 
97 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
98 {
99    /*
100     * Return true if the regdef would cause an assertion if you called
101     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
102     * program bug for it not to have the NO_RAW flag).
103     * NB that returning false here doesn't necessarily mean that calling
104     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
105     * read/write access functions which are safe for raw use" from "has
106     * read/write access functions which have side effects but has forgotten
107     * to provide raw access functions".
108     * The tests here line up with the conditions in read/write_raw_cp_reg()
109     * and assertions in raw_read()/raw_write().
110     */
111     if ((ri->type & ARM_CP_CONST) ||
112         ri->fieldoffset ||
113         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
114         return false;
115     }
116     return true;
117 }
118 
119 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
120 {
121     /* Write the coprocessor state from cpu->env to the (index,value) list. */
122     int i;
123     bool ok = true;
124 
125     for (i = 0; i < cpu->cpreg_array_len; i++) {
126         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
127         const ARMCPRegInfo *ri;
128         uint64_t newval;
129 
130         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
131         if (!ri) {
132             ok = false;
133             continue;
134         }
135         if (ri->type & ARM_CP_NO_RAW) {
136             continue;
137         }
138 
139         newval = read_raw_cp_reg(&cpu->env, ri);
140         if (kvm_sync) {
141             /*
142              * Only sync if the previous list->cpustate sync succeeded.
143              * Rather than tracking the success/failure state for every
144              * item in the list, we just recheck "does the raw write we must
145              * have made in write_list_to_cpustate() read back OK" here.
146              */
147             uint64_t oldval = cpu->cpreg_values[i];
148 
149             if (oldval == newval) {
150                 continue;
151             }
152 
153             write_raw_cp_reg(&cpu->env, ri, oldval);
154             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
155                 continue;
156             }
157 
158             write_raw_cp_reg(&cpu->env, ri, newval);
159         }
160         cpu->cpreg_values[i] = newval;
161     }
162     return ok;
163 }
164 
165 bool write_list_to_cpustate(ARMCPU *cpu)
166 {
167     int i;
168     bool ok = true;
169 
170     for (i = 0; i < cpu->cpreg_array_len; i++) {
171         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
172         uint64_t v = cpu->cpreg_values[i];
173         const ARMCPRegInfo *ri;
174 
175         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
176         if (!ri) {
177             ok = false;
178             continue;
179         }
180         if (ri->type & ARM_CP_NO_RAW) {
181             continue;
182         }
183         /*
184          * Write value and confirm it reads back as written
185          * (to catch read-only registers and partially read-only
186          * registers where the incoming migration value doesn't match)
187          */
188         write_raw_cp_reg(&cpu->env, ri, v);
189         if (read_raw_cp_reg(&cpu->env, ri) != v) {
190             ok = false;
191         }
192     }
193     return ok;
194 }
195 
196 static void add_cpreg_to_list(gpointer key, gpointer opaque)
197 {
198     ARMCPU *cpu = opaque;
199     uint32_t regidx = (uintptr_t)key;
200     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
201 
202     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
203         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
204         /* The value array need not be initialized at this point */
205         cpu->cpreg_array_len++;
206     }
207 }
208 
209 static void count_cpreg(gpointer key, gpointer opaque)
210 {
211     ARMCPU *cpu = opaque;
212     const ARMCPRegInfo *ri;
213 
214     ri = g_hash_table_lookup(cpu->cp_regs, key);
215 
216     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
217         cpu->cpreg_array_len++;
218     }
219 }
220 
221 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
222 {
223     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
224     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
225 
226     if (aidx > bidx) {
227         return 1;
228     }
229     if (aidx < bidx) {
230         return -1;
231     }
232     return 0;
233 }
234 
235 void init_cpreg_list(ARMCPU *cpu)
236 {
237     /*
238      * Initialise the cpreg_tuples[] array based on the cp_regs hash.
239      * Note that we require cpreg_tuples[] to be sorted by key ID.
240      */
241     GList *keys;
242     int arraylen;
243 
244     keys = g_hash_table_get_keys(cpu->cp_regs);
245     keys = g_list_sort(keys, cpreg_key_compare);
246 
247     cpu->cpreg_array_len = 0;
248 
249     g_list_foreach(keys, count_cpreg, cpu);
250 
251     arraylen = cpu->cpreg_array_len;
252     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
253     cpu->cpreg_values = g_new(uint64_t, arraylen);
254     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
255     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
256     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
257     cpu->cpreg_array_len = 0;
258 
259     g_list_foreach(keys, add_cpreg_to_list, cpu);
260 
261     assert(cpu->cpreg_array_len == arraylen);
262 
263     g_list_free(keys);
264 }
265 
266 /*
267  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
268  */
269 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
270                                         const ARMCPRegInfo *ri,
271                                         bool isread)
272 {
273     if (!is_a64(env) && arm_current_el(env) == 3 &&
274         arm_is_secure_below_el3(env)) {
275         return CP_ACCESS_TRAP_UNCATEGORIZED;
276     }
277     return CP_ACCESS_OK;
278 }
279 
280 /*
281  * Some secure-only AArch32 registers trap to EL3 if used from
282  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
283  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
284  * We assume that the .access field is set to PL1_RW.
285  */
286 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
287                                             const ARMCPRegInfo *ri,
288                                             bool isread)
289 {
290     if (arm_current_el(env) == 3) {
291         return CP_ACCESS_OK;
292     }
293     if (arm_is_secure_below_el3(env)) {
294         if (env->cp15.scr_el3 & SCR_EEL2) {
295             return CP_ACCESS_TRAP_EL2;
296         }
297         return CP_ACCESS_TRAP_EL3;
298     }
299     /* This will be EL1 NS and EL2 NS, which just UNDEF */
300     return CP_ACCESS_TRAP_UNCATEGORIZED;
301 }
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 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 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
366 static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
367                                     bool isread)
368 {
369     if (arm_current_el(env) == 1 &&
370         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
371         return CP_ACCESS_TRAP_EL2;
372     }
373     return CP_ACCESS_OK;
374 }
375 
376 #ifdef TARGET_AARCH64
377 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
378 static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
379                                     bool isread)
380 {
381     if (arm_current_el(env) == 1 &&
382         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
383         return CP_ACCESS_TRAP_EL2;
384     }
385     return CP_ACCESS_OK;
386 }
387 #endif
388 
389 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
390 {
391     ARMCPU *cpu = env_archcpu(env);
392 
393     raw_write(env, ri, value);
394     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
395 }
396 
397 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
398 {
399     ARMCPU *cpu = env_archcpu(env);
400 
401     if (raw_read(env, ri) != value) {
402         /*
403          * Unlike real hardware the qemu TLB uses virtual addresses,
404          * not modified virtual addresses, so this causes a TLB flush.
405          */
406         tlb_flush(CPU(cpu));
407         raw_write(env, ri, value);
408     }
409 }
410 
411 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
412                              uint64_t value)
413 {
414     ARMCPU *cpu = env_archcpu(env);
415 
416     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
417         && !extended_addresses_enabled(env)) {
418         /*
419          * For VMSA (when not using the LPAE long descriptor page table
420          * format) this register includes the ASID, so do a TLB flush.
421          * For PMSA it is purely a process ID and no action is needed.
422          */
423         tlb_flush(CPU(cpu));
424     }
425     raw_write(env, ri, value);
426 }
427 
428 static int alle1_tlbmask(CPUARMState *env)
429 {
430     /*
431      * Note that the 'ALL' scope must invalidate both stage 1 and
432      * stage 2 translations, whereas most other scopes only invalidate
433      * stage 1 translations.
434      */
435     return (ARMMMUIdxBit_E10_1 |
436             ARMMMUIdxBit_E10_1_PAN |
437             ARMMMUIdxBit_E10_0 |
438             ARMMMUIdxBit_Stage2 |
439             ARMMMUIdxBit_Stage2_S);
440 }
441 
442 
443 /* IS variants of TLB operations must affect all cores */
444 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
445                              uint64_t value)
446 {
447     CPUState *cs = env_cpu(env);
448 
449     tlb_flush_all_cpus_synced(cs);
450 }
451 
452 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
453                              uint64_t value)
454 {
455     CPUState *cs = env_cpu(env);
456 
457     tlb_flush_all_cpus_synced(cs);
458 }
459 
460 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
461                              uint64_t value)
462 {
463     CPUState *cs = env_cpu(env);
464 
465     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
466 }
467 
468 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
469                              uint64_t value)
470 {
471     CPUState *cs = env_cpu(env);
472 
473     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
474 }
475 
476 /*
477  * Non-IS variants of TLB operations are upgraded to
478  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
479  * force broadcast of these operations.
480  */
481 static bool tlb_force_broadcast(CPUARMState *env)
482 {
483     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
484 }
485 
486 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
487                           uint64_t value)
488 {
489     /* Invalidate all (TLBIALL) */
490     CPUState *cs = env_cpu(env);
491 
492     if (tlb_force_broadcast(env)) {
493         tlb_flush_all_cpus_synced(cs);
494     } else {
495         tlb_flush(cs);
496     }
497 }
498 
499 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
500                           uint64_t value)
501 {
502     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
503     CPUState *cs = env_cpu(env);
504 
505     value &= TARGET_PAGE_MASK;
506     if (tlb_force_broadcast(env)) {
507         tlb_flush_page_all_cpus_synced(cs, value);
508     } else {
509         tlb_flush_page(cs, value);
510     }
511 }
512 
513 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
514                            uint64_t value)
515 {
516     /* Invalidate by ASID (TLBIASID) */
517     CPUState *cs = env_cpu(env);
518 
519     if (tlb_force_broadcast(env)) {
520         tlb_flush_all_cpus_synced(cs);
521     } else {
522         tlb_flush(cs);
523     }
524 }
525 
526 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
527                            uint64_t value)
528 {
529     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
530     CPUState *cs = env_cpu(env);
531 
532     value &= TARGET_PAGE_MASK;
533     if (tlb_force_broadcast(env)) {
534         tlb_flush_page_all_cpus_synced(cs, value);
535     } else {
536         tlb_flush_page(cs, value);
537     }
538 }
539 
540 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
541                                uint64_t value)
542 {
543     CPUState *cs = env_cpu(env);
544 
545     tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
546 }
547 
548 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
549                                   uint64_t value)
550 {
551     CPUState *cs = env_cpu(env);
552 
553     tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
554 }
555 
556 
557 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
558                               uint64_t value)
559 {
560     CPUState *cs = env_cpu(env);
561 
562     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
563 }
564 
565 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
566                                  uint64_t value)
567 {
568     CPUState *cs = env_cpu(env);
569 
570     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
571 }
572 
573 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
574                               uint64_t value)
575 {
576     CPUState *cs = env_cpu(env);
577     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
578 
579     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
580 }
581 
582 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
583                                  uint64_t value)
584 {
585     CPUState *cs = env_cpu(env);
586     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
587 
588     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
589                                              ARMMMUIdxBit_E2);
590 }
591 
592 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
593                                 uint64_t value)
594 {
595     CPUState *cs = env_cpu(env);
596     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
597 
598     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
599 }
600 
601 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
602                                 uint64_t value)
603 {
604     CPUState *cs = env_cpu(env);
605     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
606 
607     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
608 }
609 
610 static const ARMCPRegInfo cp_reginfo[] = {
611     /*
612      * Define the secure and non-secure FCSE identifier CP registers
613      * separately because there is no secure bank in V8 (no _EL3).  This allows
614      * the secure register to be properly reset and migrated. There is also no
615      * v8 EL1 version of the register so the non-secure instance stands alone.
616      */
617     { .name = "FCSEIDR",
618       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
619       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
620       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
621       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
622     { .name = "FCSEIDR_S",
623       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
624       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
625       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
626       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
627     /*
628      * Define the secure and non-secure context identifier CP registers
629      * separately because there is no secure bank in V8 (no _EL3).  This allows
630      * the secure register to be properly reset and migrated.  In the
631      * non-secure case, the 32-bit register will have reset and migration
632      * disabled during registration as it is handled by the 64-bit instance.
633      */
634     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
635       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
636       .access = PL1_RW, .accessfn = access_tvm_trvm,
637       .fgt = FGT_CONTEXTIDR_EL1,
638       .secure = ARM_CP_SECSTATE_NS,
639       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
640       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
641     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
642       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
643       .access = PL1_RW, .accessfn = access_tvm_trvm,
644       .secure = ARM_CP_SECSTATE_S,
645       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
646       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
647 };
648 
649 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
650     /*
651      * NB: Some of these registers exist in v8 but with more precise
652      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
653      */
654     /* MMU Domain access control / MPU write buffer control */
655     { .name = "DACR",
656       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
657       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
658       .writefn = dacr_write, .raw_writefn = raw_write,
659       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
660                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
661     /*
662      * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
663      * For v6 and v5, these mappings are overly broad.
664      */
665     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
666       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
667     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
668       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
669     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
670       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
671     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
672       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
673     /* Cache maintenance ops; some of this space may be overridden later. */
674     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
675       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
676       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
677 };
678 
679 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
680     /*
681      * Not all pre-v6 cores implemented this WFI, so this is slightly
682      * over-broad.
683      */
684     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
685       .access = PL1_W, .type = ARM_CP_WFI },
686 };
687 
688 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
689     /*
690      * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
691      * is UNPREDICTABLE; we choose to NOP as most implementations do).
692      */
693     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
694       .access = PL1_W, .type = ARM_CP_WFI },
695     /*
696      * L1 cache lockdown. Not architectural in v6 and earlier but in practice
697      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
698      * OMAPCP will override this space.
699      */
700     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
701       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
702       .resetvalue = 0 },
703     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
704       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
705       .resetvalue = 0 },
706     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
707     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
708       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
709       .resetvalue = 0 },
710     /*
711      * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
712      * implementing it as RAZ means the "debug architecture version" bits
713      * will read as a reserved value, which should cause Linux to not try
714      * to use the debug hardware.
715      */
716     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
717       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
718     /*
719      * MMU TLB control. Note that the wildcarding means we cover not just
720      * the unified TLB ops but also the dside/iside/inner-shareable variants.
721      */
722     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
723       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
724       .type = ARM_CP_NO_RAW },
725     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
726       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
727       .type = ARM_CP_NO_RAW },
728     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
729       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
730       .type = ARM_CP_NO_RAW },
731     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
732       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
733       .type = ARM_CP_NO_RAW },
734     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
735       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
736     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
737       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
738 };
739 
740 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
741                         uint64_t value)
742 {
743     uint32_t mask = 0;
744 
745     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
746     if (!arm_feature(env, ARM_FEATURE_V8)) {
747         /*
748          * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
749          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
750          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
751          */
752         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
753             /* VFP coprocessor: cp10 & cp11 [23:20] */
754             mask |= R_CPACR_ASEDIS_MASK |
755                     R_CPACR_D32DIS_MASK |
756                     R_CPACR_CP11_MASK |
757                     R_CPACR_CP10_MASK;
758 
759             if (!arm_feature(env, ARM_FEATURE_NEON)) {
760                 /* ASEDIS [31] bit is RAO/WI */
761                 value |= R_CPACR_ASEDIS_MASK;
762             }
763 
764             /*
765              * VFPv3 and upwards with NEON implement 32 double precision
766              * registers (D0-D31).
767              */
768             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
769                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
770                 value |= R_CPACR_D32DIS_MASK;
771             }
772         }
773         value &= mask;
774     }
775 
776     /*
777      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
778      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
779      */
780     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
781         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
782         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
783         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
784     }
785 
786     env->cp15.cpacr_el1 = value;
787 }
788 
789 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
790 {
791     /*
792      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
793      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
794      */
795     uint64_t value = env->cp15.cpacr_el1;
796 
797     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
798         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
799         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
800     }
801     return value;
802 }
803 
804 
805 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
806 {
807     /*
808      * Call cpacr_write() so that we reset with the correct RAO bits set
809      * for our CPU features.
810      */
811     cpacr_write(env, ri, 0);
812 }
813 
814 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
815                                    bool isread)
816 {
817     if (arm_feature(env, ARM_FEATURE_V8)) {
818         /* Check if CPACR accesses are to be trapped to EL2 */
819         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
820             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
821             return CP_ACCESS_TRAP_EL2;
822         /* Check if CPACR accesses are to be trapped to EL3 */
823         } else if (arm_current_el(env) < 3 &&
824                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
825             return CP_ACCESS_TRAP_EL3;
826         }
827     }
828 
829     return CP_ACCESS_OK;
830 }
831 
832 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
833                                   bool isread)
834 {
835     /* Check if CPTR accesses are set to trap to EL3 */
836     if (arm_current_el(env) == 2 &&
837         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
838         return CP_ACCESS_TRAP_EL3;
839     }
840 
841     return CP_ACCESS_OK;
842 }
843 
844 static const ARMCPRegInfo v6_cp_reginfo[] = {
845     /* prefetch by MVA in v6, NOP in v7 */
846     { .name = "MVA_prefetch",
847       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
848       .access = PL1_W, .type = ARM_CP_NOP },
849     /*
850      * We need to break the TB after ISB to execute self-modifying code
851      * correctly and also to take any pending interrupts immediately.
852      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
853      */
854     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
855       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
856     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
857       .access = PL0_W, .type = ARM_CP_NOP },
858     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
859       .access = PL0_W, .type = ARM_CP_NOP },
860     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
861       .access = PL1_RW, .accessfn = access_tvm_trvm,
862       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
863                              offsetof(CPUARMState, cp15.ifar_ns) },
864       .resetvalue = 0, },
865     /*
866      * Watchpoint Fault Address Register : should actually only be present
867      * for 1136, 1176, 11MPCore.
868      */
869     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
870       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
871     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
872       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
873       .fgt = FGT_CPACR_EL1,
874       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
875       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
876 };
877 
878 typedef struct pm_event {
879     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
880     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
881     bool (*supported)(CPUARMState *);
882     /*
883      * Retrieve the current count of the underlying event. The programmed
884      * counters hold a difference from the return value from this function
885      */
886     uint64_t (*get_count)(CPUARMState *);
887     /*
888      * Return how many nanoseconds it will take (at a minimum) for count events
889      * to occur. A negative value indicates the counter will never overflow, or
890      * that the counter has otherwise arranged for the overflow bit to be set
891      * and the PMU interrupt to be raised on overflow.
892      */
893     int64_t (*ns_per_count)(uint64_t);
894 } pm_event;
895 
896 static bool event_always_supported(CPUARMState *env)
897 {
898     return true;
899 }
900 
901 static uint64_t swinc_get_count(CPUARMState *env)
902 {
903     /*
904      * SW_INCR events are written directly to the pmevcntr's by writes to
905      * PMSWINC, so there is no underlying count maintained by the PMU itself
906      */
907     return 0;
908 }
909 
910 static int64_t swinc_ns_per(uint64_t ignored)
911 {
912     return -1;
913 }
914 
915 /*
916  * Return the underlying cycle count for the PMU cycle counters. If we're in
917  * usermode, simply return 0.
918  */
919 static uint64_t cycles_get_count(CPUARMState *env)
920 {
921 #ifndef CONFIG_USER_ONLY
922     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
923                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
924 #else
925     return cpu_get_host_ticks();
926 #endif
927 }
928 
929 #ifndef CONFIG_USER_ONLY
930 static int64_t cycles_ns_per(uint64_t cycles)
931 {
932     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
933 }
934 
935 static bool instructions_supported(CPUARMState *env)
936 {
937     return icount_enabled() == 1; /* Precise instruction counting */
938 }
939 
940 static uint64_t instructions_get_count(CPUARMState *env)
941 {
942     return (uint64_t)icount_get_raw();
943 }
944 
945 static int64_t instructions_ns_per(uint64_t icount)
946 {
947     return icount_to_ns((int64_t)icount);
948 }
949 #endif
950 
951 static bool pmuv3p1_events_supported(CPUARMState *env)
952 {
953     /* For events which are supported in any v8.1 PMU */
954     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
955 }
956 
957 static bool pmuv3p4_events_supported(CPUARMState *env)
958 {
959     /* For events which are supported in any v8.1 PMU */
960     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
961 }
962 
963 static uint64_t zero_event_get_count(CPUARMState *env)
964 {
965     /* For events which on QEMU never fire, so their count is always zero */
966     return 0;
967 }
968 
969 static int64_t zero_event_ns_per(uint64_t cycles)
970 {
971     /* An event which never fires can never overflow */
972     return -1;
973 }
974 
975 static const pm_event pm_events[] = {
976     { .number = 0x000, /* SW_INCR */
977       .supported = event_always_supported,
978       .get_count = swinc_get_count,
979       .ns_per_count = swinc_ns_per,
980     },
981 #ifndef CONFIG_USER_ONLY
982     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
983       .supported = instructions_supported,
984       .get_count = instructions_get_count,
985       .ns_per_count = instructions_ns_per,
986     },
987     { .number = 0x011, /* CPU_CYCLES, Cycle */
988       .supported = event_always_supported,
989       .get_count = cycles_get_count,
990       .ns_per_count = cycles_ns_per,
991     },
992 #endif
993     { .number = 0x023, /* STALL_FRONTEND */
994       .supported = pmuv3p1_events_supported,
995       .get_count = zero_event_get_count,
996       .ns_per_count = zero_event_ns_per,
997     },
998     { .number = 0x024, /* STALL_BACKEND */
999       .supported = pmuv3p1_events_supported,
1000       .get_count = zero_event_get_count,
1001       .ns_per_count = zero_event_ns_per,
1002     },
1003     { .number = 0x03c, /* STALL */
1004       .supported = pmuv3p4_events_supported,
1005       .get_count = zero_event_get_count,
1006       .ns_per_count = zero_event_ns_per,
1007     },
1008 };
1009 
1010 /*
1011  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1012  * events (i.e. the statistical profiling extension), this implementation
1013  * should first be updated to something sparse instead of the current
1014  * supported_event_map[] array.
1015  */
1016 #define MAX_EVENT_ID 0x3c
1017 #define UNSUPPORTED_EVENT UINT16_MAX
1018 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1019 
1020 /*
1021  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1022  * of ARM event numbers to indices in our pm_events array.
1023  *
1024  * Note: Events in the 0x40XX range are not currently supported.
1025  */
1026 void pmu_init(ARMCPU *cpu)
1027 {
1028     unsigned int i;
1029 
1030     /*
1031      * Empty supported_event_map and cpu->pmceid[01] before adding supported
1032      * events to them
1033      */
1034     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1035         supported_event_map[i] = UNSUPPORTED_EVENT;
1036     }
1037     cpu->pmceid0 = 0;
1038     cpu->pmceid1 = 0;
1039 
1040     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1041         const pm_event *cnt = &pm_events[i];
1042         assert(cnt->number <= MAX_EVENT_ID);
1043         /* We do not currently support events in the 0x40xx range */
1044         assert(cnt->number <= 0x3f);
1045 
1046         if (cnt->supported(&cpu->env)) {
1047             supported_event_map[cnt->number] = i;
1048             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1049             if (cnt->number & 0x20) {
1050                 cpu->pmceid1 |= event_mask;
1051             } else {
1052                 cpu->pmceid0 |= event_mask;
1053             }
1054         }
1055     }
1056 }
1057 
1058 /*
1059  * Check at runtime whether a PMU event is supported for the current machine
1060  */
1061 static bool event_supported(uint16_t number)
1062 {
1063     if (number > MAX_EVENT_ID) {
1064         return false;
1065     }
1066     return supported_event_map[number] != UNSUPPORTED_EVENT;
1067 }
1068 
1069 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1070                                    bool isread)
1071 {
1072     /*
1073      * Performance monitor registers user accessibility is controlled
1074      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1075      * trapping to EL2 or EL3 for other accesses.
1076      */
1077     int el = arm_current_el(env);
1078     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1079 
1080     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1081         return CP_ACCESS_TRAP;
1082     }
1083     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1084         return CP_ACCESS_TRAP_EL2;
1085     }
1086     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1087         return CP_ACCESS_TRAP_EL3;
1088     }
1089 
1090     return CP_ACCESS_OK;
1091 }
1092 
1093 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1094                                            const ARMCPRegInfo *ri,
1095                                            bool isread)
1096 {
1097     /* ER: event counter read trap control */
1098     if (arm_feature(env, ARM_FEATURE_V8)
1099         && arm_current_el(env) == 0
1100         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1101         && isread) {
1102         return CP_ACCESS_OK;
1103     }
1104 
1105     return pmreg_access(env, ri, isread);
1106 }
1107 
1108 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1109                                          const ARMCPRegInfo *ri,
1110                                          bool isread)
1111 {
1112     /* SW: software increment write trap control */
1113     if (arm_feature(env, ARM_FEATURE_V8)
1114         && arm_current_el(env) == 0
1115         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1116         && !isread) {
1117         return CP_ACCESS_OK;
1118     }
1119 
1120     return pmreg_access(env, ri, isread);
1121 }
1122 
1123 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1124                                         const ARMCPRegInfo *ri,
1125                                         bool isread)
1126 {
1127     /* ER: event counter read trap control */
1128     if (arm_feature(env, ARM_FEATURE_V8)
1129         && arm_current_el(env) == 0
1130         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1131         return CP_ACCESS_OK;
1132     }
1133 
1134     return pmreg_access(env, ri, isread);
1135 }
1136 
1137 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1138                                          const ARMCPRegInfo *ri,
1139                                          bool isread)
1140 {
1141     /* CR: cycle counter read trap control */
1142     if (arm_feature(env, ARM_FEATURE_V8)
1143         && arm_current_el(env) == 0
1144         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1145         && isread) {
1146         return CP_ACCESS_OK;
1147     }
1148 
1149     return pmreg_access(env, ri, isread);
1150 }
1151 
1152 /*
1153  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1154  * We use these to decide whether we need to wrap a write to MDCR_EL2
1155  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1156  */
1157 #define MDCR_EL2_PMU_ENABLE_BITS \
1158     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1159 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1160 
1161 /*
1162  * Returns true if the counter (pass 31 for PMCCNTR) should count events using
1163  * the current EL, security state, and register configuration.
1164  */
1165 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1166 {
1167     uint64_t filter;
1168     bool e, p, u, nsk, nsu, nsh, m;
1169     bool enabled, prohibited = false, filtered;
1170     bool secure = arm_is_secure(env);
1171     int el = arm_current_el(env);
1172     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1173     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1174 
1175     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1176         return false;
1177     }
1178 
1179     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1180             (counter < hpmn || counter == 31)) {
1181         e = env->cp15.c9_pmcr & PMCRE;
1182     } else {
1183         e = mdcr_el2 & MDCR_HPME;
1184     }
1185     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1186 
1187     /* Is event counting prohibited? */
1188     if (el == 2 && (counter < hpmn || counter == 31)) {
1189         prohibited = mdcr_el2 & MDCR_HPMD;
1190     }
1191     if (secure) {
1192         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1193     }
1194 
1195     if (counter == 31) {
1196         /*
1197          * The cycle counter defaults to running. PMCR.DP says "disable
1198          * the cycle counter when event counting is prohibited".
1199          * Some MDCR bits disable the cycle counter specifically.
1200          */
1201         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1202         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1203             if (secure) {
1204                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1205             }
1206             if (el == 2) {
1207                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1208             }
1209         }
1210     }
1211 
1212     if (counter == 31) {
1213         filter = env->cp15.pmccfiltr_el0;
1214     } else {
1215         filter = env->cp15.c14_pmevtyper[counter];
1216     }
1217 
1218     p   = filter & PMXEVTYPER_P;
1219     u   = filter & PMXEVTYPER_U;
1220     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1221     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1222     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1223     m   = arm_el_is_aa64(env, 1) &&
1224               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1225 
1226     if (el == 0) {
1227         filtered = secure ? u : u != nsu;
1228     } else if (el == 1) {
1229         filtered = secure ? p : p != nsk;
1230     } else if (el == 2) {
1231         filtered = !nsh;
1232     } else { /* EL3 */
1233         filtered = m != p;
1234     }
1235 
1236     if (counter != 31) {
1237         /*
1238          * If not checking PMCCNTR, ensure the counter is setup to an event we
1239          * support
1240          */
1241         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1242         if (!event_supported(event)) {
1243             return false;
1244         }
1245     }
1246 
1247     return enabled && !prohibited && !filtered;
1248 }
1249 
1250 static void pmu_update_irq(CPUARMState *env)
1251 {
1252     ARMCPU *cpu = env_archcpu(env);
1253     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1254             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1255 }
1256 
1257 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1258 {
1259     /*
1260      * Return true if the clock divider is enabled and the cycle counter
1261      * is supposed to tick only once every 64 clock cycles. This is
1262      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1263      * (64-bit) cycle counter PMCR.D has no effect.
1264      */
1265     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1266 }
1267 
1268 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1269 {
1270     /* Return true if the specified event counter is configured to be 64 bit */
1271 
1272     /* This isn't intended to be used with the cycle counter */
1273     assert(counter < 31);
1274 
1275     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1276         return false;
1277     }
1278 
1279     if (arm_feature(env, ARM_FEATURE_EL2)) {
1280         /*
1281          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1282          * current security state, so we don't use arm_mdcr_el2_eff() here.
1283          */
1284         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1285         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1286 
1287         if (counter >= hpmn) {
1288             return hlp;
1289         }
1290     }
1291     return env->cp15.c9_pmcr & PMCRLP;
1292 }
1293 
1294 /*
1295  * Ensure c15_ccnt is the guest-visible count so that operations such as
1296  * enabling/disabling the counter or filtering, modifying the count itself,
1297  * etc. can be done logically. This is essentially a no-op if the counter is
1298  * not enabled at the time of the call.
1299  */
1300 static void pmccntr_op_start(CPUARMState *env)
1301 {
1302     uint64_t cycles = cycles_get_count(env);
1303 
1304     if (pmu_counter_enabled(env, 31)) {
1305         uint64_t eff_cycles = cycles;
1306         if (pmccntr_clockdiv_enabled(env)) {
1307             eff_cycles /= 64;
1308         }
1309 
1310         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1311 
1312         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1313                                  1ull << 63 : 1ull << 31;
1314         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1315             env->cp15.c9_pmovsr |= (1ULL << 31);
1316             pmu_update_irq(env);
1317         }
1318 
1319         env->cp15.c15_ccnt = new_pmccntr;
1320     }
1321     env->cp15.c15_ccnt_delta = cycles;
1322 }
1323 
1324 /*
1325  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1326  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1327  * pmccntr_op_start.
1328  */
1329 static void pmccntr_op_finish(CPUARMState *env)
1330 {
1331     if (pmu_counter_enabled(env, 31)) {
1332 #ifndef CONFIG_USER_ONLY
1333         /* Calculate when the counter will next overflow */
1334         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1335         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1336             remaining_cycles = (uint32_t)remaining_cycles;
1337         }
1338         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1339 
1340         if (overflow_in > 0) {
1341             int64_t overflow_at;
1342 
1343             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1344                                  overflow_in, &overflow_at)) {
1345                 ARMCPU *cpu = env_archcpu(env);
1346                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1347             }
1348         }
1349 #endif
1350 
1351         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1352         if (pmccntr_clockdiv_enabled(env)) {
1353             prev_cycles /= 64;
1354         }
1355         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1356     }
1357 }
1358 
1359 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1360 {
1361 
1362     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1363     uint64_t count = 0;
1364     if (event_supported(event)) {
1365         uint16_t event_idx = supported_event_map[event];
1366         count = pm_events[event_idx].get_count(env);
1367     }
1368 
1369     if (pmu_counter_enabled(env, counter)) {
1370         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1371         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1372             1ULL << 63 : 1ULL << 31;
1373 
1374         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1375             env->cp15.c9_pmovsr |= (1 << counter);
1376             pmu_update_irq(env);
1377         }
1378         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1379     }
1380     env->cp15.c14_pmevcntr_delta[counter] = count;
1381 }
1382 
1383 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1384 {
1385     if (pmu_counter_enabled(env, counter)) {
1386 #ifndef CONFIG_USER_ONLY
1387         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1388         uint16_t event_idx = supported_event_map[event];
1389         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1390         int64_t overflow_in;
1391 
1392         if (!pmevcntr_is_64_bit(env, counter)) {
1393             delta = (uint32_t)delta;
1394         }
1395         overflow_in = pm_events[event_idx].ns_per_count(delta);
1396 
1397         if (overflow_in > 0) {
1398             int64_t overflow_at;
1399 
1400             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1401                                  overflow_in, &overflow_at)) {
1402                 ARMCPU *cpu = env_archcpu(env);
1403                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1404             }
1405         }
1406 #endif
1407 
1408         env->cp15.c14_pmevcntr_delta[counter] -=
1409             env->cp15.c14_pmevcntr[counter];
1410     }
1411 }
1412 
1413 void pmu_op_start(CPUARMState *env)
1414 {
1415     unsigned int i;
1416     pmccntr_op_start(env);
1417     for (i = 0; i < pmu_num_counters(env); i++) {
1418         pmevcntr_op_start(env, i);
1419     }
1420 }
1421 
1422 void pmu_op_finish(CPUARMState *env)
1423 {
1424     unsigned int i;
1425     pmccntr_op_finish(env);
1426     for (i = 0; i < pmu_num_counters(env); i++) {
1427         pmevcntr_op_finish(env, i);
1428     }
1429 }
1430 
1431 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1432 {
1433     pmu_op_start(&cpu->env);
1434 }
1435 
1436 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1437 {
1438     pmu_op_finish(&cpu->env);
1439 }
1440 
1441 void arm_pmu_timer_cb(void *opaque)
1442 {
1443     ARMCPU *cpu = opaque;
1444 
1445     /*
1446      * Update all the counter values based on the current underlying counts,
1447      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1448      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1449      * counter may expire.
1450      */
1451     pmu_op_start(&cpu->env);
1452     pmu_op_finish(&cpu->env);
1453 }
1454 
1455 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1456                        uint64_t value)
1457 {
1458     pmu_op_start(env);
1459 
1460     if (value & PMCRC) {
1461         /* The counter has been reset */
1462         env->cp15.c15_ccnt = 0;
1463     }
1464 
1465     if (value & PMCRP) {
1466         unsigned int i;
1467         for (i = 0; i < pmu_num_counters(env); i++) {
1468             env->cp15.c14_pmevcntr[i] = 0;
1469         }
1470     }
1471 
1472     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1473     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1474 
1475     pmu_op_finish(env);
1476 }
1477 
1478 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1479                           uint64_t value)
1480 {
1481     unsigned int i;
1482     uint64_t overflow_mask, new_pmswinc;
1483 
1484     for (i = 0; i < pmu_num_counters(env); i++) {
1485         /* Increment a counter's count iff: */
1486         if ((value & (1 << i)) && /* counter's bit is set */
1487                 /* counter is enabled and not filtered */
1488                 pmu_counter_enabled(env, i) &&
1489                 /* counter is SW_INCR */
1490                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1491             pmevcntr_op_start(env, i);
1492 
1493             /*
1494              * Detect if this write causes an overflow since we can't predict
1495              * PMSWINC overflows like we can for other events
1496              */
1497             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1498 
1499             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1500                 1ULL << 63 : 1ULL << 31;
1501 
1502             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1503                 env->cp15.c9_pmovsr |= (1 << i);
1504                 pmu_update_irq(env);
1505             }
1506 
1507             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1508 
1509             pmevcntr_op_finish(env, i);
1510         }
1511     }
1512 }
1513 
1514 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1515 {
1516     uint64_t ret;
1517     pmccntr_op_start(env);
1518     ret = env->cp15.c15_ccnt;
1519     pmccntr_op_finish(env);
1520     return ret;
1521 }
1522 
1523 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1524                          uint64_t value)
1525 {
1526     /*
1527      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1528      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1529      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1530      * accessed.
1531      */
1532     env->cp15.c9_pmselr = value & 0x1f;
1533 }
1534 
1535 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1536                         uint64_t value)
1537 {
1538     pmccntr_op_start(env);
1539     env->cp15.c15_ccnt = value;
1540     pmccntr_op_finish(env);
1541 }
1542 
1543 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1544                             uint64_t value)
1545 {
1546     uint64_t cur_val = pmccntr_read(env, NULL);
1547 
1548     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1549 }
1550 
1551 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1552                             uint64_t value)
1553 {
1554     pmccntr_op_start(env);
1555     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1556     pmccntr_op_finish(env);
1557 }
1558 
1559 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1560                             uint64_t value)
1561 {
1562     pmccntr_op_start(env);
1563     /* M is not accessible from AArch32 */
1564     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1565         (value & PMCCFILTR);
1566     pmccntr_op_finish(env);
1567 }
1568 
1569 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1570 {
1571     /* M is not visible in AArch32 */
1572     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1573 }
1574 
1575 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1576                             uint64_t value)
1577 {
1578     pmu_op_start(env);
1579     value &= pmu_counter_mask(env);
1580     env->cp15.c9_pmcnten |= value;
1581     pmu_op_finish(env);
1582 }
1583 
1584 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1585                              uint64_t value)
1586 {
1587     pmu_op_start(env);
1588     value &= pmu_counter_mask(env);
1589     env->cp15.c9_pmcnten &= ~value;
1590     pmu_op_finish(env);
1591 }
1592 
1593 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1594                          uint64_t value)
1595 {
1596     value &= pmu_counter_mask(env);
1597     env->cp15.c9_pmovsr &= ~value;
1598     pmu_update_irq(env);
1599 }
1600 
1601 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1602                          uint64_t value)
1603 {
1604     value &= pmu_counter_mask(env);
1605     env->cp15.c9_pmovsr |= value;
1606     pmu_update_irq(env);
1607 }
1608 
1609 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1610                              uint64_t value, const uint8_t counter)
1611 {
1612     if (counter == 31) {
1613         pmccfiltr_write(env, ri, value);
1614     } else if (counter < pmu_num_counters(env)) {
1615         pmevcntr_op_start(env, counter);
1616 
1617         /*
1618          * If this counter's event type is changing, store the current
1619          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1620          * pmevcntr_op_finish has the correct baseline when it converts back to
1621          * a delta.
1622          */
1623         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1624             PMXEVTYPER_EVTCOUNT;
1625         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1626         if (old_event != new_event) {
1627             uint64_t count = 0;
1628             if (event_supported(new_event)) {
1629                 uint16_t event_idx = supported_event_map[new_event];
1630                 count = pm_events[event_idx].get_count(env);
1631             }
1632             env->cp15.c14_pmevcntr_delta[counter] = count;
1633         }
1634 
1635         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1636         pmevcntr_op_finish(env, counter);
1637     }
1638     /*
1639      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1640      * PMSELR value is equal to or greater than the number of implemented
1641      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1642      */
1643 }
1644 
1645 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1646                                const uint8_t counter)
1647 {
1648     if (counter == 31) {
1649         return env->cp15.pmccfiltr_el0;
1650     } else if (counter < pmu_num_counters(env)) {
1651         return env->cp15.c14_pmevtyper[counter];
1652     } else {
1653       /*
1654        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1655        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1656        */
1657         return 0;
1658     }
1659 }
1660 
1661 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1662                               uint64_t value)
1663 {
1664     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1665     pmevtyper_write(env, ri, value, counter);
1666 }
1667 
1668 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1669                                uint64_t value)
1670 {
1671     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1672     env->cp15.c14_pmevtyper[counter] = value;
1673 
1674     /*
1675      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1676      * pmu_op_finish calls when loading saved state for a migration. Because
1677      * we're potentially updating the type of event here, the value written to
1678      * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1679      * different counter type. Therefore, we need to set this value to the
1680      * current count for the counter type we're writing so that pmu_op_finish
1681      * has the correct count for its calculation.
1682      */
1683     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1684     if (event_supported(event)) {
1685         uint16_t event_idx = supported_event_map[event];
1686         env->cp15.c14_pmevcntr_delta[counter] =
1687             pm_events[event_idx].get_count(env);
1688     }
1689 }
1690 
1691 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1692 {
1693     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1694     return pmevtyper_read(env, ri, counter);
1695 }
1696 
1697 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1698                              uint64_t value)
1699 {
1700     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1701 }
1702 
1703 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1704 {
1705     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1706 }
1707 
1708 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1709                              uint64_t value, uint8_t counter)
1710 {
1711     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1712         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1713         value &= MAKE_64BIT_MASK(0, 32);
1714     }
1715     if (counter < pmu_num_counters(env)) {
1716         pmevcntr_op_start(env, counter);
1717         env->cp15.c14_pmevcntr[counter] = value;
1718         pmevcntr_op_finish(env, counter);
1719     }
1720     /*
1721      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1722      * are CONSTRAINED UNPREDICTABLE.
1723      */
1724 }
1725 
1726 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1727                               uint8_t counter)
1728 {
1729     if (counter < pmu_num_counters(env)) {
1730         uint64_t ret;
1731         pmevcntr_op_start(env, counter);
1732         ret = env->cp15.c14_pmevcntr[counter];
1733         pmevcntr_op_finish(env, counter);
1734         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1735             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1736             ret &= MAKE_64BIT_MASK(0, 32);
1737         }
1738         return ret;
1739     } else {
1740       /*
1741        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1742        * are CONSTRAINED UNPREDICTABLE.
1743        */
1744         return 0;
1745     }
1746 }
1747 
1748 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1749                              uint64_t value)
1750 {
1751     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1752     pmevcntr_write(env, ri, value, counter);
1753 }
1754 
1755 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1756 {
1757     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1758     return pmevcntr_read(env, ri, counter);
1759 }
1760 
1761 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1762                              uint64_t value)
1763 {
1764     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1765     assert(counter < pmu_num_counters(env));
1766     env->cp15.c14_pmevcntr[counter] = value;
1767     pmevcntr_write(env, ri, value, counter);
1768 }
1769 
1770 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1771 {
1772     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1773     assert(counter < pmu_num_counters(env));
1774     return env->cp15.c14_pmevcntr[counter];
1775 }
1776 
1777 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1778                              uint64_t value)
1779 {
1780     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1781 }
1782 
1783 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1784 {
1785     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1786 }
1787 
1788 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1789                             uint64_t value)
1790 {
1791     if (arm_feature(env, ARM_FEATURE_V8)) {
1792         env->cp15.c9_pmuserenr = value & 0xf;
1793     } else {
1794         env->cp15.c9_pmuserenr = value & 1;
1795     }
1796 }
1797 
1798 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1799                              uint64_t value)
1800 {
1801     /* We have no event counters so only the C bit can be changed */
1802     value &= pmu_counter_mask(env);
1803     env->cp15.c9_pminten |= value;
1804     pmu_update_irq(env);
1805 }
1806 
1807 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1808                              uint64_t value)
1809 {
1810     value &= pmu_counter_mask(env);
1811     env->cp15.c9_pminten &= ~value;
1812     pmu_update_irq(env);
1813 }
1814 
1815 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1816                        uint64_t value)
1817 {
1818     /*
1819      * Note that even though the AArch64 view of this register has bits
1820      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1821      * architectural requirements for bits which are RES0 only in some
1822      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1823      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1824      */
1825     raw_write(env, ri, value & ~0x1FULL);
1826 }
1827 
1828 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1829 {
1830     /* Begin with base v8.0 state.  */
1831     uint64_t valid_mask = 0x3fff;
1832     ARMCPU *cpu = env_archcpu(env);
1833     uint64_t changed;
1834 
1835     /*
1836      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1837      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1838      * Instead, choose the format based on the mode of EL3.
1839      */
1840     if (arm_el_is_aa64(env, 3)) {
1841         value |= SCR_FW | SCR_AW;      /* RES1 */
1842         valid_mask &= ~SCR_NET;        /* RES0 */
1843 
1844         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1845             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1846             value |= SCR_RW;           /* RAO/WI */
1847         }
1848         if (cpu_isar_feature(aa64_ras, cpu)) {
1849             valid_mask |= SCR_TERR;
1850         }
1851         if (cpu_isar_feature(aa64_lor, cpu)) {
1852             valid_mask |= SCR_TLOR;
1853         }
1854         if (cpu_isar_feature(aa64_pauth, cpu)) {
1855             valid_mask |= SCR_API | SCR_APK;
1856         }
1857         if (cpu_isar_feature(aa64_sel2, cpu)) {
1858             valid_mask |= SCR_EEL2;
1859         } else if (cpu_isar_feature(aa64_rme, cpu)) {
1860             /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1861             value |= SCR_NS;
1862         }
1863         if (cpu_isar_feature(aa64_mte, cpu)) {
1864             valid_mask |= SCR_ATA;
1865         }
1866         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1867             valid_mask |= SCR_ENSCXT;
1868         }
1869         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1870             valid_mask |= SCR_EASE | SCR_NMEA;
1871         }
1872         if (cpu_isar_feature(aa64_sme, cpu)) {
1873             valid_mask |= SCR_ENTP2;
1874         }
1875         if (cpu_isar_feature(aa64_hcx, cpu)) {
1876             valid_mask |= SCR_HXEN;
1877         }
1878         if (cpu_isar_feature(aa64_fgt, cpu)) {
1879             valid_mask |= SCR_FGTEN;
1880         }
1881         if (cpu_isar_feature(aa64_rme, cpu)) {
1882             valid_mask |= SCR_NSE | SCR_GPF;
1883         }
1884     } else {
1885         valid_mask &= ~(SCR_RW | SCR_ST);
1886         if (cpu_isar_feature(aa32_ras, cpu)) {
1887             valid_mask |= SCR_TERR;
1888         }
1889     }
1890 
1891     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1892         valid_mask &= ~SCR_HCE;
1893 
1894         /*
1895          * On ARMv7, SMD (or SCD as it is called in v7) is only
1896          * supported if EL2 exists. The bit is UNK/SBZP when
1897          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1898          * when EL2 is unavailable.
1899          * On ARMv8, this bit is always available.
1900          */
1901         if (arm_feature(env, ARM_FEATURE_V7) &&
1902             !arm_feature(env, ARM_FEATURE_V8)) {
1903             valid_mask &= ~SCR_SMD;
1904         }
1905     }
1906 
1907     /* Clear all-context RES0 bits.  */
1908     value &= valid_mask;
1909     changed = env->cp15.scr_el3 ^ value;
1910     env->cp15.scr_el3 = value;
1911 
1912     /*
1913      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1914      * we must invalidate all TLBs below EL3.
1915      */
1916     if (changed & (SCR_NS | SCR_NSE)) {
1917         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1918                                            ARMMMUIdxBit_E20_0 |
1919                                            ARMMMUIdxBit_E10_1 |
1920                                            ARMMMUIdxBit_E20_2 |
1921                                            ARMMMUIdxBit_E10_1_PAN |
1922                                            ARMMMUIdxBit_E20_2_PAN |
1923                                            ARMMMUIdxBit_E2));
1924     }
1925 }
1926 
1927 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1928 {
1929     /*
1930      * scr_write will set the RES1 bits on an AArch64-only CPU.
1931      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1932      */
1933     scr_write(env, ri, 0);
1934 }
1935 
1936 static CPAccessResult access_tid4(CPUARMState *env,
1937                                   const ARMCPRegInfo *ri,
1938                                   bool isread)
1939 {
1940     if (arm_current_el(env) == 1 &&
1941         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1942         return CP_ACCESS_TRAP_EL2;
1943     }
1944 
1945     return CP_ACCESS_OK;
1946 }
1947 
1948 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1949 {
1950     ARMCPU *cpu = env_archcpu(env);
1951 
1952     /*
1953      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1954      * bank
1955      */
1956     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1957                                         ri->secure & ARM_CP_SECSTATE_S);
1958 
1959     return cpu->ccsidr[index];
1960 }
1961 
1962 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1963                          uint64_t value)
1964 {
1965     raw_write(env, ri, value & 0xf);
1966 }
1967 
1968 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1969 {
1970     CPUState *cs = env_cpu(env);
1971     bool el1 = arm_current_el(env) == 1;
1972     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1973     uint64_t ret = 0;
1974 
1975     if (hcr_el2 & HCR_IMO) {
1976         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1977             ret |= CPSR_I;
1978         }
1979     } else {
1980         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1981             ret |= CPSR_I;
1982         }
1983     }
1984 
1985     if (hcr_el2 & HCR_FMO) {
1986         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1987             ret |= CPSR_F;
1988         }
1989     } else {
1990         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1991             ret |= CPSR_F;
1992         }
1993     }
1994 
1995     if (hcr_el2 & HCR_AMO) {
1996         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1997             ret |= CPSR_A;
1998         }
1999     }
2000 
2001     return ret;
2002 }
2003 
2004 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2005                                        bool isread)
2006 {
2007     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2008         return CP_ACCESS_TRAP_EL2;
2009     }
2010 
2011     return CP_ACCESS_OK;
2012 }
2013 
2014 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2015                                        bool isread)
2016 {
2017     if (arm_feature(env, ARM_FEATURE_V8)) {
2018         return access_aa64_tid1(env, ri, isread);
2019     }
2020 
2021     return CP_ACCESS_OK;
2022 }
2023 
2024 static const ARMCPRegInfo v7_cp_reginfo[] = {
2025     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2026     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2027       .access = PL1_W, .type = ARM_CP_NOP },
2028     /*
2029      * Performance monitors are implementation defined in v7,
2030      * but with an ARM recommended set of registers, which we
2031      * follow.
2032      *
2033      * Performance registers fall into three categories:
2034      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2035      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2036      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2037      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2038      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2039      */
2040     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2041       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2042       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2043       .writefn = pmcntenset_write,
2044       .accessfn = pmreg_access,
2045       .fgt = FGT_PMCNTEN,
2046       .raw_writefn = raw_write },
2047     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2048       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2049       .access = PL0_RW, .accessfn = pmreg_access,
2050       .fgt = FGT_PMCNTEN,
2051       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2052       .writefn = pmcntenset_write, .raw_writefn = raw_write },
2053     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2054       .access = PL0_RW,
2055       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2056       .accessfn = pmreg_access,
2057       .fgt = FGT_PMCNTEN,
2058       .writefn = pmcntenclr_write,
2059       .type = ARM_CP_ALIAS | ARM_CP_IO },
2060     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2061       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2062       .access = PL0_RW, .accessfn = pmreg_access,
2063       .fgt = FGT_PMCNTEN,
2064       .type = ARM_CP_ALIAS | ARM_CP_IO,
2065       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2066       .writefn = pmcntenclr_write },
2067     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2068       .access = PL0_RW, .type = ARM_CP_IO,
2069       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2070       .accessfn = pmreg_access,
2071       .fgt = FGT_PMOVS,
2072       .writefn = pmovsr_write,
2073       .raw_writefn = raw_write },
2074     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2075       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2076       .access = PL0_RW, .accessfn = pmreg_access,
2077       .fgt = FGT_PMOVS,
2078       .type = ARM_CP_ALIAS | ARM_CP_IO,
2079       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2080       .writefn = pmovsr_write,
2081       .raw_writefn = raw_write },
2082     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2083       .access = PL0_W, .accessfn = pmreg_access_swinc,
2084       .fgt = FGT_PMSWINC_EL0,
2085       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2086       .writefn = pmswinc_write },
2087     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2088       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2089       .access = PL0_W, .accessfn = pmreg_access_swinc,
2090       .fgt = FGT_PMSWINC_EL0,
2091       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2092       .writefn = pmswinc_write },
2093     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2094       .access = PL0_RW, .type = ARM_CP_ALIAS,
2095       .fgt = FGT_PMSELR_EL0,
2096       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2097       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2098       .raw_writefn = raw_write},
2099     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2100       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2101       .access = PL0_RW, .accessfn = pmreg_access_selr,
2102       .fgt = FGT_PMSELR_EL0,
2103       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2104       .writefn = pmselr_write, .raw_writefn = raw_write, },
2105     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2106       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2107       .fgt = FGT_PMCCNTR_EL0,
2108       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2109       .accessfn = pmreg_access_ccntr },
2110     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2111       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2112       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2113       .fgt = FGT_PMCCNTR_EL0,
2114       .type = ARM_CP_IO,
2115       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2116       .readfn = pmccntr_read, .writefn = pmccntr_write,
2117       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2118     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2119       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2120       .access = PL0_RW, .accessfn = pmreg_access,
2121       .fgt = FGT_PMCCFILTR_EL0,
2122       .type = ARM_CP_ALIAS | ARM_CP_IO,
2123       .resetvalue = 0, },
2124     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2125       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2126       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2127       .access = PL0_RW, .accessfn = pmreg_access,
2128       .fgt = FGT_PMCCFILTR_EL0,
2129       .type = ARM_CP_IO,
2130       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2131       .resetvalue = 0, },
2132     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2133       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2134       .accessfn = pmreg_access,
2135       .fgt = FGT_PMEVTYPERN_EL0,
2136       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2137     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2138       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2139       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2140       .accessfn = pmreg_access,
2141       .fgt = FGT_PMEVTYPERN_EL0,
2142       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2143     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2144       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2145       .accessfn = pmreg_access_xevcntr,
2146       .fgt = FGT_PMEVCNTRN_EL0,
2147       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2148     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2149       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2150       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2151       .accessfn = pmreg_access_xevcntr,
2152       .fgt = FGT_PMEVCNTRN_EL0,
2153       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2154     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2155       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2156       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2157       .resetvalue = 0,
2158       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2159     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2160       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2161       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2162       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2163       .resetvalue = 0,
2164       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2165     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2166       .access = PL1_RW, .accessfn = access_tpm,
2167       .fgt = FGT_PMINTEN,
2168       .type = ARM_CP_ALIAS | ARM_CP_IO,
2169       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2170       .resetvalue = 0,
2171       .writefn = pmintenset_write, .raw_writefn = raw_write },
2172     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2173       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2174       .access = PL1_RW, .accessfn = access_tpm,
2175       .fgt = FGT_PMINTEN,
2176       .type = ARM_CP_IO,
2177       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2178       .writefn = pmintenset_write, .raw_writefn = raw_write,
2179       .resetvalue = 0x0 },
2180     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2181       .access = PL1_RW, .accessfn = access_tpm,
2182       .fgt = FGT_PMINTEN,
2183       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2184       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2185       .writefn = pmintenclr_write, },
2186     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2187       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2188       .access = PL1_RW, .accessfn = access_tpm,
2189       .fgt = FGT_PMINTEN,
2190       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2191       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2192       .writefn = pmintenclr_write },
2193     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2194       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2195       .access = PL1_R,
2196       .accessfn = access_tid4,
2197       .fgt = FGT_CCSIDR_EL1,
2198       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2199     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2200       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2201       .access = PL1_RW,
2202       .accessfn = access_tid4,
2203       .fgt = FGT_CSSELR_EL1,
2204       .writefn = csselr_write, .resetvalue = 0,
2205       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2206                              offsetof(CPUARMState, cp15.csselr_ns) } },
2207     /*
2208      * Auxiliary ID register: this actually has an IMPDEF value but for now
2209      * just RAZ for all cores:
2210      */
2211     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2212       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2213       .access = PL1_R, .type = ARM_CP_CONST,
2214       .accessfn = access_aa64_tid1,
2215       .fgt = FGT_AIDR_EL1,
2216       .resetvalue = 0 },
2217     /*
2218      * Auxiliary fault status registers: these also are IMPDEF, and we
2219      * choose to RAZ/WI for all cores.
2220      */
2221     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2222       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2223       .access = PL1_RW, .accessfn = access_tvm_trvm,
2224       .fgt = FGT_AFSR0_EL1,
2225       .type = ARM_CP_CONST, .resetvalue = 0 },
2226     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2227       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2228       .access = PL1_RW, .accessfn = access_tvm_trvm,
2229       .fgt = FGT_AFSR1_EL1,
2230       .type = ARM_CP_CONST, .resetvalue = 0 },
2231     /*
2232      * MAIR can just read-as-written because we don't implement caches
2233      * and so don't need to care about memory attributes.
2234      */
2235     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2236       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2237       .access = PL1_RW, .accessfn = access_tvm_trvm,
2238       .fgt = FGT_MAIR_EL1,
2239       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2240       .resetvalue = 0 },
2241     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2242       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2243       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2244       .resetvalue = 0 },
2245     /*
2246      * For non-long-descriptor page tables these are PRRR and NMRR;
2247      * regardless they still act as reads-as-written for QEMU.
2248      */
2249      /*
2250       * MAIR0/1 are defined separately from their 64-bit counterpart which
2251       * allows them to assign the correct fieldoffset based on the endianness
2252       * handled in the field definitions.
2253       */
2254     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2255       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2256       .access = PL1_RW, .accessfn = access_tvm_trvm,
2257       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2258                              offsetof(CPUARMState, cp15.mair0_ns) },
2259       .resetfn = arm_cp_reset_ignore },
2260     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2261       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2262       .access = PL1_RW, .accessfn = access_tvm_trvm,
2263       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2264                              offsetof(CPUARMState, cp15.mair1_ns) },
2265       .resetfn = arm_cp_reset_ignore },
2266     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2267       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2268       .fgt = FGT_ISR_EL1,
2269       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2270     /* 32 bit ITLB invalidates */
2271     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2272       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2273       .writefn = tlbiall_write },
2274     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2275       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2276       .writefn = tlbimva_write },
2277     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2278       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2279       .writefn = tlbiasid_write },
2280     /* 32 bit DTLB invalidates */
2281     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2282       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2283       .writefn = tlbiall_write },
2284     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2285       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2286       .writefn = tlbimva_write },
2287     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2288       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2289       .writefn = tlbiasid_write },
2290     /* 32 bit TLB invalidates */
2291     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2292       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2293       .writefn = tlbiall_write },
2294     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2295       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2296       .writefn = tlbimva_write },
2297     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2298       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2299       .writefn = tlbiasid_write },
2300     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2301       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2302       .writefn = tlbimvaa_write },
2303 };
2304 
2305 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2306     /* 32 bit TLB invalidates, Inner Shareable */
2307     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2308       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2309       .writefn = tlbiall_is_write },
2310     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2311       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2312       .writefn = tlbimva_is_write },
2313     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2314       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2315       .writefn = tlbiasid_is_write },
2316     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2317       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2318       .writefn = tlbimvaa_is_write },
2319 };
2320 
2321 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2322     /* PMOVSSET is not implemented in v7 before v7ve */
2323     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2324       .access = PL0_RW, .accessfn = pmreg_access,
2325       .fgt = FGT_PMOVS,
2326       .type = ARM_CP_ALIAS | ARM_CP_IO,
2327       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2328       .writefn = pmovsset_write,
2329       .raw_writefn = raw_write },
2330     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2331       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2332       .access = PL0_RW, .accessfn = pmreg_access,
2333       .fgt = FGT_PMOVS,
2334       .type = ARM_CP_ALIAS | ARM_CP_IO,
2335       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2336       .writefn = pmovsset_write,
2337       .raw_writefn = raw_write },
2338 };
2339 
2340 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2341                         uint64_t value)
2342 {
2343     value &= 1;
2344     env->teecr = value;
2345 }
2346 
2347 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2348                                    bool isread)
2349 {
2350     /*
2351      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2352      * at all, so we don't need to check whether we're v8A.
2353      */
2354     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2355         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2356         return CP_ACCESS_TRAP_EL2;
2357     }
2358     return CP_ACCESS_OK;
2359 }
2360 
2361 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2362                                     bool isread)
2363 {
2364     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2365         return CP_ACCESS_TRAP;
2366     }
2367     return teecr_access(env, ri, isread);
2368 }
2369 
2370 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2371     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2372       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2373       .resetvalue = 0,
2374       .writefn = teecr_write, .accessfn = teecr_access },
2375     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2376       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2377       .accessfn = teehbr_access, .resetvalue = 0 },
2378 };
2379 
2380 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2381     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2382       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2383       .access = PL0_RW,
2384       .fgt = FGT_TPIDR_EL0,
2385       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2386     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2387       .access = PL0_RW,
2388       .fgt = FGT_TPIDR_EL0,
2389       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2390                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2391       .resetfn = arm_cp_reset_ignore },
2392     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2393       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2394       .access = PL0_R | PL1_W,
2395       .fgt = FGT_TPIDRRO_EL0,
2396       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2397       .resetvalue = 0},
2398     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2399       .access = PL0_R | PL1_W,
2400       .fgt = FGT_TPIDRRO_EL0,
2401       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2402                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2403       .resetfn = arm_cp_reset_ignore },
2404     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2405       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2406       .access = PL1_RW,
2407       .fgt = FGT_TPIDR_EL1,
2408       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2409     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2410       .access = PL1_RW,
2411       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2412                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2413       .resetvalue = 0 },
2414 };
2415 
2416 #ifndef CONFIG_USER_ONLY
2417 
2418 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2419                                        bool isread)
2420 {
2421     /*
2422      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2423      * Writable only at the highest implemented exception level.
2424      */
2425     int el = arm_current_el(env);
2426     uint64_t hcr;
2427     uint32_t cntkctl;
2428 
2429     switch (el) {
2430     case 0:
2431         hcr = arm_hcr_el2_eff(env);
2432         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2433             cntkctl = env->cp15.cnthctl_el2;
2434         } else {
2435             cntkctl = env->cp15.c14_cntkctl;
2436         }
2437         if (!extract32(cntkctl, 0, 2)) {
2438             return CP_ACCESS_TRAP;
2439         }
2440         break;
2441     case 1:
2442         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2443             arm_is_secure_below_el3(env)) {
2444             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2445             return CP_ACCESS_TRAP_UNCATEGORIZED;
2446         }
2447         break;
2448     case 2:
2449     case 3:
2450         break;
2451     }
2452 
2453     if (!isread && el < arm_highest_el(env)) {
2454         return CP_ACCESS_TRAP_UNCATEGORIZED;
2455     }
2456 
2457     return CP_ACCESS_OK;
2458 }
2459 
2460 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2461                                         bool isread)
2462 {
2463     unsigned int cur_el = arm_current_el(env);
2464     bool has_el2 = arm_is_el2_enabled(env);
2465     uint64_t hcr = arm_hcr_el2_eff(env);
2466 
2467     switch (cur_el) {
2468     case 0:
2469         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2470         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2471             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2472                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2473         }
2474 
2475         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2476         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2477             return CP_ACCESS_TRAP;
2478         }
2479         /* fall through */
2480     case 1:
2481         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2482         if (has_el2 && timeridx == GTIMER_PHYS &&
2483             (hcr & HCR_E2H
2484              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2485              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2486             return CP_ACCESS_TRAP_EL2;
2487         }
2488         break;
2489     }
2490     return CP_ACCESS_OK;
2491 }
2492 
2493 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2494                                       bool isread)
2495 {
2496     unsigned int cur_el = arm_current_el(env);
2497     bool has_el2 = arm_is_el2_enabled(env);
2498     uint64_t hcr = arm_hcr_el2_eff(env);
2499 
2500     switch (cur_el) {
2501     case 0:
2502         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2503             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2504             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2505                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2506         }
2507 
2508         /*
2509          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2510          * EL0 if EL0[PV]TEN is zero.
2511          */
2512         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2513             return CP_ACCESS_TRAP;
2514         }
2515         /* fall through */
2516 
2517     case 1:
2518         if (has_el2 && timeridx == GTIMER_PHYS) {
2519             if (hcr & HCR_E2H) {
2520                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2521                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2522                     return CP_ACCESS_TRAP_EL2;
2523                 }
2524             } else {
2525                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2526                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2527                     return CP_ACCESS_TRAP_EL2;
2528                 }
2529             }
2530         }
2531         break;
2532     }
2533     return CP_ACCESS_OK;
2534 }
2535 
2536 static CPAccessResult gt_pct_access(CPUARMState *env,
2537                                     const ARMCPRegInfo *ri,
2538                                     bool isread)
2539 {
2540     return gt_counter_access(env, GTIMER_PHYS, isread);
2541 }
2542 
2543 static CPAccessResult gt_vct_access(CPUARMState *env,
2544                                     const ARMCPRegInfo *ri,
2545                                     bool isread)
2546 {
2547     return gt_counter_access(env, GTIMER_VIRT, isread);
2548 }
2549 
2550 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2551                                        bool isread)
2552 {
2553     return gt_timer_access(env, GTIMER_PHYS, isread);
2554 }
2555 
2556 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2557                                        bool isread)
2558 {
2559     return gt_timer_access(env, GTIMER_VIRT, isread);
2560 }
2561 
2562 static CPAccessResult gt_stimer_access(CPUARMState *env,
2563                                        const ARMCPRegInfo *ri,
2564                                        bool isread)
2565 {
2566     /*
2567      * The AArch64 register view of the secure physical timer is
2568      * always accessible from EL3, and configurably accessible from
2569      * Secure EL1.
2570      */
2571     switch (arm_current_el(env)) {
2572     case 1:
2573         if (!arm_is_secure(env)) {
2574             return CP_ACCESS_TRAP;
2575         }
2576         if (!(env->cp15.scr_el3 & SCR_ST)) {
2577             return CP_ACCESS_TRAP_EL3;
2578         }
2579         return CP_ACCESS_OK;
2580     case 0:
2581     case 2:
2582         return CP_ACCESS_TRAP;
2583     case 3:
2584         return CP_ACCESS_OK;
2585     default:
2586         g_assert_not_reached();
2587     }
2588 }
2589 
2590 static uint64_t gt_get_countervalue(CPUARMState *env)
2591 {
2592     ARMCPU *cpu = env_archcpu(env);
2593 
2594     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2595 }
2596 
2597 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2598 {
2599     CPUARMState *env = &cpu->env;
2600     uint64_t cnthctl = env->cp15.cnthctl_el2;
2601     ARMSecuritySpace ss = arm_security_space(env);
2602     /* ISTATUS && !IMASK */
2603     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2604 
2605     /*
2606      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2607      * It is RES0 in Secure and NonSecure state.
2608      */
2609     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2610         ((timeridx == GTIMER_VIRT && (cnthctl & CNTHCTL_CNTVMASK)) ||
2611          (timeridx == GTIMER_PHYS && (cnthctl & CNTHCTL_CNTPMASK)))) {
2612         irqstate = 0;
2613     }
2614 
2615     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2616     trace_arm_gt_update_irq(timeridx, irqstate);
2617 }
2618 
2619 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2620 {
2621     /*
2622      * Changing security state between Root and Secure/NonSecure, which may
2623      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2624      * mask bits. Update the IRQ state accordingly.
2625      */
2626     gt_update_irq(cpu, GTIMER_VIRT);
2627     gt_update_irq(cpu, GTIMER_PHYS);
2628 }
2629 
2630 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2631 {
2632     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2633 
2634     if (gt->ctl & 1) {
2635         /*
2636          * Timer enabled: calculate and set current ISTATUS, irq, and
2637          * reset timer to when ISTATUS next has to change
2638          */
2639         uint64_t offset = timeridx == GTIMER_VIRT ?
2640                                       cpu->env.cp15.cntvoff_el2 : 0;
2641         uint64_t count = gt_get_countervalue(&cpu->env);
2642         /* Note that this must be unsigned 64 bit arithmetic: */
2643         int istatus = count - offset >= gt->cval;
2644         uint64_t nexttick;
2645 
2646         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2647 
2648         if (istatus) {
2649             /*
2650              * Next transition is when (count - offset) rolls back over to 0.
2651              * If offset > count then this is when count == offset;
2652              * if offset <= count then this is when count == offset + 2^64
2653              * For the latter case we set nexttick to an "as far in future
2654              * as possible" value and let the code below handle it.
2655              */
2656             if (offset > count) {
2657                 nexttick = offset;
2658             } else {
2659                 nexttick = UINT64_MAX;
2660             }
2661         } else {
2662             /*
2663              * Next transition is when (count - offset) == cval, i.e.
2664              * when count == (cval + offset).
2665              * If that would overflow, then again we set up the next interrupt
2666              * for "as far in the future as possible" for the code below.
2667              */
2668             if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2669                 nexttick = UINT64_MAX;
2670             }
2671         }
2672         /*
2673          * Note that the desired next expiry time might be beyond the
2674          * signed-64-bit range of a QEMUTimer -- in this case we just
2675          * set the timer for as far in the future as possible. When the
2676          * timer expires we will reset the timer for any remaining period.
2677          */
2678         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2679             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2680         } else {
2681             timer_mod(cpu->gt_timer[timeridx], nexttick);
2682         }
2683         trace_arm_gt_recalc(timeridx, nexttick);
2684     } else {
2685         /* Timer disabled: ISTATUS and timer output always clear */
2686         gt->ctl &= ~4;
2687         timer_del(cpu->gt_timer[timeridx]);
2688         trace_arm_gt_recalc_disabled(timeridx);
2689     }
2690     gt_update_irq(cpu, timeridx);
2691 }
2692 
2693 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2694                            int timeridx)
2695 {
2696     ARMCPU *cpu = env_archcpu(env);
2697 
2698     timer_del(cpu->gt_timer[timeridx]);
2699 }
2700 
2701 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2702 {
2703     return gt_get_countervalue(env);
2704 }
2705 
2706 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2707 {
2708     uint64_t hcr;
2709 
2710     switch (arm_current_el(env)) {
2711     case 2:
2712         hcr = arm_hcr_el2_eff(env);
2713         if (hcr & HCR_E2H) {
2714             return 0;
2715         }
2716         break;
2717     case 0:
2718         hcr = arm_hcr_el2_eff(env);
2719         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2720             return 0;
2721         }
2722         break;
2723     }
2724 
2725     return env->cp15.cntvoff_el2;
2726 }
2727 
2728 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2729 {
2730     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2731 }
2732 
2733 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2734                           int timeridx,
2735                           uint64_t value)
2736 {
2737     trace_arm_gt_cval_write(timeridx, value);
2738     env->cp15.c14_timer[timeridx].cval = value;
2739     gt_recalc_timer(env_archcpu(env), timeridx);
2740 }
2741 
2742 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2743                              int timeridx)
2744 {
2745     uint64_t offset = 0;
2746 
2747     switch (timeridx) {
2748     case GTIMER_VIRT:
2749     case GTIMER_HYPVIRT:
2750         offset = gt_virt_cnt_offset(env);
2751         break;
2752     }
2753 
2754     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2755                       (gt_get_countervalue(env) - offset));
2756 }
2757 
2758 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2759                           int timeridx,
2760                           uint64_t value)
2761 {
2762     uint64_t offset = 0;
2763 
2764     switch (timeridx) {
2765     case GTIMER_VIRT:
2766     case GTIMER_HYPVIRT:
2767         offset = gt_virt_cnt_offset(env);
2768         break;
2769     }
2770 
2771     trace_arm_gt_tval_write(timeridx, value);
2772     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2773                                          sextract64(value, 0, 32);
2774     gt_recalc_timer(env_archcpu(env), timeridx);
2775 }
2776 
2777 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2778                          int timeridx,
2779                          uint64_t value)
2780 {
2781     ARMCPU *cpu = env_archcpu(env);
2782     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2783 
2784     trace_arm_gt_ctl_write(timeridx, value);
2785     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2786     if ((oldval ^ value) & 1) {
2787         /* Enable toggled */
2788         gt_recalc_timer(cpu, timeridx);
2789     } else if ((oldval ^ value) & 2) {
2790         /*
2791          * IMASK toggled: don't need to recalculate,
2792          * just set the interrupt line based on ISTATUS
2793          */
2794         trace_arm_gt_imask_toggle(timeridx);
2795         gt_update_irq(cpu, timeridx);
2796     }
2797 }
2798 
2799 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2800 {
2801     gt_timer_reset(env, ri, GTIMER_PHYS);
2802 }
2803 
2804 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2805                                uint64_t value)
2806 {
2807     gt_cval_write(env, ri, GTIMER_PHYS, value);
2808 }
2809 
2810 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2811 {
2812     return gt_tval_read(env, ri, GTIMER_PHYS);
2813 }
2814 
2815 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2816                                uint64_t value)
2817 {
2818     gt_tval_write(env, ri, GTIMER_PHYS, value);
2819 }
2820 
2821 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2822                               uint64_t value)
2823 {
2824     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2825 }
2826 
2827 static int gt_phys_redir_timeridx(CPUARMState *env)
2828 {
2829     switch (arm_mmu_idx(env)) {
2830     case ARMMMUIdx_E20_0:
2831     case ARMMMUIdx_E20_2:
2832     case ARMMMUIdx_E20_2_PAN:
2833         return GTIMER_HYP;
2834     default:
2835         return GTIMER_PHYS;
2836     }
2837 }
2838 
2839 static int gt_virt_redir_timeridx(CPUARMState *env)
2840 {
2841     switch (arm_mmu_idx(env)) {
2842     case ARMMMUIdx_E20_0:
2843     case ARMMMUIdx_E20_2:
2844     case ARMMMUIdx_E20_2_PAN:
2845         return GTIMER_HYPVIRT;
2846     default:
2847         return GTIMER_VIRT;
2848     }
2849 }
2850 
2851 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2852                                         const ARMCPRegInfo *ri)
2853 {
2854     int timeridx = gt_phys_redir_timeridx(env);
2855     return env->cp15.c14_timer[timeridx].cval;
2856 }
2857 
2858 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2859                                      uint64_t value)
2860 {
2861     int timeridx = gt_phys_redir_timeridx(env);
2862     gt_cval_write(env, ri, timeridx, value);
2863 }
2864 
2865 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2866                                         const ARMCPRegInfo *ri)
2867 {
2868     int timeridx = gt_phys_redir_timeridx(env);
2869     return gt_tval_read(env, ri, timeridx);
2870 }
2871 
2872 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2873                                      uint64_t value)
2874 {
2875     int timeridx = gt_phys_redir_timeridx(env);
2876     gt_tval_write(env, ri, timeridx, value);
2877 }
2878 
2879 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2880                                        const ARMCPRegInfo *ri)
2881 {
2882     int timeridx = gt_phys_redir_timeridx(env);
2883     return env->cp15.c14_timer[timeridx].ctl;
2884 }
2885 
2886 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2887                                     uint64_t value)
2888 {
2889     int timeridx = gt_phys_redir_timeridx(env);
2890     gt_ctl_write(env, ri, timeridx, value);
2891 }
2892 
2893 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2894 {
2895     gt_timer_reset(env, ri, GTIMER_VIRT);
2896 }
2897 
2898 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2899                                uint64_t value)
2900 {
2901     gt_cval_write(env, ri, GTIMER_VIRT, value);
2902 }
2903 
2904 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2905 {
2906     return gt_tval_read(env, ri, GTIMER_VIRT);
2907 }
2908 
2909 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2910                                uint64_t value)
2911 {
2912     gt_tval_write(env, ri, GTIMER_VIRT, value);
2913 }
2914 
2915 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2916                               uint64_t value)
2917 {
2918     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2919 }
2920 
2921 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2922                              uint64_t value)
2923 {
2924     ARMCPU *cpu = env_archcpu(env);
2925     uint32_t oldval = env->cp15.cnthctl_el2;
2926 
2927     raw_write(env, ri, value);
2928 
2929     if ((oldval ^ value) & CNTHCTL_CNTVMASK) {
2930         gt_update_irq(cpu, GTIMER_VIRT);
2931     } else if ((oldval ^ value) & CNTHCTL_CNTPMASK) {
2932         gt_update_irq(cpu, GTIMER_PHYS);
2933     }
2934 }
2935 
2936 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2937                               uint64_t value)
2938 {
2939     ARMCPU *cpu = env_archcpu(env);
2940 
2941     trace_arm_gt_cntvoff_write(value);
2942     raw_write(env, ri, value);
2943     gt_recalc_timer(cpu, GTIMER_VIRT);
2944 }
2945 
2946 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2947                                         const ARMCPRegInfo *ri)
2948 {
2949     int timeridx = gt_virt_redir_timeridx(env);
2950     return env->cp15.c14_timer[timeridx].cval;
2951 }
2952 
2953 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2954                                      uint64_t value)
2955 {
2956     int timeridx = gt_virt_redir_timeridx(env);
2957     gt_cval_write(env, ri, timeridx, value);
2958 }
2959 
2960 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2961                                         const ARMCPRegInfo *ri)
2962 {
2963     int timeridx = gt_virt_redir_timeridx(env);
2964     return gt_tval_read(env, ri, timeridx);
2965 }
2966 
2967 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2968                                      uint64_t value)
2969 {
2970     int timeridx = gt_virt_redir_timeridx(env);
2971     gt_tval_write(env, ri, timeridx, value);
2972 }
2973 
2974 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2975                                        const ARMCPRegInfo *ri)
2976 {
2977     int timeridx = gt_virt_redir_timeridx(env);
2978     return env->cp15.c14_timer[timeridx].ctl;
2979 }
2980 
2981 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2982                                     uint64_t value)
2983 {
2984     int timeridx = gt_virt_redir_timeridx(env);
2985     gt_ctl_write(env, ri, timeridx, value);
2986 }
2987 
2988 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2989 {
2990     gt_timer_reset(env, ri, GTIMER_HYP);
2991 }
2992 
2993 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2994                               uint64_t value)
2995 {
2996     gt_cval_write(env, ri, GTIMER_HYP, value);
2997 }
2998 
2999 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3000 {
3001     return gt_tval_read(env, ri, GTIMER_HYP);
3002 }
3003 
3004 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3005                               uint64_t value)
3006 {
3007     gt_tval_write(env, ri, GTIMER_HYP, value);
3008 }
3009 
3010 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3011                               uint64_t value)
3012 {
3013     gt_ctl_write(env, ri, GTIMER_HYP, value);
3014 }
3015 
3016 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3017 {
3018     gt_timer_reset(env, ri, GTIMER_SEC);
3019 }
3020 
3021 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3022                               uint64_t value)
3023 {
3024     gt_cval_write(env, ri, GTIMER_SEC, value);
3025 }
3026 
3027 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3028 {
3029     return gt_tval_read(env, ri, GTIMER_SEC);
3030 }
3031 
3032 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3033                               uint64_t value)
3034 {
3035     gt_tval_write(env, ri, GTIMER_SEC, value);
3036 }
3037 
3038 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3039                               uint64_t value)
3040 {
3041     gt_ctl_write(env, ri, GTIMER_SEC, value);
3042 }
3043 
3044 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3045 {
3046     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3047 }
3048 
3049 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3050                              uint64_t value)
3051 {
3052     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3053 }
3054 
3055 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3056 {
3057     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3058 }
3059 
3060 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3061                              uint64_t value)
3062 {
3063     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3064 }
3065 
3066 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3067                             uint64_t value)
3068 {
3069     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3070 }
3071 
3072 void arm_gt_ptimer_cb(void *opaque)
3073 {
3074     ARMCPU *cpu = opaque;
3075 
3076     gt_recalc_timer(cpu, GTIMER_PHYS);
3077 }
3078 
3079 void arm_gt_vtimer_cb(void *opaque)
3080 {
3081     ARMCPU *cpu = opaque;
3082 
3083     gt_recalc_timer(cpu, GTIMER_VIRT);
3084 }
3085 
3086 void arm_gt_htimer_cb(void *opaque)
3087 {
3088     ARMCPU *cpu = opaque;
3089 
3090     gt_recalc_timer(cpu, GTIMER_HYP);
3091 }
3092 
3093 void arm_gt_stimer_cb(void *opaque)
3094 {
3095     ARMCPU *cpu = opaque;
3096 
3097     gt_recalc_timer(cpu, GTIMER_SEC);
3098 }
3099 
3100 void arm_gt_hvtimer_cb(void *opaque)
3101 {
3102     ARMCPU *cpu = opaque;
3103 
3104     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3105 }
3106 
3107 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3108 {
3109     ARMCPU *cpu = env_archcpu(env);
3110 
3111     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3112 }
3113 
3114 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3115     /*
3116      * Note that CNTFRQ is purely reads-as-written for the benefit
3117      * of software; writing it doesn't actually change the timer frequency.
3118      * Our reset value matches the fixed frequency we implement the timer at.
3119      */
3120     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3121       .type = ARM_CP_ALIAS,
3122       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3123       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3124     },
3125     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3126       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3127       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3128       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3129       .resetfn = arm_gt_cntfrq_reset,
3130     },
3131     /* overall control: mostly access permissions */
3132     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3133       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3134       .access = PL1_RW,
3135       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3136       .resetvalue = 0,
3137     },
3138     /* per-timer control */
3139     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3140       .secure = ARM_CP_SECSTATE_NS,
3141       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3142       .accessfn = gt_ptimer_access,
3143       .fieldoffset = offsetoflow32(CPUARMState,
3144                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3145       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3146       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3147     },
3148     { .name = "CNTP_CTL_S",
3149       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3150       .secure = ARM_CP_SECSTATE_S,
3151       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3152       .accessfn = gt_ptimer_access,
3153       .fieldoffset = offsetoflow32(CPUARMState,
3154                                    cp15.c14_timer[GTIMER_SEC].ctl),
3155       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3156     },
3157     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3158       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3159       .type = ARM_CP_IO, .access = PL0_RW,
3160       .accessfn = gt_ptimer_access,
3161       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3162       .resetvalue = 0,
3163       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3164       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3165     },
3166     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3167       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3168       .accessfn = gt_vtimer_access,
3169       .fieldoffset = offsetoflow32(CPUARMState,
3170                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3171       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3172       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3173     },
3174     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3175       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3176       .type = ARM_CP_IO, .access = PL0_RW,
3177       .accessfn = gt_vtimer_access,
3178       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3179       .resetvalue = 0,
3180       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3181       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3182     },
3183     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3184     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3185       .secure = ARM_CP_SECSTATE_NS,
3186       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3187       .accessfn = gt_ptimer_access,
3188       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3189     },
3190     { .name = "CNTP_TVAL_S",
3191       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3192       .secure = ARM_CP_SECSTATE_S,
3193       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3194       .accessfn = gt_ptimer_access,
3195       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3196     },
3197     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3198       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3199       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3200       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3201       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3202     },
3203     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3204       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3205       .accessfn = gt_vtimer_access,
3206       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3207     },
3208     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3209       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3210       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3211       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3212       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3213     },
3214     /* The counter itself */
3215     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3216       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3217       .accessfn = gt_pct_access,
3218       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3219     },
3220     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3221       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3222       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3223       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3224     },
3225     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3226       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3227       .accessfn = gt_vct_access,
3228       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3229     },
3230     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3231       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3232       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3233       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3234     },
3235     /* Comparison value, indicating when the timer goes off */
3236     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3237       .secure = ARM_CP_SECSTATE_NS,
3238       .access = PL0_RW,
3239       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3240       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3241       .accessfn = gt_ptimer_access,
3242       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3243       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3244     },
3245     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3246       .secure = ARM_CP_SECSTATE_S,
3247       .access = PL0_RW,
3248       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3249       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3250       .accessfn = gt_ptimer_access,
3251       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3252     },
3253     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3254       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3255       .access = PL0_RW,
3256       .type = ARM_CP_IO,
3257       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3258       .resetvalue = 0, .accessfn = gt_ptimer_access,
3259       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3260       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3261     },
3262     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3263       .access = PL0_RW,
3264       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3265       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3266       .accessfn = gt_vtimer_access,
3267       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3268       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3269     },
3270     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3271       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3272       .access = PL0_RW,
3273       .type = ARM_CP_IO,
3274       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3275       .resetvalue = 0, .accessfn = gt_vtimer_access,
3276       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3277       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3278     },
3279     /*
3280      * Secure timer -- this is actually restricted to only EL3
3281      * and configurably Secure-EL1 via the accessfn.
3282      */
3283     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3284       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3285       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3286       .accessfn = gt_stimer_access,
3287       .readfn = gt_sec_tval_read,
3288       .writefn = gt_sec_tval_write,
3289       .resetfn = gt_sec_timer_reset,
3290     },
3291     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3292       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3293       .type = ARM_CP_IO, .access = PL1_RW,
3294       .accessfn = gt_stimer_access,
3295       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3296       .resetvalue = 0,
3297       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3298     },
3299     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3300       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3301       .type = ARM_CP_IO, .access = PL1_RW,
3302       .accessfn = gt_stimer_access,
3303       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3304       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3305     },
3306 };
3307 
3308 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3309                                  bool isread)
3310 {
3311     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3312         return CP_ACCESS_TRAP;
3313     }
3314     return CP_ACCESS_OK;
3315 }
3316 
3317 #else
3318 
3319 /*
3320  * In user-mode most of the generic timer registers are inaccessible
3321  * however modern kernels (4.12+) allow access to cntvct_el0
3322  */
3323 
3324 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3325 {
3326     ARMCPU *cpu = env_archcpu(env);
3327 
3328     /*
3329      * Currently we have no support for QEMUTimer in linux-user so we
3330      * can't call gt_get_countervalue(env), instead we directly
3331      * call the lower level functions.
3332      */
3333     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3334 }
3335 
3336 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3337     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3338       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3339       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3340       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3341       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3342     },
3343     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3344       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3345       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3346       .readfn = gt_virt_cnt_read,
3347     },
3348 };
3349 
3350 #endif
3351 
3352 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3353 {
3354     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3355         raw_write(env, ri, value);
3356     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3357         raw_write(env, ri, value & 0xfffff6ff);
3358     } else {
3359         raw_write(env, ri, value & 0xfffff1ff);
3360     }
3361 }
3362 
3363 #ifndef CONFIG_USER_ONLY
3364 /* get_phys_addr() isn't present for user-mode-only targets */
3365 
3366 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3367                                  bool isread)
3368 {
3369     if (ri->opc2 & 4) {
3370         /*
3371          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3372          * Secure EL1 (which can only happen if EL3 is AArch64).
3373          * They are simply UNDEF if executed from NS EL1.
3374          * They function normally from EL2 or EL3.
3375          */
3376         if (arm_current_el(env) == 1) {
3377             if (arm_is_secure_below_el3(env)) {
3378                 if (env->cp15.scr_el3 & SCR_EEL2) {
3379                     return CP_ACCESS_TRAP_EL2;
3380                 }
3381                 return CP_ACCESS_TRAP_EL3;
3382             }
3383             return CP_ACCESS_TRAP_UNCATEGORIZED;
3384         }
3385     }
3386     return CP_ACCESS_OK;
3387 }
3388 
3389 #ifdef CONFIG_TCG
3390 static int par_el1_shareability(GetPhysAddrResult *res)
3391 {
3392     /*
3393      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3394      * memory -- see pseudocode PAREncodeShareability().
3395      */
3396     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3397         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3398         return 2;
3399     }
3400     return res->cacheattrs.shareability;
3401 }
3402 
3403 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3404                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3405                              ARMSecuritySpace ss)
3406 {
3407     bool ret;
3408     uint64_t par64;
3409     bool format64 = false;
3410     ARMMMUFaultInfo fi = {};
3411     GetPhysAddrResult res = {};
3412 
3413     /*
3414      * I_MXTJT: Granule protection checks are not performed on the final address
3415      * of a successful translation.
3416      */
3417     ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss,
3418                                          &res, &fi);
3419 
3420     /*
3421      * ATS operations only do S1 or S1+S2 translations, so we never
3422      * have to deal with the ARMCacheAttrs format for S2 only.
3423      */
3424     assert(!res.cacheattrs.is_s2_format);
3425 
3426     if (ret) {
3427         /*
3428          * Some kinds of translation fault must cause exceptions rather
3429          * than being reported in the PAR.
3430          */
3431         int current_el = arm_current_el(env);
3432         int target_el;
3433         uint32_t syn, fsr, fsc;
3434         bool take_exc = false;
3435 
3436         if (fi.s1ptw && current_el == 1
3437             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3438             /*
3439              * Synchronous stage 2 fault on an access made as part of the
3440              * translation table walk for AT S1E0* or AT S1E1* insn
3441              * executed from NS EL1. If this is a synchronous external abort
3442              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3443              * to EL3. Otherwise the fault is taken as an exception to EL2,
3444              * and HPFAR_EL2 holds the faulting IPA.
3445              */
3446             if (fi.type == ARMFault_SyncExternalOnWalk &&
3447                 (env->cp15.scr_el3 & SCR_EA)) {
3448                 target_el = 3;
3449             } else {
3450                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3451                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3452                     env->cp15.hpfar_el2 |= HPFAR_NS;
3453                 }
3454                 target_el = 2;
3455             }
3456             take_exc = true;
3457         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3458             /*
3459              * Synchronous external aborts during a translation table walk
3460              * are taken as Data Abort exceptions.
3461              */
3462             if (fi.stage2) {
3463                 if (current_el == 3) {
3464                     target_el = 3;
3465                 } else {
3466                     target_el = 2;
3467                 }
3468             } else {
3469                 target_el = exception_target_el(env);
3470             }
3471             take_exc = true;
3472         }
3473 
3474         if (take_exc) {
3475             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3476             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3477                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3478                 fsr = arm_fi_to_lfsc(&fi);
3479                 fsc = extract32(fsr, 0, 6);
3480             } else {
3481                 fsr = arm_fi_to_sfsc(&fi);
3482                 fsc = 0x3f;
3483             }
3484             /*
3485              * Report exception with ESR indicating a fault due to a
3486              * translation table walk for a cache maintenance instruction.
3487              */
3488             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3489                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3490             env->exception.vaddress = value;
3491             env->exception.fsr = fsr;
3492             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3493         }
3494     }
3495 
3496     if (is_a64(env)) {
3497         format64 = true;
3498     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3499         /*
3500          * ATS1Cxx:
3501          * * TTBCR.EAE determines whether the result is returned using the
3502          *   32-bit or the 64-bit PAR format
3503          * * Instructions executed in Hyp mode always use the 64bit format
3504          *
3505          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3506          * * The Non-secure TTBCR.EAE bit is set to 1
3507          * * The implementation includes EL2, and the value of HCR.VM is 1
3508          *
3509          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3510          *
3511          * ATS1Hx always uses the 64bit format.
3512          */
3513         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3514 
3515         if (arm_feature(env, ARM_FEATURE_EL2)) {
3516             if (mmu_idx == ARMMMUIdx_E10_0 ||
3517                 mmu_idx == ARMMMUIdx_E10_1 ||
3518                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3519                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3520             } else {
3521                 format64 |= arm_current_el(env) == 2;
3522             }
3523         }
3524     }
3525 
3526     if (format64) {
3527         /* Create a 64-bit PAR */
3528         par64 = (1 << 11); /* LPAE bit always set */
3529         if (!ret) {
3530             par64 |= res.f.phys_addr & ~0xfffULL;
3531             if (!res.f.attrs.secure) {
3532                 par64 |= (1 << 9); /* NS */
3533             }
3534             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3535             par64 |= par_el1_shareability(&res) << 7; /* SH */
3536         } else {
3537             uint32_t fsr = arm_fi_to_lfsc(&fi);
3538 
3539             par64 |= 1; /* F */
3540             par64 |= (fsr & 0x3f) << 1; /* FS */
3541             if (fi.stage2) {
3542                 par64 |= (1 << 9); /* S */
3543             }
3544             if (fi.s1ptw) {
3545                 par64 |= (1 << 8); /* PTW */
3546             }
3547         }
3548     } else {
3549         /*
3550          * fsr is a DFSR/IFSR value for the short descriptor
3551          * translation table format (with WnR always clear).
3552          * Convert it to a 32-bit PAR.
3553          */
3554         if (!ret) {
3555             /* We do not set any attribute bits in the PAR */
3556             if (res.f.lg_page_size == 24
3557                 && arm_feature(env, ARM_FEATURE_V7)) {
3558                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3559             } else {
3560                 par64 = res.f.phys_addr & 0xfffff000;
3561             }
3562             if (!res.f.attrs.secure) {
3563                 par64 |= (1 << 9); /* NS */
3564             }
3565         } else {
3566             uint32_t fsr = arm_fi_to_sfsc(&fi);
3567 
3568             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3569                     ((fsr & 0xf) << 1) | 1;
3570         }
3571     }
3572     return par64;
3573 }
3574 #endif /* CONFIG_TCG */
3575 
3576 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3577 {
3578 #ifdef CONFIG_TCG
3579     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3580     uint64_t par64;
3581     ARMMMUIdx mmu_idx;
3582     int el = arm_current_el(env);
3583     ARMSecuritySpace ss = arm_security_space(env);
3584 
3585     switch (ri->opc2 & 6) {
3586     case 0:
3587         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3588         switch (el) {
3589         case 3:
3590             mmu_idx = ARMMMUIdx_E3;
3591             break;
3592         case 2:
3593             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3594             /* fall through */
3595         case 1:
3596             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3597                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3598             } else {
3599                 mmu_idx = ARMMMUIdx_Stage1_E1;
3600             }
3601             break;
3602         default:
3603             g_assert_not_reached();
3604         }
3605         break;
3606     case 2:
3607         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3608         switch (el) {
3609         case 3:
3610             mmu_idx = ARMMMUIdx_E10_0;
3611             break;
3612         case 2:
3613             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3614             mmu_idx = ARMMMUIdx_Stage1_E0;
3615             break;
3616         case 1:
3617             mmu_idx = ARMMMUIdx_Stage1_E0;
3618             break;
3619         default:
3620             g_assert_not_reached();
3621         }
3622         break;
3623     case 4:
3624         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3625         mmu_idx = ARMMMUIdx_E10_1;
3626         ss = ARMSS_NonSecure;
3627         break;
3628     case 6:
3629         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3630         mmu_idx = ARMMMUIdx_E10_0;
3631         ss = ARMSS_NonSecure;
3632         break;
3633     default:
3634         g_assert_not_reached();
3635     }
3636 
3637     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3638 
3639     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3640 #else
3641     /* Handled by hardware accelerator. */
3642     g_assert_not_reached();
3643 #endif /* CONFIG_TCG */
3644 }
3645 
3646 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3647                         uint64_t value)
3648 {
3649 #ifdef CONFIG_TCG
3650     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3651     uint64_t par64;
3652 
3653     /* There is no SecureEL2 for AArch32. */
3654     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3655                          ARMSS_NonSecure);
3656 
3657     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3658 #else
3659     /* Handled by hardware accelerator. */
3660     g_assert_not_reached();
3661 #endif /* CONFIG_TCG */
3662 }
3663 
3664 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3665                                      bool isread)
3666 {
3667     /*
3668      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3669      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3670      * only happen when executing at EL3 because that combination also causes an
3671      * illegal exception return. We don't need to check FEAT_RME either, because
3672      * scr_write() ensures that the NSE bit is not set otherwise.
3673      */
3674     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3675         return CP_ACCESS_TRAP;
3676     }
3677     return CP_ACCESS_OK;
3678 }
3679 
3680 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3681                                      bool isread)
3682 {
3683     if (arm_current_el(env) == 3 &&
3684         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3685         return CP_ACCESS_TRAP;
3686     }
3687     return at_e012_access(env, ri, isread);
3688 }
3689 
3690 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3691                         uint64_t value)
3692 {
3693 #ifdef CONFIG_TCG
3694     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3695     ARMMMUIdx mmu_idx;
3696     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3697     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3698 
3699     switch (ri->opc2 & 6) {
3700     case 0:
3701         switch (ri->opc1) {
3702         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3703             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3704                 mmu_idx = regime_e20 ?
3705                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3706             } else {
3707                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3708             }
3709             break;
3710         case 4: /* AT S1E2R, AT S1E2W */
3711             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3712             break;
3713         case 6: /* AT S1E3R, AT S1E3W */
3714             mmu_idx = ARMMMUIdx_E3;
3715             break;
3716         default:
3717             g_assert_not_reached();
3718         }
3719         break;
3720     case 2: /* AT S1E0R, AT S1E0W */
3721         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3722         break;
3723     case 4: /* AT S12E1R, AT S12E1W */
3724         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3725         break;
3726     case 6: /* AT S12E0R, AT S12E0W */
3727         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3728         break;
3729     default:
3730         g_assert_not_reached();
3731     }
3732 
3733     env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3734                                        mmu_idx, arm_security_space(env));
3735 #else
3736     /* Handled by hardware accelerator. */
3737     g_assert_not_reached();
3738 #endif /* CONFIG_TCG */
3739 }
3740 #endif
3741 
3742 /* Return basic MPU access permission bits.  */
3743 static uint32_t simple_mpu_ap_bits(uint32_t val)
3744 {
3745     uint32_t ret;
3746     uint32_t mask;
3747     int i;
3748     ret = 0;
3749     mask = 3;
3750     for (i = 0; i < 16; i += 2) {
3751         ret |= (val >> i) & mask;
3752         mask <<= 2;
3753     }
3754     return ret;
3755 }
3756 
3757 /* Pad basic MPU access permission bits to extended format.  */
3758 static uint32_t extended_mpu_ap_bits(uint32_t val)
3759 {
3760     uint32_t ret;
3761     uint32_t mask;
3762     int i;
3763     ret = 0;
3764     mask = 3;
3765     for (i = 0; i < 16; i += 2) {
3766         ret |= (val & mask) << i;
3767         mask <<= 2;
3768     }
3769     return ret;
3770 }
3771 
3772 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3773                                  uint64_t value)
3774 {
3775     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3776 }
3777 
3778 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3779 {
3780     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3781 }
3782 
3783 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3784                                  uint64_t value)
3785 {
3786     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3787 }
3788 
3789 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3790 {
3791     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3792 }
3793 
3794 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3795 {
3796     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3797 
3798     if (!u32p) {
3799         return 0;
3800     }
3801 
3802     u32p += env->pmsav7.rnr[M_REG_NS];
3803     return *u32p;
3804 }
3805 
3806 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3807                          uint64_t value)
3808 {
3809     ARMCPU *cpu = env_archcpu(env);
3810     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3811 
3812     if (!u32p) {
3813         return;
3814     }
3815 
3816     u32p += env->pmsav7.rnr[M_REG_NS];
3817     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3818     *u32p = value;
3819 }
3820 
3821 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3822                               uint64_t value)
3823 {
3824     ARMCPU *cpu = env_archcpu(env);
3825     uint32_t nrgs = cpu->pmsav7_dregion;
3826 
3827     if (value >= nrgs) {
3828         qemu_log_mask(LOG_GUEST_ERROR,
3829                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3830                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3831         return;
3832     }
3833 
3834     raw_write(env, ri, value);
3835 }
3836 
3837 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3838                           uint64_t value)
3839 {
3840     ARMCPU *cpu = env_archcpu(env);
3841 
3842     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3843     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3844 }
3845 
3846 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3847 {
3848     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3849 }
3850 
3851 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3852                           uint64_t value)
3853 {
3854     ARMCPU *cpu = env_archcpu(env);
3855 
3856     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3857     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3858 }
3859 
3860 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3861 {
3862     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3863 }
3864 
3865 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3866                            uint64_t value)
3867 {
3868     ARMCPU *cpu = env_archcpu(env);
3869 
3870     /*
3871      * Ignore writes that would select not implemented region.
3872      * This is architecturally UNPREDICTABLE.
3873      */
3874     if (value >= cpu->pmsav7_dregion) {
3875         return;
3876     }
3877 
3878     env->pmsav7.rnr[M_REG_NS] = value;
3879 }
3880 
3881 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3882                           uint64_t value)
3883 {
3884     ARMCPU *cpu = env_archcpu(env);
3885 
3886     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3887     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3888 }
3889 
3890 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3891 {
3892     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3893 }
3894 
3895 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3896                           uint64_t value)
3897 {
3898     ARMCPU *cpu = env_archcpu(env);
3899 
3900     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3901     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3902 }
3903 
3904 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3905 {
3906     return env->pmsav8.hprlar[env->pmsav8.hprselr];
3907 }
3908 
3909 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3910                           uint64_t value)
3911 {
3912     uint32_t n;
3913     uint32_t bit;
3914     ARMCPU *cpu = env_archcpu(env);
3915 
3916     /* Ignore writes to unimplemented regions */
3917     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3918     value &= MAKE_64BIT_MASK(0, rmax);
3919 
3920     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3921 
3922     /* Register alias is only valid for first 32 indexes */
3923     for (n = 0; n < rmax; ++n) {
3924         bit = extract32(value, n, 1);
3925         env->pmsav8.hprlar[n] = deposit32(
3926                     env->pmsav8.hprlar[n], 0, 1, bit);
3927     }
3928 }
3929 
3930 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3931 {
3932     uint32_t n;
3933     uint32_t result = 0x0;
3934     ARMCPU *cpu = env_archcpu(env);
3935 
3936     /* Register alias is only valid for first 32 indexes */
3937     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3938         if (env->pmsav8.hprlar[n] & 0x1) {
3939             result |= (0x1 << n);
3940         }
3941     }
3942     return result;
3943 }
3944 
3945 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3946                            uint64_t value)
3947 {
3948     ARMCPU *cpu = env_archcpu(env);
3949 
3950     /*
3951      * Ignore writes that would select not implemented region.
3952      * This is architecturally UNPREDICTABLE.
3953      */
3954     if (value >= cpu->pmsav8r_hdregion) {
3955         return;
3956     }
3957 
3958     env->pmsav8.hprselr = value;
3959 }
3960 
3961 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
3962                           uint64_t value)
3963 {
3964     ARMCPU *cpu = env_archcpu(env);
3965     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3966                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3967 
3968     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3969 
3970     if (ri->opc1 & 4) {
3971         if (index >= cpu->pmsav8r_hdregion) {
3972             return;
3973         }
3974         if (ri->opc2 & 0x1) {
3975             env->pmsav8.hprlar[index] = value;
3976         } else {
3977             env->pmsav8.hprbar[index] = value;
3978         }
3979     } else {
3980         if (index >= cpu->pmsav7_dregion) {
3981             return;
3982         }
3983         if (ri->opc2 & 0x1) {
3984             env->pmsav8.rlar[M_REG_NS][index] = value;
3985         } else {
3986             env->pmsav8.rbar[M_REG_NS][index] = value;
3987         }
3988     }
3989 }
3990 
3991 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
3992 {
3993     ARMCPU *cpu = env_archcpu(env);
3994     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3995                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3996 
3997     if (ri->opc1 & 4) {
3998         if (index >= cpu->pmsav8r_hdregion) {
3999             return 0x0;
4000         }
4001         if (ri->opc2 & 0x1) {
4002             return env->pmsav8.hprlar[index];
4003         } else {
4004             return env->pmsav8.hprbar[index];
4005         }
4006     } else {
4007         if (index >= cpu->pmsav7_dregion) {
4008             return 0x0;
4009         }
4010         if (ri->opc2 & 0x1) {
4011             return env->pmsav8.rlar[M_REG_NS][index];
4012         } else {
4013             return env->pmsav8.rbar[M_REG_NS][index];
4014         }
4015     }
4016 }
4017 
4018 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4019     { .name = "PRBAR",
4020       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4021       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4022       .accessfn = access_tvm_trvm,
4023       .readfn = prbar_read, .writefn = prbar_write },
4024     { .name = "PRLAR",
4025       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4026       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4027       .accessfn = access_tvm_trvm,
4028       .readfn = prlar_read, .writefn = prlar_write },
4029     { .name = "PRSELR", .resetvalue = 0,
4030       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4031       .access = PL1_RW, .accessfn = access_tvm_trvm,
4032       .writefn = prselr_write,
4033       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4034     { .name = "HPRBAR", .resetvalue = 0,
4035       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4036       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4037       .readfn = hprbar_read, .writefn = hprbar_write },
4038     { .name = "HPRLAR",
4039       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4040       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4041       .readfn = hprlar_read, .writefn = hprlar_write },
4042     { .name = "HPRSELR", .resetvalue = 0,
4043       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4044       .access = PL2_RW,
4045       .writefn = hprselr_write,
4046       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4047     { .name = "HPRENR",
4048       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4049       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4050       .readfn = hprenr_read, .writefn = hprenr_write },
4051 };
4052 
4053 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4054     /*
4055      * Reset for all these registers is handled in arm_cpu_reset(),
4056      * because the PMSAv7 is also used by M-profile CPUs, which do
4057      * not register cpregs but still need the state to be reset.
4058      */
4059     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4060       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4061       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4062       .readfn = pmsav7_read, .writefn = pmsav7_write,
4063       .resetfn = arm_cp_reset_ignore },
4064     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4065       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4066       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4067       .readfn = pmsav7_read, .writefn = pmsav7_write,
4068       .resetfn = arm_cp_reset_ignore },
4069     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4070       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4071       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4072       .readfn = pmsav7_read, .writefn = pmsav7_write,
4073       .resetfn = arm_cp_reset_ignore },
4074     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4075       .access = PL1_RW,
4076       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4077       .writefn = pmsav7_rgnr_write,
4078       .resetfn = arm_cp_reset_ignore },
4079 };
4080 
4081 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4082     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4083       .access = PL1_RW, .type = ARM_CP_ALIAS,
4084       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4085       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4086     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4087       .access = PL1_RW, .type = ARM_CP_ALIAS,
4088       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4089       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4090     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4091       .access = PL1_RW,
4092       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4093       .resetvalue = 0, },
4094     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4095       .access = PL1_RW,
4096       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4097       .resetvalue = 0, },
4098     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4099       .access = PL1_RW,
4100       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4101     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4102       .access = PL1_RW,
4103       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4104     /* Protection region base and size registers */
4105     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4106       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4107       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4108     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4109       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4110       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4111     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4112       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4113       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4114     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4115       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4116       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4117     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4118       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4119       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4120     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4121       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4122       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4123     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4124       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4125       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4126     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4127       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4128       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4129 };
4130 
4131 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4132                              uint64_t value)
4133 {
4134     ARMCPU *cpu = env_archcpu(env);
4135 
4136     if (!arm_feature(env, ARM_FEATURE_V8)) {
4137         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4138             /*
4139              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4140              * using Long-descriptor translation table format
4141              */
4142             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4143         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4144             /*
4145              * In an implementation that includes the Security Extensions
4146              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4147              * Short-descriptor translation table format.
4148              */
4149             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4150         } else {
4151             value &= TTBCR_N;
4152         }
4153     }
4154 
4155     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4156         /*
4157          * With LPAE the TTBCR could result in a change of ASID
4158          * via the TTBCR.A1 bit, so do a TLB flush.
4159          */
4160         tlb_flush(CPU(cpu));
4161     }
4162     raw_write(env, ri, value);
4163 }
4164 
4165 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4166                                uint64_t value)
4167 {
4168     ARMCPU *cpu = env_archcpu(env);
4169 
4170     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4171     tlb_flush(CPU(cpu));
4172     raw_write(env, ri, value);
4173 }
4174 
4175 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4176                             uint64_t value)
4177 {
4178     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4179     if (cpreg_field_is_64bit(ri) &&
4180         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4181         ARMCPU *cpu = env_archcpu(env);
4182         tlb_flush(CPU(cpu));
4183     }
4184     raw_write(env, ri, value);
4185 }
4186 
4187 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4188                                     uint64_t value)
4189 {
4190     /*
4191      * If we are running with E2&0 regime, then an ASID is active.
4192      * Flush if that might be changing.  Note we're not checking
4193      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4194      * holds the active ASID, only checking the field that might.
4195      */
4196     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4197         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4198         uint16_t mask = ARMMMUIdxBit_E20_2 |
4199                         ARMMMUIdxBit_E20_2_PAN |
4200                         ARMMMUIdxBit_E20_0;
4201         tlb_flush_by_mmuidx(env_cpu(env), mask);
4202     }
4203     raw_write(env, ri, value);
4204 }
4205 
4206 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4207                         uint64_t value)
4208 {
4209     ARMCPU *cpu = env_archcpu(env);
4210     CPUState *cs = CPU(cpu);
4211 
4212     /*
4213      * A change in VMID to the stage2 page table (Stage2) invalidates
4214      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4215      */
4216     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4217         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4218     }
4219     raw_write(env, ri, value);
4220 }
4221 
4222 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4223     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4224       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4225       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4226                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4227     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4228       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4229       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4230                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4231     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4232       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4233       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4234                              offsetof(CPUARMState, cp15.dfar_ns) } },
4235     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4236       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4237       .access = PL1_RW, .accessfn = access_tvm_trvm,
4238       .fgt = FGT_FAR_EL1,
4239       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4240       .resetvalue = 0, },
4241 };
4242 
4243 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4244     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4245       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4246       .access = PL1_RW, .accessfn = access_tvm_trvm,
4247       .fgt = FGT_ESR_EL1,
4248       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4249     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4250       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4251       .access = PL1_RW, .accessfn = access_tvm_trvm,
4252       .fgt = FGT_TTBR0_EL1,
4253       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4254       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4255                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4256     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4257       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4258       .access = PL1_RW, .accessfn = access_tvm_trvm,
4259       .fgt = FGT_TTBR1_EL1,
4260       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4261       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4262                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4263     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4264       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4265       .access = PL1_RW, .accessfn = access_tvm_trvm,
4266       .fgt = FGT_TCR_EL1,
4267       .writefn = vmsa_tcr_el12_write,
4268       .raw_writefn = raw_write,
4269       .resetvalue = 0,
4270       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4271     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4272       .access = PL1_RW, .accessfn = access_tvm_trvm,
4273       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4274       .raw_writefn = raw_write,
4275       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4276                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4277 };
4278 
4279 /*
4280  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4281  * qemu tlbs nor adjusting cached masks.
4282  */
4283 static const ARMCPRegInfo ttbcr2_reginfo = {
4284     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4285     .access = PL1_RW, .accessfn = access_tvm_trvm,
4286     .type = ARM_CP_ALIAS,
4287     .bank_fieldoffsets = {
4288         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4289         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4290     },
4291 };
4292 
4293 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4294                                 uint64_t value)
4295 {
4296     env->cp15.c15_ticonfig = value & 0xe7;
4297     /* The OS_TYPE bit in this register changes the reported CPUID! */
4298     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4299         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4300 }
4301 
4302 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4303                                 uint64_t value)
4304 {
4305     env->cp15.c15_threadid = value & 0xffff;
4306 }
4307 
4308 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4309                            uint64_t value)
4310 {
4311     /* Wait-for-interrupt (deprecated) */
4312     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4313 }
4314 
4315 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4316                                   uint64_t value)
4317 {
4318     /*
4319      * On OMAP there are registers indicating the max/min index of dcache lines
4320      * containing a dirty line; cache flush operations have to reset these.
4321      */
4322     env->cp15.c15_i_max = 0x000;
4323     env->cp15.c15_i_min = 0xff0;
4324 }
4325 
4326 static const ARMCPRegInfo omap_cp_reginfo[] = {
4327     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4328       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4329       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4330       .resetvalue = 0, },
4331     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4332       .access = PL1_RW, .type = ARM_CP_NOP },
4333     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4334       .access = PL1_RW,
4335       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4336       .writefn = omap_ticonfig_write },
4337     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4338       .access = PL1_RW,
4339       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4340     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4341       .access = PL1_RW, .resetvalue = 0xff0,
4342       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4343     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4344       .access = PL1_RW,
4345       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4346       .writefn = omap_threadid_write },
4347     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4348       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4349       .type = ARM_CP_NO_RAW,
4350       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4351     /*
4352      * TODO: Peripheral port remap register:
4353      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4354      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4355      * when MMU is off.
4356      */
4357     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4358       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4359       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4360       .writefn = omap_cachemaint_write },
4361     { .name = "C9", .cp = 15, .crn = 9,
4362       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4363       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4364 };
4365 
4366 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4367                               uint64_t value)
4368 {
4369     env->cp15.c15_cpar = value & 0x3fff;
4370 }
4371 
4372 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4373     { .name = "XSCALE_CPAR",
4374       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4375       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4376       .writefn = xscale_cpar_write, },
4377     { .name = "XSCALE_AUXCR",
4378       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4379       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4380       .resetvalue = 0, },
4381     /*
4382      * XScale specific cache-lockdown: since we have no cache we NOP these
4383      * and hope the guest does not really rely on cache behaviour.
4384      */
4385     { .name = "XSCALE_LOCK_ICACHE_LINE",
4386       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4387       .access = PL1_W, .type = ARM_CP_NOP },
4388     { .name = "XSCALE_UNLOCK_ICACHE",
4389       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4390       .access = PL1_W, .type = ARM_CP_NOP },
4391     { .name = "XSCALE_DCACHE_LOCK",
4392       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4393       .access = PL1_RW, .type = ARM_CP_NOP },
4394     { .name = "XSCALE_UNLOCK_DCACHE",
4395       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4396       .access = PL1_W, .type = ARM_CP_NOP },
4397 };
4398 
4399 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4400     /*
4401      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4402      * implementation of this implementation-defined space.
4403      * Ideally this should eventually disappear in favour of actually
4404      * implementing the correct behaviour for all cores.
4405      */
4406     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4407       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4408       .access = PL1_RW,
4409       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4410       .resetvalue = 0 },
4411 };
4412 
4413 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4414     /* Cache status: RAZ because we have no cache so it's always clean */
4415     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4416       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4417       .resetvalue = 0 },
4418 };
4419 
4420 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4421     /* We never have a block transfer operation in progress */
4422     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4423       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4424       .resetvalue = 0 },
4425     /* The cache ops themselves: these all NOP for QEMU */
4426     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4427       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4428     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4429       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4430     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4431       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4432     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4433       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4434     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4435       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4436     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4437       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4438 };
4439 
4440 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4441     /*
4442      * The cache test-and-clean instructions always return (1 << 30)
4443      * to indicate that there are no dirty cache lines.
4444      */
4445     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4446       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4447       .resetvalue = (1 << 30) },
4448     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4449       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4450       .resetvalue = (1 << 30) },
4451 };
4452 
4453 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4454     /* Ignore ReadBuffer accesses */
4455     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4456       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4457       .access = PL1_RW, .resetvalue = 0,
4458       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4459 };
4460 
4461 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4462 {
4463     unsigned int cur_el = arm_current_el(env);
4464 
4465     if (arm_is_el2_enabled(env) && cur_el == 1) {
4466         return env->cp15.vpidr_el2;
4467     }
4468     return raw_read(env, ri);
4469 }
4470 
4471 static uint64_t mpidr_read_val(CPUARMState *env)
4472 {
4473     ARMCPU *cpu = env_archcpu(env);
4474     uint64_t mpidr = cpu->mp_affinity;
4475 
4476     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4477         mpidr |= (1U << 31);
4478         /*
4479          * Cores which are uniprocessor (non-coherent)
4480          * but still implement the MP extensions set
4481          * bit 30. (For instance, Cortex-R5).
4482          */
4483         if (cpu->mp_is_up) {
4484             mpidr |= (1u << 30);
4485         }
4486     }
4487     return mpidr;
4488 }
4489 
4490 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4491 {
4492     unsigned int cur_el = arm_current_el(env);
4493 
4494     if (arm_is_el2_enabled(env) && cur_el == 1) {
4495         return env->cp15.vmpidr_el2;
4496     }
4497     return mpidr_read_val(env);
4498 }
4499 
4500 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4501     /* NOP AMAIR0/1 */
4502     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4503       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4504       .access = PL1_RW, .accessfn = access_tvm_trvm,
4505       .fgt = FGT_AMAIR_EL1,
4506       .type = ARM_CP_CONST, .resetvalue = 0 },
4507     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4508     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4509       .access = PL1_RW, .accessfn = access_tvm_trvm,
4510       .type = ARM_CP_CONST, .resetvalue = 0 },
4511     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4512       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4513       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4514                              offsetof(CPUARMState, cp15.par_ns)} },
4515     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4516       .access = PL1_RW, .accessfn = access_tvm_trvm,
4517       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4518       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4519                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4520       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4521     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4522       .access = PL1_RW, .accessfn = access_tvm_trvm,
4523       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4524       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4525                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4526       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4527 };
4528 
4529 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4530 {
4531     return vfp_get_fpcr(env);
4532 }
4533 
4534 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4535                             uint64_t value)
4536 {
4537     vfp_set_fpcr(env, value);
4538 }
4539 
4540 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4541 {
4542     return vfp_get_fpsr(env);
4543 }
4544 
4545 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4546                             uint64_t value)
4547 {
4548     vfp_set_fpsr(env, value);
4549 }
4550 
4551 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4552                                        bool isread)
4553 {
4554     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4555         return CP_ACCESS_TRAP;
4556     }
4557     return CP_ACCESS_OK;
4558 }
4559 
4560 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4561                             uint64_t value)
4562 {
4563     env->daif = value & PSTATE_DAIF;
4564 }
4565 
4566 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4567 {
4568     return env->pstate & PSTATE_PAN;
4569 }
4570 
4571 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4572                            uint64_t value)
4573 {
4574     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4575 }
4576 
4577 static const ARMCPRegInfo pan_reginfo = {
4578     .name = "PAN", .state = ARM_CP_STATE_AA64,
4579     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4580     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4581     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4582 };
4583 
4584 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4585 {
4586     return env->pstate & PSTATE_UAO;
4587 }
4588 
4589 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4590                            uint64_t value)
4591 {
4592     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4593 }
4594 
4595 static const ARMCPRegInfo uao_reginfo = {
4596     .name = "UAO", .state = ARM_CP_STATE_AA64,
4597     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4598     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4599     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4600 };
4601 
4602 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4603 {
4604     return env->pstate & PSTATE_DIT;
4605 }
4606 
4607 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4608                            uint64_t value)
4609 {
4610     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4611 }
4612 
4613 static const ARMCPRegInfo dit_reginfo = {
4614     .name = "DIT", .state = ARM_CP_STATE_AA64,
4615     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4616     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4617     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4618 };
4619 
4620 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4621 {
4622     return env->pstate & PSTATE_SSBS;
4623 }
4624 
4625 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4626                            uint64_t value)
4627 {
4628     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4629 }
4630 
4631 static const ARMCPRegInfo ssbs_reginfo = {
4632     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4633     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4634     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4635     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4636 };
4637 
4638 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4639                                               const ARMCPRegInfo *ri,
4640                                               bool isread)
4641 {
4642     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4643     switch (arm_current_el(env)) {
4644     case 0:
4645         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4646         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4647             return CP_ACCESS_TRAP;
4648         }
4649         /* fall through */
4650     case 1:
4651         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4652         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4653             return CP_ACCESS_TRAP_EL2;
4654         }
4655         break;
4656     }
4657     return CP_ACCESS_OK;
4658 }
4659 
4660 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4661 {
4662     /* Cache invalidate/clean to Point of Unification... */
4663     switch (arm_current_el(env)) {
4664     case 0:
4665         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4666         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4667             return CP_ACCESS_TRAP;
4668         }
4669         /* fall through */
4670     case 1:
4671         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4672         if (arm_hcr_el2_eff(env) & hcrflags) {
4673             return CP_ACCESS_TRAP_EL2;
4674         }
4675         break;
4676     }
4677     return CP_ACCESS_OK;
4678 }
4679 
4680 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4681                                    bool isread)
4682 {
4683     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4684 }
4685 
4686 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4687                                   bool isread)
4688 {
4689     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4690 }
4691 
4692 /*
4693  * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4694  * Page D4-1736 (DDI0487A.b)
4695  */
4696 
4697 static int vae1_tlbmask(CPUARMState *env)
4698 {
4699     uint64_t hcr = arm_hcr_el2_eff(env);
4700     uint16_t mask;
4701 
4702     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4703         mask = ARMMMUIdxBit_E20_2 |
4704                ARMMMUIdxBit_E20_2_PAN |
4705                ARMMMUIdxBit_E20_0;
4706     } else {
4707         mask = ARMMMUIdxBit_E10_1 |
4708                ARMMMUIdxBit_E10_1_PAN |
4709                ARMMMUIdxBit_E10_0;
4710     }
4711     return mask;
4712 }
4713 
4714 static int vae2_tlbmask(CPUARMState *env)
4715 {
4716     uint64_t hcr = arm_hcr_el2_eff(env);
4717     uint16_t mask;
4718 
4719     if (hcr & HCR_E2H) {
4720         mask = ARMMMUIdxBit_E20_2 |
4721                ARMMMUIdxBit_E20_2_PAN |
4722                ARMMMUIdxBit_E20_0;
4723     } else {
4724         mask = ARMMMUIdxBit_E2;
4725     }
4726     return mask;
4727 }
4728 
4729 /* Return 56 if TBI is enabled, 64 otherwise. */
4730 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4731                               uint64_t addr)
4732 {
4733     uint64_t tcr = regime_tcr(env, mmu_idx);
4734     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4735     int select = extract64(addr, 55, 1);
4736 
4737     return (tbi >> select) & 1 ? 56 : 64;
4738 }
4739 
4740 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4741 {
4742     uint64_t hcr = arm_hcr_el2_eff(env);
4743     ARMMMUIdx mmu_idx;
4744 
4745     /* Only the regime of the mmu_idx below is significant. */
4746     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4747         mmu_idx = ARMMMUIdx_E20_0;
4748     } else {
4749         mmu_idx = ARMMMUIdx_E10_0;
4750     }
4751 
4752     return tlbbits_for_regime(env, mmu_idx, addr);
4753 }
4754 
4755 static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
4756 {
4757     uint64_t hcr = arm_hcr_el2_eff(env);
4758     ARMMMUIdx mmu_idx;
4759 
4760     /*
4761      * Only the regime of the mmu_idx below is significant.
4762      * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
4763      * only has one.
4764      */
4765     if (hcr & HCR_E2H) {
4766         mmu_idx = ARMMMUIdx_E20_2;
4767     } else {
4768         mmu_idx = ARMMMUIdx_E2;
4769     }
4770 
4771     return tlbbits_for_regime(env, mmu_idx, addr);
4772 }
4773 
4774 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4775                                       uint64_t value)
4776 {
4777     CPUState *cs = env_cpu(env);
4778     int mask = vae1_tlbmask(env);
4779 
4780     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4781 }
4782 
4783 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4784                                     uint64_t value)
4785 {
4786     CPUState *cs = env_cpu(env);
4787     int mask = vae1_tlbmask(env);
4788 
4789     if (tlb_force_broadcast(env)) {
4790         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4791     } else {
4792         tlb_flush_by_mmuidx(cs, mask);
4793     }
4794 }
4795 
4796 static int e2_tlbmask(CPUARMState *env)
4797 {
4798     return (ARMMMUIdxBit_E20_0 |
4799             ARMMMUIdxBit_E20_2 |
4800             ARMMMUIdxBit_E20_2_PAN |
4801             ARMMMUIdxBit_E2);
4802 }
4803 
4804 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4805                                   uint64_t value)
4806 {
4807     CPUState *cs = env_cpu(env);
4808     int mask = alle1_tlbmask(env);
4809 
4810     tlb_flush_by_mmuidx(cs, mask);
4811 }
4812 
4813 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4814                                   uint64_t value)
4815 {
4816     CPUState *cs = env_cpu(env);
4817     int mask = e2_tlbmask(env);
4818 
4819     tlb_flush_by_mmuidx(cs, mask);
4820 }
4821 
4822 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4823                                   uint64_t value)
4824 {
4825     ARMCPU *cpu = env_archcpu(env);
4826     CPUState *cs = CPU(cpu);
4827 
4828     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4829 }
4830 
4831 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4832                                     uint64_t value)
4833 {
4834     CPUState *cs = env_cpu(env);
4835     int mask = alle1_tlbmask(env);
4836 
4837     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4838 }
4839 
4840 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4841                                     uint64_t value)
4842 {
4843     CPUState *cs = env_cpu(env);
4844     int mask = e2_tlbmask(env);
4845 
4846     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4847 }
4848 
4849 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4850                                     uint64_t value)
4851 {
4852     CPUState *cs = env_cpu(env);
4853 
4854     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4855 }
4856 
4857 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4858                                  uint64_t value)
4859 {
4860     /*
4861      * Invalidate by VA, EL2
4862      * Currently handles both VAE2 and VALE2, since we don't support
4863      * flush-last-level-only.
4864      */
4865     CPUState *cs = env_cpu(env);
4866     int mask = vae2_tlbmask(env);
4867     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4868     int bits = vae2_tlbbits(env, pageaddr);
4869 
4870     tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4871 }
4872 
4873 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4874                                  uint64_t value)
4875 {
4876     /*
4877      * Invalidate by VA, EL3
4878      * Currently handles both VAE3 and VALE3, since we don't support
4879      * flush-last-level-only.
4880      */
4881     ARMCPU *cpu = env_archcpu(env);
4882     CPUState *cs = CPU(cpu);
4883     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4884 
4885     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4886 }
4887 
4888 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4889                                    uint64_t value)
4890 {
4891     CPUState *cs = env_cpu(env);
4892     int mask = vae1_tlbmask(env);
4893     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4894     int bits = vae1_tlbbits(env, pageaddr);
4895 
4896     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4897 }
4898 
4899 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4900                                  uint64_t value)
4901 {
4902     /*
4903      * Invalidate by VA, EL1&0 (AArch64 version).
4904      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4905      * since we don't support flush-for-specific-ASID-only or
4906      * flush-last-level-only.
4907      */
4908     CPUState *cs = env_cpu(env);
4909     int mask = vae1_tlbmask(env);
4910     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4911     int bits = vae1_tlbbits(env, pageaddr);
4912 
4913     if (tlb_force_broadcast(env)) {
4914         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4915     } else {
4916         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4917     }
4918 }
4919 
4920 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4921                                    uint64_t value)
4922 {
4923     CPUState *cs = env_cpu(env);
4924     int mask = vae2_tlbmask(env);
4925     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4926     int bits = vae2_tlbbits(env, pageaddr);
4927 
4928     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4929 }
4930 
4931 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4932                                    uint64_t value)
4933 {
4934     CPUState *cs = env_cpu(env);
4935     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4936     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
4937 
4938     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4939                                                   ARMMMUIdxBit_E3, bits);
4940 }
4941 
4942 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4943 {
4944     /*
4945      * The MSB of value is the NS field, which only applies if SEL2
4946      * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4947      */
4948     return (value >= 0
4949             && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4950             && arm_is_secure_below_el3(env)
4951             ? ARMMMUIdxBit_Stage2_S
4952             : ARMMMUIdxBit_Stage2);
4953 }
4954 
4955 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4956                                     uint64_t value)
4957 {
4958     CPUState *cs = env_cpu(env);
4959     int mask = ipas2e1_tlbmask(env, value);
4960     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4961 
4962     if (tlb_force_broadcast(env)) {
4963         tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4964     } else {
4965         tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4966     }
4967 }
4968 
4969 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4970                                       uint64_t value)
4971 {
4972     CPUState *cs = env_cpu(env);
4973     int mask = ipas2e1_tlbmask(env, value);
4974     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4975 
4976     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4977 }
4978 
4979 #ifdef TARGET_AARCH64
4980 typedef struct {
4981     uint64_t base;
4982     uint64_t length;
4983 } TLBIRange;
4984 
4985 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4986 {
4987     /*
4988      * Note that the TLBI range TG field encoding differs from both
4989      * TG0 and TG1 encodings.
4990      */
4991     switch (tg) {
4992     case 1:
4993         return Gran4K;
4994     case 2:
4995         return Gran16K;
4996     case 3:
4997         return Gran64K;
4998     default:
4999         return GranInvalid;
5000     }
5001 }
5002 
5003 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
5004                                      uint64_t value)
5005 {
5006     unsigned int page_size_granule, page_shift, num, scale, exponent;
5007     /* Extract one bit to represent the va selector in use. */
5008     uint64_t select = sextract64(value, 36, 1);
5009     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
5010     TLBIRange ret = { };
5011     ARMGranuleSize gran;
5012 
5013     page_size_granule = extract64(value, 46, 2);
5014     gran = tlbi_range_tg_to_gran_size(page_size_granule);
5015 
5016     /* The granule encoded in value must match the granule in use. */
5017     if (gran != param.gran) {
5018         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
5019                       page_size_granule);
5020         return ret;
5021     }
5022 
5023     page_shift = arm_granule_bits(gran);
5024     num = extract64(value, 39, 5);
5025     scale = extract64(value, 44, 2);
5026     exponent = (5 * scale) + 1;
5027 
5028     ret.length = (num + 1) << (exponent + page_shift);
5029 
5030     if (param.select) {
5031         ret.base = sextract64(value, 0, 37);
5032     } else {
5033         ret.base = extract64(value, 0, 37);
5034     }
5035     if (param.ds) {
5036         /*
5037          * With DS=1, BaseADDR is always shifted 16 so that it is able
5038          * to address all 52 va bits.  The input address is perforce
5039          * aligned on a 64k boundary regardless of translation granule.
5040          */
5041         page_shift = 16;
5042     }
5043     ret.base <<= page_shift;
5044 
5045     return ret;
5046 }
5047 
5048 static void do_rvae_write(CPUARMState *env, uint64_t value,
5049                           int idxmap, bool synced)
5050 {
5051     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
5052     TLBIRange range;
5053     int bits;
5054 
5055     range = tlbi_aa64_get_range(env, one_idx, value);
5056     bits = tlbbits_for_regime(env, one_idx, range.base);
5057 
5058     if (synced) {
5059         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
5060                                                   range.base,
5061                                                   range.length,
5062                                                   idxmap,
5063                                                   bits);
5064     } else {
5065         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5066                                   range.length, idxmap, bits);
5067     }
5068 }
5069 
5070 static void tlbi_aa64_rvae1_write(CPUARMState *env,
5071                                   const ARMCPRegInfo *ri,
5072                                   uint64_t value)
5073 {
5074     /*
5075      * Invalidate by VA range, EL1&0.
5076      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5077      * since we don't support flush-for-specific-ASID-only or
5078      * flush-last-level-only.
5079      */
5080 
5081     do_rvae_write(env, value, vae1_tlbmask(env),
5082                   tlb_force_broadcast(env));
5083 }
5084 
5085 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5086                                     const ARMCPRegInfo *ri,
5087                                     uint64_t value)
5088 {
5089     /*
5090      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5091      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5092      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5093      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5094      * shareable specific flushes.
5095      */
5096 
5097     do_rvae_write(env, value, vae1_tlbmask(env), true);
5098 }
5099 
5100 static void tlbi_aa64_rvae2_write(CPUARMState *env,
5101                                   const ARMCPRegInfo *ri,
5102                                   uint64_t value)
5103 {
5104     /*
5105      * Invalidate by VA range, EL2.
5106      * Currently handles all of RVAE2 and RVALE2,
5107      * since we don't support flush-for-specific-ASID-only or
5108      * flush-last-level-only.
5109      */
5110 
5111     do_rvae_write(env, value, vae2_tlbmask(env),
5112                   tlb_force_broadcast(env));
5113 
5114 
5115 }
5116 
5117 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5118                                     const ARMCPRegInfo *ri,
5119                                     uint64_t value)
5120 {
5121     /*
5122      * Invalidate by VA range, Inner/Outer Shareable, EL2.
5123      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5124      * since we don't support flush-for-specific-ASID-only,
5125      * flush-last-level-only or inner/outer shareable specific flushes.
5126      */
5127 
5128     do_rvae_write(env, value, vae2_tlbmask(env), true);
5129 
5130 }
5131 
5132 static void tlbi_aa64_rvae3_write(CPUARMState *env,
5133                                   const ARMCPRegInfo *ri,
5134                                   uint64_t value)
5135 {
5136     /*
5137      * Invalidate by VA range, EL3.
5138      * Currently handles all of RVAE3 and RVALE3,
5139      * since we don't support flush-for-specific-ASID-only or
5140      * flush-last-level-only.
5141      */
5142 
5143     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5144 }
5145 
5146 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5147                                     const ARMCPRegInfo *ri,
5148                                     uint64_t value)
5149 {
5150     /*
5151      * Invalidate by VA range, EL3, Inner/Outer Shareable.
5152      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5153      * since we don't support flush-for-specific-ASID-only,
5154      * flush-last-level-only or inner/outer specific flushes.
5155      */
5156 
5157     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5158 }
5159 
5160 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5161                                      uint64_t value)
5162 {
5163     do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5164                   tlb_force_broadcast(env));
5165 }
5166 
5167 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5168                                        const ARMCPRegInfo *ri,
5169                                        uint64_t value)
5170 {
5171     do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5172 }
5173 #endif
5174 
5175 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5176                                       bool isread)
5177 {
5178     int cur_el = arm_current_el(env);
5179 
5180     if (cur_el < 2) {
5181         uint64_t hcr = arm_hcr_el2_eff(env);
5182 
5183         if (cur_el == 0) {
5184             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5185                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5186                     return CP_ACCESS_TRAP_EL2;
5187                 }
5188             } else {
5189                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5190                     return CP_ACCESS_TRAP;
5191                 }
5192                 if (hcr & HCR_TDZ) {
5193                     return CP_ACCESS_TRAP_EL2;
5194                 }
5195             }
5196         } else if (hcr & HCR_TDZ) {
5197             return CP_ACCESS_TRAP_EL2;
5198         }
5199     }
5200     return CP_ACCESS_OK;
5201 }
5202 
5203 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5204 {
5205     ARMCPU *cpu = env_archcpu(env);
5206     int dzp_bit = 1 << 4;
5207 
5208     /* DZP indicates whether DC ZVA access is allowed */
5209     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5210         dzp_bit = 0;
5211     }
5212     return cpu->dcz_blocksize | dzp_bit;
5213 }
5214 
5215 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5216                                     bool isread)
5217 {
5218     if (!(env->pstate & PSTATE_SP)) {
5219         /*
5220          * Access to SP_EL0 is undefined if it's being used as
5221          * the stack pointer.
5222          */
5223         return CP_ACCESS_TRAP_UNCATEGORIZED;
5224     }
5225     return CP_ACCESS_OK;
5226 }
5227 
5228 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5229 {
5230     return env->pstate & PSTATE_SP;
5231 }
5232 
5233 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5234 {
5235     update_spsel(env, val);
5236 }
5237 
5238 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5239                         uint64_t value)
5240 {
5241     ARMCPU *cpu = env_archcpu(env);
5242 
5243     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5244         /* M bit is RAZ/WI for PMSA with no MPU implemented */
5245         value &= ~SCTLR_M;
5246     }
5247 
5248     /* ??? Lots of these bits are not implemented.  */
5249 
5250     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5251         if (ri->opc1 == 6) { /* SCTLR_EL3 */
5252             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5253         } else {
5254             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5255                        SCTLR_ATA0 | SCTLR_ATA);
5256         }
5257     }
5258 
5259     if (raw_read(env, ri) == value) {
5260         /*
5261          * Skip the TLB flush if nothing actually changed; Linux likes
5262          * to do a lot of pointless SCTLR writes.
5263          */
5264         return;
5265     }
5266 
5267     raw_write(env, ri, value);
5268 
5269     /* This may enable/disable the MMU, so do a TLB flush.  */
5270     tlb_flush(CPU(cpu));
5271 
5272     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
5273         /*
5274          * Normally we would always end the TB on an SCTLR write; see the
5275          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5276          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5277          * of hflags from the translator, so do it here.
5278          */
5279         arm_rebuild_hflags(env);
5280     }
5281 }
5282 
5283 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5284                            uint64_t value)
5285 {
5286     /*
5287      * Some MDCR_EL3 bits affect whether PMU counters are running:
5288      * if we are trying to change any of those then we must
5289      * bracket this update with PMU start/finish calls.
5290      */
5291     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5292 
5293     if (pmu_op) {
5294         pmu_op_start(env);
5295     }
5296     env->cp15.mdcr_el3 = value;
5297     if (pmu_op) {
5298         pmu_op_finish(env);
5299     }
5300 }
5301 
5302 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5303                        uint64_t value)
5304 {
5305     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5306     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5307 }
5308 
5309 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5310                            uint64_t value)
5311 {
5312     /*
5313      * Some MDCR_EL2 bits affect whether PMU counters are running:
5314      * if we are trying to change any of those then we must
5315      * bracket this update with PMU start/finish calls.
5316      */
5317     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5318 
5319     if (pmu_op) {
5320         pmu_op_start(env);
5321     }
5322     env->cp15.mdcr_el2 = value;
5323     if (pmu_op) {
5324         pmu_op_finish(env);
5325     }
5326 }
5327 
5328 #ifdef CONFIG_USER_ONLY
5329 /*
5330  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5331  * code to get around W^X restrictions, where one region is writable and the
5332  * other is executable.
5333  *
5334  * Since the executable region is never written to we cannot detect code
5335  * changes when running in user mode, and rely on the emulated JIT telling us
5336  * that the code has changed by executing this instruction.
5337  */
5338 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5339                           uint64_t value)
5340 {
5341     uint64_t icache_line_mask, start_address, end_address;
5342     const ARMCPU *cpu;
5343 
5344     cpu = env_archcpu(env);
5345 
5346     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5347     start_address = value & ~icache_line_mask;
5348     end_address = value | icache_line_mask;
5349 
5350     mmap_lock();
5351 
5352     tb_invalidate_phys_range(start_address, end_address);
5353 
5354     mmap_unlock();
5355 }
5356 #endif
5357 
5358 static const ARMCPRegInfo v8_cp_reginfo[] = {
5359     /*
5360      * Minimal set of EL0-visible registers. This will need to be expanded
5361      * significantly for system emulation of AArch64 CPUs.
5362      */
5363     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5364       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5365       .access = PL0_RW, .type = ARM_CP_NZCV },
5366     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5367       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5368       .type = ARM_CP_NO_RAW,
5369       .access = PL0_RW, .accessfn = aa64_daif_access,
5370       .fieldoffset = offsetof(CPUARMState, daif),
5371       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5372     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5373       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5374       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5375       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5376     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5377       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5378       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5379       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5380     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5381       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5382       .access = PL0_R, .type = ARM_CP_NO_RAW,
5383       .fgt = FGT_DCZID_EL0,
5384       .readfn = aa64_dczid_read },
5385     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5386       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5387       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5388 #ifndef CONFIG_USER_ONLY
5389       /* Avoid overhead of an access check that always passes in user-mode */
5390       .accessfn = aa64_zva_access,
5391       .fgt = FGT_DCZVA,
5392 #endif
5393     },
5394     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5395       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5396       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5397     /*
5398      * Instruction cache ops. All of these except `IC IVAU` NOP because we
5399      * don't emulate caches.
5400      */
5401     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5402       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5403       .access = PL1_W, .type = ARM_CP_NOP,
5404       .fgt = FGT_ICIALLUIS,
5405       .accessfn = access_ticab },
5406     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5407       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5408       .access = PL1_W, .type = ARM_CP_NOP,
5409       .fgt = FGT_ICIALLU,
5410       .accessfn = access_tocu },
5411     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5412       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5413       .access = PL0_W,
5414       .fgt = FGT_ICIVAU,
5415       .accessfn = access_tocu,
5416 #ifdef CONFIG_USER_ONLY
5417       .type = ARM_CP_NO_RAW,
5418       .writefn = ic_ivau_write
5419 #else
5420       .type = ARM_CP_NOP
5421 #endif
5422     },
5423     /* Cache ops: all NOPs since we don't emulate caches */
5424     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5425       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5426       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5427       .fgt = FGT_DCIVAC,
5428       .type = ARM_CP_NOP },
5429     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5430       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5431       .fgt = FGT_DCISW,
5432       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5433     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5434       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5435       .access = PL0_W, .type = ARM_CP_NOP,
5436       .fgt = FGT_DCCVAC,
5437       .accessfn = aa64_cacheop_poc_access },
5438     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5439       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5440       .fgt = FGT_DCCSW,
5441       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5442     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5443       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5444       .access = PL0_W, .type = ARM_CP_NOP,
5445       .fgt = FGT_DCCVAU,
5446       .accessfn = access_tocu },
5447     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5448       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5449       .access = PL0_W, .type = ARM_CP_NOP,
5450       .fgt = FGT_DCCIVAC,
5451       .accessfn = aa64_cacheop_poc_access },
5452     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5453       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5454       .fgt = FGT_DCCISW,
5455       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5456     /* TLBI operations */
5457     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5458       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5459       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5460       .fgt = FGT_TLBIVMALLE1IS,
5461       .writefn = tlbi_aa64_vmalle1is_write },
5462     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5463       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5464       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5465       .fgt = FGT_TLBIVAE1IS,
5466       .writefn = tlbi_aa64_vae1is_write },
5467     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5468       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5469       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5470       .fgt = FGT_TLBIASIDE1IS,
5471       .writefn = tlbi_aa64_vmalle1is_write },
5472     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5473       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5474       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5475       .fgt = FGT_TLBIVAAE1IS,
5476       .writefn = tlbi_aa64_vae1is_write },
5477     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5478       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5479       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5480       .fgt = FGT_TLBIVALE1IS,
5481       .writefn = tlbi_aa64_vae1is_write },
5482     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5483       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5484       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5485       .fgt = FGT_TLBIVAALE1IS,
5486       .writefn = tlbi_aa64_vae1is_write },
5487     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5488       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5489       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5490       .fgt = FGT_TLBIVMALLE1,
5491       .writefn = tlbi_aa64_vmalle1_write },
5492     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5493       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5494       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5495       .fgt = FGT_TLBIVAE1,
5496       .writefn = tlbi_aa64_vae1_write },
5497     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5498       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5499       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5500       .fgt = FGT_TLBIASIDE1,
5501       .writefn = tlbi_aa64_vmalle1_write },
5502     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5503       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5504       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5505       .fgt = FGT_TLBIVAAE1,
5506       .writefn = tlbi_aa64_vae1_write },
5507     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5508       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5509       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5510       .fgt = FGT_TLBIVALE1,
5511       .writefn = tlbi_aa64_vae1_write },
5512     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5513       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5514       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5515       .fgt = FGT_TLBIVAALE1,
5516       .writefn = tlbi_aa64_vae1_write },
5517     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5518       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5519       .access = PL2_W, .type = ARM_CP_NO_RAW,
5520       .writefn = tlbi_aa64_ipas2e1is_write },
5521     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5522       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5523       .access = PL2_W, .type = ARM_CP_NO_RAW,
5524       .writefn = tlbi_aa64_ipas2e1is_write },
5525     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5526       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5527       .access = PL2_W, .type = ARM_CP_NO_RAW,
5528       .writefn = tlbi_aa64_alle1is_write },
5529     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5530       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5531       .access = PL2_W, .type = ARM_CP_NO_RAW,
5532       .writefn = tlbi_aa64_alle1is_write },
5533     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5534       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5535       .access = PL2_W, .type = ARM_CP_NO_RAW,
5536       .writefn = tlbi_aa64_ipas2e1_write },
5537     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5538       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5539       .access = PL2_W, .type = ARM_CP_NO_RAW,
5540       .writefn = tlbi_aa64_ipas2e1_write },
5541     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5542       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5543       .access = PL2_W, .type = ARM_CP_NO_RAW,
5544       .writefn = tlbi_aa64_alle1_write },
5545     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5546       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5547       .access = PL2_W, .type = ARM_CP_NO_RAW,
5548       .writefn = tlbi_aa64_alle1is_write },
5549 #ifndef CONFIG_USER_ONLY
5550     /* 64 bit address translation operations */
5551     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5552       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5553       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5554       .fgt = FGT_ATS1E1R,
5555       .accessfn = at_e012_access, .writefn = ats_write64 },
5556     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5557       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5558       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5559       .fgt = FGT_ATS1E1W,
5560       .accessfn = at_e012_access, .writefn = ats_write64 },
5561     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5562       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5563       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5564       .fgt = FGT_ATS1E0R,
5565       .accessfn = at_e012_access, .writefn = ats_write64 },
5566     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5567       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5568       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5569       .fgt = FGT_ATS1E0W,
5570       .accessfn = at_e012_access, .writefn = ats_write64 },
5571     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5572       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5573       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5574       .accessfn = at_e012_access, .writefn = ats_write64 },
5575     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5576       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5577       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5578       .accessfn = at_e012_access, .writefn = ats_write64 },
5579     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5580       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5581       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5582       .accessfn = at_e012_access, .writefn = ats_write64 },
5583     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5584       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5585       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5586       .accessfn = at_e012_access, .writefn = ats_write64 },
5587     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5588     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5589       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5590       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5591       .writefn = ats_write64 },
5592     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5593       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5594       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5595       .writefn = ats_write64 },
5596     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5597       .type = ARM_CP_ALIAS,
5598       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5599       .access = PL1_RW, .resetvalue = 0,
5600       .fgt = FGT_PAR_EL1,
5601       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5602       .writefn = par_write },
5603 #endif
5604     /* TLB invalidate last level of translation table walk */
5605     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5606       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5607       .writefn = tlbimva_is_write },
5608     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5609       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5610       .writefn = tlbimvaa_is_write },
5611     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5612       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5613       .writefn = tlbimva_write },
5614     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5615       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5616       .writefn = tlbimvaa_write },
5617     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5618       .type = ARM_CP_NO_RAW, .access = PL2_W,
5619       .writefn = tlbimva_hyp_write },
5620     { .name = "TLBIMVALHIS",
5621       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5622       .type = ARM_CP_NO_RAW, .access = PL2_W,
5623       .writefn = tlbimva_hyp_is_write },
5624     { .name = "TLBIIPAS2",
5625       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5626       .type = ARM_CP_NO_RAW, .access = PL2_W,
5627       .writefn = tlbiipas2_hyp_write },
5628     { .name = "TLBIIPAS2IS",
5629       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5630       .type = ARM_CP_NO_RAW, .access = PL2_W,
5631       .writefn = tlbiipas2is_hyp_write },
5632     { .name = "TLBIIPAS2L",
5633       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5634       .type = ARM_CP_NO_RAW, .access = PL2_W,
5635       .writefn = tlbiipas2_hyp_write },
5636     { .name = "TLBIIPAS2LIS",
5637       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5638       .type = ARM_CP_NO_RAW, .access = PL2_W,
5639       .writefn = tlbiipas2is_hyp_write },
5640     /* 32 bit cache operations */
5641     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5642       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5643     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5644       .type = ARM_CP_NOP, .access = PL1_W },
5645     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5646       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5647     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5648       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5649     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5650       .type = ARM_CP_NOP, .access = PL1_W },
5651     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5652       .type = ARM_CP_NOP, .access = PL1_W },
5653     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5654       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5655     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5656       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5657     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5658       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5659     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5660       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5661     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5662       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5663     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5664       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5665     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5666       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5667     /* MMU Domain access control / MPU write buffer control */
5668     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5669       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5670       .writefn = dacr_write, .raw_writefn = raw_write,
5671       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5672                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5673     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5674       .type = ARM_CP_ALIAS,
5675       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5676       .access = PL1_RW,
5677       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5678     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5679       .type = ARM_CP_ALIAS,
5680       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5681       .access = PL1_RW,
5682       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5683     /*
5684      * We rely on the access checks not allowing the guest to write to the
5685      * state field when SPSel indicates that it's being used as the stack
5686      * pointer.
5687      */
5688     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5689       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5690       .access = PL1_RW, .accessfn = sp_el0_access,
5691       .type = ARM_CP_ALIAS,
5692       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5693     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5694       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5695       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5696       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5697     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5698       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5699       .type = ARM_CP_NO_RAW,
5700       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5701     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5702       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5703       .access = PL2_RW,
5704       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5705       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5706     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5707       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5708       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5709       .writefn = dacr_write, .raw_writefn = raw_write,
5710       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5711     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5712       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5713       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5714       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5715     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5716       .type = ARM_CP_ALIAS,
5717       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5718       .access = PL2_RW,
5719       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5720     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5721       .type = ARM_CP_ALIAS,
5722       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5723       .access = PL2_RW,
5724       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5725     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5726       .type = ARM_CP_ALIAS,
5727       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5728       .access = PL2_RW,
5729       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5730     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5731       .type = ARM_CP_ALIAS,
5732       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5733       .access = PL2_RW,
5734       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5735     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5736       .type = ARM_CP_IO,
5737       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5738       .resetvalue = 0,
5739       .access = PL3_RW,
5740       .writefn = mdcr_el3_write,
5741       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5742     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5743       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5744       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5745       .writefn = sdcr_write,
5746       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5747 };
5748 
5749 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5750 {
5751     ARMCPU *cpu = env_archcpu(env);
5752 
5753     if (arm_feature(env, ARM_FEATURE_V8)) {
5754         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5755     } else {
5756         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5757     }
5758 
5759     if (arm_feature(env, ARM_FEATURE_EL3)) {
5760         valid_mask &= ~HCR_HCD;
5761     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5762         /*
5763          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5764          * However, if we're using the SMC PSCI conduit then QEMU is
5765          * effectively acting like EL3 firmware and so the guest at
5766          * EL2 should retain the ability to prevent EL1 from being
5767          * able to make SMC calls into the ersatz firmware, so in
5768          * that case HCR.TSC should be read/write.
5769          */
5770         valid_mask &= ~HCR_TSC;
5771     }
5772 
5773     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5774         if (cpu_isar_feature(aa64_vh, cpu)) {
5775             valid_mask |= HCR_E2H;
5776         }
5777         if (cpu_isar_feature(aa64_ras, cpu)) {
5778             valid_mask |= HCR_TERR | HCR_TEA;
5779         }
5780         if (cpu_isar_feature(aa64_lor, cpu)) {
5781             valid_mask |= HCR_TLOR;
5782         }
5783         if (cpu_isar_feature(aa64_pauth, cpu)) {
5784             valid_mask |= HCR_API | HCR_APK;
5785         }
5786         if (cpu_isar_feature(aa64_mte, cpu)) {
5787             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5788         }
5789         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5790             valid_mask |= HCR_ENSCXT;
5791         }
5792         if (cpu_isar_feature(aa64_fwb, cpu)) {
5793             valid_mask |= HCR_FWB;
5794         }
5795         if (cpu_isar_feature(aa64_rme, cpu)) {
5796             valid_mask |= HCR_GPF;
5797         }
5798     }
5799 
5800     if (cpu_isar_feature(any_evt, cpu)) {
5801         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5802     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5803         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5804     }
5805 
5806     /* Clear RES0 bits.  */
5807     value &= valid_mask;
5808 
5809     /*
5810      * These bits change the MMU setup:
5811      * HCR_VM enables stage 2 translation
5812      * HCR_PTW forbids certain page-table setups
5813      * HCR_DC disables stage1 and enables stage2 translation
5814      * HCR_DCT enables tagging on (disabled) stage1 translation
5815      * HCR_FWB changes the interpretation of stage2 descriptor bits
5816      */
5817     if ((env->cp15.hcr_el2 ^ value) &
5818         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5819         tlb_flush(CPU(cpu));
5820     }
5821     env->cp15.hcr_el2 = value;
5822 
5823     /*
5824      * Updates to VI and VF require us to update the status of
5825      * virtual interrupts, which are the logical OR of these bits
5826      * and the state of the input lines from the GIC. (This requires
5827      * that we have the iothread lock, which is done by marking the
5828      * reginfo structs as ARM_CP_IO.)
5829      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5830      * possible for it to be taken immediately, because VIRQ and
5831      * VFIQ are masked unless running at EL0 or EL1, and HCR
5832      * can only be written at EL2.
5833      */
5834     g_assert(qemu_mutex_iothread_locked());
5835     arm_cpu_update_virq(cpu);
5836     arm_cpu_update_vfiq(cpu);
5837     arm_cpu_update_vserr(cpu);
5838 }
5839 
5840 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5841 {
5842     do_hcr_write(env, value, 0);
5843 }
5844 
5845 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5846                           uint64_t value)
5847 {
5848     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5849     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5850     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5851 }
5852 
5853 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5854                          uint64_t value)
5855 {
5856     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5857     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5858     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5859 }
5860 
5861 /*
5862  * Return the effective value of HCR_EL2, at the given security state.
5863  * Bits that are not included here:
5864  * RW       (read from SCR_EL3.RW as needed)
5865  */
5866 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5867 {
5868     uint64_t ret = env->cp15.hcr_el2;
5869 
5870     assert(space != ARMSS_Root);
5871 
5872     if (!arm_is_el2_enabled_secstate(env, space)) {
5873         /*
5874          * "This register has no effect if EL2 is not enabled in the
5875          * current Security state".  This is ARMv8.4-SecEL2 speak for
5876          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5877          *
5878          * Prior to that, the language was "In an implementation that
5879          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5880          * as if this field is 0 for all purposes other than a direct
5881          * read or write access of HCR_EL2".  With lots of enumeration
5882          * on a per-field basis.  In current QEMU, this is condition
5883          * is arm_is_secure_below_el3.
5884          *
5885          * Since the v8.4 language applies to the entire register, and
5886          * appears to be backward compatible, use that.
5887          */
5888         return 0;
5889     }
5890 
5891     /*
5892      * For a cpu that supports both aarch64 and aarch32, we can set bits
5893      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5894      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5895      */
5896     if (!arm_el_is_aa64(env, 2)) {
5897         uint64_t aa32_valid;
5898 
5899         /*
5900          * These bits are up-to-date as of ARMv8.6.
5901          * For HCR, it's easiest to list just the 2 bits that are invalid.
5902          * For HCR2, list those that are valid.
5903          */
5904         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5905         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5906                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5907         ret &= aa32_valid;
5908     }
5909 
5910     if (ret & HCR_TGE) {
5911         /* These bits are up-to-date as of ARMv8.6.  */
5912         if (ret & HCR_E2H) {
5913             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5914                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5915                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5916                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5917                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5918                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5919         } else {
5920             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5921         }
5922         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5923                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5924                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5925                  HCR_TLOR);
5926     }
5927 
5928     return ret;
5929 }
5930 
5931 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5932 {
5933     if (arm_feature(env, ARM_FEATURE_M)) {
5934         return 0;
5935     }
5936     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
5937 }
5938 
5939 /*
5940  * Corresponds to ARM pseudocode function ELIsInHost().
5941  */
5942 bool el_is_in_host(CPUARMState *env, int el)
5943 {
5944     uint64_t mask;
5945 
5946     /*
5947      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5948      * Perform the simplest bit tests first, and validate EL2 afterward.
5949      */
5950     if (el & 1) {
5951         return false; /* EL1 or EL3 */
5952     }
5953 
5954     /*
5955      * Note that hcr_write() checks isar_feature_aa64_vh(),
5956      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5957      */
5958     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5959     if ((env->cp15.hcr_el2 & mask) != mask) {
5960         return false;
5961     }
5962 
5963     /* TGE and/or E2H set: double check those bits are currently legal. */
5964     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5965 }
5966 
5967 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5968                        uint64_t value)
5969 {
5970     uint64_t valid_mask = 0;
5971 
5972     /* FEAT_MOPS adds MSCEn and MCE2 */
5973     if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5974         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
5975     }
5976 
5977     /* Clear RES0 bits.  */
5978     env->cp15.hcrx_el2 = value & valid_mask;
5979 }
5980 
5981 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5982                                   bool isread)
5983 {
5984     if (arm_current_el(env) < 3
5985         && arm_feature(env, ARM_FEATURE_EL3)
5986         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5987         return CP_ACCESS_TRAP_EL3;
5988     }
5989     return CP_ACCESS_OK;
5990 }
5991 
5992 static const ARMCPRegInfo hcrx_el2_reginfo = {
5993     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5994     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5995     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5996     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5997 };
5998 
5999 /* Return the effective value of HCRX_EL2.  */
6000 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
6001 {
6002     /*
6003      * The bits in this register behave as 0 for all purposes other than
6004      * direct reads of the register if SCR_EL3.HXEn is 0.
6005      * If EL2 is not enabled in the current security state, then the
6006      * bit may behave as if 0, or as if 1, depending on the bit.
6007      * For the moment, we treat the EL2-disabled case as taking
6008      * priority over the HXEn-disabled case. This is true for the only
6009      * bit for a feature which we implement where the answer is different
6010      * for the two cases (MSCEn for FEAT_MOPS).
6011      * This may need to be revisited for future bits.
6012      */
6013     if (!arm_is_el2_enabled(env)) {
6014         uint64_t hcrx = 0;
6015         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6016             /* MSCEn behaves as 1 if EL2 is not enabled */
6017             hcrx |= HCRX_MSCEN;
6018         }
6019         return hcrx;
6020     }
6021     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
6022         return 0;
6023     }
6024     return env->cp15.hcrx_el2;
6025 }
6026 
6027 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6028                            uint64_t value)
6029 {
6030     /*
6031      * For A-profile AArch32 EL3, if NSACR.CP10
6032      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6033      */
6034     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6035         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6036         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6037         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
6038     }
6039     env->cp15.cptr_el[2] = value;
6040 }
6041 
6042 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6043 {
6044     /*
6045      * For A-profile AArch32 EL3, if NSACR.CP10
6046      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6047      */
6048     uint64_t value = env->cp15.cptr_el[2];
6049 
6050     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6051         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6052         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6053     }
6054     return value;
6055 }
6056 
6057 static const ARMCPRegInfo el2_cp_reginfo[] = {
6058     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
6059       .type = ARM_CP_IO,
6060       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6061       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6062       .writefn = hcr_write, .raw_writefn = raw_write },
6063     { .name = "HCR", .state = ARM_CP_STATE_AA32,
6064       .type = ARM_CP_ALIAS | ARM_CP_IO,
6065       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6066       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6067       .writefn = hcr_writelow },
6068     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6069       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6070       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6071     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
6072       .type = ARM_CP_ALIAS,
6073       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
6074       .access = PL2_RW,
6075       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
6076     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
6077       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6078       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
6079     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
6080       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6081       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
6082     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6083       .type = ARM_CP_ALIAS,
6084       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6085       .access = PL2_RW,
6086       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
6087     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
6088       .type = ARM_CP_ALIAS,
6089       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
6090       .access = PL2_RW,
6091       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
6092     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
6093       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6094       .access = PL2_RW, .writefn = vbar_write,
6095       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6096       .resetvalue = 0 },
6097     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6098       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
6099       .access = PL3_RW, .type = ARM_CP_ALIAS,
6100       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
6101     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6102       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6103       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
6104       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6105       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
6106     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6107       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6108       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6109       .resetvalue = 0 },
6110     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
6111       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
6112       .access = PL2_RW, .type = ARM_CP_ALIAS,
6113       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
6114     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6115       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6116       .access = PL2_RW, .type = ARM_CP_CONST,
6117       .resetvalue = 0 },
6118     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
6119     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
6120       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
6121       .access = PL2_RW, .type = ARM_CP_CONST,
6122       .resetvalue = 0 },
6123     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6124       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6125       .access = PL2_RW, .type = ARM_CP_CONST,
6126       .resetvalue = 0 },
6127     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6128       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6129       .access = PL2_RW, .type = ARM_CP_CONST,
6130       .resetvalue = 0 },
6131     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6132       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6133       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
6134       .raw_writefn = raw_write,
6135       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
6136     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6137       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6138       .type = ARM_CP_ALIAS,
6139       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6140       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
6141     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6142       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6143       .access = PL2_RW,
6144       /* no .writefn needed as this can't cause an ASID change */
6145       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
6146     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6147       .cp = 15, .opc1 = 6, .crm = 2,
6148       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6149       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6150       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
6151       .writefn = vttbr_write, .raw_writefn = raw_write },
6152     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6153       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
6154       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
6155       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
6156     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6157       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6158       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6159       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
6160     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6161       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6162       .access = PL2_RW, .resetvalue = 0,
6163       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
6164     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6165       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
6166       .access = PL2_RW, .resetvalue = 0,
6167       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
6168       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6169     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6170       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6171       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6172     { .name = "TLBIALLNSNH",
6173       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6174       .type = ARM_CP_NO_RAW, .access = PL2_W,
6175       .writefn = tlbiall_nsnh_write },
6176     { .name = "TLBIALLNSNHIS",
6177       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6178       .type = ARM_CP_NO_RAW, .access = PL2_W,
6179       .writefn = tlbiall_nsnh_is_write },
6180     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6181       .type = ARM_CP_NO_RAW, .access = PL2_W,
6182       .writefn = tlbiall_hyp_write },
6183     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6184       .type = ARM_CP_NO_RAW, .access = PL2_W,
6185       .writefn = tlbiall_hyp_is_write },
6186     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6187       .type = ARM_CP_NO_RAW, .access = PL2_W,
6188       .writefn = tlbimva_hyp_write },
6189     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6190       .type = ARM_CP_NO_RAW, .access = PL2_W,
6191       .writefn = tlbimva_hyp_is_write },
6192     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6193       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6194       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6195       .writefn = tlbi_aa64_alle2_write },
6196     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6197       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6198       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6199       .writefn = tlbi_aa64_vae2_write },
6200     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6201       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6202       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6203       .writefn = tlbi_aa64_vae2_write },
6204     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6205       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6206       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6207       .writefn = tlbi_aa64_alle2is_write },
6208     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6209       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6210       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6211       .writefn = tlbi_aa64_vae2is_write },
6212     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6213       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6214       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6215       .writefn = tlbi_aa64_vae2is_write },
6216 #ifndef CONFIG_USER_ONLY
6217     /*
6218      * Unlike the other EL2-related AT operations, these must
6219      * UNDEF from EL3 if EL2 is not implemented, which is why we
6220      * define them here rather than with the rest of the AT ops.
6221      */
6222     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6223       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6224       .access = PL2_W, .accessfn = at_s1e2_access,
6225       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6226       .writefn = ats_write64 },
6227     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6228       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6229       .access = PL2_W, .accessfn = at_s1e2_access,
6230       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6231       .writefn = ats_write64 },
6232     /*
6233      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
6234      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6235      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6236      * to behave as if SCR.NS was 1.
6237      */
6238     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6239       .access = PL2_W,
6240       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6241     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6242       .access = PL2_W,
6243       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6244     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6245       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6246       /*
6247        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6248        * reset values as IMPDEF. We choose to reset to 3 to comply with
6249        * both ARMv7 and ARMv8.
6250        */
6251       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6252       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
6253       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6254     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6255       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6256       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6257       .writefn = gt_cntvoff_write,
6258       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6259     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6260       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6261       .writefn = gt_cntvoff_write,
6262       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6263     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6264       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6265       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6266       .type = ARM_CP_IO, .access = PL2_RW,
6267       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6268     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6269       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6270       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6271       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6272     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6273       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6274       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6275       .resetfn = gt_hyp_timer_reset,
6276       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6277     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6278       .type = ARM_CP_IO,
6279       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6280       .access = PL2_RW,
6281       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6282       .resetvalue = 0,
6283       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6284 #endif
6285     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6286       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6287       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6288       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6289     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6290       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6291       .access = PL2_RW,
6292       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6293     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6294       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6295       .access = PL2_RW,
6296       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6297 };
6298 
6299 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6300     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6301       .type = ARM_CP_ALIAS | ARM_CP_IO,
6302       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6303       .access = PL2_RW,
6304       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6305       .writefn = hcr_writehigh },
6306 };
6307 
6308 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6309                                   bool isread)
6310 {
6311     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6312         return CP_ACCESS_OK;
6313     }
6314     return CP_ACCESS_TRAP_UNCATEGORIZED;
6315 }
6316 
6317 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6318     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6319       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6320       .access = PL2_RW, .accessfn = sel2_access,
6321       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6322     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6323       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6324       .access = PL2_RW, .accessfn = sel2_access,
6325       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6326 };
6327 
6328 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6329                                    bool isread)
6330 {
6331     /*
6332      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6333      * At Secure EL1 it traps to EL3 or EL2.
6334      */
6335     if (arm_current_el(env) == 3) {
6336         return CP_ACCESS_OK;
6337     }
6338     if (arm_is_secure_below_el3(env)) {
6339         if (env->cp15.scr_el3 & SCR_EEL2) {
6340             return CP_ACCESS_TRAP_EL2;
6341         }
6342         return CP_ACCESS_TRAP_EL3;
6343     }
6344     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6345     if (isread) {
6346         return CP_ACCESS_OK;
6347     }
6348     return CP_ACCESS_TRAP_UNCATEGORIZED;
6349 }
6350 
6351 static const ARMCPRegInfo el3_cp_reginfo[] = {
6352     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6353       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6354       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6355       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
6356     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6357       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6358       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6359       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6360       .writefn = scr_write, .raw_writefn = raw_write },
6361     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6362       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6363       .access = PL3_RW, .resetvalue = 0,
6364       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6365     { .name = "SDER",
6366       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6367       .access = PL3_RW, .resetvalue = 0,
6368       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6369     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6370       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6371       .writefn = vbar_write, .resetvalue = 0,
6372       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6373     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6374       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6375       .access = PL3_RW, .resetvalue = 0,
6376       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6377     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6378       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6379       .access = PL3_RW,
6380       /* no .writefn needed as this can't cause an ASID change */
6381       .resetvalue = 0,
6382       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6383     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6384       .type = ARM_CP_ALIAS,
6385       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6386       .access = PL3_RW,
6387       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6388     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6389       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6390       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6391     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6392       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6393       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6394     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6395       .type = ARM_CP_ALIAS,
6396       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6397       .access = PL3_RW,
6398       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6399     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6400       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6401       .access = PL3_RW, .writefn = vbar_write,
6402       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6403       .resetvalue = 0 },
6404     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6405       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6406       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6407       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6408     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6409       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6410       .access = PL3_RW, .resetvalue = 0,
6411       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6412     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6413       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6414       .access = PL3_RW, .type = ARM_CP_CONST,
6415       .resetvalue = 0 },
6416     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6417       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6418       .access = PL3_RW, .type = ARM_CP_CONST,
6419       .resetvalue = 0 },
6420     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6421       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6422       .access = PL3_RW, .type = ARM_CP_CONST,
6423       .resetvalue = 0 },
6424     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6425       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6426       .access = PL3_W, .type = ARM_CP_NO_RAW,
6427       .writefn = tlbi_aa64_alle3is_write },
6428     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6429       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6430       .access = PL3_W, .type = ARM_CP_NO_RAW,
6431       .writefn = tlbi_aa64_vae3is_write },
6432     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6433       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6434       .access = PL3_W, .type = ARM_CP_NO_RAW,
6435       .writefn = tlbi_aa64_vae3is_write },
6436     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6437       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6438       .access = PL3_W, .type = ARM_CP_NO_RAW,
6439       .writefn = tlbi_aa64_alle3_write },
6440     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6441       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6442       .access = PL3_W, .type = ARM_CP_NO_RAW,
6443       .writefn = tlbi_aa64_vae3_write },
6444     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6445       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6446       .access = PL3_W, .type = ARM_CP_NO_RAW,
6447       .writefn = tlbi_aa64_vae3_write },
6448 };
6449 
6450 #ifndef CONFIG_USER_ONLY
6451 /* Test if system register redirection is to occur in the current state.  */
6452 static bool redirect_for_e2h(CPUARMState *env)
6453 {
6454     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6455 }
6456 
6457 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6458 {
6459     CPReadFn *readfn;
6460 
6461     if (redirect_for_e2h(env)) {
6462         /* Switch to the saved EL2 version of the register.  */
6463         ri = ri->opaque;
6464         readfn = ri->readfn;
6465     } else {
6466         readfn = ri->orig_readfn;
6467     }
6468     if (readfn == NULL) {
6469         readfn = raw_read;
6470     }
6471     return readfn(env, ri);
6472 }
6473 
6474 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6475                           uint64_t value)
6476 {
6477     CPWriteFn *writefn;
6478 
6479     if (redirect_for_e2h(env)) {
6480         /* Switch to the saved EL2 version of the register.  */
6481         ri = ri->opaque;
6482         writefn = ri->writefn;
6483     } else {
6484         writefn = ri->orig_writefn;
6485     }
6486     if (writefn == NULL) {
6487         writefn = raw_write;
6488     }
6489     writefn(env, ri, value);
6490 }
6491 
6492 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6493 {
6494     struct E2HAlias {
6495         uint32_t src_key, dst_key, new_key;
6496         const char *src_name, *dst_name, *new_name;
6497         bool (*feature)(const ARMISARegisters *id);
6498     };
6499 
6500 #define K(op0, op1, crn, crm, op2) \
6501     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6502 
6503     static const struct E2HAlias aliases[] = {
6504         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6505           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6506         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6507           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6508         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6509           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6510         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6511           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6512         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6513           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6514         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6515           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6516         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6517           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6518         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6519           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6520         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6521           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6522         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6523           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6524         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6525           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6526         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6527           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6528         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6529           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6530         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6531           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6532         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6533           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6534         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6535           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6536 
6537         /*
6538          * Note that redirection of ZCR is mentioned in the description
6539          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6540          * not in the summary table.
6541          */
6542         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6543           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6544         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6545           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6546 
6547         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6548           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6549 
6550         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6551           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6552           isar_feature_aa64_scxtnum },
6553 
6554         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6555         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6556     };
6557 #undef K
6558 
6559     size_t i;
6560 
6561     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6562         const struct E2HAlias *a = &aliases[i];
6563         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6564         bool ok;
6565 
6566         if (a->feature && !a->feature(&cpu->isar)) {
6567             continue;
6568         }
6569 
6570         src_reg = g_hash_table_lookup(cpu->cp_regs,
6571                                       (gpointer)(uintptr_t)a->src_key);
6572         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6573                                       (gpointer)(uintptr_t)a->dst_key);
6574         g_assert(src_reg != NULL);
6575         g_assert(dst_reg != NULL);
6576 
6577         /* Cross-compare names to detect typos in the keys.  */
6578         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6579         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6580 
6581         /* None of the core system registers use opaque; we will.  */
6582         g_assert(src_reg->opaque == NULL);
6583 
6584         /* Create alias before redirection so we dup the right data. */
6585         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6586 
6587         new_reg->name = a->new_name;
6588         new_reg->type |= ARM_CP_ALIAS;
6589         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6590         new_reg->access &= PL2_RW | PL3_RW;
6591 
6592         ok = g_hash_table_insert(cpu->cp_regs,
6593                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6594         g_assert(ok);
6595 
6596         src_reg->opaque = dst_reg;
6597         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6598         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6599         if (!src_reg->raw_readfn) {
6600             src_reg->raw_readfn = raw_read;
6601         }
6602         if (!src_reg->raw_writefn) {
6603             src_reg->raw_writefn = raw_write;
6604         }
6605         src_reg->readfn = el2_e2h_read;
6606         src_reg->writefn = el2_e2h_write;
6607     }
6608 }
6609 #endif
6610 
6611 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6612                                      bool isread)
6613 {
6614     int cur_el = arm_current_el(env);
6615 
6616     if (cur_el < 2) {
6617         uint64_t hcr = arm_hcr_el2_eff(env);
6618 
6619         if (cur_el == 0) {
6620             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6621                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6622                     return CP_ACCESS_TRAP_EL2;
6623                 }
6624             } else {
6625                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6626                     return CP_ACCESS_TRAP;
6627                 }
6628                 if (hcr & HCR_TID2) {
6629                     return CP_ACCESS_TRAP_EL2;
6630                 }
6631             }
6632         } else if (hcr & HCR_TID2) {
6633             return CP_ACCESS_TRAP_EL2;
6634         }
6635     }
6636 
6637     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6638         return CP_ACCESS_TRAP_EL2;
6639     }
6640 
6641     return CP_ACCESS_OK;
6642 }
6643 
6644 /*
6645  * Check for traps to RAS registers, which are controlled
6646  * by HCR_EL2.TERR and SCR_EL3.TERR.
6647  */
6648 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6649                                   bool isread)
6650 {
6651     int el = arm_current_el(env);
6652 
6653     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6654         return CP_ACCESS_TRAP_EL2;
6655     }
6656     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6657         return CP_ACCESS_TRAP_EL3;
6658     }
6659     return CP_ACCESS_OK;
6660 }
6661 
6662 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6663 {
6664     int el = arm_current_el(env);
6665 
6666     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6667         return env->cp15.vdisr_el2;
6668     }
6669     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6670         return 0; /* RAZ/WI */
6671     }
6672     return env->cp15.disr_el1;
6673 }
6674 
6675 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6676 {
6677     int el = arm_current_el(env);
6678 
6679     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6680         env->cp15.vdisr_el2 = val;
6681         return;
6682     }
6683     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6684         return; /* RAZ/WI */
6685     }
6686     env->cp15.disr_el1 = val;
6687 }
6688 
6689 /*
6690  * Minimal RAS implementation with no Error Records.
6691  * Which means that all of the Error Record registers:
6692  *   ERXADDR_EL1
6693  *   ERXCTLR_EL1
6694  *   ERXFR_EL1
6695  *   ERXMISC0_EL1
6696  *   ERXMISC1_EL1
6697  *   ERXMISC2_EL1
6698  *   ERXMISC3_EL1
6699  *   ERXPFGCDN_EL1  (RASv1p1)
6700  *   ERXPFGCTL_EL1  (RASv1p1)
6701  *   ERXPFGF_EL1    (RASv1p1)
6702  *   ERXSTATUS_EL1
6703  * and
6704  *   ERRSELR_EL1
6705  * may generate UNDEFINED, which is the effect we get by not
6706  * listing them at all.
6707  *
6708  * These registers have fine-grained trap bits, but UNDEF-to-EL1
6709  * is higher priority than FGT-to-EL2 so we do not need to list them
6710  * in order to check for an FGT.
6711  */
6712 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6713     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6714       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6715       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6716       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6717     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6718       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6719       .access = PL1_R, .accessfn = access_terr,
6720       .fgt = FGT_ERRIDR_EL1,
6721       .type = ARM_CP_CONST, .resetvalue = 0 },
6722     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6723       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6724       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6725     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6726       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6727       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6728 };
6729 
6730 /*
6731  * Return the exception level to which exceptions should be taken
6732  * via SVEAccessTrap.  This excludes the check for whether the exception
6733  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6734  * be found by testing 0 < fp_exception_el < sve_exception_el.
6735  *
6736  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6737  * pseudocode does *not* separate out the FP trap checks, but has them
6738  * all in one function.
6739  */
6740 int sve_exception_el(CPUARMState *env, int el)
6741 {
6742 #ifndef CONFIG_USER_ONLY
6743     if (el <= 1 && !el_is_in_host(env, el)) {
6744         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6745         case 1:
6746             if (el != 0) {
6747                 break;
6748             }
6749             /* fall through */
6750         case 0:
6751         case 2:
6752             return 1;
6753         }
6754     }
6755 
6756     if (el <= 2 && arm_is_el2_enabled(env)) {
6757         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6758         if (env->cp15.hcr_el2 & HCR_E2H) {
6759             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6760             case 1:
6761                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6762                     break;
6763                 }
6764                 /* fall through */
6765             case 0:
6766             case 2:
6767                 return 2;
6768             }
6769         } else {
6770             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6771                 return 2;
6772             }
6773         }
6774     }
6775 
6776     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6777     if (arm_feature(env, ARM_FEATURE_EL3)
6778         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6779         return 3;
6780     }
6781 #endif
6782     return 0;
6783 }
6784 
6785 /*
6786  * Return the exception level to which exceptions should be taken for SME.
6787  * C.f. the ARM pseudocode function CheckSMEAccess.
6788  */
6789 int sme_exception_el(CPUARMState *env, int el)
6790 {
6791 #ifndef CONFIG_USER_ONLY
6792     if (el <= 1 && !el_is_in_host(env, el)) {
6793         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6794         case 1:
6795             if (el != 0) {
6796                 break;
6797             }
6798             /* fall through */
6799         case 0:
6800         case 2:
6801             return 1;
6802         }
6803     }
6804 
6805     if (el <= 2 && arm_is_el2_enabled(env)) {
6806         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6807         if (env->cp15.hcr_el2 & HCR_E2H) {
6808             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6809             case 1:
6810                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6811                     break;
6812                 }
6813                 /* fall through */
6814             case 0:
6815             case 2:
6816                 return 2;
6817             }
6818         } else {
6819             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6820                 return 2;
6821             }
6822         }
6823     }
6824 
6825     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6826     if (arm_feature(env, ARM_FEATURE_EL3)
6827         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6828         return 3;
6829     }
6830 #endif
6831     return 0;
6832 }
6833 
6834 /*
6835  * Given that SVE is enabled, return the vector length for EL.
6836  */
6837 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6838 {
6839     ARMCPU *cpu = env_archcpu(env);
6840     uint64_t *cr = env->vfp.zcr_el;
6841     uint32_t map = cpu->sve_vq.map;
6842     uint32_t len = ARM_MAX_VQ - 1;
6843 
6844     if (sm) {
6845         cr = env->vfp.smcr_el;
6846         map = cpu->sme_vq.map;
6847     }
6848 
6849     if (el <= 1 && !el_is_in_host(env, el)) {
6850         len = MIN(len, 0xf & (uint32_t)cr[1]);
6851     }
6852     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6853         len = MIN(len, 0xf & (uint32_t)cr[2]);
6854     }
6855     if (arm_feature(env, ARM_FEATURE_EL3)) {
6856         len = MIN(len, 0xf & (uint32_t)cr[3]);
6857     }
6858 
6859     map &= MAKE_64BIT_MASK(0, len + 1);
6860     if (map != 0) {
6861         return 31 - clz32(map);
6862     }
6863 
6864     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6865     assert(sm);
6866     return ctz32(cpu->sme_vq.map);
6867 }
6868 
6869 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6870 {
6871     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6872 }
6873 
6874 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6875                       uint64_t value)
6876 {
6877     int cur_el = arm_current_el(env);
6878     int old_len = sve_vqm1_for_el(env, cur_el);
6879     int new_len;
6880 
6881     /* Bits other than [3:0] are RAZ/WI.  */
6882     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6883     raw_write(env, ri, value & 0xf);
6884 
6885     /*
6886      * Because we arrived here, we know both FP and SVE are enabled;
6887      * otherwise we would have trapped access to the ZCR_ELn register.
6888      */
6889     new_len = sve_vqm1_for_el(env, cur_el);
6890     if (new_len < old_len) {
6891         aarch64_sve_narrow_vq(env, new_len + 1);
6892     }
6893 }
6894 
6895 static const ARMCPRegInfo zcr_reginfo[] = {
6896     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6897       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6898       .access = PL1_RW, .type = ARM_CP_SVE,
6899       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6900       .writefn = zcr_write, .raw_writefn = raw_write },
6901     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6902       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6903       .access = PL2_RW, .type = ARM_CP_SVE,
6904       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6905       .writefn = zcr_write, .raw_writefn = raw_write },
6906     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6907       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6908       .access = PL3_RW, .type = ARM_CP_SVE,
6909       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6910       .writefn = zcr_write, .raw_writefn = raw_write },
6911 };
6912 
6913 #ifdef TARGET_AARCH64
6914 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6915                                     bool isread)
6916 {
6917     int el = arm_current_el(env);
6918 
6919     if (el == 0) {
6920         uint64_t sctlr = arm_sctlr(env, el);
6921         if (!(sctlr & SCTLR_EnTP2)) {
6922             return CP_ACCESS_TRAP;
6923         }
6924     }
6925     /* TODO: FEAT_FGT */
6926     if (el < 3
6927         && arm_feature(env, ARM_FEATURE_EL3)
6928         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6929         return CP_ACCESS_TRAP_EL3;
6930     }
6931     return CP_ACCESS_OK;
6932 }
6933 
6934 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6935                                  bool isread)
6936 {
6937     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6938     if (arm_current_el(env) < 3
6939         && arm_feature(env, ARM_FEATURE_EL3)
6940         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6941         return CP_ACCESS_TRAP_EL3;
6942     }
6943     return CP_ACCESS_OK;
6944 }
6945 
6946 /* ResetSVEState */
6947 static void arm_reset_sve_state(CPUARMState *env)
6948 {
6949     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6950     /* Recall that FFR is stored as pregs[16]. */
6951     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6952     vfp_set_fpcr(env, 0x0800009f);
6953 }
6954 
6955 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6956 {
6957     uint64_t change = (env->svcr ^ new) & mask;
6958 
6959     if (change == 0) {
6960         return;
6961     }
6962     env->svcr ^= change;
6963 
6964     if (change & R_SVCR_SM_MASK) {
6965         arm_reset_sve_state(env);
6966     }
6967 
6968     /*
6969      * ResetSMEState.
6970      *
6971      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
6972      * on enable: while disabled, the storage is inaccessible and the
6973      * value does not matter.  We're not saving the storage in vmstate
6974      * when disabled either.
6975      */
6976     if (change & new & R_SVCR_ZA_MASK) {
6977         memset(env->zarray, 0, sizeof(env->zarray));
6978     }
6979 
6980     if (tcg_enabled()) {
6981         arm_rebuild_hflags(env);
6982     }
6983 }
6984 
6985 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6986                        uint64_t value)
6987 {
6988     aarch64_set_svcr(env, value, -1);
6989 }
6990 
6991 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6992                        uint64_t value)
6993 {
6994     int cur_el = arm_current_el(env);
6995     int old_len = sve_vqm1_for_el(env, cur_el);
6996     int new_len;
6997 
6998     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6999     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
7000     raw_write(env, ri, value);
7001 
7002     /*
7003      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
7004      * when SVL is widened (old values kept, or zeros).  Choose to keep the
7005      * current values for simplicity.  But for QEMU internals, we must still
7006      * apply the narrower SVL to the Zregs and Pregs -- see the comment
7007      * above aarch64_sve_narrow_vq.
7008      */
7009     new_len = sve_vqm1_for_el(env, cur_el);
7010     if (new_len < old_len) {
7011         aarch64_sve_narrow_vq(env, new_len + 1);
7012     }
7013 }
7014 
7015 static const ARMCPRegInfo sme_reginfo[] = {
7016     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7017       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7018       .access = PL0_RW, .accessfn = access_tpidr2,
7019       .fgt = FGT_NTPIDR2_EL0,
7020       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
7021     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7022       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7023       .access = PL0_RW, .type = ARM_CP_SME,
7024       .fieldoffset = offsetof(CPUARMState, svcr),
7025       .writefn = svcr_write, .raw_writefn = raw_write },
7026     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7027       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7028       .access = PL1_RW, .type = ARM_CP_SME,
7029       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7030       .writefn = smcr_write, .raw_writefn = raw_write },
7031     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7032       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7033       .access = PL2_RW, .type = ARM_CP_SME,
7034       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7035       .writefn = smcr_write, .raw_writefn = raw_write },
7036     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7037       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7038       .access = PL3_RW, .type = ARM_CP_SME,
7039       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7040       .writefn = smcr_write, .raw_writefn = raw_write },
7041     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7042       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7043       .access = PL1_R, .accessfn = access_aa64_tid1,
7044       /*
7045        * IMPLEMENTOR = 0 (software)
7046        * REVISION    = 0 (implementation defined)
7047        * SMPS        = 0 (no streaming execution priority in QEMU)
7048        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
7049        */
7050       .type = ARM_CP_CONST, .resetvalue = 0, },
7051     /*
7052      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7053      */
7054     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7055       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7056       .access = PL1_RW, .accessfn = access_esm,
7057       .fgt = FGT_NSMPRI_EL1,
7058       .type = ARM_CP_CONST, .resetvalue = 0 },
7059     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7060       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7061       .access = PL2_RW, .accessfn = access_esm,
7062       .type = ARM_CP_CONST, .resetvalue = 0 },
7063 };
7064 
7065 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7066                                   uint64_t value)
7067 {
7068     CPUState *cs = env_cpu(env);
7069 
7070     tlb_flush(cs);
7071 }
7072 
7073 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7074                         uint64_t value)
7075 {
7076     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7077     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7078         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7079         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7080 
7081     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7082 }
7083 
7084 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7085 {
7086     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7087                                      env_archcpu(env)->reset_l0gptsz);
7088 }
7089 
7090 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7091                                     uint64_t value)
7092 {
7093     CPUState *cs = env_cpu(env);
7094 
7095     tlb_flush_all_cpus_synced(cs);
7096 }
7097 
7098 static const ARMCPRegInfo rme_reginfo[] = {
7099     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7100       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7101       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7102       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7103     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7104       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7105       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7106     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7107       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7108       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7109     { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7110       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7111       .access = PL3_W, .type = ARM_CP_NO_RAW,
7112       .writefn = tlbi_aa64_paall_write },
7113     { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7114       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7115       .access = PL3_W, .type = ARM_CP_NO_RAW,
7116       .writefn = tlbi_aa64_paallos_write },
7117     /*
7118      * QEMU does not have a way to invalidate by physical address, thus
7119      * invalidating a range of physical addresses is accomplished by
7120      * flushing all tlb entries in the outer shareable domain,
7121      * just like PAALLOS.
7122      */
7123     { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7124       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7125       .access = PL3_W, .type = ARM_CP_NO_RAW,
7126       .writefn = tlbi_aa64_paallos_write },
7127     { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7128       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7129       .access = PL3_W, .type = ARM_CP_NO_RAW,
7130       .writefn = tlbi_aa64_paallos_write },
7131     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7132       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7133       .access = PL3_W, .type = ARM_CP_NOP },
7134 };
7135 
7136 static const ARMCPRegInfo rme_mte_reginfo[] = {
7137     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7138       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7139       .access = PL3_W, .type = ARM_CP_NOP },
7140 };
7141 #endif /* TARGET_AARCH64 */
7142 
7143 static void define_pmu_regs(ARMCPU *cpu)
7144 {
7145     /*
7146      * v7 performance monitor control register: same implementor
7147      * field as main ID register, and we implement four counters in
7148      * addition to the cycle count register.
7149      */
7150     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
7151     ARMCPRegInfo pmcr = {
7152         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7153         .access = PL0_RW,
7154         .fgt = FGT_PMCR_EL0,
7155         .type = ARM_CP_IO | ARM_CP_ALIAS,
7156         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7157         .accessfn = pmreg_access, .writefn = pmcr_write,
7158         .raw_writefn = raw_write,
7159     };
7160     ARMCPRegInfo pmcr64 = {
7161         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7162         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7163         .access = PL0_RW, .accessfn = pmreg_access,
7164         .fgt = FGT_PMCR_EL0,
7165         .type = ARM_CP_IO,
7166         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
7167         .resetvalue = cpu->isar.reset_pmcr_el0,
7168         .writefn = pmcr_write, .raw_writefn = raw_write,
7169     };
7170 
7171     define_one_arm_cp_reg(cpu, &pmcr);
7172     define_one_arm_cp_reg(cpu, &pmcr64);
7173     for (i = 0; i < pmcrn; i++) {
7174         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7175         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7176         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7177         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7178         ARMCPRegInfo pmev_regs[] = {
7179             { .name = pmevcntr_name, .cp = 15, .crn = 14,
7180               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7181               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7182               .fgt = FGT_PMEVCNTRN_EL0,
7183               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7184               .accessfn = pmreg_access_xevcntr },
7185             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7186               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
7187               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
7188               .type = ARM_CP_IO,
7189               .fgt = FGT_PMEVCNTRN_EL0,
7190               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7191               .raw_readfn = pmevcntr_rawread,
7192               .raw_writefn = pmevcntr_rawwrite },
7193             { .name = pmevtyper_name, .cp = 15, .crn = 14,
7194               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7195               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7196               .fgt = FGT_PMEVTYPERN_EL0,
7197               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7198               .accessfn = pmreg_access },
7199             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7200               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7201               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
7202               .fgt = FGT_PMEVTYPERN_EL0,
7203               .type = ARM_CP_IO,
7204               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7205               .raw_writefn = pmevtyper_rawwrite },
7206         };
7207         define_arm_cp_regs(cpu, pmev_regs);
7208         g_free(pmevcntr_name);
7209         g_free(pmevcntr_el0_name);
7210         g_free(pmevtyper_name);
7211         g_free(pmevtyper_el0_name);
7212     }
7213     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
7214         ARMCPRegInfo v81_pmu_regs[] = {
7215             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7216               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7217               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7218               .fgt = FGT_PMCEIDN_EL0,
7219               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7220             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7221               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7222               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7223               .fgt = FGT_PMCEIDN_EL0,
7224               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
7225         };
7226         define_arm_cp_regs(cpu, v81_pmu_regs);
7227     }
7228     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
7229         static const ARMCPRegInfo v84_pmmir = {
7230             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7231             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7232             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7233             .fgt = FGT_PMMIR_EL1,
7234             .resetvalue = 0
7235         };
7236         define_one_arm_cp_reg(cpu, &v84_pmmir);
7237     }
7238 }
7239 
7240 #ifndef CONFIG_USER_ONLY
7241 /*
7242  * We don't know until after realize whether there's a GICv3
7243  * attached, and that is what registers the gicv3 sysregs.
7244  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7245  * at runtime.
7246  */
7247 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7248 {
7249     ARMCPU *cpu = env_archcpu(env);
7250     uint64_t pfr1 = cpu->isar.id_pfr1;
7251 
7252     if (env->gicv3state) {
7253         pfr1 |= 1 << 28;
7254     }
7255     return pfr1;
7256 }
7257 
7258 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7259 {
7260     ARMCPU *cpu = env_archcpu(env);
7261     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
7262 
7263     if (env->gicv3state) {
7264         pfr0 |= 1 << 24;
7265     }
7266     return pfr0;
7267 }
7268 #endif
7269 
7270 /*
7271  * Shared logic between LORID and the rest of the LOR* registers.
7272  * Secure state exclusion has already been dealt with.
7273  */
7274 static CPAccessResult access_lor_ns(CPUARMState *env,
7275                                     const ARMCPRegInfo *ri, bool isread)
7276 {
7277     int el = arm_current_el(env);
7278 
7279     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7280         return CP_ACCESS_TRAP_EL2;
7281     }
7282     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7283         return CP_ACCESS_TRAP_EL3;
7284     }
7285     return CP_ACCESS_OK;
7286 }
7287 
7288 static CPAccessResult access_lor_other(CPUARMState *env,
7289                                        const ARMCPRegInfo *ri, bool isread)
7290 {
7291     if (arm_is_secure_below_el3(env)) {
7292         /* Access denied in secure mode.  */
7293         return CP_ACCESS_TRAP;
7294     }
7295     return access_lor_ns(env, ri, isread);
7296 }
7297 
7298 /*
7299  * A trivial implementation of ARMv8.1-LOR leaves all of these
7300  * registers fixed at 0, which indicates that there are zero
7301  * supported Limited Ordering regions.
7302  */
7303 static const ARMCPRegInfo lor_reginfo[] = {
7304     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7305       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7306       .access = PL1_RW, .accessfn = access_lor_other,
7307       .fgt = FGT_LORSA_EL1,
7308       .type = ARM_CP_CONST, .resetvalue = 0 },
7309     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7310       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7311       .access = PL1_RW, .accessfn = access_lor_other,
7312       .fgt = FGT_LOREA_EL1,
7313       .type = ARM_CP_CONST, .resetvalue = 0 },
7314     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7315       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7316       .access = PL1_RW, .accessfn = access_lor_other,
7317       .fgt = FGT_LORN_EL1,
7318       .type = ARM_CP_CONST, .resetvalue = 0 },
7319     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7320       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7321       .access = PL1_RW, .accessfn = access_lor_other,
7322       .fgt = FGT_LORC_EL1,
7323       .type = ARM_CP_CONST, .resetvalue = 0 },
7324     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7325       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7326       .access = PL1_R, .accessfn = access_lor_ns,
7327       .fgt = FGT_LORID_EL1,
7328       .type = ARM_CP_CONST, .resetvalue = 0 },
7329 };
7330 
7331 #ifdef TARGET_AARCH64
7332 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7333                                    bool isread)
7334 {
7335     int el = arm_current_el(env);
7336 
7337     if (el < 2 &&
7338         arm_is_el2_enabled(env) &&
7339         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7340         return CP_ACCESS_TRAP_EL2;
7341     }
7342     if (el < 3 &&
7343         arm_feature(env, ARM_FEATURE_EL3) &&
7344         !(env->cp15.scr_el3 & SCR_APK)) {
7345         return CP_ACCESS_TRAP_EL3;
7346     }
7347     return CP_ACCESS_OK;
7348 }
7349 
7350 static const ARMCPRegInfo pauth_reginfo[] = {
7351     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7352       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7353       .access = PL1_RW, .accessfn = access_pauth,
7354       .fgt = FGT_APDAKEY,
7355       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7356     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7357       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7358       .access = PL1_RW, .accessfn = access_pauth,
7359       .fgt = FGT_APDAKEY,
7360       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7361     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7362       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7363       .access = PL1_RW, .accessfn = access_pauth,
7364       .fgt = FGT_APDBKEY,
7365       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7366     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7367       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7368       .access = PL1_RW, .accessfn = access_pauth,
7369       .fgt = FGT_APDBKEY,
7370       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7371     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7372       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7373       .access = PL1_RW, .accessfn = access_pauth,
7374       .fgt = FGT_APGAKEY,
7375       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7376     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7377       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7378       .access = PL1_RW, .accessfn = access_pauth,
7379       .fgt = FGT_APGAKEY,
7380       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7381     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7382       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7383       .access = PL1_RW, .accessfn = access_pauth,
7384       .fgt = FGT_APIAKEY,
7385       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7386     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7387       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7388       .access = PL1_RW, .accessfn = access_pauth,
7389       .fgt = FGT_APIAKEY,
7390       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7391     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7392       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7393       .access = PL1_RW, .accessfn = access_pauth,
7394       .fgt = FGT_APIBKEY,
7395       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7396     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7397       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7398       .access = PL1_RW, .accessfn = access_pauth,
7399       .fgt = FGT_APIBKEY,
7400       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7401 };
7402 
7403 static const ARMCPRegInfo tlbirange_reginfo[] = {
7404     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7405       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7406       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7407       .fgt = FGT_TLBIRVAE1IS,
7408       .writefn = tlbi_aa64_rvae1is_write },
7409     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7410       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7411       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7412       .fgt = FGT_TLBIRVAAE1IS,
7413       .writefn = tlbi_aa64_rvae1is_write },
7414    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7415       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7416       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7417       .fgt = FGT_TLBIRVALE1IS,
7418       .writefn = tlbi_aa64_rvae1is_write },
7419     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7420       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7421       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7422       .fgt = FGT_TLBIRVAALE1IS,
7423       .writefn = tlbi_aa64_rvae1is_write },
7424     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7425       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7426       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7427       .fgt = FGT_TLBIRVAE1OS,
7428       .writefn = tlbi_aa64_rvae1is_write },
7429     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7430       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7431       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7432       .fgt = FGT_TLBIRVAAE1OS,
7433       .writefn = tlbi_aa64_rvae1is_write },
7434    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7435       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7436       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7437       .fgt = FGT_TLBIRVALE1OS,
7438       .writefn = tlbi_aa64_rvae1is_write },
7439     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7440       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7441       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7442       .fgt = FGT_TLBIRVAALE1OS,
7443       .writefn = tlbi_aa64_rvae1is_write },
7444     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7445       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7446       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7447       .fgt = FGT_TLBIRVAE1,
7448       .writefn = tlbi_aa64_rvae1_write },
7449     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7450       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7451       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7452       .fgt = FGT_TLBIRVAAE1,
7453       .writefn = tlbi_aa64_rvae1_write },
7454    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7455       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7456       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7457       .fgt = FGT_TLBIRVALE1,
7458       .writefn = tlbi_aa64_rvae1_write },
7459     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7460       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7461       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7462       .fgt = FGT_TLBIRVAALE1,
7463       .writefn = tlbi_aa64_rvae1_write },
7464     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7465       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7466       .access = PL2_W, .type = ARM_CP_NO_RAW,
7467       .writefn = tlbi_aa64_ripas2e1is_write },
7468     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7469       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7470       .access = PL2_W, .type = ARM_CP_NO_RAW,
7471       .writefn = tlbi_aa64_ripas2e1is_write },
7472     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7473       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7474       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7475       .writefn = tlbi_aa64_rvae2is_write },
7476    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7477       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7478       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7479       .writefn = tlbi_aa64_rvae2is_write },
7480     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7481       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7482       .access = PL2_W, .type = ARM_CP_NO_RAW,
7483       .writefn = tlbi_aa64_ripas2e1_write },
7484     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7485       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7486       .access = PL2_W, .type = ARM_CP_NO_RAW,
7487       .writefn = tlbi_aa64_ripas2e1_write },
7488    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7489       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7490       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7491       .writefn = tlbi_aa64_rvae2is_write },
7492    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7493       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7494       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7495       .writefn = tlbi_aa64_rvae2is_write },
7496     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7497       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7498       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7499       .writefn = tlbi_aa64_rvae2_write },
7500    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7501       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7502       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7503       .writefn = tlbi_aa64_rvae2_write },
7504    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7505       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7506       .access = PL3_W, .type = ARM_CP_NO_RAW,
7507       .writefn = tlbi_aa64_rvae3is_write },
7508    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7509       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7510       .access = PL3_W, .type = ARM_CP_NO_RAW,
7511       .writefn = tlbi_aa64_rvae3is_write },
7512    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7513       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7514       .access = PL3_W, .type = ARM_CP_NO_RAW,
7515       .writefn = tlbi_aa64_rvae3is_write },
7516    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7517       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7518       .access = PL3_W, .type = ARM_CP_NO_RAW,
7519       .writefn = tlbi_aa64_rvae3is_write },
7520    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7521       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7522       .access = PL3_W, .type = ARM_CP_NO_RAW,
7523       .writefn = tlbi_aa64_rvae3_write },
7524    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7525       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7526       .access = PL3_W, .type = ARM_CP_NO_RAW,
7527       .writefn = tlbi_aa64_rvae3_write },
7528 };
7529 
7530 static const ARMCPRegInfo tlbios_reginfo[] = {
7531     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7532       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7533       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7534       .fgt = FGT_TLBIVMALLE1OS,
7535       .writefn = tlbi_aa64_vmalle1is_write },
7536     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7537       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7538       .fgt = FGT_TLBIVAE1OS,
7539       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7540       .writefn = tlbi_aa64_vae1is_write },
7541     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7542       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7543       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7544       .fgt = FGT_TLBIASIDE1OS,
7545       .writefn = tlbi_aa64_vmalle1is_write },
7546     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7547       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7548       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7549       .fgt = FGT_TLBIVAAE1OS,
7550       .writefn = tlbi_aa64_vae1is_write },
7551     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7552       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7553       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7554       .fgt = FGT_TLBIVALE1OS,
7555       .writefn = tlbi_aa64_vae1is_write },
7556     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7557       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7558       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7559       .fgt = FGT_TLBIVAALE1OS,
7560       .writefn = tlbi_aa64_vae1is_write },
7561     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7562       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7563       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7564       .writefn = tlbi_aa64_alle2is_write },
7565     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7566       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7567       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7568       .writefn = tlbi_aa64_vae2is_write },
7569    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7570       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7571       .access = PL2_W, .type = ARM_CP_NO_RAW,
7572       .writefn = tlbi_aa64_alle1is_write },
7573     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7574       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7575       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7576       .writefn = tlbi_aa64_vae2is_write },
7577     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7578       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7579       .access = PL2_W, .type = ARM_CP_NO_RAW,
7580       .writefn = tlbi_aa64_alle1is_write },
7581     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7582       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7583       .access = PL2_W, .type = ARM_CP_NOP },
7584     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7585       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7586       .access = PL2_W, .type = ARM_CP_NOP },
7587     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7588       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7589       .access = PL2_W, .type = ARM_CP_NOP },
7590     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7591       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7592       .access = PL2_W, .type = ARM_CP_NOP },
7593     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7594       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7595       .access = PL3_W, .type = ARM_CP_NO_RAW,
7596       .writefn = tlbi_aa64_alle3is_write },
7597     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7598       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7599       .access = PL3_W, .type = ARM_CP_NO_RAW,
7600       .writefn = tlbi_aa64_vae3is_write },
7601     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7602       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7603       .access = PL3_W, .type = ARM_CP_NO_RAW,
7604       .writefn = tlbi_aa64_vae3is_write },
7605 };
7606 
7607 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7608 {
7609     Error *err = NULL;
7610     uint64_t ret;
7611 
7612     /* Success sets NZCV = 0000.  */
7613     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7614 
7615     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7616         /*
7617          * ??? Failed, for unknown reasons in the crypto subsystem.
7618          * The best we can do is log the reason and return the
7619          * timed-out indication to the guest.  There is no reason
7620          * we know to expect this failure to be transitory, so the
7621          * guest may well hang retrying the operation.
7622          */
7623         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7624                       ri->name, error_get_pretty(err));
7625         error_free(err);
7626 
7627         env->ZF = 0; /* NZCF = 0100 */
7628         return 0;
7629     }
7630     return ret;
7631 }
7632 
7633 /* We do not support re-seeding, so the two registers operate the same.  */
7634 static const ARMCPRegInfo rndr_reginfo[] = {
7635     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7636       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7637       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7638       .access = PL0_R, .readfn = rndr_readfn },
7639     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7640       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7641       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7642       .access = PL0_R, .readfn = rndr_readfn },
7643 };
7644 
7645 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7646                           uint64_t value)
7647 {
7648     ARMCPU *cpu = env_archcpu(env);
7649     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7650     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7651     uint64_t vaddr_in = (uint64_t) value;
7652     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7653     void *haddr;
7654     int mem_idx = cpu_mmu_index(env, false);
7655 
7656     /* This won't be crossing page boundaries */
7657     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7658     if (haddr) {
7659 #ifndef CONFIG_USER_ONLY
7660 
7661         ram_addr_t offset;
7662         MemoryRegion *mr;
7663 
7664         /* RCU lock is already being held */
7665         mr = memory_region_from_host(haddr, &offset);
7666 
7667         if (mr) {
7668             memory_region_writeback(mr, offset, dline_size);
7669         }
7670 #endif /*CONFIG_USER_ONLY*/
7671     }
7672 }
7673 
7674 static const ARMCPRegInfo dcpop_reg[] = {
7675     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7676       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7677       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7678       .fgt = FGT_DCCVAP,
7679       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7680 };
7681 
7682 static const ARMCPRegInfo dcpodp_reg[] = {
7683     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7684       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7685       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7686       .fgt = FGT_DCCVADP,
7687       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7688 };
7689 
7690 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7691                                        bool isread)
7692 {
7693     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7694         return CP_ACCESS_TRAP_EL2;
7695     }
7696 
7697     return CP_ACCESS_OK;
7698 }
7699 
7700 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7701                                  bool isread)
7702 {
7703     int el = arm_current_el(env);
7704 
7705     if (el < 2 && arm_is_el2_enabled(env)) {
7706         uint64_t hcr = arm_hcr_el2_eff(env);
7707         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7708             return CP_ACCESS_TRAP_EL2;
7709         }
7710     }
7711     if (el < 3 &&
7712         arm_feature(env, ARM_FEATURE_EL3) &&
7713         !(env->cp15.scr_el3 & SCR_ATA)) {
7714         return CP_ACCESS_TRAP_EL3;
7715     }
7716     return CP_ACCESS_OK;
7717 }
7718 
7719 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7720 {
7721     return env->pstate & PSTATE_TCO;
7722 }
7723 
7724 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7725 {
7726     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7727 }
7728 
7729 static const ARMCPRegInfo mte_reginfo[] = {
7730     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7731       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7732       .access = PL1_RW, .accessfn = access_mte,
7733       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7734     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7735       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7736       .access = PL1_RW, .accessfn = access_mte,
7737       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7738     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7739       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7740       .access = PL2_RW, .accessfn = access_mte,
7741       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7742     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7743       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7744       .access = PL3_RW,
7745       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7746     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7747       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7748       .access = PL1_RW, .accessfn = access_mte,
7749       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7750     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7751       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7752       .access = PL1_RW, .accessfn = access_mte,
7753       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7754     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7755       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7756       .type = ARM_CP_NO_RAW,
7757       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7758     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7759       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7760       .type = ARM_CP_NOP, .access = PL1_W,
7761       .fgt = FGT_DCIVAC,
7762       .accessfn = aa64_cacheop_poc_access },
7763     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7764       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7765       .fgt = FGT_DCISW,
7766       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7767     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7768       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7769       .type = ARM_CP_NOP, .access = PL1_W,
7770       .fgt = FGT_DCIVAC,
7771       .accessfn = aa64_cacheop_poc_access },
7772     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7773       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7774       .fgt = FGT_DCISW,
7775       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7776     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7777       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7778       .fgt = FGT_DCCSW,
7779       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7780     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7781       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7782       .fgt = FGT_DCCSW,
7783       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7784     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7785       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7786       .fgt = FGT_DCCISW,
7787       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7788     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7789       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7790       .fgt = FGT_DCCISW,
7791       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7792 };
7793 
7794 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7795     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7796       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7797       .type = ARM_CP_CONST, .access = PL0_RW, },
7798 };
7799 
7800 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7801     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7802       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7803       .type = ARM_CP_NOP, .access = PL0_W,
7804       .fgt = FGT_DCCVAC,
7805       .accessfn = aa64_cacheop_poc_access },
7806     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7807       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7808       .type = ARM_CP_NOP, .access = PL0_W,
7809       .fgt = FGT_DCCVAC,
7810       .accessfn = aa64_cacheop_poc_access },
7811     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7812       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7813       .type = ARM_CP_NOP, .access = PL0_W,
7814       .fgt = FGT_DCCVAP,
7815       .accessfn = aa64_cacheop_poc_access },
7816     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7817       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7818       .type = ARM_CP_NOP, .access = PL0_W,
7819       .fgt = FGT_DCCVAP,
7820       .accessfn = aa64_cacheop_poc_access },
7821     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7822       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7823       .type = ARM_CP_NOP, .access = PL0_W,
7824       .fgt = FGT_DCCVADP,
7825       .accessfn = aa64_cacheop_poc_access },
7826     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7827       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7828       .type = ARM_CP_NOP, .access = PL0_W,
7829       .fgt = FGT_DCCVADP,
7830       .accessfn = aa64_cacheop_poc_access },
7831     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7832       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7833       .type = ARM_CP_NOP, .access = PL0_W,
7834       .fgt = FGT_DCCIVAC,
7835       .accessfn = aa64_cacheop_poc_access },
7836     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7837       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7838       .type = ARM_CP_NOP, .access = PL0_W,
7839       .fgt = FGT_DCCIVAC,
7840       .accessfn = aa64_cacheop_poc_access },
7841     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7842       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7843       .access = PL0_W, .type = ARM_CP_DC_GVA,
7844 #ifndef CONFIG_USER_ONLY
7845       /* Avoid overhead of an access check that always passes in user-mode */
7846       .accessfn = aa64_zva_access,
7847       .fgt = FGT_DCZVA,
7848 #endif
7849     },
7850     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7851       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7852       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7853 #ifndef CONFIG_USER_ONLY
7854       /* Avoid overhead of an access check that always passes in user-mode */
7855       .accessfn = aa64_zva_access,
7856       .fgt = FGT_DCZVA,
7857 #endif
7858     },
7859 };
7860 
7861 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7862                                      bool isread)
7863 {
7864     uint64_t hcr = arm_hcr_el2_eff(env);
7865     int el = arm_current_el(env);
7866 
7867     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7868         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7869             if (hcr & HCR_TGE) {
7870                 return CP_ACCESS_TRAP_EL2;
7871             }
7872             return CP_ACCESS_TRAP;
7873         }
7874     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7875         return CP_ACCESS_TRAP_EL2;
7876     }
7877     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7878         return CP_ACCESS_TRAP_EL2;
7879     }
7880     if (el < 3
7881         && arm_feature(env, ARM_FEATURE_EL3)
7882         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7883         return CP_ACCESS_TRAP_EL3;
7884     }
7885     return CP_ACCESS_OK;
7886 }
7887 
7888 static const ARMCPRegInfo scxtnum_reginfo[] = {
7889     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7890       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7891       .access = PL0_RW, .accessfn = access_scxtnum,
7892       .fgt = FGT_SCXTNUM_EL0,
7893       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7894     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7895       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7896       .access = PL1_RW, .accessfn = access_scxtnum,
7897       .fgt = FGT_SCXTNUM_EL1,
7898       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7899     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7900       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7901       .access = PL2_RW, .accessfn = access_scxtnum,
7902       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7903     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7904       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7905       .access = PL3_RW,
7906       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7907 };
7908 
7909 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7910                                  bool isread)
7911 {
7912     if (arm_current_el(env) == 2 &&
7913         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7914         return CP_ACCESS_TRAP_EL3;
7915     }
7916     return CP_ACCESS_OK;
7917 }
7918 
7919 static const ARMCPRegInfo fgt_reginfo[] = {
7920     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7921       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7922       .access = PL2_RW, .accessfn = access_fgt,
7923       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7924     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7925       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7926       .access = PL2_RW, .accessfn = access_fgt,
7927       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7928     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7929       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7930       .access = PL2_RW, .accessfn = access_fgt,
7931       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7932     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7933       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7934       .access = PL2_RW, .accessfn = access_fgt,
7935       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7936     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7937       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7938       .access = PL2_RW, .accessfn = access_fgt,
7939       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7940 };
7941 #endif /* TARGET_AARCH64 */
7942 
7943 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7944                                      bool isread)
7945 {
7946     int el = arm_current_el(env);
7947 
7948     if (el == 0) {
7949         uint64_t sctlr = arm_sctlr(env, el);
7950         if (!(sctlr & SCTLR_EnRCTX)) {
7951             return CP_ACCESS_TRAP;
7952         }
7953     } else if (el == 1) {
7954         uint64_t hcr = arm_hcr_el2_eff(env);
7955         if (hcr & HCR_NV) {
7956             return CP_ACCESS_TRAP_EL2;
7957         }
7958     }
7959     return CP_ACCESS_OK;
7960 }
7961 
7962 static const ARMCPRegInfo predinv_reginfo[] = {
7963     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7964       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7965       .fgt = FGT_CFPRCTX,
7966       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7967     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7968       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7969       .fgt = FGT_DVPRCTX,
7970       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7971     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7972       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7973       .fgt = FGT_CPPRCTX,
7974       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7975     /*
7976      * Note the AArch32 opcodes have a different OPC1.
7977      */
7978     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7979       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7980       .fgt = FGT_CFPRCTX,
7981       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7982     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7983       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7984       .fgt = FGT_DVPRCTX,
7985       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7986     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7987       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7988       .fgt = FGT_CPPRCTX,
7989       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7990 };
7991 
7992 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7993 {
7994     /* Read the high 32 bits of the current CCSIDR */
7995     return extract64(ccsidr_read(env, ri), 32, 32);
7996 }
7997 
7998 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7999     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
8000       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
8001       .access = PL1_R,
8002       .accessfn = access_tid4,
8003       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
8004 };
8005 
8006 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8007                                        bool isread)
8008 {
8009     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
8010         return CP_ACCESS_TRAP_EL2;
8011     }
8012 
8013     return CP_ACCESS_OK;
8014 }
8015 
8016 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8017                                        bool isread)
8018 {
8019     if (arm_feature(env, ARM_FEATURE_V8)) {
8020         return access_aa64_tid3(env, ri, isread);
8021     }
8022 
8023     return CP_ACCESS_OK;
8024 }
8025 
8026 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8027                                      bool isread)
8028 {
8029     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8030         return CP_ACCESS_TRAP_EL2;
8031     }
8032 
8033     return CP_ACCESS_OK;
8034 }
8035 
8036 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8037                                         const ARMCPRegInfo *ri, bool isread)
8038 {
8039     /*
8040      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8041      * in v7A, not in v8A.
8042      */
8043     if (!arm_feature(env, ARM_FEATURE_V8) &&
8044         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8045         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8046         return CP_ACCESS_TRAP_EL2;
8047     }
8048     return CP_ACCESS_OK;
8049 }
8050 
8051 static const ARMCPRegInfo jazelle_regs[] = {
8052     { .name = "JIDR",
8053       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8054       .access = PL1_R, .accessfn = access_jazelle,
8055       .type = ARM_CP_CONST, .resetvalue = 0 },
8056     { .name = "JOSCR",
8057       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8058       .accessfn = access_joscr_jmcr,
8059       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8060     { .name = "JMCR",
8061       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8062       .accessfn = access_joscr_jmcr,
8063       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8064 };
8065 
8066 static const ARMCPRegInfo contextidr_el2 = {
8067     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8068     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8069     .access = PL2_RW,
8070     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8071 };
8072 
8073 static const ARMCPRegInfo vhe_reginfo[] = {
8074     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8075       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8076       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
8077       .raw_writefn = raw_write,
8078       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8079 #ifndef CONFIG_USER_ONLY
8080     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8081       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8082       .fieldoffset =
8083         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8084       .type = ARM_CP_IO, .access = PL2_RW,
8085       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8086     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8087       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8088       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8089       .resetfn = gt_hv_timer_reset,
8090       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8091     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8092       .type = ARM_CP_IO,
8093       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8094       .access = PL2_RW,
8095       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8096       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
8097     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8098       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8099       .type = ARM_CP_IO | ARM_CP_ALIAS,
8100       .access = PL2_RW, .accessfn = e2h_access,
8101       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8102       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8103     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8104       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8105       .type = ARM_CP_IO | ARM_CP_ALIAS,
8106       .access = PL2_RW, .accessfn = e2h_access,
8107       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8108       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8109     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8110       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8111       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8112       .access = PL2_RW, .accessfn = e2h_access,
8113       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8114     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8115       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8116       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8117       .access = PL2_RW, .accessfn = e2h_access,
8118       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8119     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8120       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8121       .type = ARM_CP_IO | ARM_CP_ALIAS,
8122       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8123       .access = PL2_RW, .accessfn = e2h_access,
8124       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8125     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8126       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8127       .type = ARM_CP_IO | ARM_CP_ALIAS,
8128       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8129       .access = PL2_RW, .accessfn = e2h_access,
8130       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8131 #endif
8132 };
8133 
8134 #ifndef CONFIG_USER_ONLY
8135 static const ARMCPRegInfo ats1e1_reginfo[] = {
8136     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
8137       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8138       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8139       .fgt = FGT_ATS1E1RP,
8140       .accessfn = at_e012_access, .writefn = ats_write64 },
8141     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
8142       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8143       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8144       .fgt = FGT_ATS1E1WP,
8145       .accessfn = at_e012_access, .writefn = ats_write64 },
8146 };
8147 
8148 static const ARMCPRegInfo ats1cp_reginfo[] = {
8149     { .name = "ATS1CPRP",
8150       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8151       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8152       .writefn = ats_write },
8153     { .name = "ATS1CPWP",
8154       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8155       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8156       .writefn = ats_write },
8157 };
8158 #endif
8159 
8160 /*
8161  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8162  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8163  * is non-zero, which is never for ARMv7, optionally in ARMv8
8164  * and mandatorily for ARMv8.2 and up.
8165  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8166  * implementation is RAZ/WI we can ignore this detail, as we
8167  * do for ACTLR.
8168  */
8169 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8170     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8171       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
8172       .access = PL1_RW, .accessfn = access_tacr,
8173       .type = ARM_CP_CONST, .resetvalue = 0 },
8174     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8175       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8176       .access = PL2_RW, .type = ARM_CP_CONST,
8177       .resetvalue = 0 },
8178 };
8179 
8180 void register_cp_regs_for_features(ARMCPU *cpu)
8181 {
8182     /* Register all the coprocessor registers based on feature bits */
8183     CPUARMState *env = &cpu->env;
8184     if (arm_feature(env, ARM_FEATURE_M)) {
8185         /* M profile has no coprocessor registers */
8186         return;
8187     }
8188 
8189     define_arm_cp_regs(cpu, cp_reginfo);
8190     if (!arm_feature(env, ARM_FEATURE_V8)) {
8191         /*
8192          * Must go early as it is full of wildcards that may be
8193          * overridden by later definitions.
8194          */
8195         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8196     }
8197 
8198     if (arm_feature(env, ARM_FEATURE_V6)) {
8199         /* The ID registers all have impdef reset values */
8200         ARMCPRegInfo v6_idregs[] = {
8201             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8202               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8203               .access = PL1_R, .type = ARM_CP_CONST,
8204               .accessfn = access_aa32_tid3,
8205               .resetvalue = cpu->isar.id_pfr0 },
8206             /*
8207              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
8208              * the value of the GIC field until after we define these regs.
8209              */
8210             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8211               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
8212               .access = PL1_R, .type = ARM_CP_NO_RAW,
8213               .accessfn = access_aa32_tid3,
8214 #ifdef CONFIG_USER_ONLY
8215               .type = ARM_CP_CONST,
8216               .resetvalue = cpu->isar.id_pfr1,
8217 #else
8218               .type = ARM_CP_NO_RAW,
8219               .accessfn = access_aa32_tid3,
8220               .readfn = id_pfr1_read,
8221               .writefn = arm_cp_write_ignore
8222 #endif
8223             },
8224             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8225               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8226               .access = PL1_R, .type = ARM_CP_CONST,
8227               .accessfn = access_aa32_tid3,
8228               .resetvalue = cpu->isar.id_dfr0 },
8229             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8230               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8231               .access = PL1_R, .type = ARM_CP_CONST,
8232               .accessfn = access_aa32_tid3,
8233               .resetvalue = cpu->id_afr0 },
8234             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8235               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8236               .access = PL1_R, .type = ARM_CP_CONST,
8237               .accessfn = access_aa32_tid3,
8238               .resetvalue = cpu->isar.id_mmfr0 },
8239             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8240               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8241               .access = PL1_R, .type = ARM_CP_CONST,
8242               .accessfn = access_aa32_tid3,
8243               .resetvalue = cpu->isar.id_mmfr1 },
8244             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8245               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8246               .access = PL1_R, .type = ARM_CP_CONST,
8247               .accessfn = access_aa32_tid3,
8248               .resetvalue = cpu->isar.id_mmfr2 },
8249             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8250               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8251               .access = PL1_R, .type = ARM_CP_CONST,
8252               .accessfn = access_aa32_tid3,
8253               .resetvalue = cpu->isar.id_mmfr3 },
8254             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8255               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8256               .access = PL1_R, .type = ARM_CP_CONST,
8257               .accessfn = access_aa32_tid3,
8258               .resetvalue = cpu->isar.id_isar0 },
8259             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8260               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8261               .access = PL1_R, .type = ARM_CP_CONST,
8262               .accessfn = access_aa32_tid3,
8263               .resetvalue = cpu->isar.id_isar1 },
8264             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8265               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8266               .access = PL1_R, .type = ARM_CP_CONST,
8267               .accessfn = access_aa32_tid3,
8268               .resetvalue = cpu->isar.id_isar2 },
8269             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8270               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8271               .access = PL1_R, .type = ARM_CP_CONST,
8272               .accessfn = access_aa32_tid3,
8273               .resetvalue = cpu->isar.id_isar3 },
8274             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8275               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8276               .access = PL1_R, .type = ARM_CP_CONST,
8277               .accessfn = access_aa32_tid3,
8278               .resetvalue = cpu->isar.id_isar4 },
8279             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8280               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8281               .access = PL1_R, .type = ARM_CP_CONST,
8282               .accessfn = access_aa32_tid3,
8283               .resetvalue = cpu->isar.id_isar5 },
8284             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8285               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8286               .access = PL1_R, .type = ARM_CP_CONST,
8287               .accessfn = access_aa32_tid3,
8288               .resetvalue = cpu->isar.id_mmfr4 },
8289             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
8290               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8291               .access = PL1_R, .type = ARM_CP_CONST,
8292               .accessfn = access_aa32_tid3,
8293               .resetvalue = cpu->isar.id_isar6 },
8294         };
8295         define_arm_cp_regs(cpu, v6_idregs);
8296         define_arm_cp_regs(cpu, v6_cp_reginfo);
8297     } else {
8298         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8299     }
8300     if (arm_feature(env, ARM_FEATURE_V6K)) {
8301         define_arm_cp_regs(cpu, v6k_cp_reginfo);
8302     }
8303     if (arm_feature(env, ARM_FEATURE_V7MP) &&
8304         !arm_feature(env, ARM_FEATURE_PMSA)) {
8305         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8306     }
8307     if (arm_feature(env, ARM_FEATURE_V7VE)) {
8308         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8309     }
8310     if (arm_feature(env, ARM_FEATURE_V7)) {
8311         ARMCPRegInfo clidr = {
8312             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8313             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
8314             .access = PL1_R, .type = ARM_CP_CONST,
8315             .accessfn = access_tid4,
8316             .fgt = FGT_CLIDR_EL1,
8317             .resetvalue = cpu->clidr
8318         };
8319         define_one_arm_cp_reg(cpu, &clidr);
8320         define_arm_cp_regs(cpu, v7_cp_reginfo);
8321         define_debug_regs(cpu);
8322         define_pmu_regs(cpu);
8323     } else {
8324         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
8325     }
8326     if (arm_feature(env, ARM_FEATURE_V8)) {
8327         /*
8328          * v8 ID registers, which all have impdef reset values.
8329          * Note that within the ID register ranges the unused slots
8330          * must all RAZ, not UNDEF; future architecture versions may
8331          * define new registers here.
8332          * ID registers which are AArch64 views of the AArch32 ID registers
8333          * which already existed in v6 and v7 are handled elsewhere,
8334          * in v6_idregs[].
8335          */
8336         int i;
8337         ARMCPRegInfo v8_idregs[] = {
8338             /*
8339              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8340              * emulation because we don't know the right value for the
8341              * GIC field until after we define these regs.
8342              */
8343             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8344               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
8345               .access = PL1_R,
8346 #ifdef CONFIG_USER_ONLY
8347               .type = ARM_CP_CONST,
8348               .resetvalue = cpu->isar.id_aa64pfr0
8349 #else
8350               .type = ARM_CP_NO_RAW,
8351               .accessfn = access_aa64_tid3,
8352               .readfn = id_aa64pfr0_read,
8353               .writefn = arm_cp_write_ignore
8354 #endif
8355             },
8356             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8357               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8358               .access = PL1_R, .type = ARM_CP_CONST,
8359               .accessfn = access_aa64_tid3,
8360               .resetvalue = cpu->isar.id_aa64pfr1},
8361             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8362               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8363               .access = PL1_R, .type = ARM_CP_CONST,
8364               .accessfn = access_aa64_tid3,
8365               .resetvalue = 0 },
8366             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8367               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8368               .access = PL1_R, .type = ARM_CP_CONST,
8369               .accessfn = access_aa64_tid3,
8370               .resetvalue = 0 },
8371             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
8372               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8373               .access = PL1_R, .type = ARM_CP_CONST,
8374               .accessfn = access_aa64_tid3,
8375               .resetvalue = cpu->isar.id_aa64zfr0 },
8376             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
8377               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8378               .access = PL1_R, .type = ARM_CP_CONST,
8379               .accessfn = access_aa64_tid3,
8380               .resetvalue = cpu->isar.id_aa64smfr0 },
8381             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8382               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8383               .access = PL1_R, .type = ARM_CP_CONST,
8384               .accessfn = access_aa64_tid3,
8385               .resetvalue = 0 },
8386             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8387               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8388               .access = PL1_R, .type = ARM_CP_CONST,
8389               .accessfn = access_aa64_tid3,
8390               .resetvalue = 0 },
8391             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8392               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8393               .access = PL1_R, .type = ARM_CP_CONST,
8394               .accessfn = access_aa64_tid3,
8395               .resetvalue = cpu->isar.id_aa64dfr0 },
8396             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8397               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8398               .access = PL1_R, .type = ARM_CP_CONST,
8399               .accessfn = access_aa64_tid3,
8400               .resetvalue = cpu->isar.id_aa64dfr1 },
8401             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8402               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8403               .access = PL1_R, .type = ARM_CP_CONST,
8404               .accessfn = access_aa64_tid3,
8405               .resetvalue = 0 },
8406             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8407               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8408               .access = PL1_R, .type = ARM_CP_CONST,
8409               .accessfn = access_aa64_tid3,
8410               .resetvalue = 0 },
8411             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8412               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8413               .access = PL1_R, .type = ARM_CP_CONST,
8414               .accessfn = access_aa64_tid3,
8415               .resetvalue = cpu->id_aa64afr0 },
8416             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8417               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8418               .access = PL1_R, .type = ARM_CP_CONST,
8419               .accessfn = access_aa64_tid3,
8420               .resetvalue = cpu->id_aa64afr1 },
8421             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8422               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8423               .access = PL1_R, .type = ARM_CP_CONST,
8424               .accessfn = access_aa64_tid3,
8425               .resetvalue = 0 },
8426             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8427               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8428               .access = PL1_R, .type = ARM_CP_CONST,
8429               .accessfn = access_aa64_tid3,
8430               .resetvalue = 0 },
8431             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8432               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8433               .access = PL1_R, .type = ARM_CP_CONST,
8434               .accessfn = access_aa64_tid3,
8435               .resetvalue = cpu->isar.id_aa64isar0 },
8436             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8437               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8438               .access = PL1_R, .type = ARM_CP_CONST,
8439               .accessfn = access_aa64_tid3,
8440               .resetvalue = cpu->isar.id_aa64isar1 },
8441             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8442               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8443               .access = PL1_R, .type = ARM_CP_CONST,
8444               .accessfn = access_aa64_tid3,
8445               .resetvalue = cpu->isar.id_aa64isar2 },
8446             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8447               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8448               .access = PL1_R, .type = ARM_CP_CONST,
8449               .accessfn = access_aa64_tid3,
8450               .resetvalue = 0 },
8451             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8452               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8453               .access = PL1_R, .type = ARM_CP_CONST,
8454               .accessfn = access_aa64_tid3,
8455               .resetvalue = 0 },
8456             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8457               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8458               .access = PL1_R, .type = ARM_CP_CONST,
8459               .accessfn = access_aa64_tid3,
8460               .resetvalue = 0 },
8461             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8462               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8463               .access = PL1_R, .type = ARM_CP_CONST,
8464               .accessfn = access_aa64_tid3,
8465               .resetvalue = 0 },
8466             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8467               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8468               .access = PL1_R, .type = ARM_CP_CONST,
8469               .accessfn = access_aa64_tid3,
8470               .resetvalue = 0 },
8471             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8472               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8473               .access = PL1_R, .type = ARM_CP_CONST,
8474               .accessfn = access_aa64_tid3,
8475               .resetvalue = cpu->isar.id_aa64mmfr0 },
8476             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8477               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8478               .access = PL1_R, .type = ARM_CP_CONST,
8479               .accessfn = access_aa64_tid3,
8480               .resetvalue = cpu->isar.id_aa64mmfr1 },
8481             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8482               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8483               .access = PL1_R, .type = ARM_CP_CONST,
8484               .accessfn = access_aa64_tid3,
8485               .resetvalue = cpu->isar.id_aa64mmfr2 },
8486             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8487               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8488               .access = PL1_R, .type = ARM_CP_CONST,
8489               .accessfn = access_aa64_tid3,
8490               .resetvalue = 0 },
8491             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8492               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8493               .access = PL1_R, .type = ARM_CP_CONST,
8494               .accessfn = access_aa64_tid3,
8495               .resetvalue = 0 },
8496             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8497               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8498               .access = PL1_R, .type = ARM_CP_CONST,
8499               .accessfn = access_aa64_tid3,
8500               .resetvalue = 0 },
8501             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8502               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8503               .access = PL1_R, .type = ARM_CP_CONST,
8504               .accessfn = access_aa64_tid3,
8505               .resetvalue = 0 },
8506             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8507               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8508               .access = PL1_R, .type = ARM_CP_CONST,
8509               .accessfn = access_aa64_tid3,
8510               .resetvalue = 0 },
8511             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8512               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8513               .access = PL1_R, .type = ARM_CP_CONST,
8514               .accessfn = access_aa64_tid3,
8515               .resetvalue = cpu->isar.mvfr0 },
8516             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8517               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8518               .access = PL1_R, .type = ARM_CP_CONST,
8519               .accessfn = access_aa64_tid3,
8520               .resetvalue = cpu->isar.mvfr1 },
8521             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8522               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8523               .access = PL1_R, .type = ARM_CP_CONST,
8524               .accessfn = access_aa64_tid3,
8525               .resetvalue = cpu->isar.mvfr2 },
8526             /*
8527              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8528              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8529              * as RAZ, since it is in the "reserved for future ID
8530              * registers, RAZ" part of the AArch32 encoding space.
8531              */
8532             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8533               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8534               .access = PL1_R, .type = ARM_CP_CONST,
8535               .accessfn = access_aa64_tid3,
8536               .resetvalue = 0 },
8537             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8538               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8539               .access = PL1_R, .type = ARM_CP_CONST,
8540               .accessfn = access_aa64_tid3,
8541               .resetvalue = 0 },
8542             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8543               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8544               .access = PL1_R, .type = ARM_CP_CONST,
8545               .accessfn = access_aa64_tid3,
8546               .resetvalue = 0 },
8547             /*
8548              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8549              * they're also RAZ for AArch64, and in v8 are gradually
8550              * being filled with AArch64-view-of-AArch32-ID-register
8551              * for new ID registers.
8552              */
8553             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8554               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8555               .access = PL1_R, .type = ARM_CP_CONST,
8556               .accessfn = access_aa64_tid3,
8557               .resetvalue = 0 },
8558             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8559               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8560               .access = PL1_R, .type = ARM_CP_CONST,
8561               .accessfn = access_aa64_tid3,
8562               .resetvalue = cpu->isar.id_pfr2 },
8563             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8564               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8565               .access = PL1_R, .type = ARM_CP_CONST,
8566               .accessfn = access_aa64_tid3,
8567               .resetvalue = cpu->isar.id_dfr1 },
8568             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8569               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8570               .access = PL1_R, .type = ARM_CP_CONST,
8571               .accessfn = access_aa64_tid3,
8572               .resetvalue = cpu->isar.id_mmfr5 },
8573             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8574               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8575               .access = PL1_R, .type = ARM_CP_CONST,
8576               .accessfn = access_aa64_tid3,
8577               .resetvalue = 0 },
8578             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8579               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8580               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8581               .fgt = FGT_PMCEIDN_EL0,
8582               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8583             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8584               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8585               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8586               .fgt = FGT_PMCEIDN_EL0,
8587               .resetvalue = cpu->pmceid0 },
8588             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8589               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8590               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8591               .fgt = FGT_PMCEIDN_EL0,
8592               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8593             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8594               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8595               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8596               .fgt = FGT_PMCEIDN_EL0,
8597               .resetvalue = cpu->pmceid1 },
8598         };
8599 #ifdef CONFIG_USER_ONLY
8600         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8601             { .name = "ID_AA64PFR0_EL1",
8602               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8603                                R_ID_AA64PFR0_ADVSIMD_MASK |
8604                                R_ID_AA64PFR0_SVE_MASK |
8605                                R_ID_AA64PFR0_DIT_MASK,
8606               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8607                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8608             { .name = "ID_AA64PFR1_EL1",
8609               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8610                                R_ID_AA64PFR1_SSBS_MASK |
8611                                R_ID_AA64PFR1_MTE_MASK |
8612                                R_ID_AA64PFR1_SME_MASK },
8613             { .name = "ID_AA64PFR*_EL1_RESERVED",
8614               .is_glob = true },
8615             { .name = "ID_AA64ZFR0_EL1",
8616               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8617                                R_ID_AA64ZFR0_AES_MASK |
8618                                R_ID_AA64ZFR0_BITPERM_MASK |
8619                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8620                                R_ID_AA64ZFR0_SHA3_MASK |
8621                                R_ID_AA64ZFR0_SM4_MASK |
8622                                R_ID_AA64ZFR0_I8MM_MASK |
8623                                R_ID_AA64ZFR0_F32MM_MASK |
8624                                R_ID_AA64ZFR0_F64MM_MASK },
8625             { .name = "ID_AA64SMFR0_EL1",
8626               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8627                                R_ID_AA64SMFR0_BI32I32_MASK |
8628                                R_ID_AA64SMFR0_B16F32_MASK |
8629                                R_ID_AA64SMFR0_F16F32_MASK |
8630                                R_ID_AA64SMFR0_I8I32_MASK |
8631                                R_ID_AA64SMFR0_F16F16_MASK |
8632                                R_ID_AA64SMFR0_B16B16_MASK |
8633                                R_ID_AA64SMFR0_I16I32_MASK |
8634                                R_ID_AA64SMFR0_F64F64_MASK |
8635                                R_ID_AA64SMFR0_I16I64_MASK |
8636                                R_ID_AA64SMFR0_SMEVER_MASK |
8637                                R_ID_AA64SMFR0_FA64_MASK },
8638             { .name = "ID_AA64MMFR0_EL1",
8639               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8640               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8641                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8642             { .name = "ID_AA64MMFR1_EL1",
8643               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8644             { .name = "ID_AA64MMFR2_EL1",
8645               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8646             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8647               .is_glob = true },
8648             { .name = "ID_AA64DFR0_EL1",
8649               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8650             { .name = "ID_AA64DFR1_EL1" },
8651             { .name = "ID_AA64DFR*_EL1_RESERVED",
8652               .is_glob = true },
8653             { .name = "ID_AA64AFR*",
8654               .is_glob = true },
8655             { .name = "ID_AA64ISAR0_EL1",
8656               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8657                                R_ID_AA64ISAR0_SHA1_MASK |
8658                                R_ID_AA64ISAR0_SHA2_MASK |
8659                                R_ID_AA64ISAR0_CRC32_MASK |
8660                                R_ID_AA64ISAR0_ATOMIC_MASK |
8661                                R_ID_AA64ISAR0_RDM_MASK |
8662                                R_ID_AA64ISAR0_SHA3_MASK |
8663                                R_ID_AA64ISAR0_SM3_MASK |
8664                                R_ID_AA64ISAR0_SM4_MASK |
8665                                R_ID_AA64ISAR0_DP_MASK |
8666                                R_ID_AA64ISAR0_FHM_MASK |
8667                                R_ID_AA64ISAR0_TS_MASK |
8668                                R_ID_AA64ISAR0_RNDR_MASK },
8669             { .name = "ID_AA64ISAR1_EL1",
8670               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8671                                R_ID_AA64ISAR1_APA_MASK |
8672                                R_ID_AA64ISAR1_API_MASK |
8673                                R_ID_AA64ISAR1_JSCVT_MASK |
8674                                R_ID_AA64ISAR1_FCMA_MASK |
8675                                R_ID_AA64ISAR1_LRCPC_MASK |
8676                                R_ID_AA64ISAR1_GPA_MASK |
8677                                R_ID_AA64ISAR1_GPI_MASK |
8678                                R_ID_AA64ISAR1_FRINTTS_MASK |
8679                                R_ID_AA64ISAR1_SB_MASK |
8680                                R_ID_AA64ISAR1_BF16_MASK |
8681                                R_ID_AA64ISAR1_DGH_MASK |
8682                                R_ID_AA64ISAR1_I8MM_MASK },
8683             { .name = "ID_AA64ISAR2_EL1",
8684               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8685                                R_ID_AA64ISAR2_RPRES_MASK |
8686                                R_ID_AA64ISAR2_GPA3_MASK |
8687                                R_ID_AA64ISAR2_APA3_MASK |
8688                                R_ID_AA64ISAR2_MOPS_MASK |
8689                                R_ID_AA64ISAR2_BC_MASK |
8690                                R_ID_AA64ISAR2_RPRFM_MASK |
8691                                R_ID_AA64ISAR2_CSSC_MASK },
8692             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8693               .is_glob = true },
8694         };
8695         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8696 #endif
8697         /*
8698          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8699          * TODO: For RMR, a write with bit 1 set should do something with
8700          * cpu_reset(). In the meantime, "the bit is strictly a request",
8701          * so we are in spec just ignoring writes.
8702          */
8703         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8704             !arm_feature(env, ARM_FEATURE_EL2)) {
8705             ARMCPRegInfo el1_reset_regs[] = {
8706                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8707                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8708                   .access = PL1_R,
8709                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8710                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8711                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8712                   .access = PL1_RW, .type = ARM_CP_CONST,
8713                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8714             };
8715             define_arm_cp_regs(cpu, el1_reset_regs);
8716         }
8717         define_arm_cp_regs(cpu, v8_idregs);
8718         define_arm_cp_regs(cpu, v8_cp_reginfo);
8719 
8720         for (i = 4; i < 16; i++) {
8721             /*
8722              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8723              * For pre-v8 cores there are RAZ patterns for these in
8724              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8725              * v8 extends the "must RAZ" part of the ID register space
8726              * to also cover c0, 0, c{8-15}, {0-7}.
8727              * These are STATE_AA32 because in the AArch64 sysreg space
8728              * c4-c7 is where the AArch64 ID registers live (and we've
8729              * already defined those in v8_idregs[]), and c8-c15 are not
8730              * "must RAZ" for AArch64.
8731              */
8732             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8733             ARMCPRegInfo v8_aa32_raz_idregs = {
8734                 .name = name,
8735                 .state = ARM_CP_STATE_AA32,
8736                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8737                 .access = PL1_R, .type = ARM_CP_CONST,
8738                 .accessfn = access_aa64_tid3,
8739                 .resetvalue = 0 };
8740             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8741         }
8742     }
8743 
8744     /*
8745      * Register the base EL2 cpregs.
8746      * Pre v8, these registers are implemented only as part of the
8747      * Virtualization Extensions (EL2 present).  Beginning with v8,
8748      * if EL2 is missing but EL3 is enabled, mostly these become
8749      * RES0 from EL3, with some specific exceptions.
8750      */
8751     if (arm_feature(env, ARM_FEATURE_EL2)
8752         || (arm_feature(env, ARM_FEATURE_EL3)
8753             && arm_feature(env, ARM_FEATURE_V8))) {
8754         uint64_t vmpidr_def = mpidr_read_val(env);
8755         ARMCPRegInfo vpidr_regs[] = {
8756             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8757               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8758               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8759               .resetvalue = cpu->midr,
8760               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8761               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8762             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8763               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8764               .access = PL2_RW, .resetvalue = cpu->midr,
8765               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8766               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8767             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8768               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8769               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8770               .resetvalue = vmpidr_def,
8771               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8772               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8773             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8774               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8775               .access = PL2_RW, .resetvalue = vmpidr_def,
8776               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8777               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8778         };
8779         /*
8780          * The only field of MDCR_EL2 that has a defined architectural reset
8781          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8782          */
8783         ARMCPRegInfo mdcr_el2 = {
8784             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8785             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8786             .writefn = mdcr_el2_write,
8787             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8788             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8789         };
8790         define_one_arm_cp_reg(cpu, &mdcr_el2);
8791         define_arm_cp_regs(cpu, vpidr_regs);
8792         define_arm_cp_regs(cpu, el2_cp_reginfo);
8793         if (arm_feature(env, ARM_FEATURE_V8)) {
8794             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8795         }
8796         if (cpu_isar_feature(aa64_sel2, cpu)) {
8797             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8798         }
8799         /*
8800          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8801          * See commentary near RMR_EL1.
8802          */
8803         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8804             static const ARMCPRegInfo el2_reset_regs[] = {
8805                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8806                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8807                   .access = PL2_R,
8808                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8809                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8810                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8811                   .access = PL2_R,
8812                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8813                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
8814                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
8815                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8816             };
8817             define_arm_cp_regs(cpu, el2_reset_regs);
8818         }
8819     }
8820 
8821     /* Register the base EL3 cpregs. */
8822     if (arm_feature(env, ARM_FEATURE_EL3)) {
8823         define_arm_cp_regs(cpu, el3_cp_reginfo);
8824         ARMCPRegInfo el3_regs[] = {
8825             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8826               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8827               .access = PL3_R,
8828               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
8829             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
8830               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
8831               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8832             { .name = "RMR", .state = ARM_CP_STATE_AA32,
8833               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8834               .access = PL3_RW, .type = ARM_CP_CONST,
8835               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
8836             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8837               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8838               .access = PL3_RW,
8839               .raw_writefn = raw_write, .writefn = sctlr_write,
8840               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8841               .resetvalue = cpu->reset_sctlr },
8842         };
8843 
8844         define_arm_cp_regs(cpu, el3_regs);
8845     }
8846     /*
8847      * The behaviour of NSACR is sufficiently various that we don't
8848      * try to describe it in a single reginfo:
8849      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8850      *     reads as constant 0xc00 from NS EL1 and NS EL2
8851      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8852      *  if v7 without EL3, register doesn't exist
8853      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8854      */
8855     if (arm_feature(env, ARM_FEATURE_EL3)) {
8856         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8857             static const ARMCPRegInfo nsacr = {
8858                 .name = "NSACR", .type = ARM_CP_CONST,
8859                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8860                 .access = PL1_RW, .accessfn = nsacr_access,
8861                 .resetvalue = 0xc00
8862             };
8863             define_one_arm_cp_reg(cpu, &nsacr);
8864         } else {
8865             static const ARMCPRegInfo nsacr = {
8866                 .name = "NSACR",
8867                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8868                 .access = PL3_RW | PL1_R,
8869                 .resetvalue = 0,
8870                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8871             };
8872             define_one_arm_cp_reg(cpu, &nsacr);
8873         }
8874     } else {
8875         if (arm_feature(env, ARM_FEATURE_V8)) {
8876             static const ARMCPRegInfo nsacr = {
8877                 .name = "NSACR", .type = ARM_CP_CONST,
8878                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8879                 .access = PL1_R,
8880                 .resetvalue = 0xc00
8881             };
8882             define_one_arm_cp_reg(cpu, &nsacr);
8883         }
8884     }
8885 
8886     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8887         if (arm_feature(env, ARM_FEATURE_V6)) {
8888             /* PMSAv6 not implemented */
8889             assert(arm_feature(env, ARM_FEATURE_V7));
8890             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8891             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8892         } else {
8893             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8894         }
8895     } else {
8896         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8897         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8898         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8899         if (cpu_isar_feature(aa32_hpd, cpu)) {
8900             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8901         }
8902     }
8903     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8904         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8905     }
8906     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8907         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8908     }
8909     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8910         ARMCPRegInfo vapa_cp_reginfo[] = {
8911             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
8912               .access = PL1_RW, .resetvalue = 0,
8913               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
8914                                      offsetoflow32(CPUARMState, cp15.par_ns) },
8915               .writefn = par_write},
8916 #ifndef CONFIG_USER_ONLY
8917             /* This underdecoding is safe because the reginfo is NO_RAW. */
8918             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
8919               .access = PL1_W, .accessfn = ats_access,
8920               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
8921 #endif
8922         };
8923 
8924         /*
8925          * When LPAE exists this 32-bit PAR register is an alias of the
8926          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
8927          */
8928         if (arm_feature(env, ARM_FEATURE_LPAE)) {
8929             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
8930         }
8931         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8932     }
8933     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8934         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8935     }
8936     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8937         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8938     }
8939     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8940         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8941     }
8942     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8943         define_arm_cp_regs(cpu, omap_cp_reginfo);
8944     }
8945     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8946         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8947     }
8948     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8949         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8950     }
8951     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8952         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8953     }
8954     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8955         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8956     }
8957     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8958         define_arm_cp_regs(cpu, jazelle_regs);
8959     }
8960     /*
8961      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8962      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8963      * be read-only (ie write causes UNDEF exception).
8964      */
8965     {
8966         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8967             /*
8968              * Pre-v8 MIDR space.
8969              * Note that the MIDR isn't a simple constant register because
8970              * of the TI925 behaviour where writes to another register can
8971              * cause the MIDR value to change.
8972              *
8973              * Unimplemented registers in the c15 0 0 0 space default to
8974              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8975              * and friends override accordingly.
8976              */
8977             { .name = "MIDR",
8978               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8979               .access = PL1_R, .resetvalue = cpu->midr,
8980               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8981               .readfn = midr_read,
8982               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8983               .type = ARM_CP_OVERRIDE },
8984             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8985             { .name = "DUMMY",
8986               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8987               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8988             { .name = "DUMMY",
8989               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8990               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8991             { .name = "DUMMY",
8992               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8993               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8994             { .name = "DUMMY",
8995               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8996               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8997             { .name = "DUMMY",
8998               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8999               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9000         };
9001         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
9002             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
9003               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
9004               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
9005               .fgt = FGT_MIDR_EL1,
9006               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9007               .readfn = midr_read },
9008             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
9009             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
9010               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
9011               .access = PL1_R, .resetvalue = cpu->midr },
9012             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
9013               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
9014               .access = PL1_R,
9015               .accessfn = access_aa64_tid1,
9016               .fgt = FGT_REVIDR_EL1,
9017               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
9018         };
9019         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
9020             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
9021             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9022             .access = PL1_R, .resetvalue = cpu->midr
9023         };
9024         ARMCPRegInfo id_cp_reginfo[] = {
9025             /* These are common to v8 and pre-v8 */
9026             { .name = "CTR",
9027               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
9028               .access = PL1_R, .accessfn = ctr_el0_access,
9029               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9030             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9031               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9032               .access = PL0_R, .accessfn = ctr_el0_access,
9033               .fgt = FGT_CTR_EL0,
9034               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9035             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9036             { .name = "TCMTR",
9037               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
9038               .access = PL1_R,
9039               .accessfn = access_aa32_tid1,
9040               .type = ARM_CP_CONST, .resetvalue = 0 },
9041         };
9042         /* TLBTR is specific to VMSA */
9043         ARMCPRegInfo id_tlbtr_reginfo = {
9044               .name = "TLBTR",
9045               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
9046               .access = PL1_R,
9047               .accessfn = access_aa32_tid1,
9048               .type = ARM_CP_CONST, .resetvalue = 0,
9049         };
9050         /* MPUIR is specific to PMSA V6+ */
9051         ARMCPRegInfo id_mpuir_reginfo = {
9052               .name = "MPUIR",
9053               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9054               .access = PL1_R, .type = ARM_CP_CONST,
9055               .resetvalue = cpu->pmsav7_dregion << 8
9056         };
9057         /* HMPUIR is specific to PMSA V8 */
9058         ARMCPRegInfo id_hmpuir_reginfo = {
9059             .name = "HMPUIR",
9060             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9061             .access = PL2_R, .type = ARM_CP_CONST,
9062             .resetvalue = cpu->pmsav8r_hdregion
9063         };
9064         static const ARMCPRegInfo crn0_wi_reginfo = {
9065             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9066             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9067             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9068         };
9069 #ifdef CONFIG_USER_ONLY
9070         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
9071             { .name = "MIDR_EL1",
9072               .exported_bits = R_MIDR_EL1_REVISION_MASK |
9073                                R_MIDR_EL1_PARTNUM_MASK |
9074                                R_MIDR_EL1_ARCHITECTURE_MASK |
9075                                R_MIDR_EL1_VARIANT_MASK |
9076                                R_MIDR_EL1_IMPLEMENTER_MASK },
9077             { .name = "REVIDR_EL1" },
9078         };
9079         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9080 #endif
9081         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9082             arm_feature(env, ARM_FEATURE_STRONGARM)) {
9083             size_t i;
9084             /*
9085              * Register the blanket "writes ignored" value first to cover the
9086              * whole space. Then update the specific ID registers to allow write
9087              * access, so that they ignore writes rather than causing them to
9088              * UNDEF.
9089              */
9090             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
9091             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9092                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
9093             }
9094             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9095                 id_cp_reginfo[i].access = PL1_RW;
9096             }
9097             id_mpuir_reginfo.access = PL1_RW;
9098             id_tlbtr_reginfo.access = PL1_RW;
9099         }
9100         if (arm_feature(env, ARM_FEATURE_V8)) {
9101             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
9102             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9103                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9104             }
9105         } else {
9106             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9107         }
9108         define_arm_cp_regs(cpu, id_cp_reginfo);
9109         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9110             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
9111         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9112                    arm_feature(env, ARM_FEATURE_V8)) {
9113             uint32_t i = 0;
9114             char *tmp_string;
9115 
9116             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9117             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9118             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9119 
9120             /* Register alias is only valid for first 32 indexes */
9121             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9122                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9123                 uint8_t opc1 = extract32(i, 4, 1);
9124                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9125 
9126                 tmp_string = g_strdup_printf("PRBAR%u", i);
9127                 ARMCPRegInfo tmp_prbarn_reginfo = {
9128                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9129                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9130                     .access = PL1_RW, .resetvalue = 0,
9131                     .accessfn = access_tvm_trvm,
9132                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9133                 };
9134                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9135                 g_free(tmp_string);
9136 
9137                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9138                 tmp_string = g_strdup_printf("PRLAR%u", i);
9139                 ARMCPRegInfo tmp_prlarn_reginfo = {
9140                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9141                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9142                     .access = PL1_RW, .resetvalue = 0,
9143                     .accessfn = access_tvm_trvm,
9144                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9145                 };
9146                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9147                 g_free(tmp_string);
9148             }
9149 
9150             /* Register alias is only valid for first 32 indexes */
9151             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9152                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9153                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9154                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9155 
9156                 tmp_string = g_strdup_printf("HPRBAR%u", i);
9157                 ARMCPRegInfo tmp_hprbarn_reginfo = {
9158                     .name = tmp_string,
9159                     .type = ARM_CP_NO_RAW,
9160                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9161                     .access = PL2_RW, .resetvalue = 0,
9162                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9163                 };
9164                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9165                 g_free(tmp_string);
9166 
9167                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9168                 tmp_string = g_strdup_printf("HPRLAR%u", i);
9169                 ARMCPRegInfo tmp_hprlarn_reginfo = {
9170                     .name = tmp_string,
9171                     .type = ARM_CP_NO_RAW,
9172                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9173                     .access = PL2_RW, .resetvalue = 0,
9174                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9175                 };
9176                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9177                 g_free(tmp_string);
9178             }
9179         } else if (arm_feature(env, ARM_FEATURE_V7)) {
9180             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9181         }
9182     }
9183 
9184     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
9185         ARMCPRegInfo mpidr_cp_reginfo[] = {
9186             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9187               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
9188               .fgt = FGT_MPIDR_EL1,
9189               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
9190         };
9191 #ifdef CONFIG_USER_ONLY
9192         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
9193             { .name = "MPIDR_EL1",
9194               .fixed_bits = 0x0000000080000000 },
9195         };
9196         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9197 #endif
9198         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9199     }
9200 
9201     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
9202         ARMCPRegInfo auxcr_reginfo[] = {
9203             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9204               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
9205               .access = PL1_RW, .accessfn = access_tacr,
9206               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
9207             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9208               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9209               .access = PL2_RW, .type = ARM_CP_CONST,
9210               .resetvalue = 0 },
9211             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9212               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9213               .access = PL3_RW, .type = ARM_CP_CONST,
9214               .resetvalue = 0 },
9215         };
9216         define_arm_cp_regs(cpu, auxcr_reginfo);
9217         if (cpu_isar_feature(aa32_ac2, cpu)) {
9218             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
9219         }
9220     }
9221 
9222     if (arm_feature(env, ARM_FEATURE_CBAR)) {
9223         /*
9224          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9225          * There are two flavours:
9226          *  (1) older 32-bit only cores have a simple 32-bit CBAR
9227          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9228          *      32-bit register visible to AArch32 at a different encoding
9229          *      to the "flavour 1" register and with the bits rearranged to
9230          *      be able to squash a 64-bit address into the 32-bit view.
9231          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9232          * in future if we support AArch32-only configs of some of the
9233          * AArch64 cores we might need to add a specific feature flag
9234          * to indicate cores with "flavour 2" CBAR.
9235          */
9236         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9237             /* 32 bit view is [31:18] 0...0 [43:32]. */
9238             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
9239                 | extract64(cpu->reset_cbar, 32, 12);
9240             ARMCPRegInfo cbar_reginfo[] = {
9241                 { .name = "CBAR",
9242                   .type = ARM_CP_CONST,
9243                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
9244                   .access = PL1_R, .resetvalue = cbar32 },
9245                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
9246                   .type = ARM_CP_CONST,
9247                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
9248                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
9249             };
9250             /* We don't implement a r/w 64 bit CBAR currently */
9251             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
9252             define_arm_cp_regs(cpu, cbar_reginfo);
9253         } else {
9254             ARMCPRegInfo cbar = {
9255                 .name = "CBAR",
9256                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
9257                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
9258                 .fieldoffset = offsetof(CPUARMState,
9259                                         cp15.c15_config_base_address)
9260             };
9261             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
9262                 cbar.access = PL1_R;
9263                 cbar.fieldoffset = 0;
9264                 cbar.type = ARM_CP_CONST;
9265             }
9266             define_one_arm_cp_reg(cpu, &cbar);
9267         }
9268     }
9269 
9270     if (arm_feature(env, ARM_FEATURE_VBAR)) {
9271         static const ARMCPRegInfo vbar_cp_reginfo[] = {
9272             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
9273               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
9274               .access = PL1_RW, .writefn = vbar_write,
9275               .fgt = FGT_VBAR_EL1,
9276               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
9277                                      offsetof(CPUARMState, cp15.vbar_ns) },
9278               .resetvalue = 0 },
9279         };
9280         define_arm_cp_regs(cpu, vbar_cp_reginfo);
9281     }
9282 
9283     /* Generic registers whose values depend on the implementation */
9284     {
9285         ARMCPRegInfo sctlr = {
9286             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
9287             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
9288             .access = PL1_RW, .accessfn = access_tvm_trvm,
9289             .fgt = FGT_SCTLR_EL1,
9290             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9291                                    offsetof(CPUARMState, cp15.sctlr_ns) },
9292             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9293             .raw_writefn = raw_write,
9294         };
9295         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9296             /*
9297              * Normally we would always end the TB on an SCTLR write, but Linux
9298              * arch/arm/mach-pxa/sleep.S expects two instructions following
9299              * an MMU enable to execute from cache.  Imitate this behaviour.
9300              */
9301             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9302         }
9303         define_one_arm_cp_reg(cpu, &sctlr);
9304 
9305         if (arm_feature(env, ARM_FEATURE_PMSA) &&
9306             arm_feature(env, ARM_FEATURE_V8)) {
9307             ARMCPRegInfo vsctlr = {
9308                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9309                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9310                 .access = PL2_RW, .resetvalue = 0x0,
9311                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9312             };
9313             define_one_arm_cp_reg(cpu, &vsctlr);
9314         }
9315     }
9316 
9317     if (cpu_isar_feature(aa64_lor, cpu)) {
9318         define_arm_cp_regs(cpu, lor_reginfo);
9319     }
9320     if (cpu_isar_feature(aa64_pan, cpu)) {
9321         define_one_arm_cp_reg(cpu, &pan_reginfo);
9322     }
9323 #ifndef CONFIG_USER_ONLY
9324     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9325         define_arm_cp_regs(cpu, ats1e1_reginfo);
9326     }
9327     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9328         define_arm_cp_regs(cpu, ats1cp_reginfo);
9329     }
9330 #endif
9331     if (cpu_isar_feature(aa64_uao, cpu)) {
9332         define_one_arm_cp_reg(cpu, &uao_reginfo);
9333     }
9334 
9335     if (cpu_isar_feature(aa64_dit, cpu)) {
9336         define_one_arm_cp_reg(cpu, &dit_reginfo);
9337     }
9338     if (cpu_isar_feature(aa64_ssbs, cpu)) {
9339         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9340     }
9341     if (cpu_isar_feature(any_ras, cpu)) {
9342         define_arm_cp_regs(cpu, minimal_ras_reginfo);
9343     }
9344 
9345     if (cpu_isar_feature(aa64_vh, cpu) ||
9346         cpu_isar_feature(aa64_debugv8p2, cpu)) {
9347         define_one_arm_cp_reg(cpu, &contextidr_el2);
9348     }
9349     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9350         define_arm_cp_regs(cpu, vhe_reginfo);
9351     }
9352 
9353     if (cpu_isar_feature(aa64_sve, cpu)) {
9354         define_arm_cp_regs(cpu, zcr_reginfo);
9355     }
9356 
9357     if (cpu_isar_feature(aa64_hcx, cpu)) {
9358         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9359     }
9360 
9361 #ifdef TARGET_AARCH64
9362     if (cpu_isar_feature(aa64_sme, cpu)) {
9363         define_arm_cp_regs(cpu, sme_reginfo);
9364     }
9365     if (cpu_isar_feature(aa64_pauth, cpu)) {
9366         define_arm_cp_regs(cpu, pauth_reginfo);
9367     }
9368     if (cpu_isar_feature(aa64_rndr, cpu)) {
9369         define_arm_cp_regs(cpu, rndr_reginfo);
9370     }
9371     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9372         define_arm_cp_regs(cpu, tlbirange_reginfo);
9373     }
9374     if (cpu_isar_feature(aa64_tlbios, cpu)) {
9375         define_arm_cp_regs(cpu, tlbios_reginfo);
9376     }
9377     /* Data Cache clean instructions up to PoP */
9378     if (cpu_isar_feature(aa64_dcpop, cpu)) {
9379         define_one_arm_cp_reg(cpu, dcpop_reg);
9380 
9381         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9382             define_one_arm_cp_reg(cpu, dcpodp_reg);
9383         }
9384     }
9385 
9386     /*
9387      * If full MTE is enabled, add all of the system registers.
9388      * If only "instructions available at EL0" are enabled,
9389      * then define only a RAZ/WI version of PSTATE.TCO.
9390      */
9391     if (cpu_isar_feature(aa64_mte, cpu)) {
9392         ARMCPRegInfo gmid_reginfo = {
9393             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
9394             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
9395             .access = PL1_R, .accessfn = access_aa64_tid5,
9396             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
9397         };
9398         define_one_arm_cp_reg(cpu, &gmid_reginfo);
9399         define_arm_cp_regs(cpu, mte_reginfo);
9400         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9401     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9402         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
9403         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9404     }
9405 
9406     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9407         define_arm_cp_regs(cpu, scxtnum_reginfo);
9408     }
9409 
9410     if (cpu_isar_feature(aa64_fgt, cpu)) {
9411         define_arm_cp_regs(cpu, fgt_reginfo);
9412     }
9413 
9414     if (cpu_isar_feature(aa64_rme, cpu)) {
9415         define_arm_cp_regs(cpu, rme_reginfo);
9416         if (cpu_isar_feature(aa64_mte, cpu)) {
9417             define_arm_cp_regs(cpu, rme_mte_reginfo);
9418         }
9419     }
9420 #endif
9421 
9422     if (cpu_isar_feature(any_predinv, cpu)) {
9423         define_arm_cp_regs(cpu, predinv_reginfo);
9424     }
9425 
9426     if (cpu_isar_feature(any_ccidx, cpu)) {
9427         define_arm_cp_regs(cpu, ccsidr2_reginfo);
9428     }
9429 
9430 #ifndef CONFIG_USER_ONLY
9431     /*
9432      * Register redirections and aliases must be done last,
9433      * after the registers from the other extensions have been defined.
9434      */
9435     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9436         define_arm_vh_e2h_redirects_aliases(cpu);
9437     }
9438 #endif
9439 }
9440 
9441 /* Sort alphabetically by type name, except for "any". */
9442 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
9443 {
9444     ObjectClass *class_a = (ObjectClass *)a;
9445     ObjectClass *class_b = (ObjectClass *)b;
9446     const char *name_a, *name_b;
9447 
9448     name_a = object_class_get_name(class_a);
9449     name_b = object_class_get_name(class_b);
9450     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
9451         return 1;
9452     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
9453         return -1;
9454     } else {
9455         return strcmp(name_a, name_b);
9456     }
9457 }
9458 
9459 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
9460 {
9461     ObjectClass *oc = data;
9462     CPUClass *cc = CPU_CLASS(oc);
9463     const char *typename;
9464     char *name;
9465 
9466     typename = object_class_get_name(oc);
9467     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
9468     if (cc->deprecation_note) {
9469         qemu_printf("  %s (deprecated)\n", name);
9470     } else {
9471         qemu_printf("  %s\n", name);
9472     }
9473     g_free(name);
9474 }
9475 
9476 void arm_cpu_list(void)
9477 {
9478     GSList *list;
9479 
9480     list = object_class_get_list(TYPE_ARM_CPU, false);
9481     list = g_slist_sort(list, arm_cpu_list_compare);
9482     qemu_printf("Available CPUs:\n");
9483     g_slist_foreach(list, arm_cpu_list_entry, NULL);
9484     g_slist_free(list);
9485 }
9486 
9487 /*
9488  * Private utility function for define_one_arm_cp_reg_with_opaque():
9489  * add a single reginfo struct to the hash table.
9490  */
9491 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9492                                    void *opaque, CPState state,
9493                                    CPSecureState secstate,
9494                                    int crm, int opc1, int opc2,
9495                                    const char *name)
9496 {
9497     CPUARMState *env = &cpu->env;
9498     uint32_t key;
9499     ARMCPRegInfo *r2;
9500     bool is64 = r->type & ARM_CP_64BIT;
9501     bool ns = secstate & ARM_CP_SECSTATE_NS;
9502     int cp = r->cp;
9503     size_t name_len;
9504     bool make_const;
9505 
9506     switch (state) {
9507     case ARM_CP_STATE_AA32:
9508         /* We assume it is a cp15 register if the .cp field is left unset. */
9509         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9510             cp = 15;
9511         }
9512         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9513         break;
9514     case ARM_CP_STATE_AA64:
9515         /*
9516          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9517          * cp == 0 as equivalent to the value for "standard guest-visible
9518          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9519          * in their AArch64 view (the .cp value may be non-zero for the
9520          * benefit of the AArch32 view).
9521          */
9522         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9523             cp = CP_REG_ARM64_SYSREG_CP;
9524         }
9525         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9526         break;
9527     default:
9528         g_assert_not_reached();
9529     }
9530 
9531     /* Overriding of an existing definition must be explicitly requested. */
9532     if (!(r->type & ARM_CP_OVERRIDE)) {
9533         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9534         if (oldreg) {
9535             assert(oldreg->type & ARM_CP_OVERRIDE);
9536         }
9537     }
9538 
9539     /*
9540      * Eliminate registers that are not present because the EL is missing.
9541      * Doing this here makes it easier to put all registers for a given
9542      * feature into the same ARMCPRegInfo array and define them all at once.
9543      */
9544     make_const = false;
9545     if (arm_feature(env, ARM_FEATURE_EL3)) {
9546         /*
9547          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9548          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9549          */
9550         int min_el = ctz32(r->access) / 2;
9551         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9552             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9553                 return;
9554             }
9555             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9556         }
9557     } else {
9558         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9559                                  ? PL2_RW : PL1_RW);
9560         if ((r->access & max_el) == 0) {
9561             return;
9562         }
9563     }
9564 
9565     /* Combine cpreg and name into one allocation. */
9566     name_len = strlen(name) + 1;
9567     r2 = g_malloc(sizeof(*r2) + name_len);
9568     *r2 = *r;
9569     r2->name = memcpy(r2 + 1, name, name_len);
9570 
9571     /*
9572      * Update fields to match the instantiation, overwiting wildcards
9573      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9574      */
9575     r2->cp = cp;
9576     r2->crm = crm;
9577     r2->opc1 = opc1;
9578     r2->opc2 = opc2;
9579     r2->state = state;
9580     r2->secure = secstate;
9581     if (opaque) {
9582         r2->opaque = opaque;
9583     }
9584 
9585     if (make_const) {
9586         /* This should not have been a very special register to begin. */
9587         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9588         assert(old_special == 0 || old_special == ARM_CP_NOP);
9589         /*
9590          * Set the special function to CONST, retaining the other flags.
9591          * This is important for e.g. ARM_CP_SVE so that we still
9592          * take the SVE trap if CPTR_EL3.EZ == 0.
9593          */
9594         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9595         /*
9596          * Usually, these registers become RES0, but there are a few
9597          * special cases like VPIDR_EL2 which have a constant non-zero
9598          * value with writes ignored.
9599          */
9600         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9601             r2->resetvalue = 0;
9602         }
9603         /*
9604          * ARM_CP_CONST has precedence, so removing the callbacks and
9605          * offsets are not strictly necessary, but it is potentially
9606          * less confusing to debug later.
9607          */
9608         r2->readfn = NULL;
9609         r2->writefn = NULL;
9610         r2->raw_readfn = NULL;
9611         r2->raw_writefn = NULL;
9612         r2->resetfn = NULL;
9613         r2->fieldoffset = 0;
9614         r2->bank_fieldoffsets[0] = 0;
9615         r2->bank_fieldoffsets[1] = 0;
9616     } else {
9617         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9618 
9619         if (isbanked) {
9620             /*
9621              * Register is banked (using both entries in array).
9622              * Overwriting fieldoffset as the array is only used to define
9623              * banked registers but later only fieldoffset is used.
9624              */
9625             r2->fieldoffset = r->bank_fieldoffsets[ns];
9626         }
9627         if (state == ARM_CP_STATE_AA32) {
9628             if (isbanked) {
9629                 /*
9630                  * If the register is banked then we don't need to migrate or
9631                  * reset the 32-bit instance in certain cases:
9632                  *
9633                  * 1) If the register has both 32-bit and 64-bit instances
9634                  *    then we can count on the 64-bit instance taking care
9635                  *    of the non-secure bank.
9636                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9637                  *    version taking care of the secure bank.  This requires
9638                  *    that separate 32 and 64-bit definitions are provided.
9639                  */
9640                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9641                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9642                     r2->type |= ARM_CP_ALIAS;
9643                 }
9644             } else if ((secstate != r->secure) && !ns) {
9645                 /*
9646                  * The register is not banked so we only want to allow
9647                  * migration of the non-secure instance.
9648                  */
9649                 r2->type |= ARM_CP_ALIAS;
9650             }
9651 
9652             if (HOST_BIG_ENDIAN &&
9653                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9654                 r2->fieldoffset += sizeof(uint32_t);
9655             }
9656         }
9657     }
9658 
9659     /*
9660      * By convention, for wildcarded registers only the first
9661      * entry is used for migration; the others are marked as
9662      * ALIAS so we don't try to transfer the register
9663      * multiple times. Special registers (ie NOP/WFI) are
9664      * never migratable and not even raw-accessible.
9665      */
9666     if (r2->type & ARM_CP_SPECIAL_MASK) {
9667         r2->type |= ARM_CP_NO_RAW;
9668     }
9669     if (((r->crm == CP_ANY) && crm != 0) ||
9670         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9671         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9672         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9673     }
9674 
9675     /*
9676      * Check that raw accesses are either forbidden or handled. Note that
9677      * we can't assert this earlier because the setup of fieldoffset for
9678      * banked registers has to be done first.
9679      */
9680     if (!(r2->type & ARM_CP_NO_RAW)) {
9681         assert(!raw_accessors_invalid(r2));
9682     }
9683 
9684     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9685 }
9686 
9687 
9688 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9689                                        const ARMCPRegInfo *r, void *opaque)
9690 {
9691     /*
9692      * Define implementations of coprocessor registers.
9693      * We store these in a hashtable because typically
9694      * there are less than 150 registers in a space which
9695      * is 16*16*16*8*8 = 262144 in size.
9696      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9697      * If a register is defined twice then the second definition is
9698      * used, so this can be used to define some generic registers and
9699      * then override them with implementation specific variations.
9700      * At least one of the original and the second definition should
9701      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9702      * against accidental use.
9703      *
9704      * The state field defines whether the register is to be
9705      * visible in the AArch32 or AArch64 execution state. If the
9706      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9707      * reginfo structure for the AArch32 view, which sees the lower
9708      * 32 bits of the 64 bit register.
9709      *
9710      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9711      * be wildcarded. AArch64 registers are always considered to be 64
9712      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9713      * the register, if any.
9714      */
9715     int crm, opc1, opc2;
9716     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9717     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9718     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9719     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9720     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9721     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9722     CPState state;
9723 
9724     /* 64 bit registers have only CRm and Opc1 fields */
9725     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9726     /* op0 only exists in the AArch64 encodings */
9727     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9728     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9729     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9730     /*
9731      * This API is only for Arm's system coprocessors (14 and 15) or
9732      * (M-profile or v7A-and-earlier only) for implementation defined
9733      * coprocessors in the range 0..7.  Our decode assumes this, since
9734      * 8..13 can be used for other insns including VFP and Neon. See
9735      * valid_cp() in translate.c.  Assert here that we haven't tried
9736      * to use an invalid coprocessor number.
9737      */
9738     switch (r->state) {
9739     case ARM_CP_STATE_BOTH:
9740         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9741         if (r->cp == 0) {
9742             break;
9743         }
9744         /* fall through */
9745     case ARM_CP_STATE_AA32:
9746         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9747             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9748             assert(r->cp >= 14 && r->cp <= 15);
9749         } else {
9750             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9751         }
9752         break;
9753     case ARM_CP_STATE_AA64:
9754         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9755         break;
9756     default:
9757         g_assert_not_reached();
9758     }
9759     /*
9760      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9761      * encodes a minimum access level for the register. We roll this
9762      * runtime check into our general permission check code, so check
9763      * here that the reginfo's specified permissions are strict enough
9764      * to encompass the generic architectural permission check.
9765      */
9766     if (r->state != ARM_CP_STATE_AA32) {
9767         CPAccessRights mask;
9768         switch (r->opc1) {
9769         case 0:
9770             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9771             mask = PL0U_R | PL1_RW;
9772             break;
9773         case 1: case 2:
9774             /* min_EL EL1 */
9775             mask = PL1_RW;
9776             break;
9777         case 3:
9778             /* min_EL EL0 */
9779             mask = PL0_RW;
9780             break;
9781         case 4:
9782         case 5:
9783             /* min_EL EL2 */
9784             mask = PL2_RW;
9785             break;
9786         case 6:
9787             /* min_EL EL3 */
9788             mask = PL3_RW;
9789             break;
9790         case 7:
9791             /* min_EL EL1, secure mode only (we don't check the latter) */
9792             mask = PL1_RW;
9793             break;
9794         default:
9795             /* broken reginfo with out-of-range opc1 */
9796             g_assert_not_reached();
9797         }
9798         /* assert our permissions are not too lax (stricter is fine) */
9799         assert((r->access & ~mask) == 0);
9800     }
9801 
9802     /*
9803      * Check that the register definition has enough info to handle
9804      * reads and writes if they are permitted.
9805      */
9806     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9807         if (r->access & PL3_R) {
9808             assert((r->fieldoffset ||
9809                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9810                    r->readfn);
9811         }
9812         if (r->access & PL3_W) {
9813             assert((r->fieldoffset ||
9814                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9815                    r->writefn);
9816         }
9817     }
9818 
9819     for (crm = crmmin; crm <= crmmax; crm++) {
9820         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9821             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9822                 for (state = ARM_CP_STATE_AA32;
9823                      state <= ARM_CP_STATE_AA64; state++) {
9824                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9825                         continue;
9826                     }
9827                     if (state == ARM_CP_STATE_AA32) {
9828                         /*
9829                          * Under AArch32 CP registers can be common
9830                          * (same for secure and non-secure world) or banked.
9831                          */
9832                         char *name;
9833 
9834                         switch (r->secure) {
9835                         case ARM_CP_SECSTATE_S:
9836                         case ARM_CP_SECSTATE_NS:
9837                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9838                                                    r->secure, crm, opc1, opc2,
9839                                                    r->name);
9840                             break;
9841                         case ARM_CP_SECSTATE_BOTH:
9842                             name = g_strdup_printf("%s_S", r->name);
9843                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9844                                                    ARM_CP_SECSTATE_S,
9845                                                    crm, opc1, opc2, name);
9846                             g_free(name);
9847                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9848                                                    ARM_CP_SECSTATE_NS,
9849                                                    crm, opc1, opc2, r->name);
9850                             break;
9851                         default:
9852                             g_assert_not_reached();
9853                         }
9854                     } else {
9855                         /*
9856                          * AArch64 registers get mapped to non-secure instance
9857                          * of AArch32
9858                          */
9859                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9860                                                ARM_CP_SECSTATE_NS,
9861                                                crm, opc1, opc2, r->name);
9862                     }
9863                 }
9864             }
9865         }
9866     }
9867 }
9868 
9869 /* Define a whole list of registers */
9870 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9871                                         void *opaque, size_t len)
9872 {
9873     size_t i;
9874     for (i = 0; i < len; ++i) {
9875         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9876     }
9877 }
9878 
9879 /*
9880  * Modify ARMCPRegInfo for access from userspace.
9881  *
9882  * This is a data driven modification directed by
9883  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9884  * user-space cannot alter any values and dynamic values pertaining to
9885  * execution state are hidden from user space view anyway.
9886  */
9887 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9888                                  const ARMCPRegUserSpaceInfo *mods,
9889                                  size_t mods_len)
9890 {
9891     for (size_t mi = 0; mi < mods_len; ++mi) {
9892         const ARMCPRegUserSpaceInfo *m = mods + mi;
9893         GPatternSpec *pat = NULL;
9894 
9895         if (m->is_glob) {
9896             pat = g_pattern_spec_new(m->name);
9897         }
9898         for (size_t ri = 0; ri < regs_len; ++ri) {
9899             ARMCPRegInfo *r = regs + ri;
9900 
9901             if (pat && g_pattern_match_string(pat, r->name)) {
9902                 r->type = ARM_CP_CONST;
9903                 r->access = PL0U_R;
9904                 r->resetvalue = 0;
9905                 /* continue */
9906             } else if (strcmp(r->name, m->name) == 0) {
9907                 r->type = ARM_CP_CONST;
9908                 r->access = PL0U_R;
9909                 r->resetvalue &= m->exported_bits;
9910                 r->resetvalue |= m->fixed_bits;
9911                 break;
9912             }
9913         }
9914         if (pat) {
9915             g_pattern_spec_free(pat);
9916         }
9917     }
9918 }
9919 
9920 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9921 {
9922     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9923 }
9924 
9925 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9926                          uint64_t value)
9927 {
9928     /* Helper coprocessor write function for write-ignore registers */
9929 }
9930 
9931 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9932 {
9933     /* Helper coprocessor write function for read-as-zero registers */
9934     return 0;
9935 }
9936 
9937 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9938 {
9939     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9940 }
9941 
9942 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9943 {
9944     /*
9945      * Return true if it is not valid for us to switch to
9946      * this CPU mode (ie all the UNPREDICTABLE cases in
9947      * the ARM ARM CPSRWriteByInstr pseudocode).
9948      */
9949 
9950     /* Changes to or from Hyp via MSR and CPS are illegal. */
9951     if (write_type == CPSRWriteByInstr &&
9952         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9953          mode == ARM_CPU_MODE_HYP)) {
9954         return 1;
9955     }
9956 
9957     switch (mode) {
9958     case ARM_CPU_MODE_USR:
9959         return 0;
9960     case ARM_CPU_MODE_SYS:
9961     case ARM_CPU_MODE_SVC:
9962     case ARM_CPU_MODE_ABT:
9963     case ARM_CPU_MODE_UND:
9964     case ARM_CPU_MODE_IRQ:
9965     case ARM_CPU_MODE_FIQ:
9966         /*
9967          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9968          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9969          */
9970         /*
9971          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9972          * and CPS are treated as illegal mode changes.
9973          */
9974         if (write_type == CPSRWriteByInstr &&
9975             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9976             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9977             return 1;
9978         }
9979         return 0;
9980     case ARM_CPU_MODE_HYP:
9981         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9982     case ARM_CPU_MODE_MON:
9983         return arm_current_el(env) < 3;
9984     default:
9985         return 1;
9986     }
9987 }
9988 
9989 uint32_t cpsr_read(CPUARMState *env)
9990 {
9991     int ZF;
9992     ZF = (env->ZF == 0);
9993     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9994         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9995         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9996         | ((env->condexec_bits & 0xfc) << 8)
9997         | (env->GE << 16) | (env->daif & CPSR_AIF);
9998 }
9999 
10000 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
10001                 CPSRWriteType write_type)
10002 {
10003     uint32_t changed_daif;
10004     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
10005         (mask & (CPSR_M | CPSR_E | CPSR_IL));
10006 
10007     if (mask & CPSR_NZCV) {
10008         env->ZF = (~val) & CPSR_Z;
10009         env->NF = val;
10010         env->CF = (val >> 29) & 1;
10011         env->VF = (val << 3) & 0x80000000;
10012     }
10013     if (mask & CPSR_Q) {
10014         env->QF = ((val & CPSR_Q) != 0);
10015     }
10016     if (mask & CPSR_T) {
10017         env->thumb = ((val & CPSR_T) != 0);
10018     }
10019     if (mask & CPSR_IT_0_1) {
10020         env->condexec_bits &= ~3;
10021         env->condexec_bits |= (val >> 25) & 3;
10022     }
10023     if (mask & CPSR_IT_2_7) {
10024         env->condexec_bits &= 3;
10025         env->condexec_bits |= (val >> 8) & 0xfc;
10026     }
10027     if (mask & CPSR_GE) {
10028         env->GE = (val >> 16) & 0xf;
10029     }
10030 
10031     /*
10032      * In a V7 implementation that includes the security extensions but does
10033      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10034      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10035      * bits respectively.
10036      *
10037      * In a V8 implementation, it is permitted for privileged software to
10038      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10039      */
10040     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
10041         arm_feature(env, ARM_FEATURE_EL3) &&
10042         !arm_feature(env, ARM_FEATURE_EL2) &&
10043         !arm_is_secure(env)) {
10044 
10045         changed_daif = (env->daif ^ val) & mask;
10046 
10047         if (changed_daif & CPSR_A) {
10048             /*
10049              * Check to see if we are allowed to change the masking of async
10050              * abort exceptions from a non-secure state.
10051              */
10052             if (!(env->cp15.scr_el3 & SCR_AW)) {
10053                 qemu_log_mask(LOG_GUEST_ERROR,
10054                               "Ignoring attempt to switch CPSR_A flag from "
10055                               "non-secure world with SCR.AW bit clear\n");
10056                 mask &= ~CPSR_A;
10057             }
10058         }
10059 
10060         if (changed_daif & CPSR_F) {
10061             /*
10062              * Check to see if we are allowed to change the masking of FIQ
10063              * exceptions from a non-secure state.
10064              */
10065             if (!(env->cp15.scr_el3 & SCR_FW)) {
10066                 qemu_log_mask(LOG_GUEST_ERROR,
10067                               "Ignoring attempt to switch CPSR_F flag from "
10068                               "non-secure world with SCR.FW bit clear\n");
10069                 mask &= ~CPSR_F;
10070             }
10071 
10072             /*
10073              * Check whether non-maskable FIQ (NMFI) support is enabled.
10074              * If this bit is set software is not allowed to mask
10075              * FIQs, but is allowed to set CPSR_F to 0.
10076              */
10077             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10078                 (val & CPSR_F)) {
10079                 qemu_log_mask(LOG_GUEST_ERROR,
10080                               "Ignoring attempt to enable CPSR_F flag "
10081                               "(non-maskable FIQ [NMFI] support enabled)\n");
10082                 mask &= ~CPSR_F;
10083             }
10084         }
10085     }
10086 
10087     env->daif &= ~(CPSR_AIF & mask);
10088     env->daif |= val & CPSR_AIF & mask;
10089 
10090     if (write_type != CPSRWriteRaw &&
10091         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
10092         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
10093             /*
10094              * Note that we can only get here in USR mode if this is a
10095              * gdb stub write; for this case we follow the architectural
10096              * behaviour for guest writes in USR mode of ignoring an attempt
10097              * to switch mode. (Those are caught by translate.c for writes
10098              * triggered by guest instructions.)
10099              */
10100             mask &= ~CPSR_M;
10101         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
10102             /*
10103              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
10104              * v7, and has defined behaviour in v8:
10105              *  + leave CPSR.M untouched
10106              *  + allow changes to the other CPSR fields
10107              *  + set PSTATE.IL
10108              * For user changes via the GDB stub, we don't set PSTATE.IL,
10109              * as this would be unnecessarily harsh for a user error.
10110              */
10111             mask &= ~CPSR_M;
10112             if (write_type != CPSRWriteByGDBStub &&
10113                 arm_feature(env, ARM_FEATURE_V8)) {
10114                 mask |= CPSR_IL;
10115                 val |= CPSR_IL;
10116             }
10117             qemu_log_mask(LOG_GUEST_ERROR,
10118                           "Illegal AArch32 mode switch attempt from %s to %s\n",
10119                           aarch32_mode_name(env->uncached_cpsr),
10120                           aarch32_mode_name(val));
10121         } else {
10122             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10123                           write_type == CPSRWriteExceptionReturn ?
10124                           "Exception return from AArch32" :
10125                           "AArch32 mode switch from",
10126                           aarch32_mode_name(env->uncached_cpsr),
10127                           aarch32_mode_name(val), env->regs[15]);
10128             switch_mode(env, val & CPSR_M);
10129         }
10130     }
10131     mask &= ~CACHED_CPSR_BITS;
10132     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
10133     if (tcg_enabled() && rebuild_hflags) {
10134         arm_rebuild_hflags(env);
10135     }
10136 }
10137 
10138 /* Sign/zero extend */
10139 uint32_t HELPER(sxtb16)(uint32_t x)
10140 {
10141     uint32_t res;
10142     res = (uint16_t)(int8_t)x;
10143     res |= (uint32_t)(int8_t)(x >> 16) << 16;
10144     return res;
10145 }
10146 
10147 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
10148 {
10149     /*
10150      * Take a division-by-zero exception if necessary; otherwise return
10151      * to get the usual non-trapping division behaviour (result of 0)
10152      */
10153     if (arm_feature(env, ARM_FEATURE_M)
10154         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
10155         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
10156     }
10157 }
10158 
10159 uint32_t HELPER(uxtb16)(uint32_t x)
10160 {
10161     uint32_t res;
10162     res = (uint16_t)(uint8_t)x;
10163     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
10164     return res;
10165 }
10166 
10167 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
10168 {
10169     if (den == 0) {
10170         handle_possible_div0_trap(env, GETPC());
10171         return 0;
10172     }
10173     if (num == INT_MIN && den == -1) {
10174         return INT_MIN;
10175     }
10176     return num / den;
10177 }
10178 
10179 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
10180 {
10181     if (den == 0) {
10182         handle_possible_div0_trap(env, GETPC());
10183         return 0;
10184     }
10185     return num / den;
10186 }
10187 
10188 uint32_t HELPER(rbit)(uint32_t x)
10189 {
10190     return revbit32(x);
10191 }
10192 
10193 #ifdef CONFIG_USER_ONLY
10194 
10195 static void switch_mode(CPUARMState *env, int mode)
10196 {
10197     ARMCPU *cpu = env_archcpu(env);
10198 
10199     if (mode != ARM_CPU_MODE_USR) {
10200         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10201     }
10202 }
10203 
10204 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10205                                  uint32_t cur_el, bool secure)
10206 {
10207     return 1;
10208 }
10209 
10210 void aarch64_sync_64_to_32(CPUARMState *env)
10211 {
10212     g_assert_not_reached();
10213 }
10214 
10215 #else
10216 
10217 static void switch_mode(CPUARMState *env, int mode)
10218 {
10219     int old_mode;
10220     int i;
10221 
10222     old_mode = env->uncached_cpsr & CPSR_M;
10223     if (mode == old_mode) {
10224         return;
10225     }
10226 
10227     if (old_mode == ARM_CPU_MODE_FIQ) {
10228         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10229         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
10230     } else if (mode == ARM_CPU_MODE_FIQ) {
10231         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10232         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
10233     }
10234 
10235     i = bank_number(old_mode);
10236     env->banked_r13[i] = env->regs[13];
10237     env->banked_spsr[i] = env->spsr;
10238 
10239     i = bank_number(mode);
10240     env->regs[13] = env->banked_r13[i];
10241     env->spsr = env->banked_spsr[i];
10242 
10243     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10244     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
10245 }
10246 
10247 /*
10248  * Physical Interrupt Target EL Lookup Table
10249  *
10250  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10251  *
10252  * The below multi-dimensional table is used for looking up the target
10253  * exception level given numerous condition criteria.  Specifically, the
10254  * target EL is based on SCR and HCR routing controls as well as the
10255  * currently executing EL and secure state.
10256  *
10257  *    Dimensions:
10258  *    target_el_table[2][2][2][2][2][4]
10259  *                    |  |  |  |  |  +--- Current EL
10260  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
10261  *                    |  |  |  +--------- HCR mask override
10262  *                    |  |  +------------ SCR exec state control
10263  *                    |  +--------------- SCR mask override
10264  *                    +------------------ 32-bit(0)/64-bit(1) EL3
10265  *
10266  *    The table values are as such:
10267  *    0-3 = EL0-EL3
10268  *     -1 = Cannot occur
10269  *
10270  * The ARM ARM target EL table includes entries indicating that an "exception
10271  * is not taken".  The two cases where this is applicable are:
10272  *    1) An exception is taken from EL3 but the SCR does not have the exception
10273  *    routed to EL3.
10274  *    2) An exception is taken from EL2 but the HCR does not have the exception
10275  *    routed to EL2.
10276  * In these two cases, the below table contain a target of EL1.  This value is
10277  * returned as it is expected that the consumer of the table data will check
10278  * for "target EL >= current EL" to ensure the exception is not taken.
10279  *
10280  *            SCR     HCR
10281  *         64  EA     AMO                 From
10282  *        BIT IRQ     IMO      Non-secure         Secure
10283  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
10284  */
10285 static const int8_t target_el_table[2][2][2][2][2][4] = {
10286     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10287        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
10288       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10289        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
10290      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10291        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
10292       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10293        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
10294     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
10295        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
10296       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
10297        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
10298      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
10299        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
10300       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
10301        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
10302 };
10303 
10304 /*
10305  * Determine the target EL for physical exceptions
10306  */
10307 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10308                                  uint32_t cur_el, bool secure)
10309 {
10310     CPUARMState *env = cpu_env(cs);
10311     bool rw;
10312     bool scr;
10313     bool hcr;
10314     int target_el;
10315     /* Is the highest EL AArch64? */
10316     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10317     uint64_t hcr_el2;
10318 
10319     if (arm_feature(env, ARM_FEATURE_EL3)) {
10320         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10321     } else {
10322         /*
10323          * Either EL2 is the highest EL (and so the EL2 register width
10324          * is given by is64); or there is no EL2 or EL3, in which case
10325          * the value of 'rw' does not affect the table lookup anyway.
10326          */
10327         rw = is64;
10328     }
10329 
10330     hcr_el2 = arm_hcr_el2_eff(env);
10331     switch (excp_idx) {
10332     case EXCP_IRQ:
10333         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
10334         hcr = hcr_el2 & HCR_IMO;
10335         break;
10336     case EXCP_FIQ:
10337         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
10338         hcr = hcr_el2 & HCR_FMO;
10339         break;
10340     default:
10341         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
10342         hcr = hcr_el2 & HCR_AMO;
10343         break;
10344     };
10345 
10346     /*
10347      * For these purposes, TGE and AMO/IMO/FMO both force the
10348      * interrupt to EL2.  Fold TGE into the bit extracted above.
10349      */
10350     hcr |= (hcr_el2 & HCR_TGE) != 0;
10351 
10352     /* Perform a table-lookup for the target EL given the current state */
10353     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10354 
10355     assert(target_el > 0);
10356 
10357     return target_el;
10358 }
10359 
10360 void arm_log_exception(CPUState *cs)
10361 {
10362     int idx = cs->exception_index;
10363 
10364     if (qemu_loglevel_mask(CPU_LOG_INT)) {
10365         const char *exc = NULL;
10366         static const char * const excnames[] = {
10367             [EXCP_UDEF] = "Undefined Instruction",
10368             [EXCP_SWI] = "SVC",
10369             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10370             [EXCP_DATA_ABORT] = "Data Abort",
10371             [EXCP_IRQ] = "IRQ",
10372             [EXCP_FIQ] = "FIQ",
10373             [EXCP_BKPT] = "Breakpoint",
10374             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10375             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10376             [EXCP_HVC] = "Hypervisor Call",
10377             [EXCP_HYP_TRAP] = "Hypervisor Trap",
10378             [EXCP_SMC] = "Secure Monitor Call",
10379             [EXCP_VIRQ] = "Virtual IRQ",
10380             [EXCP_VFIQ] = "Virtual FIQ",
10381             [EXCP_SEMIHOST] = "Semihosting call",
10382             [EXCP_NOCP] = "v7M NOCP UsageFault",
10383             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10384             [EXCP_STKOF] = "v8M STKOF UsageFault",
10385             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10386             [EXCP_LSERR] = "v8M LSERR UsageFault",
10387             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
10388             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
10389             [EXCP_VSERR] = "Virtual SERR",
10390             [EXCP_GPC] = "Granule Protection Check",
10391         };
10392 
10393         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10394             exc = excnames[idx];
10395         }
10396         if (!exc) {
10397             exc = "unknown";
10398         }
10399         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10400                       idx, exc, cs->cpu_index);
10401     }
10402 }
10403 
10404 /*
10405  * Function used to synchronize QEMU's AArch64 register set with AArch32
10406  * register set.  This is necessary when switching between AArch32 and AArch64
10407  * execution state.
10408  */
10409 void aarch64_sync_32_to_64(CPUARMState *env)
10410 {
10411     int i;
10412     uint32_t mode = env->uncached_cpsr & CPSR_M;
10413 
10414     /* We can blanket copy R[0:7] to X[0:7] */
10415     for (i = 0; i < 8; i++) {
10416         env->xregs[i] = env->regs[i];
10417     }
10418 
10419     /*
10420      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10421      * Otherwise, they come from the banked user regs.
10422      */
10423     if (mode == ARM_CPU_MODE_FIQ) {
10424         for (i = 8; i < 13; i++) {
10425             env->xregs[i] = env->usr_regs[i - 8];
10426         }
10427     } else {
10428         for (i = 8; i < 13; i++) {
10429             env->xregs[i] = env->regs[i];
10430         }
10431     }
10432 
10433     /*
10434      * Registers x13-x23 are the various mode SP and FP registers. Registers
10435      * r13 and r14 are only copied if we are in that mode, otherwise we copy
10436      * from the mode banked register.
10437      */
10438     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10439         env->xregs[13] = env->regs[13];
10440         env->xregs[14] = env->regs[14];
10441     } else {
10442         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10443         /* HYP is an exception in that it is copied from r14 */
10444         if (mode == ARM_CPU_MODE_HYP) {
10445             env->xregs[14] = env->regs[14];
10446         } else {
10447             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
10448         }
10449     }
10450 
10451     if (mode == ARM_CPU_MODE_HYP) {
10452         env->xregs[15] = env->regs[13];
10453     } else {
10454         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
10455     }
10456 
10457     if (mode == ARM_CPU_MODE_IRQ) {
10458         env->xregs[16] = env->regs[14];
10459         env->xregs[17] = env->regs[13];
10460     } else {
10461         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10462         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10463     }
10464 
10465     if (mode == ARM_CPU_MODE_SVC) {
10466         env->xregs[18] = env->regs[14];
10467         env->xregs[19] = env->regs[13];
10468     } else {
10469         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10470         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10471     }
10472 
10473     if (mode == ARM_CPU_MODE_ABT) {
10474         env->xregs[20] = env->regs[14];
10475         env->xregs[21] = env->regs[13];
10476     } else {
10477         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10478         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10479     }
10480 
10481     if (mode == ARM_CPU_MODE_UND) {
10482         env->xregs[22] = env->regs[14];
10483         env->xregs[23] = env->regs[13];
10484     } else {
10485         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10486         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10487     }
10488 
10489     /*
10490      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10491      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10492      * FIQ bank for r8-r14.
10493      */
10494     if (mode == ARM_CPU_MODE_FIQ) {
10495         for (i = 24; i < 31; i++) {
10496             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10497         }
10498     } else {
10499         for (i = 24; i < 29; i++) {
10500             env->xregs[i] = env->fiq_regs[i - 24];
10501         }
10502         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10503         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10504     }
10505 
10506     env->pc = env->regs[15];
10507 }
10508 
10509 /*
10510  * Function used to synchronize QEMU's AArch32 register set with AArch64
10511  * register set.  This is necessary when switching between AArch32 and AArch64
10512  * execution state.
10513  */
10514 void aarch64_sync_64_to_32(CPUARMState *env)
10515 {
10516     int i;
10517     uint32_t mode = env->uncached_cpsr & CPSR_M;
10518 
10519     /* We can blanket copy X[0:7] to R[0:7] */
10520     for (i = 0; i < 8; i++) {
10521         env->regs[i] = env->xregs[i];
10522     }
10523 
10524     /*
10525      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10526      * Otherwise, we copy x8-x12 into the banked user regs.
10527      */
10528     if (mode == ARM_CPU_MODE_FIQ) {
10529         for (i = 8; i < 13; i++) {
10530             env->usr_regs[i - 8] = env->xregs[i];
10531         }
10532     } else {
10533         for (i = 8; i < 13; i++) {
10534             env->regs[i] = env->xregs[i];
10535         }
10536     }
10537 
10538     /*
10539      * Registers r13 & r14 depend on the current mode.
10540      * If we are in a given mode, we copy the corresponding x registers to r13
10541      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10542      * for the mode.
10543      */
10544     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10545         env->regs[13] = env->xregs[13];
10546         env->regs[14] = env->xregs[14];
10547     } else {
10548         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10549 
10550         /*
10551          * HYP is an exception in that it does not have its own banked r14 but
10552          * shares the USR r14
10553          */
10554         if (mode == ARM_CPU_MODE_HYP) {
10555             env->regs[14] = env->xregs[14];
10556         } else {
10557             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10558         }
10559     }
10560 
10561     if (mode == ARM_CPU_MODE_HYP) {
10562         env->regs[13] = env->xregs[15];
10563     } else {
10564         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10565     }
10566 
10567     if (mode == ARM_CPU_MODE_IRQ) {
10568         env->regs[14] = env->xregs[16];
10569         env->regs[13] = env->xregs[17];
10570     } else {
10571         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10572         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10573     }
10574 
10575     if (mode == ARM_CPU_MODE_SVC) {
10576         env->regs[14] = env->xregs[18];
10577         env->regs[13] = env->xregs[19];
10578     } else {
10579         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10580         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10581     }
10582 
10583     if (mode == ARM_CPU_MODE_ABT) {
10584         env->regs[14] = env->xregs[20];
10585         env->regs[13] = env->xregs[21];
10586     } else {
10587         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10588         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10589     }
10590 
10591     if (mode == ARM_CPU_MODE_UND) {
10592         env->regs[14] = env->xregs[22];
10593         env->regs[13] = env->xregs[23];
10594     } else {
10595         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10596         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10597     }
10598 
10599     /*
10600      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10601      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10602      * FIQ bank for r8-r14.
10603      */
10604     if (mode == ARM_CPU_MODE_FIQ) {
10605         for (i = 24; i < 31; i++) {
10606             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10607         }
10608     } else {
10609         for (i = 24; i < 29; i++) {
10610             env->fiq_regs[i - 24] = env->xregs[i];
10611         }
10612         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10613         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10614     }
10615 
10616     env->regs[15] = env->pc;
10617 }
10618 
10619 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10620                                    uint32_t mask, uint32_t offset,
10621                                    uint32_t newpc)
10622 {
10623     int new_el;
10624 
10625     /* Change the CPU state so as to actually take the exception. */
10626     switch_mode(env, new_mode);
10627 
10628     /*
10629      * For exceptions taken to AArch32 we must clear the SS bit in both
10630      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10631      */
10632     env->pstate &= ~PSTATE_SS;
10633     env->spsr = cpsr_read(env);
10634     /* Clear IT bits.  */
10635     env->condexec_bits = 0;
10636     /* Switch to the new mode, and to the correct instruction set.  */
10637     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10638 
10639     /* This must be after mode switching. */
10640     new_el = arm_current_el(env);
10641 
10642     /* Set new mode endianness */
10643     env->uncached_cpsr &= ~CPSR_E;
10644     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10645         env->uncached_cpsr |= CPSR_E;
10646     }
10647     /* J and IL must always be cleared for exception entry */
10648     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10649     env->daif |= mask;
10650 
10651     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10652         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10653             env->uncached_cpsr |= CPSR_SSBS;
10654         } else {
10655             env->uncached_cpsr &= ~CPSR_SSBS;
10656         }
10657     }
10658 
10659     if (new_mode == ARM_CPU_MODE_HYP) {
10660         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10661         env->elr_el[2] = env->regs[15];
10662     } else {
10663         /* CPSR.PAN is normally preserved preserved unless...  */
10664         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10665             switch (new_el) {
10666             case 3:
10667                 if (!arm_is_secure_below_el3(env)) {
10668                     /* ... the target is EL3, from non-secure state.  */
10669                     env->uncached_cpsr &= ~CPSR_PAN;
10670                     break;
10671                 }
10672                 /* ... the target is EL3, from secure state ... */
10673                 /* fall through */
10674             case 1:
10675                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10676                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10677                     env->uncached_cpsr |= CPSR_PAN;
10678                 }
10679                 break;
10680             }
10681         }
10682         /*
10683          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10684          * and we should just guard the thumb mode on V4
10685          */
10686         if (arm_feature(env, ARM_FEATURE_V4T)) {
10687             env->thumb =
10688                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10689         }
10690         env->regs[14] = env->regs[15] + offset;
10691     }
10692     env->regs[15] = newpc;
10693 
10694     if (tcg_enabled()) {
10695         arm_rebuild_hflags(env);
10696     }
10697 }
10698 
10699 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10700 {
10701     /*
10702      * Handle exception entry to Hyp mode; this is sufficiently
10703      * different to entry to other AArch32 modes that we handle it
10704      * separately here.
10705      *
10706      * The vector table entry used is always the 0x14 Hyp mode entry point,
10707      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10708      * The offset applied to the preferred return address is always zero
10709      * (see DDI0487C.a section G1.12.3).
10710      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10711      */
10712     uint32_t addr, mask;
10713     ARMCPU *cpu = ARM_CPU(cs);
10714     CPUARMState *env = &cpu->env;
10715 
10716     switch (cs->exception_index) {
10717     case EXCP_UDEF:
10718         addr = 0x04;
10719         break;
10720     case EXCP_SWI:
10721         addr = 0x08;
10722         break;
10723     case EXCP_BKPT:
10724         /* Fall through to prefetch abort.  */
10725     case EXCP_PREFETCH_ABORT:
10726         env->cp15.ifar_s = env->exception.vaddress;
10727         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10728                       (uint32_t)env->exception.vaddress);
10729         addr = 0x0c;
10730         break;
10731     case EXCP_DATA_ABORT:
10732         env->cp15.dfar_s = env->exception.vaddress;
10733         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10734                       (uint32_t)env->exception.vaddress);
10735         addr = 0x10;
10736         break;
10737     case EXCP_IRQ:
10738         addr = 0x18;
10739         break;
10740     case EXCP_FIQ:
10741         addr = 0x1c;
10742         break;
10743     case EXCP_HVC:
10744         addr = 0x08;
10745         break;
10746     case EXCP_HYP_TRAP:
10747         addr = 0x14;
10748         break;
10749     default:
10750         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10751     }
10752 
10753     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10754         if (!arm_feature(env, ARM_FEATURE_V8)) {
10755             /*
10756              * QEMU syndrome values are v8-style. v7 has the IL bit
10757              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10758              * If this is a v7 CPU, squash the IL bit in those cases.
10759              */
10760             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10761                 (cs->exception_index == EXCP_DATA_ABORT &&
10762                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10763                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10764                 env->exception.syndrome &= ~ARM_EL_IL;
10765             }
10766         }
10767         env->cp15.esr_el[2] = env->exception.syndrome;
10768     }
10769 
10770     if (arm_current_el(env) != 2 && addr < 0x14) {
10771         addr = 0x14;
10772     }
10773 
10774     mask = 0;
10775     if (!(env->cp15.scr_el3 & SCR_EA)) {
10776         mask |= CPSR_A;
10777     }
10778     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10779         mask |= CPSR_I;
10780     }
10781     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10782         mask |= CPSR_F;
10783     }
10784 
10785     addr += env->cp15.hvbar;
10786 
10787     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10788 }
10789 
10790 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10791 {
10792     ARMCPU *cpu = ARM_CPU(cs);
10793     CPUARMState *env = &cpu->env;
10794     uint32_t addr;
10795     uint32_t mask;
10796     int new_mode;
10797     uint32_t offset;
10798     uint32_t moe;
10799 
10800     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10801     switch (syn_get_ec(env->exception.syndrome)) {
10802     case EC_BREAKPOINT:
10803     case EC_BREAKPOINT_SAME_EL:
10804         moe = 1;
10805         break;
10806     case EC_WATCHPOINT:
10807     case EC_WATCHPOINT_SAME_EL:
10808         moe = 10;
10809         break;
10810     case EC_AA32_BKPT:
10811         moe = 3;
10812         break;
10813     case EC_VECTORCATCH:
10814         moe = 5;
10815         break;
10816     default:
10817         moe = 0;
10818         break;
10819     }
10820 
10821     if (moe) {
10822         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10823     }
10824 
10825     if (env->exception.target_el == 2) {
10826         arm_cpu_do_interrupt_aarch32_hyp(cs);
10827         return;
10828     }
10829 
10830     switch (cs->exception_index) {
10831     case EXCP_UDEF:
10832         new_mode = ARM_CPU_MODE_UND;
10833         addr = 0x04;
10834         mask = CPSR_I;
10835         if (env->thumb) {
10836             offset = 2;
10837         } else {
10838             offset = 4;
10839         }
10840         break;
10841     case EXCP_SWI:
10842         new_mode = ARM_CPU_MODE_SVC;
10843         addr = 0x08;
10844         mask = CPSR_I;
10845         /* The PC already points to the next instruction.  */
10846         offset = 0;
10847         break;
10848     case EXCP_BKPT:
10849         /* Fall through to prefetch abort.  */
10850     case EXCP_PREFETCH_ABORT:
10851         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10852         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10853         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10854                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10855         new_mode = ARM_CPU_MODE_ABT;
10856         addr = 0x0c;
10857         mask = CPSR_A | CPSR_I;
10858         offset = 4;
10859         break;
10860     case EXCP_DATA_ABORT:
10861         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10862         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10863         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10864                       env->exception.fsr,
10865                       (uint32_t)env->exception.vaddress);
10866         new_mode = ARM_CPU_MODE_ABT;
10867         addr = 0x10;
10868         mask = CPSR_A | CPSR_I;
10869         offset = 8;
10870         break;
10871     case EXCP_IRQ:
10872         new_mode = ARM_CPU_MODE_IRQ;
10873         addr = 0x18;
10874         /* Disable IRQ and imprecise data aborts.  */
10875         mask = CPSR_A | CPSR_I;
10876         offset = 4;
10877         if (env->cp15.scr_el3 & SCR_IRQ) {
10878             /* IRQ routed to monitor mode */
10879             new_mode = ARM_CPU_MODE_MON;
10880             mask |= CPSR_F;
10881         }
10882         break;
10883     case EXCP_FIQ:
10884         new_mode = ARM_CPU_MODE_FIQ;
10885         addr = 0x1c;
10886         /* Disable FIQ, IRQ and imprecise data aborts.  */
10887         mask = CPSR_A | CPSR_I | CPSR_F;
10888         if (env->cp15.scr_el3 & SCR_FIQ) {
10889             /* FIQ routed to monitor mode */
10890             new_mode = ARM_CPU_MODE_MON;
10891         }
10892         offset = 4;
10893         break;
10894     case EXCP_VIRQ:
10895         new_mode = ARM_CPU_MODE_IRQ;
10896         addr = 0x18;
10897         /* Disable IRQ and imprecise data aborts.  */
10898         mask = CPSR_A | CPSR_I;
10899         offset = 4;
10900         break;
10901     case EXCP_VFIQ:
10902         new_mode = ARM_CPU_MODE_FIQ;
10903         addr = 0x1c;
10904         /* Disable FIQ, IRQ and imprecise data aborts.  */
10905         mask = CPSR_A | CPSR_I | CPSR_F;
10906         offset = 4;
10907         break;
10908     case EXCP_VSERR:
10909         {
10910             /*
10911              * Note that this is reported as a data abort, but the DFAR
10912              * has an UNKNOWN value.  Construct the SError syndrome from
10913              * AET and ExT fields.
10914              */
10915             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10916 
10917             if (extended_addresses_enabled(env)) {
10918                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10919             } else {
10920                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10921             }
10922             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10923             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10924             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10925                           env->exception.fsr);
10926 
10927             new_mode = ARM_CPU_MODE_ABT;
10928             addr = 0x10;
10929             mask = CPSR_A | CPSR_I;
10930             offset = 8;
10931         }
10932         break;
10933     case EXCP_SMC:
10934         new_mode = ARM_CPU_MODE_MON;
10935         addr = 0x08;
10936         mask = CPSR_A | CPSR_I | CPSR_F;
10937         offset = 0;
10938         break;
10939     default:
10940         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10941         return; /* Never happens.  Keep compiler happy.  */
10942     }
10943 
10944     if (new_mode == ARM_CPU_MODE_MON) {
10945         addr += env->cp15.mvbar;
10946     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10947         /* High vectors. When enabled, base address cannot be remapped. */
10948         addr += 0xffff0000;
10949     } else {
10950         /*
10951          * ARM v7 architectures provide a vector base address register to remap
10952          * the interrupt vector table.
10953          * This register is only followed in non-monitor mode, and is banked.
10954          * Note: only bits 31:5 are valid.
10955          */
10956         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10957     }
10958 
10959     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10960         env->cp15.scr_el3 &= ~SCR_NS;
10961     }
10962 
10963     take_aarch32_exception(env, new_mode, mask, offset, addr);
10964 }
10965 
10966 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10967 {
10968     /*
10969      * Return the register number of the AArch64 view of the AArch32
10970      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10971      * be that of the AArch32 mode the exception came from.
10972      */
10973     int mode = env->uncached_cpsr & CPSR_M;
10974 
10975     switch (aarch32_reg) {
10976     case 0 ... 7:
10977         return aarch32_reg;
10978     case 8 ... 12:
10979         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10980     case 13:
10981         switch (mode) {
10982         case ARM_CPU_MODE_USR:
10983         case ARM_CPU_MODE_SYS:
10984             return 13;
10985         case ARM_CPU_MODE_HYP:
10986             return 15;
10987         case ARM_CPU_MODE_IRQ:
10988             return 17;
10989         case ARM_CPU_MODE_SVC:
10990             return 19;
10991         case ARM_CPU_MODE_ABT:
10992             return 21;
10993         case ARM_CPU_MODE_UND:
10994             return 23;
10995         case ARM_CPU_MODE_FIQ:
10996             return 29;
10997         default:
10998             g_assert_not_reached();
10999         }
11000     case 14:
11001         switch (mode) {
11002         case ARM_CPU_MODE_USR:
11003         case ARM_CPU_MODE_SYS:
11004         case ARM_CPU_MODE_HYP:
11005             return 14;
11006         case ARM_CPU_MODE_IRQ:
11007             return 16;
11008         case ARM_CPU_MODE_SVC:
11009             return 18;
11010         case ARM_CPU_MODE_ABT:
11011             return 20;
11012         case ARM_CPU_MODE_UND:
11013             return 22;
11014         case ARM_CPU_MODE_FIQ:
11015             return 30;
11016         default:
11017             g_assert_not_reached();
11018         }
11019     case 15:
11020         return 31;
11021     default:
11022         g_assert_not_reached();
11023     }
11024 }
11025 
11026 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11027 {
11028     uint32_t ret = cpsr_read(env);
11029 
11030     /* Move DIT to the correct location for SPSR_ELx */
11031     if (ret & CPSR_DIT) {
11032         ret &= ~CPSR_DIT;
11033         ret |= PSTATE_DIT;
11034     }
11035     /* Merge PSTATE.SS into SPSR_ELx */
11036     ret |= env->pstate & PSTATE_SS;
11037 
11038     return ret;
11039 }
11040 
11041 static bool syndrome_is_sync_extabt(uint32_t syndrome)
11042 {
11043     /* Return true if this syndrome value is a synchronous external abort */
11044     switch (syn_get_ec(syndrome)) {
11045     case EC_INSNABORT:
11046     case EC_INSNABORT_SAME_EL:
11047     case EC_DATAABORT:
11048     case EC_DATAABORT_SAME_EL:
11049         /* Look at fault status code for all the synchronous ext abort cases */
11050         switch (syndrome & 0x3f) {
11051         case 0x10:
11052         case 0x13:
11053         case 0x14:
11054         case 0x15:
11055         case 0x16:
11056         case 0x17:
11057             return true;
11058         default:
11059             return false;
11060         }
11061     default:
11062         return false;
11063     }
11064 }
11065 
11066 /* Handle exception entry to a target EL which is using AArch64 */
11067 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
11068 {
11069     ARMCPU *cpu = ARM_CPU(cs);
11070     CPUARMState *env = &cpu->env;
11071     unsigned int new_el = env->exception.target_el;
11072     target_ulong addr = env->cp15.vbar_el[new_el];
11073     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
11074     unsigned int old_mode;
11075     unsigned int cur_el = arm_current_el(env);
11076     int rt;
11077 
11078     if (tcg_enabled()) {
11079         /*
11080          * Note that new_el can never be 0.  If cur_el is 0, then
11081          * el0_a64 is is_a64(), else el0_a64 is ignored.
11082          */
11083         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11084     }
11085 
11086     if (cur_el < new_el) {
11087         /*
11088          * Entry vector offset depends on whether the implemented EL
11089          * immediately lower than the target level is using AArch32 or AArch64
11090          */
11091         bool is_aa64;
11092         uint64_t hcr;
11093 
11094         switch (new_el) {
11095         case 3:
11096             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11097             break;
11098         case 2:
11099             hcr = arm_hcr_el2_eff(env);
11100             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11101                 is_aa64 = (hcr & HCR_RW) != 0;
11102                 break;
11103             }
11104             /* fall through */
11105         case 1:
11106             is_aa64 = is_a64(env);
11107             break;
11108         default:
11109             g_assert_not_reached();
11110         }
11111 
11112         if (is_aa64) {
11113             addr += 0x400;
11114         } else {
11115             addr += 0x600;
11116         }
11117     } else if (pstate_read(env) & PSTATE_SP) {
11118         addr += 0x200;
11119     }
11120 
11121     switch (cs->exception_index) {
11122     case EXCP_GPC:
11123         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11124                       env->cp15.mfar_el3);
11125         /* fall through */
11126     case EXCP_PREFETCH_ABORT:
11127     case EXCP_DATA_ABORT:
11128         /*
11129          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11130          * to be taken to the SError vector entrypoint.
11131          */
11132         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11133             syndrome_is_sync_extabt(env->exception.syndrome)) {
11134             addr += 0x180;
11135         }
11136         env->cp15.far_el[new_el] = env->exception.vaddress;
11137         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11138                       env->cp15.far_el[new_el]);
11139         /* fall through */
11140     case EXCP_BKPT:
11141     case EXCP_UDEF:
11142     case EXCP_SWI:
11143     case EXCP_HVC:
11144     case EXCP_HYP_TRAP:
11145     case EXCP_SMC:
11146         switch (syn_get_ec(env->exception.syndrome)) {
11147         case EC_ADVSIMDFPACCESSTRAP:
11148             /*
11149              * QEMU internal FP/SIMD syndromes from AArch32 include the
11150              * TA and coproc fields which are only exposed if the exception
11151              * is taken to AArch32 Hyp mode. Mask them out to get a valid
11152              * AArch64 format syndrome.
11153              */
11154             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
11155             break;
11156         case EC_CP14RTTRAP:
11157         case EC_CP15RTTRAP:
11158         case EC_CP14DTTRAP:
11159             /*
11160              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11161              * the raw register field from the insn; when taking this to
11162              * AArch64 we must convert it to the AArch64 view of the register
11163              * number. Notice that we read a 4-bit AArch32 register number and
11164              * write back a 5-bit AArch64 one.
11165              */
11166             rt = extract32(env->exception.syndrome, 5, 4);
11167             rt = aarch64_regnum(env, rt);
11168             env->exception.syndrome = deposit32(env->exception.syndrome,
11169                                                 5, 5, rt);
11170             break;
11171         case EC_CP15RRTTRAP:
11172         case EC_CP14RRTTRAP:
11173             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11174             rt = extract32(env->exception.syndrome, 5, 4);
11175             rt = aarch64_regnum(env, rt);
11176             env->exception.syndrome = deposit32(env->exception.syndrome,
11177                                                 5, 5, rt);
11178             rt = extract32(env->exception.syndrome, 10, 4);
11179             rt = aarch64_regnum(env, rt);
11180             env->exception.syndrome = deposit32(env->exception.syndrome,
11181                                                 10, 5, rt);
11182             break;
11183         }
11184         env->cp15.esr_el[new_el] = env->exception.syndrome;
11185         break;
11186     case EXCP_IRQ:
11187     case EXCP_VIRQ:
11188         addr += 0x80;
11189         break;
11190     case EXCP_FIQ:
11191     case EXCP_VFIQ:
11192         addr += 0x100;
11193         break;
11194     case EXCP_VSERR:
11195         addr += 0x180;
11196         /* Construct the SError syndrome from IDS and ISS fields. */
11197         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11198         env->cp15.esr_el[new_el] = env->exception.syndrome;
11199         break;
11200     default:
11201         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11202     }
11203 
11204     if (is_a64(env)) {
11205         old_mode = pstate_read(env);
11206         aarch64_save_sp(env, arm_current_el(env));
11207         env->elr_el[new_el] = env->pc;
11208     } else {
11209         old_mode = cpsr_read_for_spsr_elx(env);
11210         env->elr_el[new_el] = env->regs[15];
11211 
11212         aarch64_sync_32_to_64(env);
11213 
11214         env->condexec_bits = 0;
11215     }
11216     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11217 
11218     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11219                   env->elr_el[new_el]);
11220 
11221     if (cpu_isar_feature(aa64_pan, cpu)) {
11222         /* The value of PSTATE.PAN is normally preserved, except when ... */
11223         new_mode |= old_mode & PSTATE_PAN;
11224         switch (new_el) {
11225         case 2:
11226             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
11227             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11228                 != (HCR_E2H | HCR_TGE)) {
11229                 break;
11230             }
11231             /* fall through */
11232         case 1:
11233             /* ... the target is EL1 ... */
11234             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
11235             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11236                 new_mode |= PSTATE_PAN;
11237             }
11238             break;
11239         }
11240     }
11241     if (cpu_isar_feature(aa64_mte, cpu)) {
11242         new_mode |= PSTATE_TCO;
11243     }
11244 
11245     if (cpu_isar_feature(aa64_ssbs, cpu)) {
11246         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11247             new_mode |= PSTATE_SSBS;
11248         } else {
11249             new_mode &= ~PSTATE_SSBS;
11250         }
11251     }
11252 
11253     pstate_write(env, PSTATE_DAIF | new_mode);
11254     env->aarch64 = true;
11255     aarch64_restore_sp(env, new_el);
11256 
11257     if (tcg_enabled()) {
11258         helper_rebuild_hflags_a64(env, new_el);
11259     }
11260 
11261     env->pc = addr;
11262 
11263     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11264                   new_el, env->pc, pstate_read(env));
11265 }
11266 
11267 /*
11268  * Do semihosting call and set the appropriate return value. All the
11269  * permission and validity checks have been done at translate time.
11270  *
11271  * We only see semihosting exceptions in TCG only as they are not
11272  * trapped to the hypervisor in KVM.
11273  */
11274 #ifdef CONFIG_TCG
11275 static void tcg_handle_semihosting(CPUState *cs)
11276 {
11277     ARMCPU *cpu = ARM_CPU(cs);
11278     CPUARMState *env = &cpu->env;
11279 
11280     if (is_a64(env)) {
11281         qemu_log_mask(CPU_LOG_INT,
11282                       "...handling as semihosting call 0x%" PRIx64 "\n",
11283                       env->xregs[0]);
11284         do_common_semihosting(cs);
11285         env->pc += 4;
11286     } else {
11287         qemu_log_mask(CPU_LOG_INT,
11288                       "...handling as semihosting call 0x%x\n",
11289                       env->regs[0]);
11290         do_common_semihosting(cs);
11291         env->regs[15] += env->thumb ? 2 : 4;
11292     }
11293 }
11294 #endif
11295 
11296 /*
11297  * Handle a CPU exception for A and R profile CPUs.
11298  * Do any appropriate logging, handle PSCI calls, and then hand off
11299  * to the AArch64-entry or AArch32-entry function depending on the
11300  * target exception level's register width.
11301  *
11302  * Note: this is used for both TCG (as the do_interrupt tcg op),
11303  *       and KVM to re-inject guest debug exceptions, and to
11304  *       inject a Synchronous-External-Abort.
11305  */
11306 void arm_cpu_do_interrupt(CPUState *cs)
11307 {
11308     ARMCPU *cpu = ARM_CPU(cs);
11309     CPUARMState *env = &cpu->env;
11310     unsigned int new_el = env->exception.target_el;
11311 
11312     assert(!arm_feature(env, ARM_FEATURE_M));
11313 
11314     arm_log_exception(cs);
11315     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11316                   new_el);
11317     if (qemu_loglevel_mask(CPU_LOG_INT)
11318         && !excp_is_internal(cs->exception_index)) {
11319         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
11320                       syn_get_ec(env->exception.syndrome),
11321                       env->exception.syndrome);
11322     }
11323 
11324     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
11325         arm_handle_psci_call(cpu);
11326         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11327         return;
11328     }
11329 
11330     /*
11331      * Semihosting semantics depend on the register width of the code
11332      * that caused the exception, not the target exception level, so
11333      * must be handled here.
11334      */
11335 #ifdef CONFIG_TCG
11336     if (cs->exception_index == EXCP_SEMIHOST) {
11337         tcg_handle_semihosting(cs);
11338         return;
11339     }
11340 #endif
11341 
11342     /*
11343      * Hooks may change global state so BQL should be held, also the
11344      * BQL needs to be held for any modification of
11345      * cs->interrupt_request.
11346      */
11347     g_assert(qemu_mutex_iothread_locked());
11348 
11349     arm_call_pre_el_change_hook(cpu);
11350 
11351     assert(!excp_is_internal(cs->exception_index));
11352     if (arm_el_is_aa64(env, new_el)) {
11353         arm_cpu_do_interrupt_aarch64(cs);
11354     } else {
11355         arm_cpu_do_interrupt_aarch32(cs);
11356     }
11357 
11358     arm_call_el_change_hook(cpu);
11359 
11360     if (!kvm_enabled()) {
11361         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11362     }
11363 }
11364 #endif /* !CONFIG_USER_ONLY */
11365 
11366 uint64_t arm_sctlr(CPUARMState *env, int el)
11367 {
11368     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11369     if (el == 0) {
11370         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
11371         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
11372     }
11373     return env->cp15.sctlr_el[el];
11374 }
11375 
11376 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11377 {
11378     if (regime_has_2_ranges(mmu_idx)) {
11379         return extract64(tcr, 37, 2);
11380     } else if (regime_is_stage2(mmu_idx)) {
11381         return 0; /* VTCR_EL2 */
11382     } else {
11383         /* Replicate the single TBI bit so we always have 2 bits.  */
11384         return extract32(tcr, 20, 1) * 3;
11385     }
11386 }
11387 
11388 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11389 {
11390     if (regime_has_2_ranges(mmu_idx)) {
11391         return extract64(tcr, 51, 2);
11392     } else if (regime_is_stage2(mmu_idx)) {
11393         return 0; /* VTCR_EL2 */
11394     } else {
11395         /* Replicate the single TBID bit so we always have 2 bits.  */
11396         return extract32(tcr, 29, 1) * 3;
11397     }
11398 }
11399 
11400 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11401 {
11402     if (regime_has_2_ranges(mmu_idx)) {
11403         return extract64(tcr, 57, 2);
11404     } else {
11405         /* Replicate the single TCMA bit so we always have 2 bits.  */
11406         return extract32(tcr, 30, 1) * 3;
11407     }
11408 }
11409 
11410 static ARMGranuleSize tg0_to_gran_size(int tg)
11411 {
11412     switch (tg) {
11413     case 0:
11414         return Gran4K;
11415     case 1:
11416         return Gran64K;
11417     case 2:
11418         return Gran16K;
11419     default:
11420         return GranInvalid;
11421     }
11422 }
11423 
11424 static ARMGranuleSize tg1_to_gran_size(int tg)
11425 {
11426     switch (tg) {
11427     case 1:
11428         return Gran16K;
11429     case 2:
11430         return Gran4K;
11431     case 3:
11432         return Gran64K;
11433     default:
11434         return GranInvalid;
11435     }
11436 }
11437 
11438 static inline bool have4k(ARMCPU *cpu, bool stage2)
11439 {
11440     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11441         : cpu_isar_feature(aa64_tgran4, cpu);
11442 }
11443 
11444 static inline bool have16k(ARMCPU *cpu, bool stage2)
11445 {
11446     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11447         : cpu_isar_feature(aa64_tgran16, cpu);
11448 }
11449 
11450 static inline bool have64k(ARMCPU *cpu, bool stage2)
11451 {
11452     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11453         : cpu_isar_feature(aa64_tgran64, cpu);
11454 }
11455 
11456 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11457                                          bool stage2)
11458 {
11459     switch (gran) {
11460     case Gran4K:
11461         if (have4k(cpu, stage2)) {
11462             return gran;
11463         }
11464         break;
11465     case Gran16K:
11466         if (have16k(cpu, stage2)) {
11467             return gran;
11468         }
11469         break;
11470     case Gran64K:
11471         if (have64k(cpu, stage2)) {
11472             return gran;
11473         }
11474         break;
11475     case GranInvalid:
11476         break;
11477     }
11478     /*
11479      * If the guest selects a granule size that isn't implemented,
11480      * the architecture requires that we behave as if it selected one
11481      * that is (with an IMPDEF choice of which one to pick). We choose
11482      * to implement the smallest supported granule size.
11483      */
11484     if (have4k(cpu, stage2)) {
11485         return Gran4K;
11486     }
11487     if (have16k(cpu, stage2)) {
11488         return Gran16K;
11489     }
11490     assert(have64k(cpu, stage2));
11491     return Gran64K;
11492 }
11493 
11494 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11495                                    ARMMMUIdx mmu_idx, bool data,
11496                                    bool el1_is_aa32)
11497 {
11498     uint64_t tcr = regime_tcr(env, mmu_idx);
11499     bool epd, hpd, tsz_oob, ds, ha, hd;
11500     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11501     ARMGranuleSize gran;
11502     ARMCPU *cpu = env_archcpu(env);
11503     bool stage2 = regime_is_stage2(mmu_idx);
11504 
11505     if (!regime_has_2_ranges(mmu_idx)) {
11506         select = 0;
11507         tsz = extract32(tcr, 0, 6);
11508         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11509         if (stage2) {
11510             /* VTCR_EL2 */
11511             hpd = false;
11512         } else {
11513             hpd = extract32(tcr, 24, 1);
11514         }
11515         epd = false;
11516         sh = extract32(tcr, 12, 2);
11517         ps = extract32(tcr, 16, 3);
11518         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11519         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11520         ds = extract64(tcr, 32, 1);
11521     } else {
11522         bool e0pd;
11523 
11524         /*
11525          * Bit 55 is always between the two regions, and is canonical for
11526          * determining if address tagging is enabled.
11527          */
11528         select = extract64(va, 55, 1);
11529         if (!select) {
11530             tsz = extract32(tcr, 0, 6);
11531             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11532             epd = extract32(tcr, 7, 1);
11533             sh = extract32(tcr, 12, 2);
11534             hpd = extract64(tcr, 41, 1);
11535             e0pd = extract64(tcr, 55, 1);
11536         } else {
11537             tsz = extract32(tcr, 16, 6);
11538             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11539             epd = extract32(tcr, 23, 1);
11540             sh = extract32(tcr, 28, 2);
11541             hpd = extract64(tcr, 42, 1);
11542             e0pd = extract64(tcr, 56, 1);
11543         }
11544         ps = extract64(tcr, 32, 3);
11545         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11546         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11547         ds = extract64(tcr, 59, 1);
11548 
11549         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11550             regime_is_user(env, mmu_idx)) {
11551             epd = true;
11552         }
11553     }
11554 
11555     gran = sanitize_gran_size(cpu, gran, stage2);
11556 
11557     if (cpu_isar_feature(aa64_st, cpu)) {
11558         max_tsz = 48 - (gran == Gran64K);
11559     } else {
11560         max_tsz = 39;
11561     }
11562 
11563     /*
11564      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11565      * adjust the effective value of DS, as documented.
11566      */
11567     min_tsz = 16;
11568     if (gran == Gran64K) {
11569         if (cpu_isar_feature(aa64_lva, cpu)) {
11570             min_tsz = 12;
11571         }
11572         ds = false;
11573     } else if (ds) {
11574         if (regime_is_stage2(mmu_idx)) {
11575             if (gran == Gran16K) {
11576                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11577             } else {
11578                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11579             }
11580         } else {
11581             if (gran == Gran16K) {
11582                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11583             } else {
11584                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11585             }
11586         }
11587         if (ds) {
11588             min_tsz = 12;
11589         }
11590     }
11591 
11592     if (stage2 && el1_is_aa32) {
11593         /*
11594          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11595          * are loosened: a configured IPA of 40 bits is permitted even if
11596          * the implemented PA is less than that (and so a 40 bit IPA would
11597          * fault for an AArch64 EL1). See R_DTLMN.
11598          */
11599         min_tsz = MIN(min_tsz, 24);
11600     }
11601 
11602     if (tsz > max_tsz) {
11603         tsz = max_tsz;
11604         tsz_oob = true;
11605     } else if (tsz < min_tsz) {
11606         tsz = min_tsz;
11607         tsz_oob = true;
11608     } else {
11609         tsz_oob = false;
11610     }
11611 
11612     /* Present TBI as a composite with TBID.  */
11613     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11614     if (!data) {
11615         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11616     }
11617     tbi = (tbi >> select) & 1;
11618 
11619     return (ARMVAParameters) {
11620         .tsz = tsz,
11621         .ps = ps,
11622         .sh = sh,
11623         .select = select,
11624         .tbi = tbi,
11625         .epd = epd,
11626         .hpd = hpd,
11627         .tsz_oob = tsz_oob,
11628         .ds = ds,
11629         .ha = ha,
11630         .hd = ha && hd,
11631         .gran = gran,
11632     };
11633 }
11634 
11635 /*
11636  * Note that signed overflow is undefined in C.  The following routines are
11637  * careful to use unsigned types where modulo arithmetic is required.
11638  * Failure to do so _will_ break on newer gcc.
11639  */
11640 
11641 /* Signed saturating arithmetic.  */
11642 
11643 /* Perform 16-bit signed saturating addition.  */
11644 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11645 {
11646     uint16_t res;
11647 
11648     res = a + b;
11649     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11650         if (a & 0x8000) {
11651             res = 0x8000;
11652         } else {
11653             res = 0x7fff;
11654         }
11655     }
11656     return res;
11657 }
11658 
11659 /* Perform 8-bit signed saturating addition.  */
11660 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11661 {
11662     uint8_t res;
11663 
11664     res = a + b;
11665     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11666         if (a & 0x80) {
11667             res = 0x80;
11668         } else {
11669             res = 0x7f;
11670         }
11671     }
11672     return res;
11673 }
11674 
11675 /* Perform 16-bit signed saturating subtraction.  */
11676 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11677 {
11678     uint16_t res;
11679 
11680     res = a - b;
11681     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11682         if (a & 0x8000) {
11683             res = 0x8000;
11684         } else {
11685             res = 0x7fff;
11686         }
11687     }
11688     return res;
11689 }
11690 
11691 /* Perform 8-bit signed saturating subtraction.  */
11692 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11693 {
11694     uint8_t res;
11695 
11696     res = a - b;
11697     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11698         if (a & 0x80) {
11699             res = 0x80;
11700         } else {
11701             res = 0x7f;
11702         }
11703     }
11704     return res;
11705 }
11706 
11707 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11708 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11709 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
11710 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
11711 #define PFX q
11712 
11713 #include "op_addsub.h"
11714 
11715 /* Unsigned saturating arithmetic.  */
11716 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11717 {
11718     uint16_t res;
11719     res = a + b;
11720     if (res < a) {
11721         res = 0xffff;
11722     }
11723     return res;
11724 }
11725 
11726 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11727 {
11728     if (a > b) {
11729         return a - b;
11730     } else {
11731         return 0;
11732     }
11733 }
11734 
11735 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11736 {
11737     uint8_t res;
11738     res = a + b;
11739     if (res < a) {
11740         res = 0xff;
11741     }
11742     return res;
11743 }
11744 
11745 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11746 {
11747     if (a > b) {
11748         return a - b;
11749     } else {
11750         return 0;
11751     }
11752 }
11753 
11754 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11755 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11756 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
11757 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
11758 #define PFX uq
11759 
11760 #include "op_addsub.h"
11761 
11762 /* Signed modulo arithmetic.  */
11763 #define SARITH16(a, b, n, op) do { \
11764     int32_t sum; \
11765     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
11766     RESULT(sum, n, 16); \
11767     if (sum >= 0) \
11768         ge |= 3 << (n * 2); \
11769     } while (0)
11770 
11771 #define SARITH8(a, b, n, op) do { \
11772     int32_t sum; \
11773     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
11774     RESULT(sum, n, 8); \
11775     if (sum >= 0) \
11776         ge |= 1 << n; \
11777     } while (0)
11778 
11779 
11780 #define ADD16(a, b, n) SARITH16(a, b, n, +)
11781 #define SUB16(a, b, n) SARITH16(a, b, n, -)
11782 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
11783 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
11784 #define PFX s
11785 #define ARITH_GE
11786 
11787 #include "op_addsub.h"
11788 
11789 /* Unsigned modulo arithmetic.  */
11790 #define ADD16(a, b, n) do { \
11791     uint32_t sum; \
11792     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11793     RESULT(sum, n, 16); \
11794     if ((sum >> 16) == 1) \
11795         ge |= 3 << (n * 2); \
11796     } while (0)
11797 
11798 #define ADD8(a, b, n) do { \
11799     uint32_t sum; \
11800     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11801     RESULT(sum, n, 8); \
11802     if ((sum >> 8) == 1) \
11803         ge |= 1 << n; \
11804     } while (0)
11805 
11806 #define SUB16(a, b, n) do { \
11807     uint32_t sum; \
11808     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11809     RESULT(sum, n, 16); \
11810     if ((sum >> 16) == 0) \
11811         ge |= 3 << (n * 2); \
11812     } while (0)
11813 
11814 #define SUB8(a, b, n) do { \
11815     uint32_t sum; \
11816     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11817     RESULT(sum, n, 8); \
11818     if ((sum >> 8) == 0) \
11819         ge |= 1 << n; \
11820     } while (0)
11821 
11822 #define PFX u
11823 #define ARITH_GE
11824 
11825 #include "op_addsub.h"
11826 
11827 /* Halved signed arithmetic.  */
11828 #define ADD16(a, b, n) \
11829   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11830 #define SUB16(a, b, n) \
11831   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11832 #define ADD8(a, b, n) \
11833   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11834 #define SUB8(a, b, n) \
11835   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11836 #define PFX sh
11837 
11838 #include "op_addsub.h"
11839 
11840 /* Halved unsigned arithmetic.  */
11841 #define ADD16(a, b, n) \
11842   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11843 #define SUB16(a, b, n) \
11844   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11845 #define ADD8(a, b, n) \
11846   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11847 #define SUB8(a, b, n) \
11848   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11849 #define PFX uh
11850 
11851 #include "op_addsub.h"
11852 
11853 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11854 {
11855     if (a > b) {
11856         return a - b;
11857     } else {
11858         return b - a;
11859     }
11860 }
11861 
11862 /* Unsigned sum of absolute byte differences.  */
11863 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11864 {
11865     uint32_t sum;
11866     sum = do_usad(a, b);
11867     sum += do_usad(a >> 8, b >> 8);
11868     sum += do_usad(a >> 16, b >> 16);
11869     sum += do_usad(a >> 24, b >> 24);
11870     return sum;
11871 }
11872 
11873 /* For ARMv6 SEL instruction.  */
11874 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11875 {
11876     uint32_t mask;
11877 
11878     mask = 0;
11879     if (flags & 1) {
11880         mask |= 0xff;
11881     }
11882     if (flags & 2) {
11883         mask |= 0xff00;
11884     }
11885     if (flags & 4) {
11886         mask |= 0xff0000;
11887     }
11888     if (flags & 8) {
11889         mask |= 0xff000000;
11890     }
11891     return (a & mask) | (b & ~mask);
11892 }
11893 
11894 /*
11895  * CRC helpers.
11896  * The upper bytes of val (above the number specified by 'bytes') must have
11897  * been zeroed out by the caller.
11898  */
11899 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11900 {
11901     uint8_t buf[4];
11902 
11903     stl_le_p(buf, val);
11904 
11905     /* zlib crc32 converts the accumulator and output to one's complement.  */
11906     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11907 }
11908 
11909 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11910 {
11911     uint8_t buf[4];
11912 
11913     stl_le_p(buf, val);
11914 
11915     /* Linux crc32c converts the output to one's complement.  */
11916     return crc32c(acc, buf, bytes) ^ 0xffffffff;
11917 }
11918 
11919 /*
11920  * Return the exception level to which FP-disabled exceptions should
11921  * be taken, or 0 if FP is enabled.
11922  */
11923 int fp_exception_el(CPUARMState *env, int cur_el)
11924 {
11925 #ifndef CONFIG_USER_ONLY
11926     uint64_t hcr_el2;
11927 
11928     /*
11929      * CPACR and the CPTR registers don't exist before v6, so FP is
11930      * always accessible
11931      */
11932     if (!arm_feature(env, ARM_FEATURE_V6)) {
11933         return 0;
11934     }
11935 
11936     if (arm_feature(env, ARM_FEATURE_M)) {
11937         /* CPACR can cause a NOCP UsageFault taken to current security state */
11938         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11939             return 1;
11940         }
11941 
11942         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11943             if (!extract32(env->v7m.nsacr, 10, 1)) {
11944                 /* FP insns cause a NOCP UsageFault taken to Secure */
11945                 return 3;
11946             }
11947         }
11948 
11949         return 0;
11950     }
11951 
11952     hcr_el2 = arm_hcr_el2_eff(env);
11953 
11954     /*
11955      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11956      * 0, 2 : trap EL0 and EL1/PL1 accesses
11957      * 1    : trap only EL0 accesses
11958      * 3    : trap no accesses
11959      * This register is ignored if E2H+TGE are both set.
11960      */
11961     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11962         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11963 
11964         switch (fpen) {
11965         case 1:
11966             if (cur_el != 0) {
11967                 break;
11968             }
11969             /* fall through */
11970         case 0:
11971         case 2:
11972             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11973             if (!arm_el_is_aa64(env, 3)
11974                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11975                 return 3;
11976             }
11977             if (cur_el <= 1) {
11978                 return 1;
11979             }
11980             break;
11981         }
11982     }
11983 
11984     /*
11985      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11986      * to control non-secure access to the FPU. It doesn't have any
11987      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11988      */
11989     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11990          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11991         if (!extract32(env->cp15.nsacr, 10, 1)) {
11992             /* FP insns act as UNDEF */
11993             return cur_el == 2 ? 2 : 1;
11994         }
11995     }
11996 
11997     /*
11998      * CPTR_EL2 is present in v7VE or v8, and changes format
11999      * with HCR_EL2.E2H (regardless of TGE).
12000      */
12001     if (cur_el <= 2) {
12002         if (hcr_el2 & HCR_E2H) {
12003             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
12004             case 1:
12005                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
12006                     break;
12007                 }
12008                 /* fall through */
12009             case 0:
12010             case 2:
12011                 return 2;
12012             }
12013         } else if (arm_is_el2_enabled(env)) {
12014             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
12015                 return 2;
12016             }
12017         }
12018     }
12019 
12020     /* CPTR_EL3 : present in v8 */
12021     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
12022         /* Trap all FP ops to EL3 */
12023         return 3;
12024     }
12025 #endif
12026     return 0;
12027 }
12028 
12029 /* Return the exception level we're running at if this is our mmu_idx */
12030 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12031 {
12032     if (mmu_idx & ARM_MMU_IDX_M) {
12033         return mmu_idx & ARM_MMU_IDX_M_PRIV;
12034     }
12035 
12036     switch (mmu_idx) {
12037     case ARMMMUIdx_E10_0:
12038     case ARMMMUIdx_E20_0:
12039         return 0;
12040     case ARMMMUIdx_E10_1:
12041     case ARMMMUIdx_E10_1_PAN:
12042         return 1;
12043     case ARMMMUIdx_E2:
12044     case ARMMMUIdx_E20_2:
12045     case ARMMMUIdx_E20_2_PAN:
12046         return 2;
12047     case ARMMMUIdx_E3:
12048         return 3;
12049     default:
12050         g_assert_not_reached();
12051     }
12052 }
12053 
12054 #ifndef CONFIG_TCG
12055 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12056 {
12057     g_assert_not_reached();
12058 }
12059 #endif
12060 
12061 static bool arm_pan_enabled(CPUARMState *env)
12062 {
12063     if (is_a64(env)) {
12064         return env->pstate & PSTATE_PAN;
12065     } else {
12066         return env->uncached_cpsr & CPSR_PAN;
12067     }
12068 }
12069 
12070 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12071 {
12072     ARMMMUIdx idx;
12073     uint64_t hcr;
12074 
12075     if (arm_feature(env, ARM_FEATURE_M)) {
12076         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12077     }
12078 
12079     /* See ARM pseudo-function ELIsInHost.  */
12080     switch (el) {
12081     case 0:
12082         hcr = arm_hcr_el2_eff(env);
12083         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12084             idx = ARMMMUIdx_E20_0;
12085         } else {
12086             idx = ARMMMUIdx_E10_0;
12087         }
12088         break;
12089     case 1:
12090         if (arm_pan_enabled(env)) {
12091             idx = ARMMMUIdx_E10_1_PAN;
12092         } else {
12093             idx = ARMMMUIdx_E10_1;
12094         }
12095         break;
12096     case 2:
12097         /* Note that TGE does not apply at EL2.  */
12098         if (arm_hcr_el2_eff(env) & HCR_E2H) {
12099             if (arm_pan_enabled(env)) {
12100                 idx = ARMMMUIdx_E20_2_PAN;
12101             } else {
12102                 idx = ARMMMUIdx_E20_2;
12103             }
12104         } else {
12105             idx = ARMMMUIdx_E2;
12106         }
12107         break;
12108     case 3:
12109         return ARMMMUIdx_E3;
12110     default:
12111         g_assert_not_reached();
12112     }
12113 
12114     return idx;
12115 }
12116 
12117 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12118 {
12119     return arm_mmu_idx_el(env, arm_current_el(env));
12120 }
12121 
12122 static bool mve_no_pred(CPUARMState *env)
12123 {
12124     /*
12125      * Return true if there is definitely no predication of MVE
12126      * instructions by VPR or LTPSIZE. (Returning false even if there
12127      * isn't any predication is OK; generated code will just be
12128      * a little worse.)
12129      * If the CPU does not implement MVE then this TB flag is always 0.
12130      *
12131      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12132      * logic in gen_update_fp_context() needs to be updated to match.
12133      *
12134      * We do not include the effect of the ECI bits here -- they are
12135      * tracked in other TB flags. This simplifies the logic for
12136      * "when did we emit code that changes the MVE_NO_PRED TB flag
12137      * and thus need to end the TB?".
12138      */
12139     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12140         return false;
12141     }
12142     if (env->v7m.vpr) {
12143         return false;
12144     }
12145     if (env->v7m.ltpsize < 4) {
12146         return false;
12147     }
12148     return true;
12149 }
12150 
12151 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12152                           uint64_t *cs_base, uint32_t *pflags)
12153 {
12154     CPUARMTBFlags flags;
12155 
12156     assert_hflags_rebuild_correctly(env);
12157     flags = env->hflags;
12158 
12159     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12160         *pc = env->pc;
12161         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12162             DP_TBFLAG_A64(flags, BTYPE, env->btype);
12163         }
12164     } else {
12165         *pc = env->regs[15];
12166 
12167         if (arm_feature(env, ARM_FEATURE_M)) {
12168             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12169                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12170                 != env->v7m.secure) {
12171                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12172             }
12173 
12174             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12175                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12176                  (env->v7m.secure &&
12177                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12178                 /*
12179                  * ASPEN is set, but FPCA/SFPA indicate that there is no
12180                  * active FP context; we must create a new FP context before
12181                  * executing any FP insn.
12182                  */
12183                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12184             }
12185 
12186             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12187             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12188                 DP_TBFLAG_M32(flags, LSPACT, 1);
12189             }
12190 
12191             if (mve_no_pred(env)) {
12192                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12193             }
12194         } else {
12195             /*
12196              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12197              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12198              */
12199             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12200                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12201             } else {
12202                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12203                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12204             }
12205             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12206                 DP_TBFLAG_A32(flags, VFPEN, 1);
12207             }
12208         }
12209 
12210         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12211         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12212     }
12213 
12214     /*
12215      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12216      * states defined in the ARM ARM for software singlestep:
12217      *  SS_ACTIVE   PSTATE.SS   State
12218      *     0            x       Inactive (the TB flag for SS is always 0)
12219      *     1            0       Active-pending
12220      *     1            1       Active-not-pending
12221      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12222      */
12223     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12224         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12225     }
12226 
12227     *pflags = flags.flags;
12228     *cs_base = flags.flags2;
12229 }
12230 
12231 #ifdef TARGET_AARCH64
12232 /*
12233  * The manual says that when SVE is enabled and VQ is widened the
12234  * implementation is allowed to zero the previously inaccessible
12235  * portion of the registers.  The corollary to that is that when
12236  * SVE is enabled and VQ is narrowed we are also allowed to zero
12237  * the now inaccessible portion of the registers.
12238  *
12239  * The intent of this is that no predicate bit beyond VQ is ever set.
12240  * Which means that some operations on predicate registers themselves
12241  * may operate on full uint64_t or even unrolled across the maximum
12242  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
12243  * may well be cheaper than conditionals to restrict the operation
12244  * to the relevant portion of a uint16_t[16].
12245  */
12246 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12247 {
12248     int i, j;
12249     uint64_t pmask;
12250 
12251     assert(vq >= 1 && vq <= ARM_MAX_VQ);
12252     assert(vq <= env_archcpu(env)->sve_max_vq);
12253 
12254     /* Zap the high bits of the zregs.  */
12255     for (i = 0; i < 32; i++) {
12256         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12257     }
12258 
12259     /* Zap the high bits of the pregs and ffr.  */
12260     pmask = 0;
12261     if (vq & 3) {
12262         pmask = ~(-1ULL << (16 * (vq & 3)));
12263     }
12264     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12265         for (i = 0; i < 17; ++i) {
12266             env->vfp.pregs[i].p[j] &= pmask;
12267         }
12268         pmask = 0;
12269     }
12270 }
12271 
12272 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12273 {
12274     int exc_el;
12275 
12276     if (sm) {
12277         exc_el = sme_exception_el(env, el);
12278     } else {
12279         exc_el = sve_exception_el(env, el);
12280     }
12281     if (exc_el) {
12282         return 0; /* disabled */
12283     }
12284     return sve_vqm1_for_el_sm(env, el, sm);
12285 }
12286 
12287 /*
12288  * Notice a change in SVE vector size when changing EL.
12289  */
12290 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12291                            int new_el, bool el0_a64)
12292 {
12293     ARMCPU *cpu = env_archcpu(env);
12294     int old_len, new_len;
12295     bool old_a64, new_a64, sm;
12296 
12297     /* Nothing to do if no SVE.  */
12298     if (!cpu_isar_feature(aa64_sve, cpu)) {
12299         return;
12300     }
12301 
12302     /* Nothing to do if FP is disabled in either EL.  */
12303     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12304         return;
12305     }
12306 
12307     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12308     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12309 
12310     /*
12311      * Both AArch64.TakeException and AArch64.ExceptionReturn
12312      * invoke ResetSVEState when taking an exception from, or
12313      * returning to, AArch32 state when PSTATE.SM is enabled.
12314      */
12315     sm = FIELD_EX64(env->svcr, SVCR, SM);
12316     if (old_a64 != new_a64 && sm) {
12317         arm_reset_sve_state(env);
12318         return;
12319     }
12320 
12321     /*
12322      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12323      * at ELx, or not available because the EL is in AArch32 state, then
12324      * for all purposes other than a direct read, the ZCR_ELx.LEN field
12325      * has an effective value of 0".
12326      *
12327      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12328      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12329      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
12330      * we already have the correct register contents when encountering the
12331      * vq0->vq0 transition between EL0->EL1.
12332      */
12333     old_len = new_len = 0;
12334     if (old_a64) {
12335         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12336     }
12337     if (new_a64) {
12338         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12339     }
12340 
12341     /* When changing vector length, clear inaccessible state.  */
12342     if (new_len < old_len) {
12343         aarch64_sve_narrow_vq(env, new_len + 1);
12344     }
12345 }
12346 #endif
12347 
12348 #ifndef CONFIG_USER_ONLY
12349 ARMSecuritySpace arm_security_space(CPUARMState *env)
12350 {
12351     if (arm_feature(env, ARM_FEATURE_M)) {
12352         return arm_secure_to_space(env->v7m.secure);
12353     }
12354 
12355     /*
12356      * If EL3 is not supported then the secure state is implementation
12357      * defined, in which case QEMU defaults to non-secure.
12358      */
12359     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12360         return ARMSS_NonSecure;
12361     }
12362 
12363     /* Check for AArch64 EL3 or AArch32 Mon. */
12364     if (is_a64(env)) {
12365         if (extract32(env->pstate, 2, 2) == 3) {
12366             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
12367                 return ARMSS_Root;
12368             } else {
12369                 return ARMSS_Secure;
12370             }
12371         }
12372     } else {
12373         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
12374             return ARMSS_Secure;
12375         }
12376     }
12377 
12378     return arm_security_space_below_el3(env);
12379 }
12380 
12381 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
12382 {
12383     assert(!arm_feature(env, ARM_FEATURE_M));
12384 
12385     /*
12386      * If EL3 is not supported then the secure state is implementation
12387      * defined, in which case QEMU defaults to non-secure.
12388      */
12389     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12390         return ARMSS_NonSecure;
12391     }
12392 
12393     /*
12394      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
12395      * Ignoring NSE when !NS retains consistency without having to
12396      * modify other predicates.
12397      */
12398     if (!(env->cp15.scr_el3 & SCR_NS)) {
12399         return ARMSS_Secure;
12400     } else if (env->cp15.scr_el3 & SCR_NSE) {
12401         return ARMSS_Realm;
12402     } else {
12403         return ARMSS_NonSecure;
12404     }
12405 }
12406 #endif /* !CONFIG_USER_ONLY */
12407