xref: /qemu/target/arm/helper.c (revision efade66d)
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 uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1479 {
1480     uint64_t pmcr = env->cp15.c9_pmcr;
1481 
1482     /*
1483      * If EL2 is implemented and enabled for the current security state, reads
1484      * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1485      */
1486     if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1487         pmcr &= ~PMCRN_MASK;
1488         pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1489     }
1490 
1491     return pmcr;
1492 }
1493 
1494 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1495                           uint64_t value)
1496 {
1497     unsigned int i;
1498     uint64_t overflow_mask, new_pmswinc;
1499 
1500     for (i = 0; i < pmu_num_counters(env); i++) {
1501         /* Increment a counter's count iff: */
1502         if ((value & (1 << i)) && /* counter's bit is set */
1503                 /* counter is enabled and not filtered */
1504                 pmu_counter_enabled(env, i) &&
1505                 /* counter is SW_INCR */
1506                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1507             pmevcntr_op_start(env, i);
1508 
1509             /*
1510              * Detect if this write causes an overflow since we can't predict
1511              * PMSWINC overflows like we can for other events
1512              */
1513             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1514 
1515             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1516                 1ULL << 63 : 1ULL << 31;
1517 
1518             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1519                 env->cp15.c9_pmovsr |= (1 << i);
1520                 pmu_update_irq(env);
1521             }
1522 
1523             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1524 
1525             pmevcntr_op_finish(env, i);
1526         }
1527     }
1528 }
1529 
1530 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1531 {
1532     uint64_t ret;
1533     pmccntr_op_start(env);
1534     ret = env->cp15.c15_ccnt;
1535     pmccntr_op_finish(env);
1536     return ret;
1537 }
1538 
1539 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1540                          uint64_t value)
1541 {
1542     /*
1543      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1544      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1545      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1546      * accessed.
1547      */
1548     env->cp15.c9_pmselr = value & 0x1f;
1549 }
1550 
1551 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1552                         uint64_t value)
1553 {
1554     pmccntr_op_start(env);
1555     env->cp15.c15_ccnt = value;
1556     pmccntr_op_finish(env);
1557 }
1558 
1559 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1560                             uint64_t value)
1561 {
1562     uint64_t cur_val = pmccntr_read(env, NULL);
1563 
1564     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1565 }
1566 
1567 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1568                             uint64_t value)
1569 {
1570     pmccntr_op_start(env);
1571     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1572     pmccntr_op_finish(env);
1573 }
1574 
1575 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1576                             uint64_t value)
1577 {
1578     pmccntr_op_start(env);
1579     /* M is not accessible from AArch32 */
1580     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1581         (value & PMCCFILTR);
1582     pmccntr_op_finish(env);
1583 }
1584 
1585 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1586 {
1587     /* M is not visible in AArch32 */
1588     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1589 }
1590 
1591 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1592                             uint64_t value)
1593 {
1594     pmu_op_start(env);
1595     value &= pmu_counter_mask(env);
1596     env->cp15.c9_pmcnten |= value;
1597     pmu_op_finish(env);
1598 }
1599 
1600 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1601                              uint64_t value)
1602 {
1603     pmu_op_start(env);
1604     value &= pmu_counter_mask(env);
1605     env->cp15.c9_pmcnten &= ~value;
1606     pmu_op_finish(env);
1607 }
1608 
1609 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1610                          uint64_t value)
1611 {
1612     value &= pmu_counter_mask(env);
1613     env->cp15.c9_pmovsr &= ~value;
1614     pmu_update_irq(env);
1615 }
1616 
1617 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1618                          uint64_t value)
1619 {
1620     value &= pmu_counter_mask(env);
1621     env->cp15.c9_pmovsr |= value;
1622     pmu_update_irq(env);
1623 }
1624 
1625 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1626                              uint64_t value, const uint8_t counter)
1627 {
1628     if (counter == 31) {
1629         pmccfiltr_write(env, ri, value);
1630     } else if (counter < pmu_num_counters(env)) {
1631         pmevcntr_op_start(env, counter);
1632 
1633         /*
1634          * If this counter's event type is changing, store the current
1635          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1636          * pmevcntr_op_finish has the correct baseline when it converts back to
1637          * a delta.
1638          */
1639         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1640             PMXEVTYPER_EVTCOUNT;
1641         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1642         if (old_event != new_event) {
1643             uint64_t count = 0;
1644             if (event_supported(new_event)) {
1645                 uint16_t event_idx = supported_event_map[new_event];
1646                 count = pm_events[event_idx].get_count(env);
1647             }
1648             env->cp15.c14_pmevcntr_delta[counter] = count;
1649         }
1650 
1651         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1652         pmevcntr_op_finish(env, counter);
1653     }
1654     /*
1655      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1656      * PMSELR value is equal to or greater than the number of implemented
1657      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1658      */
1659 }
1660 
1661 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1662                                const uint8_t counter)
1663 {
1664     if (counter == 31) {
1665         return env->cp15.pmccfiltr_el0;
1666     } else if (counter < pmu_num_counters(env)) {
1667         return env->cp15.c14_pmevtyper[counter];
1668     } else {
1669       /*
1670        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1671        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1672        */
1673         return 0;
1674     }
1675 }
1676 
1677 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1678                               uint64_t value)
1679 {
1680     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1681     pmevtyper_write(env, ri, value, counter);
1682 }
1683 
1684 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1685                                uint64_t value)
1686 {
1687     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1688     env->cp15.c14_pmevtyper[counter] = value;
1689 
1690     /*
1691      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1692      * pmu_op_finish calls when loading saved state for a migration. Because
1693      * we're potentially updating the type of event here, the value written to
1694      * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1695      * different counter type. Therefore, we need to set this value to the
1696      * current count for the counter type we're writing so that pmu_op_finish
1697      * has the correct count for its calculation.
1698      */
1699     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1700     if (event_supported(event)) {
1701         uint16_t event_idx = supported_event_map[event];
1702         env->cp15.c14_pmevcntr_delta[counter] =
1703             pm_events[event_idx].get_count(env);
1704     }
1705 }
1706 
1707 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1708 {
1709     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1710     return pmevtyper_read(env, ri, counter);
1711 }
1712 
1713 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1714                              uint64_t value)
1715 {
1716     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1717 }
1718 
1719 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1720 {
1721     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1722 }
1723 
1724 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1725                              uint64_t value, uint8_t counter)
1726 {
1727     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1728         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1729         value &= MAKE_64BIT_MASK(0, 32);
1730     }
1731     if (counter < pmu_num_counters(env)) {
1732         pmevcntr_op_start(env, counter);
1733         env->cp15.c14_pmevcntr[counter] = value;
1734         pmevcntr_op_finish(env, counter);
1735     }
1736     /*
1737      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1738      * are CONSTRAINED UNPREDICTABLE.
1739      */
1740 }
1741 
1742 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1743                               uint8_t counter)
1744 {
1745     if (counter < pmu_num_counters(env)) {
1746         uint64_t ret;
1747         pmevcntr_op_start(env, counter);
1748         ret = env->cp15.c14_pmevcntr[counter];
1749         pmevcntr_op_finish(env, counter);
1750         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1751             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1752             ret &= MAKE_64BIT_MASK(0, 32);
1753         }
1754         return ret;
1755     } else {
1756       /*
1757        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1758        * are CONSTRAINED UNPREDICTABLE.
1759        */
1760         return 0;
1761     }
1762 }
1763 
1764 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1765                              uint64_t value)
1766 {
1767     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1768     pmevcntr_write(env, ri, value, counter);
1769 }
1770 
1771 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1772 {
1773     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1774     return pmevcntr_read(env, ri, counter);
1775 }
1776 
1777 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1778                              uint64_t value)
1779 {
1780     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1781     assert(counter < pmu_num_counters(env));
1782     env->cp15.c14_pmevcntr[counter] = value;
1783     pmevcntr_write(env, ri, value, counter);
1784 }
1785 
1786 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1787 {
1788     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1789     assert(counter < pmu_num_counters(env));
1790     return env->cp15.c14_pmevcntr[counter];
1791 }
1792 
1793 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1794                              uint64_t value)
1795 {
1796     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1797 }
1798 
1799 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1800 {
1801     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1802 }
1803 
1804 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1805                             uint64_t value)
1806 {
1807     if (arm_feature(env, ARM_FEATURE_V8)) {
1808         env->cp15.c9_pmuserenr = value & 0xf;
1809     } else {
1810         env->cp15.c9_pmuserenr = value & 1;
1811     }
1812 }
1813 
1814 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1815                              uint64_t value)
1816 {
1817     /* We have no event counters so only the C bit can be changed */
1818     value &= pmu_counter_mask(env);
1819     env->cp15.c9_pminten |= value;
1820     pmu_update_irq(env);
1821 }
1822 
1823 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1824                              uint64_t value)
1825 {
1826     value &= pmu_counter_mask(env);
1827     env->cp15.c9_pminten &= ~value;
1828     pmu_update_irq(env);
1829 }
1830 
1831 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1832                        uint64_t value)
1833 {
1834     /*
1835      * Note that even though the AArch64 view of this register has bits
1836      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1837      * architectural requirements for bits which are RES0 only in some
1838      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1839      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1840      */
1841     raw_write(env, ri, value & ~0x1FULL);
1842 }
1843 
1844 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1845 {
1846     /* Begin with base v8.0 state.  */
1847     uint64_t valid_mask = 0x3fff;
1848     ARMCPU *cpu = env_archcpu(env);
1849     uint64_t changed;
1850 
1851     /*
1852      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1853      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1854      * Instead, choose the format based on the mode of EL3.
1855      */
1856     if (arm_el_is_aa64(env, 3)) {
1857         value |= SCR_FW | SCR_AW;      /* RES1 */
1858         valid_mask &= ~SCR_NET;        /* RES0 */
1859 
1860         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1861             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1862             value |= SCR_RW;           /* RAO/WI */
1863         }
1864         if (cpu_isar_feature(aa64_ras, cpu)) {
1865             valid_mask |= SCR_TERR;
1866         }
1867         if (cpu_isar_feature(aa64_lor, cpu)) {
1868             valid_mask |= SCR_TLOR;
1869         }
1870         if (cpu_isar_feature(aa64_pauth, cpu)) {
1871             valid_mask |= SCR_API | SCR_APK;
1872         }
1873         if (cpu_isar_feature(aa64_sel2, cpu)) {
1874             valid_mask |= SCR_EEL2;
1875         } else if (cpu_isar_feature(aa64_rme, cpu)) {
1876             /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1877             value |= SCR_NS;
1878         }
1879         if (cpu_isar_feature(aa64_mte, cpu)) {
1880             valid_mask |= SCR_ATA;
1881         }
1882         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1883             valid_mask |= SCR_ENSCXT;
1884         }
1885         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1886             valid_mask |= SCR_EASE | SCR_NMEA;
1887         }
1888         if (cpu_isar_feature(aa64_sme, cpu)) {
1889             valid_mask |= SCR_ENTP2;
1890         }
1891         if (cpu_isar_feature(aa64_hcx, cpu)) {
1892             valid_mask |= SCR_HXEN;
1893         }
1894         if (cpu_isar_feature(aa64_fgt, cpu)) {
1895             valid_mask |= SCR_FGTEN;
1896         }
1897         if (cpu_isar_feature(aa64_rme, cpu)) {
1898             valid_mask |= SCR_NSE | SCR_GPF;
1899         }
1900     } else {
1901         valid_mask &= ~(SCR_RW | SCR_ST);
1902         if (cpu_isar_feature(aa32_ras, cpu)) {
1903             valid_mask |= SCR_TERR;
1904         }
1905     }
1906 
1907     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1908         valid_mask &= ~SCR_HCE;
1909 
1910         /*
1911          * On ARMv7, SMD (or SCD as it is called in v7) is only
1912          * supported if EL2 exists. The bit is UNK/SBZP when
1913          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1914          * when EL2 is unavailable.
1915          * On ARMv8, this bit is always available.
1916          */
1917         if (arm_feature(env, ARM_FEATURE_V7) &&
1918             !arm_feature(env, ARM_FEATURE_V8)) {
1919             valid_mask &= ~SCR_SMD;
1920         }
1921     }
1922 
1923     /* Clear all-context RES0 bits.  */
1924     value &= valid_mask;
1925     changed = env->cp15.scr_el3 ^ value;
1926     env->cp15.scr_el3 = value;
1927 
1928     /*
1929      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1930      * we must invalidate all TLBs below EL3.
1931      */
1932     if (changed & (SCR_NS | SCR_NSE)) {
1933         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1934                                            ARMMMUIdxBit_E20_0 |
1935                                            ARMMMUIdxBit_E10_1 |
1936                                            ARMMMUIdxBit_E20_2 |
1937                                            ARMMMUIdxBit_E10_1_PAN |
1938                                            ARMMMUIdxBit_E20_2_PAN |
1939                                            ARMMMUIdxBit_E2));
1940     }
1941 }
1942 
1943 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1944 {
1945     /*
1946      * scr_write will set the RES1 bits on an AArch64-only CPU.
1947      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1948      */
1949     scr_write(env, ri, 0);
1950 }
1951 
1952 static CPAccessResult access_tid4(CPUARMState *env,
1953                                   const ARMCPRegInfo *ri,
1954                                   bool isread)
1955 {
1956     if (arm_current_el(env) == 1 &&
1957         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1958         return CP_ACCESS_TRAP_EL2;
1959     }
1960 
1961     return CP_ACCESS_OK;
1962 }
1963 
1964 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1965 {
1966     ARMCPU *cpu = env_archcpu(env);
1967 
1968     /*
1969      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1970      * bank
1971      */
1972     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1973                                         ri->secure & ARM_CP_SECSTATE_S);
1974 
1975     return cpu->ccsidr[index];
1976 }
1977 
1978 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1979                          uint64_t value)
1980 {
1981     raw_write(env, ri, value & 0xf);
1982 }
1983 
1984 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1985 {
1986     CPUState *cs = env_cpu(env);
1987     bool el1 = arm_current_el(env) == 1;
1988     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1989     uint64_t ret = 0;
1990 
1991     if (hcr_el2 & HCR_IMO) {
1992         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1993             ret |= CPSR_I;
1994         }
1995     } else {
1996         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1997             ret |= CPSR_I;
1998         }
1999     }
2000 
2001     if (hcr_el2 & HCR_FMO) {
2002         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2003             ret |= CPSR_F;
2004         }
2005     } else {
2006         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2007             ret |= CPSR_F;
2008         }
2009     }
2010 
2011     if (hcr_el2 & HCR_AMO) {
2012         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
2013             ret |= CPSR_A;
2014         }
2015     }
2016 
2017     return ret;
2018 }
2019 
2020 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2021                                        bool isread)
2022 {
2023     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2024         return CP_ACCESS_TRAP_EL2;
2025     }
2026 
2027     return CP_ACCESS_OK;
2028 }
2029 
2030 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2031                                        bool isread)
2032 {
2033     if (arm_feature(env, ARM_FEATURE_V8)) {
2034         return access_aa64_tid1(env, ri, isread);
2035     }
2036 
2037     return CP_ACCESS_OK;
2038 }
2039 
2040 static const ARMCPRegInfo v7_cp_reginfo[] = {
2041     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2042     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2043       .access = PL1_W, .type = ARM_CP_NOP },
2044     /*
2045      * Performance monitors are implementation defined in v7,
2046      * but with an ARM recommended set of registers, which we
2047      * follow.
2048      *
2049      * Performance registers fall into three categories:
2050      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2051      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2052      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2053      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2054      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2055      */
2056     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2057       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2058       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2059       .writefn = pmcntenset_write,
2060       .accessfn = pmreg_access,
2061       .fgt = FGT_PMCNTEN,
2062       .raw_writefn = raw_write },
2063     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2064       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2065       .access = PL0_RW, .accessfn = pmreg_access,
2066       .fgt = FGT_PMCNTEN,
2067       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2068       .writefn = pmcntenset_write, .raw_writefn = raw_write },
2069     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2070       .access = PL0_RW,
2071       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2072       .accessfn = pmreg_access,
2073       .fgt = FGT_PMCNTEN,
2074       .writefn = pmcntenclr_write,
2075       .type = ARM_CP_ALIAS | ARM_CP_IO },
2076     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2077       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2078       .access = PL0_RW, .accessfn = pmreg_access,
2079       .fgt = FGT_PMCNTEN,
2080       .type = ARM_CP_ALIAS | ARM_CP_IO,
2081       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2082       .writefn = pmcntenclr_write },
2083     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2084       .access = PL0_RW, .type = ARM_CP_IO,
2085       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2086       .accessfn = pmreg_access,
2087       .fgt = FGT_PMOVS,
2088       .writefn = pmovsr_write,
2089       .raw_writefn = raw_write },
2090     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2091       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2092       .access = PL0_RW, .accessfn = pmreg_access,
2093       .fgt = FGT_PMOVS,
2094       .type = ARM_CP_ALIAS | ARM_CP_IO,
2095       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2096       .writefn = pmovsr_write,
2097       .raw_writefn = raw_write },
2098     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2099       .access = PL0_W, .accessfn = pmreg_access_swinc,
2100       .fgt = FGT_PMSWINC_EL0,
2101       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2102       .writefn = pmswinc_write },
2103     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2104       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2105       .access = PL0_W, .accessfn = pmreg_access_swinc,
2106       .fgt = FGT_PMSWINC_EL0,
2107       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2108       .writefn = pmswinc_write },
2109     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2110       .access = PL0_RW, .type = ARM_CP_ALIAS,
2111       .fgt = FGT_PMSELR_EL0,
2112       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2113       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2114       .raw_writefn = raw_write},
2115     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2116       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2117       .access = PL0_RW, .accessfn = pmreg_access_selr,
2118       .fgt = FGT_PMSELR_EL0,
2119       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2120       .writefn = pmselr_write, .raw_writefn = raw_write, },
2121     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2122       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2123       .fgt = FGT_PMCCNTR_EL0,
2124       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2125       .accessfn = pmreg_access_ccntr },
2126     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2127       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2128       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2129       .fgt = FGT_PMCCNTR_EL0,
2130       .type = ARM_CP_IO,
2131       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2132       .readfn = pmccntr_read, .writefn = pmccntr_write,
2133       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2134     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2135       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2136       .access = PL0_RW, .accessfn = pmreg_access,
2137       .fgt = FGT_PMCCFILTR_EL0,
2138       .type = ARM_CP_ALIAS | ARM_CP_IO,
2139       .resetvalue = 0, },
2140     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2141       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2142       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2143       .access = PL0_RW, .accessfn = pmreg_access,
2144       .fgt = FGT_PMCCFILTR_EL0,
2145       .type = ARM_CP_IO,
2146       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2147       .resetvalue = 0, },
2148     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2149       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2150       .accessfn = pmreg_access,
2151       .fgt = FGT_PMEVTYPERN_EL0,
2152       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2153     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2154       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2155       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2156       .accessfn = pmreg_access,
2157       .fgt = FGT_PMEVTYPERN_EL0,
2158       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2159     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2160       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2161       .accessfn = pmreg_access_xevcntr,
2162       .fgt = FGT_PMEVCNTRN_EL0,
2163       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2164     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2165       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2166       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2167       .accessfn = pmreg_access_xevcntr,
2168       .fgt = FGT_PMEVCNTRN_EL0,
2169       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2170     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2171       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2172       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2173       .resetvalue = 0,
2174       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2175     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2176       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2177       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2178       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2179       .resetvalue = 0,
2180       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2181     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2182       .access = PL1_RW, .accessfn = access_tpm,
2183       .fgt = FGT_PMINTEN,
2184       .type = ARM_CP_ALIAS | ARM_CP_IO,
2185       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2186       .resetvalue = 0,
2187       .writefn = pmintenset_write, .raw_writefn = raw_write },
2188     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2189       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2190       .access = PL1_RW, .accessfn = access_tpm,
2191       .fgt = FGT_PMINTEN,
2192       .type = ARM_CP_IO,
2193       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2194       .writefn = pmintenset_write, .raw_writefn = raw_write,
2195       .resetvalue = 0x0 },
2196     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2197       .access = PL1_RW, .accessfn = access_tpm,
2198       .fgt = FGT_PMINTEN,
2199       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2200       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2201       .writefn = pmintenclr_write, },
2202     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2203       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2204       .access = PL1_RW, .accessfn = access_tpm,
2205       .fgt = FGT_PMINTEN,
2206       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2207       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2208       .writefn = pmintenclr_write },
2209     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2210       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2211       .access = PL1_R,
2212       .accessfn = access_tid4,
2213       .fgt = FGT_CCSIDR_EL1,
2214       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2215     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2216       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2217       .access = PL1_RW,
2218       .accessfn = access_tid4,
2219       .fgt = FGT_CSSELR_EL1,
2220       .writefn = csselr_write, .resetvalue = 0,
2221       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2222                              offsetof(CPUARMState, cp15.csselr_ns) } },
2223     /*
2224      * Auxiliary ID register: this actually has an IMPDEF value but for now
2225      * just RAZ for all cores:
2226      */
2227     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2228       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2229       .access = PL1_R, .type = ARM_CP_CONST,
2230       .accessfn = access_aa64_tid1,
2231       .fgt = FGT_AIDR_EL1,
2232       .resetvalue = 0 },
2233     /*
2234      * Auxiliary fault status registers: these also are IMPDEF, and we
2235      * choose to RAZ/WI for all cores.
2236      */
2237     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2238       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2239       .access = PL1_RW, .accessfn = access_tvm_trvm,
2240       .fgt = FGT_AFSR0_EL1,
2241       .type = ARM_CP_CONST, .resetvalue = 0 },
2242     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2243       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2244       .access = PL1_RW, .accessfn = access_tvm_trvm,
2245       .fgt = FGT_AFSR1_EL1,
2246       .type = ARM_CP_CONST, .resetvalue = 0 },
2247     /*
2248      * MAIR can just read-as-written because we don't implement caches
2249      * and so don't need to care about memory attributes.
2250      */
2251     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2252       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2253       .access = PL1_RW, .accessfn = access_tvm_trvm,
2254       .fgt = FGT_MAIR_EL1,
2255       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2256       .resetvalue = 0 },
2257     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2258       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2259       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2260       .resetvalue = 0 },
2261     /*
2262      * For non-long-descriptor page tables these are PRRR and NMRR;
2263      * regardless they still act as reads-as-written for QEMU.
2264      */
2265      /*
2266       * MAIR0/1 are defined separately from their 64-bit counterpart which
2267       * allows them to assign the correct fieldoffset based on the endianness
2268       * handled in the field definitions.
2269       */
2270     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2271       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2272       .access = PL1_RW, .accessfn = access_tvm_trvm,
2273       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2274                              offsetof(CPUARMState, cp15.mair0_ns) },
2275       .resetfn = arm_cp_reset_ignore },
2276     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2277       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2278       .access = PL1_RW, .accessfn = access_tvm_trvm,
2279       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2280                              offsetof(CPUARMState, cp15.mair1_ns) },
2281       .resetfn = arm_cp_reset_ignore },
2282     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2283       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2284       .fgt = FGT_ISR_EL1,
2285       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2286     /* 32 bit ITLB invalidates */
2287     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2288       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2289       .writefn = tlbiall_write },
2290     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2291       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2292       .writefn = tlbimva_write },
2293     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2294       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2295       .writefn = tlbiasid_write },
2296     /* 32 bit DTLB invalidates */
2297     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2298       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2299       .writefn = tlbiall_write },
2300     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2301       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2302       .writefn = tlbimva_write },
2303     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2304       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2305       .writefn = tlbiasid_write },
2306     /* 32 bit TLB invalidates */
2307     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2308       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2309       .writefn = tlbiall_write },
2310     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2311       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2312       .writefn = tlbimva_write },
2313     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2314       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2315       .writefn = tlbiasid_write },
2316     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2317       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2318       .writefn = tlbimvaa_write },
2319 };
2320 
2321 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2322     /* 32 bit TLB invalidates, Inner Shareable */
2323     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2324       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2325       .writefn = tlbiall_is_write },
2326     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2327       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2328       .writefn = tlbimva_is_write },
2329     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2330       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2331       .writefn = tlbiasid_is_write },
2332     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2333       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2334       .writefn = tlbimvaa_is_write },
2335 };
2336 
2337 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2338     /* PMOVSSET is not implemented in v7 before v7ve */
2339     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2340       .access = PL0_RW, .accessfn = pmreg_access,
2341       .fgt = FGT_PMOVS,
2342       .type = ARM_CP_ALIAS | ARM_CP_IO,
2343       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2344       .writefn = pmovsset_write,
2345       .raw_writefn = raw_write },
2346     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2347       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2348       .access = PL0_RW, .accessfn = pmreg_access,
2349       .fgt = FGT_PMOVS,
2350       .type = ARM_CP_ALIAS | ARM_CP_IO,
2351       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2352       .writefn = pmovsset_write,
2353       .raw_writefn = raw_write },
2354 };
2355 
2356 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2357                         uint64_t value)
2358 {
2359     value &= 1;
2360     env->teecr = value;
2361 }
2362 
2363 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2364                                    bool isread)
2365 {
2366     /*
2367      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2368      * at all, so we don't need to check whether we're v8A.
2369      */
2370     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2371         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2372         return CP_ACCESS_TRAP_EL2;
2373     }
2374     return CP_ACCESS_OK;
2375 }
2376 
2377 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2378                                     bool isread)
2379 {
2380     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2381         return CP_ACCESS_TRAP;
2382     }
2383     return teecr_access(env, ri, isread);
2384 }
2385 
2386 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2387     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2388       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2389       .resetvalue = 0,
2390       .writefn = teecr_write, .accessfn = teecr_access },
2391     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2392       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2393       .accessfn = teehbr_access, .resetvalue = 0 },
2394 };
2395 
2396 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2397     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2398       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2399       .access = PL0_RW,
2400       .fgt = FGT_TPIDR_EL0,
2401       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2402     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2403       .access = PL0_RW,
2404       .fgt = FGT_TPIDR_EL0,
2405       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2406                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2407       .resetfn = arm_cp_reset_ignore },
2408     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2409       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2410       .access = PL0_R | PL1_W,
2411       .fgt = FGT_TPIDRRO_EL0,
2412       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2413       .resetvalue = 0},
2414     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2415       .access = PL0_R | PL1_W,
2416       .fgt = FGT_TPIDRRO_EL0,
2417       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2418                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2419       .resetfn = arm_cp_reset_ignore },
2420     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2421       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2422       .access = PL1_RW,
2423       .fgt = FGT_TPIDR_EL1,
2424       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2425     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2426       .access = PL1_RW,
2427       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2428                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2429       .resetvalue = 0 },
2430 };
2431 
2432 #ifndef CONFIG_USER_ONLY
2433 
2434 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2435                                        bool isread)
2436 {
2437     /*
2438      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2439      * Writable only at the highest implemented exception level.
2440      */
2441     int el = arm_current_el(env);
2442     uint64_t hcr;
2443     uint32_t cntkctl;
2444 
2445     switch (el) {
2446     case 0:
2447         hcr = arm_hcr_el2_eff(env);
2448         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2449             cntkctl = env->cp15.cnthctl_el2;
2450         } else {
2451             cntkctl = env->cp15.c14_cntkctl;
2452         }
2453         if (!extract32(cntkctl, 0, 2)) {
2454             return CP_ACCESS_TRAP;
2455         }
2456         break;
2457     case 1:
2458         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2459             arm_is_secure_below_el3(env)) {
2460             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2461             return CP_ACCESS_TRAP_UNCATEGORIZED;
2462         }
2463         break;
2464     case 2:
2465     case 3:
2466         break;
2467     }
2468 
2469     if (!isread && el < arm_highest_el(env)) {
2470         return CP_ACCESS_TRAP_UNCATEGORIZED;
2471     }
2472 
2473     return CP_ACCESS_OK;
2474 }
2475 
2476 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2477                                         bool isread)
2478 {
2479     unsigned int cur_el = arm_current_el(env);
2480     bool has_el2 = arm_is_el2_enabled(env);
2481     uint64_t hcr = arm_hcr_el2_eff(env);
2482 
2483     switch (cur_el) {
2484     case 0:
2485         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2486         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2487             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2488                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2489         }
2490 
2491         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2492         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2493             return CP_ACCESS_TRAP;
2494         }
2495         /* fall through */
2496     case 1:
2497         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2498         if (has_el2 && timeridx == GTIMER_PHYS &&
2499             (hcr & HCR_E2H
2500              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2501              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2502             return CP_ACCESS_TRAP_EL2;
2503         }
2504         break;
2505     }
2506     return CP_ACCESS_OK;
2507 }
2508 
2509 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2510                                       bool isread)
2511 {
2512     unsigned int cur_el = arm_current_el(env);
2513     bool has_el2 = arm_is_el2_enabled(env);
2514     uint64_t hcr = arm_hcr_el2_eff(env);
2515 
2516     switch (cur_el) {
2517     case 0:
2518         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2519             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2520             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2521                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2522         }
2523 
2524         /*
2525          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2526          * EL0 if EL0[PV]TEN is zero.
2527          */
2528         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2529             return CP_ACCESS_TRAP;
2530         }
2531         /* fall through */
2532 
2533     case 1:
2534         if (has_el2 && timeridx == GTIMER_PHYS) {
2535             if (hcr & HCR_E2H) {
2536                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2537                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2538                     return CP_ACCESS_TRAP_EL2;
2539                 }
2540             } else {
2541                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2542                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2543                     return CP_ACCESS_TRAP_EL2;
2544                 }
2545             }
2546         }
2547         break;
2548     }
2549     return CP_ACCESS_OK;
2550 }
2551 
2552 static CPAccessResult gt_pct_access(CPUARMState *env,
2553                                     const ARMCPRegInfo *ri,
2554                                     bool isread)
2555 {
2556     return gt_counter_access(env, GTIMER_PHYS, isread);
2557 }
2558 
2559 static CPAccessResult gt_vct_access(CPUARMState *env,
2560                                     const ARMCPRegInfo *ri,
2561                                     bool isread)
2562 {
2563     return gt_counter_access(env, GTIMER_VIRT, isread);
2564 }
2565 
2566 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2567                                        bool isread)
2568 {
2569     return gt_timer_access(env, GTIMER_PHYS, isread);
2570 }
2571 
2572 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2573                                        bool isread)
2574 {
2575     return gt_timer_access(env, GTIMER_VIRT, isread);
2576 }
2577 
2578 static CPAccessResult gt_stimer_access(CPUARMState *env,
2579                                        const ARMCPRegInfo *ri,
2580                                        bool isread)
2581 {
2582     /*
2583      * The AArch64 register view of the secure physical timer is
2584      * always accessible from EL3, and configurably accessible from
2585      * Secure EL1.
2586      */
2587     switch (arm_current_el(env)) {
2588     case 1:
2589         if (!arm_is_secure(env)) {
2590             return CP_ACCESS_TRAP;
2591         }
2592         if (!(env->cp15.scr_el3 & SCR_ST)) {
2593             return CP_ACCESS_TRAP_EL3;
2594         }
2595         return CP_ACCESS_OK;
2596     case 0:
2597     case 2:
2598         return CP_ACCESS_TRAP;
2599     case 3:
2600         return CP_ACCESS_OK;
2601     default:
2602         g_assert_not_reached();
2603     }
2604 }
2605 
2606 static uint64_t gt_get_countervalue(CPUARMState *env)
2607 {
2608     ARMCPU *cpu = env_archcpu(env);
2609 
2610     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2611 }
2612 
2613 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2614 {
2615     CPUARMState *env = &cpu->env;
2616     uint64_t cnthctl = env->cp15.cnthctl_el2;
2617     ARMSecuritySpace ss = arm_security_space(env);
2618     /* ISTATUS && !IMASK */
2619     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2620 
2621     /*
2622      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2623      * It is RES0 in Secure and NonSecure state.
2624      */
2625     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2626         ((timeridx == GTIMER_VIRT && (cnthctl & CNTHCTL_CNTVMASK)) ||
2627          (timeridx == GTIMER_PHYS && (cnthctl & CNTHCTL_CNTPMASK)))) {
2628         irqstate = 0;
2629     }
2630 
2631     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2632     trace_arm_gt_update_irq(timeridx, irqstate);
2633 }
2634 
2635 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2636 {
2637     /*
2638      * Changing security state between Root and Secure/NonSecure, which may
2639      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2640      * mask bits. Update the IRQ state accordingly.
2641      */
2642     gt_update_irq(cpu, GTIMER_VIRT);
2643     gt_update_irq(cpu, GTIMER_PHYS);
2644 }
2645 
2646 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2647 {
2648     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2649 
2650     if (gt->ctl & 1) {
2651         /*
2652          * Timer enabled: calculate and set current ISTATUS, irq, and
2653          * reset timer to when ISTATUS next has to change
2654          */
2655         uint64_t offset = timeridx == GTIMER_VIRT ?
2656                                       cpu->env.cp15.cntvoff_el2 : 0;
2657         uint64_t count = gt_get_countervalue(&cpu->env);
2658         /* Note that this must be unsigned 64 bit arithmetic: */
2659         int istatus = count - offset >= gt->cval;
2660         uint64_t nexttick;
2661 
2662         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2663 
2664         if (istatus) {
2665             /*
2666              * Next transition is when (count - offset) rolls back over to 0.
2667              * If offset > count then this is when count == offset;
2668              * if offset <= count then this is when count == offset + 2^64
2669              * For the latter case we set nexttick to an "as far in future
2670              * as possible" value and let the code below handle it.
2671              */
2672             if (offset > count) {
2673                 nexttick = offset;
2674             } else {
2675                 nexttick = UINT64_MAX;
2676             }
2677         } else {
2678             /*
2679              * Next transition is when (count - offset) == cval, i.e.
2680              * when count == (cval + offset).
2681              * If that would overflow, then again we set up the next interrupt
2682              * for "as far in the future as possible" for the code below.
2683              */
2684             if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2685                 nexttick = UINT64_MAX;
2686             }
2687         }
2688         /*
2689          * Note that the desired next expiry time might be beyond the
2690          * signed-64-bit range of a QEMUTimer -- in this case we just
2691          * set the timer for as far in the future as possible. When the
2692          * timer expires we will reset the timer for any remaining period.
2693          */
2694         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2695             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2696         } else {
2697             timer_mod(cpu->gt_timer[timeridx], nexttick);
2698         }
2699         trace_arm_gt_recalc(timeridx, nexttick);
2700     } else {
2701         /* Timer disabled: ISTATUS and timer output always clear */
2702         gt->ctl &= ~4;
2703         timer_del(cpu->gt_timer[timeridx]);
2704         trace_arm_gt_recalc_disabled(timeridx);
2705     }
2706     gt_update_irq(cpu, timeridx);
2707 }
2708 
2709 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2710                            int timeridx)
2711 {
2712     ARMCPU *cpu = env_archcpu(env);
2713 
2714     timer_del(cpu->gt_timer[timeridx]);
2715 }
2716 
2717 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2718 {
2719     return gt_get_countervalue(env);
2720 }
2721 
2722 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2723 {
2724     uint64_t hcr;
2725 
2726     switch (arm_current_el(env)) {
2727     case 2:
2728         hcr = arm_hcr_el2_eff(env);
2729         if (hcr & HCR_E2H) {
2730             return 0;
2731         }
2732         break;
2733     case 0:
2734         hcr = arm_hcr_el2_eff(env);
2735         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2736             return 0;
2737         }
2738         break;
2739     }
2740 
2741     return env->cp15.cntvoff_el2;
2742 }
2743 
2744 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2745 {
2746     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2747 }
2748 
2749 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2750                           int timeridx,
2751                           uint64_t value)
2752 {
2753     trace_arm_gt_cval_write(timeridx, value);
2754     env->cp15.c14_timer[timeridx].cval = value;
2755     gt_recalc_timer(env_archcpu(env), timeridx);
2756 }
2757 
2758 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2759                              int timeridx)
2760 {
2761     uint64_t offset = 0;
2762 
2763     switch (timeridx) {
2764     case GTIMER_VIRT:
2765     case GTIMER_HYPVIRT:
2766         offset = gt_virt_cnt_offset(env);
2767         break;
2768     }
2769 
2770     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2771                       (gt_get_countervalue(env) - offset));
2772 }
2773 
2774 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2775                           int timeridx,
2776                           uint64_t value)
2777 {
2778     uint64_t offset = 0;
2779 
2780     switch (timeridx) {
2781     case GTIMER_VIRT:
2782     case GTIMER_HYPVIRT:
2783         offset = gt_virt_cnt_offset(env);
2784         break;
2785     }
2786 
2787     trace_arm_gt_tval_write(timeridx, value);
2788     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2789                                          sextract64(value, 0, 32);
2790     gt_recalc_timer(env_archcpu(env), timeridx);
2791 }
2792 
2793 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2794                          int timeridx,
2795                          uint64_t value)
2796 {
2797     ARMCPU *cpu = env_archcpu(env);
2798     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2799 
2800     trace_arm_gt_ctl_write(timeridx, value);
2801     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2802     if ((oldval ^ value) & 1) {
2803         /* Enable toggled */
2804         gt_recalc_timer(cpu, timeridx);
2805     } else if ((oldval ^ value) & 2) {
2806         /*
2807          * IMASK toggled: don't need to recalculate,
2808          * just set the interrupt line based on ISTATUS
2809          */
2810         trace_arm_gt_imask_toggle(timeridx);
2811         gt_update_irq(cpu, timeridx);
2812     }
2813 }
2814 
2815 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2816 {
2817     gt_timer_reset(env, ri, GTIMER_PHYS);
2818 }
2819 
2820 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2821                                uint64_t value)
2822 {
2823     gt_cval_write(env, ri, GTIMER_PHYS, value);
2824 }
2825 
2826 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2827 {
2828     return gt_tval_read(env, ri, GTIMER_PHYS);
2829 }
2830 
2831 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2832                                uint64_t value)
2833 {
2834     gt_tval_write(env, ri, GTIMER_PHYS, value);
2835 }
2836 
2837 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2838                               uint64_t value)
2839 {
2840     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2841 }
2842 
2843 static int gt_phys_redir_timeridx(CPUARMState *env)
2844 {
2845     switch (arm_mmu_idx(env)) {
2846     case ARMMMUIdx_E20_0:
2847     case ARMMMUIdx_E20_2:
2848     case ARMMMUIdx_E20_2_PAN:
2849         return GTIMER_HYP;
2850     default:
2851         return GTIMER_PHYS;
2852     }
2853 }
2854 
2855 static int gt_virt_redir_timeridx(CPUARMState *env)
2856 {
2857     switch (arm_mmu_idx(env)) {
2858     case ARMMMUIdx_E20_0:
2859     case ARMMMUIdx_E20_2:
2860     case ARMMMUIdx_E20_2_PAN:
2861         return GTIMER_HYPVIRT;
2862     default:
2863         return GTIMER_VIRT;
2864     }
2865 }
2866 
2867 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2868                                         const ARMCPRegInfo *ri)
2869 {
2870     int timeridx = gt_phys_redir_timeridx(env);
2871     return env->cp15.c14_timer[timeridx].cval;
2872 }
2873 
2874 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2875                                      uint64_t value)
2876 {
2877     int timeridx = gt_phys_redir_timeridx(env);
2878     gt_cval_write(env, ri, timeridx, value);
2879 }
2880 
2881 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2882                                         const ARMCPRegInfo *ri)
2883 {
2884     int timeridx = gt_phys_redir_timeridx(env);
2885     return gt_tval_read(env, ri, timeridx);
2886 }
2887 
2888 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2889                                      uint64_t value)
2890 {
2891     int timeridx = gt_phys_redir_timeridx(env);
2892     gt_tval_write(env, ri, timeridx, value);
2893 }
2894 
2895 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2896                                        const ARMCPRegInfo *ri)
2897 {
2898     int timeridx = gt_phys_redir_timeridx(env);
2899     return env->cp15.c14_timer[timeridx].ctl;
2900 }
2901 
2902 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2903                                     uint64_t value)
2904 {
2905     int timeridx = gt_phys_redir_timeridx(env);
2906     gt_ctl_write(env, ri, timeridx, value);
2907 }
2908 
2909 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2910 {
2911     gt_timer_reset(env, ri, GTIMER_VIRT);
2912 }
2913 
2914 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2915                                uint64_t value)
2916 {
2917     gt_cval_write(env, ri, GTIMER_VIRT, value);
2918 }
2919 
2920 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2921 {
2922     return gt_tval_read(env, ri, GTIMER_VIRT);
2923 }
2924 
2925 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2926                                uint64_t value)
2927 {
2928     gt_tval_write(env, ri, GTIMER_VIRT, value);
2929 }
2930 
2931 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2932                               uint64_t value)
2933 {
2934     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2935 }
2936 
2937 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2938                              uint64_t value)
2939 {
2940     ARMCPU *cpu = env_archcpu(env);
2941     uint32_t oldval = env->cp15.cnthctl_el2;
2942 
2943     raw_write(env, ri, value);
2944 
2945     if ((oldval ^ value) & CNTHCTL_CNTVMASK) {
2946         gt_update_irq(cpu, GTIMER_VIRT);
2947     } else if ((oldval ^ value) & CNTHCTL_CNTPMASK) {
2948         gt_update_irq(cpu, GTIMER_PHYS);
2949     }
2950 }
2951 
2952 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2953                               uint64_t value)
2954 {
2955     ARMCPU *cpu = env_archcpu(env);
2956 
2957     trace_arm_gt_cntvoff_write(value);
2958     raw_write(env, ri, value);
2959     gt_recalc_timer(cpu, GTIMER_VIRT);
2960 }
2961 
2962 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2963                                         const ARMCPRegInfo *ri)
2964 {
2965     int timeridx = gt_virt_redir_timeridx(env);
2966     return env->cp15.c14_timer[timeridx].cval;
2967 }
2968 
2969 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2970                                      uint64_t value)
2971 {
2972     int timeridx = gt_virt_redir_timeridx(env);
2973     gt_cval_write(env, ri, timeridx, value);
2974 }
2975 
2976 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2977                                         const ARMCPRegInfo *ri)
2978 {
2979     int timeridx = gt_virt_redir_timeridx(env);
2980     return gt_tval_read(env, ri, timeridx);
2981 }
2982 
2983 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2984                                      uint64_t value)
2985 {
2986     int timeridx = gt_virt_redir_timeridx(env);
2987     gt_tval_write(env, ri, timeridx, value);
2988 }
2989 
2990 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2991                                        const ARMCPRegInfo *ri)
2992 {
2993     int timeridx = gt_virt_redir_timeridx(env);
2994     return env->cp15.c14_timer[timeridx].ctl;
2995 }
2996 
2997 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2998                                     uint64_t value)
2999 {
3000     int timeridx = gt_virt_redir_timeridx(env);
3001     gt_ctl_write(env, ri, timeridx, value);
3002 }
3003 
3004 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3005 {
3006     gt_timer_reset(env, ri, GTIMER_HYP);
3007 }
3008 
3009 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3010                               uint64_t value)
3011 {
3012     gt_cval_write(env, ri, GTIMER_HYP, value);
3013 }
3014 
3015 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3016 {
3017     return gt_tval_read(env, ri, GTIMER_HYP);
3018 }
3019 
3020 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3021                               uint64_t value)
3022 {
3023     gt_tval_write(env, ri, GTIMER_HYP, value);
3024 }
3025 
3026 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3027                               uint64_t value)
3028 {
3029     gt_ctl_write(env, ri, GTIMER_HYP, value);
3030 }
3031 
3032 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3033 {
3034     gt_timer_reset(env, ri, GTIMER_SEC);
3035 }
3036 
3037 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3038                               uint64_t value)
3039 {
3040     gt_cval_write(env, ri, GTIMER_SEC, value);
3041 }
3042 
3043 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3044 {
3045     return gt_tval_read(env, ri, GTIMER_SEC);
3046 }
3047 
3048 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3049                               uint64_t value)
3050 {
3051     gt_tval_write(env, ri, GTIMER_SEC, value);
3052 }
3053 
3054 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3055                               uint64_t value)
3056 {
3057     gt_ctl_write(env, ri, GTIMER_SEC, value);
3058 }
3059 
3060 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3061 {
3062     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3063 }
3064 
3065 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3066                              uint64_t value)
3067 {
3068     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3069 }
3070 
3071 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3072 {
3073     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3074 }
3075 
3076 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3077                              uint64_t value)
3078 {
3079     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3080 }
3081 
3082 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3083                             uint64_t value)
3084 {
3085     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3086 }
3087 
3088 void arm_gt_ptimer_cb(void *opaque)
3089 {
3090     ARMCPU *cpu = opaque;
3091 
3092     gt_recalc_timer(cpu, GTIMER_PHYS);
3093 }
3094 
3095 void arm_gt_vtimer_cb(void *opaque)
3096 {
3097     ARMCPU *cpu = opaque;
3098 
3099     gt_recalc_timer(cpu, GTIMER_VIRT);
3100 }
3101 
3102 void arm_gt_htimer_cb(void *opaque)
3103 {
3104     ARMCPU *cpu = opaque;
3105 
3106     gt_recalc_timer(cpu, GTIMER_HYP);
3107 }
3108 
3109 void arm_gt_stimer_cb(void *opaque)
3110 {
3111     ARMCPU *cpu = opaque;
3112 
3113     gt_recalc_timer(cpu, GTIMER_SEC);
3114 }
3115 
3116 void arm_gt_hvtimer_cb(void *opaque)
3117 {
3118     ARMCPU *cpu = opaque;
3119 
3120     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3121 }
3122 
3123 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3124 {
3125     ARMCPU *cpu = env_archcpu(env);
3126 
3127     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3128 }
3129 
3130 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3131     /*
3132      * Note that CNTFRQ is purely reads-as-written for the benefit
3133      * of software; writing it doesn't actually change the timer frequency.
3134      * Our reset value matches the fixed frequency we implement the timer at.
3135      */
3136     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3137       .type = ARM_CP_ALIAS,
3138       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3139       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3140     },
3141     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3142       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3143       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3144       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3145       .resetfn = arm_gt_cntfrq_reset,
3146     },
3147     /* overall control: mostly access permissions */
3148     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3149       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3150       .access = PL1_RW,
3151       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3152       .resetvalue = 0,
3153     },
3154     /* per-timer control */
3155     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3156       .secure = ARM_CP_SECSTATE_NS,
3157       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3158       .accessfn = gt_ptimer_access,
3159       .fieldoffset = offsetoflow32(CPUARMState,
3160                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3161       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3162       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3163     },
3164     { .name = "CNTP_CTL_S",
3165       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3166       .secure = ARM_CP_SECSTATE_S,
3167       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3168       .accessfn = gt_ptimer_access,
3169       .fieldoffset = offsetoflow32(CPUARMState,
3170                                    cp15.c14_timer[GTIMER_SEC].ctl),
3171       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3172     },
3173     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3174       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3175       .type = ARM_CP_IO, .access = PL0_RW,
3176       .accessfn = gt_ptimer_access,
3177       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3178       .resetvalue = 0,
3179       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3180       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3181     },
3182     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3183       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3184       .accessfn = gt_vtimer_access,
3185       .fieldoffset = offsetoflow32(CPUARMState,
3186                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3187       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3188       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3189     },
3190     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3191       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3192       .type = ARM_CP_IO, .access = PL0_RW,
3193       .accessfn = gt_vtimer_access,
3194       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3195       .resetvalue = 0,
3196       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3197       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3198     },
3199     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3200     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3201       .secure = ARM_CP_SECSTATE_NS,
3202       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3203       .accessfn = gt_ptimer_access,
3204       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3205     },
3206     { .name = "CNTP_TVAL_S",
3207       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3208       .secure = ARM_CP_SECSTATE_S,
3209       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3210       .accessfn = gt_ptimer_access,
3211       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3212     },
3213     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3214       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3215       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3216       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3217       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3218     },
3219     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3220       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3221       .accessfn = gt_vtimer_access,
3222       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3223     },
3224     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3225       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3226       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3227       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3228       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3229     },
3230     /* The counter itself */
3231     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3232       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3233       .accessfn = gt_pct_access,
3234       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3235     },
3236     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3237       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3238       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3239       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3240     },
3241     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3242       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3243       .accessfn = gt_vct_access,
3244       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3245     },
3246     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3247       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3248       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3249       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3250     },
3251     /* Comparison value, indicating when the timer goes off */
3252     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3253       .secure = ARM_CP_SECSTATE_NS,
3254       .access = PL0_RW,
3255       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3256       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3257       .accessfn = gt_ptimer_access,
3258       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3259       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3260     },
3261     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3262       .secure = ARM_CP_SECSTATE_S,
3263       .access = PL0_RW,
3264       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3265       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3266       .accessfn = gt_ptimer_access,
3267       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3268     },
3269     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3270       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3271       .access = PL0_RW,
3272       .type = ARM_CP_IO,
3273       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3274       .resetvalue = 0, .accessfn = gt_ptimer_access,
3275       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3276       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3277     },
3278     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3279       .access = PL0_RW,
3280       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3281       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3282       .accessfn = gt_vtimer_access,
3283       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3284       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3285     },
3286     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3287       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3288       .access = PL0_RW,
3289       .type = ARM_CP_IO,
3290       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3291       .resetvalue = 0, .accessfn = gt_vtimer_access,
3292       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3293       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3294     },
3295     /*
3296      * Secure timer -- this is actually restricted to only EL3
3297      * and configurably Secure-EL1 via the accessfn.
3298      */
3299     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3300       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3301       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3302       .accessfn = gt_stimer_access,
3303       .readfn = gt_sec_tval_read,
3304       .writefn = gt_sec_tval_write,
3305       .resetfn = gt_sec_timer_reset,
3306     },
3307     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3308       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3309       .type = ARM_CP_IO, .access = PL1_RW,
3310       .accessfn = gt_stimer_access,
3311       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3312       .resetvalue = 0,
3313       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3314     },
3315     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3316       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3317       .type = ARM_CP_IO, .access = PL1_RW,
3318       .accessfn = gt_stimer_access,
3319       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3320       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3321     },
3322 };
3323 
3324 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3325                                  bool isread)
3326 {
3327     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3328         return CP_ACCESS_TRAP;
3329     }
3330     return CP_ACCESS_OK;
3331 }
3332 
3333 #else
3334 
3335 /*
3336  * In user-mode most of the generic timer registers are inaccessible
3337  * however modern kernels (4.12+) allow access to cntvct_el0
3338  */
3339 
3340 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3341 {
3342     ARMCPU *cpu = env_archcpu(env);
3343 
3344     /*
3345      * Currently we have no support for QEMUTimer in linux-user so we
3346      * can't call gt_get_countervalue(env), instead we directly
3347      * call the lower level functions.
3348      */
3349     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3350 }
3351 
3352 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3353     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3354       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3355       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3356       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3357       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3358     },
3359     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3360       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3361       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3362       .readfn = gt_virt_cnt_read,
3363     },
3364 };
3365 
3366 #endif
3367 
3368 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3369 {
3370     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3371         raw_write(env, ri, value);
3372     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3373         raw_write(env, ri, value & 0xfffff6ff);
3374     } else {
3375         raw_write(env, ri, value & 0xfffff1ff);
3376     }
3377 }
3378 
3379 #ifndef CONFIG_USER_ONLY
3380 /* get_phys_addr() isn't present for user-mode-only targets */
3381 
3382 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3383                                  bool isread)
3384 {
3385     if (ri->opc2 & 4) {
3386         /*
3387          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3388          * Secure EL1 (which can only happen if EL3 is AArch64).
3389          * They are simply UNDEF if executed from NS EL1.
3390          * They function normally from EL2 or EL3.
3391          */
3392         if (arm_current_el(env) == 1) {
3393             if (arm_is_secure_below_el3(env)) {
3394                 if (env->cp15.scr_el3 & SCR_EEL2) {
3395                     return CP_ACCESS_TRAP_EL2;
3396                 }
3397                 return CP_ACCESS_TRAP_EL3;
3398             }
3399             return CP_ACCESS_TRAP_UNCATEGORIZED;
3400         }
3401     }
3402     return CP_ACCESS_OK;
3403 }
3404 
3405 #ifdef CONFIG_TCG
3406 static int par_el1_shareability(GetPhysAddrResult *res)
3407 {
3408     /*
3409      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3410      * memory -- see pseudocode PAREncodeShareability().
3411      */
3412     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3413         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3414         return 2;
3415     }
3416     return res->cacheattrs.shareability;
3417 }
3418 
3419 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3420                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3421                              ARMSecuritySpace ss)
3422 {
3423     bool ret;
3424     uint64_t par64;
3425     bool format64 = false;
3426     ARMMMUFaultInfo fi = {};
3427     GetPhysAddrResult res = {};
3428 
3429     /*
3430      * I_MXTJT: Granule protection checks are not performed on the final address
3431      * of a successful translation.
3432      */
3433     ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss,
3434                                          &res, &fi);
3435 
3436     /*
3437      * ATS operations only do S1 or S1+S2 translations, so we never
3438      * have to deal with the ARMCacheAttrs format for S2 only.
3439      */
3440     assert(!res.cacheattrs.is_s2_format);
3441 
3442     if (ret) {
3443         /*
3444          * Some kinds of translation fault must cause exceptions rather
3445          * than being reported in the PAR.
3446          */
3447         int current_el = arm_current_el(env);
3448         int target_el;
3449         uint32_t syn, fsr, fsc;
3450         bool take_exc = false;
3451 
3452         if (fi.s1ptw && current_el == 1
3453             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3454             /*
3455              * Synchronous stage 2 fault on an access made as part of the
3456              * translation table walk for AT S1E0* or AT S1E1* insn
3457              * executed from NS EL1. If this is a synchronous external abort
3458              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3459              * to EL3. Otherwise the fault is taken as an exception to EL2,
3460              * and HPFAR_EL2 holds the faulting IPA.
3461              */
3462             if (fi.type == ARMFault_SyncExternalOnWalk &&
3463                 (env->cp15.scr_el3 & SCR_EA)) {
3464                 target_el = 3;
3465             } else {
3466                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3467                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3468                     env->cp15.hpfar_el2 |= HPFAR_NS;
3469                 }
3470                 target_el = 2;
3471             }
3472             take_exc = true;
3473         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3474             /*
3475              * Synchronous external aborts during a translation table walk
3476              * are taken as Data Abort exceptions.
3477              */
3478             if (fi.stage2) {
3479                 if (current_el == 3) {
3480                     target_el = 3;
3481                 } else {
3482                     target_el = 2;
3483                 }
3484             } else {
3485                 target_el = exception_target_el(env);
3486             }
3487             take_exc = true;
3488         }
3489 
3490         if (take_exc) {
3491             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3492             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3493                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3494                 fsr = arm_fi_to_lfsc(&fi);
3495                 fsc = extract32(fsr, 0, 6);
3496             } else {
3497                 fsr = arm_fi_to_sfsc(&fi);
3498                 fsc = 0x3f;
3499             }
3500             /*
3501              * Report exception with ESR indicating a fault due to a
3502              * translation table walk for a cache maintenance instruction.
3503              */
3504             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3505                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3506             env->exception.vaddress = value;
3507             env->exception.fsr = fsr;
3508             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3509         }
3510     }
3511 
3512     if (is_a64(env)) {
3513         format64 = true;
3514     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3515         /*
3516          * ATS1Cxx:
3517          * * TTBCR.EAE determines whether the result is returned using the
3518          *   32-bit or the 64-bit PAR format
3519          * * Instructions executed in Hyp mode always use the 64bit format
3520          *
3521          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3522          * * The Non-secure TTBCR.EAE bit is set to 1
3523          * * The implementation includes EL2, and the value of HCR.VM is 1
3524          *
3525          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3526          *
3527          * ATS1Hx always uses the 64bit format.
3528          */
3529         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3530 
3531         if (arm_feature(env, ARM_FEATURE_EL2)) {
3532             if (mmu_idx == ARMMMUIdx_E10_0 ||
3533                 mmu_idx == ARMMMUIdx_E10_1 ||
3534                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3535                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3536             } else {
3537                 format64 |= arm_current_el(env) == 2;
3538             }
3539         }
3540     }
3541 
3542     if (format64) {
3543         /* Create a 64-bit PAR */
3544         par64 = (1 << 11); /* LPAE bit always set */
3545         if (!ret) {
3546             par64 |= res.f.phys_addr & ~0xfffULL;
3547             if (!res.f.attrs.secure) {
3548                 par64 |= (1 << 9); /* NS */
3549             }
3550             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3551             par64 |= par_el1_shareability(&res) << 7; /* SH */
3552         } else {
3553             uint32_t fsr = arm_fi_to_lfsc(&fi);
3554 
3555             par64 |= 1; /* F */
3556             par64 |= (fsr & 0x3f) << 1; /* FS */
3557             if (fi.stage2) {
3558                 par64 |= (1 << 9); /* S */
3559             }
3560             if (fi.s1ptw) {
3561                 par64 |= (1 << 8); /* PTW */
3562             }
3563         }
3564     } else {
3565         /*
3566          * fsr is a DFSR/IFSR value for the short descriptor
3567          * translation table format (with WnR always clear).
3568          * Convert it to a 32-bit PAR.
3569          */
3570         if (!ret) {
3571             /* We do not set any attribute bits in the PAR */
3572             if (res.f.lg_page_size == 24
3573                 && arm_feature(env, ARM_FEATURE_V7)) {
3574                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3575             } else {
3576                 par64 = res.f.phys_addr & 0xfffff000;
3577             }
3578             if (!res.f.attrs.secure) {
3579                 par64 |= (1 << 9); /* NS */
3580             }
3581         } else {
3582             uint32_t fsr = arm_fi_to_sfsc(&fi);
3583 
3584             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3585                     ((fsr & 0xf) << 1) | 1;
3586         }
3587     }
3588     return par64;
3589 }
3590 #endif /* CONFIG_TCG */
3591 
3592 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3593 {
3594 #ifdef CONFIG_TCG
3595     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3596     uint64_t par64;
3597     ARMMMUIdx mmu_idx;
3598     int el = arm_current_el(env);
3599     ARMSecuritySpace ss = arm_security_space(env);
3600 
3601     switch (ri->opc2 & 6) {
3602     case 0:
3603         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3604         switch (el) {
3605         case 3:
3606             mmu_idx = ARMMMUIdx_E3;
3607             break;
3608         case 2:
3609             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3610             /* fall through */
3611         case 1:
3612             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3613                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3614             } else {
3615                 mmu_idx = ARMMMUIdx_Stage1_E1;
3616             }
3617             break;
3618         default:
3619             g_assert_not_reached();
3620         }
3621         break;
3622     case 2:
3623         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3624         switch (el) {
3625         case 3:
3626             mmu_idx = ARMMMUIdx_E10_0;
3627             break;
3628         case 2:
3629             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3630             mmu_idx = ARMMMUIdx_Stage1_E0;
3631             break;
3632         case 1:
3633             mmu_idx = ARMMMUIdx_Stage1_E0;
3634             break;
3635         default:
3636             g_assert_not_reached();
3637         }
3638         break;
3639     case 4:
3640         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3641         mmu_idx = ARMMMUIdx_E10_1;
3642         ss = ARMSS_NonSecure;
3643         break;
3644     case 6:
3645         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3646         mmu_idx = ARMMMUIdx_E10_0;
3647         ss = ARMSS_NonSecure;
3648         break;
3649     default:
3650         g_assert_not_reached();
3651     }
3652 
3653     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3654 
3655     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3656 #else
3657     /* Handled by hardware accelerator. */
3658     g_assert_not_reached();
3659 #endif /* CONFIG_TCG */
3660 }
3661 
3662 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3663                         uint64_t value)
3664 {
3665 #ifdef CONFIG_TCG
3666     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3667     uint64_t par64;
3668 
3669     /* There is no SecureEL2 for AArch32. */
3670     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3671                          ARMSS_NonSecure);
3672 
3673     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3674 #else
3675     /* Handled by hardware accelerator. */
3676     g_assert_not_reached();
3677 #endif /* CONFIG_TCG */
3678 }
3679 
3680 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3681                                      bool isread)
3682 {
3683     /*
3684      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3685      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3686      * only happen when executing at EL3 because that combination also causes an
3687      * illegal exception return. We don't need to check FEAT_RME either, because
3688      * scr_write() ensures that the NSE bit is not set otherwise.
3689      */
3690     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3691         return CP_ACCESS_TRAP;
3692     }
3693     return CP_ACCESS_OK;
3694 }
3695 
3696 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3697                                      bool isread)
3698 {
3699     if (arm_current_el(env) == 3 &&
3700         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3701         return CP_ACCESS_TRAP;
3702     }
3703     return at_e012_access(env, ri, isread);
3704 }
3705 
3706 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3707                         uint64_t value)
3708 {
3709 #ifdef CONFIG_TCG
3710     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3711     ARMMMUIdx mmu_idx;
3712     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3713     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3714 
3715     switch (ri->opc2 & 6) {
3716     case 0:
3717         switch (ri->opc1) {
3718         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3719             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3720                 mmu_idx = regime_e20 ?
3721                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3722             } else {
3723                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3724             }
3725             break;
3726         case 4: /* AT S1E2R, AT S1E2W */
3727             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3728             break;
3729         case 6: /* AT S1E3R, AT S1E3W */
3730             mmu_idx = ARMMMUIdx_E3;
3731             break;
3732         default:
3733             g_assert_not_reached();
3734         }
3735         break;
3736     case 2: /* AT S1E0R, AT S1E0W */
3737         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3738         break;
3739     case 4: /* AT S12E1R, AT S12E1W */
3740         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3741         break;
3742     case 6: /* AT S12E0R, AT S12E0W */
3743         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3744         break;
3745     default:
3746         g_assert_not_reached();
3747     }
3748 
3749     env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3750                                        mmu_idx, arm_security_space(env));
3751 #else
3752     /* Handled by hardware accelerator. */
3753     g_assert_not_reached();
3754 #endif /* CONFIG_TCG */
3755 }
3756 #endif
3757 
3758 /* Return basic MPU access permission bits.  */
3759 static uint32_t simple_mpu_ap_bits(uint32_t val)
3760 {
3761     uint32_t ret;
3762     uint32_t mask;
3763     int i;
3764     ret = 0;
3765     mask = 3;
3766     for (i = 0; i < 16; i += 2) {
3767         ret |= (val >> i) & mask;
3768         mask <<= 2;
3769     }
3770     return ret;
3771 }
3772 
3773 /* Pad basic MPU access permission bits to extended format.  */
3774 static uint32_t extended_mpu_ap_bits(uint32_t val)
3775 {
3776     uint32_t ret;
3777     uint32_t mask;
3778     int i;
3779     ret = 0;
3780     mask = 3;
3781     for (i = 0; i < 16; i += 2) {
3782         ret |= (val & mask) << i;
3783         mask <<= 2;
3784     }
3785     return ret;
3786 }
3787 
3788 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3789                                  uint64_t value)
3790 {
3791     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3792 }
3793 
3794 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3795 {
3796     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3797 }
3798 
3799 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3800                                  uint64_t value)
3801 {
3802     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3803 }
3804 
3805 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3806 {
3807     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3808 }
3809 
3810 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3811 {
3812     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3813 
3814     if (!u32p) {
3815         return 0;
3816     }
3817 
3818     u32p += env->pmsav7.rnr[M_REG_NS];
3819     return *u32p;
3820 }
3821 
3822 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3823                          uint64_t value)
3824 {
3825     ARMCPU *cpu = env_archcpu(env);
3826     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3827 
3828     if (!u32p) {
3829         return;
3830     }
3831 
3832     u32p += env->pmsav7.rnr[M_REG_NS];
3833     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3834     *u32p = value;
3835 }
3836 
3837 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3838                               uint64_t value)
3839 {
3840     ARMCPU *cpu = env_archcpu(env);
3841     uint32_t nrgs = cpu->pmsav7_dregion;
3842 
3843     if (value >= nrgs) {
3844         qemu_log_mask(LOG_GUEST_ERROR,
3845                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3846                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3847         return;
3848     }
3849 
3850     raw_write(env, ri, value);
3851 }
3852 
3853 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3854                           uint64_t value)
3855 {
3856     ARMCPU *cpu = env_archcpu(env);
3857 
3858     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3859     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3860 }
3861 
3862 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3863 {
3864     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3865 }
3866 
3867 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3868                           uint64_t value)
3869 {
3870     ARMCPU *cpu = env_archcpu(env);
3871 
3872     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3873     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3874 }
3875 
3876 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3877 {
3878     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3879 }
3880 
3881 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3882                            uint64_t value)
3883 {
3884     ARMCPU *cpu = env_archcpu(env);
3885 
3886     /*
3887      * Ignore writes that would select not implemented region.
3888      * This is architecturally UNPREDICTABLE.
3889      */
3890     if (value >= cpu->pmsav7_dregion) {
3891         return;
3892     }
3893 
3894     env->pmsav7.rnr[M_REG_NS] = value;
3895 }
3896 
3897 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3898                           uint64_t value)
3899 {
3900     ARMCPU *cpu = env_archcpu(env);
3901 
3902     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3903     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3904 }
3905 
3906 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3907 {
3908     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3909 }
3910 
3911 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3912                           uint64_t value)
3913 {
3914     ARMCPU *cpu = env_archcpu(env);
3915 
3916     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3917     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3918 }
3919 
3920 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3921 {
3922     return env->pmsav8.hprlar[env->pmsav8.hprselr];
3923 }
3924 
3925 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3926                           uint64_t value)
3927 {
3928     uint32_t n;
3929     uint32_t bit;
3930     ARMCPU *cpu = env_archcpu(env);
3931 
3932     /* Ignore writes to unimplemented regions */
3933     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3934     value &= MAKE_64BIT_MASK(0, rmax);
3935 
3936     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3937 
3938     /* Register alias is only valid for first 32 indexes */
3939     for (n = 0; n < rmax; ++n) {
3940         bit = extract32(value, n, 1);
3941         env->pmsav8.hprlar[n] = deposit32(
3942                     env->pmsav8.hprlar[n], 0, 1, bit);
3943     }
3944 }
3945 
3946 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3947 {
3948     uint32_t n;
3949     uint32_t result = 0x0;
3950     ARMCPU *cpu = env_archcpu(env);
3951 
3952     /* Register alias is only valid for first 32 indexes */
3953     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3954         if (env->pmsav8.hprlar[n] & 0x1) {
3955             result |= (0x1 << n);
3956         }
3957     }
3958     return result;
3959 }
3960 
3961 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3962                            uint64_t value)
3963 {
3964     ARMCPU *cpu = env_archcpu(env);
3965 
3966     /*
3967      * Ignore writes that would select not implemented region.
3968      * This is architecturally UNPREDICTABLE.
3969      */
3970     if (value >= cpu->pmsav8r_hdregion) {
3971         return;
3972     }
3973 
3974     env->pmsav8.hprselr = value;
3975 }
3976 
3977 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
3978                           uint64_t value)
3979 {
3980     ARMCPU *cpu = env_archcpu(env);
3981     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3982                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3983 
3984     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3985 
3986     if (ri->opc1 & 4) {
3987         if (index >= cpu->pmsav8r_hdregion) {
3988             return;
3989         }
3990         if (ri->opc2 & 0x1) {
3991             env->pmsav8.hprlar[index] = value;
3992         } else {
3993             env->pmsav8.hprbar[index] = value;
3994         }
3995     } else {
3996         if (index >= cpu->pmsav7_dregion) {
3997             return;
3998         }
3999         if (ri->opc2 & 0x1) {
4000             env->pmsav8.rlar[M_REG_NS][index] = value;
4001         } else {
4002             env->pmsav8.rbar[M_REG_NS][index] = value;
4003         }
4004     }
4005 }
4006 
4007 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4008 {
4009     ARMCPU *cpu = env_archcpu(env);
4010     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4011                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4012 
4013     if (ri->opc1 & 4) {
4014         if (index >= cpu->pmsav8r_hdregion) {
4015             return 0x0;
4016         }
4017         if (ri->opc2 & 0x1) {
4018             return env->pmsav8.hprlar[index];
4019         } else {
4020             return env->pmsav8.hprbar[index];
4021         }
4022     } else {
4023         if (index >= cpu->pmsav7_dregion) {
4024             return 0x0;
4025         }
4026         if (ri->opc2 & 0x1) {
4027             return env->pmsav8.rlar[M_REG_NS][index];
4028         } else {
4029             return env->pmsav8.rbar[M_REG_NS][index];
4030         }
4031     }
4032 }
4033 
4034 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4035     { .name = "PRBAR",
4036       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4037       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4038       .accessfn = access_tvm_trvm,
4039       .readfn = prbar_read, .writefn = prbar_write },
4040     { .name = "PRLAR",
4041       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4042       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4043       .accessfn = access_tvm_trvm,
4044       .readfn = prlar_read, .writefn = prlar_write },
4045     { .name = "PRSELR", .resetvalue = 0,
4046       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4047       .access = PL1_RW, .accessfn = access_tvm_trvm,
4048       .writefn = prselr_write,
4049       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4050     { .name = "HPRBAR", .resetvalue = 0,
4051       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4052       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4053       .readfn = hprbar_read, .writefn = hprbar_write },
4054     { .name = "HPRLAR",
4055       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4056       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4057       .readfn = hprlar_read, .writefn = hprlar_write },
4058     { .name = "HPRSELR", .resetvalue = 0,
4059       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4060       .access = PL2_RW,
4061       .writefn = hprselr_write,
4062       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4063     { .name = "HPRENR",
4064       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4065       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4066       .readfn = hprenr_read, .writefn = hprenr_write },
4067 };
4068 
4069 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4070     /*
4071      * Reset for all these registers is handled in arm_cpu_reset(),
4072      * because the PMSAv7 is also used by M-profile CPUs, which do
4073      * not register cpregs but still need the state to be reset.
4074      */
4075     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4076       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4077       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4078       .readfn = pmsav7_read, .writefn = pmsav7_write,
4079       .resetfn = arm_cp_reset_ignore },
4080     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4081       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4082       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4083       .readfn = pmsav7_read, .writefn = pmsav7_write,
4084       .resetfn = arm_cp_reset_ignore },
4085     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4086       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4087       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4088       .readfn = pmsav7_read, .writefn = pmsav7_write,
4089       .resetfn = arm_cp_reset_ignore },
4090     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4091       .access = PL1_RW,
4092       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4093       .writefn = pmsav7_rgnr_write,
4094       .resetfn = arm_cp_reset_ignore },
4095 };
4096 
4097 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4098     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4099       .access = PL1_RW, .type = ARM_CP_ALIAS,
4100       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4101       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4102     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4103       .access = PL1_RW, .type = ARM_CP_ALIAS,
4104       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4105       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4106     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4107       .access = PL1_RW,
4108       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4109       .resetvalue = 0, },
4110     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4111       .access = PL1_RW,
4112       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4113       .resetvalue = 0, },
4114     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4115       .access = PL1_RW,
4116       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4117     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4118       .access = PL1_RW,
4119       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4120     /* Protection region base and size registers */
4121     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4122       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4123       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4124     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4125       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4126       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4127     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4128       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4129       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4130     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4131       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4132       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4133     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4134       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4135       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4136     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4137       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4138       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4139     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4140       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4141       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4142     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4143       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4144       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4145 };
4146 
4147 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4148                              uint64_t value)
4149 {
4150     ARMCPU *cpu = env_archcpu(env);
4151 
4152     if (!arm_feature(env, ARM_FEATURE_V8)) {
4153         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4154             /*
4155              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4156              * using Long-descriptor translation table format
4157              */
4158             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4159         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4160             /*
4161              * In an implementation that includes the Security Extensions
4162              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4163              * Short-descriptor translation table format.
4164              */
4165             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4166         } else {
4167             value &= TTBCR_N;
4168         }
4169     }
4170 
4171     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4172         /*
4173          * With LPAE the TTBCR could result in a change of ASID
4174          * via the TTBCR.A1 bit, so do a TLB flush.
4175          */
4176         tlb_flush(CPU(cpu));
4177     }
4178     raw_write(env, ri, value);
4179 }
4180 
4181 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4182                                uint64_t value)
4183 {
4184     ARMCPU *cpu = env_archcpu(env);
4185 
4186     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4187     tlb_flush(CPU(cpu));
4188     raw_write(env, ri, value);
4189 }
4190 
4191 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4192                             uint64_t value)
4193 {
4194     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4195     if (cpreg_field_is_64bit(ri) &&
4196         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4197         ARMCPU *cpu = env_archcpu(env);
4198         tlb_flush(CPU(cpu));
4199     }
4200     raw_write(env, ri, value);
4201 }
4202 
4203 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4204                                     uint64_t value)
4205 {
4206     /*
4207      * If we are running with E2&0 regime, then an ASID is active.
4208      * Flush if that might be changing.  Note we're not checking
4209      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4210      * holds the active ASID, only checking the field that might.
4211      */
4212     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4213         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4214         uint16_t mask = ARMMMUIdxBit_E20_2 |
4215                         ARMMMUIdxBit_E20_2_PAN |
4216                         ARMMMUIdxBit_E20_0;
4217         tlb_flush_by_mmuidx(env_cpu(env), mask);
4218     }
4219     raw_write(env, ri, value);
4220 }
4221 
4222 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4223                         uint64_t value)
4224 {
4225     ARMCPU *cpu = env_archcpu(env);
4226     CPUState *cs = CPU(cpu);
4227 
4228     /*
4229      * A change in VMID to the stage2 page table (Stage2) invalidates
4230      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4231      */
4232     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4233         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4234     }
4235     raw_write(env, ri, value);
4236 }
4237 
4238 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4239     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4240       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4241       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4242                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4243     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4244       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4245       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4246                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4247     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4248       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4249       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4250                              offsetof(CPUARMState, cp15.dfar_ns) } },
4251     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4252       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4253       .access = PL1_RW, .accessfn = access_tvm_trvm,
4254       .fgt = FGT_FAR_EL1,
4255       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4256       .resetvalue = 0, },
4257 };
4258 
4259 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4260     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4261       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4262       .access = PL1_RW, .accessfn = access_tvm_trvm,
4263       .fgt = FGT_ESR_EL1,
4264       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4265     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4266       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4267       .access = PL1_RW, .accessfn = access_tvm_trvm,
4268       .fgt = FGT_TTBR0_EL1,
4269       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4270       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4271                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4272     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4273       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4274       .access = PL1_RW, .accessfn = access_tvm_trvm,
4275       .fgt = FGT_TTBR1_EL1,
4276       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4277       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4278                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4279     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4280       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4281       .access = PL1_RW, .accessfn = access_tvm_trvm,
4282       .fgt = FGT_TCR_EL1,
4283       .writefn = vmsa_tcr_el12_write,
4284       .raw_writefn = raw_write,
4285       .resetvalue = 0,
4286       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4287     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4288       .access = PL1_RW, .accessfn = access_tvm_trvm,
4289       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4290       .raw_writefn = raw_write,
4291       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4292                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4293 };
4294 
4295 /*
4296  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4297  * qemu tlbs nor adjusting cached masks.
4298  */
4299 static const ARMCPRegInfo ttbcr2_reginfo = {
4300     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4301     .access = PL1_RW, .accessfn = access_tvm_trvm,
4302     .type = ARM_CP_ALIAS,
4303     .bank_fieldoffsets = {
4304         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4305         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4306     },
4307 };
4308 
4309 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4310                                 uint64_t value)
4311 {
4312     env->cp15.c15_ticonfig = value & 0xe7;
4313     /* The OS_TYPE bit in this register changes the reported CPUID! */
4314     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4315         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4316 }
4317 
4318 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4319                                 uint64_t value)
4320 {
4321     env->cp15.c15_threadid = value & 0xffff;
4322 }
4323 
4324 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4325                            uint64_t value)
4326 {
4327     /* Wait-for-interrupt (deprecated) */
4328     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4329 }
4330 
4331 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4332                                   uint64_t value)
4333 {
4334     /*
4335      * On OMAP there are registers indicating the max/min index of dcache lines
4336      * containing a dirty line; cache flush operations have to reset these.
4337      */
4338     env->cp15.c15_i_max = 0x000;
4339     env->cp15.c15_i_min = 0xff0;
4340 }
4341 
4342 static const ARMCPRegInfo omap_cp_reginfo[] = {
4343     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4344       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4345       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4346       .resetvalue = 0, },
4347     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4348       .access = PL1_RW, .type = ARM_CP_NOP },
4349     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4350       .access = PL1_RW,
4351       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4352       .writefn = omap_ticonfig_write },
4353     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4354       .access = PL1_RW,
4355       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4356     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4357       .access = PL1_RW, .resetvalue = 0xff0,
4358       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4359     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4360       .access = PL1_RW,
4361       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4362       .writefn = omap_threadid_write },
4363     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4364       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4365       .type = ARM_CP_NO_RAW,
4366       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4367     /*
4368      * TODO: Peripheral port remap register:
4369      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4370      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4371      * when MMU is off.
4372      */
4373     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4374       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4375       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4376       .writefn = omap_cachemaint_write },
4377     { .name = "C9", .cp = 15, .crn = 9,
4378       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4379       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4380 };
4381 
4382 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4383                               uint64_t value)
4384 {
4385     env->cp15.c15_cpar = value & 0x3fff;
4386 }
4387 
4388 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4389     { .name = "XSCALE_CPAR",
4390       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4391       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4392       .writefn = xscale_cpar_write, },
4393     { .name = "XSCALE_AUXCR",
4394       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4395       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4396       .resetvalue = 0, },
4397     /*
4398      * XScale specific cache-lockdown: since we have no cache we NOP these
4399      * and hope the guest does not really rely on cache behaviour.
4400      */
4401     { .name = "XSCALE_LOCK_ICACHE_LINE",
4402       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4403       .access = PL1_W, .type = ARM_CP_NOP },
4404     { .name = "XSCALE_UNLOCK_ICACHE",
4405       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4406       .access = PL1_W, .type = ARM_CP_NOP },
4407     { .name = "XSCALE_DCACHE_LOCK",
4408       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4409       .access = PL1_RW, .type = ARM_CP_NOP },
4410     { .name = "XSCALE_UNLOCK_DCACHE",
4411       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4412       .access = PL1_W, .type = ARM_CP_NOP },
4413 };
4414 
4415 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4416     /*
4417      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4418      * implementation of this implementation-defined space.
4419      * Ideally this should eventually disappear in favour of actually
4420      * implementing the correct behaviour for all cores.
4421      */
4422     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4423       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4424       .access = PL1_RW,
4425       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4426       .resetvalue = 0 },
4427 };
4428 
4429 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4430     /* Cache status: RAZ because we have no cache so it's always clean */
4431     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4432       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4433       .resetvalue = 0 },
4434 };
4435 
4436 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4437     /* We never have a block transfer operation in progress */
4438     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4439       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4440       .resetvalue = 0 },
4441     /* The cache ops themselves: these all NOP for QEMU */
4442     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4443       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4444     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4445       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4446     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4447       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4448     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4449       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4450     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4451       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4452     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4453       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4454 };
4455 
4456 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4457     /*
4458      * The cache test-and-clean instructions always return (1 << 30)
4459      * to indicate that there are no dirty cache lines.
4460      */
4461     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4462       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4463       .resetvalue = (1 << 30) },
4464     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4465       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4466       .resetvalue = (1 << 30) },
4467 };
4468 
4469 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4470     /* Ignore ReadBuffer accesses */
4471     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4472       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4473       .access = PL1_RW, .resetvalue = 0,
4474       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4475 };
4476 
4477 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4478 {
4479     unsigned int cur_el = arm_current_el(env);
4480 
4481     if (arm_is_el2_enabled(env) && cur_el == 1) {
4482         return env->cp15.vpidr_el2;
4483     }
4484     return raw_read(env, ri);
4485 }
4486 
4487 static uint64_t mpidr_read_val(CPUARMState *env)
4488 {
4489     ARMCPU *cpu = env_archcpu(env);
4490     uint64_t mpidr = cpu->mp_affinity;
4491 
4492     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4493         mpidr |= (1U << 31);
4494         /*
4495          * Cores which are uniprocessor (non-coherent)
4496          * but still implement the MP extensions set
4497          * bit 30. (For instance, Cortex-R5).
4498          */
4499         if (cpu->mp_is_up) {
4500             mpidr |= (1u << 30);
4501         }
4502     }
4503     return mpidr;
4504 }
4505 
4506 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4507 {
4508     unsigned int cur_el = arm_current_el(env);
4509 
4510     if (arm_is_el2_enabled(env) && cur_el == 1) {
4511         return env->cp15.vmpidr_el2;
4512     }
4513     return mpidr_read_val(env);
4514 }
4515 
4516 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4517     /* NOP AMAIR0/1 */
4518     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4519       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4520       .access = PL1_RW, .accessfn = access_tvm_trvm,
4521       .fgt = FGT_AMAIR_EL1,
4522       .type = ARM_CP_CONST, .resetvalue = 0 },
4523     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4524     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4525       .access = PL1_RW, .accessfn = access_tvm_trvm,
4526       .type = ARM_CP_CONST, .resetvalue = 0 },
4527     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4528       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4529       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4530                              offsetof(CPUARMState, cp15.par_ns)} },
4531     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4532       .access = PL1_RW, .accessfn = access_tvm_trvm,
4533       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4534       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4535                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4536       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4537     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4538       .access = PL1_RW, .accessfn = access_tvm_trvm,
4539       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4540       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4541                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4542       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4543 };
4544 
4545 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4546 {
4547     return vfp_get_fpcr(env);
4548 }
4549 
4550 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4551                             uint64_t value)
4552 {
4553     vfp_set_fpcr(env, value);
4554 }
4555 
4556 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4557 {
4558     return vfp_get_fpsr(env);
4559 }
4560 
4561 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4562                             uint64_t value)
4563 {
4564     vfp_set_fpsr(env, value);
4565 }
4566 
4567 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4568                                        bool isread)
4569 {
4570     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4571         return CP_ACCESS_TRAP;
4572     }
4573     return CP_ACCESS_OK;
4574 }
4575 
4576 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4577                             uint64_t value)
4578 {
4579     env->daif = value & PSTATE_DAIF;
4580 }
4581 
4582 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4583 {
4584     return env->pstate & PSTATE_PAN;
4585 }
4586 
4587 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4588                            uint64_t value)
4589 {
4590     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4591 }
4592 
4593 static const ARMCPRegInfo pan_reginfo = {
4594     .name = "PAN", .state = ARM_CP_STATE_AA64,
4595     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4596     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4597     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4598 };
4599 
4600 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4601 {
4602     return env->pstate & PSTATE_UAO;
4603 }
4604 
4605 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4606                            uint64_t value)
4607 {
4608     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4609 }
4610 
4611 static const ARMCPRegInfo uao_reginfo = {
4612     .name = "UAO", .state = ARM_CP_STATE_AA64,
4613     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4614     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4615     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4616 };
4617 
4618 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4619 {
4620     return env->pstate & PSTATE_DIT;
4621 }
4622 
4623 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4624                            uint64_t value)
4625 {
4626     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4627 }
4628 
4629 static const ARMCPRegInfo dit_reginfo = {
4630     .name = "DIT", .state = ARM_CP_STATE_AA64,
4631     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4632     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4633     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4634 };
4635 
4636 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4637 {
4638     return env->pstate & PSTATE_SSBS;
4639 }
4640 
4641 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4642                            uint64_t value)
4643 {
4644     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4645 }
4646 
4647 static const ARMCPRegInfo ssbs_reginfo = {
4648     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4649     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4650     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4651     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4652 };
4653 
4654 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4655                                               const ARMCPRegInfo *ri,
4656                                               bool isread)
4657 {
4658     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4659     switch (arm_current_el(env)) {
4660     case 0:
4661         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4662         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4663             return CP_ACCESS_TRAP;
4664         }
4665         /* fall through */
4666     case 1:
4667         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4668         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4669             return CP_ACCESS_TRAP_EL2;
4670         }
4671         break;
4672     }
4673     return CP_ACCESS_OK;
4674 }
4675 
4676 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4677 {
4678     /* Cache invalidate/clean to Point of Unification... */
4679     switch (arm_current_el(env)) {
4680     case 0:
4681         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4682         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4683             return CP_ACCESS_TRAP;
4684         }
4685         /* fall through */
4686     case 1:
4687         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4688         if (arm_hcr_el2_eff(env) & hcrflags) {
4689             return CP_ACCESS_TRAP_EL2;
4690         }
4691         break;
4692     }
4693     return CP_ACCESS_OK;
4694 }
4695 
4696 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4697                                    bool isread)
4698 {
4699     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4700 }
4701 
4702 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4703                                   bool isread)
4704 {
4705     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4706 }
4707 
4708 /*
4709  * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4710  * Page D4-1736 (DDI0487A.b)
4711  */
4712 
4713 static int vae1_tlbmask(CPUARMState *env)
4714 {
4715     uint64_t hcr = arm_hcr_el2_eff(env);
4716     uint16_t mask;
4717 
4718     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4719         mask = ARMMMUIdxBit_E20_2 |
4720                ARMMMUIdxBit_E20_2_PAN |
4721                ARMMMUIdxBit_E20_0;
4722     } else {
4723         mask = ARMMMUIdxBit_E10_1 |
4724                ARMMMUIdxBit_E10_1_PAN |
4725                ARMMMUIdxBit_E10_0;
4726     }
4727     return mask;
4728 }
4729 
4730 static int vae2_tlbmask(CPUARMState *env)
4731 {
4732     uint64_t hcr = arm_hcr_el2_eff(env);
4733     uint16_t mask;
4734 
4735     if (hcr & HCR_E2H) {
4736         mask = ARMMMUIdxBit_E20_2 |
4737                ARMMMUIdxBit_E20_2_PAN |
4738                ARMMMUIdxBit_E20_0;
4739     } else {
4740         mask = ARMMMUIdxBit_E2;
4741     }
4742     return mask;
4743 }
4744 
4745 /* Return 56 if TBI is enabled, 64 otherwise. */
4746 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4747                               uint64_t addr)
4748 {
4749     uint64_t tcr = regime_tcr(env, mmu_idx);
4750     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4751     int select = extract64(addr, 55, 1);
4752 
4753     return (tbi >> select) & 1 ? 56 : 64;
4754 }
4755 
4756 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4757 {
4758     uint64_t hcr = arm_hcr_el2_eff(env);
4759     ARMMMUIdx mmu_idx;
4760 
4761     /* Only the regime of the mmu_idx below is significant. */
4762     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4763         mmu_idx = ARMMMUIdx_E20_0;
4764     } else {
4765         mmu_idx = ARMMMUIdx_E10_0;
4766     }
4767 
4768     return tlbbits_for_regime(env, mmu_idx, addr);
4769 }
4770 
4771 static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
4772 {
4773     uint64_t hcr = arm_hcr_el2_eff(env);
4774     ARMMMUIdx mmu_idx;
4775 
4776     /*
4777      * Only the regime of the mmu_idx below is significant.
4778      * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
4779      * only has one.
4780      */
4781     if (hcr & HCR_E2H) {
4782         mmu_idx = ARMMMUIdx_E20_2;
4783     } else {
4784         mmu_idx = ARMMMUIdx_E2;
4785     }
4786 
4787     return tlbbits_for_regime(env, mmu_idx, addr);
4788 }
4789 
4790 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4791                                       uint64_t value)
4792 {
4793     CPUState *cs = env_cpu(env);
4794     int mask = vae1_tlbmask(env);
4795 
4796     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4797 }
4798 
4799 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4800                                     uint64_t value)
4801 {
4802     CPUState *cs = env_cpu(env);
4803     int mask = vae1_tlbmask(env);
4804 
4805     if (tlb_force_broadcast(env)) {
4806         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4807     } else {
4808         tlb_flush_by_mmuidx(cs, mask);
4809     }
4810 }
4811 
4812 static int e2_tlbmask(CPUARMState *env)
4813 {
4814     return (ARMMMUIdxBit_E20_0 |
4815             ARMMMUIdxBit_E20_2 |
4816             ARMMMUIdxBit_E20_2_PAN |
4817             ARMMMUIdxBit_E2);
4818 }
4819 
4820 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4821                                   uint64_t value)
4822 {
4823     CPUState *cs = env_cpu(env);
4824     int mask = alle1_tlbmask(env);
4825 
4826     tlb_flush_by_mmuidx(cs, mask);
4827 }
4828 
4829 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4830                                   uint64_t value)
4831 {
4832     CPUState *cs = env_cpu(env);
4833     int mask = e2_tlbmask(env);
4834 
4835     tlb_flush_by_mmuidx(cs, mask);
4836 }
4837 
4838 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4839                                   uint64_t value)
4840 {
4841     ARMCPU *cpu = env_archcpu(env);
4842     CPUState *cs = CPU(cpu);
4843 
4844     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4845 }
4846 
4847 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4848                                     uint64_t value)
4849 {
4850     CPUState *cs = env_cpu(env);
4851     int mask = alle1_tlbmask(env);
4852 
4853     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4854 }
4855 
4856 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4857                                     uint64_t value)
4858 {
4859     CPUState *cs = env_cpu(env);
4860     int mask = e2_tlbmask(env);
4861 
4862     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4863 }
4864 
4865 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4866                                     uint64_t value)
4867 {
4868     CPUState *cs = env_cpu(env);
4869 
4870     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4871 }
4872 
4873 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4874                                  uint64_t value)
4875 {
4876     /*
4877      * Invalidate by VA, EL2
4878      * Currently handles both VAE2 and VALE2, since we don't support
4879      * flush-last-level-only.
4880      */
4881     CPUState *cs = env_cpu(env);
4882     int mask = vae2_tlbmask(env);
4883     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4884     int bits = vae2_tlbbits(env, pageaddr);
4885 
4886     tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4887 }
4888 
4889 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4890                                  uint64_t value)
4891 {
4892     /*
4893      * Invalidate by VA, EL3
4894      * Currently handles both VAE3 and VALE3, since we don't support
4895      * flush-last-level-only.
4896      */
4897     ARMCPU *cpu = env_archcpu(env);
4898     CPUState *cs = CPU(cpu);
4899     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4900 
4901     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4902 }
4903 
4904 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4905                                    uint64_t value)
4906 {
4907     CPUState *cs = env_cpu(env);
4908     int mask = vae1_tlbmask(env);
4909     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4910     int bits = vae1_tlbbits(env, pageaddr);
4911 
4912     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4913 }
4914 
4915 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4916                                  uint64_t value)
4917 {
4918     /*
4919      * Invalidate by VA, EL1&0 (AArch64 version).
4920      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4921      * since we don't support flush-for-specific-ASID-only or
4922      * flush-last-level-only.
4923      */
4924     CPUState *cs = env_cpu(env);
4925     int mask = vae1_tlbmask(env);
4926     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4927     int bits = vae1_tlbbits(env, pageaddr);
4928 
4929     if (tlb_force_broadcast(env)) {
4930         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4931     } else {
4932         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4933     }
4934 }
4935 
4936 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4937                                    uint64_t value)
4938 {
4939     CPUState *cs = env_cpu(env);
4940     int mask = vae2_tlbmask(env);
4941     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4942     int bits = vae2_tlbbits(env, pageaddr);
4943 
4944     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4945 }
4946 
4947 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4948                                    uint64_t value)
4949 {
4950     CPUState *cs = env_cpu(env);
4951     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4952     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
4953 
4954     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4955                                                   ARMMMUIdxBit_E3, bits);
4956 }
4957 
4958 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4959 {
4960     /*
4961      * The MSB of value is the NS field, which only applies if SEL2
4962      * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4963      */
4964     return (value >= 0
4965             && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4966             && arm_is_secure_below_el3(env)
4967             ? ARMMMUIdxBit_Stage2_S
4968             : ARMMMUIdxBit_Stage2);
4969 }
4970 
4971 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4972                                     uint64_t value)
4973 {
4974     CPUState *cs = env_cpu(env);
4975     int mask = ipas2e1_tlbmask(env, value);
4976     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4977 
4978     if (tlb_force_broadcast(env)) {
4979         tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4980     } else {
4981         tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4982     }
4983 }
4984 
4985 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4986                                       uint64_t value)
4987 {
4988     CPUState *cs = env_cpu(env);
4989     int mask = ipas2e1_tlbmask(env, value);
4990     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4991 
4992     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4993 }
4994 
4995 #ifdef TARGET_AARCH64
4996 typedef struct {
4997     uint64_t base;
4998     uint64_t length;
4999 } TLBIRange;
5000 
5001 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
5002 {
5003     /*
5004      * Note that the TLBI range TG field encoding differs from both
5005      * TG0 and TG1 encodings.
5006      */
5007     switch (tg) {
5008     case 1:
5009         return Gran4K;
5010     case 2:
5011         return Gran16K;
5012     case 3:
5013         return Gran64K;
5014     default:
5015         return GranInvalid;
5016     }
5017 }
5018 
5019 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
5020                                      uint64_t value)
5021 {
5022     unsigned int page_size_granule, page_shift, num, scale, exponent;
5023     /* Extract one bit to represent the va selector in use. */
5024     uint64_t select = sextract64(value, 36, 1);
5025     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
5026     TLBIRange ret = { };
5027     ARMGranuleSize gran;
5028 
5029     page_size_granule = extract64(value, 46, 2);
5030     gran = tlbi_range_tg_to_gran_size(page_size_granule);
5031 
5032     /* The granule encoded in value must match the granule in use. */
5033     if (gran != param.gran) {
5034         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
5035                       page_size_granule);
5036         return ret;
5037     }
5038 
5039     page_shift = arm_granule_bits(gran);
5040     num = extract64(value, 39, 5);
5041     scale = extract64(value, 44, 2);
5042     exponent = (5 * scale) + 1;
5043 
5044     ret.length = (num + 1) << (exponent + page_shift);
5045 
5046     if (param.select) {
5047         ret.base = sextract64(value, 0, 37);
5048     } else {
5049         ret.base = extract64(value, 0, 37);
5050     }
5051     if (param.ds) {
5052         /*
5053          * With DS=1, BaseADDR is always shifted 16 so that it is able
5054          * to address all 52 va bits.  The input address is perforce
5055          * aligned on a 64k boundary regardless of translation granule.
5056          */
5057         page_shift = 16;
5058     }
5059     ret.base <<= page_shift;
5060 
5061     return ret;
5062 }
5063 
5064 static void do_rvae_write(CPUARMState *env, uint64_t value,
5065                           int idxmap, bool synced)
5066 {
5067     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
5068     TLBIRange range;
5069     int bits;
5070 
5071     range = tlbi_aa64_get_range(env, one_idx, value);
5072     bits = tlbbits_for_regime(env, one_idx, range.base);
5073 
5074     if (synced) {
5075         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
5076                                                   range.base,
5077                                                   range.length,
5078                                                   idxmap,
5079                                                   bits);
5080     } else {
5081         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5082                                   range.length, idxmap, bits);
5083     }
5084 }
5085 
5086 static void tlbi_aa64_rvae1_write(CPUARMState *env,
5087                                   const ARMCPRegInfo *ri,
5088                                   uint64_t value)
5089 {
5090     /*
5091      * Invalidate by VA range, EL1&0.
5092      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5093      * since we don't support flush-for-specific-ASID-only or
5094      * flush-last-level-only.
5095      */
5096 
5097     do_rvae_write(env, value, vae1_tlbmask(env),
5098                   tlb_force_broadcast(env));
5099 }
5100 
5101 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5102                                     const ARMCPRegInfo *ri,
5103                                     uint64_t value)
5104 {
5105     /*
5106      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5107      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5108      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5109      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5110      * shareable specific flushes.
5111      */
5112 
5113     do_rvae_write(env, value, vae1_tlbmask(env), true);
5114 }
5115 
5116 static void tlbi_aa64_rvae2_write(CPUARMState *env,
5117                                   const ARMCPRegInfo *ri,
5118                                   uint64_t value)
5119 {
5120     /*
5121      * Invalidate by VA range, EL2.
5122      * Currently handles all of RVAE2 and RVALE2,
5123      * since we don't support flush-for-specific-ASID-only or
5124      * flush-last-level-only.
5125      */
5126 
5127     do_rvae_write(env, value, vae2_tlbmask(env),
5128                   tlb_force_broadcast(env));
5129 
5130 
5131 }
5132 
5133 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5134                                     const ARMCPRegInfo *ri,
5135                                     uint64_t value)
5136 {
5137     /*
5138      * Invalidate by VA range, Inner/Outer Shareable, EL2.
5139      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5140      * since we don't support flush-for-specific-ASID-only,
5141      * flush-last-level-only or inner/outer shareable specific flushes.
5142      */
5143 
5144     do_rvae_write(env, value, vae2_tlbmask(env), true);
5145 
5146 }
5147 
5148 static void tlbi_aa64_rvae3_write(CPUARMState *env,
5149                                   const ARMCPRegInfo *ri,
5150                                   uint64_t value)
5151 {
5152     /*
5153      * Invalidate by VA range, EL3.
5154      * Currently handles all of RVAE3 and RVALE3,
5155      * since we don't support flush-for-specific-ASID-only or
5156      * flush-last-level-only.
5157      */
5158 
5159     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5160 }
5161 
5162 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5163                                     const ARMCPRegInfo *ri,
5164                                     uint64_t value)
5165 {
5166     /*
5167      * Invalidate by VA range, EL3, Inner/Outer Shareable.
5168      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5169      * since we don't support flush-for-specific-ASID-only,
5170      * flush-last-level-only or inner/outer specific flushes.
5171      */
5172 
5173     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5174 }
5175 
5176 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5177                                      uint64_t value)
5178 {
5179     do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5180                   tlb_force_broadcast(env));
5181 }
5182 
5183 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5184                                        const ARMCPRegInfo *ri,
5185                                        uint64_t value)
5186 {
5187     do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5188 }
5189 #endif
5190 
5191 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5192                                       bool isread)
5193 {
5194     int cur_el = arm_current_el(env);
5195 
5196     if (cur_el < 2) {
5197         uint64_t hcr = arm_hcr_el2_eff(env);
5198 
5199         if (cur_el == 0) {
5200             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5201                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5202                     return CP_ACCESS_TRAP_EL2;
5203                 }
5204             } else {
5205                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5206                     return CP_ACCESS_TRAP;
5207                 }
5208                 if (hcr & HCR_TDZ) {
5209                     return CP_ACCESS_TRAP_EL2;
5210                 }
5211             }
5212         } else if (hcr & HCR_TDZ) {
5213             return CP_ACCESS_TRAP_EL2;
5214         }
5215     }
5216     return CP_ACCESS_OK;
5217 }
5218 
5219 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5220 {
5221     ARMCPU *cpu = env_archcpu(env);
5222     int dzp_bit = 1 << 4;
5223 
5224     /* DZP indicates whether DC ZVA access is allowed */
5225     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5226         dzp_bit = 0;
5227     }
5228     return cpu->dcz_blocksize | dzp_bit;
5229 }
5230 
5231 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5232                                     bool isread)
5233 {
5234     if (!(env->pstate & PSTATE_SP)) {
5235         /*
5236          * Access to SP_EL0 is undefined if it's being used as
5237          * the stack pointer.
5238          */
5239         return CP_ACCESS_TRAP_UNCATEGORIZED;
5240     }
5241     return CP_ACCESS_OK;
5242 }
5243 
5244 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5245 {
5246     return env->pstate & PSTATE_SP;
5247 }
5248 
5249 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5250 {
5251     update_spsel(env, val);
5252 }
5253 
5254 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5255                         uint64_t value)
5256 {
5257     ARMCPU *cpu = env_archcpu(env);
5258 
5259     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5260         /* M bit is RAZ/WI for PMSA with no MPU implemented */
5261         value &= ~SCTLR_M;
5262     }
5263 
5264     /* ??? Lots of these bits are not implemented.  */
5265 
5266     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5267         if (ri->opc1 == 6) { /* SCTLR_EL3 */
5268             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5269         } else {
5270             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5271                        SCTLR_ATA0 | SCTLR_ATA);
5272         }
5273     }
5274 
5275     if (raw_read(env, ri) == value) {
5276         /*
5277          * Skip the TLB flush if nothing actually changed; Linux likes
5278          * to do a lot of pointless SCTLR writes.
5279          */
5280         return;
5281     }
5282 
5283     raw_write(env, ri, value);
5284 
5285     /* This may enable/disable the MMU, so do a TLB flush.  */
5286     tlb_flush(CPU(cpu));
5287 
5288     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
5289         /*
5290          * Normally we would always end the TB on an SCTLR write; see the
5291          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5292          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5293          * of hflags from the translator, so do it here.
5294          */
5295         arm_rebuild_hflags(env);
5296     }
5297 }
5298 
5299 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5300                            uint64_t value)
5301 {
5302     /*
5303      * Some MDCR_EL3 bits affect whether PMU counters are running:
5304      * if we are trying to change any of those then we must
5305      * bracket this update with PMU start/finish calls.
5306      */
5307     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5308 
5309     if (pmu_op) {
5310         pmu_op_start(env);
5311     }
5312     env->cp15.mdcr_el3 = value;
5313     if (pmu_op) {
5314         pmu_op_finish(env);
5315     }
5316 }
5317 
5318 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5319                        uint64_t value)
5320 {
5321     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5322     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5323 }
5324 
5325 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5326                            uint64_t value)
5327 {
5328     /*
5329      * Some MDCR_EL2 bits affect whether PMU counters are running:
5330      * if we are trying to change any of those then we must
5331      * bracket this update with PMU start/finish calls.
5332      */
5333     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5334 
5335     if (pmu_op) {
5336         pmu_op_start(env);
5337     }
5338     env->cp15.mdcr_el2 = value;
5339     if (pmu_op) {
5340         pmu_op_finish(env);
5341     }
5342 }
5343 
5344 #ifdef CONFIG_USER_ONLY
5345 /*
5346  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5347  * code to get around W^X restrictions, where one region is writable and the
5348  * other is executable.
5349  *
5350  * Since the executable region is never written to we cannot detect code
5351  * changes when running in user mode, and rely on the emulated JIT telling us
5352  * that the code has changed by executing this instruction.
5353  */
5354 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5355                           uint64_t value)
5356 {
5357     uint64_t icache_line_mask, start_address, end_address;
5358     const ARMCPU *cpu;
5359 
5360     cpu = env_archcpu(env);
5361 
5362     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5363     start_address = value & ~icache_line_mask;
5364     end_address = value | icache_line_mask;
5365 
5366     mmap_lock();
5367 
5368     tb_invalidate_phys_range(start_address, end_address);
5369 
5370     mmap_unlock();
5371 }
5372 #endif
5373 
5374 static const ARMCPRegInfo v8_cp_reginfo[] = {
5375     /*
5376      * Minimal set of EL0-visible registers. This will need to be expanded
5377      * significantly for system emulation of AArch64 CPUs.
5378      */
5379     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5380       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5381       .access = PL0_RW, .type = ARM_CP_NZCV },
5382     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5383       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5384       .type = ARM_CP_NO_RAW,
5385       .access = PL0_RW, .accessfn = aa64_daif_access,
5386       .fieldoffset = offsetof(CPUARMState, daif),
5387       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5388     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5389       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5390       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5391       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5392     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5393       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5394       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5395       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5396     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5397       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5398       .access = PL0_R, .type = ARM_CP_NO_RAW,
5399       .fgt = FGT_DCZID_EL0,
5400       .readfn = aa64_dczid_read },
5401     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5402       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5403       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5404 #ifndef CONFIG_USER_ONLY
5405       /* Avoid overhead of an access check that always passes in user-mode */
5406       .accessfn = aa64_zva_access,
5407       .fgt = FGT_DCZVA,
5408 #endif
5409     },
5410     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5411       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5412       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5413     /*
5414      * Instruction cache ops. All of these except `IC IVAU` NOP because we
5415      * don't emulate caches.
5416      */
5417     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5418       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5419       .access = PL1_W, .type = ARM_CP_NOP,
5420       .fgt = FGT_ICIALLUIS,
5421       .accessfn = access_ticab },
5422     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5423       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5424       .access = PL1_W, .type = ARM_CP_NOP,
5425       .fgt = FGT_ICIALLU,
5426       .accessfn = access_tocu },
5427     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5428       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5429       .access = PL0_W,
5430       .fgt = FGT_ICIVAU,
5431       .accessfn = access_tocu,
5432 #ifdef CONFIG_USER_ONLY
5433       .type = ARM_CP_NO_RAW,
5434       .writefn = ic_ivau_write
5435 #else
5436       .type = ARM_CP_NOP
5437 #endif
5438     },
5439     /* Cache ops: all NOPs since we don't emulate caches */
5440     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5441       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5442       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5443       .fgt = FGT_DCIVAC,
5444       .type = ARM_CP_NOP },
5445     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5446       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5447       .fgt = FGT_DCISW,
5448       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5449     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5450       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5451       .access = PL0_W, .type = ARM_CP_NOP,
5452       .fgt = FGT_DCCVAC,
5453       .accessfn = aa64_cacheop_poc_access },
5454     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5455       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5456       .fgt = FGT_DCCSW,
5457       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5458     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5459       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5460       .access = PL0_W, .type = ARM_CP_NOP,
5461       .fgt = FGT_DCCVAU,
5462       .accessfn = access_tocu },
5463     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5464       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5465       .access = PL0_W, .type = ARM_CP_NOP,
5466       .fgt = FGT_DCCIVAC,
5467       .accessfn = aa64_cacheop_poc_access },
5468     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5469       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5470       .fgt = FGT_DCCISW,
5471       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5472     /* TLBI operations */
5473     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5474       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5475       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5476       .fgt = FGT_TLBIVMALLE1IS,
5477       .writefn = tlbi_aa64_vmalle1is_write },
5478     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5479       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5480       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5481       .fgt = FGT_TLBIVAE1IS,
5482       .writefn = tlbi_aa64_vae1is_write },
5483     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5484       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5485       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5486       .fgt = FGT_TLBIASIDE1IS,
5487       .writefn = tlbi_aa64_vmalle1is_write },
5488     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5489       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5490       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5491       .fgt = FGT_TLBIVAAE1IS,
5492       .writefn = tlbi_aa64_vae1is_write },
5493     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5494       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5495       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5496       .fgt = FGT_TLBIVALE1IS,
5497       .writefn = tlbi_aa64_vae1is_write },
5498     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5499       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5500       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5501       .fgt = FGT_TLBIVAALE1IS,
5502       .writefn = tlbi_aa64_vae1is_write },
5503     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5504       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5505       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5506       .fgt = FGT_TLBIVMALLE1,
5507       .writefn = tlbi_aa64_vmalle1_write },
5508     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5509       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5510       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5511       .fgt = FGT_TLBIVAE1,
5512       .writefn = tlbi_aa64_vae1_write },
5513     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5514       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5515       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5516       .fgt = FGT_TLBIASIDE1,
5517       .writefn = tlbi_aa64_vmalle1_write },
5518     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5519       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5520       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5521       .fgt = FGT_TLBIVAAE1,
5522       .writefn = tlbi_aa64_vae1_write },
5523     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5524       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5525       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5526       .fgt = FGT_TLBIVALE1,
5527       .writefn = tlbi_aa64_vae1_write },
5528     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5529       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5530       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5531       .fgt = FGT_TLBIVAALE1,
5532       .writefn = tlbi_aa64_vae1_write },
5533     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5534       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5535       .access = PL2_W, .type = ARM_CP_NO_RAW,
5536       .writefn = tlbi_aa64_ipas2e1is_write },
5537     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5538       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5539       .access = PL2_W, .type = ARM_CP_NO_RAW,
5540       .writefn = tlbi_aa64_ipas2e1is_write },
5541     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5542       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5543       .access = PL2_W, .type = ARM_CP_NO_RAW,
5544       .writefn = tlbi_aa64_alle1is_write },
5545     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5546       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5547       .access = PL2_W, .type = ARM_CP_NO_RAW,
5548       .writefn = tlbi_aa64_alle1is_write },
5549     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5550       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5551       .access = PL2_W, .type = ARM_CP_NO_RAW,
5552       .writefn = tlbi_aa64_ipas2e1_write },
5553     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5554       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5555       .access = PL2_W, .type = ARM_CP_NO_RAW,
5556       .writefn = tlbi_aa64_ipas2e1_write },
5557     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5558       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5559       .access = PL2_W, .type = ARM_CP_NO_RAW,
5560       .writefn = tlbi_aa64_alle1_write },
5561     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5562       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5563       .access = PL2_W, .type = ARM_CP_NO_RAW,
5564       .writefn = tlbi_aa64_alle1is_write },
5565 #ifndef CONFIG_USER_ONLY
5566     /* 64 bit address translation operations */
5567     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5568       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5569       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5570       .fgt = FGT_ATS1E1R,
5571       .accessfn = at_e012_access, .writefn = ats_write64 },
5572     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5573       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5574       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5575       .fgt = FGT_ATS1E1W,
5576       .accessfn = at_e012_access, .writefn = ats_write64 },
5577     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5578       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5579       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5580       .fgt = FGT_ATS1E0R,
5581       .accessfn = at_e012_access, .writefn = ats_write64 },
5582     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5583       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5584       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5585       .fgt = FGT_ATS1E0W,
5586       .accessfn = at_e012_access, .writefn = ats_write64 },
5587     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5588       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5589       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5590       .accessfn = at_e012_access, .writefn = ats_write64 },
5591     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5592       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5593       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5594       .accessfn = at_e012_access, .writefn = ats_write64 },
5595     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5596       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5597       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5598       .accessfn = at_e012_access, .writefn = ats_write64 },
5599     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5600       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5601       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5602       .accessfn = at_e012_access, .writefn = ats_write64 },
5603     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5604     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5605       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5606       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5607       .writefn = ats_write64 },
5608     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5609       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5610       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5611       .writefn = ats_write64 },
5612     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5613       .type = ARM_CP_ALIAS,
5614       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5615       .access = PL1_RW, .resetvalue = 0,
5616       .fgt = FGT_PAR_EL1,
5617       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5618       .writefn = par_write },
5619 #endif
5620     /* TLB invalidate last level of translation table walk */
5621     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5622       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5623       .writefn = tlbimva_is_write },
5624     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5625       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5626       .writefn = tlbimvaa_is_write },
5627     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5628       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5629       .writefn = tlbimva_write },
5630     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5631       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5632       .writefn = tlbimvaa_write },
5633     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5634       .type = ARM_CP_NO_RAW, .access = PL2_W,
5635       .writefn = tlbimva_hyp_write },
5636     { .name = "TLBIMVALHIS",
5637       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5638       .type = ARM_CP_NO_RAW, .access = PL2_W,
5639       .writefn = tlbimva_hyp_is_write },
5640     { .name = "TLBIIPAS2",
5641       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5642       .type = ARM_CP_NO_RAW, .access = PL2_W,
5643       .writefn = tlbiipas2_hyp_write },
5644     { .name = "TLBIIPAS2IS",
5645       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5646       .type = ARM_CP_NO_RAW, .access = PL2_W,
5647       .writefn = tlbiipas2is_hyp_write },
5648     { .name = "TLBIIPAS2L",
5649       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5650       .type = ARM_CP_NO_RAW, .access = PL2_W,
5651       .writefn = tlbiipas2_hyp_write },
5652     { .name = "TLBIIPAS2LIS",
5653       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5654       .type = ARM_CP_NO_RAW, .access = PL2_W,
5655       .writefn = tlbiipas2is_hyp_write },
5656     /* 32 bit cache operations */
5657     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5658       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5659     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5660       .type = ARM_CP_NOP, .access = PL1_W },
5661     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5662       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5663     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5664       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5665     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5666       .type = ARM_CP_NOP, .access = PL1_W },
5667     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5668       .type = ARM_CP_NOP, .access = PL1_W },
5669     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5670       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5671     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5672       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5673     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5674       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5675     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5676       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5677     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5678       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5679     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5680       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5681     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5682       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5683     /* MMU Domain access control / MPU write buffer control */
5684     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5685       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5686       .writefn = dacr_write, .raw_writefn = raw_write,
5687       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5688                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5689     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5690       .type = ARM_CP_ALIAS,
5691       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5692       .access = PL1_RW,
5693       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5694     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5695       .type = ARM_CP_ALIAS,
5696       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5697       .access = PL1_RW,
5698       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5699     /*
5700      * We rely on the access checks not allowing the guest to write to the
5701      * state field when SPSel indicates that it's being used as the stack
5702      * pointer.
5703      */
5704     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5705       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5706       .access = PL1_RW, .accessfn = sp_el0_access,
5707       .type = ARM_CP_ALIAS,
5708       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5709     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5710       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5711       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5712       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5713     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5714       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5715       .type = ARM_CP_NO_RAW,
5716       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5717     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5718       .type = ARM_CP_ALIAS,
5719       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5720       .access = PL2_RW,
5721       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5722     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5723       .type = ARM_CP_ALIAS,
5724       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5725       .access = PL2_RW,
5726       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5727     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5728       .type = ARM_CP_ALIAS,
5729       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5730       .access = PL2_RW,
5731       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5732     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5733       .type = ARM_CP_ALIAS,
5734       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5735       .access = PL2_RW,
5736       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5737     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5738       .type = ARM_CP_IO,
5739       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5740       .resetvalue = 0,
5741       .access = PL3_RW,
5742       .writefn = mdcr_el3_write,
5743       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5744     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5745       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5746       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5747       .writefn = sdcr_write,
5748       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5749 };
5750 
5751 /* These are present only when EL1 supports AArch32 */
5752 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5753     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5754       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5755       .access = PL2_RW,
5756       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5757       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5758     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5759       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5760       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5761       .writefn = dacr_write, .raw_writefn = raw_write,
5762       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5763     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5764       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5765       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5766       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5767 };
5768 
5769 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5770 {
5771     ARMCPU *cpu = env_archcpu(env);
5772 
5773     if (arm_feature(env, ARM_FEATURE_V8)) {
5774         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5775     } else {
5776         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5777     }
5778 
5779     if (arm_feature(env, ARM_FEATURE_EL3)) {
5780         valid_mask &= ~HCR_HCD;
5781     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5782         /*
5783          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5784          * However, if we're using the SMC PSCI conduit then QEMU is
5785          * effectively acting like EL3 firmware and so the guest at
5786          * EL2 should retain the ability to prevent EL1 from being
5787          * able to make SMC calls into the ersatz firmware, so in
5788          * that case HCR.TSC should be read/write.
5789          */
5790         valid_mask &= ~HCR_TSC;
5791     }
5792 
5793     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5794         if (cpu_isar_feature(aa64_vh, cpu)) {
5795             valid_mask |= HCR_E2H;
5796         }
5797         if (cpu_isar_feature(aa64_ras, cpu)) {
5798             valid_mask |= HCR_TERR | HCR_TEA;
5799         }
5800         if (cpu_isar_feature(aa64_lor, cpu)) {
5801             valid_mask |= HCR_TLOR;
5802         }
5803         if (cpu_isar_feature(aa64_pauth, cpu)) {
5804             valid_mask |= HCR_API | HCR_APK;
5805         }
5806         if (cpu_isar_feature(aa64_mte, cpu)) {
5807             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5808         }
5809         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5810             valid_mask |= HCR_ENSCXT;
5811         }
5812         if (cpu_isar_feature(aa64_fwb, cpu)) {
5813             valid_mask |= HCR_FWB;
5814         }
5815         if (cpu_isar_feature(aa64_rme, cpu)) {
5816             valid_mask |= HCR_GPF;
5817         }
5818     }
5819 
5820     if (cpu_isar_feature(any_evt, cpu)) {
5821         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5822     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5823         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5824     }
5825 
5826     /* Clear RES0 bits.  */
5827     value &= valid_mask;
5828 
5829     /*
5830      * These bits change the MMU setup:
5831      * HCR_VM enables stage 2 translation
5832      * HCR_PTW forbids certain page-table setups
5833      * HCR_DC disables stage1 and enables stage2 translation
5834      * HCR_DCT enables tagging on (disabled) stage1 translation
5835      * HCR_FWB changes the interpretation of stage2 descriptor bits
5836      */
5837     if ((env->cp15.hcr_el2 ^ value) &
5838         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5839         tlb_flush(CPU(cpu));
5840     }
5841     env->cp15.hcr_el2 = value;
5842 
5843     /*
5844      * Updates to VI and VF require us to update the status of
5845      * virtual interrupts, which are the logical OR of these bits
5846      * and the state of the input lines from the GIC. (This requires
5847      * that we have the iothread lock, which is done by marking the
5848      * reginfo structs as ARM_CP_IO.)
5849      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5850      * possible for it to be taken immediately, because VIRQ and
5851      * VFIQ are masked unless running at EL0 or EL1, and HCR
5852      * can only be written at EL2.
5853      */
5854     g_assert(qemu_mutex_iothread_locked());
5855     arm_cpu_update_virq(cpu);
5856     arm_cpu_update_vfiq(cpu);
5857     arm_cpu_update_vserr(cpu);
5858 }
5859 
5860 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5861 {
5862     do_hcr_write(env, value, 0);
5863 }
5864 
5865 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5866                           uint64_t value)
5867 {
5868     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5869     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5870     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5871 }
5872 
5873 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5874                          uint64_t value)
5875 {
5876     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5877     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5878     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5879 }
5880 
5881 /*
5882  * Return the effective value of HCR_EL2, at the given security state.
5883  * Bits that are not included here:
5884  * RW       (read from SCR_EL3.RW as needed)
5885  */
5886 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5887 {
5888     uint64_t ret = env->cp15.hcr_el2;
5889 
5890     assert(space != ARMSS_Root);
5891 
5892     if (!arm_is_el2_enabled_secstate(env, space)) {
5893         /*
5894          * "This register has no effect if EL2 is not enabled in the
5895          * current Security state".  This is ARMv8.4-SecEL2 speak for
5896          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5897          *
5898          * Prior to that, the language was "In an implementation that
5899          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5900          * as if this field is 0 for all purposes other than a direct
5901          * read or write access of HCR_EL2".  With lots of enumeration
5902          * on a per-field basis.  In current QEMU, this is condition
5903          * is arm_is_secure_below_el3.
5904          *
5905          * Since the v8.4 language applies to the entire register, and
5906          * appears to be backward compatible, use that.
5907          */
5908         return 0;
5909     }
5910 
5911     /*
5912      * For a cpu that supports both aarch64 and aarch32, we can set bits
5913      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5914      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5915      */
5916     if (!arm_el_is_aa64(env, 2)) {
5917         uint64_t aa32_valid;
5918 
5919         /*
5920          * These bits are up-to-date as of ARMv8.6.
5921          * For HCR, it's easiest to list just the 2 bits that are invalid.
5922          * For HCR2, list those that are valid.
5923          */
5924         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5925         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5926                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5927         ret &= aa32_valid;
5928     }
5929 
5930     if (ret & HCR_TGE) {
5931         /* These bits are up-to-date as of ARMv8.6.  */
5932         if (ret & HCR_E2H) {
5933             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5934                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5935                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5936                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5937                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5938                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5939         } else {
5940             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5941         }
5942         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5943                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5944                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5945                  HCR_TLOR);
5946     }
5947 
5948     return ret;
5949 }
5950 
5951 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5952 {
5953     if (arm_feature(env, ARM_FEATURE_M)) {
5954         return 0;
5955     }
5956     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
5957 }
5958 
5959 /*
5960  * Corresponds to ARM pseudocode function ELIsInHost().
5961  */
5962 bool el_is_in_host(CPUARMState *env, int el)
5963 {
5964     uint64_t mask;
5965 
5966     /*
5967      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5968      * Perform the simplest bit tests first, and validate EL2 afterward.
5969      */
5970     if (el & 1) {
5971         return false; /* EL1 or EL3 */
5972     }
5973 
5974     /*
5975      * Note that hcr_write() checks isar_feature_aa64_vh(),
5976      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5977      */
5978     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5979     if ((env->cp15.hcr_el2 & mask) != mask) {
5980         return false;
5981     }
5982 
5983     /* TGE and/or E2H set: double check those bits are currently legal. */
5984     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5985 }
5986 
5987 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5988                        uint64_t value)
5989 {
5990     uint64_t valid_mask = 0;
5991 
5992     /* FEAT_MOPS adds MSCEn and MCE2 */
5993     if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5994         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
5995     }
5996 
5997     /* Clear RES0 bits.  */
5998     env->cp15.hcrx_el2 = value & valid_mask;
5999 }
6000 
6001 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
6002                                   bool isread)
6003 {
6004     if (arm_current_el(env) < 3
6005         && arm_feature(env, ARM_FEATURE_EL3)
6006         && !(env->cp15.scr_el3 & SCR_HXEN)) {
6007         return CP_ACCESS_TRAP_EL3;
6008     }
6009     return CP_ACCESS_OK;
6010 }
6011 
6012 static const ARMCPRegInfo hcrx_el2_reginfo = {
6013     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
6014     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
6015     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
6016     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
6017 };
6018 
6019 /* Return the effective value of HCRX_EL2.  */
6020 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
6021 {
6022     /*
6023      * The bits in this register behave as 0 for all purposes other than
6024      * direct reads of the register if SCR_EL3.HXEn is 0.
6025      * If EL2 is not enabled in the current security state, then the
6026      * bit may behave as if 0, or as if 1, depending on the bit.
6027      * For the moment, we treat the EL2-disabled case as taking
6028      * priority over the HXEn-disabled case. This is true for the only
6029      * bit for a feature which we implement where the answer is different
6030      * for the two cases (MSCEn for FEAT_MOPS).
6031      * This may need to be revisited for future bits.
6032      */
6033     if (!arm_is_el2_enabled(env)) {
6034         uint64_t hcrx = 0;
6035         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6036             /* MSCEn behaves as 1 if EL2 is not enabled */
6037             hcrx |= HCRX_MSCEN;
6038         }
6039         return hcrx;
6040     }
6041     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
6042         return 0;
6043     }
6044     return env->cp15.hcrx_el2;
6045 }
6046 
6047 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6048                            uint64_t value)
6049 {
6050     /*
6051      * For A-profile AArch32 EL3, if NSACR.CP10
6052      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6053      */
6054     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6055         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6056         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6057         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
6058     }
6059     env->cp15.cptr_el[2] = value;
6060 }
6061 
6062 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6063 {
6064     /*
6065      * For A-profile AArch32 EL3, if NSACR.CP10
6066      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6067      */
6068     uint64_t value = env->cp15.cptr_el[2];
6069 
6070     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6071         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6072         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6073     }
6074     return value;
6075 }
6076 
6077 static const ARMCPRegInfo el2_cp_reginfo[] = {
6078     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
6079       .type = ARM_CP_IO,
6080       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6081       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6082       .writefn = hcr_write, .raw_writefn = raw_write },
6083     { .name = "HCR", .state = ARM_CP_STATE_AA32,
6084       .type = ARM_CP_ALIAS | ARM_CP_IO,
6085       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6086       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6087       .writefn = hcr_writelow },
6088     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6089       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6090       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6091     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
6092       .type = ARM_CP_ALIAS,
6093       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
6094       .access = PL2_RW,
6095       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
6096     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
6097       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6098       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
6099     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
6100       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6101       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
6102     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6103       .type = ARM_CP_ALIAS,
6104       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6105       .access = PL2_RW,
6106       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
6107     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
6108       .type = ARM_CP_ALIAS,
6109       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
6110       .access = PL2_RW,
6111       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
6112     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
6113       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6114       .access = PL2_RW, .writefn = vbar_write,
6115       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6116       .resetvalue = 0 },
6117     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6118       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
6119       .access = PL3_RW, .type = ARM_CP_ALIAS,
6120       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
6121     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6122       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6123       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
6124       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6125       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
6126     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6127       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6128       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6129       .resetvalue = 0 },
6130     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
6131       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
6132       .access = PL2_RW, .type = ARM_CP_ALIAS,
6133       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
6134     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6135       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6136       .access = PL2_RW, .type = ARM_CP_CONST,
6137       .resetvalue = 0 },
6138     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
6139     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
6140       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
6141       .access = PL2_RW, .type = ARM_CP_CONST,
6142       .resetvalue = 0 },
6143     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6144       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6145       .access = PL2_RW, .type = ARM_CP_CONST,
6146       .resetvalue = 0 },
6147     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6148       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6149       .access = PL2_RW, .type = ARM_CP_CONST,
6150       .resetvalue = 0 },
6151     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6152       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6153       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
6154       .raw_writefn = raw_write,
6155       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
6156     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6157       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6158       .type = ARM_CP_ALIAS,
6159       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6160       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
6161     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6162       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6163       .access = PL2_RW,
6164       /* no .writefn needed as this can't cause an ASID change */
6165       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
6166     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6167       .cp = 15, .opc1 = 6, .crm = 2,
6168       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6169       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6170       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
6171       .writefn = vttbr_write, .raw_writefn = raw_write },
6172     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6173       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
6174       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
6175       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
6176     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6177       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6178       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6179       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
6180     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6181       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6182       .access = PL2_RW, .resetvalue = 0,
6183       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
6184     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6185       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
6186       .access = PL2_RW, .resetvalue = 0,
6187       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
6188       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6189     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6190       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6191       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6192     { .name = "TLBIALLNSNH",
6193       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6194       .type = ARM_CP_NO_RAW, .access = PL2_W,
6195       .writefn = tlbiall_nsnh_write },
6196     { .name = "TLBIALLNSNHIS",
6197       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6198       .type = ARM_CP_NO_RAW, .access = PL2_W,
6199       .writefn = tlbiall_nsnh_is_write },
6200     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6201       .type = ARM_CP_NO_RAW, .access = PL2_W,
6202       .writefn = tlbiall_hyp_write },
6203     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6204       .type = ARM_CP_NO_RAW, .access = PL2_W,
6205       .writefn = tlbiall_hyp_is_write },
6206     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6207       .type = ARM_CP_NO_RAW, .access = PL2_W,
6208       .writefn = tlbimva_hyp_write },
6209     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6210       .type = ARM_CP_NO_RAW, .access = PL2_W,
6211       .writefn = tlbimva_hyp_is_write },
6212     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6213       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6214       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6215       .writefn = tlbi_aa64_alle2_write },
6216     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6217       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6218       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6219       .writefn = tlbi_aa64_vae2_write },
6220     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6221       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6222       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6223       .writefn = tlbi_aa64_vae2_write },
6224     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6225       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6226       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6227       .writefn = tlbi_aa64_alle2is_write },
6228     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6229       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6230       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6231       .writefn = tlbi_aa64_vae2is_write },
6232     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6233       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6234       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6235       .writefn = tlbi_aa64_vae2is_write },
6236 #ifndef CONFIG_USER_ONLY
6237     /*
6238      * Unlike the other EL2-related AT operations, these must
6239      * UNDEF from EL3 if EL2 is not implemented, which is why we
6240      * define them here rather than with the rest of the AT ops.
6241      */
6242     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6243       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6244       .access = PL2_W, .accessfn = at_s1e2_access,
6245       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6246       .writefn = ats_write64 },
6247     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6248       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6249       .access = PL2_W, .accessfn = at_s1e2_access,
6250       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6251       .writefn = ats_write64 },
6252     /*
6253      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
6254      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6255      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6256      * to behave as if SCR.NS was 1.
6257      */
6258     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6259       .access = PL2_W,
6260       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6261     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6262       .access = PL2_W,
6263       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6264     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6265       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6266       /*
6267        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6268        * reset values as IMPDEF. We choose to reset to 3 to comply with
6269        * both ARMv7 and ARMv8.
6270        */
6271       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6272       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
6273       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6274     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6275       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6276       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6277       .writefn = gt_cntvoff_write,
6278       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6279     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6280       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6281       .writefn = gt_cntvoff_write,
6282       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6283     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6284       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6285       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6286       .type = ARM_CP_IO, .access = PL2_RW,
6287       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6288     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6289       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6290       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6291       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6292     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6293       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6294       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6295       .resetfn = gt_hyp_timer_reset,
6296       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6297     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6298       .type = ARM_CP_IO,
6299       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6300       .access = PL2_RW,
6301       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6302       .resetvalue = 0,
6303       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6304 #endif
6305     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6306       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6307       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6308       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6309     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6310       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6311       .access = PL2_RW,
6312       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6313     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6314       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6315       .access = PL2_RW,
6316       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6317 };
6318 
6319 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6320     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6321       .type = ARM_CP_ALIAS | ARM_CP_IO,
6322       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6323       .access = PL2_RW,
6324       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6325       .writefn = hcr_writehigh },
6326 };
6327 
6328 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6329                                   bool isread)
6330 {
6331     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6332         return CP_ACCESS_OK;
6333     }
6334     return CP_ACCESS_TRAP_UNCATEGORIZED;
6335 }
6336 
6337 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6338     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6339       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6340       .access = PL2_RW, .accessfn = sel2_access,
6341       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6342     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6343       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6344       .access = PL2_RW, .accessfn = sel2_access,
6345       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6346 };
6347 
6348 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6349                                    bool isread)
6350 {
6351     /*
6352      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6353      * At Secure EL1 it traps to EL3 or EL2.
6354      */
6355     if (arm_current_el(env) == 3) {
6356         return CP_ACCESS_OK;
6357     }
6358     if (arm_is_secure_below_el3(env)) {
6359         if (env->cp15.scr_el3 & SCR_EEL2) {
6360             return CP_ACCESS_TRAP_EL2;
6361         }
6362         return CP_ACCESS_TRAP_EL3;
6363     }
6364     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6365     if (isread) {
6366         return CP_ACCESS_OK;
6367     }
6368     return CP_ACCESS_TRAP_UNCATEGORIZED;
6369 }
6370 
6371 static const ARMCPRegInfo el3_cp_reginfo[] = {
6372     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6373       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6374       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6375       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
6376     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6377       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6378       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6379       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6380       .writefn = scr_write, .raw_writefn = raw_write },
6381     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6382       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6383       .access = PL3_RW, .resetvalue = 0,
6384       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6385     { .name = "SDER",
6386       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6387       .access = PL3_RW, .resetvalue = 0,
6388       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6389     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6390       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6391       .writefn = vbar_write, .resetvalue = 0,
6392       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6393     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6394       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6395       .access = PL3_RW, .resetvalue = 0,
6396       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6397     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6398       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6399       .access = PL3_RW,
6400       /* no .writefn needed as this can't cause an ASID change */
6401       .resetvalue = 0,
6402       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6403     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6404       .type = ARM_CP_ALIAS,
6405       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6406       .access = PL3_RW,
6407       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6408     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6409       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6410       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6411     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6412       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6413       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6414     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6415       .type = ARM_CP_ALIAS,
6416       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6417       .access = PL3_RW,
6418       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6419     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6420       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6421       .access = PL3_RW, .writefn = vbar_write,
6422       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6423       .resetvalue = 0 },
6424     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6425       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6426       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6427       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6428     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6429       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6430       .access = PL3_RW, .resetvalue = 0,
6431       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6432     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6433       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6434       .access = PL3_RW, .type = ARM_CP_CONST,
6435       .resetvalue = 0 },
6436     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6437       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6438       .access = PL3_RW, .type = ARM_CP_CONST,
6439       .resetvalue = 0 },
6440     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6441       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6442       .access = PL3_RW, .type = ARM_CP_CONST,
6443       .resetvalue = 0 },
6444     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6445       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6446       .access = PL3_W, .type = ARM_CP_NO_RAW,
6447       .writefn = tlbi_aa64_alle3is_write },
6448     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6449       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6450       .access = PL3_W, .type = ARM_CP_NO_RAW,
6451       .writefn = tlbi_aa64_vae3is_write },
6452     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6453       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6454       .access = PL3_W, .type = ARM_CP_NO_RAW,
6455       .writefn = tlbi_aa64_vae3is_write },
6456     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6457       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6458       .access = PL3_W, .type = ARM_CP_NO_RAW,
6459       .writefn = tlbi_aa64_alle3_write },
6460     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6461       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6462       .access = PL3_W, .type = ARM_CP_NO_RAW,
6463       .writefn = tlbi_aa64_vae3_write },
6464     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6465       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6466       .access = PL3_W, .type = ARM_CP_NO_RAW,
6467       .writefn = tlbi_aa64_vae3_write },
6468 };
6469 
6470 #ifndef CONFIG_USER_ONLY
6471 /* Test if system register redirection is to occur in the current state.  */
6472 static bool redirect_for_e2h(CPUARMState *env)
6473 {
6474     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6475 }
6476 
6477 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6478 {
6479     CPReadFn *readfn;
6480 
6481     if (redirect_for_e2h(env)) {
6482         /* Switch to the saved EL2 version of the register.  */
6483         ri = ri->opaque;
6484         readfn = ri->readfn;
6485     } else {
6486         readfn = ri->orig_readfn;
6487     }
6488     if (readfn == NULL) {
6489         readfn = raw_read;
6490     }
6491     return readfn(env, ri);
6492 }
6493 
6494 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6495                           uint64_t value)
6496 {
6497     CPWriteFn *writefn;
6498 
6499     if (redirect_for_e2h(env)) {
6500         /* Switch to the saved EL2 version of the register.  */
6501         ri = ri->opaque;
6502         writefn = ri->writefn;
6503     } else {
6504         writefn = ri->orig_writefn;
6505     }
6506     if (writefn == NULL) {
6507         writefn = raw_write;
6508     }
6509     writefn(env, ri, value);
6510 }
6511 
6512 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6513 {
6514     struct E2HAlias {
6515         uint32_t src_key, dst_key, new_key;
6516         const char *src_name, *dst_name, *new_name;
6517         bool (*feature)(const ARMISARegisters *id);
6518     };
6519 
6520 #define K(op0, op1, crn, crm, op2) \
6521     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6522 
6523     static const struct E2HAlias aliases[] = {
6524         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6525           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6526         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6527           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6528         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6529           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6530         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6531           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6532         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6533           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6534         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6535           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6536         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6537           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6538         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6539           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6540         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6541           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6542         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6543           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6544         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6545           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6546         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6547           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6548         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6549           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6550         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6551           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6552         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6553           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6554         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6555           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6556 
6557         /*
6558          * Note that redirection of ZCR is mentioned in the description
6559          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6560          * not in the summary table.
6561          */
6562         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6563           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6564         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6565           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6566 
6567         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6568           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6569 
6570         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6571           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6572           isar_feature_aa64_scxtnum },
6573 
6574         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6575         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6576     };
6577 #undef K
6578 
6579     size_t i;
6580 
6581     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6582         const struct E2HAlias *a = &aliases[i];
6583         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6584         bool ok;
6585 
6586         if (a->feature && !a->feature(&cpu->isar)) {
6587             continue;
6588         }
6589 
6590         src_reg = g_hash_table_lookup(cpu->cp_regs,
6591                                       (gpointer)(uintptr_t)a->src_key);
6592         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6593                                       (gpointer)(uintptr_t)a->dst_key);
6594         g_assert(src_reg != NULL);
6595         g_assert(dst_reg != NULL);
6596 
6597         /* Cross-compare names to detect typos in the keys.  */
6598         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6599         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6600 
6601         /* None of the core system registers use opaque; we will.  */
6602         g_assert(src_reg->opaque == NULL);
6603 
6604         /* Create alias before redirection so we dup the right data. */
6605         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6606 
6607         new_reg->name = a->new_name;
6608         new_reg->type |= ARM_CP_ALIAS;
6609         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6610         new_reg->access &= PL2_RW | PL3_RW;
6611 
6612         ok = g_hash_table_insert(cpu->cp_regs,
6613                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6614         g_assert(ok);
6615 
6616         src_reg->opaque = dst_reg;
6617         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6618         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6619         if (!src_reg->raw_readfn) {
6620             src_reg->raw_readfn = raw_read;
6621         }
6622         if (!src_reg->raw_writefn) {
6623             src_reg->raw_writefn = raw_write;
6624         }
6625         src_reg->readfn = el2_e2h_read;
6626         src_reg->writefn = el2_e2h_write;
6627     }
6628 }
6629 #endif
6630 
6631 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6632                                      bool isread)
6633 {
6634     int cur_el = arm_current_el(env);
6635 
6636     if (cur_el < 2) {
6637         uint64_t hcr = arm_hcr_el2_eff(env);
6638 
6639         if (cur_el == 0) {
6640             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6641                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6642                     return CP_ACCESS_TRAP_EL2;
6643                 }
6644             } else {
6645                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6646                     return CP_ACCESS_TRAP;
6647                 }
6648                 if (hcr & HCR_TID2) {
6649                     return CP_ACCESS_TRAP_EL2;
6650                 }
6651             }
6652         } else if (hcr & HCR_TID2) {
6653             return CP_ACCESS_TRAP_EL2;
6654         }
6655     }
6656 
6657     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6658         return CP_ACCESS_TRAP_EL2;
6659     }
6660 
6661     return CP_ACCESS_OK;
6662 }
6663 
6664 /*
6665  * Check for traps to RAS registers, which are controlled
6666  * by HCR_EL2.TERR and SCR_EL3.TERR.
6667  */
6668 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6669                                   bool isread)
6670 {
6671     int el = arm_current_el(env);
6672 
6673     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6674         return CP_ACCESS_TRAP_EL2;
6675     }
6676     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6677         return CP_ACCESS_TRAP_EL3;
6678     }
6679     return CP_ACCESS_OK;
6680 }
6681 
6682 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6683 {
6684     int el = arm_current_el(env);
6685 
6686     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6687         return env->cp15.vdisr_el2;
6688     }
6689     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6690         return 0; /* RAZ/WI */
6691     }
6692     return env->cp15.disr_el1;
6693 }
6694 
6695 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6696 {
6697     int el = arm_current_el(env);
6698 
6699     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6700         env->cp15.vdisr_el2 = val;
6701         return;
6702     }
6703     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6704         return; /* RAZ/WI */
6705     }
6706     env->cp15.disr_el1 = val;
6707 }
6708 
6709 /*
6710  * Minimal RAS implementation with no Error Records.
6711  * Which means that all of the Error Record registers:
6712  *   ERXADDR_EL1
6713  *   ERXCTLR_EL1
6714  *   ERXFR_EL1
6715  *   ERXMISC0_EL1
6716  *   ERXMISC1_EL1
6717  *   ERXMISC2_EL1
6718  *   ERXMISC3_EL1
6719  *   ERXPFGCDN_EL1  (RASv1p1)
6720  *   ERXPFGCTL_EL1  (RASv1p1)
6721  *   ERXPFGF_EL1    (RASv1p1)
6722  *   ERXSTATUS_EL1
6723  * and
6724  *   ERRSELR_EL1
6725  * may generate UNDEFINED, which is the effect we get by not
6726  * listing them at all.
6727  *
6728  * These registers have fine-grained trap bits, but UNDEF-to-EL1
6729  * is higher priority than FGT-to-EL2 so we do not need to list them
6730  * in order to check for an FGT.
6731  */
6732 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6733     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6734       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6735       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6736       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6737     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6738       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6739       .access = PL1_R, .accessfn = access_terr,
6740       .fgt = FGT_ERRIDR_EL1,
6741       .type = ARM_CP_CONST, .resetvalue = 0 },
6742     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6743       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6744       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6745     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6746       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6747       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6748 };
6749 
6750 /*
6751  * Return the exception level to which exceptions should be taken
6752  * via SVEAccessTrap.  This excludes the check for whether the exception
6753  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6754  * be found by testing 0 < fp_exception_el < sve_exception_el.
6755  *
6756  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6757  * pseudocode does *not* separate out the FP trap checks, but has them
6758  * all in one function.
6759  */
6760 int sve_exception_el(CPUARMState *env, int el)
6761 {
6762 #ifndef CONFIG_USER_ONLY
6763     if (el <= 1 && !el_is_in_host(env, el)) {
6764         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6765         case 1:
6766             if (el != 0) {
6767                 break;
6768             }
6769             /* fall through */
6770         case 0:
6771         case 2:
6772             return 1;
6773         }
6774     }
6775 
6776     if (el <= 2 && arm_is_el2_enabled(env)) {
6777         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6778         if (env->cp15.hcr_el2 & HCR_E2H) {
6779             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6780             case 1:
6781                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6782                     break;
6783                 }
6784                 /* fall through */
6785             case 0:
6786             case 2:
6787                 return 2;
6788             }
6789         } else {
6790             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6791                 return 2;
6792             }
6793         }
6794     }
6795 
6796     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6797     if (arm_feature(env, ARM_FEATURE_EL3)
6798         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6799         return 3;
6800     }
6801 #endif
6802     return 0;
6803 }
6804 
6805 /*
6806  * Return the exception level to which exceptions should be taken for SME.
6807  * C.f. the ARM pseudocode function CheckSMEAccess.
6808  */
6809 int sme_exception_el(CPUARMState *env, int el)
6810 {
6811 #ifndef CONFIG_USER_ONLY
6812     if (el <= 1 && !el_is_in_host(env, el)) {
6813         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6814         case 1:
6815             if (el != 0) {
6816                 break;
6817             }
6818             /* fall through */
6819         case 0:
6820         case 2:
6821             return 1;
6822         }
6823     }
6824 
6825     if (el <= 2 && arm_is_el2_enabled(env)) {
6826         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6827         if (env->cp15.hcr_el2 & HCR_E2H) {
6828             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6829             case 1:
6830                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6831                     break;
6832                 }
6833                 /* fall through */
6834             case 0:
6835             case 2:
6836                 return 2;
6837             }
6838         } else {
6839             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6840                 return 2;
6841             }
6842         }
6843     }
6844 
6845     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6846     if (arm_feature(env, ARM_FEATURE_EL3)
6847         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6848         return 3;
6849     }
6850 #endif
6851     return 0;
6852 }
6853 
6854 /*
6855  * Given that SVE is enabled, return the vector length for EL.
6856  */
6857 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6858 {
6859     ARMCPU *cpu = env_archcpu(env);
6860     uint64_t *cr = env->vfp.zcr_el;
6861     uint32_t map = cpu->sve_vq.map;
6862     uint32_t len = ARM_MAX_VQ - 1;
6863 
6864     if (sm) {
6865         cr = env->vfp.smcr_el;
6866         map = cpu->sme_vq.map;
6867     }
6868 
6869     if (el <= 1 && !el_is_in_host(env, el)) {
6870         len = MIN(len, 0xf & (uint32_t)cr[1]);
6871     }
6872     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6873         len = MIN(len, 0xf & (uint32_t)cr[2]);
6874     }
6875     if (arm_feature(env, ARM_FEATURE_EL3)) {
6876         len = MIN(len, 0xf & (uint32_t)cr[3]);
6877     }
6878 
6879     map &= MAKE_64BIT_MASK(0, len + 1);
6880     if (map != 0) {
6881         return 31 - clz32(map);
6882     }
6883 
6884     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6885     assert(sm);
6886     return ctz32(cpu->sme_vq.map);
6887 }
6888 
6889 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6890 {
6891     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6892 }
6893 
6894 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6895                       uint64_t value)
6896 {
6897     int cur_el = arm_current_el(env);
6898     int old_len = sve_vqm1_for_el(env, cur_el);
6899     int new_len;
6900 
6901     /* Bits other than [3:0] are RAZ/WI.  */
6902     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6903     raw_write(env, ri, value & 0xf);
6904 
6905     /*
6906      * Because we arrived here, we know both FP and SVE are enabled;
6907      * otherwise we would have trapped access to the ZCR_ELn register.
6908      */
6909     new_len = sve_vqm1_for_el(env, cur_el);
6910     if (new_len < old_len) {
6911         aarch64_sve_narrow_vq(env, new_len + 1);
6912     }
6913 }
6914 
6915 static const ARMCPRegInfo zcr_reginfo[] = {
6916     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6917       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6918       .access = PL1_RW, .type = ARM_CP_SVE,
6919       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6920       .writefn = zcr_write, .raw_writefn = raw_write },
6921     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6922       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6923       .access = PL2_RW, .type = ARM_CP_SVE,
6924       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6925       .writefn = zcr_write, .raw_writefn = raw_write },
6926     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6927       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6928       .access = PL3_RW, .type = ARM_CP_SVE,
6929       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6930       .writefn = zcr_write, .raw_writefn = raw_write },
6931 };
6932 
6933 #ifdef TARGET_AARCH64
6934 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6935                                     bool isread)
6936 {
6937     int el = arm_current_el(env);
6938 
6939     if (el == 0) {
6940         uint64_t sctlr = arm_sctlr(env, el);
6941         if (!(sctlr & SCTLR_EnTP2)) {
6942             return CP_ACCESS_TRAP;
6943         }
6944     }
6945     /* TODO: FEAT_FGT */
6946     if (el < 3
6947         && arm_feature(env, ARM_FEATURE_EL3)
6948         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6949         return CP_ACCESS_TRAP_EL3;
6950     }
6951     return CP_ACCESS_OK;
6952 }
6953 
6954 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6955                                  bool isread)
6956 {
6957     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6958     if (arm_current_el(env) < 3
6959         && arm_feature(env, ARM_FEATURE_EL3)
6960         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6961         return CP_ACCESS_TRAP_EL3;
6962     }
6963     return CP_ACCESS_OK;
6964 }
6965 
6966 /* ResetSVEState */
6967 static void arm_reset_sve_state(CPUARMState *env)
6968 {
6969     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6970     /* Recall that FFR is stored as pregs[16]. */
6971     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6972     vfp_set_fpcr(env, 0x0800009f);
6973 }
6974 
6975 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6976 {
6977     uint64_t change = (env->svcr ^ new) & mask;
6978 
6979     if (change == 0) {
6980         return;
6981     }
6982     env->svcr ^= change;
6983 
6984     if (change & R_SVCR_SM_MASK) {
6985         arm_reset_sve_state(env);
6986     }
6987 
6988     /*
6989      * ResetSMEState.
6990      *
6991      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
6992      * on enable: while disabled, the storage is inaccessible and the
6993      * value does not matter.  We're not saving the storage in vmstate
6994      * when disabled either.
6995      */
6996     if (change & new & R_SVCR_ZA_MASK) {
6997         memset(env->zarray, 0, sizeof(env->zarray));
6998     }
6999 
7000     if (tcg_enabled()) {
7001         arm_rebuild_hflags(env);
7002     }
7003 }
7004 
7005 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7006                        uint64_t value)
7007 {
7008     aarch64_set_svcr(env, value, -1);
7009 }
7010 
7011 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7012                        uint64_t value)
7013 {
7014     int cur_el = arm_current_el(env);
7015     int old_len = sve_vqm1_for_el(env, cur_el);
7016     int new_len;
7017 
7018     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
7019     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
7020     raw_write(env, ri, value);
7021 
7022     /*
7023      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
7024      * when SVL is widened (old values kept, or zeros).  Choose to keep the
7025      * current values for simplicity.  But for QEMU internals, we must still
7026      * apply the narrower SVL to the Zregs and Pregs -- see the comment
7027      * above aarch64_sve_narrow_vq.
7028      */
7029     new_len = sve_vqm1_for_el(env, cur_el);
7030     if (new_len < old_len) {
7031         aarch64_sve_narrow_vq(env, new_len + 1);
7032     }
7033 }
7034 
7035 static const ARMCPRegInfo sme_reginfo[] = {
7036     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7037       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7038       .access = PL0_RW, .accessfn = access_tpidr2,
7039       .fgt = FGT_NTPIDR2_EL0,
7040       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
7041     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7042       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7043       .access = PL0_RW, .type = ARM_CP_SME,
7044       .fieldoffset = offsetof(CPUARMState, svcr),
7045       .writefn = svcr_write, .raw_writefn = raw_write },
7046     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7047       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7048       .access = PL1_RW, .type = ARM_CP_SME,
7049       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7050       .writefn = smcr_write, .raw_writefn = raw_write },
7051     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7052       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7053       .access = PL2_RW, .type = ARM_CP_SME,
7054       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7055       .writefn = smcr_write, .raw_writefn = raw_write },
7056     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7057       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7058       .access = PL3_RW, .type = ARM_CP_SME,
7059       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7060       .writefn = smcr_write, .raw_writefn = raw_write },
7061     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7062       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7063       .access = PL1_R, .accessfn = access_aa64_tid1,
7064       /*
7065        * IMPLEMENTOR = 0 (software)
7066        * REVISION    = 0 (implementation defined)
7067        * SMPS        = 0 (no streaming execution priority in QEMU)
7068        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
7069        */
7070       .type = ARM_CP_CONST, .resetvalue = 0, },
7071     /*
7072      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7073      */
7074     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7075       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7076       .access = PL1_RW, .accessfn = access_esm,
7077       .fgt = FGT_NSMPRI_EL1,
7078       .type = ARM_CP_CONST, .resetvalue = 0 },
7079     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7080       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7081       .access = PL2_RW, .accessfn = access_esm,
7082       .type = ARM_CP_CONST, .resetvalue = 0 },
7083 };
7084 
7085 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7086                                   uint64_t value)
7087 {
7088     CPUState *cs = env_cpu(env);
7089 
7090     tlb_flush(cs);
7091 }
7092 
7093 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7094                         uint64_t value)
7095 {
7096     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7097     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7098         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7099         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7100 
7101     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7102 }
7103 
7104 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7105 {
7106     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7107                                      env_archcpu(env)->reset_l0gptsz);
7108 }
7109 
7110 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7111                                     uint64_t value)
7112 {
7113     CPUState *cs = env_cpu(env);
7114 
7115     tlb_flush_all_cpus_synced(cs);
7116 }
7117 
7118 static const ARMCPRegInfo rme_reginfo[] = {
7119     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7120       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7121       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7122       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7123     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7124       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7125       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7126     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7127       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7128       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7129     { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7130       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7131       .access = PL3_W, .type = ARM_CP_NO_RAW,
7132       .writefn = tlbi_aa64_paall_write },
7133     { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7134       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7135       .access = PL3_W, .type = ARM_CP_NO_RAW,
7136       .writefn = tlbi_aa64_paallos_write },
7137     /*
7138      * QEMU does not have a way to invalidate by physical address, thus
7139      * invalidating a range of physical addresses is accomplished by
7140      * flushing all tlb entries in the outer shareable domain,
7141      * just like PAALLOS.
7142      */
7143     { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7144       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7145       .access = PL3_W, .type = ARM_CP_NO_RAW,
7146       .writefn = tlbi_aa64_paallos_write },
7147     { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7148       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7149       .access = PL3_W, .type = ARM_CP_NO_RAW,
7150       .writefn = tlbi_aa64_paallos_write },
7151     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7152       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7153       .access = PL3_W, .type = ARM_CP_NOP },
7154 };
7155 
7156 static const ARMCPRegInfo rme_mte_reginfo[] = {
7157     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7158       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7159       .access = PL3_W, .type = ARM_CP_NOP },
7160 };
7161 #endif /* TARGET_AARCH64 */
7162 
7163 static void define_pmu_regs(ARMCPU *cpu)
7164 {
7165     /*
7166      * v7 performance monitor control register: same implementor
7167      * field as main ID register, and we implement four counters in
7168      * addition to the cycle count register.
7169      */
7170     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
7171     ARMCPRegInfo pmcr = {
7172         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7173         .access = PL0_RW,
7174         .fgt = FGT_PMCR_EL0,
7175         .type = ARM_CP_IO | ARM_CP_ALIAS,
7176         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7177         .accessfn = pmreg_access,
7178         .readfn = pmcr_read, .raw_readfn = raw_read,
7179         .writefn = pmcr_write, .raw_writefn = raw_write,
7180     };
7181     ARMCPRegInfo pmcr64 = {
7182         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7183         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7184         .access = PL0_RW, .accessfn = pmreg_access,
7185         .fgt = FGT_PMCR_EL0,
7186         .type = ARM_CP_IO,
7187         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
7188         .resetvalue = cpu->isar.reset_pmcr_el0,
7189         .readfn = pmcr_read, .raw_readfn = raw_read,
7190         .writefn = pmcr_write, .raw_writefn = raw_write,
7191     };
7192 
7193     define_one_arm_cp_reg(cpu, &pmcr);
7194     define_one_arm_cp_reg(cpu, &pmcr64);
7195     for (i = 0; i < pmcrn; i++) {
7196         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7197         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7198         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7199         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7200         ARMCPRegInfo pmev_regs[] = {
7201             { .name = pmevcntr_name, .cp = 15, .crn = 14,
7202               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7203               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7204               .fgt = FGT_PMEVCNTRN_EL0,
7205               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7206               .accessfn = pmreg_access_xevcntr },
7207             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7208               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
7209               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
7210               .type = ARM_CP_IO,
7211               .fgt = FGT_PMEVCNTRN_EL0,
7212               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7213               .raw_readfn = pmevcntr_rawread,
7214               .raw_writefn = pmevcntr_rawwrite },
7215             { .name = pmevtyper_name, .cp = 15, .crn = 14,
7216               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7217               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7218               .fgt = FGT_PMEVTYPERN_EL0,
7219               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7220               .accessfn = pmreg_access },
7221             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7222               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7223               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
7224               .fgt = FGT_PMEVTYPERN_EL0,
7225               .type = ARM_CP_IO,
7226               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7227               .raw_writefn = pmevtyper_rawwrite },
7228         };
7229         define_arm_cp_regs(cpu, pmev_regs);
7230         g_free(pmevcntr_name);
7231         g_free(pmevcntr_el0_name);
7232         g_free(pmevtyper_name);
7233         g_free(pmevtyper_el0_name);
7234     }
7235     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
7236         ARMCPRegInfo v81_pmu_regs[] = {
7237             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7238               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7239               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7240               .fgt = FGT_PMCEIDN_EL0,
7241               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7242             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7243               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7244               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7245               .fgt = FGT_PMCEIDN_EL0,
7246               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
7247         };
7248         define_arm_cp_regs(cpu, v81_pmu_regs);
7249     }
7250     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
7251         static const ARMCPRegInfo v84_pmmir = {
7252             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7253             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7254             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7255             .fgt = FGT_PMMIR_EL1,
7256             .resetvalue = 0
7257         };
7258         define_one_arm_cp_reg(cpu, &v84_pmmir);
7259     }
7260 }
7261 
7262 #ifndef CONFIG_USER_ONLY
7263 /*
7264  * We don't know until after realize whether there's a GICv3
7265  * attached, and that is what registers the gicv3 sysregs.
7266  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7267  * at runtime.
7268  */
7269 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7270 {
7271     ARMCPU *cpu = env_archcpu(env);
7272     uint64_t pfr1 = cpu->isar.id_pfr1;
7273 
7274     if (env->gicv3state) {
7275         pfr1 |= 1 << 28;
7276     }
7277     return pfr1;
7278 }
7279 
7280 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7281 {
7282     ARMCPU *cpu = env_archcpu(env);
7283     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
7284 
7285     if (env->gicv3state) {
7286         pfr0 |= 1 << 24;
7287     }
7288     return pfr0;
7289 }
7290 #endif
7291 
7292 /*
7293  * Shared logic between LORID and the rest of the LOR* registers.
7294  * Secure state exclusion has already been dealt with.
7295  */
7296 static CPAccessResult access_lor_ns(CPUARMState *env,
7297                                     const ARMCPRegInfo *ri, bool isread)
7298 {
7299     int el = arm_current_el(env);
7300 
7301     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7302         return CP_ACCESS_TRAP_EL2;
7303     }
7304     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7305         return CP_ACCESS_TRAP_EL3;
7306     }
7307     return CP_ACCESS_OK;
7308 }
7309 
7310 static CPAccessResult access_lor_other(CPUARMState *env,
7311                                        const ARMCPRegInfo *ri, bool isread)
7312 {
7313     if (arm_is_secure_below_el3(env)) {
7314         /* Access denied in secure mode.  */
7315         return CP_ACCESS_TRAP;
7316     }
7317     return access_lor_ns(env, ri, isread);
7318 }
7319 
7320 /*
7321  * A trivial implementation of ARMv8.1-LOR leaves all of these
7322  * registers fixed at 0, which indicates that there are zero
7323  * supported Limited Ordering regions.
7324  */
7325 static const ARMCPRegInfo lor_reginfo[] = {
7326     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7327       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7328       .access = PL1_RW, .accessfn = access_lor_other,
7329       .fgt = FGT_LORSA_EL1,
7330       .type = ARM_CP_CONST, .resetvalue = 0 },
7331     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7332       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7333       .access = PL1_RW, .accessfn = access_lor_other,
7334       .fgt = FGT_LOREA_EL1,
7335       .type = ARM_CP_CONST, .resetvalue = 0 },
7336     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7337       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7338       .access = PL1_RW, .accessfn = access_lor_other,
7339       .fgt = FGT_LORN_EL1,
7340       .type = ARM_CP_CONST, .resetvalue = 0 },
7341     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7342       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7343       .access = PL1_RW, .accessfn = access_lor_other,
7344       .fgt = FGT_LORC_EL1,
7345       .type = ARM_CP_CONST, .resetvalue = 0 },
7346     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7347       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7348       .access = PL1_R, .accessfn = access_lor_ns,
7349       .fgt = FGT_LORID_EL1,
7350       .type = ARM_CP_CONST, .resetvalue = 0 },
7351 };
7352 
7353 #ifdef TARGET_AARCH64
7354 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7355                                    bool isread)
7356 {
7357     int el = arm_current_el(env);
7358 
7359     if (el < 2 &&
7360         arm_is_el2_enabled(env) &&
7361         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7362         return CP_ACCESS_TRAP_EL2;
7363     }
7364     if (el < 3 &&
7365         arm_feature(env, ARM_FEATURE_EL3) &&
7366         !(env->cp15.scr_el3 & SCR_APK)) {
7367         return CP_ACCESS_TRAP_EL3;
7368     }
7369     return CP_ACCESS_OK;
7370 }
7371 
7372 static const ARMCPRegInfo pauth_reginfo[] = {
7373     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7374       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7375       .access = PL1_RW, .accessfn = access_pauth,
7376       .fgt = FGT_APDAKEY,
7377       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7378     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7379       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7380       .access = PL1_RW, .accessfn = access_pauth,
7381       .fgt = FGT_APDAKEY,
7382       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7383     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7384       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7385       .access = PL1_RW, .accessfn = access_pauth,
7386       .fgt = FGT_APDBKEY,
7387       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7388     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7389       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7390       .access = PL1_RW, .accessfn = access_pauth,
7391       .fgt = FGT_APDBKEY,
7392       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7393     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7394       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7395       .access = PL1_RW, .accessfn = access_pauth,
7396       .fgt = FGT_APGAKEY,
7397       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7398     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7399       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7400       .access = PL1_RW, .accessfn = access_pauth,
7401       .fgt = FGT_APGAKEY,
7402       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7403     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7404       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7405       .access = PL1_RW, .accessfn = access_pauth,
7406       .fgt = FGT_APIAKEY,
7407       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7408     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7409       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7410       .access = PL1_RW, .accessfn = access_pauth,
7411       .fgt = FGT_APIAKEY,
7412       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7413     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7414       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7415       .access = PL1_RW, .accessfn = access_pauth,
7416       .fgt = FGT_APIBKEY,
7417       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7418     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7419       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7420       .access = PL1_RW, .accessfn = access_pauth,
7421       .fgt = FGT_APIBKEY,
7422       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7423 };
7424 
7425 static const ARMCPRegInfo tlbirange_reginfo[] = {
7426     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7427       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7428       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7429       .fgt = FGT_TLBIRVAE1IS,
7430       .writefn = tlbi_aa64_rvae1is_write },
7431     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7432       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7433       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7434       .fgt = FGT_TLBIRVAAE1IS,
7435       .writefn = tlbi_aa64_rvae1is_write },
7436    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7437       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7438       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7439       .fgt = FGT_TLBIRVALE1IS,
7440       .writefn = tlbi_aa64_rvae1is_write },
7441     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7442       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7443       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7444       .fgt = FGT_TLBIRVAALE1IS,
7445       .writefn = tlbi_aa64_rvae1is_write },
7446     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7447       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7448       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7449       .fgt = FGT_TLBIRVAE1OS,
7450       .writefn = tlbi_aa64_rvae1is_write },
7451     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7452       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7453       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7454       .fgt = FGT_TLBIRVAAE1OS,
7455       .writefn = tlbi_aa64_rvae1is_write },
7456    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7457       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7458       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7459       .fgt = FGT_TLBIRVALE1OS,
7460       .writefn = tlbi_aa64_rvae1is_write },
7461     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7462       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7463       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7464       .fgt = FGT_TLBIRVAALE1OS,
7465       .writefn = tlbi_aa64_rvae1is_write },
7466     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7467       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7468       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7469       .fgt = FGT_TLBIRVAE1,
7470       .writefn = tlbi_aa64_rvae1_write },
7471     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7472       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7473       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7474       .fgt = FGT_TLBIRVAAE1,
7475       .writefn = tlbi_aa64_rvae1_write },
7476    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7477       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7478       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7479       .fgt = FGT_TLBIRVALE1,
7480       .writefn = tlbi_aa64_rvae1_write },
7481     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7482       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7483       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7484       .fgt = FGT_TLBIRVAALE1,
7485       .writefn = tlbi_aa64_rvae1_write },
7486     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7487       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7488       .access = PL2_W, .type = ARM_CP_NO_RAW,
7489       .writefn = tlbi_aa64_ripas2e1is_write },
7490     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7491       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7492       .access = PL2_W, .type = ARM_CP_NO_RAW,
7493       .writefn = tlbi_aa64_ripas2e1is_write },
7494     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7495       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7496       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7497       .writefn = tlbi_aa64_rvae2is_write },
7498    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7499       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7500       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7501       .writefn = tlbi_aa64_rvae2is_write },
7502     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7503       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7504       .access = PL2_W, .type = ARM_CP_NO_RAW,
7505       .writefn = tlbi_aa64_ripas2e1_write },
7506     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7507       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7508       .access = PL2_W, .type = ARM_CP_NO_RAW,
7509       .writefn = tlbi_aa64_ripas2e1_write },
7510    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7511       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7512       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7513       .writefn = tlbi_aa64_rvae2is_write },
7514    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7515       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7516       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7517       .writefn = tlbi_aa64_rvae2is_write },
7518     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7519       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7520       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7521       .writefn = tlbi_aa64_rvae2_write },
7522    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7523       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7524       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7525       .writefn = tlbi_aa64_rvae2_write },
7526    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7527       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7528       .access = PL3_W, .type = ARM_CP_NO_RAW,
7529       .writefn = tlbi_aa64_rvae3is_write },
7530    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7531       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7532       .access = PL3_W, .type = ARM_CP_NO_RAW,
7533       .writefn = tlbi_aa64_rvae3is_write },
7534    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7535       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7536       .access = PL3_W, .type = ARM_CP_NO_RAW,
7537       .writefn = tlbi_aa64_rvae3is_write },
7538    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7539       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7540       .access = PL3_W, .type = ARM_CP_NO_RAW,
7541       .writefn = tlbi_aa64_rvae3is_write },
7542    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7543       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7544       .access = PL3_W, .type = ARM_CP_NO_RAW,
7545       .writefn = tlbi_aa64_rvae3_write },
7546    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7547       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7548       .access = PL3_W, .type = ARM_CP_NO_RAW,
7549       .writefn = tlbi_aa64_rvae3_write },
7550 };
7551 
7552 static const ARMCPRegInfo tlbios_reginfo[] = {
7553     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7554       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7555       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7556       .fgt = FGT_TLBIVMALLE1OS,
7557       .writefn = tlbi_aa64_vmalle1is_write },
7558     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7559       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7560       .fgt = FGT_TLBIVAE1OS,
7561       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7562       .writefn = tlbi_aa64_vae1is_write },
7563     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7564       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7565       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7566       .fgt = FGT_TLBIASIDE1OS,
7567       .writefn = tlbi_aa64_vmalle1is_write },
7568     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7569       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7570       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7571       .fgt = FGT_TLBIVAAE1OS,
7572       .writefn = tlbi_aa64_vae1is_write },
7573     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7574       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7575       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7576       .fgt = FGT_TLBIVALE1OS,
7577       .writefn = tlbi_aa64_vae1is_write },
7578     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7579       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7580       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7581       .fgt = FGT_TLBIVAALE1OS,
7582       .writefn = tlbi_aa64_vae1is_write },
7583     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7584       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7585       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7586       .writefn = tlbi_aa64_alle2is_write },
7587     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7588       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7589       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7590       .writefn = tlbi_aa64_vae2is_write },
7591    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7592       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7593       .access = PL2_W, .type = ARM_CP_NO_RAW,
7594       .writefn = tlbi_aa64_alle1is_write },
7595     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7596       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7597       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7598       .writefn = tlbi_aa64_vae2is_write },
7599     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7600       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7601       .access = PL2_W, .type = ARM_CP_NO_RAW,
7602       .writefn = tlbi_aa64_alle1is_write },
7603     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7604       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7605       .access = PL2_W, .type = ARM_CP_NOP },
7606     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7607       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7608       .access = PL2_W, .type = ARM_CP_NOP },
7609     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7610       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7611       .access = PL2_W, .type = ARM_CP_NOP },
7612     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7613       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7614       .access = PL2_W, .type = ARM_CP_NOP },
7615     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7616       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7617       .access = PL3_W, .type = ARM_CP_NO_RAW,
7618       .writefn = tlbi_aa64_alle3is_write },
7619     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7620       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7621       .access = PL3_W, .type = ARM_CP_NO_RAW,
7622       .writefn = tlbi_aa64_vae3is_write },
7623     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7624       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7625       .access = PL3_W, .type = ARM_CP_NO_RAW,
7626       .writefn = tlbi_aa64_vae3is_write },
7627 };
7628 
7629 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7630 {
7631     Error *err = NULL;
7632     uint64_t ret;
7633 
7634     /* Success sets NZCV = 0000.  */
7635     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7636 
7637     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7638         /*
7639          * ??? Failed, for unknown reasons in the crypto subsystem.
7640          * The best we can do is log the reason and return the
7641          * timed-out indication to the guest.  There is no reason
7642          * we know to expect this failure to be transitory, so the
7643          * guest may well hang retrying the operation.
7644          */
7645         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7646                       ri->name, error_get_pretty(err));
7647         error_free(err);
7648 
7649         env->ZF = 0; /* NZCF = 0100 */
7650         return 0;
7651     }
7652     return ret;
7653 }
7654 
7655 /* We do not support re-seeding, so the two registers operate the same.  */
7656 static const ARMCPRegInfo rndr_reginfo[] = {
7657     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7658       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7659       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7660       .access = PL0_R, .readfn = rndr_readfn },
7661     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7662       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7663       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7664       .access = PL0_R, .readfn = rndr_readfn },
7665 };
7666 
7667 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7668                           uint64_t value)
7669 {
7670 #ifdef CONFIG_TCG
7671     ARMCPU *cpu = env_archcpu(env);
7672     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7673     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7674     uint64_t vaddr_in = (uint64_t) value;
7675     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7676     void *haddr;
7677     int mem_idx = cpu_mmu_index(env, false);
7678 
7679     /* This won't be crossing page boundaries */
7680     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7681     if (haddr) {
7682 #ifndef CONFIG_USER_ONLY
7683 
7684         ram_addr_t offset;
7685         MemoryRegion *mr;
7686 
7687         /* RCU lock is already being held */
7688         mr = memory_region_from_host(haddr, &offset);
7689 
7690         if (mr) {
7691             memory_region_writeback(mr, offset, dline_size);
7692         }
7693 #endif /*CONFIG_USER_ONLY*/
7694     }
7695 #else
7696     /* Handled by hardware accelerator. */
7697     g_assert_not_reached();
7698 #endif /* CONFIG_TCG */
7699 }
7700 
7701 static const ARMCPRegInfo dcpop_reg[] = {
7702     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7703       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7704       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7705       .fgt = FGT_DCCVAP,
7706       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7707 };
7708 
7709 static const ARMCPRegInfo dcpodp_reg[] = {
7710     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7711       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7712       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7713       .fgt = FGT_DCCVADP,
7714       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7715 };
7716 
7717 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7718                                        bool isread)
7719 {
7720     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7721         return CP_ACCESS_TRAP_EL2;
7722     }
7723 
7724     return CP_ACCESS_OK;
7725 }
7726 
7727 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7728                                  bool isread)
7729 {
7730     int el = arm_current_el(env);
7731 
7732     if (el < 2 && arm_is_el2_enabled(env)) {
7733         uint64_t hcr = arm_hcr_el2_eff(env);
7734         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7735             return CP_ACCESS_TRAP_EL2;
7736         }
7737     }
7738     if (el < 3 &&
7739         arm_feature(env, ARM_FEATURE_EL3) &&
7740         !(env->cp15.scr_el3 & SCR_ATA)) {
7741         return CP_ACCESS_TRAP_EL3;
7742     }
7743     return CP_ACCESS_OK;
7744 }
7745 
7746 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7747 {
7748     return env->pstate & PSTATE_TCO;
7749 }
7750 
7751 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7752 {
7753     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7754 }
7755 
7756 static const ARMCPRegInfo mte_reginfo[] = {
7757     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7758       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7759       .access = PL1_RW, .accessfn = access_mte,
7760       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7761     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7762       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7763       .access = PL1_RW, .accessfn = access_mte,
7764       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7765     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7766       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7767       .access = PL2_RW, .accessfn = access_mte,
7768       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7769     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7770       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7771       .access = PL3_RW,
7772       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7773     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7774       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7775       .access = PL1_RW, .accessfn = access_mte,
7776       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7777     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7778       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7779       .access = PL1_RW, .accessfn = access_mte,
7780       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7781     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7782       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7783       .type = ARM_CP_NO_RAW,
7784       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7785     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7786       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7787       .type = ARM_CP_NOP, .access = PL1_W,
7788       .fgt = FGT_DCIVAC,
7789       .accessfn = aa64_cacheop_poc_access },
7790     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7791       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7792       .fgt = FGT_DCISW,
7793       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7794     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7795       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7796       .type = ARM_CP_NOP, .access = PL1_W,
7797       .fgt = FGT_DCIVAC,
7798       .accessfn = aa64_cacheop_poc_access },
7799     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7800       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7801       .fgt = FGT_DCISW,
7802       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7803     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7804       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7805       .fgt = FGT_DCCSW,
7806       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7807     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7808       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7809       .fgt = FGT_DCCSW,
7810       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7811     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7812       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7813       .fgt = FGT_DCCISW,
7814       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7815     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7816       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7817       .fgt = FGT_DCCISW,
7818       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7819 };
7820 
7821 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7822     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7823       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7824       .type = ARM_CP_CONST, .access = PL0_RW, },
7825 };
7826 
7827 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7828     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7829       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7830       .type = ARM_CP_NOP, .access = PL0_W,
7831       .fgt = FGT_DCCVAC,
7832       .accessfn = aa64_cacheop_poc_access },
7833     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7834       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7835       .type = ARM_CP_NOP, .access = PL0_W,
7836       .fgt = FGT_DCCVAC,
7837       .accessfn = aa64_cacheop_poc_access },
7838     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7839       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7840       .type = ARM_CP_NOP, .access = PL0_W,
7841       .fgt = FGT_DCCVAP,
7842       .accessfn = aa64_cacheop_poc_access },
7843     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7844       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7845       .type = ARM_CP_NOP, .access = PL0_W,
7846       .fgt = FGT_DCCVAP,
7847       .accessfn = aa64_cacheop_poc_access },
7848     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7849       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7850       .type = ARM_CP_NOP, .access = PL0_W,
7851       .fgt = FGT_DCCVADP,
7852       .accessfn = aa64_cacheop_poc_access },
7853     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7854       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7855       .type = ARM_CP_NOP, .access = PL0_W,
7856       .fgt = FGT_DCCVADP,
7857       .accessfn = aa64_cacheop_poc_access },
7858     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7859       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7860       .type = ARM_CP_NOP, .access = PL0_W,
7861       .fgt = FGT_DCCIVAC,
7862       .accessfn = aa64_cacheop_poc_access },
7863     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7864       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7865       .type = ARM_CP_NOP, .access = PL0_W,
7866       .fgt = FGT_DCCIVAC,
7867       .accessfn = aa64_cacheop_poc_access },
7868     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7869       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7870       .access = PL0_W, .type = ARM_CP_DC_GVA,
7871 #ifndef CONFIG_USER_ONLY
7872       /* Avoid overhead of an access check that always passes in user-mode */
7873       .accessfn = aa64_zva_access,
7874       .fgt = FGT_DCZVA,
7875 #endif
7876     },
7877     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7878       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7879       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7880 #ifndef CONFIG_USER_ONLY
7881       /* Avoid overhead of an access check that always passes in user-mode */
7882       .accessfn = aa64_zva_access,
7883       .fgt = FGT_DCZVA,
7884 #endif
7885     },
7886 };
7887 
7888 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7889                                      bool isread)
7890 {
7891     uint64_t hcr = arm_hcr_el2_eff(env);
7892     int el = arm_current_el(env);
7893 
7894     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7895         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7896             if (hcr & HCR_TGE) {
7897                 return CP_ACCESS_TRAP_EL2;
7898             }
7899             return CP_ACCESS_TRAP;
7900         }
7901     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7902         return CP_ACCESS_TRAP_EL2;
7903     }
7904     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7905         return CP_ACCESS_TRAP_EL2;
7906     }
7907     if (el < 3
7908         && arm_feature(env, ARM_FEATURE_EL3)
7909         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7910         return CP_ACCESS_TRAP_EL3;
7911     }
7912     return CP_ACCESS_OK;
7913 }
7914 
7915 static const ARMCPRegInfo scxtnum_reginfo[] = {
7916     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7917       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7918       .access = PL0_RW, .accessfn = access_scxtnum,
7919       .fgt = FGT_SCXTNUM_EL0,
7920       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7921     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7922       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7923       .access = PL1_RW, .accessfn = access_scxtnum,
7924       .fgt = FGT_SCXTNUM_EL1,
7925       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7926     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7927       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7928       .access = PL2_RW, .accessfn = access_scxtnum,
7929       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7930     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7931       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7932       .access = PL3_RW,
7933       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7934 };
7935 
7936 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7937                                  bool isread)
7938 {
7939     if (arm_current_el(env) == 2 &&
7940         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7941         return CP_ACCESS_TRAP_EL3;
7942     }
7943     return CP_ACCESS_OK;
7944 }
7945 
7946 static const ARMCPRegInfo fgt_reginfo[] = {
7947     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7948       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7949       .access = PL2_RW, .accessfn = access_fgt,
7950       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7951     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7952       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7953       .access = PL2_RW, .accessfn = access_fgt,
7954       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7955     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7956       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7957       .access = PL2_RW, .accessfn = access_fgt,
7958       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7959     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7960       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7961       .access = PL2_RW, .accessfn = access_fgt,
7962       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7963     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7964       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7965       .access = PL2_RW, .accessfn = access_fgt,
7966       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7967 };
7968 #endif /* TARGET_AARCH64 */
7969 
7970 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7971                                      bool isread)
7972 {
7973     int el = arm_current_el(env);
7974 
7975     if (el == 0) {
7976         uint64_t sctlr = arm_sctlr(env, el);
7977         if (!(sctlr & SCTLR_EnRCTX)) {
7978             return CP_ACCESS_TRAP;
7979         }
7980     } else if (el == 1) {
7981         uint64_t hcr = arm_hcr_el2_eff(env);
7982         if (hcr & HCR_NV) {
7983             return CP_ACCESS_TRAP_EL2;
7984         }
7985     }
7986     return CP_ACCESS_OK;
7987 }
7988 
7989 static const ARMCPRegInfo predinv_reginfo[] = {
7990     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7991       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7992       .fgt = FGT_CFPRCTX,
7993       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7994     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7995       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7996       .fgt = FGT_DVPRCTX,
7997       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7998     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7999       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
8000       .fgt = FGT_CPPRCTX,
8001       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8002     /*
8003      * Note the AArch32 opcodes have a different OPC1.
8004      */
8005     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
8006       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
8007       .fgt = FGT_CFPRCTX,
8008       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8009     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
8010       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
8011       .fgt = FGT_DVPRCTX,
8012       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8013     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
8014       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
8015       .fgt = FGT_CPPRCTX,
8016       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8017 };
8018 
8019 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
8020 {
8021     /* Read the high 32 bits of the current CCSIDR */
8022     return extract64(ccsidr_read(env, ri), 32, 32);
8023 }
8024 
8025 static const ARMCPRegInfo ccsidr2_reginfo[] = {
8026     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
8027       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
8028       .access = PL1_R,
8029       .accessfn = access_tid4,
8030       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
8031 };
8032 
8033 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8034                                        bool isread)
8035 {
8036     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
8037         return CP_ACCESS_TRAP_EL2;
8038     }
8039 
8040     return CP_ACCESS_OK;
8041 }
8042 
8043 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8044                                        bool isread)
8045 {
8046     if (arm_feature(env, ARM_FEATURE_V8)) {
8047         return access_aa64_tid3(env, ri, isread);
8048     }
8049 
8050     return CP_ACCESS_OK;
8051 }
8052 
8053 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8054                                      bool isread)
8055 {
8056     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8057         return CP_ACCESS_TRAP_EL2;
8058     }
8059 
8060     return CP_ACCESS_OK;
8061 }
8062 
8063 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8064                                         const ARMCPRegInfo *ri, bool isread)
8065 {
8066     /*
8067      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8068      * in v7A, not in v8A.
8069      */
8070     if (!arm_feature(env, ARM_FEATURE_V8) &&
8071         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8072         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8073         return CP_ACCESS_TRAP_EL2;
8074     }
8075     return CP_ACCESS_OK;
8076 }
8077 
8078 static const ARMCPRegInfo jazelle_regs[] = {
8079     { .name = "JIDR",
8080       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8081       .access = PL1_R, .accessfn = access_jazelle,
8082       .type = ARM_CP_CONST, .resetvalue = 0 },
8083     { .name = "JOSCR",
8084       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8085       .accessfn = access_joscr_jmcr,
8086       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8087     { .name = "JMCR",
8088       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8089       .accessfn = access_joscr_jmcr,
8090       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8091 };
8092 
8093 static const ARMCPRegInfo contextidr_el2 = {
8094     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8095     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8096     .access = PL2_RW,
8097     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8098 };
8099 
8100 static const ARMCPRegInfo vhe_reginfo[] = {
8101     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8102       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8103       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
8104       .raw_writefn = raw_write,
8105       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8106 #ifndef CONFIG_USER_ONLY
8107     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8108       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8109       .fieldoffset =
8110         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8111       .type = ARM_CP_IO, .access = PL2_RW,
8112       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8113     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8114       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8115       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8116       .resetfn = gt_hv_timer_reset,
8117       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8118     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8119       .type = ARM_CP_IO,
8120       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8121       .access = PL2_RW,
8122       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8123       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
8124     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8125       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8126       .type = ARM_CP_IO | ARM_CP_ALIAS,
8127       .access = PL2_RW, .accessfn = e2h_access,
8128       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8129       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8130     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8131       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8132       .type = ARM_CP_IO | ARM_CP_ALIAS,
8133       .access = PL2_RW, .accessfn = e2h_access,
8134       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8135       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8136     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8137       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8138       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8139       .access = PL2_RW, .accessfn = e2h_access,
8140       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8141     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8142       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8143       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8144       .access = PL2_RW, .accessfn = e2h_access,
8145       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8146     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8147       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8148       .type = ARM_CP_IO | ARM_CP_ALIAS,
8149       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8150       .access = PL2_RW, .accessfn = e2h_access,
8151       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8152     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8153       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8154       .type = ARM_CP_IO | ARM_CP_ALIAS,
8155       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8156       .access = PL2_RW, .accessfn = e2h_access,
8157       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8158 #endif
8159 };
8160 
8161 #ifndef CONFIG_USER_ONLY
8162 static const ARMCPRegInfo ats1e1_reginfo[] = {
8163     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
8164       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8165       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8166       .fgt = FGT_ATS1E1RP,
8167       .accessfn = at_e012_access, .writefn = ats_write64 },
8168     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
8169       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8170       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8171       .fgt = FGT_ATS1E1WP,
8172       .accessfn = at_e012_access, .writefn = ats_write64 },
8173 };
8174 
8175 static const ARMCPRegInfo ats1cp_reginfo[] = {
8176     { .name = "ATS1CPRP",
8177       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8178       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8179       .writefn = ats_write },
8180     { .name = "ATS1CPWP",
8181       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8182       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8183       .writefn = ats_write },
8184 };
8185 #endif
8186 
8187 /*
8188  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8189  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8190  * is non-zero, which is never for ARMv7, optionally in ARMv8
8191  * and mandatorily for ARMv8.2 and up.
8192  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8193  * implementation is RAZ/WI we can ignore this detail, as we
8194  * do for ACTLR.
8195  */
8196 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8197     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8198       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
8199       .access = PL1_RW, .accessfn = access_tacr,
8200       .type = ARM_CP_CONST, .resetvalue = 0 },
8201     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8202       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8203       .access = PL2_RW, .type = ARM_CP_CONST,
8204       .resetvalue = 0 },
8205 };
8206 
8207 void register_cp_regs_for_features(ARMCPU *cpu)
8208 {
8209     /* Register all the coprocessor registers based on feature bits */
8210     CPUARMState *env = &cpu->env;
8211     if (arm_feature(env, ARM_FEATURE_M)) {
8212         /* M profile has no coprocessor registers */
8213         return;
8214     }
8215 
8216     define_arm_cp_regs(cpu, cp_reginfo);
8217     if (!arm_feature(env, ARM_FEATURE_V8)) {
8218         /*
8219          * Must go early as it is full of wildcards that may be
8220          * overridden by later definitions.
8221          */
8222         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8223     }
8224 
8225     if (arm_feature(env, ARM_FEATURE_V6)) {
8226         /* The ID registers all have impdef reset values */
8227         ARMCPRegInfo v6_idregs[] = {
8228             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8229               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8230               .access = PL1_R, .type = ARM_CP_CONST,
8231               .accessfn = access_aa32_tid3,
8232               .resetvalue = cpu->isar.id_pfr0 },
8233             /*
8234              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
8235              * the value of the GIC field until after we define these regs.
8236              */
8237             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8238               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
8239               .access = PL1_R, .type = ARM_CP_NO_RAW,
8240               .accessfn = access_aa32_tid3,
8241 #ifdef CONFIG_USER_ONLY
8242               .type = ARM_CP_CONST,
8243               .resetvalue = cpu->isar.id_pfr1,
8244 #else
8245               .type = ARM_CP_NO_RAW,
8246               .accessfn = access_aa32_tid3,
8247               .readfn = id_pfr1_read,
8248               .writefn = arm_cp_write_ignore
8249 #endif
8250             },
8251             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8252               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8253               .access = PL1_R, .type = ARM_CP_CONST,
8254               .accessfn = access_aa32_tid3,
8255               .resetvalue = cpu->isar.id_dfr0 },
8256             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8257               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8258               .access = PL1_R, .type = ARM_CP_CONST,
8259               .accessfn = access_aa32_tid3,
8260               .resetvalue = cpu->id_afr0 },
8261             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8262               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8263               .access = PL1_R, .type = ARM_CP_CONST,
8264               .accessfn = access_aa32_tid3,
8265               .resetvalue = cpu->isar.id_mmfr0 },
8266             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8267               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8268               .access = PL1_R, .type = ARM_CP_CONST,
8269               .accessfn = access_aa32_tid3,
8270               .resetvalue = cpu->isar.id_mmfr1 },
8271             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8272               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8273               .access = PL1_R, .type = ARM_CP_CONST,
8274               .accessfn = access_aa32_tid3,
8275               .resetvalue = cpu->isar.id_mmfr2 },
8276             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8277               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8278               .access = PL1_R, .type = ARM_CP_CONST,
8279               .accessfn = access_aa32_tid3,
8280               .resetvalue = cpu->isar.id_mmfr3 },
8281             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8282               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8283               .access = PL1_R, .type = ARM_CP_CONST,
8284               .accessfn = access_aa32_tid3,
8285               .resetvalue = cpu->isar.id_isar0 },
8286             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8287               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8288               .access = PL1_R, .type = ARM_CP_CONST,
8289               .accessfn = access_aa32_tid3,
8290               .resetvalue = cpu->isar.id_isar1 },
8291             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8292               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8293               .access = PL1_R, .type = ARM_CP_CONST,
8294               .accessfn = access_aa32_tid3,
8295               .resetvalue = cpu->isar.id_isar2 },
8296             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8297               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8298               .access = PL1_R, .type = ARM_CP_CONST,
8299               .accessfn = access_aa32_tid3,
8300               .resetvalue = cpu->isar.id_isar3 },
8301             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8302               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8303               .access = PL1_R, .type = ARM_CP_CONST,
8304               .accessfn = access_aa32_tid3,
8305               .resetvalue = cpu->isar.id_isar4 },
8306             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8307               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8308               .access = PL1_R, .type = ARM_CP_CONST,
8309               .accessfn = access_aa32_tid3,
8310               .resetvalue = cpu->isar.id_isar5 },
8311             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8312               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8313               .access = PL1_R, .type = ARM_CP_CONST,
8314               .accessfn = access_aa32_tid3,
8315               .resetvalue = cpu->isar.id_mmfr4 },
8316             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
8317               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8318               .access = PL1_R, .type = ARM_CP_CONST,
8319               .accessfn = access_aa32_tid3,
8320               .resetvalue = cpu->isar.id_isar6 },
8321         };
8322         define_arm_cp_regs(cpu, v6_idregs);
8323         define_arm_cp_regs(cpu, v6_cp_reginfo);
8324     } else {
8325         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8326     }
8327     if (arm_feature(env, ARM_FEATURE_V6K)) {
8328         define_arm_cp_regs(cpu, v6k_cp_reginfo);
8329     }
8330     if (arm_feature(env, ARM_FEATURE_V7MP) &&
8331         !arm_feature(env, ARM_FEATURE_PMSA)) {
8332         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8333     }
8334     if (arm_feature(env, ARM_FEATURE_V7VE)) {
8335         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8336     }
8337     if (arm_feature(env, ARM_FEATURE_V7)) {
8338         ARMCPRegInfo clidr = {
8339             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8340             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
8341             .access = PL1_R, .type = ARM_CP_CONST,
8342             .accessfn = access_tid4,
8343             .fgt = FGT_CLIDR_EL1,
8344             .resetvalue = cpu->clidr
8345         };
8346         define_one_arm_cp_reg(cpu, &clidr);
8347         define_arm_cp_regs(cpu, v7_cp_reginfo);
8348         define_debug_regs(cpu);
8349         define_pmu_regs(cpu);
8350     } else {
8351         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
8352     }
8353     if (arm_feature(env, ARM_FEATURE_V8)) {
8354         /*
8355          * v8 ID registers, which all have impdef reset values.
8356          * Note that within the ID register ranges the unused slots
8357          * must all RAZ, not UNDEF; future architecture versions may
8358          * define new registers here.
8359          * ID registers which are AArch64 views of the AArch32 ID registers
8360          * which already existed in v6 and v7 are handled elsewhere,
8361          * in v6_idregs[].
8362          */
8363         int i;
8364         ARMCPRegInfo v8_idregs[] = {
8365             /*
8366              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8367              * emulation because we don't know the right value for the
8368              * GIC field until after we define these regs.
8369              */
8370             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8371               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
8372               .access = PL1_R,
8373 #ifdef CONFIG_USER_ONLY
8374               .type = ARM_CP_CONST,
8375               .resetvalue = cpu->isar.id_aa64pfr0
8376 #else
8377               .type = ARM_CP_NO_RAW,
8378               .accessfn = access_aa64_tid3,
8379               .readfn = id_aa64pfr0_read,
8380               .writefn = arm_cp_write_ignore
8381 #endif
8382             },
8383             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8384               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8385               .access = PL1_R, .type = ARM_CP_CONST,
8386               .accessfn = access_aa64_tid3,
8387               .resetvalue = cpu->isar.id_aa64pfr1},
8388             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8389               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8390               .access = PL1_R, .type = ARM_CP_CONST,
8391               .accessfn = access_aa64_tid3,
8392               .resetvalue = 0 },
8393             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8394               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8395               .access = PL1_R, .type = ARM_CP_CONST,
8396               .accessfn = access_aa64_tid3,
8397               .resetvalue = 0 },
8398             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
8399               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8400               .access = PL1_R, .type = ARM_CP_CONST,
8401               .accessfn = access_aa64_tid3,
8402               .resetvalue = cpu->isar.id_aa64zfr0 },
8403             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
8404               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8405               .access = PL1_R, .type = ARM_CP_CONST,
8406               .accessfn = access_aa64_tid3,
8407               .resetvalue = cpu->isar.id_aa64smfr0 },
8408             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8409               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8410               .access = PL1_R, .type = ARM_CP_CONST,
8411               .accessfn = access_aa64_tid3,
8412               .resetvalue = 0 },
8413             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8414               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8415               .access = PL1_R, .type = ARM_CP_CONST,
8416               .accessfn = access_aa64_tid3,
8417               .resetvalue = 0 },
8418             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8419               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8420               .access = PL1_R, .type = ARM_CP_CONST,
8421               .accessfn = access_aa64_tid3,
8422               .resetvalue = cpu->isar.id_aa64dfr0 },
8423             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8424               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8425               .access = PL1_R, .type = ARM_CP_CONST,
8426               .accessfn = access_aa64_tid3,
8427               .resetvalue = cpu->isar.id_aa64dfr1 },
8428             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8429               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8430               .access = PL1_R, .type = ARM_CP_CONST,
8431               .accessfn = access_aa64_tid3,
8432               .resetvalue = 0 },
8433             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8434               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8435               .access = PL1_R, .type = ARM_CP_CONST,
8436               .accessfn = access_aa64_tid3,
8437               .resetvalue = 0 },
8438             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8439               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8440               .access = PL1_R, .type = ARM_CP_CONST,
8441               .accessfn = access_aa64_tid3,
8442               .resetvalue = cpu->id_aa64afr0 },
8443             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8444               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8445               .access = PL1_R, .type = ARM_CP_CONST,
8446               .accessfn = access_aa64_tid3,
8447               .resetvalue = cpu->id_aa64afr1 },
8448             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8449               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8450               .access = PL1_R, .type = ARM_CP_CONST,
8451               .accessfn = access_aa64_tid3,
8452               .resetvalue = 0 },
8453             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8454               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8455               .access = PL1_R, .type = ARM_CP_CONST,
8456               .accessfn = access_aa64_tid3,
8457               .resetvalue = 0 },
8458             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8459               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8460               .access = PL1_R, .type = ARM_CP_CONST,
8461               .accessfn = access_aa64_tid3,
8462               .resetvalue = cpu->isar.id_aa64isar0 },
8463             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8464               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8465               .access = PL1_R, .type = ARM_CP_CONST,
8466               .accessfn = access_aa64_tid3,
8467               .resetvalue = cpu->isar.id_aa64isar1 },
8468             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8469               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8470               .access = PL1_R, .type = ARM_CP_CONST,
8471               .accessfn = access_aa64_tid3,
8472               .resetvalue = cpu->isar.id_aa64isar2 },
8473             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8474               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8475               .access = PL1_R, .type = ARM_CP_CONST,
8476               .accessfn = access_aa64_tid3,
8477               .resetvalue = 0 },
8478             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8479               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8480               .access = PL1_R, .type = ARM_CP_CONST,
8481               .accessfn = access_aa64_tid3,
8482               .resetvalue = 0 },
8483             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8484               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8485               .access = PL1_R, .type = ARM_CP_CONST,
8486               .accessfn = access_aa64_tid3,
8487               .resetvalue = 0 },
8488             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8489               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8490               .access = PL1_R, .type = ARM_CP_CONST,
8491               .accessfn = access_aa64_tid3,
8492               .resetvalue = 0 },
8493             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8494               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8495               .access = PL1_R, .type = ARM_CP_CONST,
8496               .accessfn = access_aa64_tid3,
8497               .resetvalue = 0 },
8498             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8499               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8500               .access = PL1_R, .type = ARM_CP_CONST,
8501               .accessfn = access_aa64_tid3,
8502               .resetvalue = cpu->isar.id_aa64mmfr0 },
8503             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8504               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8505               .access = PL1_R, .type = ARM_CP_CONST,
8506               .accessfn = access_aa64_tid3,
8507               .resetvalue = cpu->isar.id_aa64mmfr1 },
8508             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8509               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8510               .access = PL1_R, .type = ARM_CP_CONST,
8511               .accessfn = access_aa64_tid3,
8512               .resetvalue = cpu->isar.id_aa64mmfr2 },
8513             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8514               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8515               .access = PL1_R, .type = ARM_CP_CONST,
8516               .accessfn = access_aa64_tid3,
8517               .resetvalue = 0 },
8518             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8519               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8520               .access = PL1_R, .type = ARM_CP_CONST,
8521               .accessfn = access_aa64_tid3,
8522               .resetvalue = 0 },
8523             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8524               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8525               .access = PL1_R, .type = ARM_CP_CONST,
8526               .accessfn = access_aa64_tid3,
8527               .resetvalue = 0 },
8528             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8529               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8530               .access = PL1_R, .type = ARM_CP_CONST,
8531               .accessfn = access_aa64_tid3,
8532               .resetvalue = 0 },
8533             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8534               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8535               .access = PL1_R, .type = ARM_CP_CONST,
8536               .accessfn = access_aa64_tid3,
8537               .resetvalue = 0 },
8538             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8539               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8540               .access = PL1_R, .type = ARM_CP_CONST,
8541               .accessfn = access_aa64_tid3,
8542               .resetvalue = cpu->isar.mvfr0 },
8543             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8544               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8545               .access = PL1_R, .type = ARM_CP_CONST,
8546               .accessfn = access_aa64_tid3,
8547               .resetvalue = cpu->isar.mvfr1 },
8548             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8549               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8550               .access = PL1_R, .type = ARM_CP_CONST,
8551               .accessfn = access_aa64_tid3,
8552               .resetvalue = cpu->isar.mvfr2 },
8553             /*
8554              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8555              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8556              * as RAZ, since it is in the "reserved for future ID
8557              * registers, RAZ" part of the AArch32 encoding space.
8558              */
8559             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8560               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8561               .access = PL1_R, .type = ARM_CP_CONST,
8562               .accessfn = access_aa64_tid3,
8563               .resetvalue = 0 },
8564             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8565               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8566               .access = PL1_R, .type = ARM_CP_CONST,
8567               .accessfn = access_aa64_tid3,
8568               .resetvalue = 0 },
8569             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8570               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8571               .access = PL1_R, .type = ARM_CP_CONST,
8572               .accessfn = access_aa64_tid3,
8573               .resetvalue = 0 },
8574             /*
8575              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8576              * they're also RAZ for AArch64, and in v8 are gradually
8577              * being filled with AArch64-view-of-AArch32-ID-register
8578              * for new ID registers.
8579              */
8580             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8581               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8582               .access = PL1_R, .type = ARM_CP_CONST,
8583               .accessfn = access_aa64_tid3,
8584               .resetvalue = 0 },
8585             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8586               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8587               .access = PL1_R, .type = ARM_CP_CONST,
8588               .accessfn = access_aa64_tid3,
8589               .resetvalue = cpu->isar.id_pfr2 },
8590             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8591               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8592               .access = PL1_R, .type = ARM_CP_CONST,
8593               .accessfn = access_aa64_tid3,
8594               .resetvalue = cpu->isar.id_dfr1 },
8595             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8596               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8597               .access = PL1_R, .type = ARM_CP_CONST,
8598               .accessfn = access_aa64_tid3,
8599               .resetvalue = cpu->isar.id_mmfr5 },
8600             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8601               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8602               .access = PL1_R, .type = ARM_CP_CONST,
8603               .accessfn = access_aa64_tid3,
8604               .resetvalue = 0 },
8605             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8606               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8607               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8608               .fgt = FGT_PMCEIDN_EL0,
8609               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8610             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8611               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8612               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8613               .fgt = FGT_PMCEIDN_EL0,
8614               .resetvalue = cpu->pmceid0 },
8615             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8616               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8617               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8618               .fgt = FGT_PMCEIDN_EL0,
8619               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8620             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8621               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8622               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8623               .fgt = FGT_PMCEIDN_EL0,
8624               .resetvalue = cpu->pmceid1 },
8625         };
8626 #ifdef CONFIG_USER_ONLY
8627         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8628             { .name = "ID_AA64PFR0_EL1",
8629               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8630                                R_ID_AA64PFR0_ADVSIMD_MASK |
8631                                R_ID_AA64PFR0_SVE_MASK |
8632                                R_ID_AA64PFR0_DIT_MASK,
8633               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8634                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8635             { .name = "ID_AA64PFR1_EL1",
8636               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8637                                R_ID_AA64PFR1_SSBS_MASK |
8638                                R_ID_AA64PFR1_MTE_MASK |
8639                                R_ID_AA64PFR1_SME_MASK },
8640             { .name = "ID_AA64PFR*_EL1_RESERVED",
8641               .is_glob = true },
8642             { .name = "ID_AA64ZFR0_EL1",
8643               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8644                                R_ID_AA64ZFR0_AES_MASK |
8645                                R_ID_AA64ZFR0_BITPERM_MASK |
8646                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8647                                R_ID_AA64ZFR0_SHA3_MASK |
8648                                R_ID_AA64ZFR0_SM4_MASK |
8649                                R_ID_AA64ZFR0_I8MM_MASK |
8650                                R_ID_AA64ZFR0_F32MM_MASK |
8651                                R_ID_AA64ZFR0_F64MM_MASK },
8652             { .name = "ID_AA64SMFR0_EL1",
8653               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8654                                R_ID_AA64SMFR0_BI32I32_MASK |
8655                                R_ID_AA64SMFR0_B16F32_MASK |
8656                                R_ID_AA64SMFR0_F16F32_MASK |
8657                                R_ID_AA64SMFR0_I8I32_MASK |
8658                                R_ID_AA64SMFR0_F16F16_MASK |
8659                                R_ID_AA64SMFR0_B16B16_MASK |
8660                                R_ID_AA64SMFR0_I16I32_MASK |
8661                                R_ID_AA64SMFR0_F64F64_MASK |
8662                                R_ID_AA64SMFR0_I16I64_MASK |
8663                                R_ID_AA64SMFR0_SMEVER_MASK |
8664                                R_ID_AA64SMFR0_FA64_MASK },
8665             { .name = "ID_AA64MMFR0_EL1",
8666               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8667               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8668                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8669             { .name = "ID_AA64MMFR1_EL1",
8670               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8671             { .name = "ID_AA64MMFR2_EL1",
8672               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8673             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8674               .is_glob = true },
8675             { .name = "ID_AA64DFR0_EL1",
8676               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8677             { .name = "ID_AA64DFR1_EL1" },
8678             { .name = "ID_AA64DFR*_EL1_RESERVED",
8679               .is_glob = true },
8680             { .name = "ID_AA64AFR*",
8681               .is_glob = true },
8682             { .name = "ID_AA64ISAR0_EL1",
8683               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8684                                R_ID_AA64ISAR0_SHA1_MASK |
8685                                R_ID_AA64ISAR0_SHA2_MASK |
8686                                R_ID_AA64ISAR0_CRC32_MASK |
8687                                R_ID_AA64ISAR0_ATOMIC_MASK |
8688                                R_ID_AA64ISAR0_RDM_MASK |
8689                                R_ID_AA64ISAR0_SHA3_MASK |
8690                                R_ID_AA64ISAR0_SM3_MASK |
8691                                R_ID_AA64ISAR0_SM4_MASK |
8692                                R_ID_AA64ISAR0_DP_MASK |
8693                                R_ID_AA64ISAR0_FHM_MASK |
8694                                R_ID_AA64ISAR0_TS_MASK |
8695                                R_ID_AA64ISAR0_RNDR_MASK },
8696             { .name = "ID_AA64ISAR1_EL1",
8697               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8698                                R_ID_AA64ISAR1_APA_MASK |
8699                                R_ID_AA64ISAR1_API_MASK |
8700                                R_ID_AA64ISAR1_JSCVT_MASK |
8701                                R_ID_AA64ISAR1_FCMA_MASK |
8702                                R_ID_AA64ISAR1_LRCPC_MASK |
8703                                R_ID_AA64ISAR1_GPA_MASK |
8704                                R_ID_AA64ISAR1_GPI_MASK |
8705                                R_ID_AA64ISAR1_FRINTTS_MASK |
8706                                R_ID_AA64ISAR1_SB_MASK |
8707                                R_ID_AA64ISAR1_BF16_MASK |
8708                                R_ID_AA64ISAR1_DGH_MASK |
8709                                R_ID_AA64ISAR1_I8MM_MASK },
8710             { .name = "ID_AA64ISAR2_EL1",
8711               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8712                                R_ID_AA64ISAR2_RPRES_MASK |
8713                                R_ID_AA64ISAR2_GPA3_MASK |
8714                                R_ID_AA64ISAR2_APA3_MASK |
8715                                R_ID_AA64ISAR2_MOPS_MASK |
8716                                R_ID_AA64ISAR2_BC_MASK |
8717                                R_ID_AA64ISAR2_RPRFM_MASK |
8718                                R_ID_AA64ISAR2_CSSC_MASK },
8719             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8720               .is_glob = true },
8721         };
8722         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8723 #endif
8724         /*
8725          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8726          * TODO: For RMR, a write with bit 1 set should do something with
8727          * cpu_reset(). In the meantime, "the bit is strictly a request",
8728          * so we are in spec just ignoring writes.
8729          */
8730         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8731             !arm_feature(env, ARM_FEATURE_EL2)) {
8732             ARMCPRegInfo el1_reset_regs[] = {
8733                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8734                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8735                   .access = PL1_R,
8736                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8737                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8738                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8739                   .access = PL1_RW, .type = ARM_CP_CONST,
8740                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8741             };
8742             define_arm_cp_regs(cpu, el1_reset_regs);
8743         }
8744         define_arm_cp_regs(cpu, v8_idregs);
8745         define_arm_cp_regs(cpu, v8_cp_reginfo);
8746         if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
8747             define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
8748         }
8749 
8750         for (i = 4; i < 16; i++) {
8751             /*
8752              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8753              * For pre-v8 cores there are RAZ patterns for these in
8754              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8755              * v8 extends the "must RAZ" part of the ID register space
8756              * to also cover c0, 0, c{8-15}, {0-7}.
8757              * These are STATE_AA32 because in the AArch64 sysreg space
8758              * c4-c7 is where the AArch64 ID registers live (and we've
8759              * already defined those in v8_idregs[]), and c8-c15 are not
8760              * "must RAZ" for AArch64.
8761              */
8762             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8763             ARMCPRegInfo v8_aa32_raz_idregs = {
8764                 .name = name,
8765                 .state = ARM_CP_STATE_AA32,
8766                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8767                 .access = PL1_R, .type = ARM_CP_CONST,
8768                 .accessfn = access_aa64_tid3,
8769                 .resetvalue = 0 };
8770             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8771         }
8772     }
8773 
8774     /*
8775      * Register the base EL2 cpregs.
8776      * Pre v8, these registers are implemented only as part of the
8777      * Virtualization Extensions (EL2 present).  Beginning with v8,
8778      * if EL2 is missing but EL3 is enabled, mostly these become
8779      * RES0 from EL3, with some specific exceptions.
8780      */
8781     if (arm_feature(env, ARM_FEATURE_EL2)
8782         || (arm_feature(env, ARM_FEATURE_EL3)
8783             && arm_feature(env, ARM_FEATURE_V8))) {
8784         uint64_t vmpidr_def = mpidr_read_val(env);
8785         ARMCPRegInfo vpidr_regs[] = {
8786             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8787               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8788               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8789               .resetvalue = cpu->midr,
8790               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8791               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8792             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8793               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8794               .access = PL2_RW, .resetvalue = cpu->midr,
8795               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8796               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8797             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8798               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8799               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8800               .resetvalue = vmpidr_def,
8801               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8802               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8803             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8804               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8805               .access = PL2_RW, .resetvalue = vmpidr_def,
8806               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8807               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8808         };
8809         /*
8810          * The only field of MDCR_EL2 that has a defined architectural reset
8811          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8812          */
8813         ARMCPRegInfo mdcr_el2 = {
8814             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8815             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8816             .writefn = mdcr_el2_write,
8817             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8818             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8819         };
8820         define_one_arm_cp_reg(cpu, &mdcr_el2);
8821         define_arm_cp_regs(cpu, vpidr_regs);
8822         define_arm_cp_regs(cpu, el2_cp_reginfo);
8823         if (arm_feature(env, ARM_FEATURE_V8)) {
8824             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8825         }
8826         if (cpu_isar_feature(aa64_sel2, cpu)) {
8827             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8828         }
8829         /*
8830          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8831          * See commentary near RMR_EL1.
8832          */
8833         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8834             static const ARMCPRegInfo el2_reset_regs[] = {
8835                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8836                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8837                   .access = PL2_R,
8838                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8839                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8840                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8841                   .access = PL2_R,
8842                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8843                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
8844                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
8845                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8846             };
8847             define_arm_cp_regs(cpu, el2_reset_regs);
8848         }
8849     }
8850 
8851     /* Register the base EL3 cpregs. */
8852     if (arm_feature(env, ARM_FEATURE_EL3)) {
8853         define_arm_cp_regs(cpu, el3_cp_reginfo);
8854         ARMCPRegInfo el3_regs[] = {
8855             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8856               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8857               .access = PL3_R,
8858               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
8859             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
8860               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
8861               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8862             { .name = "RMR", .state = ARM_CP_STATE_AA32,
8863               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8864               .access = PL3_RW, .type = ARM_CP_CONST,
8865               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
8866             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8867               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8868               .access = PL3_RW,
8869               .raw_writefn = raw_write, .writefn = sctlr_write,
8870               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8871               .resetvalue = cpu->reset_sctlr },
8872         };
8873 
8874         define_arm_cp_regs(cpu, el3_regs);
8875     }
8876     /*
8877      * The behaviour of NSACR is sufficiently various that we don't
8878      * try to describe it in a single reginfo:
8879      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8880      *     reads as constant 0xc00 from NS EL1 and NS EL2
8881      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8882      *  if v7 without EL3, register doesn't exist
8883      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8884      */
8885     if (arm_feature(env, ARM_FEATURE_EL3)) {
8886         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8887             static const ARMCPRegInfo nsacr = {
8888                 .name = "NSACR", .type = ARM_CP_CONST,
8889                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8890                 .access = PL1_RW, .accessfn = nsacr_access,
8891                 .resetvalue = 0xc00
8892             };
8893             define_one_arm_cp_reg(cpu, &nsacr);
8894         } else {
8895             static const ARMCPRegInfo nsacr = {
8896                 .name = "NSACR",
8897                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8898                 .access = PL3_RW | PL1_R,
8899                 .resetvalue = 0,
8900                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8901             };
8902             define_one_arm_cp_reg(cpu, &nsacr);
8903         }
8904     } else {
8905         if (arm_feature(env, ARM_FEATURE_V8)) {
8906             static const ARMCPRegInfo nsacr = {
8907                 .name = "NSACR", .type = ARM_CP_CONST,
8908                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8909                 .access = PL1_R,
8910                 .resetvalue = 0xc00
8911             };
8912             define_one_arm_cp_reg(cpu, &nsacr);
8913         }
8914     }
8915 
8916     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8917         if (arm_feature(env, ARM_FEATURE_V6)) {
8918             /* PMSAv6 not implemented */
8919             assert(arm_feature(env, ARM_FEATURE_V7));
8920             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8921             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8922         } else {
8923             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8924         }
8925     } else {
8926         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8927         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8928         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8929         if (cpu_isar_feature(aa32_hpd, cpu)) {
8930             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8931         }
8932     }
8933     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8934         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8935     }
8936     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8937         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8938     }
8939     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8940         ARMCPRegInfo vapa_cp_reginfo[] = {
8941             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
8942               .access = PL1_RW, .resetvalue = 0,
8943               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
8944                                      offsetoflow32(CPUARMState, cp15.par_ns) },
8945               .writefn = par_write},
8946 #ifndef CONFIG_USER_ONLY
8947             /* This underdecoding is safe because the reginfo is NO_RAW. */
8948             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
8949               .access = PL1_W, .accessfn = ats_access,
8950               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
8951 #endif
8952         };
8953 
8954         /*
8955          * When LPAE exists this 32-bit PAR register is an alias of the
8956          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
8957          */
8958         if (arm_feature(env, ARM_FEATURE_LPAE)) {
8959             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
8960         }
8961         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8962     }
8963     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8964         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8965     }
8966     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8967         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8968     }
8969     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8970         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8971     }
8972     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8973         define_arm_cp_regs(cpu, omap_cp_reginfo);
8974     }
8975     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8976         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8977     }
8978     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8979         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8980     }
8981     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8982         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8983     }
8984     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8985         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8986     }
8987     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8988         define_arm_cp_regs(cpu, jazelle_regs);
8989     }
8990     /*
8991      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8992      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8993      * be read-only (ie write causes UNDEF exception).
8994      */
8995     {
8996         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8997             /*
8998              * Pre-v8 MIDR space.
8999              * Note that the MIDR isn't a simple constant register because
9000              * of the TI925 behaviour where writes to another register can
9001              * cause the MIDR value to change.
9002              *
9003              * Unimplemented registers in the c15 0 0 0 space default to
9004              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
9005              * and friends override accordingly.
9006              */
9007             { .name = "MIDR",
9008               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
9009               .access = PL1_R, .resetvalue = cpu->midr,
9010               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
9011               .readfn = midr_read,
9012               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9013               .type = ARM_CP_OVERRIDE },
9014             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
9015             { .name = "DUMMY",
9016               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
9017               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9018             { .name = "DUMMY",
9019               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
9020               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9021             { .name = "DUMMY",
9022               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
9023               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9024             { .name = "DUMMY",
9025               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
9026               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9027             { .name = "DUMMY",
9028               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
9029               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9030         };
9031         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
9032             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
9033               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
9034               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
9035               .fgt = FGT_MIDR_EL1,
9036               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9037               .readfn = midr_read },
9038             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
9039             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
9040               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
9041               .access = PL1_R, .resetvalue = cpu->midr },
9042             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
9043               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
9044               .access = PL1_R,
9045               .accessfn = access_aa64_tid1,
9046               .fgt = FGT_REVIDR_EL1,
9047               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
9048         };
9049         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
9050             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
9051             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9052             .access = PL1_R, .resetvalue = cpu->midr
9053         };
9054         ARMCPRegInfo id_cp_reginfo[] = {
9055             /* These are common to v8 and pre-v8 */
9056             { .name = "CTR",
9057               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
9058               .access = PL1_R, .accessfn = ctr_el0_access,
9059               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9060             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9061               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9062               .access = PL0_R, .accessfn = ctr_el0_access,
9063               .fgt = FGT_CTR_EL0,
9064               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9065             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9066             { .name = "TCMTR",
9067               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
9068               .access = PL1_R,
9069               .accessfn = access_aa32_tid1,
9070               .type = ARM_CP_CONST, .resetvalue = 0 },
9071         };
9072         /* TLBTR is specific to VMSA */
9073         ARMCPRegInfo id_tlbtr_reginfo = {
9074               .name = "TLBTR",
9075               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
9076               .access = PL1_R,
9077               .accessfn = access_aa32_tid1,
9078               .type = ARM_CP_CONST, .resetvalue = 0,
9079         };
9080         /* MPUIR is specific to PMSA V6+ */
9081         ARMCPRegInfo id_mpuir_reginfo = {
9082               .name = "MPUIR",
9083               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9084               .access = PL1_R, .type = ARM_CP_CONST,
9085               .resetvalue = cpu->pmsav7_dregion << 8
9086         };
9087         /* HMPUIR is specific to PMSA V8 */
9088         ARMCPRegInfo id_hmpuir_reginfo = {
9089             .name = "HMPUIR",
9090             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9091             .access = PL2_R, .type = ARM_CP_CONST,
9092             .resetvalue = cpu->pmsav8r_hdregion
9093         };
9094         static const ARMCPRegInfo crn0_wi_reginfo = {
9095             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9096             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9097             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9098         };
9099 #ifdef CONFIG_USER_ONLY
9100         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
9101             { .name = "MIDR_EL1",
9102               .exported_bits = R_MIDR_EL1_REVISION_MASK |
9103                                R_MIDR_EL1_PARTNUM_MASK |
9104                                R_MIDR_EL1_ARCHITECTURE_MASK |
9105                                R_MIDR_EL1_VARIANT_MASK |
9106                                R_MIDR_EL1_IMPLEMENTER_MASK },
9107             { .name = "REVIDR_EL1" },
9108         };
9109         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9110 #endif
9111         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9112             arm_feature(env, ARM_FEATURE_STRONGARM)) {
9113             size_t i;
9114             /*
9115              * Register the blanket "writes ignored" value first to cover the
9116              * whole space. Then update the specific ID registers to allow write
9117              * access, so that they ignore writes rather than causing them to
9118              * UNDEF.
9119              */
9120             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
9121             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9122                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
9123             }
9124             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9125                 id_cp_reginfo[i].access = PL1_RW;
9126             }
9127             id_mpuir_reginfo.access = PL1_RW;
9128             id_tlbtr_reginfo.access = PL1_RW;
9129         }
9130         if (arm_feature(env, ARM_FEATURE_V8)) {
9131             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
9132             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9133                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9134             }
9135         } else {
9136             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9137         }
9138         define_arm_cp_regs(cpu, id_cp_reginfo);
9139         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9140             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
9141         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9142                    arm_feature(env, ARM_FEATURE_V8)) {
9143             uint32_t i = 0;
9144             char *tmp_string;
9145 
9146             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9147             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9148             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9149 
9150             /* Register alias is only valid for first 32 indexes */
9151             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9152                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9153                 uint8_t opc1 = extract32(i, 4, 1);
9154                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9155 
9156                 tmp_string = g_strdup_printf("PRBAR%u", i);
9157                 ARMCPRegInfo tmp_prbarn_reginfo = {
9158                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9159                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9160                     .access = PL1_RW, .resetvalue = 0,
9161                     .accessfn = access_tvm_trvm,
9162                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9163                 };
9164                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9165                 g_free(tmp_string);
9166 
9167                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9168                 tmp_string = g_strdup_printf("PRLAR%u", i);
9169                 ARMCPRegInfo tmp_prlarn_reginfo = {
9170                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9171                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9172                     .access = PL1_RW, .resetvalue = 0,
9173                     .accessfn = access_tvm_trvm,
9174                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9175                 };
9176                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9177                 g_free(tmp_string);
9178             }
9179 
9180             /* Register alias is only valid for first 32 indexes */
9181             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9182                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9183                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9184                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9185 
9186                 tmp_string = g_strdup_printf("HPRBAR%u", i);
9187                 ARMCPRegInfo tmp_hprbarn_reginfo = {
9188                     .name = tmp_string,
9189                     .type = ARM_CP_NO_RAW,
9190                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9191                     .access = PL2_RW, .resetvalue = 0,
9192                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9193                 };
9194                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9195                 g_free(tmp_string);
9196 
9197                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9198                 tmp_string = g_strdup_printf("HPRLAR%u", i);
9199                 ARMCPRegInfo tmp_hprlarn_reginfo = {
9200                     .name = tmp_string,
9201                     .type = ARM_CP_NO_RAW,
9202                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9203                     .access = PL2_RW, .resetvalue = 0,
9204                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9205                 };
9206                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9207                 g_free(tmp_string);
9208             }
9209         } else if (arm_feature(env, ARM_FEATURE_V7)) {
9210             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9211         }
9212     }
9213 
9214     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
9215         ARMCPRegInfo mpidr_cp_reginfo[] = {
9216             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9217               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
9218               .fgt = FGT_MPIDR_EL1,
9219               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
9220         };
9221 #ifdef CONFIG_USER_ONLY
9222         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
9223             { .name = "MPIDR_EL1",
9224               .fixed_bits = 0x0000000080000000 },
9225         };
9226         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9227 #endif
9228         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9229     }
9230 
9231     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
9232         ARMCPRegInfo auxcr_reginfo[] = {
9233             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9234               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
9235               .access = PL1_RW, .accessfn = access_tacr,
9236               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
9237             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9238               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9239               .access = PL2_RW, .type = ARM_CP_CONST,
9240               .resetvalue = 0 },
9241             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9242               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9243               .access = PL3_RW, .type = ARM_CP_CONST,
9244               .resetvalue = 0 },
9245         };
9246         define_arm_cp_regs(cpu, auxcr_reginfo);
9247         if (cpu_isar_feature(aa32_ac2, cpu)) {
9248             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
9249         }
9250     }
9251 
9252     if (arm_feature(env, ARM_FEATURE_CBAR)) {
9253         /*
9254          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9255          * There are two flavours:
9256          *  (1) older 32-bit only cores have a simple 32-bit CBAR
9257          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9258          *      32-bit register visible to AArch32 at a different encoding
9259          *      to the "flavour 1" register and with the bits rearranged to
9260          *      be able to squash a 64-bit address into the 32-bit view.
9261          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9262          * in future if we support AArch32-only configs of some of the
9263          * AArch64 cores we might need to add a specific feature flag
9264          * to indicate cores with "flavour 2" CBAR.
9265          */
9266         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9267             /* 32 bit view is [31:18] 0...0 [43:32]. */
9268             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
9269                 | extract64(cpu->reset_cbar, 32, 12);
9270             ARMCPRegInfo cbar_reginfo[] = {
9271                 { .name = "CBAR",
9272                   .type = ARM_CP_CONST,
9273                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
9274                   .access = PL1_R, .resetvalue = cbar32 },
9275                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
9276                   .type = ARM_CP_CONST,
9277                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
9278                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
9279             };
9280             /* We don't implement a r/w 64 bit CBAR currently */
9281             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
9282             define_arm_cp_regs(cpu, cbar_reginfo);
9283         } else {
9284             ARMCPRegInfo cbar = {
9285                 .name = "CBAR",
9286                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
9287                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
9288                 .fieldoffset = offsetof(CPUARMState,
9289                                         cp15.c15_config_base_address)
9290             };
9291             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
9292                 cbar.access = PL1_R;
9293                 cbar.fieldoffset = 0;
9294                 cbar.type = ARM_CP_CONST;
9295             }
9296             define_one_arm_cp_reg(cpu, &cbar);
9297         }
9298     }
9299 
9300     if (arm_feature(env, ARM_FEATURE_VBAR)) {
9301         static const ARMCPRegInfo vbar_cp_reginfo[] = {
9302             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
9303               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
9304               .access = PL1_RW, .writefn = vbar_write,
9305               .fgt = FGT_VBAR_EL1,
9306               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
9307                                      offsetof(CPUARMState, cp15.vbar_ns) },
9308               .resetvalue = 0 },
9309         };
9310         define_arm_cp_regs(cpu, vbar_cp_reginfo);
9311     }
9312 
9313     /* Generic registers whose values depend on the implementation */
9314     {
9315         ARMCPRegInfo sctlr = {
9316             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
9317             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
9318             .access = PL1_RW, .accessfn = access_tvm_trvm,
9319             .fgt = FGT_SCTLR_EL1,
9320             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9321                                    offsetof(CPUARMState, cp15.sctlr_ns) },
9322             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9323             .raw_writefn = raw_write,
9324         };
9325         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9326             /*
9327              * Normally we would always end the TB on an SCTLR write, but Linux
9328              * arch/arm/mach-pxa/sleep.S expects two instructions following
9329              * an MMU enable to execute from cache.  Imitate this behaviour.
9330              */
9331             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9332         }
9333         define_one_arm_cp_reg(cpu, &sctlr);
9334 
9335         if (arm_feature(env, ARM_FEATURE_PMSA) &&
9336             arm_feature(env, ARM_FEATURE_V8)) {
9337             ARMCPRegInfo vsctlr = {
9338                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9339                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9340                 .access = PL2_RW, .resetvalue = 0x0,
9341                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9342             };
9343             define_one_arm_cp_reg(cpu, &vsctlr);
9344         }
9345     }
9346 
9347     if (cpu_isar_feature(aa64_lor, cpu)) {
9348         define_arm_cp_regs(cpu, lor_reginfo);
9349     }
9350     if (cpu_isar_feature(aa64_pan, cpu)) {
9351         define_one_arm_cp_reg(cpu, &pan_reginfo);
9352     }
9353 #ifndef CONFIG_USER_ONLY
9354     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9355         define_arm_cp_regs(cpu, ats1e1_reginfo);
9356     }
9357     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9358         define_arm_cp_regs(cpu, ats1cp_reginfo);
9359     }
9360 #endif
9361     if (cpu_isar_feature(aa64_uao, cpu)) {
9362         define_one_arm_cp_reg(cpu, &uao_reginfo);
9363     }
9364 
9365     if (cpu_isar_feature(aa64_dit, cpu)) {
9366         define_one_arm_cp_reg(cpu, &dit_reginfo);
9367     }
9368     if (cpu_isar_feature(aa64_ssbs, cpu)) {
9369         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9370     }
9371     if (cpu_isar_feature(any_ras, cpu)) {
9372         define_arm_cp_regs(cpu, minimal_ras_reginfo);
9373     }
9374 
9375     if (cpu_isar_feature(aa64_vh, cpu) ||
9376         cpu_isar_feature(aa64_debugv8p2, cpu)) {
9377         define_one_arm_cp_reg(cpu, &contextidr_el2);
9378     }
9379     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9380         define_arm_cp_regs(cpu, vhe_reginfo);
9381     }
9382 
9383     if (cpu_isar_feature(aa64_sve, cpu)) {
9384         define_arm_cp_regs(cpu, zcr_reginfo);
9385     }
9386 
9387     if (cpu_isar_feature(aa64_hcx, cpu)) {
9388         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9389     }
9390 
9391 #ifdef TARGET_AARCH64
9392     if (cpu_isar_feature(aa64_sme, cpu)) {
9393         define_arm_cp_regs(cpu, sme_reginfo);
9394     }
9395     if (cpu_isar_feature(aa64_pauth, cpu)) {
9396         define_arm_cp_regs(cpu, pauth_reginfo);
9397     }
9398     if (cpu_isar_feature(aa64_rndr, cpu)) {
9399         define_arm_cp_regs(cpu, rndr_reginfo);
9400     }
9401     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9402         define_arm_cp_regs(cpu, tlbirange_reginfo);
9403     }
9404     if (cpu_isar_feature(aa64_tlbios, cpu)) {
9405         define_arm_cp_regs(cpu, tlbios_reginfo);
9406     }
9407     /* Data Cache clean instructions up to PoP */
9408     if (cpu_isar_feature(aa64_dcpop, cpu)) {
9409         define_one_arm_cp_reg(cpu, dcpop_reg);
9410 
9411         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9412             define_one_arm_cp_reg(cpu, dcpodp_reg);
9413         }
9414     }
9415 
9416     /*
9417      * If full MTE is enabled, add all of the system registers.
9418      * If only "instructions available at EL0" are enabled,
9419      * then define only a RAZ/WI version of PSTATE.TCO.
9420      */
9421     if (cpu_isar_feature(aa64_mte, cpu)) {
9422         ARMCPRegInfo gmid_reginfo = {
9423             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
9424             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
9425             .access = PL1_R, .accessfn = access_aa64_tid5,
9426             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
9427         };
9428         define_one_arm_cp_reg(cpu, &gmid_reginfo);
9429         define_arm_cp_regs(cpu, mte_reginfo);
9430         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9431     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9432         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
9433         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9434     }
9435 
9436     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9437         define_arm_cp_regs(cpu, scxtnum_reginfo);
9438     }
9439 
9440     if (cpu_isar_feature(aa64_fgt, cpu)) {
9441         define_arm_cp_regs(cpu, fgt_reginfo);
9442     }
9443 
9444     if (cpu_isar_feature(aa64_rme, cpu)) {
9445         define_arm_cp_regs(cpu, rme_reginfo);
9446         if (cpu_isar_feature(aa64_mte, cpu)) {
9447             define_arm_cp_regs(cpu, rme_mte_reginfo);
9448         }
9449     }
9450 #endif
9451 
9452     if (cpu_isar_feature(any_predinv, cpu)) {
9453         define_arm_cp_regs(cpu, predinv_reginfo);
9454     }
9455 
9456     if (cpu_isar_feature(any_ccidx, cpu)) {
9457         define_arm_cp_regs(cpu, ccsidr2_reginfo);
9458     }
9459 
9460 #ifndef CONFIG_USER_ONLY
9461     /*
9462      * Register redirections and aliases must be done last,
9463      * after the registers from the other extensions have been defined.
9464      */
9465     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9466         define_arm_vh_e2h_redirects_aliases(cpu);
9467     }
9468 #endif
9469 }
9470 
9471 /* Sort alphabetically by type name, except for "any". */
9472 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
9473 {
9474     ObjectClass *class_a = (ObjectClass *)a;
9475     ObjectClass *class_b = (ObjectClass *)b;
9476     const char *name_a, *name_b;
9477 
9478     name_a = object_class_get_name(class_a);
9479     name_b = object_class_get_name(class_b);
9480     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
9481         return 1;
9482     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
9483         return -1;
9484     } else {
9485         return strcmp(name_a, name_b);
9486     }
9487 }
9488 
9489 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
9490 {
9491     ObjectClass *oc = data;
9492     CPUClass *cc = CPU_CLASS(oc);
9493     const char *typename;
9494     char *name;
9495 
9496     typename = object_class_get_name(oc);
9497     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
9498     if (cc->deprecation_note) {
9499         qemu_printf("  %s (deprecated)\n", name);
9500     } else {
9501         qemu_printf("  %s\n", name);
9502     }
9503     g_free(name);
9504 }
9505 
9506 void arm_cpu_list(void)
9507 {
9508     GSList *list;
9509 
9510     list = object_class_get_list(TYPE_ARM_CPU, false);
9511     list = g_slist_sort(list, arm_cpu_list_compare);
9512     qemu_printf("Available CPUs:\n");
9513     g_slist_foreach(list, arm_cpu_list_entry, NULL);
9514     g_slist_free(list);
9515 }
9516 
9517 /*
9518  * Private utility function for define_one_arm_cp_reg_with_opaque():
9519  * add a single reginfo struct to the hash table.
9520  */
9521 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9522                                    void *opaque, CPState state,
9523                                    CPSecureState secstate,
9524                                    int crm, int opc1, int opc2,
9525                                    const char *name)
9526 {
9527     CPUARMState *env = &cpu->env;
9528     uint32_t key;
9529     ARMCPRegInfo *r2;
9530     bool is64 = r->type & ARM_CP_64BIT;
9531     bool ns = secstate & ARM_CP_SECSTATE_NS;
9532     int cp = r->cp;
9533     size_t name_len;
9534     bool make_const;
9535 
9536     switch (state) {
9537     case ARM_CP_STATE_AA32:
9538         /* We assume it is a cp15 register if the .cp field is left unset. */
9539         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9540             cp = 15;
9541         }
9542         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9543         break;
9544     case ARM_CP_STATE_AA64:
9545         /*
9546          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9547          * cp == 0 as equivalent to the value for "standard guest-visible
9548          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9549          * in their AArch64 view (the .cp value may be non-zero for the
9550          * benefit of the AArch32 view).
9551          */
9552         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9553             cp = CP_REG_ARM64_SYSREG_CP;
9554         }
9555         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9556         break;
9557     default:
9558         g_assert_not_reached();
9559     }
9560 
9561     /* Overriding of an existing definition must be explicitly requested. */
9562     if (!(r->type & ARM_CP_OVERRIDE)) {
9563         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9564         if (oldreg) {
9565             assert(oldreg->type & ARM_CP_OVERRIDE);
9566         }
9567     }
9568 
9569     /*
9570      * Eliminate registers that are not present because the EL is missing.
9571      * Doing this here makes it easier to put all registers for a given
9572      * feature into the same ARMCPRegInfo array and define them all at once.
9573      */
9574     make_const = false;
9575     if (arm_feature(env, ARM_FEATURE_EL3)) {
9576         /*
9577          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9578          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9579          */
9580         int min_el = ctz32(r->access) / 2;
9581         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9582             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9583                 return;
9584             }
9585             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9586         }
9587     } else {
9588         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9589                                  ? PL2_RW : PL1_RW);
9590         if ((r->access & max_el) == 0) {
9591             return;
9592         }
9593     }
9594 
9595     /* Combine cpreg and name into one allocation. */
9596     name_len = strlen(name) + 1;
9597     r2 = g_malloc(sizeof(*r2) + name_len);
9598     *r2 = *r;
9599     r2->name = memcpy(r2 + 1, name, name_len);
9600 
9601     /*
9602      * Update fields to match the instantiation, overwiting wildcards
9603      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9604      */
9605     r2->cp = cp;
9606     r2->crm = crm;
9607     r2->opc1 = opc1;
9608     r2->opc2 = opc2;
9609     r2->state = state;
9610     r2->secure = secstate;
9611     if (opaque) {
9612         r2->opaque = opaque;
9613     }
9614 
9615     if (make_const) {
9616         /* This should not have been a very special register to begin. */
9617         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9618         assert(old_special == 0 || old_special == ARM_CP_NOP);
9619         /*
9620          * Set the special function to CONST, retaining the other flags.
9621          * This is important for e.g. ARM_CP_SVE so that we still
9622          * take the SVE trap if CPTR_EL3.EZ == 0.
9623          */
9624         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9625         /*
9626          * Usually, these registers become RES0, but there are a few
9627          * special cases like VPIDR_EL2 which have a constant non-zero
9628          * value with writes ignored.
9629          */
9630         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9631             r2->resetvalue = 0;
9632         }
9633         /*
9634          * ARM_CP_CONST has precedence, so removing the callbacks and
9635          * offsets are not strictly necessary, but it is potentially
9636          * less confusing to debug later.
9637          */
9638         r2->readfn = NULL;
9639         r2->writefn = NULL;
9640         r2->raw_readfn = NULL;
9641         r2->raw_writefn = NULL;
9642         r2->resetfn = NULL;
9643         r2->fieldoffset = 0;
9644         r2->bank_fieldoffsets[0] = 0;
9645         r2->bank_fieldoffsets[1] = 0;
9646     } else {
9647         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9648 
9649         if (isbanked) {
9650             /*
9651              * Register is banked (using both entries in array).
9652              * Overwriting fieldoffset as the array is only used to define
9653              * banked registers but later only fieldoffset is used.
9654              */
9655             r2->fieldoffset = r->bank_fieldoffsets[ns];
9656         }
9657         if (state == ARM_CP_STATE_AA32) {
9658             if (isbanked) {
9659                 /*
9660                  * If the register is banked then we don't need to migrate or
9661                  * reset the 32-bit instance in certain cases:
9662                  *
9663                  * 1) If the register has both 32-bit and 64-bit instances
9664                  *    then we can count on the 64-bit instance taking care
9665                  *    of the non-secure bank.
9666                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9667                  *    version taking care of the secure bank.  This requires
9668                  *    that separate 32 and 64-bit definitions are provided.
9669                  */
9670                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9671                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9672                     r2->type |= ARM_CP_ALIAS;
9673                 }
9674             } else if ((secstate != r->secure) && !ns) {
9675                 /*
9676                  * The register is not banked so we only want to allow
9677                  * migration of the non-secure instance.
9678                  */
9679                 r2->type |= ARM_CP_ALIAS;
9680             }
9681 
9682             if (HOST_BIG_ENDIAN &&
9683                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9684                 r2->fieldoffset += sizeof(uint32_t);
9685             }
9686         }
9687     }
9688 
9689     /*
9690      * By convention, for wildcarded registers only the first
9691      * entry is used for migration; the others are marked as
9692      * ALIAS so we don't try to transfer the register
9693      * multiple times. Special registers (ie NOP/WFI) are
9694      * never migratable and not even raw-accessible.
9695      */
9696     if (r2->type & ARM_CP_SPECIAL_MASK) {
9697         r2->type |= ARM_CP_NO_RAW;
9698     }
9699     if (((r->crm == CP_ANY) && crm != 0) ||
9700         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9701         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9702         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9703     }
9704 
9705     /*
9706      * Check that raw accesses are either forbidden or handled. Note that
9707      * we can't assert this earlier because the setup of fieldoffset for
9708      * banked registers has to be done first.
9709      */
9710     if (!(r2->type & ARM_CP_NO_RAW)) {
9711         assert(!raw_accessors_invalid(r2));
9712     }
9713 
9714     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9715 }
9716 
9717 
9718 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9719                                        const ARMCPRegInfo *r, void *opaque)
9720 {
9721     /*
9722      * Define implementations of coprocessor registers.
9723      * We store these in a hashtable because typically
9724      * there are less than 150 registers in a space which
9725      * is 16*16*16*8*8 = 262144 in size.
9726      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9727      * If a register is defined twice then the second definition is
9728      * used, so this can be used to define some generic registers and
9729      * then override them with implementation specific variations.
9730      * At least one of the original and the second definition should
9731      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9732      * against accidental use.
9733      *
9734      * The state field defines whether the register is to be
9735      * visible in the AArch32 or AArch64 execution state. If the
9736      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9737      * reginfo structure for the AArch32 view, which sees the lower
9738      * 32 bits of the 64 bit register.
9739      *
9740      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9741      * be wildcarded. AArch64 registers are always considered to be 64
9742      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9743      * the register, if any.
9744      */
9745     int crm, opc1, opc2;
9746     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9747     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9748     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9749     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9750     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9751     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9752     CPState state;
9753 
9754     /* 64 bit registers have only CRm and Opc1 fields */
9755     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9756     /* op0 only exists in the AArch64 encodings */
9757     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9758     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9759     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9760     /*
9761      * This API is only for Arm's system coprocessors (14 and 15) or
9762      * (M-profile or v7A-and-earlier only) for implementation defined
9763      * coprocessors in the range 0..7.  Our decode assumes this, since
9764      * 8..13 can be used for other insns including VFP and Neon. See
9765      * valid_cp() in translate.c.  Assert here that we haven't tried
9766      * to use an invalid coprocessor number.
9767      */
9768     switch (r->state) {
9769     case ARM_CP_STATE_BOTH:
9770         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9771         if (r->cp == 0) {
9772             break;
9773         }
9774         /* fall through */
9775     case ARM_CP_STATE_AA32:
9776         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9777             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9778             assert(r->cp >= 14 && r->cp <= 15);
9779         } else {
9780             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9781         }
9782         break;
9783     case ARM_CP_STATE_AA64:
9784         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9785         break;
9786     default:
9787         g_assert_not_reached();
9788     }
9789     /*
9790      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9791      * encodes a minimum access level for the register. We roll this
9792      * runtime check into our general permission check code, so check
9793      * here that the reginfo's specified permissions are strict enough
9794      * to encompass the generic architectural permission check.
9795      */
9796     if (r->state != ARM_CP_STATE_AA32) {
9797         CPAccessRights mask;
9798         switch (r->opc1) {
9799         case 0:
9800             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9801             mask = PL0U_R | PL1_RW;
9802             break;
9803         case 1: case 2:
9804             /* min_EL EL1 */
9805             mask = PL1_RW;
9806             break;
9807         case 3:
9808             /* min_EL EL0 */
9809             mask = PL0_RW;
9810             break;
9811         case 4:
9812         case 5:
9813             /* min_EL EL2 */
9814             mask = PL2_RW;
9815             break;
9816         case 6:
9817             /* min_EL EL3 */
9818             mask = PL3_RW;
9819             break;
9820         case 7:
9821             /* min_EL EL1, secure mode only (we don't check the latter) */
9822             mask = PL1_RW;
9823             break;
9824         default:
9825             /* broken reginfo with out-of-range opc1 */
9826             g_assert_not_reached();
9827         }
9828         /* assert our permissions are not too lax (stricter is fine) */
9829         assert((r->access & ~mask) == 0);
9830     }
9831 
9832     /*
9833      * Check that the register definition has enough info to handle
9834      * reads and writes if they are permitted.
9835      */
9836     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9837         if (r->access & PL3_R) {
9838             assert((r->fieldoffset ||
9839                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9840                    r->readfn);
9841         }
9842         if (r->access & PL3_W) {
9843             assert((r->fieldoffset ||
9844                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9845                    r->writefn);
9846         }
9847     }
9848 
9849     for (crm = crmmin; crm <= crmmax; crm++) {
9850         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9851             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9852                 for (state = ARM_CP_STATE_AA32;
9853                      state <= ARM_CP_STATE_AA64; state++) {
9854                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9855                         continue;
9856                     }
9857                     if (state == ARM_CP_STATE_AA32) {
9858                         /*
9859                          * Under AArch32 CP registers can be common
9860                          * (same for secure and non-secure world) or banked.
9861                          */
9862                         char *name;
9863 
9864                         switch (r->secure) {
9865                         case ARM_CP_SECSTATE_S:
9866                         case ARM_CP_SECSTATE_NS:
9867                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9868                                                    r->secure, crm, opc1, opc2,
9869                                                    r->name);
9870                             break;
9871                         case ARM_CP_SECSTATE_BOTH:
9872                             name = g_strdup_printf("%s_S", r->name);
9873                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9874                                                    ARM_CP_SECSTATE_S,
9875                                                    crm, opc1, opc2, name);
9876                             g_free(name);
9877                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9878                                                    ARM_CP_SECSTATE_NS,
9879                                                    crm, opc1, opc2, r->name);
9880                             break;
9881                         default:
9882                             g_assert_not_reached();
9883                         }
9884                     } else {
9885                         /*
9886                          * AArch64 registers get mapped to non-secure instance
9887                          * of AArch32
9888                          */
9889                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9890                                                ARM_CP_SECSTATE_NS,
9891                                                crm, opc1, opc2, r->name);
9892                     }
9893                 }
9894             }
9895         }
9896     }
9897 }
9898 
9899 /* Define a whole list of registers */
9900 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9901                                         void *opaque, size_t len)
9902 {
9903     size_t i;
9904     for (i = 0; i < len; ++i) {
9905         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9906     }
9907 }
9908 
9909 /*
9910  * Modify ARMCPRegInfo for access from userspace.
9911  *
9912  * This is a data driven modification directed by
9913  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9914  * user-space cannot alter any values and dynamic values pertaining to
9915  * execution state are hidden from user space view anyway.
9916  */
9917 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9918                                  const ARMCPRegUserSpaceInfo *mods,
9919                                  size_t mods_len)
9920 {
9921     for (size_t mi = 0; mi < mods_len; ++mi) {
9922         const ARMCPRegUserSpaceInfo *m = mods + mi;
9923         GPatternSpec *pat = NULL;
9924 
9925         if (m->is_glob) {
9926             pat = g_pattern_spec_new(m->name);
9927         }
9928         for (size_t ri = 0; ri < regs_len; ++ri) {
9929             ARMCPRegInfo *r = regs + ri;
9930 
9931             if (pat && g_pattern_match_string(pat, r->name)) {
9932                 r->type = ARM_CP_CONST;
9933                 r->access = PL0U_R;
9934                 r->resetvalue = 0;
9935                 /* continue */
9936             } else if (strcmp(r->name, m->name) == 0) {
9937                 r->type = ARM_CP_CONST;
9938                 r->access = PL0U_R;
9939                 r->resetvalue &= m->exported_bits;
9940                 r->resetvalue |= m->fixed_bits;
9941                 break;
9942             }
9943         }
9944         if (pat) {
9945             g_pattern_spec_free(pat);
9946         }
9947     }
9948 }
9949 
9950 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9951 {
9952     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9953 }
9954 
9955 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9956                          uint64_t value)
9957 {
9958     /* Helper coprocessor write function for write-ignore registers */
9959 }
9960 
9961 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9962 {
9963     /* Helper coprocessor write function for read-as-zero registers */
9964     return 0;
9965 }
9966 
9967 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9968 {
9969     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9970 }
9971 
9972 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9973 {
9974     /*
9975      * Return true if it is not valid for us to switch to
9976      * this CPU mode (ie all the UNPREDICTABLE cases in
9977      * the ARM ARM CPSRWriteByInstr pseudocode).
9978      */
9979 
9980     /* Changes to or from Hyp via MSR and CPS are illegal. */
9981     if (write_type == CPSRWriteByInstr &&
9982         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9983          mode == ARM_CPU_MODE_HYP)) {
9984         return 1;
9985     }
9986 
9987     switch (mode) {
9988     case ARM_CPU_MODE_USR:
9989         return 0;
9990     case ARM_CPU_MODE_SYS:
9991     case ARM_CPU_MODE_SVC:
9992     case ARM_CPU_MODE_ABT:
9993     case ARM_CPU_MODE_UND:
9994     case ARM_CPU_MODE_IRQ:
9995     case ARM_CPU_MODE_FIQ:
9996         /*
9997          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9998          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9999          */
10000         /*
10001          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10002          * and CPS are treated as illegal mode changes.
10003          */
10004         if (write_type == CPSRWriteByInstr &&
10005             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
10006             (arm_hcr_el2_eff(env) & HCR_TGE)) {
10007             return 1;
10008         }
10009         return 0;
10010     case ARM_CPU_MODE_HYP:
10011         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
10012     case ARM_CPU_MODE_MON:
10013         return arm_current_el(env) < 3;
10014     default:
10015         return 1;
10016     }
10017 }
10018 
10019 uint32_t cpsr_read(CPUARMState *env)
10020 {
10021     int ZF;
10022     ZF = (env->ZF == 0);
10023     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
10024         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
10025         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
10026         | ((env->condexec_bits & 0xfc) << 8)
10027         | (env->GE << 16) | (env->daif & CPSR_AIF);
10028 }
10029 
10030 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
10031                 CPSRWriteType write_type)
10032 {
10033     uint32_t changed_daif;
10034     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
10035         (mask & (CPSR_M | CPSR_E | CPSR_IL));
10036 
10037     if (mask & CPSR_NZCV) {
10038         env->ZF = (~val) & CPSR_Z;
10039         env->NF = val;
10040         env->CF = (val >> 29) & 1;
10041         env->VF = (val << 3) & 0x80000000;
10042     }
10043     if (mask & CPSR_Q) {
10044         env->QF = ((val & CPSR_Q) != 0);
10045     }
10046     if (mask & CPSR_T) {
10047         env->thumb = ((val & CPSR_T) != 0);
10048     }
10049     if (mask & CPSR_IT_0_1) {
10050         env->condexec_bits &= ~3;
10051         env->condexec_bits |= (val >> 25) & 3;
10052     }
10053     if (mask & CPSR_IT_2_7) {
10054         env->condexec_bits &= 3;
10055         env->condexec_bits |= (val >> 8) & 0xfc;
10056     }
10057     if (mask & CPSR_GE) {
10058         env->GE = (val >> 16) & 0xf;
10059     }
10060 
10061     /*
10062      * In a V7 implementation that includes the security extensions but does
10063      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10064      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10065      * bits respectively.
10066      *
10067      * In a V8 implementation, it is permitted for privileged software to
10068      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10069      */
10070     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
10071         arm_feature(env, ARM_FEATURE_EL3) &&
10072         !arm_feature(env, ARM_FEATURE_EL2) &&
10073         !arm_is_secure(env)) {
10074 
10075         changed_daif = (env->daif ^ val) & mask;
10076 
10077         if (changed_daif & CPSR_A) {
10078             /*
10079              * Check to see if we are allowed to change the masking of async
10080              * abort exceptions from a non-secure state.
10081              */
10082             if (!(env->cp15.scr_el3 & SCR_AW)) {
10083                 qemu_log_mask(LOG_GUEST_ERROR,
10084                               "Ignoring attempt to switch CPSR_A flag from "
10085                               "non-secure world with SCR.AW bit clear\n");
10086                 mask &= ~CPSR_A;
10087             }
10088         }
10089 
10090         if (changed_daif & CPSR_F) {
10091             /*
10092              * Check to see if we are allowed to change the masking of FIQ
10093              * exceptions from a non-secure state.
10094              */
10095             if (!(env->cp15.scr_el3 & SCR_FW)) {
10096                 qemu_log_mask(LOG_GUEST_ERROR,
10097                               "Ignoring attempt to switch CPSR_F flag from "
10098                               "non-secure world with SCR.FW bit clear\n");
10099                 mask &= ~CPSR_F;
10100             }
10101 
10102             /*
10103              * Check whether non-maskable FIQ (NMFI) support is enabled.
10104              * If this bit is set software is not allowed to mask
10105              * FIQs, but is allowed to set CPSR_F to 0.
10106              */
10107             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10108                 (val & CPSR_F)) {
10109                 qemu_log_mask(LOG_GUEST_ERROR,
10110                               "Ignoring attempt to enable CPSR_F flag "
10111                               "(non-maskable FIQ [NMFI] support enabled)\n");
10112                 mask &= ~CPSR_F;
10113             }
10114         }
10115     }
10116 
10117     env->daif &= ~(CPSR_AIF & mask);
10118     env->daif |= val & CPSR_AIF & mask;
10119 
10120     if (write_type != CPSRWriteRaw &&
10121         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
10122         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
10123             /*
10124              * Note that we can only get here in USR mode if this is a
10125              * gdb stub write; for this case we follow the architectural
10126              * behaviour for guest writes in USR mode of ignoring an attempt
10127              * to switch mode. (Those are caught by translate.c for writes
10128              * triggered by guest instructions.)
10129              */
10130             mask &= ~CPSR_M;
10131         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
10132             /*
10133              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
10134              * v7, and has defined behaviour in v8:
10135              *  + leave CPSR.M untouched
10136              *  + allow changes to the other CPSR fields
10137              *  + set PSTATE.IL
10138              * For user changes via the GDB stub, we don't set PSTATE.IL,
10139              * as this would be unnecessarily harsh for a user error.
10140              */
10141             mask &= ~CPSR_M;
10142             if (write_type != CPSRWriteByGDBStub &&
10143                 arm_feature(env, ARM_FEATURE_V8)) {
10144                 mask |= CPSR_IL;
10145                 val |= CPSR_IL;
10146             }
10147             qemu_log_mask(LOG_GUEST_ERROR,
10148                           "Illegal AArch32 mode switch attempt from %s to %s\n",
10149                           aarch32_mode_name(env->uncached_cpsr),
10150                           aarch32_mode_name(val));
10151         } else {
10152             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10153                           write_type == CPSRWriteExceptionReturn ?
10154                           "Exception return from AArch32" :
10155                           "AArch32 mode switch from",
10156                           aarch32_mode_name(env->uncached_cpsr),
10157                           aarch32_mode_name(val), env->regs[15]);
10158             switch_mode(env, val & CPSR_M);
10159         }
10160     }
10161     mask &= ~CACHED_CPSR_BITS;
10162     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
10163     if (tcg_enabled() && rebuild_hflags) {
10164         arm_rebuild_hflags(env);
10165     }
10166 }
10167 
10168 #ifdef CONFIG_USER_ONLY
10169 
10170 static void switch_mode(CPUARMState *env, int mode)
10171 {
10172     ARMCPU *cpu = env_archcpu(env);
10173 
10174     if (mode != ARM_CPU_MODE_USR) {
10175         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10176     }
10177 }
10178 
10179 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10180                                  uint32_t cur_el, bool secure)
10181 {
10182     return 1;
10183 }
10184 
10185 void aarch64_sync_64_to_32(CPUARMState *env)
10186 {
10187     g_assert_not_reached();
10188 }
10189 
10190 #else
10191 
10192 static void switch_mode(CPUARMState *env, int mode)
10193 {
10194     int old_mode;
10195     int i;
10196 
10197     old_mode = env->uncached_cpsr & CPSR_M;
10198     if (mode == old_mode) {
10199         return;
10200     }
10201 
10202     if (old_mode == ARM_CPU_MODE_FIQ) {
10203         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10204         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
10205     } else if (mode == ARM_CPU_MODE_FIQ) {
10206         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10207         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
10208     }
10209 
10210     i = bank_number(old_mode);
10211     env->banked_r13[i] = env->regs[13];
10212     env->banked_spsr[i] = env->spsr;
10213 
10214     i = bank_number(mode);
10215     env->regs[13] = env->banked_r13[i];
10216     env->spsr = env->banked_spsr[i];
10217 
10218     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10219     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
10220 }
10221 
10222 /*
10223  * Physical Interrupt Target EL Lookup Table
10224  *
10225  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10226  *
10227  * The below multi-dimensional table is used for looking up the target
10228  * exception level given numerous condition criteria.  Specifically, the
10229  * target EL is based on SCR and HCR routing controls as well as the
10230  * currently executing EL and secure state.
10231  *
10232  *    Dimensions:
10233  *    target_el_table[2][2][2][2][2][4]
10234  *                    |  |  |  |  |  +--- Current EL
10235  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
10236  *                    |  |  |  +--------- HCR mask override
10237  *                    |  |  +------------ SCR exec state control
10238  *                    |  +--------------- SCR mask override
10239  *                    +------------------ 32-bit(0)/64-bit(1) EL3
10240  *
10241  *    The table values are as such:
10242  *    0-3 = EL0-EL3
10243  *     -1 = Cannot occur
10244  *
10245  * The ARM ARM target EL table includes entries indicating that an "exception
10246  * is not taken".  The two cases where this is applicable are:
10247  *    1) An exception is taken from EL3 but the SCR does not have the exception
10248  *    routed to EL3.
10249  *    2) An exception is taken from EL2 but the HCR does not have the exception
10250  *    routed to EL2.
10251  * In these two cases, the below table contain a target of EL1.  This value is
10252  * returned as it is expected that the consumer of the table data will check
10253  * for "target EL >= current EL" to ensure the exception is not taken.
10254  *
10255  *            SCR     HCR
10256  *         64  EA     AMO                 From
10257  *        BIT IRQ     IMO      Non-secure         Secure
10258  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
10259  */
10260 static const int8_t target_el_table[2][2][2][2][2][4] = {
10261     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10262        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
10263       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10264        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
10265      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10266        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
10267       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10268        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
10269     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
10270        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
10271       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
10272        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
10273      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
10274        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
10275       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
10276        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
10277 };
10278 
10279 /*
10280  * Determine the target EL for physical exceptions
10281  */
10282 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10283                                  uint32_t cur_el, bool secure)
10284 {
10285     CPUARMState *env = cpu_env(cs);
10286     bool rw;
10287     bool scr;
10288     bool hcr;
10289     int target_el;
10290     /* Is the highest EL AArch64? */
10291     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10292     uint64_t hcr_el2;
10293 
10294     if (arm_feature(env, ARM_FEATURE_EL3)) {
10295         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10296     } else {
10297         /*
10298          * Either EL2 is the highest EL (and so the EL2 register width
10299          * is given by is64); or there is no EL2 or EL3, in which case
10300          * the value of 'rw' does not affect the table lookup anyway.
10301          */
10302         rw = is64;
10303     }
10304 
10305     hcr_el2 = arm_hcr_el2_eff(env);
10306     switch (excp_idx) {
10307     case EXCP_IRQ:
10308         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
10309         hcr = hcr_el2 & HCR_IMO;
10310         break;
10311     case EXCP_FIQ:
10312         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
10313         hcr = hcr_el2 & HCR_FMO;
10314         break;
10315     default:
10316         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
10317         hcr = hcr_el2 & HCR_AMO;
10318         break;
10319     };
10320 
10321     /*
10322      * For these purposes, TGE and AMO/IMO/FMO both force the
10323      * interrupt to EL2.  Fold TGE into the bit extracted above.
10324      */
10325     hcr |= (hcr_el2 & HCR_TGE) != 0;
10326 
10327     /* Perform a table-lookup for the target EL given the current state */
10328     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10329 
10330     assert(target_el > 0);
10331 
10332     return target_el;
10333 }
10334 
10335 void arm_log_exception(CPUState *cs)
10336 {
10337     int idx = cs->exception_index;
10338 
10339     if (qemu_loglevel_mask(CPU_LOG_INT)) {
10340         const char *exc = NULL;
10341         static const char * const excnames[] = {
10342             [EXCP_UDEF] = "Undefined Instruction",
10343             [EXCP_SWI] = "SVC",
10344             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10345             [EXCP_DATA_ABORT] = "Data Abort",
10346             [EXCP_IRQ] = "IRQ",
10347             [EXCP_FIQ] = "FIQ",
10348             [EXCP_BKPT] = "Breakpoint",
10349             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10350             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10351             [EXCP_HVC] = "Hypervisor Call",
10352             [EXCP_HYP_TRAP] = "Hypervisor Trap",
10353             [EXCP_SMC] = "Secure Monitor Call",
10354             [EXCP_VIRQ] = "Virtual IRQ",
10355             [EXCP_VFIQ] = "Virtual FIQ",
10356             [EXCP_SEMIHOST] = "Semihosting call",
10357             [EXCP_NOCP] = "v7M NOCP UsageFault",
10358             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10359             [EXCP_STKOF] = "v8M STKOF UsageFault",
10360             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10361             [EXCP_LSERR] = "v8M LSERR UsageFault",
10362             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
10363             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
10364             [EXCP_VSERR] = "Virtual SERR",
10365             [EXCP_GPC] = "Granule Protection Check",
10366         };
10367 
10368         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10369             exc = excnames[idx];
10370         }
10371         if (!exc) {
10372             exc = "unknown";
10373         }
10374         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10375                       idx, exc, cs->cpu_index);
10376     }
10377 }
10378 
10379 /*
10380  * Function used to synchronize QEMU's AArch64 register set with AArch32
10381  * register set.  This is necessary when switching between AArch32 and AArch64
10382  * execution state.
10383  */
10384 void aarch64_sync_32_to_64(CPUARMState *env)
10385 {
10386     int i;
10387     uint32_t mode = env->uncached_cpsr & CPSR_M;
10388 
10389     /* We can blanket copy R[0:7] to X[0:7] */
10390     for (i = 0; i < 8; i++) {
10391         env->xregs[i] = env->regs[i];
10392     }
10393 
10394     /*
10395      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10396      * Otherwise, they come from the banked user regs.
10397      */
10398     if (mode == ARM_CPU_MODE_FIQ) {
10399         for (i = 8; i < 13; i++) {
10400             env->xregs[i] = env->usr_regs[i - 8];
10401         }
10402     } else {
10403         for (i = 8; i < 13; i++) {
10404             env->xregs[i] = env->regs[i];
10405         }
10406     }
10407 
10408     /*
10409      * Registers x13-x23 are the various mode SP and FP registers. Registers
10410      * r13 and r14 are only copied if we are in that mode, otherwise we copy
10411      * from the mode banked register.
10412      */
10413     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10414         env->xregs[13] = env->regs[13];
10415         env->xregs[14] = env->regs[14];
10416     } else {
10417         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10418         /* HYP is an exception in that it is copied from r14 */
10419         if (mode == ARM_CPU_MODE_HYP) {
10420             env->xregs[14] = env->regs[14];
10421         } else {
10422             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
10423         }
10424     }
10425 
10426     if (mode == ARM_CPU_MODE_HYP) {
10427         env->xregs[15] = env->regs[13];
10428     } else {
10429         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
10430     }
10431 
10432     if (mode == ARM_CPU_MODE_IRQ) {
10433         env->xregs[16] = env->regs[14];
10434         env->xregs[17] = env->regs[13];
10435     } else {
10436         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10437         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10438     }
10439 
10440     if (mode == ARM_CPU_MODE_SVC) {
10441         env->xregs[18] = env->regs[14];
10442         env->xregs[19] = env->regs[13];
10443     } else {
10444         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10445         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10446     }
10447 
10448     if (mode == ARM_CPU_MODE_ABT) {
10449         env->xregs[20] = env->regs[14];
10450         env->xregs[21] = env->regs[13];
10451     } else {
10452         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10453         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10454     }
10455 
10456     if (mode == ARM_CPU_MODE_UND) {
10457         env->xregs[22] = env->regs[14];
10458         env->xregs[23] = env->regs[13];
10459     } else {
10460         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10461         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10462     }
10463 
10464     /*
10465      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10466      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10467      * FIQ bank for r8-r14.
10468      */
10469     if (mode == ARM_CPU_MODE_FIQ) {
10470         for (i = 24; i < 31; i++) {
10471             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10472         }
10473     } else {
10474         for (i = 24; i < 29; i++) {
10475             env->xregs[i] = env->fiq_regs[i - 24];
10476         }
10477         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10478         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10479     }
10480 
10481     env->pc = env->regs[15];
10482 }
10483 
10484 /*
10485  * Function used to synchronize QEMU's AArch32 register set with AArch64
10486  * register set.  This is necessary when switching between AArch32 and AArch64
10487  * execution state.
10488  */
10489 void aarch64_sync_64_to_32(CPUARMState *env)
10490 {
10491     int i;
10492     uint32_t mode = env->uncached_cpsr & CPSR_M;
10493 
10494     /* We can blanket copy X[0:7] to R[0:7] */
10495     for (i = 0; i < 8; i++) {
10496         env->regs[i] = env->xregs[i];
10497     }
10498 
10499     /*
10500      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10501      * Otherwise, we copy x8-x12 into the banked user regs.
10502      */
10503     if (mode == ARM_CPU_MODE_FIQ) {
10504         for (i = 8; i < 13; i++) {
10505             env->usr_regs[i - 8] = env->xregs[i];
10506         }
10507     } else {
10508         for (i = 8; i < 13; i++) {
10509             env->regs[i] = env->xregs[i];
10510         }
10511     }
10512 
10513     /*
10514      * Registers r13 & r14 depend on the current mode.
10515      * If we are in a given mode, we copy the corresponding x registers to r13
10516      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10517      * for the mode.
10518      */
10519     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10520         env->regs[13] = env->xregs[13];
10521         env->regs[14] = env->xregs[14];
10522     } else {
10523         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10524 
10525         /*
10526          * HYP is an exception in that it does not have its own banked r14 but
10527          * shares the USR r14
10528          */
10529         if (mode == ARM_CPU_MODE_HYP) {
10530             env->regs[14] = env->xregs[14];
10531         } else {
10532             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10533         }
10534     }
10535 
10536     if (mode == ARM_CPU_MODE_HYP) {
10537         env->regs[13] = env->xregs[15];
10538     } else {
10539         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10540     }
10541 
10542     if (mode == ARM_CPU_MODE_IRQ) {
10543         env->regs[14] = env->xregs[16];
10544         env->regs[13] = env->xregs[17];
10545     } else {
10546         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10547         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10548     }
10549 
10550     if (mode == ARM_CPU_MODE_SVC) {
10551         env->regs[14] = env->xregs[18];
10552         env->regs[13] = env->xregs[19];
10553     } else {
10554         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10555         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10556     }
10557 
10558     if (mode == ARM_CPU_MODE_ABT) {
10559         env->regs[14] = env->xregs[20];
10560         env->regs[13] = env->xregs[21];
10561     } else {
10562         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10563         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10564     }
10565 
10566     if (mode == ARM_CPU_MODE_UND) {
10567         env->regs[14] = env->xregs[22];
10568         env->regs[13] = env->xregs[23];
10569     } else {
10570         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10571         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10572     }
10573 
10574     /*
10575      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10576      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10577      * FIQ bank for r8-r14.
10578      */
10579     if (mode == ARM_CPU_MODE_FIQ) {
10580         for (i = 24; i < 31; i++) {
10581             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10582         }
10583     } else {
10584         for (i = 24; i < 29; i++) {
10585             env->fiq_regs[i - 24] = env->xregs[i];
10586         }
10587         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10588         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10589     }
10590 
10591     env->regs[15] = env->pc;
10592 }
10593 
10594 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10595                                    uint32_t mask, uint32_t offset,
10596                                    uint32_t newpc)
10597 {
10598     int new_el;
10599 
10600     /* Change the CPU state so as to actually take the exception. */
10601     switch_mode(env, new_mode);
10602 
10603     /*
10604      * For exceptions taken to AArch32 we must clear the SS bit in both
10605      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10606      */
10607     env->pstate &= ~PSTATE_SS;
10608     env->spsr = cpsr_read(env);
10609     /* Clear IT bits.  */
10610     env->condexec_bits = 0;
10611     /* Switch to the new mode, and to the correct instruction set.  */
10612     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10613 
10614     /* This must be after mode switching. */
10615     new_el = arm_current_el(env);
10616 
10617     /* Set new mode endianness */
10618     env->uncached_cpsr &= ~CPSR_E;
10619     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10620         env->uncached_cpsr |= CPSR_E;
10621     }
10622     /* J and IL must always be cleared for exception entry */
10623     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10624     env->daif |= mask;
10625 
10626     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10627         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10628             env->uncached_cpsr |= CPSR_SSBS;
10629         } else {
10630             env->uncached_cpsr &= ~CPSR_SSBS;
10631         }
10632     }
10633 
10634     if (new_mode == ARM_CPU_MODE_HYP) {
10635         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10636         env->elr_el[2] = env->regs[15];
10637     } else {
10638         /* CPSR.PAN is normally preserved preserved unless...  */
10639         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10640             switch (new_el) {
10641             case 3:
10642                 if (!arm_is_secure_below_el3(env)) {
10643                     /* ... the target is EL3, from non-secure state.  */
10644                     env->uncached_cpsr &= ~CPSR_PAN;
10645                     break;
10646                 }
10647                 /* ... the target is EL3, from secure state ... */
10648                 /* fall through */
10649             case 1:
10650                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10651                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10652                     env->uncached_cpsr |= CPSR_PAN;
10653                 }
10654                 break;
10655             }
10656         }
10657         /*
10658          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10659          * and we should just guard the thumb mode on V4
10660          */
10661         if (arm_feature(env, ARM_FEATURE_V4T)) {
10662             env->thumb =
10663                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10664         }
10665         env->regs[14] = env->regs[15] + offset;
10666     }
10667     env->regs[15] = newpc;
10668 
10669     if (tcg_enabled()) {
10670         arm_rebuild_hflags(env);
10671     }
10672 }
10673 
10674 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10675 {
10676     /*
10677      * Handle exception entry to Hyp mode; this is sufficiently
10678      * different to entry to other AArch32 modes that we handle it
10679      * separately here.
10680      *
10681      * The vector table entry used is always the 0x14 Hyp mode entry point,
10682      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10683      * The offset applied to the preferred return address is always zero
10684      * (see DDI0487C.a section G1.12.3).
10685      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10686      */
10687     uint32_t addr, mask;
10688     ARMCPU *cpu = ARM_CPU(cs);
10689     CPUARMState *env = &cpu->env;
10690 
10691     switch (cs->exception_index) {
10692     case EXCP_UDEF:
10693         addr = 0x04;
10694         break;
10695     case EXCP_SWI:
10696         addr = 0x08;
10697         break;
10698     case EXCP_BKPT:
10699         /* Fall through to prefetch abort.  */
10700     case EXCP_PREFETCH_ABORT:
10701         env->cp15.ifar_s = env->exception.vaddress;
10702         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10703                       (uint32_t)env->exception.vaddress);
10704         addr = 0x0c;
10705         break;
10706     case EXCP_DATA_ABORT:
10707         env->cp15.dfar_s = env->exception.vaddress;
10708         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10709                       (uint32_t)env->exception.vaddress);
10710         addr = 0x10;
10711         break;
10712     case EXCP_IRQ:
10713         addr = 0x18;
10714         break;
10715     case EXCP_FIQ:
10716         addr = 0x1c;
10717         break;
10718     case EXCP_HVC:
10719         addr = 0x08;
10720         break;
10721     case EXCP_HYP_TRAP:
10722         addr = 0x14;
10723         break;
10724     default:
10725         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10726     }
10727 
10728     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10729         if (!arm_feature(env, ARM_FEATURE_V8)) {
10730             /*
10731              * QEMU syndrome values are v8-style. v7 has the IL bit
10732              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10733              * If this is a v7 CPU, squash the IL bit in those cases.
10734              */
10735             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10736                 (cs->exception_index == EXCP_DATA_ABORT &&
10737                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10738                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10739                 env->exception.syndrome &= ~ARM_EL_IL;
10740             }
10741         }
10742         env->cp15.esr_el[2] = env->exception.syndrome;
10743     }
10744 
10745     if (arm_current_el(env) != 2 && addr < 0x14) {
10746         addr = 0x14;
10747     }
10748 
10749     mask = 0;
10750     if (!(env->cp15.scr_el3 & SCR_EA)) {
10751         mask |= CPSR_A;
10752     }
10753     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10754         mask |= CPSR_I;
10755     }
10756     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10757         mask |= CPSR_F;
10758     }
10759 
10760     addr += env->cp15.hvbar;
10761 
10762     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10763 }
10764 
10765 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10766 {
10767     ARMCPU *cpu = ARM_CPU(cs);
10768     CPUARMState *env = &cpu->env;
10769     uint32_t addr;
10770     uint32_t mask;
10771     int new_mode;
10772     uint32_t offset;
10773     uint32_t moe;
10774 
10775     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10776     switch (syn_get_ec(env->exception.syndrome)) {
10777     case EC_BREAKPOINT:
10778     case EC_BREAKPOINT_SAME_EL:
10779         moe = 1;
10780         break;
10781     case EC_WATCHPOINT:
10782     case EC_WATCHPOINT_SAME_EL:
10783         moe = 10;
10784         break;
10785     case EC_AA32_BKPT:
10786         moe = 3;
10787         break;
10788     case EC_VECTORCATCH:
10789         moe = 5;
10790         break;
10791     default:
10792         moe = 0;
10793         break;
10794     }
10795 
10796     if (moe) {
10797         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10798     }
10799 
10800     if (env->exception.target_el == 2) {
10801         arm_cpu_do_interrupt_aarch32_hyp(cs);
10802         return;
10803     }
10804 
10805     switch (cs->exception_index) {
10806     case EXCP_UDEF:
10807         new_mode = ARM_CPU_MODE_UND;
10808         addr = 0x04;
10809         mask = CPSR_I;
10810         if (env->thumb) {
10811             offset = 2;
10812         } else {
10813             offset = 4;
10814         }
10815         break;
10816     case EXCP_SWI:
10817         new_mode = ARM_CPU_MODE_SVC;
10818         addr = 0x08;
10819         mask = CPSR_I;
10820         /* The PC already points to the next instruction.  */
10821         offset = 0;
10822         break;
10823     case EXCP_BKPT:
10824         /* Fall through to prefetch abort.  */
10825     case EXCP_PREFETCH_ABORT:
10826         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10827         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10828         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10829                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10830         new_mode = ARM_CPU_MODE_ABT;
10831         addr = 0x0c;
10832         mask = CPSR_A | CPSR_I;
10833         offset = 4;
10834         break;
10835     case EXCP_DATA_ABORT:
10836         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10837         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10838         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10839                       env->exception.fsr,
10840                       (uint32_t)env->exception.vaddress);
10841         new_mode = ARM_CPU_MODE_ABT;
10842         addr = 0x10;
10843         mask = CPSR_A | CPSR_I;
10844         offset = 8;
10845         break;
10846     case EXCP_IRQ:
10847         new_mode = ARM_CPU_MODE_IRQ;
10848         addr = 0x18;
10849         /* Disable IRQ and imprecise data aborts.  */
10850         mask = CPSR_A | CPSR_I;
10851         offset = 4;
10852         if (env->cp15.scr_el3 & SCR_IRQ) {
10853             /* IRQ routed to monitor mode */
10854             new_mode = ARM_CPU_MODE_MON;
10855             mask |= CPSR_F;
10856         }
10857         break;
10858     case EXCP_FIQ:
10859         new_mode = ARM_CPU_MODE_FIQ;
10860         addr = 0x1c;
10861         /* Disable FIQ, IRQ and imprecise data aborts.  */
10862         mask = CPSR_A | CPSR_I | CPSR_F;
10863         if (env->cp15.scr_el3 & SCR_FIQ) {
10864             /* FIQ routed to monitor mode */
10865             new_mode = ARM_CPU_MODE_MON;
10866         }
10867         offset = 4;
10868         break;
10869     case EXCP_VIRQ:
10870         new_mode = ARM_CPU_MODE_IRQ;
10871         addr = 0x18;
10872         /* Disable IRQ and imprecise data aborts.  */
10873         mask = CPSR_A | CPSR_I;
10874         offset = 4;
10875         break;
10876     case EXCP_VFIQ:
10877         new_mode = ARM_CPU_MODE_FIQ;
10878         addr = 0x1c;
10879         /* Disable FIQ, IRQ and imprecise data aborts.  */
10880         mask = CPSR_A | CPSR_I | CPSR_F;
10881         offset = 4;
10882         break;
10883     case EXCP_VSERR:
10884         {
10885             /*
10886              * Note that this is reported as a data abort, but the DFAR
10887              * has an UNKNOWN value.  Construct the SError syndrome from
10888              * AET and ExT fields.
10889              */
10890             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10891 
10892             if (extended_addresses_enabled(env)) {
10893                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10894             } else {
10895                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10896             }
10897             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10898             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10899             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10900                           env->exception.fsr);
10901 
10902             new_mode = ARM_CPU_MODE_ABT;
10903             addr = 0x10;
10904             mask = CPSR_A | CPSR_I;
10905             offset = 8;
10906         }
10907         break;
10908     case EXCP_SMC:
10909         new_mode = ARM_CPU_MODE_MON;
10910         addr = 0x08;
10911         mask = CPSR_A | CPSR_I | CPSR_F;
10912         offset = 0;
10913         break;
10914     default:
10915         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10916         return; /* Never happens.  Keep compiler happy.  */
10917     }
10918 
10919     if (new_mode == ARM_CPU_MODE_MON) {
10920         addr += env->cp15.mvbar;
10921     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10922         /* High vectors. When enabled, base address cannot be remapped. */
10923         addr += 0xffff0000;
10924     } else {
10925         /*
10926          * ARM v7 architectures provide a vector base address register to remap
10927          * the interrupt vector table.
10928          * This register is only followed in non-monitor mode, and is banked.
10929          * Note: only bits 31:5 are valid.
10930          */
10931         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10932     }
10933 
10934     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10935         env->cp15.scr_el3 &= ~SCR_NS;
10936     }
10937 
10938     take_aarch32_exception(env, new_mode, mask, offset, addr);
10939 }
10940 
10941 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10942 {
10943     /*
10944      * Return the register number of the AArch64 view of the AArch32
10945      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10946      * be that of the AArch32 mode the exception came from.
10947      */
10948     int mode = env->uncached_cpsr & CPSR_M;
10949 
10950     switch (aarch32_reg) {
10951     case 0 ... 7:
10952         return aarch32_reg;
10953     case 8 ... 12:
10954         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10955     case 13:
10956         switch (mode) {
10957         case ARM_CPU_MODE_USR:
10958         case ARM_CPU_MODE_SYS:
10959             return 13;
10960         case ARM_CPU_MODE_HYP:
10961             return 15;
10962         case ARM_CPU_MODE_IRQ:
10963             return 17;
10964         case ARM_CPU_MODE_SVC:
10965             return 19;
10966         case ARM_CPU_MODE_ABT:
10967             return 21;
10968         case ARM_CPU_MODE_UND:
10969             return 23;
10970         case ARM_CPU_MODE_FIQ:
10971             return 29;
10972         default:
10973             g_assert_not_reached();
10974         }
10975     case 14:
10976         switch (mode) {
10977         case ARM_CPU_MODE_USR:
10978         case ARM_CPU_MODE_SYS:
10979         case ARM_CPU_MODE_HYP:
10980             return 14;
10981         case ARM_CPU_MODE_IRQ:
10982             return 16;
10983         case ARM_CPU_MODE_SVC:
10984             return 18;
10985         case ARM_CPU_MODE_ABT:
10986             return 20;
10987         case ARM_CPU_MODE_UND:
10988             return 22;
10989         case ARM_CPU_MODE_FIQ:
10990             return 30;
10991         default:
10992             g_assert_not_reached();
10993         }
10994     case 15:
10995         return 31;
10996     default:
10997         g_assert_not_reached();
10998     }
10999 }
11000 
11001 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11002 {
11003     uint32_t ret = cpsr_read(env);
11004 
11005     /* Move DIT to the correct location for SPSR_ELx */
11006     if (ret & CPSR_DIT) {
11007         ret &= ~CPSR_DIT;
11008         ret |= PSTATE_DIT;
11009     }
11010     /* Merge PSTATE.SS into SPSR_ELx */
11011     ret |= env->pstate & PSTATE_SS;
11012 
11013     return ret;
11014 }
11015 
11016 static bool syndrome_is_sync_extabt(uint32_t syndrome)
11017 {
11018     /* Return true if this syndrome value is a synchronous external abort */
11019     switch (syn_get_ec(syndrome)) {
11020     case EC_INSNABORT:
11021     case EC_INSNABORT_SAME_EL:
11022     case EC_DATAABORT:
11023     case EC_DATAABORT_SAME_EL:
11024         /* Look at fault status code for all the synchronous ext abort cases */
11025         switch (syndrome & 0x3f) {
11026         case 0x10:
11027         case 0x13:
11028         case 0x14:
11029         case 0x15:
11030         case 0x16:
11031         case 0x17:
11032             return true;
11033         default:
11034             return false;
11035         }
11036     default:
11037         return false;
11038     }
11039 }
11040 
11041 /* Handle exception entry to a target EL which is using AArch64 */
11042 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
11043 {
11044     ARMCPU *cpu = ARM_CPU(cs);
11045     CPUARMState *env = &cpu->env;
11046     unsigned int new_el = env->exception.target_el;
11047     target_ulong addr = env->cp15.vbar_el[new_el];
11048     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
11049     unsigned int old_mode;
11050     unsigned int cur_el = arm_current_el(env);
11051     int rt;
11052 
11053     if (tcg_enabled()) {
11054         /*
11055          * Note that new_el can never be 0.  If cur_el is 0, then
11056          * el0_a64 is is_a64(), else el0_a64 is ignored.
11057          */
11058         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11059     }
11060 
11061     if (cur_el < new_el) {
11062         /*
11063          * Entry vector offset depends on whether the implemented EL
11064          * immediately lower than the target level is using AArch32 or AArch64
11065          */
11066         bool is_aa64;
11067         uint64_t hcr;
11068 
11069         switch (new_el) {
11070         case 3:
11071             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11072             break;
11073         case 2:
11074             hcr = arm_hcr_el2_eff(env);
11075             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11076                 is_aa64 = (hcr & HCR_RW) != 0;
11077                 break;
11078             }
11079             /* fall through */
11080         case 1:
11081             is_aa64 = is_a64(env);
11082             break;
11083         default:
11084             g_assert_not_reached();
11085         }
11086 
11087         if (is_aa64) {
11088             addr += 0x400;
11089         } else {
11090             addr += 0x600;
11091         }
11092     } else if (pstate_read(env) & PSTATE_SP) {
11093         addr += 0x200;
11094     }
11095 
11096     switch (cs->exception_index) {
11097     case EXCP_GPC:
11098         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11099                       env->cp15.mfar_el3);
11100         /* fall through */
11101     case EXCP_PREFETCH_ABORT:
11102     case EXCP_DATA_ABORT:
11103         /*
11104          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11105          * to be taken to the SError vector entrypoint.
11106          */
11107         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11108             syndrome_is_sync_extabt(env->exception.syndrome)) {
11109             addr += 0x180;
11110         }
11111         env->cp15.far_el[new_el] = env->exception.vaddress;
11112         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11113                       env->cp15.far_el[new_el]);
11114         /* fall through */
11115     case EXCP_BKPT:
11116     case EXCP_UDEF:
11117     case EXCP_SWI:
11118     case EXCP_HVC:
11119     case EXCP_HYP_TRAP:
11120     case EXCP_SMC:
11121         switch (syn_get_ec(env->exception.syndrome)) {
11122         case EC_ADVSIMDFPACCESSTRAP:
11123             /*
11124              * QEMU internal FP/SIMD syndromes from AArch32 include the
11125              * TA and coproc fields which are only exposed if the exception
11126              * is taken to AArch32 Hyp mode. Mask them out to get a valid
11127              * AArch64 format syndrome.
11128              */
11129             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
11130             break;
11131         case EC_CP14RTTRAP:
11132         case EC_CP15RTTRAP:
11133         case EC_CP14DTTRAP:
11134             /*
11135              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11136              * the raw register field from the insn; when taking this to
11137              * AArch64 we must convert it to the AArch64 view of the register
11138              * number. Notice that we read a 4-bit AArch32 register number and
11139              * write back a 5-bit AArch64 one.
11140              */
11141             rt = extract32(env->exception.syndrome, 5, 4);
11142             rt = aarch64_regnum(env, rt);
11143             env->exception.syndrome = deposit32(env->exception.syndrome,
11144                                                 5, 5, rt);
11145             break;
11146         case EC_CP15RRTTRAP:
11147         case EC_CP14RRTTRAP:
11148             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11149             rt = extract32(env->exception.syndrome, 5, 4);
11150             rt = aarch64_regnum(env, rt);
11151             env->exception.syndrome = deposit32(env->exception.syndrome,
11152                                                 5, 5, rt);
11153             rt = extract32(env->exception.syndrome, 10, 4);
11154             rt = aarch64_regnum(env, rt);
11155             env->exception.syndrome = deposit32(env->exception.syndrome,
11156                                                 10, 5, rt);
11157             break;
11158         }
11159         env->cp15.esr_el[new_el] = env->exception.syndrome;
11160         break;
11161     case EXCP_IRQ:
11162     case EXCP_VIRQ:
11163         addr += 0x80;
11164         break;
11165     case EXCP_FIQ:
11166     case EXCP_VFIQ:
11167         addr += 0x100;
11168         break;
11169     case EXCP_VSERR:
11170         addr += 0x180;
11171         /* Construct the SError syndrome from IDS and ISS fields. */
11172         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11173         env->cp15.esr_el[new_el] = env->exception.syndrome;
11174         break;
11175     default:
11176         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11177     }
11178 
11179     if (is_a64(env)) {
11180         old_mode = pstate_read(env);
11181         aarch64_save_sp(env, arm_current_el(env));
11182         env->elr_el[new_el] = env->pc;
11183     } else {
11184         old_mode = cpsr_read_for_spsr_elx(env);
11185         env->elr_el[new_el] = env->regs[15];
11186 
11187         aarch64_sync_32_to_64(env);
11188 
11189         env->condexec_bits = 0;
11190     }
11191     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11192 
11193     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11194                   env->elr_el[new_el]);
11195 
11196     if (cpu_isar_feature(aa64_pan, cpu)) {
11197         /* The value of PSTATE.PAN is normally preserved, except when ... */
11198         new_mode |= old_mode & PSTATE_PAN;
11199         switch (new_el) {
11200         case 2:
11201             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
11202             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11203                 != (HCR_E2H | HCR_TGE)) {
11204                 break;
11205             }
11206             /* fall through */
11207         case 1:
11208             /* ... the target is EL1 ... */
11209             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
11210             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11211                 new_mode |= PSTATE_PAN;
11212             }
11213             break;
11214         }
11215     }
11216     if (cpu_isar_feature(aa64_mte, cpu)) {
11217         new_mode |= PSTATE_TCO;
11218     }
11219 
11220     if (cpu_isar_feature(aa64_ssbs, cpu)) {
11221         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11222             new_mode |= PSTATE_SSBS;
11223         } else {
11224             new_mode &= ~PSTATE_SSBS;
11225         }
11226     }
11227 
11228     pstate_write(env, PSTATE_DAIF | new_mode);
11229     env->aarch64 = true;
11230     aarch64_restore_sp(env, new_el);
11231 
11232     if (tcg_enabled()) {
11233         helper_rebuild_hflags_a64(env, new_el);
11234     }
11235 
11236     env->pc = addr;
11237 
11238     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11239                   new_el, env->pc, pstate_read(env));
11240 }
11241 
11242 /*
11243  * Do semihosting call and set the appropriate return value. All the
11244  * permission and validity checks have been done at translate time.
11245  *
11246  * We only see semihosting exceptions in TCG only as they are not
11247  * trapped to the hypervisor in KVM.
11248  */
11249 #ifdef CONFIG_TCG
11250 static void tcg_handle_semihosting(CPUState *cs)
11251 {
11252     ARMCPU *cpu = ARM_CPU(cs);
11253     CPUARMState *env = &cpu->env;
11254 
11255     if (is_a64(env)) {
11256         qemu_log_mask(CPU_LOG_INT,
11257                       "...handling as semihosting call 0x%" PRIx64 "\n",
11258                       env->xregs[0]);
11259         do_common_semihosting(cs);
11260         env->pc += 4;
11261     } else {
11262         qemu_log_mask(CPU_LOG_INT,
11263                       "...handling as semihosting call 0x%x\n",
11264                       env->regs[0]);
11265         do_common_semihosting(cs);
11266         env->regs[15] += env->thumb ? 2 : 4;
11267     }
11268 }
11269 #endif
11270 
11271 /*
11272  * Handle a CPU exception for A and R profile CPUs.
11273  * Do any appropriate logging, handle PSCI calls, and then hand off
11274  * to the AArch64-entry or AArch32-entry function depending on the
11275  * target exception level's register width.
11276  *
11277  * Note: this is used for both TCG (as the do_interrupt tcg op),
11278  *       and KVM to re-inject guest debug exceptions, and to
11279  *       inject a Synchronous-External-Abort.
11280  */
11281 void arm_cpu_do_interrupt(CPUState *cs)
11282 {
11283     ARMCPU *cpu = ARM_CPU(cs);
11284     CPUARMState *env = &cpu->env;
11285     unsigned int new_el = env->exception.target_el;
11286 
11287     assert(!arm_feature(env, ARM_FEATURE_M));
11288 
11289     arm_log_exception(cs);
11290     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11291                   new_el);
11292     if (qemu_loglevel_mask(CPU_LOG_INT)
11293         && !excp_is_internal(cs->exception_index)) {
11294         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
11295                       syn_get_ec(env->exception.syndrome),
11296                       env->exception.syndrome);
11297     }
11298 
11299     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
11300         arm_handle_psci_call(cpu);
11301         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11302         return;
11303     }
11304 
11305     /*
11306      * Semihosting semantics depend on the register width of the code
11307      * that caused the exception, not the target exception level, so
11308      * must be handled here.
11309      */
11310 #ifdef CONFIG_TCG
11311     if (cs->exception_index == EXCP_SEMIHOST) {
11312         tcg_handle_semihosting(cs);
11313         return;
11314     }
11315 #endif
11316 
11317     /*
11318      * Hooks may change global state so BQL should be held, also the
11319      * BQL needs to be held for any modification of
11320      * cs->interrupt_request.
11321      */
11322     g_assert(qemu_mutex_iothread_locked());
11323 
11324     arm_call_pre_el_change_hook(cpu);
11325 
11326     assert(!excp_is_internal(cs->exception_index));
11327     if (arm_el_is_aa64(env, new_el)) {
11328         arm_cpu_do_interrupt_aarch64(cs);
11329     } else {
11330         arm_cpu_do_interrupt_aarch32(cs);
11331     }
11332 
11333     arm_call_el_change_hook(cpu);
11334 
11335     if (!kvm_enabled()) {
11336         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11337     }
11338 }
11339 #endif /* !CONFIG_USER_ONLY */
11340 
11341 uint64_t arm_sctlr(CPUARMState *env, int el)
11342 {
11343     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11344     if (el == 0) {
11345         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
11346         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
11347     }
11348     return env->cp15.sctlr_el[el];
11349 }
11350 
11351 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11352 {
11353     if (regime_has_2_ranges(mmu_idx)) {
11354         return extract64(tcr, 37, 2);
11355     } else if (regime_is_stage2(mmu_idx)) {
11356         return 0; /* VTCR_EL2 */
11357     } else {
11358         /* Replicate the single TBI bit so we always have 2 bits.  */
11359         return extract32(tcr, 20, 1) * 3;
11360     }
11361 }
11362 
11363 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11364 {
11365     if (regime_has_2_ranges(mmu_idx)) {
11366         return extract64(tcr, 51, 2);
11367     } else if (regime_is_stage2(mmu_idx)) {
11368         return 0; /* VTCR_EL2 */
11369     } else {
11370         /* Replicate the single TBID bit so we always have 2 bits.  */
11371         return extract32(tcr, 29, 1) * 3;
11372     }
11373 }
11374 
11375 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11376 {
11377     if (regime_has_2_ranges(mmu_idx)) {
11378         return extract64(tcr, 57, 2);
11379     } else {
11380         /* Replicate the single TCMA bit so we always have 2 bits.  */
11381         return extract32(tcr, 30, 1) * 3;
11382     }
11383 }
11384 
11385 static ARMGranuleSize tg0_to_gran_size(int tg)
11386 {
11387     switch (tg) {
11388     case 0:
11389         return Gran4K;
11390     case 1:
11391         return Gran64K;
11392     case 2:
11393         return Gran16K;
11394     default:
11395         return GranInvalid;
11396     }
11397 }
11398 
11399 static ARMGranuleSize tg1_to_gran_size(int tg)
11400 {
11401     switch (tg) {
11402     case 1:
11403         return Gran16K;
11404     case 2:
11405         return Gran4K;
11406     case 3:
11407         return Gran64K;
11408     default:
11409         return GranInvalid;
11410     }
11411 }
11412 
11413 static inline bool have4k(ARMCPU *cpu, bool stage2)
11414 {
11415     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11416         : cpu_isar_feature(aa64_tgran4, cpu);
11417 }
11418 
11419 static inline bool have16k(ARMCPU *cpu, bool stage2)
11420 {
11421     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11422         : cpu_isar_feature(aa64_tgran16, cpu);
11423 }
11424 
11425 static inline bool have64k(ARMCPU *cpu, bool stage2)
11426 {
11427     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11428         : cpu_isar_feature(aa64_tgran64, cpu);
11429 }
11430 
11431 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11432                                          bool stage2)
11433 {
11434     switch (gran) {
11435     case Gran4K:
11436         if (have4k(cpu, stage2)) {
11437             return gran;
11438         }
11439         break;
11440     case Gran16K:
11441         if (have16k(cpu, stage2)) {
11442             return gran;
11443         }
11444         break;
11445     case Gran64K:
11446         if (have64k(cpu, stage2)) {
11447             return gran;
11448         }
11449         break;
11450     case GranInvalid:
11451         break;
11452     }
11453     /*
11454      * If the guest selects a granule size that isn't implemented,
11455      * the architecture requires that we behave as if it selected one
11456      * that is (with an IMPDEF choice of which one to pick). We choose
11457      * to implement the smallest supported granule size.
11458      */
11459     if (have4k(cpu, stage2)) {
11460         return Gran4K;
11461     }
11462     if (have16k(cpu, stage2)) {
11463         return Gran16K;
11464     }
11465     assert(have64k(cpu, stage2));
11466     return Gran64K;
11467 }
11468 
11469 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11470                                    ARMMMUIdx mmu_idx, bool data,
11471                                    bool el1_is_aa32)
11472 {
11473     uint64_t tcr = regime_tcr(env, mmu_idx);
11474     bool epd, hpd, tsz_oob, ds, ha, hd;
11475     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11476     ARMGranuleSize gran;
11477     ARMCPU *cpu = env_archcpu(env);
11478     bool stage2 = regime_is_stage2(mmu_idx);
11479 
11480     if (!regime_has_2_ranges(mmu_idx)) {
11481         select = 0;
11482         tsz = extract32(tcr, 0, 6);
11483         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11484         if (stage2) {
11485             /* VTCR_EL2 */
11486             hpd = false;
11487         } else {
11488             hpd = extract32(tcr, 24, 1);
11489         }
11490         epd = false;
11491         sh = extract32(tcr, 12, 2);
11492         ps = extract32(tcr, 16, 3);
11493         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11494         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11495         ds = extract64(tcr, 32, 1);
11496     } else {
11497         bool e0pd;
11498 
11499         /*
11500          * Bit 55 is always between the two regions, and is canonical for
11501          * determining if address tagging is enabled.
11502          */
11503         select = extract64(va, 55, 1);
11504         if (!select) {
11505             tsz = extract32(tcr, 0, 6);
11506             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11507             epd = extract32(tcr, 7, 1);
11508             sh = extract32(tcr, 12, 2);
11509             hpd = extract64(tcr, 41, 1);
11510             e0pd = extract64(tcr, 55, 1);
11511         } else {
11512             tsz = extract32(tcr, 16, 6);
11513             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11514             epd = extract32(tcr, 23, 1);
11515             sh = extract32(tcr, 28, 2);
11516             hpd = extract64(tcr, 42, 1);
11517             e0pd = extract64(tcr, 56, 1);
11518         }
11519         ps = extract64(tcr, 32, 3);
11520         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11521         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11522         ds = extract64(tcr, 59, 1);
11523 
11524         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11525             regime_is_user(env, mmu_idx)) {
11526             epd = true;
11527         }
11528     }
11529 
11530     gran = sanitize_gran_size(cpu, gran, stage2);
11531 
11532     if (cpu_isar_feature(aa64_st, cpu)) {
11533         max_tsz = 48 - (gran == Gran64K);
11534     } else {
11535         max_tsz = 39;
11536     }
11537 
11538     /*
11539      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11540      * adjust the effective value of DS, as documented.
11541      */
11542     min_tsz = 16;
11543     if (gran == Gran64K) {
11544         if (cpu_isar_feature(aa64_lva, cpu)) {
11545             min_tsz = 12;
11546         }
11547         ds = false;
11548     } else if (ds) {
11549         if (regime_is_stage2(mmu_idx)) {
11550             if (gran == Gran16K) {
11551                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11552             } else {
11553                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11554             }
11555         } else {
11556             if (gran == Gran16K) {
11557                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11558             } else {
11559                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11560             }
11561         }
11562         if (ds) {
11563             min_tsz = 12;
11564         }
11565     }
11566 
11567     if (stage2 && el1_is_aa32) {
11568         /*
11569          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11570          * are loosened: a configured IPA of 40 bits is permitted even if
11571          * the implemented PA is less than that (and so a 40 bit IPA would
11572          * fault for an AArch64 EL1). See R_DTLMN.
11573          */
11574         min_tsz = MIN(min_tsz, 24);
11575     }
11576 
11577     if (tsz > max_tsz) {
11578         tsz = max_tsz;
11579         tsz_oob = true;
11580     } else if (tsz < min_tsz) {
11581         tsz = min_tsz;
11582         tsz_oob = true;
11583     } else {
11584         tsz_oob = false;
11585     }
11586 
11587     /* Present TBI as a composite with TBID.  */
11588     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11589     if (!data) {
11590         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11591     }
11592     tbi = (tbi >> select) & 1;
11593 
11594     return (ARMVAParameters) {
11595         .tsz = tsz,
11596         .ps = ps,
11597         .sh = sh,
11598         .select = select,
11599         .tbi = tbi,
11600         .epd = epd,
11601         .hpd = hpd,
11602         .tsz_oob = tsz_oob,
11603         .ds = ds,
11604         .ha = ha,
11605         .hd = ha && hd,
11606         .gran = gran,
11607     };
11608 }
11609 
11610 /*
11611  * Note that signed overflow is undefined in C.  The following routines are
11612  * careful to use unsigned types where modulo arithmetic is required.
11613  * Failure to do so _will_ break on newer gcc.
11614  */
11615 
11616 /* Signed saturating arithmetic.  */
11617 
11618 /* Perform 16-bit signed saturating addition.  */
11619 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11620 {
11621     uint16_t res;
11622 
11623     res = a + b;
11624     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11625         if (a & 0x8000) {
11626             res = 0x8000;
11627         } else {
11628             res = 0x7fff;
11629         }
11630     }
11631     return res;
11632 }
11633 
11634 /* Perform 8-bit signed saturating addition.  */
11635 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11636 {
11637     uint8_t res;
11638 
11639     res = a + b;
11640     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11641         if (a & 0x80) {
11642             res = 0x80;
11643         } else {
11644             res = 0x7f;
11645         }
11646     }
11647     return res;
11648 }
11649 
11650 /* Perform 16-bit signed saturating subtraction.  */
11651 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11652 {
11653     uint16_t res;
11654 
11655     res = a - b;
11656     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11657         if (a & 0x8000) {
11658             res = 0x8000;
11659         } else {
11660             res = 0x7fff;
11661         }
11662     }
11663     return res;
11664 }
11665 
11666 /* Perform 8-bit signed saturating subtraction.  */
11667 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11668 {
11669     uint8_t res;
11670 
11671     res = a - b;
11672     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11673         if (a & 0x80) {
11674             res = 0x80;
11675         } else {
11676             res = 0x7f;
11677         }
11678     }
11679     return res;
11680 }
11681 
11682 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11683 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11684 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
11685 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
11686 #define PFX q
11687 
11688 #include "op_addsub.h"
11689 
11690 /* Unsigned saturating arithmetic.  */
11691 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11692 {
11693     uint16_t res;
11694     res = a + b;
11695     if (res < a) {
11696         res = 0xffff;
11697     }
11698     return res;
11699 }
11700 
11701 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11702 {
11703     if (a > b) {
11704         return a - b;
11705     } else {
11706         return 0;
11707     }
11708 }
11709 
11710 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11711 {
11712     uint8_t res;
11713     res = a + b;
11714     if (res < a) {
11715         res = 0xff;
11716     }
11717     return res;
11718 }
11719 
11720 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11721 {
11722     if (a > b) {
11723         return a - b;
11724     } else {
11725         return 0;
11726     }
11727 }
11728 
11729 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11730 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11731 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
11732 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
11733 #define PFX uq
11734 
11735 #include "op_addsub.h"
11736 
11737 /* Signed modulo arithmetic.  */
11738 #define SARITH16(a, b, n, op) do { \
11739     int32_t sum; \
11740     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
11741     RESULT(sum, n, 16); \
11742     if (sum >= 0) \
11743         ge |= 3 << (n * 2); \
11744     } while (0)
11745 
11746 #define SARITH8(a, b, n, op) do { \
11747     int32_t sum; \
11748     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
11749     RESULT(sum, n, 8); \
11750     if (sum >= 0) \
11751         ge |= 1 << n; \
11752     } while (0)
11753 
11754 
11755 #define ADD16(a, b, n) SARITH16(a, b, n, +)
11756 #define SUB16(a, b, n) SARITH16(a, b, n, -)
11757 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
11758 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
11759 #define PFX s
11760 #define ARITH_GE
11761 
11762 #include "op_addsub.h"
11763 
11764 /* Unsigned modulo arithmetic.  */
11765 #define ADD16(a, b, n) do { \
11766     uint32_t sum; \
11767     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11768     RESULT(sum, n, 16); \
11769     if ((sum >> 16) == 1) \
11770         ge |= 3 << (n * 2); \
11771     } while (0)
11772 
11773 #define ADD8(a, b, n) do { \
11774     uint32_t sum; \
11775     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11776     RESULT(sum, n, 8); \
11777     if ((sum >> 8) == 1) \
11778         ge |= 1 << n; \
11779     } while (0)
11780 
11781 #define SUB16(a, b, n) do { \
11782     uint32_t sum; \
11783     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11784     RESULT(sum, n, 16); \
11785     if ((sum >> 16) == 0) \
11786         ge |= 3 << (n * 2); \
11787     } while (0)
11788 
11789 #define SUB8(a, b, n) do { \
11790     uint32_t sum; \
11791     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11792     RESULT(sum, n, 8); \
11793     if ((sum >> 8) == 0) \
11794         ge |= 1 << n; \
11795     } while (0)
11796 
11797 #define PFX u
11798 #define ARITH_GE
11799 
11800 #include "op_addsub.h"
11801 
11802 /* Halved signed arithmetic.  */
11803 #define ADD16(a, b, n) \
11804   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11805 #define SUB16(a, b, n) \
11806   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11807 #define ADD8(a, b, n) \
11808   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11809 #define SUB8(a, b, n) \
11810   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11811 #define PFX sh
11812 
11813 #include "op_addsub.h"
11814 
11815 /* Halved unsigned arithmetic.  */
11816 #define ADD16(a, b, n) \
11817   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11818 #define SUB16(a, b, n) \
11819   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11820 #define ADD8(a, b, n) \
11821   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11822 #define SUB8(a, b, n) \
11823   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11824 #define PFX uh
11825 
11826 #include "op_addsub.h"
11827 
11828 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11829 {
11830     if (a > b) {
11831         return a - b;
11832     } else {
11833         return b - a;
11834     }
11835 }
11836 
11837 /* Unsigned sum of absolute byte differences.  */
11838 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11839 {
11840     uint32_t sum;
11841     sum = do_usad(a, b);
11842     sum += do_usad(a >> 8, b >> 8);
11843     sum += do_usad(a >> 16, b >> 16);
11844     sum += do_usad(a >> 24, b >> 24);
11845     return sum;
11846 }
11847 
11848 /* For ARMv6 SEL instruction.  */
11849 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11850 {
11851     uint32_t mask;
11852 
11853     mask = 0;
11854     if (flags & 1) {
11855         mask |= 0xff;
11856     }
11857     if (flags & 2) {
11858         mask |= 0xff00;
11859     }
11860     if (flags & 4) {
11861         mask |= 0xff0000;
11862     }
11863     if (flags & 8) {
11864         mask |= 0xff000000;
11865     }
11866     return (a & mask) | (b & ~mask);
11867 }
11868 
11869 /*
11870  * CRC helpers.
11871  * The upper bytes of val (above the number specified by 'bytes') must have
11872  * been zeroed out by the caller.
11873  */
11874 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11875 {
11876     uint8_t buf[4];
11877 
11878     stl_le_p(buf, val);
11879 
11880     /* zlib crc32 converts the accumulator and output to one's complement.  */
11881     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11882 }
11883 
11884 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11885 {
11886     uint8_t buf[4];
11887 
11888     stl_le_p(buf, val);
11889 
11890     /* Linux crc32c converts the output to one's complement.  */
11891     return crc32c(acc, buf, bytes) ^ 0xffffffff;
11892 }
11893 
11894 /*
11895  * Return the exception level to which FP-disabled exceptions should
11896  * be taken, or 0 if FP is enabled.
11897  */
11898 int fp_exception_el(CPUARMState *env, int cur_el)
11899 {
11900 #ifndef CONFIG_USER_ONLY
11901     uint64_t hcr_el2;
11902 
11903     /*
11904      * CPACR and the CPTR registers don't exist before v6, so FP is
11905      * always accessible
11906      */
11907     if (!arm_feature(env, ARM_FEATURE_V6)) {
11908         return 0;
11909     }
11910 
11911     if (arm_feature(env, ARM_FEATURE_M)) {
11912         /* CPACR can cause a NOCP UsageFault taken to current security state */
11913         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11914             return 1;
11915         }
11916 
11917         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11918             if (!extract32(env->v7m.nsacr, 10, 1)) {
11919                 /* FP insns cause a NOCP UsageFault taken to Secure */
11920                 return 3;
11921             }
11922         }
11923 
11924         return 0;
11925     }
11926 
11927     hcr_el2 = arm_hcr_el2_eff(env);
11928 
11929     /*
11930      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11931      * 0, 2 : trap EL0 and EL1/PL1 accesses
11932      * 1    : trap only EL0 accesses
11933      * 3    : trap no accesses
11934      * This register is ignored if E2H+TGE are both set.
11935      */
11936     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11937         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11938 
11939         switch (fpen) {
11940         case 1:
11941             if (cur_el != 0) {
11942                 break;
11943             }
11944             /* fall through */
11945         case 0:
11946         case 2:
11947             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11948             if (!arm_el_is_aa64(env, 3)
11949                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11950                 return 3;
11951             }
11952             if (cur_el <= 1) {
11953                 return 1;
11954             }
11955             break;
11956         }
11957     }
11958 
11959     /*
11960      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11961      * to control non-secure access to the FPU. It doesn't have any
11962      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11963      */
11964     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11965          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11966         if (!extract32(env->cp15.nsacr, 10, 1)) {
11967             /* FP insns act as UNDEF */
11968             return cur_el == 2 ? 2 : 1;
11969         }
11970     }
11971 
11972     /*
11973      * CPTR_EL2 is present in v7VE or v8, and changes format
11974      * with HCR_EL2.E2H (regardless of TGE).
11975      */
11976     if (cur_el <= 2) {
11977         if (hcr_el2 & HCR_E2H) {
11978             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11979             case 1:
11980                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11981                     break;
11982                 }
11983                 /* fall through */
11984             case 0:
11985             case 2:
11986                 return 2;
11987             }
11988         } else if (arm_is_el2_enabled(env)) {
11989             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11990                 return 2;
11991             }
11992         }
11993     }
11994 
11995     /* CPTR_EL3 : present in v8 */
11996     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11997         /* Trap all FP ops to EL3 */
11998         return 3;
11999     }
12000 #endif
12001     return 0;
12002 }
12003 
12004 /* Return the exception level we're running at if this is our mmu_idx */
12005 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12006 {
12007     if (mmu_idx & ARM_MMU_IDX_M) {
12008         return mmu_idx & ARM_MMU_IDX_M_PRIV;
12009     }
12010 
12011     switch (mmu_idx) {
12012     case ARMMMUIdx_E10_0:
12013     case ARMMMUIdx_E20_0:
12014         return 0;
12015     case ARMMMUIdx_E10_1:
12016     case ARMMMUIdx_E10_1_PAN:
12017         return 1;
12018     case ARMMMUIdx_E2:
12019     case ARMMMUIdx_E20_2:
12020     case ARMMMUIdx_E20_2_PAN:
12021         return 2;
12022     case ARMMMUIdx_E3:
12023         return 3;
12024     default:
12025         g_assert_not_reached();
12026     }
12027 }
12028 
12029 #ifndef CONFIG_TCG
12030 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12031 {
12032     g_assert_not_reached();
12033 }
12034 #endif
12035 
12036 static bool arm_pan_enabled(CPUARMState *env)
12037 {
12038     if (is_a64(env)) {
12039         return env->pstate & PSTATE_PAN;
12040     } else {
12041         return env->uncached_cpsr & CPSR_PAN;
12042     }
12043 }
12044 
12045 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12046 {
12047     ARMMMUIdx idx;
12048     uint64_t hcr;
12049 
12050     if (arm_feature(env, ARM_FEATURE_M)) {
12051         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12052     }
12053 
12054     /* See ARM pseudo-function ELIsInHost.  */
12055     switch (el) {
12056     case 0:
12057         hcr = arm_hcr_el2_eff(env);
12058         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12059             idx = ARMMMUIdx_E20_0;
12060         } else {
12061             idx = ARMMMUIdx_E10_0;
12062         }
12063         break;
12064     case 1:
12065         if (arm_pan_enabled(env)) {
12066             idx = ARMMMUIdx_E10_1_PAN;
12067         } else {
12068             idx = ARMMMUIdx_E10_1;
12069         }
12070         break;
12071     case 2:
12072         /* Note that TGE does not apply at EL2.  */
12073         if (arm_hcr_el2_eff(env) & HCR_E2H) {
12074             if (arm_pan_enabled(env)) {
12075                 idx = ARMMMUIdx_E20_2_PAN;
12076             } else {
12077                 idx = ARMMMUIdx_E20_2;
12078             }
12079         } else {
12080             idx = ARMMMUIdx_E2;
12081         }
12082         break;
12083     case 3:
12084         return ARMMMUIdx_E3;
12085     default:
12086         g_assert_not_reached();
12087     }
12088 
12089     return idx;
12090 }
12091 
12092 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12093 {
12094     return arm_mmu_idx_el(env, arm_current_el(env));
12095 }
12096 
12097 static bool mve_no_pred(CPUARMState *env)
12098 {
12099     /*
12100      * Return true if there is definitely no predication of MVE
12101      * instructions by VPR or LTPSIZE. (Returning false even if there
12102      * isn't any predication is OK; generated code will just be
12103      * a little worse.)
12104      * If the CPU does not implement MVE then this TB flag is always 0.
12105      *
12106      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12107      * logic in gen_update_fp_context() needs to be updated to match.
12108      *
12109      * We do not include the effect of the ECI bits here -- they are
12110      * tracked in other TB flags. This simplifies the logic for
12111      * "when did we emit code that changes the MVE_NO_PRED TB flag
12112      * and thus need to end the TB?".
12113      */
12114     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12115         return false;
12116     }
12117     if (env->v7m.vpr) {
12118         return false;
12119     }
12120     if (env->v7m.ltpsize < 4) {
12121         return false;
12122     }
12123     return true;
12124 }
12125 
12126 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12127                           uint64_t *cs_base, uint32_t *pflags)
12128 {
12129     CPUARMTBFlags flags;
12130 
12131     assert_hflags_rebuild_correctly(env);
12132     flags = env->hflags;
12133 
12134     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12135         *pc = env->pc;
12136         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12137             DP_TBFLAG_A64(flags, BTYPE, env->btype);
12138         }
12139     } else {
12140         *pc = env->regs[15];
12141 
12142         if (arm_feature(env, ARM_FEATURE_M)) {
12143             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12144                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12145                 != env->v7m.secure) {
12146                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12147             }
12148 
12149             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12150                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12151                  (env->v7m.secure &&
12152                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12153                 /*
12154                  * ASPEN is set, but FPCA/SFPA indicate that there is no
12155                  * active FP context; we must create a new FP context before
12156                  * executing any FP insn.
12157                  */
12158                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12159             }
12160 
12161             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12162             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12163                 DP_TBFLAG_M32(flags, LSPACT, 1);
12164             }
12165 
12166             if (mve_no_pred(env)) {
12167                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12168             }
12169         } else {
12170             /*
12171              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12172              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12173              */
12174             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12175                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12176             } else {
12177                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12178                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12179             }
12180             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12181                 DP_TBFLAG_A32(flags, VFPEN, 1);
12182             }
12183         }
12184 
12185         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12186         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12187     }
12188 
12189     /*
12190      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12191      * states defined in the ARM ARM for software singlestep:
12192      *  SS_ACTIVE   PSTATE.SS   State
12193      *     0            x       Inactive (the TB flag for SS is always 0)
12194      *     1            0       Active-pending
12195      *     1            1       Active-not-pending
12196      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12197      */
12198     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12199         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12200     }
12201 
12202     *pflags = flags.flags;
12203     *cs_base = flags.flags2;
12204 }
12205 
12206 #ifdef TARGET_AARCH64
12207 /*
12208  * The manual says that when SVE is enabled and VQ is widened the
12209  * implementation is allowed to zero the previously inaccessible
12210  * portion of the registers.  The corollary to that is that when
12211  * SVE is enabled and VQ is narrowed we are also allowed to zero
12212  * the now inaccessible portion of the registers.
12213  *
12214  * The intent of this is that no predicate bit beyond VQ is ever set.
12215  * Which means that some operations on predicate registers themselves
12216  * may operate on full uint64_t or even unrolled across the maximum
12217  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
12218  * may well be cheaper than conditionals to restrict the operation
12219  * to the relevant portion of a uint16_t[16].
12220  */
12221 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12222 {
12223     int i, j;
12224     uint64_t pmask;
12225 
12226     assert(vq >= 1 && vq <= ARM_MAX_VQ);
12227     assert(vq <= env_archcpu(env)->sve_max_vq);
12228 
12229     /* Zap the high bits of the zregs.  */
12230     for (i = 0; i < 32; i++) {
12231         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12232     }
12233 
12234     /* Zap the high bits of the pregs and ffr.  */
12235     pmask = 0;
12236     if (vq & 3) {
12237         pmask = ~(-1ULL << (16 * (vq & 3)));
12238     }
12239     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12240         for (i = 0; i < 17; ++i) {
12241             env->vfp.pregs[i].p[j] &= pmask;
12242         }
12243         pmask = 0;
12244     }
12245 }
12246 
12247 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12248 {
12249     int exc_el;
12250 
12251     if (sm) {
12252         exc_el = sme_exception_el(env, el);
12253     } else {
12254         exc_el = sve_exception_el(env, el);
12255     }
12256     if (exc_el) {
12257         return 0; /* disabled */
12258     }
12259     return sve_vqm1_for_el_sm(env, el, sm);
12260 }
12261 
12262 /*
12263  * Notice a change in SVE vector size when changing EL.
12264  */
12265 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12266                            int new_el, bool el0_a64)
12267 {
12268     ARMCPU *cpu = env_archcpu(env);
12269     int old_len, new_len;
12270     bool old_a64, new_a64, sm;
12271 
12272     /* Nothing to do if no SVE.  */
12273     if (!cpu_isar_feature(aa64_sve, cpu)) {
12274         return;
12275     }
12276 
12277     /* Nothing to do if FP is disabled in either EL.  */
12278     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12279         return;
12280     }
12281 
12282     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12283     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12284 
12285     /*
12286      * Both AArch64.TakeException and AArch64.ExceptionReturn
12287      * invoke ResetSVEState when taking an exception from, or
12288      * returning to, AArch32 state when PSTATE.SM is enabled.
12289      */
12290     sm = FIELD_EX64(env->svcr, SVCR, SM);
12291     if (old_a64 != new_a64 && sm) {
12292         arm_reset_sve_state(env);
12293         return;
12294     }
12295 
12296     /*
12297      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12298      * at ELx, or not available because the EL is in AArch32 state, then
12299      * for all purposes other than a direct read, the ZCR_ELx.LEN field
12300      * has an effective value of 0".
12301      *
12302      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12303      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12304      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
12305      * we already have the correct register contents when encountering the
12306      * vq0->vq0 transition between EL0->EL1.
12307      */
12308     old_len = new_len = 0;
12309     if (old_a64) {
12310         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12311     }
12312     if (new_a64) {
12313         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12314     }
12315 
12316     /* When changing vector length, clear inaccessible state.  */
12317     if (new_len < old_len) {
12318         aarch64_sve_narrow_vq(env, new_len + 1);
12319     }
12320 }
12321 #endif
12322 
12323 #ifndef CONFIG_USER_ONLY
12324 ARMSecuritySpace arm_security_space(CPUARMState *env)
12325 {
12326     if (arm_feature(env, ARM_FEATURE_M)) {
12327         return arm_secure_to_space(env->v7m.secure);
12328     }
12329 
12330     /*
12331      * If EL3 is not supported then the secure state is implementation
12332      * defined, in which case QEMU defaults to non-secure.
12333      */
12334     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12335         return ARMSS_NonSecure;
12336     }
12337 
12338     /* Check for AArch64 EL3 or AArch32 Mon. */
12339     if (is_a64(env)) {
12340         if (extract32(env->pstate, 2, 2) == 3) {
12341             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
12342                 return ARMSS_Root;
12343             } else {
12344                 return ARMSS_Secure;
12345             }
12346         }
12347     } else {
12348         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
12349             return ARMSS_Secure;
12350         }
12351     }
12352 
12353     return arm_security_space_below_el3(env);
12354 }
12355 
12356 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
12357 {
12358     assert(!arm_feature(env, ARM_FEATURE_M));
12359 
12360     /*
12361      * If EL3 is not supported then the secure state is implementation
12362      * defined, in which case QEMU defaults to non-secure.
12363      */
12364     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12365         return ARMSS_NonSecure;
12366     }
12367 
12368     /*
12369      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
12370      * Ignoring NSE when !NS retains consistency without having to
12371      * modify other predicates.
12372      */
12373     if (!(env->cp15.scr_el3 & SCR_NS)) {
12374         return ARMSS_Secure;
12375     } else if (env->cp15.scr_el3 & SCR_NSE) {
12376         return ARMSS_Realm;
12377     } else {
12378         return ARMSS_NonSecure;
12379     }
12380 }
12381 #endif /* !CONFIG_USER_ONLY */
12382