xref: /openbmc/qemu/target/arm/helper.c (revision 235fe6d0)
1 /*
2  * ARM generic helpers.
3  *
4  * This code is licensed under the GNU GPL v2 or later.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "trace.h"
12 #include "cpu.h"
13 #include "internals.h"
14 #include "cpu-features.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/main-loop.h"
17 #include "qemu/timer.h"
18 #include "qemu/bitops.h"
19 #include "qemu/crc32c.h"
20 #include "qemu/qemu-print.h"
21 #include "exec/exec-all.h"
22 #include <zlib.h> /* For crc32 */
23 #include "hw/irq.h"
24 #include "sysemu/cpu-timers.h"
25 #include "sysemu/kvm.h"
26 #include "sysemu/tcg.h"
27 #include "qapi/error.h"
28 #include "qemu/guest-random.h"
29 #ifdef CONFIG_TCG
30 #include "semihosting/common-semi.h"
31 #endif
32 #include "cpregs.h"
33 
34 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
35 
36 static void switch_mode(CPUARMState *env, int mode);
37 
38 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
39 {
40     assert(ri->fieldoffset);
41     if (cpreg_field_is_64bit(ri)) {
42         return CPREG_FIELD64(env, ri);
43     } else {
44         return CPREG_FIELD32(env, ri);
45     }
46 }
47 
48 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
49 {
50     assert(ri->fieldoffset);
51     if (cpreg_field_is_64bit(ri)) {
52         CPREG_FIELD64(env, ri) = value;
53     } else {
54         CPREG_FIELD32(env, ri) = value;
55     }
56 }
57 
58 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
59 {
60     return (char *)env + ri->fieldoffset;
61 }
62 
63 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
64 {
65     /* Raw read of a coprocessor register (as needed for migration, etc). */
66     if (ri->type & ARM_CP_CONST) {
67         return ri->resetvalue;
68     } else if (ri->raw_readfn) {
69         return ri->raw_readfn(env, ri);
70     } else if (ri->readfn) {
71         return ri->readfn(env, ri);
72     } else {
73         return raw_read(env, ri);
74     }
75 }
76 
77 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
78                              uint64_t v)
79 {
80     /*
81      * Raw write of a coprocessor register (as needed for migration, etc).
82      * Note that constant registers are treated as write-ignored; the
83      * caller should check for success by whether a readback gives the
84      * value written.
85      */
86     if (ri->type & ARM_CP_CONST) {
87         return;
88     } else if (ri->raw_writefn) {
89         ri->raw_writefn(env, ri, v);
90     } else if (ri->writefn) {
91         ri->writefn(env, ri, v);
92     } else {
93         raw_write(env, ri, v);
94     }
95 }
96 
97 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
98 {
99    /*
100     * Return true if the regdef would cause an assertion if you called
101     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
102     * program bug for it not to have the NO_RAW flag).
103     * NB that returning false here doesn't necessarily mean that calling
104     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
105     * read/write access functions which are safe for raw use" from "has
106     * read/write access functions which have side effects but has forgotten
107     * to provide raw access functions".
108     * The tests here line up with the conditions in read/write_raw_cp_reg()
109     * and assertions in raw_read()/raw_write().
110     */
111     if ((ri->type & ARM_CP_CONST) ||
112         ri->fieldoffset ||
113         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
114         return false;
115     }
116     return true;
117 }
118 
119 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
120 {
121     /* Write the coprocessor state from cpu->env to the (index,value) list. */
122     int i;
123     bool ok = true;
124 
125     for (i = 0; i < cpu->cpreg_array_len; i++) {
126         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
127         const ARMCPRegInfo *ri;
128         uint64_t newval;
129 
130         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
131         if (!ri) {
132             ok = false;
133             continue;
134         }
135         if (ri->type & ARM_CP_NO_RAW) {
136             continue;
137         }
138 
139         newval = read_raw_cp_reg(&cpu->env, ri);
140         if (kvm_sync) {
141             /*
142              * Only sync if the previous list->cpustate sync succeeded.
143              * Rather than tracking the success/failure state for every
144              * item in the list, we just recheck "does the raw write we must
145              * have made in write_list_to_cpustate() read back OK" here.
146              */
147             uint64_t oldval = cpu->cpreg_values[i];
148 
149             if (oldval == newval) {
150                 continue;
151             }
152 
153             write_raw_cp_reg(&cpu->env, ri, oldval);
154             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
155                 continue;
156             }
157 
158             write_raw_cp_reg(&cpu->env, ri, newval);
159         }
160         cpu->cpreg_values[i] = newval;
161     }
162     return ok;
163 }
164 
165 bool write_list_to_cpustate(ARMCPU *cpu)
166 {
167     int i;
168     bool ok = true;
169 
170     for (i = 0; i < cpu->cpreg_array_len; i++) {
171         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
172         uint64_t v = cpu->cpreg_values[i];
173         const ARMCPRegInfo *ri;
174 
175         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
176         if (!ri) {
177             ok = false;
178             continue;
179         }
180         if (ri->type & ARM_CP_NO_RAW) {
181             continue;
182         }
183         /*
184          * Write value and confirm it reads back as written
185          * (to catch read-only registers and partially read-only
186          * registers where the incoming migration value doesn't match)
187          */
188         write_raw_cp_reg(&cpu->env, ri, v);
189         if (read_raw_cp_reg(&cpu->env, ri) != v) {
190             ok = false;
191         }
192     }
193     return ok;
194 }
195 
196 static void add_cpreg_to_list(gpointer key, gpointer opaque)
197 {
198     ARMCPU *cpu = opaque;
199     uint32_t regidx = (uintptr_t)key;
200     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
201 
202     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
203         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
204         /* The value array need not be initialized at this point */
205         cpu->cpreg_array_len++;
206     }
207 }
208 
209 static void count_cpreg(gpointer key, gpointer opaque)
210 {
211     ARMCPU *cpu = opaque;
212     const ARMCPRegInfo *ri;
213 
214     ri = g_hash_table_lookup(cpu->cp_regs, key);
215 
216     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
217         cpu->cpreg_array_len++;
218     }
219 }
220 
221 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
222 {
223     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
224     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
225 
226     if (aidx > bidx) {
227         return 1;
228     }
229     if (aidx < bidx) {
230         return -1;
231     }
232     return 0;
233 }
234 
235 void init_cpreg_list(ARMCPU *cpu)
236 {
237     /*
238      * Initialise the cpreg_tuples[] array based on the cp_regs hash.
239      * Note that we require cpreg_tuples[] to be sorted by key ID.
240      */
241     GList *keys;
242     int arraylen;
243 
244     keys = g_hash_table_get_keys(cpu->cp_regs);
245     keys = g_list_sort(keys, cpreg_key_compare);
246 
247     cpu->cpreg_array_len = 0;
248 
249     g_list_foreach(keys, count_cpreg, cpu);
250 
251     arraylen = cpu->cpreg_array_len;
252     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
253     cpu->cpreg_values = g_new(uint64_t, arraylen);
254     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
255     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
256     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
257     cpu->cpreg_array_len = 0;
258 
259     g_list_foreach(keys, add_cpreg_to_list, cpu);
260 
261     assert(cpu->cpreg_array_len == arraylen);
262 
263     g_list_free(keys);
264 }
265 
266 /*
267  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
268  */
269 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
270                                         const ARMCPRegInfo *ri,
271                                         bool isread)
272 {
273     if (!is_a64(env) && arm_current_el(env) == 3 &&
274         arm_is_secure_below_el3(env)) {
275         return CP_ACCESS_TRAP_UNCATEGORIZED;
276     }
277     return CP_ACCESS_OK;
278 }
279 
280 /*
281  * Some secure-only AArch32 registers trap to EL3 if used from
282  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
283  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
284  * We assume that the .access field is set to PL1_RW.
285  */
286 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
287                                             const ARMCPRegInfo *ri,
288                                             bool isread)
289 {
290     if (arm_current_el(env) == 3) {
291         return CP_ACCESS_OK;
292     }
293     if (arm_is_secure_below_el3(env)) {
294         if (env->cp15.scr_el3 & SCR_EEL2) {
295             return CP_ACCESS_TRAP_EL2;
296         }
297         return CP_ACCESS_TRAP_EL3;
298     }
299     /* This will be EL1 NS and EL2 NS, which just UNDEF */
300     return CP_ACCESS_TRAP_UNCATEGORIZED;
301 }
302 
303 /*
304  * Check for traps to performance monitor registers, which are controlled
305  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
306  */
307 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
308                                  bool isread)
309 {
310     int el = arm_current_el(env);
311     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
312 
313     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
314         return CP_ACCESS_TRAP_EL2;
315     }
316     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
317         return CP_ACCESS_TRAP_EL3;
318     }
319     return CP_ACCESS_OK;
320 }
321 
322 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
323 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
324                                bool isread)
325 {
326     if (arm_current_el(env) == 1) {
327         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
328         if (arm_hcr_el2_eff(env) & trap) {
329             return CP_ACCESS_TRAP_EL2;
330         }
331     }
332     return CP_ACCESS_OK;
333 }
334 
335 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
336 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
337                                  bool isread)
338 {
339     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
340         return CP_ACCESS_TRAP_EL2;
341     }
342     return CP_ACCESS_OK;
343 }
344 
345 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
346 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
347                                   bool isread)
348 {
349     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
350         return CP_ACCESS_TRAP_EL2;
351     }
352     return CP_ACCESS_OK;
353 }
354 
355 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
356 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
357                                   bool isread)
358 {
359     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
360         return CP_ACCESS_TRAP_EL2;
361     }
362     return CP_ACCESS_OK;
363 }
364 
365 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
366 static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
367                                     bool isread)
368 {
369     if (arm_current_el(env) == 1 &&
370         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
371         return CP_ACCESS_TRAP_EL2;
372     }
373     return CP_ACCESS_OK;
374 }
375 
376 #ifdef TARGET_AARCH64
377 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
378 static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
379                                     bool isread)
380 {
381     if (arm_current_el(env) == 1 &&
382         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
383         return CP_ACCESS_TRAP_EL2;
384     }
385     return CP_ACCESS_OK;
386 }
387 #endif
388 
389 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
390 {
391     ARMCPU *cpu = env_archcpu(env);
392 
393     raw_write(env, ri, value);
394     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
395 }
396 
397 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
398 {
399     ARMCPU *cpu = env_archcpu(env);
400 
401     if (raw_read(env, ri) != value) {
402         /*
403          * Unlike real hardware the qemu TLB uses virtual addresses,
404          * not modified virtual addresses, so this causes a TLB flush.
405          */
406         tlb_flush(CPU(cpu));
407         raw_write(env, ri, value);
408     }
409 }
410 
411 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
412                              uint64_t value)
413 {
414     ARMCPU *cpu = env_archcpu(env);
415 
416     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
417         && !extended_addresses_enabled(env)) {
418         /*
419          * For VMSA (when not using the LPAE long descriptor page table
420          * format) this register includes the ASID, so do a TLB flush.
421          * For PMSA it is purely a process ID and no action is needed.
422          */
423         tlb_flush(CPU(cpu));
424     }
425     raw_write(env, ri, value);
426 }
427 
428 static int alle1_tlbmask(CPUARMState *env)
429 {
430     /*
431      * Note that the 'ALL' scope must invalidate both stage 1 and
432      * stage 2 translations, whereas most other scopes only invalidate
433      * stage 1 translations.
434      */
435     return (ARMMMUIdxBit_E10_1 |
436             ARMMMUIdxBit_E10_1_PAN |
437             ARMMMUIdxBit_E10_0 |
438             ARMMMUIdxBit_Stage2 |
439             ARMMMUIdxBit_Stage2_S);
440 }
441 
442 
443 /* IS variants of TLB operations must affect all cores */
444 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
445                              uint64_t value)
446 {
447     CPUState *cs = env_cpu(env);
448 
449     tlb_flush_all_cpus_synced(cs);
450 }
451 
452 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
453                              uint64_t value)
454 {
455     CPUState *cs = env_cpu(env);
456 
457     tlb_flush_all_cpus_synced(cs);
458 }
459 
460 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
461                              uint64_t value)
462 {
463     CPUState *cs = env_cpu(env);
464 
465     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
466 }
467 
468 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
469                              uint64_t value)
470 {
471     CPUState *cs = env_cpu(env);
472 
473     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
474 }
475 
476 /*
477  * Non-IS variants of TLB operations are upgraded to
478  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
479  * force broadcast of these operations.
480  */
481 static bool tlb_force_broadcast(CPUARMState *env)
482 {
483     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
484 }
485 
486 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
487                           uint64_t value)
488 {
489     /* Invalidate all (TLBIALL) */
490     CPUState *cs = env_cpu(env);
491 
492     if (tlb_force_broadcast(env)) {
493         tlb_flush_all_cpus_synced(cs);
494     } else {
495         tlb_flush(cs);
496     }
497 }
498 
499 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
500                           uint64_t value)
501 {
502     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
503     CPUState *cs = env_cpu(env);
504 
505     value &= TARGET_PAGE_MASK;
506     if (tlb_force_broadcast(env)) {
507         tlb_flush_page_all_cpus_synced(cs, value);
508     } else {
509         tlb_flush_page(cs, value);
510     }
511 }
512 
513 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
514                            uint64_t value)
515 {
516     /* Invalidate by ASID (TLBIASID) */
517     CPUState *cs = env_cpu(env);
518 
519     if (tlb_force_broadcast(env)) {
520         tlb_flush_all_cpus_synced(cs);
521     } else {
522         tlb_flush(cs);
523     }
524 }
525 
526 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
527                            uint64_t value)
528 {
529     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
530     CPUState *cs = env_cpu(env);
531 
532     value &= TARGET_PAGE_MASK;
533     if (tlb_force_broadcast(env)) {
534         tlb_flush_page_all_cpus_synced(cs, value);
535     } else {
536         tlb_flush_page(cs, value);
537     }
538 }
539 
540 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
541                                uint64_t value)
542 {
543     CPUState *cs = env_cpu(env);
544 
545     tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
546 }
547 
548 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
549                                   uint64_t value)
550 {
551     CPUState *cs = env_cpu(env);
552 
553     tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
554 }
555 
556 
557 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
558                               uint64_t value)
559 {
560     CPUState *cs = env_cpu(env);
561 
562     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
563 }
564 
565 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
566                                  uint64_t value)
567 {
568     CPUState *cs = env_cpu(env);
569 
570     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
571 }
572 
573 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
574                               uint64_t value)
575 {
576     CPUState *cs = env_cpu(env);
577     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
578 
579     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
580 }
581 
582 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
583                                  uint64_t value)
584 {
585     CPUState *cs = env_cpu(env);
586     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
587 
588     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
589                                              ARMMMUIdxBit_E2);
590 }
591 
592 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
593                                 uint64_t value)
594 {
595     CPUState *cs = env_cpu(env);
596     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
597 
598     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
599 }
600 
601 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
602                                 uint64_t value)
603 {
604     CPUState *cs = env_cpu(env);
605     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
606 
607     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
608 }
609 
610 static const ARMCPRegInfo cp_reginfo[] = {
611     /*
612      * Define the secure and non-secure FCSE identifier CP registers
613      * separately because there is no secure bank in V8 (no _EL3).  This allows
614      * the secure register to be properly reset and migrated. There is also no
615      * v8 EL1 version of the register so the non-secure instance stands alone.
616      */
617     { .name = "FCSEIDR",
618       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
619       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
620       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
621       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
622     { .name = "FCSEIDR_S",
623       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
624       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
625       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
626       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
627     /*
628      * Define the secure and non-secure context identifier CP registers
629      * separately because there is no secure bank in V8 (no _EL3).  This allows
630      * the secure register to be properly reset and migrated.  In the
631      * non-secure case, the 32-bit register will have reset and migration
632      * disabled during registration as it is handled by the 64-bit instance.
633      */
634     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
635       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
636       .access = PL1_RW, .accessfn = access_tvm_trvm,
637       .fgt = FGT_CONTEXTIDR_EL1,
638       .secure = ARM_CP_SECSTATE_NS,
639       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
640       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
641     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
642       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
643       .access = PL1_RW, .accessfn = access_tvm_trvm,
644       .secure = ARM_CP_SECSTATE_S,
645       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
646       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
647 };
648 
649 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
650     /*
651      * NB: Some of these registers exist in v8 but with more precise
652      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
653      */
654     /* MMU Domain access control / MPU write buffer control */
655     { .name = "DACR",
656       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
657       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
658       .writefn = dacr_write, .raw_writefn = raw_write,
659       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
660                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
661     /*
662      * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
663      * For v6 and v5, these mappings are overly broad.
664      */
665     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
666       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
667     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
668       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
669     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
670       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
671     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
672       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
673     /* Cache maintenance ops; some of this space may be overridden later. */
674     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
675       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
676       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
677 };
678 
679 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
680     /*
681      * Not all pre-v6 cores implemented this WFI, so this is slightly
682      * over-broad.
683      */
684     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
685       .access = PL1_W, .type = ARM_CP_WFI },
686 };
687 
688 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
689     /*
690      * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
691      * is UNPREDICTABLE; we choose to NOP as most implementations do).
692      */
693     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
694       .access = PL1_W, .type = ARM_CP_WFI },
695     /*
696      * L1 cache lockdown. Not architectural in v6 and earlier but in practice
697      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
698      * OMAPCP will override this space.
699      */
700     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
701       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
702       .resetvalue = 0 },
703     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
704       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
705       .resetvalue = 0 },
706     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
707     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
708       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
709       .resetvalue = 0 },
710     /*
711      * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
712      * implementing it as RAZ means the "debug architecture version" bits
713      * will read as a reserved value, which should cause Linux to not try
714      * to use the debug hardware.
715      */
716     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
717       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
718     /*
719      * MMU TLB control. Note that the wildcarding means we cover not just
720      * the unified TLB ops but also the dside/iside/inner-shareable variants.
721      */
722     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
723       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
724       .type = ARM_CP_NO_RAW },
725     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
726       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
727       .type = ARM_CP_NO_RAW },
728     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
729       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
730       .type = ARM_CP_NO_RAW },
731     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
732       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
733       .type = ARM_CP_NO_RAW },
734     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
735       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
736     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
737       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
738 };
739 
740 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
741                         uint64_t value)
742 {
743     uint32_t mask = 0;
744 
745     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
746     if (!arm_feature(env, ARM_FEATURE_V8)) {
747         /*
748          * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
749          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
750          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
751          */
752         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
753             /* VFP coprocessor: cp10 & cp11 [23:20] */
754             mask |= R_CPACR_ASEDIS_MASK |
755                     R_CPACR_D32DIS_MASK |
756                     R_CPACR_CP11_MASK |
757                     R_CPACR_CP10_MASK;
758 
759             if (!arm_feature(env, ARM_FEATURE_NEON)) {
760                 /* ASEDIS [31] bit is RAO/WI */
761                 value |= R_CPACR_ASEDIS_MASK;
762             }
763 
764             /*
765              * VFPv3 and upwards with NEON implement 32 double precision
766              * registers (D0-D31).
767              */
768             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
769                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
770                 value |= R_CPACR_D32DIS_MASK;
771             }
772         }
773         value &= mask;
774     }
775 
776     /*
777      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
778      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
779      */
780     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
781         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
782         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
783         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
784     }
785 
786     env->cp15.cpacr_el1 = value;
787 }
788 
789 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
790 {
791     /*
792      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
793      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
794      */
795     uint64_t value = env->cp15.cpacr_el1;
796 
797     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
798         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
799         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
800     }
801     return value;
802 }
803 
804 
805 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
806 {
807     /*
808      * Call cpacr_write() so that we reset with the correct RAO bits set
809      * for our CPU features.
810      */
811     cpacr_write(env, ri, 0);
812 }
813 
814 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
815                                    bool isread)
816 {
817     if (arm_feature(env, ARM_FEATURE_V8)) {
818         /* Check if CPACR accesses are to be trapped to EL2 */
819         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
820             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
821             return CP_ACCESS_TRAP_EL2;
822         /* Check if CPACR accesses are to be trapped to EL3 */
823         } else if (arm_current_el(env) < 3 &&
824                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
825             return CP_ACCESS_TRAP_EL3;
826         }
827     }
828 
829     return CP_ACCESS_OK;
830 }
831 
832 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
833                                   bool isread)
834 {
835     /* Check if CPTR accesses are set to trap to EL3 */
836     if (arm_current_el(env) == 2 &&
837         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
838         return CP_ACCESS_TRAP_EL3;
839     }
840 
841     return CP_ACCESS_OK;
842 }
843 
844 static const ARMCPRegInfo v6_cp_reginfo[] = {
845     /* prefetch by MVA in v6, NOP in v7 */
846     { .name = "MVA_prefetch",
847       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
848       .access = PL1_W, .type = ARM_CP_NOP },
849     /*
850      * We need to break the TB after ISB to execute self-modifying code
851      * correctly and also to take any pending interrupts immediately.
852      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
853      */
854     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
855       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
856     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
857       .access = PL0_W, .type = ARM_CP_NOP },
858     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
859       .access = PL0_W, .type = ARM_CP_NOP },
860     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
861       .access = PL1_RW, .accessfn = access_tvm_trvm,
862       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
863                              offsetof(CPUARMState, cp15.ifar_ns) },
864       .resetvalue = 0, },
865     /*
866      * Watchpoint Fault Address Register : should actually only be present
867      * for 1136, 1176, 11MPCore.
868      */
869     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
870       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
871     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
872       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
873       .fgt = FGT_CPACR_EL1,
874       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
875       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
876 };
877 
878 typedef struct pm_event {
879     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
880     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
881     bool (*supported)(CPUARMState *);
882     /*
883      * Retrieve the current count of the underlying event. The programmed
884      * counters hold a difference from the return value from this function
885      */
886     uint64_t (*get_count)(CPUARMState *);
887     /*
888      * Return how many nanoseconds it will take (at a minimum) for count events
889      * to occur. A negative value indicates the counter will never overflow, or
890      * that the counter has otherwise arranged for the overflow bit to be set
891      * and the PMU interrupt to be raised on overflow.
892      */
893     int64_t (*ns_per_count)(uint64_t);
894 } pm_event;
895 
896 static bool event_always_supported(CPUARMState *env)
897 {
898     return true;
899 }
900 
901 static uint64_t swinc_get_count(CPUARMState *env)
902 {
903     /*
904      * SW_INCR events are written directly to the pmevcntr's by writes to
905      * PMSWINC, so there is no underlying count maintained by the PMU itself
906      */
907     return 0;
908 }
909 
910 static int64_t swinc_ns_per(uint64_t ignored)
911 {
912     return -1;
913 }
914 
915 /*
916  * Return the underlying cycle count for the PMU cycle counters. If we're in
917  * usermode, simply return 0.
918  */
919 static uint64_t cycles_get_count(CPUARMState *env)
920 {
921 #ifndef CONFIG_USER_ONLY
922     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
923                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
924 #else
925     return cpu_get_host_ticks();
926 #endif
927 }
928 
929 #ifndef CONFIG_USER_ONLY
930 static int64_t cycles_ns_per(uint64_t cycles)
931 {
932     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
933 }
934 
935 static bool instructions_supported(CPUARMState *env)
936 {
937     return icount_enabled() == 1; /* Precise instruction counting */
938 }
939 
940 static uint64_t instructions_get_count(CPUARMState *env)
941 {
942     return (uint64_t)icount_get_raw();
943 }
944 
945 static int64_t instructions_ns_per(uint64_t icount)
946 {
947     return icount_to_ns((int64_t)icount);
948 }
949 #endif
950 
951 static bool pmuv3p1_events_supported(CPUARMState *env)
952 {
953     /* For events which are supported in any v8.1 PMU */
954     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
955 }
956 
957 static bool pmuv3p4_events_supported(CPUARMState *env)
958 {
959     /* For events which are supported in any v8.1 PMU */
960     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
961 }
962 
963 static uint64_t zero_event_get_count(CPUARMState *env)
964 {
965     /* For events which on QEMU never fire, so their count is always zero */
966     return 0;
967 }
968 
969 static int64_t zero_event_ns_per(uint64_t cycles)
970 {
971     /* An event which never fires can never overflow */
972     return -1;
973 }
974 
975 static const pm_event pm_events[] = {
976     { .number = 0x000, /* SW_INCR */
977       .supported = event_always_supported,
978       .get_count = swinc_get_count,
979       .ns_per_count = swinc_ns_per,
980     },
981 #ifndef CONFIG_USER_ONLY
982     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
983       .supported = instructions_supported,
984       .get_count = instructions_get_count,
985       .ns_per_count = instructions_ns_per,
986     },
987     { .number = 0x011, /* CPU_CYCLES, Cycle */
988       .supported = event_always_supported,
989       .get_count = cycles_get_count,
990       .ns_per_count = cycles_ns_per,
991     },
992 #endif
993     { .number = 0x023, /* STALL_FRONTEND */
994       .supported = pmuv3p1_events_supported,
995       .get_count = zero_event_get_count,
996       .ns_per_count = zero_event_ns_per,
997     },
998     { .number = 0x024, /* STALL_BACKEND */
999       .supported = pmuv3p1_events_supported,
1000       .get_count = zero_event_get_count,
1001       .ns_per_count = zero_event_ns_per,
1002     },
1003     { .number = 0x03c, /* STALL */
1004       .supported = pmuv3p4_events_supported,
1005       .get_count = zero_event_get_count,
1006       .ns_per_count = zero_event_ns_per,
1007     },
1008 };
1009 
1010 /*
1011  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1012  * events (i.e. the statistical profiling extension), this implementation
1013  * should first be updated to something sparse instead of the current
1014  * supported_event_map[] array.
1015  */
1016 #define MAX_EVENT_ID 0x3c
1017 #define UNSUPPORTED_EVENT UINT16_MAX
1018 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1019 
1020 /*
1021  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1022  * of ARM event numbers to indices in our pm_events array.
1023  *
1024  * Note: Events in the 0x40XX range are not currently supported.
1025  */
1026 void pmu_init(ARMCPU *cpu)
1027 {
1028     unsigned int i;
1029 
1030     /*
1031      * Empty supported_event_map and cpu->pmceid[01] before adding supported
1032      * events to them
1033      */
1034     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1035         supported_event_map[i] = UNSUPPORTED_EVENT;
1036     }
1037     cpu->pmceid0 = 0;
1038     cpu->pmceid1 = 0;
1039 
1040     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1041         const pm_event *cnt = &pm_events[i];
1042         assert(cnt->number <= MAX_EVENT_ID);
1043         /* We do not currently support events in the 0x40xx range */
1044         assert(cnt->number <= 0x3f);
1045 
1046         if (cnt->supported(&cpu->env)) {
1047             supported_event_map[cnt->number] = i;
1048             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1049             if (cnt->number & 0x20) {
1050                 cpu->pmceid1 |= event_mask;
1051             } else {
1052                 cpu->pmceid0 |= event_mask;
1053             }
1054         }
1055     }
1056 }
1057 
1058 /*
1059  * Check at runtime whether a PMU event is supported for the current machine
1060  */
1061 static bool event_supported(uint16_t number)
1062 {
1063     if (number > MAX_EVENT_ID) {
1064         return false;
1065     }
1066     return supported_event_map[number] != UNSUPPORTED_EVENT;
1067 }
1068 
1069 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1070                                    bool isread)
1071 {
1072     /*
1073      * Performance monitor registers user accessibility is controlled
1074      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1075      * trapping to EL2 or EL3 for other accesses.
1076      */
1077     int el = arm_current_el(env);
1078     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1079 
1080     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1081         return CP_ACCESS_TRAP;
1082     }
1083     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1084         return CP_ACCESS_TRAP_EL2;
1085     }
1086     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1087         return CP_ACCESS_TRAP_EL3;
1088     }
1089 
1090     return CP_ACCESS_OK;
1091 }
1092 
1093 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1094                                            const ARMCPRegInfo *ri,
1095                                            bool isread)
1096 {
1097     /* ER: event counter read trap control */
1098     if (arm_feature(env, ARM_FEATURE_V8)
1099         && arm_current_el(env) == 0
1100         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1101         && isread) {
1102         return CP_ACCESS_OK;
1103     }
1104 
1105     return pmreg_access(env, ri, isread);
1106 }
1107 
1108 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1109                                          const ARMCPRegInfo *ri,
1110                                          bool isread)
1111 {
1112     /* SW: software increment write trap control */
1113     if (arm_feature(env, ARM_FEATURE_V8)
1114         && arm_current_el(env) == 0
1115         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1116         && !isread) {
1117         return CP_ACCESS_OK;
1118     }
1119 
1120     return pmreg_access(env, ri, isread);
1121 }
1122 
1123 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1124                                         const ARMCPRegInfo *ri,
1125                                         bool isread)
1126 {
1127     /* ER: event counter read trap control */
1128     if (arm_feature(env, ARM_FEATURE_V8)
1129         && arm_current_el(env) == 0
1130         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1131         return CP_ACCESS_OK;
1132     }
1133 
1134     return pmreg_access(env, ri, isread);
1135 }
1136 
1137 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1138                                          const ARMCPRegInfo *ri,
1139                                          bool isread)
1140 {
1141     /* CR: cycle counter read trap control */
1142     if (arm_feature(env, ARM_FEATURE_V8)
1143         && arm_current_el(env) == 0
1144         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1145         && isread) {
1146         return CP_ACCESS_OK;
1147     }
1148 
1149     return pmreg_access(env, ri, isread);
1150 }
1151 
1152 /*
1153  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1154  * We use these to decide whether we need to wrap a write to MDCR_EL2
1155  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1156  */
1157 #define MDCR_EL2_PMU_ENABLE_BITS \
1158     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1159 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1160 
1161 /*
1162  * Returns true if the counter (pass 31 for PMCCNTR) should count events using
1163  * the current EL, security state, and register configuration.
1164  */
1165 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1166 {
1167     uint64_t filter;
1168     bool e, p, u, nsk, nsu, nsh, m;
1169     bool enabled, prohibited = false, filtered;
1170     bool secure = arm_is_secure(env);
1171     int el = arm_current_el(env);
1172     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1173     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1174 
1175     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1176         return false;
1177     }
1178 
1179     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1180             (counter < hpmn || counter == 31)) {
1181         e = env->cp15.c9_pmcr & PMCRE;
1182     } else {
1183         e = mdcr_el2 & MDCR_HPME;
1184     }
1185     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1186 
1187     /* Is event counting prohibited? */
1188     if (el == 2 && (counter < hpmn || counter == 31)) {
1189         prohibited = mdcr_el2 & MDCR_HPMD;
1190     }
1191     if (secure) {
1192         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1193     }
1194 
1195     if (counter == 31) {
1196         /*
1197          * The cycle counter defaults to running. PMCR.DP says "disable
1198          * the cycle counter when event counting is prohibited".
1199          * Some MDCR bits disable the cycle counter specifically.
1200          */
1201         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1202         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1203             if (secure) {
1204                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1205             }
1206             if (el == 2) {
1207                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1208             }
1209         }
1210     }
1211 
1212     if (counter == 31) {
1213         filter = env->cp15.pmccfiltr_el0;
1214     } else {
1215         filter = env->cp15.c14_pmevtyper[counter];
1216     }
1217 
1218     p   = filter & PMXEVTYPER_P;
1219     u   = filter & PMXEVTYPER_U;
1220     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1221     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1222     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1223     m   = arm_el_is_aa64(env, 1) &&
1224               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1225 
1226     if (el == 0) {
1227         filtered = secure ? u : u != nsu;
1228     } else if (el == 1) {
1229         filtered = secure ? p : p != nsk;
1230     } else if (el == 2) {
1231         filtered = !nsh;
1232     } else { /* EL3 */
1233         filtered = m != p;
1234     }
1235 
1236     if (counter != 31) {
1237         /*
1238          * If not checking PMCCNTR, ensure the counter is setup to an event we
1239          * support
1240          */
1241         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1242         if (!event_supported(event)) {
1243             return false;
1244         }
1245     }
1246 
1247     return enabled && !prohibited && !filtered;
1248 }
1249 
1250 static void pmu_update_irq(CPUARMState *env)
1251 {
1252     ARMCPU *cpu = env_archcpu(env);
1253     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1254             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1255 }
1256 
1257 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1258 {
1259     /*
1260      * Return true if the clock divider is enabled and the cycle counter
1261      * is supposed to tick only once every 64 clock cycles. This is
1262      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1263      * (64-bit) cycle counter PMCR.D has no effect.
1264      */
1265     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1266 }
1267 
1268 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1269 {
1270     /* Return true if the specified event counter is configured to be 64 bit */
1271 
1272     /* This isn't intended to be used with the cycle counter */
1273     assert(counter < 31);
1274 
1275     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1276         return false;
1277     }
1278 
1279     if (arm_feature(env, ARM_FEATURE_EL2)) {
1280         /*
1281          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1282          * current security state, so we don't use arm_mdcr_el2_eff() here.
1283          */
1284         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1285         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1286 
1287         if (counter >= hpmn) {
1288             return hlp;
1289         }
1290     }
1291     return env->cp15.c9_pmcr & PMCRLP;
1292 }
1293 
1294 /*
1295  * Ensure c15_ccnt is the guest-visible count so that operations such as
1296  * enabling/disabling the counter or filtering, modifying the count itself,
1297  * etc. can be done logically. This is essentially a no-op if the counter is
1298  * not enabled at the time of the call.
1299  */
1300 static void pmccntr_op_start(CPUARMState *env)
1301 {
1302     uint64_t cycles = cycles_get_count(env);
1303 
1304     if (pmu_counter_enabled(env, 31)) {
1305         uint64_t eff_cycles = cycles;
1306         if (pmccntr_clockdiv_enabled(env)) {
1307             eff_cycles /= 64;
1308         }
1309 
1310         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1311 
1312         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1313                                  1ull << 63 : 1ull << 31;
1314         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1315             env->cp15.c9_pmovsr |= (1ULL << 31);
1316             pmu_update_irq(env);
1317         }
1318 
1319         env->cp15.c15_ccnt = new_pmccntr;
1320     }
1321     env->cp15.c15_ccnt_delta = cycles;
1322 }
1323 
1324 /*
1325  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1326  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1327  * pmccntr_op_start.
1328  */
1329 static void pmccntr_op_finish(CPUARMState *env)
1330 {
1331     if (pmu_counter_enabled(env, 31)) {
1332 #ifndef CONFIG_USER_ONLY
1333         /* Calculate when the counter will next overflow */
1334         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1335         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1336             remaining_cycles = (uint32_t)remaining_cycles;
1337         }
1338         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1339 
1340         if (overflow_in > 0) {
1341             int64_t overflow_at;
1342 
1343             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1344                                  overflow_in, &overflow_at)) {
1345                 ARMCPU *cpu = env_archcpu(env);
1346                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1347             }
1348         }
1349 #endif
1350 
1351         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1352         if (pmccntr_clockdiv_enabled(env)) {
1353             prev_cycles /= 64;
1354         }
1355         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1356     }
1357 }
1358 
1359 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1360 {
1361 
1362     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1363     uint64_t count = 0;
1364     if (event_supported(event)) {
1365         uint16_t event_idx = supported_event_map[event];
1366         count = pm_events[event_idx].get_count(env);
1367     }
1368 
1369     if (pmu_counter_enabled(env, counter)) {
1370         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1371         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1372             1ULL << 63 : 1ULL << 31;
1373 
1374         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1375             env->cp15.c9_pmovsr |= (1 << counter);
1376             pmu_update_irq(env);
1377         }
1378         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1379     }
1380     env->cp15.c14_pmevcntr_delta[counter] = count;
1381 }
1382 
1383 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1384 {
1385     if (pmu_counter_enabled(env, counter)) {
1386 #ifndef CONFIG_USER_ONLY
1387         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1388         uint16_t event_idx = supported_event_map[event];
1389         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1390         int64_t overflow_in;
1391 
1392         if (!pmevcntr_is_64_bit(env, counter)) {
1393             delta = (uint32_t)delta;
1394         }
1395         overflow_in = pm_events[event_idx].ns_per_count(delta);
1396 
1397         if (overflow_in > 0) {
1398             int64_t overflow_at;
1399 
1400             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1401                                  overflow_in, &overflow_at)) {
1402                 ARMCPU *cpu = env_archcpu(env);
1403                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1404             }
1405         }
1406 #endif
1407 
1408         env->cp15.c14_pmevcntr_delta[counter] -=
1409             env->cp15.c14_pmevcntr[counter];
1410     }
1411 }
1412 
1413 void pmu_op_start(CPUARMState *env)
1414 {
1415     unsigned int i;
1416     pmccntr_op_start(env);
1417     for (i = 0; i < pmu_num_counters(env); i++) {
1418         pmevcntr_op_start(env, i);
1419     }
1420 }
1421 
1422 void pmu_op_finish(CPUARMState *env)
1423 {
1424     unsigned int i;
1425     pmccntr_op_finish(env);
1426     for (i = 0; i < pmu_num_counters(env); i++) {
1427         pmevcntr_op_finish(env, i);
1428     }
1429 }
1430 
1431 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1432 {
1433     pmu_op_start(&cpu->env);
1434 }
1435 
1436 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1437 {
1438     pmu_op_finish(&cpu->env);
1439 }
1440 
1441 void arm_pmu_timer_cb(void *opaque)
1442 {
1443     ARMCPU *cpu = opaque;
1444 
1445     /*
1446      * Update all the counter values based on the current underlying counts,
1447      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1448      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1449      * counter may expire.
1450      */
1451     pmu_op_start(&cpu->env);
1452     pmu_op_finish(&cpu->env);
1453 }
1454 
1455 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1456                        uint64_t value)
1457 {
1458     pmu_op_start(env);
1459 
1460     if (value & PMCRC) {
1461         /* The counter has been reset */
1462         env->cp15.c15_ccnt = 0;
1463     }
1464 
1465     if (value & PMCRP) {
1466         unsigned int i;
1467         for (i = 0; i < pmu_num_counters(env); i++) {
1468             env->cp15.c14_pmevcntr[i] = 0;
1469         }
1470     }
1471 
1472     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1473     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1474 
1475     pmu_op_finish(env);
1476 }
1477 
1478 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1479                           uint64_t value)
1480 {
1481     unsigned int i;
1482     uint64_t overflow_mask, new_pmswinc;
1483 
1484     for (i = 0; i < pmu_num_counters(env); i++) {
1485         /* Increment a counter's count iff: */
1486         if ((value & (1 << i)) && /* counter's bit is set */
1487                 /* counter is enabled and not filtered */
1488                 pmu_counter_enabled(env, i) &&
1489                 /* counter is SW_INCR */
1490                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1491             pmevcntr_op_start(env, i);
1492 
1493             /*
1494              * Detect if this write causes an overflow since we can't predict
1495              * PMSWINC overflows like we can for other events
1496              */
1497             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1498 
1499             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1500                 1ULL << 63 : 1ULL << 31;
1501 
1502             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1503                 env->cp15.c9_pmovsr |= (1 << i);
1504                 pmu_update_irq(env);
1505             }
1506 
1507             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1508 
1509             pmevcntr_op_finish(env, i);
1510         }
1511     }
1512 }
1513 
1514 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1515 {
1516     uint64_t ret;
1517     pmccntr_op_start(env);
1518     ret = env->cp15.c15_ccnt;
1519     pmccntr_op_finish(env);
1520     return ret;
1521 }
1522 
1523 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1524                          uint64_t value)
1525 {
1526     /*
1527      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1528      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1529      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1530      * accessed.
1531      */
1532     env->cp15.c9_pmselr = value & 0x1f;
1533 }
1534 
1535 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1536                         uint64_t value)
1537 {
1538     pmccntr_op_start(env);
1539     env->cp15.c15_ccnt = value;
1540     pmccntr_op_finish(env);
1541 }
1542 
1543 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1544                             uint64_t value)
1545 {
1546     uint64_t cur_val = pmccntr_read(env, NULL);
1547 
1548     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1549 }
1550 
1551 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1552                             uint64_t value)
1553 {
1554     pmccntr_op_start(env);
1555     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1556     pmccntr_op_finish(env);
1557 }
1558 
1559 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1560                             uint64_t value)
1561 {
1562     pmccntr_op_start(env);
1563     /* M is not accessible from AArch32 */
1564     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1565         (value & PMCCFILTR);
1566     pmccntr_op_finish(env);
1567 }
1568 
1569 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1570 {
1571     /* M is not visible in AArch32 */
1572     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1573 }
1574 
1575 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1576                             uint64_t value)
1577 {
1578     pmu_op_start(env);
1579     value &= pmu_counter_mask(env);
1580     env->cp15.c9_pmcnten |= value;
1581     pmu_op_finish(env);
1582 }
1583 
1584 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1585                              uint64_t value)
1586 {
1587     pmu_op_start(env);
1588     value &= pmu_counter_mask(env);
1589     env->cp15.c9_pmcnten &= ~value;
1590     pmu_op_finish(env);
1591 }
1592 
1593 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1594                          uint64_t value)
1595 {
1596     value &= pmu_counter_mask(env);
1597     env->cp15.c9_pmovsr &= ~value;
1598     pmu_update_irq(env);
1599 }
1600 
1601 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1602                          uint64_t value)
1603 {
1604     value &= pmu_counter_mask(env);
1605     env->cp15.c9_pmovsr |= value;
1606     pmu_update_irq(env);
1607 }
1608 
1609 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1610                              uint64_t value, const uint8_t counter)
1611 {
1612     if (counter == 31) {
1613         pmccfiltr_write(env, ri, value);
1614     } else if (counter < pmu_num_counters(env)) {
1615         pmevcntr_op_start(env, counter);
1616 
1617         /*
1618          * If this counter's event type is changing, store the current
1619          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1620          * pmevcntr_op_finish has the correct baseline when it converts back to
1621          * a delta.
1622          */
1623         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1624             PMXEVTYPER_EVTCOUNT;
1625         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1626         if (old_event != new_event) {
1627             uint64_t count = 0;
1628             if (event_supported(new_event)) {
1629                 uint16_t event_idx = supported_event_map[new_event];
1630                 count = pm_events[event_idx].get_count(env);
1631             }
1632             env->cp15.c14_pmevcntr_delta[counter] = count;
1633         }
1634 
1635         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1636         pmevcntr_op_finish(env, counter);
1637     }
1638     /*
1639      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1640      * PMSELR value is equal to or greater than the number of implemented
1641      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1642      */
1643 }
1644 
1645 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1646                                const uint8_t counter)
1647 {
1648     if (counter == 31) {
1649         return env->cp15.pmccfiltr_el0;
1650     } else if (counter < pmu_num_counters(env)) {
1651         return env->cp15.c14_pmevtyper[counter];
1652     } else {
1653       /*
1654        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1655        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1656        */
1657         return 0;
1658     }
1659 }
1660 
1661 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1662                               uint64_t value)
1663 {
1664     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1665     pmevtyper_write(env, ri, value, counter);
1666 }
1667 
1668 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1669                                uint64_t value)
1670 {
1671     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1672     env->cp15.c14_pmevtyper[counter] = value;
1673 
1674     /*
1675      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1676      * pmu_op_finish calls when loading saved state for a migration. Because
1677      * we're potentially updating the type of event here, the value written to
1678      * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1679      * different counter type. Therefore, we need to set this value to the
1680      * current count for the counter type we're writing so that pmu_op_finish
1681      * has the correct count for its calculation.
1682      */
1683     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1684     if (event_supported(event)) {
1685         uint16_t event_idx = supported_event_map[event];
1686         env->cp15.c14_pmevcntr_delta[counter] =
1687             pm_events[event_idx].get_count(env);
1688     }
1689 }
1690 
1691 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1692 {
1693     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1694     return pmevtyper_read(env, ri, counter);
1695 }
1696 
1697 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1698                              uint64_t value)
1699 {
1700     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1701 }
1702 
1703 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1704 {
1705     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1706 }
1707 
1708 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1709                              uint64_t value, uint8_t counter)
1710 {
1711     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1712         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1713         value &= MAKE_64BIT_MASK(0, 32);
1714     }
1715     if (counter < pmu_num_counters(env)) {
1716         pmevcntr_op_start(env, counter);
1717         env->cp15.c14_pmevcntr[counter] = value;
1718         pmevcntr_op_finish(env, counter);
1719     }
1720     /*
1721      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1722      * are CONSTRAINED UNPREDICTABLE.
1723      */
1724 }
1725 
1726 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1727                               uint8_t counter)
1728 {
1729     if (counter < pmu_num_counters(env)) {
1730         uint64_t ret;
1731         pmevcntr_op_start(env, counter);
1732         ret = env->cp15.c14_pmevcntr[counter];
1733         pmevcntr_op_finish(env, counter);
1734         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1735             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1736             ret &= MAKE_64BIT_MASK(0, 32);
1737         }
1738         return ret;
1739     } else {
1740       /*
1741        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1742        * are CONSTRAINED UNPREDICTABLE.
1743        */
1744         return 0;
1745     }
1746 }
1747 
1748 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1749                              uint64_t value)
1750 {
1751     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1752     pmevcntr_write(env, ri, value, counter);
1753 }
1754 
1755 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1756 {
1757     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1758     return pmevcntr_read(env, ri, counter);
1759 }
1760 
1761 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1762                              uint64_t value)
1763 {
1764     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1765     assert(counter < pmu_num_counters(env));
1766     env->cp15.c14_pmevcntr[counter] = value;
1767     pmevcntr_write(env, ri, value, counter);
1768 }
1769 
1770 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1771 {
1772     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1773     assert(counter < pmu_num_counters(env));
1774     return env->cp15.c14_pmevcntr[counter];
1775 }
1776 
1777 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1778                              uint64_t value)
1779 {
1780     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1781 }
1782 
1783 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1784 {
1785     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1786 }
1787 
1788 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1789                             uint64_t value)
1790 {
1791     if (arm_feature(env, ARM_FEATURE_V8)) {
1792         env->cp15.c9_pmuserenr = value & 0xf;
1793     } else {
1794         env->cp15.c9_pmuserenr = value & 1;
1795     }
1796 }
1797 
1798 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1799                              uint64_t value)
1800 {
1801     /* We have no event counters so only the C bit can be changed */
1802     value &= pmu_counter_mask(env);
1803     env->cp15.c9_pminten |= value;
1804     pmu_update_irq(env);
1805 }
1806 
1807 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1808                              uint64_t value)
1809 {
1810     value &= pmu_counter_mask(env);
1811     env->cp15.c9_pminten &= ~value;
1812     pmu_update_irq(env);
1813 }
1814 
1815 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1816                        uint64_t value)
1817 {
1818     /*
1819      * Note that even though the AArch64 view of this register has bits
1820      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1821      * architectural requirements for bits which are RES0 only in some
1822      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1823      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1824      */
1825     raw_write(env, ri, value & ~0x1FULL);
1826 }
1827 
1828 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1829 {
1830     /* Begin with base v8.0 state.  */
1831     uint64_t valid_mask = 0x3fff;
1832     ARMCPU *cpu = env_archcpu(env);
1833     uint64_t changed;
1834 
1835     /*
1836      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1837      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1838      * Instead, choose the format based on the mode of EL3.
1839      */
1840     if (arm_el_is_aa64(env, 3)) {
1841         value |= SCR_FW | SCR_AW;      /* RES1 */
1842         valid_mask &= ~SCR_NET;        /* RES0 */
1843 
1844         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1845             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1846             value |= SCR_RW;           /* RAO/WI */
1847         }
1848         if (cpu_isar_feature(aa64_ras, cpu)) {
1849             valid_mask |= SCR_TERR;
1850         }
1851         if (cpu_isar_feature(aa64_lor, cpu)) {
1852             valid_mask |= SCR_TLOR;
1853         }
1854         if (cpu_isar_feature(aa64_pauth, cpu)) {
1855             valid_mask |= SCR_API | SCR_APK;
1856         }
1857         if (cpu_isar_feature(aa64_sel2, cpu)) {
1858             valid_mask |= SCR_EEL2;
1859         } else if (cpu_isar_feature(aa64_rme, cpu)) {
1860             /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1861             value |= SCR_NS;
1862         }
1863         if (cpu_isar_feature(aa64_mte, cpu)) {
1864             valid_mask |= SCR_ATA;
1865         }
1866         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1867             valid_mask |= SCR_ENSCXT;
1868         }
1869         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1870             valid_mask |= SCR_EASE | SCR_NMEA;
1871         }
1872         if (cpu_isar_feature(aa64_sme, cpu)) {
1873             valid_mask |= SCR_ENTP2;
1874         }
1875         if (cpu_isar_feature(aa64_hcx, cpu)) {
1876             valid_mask |= SCR_HXEN;
1877         }
1878         if (cpu_isar_feature(aa64_fgt, cpu)) {
1879             valid_mask |= SCR_FGTEN;
1880         }
1881         if (cpu_isar_feature(aa64_rme, cpu)) {
1882             valid_mask |= SCR_NSE | SCR_GPF;
1883         }
1884     } else {
1885         valid_mask &= ~(SCR_RW | SCR_ST);
1886         if (cpu_isar_feature(aa32_ras, cpu)) {
1887             valid_mask |= SCR_TERR;
1888         }
1889     }
1890 
1891     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1892         valid_mask &= ~SCR_HCE;
1893 
1894         /*
1895          * On ARMv7, SMD (or SCD as it is called in v7) is only
1896          * supported if EL2 exists. The bit is UNK/SBZP when
1897          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1898          * when EL2 is unavailable.
1899          * On ARMv8, this bit is always available.
1900          */
1901         if (arm_feature(env, ARM_FEATURE_V7) &&
1902             !arm_feature(env, ARM_FEATURE_V8)) {
1903             valid_mask &= ~SCR_SMD;
1904         }
1905     }
1906 
1907     /* Clear all-context RES0 bits.  */
1908     value &= valid_mask;
1909     changed = env->cp15.scr_el3 ^ value;
1910     env->cp15.scr_el3 = value;
1911 
1912     /*
1913      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1914      * we must invalidate all TLBs below EL3.
1915      */
1916     if (changed & (SCR_NS | SCR_NSE)) {
1917         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1918                                            ARMMMUIdxBit_E20_0 |
1919                                            ARMMMUIdxBit_E10_1 |
1920                                            ARMMMUIdxBit_E20_2 |
1921                                            ARMMMUIdxBit_E10_1_PAN |
1922                                            ARMMMUIdxBit_E20_2_PAN |
1923                                            ARMMMUIdxBit_E2));
1924     }
1925 }
1926 
1927 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1928 {
1929     /*
1930      * scr_write will set the RES1 bits on an AArch64-only CPU.
1931      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1932      */
1933     scr_write(env, ri, 0);
1934 }
1935 
1936 static CPAccessResult access_tid4(CPUARMState *env,
1937                                   const ARMCPRegInfo *ri,
1938                                   bool isread)
1939 {
1940     if (arm_current_el(env) == 1 &&
1941         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1942         return CP_ACCESS_TRAP_EL2;
1943     }
1944 
1945     return CP_ACCESS_OK;
1946 }
1947 
1948 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1949 {
1950     ARMCPU *cpu = env_archcpu(env);
1951 
1952     /*
1953      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1954      * bank
1955      */
1956     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1957                                         ri->secure & ARM_CP_SECSTATE_S);
1958 
1959     return cpu->ccsidr[index];
1960 }
1961 
1962 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1963                          uint64_t value)
1964 {
1965     raw_write(env, ri, value & 0xf);
1966 }
1967 
1968 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1969 {
1970     CPUState *cs = env_cpu(env);
1971     bool el1 = arm_current_el(env) == 1;
1972     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1973     uint64_t ret = 0;
1974 
1975     if (hcr_el2 & HCR_IMO) {
1976         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1977             ret |= CPSR_I;
1978         }
1979     } else {
1980         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1981             ret |= CPSR_I;
1982         }
1983     }
1984 
1985     if (hcr_el2 & HCR_FMO) {
1986         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1987             ret |= CPSR_F;
1988         }
1989     } else {
1990         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1991             ret |= CPSR_F;
1992         }
1993     }
1994 
1995     if (hcr_el2 & HCR_AMO) {
1996         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1997             ret |= CPSR_A;
1998         }
1999     }
2000 
2001     return ret;
2002 }
2003 
2004 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2005                                        bool isread)
2006 {
2007     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2008         return CP_ACCESS_TRAP_EL2;
2009     }
2010 
2011     return CP_ACCESS_OK;
2012 }
2013 
2014 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2015                                        bool isread)
2016 {
2017     if (arm_feature(env, ARM_FEATURE_V8)) {
2018         return access_aa64_tid1(env, ri, isread);
2019     }
2020 
2021     return CP_ACCESS_OK;
2022 }
2023 
2024 static const ARMCPRegInfo v7_cp_reginfo[] = {
2025     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2026     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2027       .access = PL1_W, .type = ARM_CP_NOP },
2028     /*
2029      * Performance monitors are implementation defined in v7,
2030      * but with an ARM recommended set of registers, which we
2031      * follow.
2032      *
2033      * Performance registers fall into three categories:
2034      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2035      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2036      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2037      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2038      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2039      */
2040     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2041       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2042       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2043       .writefn = pmcntenset_write,
2044       .accessfn = pmreg_access,
2045       .fgt = FGT_PMCNTEN,
2046       .raw_writefn = raw_write },
2047     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2048       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2049       .access = PL0_RW, .accessfn = pmreg_access,
2050       .fgt = FGT_PMCNTEN,
2051       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2052       .writefn = pmcntenset_write, .raw_writefn = raw_write },
2053     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2054       .access = PL0_RW,
2055       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2056       .accessfn = pmreg_access,
2057       .fgt = FGT_PMCNTEN,
2058       .writefn = pmcntenclr_write,
2059       .type = ARM_CP_ALIAS | ARM_CP_IO },
2060     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2061       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2062       .access = PL0_RW, .accessfn = pmreg_access,
2063       .fgt = FGT_PMCNTEN,
2064       .type = ARM_CP_ALIAS | ARM_CP_IO,
2065       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2066       .writefn = pmcntenclr_write },
2067     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2068       .access = PL0_RW, .type = ARM_CP_IO,
2069       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2070       .accessfn = pmreg_access,
2071       .fgt = FGT_PMOVS,
2072       .writefn = pmovsr_write,
2073       .raw_writefn = raw_write },
2074     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2075       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2076       .access = PL0_RW, .accessfn = pmreg_access,
2077       .fgt = FGT_PMOVS,
2078       .type = ARM_CP_ALIAS | ARM_CP_IO,
2079       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2080       .writefn = pmovsr_write,
2081       .raw_writefn = raw_write },
2082     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2083       .access = PL0_W, .accessfn = pmreg_access_swinc,
2084       .fgt = FGT_PMSWINC_EL0,
2085       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2086       .writefn = pmswinc_write },
2087     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2088       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2089       .access = PL0_W, .accessfn = pmreg_access_swinc,
2090       .fgt = FGT_PMSWINC_EL0,
2091       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2092       .writefn = pmswinc_write },
2093     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2094       .access = PL0_RW, .type = ARM_CP_ALIAS,
2095       .fgt = FGT_PMSELR_EL0,
2096       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2097       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2098       .raw_writefn = raw_write},
2099     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2100       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2101       .access = PL0_RW, .accessfn = pmreg_access_selr,
2102       .fgt = FGT_PMSELR_EL0,
2103       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2104       .writefn = pmselr_write, .raw_writefn = raw_write, },
2105     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2106       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2107       .fgt = FGT_PMCCNTR_EL0,
2108       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2109       .accessfn = pmreg_access_ccntr },
2110     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2111       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2112       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2113       .fgt = FGT_PMCCNTR_EL0,
2114       .type = ARM_CP_IO,
2115       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2116       .readfn = pmccntr_read, .writefn = pmccntr_write,
2117       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2118     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2119       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2120       .access = PL0_RW, .accessfn = pmreg_access,
2121       .fgt = FGT_PMCCFILTR_EL0,
2122       .type = ARM_CP_ALIAS | ARM_CP_IO,
2123       .resetvalue = 0, },
2124     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2125       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2126       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2127       .access = PL0_RW, .accessfn = pmreg_access,
2128       .fgt = FGT_PMCCFILTR_EL0,
2129       .type = ARM_CP_IO,
2130       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2131       .resetvalue = 0, },
2132     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2133       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2134       .accessfn = pmreg_access,
2135       .fgt = FGT_PMEVTYPERN_EL0,
2136       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2137     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2138       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2139       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2140       .accessfn = pmreg_access,
2141       .fgt = FGT_PMEVTYPERN_EL0,
2142       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2143     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2144       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2145       .accessfn = pmreg_access_xevcntr,
2146       .fgt = FGT_PMEVCNTRN_EL0,
2147       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2148     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2149       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2150       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2151       .accessfn = pmreg_access_xevcntr,
2152       .fgt = FGT_PMEVCNTRN_EL0,
2153       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2154     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2155       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2156       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2157       .resetvalue = 0,
2158       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2159     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2160       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2161       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2162       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2163       .resetvalue = 0,
2164       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2165     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2166       .access = PL1_RW, .accessfn = access_tpm,
2167       .fgt = FGT_PMINTEN,
2168       .type = ARM_CP_ALIAS | ARM_CP_IO,
2169       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2170       .resetvalue = 0,
2171       .writefn = pmintenset_write, .raw_writefn = raw_write },
2172     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2173       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2174       .access = PL1_RW, .accessfn = access_tpm,
2175       .fgt = FGT_PMINTEN,
2176       .type = ARM_CP_IO,
2177       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2178       .writefn = pmintenset_write, .raw_writefn = raw_write,
2179       .resetvalue = 0x0 },
2180     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2181       .access = PL1_RW, .accessfn = access_tpm,
2182       .fgt = FGT_PMINTEN,
2183       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2184       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2185       .writefn = pmintenclr_write, },
2186     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2187       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2188       .access = PL1_RW, .accessfn = access_tpm,
2189       .fgt = FGT_PMINTEN,
2190       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2191       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2192       .writefn = pmintenclr_write },
2193     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2194       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2195       .access = PL1_R,
2196       .accessfn = access_tid4,
2197       .fgt = FGT_CCSIDR_EL1,
2198       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2199     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2200       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2201       .access = PL1_RW,
2202       .accessfn = access_tid4,
2203       .fgt = FGT_CSSELR_EL1,
2204       .writefn = csselr_write, .resetvalue = 0,
2205       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2206                              offsetof(CPUARMState, cp15.csselr_ns) } },
2207     /*
2208      * Auxiliary ID register: this actually has an IMPDEF value but for now
2209      * just RAZ for all cores:
2210      */
2211     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2212       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2213       .access = PL1_R, .type = ARM_CP_CONST,
2214       .accessfn = access_aa64_tid1,
2215       .fgt = FGT_AIDR_EL1,
2216       .resetvalue = 0 },
2217     /*
2218      * Auxiliary fault status registers: these also are IMPDEF, and we
2219      * choose to RAZ/WI for all cores.
2220      */
2221     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2222       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2223       .access = PL1_RW, .accessfn = access_tvm_trvm,
2224       .fgt = FGT_AFSR0_EL1,
2225       .type = ARM_CP_CONST, .resetvalue = 0 },
2226     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2227       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2228       .access = PL1_RW, .accessfn = access_tvm_trvm,
2229       .fgt = FGT_AFSR1_EL1,
2230       .type = ARM_CP_CONST, .resetvalue = 0 },
2231     /*
2232      * MAIR can just read-as-written because we don't implement caches
2233      * and so don't need to care about memory attributes.
2234      */
2235     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2236       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2237       .access = PL1_RW, .accessfn = access_tvm_trvm,
2238       .fgt = FGT_MAIR_EL1,
2239       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2240       .resetvalue = 0 },
2241     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2242       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2243       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2244       .resetvalue = 0 },
2245     /*
2246      * For non-long-descriptor page tables these are PRRR and NMRR;
2247      * regardless they still act as reads-as-written for QEMU.
2248      */
2249      /*
2250       * MAIR0/1 are defined separately from their 64-bit counterpart which
2251       * allows them to assign the correct fieldoffset based on the endianness
2252       * handled in the field definitions.
2253       */
2254     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2255       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2256       .access = PL1_RW, .accessfn = access_tvm_trvm,
2257       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2258                              offsetof(CPUARMState, cp15.mair0_ns) },
2259       .resetfn = arm_cp_reset_ignore },
2260     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2261       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2262       .access = PL1_RW, .accessfn = access_tvm_trvm,
2263       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2264                              offsetof(CPUARMState, cp15.mair1_ns) },
2265       .resetfn = arm_cp_reset_ignore },
2266     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2267       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2268       .fgt = FGT_ISR_EL1,
2269       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2270     /* 32 bit ITLB invalidates */
2271     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2272       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2273       .writefn = tlbiall_write },
2274     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2275       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2276       .writefn = tlbimva_write },
2277     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2278       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2279       .writefn = tlbiasid_write },
2280     /* 32 bit DTLB invalidates */
2281     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2282       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2283       .writefn = tlbiall_write },
2284     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2285       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2286       .writefn = tlbimva_write },
2287     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2288       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2289       .writefn = tlbiasid_write },
2290     /* 32 bit TLB invalidates */
2291     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2292       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2293       .writefn = tlbiall_write },
2294     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2295       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2296       .writefn = tlbimva_write },
2297     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2298       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2299       .writefn = tlbiasid_write },
2300     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2301       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2302       .writefn = tlbimvaa_write },
2303 };
2304 
2305 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2306     /* 32 bit TLB invalidates, Inner Shareable */
2307     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2308       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2309       .writefn = tlbiall_is_write },
2310     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2311       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2312       .writefn = tlbimva_is_write },
2313     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2314       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2315       .writefn = tlbiasid_is_write },
2316     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2317       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2318       .writefn = tlbimvaa_is_write },
2319 };
2320 
2321 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2322     /* PMOVSSET is not implemented in v7 before v7ve */
2323     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2324       .access = PL0_RW, .accessfn = pmreg_access,
2325       .fgt = FGT_PMOVS,
2326       .type = ARM_CP_ALIAS | ARM_CP_IO,
2327       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2328       .writefn = pmovsset_write,
2329       .raw_writefn = raw_write },
2330     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2331       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2332       .access = PL0_RW, .accessfn = pmreg_access,
2333       .fgt = FGT_PMOVS,
2334       .type = ARM_CP_ALIAS | ARM_CP_IO,
2335       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2336       .writefn = pmovsset_write,
2337       .raw_writefn = raw_write },
2338 };
2339 
2340 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2341                         uint64_t value)
2342 {
2343     value &= 1;
2344     env->teecr = value;
2345 }
2346 
2347 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2348                                    bool isread)
2349 {
2350     /*
2351      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2352      * at all, so we don't need to check whether we're v8A.
2353      */
2354     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2355         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2356         return CP_ACCESS_TRAP_EL2;
2357     }
2358     return CP_ACCESS_OK;
2359 }
2360 
2361 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2362                                     bool isread)
2363 {
2364     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2365         return CP_ACCESS_TRAP;
2366     }
2367     return teecr_access(env, ri, isread);
2368 }
2369 
2370 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2371     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2372       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2373       .resetvalue = 0,
2374       .writefn = teecr_write, .accessfn = teecr_access },
2375     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2376       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2377       .accessfn = teehbr_access, .resetvalue = 0 },
2378 };
2379 
2380 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2381     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2382       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2383       .access = PL0_RW,
2384       .fgt = FGT_TPIDR_EL0,
2385       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2386     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2387       .access = PL0_RW,
2388       .fgt = FGT_TPIDR_EL0,
2389       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2390                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2391       .resetfn = arm_cp_reset_ignore },
2392     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2393       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2394       .access = PL0_R | PL1_W,
2395       .fgt = FGT_TPIDRRO_EL0,
2396       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2397       .resetvalue = 0},
2398     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2399       .access = PL0_R | PL1_W,
2400       .fgt = FGT_TPIDRRO_EL0,
2401       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2402                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2403       .resetfn = arm_cp_reset_ignore },
2404     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2405       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2406       .access = PL1_RW,
2407       .fgt = FGT_TPIDR_EL1,
2408       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2409     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2410       .access = PL1_RW,
2411       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2412                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2413       .resetvalue = 0 },
2414 };
2415 
2416 #ifndef CONFIG_USER_ONLY
2417 
2418 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2419                                        bool isread)
2420 {
2421     /*
2422      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2423      * Writable only at the highest implemented exception level.
2424      */
2425     int el = arm_current_el(env);
2426     uint64_t hcr;
2427     uint32_t cntkctl;
2428 
2429     switch (el) {
2430     case 0:
2431         hcr = arm_hcr_el2_eff(env);
2432         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2433             cntkctl = env->cp15.cnthctl_el2;
2434         } else {
2435             cntkctl = env->cp15.c14_cntkctl;
2436         }
2437         if (!extract32(cntkctl, 0, 2)) {
2438             return CP_ACCESS_TRAP;
2439         }
2440         break;
2441     case 1:
2442         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2443             arm_is_secure_below_el3(env)) {
2444             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2445             return CP_ACCESS_TRAP_UNCATEGORIZED;
2446         }
2447         break;
2448     case 2:
2449     case 3:
2450         break;
2451     }
2452 
2453     if (!isread && el < arm_highest_el(env)) {
2454         return CP_ACCESS_TRAP_UNCATEGORIZED;
2455     }
2456 
2457     return CP_ACCESS_OK;
2458 }
2459 
2460 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2461                                         bool isread)
2462 {
2463     unsigned int cur_el = arm_current_el(env);
2464     bool has_el2 = arm_is_el2_enabled(env);
2465     uint64_t hcr = arm_hcr_el2_eff(env);
2466 
2467     switch (cur_el) {
2468     case 0:
2469         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2470         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2471             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2472                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2473         }
2474 
2475         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2476         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2477             return CP_ACCESS_TRAP;
2478         }
2479         /* fall through */
2480     case 1:
2481         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2482         if (has_el2 && timeridx == GTIMER_PHYS &&
2483             (hcr & HCR_E2H
2484              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2485              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2486             return CP_ACCESS_TRAP_EL2;
2487         }
2488         break;
2489     }
2490     return CP_ACCESS_OK;
2491 }
2492 
2493 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2494                                       bool isread)
2495 {
2496     unsigned int cur_el = arm_current_el(env);
2497     bool has_el2 = arm_is_el2_enabled(env);
2498     uint64_t hcr = arm_hcr_el2_eff(env);
2499 
2500     switch (cur_el) {
2501     case 0:
2502         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2503             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2504             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2505                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2506         }
2507 
2508         /*
2509          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2510          * EL0 if EL0[PV]TEN is zero.
2511          */
2512         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2513             return CP_ACCESS_TRAP;
2514         }
2515         /* fall through */
2516 
2517     case 1:
2518         if (has_el2 && timeridx == GTIMER_PHYS) {
2519             if (hcr & HCR_E2H) {
2520                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2521                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2522                     return CP_ACCESS_TRAP_EL2;
2523                 }
2524             } else {
2525                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2526                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2527                     return CP_ACCESS_TRAP_EL2;
2528                 }
2529             }
2530         }
2531         break;
2532     }
2533     return CP_ACCESS_OK;
2534 }
2535 
2536 static CPAccessResult gt_pct_access(CPUARMState *env,
2537                                     const ARMCPRegInfo *ri,
2538                                     bool isread)
2539 {
2540     return gt_counter_access(env, GTIMER_PHYS, isread);
2541 }
2542 
2543 static CPAccessResult gt_vct_access(CPUARMState *env,
2544                                     const ARMCPRegInfo *ri,
2545                                     bool isread)
2546 {
2547     return gt_counter_access(env, GTIMER_VIRT, isread);
2548 }
2549 
2550 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2551                                        bool isread)
2552 {
2553     return gt_timer_access(env, GTIMER_PHYS, isread);
2554 }
2555 
2556 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2557                                        bool isread)
2558 {
2559     return gt_timer_access(env, GTIMER_VIRT, isread);
2560 }
2561 
2562 static CPAccessResult gt_stimer_access(CPUARMState *env,
2563                                        const ARMCPRegInfo *ri,
2564                                        bool isread)
2565 {
2566     /*
2567      * The AArch64 register view of the secure physical timer is
2568      * always accessible from EL3, and configurably accessible from
2569      * Secure EL1.
2570      */
2571     switch (arm_current_el(env)) {
2572     case 1:
2573         if (!arm_is_secure(env)) {
2574             return CP_ACCESS_TRAP;
2575         }
2576         if (!(env->cp15.scr_el3 & SCR_ST)) {
2577             return CP_ACCESS_TRAP_EL3;
2578         }
2579         return CP_ACCESS_OK;
2580     case 0:
2581     case 2:
2582         return CP_ACCESS_TRAP;
2583     case 3:
2584         return CP_ACCESS_OK;
2585     default:
2586         g_assert_not_reached();
2587     }
2588 }
2589 
2590 static uint64_t gt_get_countervalue(CPUARMState *env)
2591 {
2592     ARMCPU *cpu = env_archcpu(env);
2593 
2594     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2595 }
2596 
2597 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2598 {
2599     CPUARMState *env = &cpu->env;
2600     uint64_t cnthctl = env->cp15.cnthctl_el2;
2601     ARMSecuritySpace ss = arm_security_space(env);
2602     /* ISTATUS && !IMASK */
2603     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2604 
2605     /*
2606      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2607      * It is RES0 in Secure and NonSecure state.
2608      */
2609     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2610         ((timeridx == GTIMER_VIRT && (cnthctl & CNTHCTL_CNTVMASK)) ||
2611          (timeridx == GTIMER_PHYS && (cnthctl & CNTHCTL_CNTPMASK)))) {
2612         irqstate = 0;
2613     }
2614 
2615     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2616     trace_arm_gt_update_irq(timeridx, irqstate);
2617 }
2618 
2619 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2620 {
2621     /*
2622      * Changing security state between Root and Secure/NonSecure, which may
2623      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2624      * mask bits. Update the IRQ state accordingly.
2625      */
2626     gt_update_irq(cpu, GTIMER_VIRT);
2627     gt_update_irq(cpu, GTIMER_PHYS);
2628 }
2629 
2630 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2631 {
2632     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2633 
2634     if (gt->ctl & 1) {
2635         /*
2636          * Timer enabled: calculate and set current ISTATUS, irq, and
2637          * reset timer to when ISTATUS next has to change
2638          */
2639         uint64_t offset = timeridx == GTIMER_VIRT ?
2640                                       cpu->env.cp15.cntvoff_el2 : 0;
2641         uint64_t count = gt_get_countervalue(&cpu->env);
2642         /* Note that this must be unsigned 64 bit arithmetic: */
2643         int istatus = count - offset >= gt->cval;
2644         uint64_t nexttick;
2645 
2646         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2647 
2648         if (istatus) {
2649             /* Next transition is when count rolls back over to zero */
2650             nexttick = UINT64_MAX;
2651         } else {
2652             /* Next transition is when we hit cval */
2653             nexttick = gt->cval + offset;
2654         }
2655         /*
2656          * Note that the desired next expiry time might be beyond the
2657          * signed-64-bit range of a QEMUTimer -- in this case we just
2658          * set the timer for as far in the future as possible. When the
2659          * timer expires we will reset the timer for any remaining period.
2660          */
2661         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2662             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2663         } else {
2664             timer_mod(cpu->gt_timer[timeridx], nexttick);
2665         }
2666         trace_arm_gt_recalc(timeridx, nexttick);
2667     } else {
2668         /* Timer disabled: ISTATUS and timer output always clear */
2669         gt->ctl &= ~4;
2670         timer_del(cpu->gt_timer[timeridx]);
2671         trace_arm_gt_recalc_disabled(timeridx);
2672     }
2673     gt_update_irq(cpu, timeridx);
2674 }
2675 
2676 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2677                            int timeridx)
2678 {
2679     ARMCPU *cpu = env_archcpu(env);
2680 
2681     timer_del(cpu->gt_timer[timeridx]);
2682 }
2683 
2684 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2685 {
2686     return gt_get_countervalue(env);
2687 }
2688 
2689 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2690 {
2691     uint64_t hcr;
2692 
2693     switch (arm_current_el(env)) {
2694     case 2:
2695         hcr = arm_hcr_el2_eff(env);
2696         if (hcr & HCR_E2H) {
2697             return 0;
2698         }
2699         break;
2700     case 0:
2701         hcr = arm_hcr_el2_eff(env);
2702         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2703             return 0;
2704         }
2705         break;
2706     }
2707 
2708     return env->cp15.cntvoff_el2;
2709 }
2710 
2711 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2712 {
2713     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2714 }
2715 
2716 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2717                           int timeridx,
2718                           uint64_t value)
2719 {
2720     trace_arm_gt_cval_write(timeridx, value);
2721     env->cp15.c14_timer[timeridx].cval = value;
2722     gt_recalc_timer(env_archcpu(env), timeridx);
2723 }
2724 
2725 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2726                              int timeridx)
2727 {
2728     uint64_t offset = 0;
2729 
2730     switch (timeridx) {
2731     case GTIMER_VIRT:
2732     case GTIMER_HYPVIRT:
2733         offset = gt_virt_cnt_offset(env);
2734         break;
2735     }
2736 
2737     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2738                       (gt_get_countervalue(env) - offset));
2739 }
2740 
2741 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2742                           int timeridx,
2743                           uint64_t value)
2744 {
2745     uint64_t offset = 0;
2746 
2747     switch (timeridx) {
2748     case GTIMER_VIRT:
2749     case GTIMER_HYPVIRT:
2750         offset = gt_virt_cnt_offset(env);
2751         break;
2752     }
2753 
2754     trace_arm_gt_tval_write(timeridx, value);
2755     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2756                                          sextract64(value, 0, 32);
2757     gt_recalc_timer(env_archcpu(env), timeridx);
2758 }
2759 
2760 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2761                          int timeridx,
2762                          uint64_t value)
2763 {
2764     ARMCPU *cpu = env_archcpu(env);
2765     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2766 
2767     trace_arm_gt_ctl_write(timeridx, value);
2768     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2769     if ((oldval ^ value) & 1) {
2770         /* Enable toggled */
2771         gt_recalc_timer(cpu, timeridx);
2772     } else if ((oldval ^ value) & 2) {
2773         /*
2774          * IMASK toggled: don't need to recalculate,
2775          * just set the interrupt line based on ISTATUS
2776          */
2777         trace_arm_gt_imask_toggle(timeridx);
2778         gt_update_irq(cpu, timeridx);
2779     }
2780 }
2781 
2782 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2783 {
2784     gt_timer_reset(env, ri, GTIMER_PHYS);
2785 }
2786 
2787 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2788                                uint64_t value)
2789 {
2790     gt_cval_write(env, ri, GTIMER_PHYS, value);
2791 }
2792 
2793 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2794 {
2795     return gt_tval_read(env, ri, GTIMER_PHYS);
2796 }
2797 
2798 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2799                                uint64_t value)
2800 {
2801     gt_tval_write(env, ri, GTIMER_PHYS, value);
2802 }
2803 
2804 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2805                               uint64_t value)
2806 {
2807     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2808 }
2809 
2810 static int gt_phys_redir_timeridx(CPUARMState *env)
2811 {
2812     switch (arm_mmu_idx(env)) {
2813     case ARMMMUIdx_E20_0:
2814     case ARMMMUIdx_E20_2:
2815     case ARMMMUIdx_E20_2_PAN:
2816         return GTIMER_HYP;
2817     default:
2818         return GTIMER_PHYS;
2819     }
2820 }
2821 
2822 static int gt_virt_redir_timeridx(CPUARMState *env)
2823 {
2824     switch (arm_mmu_idx(env)) {
2825     case ARMMMUIdx_E20_0:
2826     case ARMMMUIdx_E20_2:
2827     case ARMMMUIdx_E20_2_PAN:
2828         return GTIMER_HYPVIRT;
2829     default:
2830         return GTIMER_VIRT;
2831     }
2832 }
2833 
2834 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2835                                         const ARMCPRegInfo *ri)
2836 {
2837     int timeridx = gt_phys_redir_timeridx(env);
2838     return env->cp15.c14_timer[timeridx].cval;
2839 }
2840 
2841 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2842                                      uint64_t value)
2843 {
2844     int timeridx = gt_phys_redir_timeridx(env);
2845     gt_cval_write(env, ri, timeridx, value);
2846 }
2847 
2848 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2849                                         const ARMCPRegInfo *ri)
2850 {
2851     int timeridx = gt_phys_redir_timeridx(env);
2852     return gt_tval_read(env, ri, timeridx);
2853 }
2854 
2855 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2856                                      uint64_t value)
2857 {
2858     int timeridx = gt_phys_redir_timeridx(env);
2859     gt_tval_write(env, ri, timeridx, value);
2860 }
2861 
2862 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2863                                        const ARMCPRegInfo *ri)
2864 {
2865     int timeridx = gt_phys_redir_timeridx(env);
2866     return env->cp15.c14_timer[timeridx].ctl;
2867 }
2868 
2869 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2870                                     uint64_t value)
2871 {
2872     int timeridx = gt_phys_redir_timeridx(env);
2873     gt_ctl_write(env, ri, timeridx, value);
2874 }
2875 
2876 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2877 {
2878     gt_timer_reset(env, ri, GTIMER_VIRT);
2879 }
2880 
2881 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2882                                uint64_t value)
2883 {
2884     gt_cval_write(env, ri, GTIMER_VIRT, value);
2885 }
2886 
2887 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2888 {
2889     return gt_tval_read(env, ri, GTIMER_VIRT);
2890 }
2891 
2892 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2893                                uint64_t value)
2894 {
2895     gt_tval_write(env, ri, GTIMER_VIRT, value);
2896 }
2897 
2898 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2899                               uint64_t value)
2900 {
2901     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2902 }
2903 
2904 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2905                              uint64_t value)
2906 {
2907     ARMCPU *cpu = env_archcpu(env);
2908     uint32_t oldval = env->cp15.cnthctl_el2;
2909 
2910     raw_write(env, ri, value);
2911 
2912     if ((oldval ^ value) & CNTHCTL_CNTVMASK) {
2913         gt_update_irq(cpu, GTIMER_VIRT);
2914     } else if ((oldval ^ value) & CNTHCTL_CNTPMASK) {
2915         gt_update_irq(cpu, GTIMER_PHYS);
2916     }
2917 }
2918 
2919 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2920                               uint64_t value)
2921 {
2922     ARMCPU *cpu = env_archcpu(env);
2923 
2924     trace_arm_gt_cntvoff_write(value);
2925     raw_write(env, ri, value);
2926     gt_recalc_timer(cpu, GTIMER_VIRT);
2927 }
2928 
2929 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2930                                         const ARMCPRegInfo *ri)
2931 {
2932     int timeridx = gt_virt_redir_timeridx(env);
2933     return env->cp15.c14_timer[timeridx].cval;
2934 }
2935 
2936 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2937                                      uint64_t value)
2938 {
2939     int timeridx = gt_virt_redir_timeridx(env);
2940     gt_cval_write(env, ri, timeridx, value);
2941 }
2942 
2943 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2944                                         const ARMCPRegInfo *ri)
2945 {
2946     int timeridx = gt_virt_redir_timeridx(env);
2947     return gt_tval_read(env, ri, timeridx);
2948 }
2949 
2950 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2951                                      uint64_t value)
2952 {
2953     int timeridx = gt_virt_redir_timeridx(env);
2954     gt_tval_write(env, ri, timeridx, value);
2955 }
2956 
2957 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2958                                        const ARMCPRegInfo *ri)
2959 {
2960     int timeridx = gt_virt_redir_timeridx(env);
2961     return env->cp15.c14_timer[timeridx].ctl;
2962 }
2963 
2964 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2965                                     uint64_t value)
2966 {
2967     int timeridx = gt_virt_redir_timeridx(env);
2968     gt_ctl_write(env, ri, timeridx, value);
2969 }
2970 
2971 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2972 {
2973     gt_timer_reset(env, ri, GTIMER_HYP);
2974 }
2975 
2976 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2977                               uint64_t value)
2978 {
2979     gt_cval_write(env, ri, GTIMER_HYP, value);
2980 }
2981 
2982 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2983 {
2984     return gt_tval_read(env, ri, GTIMER_HYP);
2985 }
2986 
2987 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2988                               uint64_t value)
2989 {
2990     gt_tval_write(env, ri, GTIMER_HYP, value);
2991 }
2992 
2993 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2994                               uint64_t value)
2995 {
2996     gt_ctl_write(env, ri, GTIMER_HYP, value);
2997 }
2998 
2999 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3000 {
3001     gt_timer_reset(env, ri, GTIMER_SEC);
3002 }
3003 
3004 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3005                               uint64_t value)
3006 {
3007     gt_cval_write(env, ri, GTIMER_SEC, value);
3008 }
3009 
3010 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3011 {
3012     return gt_tval_read(env, ri, GTIMER_SEC);
3013 }
3014 
3015 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3016                               uint64_t value)
3017 {
3018     gt_tval_write(env, ri, GTIMER_SEC, value);
3019 }
3020 
3021 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3022                               uint64_t value)
3023 {
3024     gt_ctl_write(env, ri, GTIMER_SEC, value);
3025 }
3026 
3027 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3028 {
3029     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3030 }
3031 
3032 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3033                              uint64_t value)
3034 {
3035     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3036 }
3037 
3038 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3039 {
3040     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3041 }
3042 
3043 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3044                              uint64_t value)
3045 {
3046     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3047 }
3048 
3049 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3050                             uint64_t value)
3051 {
3052     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3053 }
3054 
3055 void arm_gt_ptimer_cb(void *opaque)
3056 {
3057     ARMCPU *cpu = opaque;
3058 
3059     gt_recalc_timer(cpu, GTIMER_PHYS);
3060 }
3061 
3062 void arm_gt_vtimer_cb(void *opaque)
3063 {
3064     ARMCPU *cpu = opaque;
3065 
3066     gt_recalc_timer(cpu, GTIMER_VIRT);
3067 }
3068 
3069 void arm_gt_htimer_cb(void *opaque)
3070 {
3071     ARMCPU *cpu = opaque;
3072 
3073     gt_recalc_timer(cpu, GTIMER_HYP);
3074 }
3075 
3076 void arm_gt_stimer_cb(void *opaque)
3077 {
3078     ARMCPU *cpu = opaque;
3079 
3080     gt_recalc_timer(cpu, GTIMER_SEC);
3081 }
3082 
3083 void arm_gt_hvtimer_cb(void *opaque)
3084 {
3085     ARMCPU *cpu = opaque;
3086 
3087     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3088 }
3089 
3090 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3091 {
3092     ARMCPU *cpu = env_archcpu(env);
3093 
3094     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3095 }
3096 
3097 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3098     /*
3099      * Note that CNTFRQ is purely reads-as-written for the benefit
3100      * of software; writing it doesn't actually change the timer frequency.
3101      * Our reset value matches the fixed frequency we implement the timer at.
3102      */
3103     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3104       .type = ARM_CP_ALIAS,
3105       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3106       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3107     },
3108     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3109       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3110       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3111       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3112       .resetfn = arm_gt_cntfrq_reset,
3113     },
3114     /* overall control: mostly access permissions */
3115     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3116       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3117       .access = PL1_RW,
3118       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3119       .resetvalue = 0,
3120     },
3121     /* per-timer control */
3122     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3123       .secure = ARM_CP_SECSTATE_NS,
3124       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3125       .accessfn = gt_ptimer_access,
3126       .fieldoffset = offsetoflow32(CPUARMState,
3127                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3128       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3129       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3130     },
3131     { .name = "CNTP_CTL_S",
3132       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3133       .secure = ARM_CP_SECSTATE_S,
3134       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3135       .accessfn = gt_ptimer_access,
3136       .fieldoffset = offsetoflow32(CPUARMState,
3137                                    cp15.c14_timer[GTIMER_SEC].ctl),
3138       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3139     },
3140     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3141       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3142       .type = ARM_CP_IO, .access = PL0_RW,
3143       .accessfn = gt_ptimer_access,
3144       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3145       .resetvalue = 0,
3146       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3147       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3148     },
3149     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3150       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3151       .accessfn = gt_vtimer_access,
3152       .fieldoffset = offsetoflow32(CPUARMState,
3153                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3154       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3155       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3156     },
3157     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3158       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3159       .type = ARM_CP_IO, .access = PL0_RW,
3160       .accessfn = gt_vtimer_access,
3161       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3162       .resetvalue = 0,
3163       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3164       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3165     },
3166     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3167     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3168       .secure = ARM_CP_SECSTATE_NS,
3169       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3170       .accessfn = gt_ptimer_access,
3171       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3172     },
3173     { .name = "CNTP_TVAL_S",
3174       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3175       .secure = ARM_CP_SECSTATE_S,
3176       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3177       .accessfn = gt_ptimer_access,
3178       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3179     },
3180     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3181       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3182       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3183       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3184       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3185     },
3186     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3187       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3188       .accessfn = gt_vtimer_access,
3189       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3190     },
3191     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3192       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3193       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3194       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3195       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3196     },
3197     /* The counter itself */
3198     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3199       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3200       .accessfn = gt_pct_access,
3201       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3202     },
3203     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3204       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3205       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3206       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3207     },
3208     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3209       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3210       .accessfn = gt_vct_access,
3211       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3212     },
3213     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3214       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3215       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3216       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3217     },
3218     /* Comparison value, indicating when the timer goes off */
3219     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3220       .secure = ARM_CP_SECSTATE_NS,
3221       .access = PL0_RW,
3222       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3223       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3224       .accessfn = gt_ptimer_access,
3225       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3226       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3227     },
3228     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3229       .secure = ARM_CP_SECSTATE_S,
3230       .access = PL0_RW,
3231       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3232       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3233       .accessfn = gt_ptimer_access,
3234       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3235     },
3236     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3237       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3238       .access = PL0_RW,
3239       .type = ARM_CP_IO,
3240       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3241       .resetvalue = 0, .accessfn = gt_ptimer_access,
3242       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3243       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3244     },
3245     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3246       .access = PL0_RW,
3247       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3248       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3249       .accessfn = gt_vtimer_access,
3250       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3251       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3252     },
3253     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3254       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3255       .access = PL0_RW,
3256       .type = ARM_CP_IO,
3257       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3258       .resetvalue = 0, .accessfn = gt_vtimer_access,
3259       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3260       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3261     },
3262     /*
3263      * Secure timer -- this is actually restricted to only EL3
3264      * and configurably Secure-EL1 via the accessfn.
3265      */
3266     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3267       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3268       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3269       .accessfn = gt_stimer_access,
3270       .readfn = gt_sec_tval_read,
3271       .writefn = gt_sec_tval_write,
3272       .resetfn = gt_sec_timer_reset,
3273     },
3274     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3275       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3276       .type = ARM_CP_IO, .access = PL1_RW,
3277       .accessfn = gt_stimer_access,
3278       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3279       .resetvalue = 0,
3280       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3281     },
3282     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3283       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3284       .type = ARM_CP_IO, .access = PL1_RW,
3285       .accessfn = gt_stimer_access,
3286       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3287       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3288     },
3289 };
3290 
3291 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3292                                  bool isread)
3293 {
3294     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3295         return CP_ACCESS_TRAP;
3296     }
3297     return CP_ACCESS_OK;
3298 }
3299 
3300 #else
3301 
3302 /*
3303  * In user-mode most of the generic timer registers are inaccessible
3304  * however modern kernels (4.12+) allow access to cntvct_el0
3305  */
3306 
3307 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3308 {
3309     ARMCPU *cpu = env_archcpu(env);
3310 
3311     /*
3312      * Currently we have no support for QEMUTimer in linux-user so we
3313      * can't call gt_get_countervalue(env), instead we directly
3314      * call the lower level functions.
3315      */
3316     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3317 }
3318 
3319 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3320     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3321       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3322       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3323       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3324       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3325     },
3326     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3327       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3328       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3329       .readfn = gt_virt_cnt_read,
3330     },
3331 };
3332 
3333 #endif
3334 
3335 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3336 {
3337     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3338         raw_write(env, ri, value);
3339     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3340         raw_write(env, ri, value & 0xfffff6ff);
3341     } else {
3342         raw_write(env, ri, value & 0xfffff1ff);
3343     }
3344 }
3345 
3346 #ifndef CONFIG_USER_ONLY
3347 /* get_phys_addr() isn't present for user-mode-only targets */
3348 
3349 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3350                                  bool isread)
3351 {
3352     if (ri->opc2 & 4) {
3353         /*
3354          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3355          * Secure EL1 (which can only happen if EL3 is AArch64).
3356          * They are simply UNDEF if executed from NS EL1.
3357          * They function normally from EL2 or EL3.
3358          */
3359         if (arm_current_el(env) == 1) {
3360             if (arm_is_secure_below_el3(env)) {
3361                 if (env->cp15.scr_el3 & SCR_EEL2) {
3362                     return CP_ACCESS_TRAP_EL2;
3363                 }
3364                 return CP_ACCESS_TRAP_EL3;
3365             }
3366             return CP_ACCESS_TRAP_UNCATEGORIZED;
3367         }
3368     }
3369     return CP_ACCESS_OK;
3370 }
3371 
3372 #ifdef CONFIG_TCG
3373 static int par_el1_shareability(GetPhysAddrResult *res)
3374 {
3375     /*
3376      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3377      * memory -- see pseudocode PAREncodeShareability().
3378      */
3379     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3380         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3381         return 2;
3382     }
3383     return res->cacheattrs.shareability;
3384 }
3385 
3386 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3387                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3388                              ARMSecuritySpace ss)
3389 {
3390     bool ret;
3391     uint64_t par64;
3392     bool format64 = false;
3393     ARMMMUFaultInfo fi = {};
3394     GetPhysAddrResult res = {};
3395 
3396     /*
3397      * I_MXTJT: Granule protection checks are not performed on the final address
3398      * of a successful translation.
3399      */
3400     ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss,
3401                                          &res, &fi);
3402 
3403     /*
3404      * ATS operations only do S1 or S1+S2 translations, so we never
3405      * have to deal with the ARMCacheAttrs format for S2 only.
3406      */
3407     assert(!res.cacheattrs.is_s2_format);
3408 
3409     if (ret) {
3410         /*
3411          * Some kinds of translation fault must cause exceptions rather
3412          * than being reported in the PAR.
3413          */
3414         int current_el = arm_current_el(env);
3415         int target_el;
3416         uint32_t syn, fsr, fsc;
3417         bool take_exc = false;
3418 
3419         if (fi.s1ptw && current_el == 1
3420             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3421             /*
3422              * Synchronous stage 2 fault on an access made as part of the
3423              * translation table walk for AT S1E0* or AT S1E1* insn
3424              * executed from NS EL1. If this is a synchronous external abort
3425              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3426              * to EL3. Otherwise the fault is taken as an exception to EL2,
3427              * and HPFAR_EL2 holds the faulting IPA.
3428              */
3429             if (fi.type == ARMFault_SyncExternalOnWalk &&
3430                 (env->cp15.scr_el3 & SCR_EA)) {
3431                 target_el = 3;
3432             } else {
3433                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3434                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3435                     env->cp15.hpfar_el2 |= HPFAR_NS;
3436                 }
3437                 target_el = 2;
3438             }
3439             take_exc = true;
3440         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3441             /*
3442              * Synchronous external aborts during a translation table walk
3443              * are taken as Data Abort exceptions.
3444              */
3445             if (fi.stage2) {
3446                 if (current_el == 3) {
3447                     target_el = 3;
3448                 } else {
3449                     target_el = 2;
3450                 }
3451             } else {
3452                 target_el = exception_target_el(env);
3453             }
3454             take_exc = true;
3455         }
3456 
3457         if (take_exc) {
3458             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3459             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3460                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3461                 fsr = arm_fi_to_lfsc(&fi);
3462                 fsc = extract32(fsr, 0, 6);
3463             } else {
3464                 fsr = arm_fi_to_sfsc(&fi);
3465                 fsc = 0x3f;
3466             }
3467             /*
3468              * Report exception with ESR indicating a fault due to a
3469              * translation table walk for a cache maintenance instruction.
3470              */
3471             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3472                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3473             env->exception.vaddress = value;
3474             env->exception.fsr = fsr;
3475             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3476         }
3477     }
3478 
3479     if (is_a64(env)) {
3480         format64 = true;
3481     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3482         /*
3483          * ATS1Cxx:
3484          * * TTBCR.EAE determines whether the result is returned using the
3485          *   32-bit or the 64-bit PAR format
3486          * * Instructions executed in Hyp mode always use the 64bit format
3487          *
3488          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3489          * * The Non-secure TTBCR.EAE bit is set to 1
3490          * * The implementation includes EL2, and the value of HCR.VM is 1
3491          *
3492          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3493          *
3494          * ATS1Hx always uses the 64bit format.
3495          */
3496         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3497 
3498         if (arm_feature(env, ARM_FEATURE_EL2)) {
3499             if (mmu_idx == ARMMMUIdx_E10_0 ||
3500                 mmu_idx == ARMMMUIdx_E10_1 ||
3501                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3502                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3503             } else {
3504                 format64 |= arm_current_el(env) == 2;
3505             }
3506         }
3507     }
3508 
3509     if (format64) {
3510         /* Create a 64-bit PAR */
3511         par64 = (1 << 11); /* LPAE bit always set */
3512         if (!ret) {
3513             par64 |= res.f.phys_addr & ~0xfffULL;
3514             if (!res.f.attrs.secure) {
3515                 par64 |= (1 << 9); /* NS */
3516             }
3517             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3518             par64 |= par_el1_shareability(&res) << 7; /* SH */
3519         } else {
3520             uint32_t fsr = arm_fi_to_lfsc(&fi);
3521 
3522             par64 |= 1; /* F */
3523             par64 |= (fsr & 0x3f) << 1; /* FS */
3524             if (fi.stage2) {
3525                 par64 |= (1 << 9); /* S */
3526             }
3527             if (fi.s1ptw) {
3528                 par64 |= (1 << 8); /* PTW */
3529             }
3530         }
3531     } else {
3532         /*
3533          * fsr is a DFSR/IFSR value for the short descriptor
3534          * translation table format (with WnR always clear).
3535          * Convert it to a 32-bit PAR.
3536          */
3537         if (!ret) {
3538             /* We do not set any attribute bits in the PAR */
3539             if (res.f.lg_page_size == 24
3540                 && arm_feature(env, ARM_FEATURE_V7)) {
3541                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3542             } else {
3543                 par64 = res.f.phys_addr & 0xfffff000;
3544             }
3545             if (!res.f.attrs.secure) {
3546                 par64 |= (1 << 9); /* NS */
3547             }
3548         } else {
3549             uint32_t fsr = arm_fi_to_sfsc(&fi);
3550 
3551             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3552                     ((fsr & 0xf) << 1) | 1;
3553         }
3554     }
3555     return par64;
3556 }
3557 #endif /* CONFIG_TCG */
3558 
3559 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3560 {
3561 #ifdef CONFIG_TCG
3562     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3563     uint64_t par64;
3564     ARMMMUIdx mmu_idx;
3565     int el = arm_current_el(env);
3566     ARMSecuritySpace ss = arm_security_space(env);
3567 
3568     switch (ri->opc2 & 6) {
3569     case 0:
3570         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3571         switch (el) {
3572         case 3:
3573             mmu_idx = ARMMMUIdx_E3;
3574             break;
3575         case 2:
3576             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3577             /* fall through */
3578         case 1:
3579             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3580                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3581             } else {
3582                 mmu_idx = ARMMMUIdx_Stage1_E1;
3583             }
3584             break;
3585         default:
3586             g_assert_not_reached();
3587         }
3588         break;
3589     case 2:
3590         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3591         switch (el) {
3592         case 3:
3593             mmu_idx = ARMMMUIdx_E10_0;
3594             break;
3595         case 2:
3596             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3597             mmu_idx = ARMMMUIdx_Stage1_E0;
3598             break;
3599         case 1:
3600             mmu_idx = ARMMMUIdx_Stage1_E0;
3601             break;
3602         default:
3603             g_assert_not_reached();
3604         }
3605         break;
3606     case 4:
3607         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3608         mmu_idx = ARMMMUIdx_E10_1;
3609         ss = ARMSS_NonSecure;
3610         break;
3611     case 6:
3612         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3613         mmu_idx = ARMMMUIdx_E10_0;
3614         ss = ARMSS_NonSecure;
3615         break;
3616     default:
3617         g_assert_not_reached();
3618     }
3619 
3620     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3621 
3622     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3623 #else
3624     /* Handled by hardware accelerator. */
3625     g_assert_not_reached();
3626 #endif /* CONFIG_TCG */
3627 }
3628 
3629 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3630                         uint64_t value)
3631 {
3632 #ifdef CONFIG_TCG
3633     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3634     uint64_t par64;
3635 
3636     /* There is no SecureEL2 for AArch32. */
3637     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3638                          ARMSS_NonSecure);
3639 
3640     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3641 #else
3642     /* Handled by hardware accelerator. */
3643     g_assert_not_reached();
3644 #endif /* CONFIG_TCG */
3645 }
3646 
3647 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3648                                      bool isread)
3649 {
3650     /*
3651      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3652      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3653      * only happen when executing at EL3 because that combination also causes an
3654      * illegal exception return. We don't need to check FEAT_RME either, because
3655      * scr_write() ensures that the NSE bit is not set otherwise.
3656      */
3657     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3658         return CP_ACCESS_TRAP;
3659     }
3660     return CP_ACCESS_OK;
3661 }
3662 
3663 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3664                                      bool isread)
3665 {
3666     if (arm_current_el(env) == 3 &&
3667         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3668         return CP_ACCESS_TRAP;
3669     }
3670     return at_e012_access(env, ri, isread);
3671 }
3672 
3673 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3674                         uint64_t value)
3675 {
3676 #ifdef CONFIG_TCG
3677     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3678     ARMMMUIdx mmu_idx;
3679     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3680     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3681 
3682     switch (ri->opc2 & 6) {
3683     case 0:
3684         switch (ri->opc1) {
3685         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3686             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3687                 mmu_idx = regime_e20 ?
3688                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3689             } else {
3690                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3691             }
3692             break;
3693         case 4: /* AT S1E2R, AT S1E2W */
3694             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3695             break;
3696         case 6: /* AT S1E3R, AT S1E3W */
3697             mmu_idx = ARMMMUIdx_E3;
3698             break;
3699         default:
3700             g_assert_not_reached();
3701         }
3702         break;
3703     case 2: /* AT S1E0R, AT S1E0W */
3704         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3705         break;
3706     case 4: /* AT S12E1R, AT S12E1W */
3707         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3708         break;
3709     case 6: /* AT S12E0R, AT S12E0W */
3710         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3711         break;
3712     default:
3713         g_assert_not_reached();
3714     }
3715 
3716     env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3717                                        mmu_idx, arm_security_space(env));
3718 #else
3719     /* Handled by hardware accelerator. */
3720     g_assert_not_reached();
3721 #endif /* CONFIG_TCG */
3722 }
3723 #endif
3724 
3725 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3726     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3727       .access = PL1_RW, .resetvalue = 0,
3728       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3729                              offsetoflow32(CPUARMState, cp15.par_ns) },
3730       .writefn = par_write },
3731 #ifndef CONFIG_USER_ONLY
3732     /* This underdecoding is safe because the reginfo is NO_RAW. */
3733     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3734       .access = PL1_W, .accessfn = ats_access,
3735       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3736 #endif
3737 };
3738 
3739 /* Return basic MPU access permission bits.  */
3740 static uint32_t simple_mpu_ap_bits(uint32_t val)
3741 {
3742     uint32_t ret;
3743     uint32_t mask;
3744     int i;
3745     ret = 0;
3746     mask = 3;
3747     for (i = 0; i < 16; i += 2) {
3748         ret |= (val >> i) & mask;
3749         mask <<= 2;
3750     }
3751     return ret;
3752 }
3753 
3754 /* Pad basic MPU access permission bits to extended format.  */
3755 static uint32_t extended_mpu_ap_bits(uint32_t val)
3756 {
3757     uint32_t ret;
3758     uint32_t mask;
3759     int i;
3760     ret = 0;
3761     mask = 3;
3762     for (i = 0; i < 16; i += 2) {
3763         ret |= (val & mask) << i;
3764         mask <<= 2;
3765     }
3766     return ret;
3767 }
3768 
3769 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3770                                  uint64_t value)
3771 {
3772     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3773 }
3774 
3775 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3776 {
3777     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3778 }
3779 
3780 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3781                                  uint64_t value)
3782 {
3783     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3784 }
3785 
3786 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3787 {
3788     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3789 }
3790 
3791 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3792 {
3793     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3794 
3795     if (!u32p) {
3796         return 0;
3797     }
3798 
3799     u32p += env->pmsav7.rnr[M_REG_NS];
3800     return *u32p;
3801 }
3802 
3803 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3804                          uint64_t value)
3805 {
3806     ARMCPU *cpu = env_archcpu(env);
3807     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3808 
3809     if (!u32p) {
3810         return;
3811     }
3812 
3813     u32p += env->pmsav7.rnr[M_REG_NS];
3814     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3815     *u32p = value;
3816 }
3817 
3818 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3819                               uint64_t value)
3820 {
3821     ARMCPU *cpu = env_archcpu(env);
3822     uint32_t nrgs = cpu->pmsav7_dregion;
3823 
3824     if (value >= nrgs) {
3825         qemu_log_mask(LOG_GUEST_ERROR,
3826                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3827                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3828         return;
3829     }
3830 
3831     raw_write(env, ri, value);
3832 }
3833 
3834 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3835                           uint64_t value)
3836 {
3837     ARMCPU *cpu = env_archcpu(env);
3838 
3839     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3840     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3841 }
3842 
3843 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3844 {
3845     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3846 }
3847 
3848 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3849                           uint64_t value)
3850 {
3851     ARMCPU *cpu = env_archcpu(env);
3852 
3853     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3854     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3855 }
3856 
3857 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3858 {
3859     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3860 }
3861 
3862 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3863                            uint64_t value)
3864 {
3865     ARMCPU *cpu = env_archcpu(env);
3866 
3867     /*
3868      * Ignore writes that would select not implemented region.
3869      * This is architecturally UNPREDICTABLE.
3870      */
3871     if (value >= cpu->pmsav7_dregion) {
3872         return;
3873     }
3874 
3875     env->pmsav7.rnr[M_REG_NS] = value;
3876 }
3877 
3878 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3879                           uint64_t value)
3880 {
3881     ARMCPU *cpu = env_archcpu(env);
3882 
3883     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3884     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3885 }
3886 
3887 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3888 {
3889     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3890 }
3891 
3892 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3893                           uint64_t value)
3894 {
3895     ARMCPU *cpu = env_archcpu(env);
3896 
3897     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3898     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3899 }
3900 
3901 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3902 {
3903     return env->pmsav8.hprlar[env->pmsav8.hprselr];
3904 }
3905 
3906 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3907                           uint64_t value)
3908 {
3909     uint32_t n;
3910     uint32_t bit;
3911     ARMCPU *cpu = env_archcpu(env);
3912 
3913     /* Ignore writes to unimplemented regions */
3914     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3915     value &= MAKE_64BIT_MASK(0, rmax);
3916 
3917     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3918 
3919     /* Register alias is only valid for first 32 indexes */
3920     for (n = 0; n < rmax; ++n) {
3921         bit = extract32(value, n, 1);
3922         env->pmsav8.hprlar[n] = deposit32(
3923                     env->pmsav8.hprlar[n], 0, 1, bit);
3924     }
3925 }
3926 
3927 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3928 {
3929     uint32_t n;
3930     uint32_t result = 0x0;
3931     ARMCPU *cpu = env_archcpu(env);
3932 
3933     /* Register alias is only valid for first 32 indexes */
3934     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3935         if (env->pmsav8.hprlar[n] & 0x1) {
3936             result |= (0x1 << n);
3937         }
3938     }
3939     return result;
3940 }
3941 
3942 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3943                            uint64_t value)
3944 {
3945     ARMCPU *cpu = env_archcpu(env);
3946 
3947     /*
3948      * Ignore writes that would select not implemented region.
3949      * This is architecturally UNPREDICTABLE.
3950      */
3951     if (value >= cpu->pmsav8r_hdregion) {
3952         return;
3953     }
3954 
3955     env->pmsav8.hprselr = value;
3956 }
3957 
3958 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
3959                           uint64_t value)
3960 {
3961     ARMCPU *cpu = env_archcpu(env);
3962     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3963                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3964 
3965     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3966 
3967     if (ri->opc1 & 4) {
3968         if (index >= cpu->pmsav8r_hdregion) {
3969             return;
3970         }
3971         if (ri->opc2 & 0x1) {
3972             env->pmsav8.hprlar[index] = value;
3973         } else {
3974             env->pmsav8.hprbar[index] = value;
3975         }
3976     } else {
3977         if (index >= cpu->pmsav7_dregion) {
3978             return;
3979         }
3980         if (ri->opc2 & 0x1) {
3981             env->pmsav8.rlar[M_REG_NS][index] = value;
3982         } else {
3983             env->pmsav8.rbar[M_REG_NS][index] = value;
3984         }
3985     }
3986 }
3987 
3988 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
3989 {
3990     ARMCPU *cpu = env_archcpu(env);
3991     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3992                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3993 
3994     if (ri->opc1 & 4) {
3995         if (index >= cpu->pmsav8r_hdregion) {
3996             return 0x0;
3997         }
3998         if (ri->opc2 & 0x1) {
3999             return env->pmsav8.hprlar[index];
4000         } else {
4001             return env->pmsav8.hprbar[index];
4002         }
4003     } else {
4004         if (index >= cpu->pmsav7_dregion) {
4005             return 0x0;
4006         }
4007         if (ri->opc2 & 0x1) {
4008             return env->pmsav8.rlar[M_REG_NS][index];
4009         } else {
4010             return env->pmsav8.rbar[M_REG_NS][index];
4011         }
4012     }
4013 }
4014 
4015 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4016     { .name = "PRBAR",
4017       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4018       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4019       .accessfn = access_tvm_trvm,
4020       .readfn = prbar_read, .writefn = prbar_write },
4021     { .name = "PRLAR",
4022       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4023       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4024       .accessfn = access_tvm_trvm,
4025       .readfn = prlar_read, .writefn = prlar_write },
4026     { .name = "PRSELR", .resetvalue = 0,
4027       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4028       .access = PL1_RW, .accessfn = access_tvm_trvm,
4029       .writefn = prselr_write,
4030       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4031     { .name = "HPRBAR", .resetvalue = 0,
4032       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4033       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4034       .readfn = hprbar_read, .writefn = hprbar_write },
4035     { .name = "HPRLAR",
4036       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4037       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4038       .readfn = hprlar_read, .writefn = hprlar_write },
4039     { .name = "HPRSELR", .resetvalue = 0,
4040       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4041       .access = PL2_RW,
4042       .writefn = hprselr_write,
4043       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4044     { .name = "HPRENR",
4045       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4046       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4047       .readfn = hprenr_read, .writefn = hprenr_write },
4048 };
4049 
4050 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4051     /*
4052      * Reset for all these registers is handled in arm_cpu_reset(),
4053      * because the PMSAv7 is also used by M-profile CPUs, which do
4054      * not register cpregs but still need the state to be reset.
4055      */
4056     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4057       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4058       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4059       .readfn = pmsav7_read, .writefn = pmsav7_write,
4060       .resetfn = arm_cp_reset_ignore },
4061     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4062       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4063       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4064       .readfn = pmsav7_read, .writefn = pmsav7_write,
4065       .resetfn = arm_cp_reset_ignore },
4066     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4067       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4068       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4069       .readfn = pmsav7_read, .writefn = pmsav7_write,
4070       .resetfn = arm_cp_reset_ignore },
4071     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4072       .access = PL1_RW,
4073       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4074       .writefn = pmsav7_rgnr_write,
4075       .resetfn = arm_cp_reset_ignore },
4076 };
4077 
4078 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4079     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4080       .access = PL1_RW, .type = ARM_CP_ALIAS,
4081       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4082       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4083     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4084       .access = PL1_RW, .type = ARM_CP_ALIAS,
4085       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4086       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4087     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4088       .access = PL1_RW,
4089       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4090       .resetvalue = 0, },
4091     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4092       .access = PL1_RW,
4093       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4094       .resetvalue = 0, },
4095     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4096       .access = PL1_RW,
4097       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4098     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4099       .access = PL1_RW,
4100       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4101     /* Protection region base and size registers */
4102     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4103       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4104       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4105     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4106       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4107       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4108     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4109       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4110       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4111     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4112       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4113       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4114     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4115       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4116       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4117     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4118       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4119       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4120     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4121       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4122       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4123     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4124       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4125       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4126 };
4127 
4128 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4129                              uint64_t value)
4130 {
4131     ARMCPU *cpu = env_archcpu(env);
4132 
4133     if (!arm_feature(env, ARM_FEATURE_V8)) {
4134         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4135             /*
4136              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4137              * using Long-descriptor translation table format
4138              */
4139             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4140         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4141             /*
4142              * In an implementation that includes the Security Extensions
4143              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4144              * Short-descriptor translation table format.
4145              */
4146             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4147         } else {
4148             value &= TTBCR_N;
4149         }
4150     }
4151 
4152     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4153         /*
4154          * With LPAE the TTBCR could result in a change of ASID
4155          * via the TTBCR.A1 bit, so do a TLB flush.
4156          */
4157         tlb_flush(CPU(cpu));
4158     }
4159     raw_write(env, ri, value);
4160 }
4161 
4162 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4163                                uint64_t value)
4164 {
4165     ARMCPU *cpu = env_archcpu(env);
4166 
4167     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4168     tlb_flush(CPU(cpu));
4169     raw_write(env, ri, value);
4170 }
4171 
4172 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4173                             uint64_t value)
4174 {
4175     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4176     if (cpreg_field_is_64bit(ri) &&
4177         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4178         ARMCPU *cpu = env_archcpu(env);
4179         tlb_flush(CPU(cpu));
4180     }
4181     raw_write(env, ri, value);
4182 }
4183 
4184 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4185                                     uint64_t value)
4186 {
4187     /*
4188      * If we are running with E2&0 regime, then an ASID is active.
4189      * Flush if that might be changing.  Note we're not checking
4190      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4191      * holds the active ASID, only checking the field that might.
4192      */
4193     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4194         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4195         uint16_t mask = ARMMMUIdxBit_E20_2 |
4196                         ARMMMUIdxBit_E20_2_PAN |
4197                         ARMMMUIdxBit_E20_0;
4198         tlb_flush_by_mmuidx(env_cpu(env), mask);
4199     }
4200     raw_write(env, ri, value);
4201 }
4202 
4203 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4204                         uint64_t value)
4205 {
4206     ARMCPU *cpu = env_archcpu(env);
4207     CPUState *cs = CPU(cpu);
4208 
4209     /*
4210      * A change in VMID to the stage2 page table (Stage2) invalidates
4211      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4212      */
4213     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4214         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4215     }
4216     raw_write(env, ri, value);
4217 }
4218 
4219 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4220     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4221       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4222       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4223                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4224     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4225       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4226       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4227                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4228     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4229       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4230       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4231                              offsetof(CPUARMState, cp15.dfar_ns) } },
4232     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4233       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4234       .access = PL1_RW, .accessfn = access_tvm_trvm,
4235       .fgt = FGT_FAR_EL1,
4236       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4237       .resetvalue = 0, },
4238 };
4239 
4240 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4241     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4242       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4243       .access = PL1_RW, .accessfn = access_tvm_trvm,
4244       .fgt = FGT_ESR_EL1,
4245       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4246     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4247       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4248       .access = PL1_RW, .accessfn = access_tvm_trvm,
4249       .fgt = FGT_TTBR0_EL1,
4250       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4251       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4252                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4253     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4254       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4255       .access = PL1_RW, .accessfn = access_tvm_trvm,
4256       .fgt = FGT_TTBR1_EL1,
4257       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4258       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4259                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4260     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4261       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4262       .access = PL1_RW, .accessfn = access_tvm_trvm,
4263       .fgt = FGT_TCR_EL1,
4264       .writefn = vmsa_tcr_el12_write,
4265       .raw_writefn = raw_write,
4266       .resetvalue = 0,
4267       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4268     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4269       .access = PL1_RW, .accessfn = access_tvm_trvm,
4270       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4271       .raw_writefn = raw_write,
4272       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4273                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4274 };
4275 
4276 /*
4277  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4278  * qemu tlbs nor adjusting cached masks.
4279  */
4280 static const ARMCPRegInfo ttbcr2_reginfo = {
4281     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4282     .access = PL1_RW, .accessfn = access_tvm_trvm,
4283     .type = ARM_CP_ALIAS,
4284     .bank_fieldoffsets = {
4285         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4286         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4287     },
4288 };
4289 
4290 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4291                                 uint64_t value)
4292 {
4293     env->cp15.c15_ticonfig = value & 0xe7;
4294     /* The OS_TYPE bit in this register changes the reported CPUID! */
4295     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4296         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4297 }
4298 
4299 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4300                                 uint64_t value)
4301 {
4302     env->cp15.c15_threadid = value & 0xffff;
4303 }
4304 
4305 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4306                            uint64_t value)
4307 {
4308     /* Wait-for-interrupt (deprecated) */
4309     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4310 }
4311 
4312 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4313                                   uint64_t value)
4314 {
4315     /*
4316      * On OMAP there are registers indicating the max/min index of dcache lines
4317      * containing a dirty line; cache flush operations have to reset these.
4318      */
4319     env->cp15.c15_i_max = 0x000;
4320     env->cp15.c15_i_min = 0xff0;
4321 }
4322 
4323 static const ARMCPRegInfo omap_cp_reginfo[] = {
4324     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4325       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4326       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4327       .resetvalue = 0, },
4328     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4329       .access = PL1_RW, .type = ARM_CP_NOP },
4330     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4331       .access = PL1_RW,
4332       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4333       .writefn = omap_ticonfig_write },
4334     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4335       .access = PL1_RW,
4336       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4337     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4338       .access = PL1_RW, .resetvalue = 0xff0,
4339       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4340     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4341       .access = PL1_RW,
4342       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4343       .writefn = omap_threadid_write },
4344     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4345       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4346       .type = ARM_CP_NO_RAW,
4347       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4348     /*
4349      * TODO: Peripheral port remap register:
4350      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4351      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4352      * when MMU is off.
4353      */
4354     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4355       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4356       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4357       .writefn = omap_cachemaint_write },
4358     { .name = "C9", .cp = 15, .crn = 9,
4359       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4360       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4361 };
4362 
4363 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4364                               uint64_t value)
4365 {
4366     env->cp15.c15_cpar = value & 0x3fff;
4367 }
4368 
4369 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4370     { .name = "XSCALE_CPAR",
4371       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4372       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4373       .writefn = xscale_cpar_write, },
4374     { .name = "XSCALE_AUXCR",
4375       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4376       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4377       .resetvalue = 0, },
4378     /*
4379      * XScale specific cache-lockdown: since we have no cache we NOP these
4380      * and hope the guest does not really rely on cache behaviour.
4381      */
4382     { .name = "XSCALE_LOCK_ICACHE_LINE",
4383       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4384       .access = PL1_W, .type = ARM_CP_NOP },
4385     { .name = "XSCALE_UNLOCK_ICACHE",
4386       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4387       .access = PL1_W, .type = ARM_CP_NOP },
4388     { .name = "XSCALE_DCACHE_LOCK",
4389       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4390       .access = PL1_RW, .type = ARM_CP_NOP },
4391     { .name = "XSCALE_UNLOCK_DCACHE",
4392       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4393       .access = PL1_W, .type = ARM_CP_NOP },
4394 };
4395 
4396 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4397     /*
4398      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4399      * implementation of this implementation-defined space.
4400      * Ideally this should eventually disappear in favour of actually
4401      * implementing the correct behaviour for all cores.
4402      */
4403     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4404       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4405       .access = PL1_RW,
4406       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4407       .resetvalue = 0 },
4408 };
4409 
4410 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4411     /* Cache status: RAZ because we have no cache so it's always clean */
4412     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4413       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4414       .resetvalue = 0 },
4415 };
4416 
4417 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4418     /* We never have a block transfer operation in progress */
4419     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4420       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4421       .resetvalue = 0 },
4422     /* The cache ops themselves: these all NOP for QEMU */
4423     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4424       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4425     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4426       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4427     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4428       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4429     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4430       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4431     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4432       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4433     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4434       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4435 };
4436 
4437 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4438     /*
4439      * The cache test-and-clean instructions always return (1 << 30)
4440      * to indicate that there are no dirty cache lines.
4441      */
4442     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4443       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4444       .resetvalue = (1 << 30) },
4445     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4446       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4447       .resetvalue = (1 << 30) },
4448 };
4449 
4450 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4451     /* Ignore ReadBuffer accesses */
4452     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4453       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4454       .access = PL1_RW, .resetvalue = 0,
4455       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4456 };
4457 
4458 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4459 {
4460     unsigned int cur_el = arm_current_el(env);
4461 
4462     if (arm_is_el2_enabled(env) && cur_el == 1) {
4463         return env->cp15.vpidr_el2;
4464     }
4465     return raw_read(env, ri);
4466 }
4467 
4468 static uint64_t mpidr_read_val(CPUARMState *env)
4469 {
4470     ARMCPU *cpu = env_archcpu(env);
4471     uint64_t mpidr = cpu->mp_affinity;
4472 
4473     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4474         mpidr |= (1U << 31);
4475         /*
4476          * Cores which are uniprocessor (non-coherent)
4477          * but still implement the MP extensions set
4478          * bit 30. (For instance, Cortex-R5).
4479          */
4480         if (cpu->mp_is_up) {
4481             mpidr |= (1u << 30);
4482         }
4483     }
4484     return mpidr;
4485 }
4486 
4487 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4488 {
4489     unsigned int cur_el = arm_current_el(env);
4490 
4491     if (arm_is_el2_enabled(env) && cur_el == 1) {
4492         return env->cp15.vmpidr_el2;
4493     }
4494     return mpidr_read_val(env);
4495 }
4496 
4497 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4498     /* NOP AMAIR0/1 */
4499     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4500       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4501       .access = PL1_RW, .accessfn = access_tvm_trvm,
4502       .fgt = FGT_AMAIR_EL1,
4503       .type = ARM_CP_CONST, .resetvalue = 0 },
4504     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4505     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4506       .access = PL1_RW, .accessfn = access_tvm_trvm,
4507       .type = ARM_CP_CONST, .resetvalue = 0 },
4508     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4509       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4510       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4511                              offsetof(CPUARMState, cp15.par_ns)} },
4512     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4513       .access = PL1_RW, .accessfn = access_tvm_trvm,
4514       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4515       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4516                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4517       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4518     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4519       .access = PL1_RW, .accessfn = access_tvm_trvm,
4520       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4521       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4522                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4523       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4524 };
4525 
4526 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4527 {
4528     return vfp_get_fpcr(env);
4529 }
4530 
4531 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4532                             uint64_t value)
4533 {
4534     vfp_set_fpcr(env, value);
4535 }
4536 
4537 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4538 {
4539     return vfp_get_fpsr(env);
4540 }
4541 
4542 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4543                             uint64_t value)
4544 {
4545     vfp_set_fpsr(env, value);
4546 }
4547 
4548 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4549                                        bool isread)
4550 {
4551     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4552         return CP_ACCESS_TRAP;
4553     }
4554     return CP_ACCESS_OK;
4555 }
4556 
4557 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4558                             uint64_t value)
4559 {
4560     env->daif = value & PSTATE_DAIF;
4561 }
4562 
4563 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4564 {
4565     return env->pstate & PSTATE_PAN;
4566 }
4567 
4568 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4569                            uint64_t value)
4570 {
4571     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4572 }
4573 
4574 static const ARMCPRegInfo pan_reginfo = {
4575     .name = "PAN", .state = ARM_CP_STATE_AA64,
4576     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4577     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4578     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4579 };
4580 
4581 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4582 {
4583     return env->pstate & PSTATE_UAO;
4584 }
4585 
4586 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4587                            uint64_t value)
4588 {
4589     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4590 }
4591 
4592 static const ARMCPRegInfo uao_reginfo = {
4593     .name = "UAO", .state = ARM_CP_STATE_AA64,
4594     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4595     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4596     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4597 };
4598 
4599 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4600 {
4601     return env->pstate & PSTATE_DIT;
4602 }
4603 
4604 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4605                            uint64_t value)
4606 {
4607     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4608 }
4609 
4610 static const ARMCPRegInfo dit_reginfo = {
4611     .name = "DIT", .state = ARM_CP_STATE_AA64,
4612     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4613     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4614     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4615 };
4616 
4617 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4618 {
4619     return env->pstate & PSTATE_SSBS;
4620 }
4621 
4622 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4623                            uint64_t value)
4624 {
4625     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4626 }
4627 
4628 static const ARMCPRegInfo ssbs_reginfo = {
4629     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4630     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4631     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4632     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4633 };
4634 
4635 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4636                                               const ARMCPRegInfo *ri,
4637                                               bool isread)
4638 {
4639     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4640     switch (arm_current_el(env)) {
4641     case 0:
4642         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4643         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4644             return CP_ACCESS_TRAP;
4645         }
4646         /* fall through */
4647     case 1:
4648         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4649         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4650             return CP_ACCESS_TRAP_EL2;
4651         }
4652         break;
4653     }
4654     return CP_ACCESS_OK;
4655 }
4656 
4657 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4658 {
4659     /* Cache invalidate/clean to Point of Unification... */
4660     switch (arm_current_el(env)) {
4661     case 0:
4662         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4663         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4664             return CP_ACCESS_TRAP;
4665         }
4666         /* fall through */
4667     case 1:
4668         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4669         if (arm_hcr_el2_eff(env) & hcrflags) {
4670             return CP_ACCESS_TRAP_EL2;
4671         }
4672         break;
4673     }
4674     return CP_ACCESS_OK;
4675 }
4676 
4677 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4678                                    bool isread)
4679 {
4680     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4681 }
4682 
4683 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4684                                   bool isread)
4685 {
4686     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4687 }
4688 
4689 /*
4690  * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4691  * Page D4-1736 (DDI0487A.b)
4692  */
4693 
4694 static int vae1_tlbmask(CPUARMState *env)
4695 {
4696     uint64_t hcr = arm_hcr_el2_eff(env);
4697     uint16_t mask;
4698 
4699     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4700         mask = ARMMMUIdxBit_E20_2 |
4701                ARMMMUIdxBit_E20_2_PAN |
4702                ARMMMUIdxBit_E20_0;
4703     } else {
4704         mask = ARMMMUIdxBit_E10_1 |
4705                ARMMMUIdxBit_E10_1_PAN |
4706                ARMMMUIdxBit_E10_0;
4707     }
4708     return mask;
4709 }
4710 
4711 static int vae2_tlbmask(CPUARMState *env)
4712 {
4713     uint64_t hcr = arm_hcr_el2_eff(env);
4714     uint16_t mask;
4715 
4716     if (hcr & HCR_E2H) {
4717         mask = ARMMMUIdxBit_E20_2 |
4718                ARMMMUIdxBit_E20_2_PAN |
4719                ARMMMUIdxBit_E20_0;
4720     } else {
4721         mask = ARMMMUIdxBit_E2;
4722     }
4723     return mask;
4724 }
4725 
4726 /* Return 56 if TBI is enabled, 64 otherwise. */
4727 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4728                               uint64_t addr)
4729 {
4730     uint64_t tcr = regime_tcr(env, mmu_idx);
4731     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4732     int select = extract64(addr, 55, 1);
4733 
4734     return (tbi >> select) & 1 ? 56 : 64;
4735 }
4736 
4737 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4738 {
4739     uint64_t hcr = arm_hcr_el2_eff(env);
4740     ARMMMUIdx mmu_idx;
4741 
4742     /* Only the regime of the mmu_idx below is significant. */
4743     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4744         mmu_idx = ARMMMUIdx_E20_0;
4745     } else {
4746         mmu_idx = ARMMMUIdx_E10_0;
4747     }
4748 
4749     return tlbbits_for_regime(env, mmu_idx, addr);
4750 }
4751 
4752 static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
4753 {
4754     uint64_t hcr = arm_hcr_el2_eff(env);
4755     ARMMMUIdx mmu_idx;
4756 
4757     /*
4758      * Only the regime of the mmu_idx below is significant.
4759      * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
4760      * only has one.
4761      */
4762     if (hcr & HCR_E2H) {
4763         mmu_idx = ARMMMUIdx_E20_2;
4764     } else {
4765         mmu_idx = ARMMMUIdx_E2;
4766     }
4767 
4768     return tlbbits_for_regime(env, mmu_idx, addr);
4769 }
4770 
4771 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4772                                       uint64_t value)
4773 {
4774     CPUState *cs = env_cpu(env);
4775     int mask = vae1_tlbmask(env);
4776 
4777     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4778 }
4779 
4780 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4781                                     uint64_t value)
4782 {
4783     CPUState *cs = env_cpu(env);
4784     int mask = vae1_tlbmask(env);
4785 
4786     if (tlb_force_broadcast(env)) {
4787         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4788     } else {
4789         tlb_flush_by_mmuidx(cs, mask);
4790     }
4791 }
4792 
4793 static int e2_tlbmask(CPUARMState *env)
4794 {
4795     return (ARMMMUIdxBit_E20_0 |
4796             ARMMMUIdxBit_E20_2 |
4797             ARMMMUIdxBit_E20_2_PAN |
4798             ARMMMUIdxBit_E2);
4799 }
4800 
4801 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4802                                   uint64_t value)
4803 {
4804     CPUState *cs = env_cpu(env);
4805     int mask = alle1_tlbmask(env);
4806 
4807     tlb_flush_by_mmuidx(cs, mask);
4808 }
4809 
4810 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4811                                   uint64_t value)
4812 {
4813     CPUState *cs = env_cpu(env);
4814     int mask = e2_tlbmask(env);
4815 
4816     tlb_flush_by_mmuidx(cs, mask);
4817 }
4818 
4819 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4820                                   uint64_t value)
4821 {
4822     ARMCPU *cpu = env_archcpu(env);
4823     CPUState *cs = CPU(cpu);
4824 
4825     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4826 }
4827 
4828 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4829                                     uint64_t value)
4830 {
4831     CPUState *cs = env_cpu(env);
4832     int mask = alle1_tlbmask(env);
4833 
4834     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4835 }
4836 
4837 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4838                                     uint64_t value)
4839 {
4840     CPUState *cs = env_cpu(env);
4841     int mask = e2_tlbmask(env);
4842 
4843     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4844 }
4845 
4846 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4847                                     uint64_t value)
4848 {
4849     CPUState *cs = env_cpu(env);
4850 
4851     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4852 }
4853 
4854 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4855                                  uint64_t value)
4856 {
4857     /*
4858      * Invalidate by VA, EL2
4859      * Currently handles both VAE2 and VALE2, since we don't support
4860      * flush-last-level-only.
4861      */
4862     CPUState *cs = env_cpu(env);
4863     int mask = vae2_tlbmask(env);
4864     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4865     int bits = vae2_tlbbits(env, pageaddr);
4866 
4867     tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4868 }
4869 
4870 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4871                                  uint64_t value)
4872 {
4873     /*
4874      * Invalidate by VA, EL3
4875      * Currently handles both VAE3 and VALE3, since we don't support
4876      * flush-last-level-only.
4877      */
4878     ARMCPU *cpu = env_archcpu(env);
4879     CPUState *cs = CPU(cpu);
4880     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4881 
4882     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4883 }
4884 
4885 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4886                                    uint64_t value)
4887 {
4888     CPUState *cs = env_cpu(env);
4889     int mask = vae1_tlbmask(env);
4890     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4891     int bits = vae1_tlbbits(env, pageaddr);
4892 
4893     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4894 }
4895 
4896 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4897                                  uint64_t value)
4898 {
4899     /*
4900      * Invalidate by VA, EL1&0 (AArch64 version).
4901      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4902      * since we don't support flush-for-specific-ASID-only or
4903      * flush-last-level-only.
4904      */
4905     CPUState *cs = env_cpu(env);
4906     int mask = vae1_tlbmask(env);
4907     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4908     int bits = vae1_tlbbits(env, pageaddr);
4909 
4910     if (tlb_force_broadcast(env)) {
4911         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4912     } else {
4913         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4914     }
4915 }
4916 
4917 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4918                                    uint64_t value)
4919 {
4920     CPUState *cs = env_cpu(env);
4921     int mask = vae2_tlbmask(env);
4922     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4923     int bits = vae2_tlbbits(env, pageaddr);
4924 
4925     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4926 }
4927 
4928 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4929                                    uint64_t value)
4930 {
4931     CPUState *cs = env_cpu(env);
4932     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4933     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
4934 
4935     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4936                                                   ARMMMUIdxBit_E3, bits);
4937 }
4938 
4939 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4940 {
4941     /*
4942      * The MSB of value is the NS field, which only applies if SEL2
4943      * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4944      */
4945     return (value >= 0
4946             && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4947             && arm_is_secure_below_el3(env)
4948             ? ARMMMUIdxBit_Stage2_S
4949             : ARMMMUIdxBit_Stage2);
4950 }
4951 
4952 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4953                                     uint64_t value)
4954 {
4955     CPUState *cs = env_cpu(env);
4956     int mask = ipas2e1_tlbmask(env, value);
4957     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4958 
4959     if (tlb_force_broadcast(env)) {
4960         tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4961     } else {
4962         tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4963     }
4964 }
4965 
4966 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4967                                       uint64_t value)
4968 {
4969     CPUState *cs = env_cpu(env);
4970     int mask = ipas2e1_tlbmask(env, value);
4971     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4972 
4973     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4974 }
4975 
4976 #ifdef TARGET_AARCH64
4977 typedef struct {
4978     uint64_t base;
4979     uint64_t length;
4980 } TLBIRange;
4981 
4982 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4983 {
4984     /*
4985      * Note that the TLBI range TG field encoding differs from both
4986      * TG0 and TG1 encodings.
4987      */
4988     switch (tg) {
4989     case 1:
4990         return Gran4K;
4991     case 2:
4992         return Gran16K;
4993     case 3:
4994         return Gran64K;
4995     default:
4996         return GranInvalid;
4997     }
4998 }
4999 
5000 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
5001                                      uint64_t value)
5002 {
5003     unsigned int page_size_granule, page_shift, num, scale, exponent;
5004     /* Extract one bit to represent the va selector in use. */
5005     uint64_t select = sextract64(value, 36, 1);
5006     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
5007     TLBIRange ret = { };
5008     ARMGranuleSize gran;
5009 
5010     page_size_granule = extract64(value, 46, 2);
5011     gran = tlbi_range_tg_to_gran_size(page_size_granule);
5012 
5013     /* The granule encoded in value must match the granule in use. */
5014     if (gran != param.gran) {
5015         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
5016                       page_size_granule);
5017         return ret;
5018     }
5019 
5020     page_shift = arm_granule_bits(gran);
5021     num = extract64(value, 39, 5);
5022     scale = extract64(value, 44, 2);
5023     exponent = (5 * scale) + 1;
5024 
5025     ret.length = (num + 1) << (exponent + page_shift);
5026 
5027     if (param.select) {
5028         ret.base = sextract64(value, 0, 37);
5029     } else {
5030         ret.base = extract64(value, 0, 37);
5031     }
5032     if (param.ds) {
5033         /*
5034          * With DS=1, BaseADDR is always shifted 16 so that it is able
5035          * to address all 52 va bits.  The input address is perforce
5036          * aligned on a 64k boundary regardless of translation granule.
5037          */
5038         page_shift = 16;
5039     }
5040     ret.base <<= page_shift;
5041 
5042     return ret;
5043 }
5044 
5045 static void do_rvae_write(CPUARMState *env, uint64_t value,
5046                           int idxmap, bool synced)
5047 {
5048     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
5049     TLBIRange range;
5050     int bits;
5051 
5052     range = tlbi_aa64_get_range(env, one_idx, value);
5053     bits = tlbbits_for_regime(env, one_idx, range.base);
5054 
5055     if (synced) {
5056         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
5057                                                   range.base,
5058                                                   range.length,
5059                                                   idxmap,
5060                                                   bits);
5061     } else {
5062         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5063                                   range.length, idxmap, bits);
5064     }
5065 }
5066 
5067 static void tlbi_aa64_rvae1_write(CPUARMState *env,
5068                                   const ARMCPRegInfo *ri,
5069                                   uint64_t value)
5070 {
5071     /*
5072      * Invalidate by VA range, EL1&0.
5073      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5074      * since we don't support flush-for-specific-ASID-only or
5075      * flush-last-level-only.
5076      */
5077 
5078     do_rvae_write(env, value, vae1_tlbmask(env),
5079                   tlb_force_broadcast(env));
5080 }
5081 
5082 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5083                                     const ARMCPRegInfo *ri,
5084                                     uint64_t value)
5085 {
5086     /*
5087      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5088      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5089      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5090      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5091      * shareable specific flushes.
5092      */
5093 
5094     do_rvae_write(env, value, vae1_tlbmask(env), true);
5095 }
5096 
5097 static void tlbi_aa64_rvae2_write(CPUARMState *env,
5098                                   const ARMCPRegInfo *ri,
5099                                   uint64_t value)
5100 {
5101     /*
5102      * Invalidate by VA range, EL2.
5103      * Currently handles all of RVAE2 and RVALE2,
5104      * since we don't support flush-for-specific-ASID-only or
5105      * flush-last-level-only.
5106      */
5107 
5108     do_rvae_write(env, value, vae2_tlbmask(env),
5109                   tlb_force_broadcast(env));
5110 
5111 
5112 }
5113 
5114 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5115                                     const ARMCPRegInfo *ri,
5116                                     uint64_t value)
5117 {
5118     /*
5119      * Invalidate by VA range, Inner/Outer Shareable, EL2.
5120      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5121      * since we don't support flush-for-specific-ASID-only,
5122      * flush-last-level-only or inner/outer shareable specific flushes.
5123      */
5124 
5125     do_rvae_write(env, value, vae2_tlbmask(env), true);
5126 
5127 }
5128 
5129 static void tlbi_aa64_rvae3_write(CPUARMState *env,
5130                                   const ARMCPRegInfo *ri,
5131                                   uint64_t value)
5132 {
5133     /*
5134      * Invalidate by VA range, EL3.
5135      * Currently handles all of RVAE3 and RVALE3,
5136      * since we don't support flush-for-specific-ASID-only or
5137      * flush-last-level-only.
5138      */
5139 
5140     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5141 }
5142 
5143 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5144                                     const ARMCPRegInfo *ri,
5145                                     uint64_t value)
5146 {
5147     /*
5148      * Invalidate by VA range, EL3, Inner/Outer Shareable.
5149      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5150      * since we don't support flush-for-specific-ASID-only,
5151      * flush-last-level-only or inner/outer specific flushes.
5152      */
5153 
5154     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5155 }
5156 
5157 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5158                                      uint64_t value)
5159 {
5160     do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5161                   tlb_force_broadcast(env));
5162 }
5163 
5164 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5165                                        const ARMCPRegInfo *ri,
5166                                        uint64_t value)
5167 {
5168     do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5169 }
5170 #endif
5171 
5172 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5173                                       bool isread)
5174 {
5175     int cur_el = arm_current_el(env);
5176 
5177     if (cur_el < 2) {
5178         uint64_t hcr = arm_hcr_el2_eff(env);
5179 
5180         if (cur_el == 0) {
5181             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5182                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5183                     return CP_ACCESS_TRAP_EL2;
5184                 }
5185             } else {
5186                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5187                     return CP_ACCESS_TRAP;
5188                 }
5189                 if (hcr & HCR_TDZ) {
5190                     return CP_ACCESS_TRAP_EL2;
5191                 }
5192             }
5193         } else if (hcr & HCR_TDZ) {
5194             return CP_ACCESS_TRAP_EL2;
5195         }
5196     }
5197     return CP_ACCESS_OK;
5198 }
5199 
5200 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5201 {
5202     ARMCPU *cpu = env_archcpu(env);
5203     int dzp_bit = 1 << 4;
5204 
5205     /* DZP indicates whether DC ZVA access is allowed */
5206     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5207         dzp_bit = 0;
5208     }
5209     return cpu->dcz_blocksize | dzp_bit;
5210 }
5211 
5212 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5213                                     bool isread)
5214 {
5215     if (!(env->pstate & PSTATE_SP)) {
5216         /*
5217          * Access to SP_EL0 is undefined if it's being used as
5218          * the stack pointer.
5219          */
5220         return CP_ACCESS_TRAP_UNCATEGORIZED;
5221     }
5222     return CP_ACCESS_OK;
5223 }
5224 
5225 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5226 {
5227     return env->pstate & PSTATE_SP;
5228 }
5229 
5230 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5231 {
5232     update_spsel(env, val);
5233 }
5234 
5235 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5236                         uint64_t value)
5237 {
5238     ARMCPU *cpu = env_archcpu(env);
5239 
5240     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5241         /* M bit is RAZ/WI for PMSA with no MPU implemented */
5242         value &= ~SCTLR_M;
5243     }
5244 
5245     /* ??? Lots of these bits are not implemented.  */
5246 
5247     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5248         if (ri->opc1 == 6) { /* SCTLR_EL3 */
5249             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5250         } else {
5251             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5252                        SCTLR_ATA0 | SCTLR_ATA);
5253         }
5254     }
5255 
5256     if (raw_read(env, ri) == value) {
5257         /*
5258          * Skip the TLB flush if nothing actually changed; Linux likes
5259          * to do a lot of pointless SCTLR writes.
5260          */
5261         return;
5262     }
5263 
5264     raw_write(env, ri, value);
5265 
5266     /* This may enable/disable the MMU, so do a TLB flush.  */
5267     tlb_flush(CPU(cpu));
5268 
5269     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
5270         /*
5271          * Normally we would always end the TB on an SCTLR write; see the
5272          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5273          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5274          * of hflags from the translator, so do it here.
5275          */
5276         arm_rebuild_hflags(env);
5277     }
5278 }
5279 
5280 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5281                            uint64_t value)
5282 {
5283     /*
5284      * Some MDCR_EL3 bits affect whether PMU counters are running:
5285      * if we are trying to change any of those then we must
5286      * bracket this update with PMU start/finish calls.
5287      */
5288     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5289 
5290     if (pmu_op) {
5291         pmu_op_start(env);
5292     }
5293     env->cp15.mdcr_el3 = value;
5294     if (pmu_op) {
5295         pmu_op_finish(env);
5296     }
5297 }
5298 
5299 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5300                        uint64_t value)
5301 {
5302     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5303     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5304 }
5305 
5306 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5307                            uint64_t value)
5308 {
5309     /*
5310      * Some MDCR_EL2 bits affect whether PMU counters are running:
5311      * if we are trying to change any of those then we must
5312      * bracket this update with PMU start/finish calls.
5313      */
5314     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5315 
5316     if (pmu_op) {
5317         pmu_op_start(env);
5318     }
5319     env->cp15.mdcr_el2 = value;
5320     if (pmu_op) {
5321         pmu_op_finish(env);
5322     }
5323 }
5324 
5325 #ifdef CONFIG_USER_ONLY
5326 /*
5327  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5328  * code to get around W^X restrictions, where one region is writable and the
5329  * other is executable.
5330  *
5331  * Since the executable region is never written to we cannot detect code
5332  * changes when running in user mode, and rely on the emulated JIT telling us
5333  * that the code has changed by executing this instruction.
5334  */
5335 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5336                           uint64_t value)
5337 {
5338     uint64_t icache_line_mask, start_address, end_address;
5339     const ARMCPU *cpu;
5340 
5341     cpu = env_archcpu(env);
5342 
5343     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5344     start_address = value & ~icache_line_mask;
5345     end_address = value | icache_line_mask;
5346 
5347     mmap_lock();
5348 
5349     tb_invalidate_phys_range(start_address, end_address);
5350 
5351     mmap_unlock();
5352 }
5353 #endif
5354 
5355 static const ARMCPRegInfo v8_cp_reginfo[] = {
5356     /*
5357      * Minimal set of EL0-visible registers. This will need to be expanded
5358      * significantly for system emulation of AArch64 CPUs.
5359      */
5360     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5361       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5362       .access = PL0_RW, .type = ARM_CP_NZCV },
5363     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5364       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5365       .type = ARM_CP_NO_RAW,
5366       .access = PL0_RW, .accessfn = aa64_daif_access,
5367       .fieldoffset = offsetof(CPUARMState, daif),
5368       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5369     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5370       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5371       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5372       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5373     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5374       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5375       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5376       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5377     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5378       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5379       .access = PL0_R, .type = ARM_CP_NO_RAW,
5380       .fgt = FGT_DCZID_EL0,
5381       .readfn = aa64_dczid_read },
5382     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5383       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5384       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5385 #ifndef CONFIG_USER_ONLY
5386       /* Avoid overhead of an access check that always passes in user-mode */
5387       .accessfn = aa64_zva_access,
5388       .fgt = FGT_DCZVA,
5389 #endif
5390     },
5391     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5392       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5393       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5394     /*
5395      * Instruction cache ops. All of these except `IC IVAU` NOP because we
5396      * don't emulate caches.
5397      */
5398     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5399       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5400       .access = PL1_W, .type = ARM_CP_NOP,
5401       .fgt = FGT_ICIALLUIS,
5402       .accessfn = access_ticab },
5403     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5404       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5405       .access = PL1_W, .type = ARM_CP_NOP,
5406       .fgt = FGT_ICIALLU,
5407       .accessfn = access_tocu },
5408     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5409       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5410       .access = PL0_W,
5411       .fgt = FGT_ICIVAU,
5412       .accessfn = access_tocu,
5413 #ifdef CONFIG_USER_ONLY
5414       .type = ARM_CP_NO_RAW,
5415       .writefn = ic_ivau_write
5416 #else
5417       .type = ARM_CP_NOP
5418 #endif
5419     },
5420     /* Cache ops: all NOPs since we don't emulate caches */
5421     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5422       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5423       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5424       .fgt = FGT_DCIVAC,
5425       .type = ARM_CP_NOP },
5426     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5427       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5428       .fgt = FGT_DCISW,
5429       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5430     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5431       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5432       .access = PL0_W, .type = ARM_CP_NOP,
5433       .fgt = FGT_DCCVAC,
5434       .accessfn = aa64_cacheop_poc_access },
5435     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5436       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5437       .fgt = FGT_DCCSW,
5438       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5439     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5440       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5441       .access = PL0_W, .type = ARM_CP_NOP,
5442       .fgt = FGT_DCCVAU,
5443       .accessfn = access_tocu },
5444     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5445       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5446       .access = PL0_W, .type = ARM_CP_NOP,
5447       .fgt = FGT_DCCIVAC,
5448       .accessfn = aa64_cacheop_poc_access },
5449     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5450       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5451       .fgt = FGT_DCCISW,
5452       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5453     /* TLBI operations */
5454     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5455       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5456       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5457       .fgt = FGT_TLBIVMALLE1IS,
5458       .writefn = tlbi_aa64_vmalle1is_write },
5459     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5460       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5461       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5462       .fgt = FGT_TLBIVAE1IS,
5463       .writefn = tlbi_aa64_vae1is_write },
5464     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5465       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5466       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5467       .fgt = FGT_TLBIASIDE1IS,
5468       .writefn = tlbi_aa64_vmalle1is_write },
5469     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5470       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5471       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5472       .fgt = FGT_TLBIVAAE1IS,
5473       .writefn = tlbi_aa64_vae1is_write },
5474     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5475       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5476       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5477       .fgt = FGT_TLBIVALE1IS,
5478       .writefn = tlbi_aa64_vae1is_write },
5479     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5480       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5481       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5482       .fgt = FGT_TLBIVAALE1IS,
5483       .writefn = tlbi_aa64_vae1is_write },
5484     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5485       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5486       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5487       .fgt = FGT_TLBIVMALLE1,
5488       .writefn = tlbi_aa64_vmalle1_write },
5489     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5490       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5491       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5492       .fgt = FGT_TLBIVAE1,
5493       .writefn = tlbi_aa64_vae1_write },
5494     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5495       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5496       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5497       .fgt = FGT_TLBIASIDE1,
5498       .writefn = tlbi_aa64_vmalle1_write },
5499     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5500       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5501       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5502       .fgt = FGT_TLBIVAAE1,
5503       .writefn = tlbi_aa64_vae1_write },
5504     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5505       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5506       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5507       .fgt = FGT_TLBIVALE1,
5508       .writefn = tlbi_aa64_vae1_write },
5509     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5510       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5511       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5512       .fgt = FGT_TLBIVAALE1,
5513       .writefn = tlbi_aa64_vae1_write },
5514     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5515       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5516       .access = PL2_W, .type = ARM_CP_NO_RAW,
5517       .writefn = tlbi_aa64_ipas2e1is_write },
5518     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5519       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5520       .access = PL2_W, .type = ARM_CP_NO_RAW,
5521       .writefn = tlbi_aa64_ipas2e1is_write },
5522     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5523       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5524       .access = PL2_W, .type = ARM_CP_NO_RAW,
5525       .writefn = tlbi_aa64_alle1is_write },
5526     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5527       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5528       .access = PL2_W, .type = ARM_CP_NO_RAW,
5529       .writefn = tlbi_aa64_alle1is_write },
5530     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5531       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5532       .access = PL2_W, .type = ARM_CP_NO_RAW,
5533       .writefn = tlbi_aa64_ipas2e1_write },
5534     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5535       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5536       .access = PL2_W, .type = ARM_CP_NO_RAW,
5537       .writefn = tlbi_aa64_ipas2e1_write },
5538     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5539       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5540       .access = PL2_W, .type = ARM_CP_NO_RAW,
5541       .writefn = tlbi_aa64_alle1_write },
5542     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5543       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5544       .access = PL2_W, .type = ARM_CP_NO_RAW,
5545       .writefn = tlbi_aa64_alle1is_write },
5546 #ifndef CONFIG_USER_ONLY
5547     /* 64 bit address translation operations */
5548     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5549       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5550       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5551       .fgt = FGT_ATS1E1R,
5552       .accessfn = at_e012_access, .writefn = ats_write64 },
5553     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5554       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5555       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5556       .fgt = FGT_ATS1E1W,
5557       .accessfn = at_e012_access, .writefn = ats_write64 },
5558     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5559       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5560       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5561       .fgt = FGT_ATS1E0R,
5562       .accessfn = at_e012_access, .writefn = ats_write64 },
5563     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5564       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5565       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5566       .fgt = FGT_ATS1E0W,
5567       .accessfn = at_e012_access, .writefn = ats_write64 },
5568     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5569       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5570       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5571       .accessfn = at_e012_access, .writefn = ats_write64 },
5572     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5573       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5574       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5575       .accessfn = at_e012_access, .writefn = ats_write64 },
5576     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5577       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5578       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5579       .accessfn = at_e012_access, .writefn = ats_write64 },
5580     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5581       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5582       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5583       .accessfn = at_e012_access, .writefn = ats_write64 },
5584     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5585     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5586       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5587       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5588       .writefn = ats_write64 },
5589     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5590       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5591       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5592       .writefn = ats_write64 },
5593     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5594       .type = ARM_CP_ALIAS,
5595       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5596       .access = PL1_RW, .resetvalue = 0,
5597       .fgt = FGT_PAR_EL1,
5598       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5599       .writefn = par_write },
5600 #endif
5601     /* TLB invalidate last level of translation table walk */
5602     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5603       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5604       .writefn = tlbimva_is_write },
5605     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5606       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5607       .writefn = tlbimvaa_is_write },
5608     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5609       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5610       .writefn = tlbimva_write },
5611     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5612       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5613       .writefn = tlbimvaa_write },
5614     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5615       .type = ARM_CP_NO_RAW, .access = PL2_W,
5616       .writefn = tlbimva_hyp_write },
5617     { .name = "TLBIMVALHIS",
5618       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5619       .type = ARM_CP_NO_RAW, .access = PL2_W,
5620       .writefn = tlbimva_hyp_is_write },
5621     { .name = "TLBIIPAS2",
5622       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5623       .type = ARM_CP_NO_RAW, .access = PL2_W,
5624       .writefn = tlbiipas2_hyp_write },
5625     { .name = "TLBIIPAS2IS",
5626       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5627       .type = ARM_CP_NO_RAW, .access = PL2_W,
5628       .writefn = tlbiipas2is_hyp_write },
5629     { .name = "TLBIIPAS2L",
5630       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5631       .type = ARM_CP_NO_RAW, .access = PL2_W,
5632       .writefn = tlbiipas2_hyp_write },
5633     { .name = "TLBIIPAS2LIS",
5634       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5635       .type = ARM_CP_NO_RAW, .access = PL2_W,
5636       .writefn = tlbiipas2is_hyp_write },
5637     /* 32 bit cache operations */
5638     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5639       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5640     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5641       .type = ARM_CP_NOP, .access = PL1_W },
5642     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5643       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5644     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5645       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5646     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5647       .type = ARM_CP_NOP, .access = PL1_W },
5648     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5649       .type = ARM_CP_NOP, .access = PL1_W },
5650     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5651       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5652     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5653       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5654     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5655       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5656     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5657       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5658     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5659       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5660     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5661       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5662     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5663       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5664     /* MMU Domain access control / MPU write buffer control */
5665     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5666       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5667       .writefn = dacr_write, .raw_writefn = raw_write,
5668       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5669                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5670     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5671       .type = ARM_CP_ALIAS,
5672       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5673       .access = PL1_RW,
5674       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5675     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5676       .type = ARM_CP_ALIAS,
5677       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5678       .access = PL1_RW,
5679       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5680     /*
5681      * We rely on the access checks not allowing the guest to write to the
5682      * state field when SPSel indicates that it's being used as the stack
5683      * pointer.
5684      */
5685     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5686       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5687       .access = PL1_RW, .accessfn = sp_el0_access,
5688       .type = ARM_CP_ALIAS,
5689       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5690     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5691       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5692       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5693       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5694     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5695       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5696       .type = ARM_CP_NO_RAW,
5697       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5698     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5699       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5700       .access = PL2_RW,
5701       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5702       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5703     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5704       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5705       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5706       .writefn = dacr_write, .raw_writefn = raw_write,
5707       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5708     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5709       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5710       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5711       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5712     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5713       .type = ARM_CP_ALIAS,
5714       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5715       .access = PL2_RW,
5716       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5717     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5718       .type = ARM_CP_ALIAS,
5719       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5720       .access = PL2_RW,
5721       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5722     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5723       .type = ARM_CP_ALIAS,
5724       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5725       .access = PL2_RW,
5726       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5727     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5728       .type = ARM_CP_ALIAS,
5729       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5730       .access = PL2_RW,
5731       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5732     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5733       .type = ARM_CP_IO,
5734       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5735       .resetvalue = 0,
5736       .access = PL3_RW,
5737       .writefn = mdcr_el3_write,
5738       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5739     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5740       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5741       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5742       .writefn = sdcr_write,
5743       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5744 };
5745 
5746 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5747 {
5748     ARMCPU *cpu = env_archcpu(env);
5749 
5750     if (arm_feature(env, ARM_FEATURE_V8)) {
5751         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5752     } else {
5753         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5754     }
5755 
5756     if (arm_feature(env, ARM_FEATURE_EL3)) {
5757         valid_mask &= ~HCR_HCD;
5758     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5759         /*
5760          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5761          * However, if we're using the SMC PSCI conduit then QEMU is
5762          * effectively acting like EL3 firmware and so the guest at
5763          * EL2 should retain the ability to prevent EL1 from being
5764          * able to make SMC calls into the ersatz firmware, so in
5765          * that case HCR.TSC should be read/write.
5766          */
5767         valid_mask &= ~HCR_TSC;
5768     }
5769 
5770     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5771         if (cpu_isar_feature(aa64_vh, cpu)) {
5772             valid_mask |= HCR_E2H;
5773         }
5774         if (cpu_isar_feature(aa64_ras, cpu)) {
5775             valid_mask |= HCR_TERR | HCR_TEA;
5776         }
5777         if (cpu_isar_feature(aa64_lor, cpu)) {
5778             valid_mask |= HCR_TLOR;
5779         }
5780         if (cpu_isar_feature(aa64_pauth, cpu)) {
5781             valid_mask |= HCR_API | HCR_APK;
5782         }
5783         if (cpu_isar_feature(aa64_mte, cpu)) {
5784             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5785         }
5786         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5787             valid_mask |= HCR_ENSCXT;
5788         }
5789         if (cpu_isar_feature(aa64_fwb, cpu)) {
5790             valid_mask |= HCR_FWB;
5791         }
5792         if (cpu_isar_feature(aa64_rme, cpu)) {
5793             valid_mask |= HCR_GPF;
5794         }
5795     }
5796 
5797     if (cpu_isar_feature(any_evt, cpu)) {
5798         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5799     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5800         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5801     }
5802 
5803     /* Clear RES0 bits.  */
5804     value &= valid_mask;
5805 
5806     /*
5807      * These bits change the MMU setup:
5808      * HCR_VM enables stage 2 translation
5809      * HCR_PTW forbids certain page-table setups
5810      * HCR_DC disables stage1 and enables stage2 translation
5811      * HCR_DCT enables tagging on (disabled) stage1 translation
5812      * HCR_FWB changes the interpretation of stage2 descriptor bits
5813      */
5814     if ((env->cp15.hcr_el2 ^ value) &
5815         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5816         tlb_flush(CPU(cpu));
5817     }
5818     env->cp15.hcr_el2 = value;
5819 
5820     /*
5821      * Updates to VI and VF require us to update the status of
5822      * virtual interrupts, which are the logical OR of these bits
5823      * and the state of the input lines from the GIC. (This requires
5824      * that we have the iothread lock, which is done by marking the
5825      * reginfo structs as ARM_CP_IO.)
5826      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5827      * possible for it to be taken immediately, because VIRQ and
5828      * VFIQ are masked unless running at EL0 or EL1, and HCR
5829      * can only be written at EL2.
5830      */
5831     g_assert(qemu_mutex_iothread_locked());
5832     arm_cpu_update_virq(cpu);
5833     arm_cpu_update_vfiq(cpu);
5834     arm_cpu_update_vserr(cpu);
5835 }
5836 
5837 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5838 {
5839     do_hcr_write(env, value, 0);
5840 }
5841 
5842 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5843                           uint64_t value)
5844 {
5845     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5846     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5847     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5848 }
5849 
5850 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5851                          uint64_t value)
5852 {
5853     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5854     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5855     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5856 }
5857 
5858 /*
5859  * Return the effective value of HCR_EL2, at the given security state.
5860  * Bits that are not included here:
5861  * RW       (read from SCR_EL3.RW as needed)
5862  */
5863 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5864 {
5865     uint64_t ret = env->cp15.hcr_el2;
5866 
5867     assert(space != ARMSS_Root);
5868 
5869     if (!arm_is_el2_enabled_secstate(env, space)) {
5870         /*
5871          * "This register has no effect if EL2 is not enabled in the
5872          * current Security state".  This is ARMv8.4-SecEL2 speak for
5873          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5874          *
5875          * Prior to that, the language was "In an implementation that
5876          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5877          * as if this field is 0 for all purposes other than a direct
5878          * read or write access of HCR_EL2".  With lots of enumeration
5879          * on a per-field basis.  In current QEMU, this is condition
5880          * is arm_is_secure_below_el3.
5881          *
5882          * Since the v8.4 language applies to the entire register, and
5883          * appears to be backward compatible, use that.
5884          */
5885         return 0;
5886     }
5887 
5888     /*
5889      * For a cpu that supports both aarch64 and aarch32, we can set bits
5890      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5891      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5892      */
5893     if (!arm_el_is_aa64(env, 2)) {
5894         uint64_t aa32_valid;
5895 
5896         /*
5897          * These bits are up-to-date as of ARMv8.6.
5898          * For HCR, it's easiest to list just the 2 bits that are invalid.
5899          * For HCR2, list those that are valid.
5900          */
5901         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5902         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5903                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5904         ret &= aa32_valid;
5905     }
5906 
5907     if (ret & HCR_TGE) {
5908         /* These bits are up-to-date as of ARMv8.6.  */
5909         if (ret & HCR_E2H) {
5910             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5911                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5912                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5913                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5914                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5915                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5916         } else {
5917             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5918         }
5919         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5920                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5921                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5922                  HCR_TLOR);
5923     }
5924 
5925     return ret;
5926 }
5927 
5928 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5929 {
5930     if (arm_feature(env, ARM_FEATURE_M)) {
5931         return 0;
5932     }
5933     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
5934 }
5935 
5936 /*
5937  * Corresponds to ARM pseudocode function ELIsInHost().
5938  */
5939 bool el_is_in_host(CPUARMState *env, int el)
5940 {
5941     uint64_t mask;
5942 
5943     /*
5944      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5945      * Perform the simplest bit tests first, and validate EL2 afterward.
5946      */
5947     if (el & 1) {
5948         return false; /* EL1 or EL3 */
5949     }
5950 
5951     /*
5952      * Note that hcr_write() checks isar_feature_aa64_vh(),
5953      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5954      */
5955     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5956     if ((env->cp15.hcr_el2 & mask) != mask) {
5957         return false;
5958     }
5959 
5960     /* TGE and/or E2H set: double check those bits are currently legal. */
5961     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5962 }
5963 
5964 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5965                        uint64_t value)
5966 {
5967     uint64_t valid_mask = 0;
5968 
5969     /* FEAT_MOPS adds MSCEn and MCE2 */
5970     if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5971         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
5972     }
5973 
5974     /* Clear RES0 bits.  */
5975     env->cp15.hcrx_el2 = value & valid_mask;
5976 }
5977 
5978 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5979                                   bool isread)
5980 {
5981     if (arm_current_el(env) < 3
5982         && arm_feature(env, ARM_FEATURE_EL3)
5983         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5984         return CP_ACCESS_TRAP_EL3;
5985     }
5986     return CP_ACCESS_OK;
5987 }
5988 
5989 static const ARMCPRegInfo hcrx_el2_reginfo = {
5990     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5991     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5992     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5993     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5994 };
5995 
5996 /* Return the effective value of HCRX_EL2.  */
5997 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5998 {
5999     /*
6000      * The bits in this register behave as 0 for all purposes other than
6001      * direct reads of the register if SCR_EL3.HXEn is 0.
6002      * If EL2 is not enabled in the current security state, then the
6003      * bit may behave as if 0, or as if 1, depending on the bit.
6004      * For the moment, we treat the EL2-disabled case as taking
6005      * priority over the HXEn-disabled case. This is true for the only
6006      * bit for a feature which we implement where the answer is different
6007      * for the two cases (MSCEn for FEAT_MOPS).
6008      * This may need to be revisited for future bits.
6009      */
6010     if (!arm_is_el2_enabled(env)) {
6011         uint64_t hcrx = 0;
6012         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6013             /* MSCEn behaves as 1 if EL2 is not enabled */
6014             hcrx |= HCRX_MSCEN;
6015         }
6016         return hcrx;
6017     }
6018     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
6019         return 0;
6020     }
6021     return env->cp15.hcrx_el2;
6022 }
6023 
6024 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6025                            uint64_t value)
6026 {
6027     /*
6028      * For A-profile AArch32 EL3, if NSACR.CP10
6029      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6030      */
6031     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6032         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6033         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6034         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
6035     }
6036     env->cp15.cptr_el[2] = value;
6037 }
6038 
6039 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6040 {
6041     /*
6042      * For A-profile AArch32 EL3, if NSACR.CP10
6043      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6044      */
6045     uint64_t value = env->cp15.cptr_el[2];
6046 
6047     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6048         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6049         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6050     }
6051     return value;
6052 }
6053 
6054 static const ARMCPRegInfo el2_cp_reginfo[] = {
6055     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
6056       .type = ARM_CP_IO,
6057       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6058       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6059       .writefn = hcr_write, .raw_writefn = raw_write },
6060     { .name = "HCR", .state = ARM_CP_STATE_AA32,
6061       .type = ARM_CP_ALIAS | ARM_CP_IO,
6062       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6063       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6064       .writefn = hcr_writelow },
6065     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6066       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6067       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6068     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
6069       .type = ARM_CP_ALIAS,
6070       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
6071       .access = PL2_RW,
6072       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
6073     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
6074       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6075       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
6076     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
6077       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6078       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
6079     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6080       .type = ARM_CP_ALIAS,
6081       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6082       .access = PL2_RW,
6083       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
6084     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
6085       .type = ARM_CP_ALIAS,
6086       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
6087       .access = PL2_RW,
6088       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
6089     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
6090       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6091       .access = PL2_RW, .writefn = vbar_write,
6092       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6093       .resetvalue = 0 },
6094     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6095       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
6096       .access = PL3_RW, .type = ARM_CP_ALIAS,
6097       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
6098     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6099       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6100       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
6101       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6102       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
6103     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6104       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6105       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6106       .resetvalue = 0 },
6107     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
6108       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
6109       .access = PL2_RW, .type = ARM_CP_ALIAS,
6110       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
6111     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6112       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6113       .access = PL2_RW, .type = ARM_CP_CONST,
6114       .resetvalue = 0 },
6115     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
6116     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
6117       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
6118       .access = PL2_RW, .type = ARM_CP_CONST,
6119       .resetvalue = 0 },
6120     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6121       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6122       .access = PL2_RW, .type = ARM_CP_CONST,
6123       .resetvalue = 0 },
6124     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6125       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6126       .access = PL2_RW, .type = ARM_CP_CONST,
6127       .resetvalue = 0 },
6128     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6129       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6130       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
6131       .raw_writefn = raw_write,
6132       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
6133     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6134       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6135       .type = ARM_CP_ALIAS,
6136       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6137       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
6138     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6139       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6140       .access = PL2_RW,
6141       /* no .writefn needed as this can't cause an ASID change */
6142       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
6143     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6144       .cp = 15, .opc1 = 6, .crm = 2,
6145       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6146       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6147       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
6148       .writefn = vttbr_write, .raw_writefn = raw_write },
6149     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6150       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
6151       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
6152       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
6153     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6154       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6155       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6156       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
6157     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6158       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6159       .access = PL2_RW, .resetvalue = 0,
6160       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
6161     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6162       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
6163       .access = PL2_RW, .resetvalue = 0,
6164       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
6165       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6166     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6167       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6168       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6169     { .name = "TLBIALLNSNH",
6170       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6171       .type = ARM_CP_NO_RAW, .access = PL2_W,
6172       .writefn = tlbiall_nsnh_write },
6173     { .name = "TLBIALLNSNHIS",
6174       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6175       .type = ARM_CP_NO_RAW, .access = PL2_W,
6176       .writefn = tlbiall_nsnh_is_write },
6177     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6178       .type = ARM_CP_NO_RAW, .access = PL2_W,
6179       .writefn = tlbiall_hyp_write },
6180     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6181       .type = ARM_CP_NO_RAW, .access = PL2_W,
6182       .writefn = tlbiall_hyp_is_write },
6183     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6184       .type = ARM_CP_NO_RAW, .access = PL2_W,
6185       .writefn = tlbimva_hyp_write },
6186     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6187       .type = ARM_CP_NO_RAW, .access = PL2_W,
6188       .writefn = tlbimva_hyp_is_write },
6189     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6190       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6191       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6192       .writefn = tlbi_aa64_alle2_write },
6193     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6194       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6195       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6196       .writefn = tlbi_aa64_vae2_write },
6197     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6198       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6199       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6200       .writefn = tlbi_aa64_vae2_write },
6201     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6202       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6203       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6204       .writefn = tlbi_aa64_alle2is_write },
6205     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6206       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6207       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6208       .writefn = tlbi_aa64_vae2is_write },
6209     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6210       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6211       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6212       .writefn = tlbi_aa64_vae2is_write },
6213 #ifndef CONFIG_USER_ONLY
6214     /*
6215      * Unlike the other EL2-related AT operations, these must
6216      * UNDEF from EL3 if EL2 is not implemented, which is why we
6217      * define them here rather than with the rest of the AT ops.
6218      */
6219     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6220       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6221       .access = PL2_W, .accessfn = at_s1e2_access,
6222       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6223       .writefn = ats_write64 },
6224     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6225       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6226       .access = PL2_W, .accessfn = at_s1e2_access,
6227       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6228       .writefn = ats_write64 },
6229     /*
6230      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
6231      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6232      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6233      * to behave as if SCR.NS was 1.
6234      */
6235     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6236       .access = PL2_W,
6237       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6238     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6239       .access = PL2_W,
6240       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6241     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6242       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6243       /*
6244        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6245        * reset values as IMPDEF. We choose to reset to 3 to comply with
6246        * both ARMv7 and ARMv8.
6247        */
6248       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6249       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
6250       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6251     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6252       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6253       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6254       .writefn = gt_cntvoff_write,
6255       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6256     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6257       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6258       .writefn = gt_cntvoff_write,
6259       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6260     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6261       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6262       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6263       .type = ARM_CP_IO, .access = PL2_RW,
6264       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6265     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6266       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6267       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6268       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6269     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6270       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6271       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6272       .resetfn = gt_hyp_timer_reset,
6273       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6274     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6275       .type = ARM_CP_IO,
6276       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6277       .access = PL2_RW,
6278       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6279       .resetvalue = 0,
6280       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6281 #endif
6282     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6283       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6284       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6285       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6286     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6287       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6288       .access = PL2_RW,
6289       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6290     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6291       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6292       .access = PL2_RW,
6293       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6294 };
6295 
6296 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6297     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6298       .type = ARM_CP_ALIAS | ARM_CP_IO,
6299       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6300       .access = PL2_RW,
6301       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6302       .writefn = hcr_writehigh },
6303 };
6304 
6305 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6306                                   bool isread)
6307 {
6308     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6309         return CP_ACCESS_OK;
6310     }
6311     return CP_ACCESS_TRAP_UNCATEGORIZED;
6312 }
6313 
6314 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6315     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6316       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6317       .access = PL2_RW, .accessfn = sel2_access,
6318       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6319     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6320       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6321       .access = PL2_RW, .accessfn = sel2_access,
6322       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6323 };
6324 
6325 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6326                                    bool isread)
6327 {
6328     /*
6329      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6330      * At Secure EL1 it traps to EL3 or EL2.
6331      */
6332     if (arm_current_el(env) == 3) {
6333         return CP_ACCESS_OK;
6334     }
6335     if (arm_is_secure_below_el3(env)) {
6336         if (env->cp15.scr_el3 & SCR_EEL2) {
6337             return CP_ACCESS_TRAP_EL2;
6338         }
6339         return CP_ACCESS_TRAP_EL3;
6340     }
6341     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6342     if (isread) {
6343         return CP_ACCESS_OK;
6344     }
6345     return CP_ACCESS_TRAP_UNCATEGORIZED;
6346 }
6347 
6348 static const ARMCPRegInfo el3_cp_reginfo[] = {
6349     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6350       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6351       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6352       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
6353     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6354       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6355       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6356       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6357       .writefn = scr_write, .raw_writefn = raw_write },
6358     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6359       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6360       .access = PL3_RW, .resetvalue = 0,
6361       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6362     { .name = "SDER",
6363       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6364       .access = PL3_RW, .resetvalue = 0,
6365       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6366     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6367       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6368       .writefn = vbar_write, .resetvalue = 0,
6369       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6370     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6371       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6372       .access = PL3_RW, .resetvalue = 0,
6373       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6374     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6375       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6376       .access = PL3_RW,
6377       /* no .writefn needed as this can't cause an ASID change */
6378       .resetvalue = 0,
6379       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6380     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6381       .type = ARM_CP_ALIAS,
6382       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6383       .access = PL3_RW,
6384       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6385     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6386       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6387       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6388     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6389       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6390       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6391     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6392       .type = ARM_CP_ALIAS,
6393       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6394       .access = PL3_RW,
6395       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6396     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6397       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6398       .access = PL3_RW, .writefn = vbar_write,
6399       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6400       .resetvalue = 0 },
6401     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6402       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6403       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6404       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6405     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6406       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6407       .access = PL3_RW, .resetvalue = 0,
6408       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6409     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6410       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6411       .access = PL3_RW, .type = ARM_CP_CONST,
6412       .resetvalue = 0 },
6413     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6414       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6415       .access = PL3_RW, .type = ARM_CP_CONST,
6416       .resetvalue = 0 },
6417     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6418       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6419       .access = PL3_RW, .type = ARM_CP_CONST,
6420       .resetvalue = 0 },
6421     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6422       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6423       .access = PL3_W, .type = ARM_CP_NO_RAW,
6424       .writefn = tlbi_aa64_alle3is_write },
6425     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6426       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6427       .access = PL3_W, .type = ARM_CP_NO_RAW,
6428       .writefn = tlbi_aa64_vae3is_write },
6429     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6430       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6431       .access = PL3_W, .type = ARM_CP_NO_RAW,
6432       .writefn = tlbi_aa64_vae3is_write },
6433     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6434       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6435       .access = PL3_W, .type = ARM_CP_NO_RAW,
6436       .writefn = tlbi_aa64_alle3_write },
6437     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6438       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6439       .access = PL3_W, .type = ARM_CP_NO_RAW,
6440       .writefn = tlbi_aa64_vae3_write },
6441     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6442       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6443       .access = PL3_W, .type = ARM_CP_NO_RAW,
6444       .writefn = tlbi_aa64_vae3_write },
6445 };
6446 
6447 #ifndef CONFIG_USER_ONLY
6448 /* Test if system register redirection is to occur in the current state.  */
6449 static bool redirect_for_e2h(CPUARMState *env)
6450 {
6451     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6452 }
6453 
6454 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6455 {
6456     CPReadFn *readfn;
6457 
6458     if (redirect_for_e2h(env)) {
6459         /* Switch to the saved EL2 version of the register.  */
6460         ri = ri->opaque;
6461         readfn = ri->readfn;
6462     } else {
6463         readfn = ri->orig_readfn;
6464     }
6465     if (readfn == NULL) {
6466         readfn = raw_read;
6467     }
6468     return readfn(env, ri);
6469 }
6470 
6471 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6472                           uint64_t value)
6473 {
6474     CPWriteFn *writefn;
6475 
6476     if (redirect_for_e2h(env)) {
6477         /* Switch to the saved EL2 version of the register.  */
6478         ri = ri->opaque;
6479         writefn = ri->writefn;
6480     } else {
6481         writefn = ri->orig_writefn;
6482     }
6483     if (writefn == NULL) {
6484         writefn = raw_write;
6485     }
6486     writefn(env, ri, value);
6487 }
6488 
6489 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6490 {
6491     struct E2HAlias {
6492         uint32_t src_key, dst_key, new_key;
6493         const char *src_name, *dst_name, *new_name;
6494         bool (*feature)(const ARMISARegisters *id);
6495     };
6496 
6497 #define K(op0, op1, crn, crm, op2) \
6498     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6499 
6500     static const struct E2HAlias aliases[] = {
6501         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6502           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6503         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6504           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6505         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6506           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6507         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6508           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6509         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6510           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6511         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6512           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6513         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6514           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6515         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6516           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6517         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6518           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6519         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6520           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6521         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6522           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6523         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6524           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6525         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6526           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6527         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6528           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6529         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6530           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6531         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6532           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6533 
6534         /*
6535          * Note that redirection of ZCR is mentioned in the description
6536          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6537          * not in the summary table.
6538          */
6539         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6540           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6541         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6542           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6543 
6544         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6545           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6546 
6547         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6548           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6549           isar_feature_aa64_scxtnum },
6550 
6551         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6552         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6553     };
6554 #undef K
6555 
6556     size_t i;
6557 
6558     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6559         const struct E2HAlias *a = &aliases[i];
6560         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6561         bool ok;
6562 
6563         if (a->feature && !a->feature(&cpu->isar)) {
6564             continue;
6565         }
6566 
6567         src_reg = g_hash_table_lookup(cpu->cp_regs,
6568                                       (gpointer)(uintptr_t)a->src_key);
6569         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6570                                       (gpointer)(uintptr_t)a->dst_key);
6571         g_assert(src_reg != NULL);
6572         g_assert(dst_reg != NULL);
6573 
6574         /* Cross-compare names to detect typos in the keys.  */
6575         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6576         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6577 
6578         /* None of the core system registers use opaque; we will.  */
6579         g_assert(src_reg->opaque == NULL);
6580 
6581         /* Create alias before redirection so we dup the right data. */
6582         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6583 
6584         new_reg->name = a->new_name;
6585         new_reg->type |= ARM_CP_ALIAS;
6586         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6587         new_reg->access &= PL2_RW | PL3_RW;
6588 
6589         ok = g_hash_table_insert(cpu->cp_regs,
6590                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6591         g_assert(ok);
6592 
6593         src_reg->opaque = dst_reg;
6594         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6595         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6596         if (!src_reg->raw_readfn) {
6597             src_reg->raw_readfn = raw_read;
6598         }
6599         if (!src_reg->raw_writefn) {
6600             src_reg->raw_writefn = raw_write;
6601         }
6602         src_reg->readfn = el2_e2h_read;
6603         src_reg->writefn = el2_e2h_write;
6604     }
6605 }
6606 #endif
6607 
6608 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6609                                      bool isread)
6610 {
6611     int cur_el = arm_current_el(env);
6612 
6613     if (cur_el < 2) {
6614         uint64_t hcr = arm_hcr_el2_eff(env);
6615 
6616         if (cur_el == 0) {
6617             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6618                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6619                     return CP_ACCESS_TRAP_EL2;
6620                 }
6621             } else {
6622                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6623                     return CP_ACCESS_TRAP;
6624                 }
6625                 if (hcr & HCR_TID2) {
6626                     return CP_ACCESS_TRAP_EL2;
6627                 }
6628             }
6629         } else if (hcr & HCR_TID2) {
6630             return CP_ACCESS_TRAP_EL2;
6631         }
6632     }
6633 
6634     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6635         return CP_ACCESS_TRAP_EL2;
6636     }
6637 
6638     return CP_ACCESS_OK;
6639 }
6640 
6641 /*
6642  * Check for traps to RAS registers, which are controlled
6643  * by HCR_EL2.TERR and SCR_EL3.TERR.
6644  */
6645 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6646                                   bool isread)
6647 {
6648     int el = arm_current_el(env);
6649 
6650     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6651         return CP_ACCESS_TRAP_EL2;
6652     }
6653     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6654         return CP_ACCESS_TRAP_EL3;
6655     }
6656     return CP_ACCESS_OK;
6657 }
6658 
6659 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6660 {
6661     int el = arm_current_el(env);
6662 
6663     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6664         return env->cp15.vdisr_el2;
6665     }
6666     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6667         return 0; /* RAZ/WI */
6668     }
6669     return env->cp15.disr_el1;
6670 }
6671 
6672 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6673 {
6674     int el = arm_current_el(env);
6675 
6676     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6677         env->cp15.vdisr_el2 = val;
6678         return;
6679     }
6680     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6681         return; /* RAZ/WI */
6682     }
6683     env->cp15.disr_el1 = val;
6684 }
6685 
6686 /*
6687  * Minimal RAS implementation with no Error Records.
6688  * Which means that all of the Error Record registers:
6689  *   ERXADDR_EL1
6690  *   ERXCTLR_EL1
6691  *   ERXFR_EL1
6692  *   ERXMISC0_EL1
6693  *   ERXMISC1_EL1
6694  *   ERXMISC2_EL1
6695  *   ERXMISC3_EL1
6696  *   ERXPFGCDN_EL1  (RASv1p1)
6697  *   ERXPFGCTL_EL1  (RASv1p1)
6698  *   ERXPFGF_EL1    (RASv1p1)
6699  *   ERXSTATUS_EL1
6700  * and
6701  *   ERRSELR_EL1
6702  * may generate UNDEFINED, which is the effect we get by not
6703  * listing them at all.
6704  *
6705  * These registers have fine-grained trap bits, but UNDEF-to-EL1
6706  * is higher priority than FGT-to-EL2 so we do not need to list them
6707  * in order to check for an FGT.
6708  */
6709 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6710     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6711       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6712       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6713       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6714     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6715       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6716       .access = PL1_R, .accessfn = access_terr,
6717       .fgt = FGT_ERRIDR_EL1,
6718       .type = ARM_CP_CONST, .resetvalue = 0 },
6719     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6720       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6721       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6722     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6723       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6724       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6725 };
6726 
6727 /*
6728  * Return the exception level to which exceptions should be taken
6729  * via SVEAccessTrap.  This excludes the check for whether the exception
6730  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6731  * be found by testing 0 < fp_exception_el < sve_exception_el.
6732  *
6733  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6734  * pseudocode does *not* separate out the FP trap checks, but has them
6735  * all in one function.
6736  */
6737 int sve_exception_el(CPUARMState *env, int el)
6738 {
6739 #ifndef CONFIG_USER_ONLY
6740     if (el <= 1 && !el_is_in_host(env, el)) {
6741         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6742         case 1:
6743             if (el != 0) {
6744                 break;
6745             }
6746             /* fall through */
6747         case 0:
6748         case 2:
6749             return 1;
6750         }
6751     }
6752 
6753     if (el <= 2 && arm_is_el2_enabled(env)) {
6754         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6755         if (env->cp15.hcr_el2 & HCR_E2H) {
6756             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6757             case 1:
6758                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6759                     break;
6760                 }
6761                 /* fall through */
6762             case 0:
6763             case 2:
6764                 return 2;
6765             }
6766         } else {
6767             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6768                 return 2;
6769             }
6770         }
6771     }
6772 
6773     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6774     if (arm_feature(env, ARM_FEATURE_EL3)
6775         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6776         return 3;
6777     }
6778 #endif
6779     return 0;
6780 }
6781 
6782 /*
6783  * Return the exception level to which exceptions should be taken for SME.
6784  * C.f. the ARM pseudocode function CheckSMEAccess.
6785  */
6786 int sme_exception_el(CPUARMState *env, int el)
6787 {
6788 #ifndef CONFIG_USER_ONLY
6789     if (el <= 1 && !el_is_in_host(env, el)) {
6790         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6791         case 1:
6792             if (el != 0) {
6793                 break;
6794             }
6795             /* fall through */
6796         case 0:
6797         case 2:
6798             return 1;
6799         }
6800     }
6801 
6802     if (el <= 2 && arm_is_el2_enabled(env)) {
6803         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6804         if (env->cp15.hcr_el2 & HCR_E2H) {
6805             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6806             case 1:
6807                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6808                     break;
6809                 }
6810                 /* fall through */
6811             case 0:
6812             case 2:
6813                 return 2;
6814             }
6815         } else {
6816             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6817                 return 2;
6818             }
6819         }
6820     }
6821 
6822     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6823     if (arm_feature(env, ARM_FEATURE_EL3)
6824         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6825         return 3;
6826     }
6827 #endif
6828     return 0;
6829 }
6830 
6831 /*
6832  * Given that SVE is enabled, return the vector length for EL.
6833  */
6834 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6835 {
6836     ARMCPU *cpu = env_archcpu(env);
6837     uint64_t *cr = env->vfp.zcr_el;
6838     uint32_t map = cpu->sve_vq.map;
6839     uint32_t len = ARM_MAX_VQ - 1;
6840 
6841     if (sm) {
6842         cr = env->vfp.smcr_el;
6843         map = cpu->sme_vq.map;
6844     }
6845 
6846     if (el <= 1 && !el_is_in_host(env, el)) {
6847         len = MIN(len, 0xf & (uint32_t)cr[1]);
6848     }
6849     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6850         len = MIN(len, 0xf & (uint32_t)cr[2]);
6851     }
6852     if (arm_feature(env, ARM_FEATURE_EL3)) {
6853         len = MIN(len, 0xf & (uint32_t)cr[3]);
6854     }
6855 
6856     map &= MAKE_64BIT_MASK(0, len + 1);
6857     if (map != 0) {
6858         return 31 - clz32(map);
6859     }
6860 
6861     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6862     assert(sm);
6863     return ctz32(cpu->sme_vq.map);
6864 }
6865 
6866 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6867 {
6868     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6869 }
6870 
6871 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6872                       uint64_t value)
6873 {
6874     int cur_el = arm_current_el(env);
6875     int old_len = sve_vqm1_for_el(env, cur_el);
6876     int new_len;
6877 
6878     /* Bits other than [3:0] are RAZ/WI.  */
6879     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6880     raw_write(env, ri, value & 0xf);
6881 
6882     /*
6883      * Because we arrived here, we know both FP and SVE are enabled;
6884      * otherwise we would have trapped access to the ZCR_ELn register.
6885      */
6886     new_len = sve_vqm1_for_el(env, cur_el);
6887     if (new_len < old_len) {
6888         aarch64_sve_narrow_vq(env, new_len + 1);
6889     }
6890 }
6891 
6892 static const ARMCPRegInfo zcr_reginfo[] = {
6893     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6894       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6895       .access = PL1_RW, .type = ARM_CP_SVE,
6896       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6897       .writefn = zcr_write, .raw_writefn = raw_write },
6898     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6899       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6900       .access = PL2_RW, .type = ARM_CP_SVE,
6901       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6902       .writefn = zcr_write, .raw_writefn = raw_write },
6903     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6904       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6905       .access = PL3_RW, .type = ARM_CP_SVE,
6906       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6907       .writefn = zcr_write, .raw_writefn = raw_write },
6908 };
6909 
6910 #ifdef TARGET_AARCH64
6911 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6912                                     bool isread)
6913 {
6914     int el = arm_current_el(env);
6915 
6916     if (el == 0) {
6917         uint64_t sctlr = arm_sctlr(env, el);
6918         if (!(sctlr & SCTLR_EnTP2)) {
6919             return CP_ACCESS_TRAP;
6920         }
6921     }
6922     /* TODO: FEAT_FGT */
6923     if (el < 3
6924         && arm_feature(env, ARM_FEATURE_EL3)
6925         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6926         return CP_ACCESS_TRAP_EL3;
6927     }
6928     return CP_ACCESS_OK;
6929 }
6930 
6931 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6932                                  bool isread)
6933 {
6934     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6935     if (arm_current_el(env) < 3
6936         && arm_feature(env, ARM_FEATURE_EL3)
6937         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6938         return CP_ACCESS_TRAP_EL3;
6939     }
6940     return CP_ACCESS_OK;
6941 }
6942 
6943 /* ResetSVEState */
6944 static void arm_reset_sve_state(CPUARMState *env)
6945 {
6946     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6947     /* Recall that FFR is stored as pregs[16]. */
6948     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6949     vfp_set_fpcr(env, 0x0800009f);
6950 }
6951 
6952 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6953 {
6954     uint64_t change = (env->svcr ^ new) & mask;
6955 
6956     if (change == 0) {
6957         return;
6958     }
6959     env->svcr ^= change;
6960 
6961     if (change & R_SVCR_SM_MASK) {
6962         arm_reset_sve_state(env);
6963     }
6964 
6965     /*
6966      * ResetSMEState.
6967      *
6968      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
6969      * on enable: while disabled, the storage is inaccessible and the
6970      * value does not matter.  We're not saving the storage in vmstate
6971      * when disabled either.
6972      */
6973     if (change & new & R_SVCR_ZA_MASK) {
6974         memset(env->zarray, 0, sizeof(env->zarray));
6975     }
6976 
6977     if (tcg_enabled()) {
6978         arm_rebuild_hflags(env);
6979     }
6980 }
6981 
6982 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6983                        uint64_t value)
6984 {
6985     aarch64_set_svcr(env, value, -1);
6986 }
6987 
6988 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6989                        uint64_t value)
6990 {
6991     int cur_el = arm_current_el(env);
6992     int old_len = sve_vqm1_for_el(env, cur_el);
6993     int new_len;
6994 
6995     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6996     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6997     raw_write(env, ri, value);
6998 
6999     /*
7000      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
7001      * when SVL is widened (old values kept, or zeros).  Choose to keep the
7002      * current values for simplicity.  But for QEMU internals, we must still
7003      * apply the narrower SVL to the Zregs and Pregs -- see the comment
7004      * above aarch64_sve_narrow_vq.
7005      */
7006     new_len = sve_vqm1_for_el(env, cur_el);
7007     if (new_len < old_len) {
7008         aarch64_sve_narrow_vq(env, new_len + 1);
7009     }
7010 }
7011 
7012 static const ARMCPRegInfo sme_reginfo[] = {
7013     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7014       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7015       .access = PL0_RW, .accessfn = access_tpidr2,
7016       .fgt = FGT_NTPIDR2_EL0,
7017       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
7018     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7019       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7020       .access = PL0_RW, .type = ARM_CP_SME,
7021       .fieldoffset = offsetof(CPUARMState, svcr),
7022       .writefn = svcr_write, .raw_writefn = raw_write },
7023     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7024       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7025       .access = PL1_RW, .type = ARM_CP_SME,
7026       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7027       .writefn = smcr_write, .raw_writefn = raw_write },
7028     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7029       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7030       .access = PL2_RW, .type = ARM_CP_SME,
7031       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7032       .writefn = smcr_write, .raw_writefn = raw_write },
7033     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7034       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7035       .access = PL3_RW, .type = ARM_CP_SME,
7036       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7037       .writefn = smcr_write, .raw_writefn = raw_write },
7038     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7039       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7040       .access = PL1_R, .accessfn = access_aa64_tid1,
7041       /*
7042        * IMPLEMENTOR = 0 (software)
7043        * REVISION    = 0 (implementation defined)
7044        * SMPS        = 0 (no streaming execution priority in QEMU)
7045        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
7046        */
7047       .type = ARM_CP_CONST, .resetvalue = 0, },
7048     /*
7049      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7050      */
7051     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7052       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7053       .access = PL1_RW, .accessfn = access_esm,
7054       .fgt = FGT_NSMPRI_EL1,
7055       .type = ARM_CP_CONST, .resetvalue = 0 },
7056     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7057       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7058       .access = PL2_RW, .accessfn = access_esm,
7059       .type = ARM_CP_CONST, .resetvalue = 0 },
7060 };
7061 
7062 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7063                                   uint64_t value)
7064 {
7065     CPUState *cs = env_cpu(env);
7066 
7067     tlb_flush(cs);
7068 }
7069 
7070 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7071                         uint64_t value)
7072 {
7073     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7074     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7075         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7076         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7077 
7078     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7079 }
7080 
7081 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7082 {
7083     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7084                                      env_archcpu(env)->reset_l0gptsz);
7085 }
7086 
7087 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7088                                     uint64_t value)
7089 {
7090     CPUState *cs = env_cpu(env);
7091 
7092     tlb_flush_all_cpus_synced(cs);
7093 }
7094 
7095 static const ARMCPRegInfo rme_reginfo[] = {
7096     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7097       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7098       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7099       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7100     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7101       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7102       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7103     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7104       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7105       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7106     { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7107       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7108       .access = PL3_W, .type = ARM_CP_NO_RAW,
7109       .writefn = tlbi_aa64_paall_write },
7110     { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7111       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7112       .access = PL3_W, .type = ARM_CP_NO_RAW,
7113       .writefn = tlbi_aa64_paallos_write },
7114     /*
7115      * QEMU does not have a way to invalidate by physical address, thus
7116      * invalidating a range of physical addresses is accomplished by
7117      * flushing all tlb entries in the outer shareable domain,
7118      * just like PAALLOS.
7119      */
7120     { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7121       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7122       .access = PL3_W, .type = ARM_CP_NO_RAW,
7123       .writefn = tlbi_aa64_paallos_write },
7124     { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7125       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7126       .access = PL3_W, .type = ARM_CP_NO_RAW,
7127       .writefn = tlbi_aa64_paallos_write },
7128     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7129       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7130       .access = PL3_W, .type = ARM_CP_NOP },
7131 };
7132 
7133 static const ARMCPRegInfo rme_mte_reginfo[] = {
7134     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7135       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7136       .access = PL3_W, .type = ARM_CP_NOP },
7137 };
7138 #endif /* TARGET_AARCH64 */
7139 
7140 static void define_pmu_regs(ARMCPU *cpu)
7141 {
7142     /*
7143      * v7 performance monitor control register: same implementor
7144      * field as main ID register, and we implement four counters in
7145      * addition to the cycle count register.
7146      */
7147     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
7148     ARMCPRegInfo pmcr = {
7149         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7150         .access = PL0_RW,
7151         .fgt = FGT_PMCR_EL0,
7152         .type = ARM_CP_IO | ARM_CP_ALIAS,
7153         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7154         .accessfn = pmreg_access, .writefn = pmcr_write,
7155         .raw_writefn = raw_write,
7156     };
7157     ARMCPRegInfo pmcr64 = {
7158         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7159         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7160         .access = PL0_RW, .accessfn = pmreg_access,
7161         .fgt = FGT_PMCR_EL0,
7162         .type = ARM_CP_IO,
7163         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
7164         .resetvalue = cpu->isar.reset_pmcr_el0,
7165         .writefn = pmcr_write, .raw_writefn = raw_write,
7166     };
7167 
7168     define_one_arm_cp_reg(cpu, &pmcr);
7169     define_one_arm_cp_reg(cpu, &pmcr64);
7170     for (i = 0; i < pmcrn; i++) {
7171         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7172         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7173         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7174         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7175         ARMCPRegInfo pmev_regs[] = {
7176             { .name = pmevcntr_name, .cp = 15, .crn = 14,
7177               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7178               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7179               .fgt = FGT_PMEVCNTRN_EL0,
7180               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7181               .accessfn = pmreg_access_xevcntr },
7182             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7183               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
7184               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
7185               .type = ARM_CP_IO,
7186               .fgt = FGT_PMEVCNTRN_EL0,
7187               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7188               .raw_readfn = pmevcntr_rawread,
7189               .raw_writefn = pmevcntr_rawwrite },
7190             { .name = pmevtyper_name, .cp = 15, .crn = 14,
7191               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7192               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7193               .fgt = FGT_PMEVTYPERN_EL0,
7194               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7195               .accessfn = pmreg_access },
7196             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7197               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7198               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
7199               .fgt = FGT_PMEVTYPERN_EL0,
7200               .type = ARM_CP_IO,
7201               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7202               .raw_writefn = pmevtyper_rawwrite },
7203         };
7204         define_arm_cp_regs(cpu, pmev_regs);
7205         g_free(pmevcntr_name);
7206         g_free(pmevcntr_el0_name);
7207         g_free(pmevtyper_name);
7208         g_free(pmevtyper_el0_name);
7209     }
7210     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
7211         ARMCPRegInfo v81_pmu_regs[] = {
7212             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7213               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7214               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7215               .fgt = FGT_PMCEIDN_EL0,
7216               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7217             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7218               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7219               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7220               .fgt = FGT_PMCEIDN_EL0,
7221               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
7222         };
7223         define_arm_cp_regs(cpu, v81_pmu_regs);
7224     }
7225     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
7226         static const ARMCPRegInfo v84_pmmir = {
7227             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7228             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7229             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7230             .fgt = FGT_PMMIR_EL1,
7231             .resetvalue = 0
7232         };
7233         define_one_arm_cp_reg(cpu, &v84_pmmir);
7234     }
7235 }
7236 
7237 #ifndef CONFIG_USER_ONLY
7238 /*
7239  * We don't know until after realize whether there's a GICv3
7240  * attached, and that is what registers the gicv3 sysregs.
7241  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7242  * at runtime.
7243  */
7244 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7245 {
7246     ARMCPU *cpu = env_archcpu(env);
7247     uint64_t pfr1 = cpu->isar.id_pfr1;
7248 
7249     if (env->gicv3state) {
7250         pfr1 |= 1 << 28;
7251     }
7252     return pfr1;
7253 }
7254 
7255 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7256 {
7257     ARMCPU *cpu = env_archcpu(env);
7258     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
7259 
7260     if (env->gicv3state) {
7261         pfr0 |= 1 << 24;
7262     }
7263     return pfr0;
7264 }
7265 #endif
7266 
7267 /*
7268  * Shared logic between LORID and the rest of the LOR* registers.
7269  * Secure state exclusion has already been dealt with.
7270  */
7271 static CPAccessResult access_lor_ns(CPUARMState *env,
7272                                     const ARMCPRegInfo *ri, bool isread)
7273 {
7274     int el = arm_current_el(env);
7275 
7276     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7277         return CP_ACCESS_TRAP_EL2;
7278     }
7279     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7280         return CP_ACCESS_TRAP_EL3;
7281     }
7282     return CP_ACCESS_OK;
7283 }
7284 
7285 static CPAccessResult access_lor_other(CPUARMState *env,
7286                                        const ARMCPRegInfo *ri, bool isread)
7287 {
7288     if (arm_is_secure_below_el3(env)) {
7289         /* Access denied in secure mode.  */
7290         return CP_ACCESS_TRAP;
7291     }
7292     return access_lor_ns(env, ri, isread);
7293 }
7294 
7295 /*
7296  * A trivial implementation of ARMv8.1-LOR leaves all of these
7297  * registers fixed at 0, which indicates that there are zero
7298  * supported Limited Ordering regions.
7299  */
7300 static const ARMCPRegInfo lor_reginfo[] = {
7301     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7302       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7303       .access = PL1_RW, .accessfn = access_lor_other,
7304       .fgt = FGT_LORSA_EL1,
7305       .type = ARM_CP_CONST, .resetvalue = 0 },
7306     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7307       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7308       .access = PL1_RW, .accessfn = access_lor_other,
7309       .fgt = FGT_LOREA_EL1,
7310       .type = ARM_CP_CONST, .resetvalue = 0 },
7311     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7312       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7313       .access = PL1_RW, .accessfn = access_lor_other,
7314       .fgt = FGT_LORN_EL1,
7315       .type = ARM_CP_CONST, .resetvalue = 0 },
7316     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7317       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7318       .access = PL1_RW, .accessfn = access_lor_other,
7319       .fgt = FGT_LORC_EL1,
7320       .type = ARM_CP_CONST, .resetvalue = 0 },
7321     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7322       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7323       .access = PL1_R, .accessfn = access_lor_ns,
7324       .fgt = FGT_LORID_EL1,
7325       .type = ARM_CP_CONST, .resetvalue = 0 },
7326 };
7327 
7328 #ifdef TARGET_AARCH64
7329 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7330                                    bool isread)
7331 {
7332     int el = arm_current_el(env);
7333 
7334     if (el < 2 &&
7335         arm_is_el2_enabled(env) &&
7336         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7337         return CP_ACCESS_TRAP_EL2;
7338     }
7339     if (el < 3 &&
7340         arm_feature(env, ARM_FEATURE_EL3) &&
7341         !(env->cp15.scr_el3 & SCR_APK)) {
7342         return CP_ACCESS_TRAP_EL3;
7343     }
7344     return CP_ACCESS_OK;
7345 }
7346 
7347 static const ARMCPRegInfo pauth_reginfo[] = {
7348     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7349       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7350       .access = PL1_RW, .accessfn = access_pauth,
7351       .fgt = FGT_APDAKEY,
7352       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7353     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7354       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7355       .access = PL1_RW, .accessfn = access_pauth,
7356       .fgt = FGT_APDAKEY,
7357       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7358     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7359       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7360       .access = PL1_RW, .accessfn = access_pauth,
7361       .fgt = FGT_APDBKEY,
7362       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7363     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7364       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7365       .access = PL1_RW, .accessfn = access_pauth,
7366       .fgt = FGT_APDBKEY,
7367       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7368     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7369       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7370       .access = PL1_RW, .accessfn = access_pauth,
7371       .fgt = FGT_APGAKEY,
7372       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7373     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7374       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7375       .access = PL1_RW, .accessfn = access_pauth,
7376       .fgt = FGT_APGAKEY,
7377       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7378     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7379       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7380       .access = PL1_RW, .accessfn = access_pauth,
7381       .fgt = FGT_APIAKEY,
7382       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7383     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7384       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7385       .access = PL1_RW, .accessfn = access_pauth,
7386       .fgt = FGT_APIAKEY,
7387       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7388     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7389       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7390       .access = PL1_RW, .accessfn = access_pauth,
7391       .fgt = FGT_APIBKEY,
7392       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7393     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7394       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7395       .access = PL1_RW, .accessfn = access_pauth,
7396       .fgt = FGT_APIBKEY,
7397       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7398 };
7399 
7400 static const ARMCPRegInfo tlbirange_reginfo[] = {
7401     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7402       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7403       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7404       .fgt = FGT_TLBIRVAE1IS,
7405       .writefn = tlbi_aa64_rvae1is_write },
7406     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7407       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7408       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7409       .fgt = FGT_TLBIRVAAE1IS,
7410       .writefn = tlbi_aa64_rvae1is_write },
7411    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7412       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7413       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7414       .fgt = FGT_TLBIRVALE1IS,
7415       .writefn = tlbi_aa64_rvae1is_write },
7416     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7417       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7418       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7419       .fgt = FGT_TLBIRVAALE1IS,
7420       .writefn = tlbi_aa64_rvae1is_write },
7421     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7422       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7423       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7424       .fgt = FGT_TLBIRVAE1OS,
7425       .writefn = tlbi_aa64_rvae1is_write },
7426     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7427       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7428       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7429       .fgt = FGT_TLBIRVAAE1OS,
7430       .writefn = tlbi_aa64_rvae1is_write },
7431    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7432       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7433       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7434       .fgt = FGT_TLBIRVALE1OS,
7435       .writefn = tlbi_aa64_rvae1is_write },
7436     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7437       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7438       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7439       .fgt = FGT_TLBIRVAALE1OS,
7440       .writefn = tlbi_aa64_rvae1is_write },
7441     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7442       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7443       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7444       .fgt = FGT_TLBIRVAE1,
7445       .writefn = tlbi_aa64_rvae1_write },
7446     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7447       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7448       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7449       .fgt = FGT_TLBIRVAAE1,
7450       .writefn = tlbi_aa64_rvae1_write },
7451    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7452       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7453       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7454       .fgt = FGT_TLBIRVALE1,
7455       .writefn = tlbi_aa64_rvae1_write },
7456     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7457       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7458       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7459       .fgt = FGT_TLBIRVAALE1,
7460       .writefn = tlbi_aa64_rvae1_write },
7461     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7462       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7463       .access = PL2_W, .type = ARM_CP_NO_RAW,
7464       .writefn = tlbi_aa64_ripas2e1is_write },
7465     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7466       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7467       .access = PL2_W, .type = ARM_CP_NO_RAW,
7468       .writefn = tlbi_aa64_ripas2e1is_write },
7469     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7470       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7471       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7472       .writefn = tlbi_aa64_rvae2is_write },
7473    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7474       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7475       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7476       .writefn = tlbi_aa64_rvae2is_write },
7477     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7478       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7479       .access = PL2_W, .type = ARM_CP_NO_RAW,
7480       .writefn = tlbi_aa64_ripas2e1_write },
7481     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7482       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7483       .access = PL2_W, .type = ARM_CP_NO_RAW,
7484       .writefn = tlbi_aa64_ripas2e1_write },
7485    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7486       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7487       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7488       .writefn = tlbi_aa64_rvae2is_write },
7489    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7490       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7491       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7492       .writefn = tlbi_aa64_rvae2is_write },
7493     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7494       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7495       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7496       .writefn = tlbi_aa64_rvae2_write },
7497    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7498       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7499       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7500       .writefn = tlbi_aa64_rvae2_write },
7501    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7502       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7503       .access = PL3_W, .type = ARM_CP_NO_RAW,
7504       .writefn = tlbi_aa64_rvae3is_write },
7505    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7506       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7507       .access = PL3_W, .type = ARM_CP_NO_RAW,
7508       .writefn = tlbi_aa64_rvae3is_write },
7509    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7510       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7511       .access = PL3_W, .type = ARM_CP_NO_RAW,
7512       .writefn = tlbi_aa64_rvae3is_write },
7513    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7514       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7515       .access = PL3_W, .type = ARM_CP_NO_RAW,
7516       .writefn = tlbi_aa64_rvae3is_write },
7517    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7518       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7519       .access = PL3_W, .type = ARM_CP_NO_RAW,
7520       .writefn = tlbi_aa64_rvae3_write },
7521    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7522       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7523       .access = PL3_W, .type = ARM_CP_NO_RAW,
7524       .writefn = tlbi_aa64_rvae3_write },
7525 };
7526 
7527 static const ARMCPRegInfo tlbios_reginfo[] = {
7528     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7529       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7530       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7531       .fgt = FGT_TLBIVMALLE1OS,
7532       .writefn = tlbi_aa64_vmalle1is_write },
7533     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7534       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7535       .fgt = FGT_TLBIVAE1OS,
7536       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7537       .writefn = tlbi_aa64_vae1is_write },
7538     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7539       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7540       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7541       .fgt = FGT_TLBIASIDE1OS,
7542       .writefn = tlbi_aa64_vmalle1is_write },
7543     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7544       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7545       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7546       .fgt = FGT_TLBIVAAE1OS,
7547       .writefn = tlbi_aa64_vae1is_write },
7548     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7549       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7550       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7551       .fgt = FGT_TLBIVALE1OS,
7552       .writefn = tlbi_aa64_vae1is_write },
7553     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7554       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7555       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7556       .fgt = FGT_TLBIVAALE1OS,
7557       .writefn = tlbi_aa64_vae1is_write },
7558     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7559       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7560       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7561       .writefn = tlbi_aa64_alle2is_write },
7562     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7563       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7564       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7565       .writefn = tlbi_aa64_vae2is_write },
7566    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7567       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7568       .access = PL2_W, .type = ARM_CP_NO_RAW,
7569       .writefn = tlbi_aa64_alle1is_write },
7570     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7571       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7572       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7573       .writefn = tlbi_aa64_vae2is_write },
7574     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7575       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7576       .access = PL2_W, .type = ARM_CP_NO_RAW,
7577       .writefn = tlbi_aa64_alle1is_write },
7578     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7579       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7580       .access = PL2_W, .type = ARM_CP_NOP },
7581     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7582       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7583       .access = PL2_W, .type = ARM_CP_NOP },
7584     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7585       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7586       .access = PL2_W, .type = ARM_CP_NOP },
7587     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7588       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7589       .access = PL2_W, .type = ARM_CP_NOP },
7590     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7591       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7592       .access = PL3_W, .type = ARM_CP_NO_RAW,
7593       .writefn = tlbi_aa64_alle3is_write },
7594     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7595       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7596       .access = PL3_W, .type = ARM_CP_NO_RAW,
7597       .writefn = tlbi_aa64_vae3is_write },
7598     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7599       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7600       .access = PL3_W, .type = ARM_CP_NO_RAW,
7601       .writefn = tlbi_aa64_vae3is_write },
7602 };
7603 
7604 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7605 {
7606     Error *err = NULL;
7607     uint64_t ret;
7608 
7609     /* Success sets NZCV = 0000.  */
7610     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7611 
7612     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7613         /*
7614          * ??? Failed, for unknown reasons in the crypto subsystem.
7615          * The best we can do is log the reason and return the
7616          * timed-out indication to the guest.  There is no reason
7617          * we know to expect this failure to be transitory, so the
7618          * guest may well hang retrying the operation.
7619          */
7620         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7621                       ri->name, error_get_pretty(err));
7622         error_free(err);
7623 
7624         env->ZF = 0; /* NZCF = 0100 */
7625         return 0;
7626     }
7627     return ret;
7628 }
7629 
7630 /* We do not support re-seeding, so the two registers operate the same.  */
7631 static const ARMCPRegInfo rndr_reginfo[] = {
7632     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7633       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7634       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7635       .access = PL0_R, .readfn = rndr_readfn },
7636     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7637       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7638       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7639       .access = PL0_R, .readfn = rndr_readfn },
7640 };
7641 
7642 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7643                           uint64_t value)
7644 {
7645     ARMCPU *cpu = env_archcpu(env);
7646     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7647     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7648     uint64_t vaddr_in = (uint64_t) value;
7649     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7650     void *haddr;
7651     int mem_idx = cpu_mmu_index(env, false);
7652 
7653     /* This won't be crossing page boundaries */
7654     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7655     if (haddr) {
7656 #ifndef CONFIG_USER_ONLY
7657 
7658         ram_addr_t offset;
7659         MemoryRegion *mr;
7660 
7661         /* RCU lock is already being held */
7662         mr = memory_region_from_host(haddr, &offset);
7663 
7664         if (mr) {
7665             memory_region_writeback(mr, offset, dline_size);
7666         }
7667 #endif /*CONFIG_USER_ONLY*/
7668     }
7669 }
7670 
7671 static const ARMCPRegInfo dcpop_reg[] = {
7672     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7673       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7674       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7675       .fgt = FGT_DCCVAP,
7676       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7677 };
7678 
7679 static const ARMCPRegInfo dcpodp_reg[] = {
7680     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7681       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7682       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7683       .fgt = FGT_DCCVADP,
7684       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7685 };
7686 
7687 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7688                                        bool isread)
7689 {
7690     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7691         return CP_ACCESS_TRAP_EL2;
7692     }
7693 
7694     return CP_ACCESS_OK;
7695 }
7696 
7697 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7698                                  bool isread)
7699 {
7700     int el = arm_current_el(env);
7701 
7702     if (el < 2 && arm_is_el2_enabled(env)) {
7703         uint64_t hcr = arm_hcr_el2_eff(env);
7704         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7705             return CP_ACCESS_TRAP_EL2;
7706         }
7707     }
7708     if (el < 3 &&
7709         arm_feature(env, ARM_FEATURE_EL3) &&
7710         !(env->cp15.scr_el3 & SCR_ATA)) {
7711         return CP_ACCESS_TRAP_EL3;
7712     }
7713     return CP_ACCESS_OK;
7714 }
7715 
7716 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7717 {
7718     return env->pstate & PSTATE_TCO;
7719 }
7720 
7721 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7722 {
7723     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7724 }
7725 
7726 static const ARMCPRegInfo mte_reginfo[] = {
7727     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7728       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7729       .access = PL1_RW, .accessfn = access_mte,
7730       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7731     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7732       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7733       .access = PL1_RW, .accessfn = access_mte,
7734       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7735     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7736       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7737       .access = PL2_RW, .accessfn = access_mte,
7738       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7739     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7740       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7741       .access = PL3_RW,
7742       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7743     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7744       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7745       .access = PL1_RW, .accessfn = access_mte,
7746       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7747     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7748       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7749       .access = PL1_RW, .accessfn = access_mte,
7750       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7751     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7752       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7753       .type = ARM_CP_NO_RAW,
7754       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7755     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7756       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7757       .type = ARM_CP_NOP, .access = PL1_W,
7758       .fgt = FGT_DCIVAC,
7759       .accessfn = aa64_cacheop_poc_access },
7760     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7761       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7762       .fgt = FGT_DCISW,
7763       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7764     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7765       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7766       .type = ARM_CP_NOP, .access = PL1_W,
7767       .fgt = FGT_DCIVAC,
7768       .accessfn = aa64_cacheop_poc_access },
7769     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7770       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7771       .fgt = FGT_DCISW,
7772       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7773     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7774       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7775       .fgt = FGT_DCCSW,
7776       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7777     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7778       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7779       .fgt = FGT_DCCSW,
7780       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7781     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7782       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7783       .fgt = FGT_DCCISW,
7784       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7785     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7786       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7787       .fgt = FGT_DCCISW,
7788       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7789 };
7790 
7791 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7792     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7793       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7794       .type = ARM_CP_CONST, .access = PL0_RW, },
7795 };
7796 
7797 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7798     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7799       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7800       .type = ARM_CP_NOP, .access = PL0_W,
7801       .fgt = FGT_DCCVAC,
7802       .accessfn = aa64_cacheop_poc_access },
7803     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7804       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7805       .type = ARM_CP_NOP, .access = PL0_W,
7806       .fgt = FGT_DCCVAC,
7807       .accessfn = aa64_cacheop_poc_access },
7808     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7809       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7810       .type = ARM_CP_NOP, .access = PL0_W,
7811       .fgt = FGT_DCCVAP,
7812       .accessfn = aa64_cacheop_poc_access },
7813     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7814       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7815       .type = ARM_CP_NOP, .access = PL0_W,
7816       .fgt = FGT_DCCVAP,
7817       .accessfn = aa64_cacheop_poc_access },
7818     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7819       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7820       .type = ARM_CP_NOP, .access = PL0_W,
7821       .fgt = FGT_DCCVADP,
7822       .accessfn = aa64_cacheop_poc_access },
7823     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7824       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7825       .type = ARM_CP_NOP, .access = PL0_W,
7826       .fgt = FGT_DCCVADP,
7827       .accessfn = aa64_cacheop_poc_access },
7828     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7829       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7830       .type = ARM_CP_NOP, .access = PL0_W,
7831       .fgt = FGT_DCCIVAC,
7832       .accessfn = aa64_cacheop_poc_access },
7833     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7834       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7835       .type = ARM_CP_NOP, .access = PL0_W,
7836       .fgt = FGT_DCCIVAC,
7837       .accessfn = aa64_cacheop_poc_access },
7838     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7839       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7840       .access = PL0_W, .type = ARM_CP_DC_GVA,
7841 #ifndef CONFIG_USER_ONLY
7842       /* Avoid overhead of an access check that always passes in user-mode */
7843       .accessfn = aa64_zva_access,
7844       .fgt = FGT_DCZVA,
7845 #endif
7846     },
7847     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7848       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7849       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7850 #ifndef CONFIG_USER_ONLY
7851       /* Avoid overhead of an access check that always passes in user-mode */
7852       .accessfn = aa64_zva_access,
7853       .fgt = FGT_DCZVA,
7854 #endif
7855     },
7856 };
7857 
7858 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7859                                      bool isread)
7860 {
7861     uint64_t hcr = arm_hcr_el2_eff(env);
7862     int el = arm_current_el(env);
7863 
7864     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7865         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7866             if (hcr & HCR_TGE) {
7867                 return CP_ACCESS_TRAP_EL2;
7868             }
7869             return CP_ACCESS_TRAP;
7870         }
7871     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7872         return CP_ACCESS_TRAP_EL2;
7873     }
7874     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7875         return CP_ACCESS_TRAP_EL2;
7876     }
7877     if (el < 3
7878         && arm_feature(env, ARM_FEATURE_EL3)
7879         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7880         return CP_ACCESS_TRAP_EL3;
7881     }
7882     return CP_ACCESS_OK;
7883 }
7884 
7885 static const ARMCPRegInfo scxtnum_reginfo[] = {
7886     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7887       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7888       .access = PL0_RW, .accessfn = access_scxtnum,
7889       .fgt = FGT_SCXTNUM_EL0,
7890       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7891     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7892       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7893       .access = PL1_RW, .accessfn = access_scxtnum,
7894       .fgt = FGT_SCXTNUM_EL1,
7895       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7896     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7897       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7898       .access = PL2_RW, .accessfn = access_scxtnum,
7899       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7900     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7901       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7902       .access = PL3_RW,
7903       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7904 };
7905 
7906 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7907                                  bool isread)
7908 {
7909     if (arm_current_el(env) == 2 &&
7910         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7911         return CP_ACCESS_TRAP_EL3;
7912     }
7913     return CP_ACCESS_OK;
7914 }
7915 
7916 static const ARMCPRegInfo fgt_reginfo[] = {
7917     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7918       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7919       .access = PL2_RW, .accessfn = access_fgt,
7920       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7921     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7922       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7923       .access = PL2_RW, .accessfn = access_fgt,
7924       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7925     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7926       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7927       .access = PL2_RW, .accessfn = access_fgt,
7928       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7929     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7930       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7931       .access = PL2_RW, .accessfn = access_fgt,
7932       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7933     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7934       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7935       .access = PL2_RW, .accessfn = access_fgt,
7936       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7937 };
7938 #endif /* TARGET_AARCH64 */
7939 
7940 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7941                                      bool isread)
7942 {
7943     int el = arm_current_el(env);
7944 
7945     if (el == 0) {
7946         uint64_t sctlr = arm_sctlr(env, el);
7947         if (!(sctlr & SCTLR_EnRCTX)) {
7948             return CP_ACCESS_TRAP;
7949         }
7950     } else if (el == 1) {
7951         uint64_t hcr = arm_hcr_el2_eff(env);
7952         if (hcr & HCR_NV) {
7953             return CP_ACCESS_TRAP_EL2;
7954         }
7955     }
7956     return CP_ACCESS_OK;
7957 }
7958 
7959 static const ARMCPRegInfo predinv_reginfo[] = {
7960     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7961       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7962       .fgt = FGT_CFPRCTX,
7963       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7964     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7965       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7966       .fgt = FGT_DVPRCTX,
7967       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7968     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7969       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7970       .fgt = FGT_CPPRCTX,
7971       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7972     /*
7973      * Note the AArch32 opcodes have a different OPC1.
7974      */
7975     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7976       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7977       .fgt = FGT_CFPRCTX,
7978       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7979     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7980       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7981       .fgt = FGT_DVPRCTX,
7982       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7983     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7984       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7985       .fgt = FGT_CPPRCTX,
7986       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7987 };
7988 
7989 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7990 {
7991     /* Read the high 32 bits of the current CCSIDR */
7992     return extract64(ccsidr_read(env, ri), 32, 32);
7993 }
7994 
7995 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7996     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7997       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7998       .access = PL1_R,
7999       .accessfn = access_tid4,
8000       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
8001 };
8002 
8003 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8004                                        bool isread)
8005 {
8006     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
8007         return CP_ACCESS_TRAP_EL2;
8008     }
8009 
8010     return CP_ACCESS_OK;
8011 }
8012 
8013 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8014                                        bool isread)
8015 {
8016     if (arm_feature(env, ARM_FEATURE_V8)) {
8017         return access_aa64_tid3(env, ri, isread);
8018     }
8019 
8020     return CP_ACCESS_OK;
8021 }
8022 
8023 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8024                                      bool isread)
8025 {
8026     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8027         return CP_ACCESS_TRAP_EL2;
8028     }
8029 
8030     return CP_ACCESS_OK;
8031 }
8032 
8033 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8034                                         const ARMCPRegInfo *ri, bool isread)
8035 {
8036     /*
8037      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8038      * in v7A, not in v8A.
8039      */
8040     if (!arm_feature(env, ARM_FEATURE_V8) &&
8041         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8042         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8043         return CP_ACCESS_TRAP_EL2;
8044     }
8045     return CP_ACCESS_OK;
8046 }
8047 
8048 static const ARMCPRegInfo jazelle_regs[] = {
8049     { .name = "JIDR",
8050       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8051       .access = PL1_R, .accessfn = access_jazelle,
8052       .type = ARM_CP_CONST, .resetvalue = 0 },
8053     { .name = "JOSCR",
8054       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8055       .accessfn = access_joscr_jmcr,
8056       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8057     { .name = "JMCR",
8058       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8059       .accessfn = access_joscr_jmcr,
8060       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8061 };
8062 
8063 static const ARMCPRegInfo contextidr_el2 = {
8064     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8065     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8066     .access = PL2_RW,
8067     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8068 };
8069 
8070 static const ARMCPRegInfo vhe_reginfo[] = {
8071     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8072       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8073       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
8074       .raw_writefn = raw_write,
8075       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8076 #ifndef CONFIG_USER_ONLY
8077     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8078       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8079       .fieldoffset =
8080         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8081       .type = ARM_CP_IO, .access = PL2_RW,
8082       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8083     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8084       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8085       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8086       .resetfn = gt_hv_timer_reset,
8087       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8088     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8089       .type = ARM_CP_IO,
8090       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8091       .access = PL2_RW,
8092       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8093       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
8094     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8095       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8096       .type = ARM_CP_IO | ARM_CP_ALIAS,
8097       .access = PL2_RW, .accessfn = e2h_access,
8098       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8099       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8100     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8101       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8102       .type = ARM_CP_IO | ARM_CP_ALIAS,
8103       .access = PL2_RW, .accessfn = e2h_access,
8104       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8105       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8106     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8107       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8108       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8109       .access = PL2_RW, .accessfn = e2h_access,
8110       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8111     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8112       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8113       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8114       .access = PL2_RW, .accessfn = e2h_access,
8115       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8116     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8117       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8118       .type = ARM_CP_IO | ARM_CP_ALIAS,
8119       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8120       .access = PL2_RW, .accessfn = e2h_access,
8121       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8122     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8123       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8124       .type = ARM_CP_IO | ARM_CP_ALIAS,
8125       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8126       .access = PL2_RW, .accessfn = e2h_access,
8127       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8128 #endif
8129 };
8130 
8131 #ifndef CONFIG_USER_ONLY
8132 static const ARMCPRegInfo ats1e1_reginfo[] = {
8133     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
8134       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8135       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8136       .fgt = FGT_ATS1E1RP,
8137       .accessfn = at_e012_access, .writefn = ats_write64 },
8138     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
8139       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8140       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8141       .fgt = FGT_ATS1E1WP,
8142       .accessfn = at_e012_access, .writefn = ats_write64 },
8143 };
8144 
8145 static const ARMCPRegInfo ats1cp_reginfo[] = {
8146     { .name = "ATS1CPRP",
8147       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8148       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8149       .writefn = ats_write },
8150     { .name = "ATS1CPWP",
8151       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8152       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8153       .writefn = ats_write },
8154 };
8155 #endif
8156 
8157 /*
8158  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8159  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8160  * is non-zero, which is never for ARMv7, optionally in ARMv8
8161  * and mandatorily for ARMv8.2 and up.
8162  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8163  * implementation is RAZ/WI we can ignore this detail, as we
8164  * do for ACTLR.
8165  */
8166 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8167     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8168       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
8169       .access = PL1_RW, .accessfn = access_tacr,
8170       .type = ARM_CP_CONST, .resetvalue = 0 },
8171     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8172       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8173       .access = PL2_RW, .type = ARM_CP_CONST,
8174       .resetvalue = 0 },
8175 };
8176 
8177 void register_cp_regs_for_features(ARMCPU *cpu)
8178 {
8179     /* Register all the coprocessor registers based on feature bits */
8180     CPUARMState *env = &cpu->env;
8181     if (arm_feature(env, ARM_FEATURE_M)) {
8182         /* M profile has no coprocessor registers */
8183         return;
8184     }
8185 
8186     define_arm_cp_regs(cpu, cp_reginfo);
8187     if (!arm_feature(env, ARM_FEATURE_V8)) {
8188         /*
8189          * Must go early as it is full of wildcards that may be
8190          * overridden by later definitions.
8191          */
8192         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8193     }
8194 
8195     if (arm_feature(env, ARM_FEATURE_V6)) {
8196         /* The ID registers all have impdef reset values */
8197         ARMCPRegInfo v6_idregs[] = {
8198             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8199               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8200               .access = PL1_R, .type = ARM_CP_CONST,
8201               .accessfn = access_aa32_tid3,
8202               .resetvalue = cpu->isar.id_pfr0 },
8203             /*
8204              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
8205              * the value of the GIC field until after we define these regs.
8206              */
8207             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8208               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
8209               .access = PL1_R, .type = ARM_CP_NO_RAW,
8210               .accessfn = access_aa32_tid3,
8211 #ifdef CONFIG_USER_ONLY
8212               .type = ARM_CP_CONST,
8213               .resetvalue = cpu->isar.id_pfr1,
8214 #else
8215               .type = ARM_CP_NO_RAW,
8216               .accessfn = access_aa32_tid3,
8217               .readfn = id_pfr1_read,
8218               .writefn = arm_cp_write_ignore
8219 #endif
8220             },
8221             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8222               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8223               .access = PL1_R, .type = ARM_CP_CONST,
8224               .accessfn = access_aa32_tid3,
8225               .resetvalue = cpu->isar.id_dfr0 },
8226             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8227               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8228               .access = PL1_R, .type = ARM_CP_CONST,
8229               .accessfn = access_aa32_tid3,
8230               .resetvalue = cpu->id_afr0 },
8231             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8232               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8233               .access = PL1_R, .type = ARM_CP_CONST,
8234               .accessfn = access_aa32_tid3,
8235               .resetvalue = cpu->isar.id_mmfr0 },
8236             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8237               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8238               .access = PL1_R, .type = ARM_CP_CONST,
8239               .accessfn = access_aa32_tid3,
8240               .resetvalue = cpu->isar.id_mmfr1 },
8241             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8242               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8243               .access = PL1_R, .type = ARM_CP_CONST,
8244               .accessfn = access_aa32_tid3,
8245               .resetvalue = cpu->isar.id_mmfr2 },
8246             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8247               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8248               .access = PL1_R, .type = ARM_CP_CONST,
8249               .accessfn = access_aa32_tid3,
8250               .resetvalue = cpu->isar.id_mmfr3 },
8251             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8252               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8253               .access = PL1_R, .type = ARM_CP_CONST,
8254               .accessfn = access_aa32_tid3,
8255               .resetvalue = cpu->isar.id_isar0 },
8256             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8257               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8258               .access = PL1_R, .type = ARM_CP_CONST,
8259               .accessfn = access_aa32_tid3,
8260               .resetvalue = cpu->isar.id_isar1 },
8261             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8262               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8263               .access = PL1_R, .type = ARM_CP_CONST,
8264               .accessfn = access_aa32_tid3,
8265               .resetvalue = cpu->isar.id_isar2 },
8266             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8267               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8268               .access = PL1_R, .type = ARM_CP_CONST,
8269               .accessfn = access_aa32_tid3,
8270               .resetvalue = cpu->isar.id_isar3 },
8271             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8272               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8273               .access = PL1_R, .type = ARM_CP_CONST,
8274               .accessfn = access_aa32_tid3,
8275               .resetvalue = cpu->isar.id_isar4 },
8276             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8277               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8278               .access = PL1_R, .type = ARM_CP_CONST,
8279               .accessfn = access_aa32_tid3,
8280               .resetvalue = cpu->isar.id_isar5 },
8281             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8282               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8283               .access = PL1_R, .type = ARM_CP_CONST,
8284               .accessfn = access_aa32_tid3,
8285               .resetvalue = cpu->isar.id_mmfr4 },
8286             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
8287               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8288               .access = PL1_R, .type = ARM_CP_CONST,
8289               .accessfn = access_aa32_tid3,
8290               .resetvalue = cpu->isar.id_isar6 },
8291         };
8292         define_arm_cp_regs(cpu, v6_idregs);
8293         define_arm_cp_regs(cpu, v6_cp_reginfo);
8294     } else {
8295         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8296     }
8297     if (arm_feature(env, ARM_FEATURE_V6K)) {
8298         define_arm_cp_regs(cpu, v6k_cp_reginfo);
8299     }
8300     if (arm_feature(env, ARM_FEATURE_V7MP) &&
8301         !arm_feature(env, ARM_FEATURE_PMSA)) {
8302         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8303     }
8304     if (arm_feature(env, ARM_FEATURE_V7VE)) {
8305         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8306     }
8307     if (arm_feature(env, ARM_FEATURE_V7)) {
8308         ARMCPRegInfo clidr = {
8309             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8310             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
8311             .access = PL1_R, .type = ARM_CP_CONST,
8312             .accessfn = access_tid4,
8313             .fgt = FGT_CLIDR_EL1,
8314             .resetvalue = cpu->clidr
8315         };
8316         define_one_arm_cp_reg(cpu, &clidr);
8317         define_arm_cp_regs(cpu, v7_cp_reginfo);
8318         define_debug_regs(cpu);
8319         define_pmu_regs(cpu);
8320     } else {
8321         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
8322     }
8323     if (arm_feature(env, ARM_FEATURE_V8)) {
8324         /*
8325          * v8 ID registers, which all have impdef reset values.
8326          * Note that within the ID register ranges the unused slots
8327          * must all RAZ, not UNDEF; future architecture versions may
8328          * define new registers here.
8329          * ID registers which are AArch64 views of the AArch32 ID registers
8330          * which already existed in v6 and v7 are handled elsewhere,
8331          * in v6_idregs[].
8332          */
8333         int i;
8334         ARMCPRegInfo v8_idregs[] = {
8335             /*
8336              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8337              * emulation because we don't know the right value for the
8338              * GIC field until after we define these regs.
8339              */
8340             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8341               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
8342               .access = PL1_R,
8343 #ifdef CONFIG_USER_ONLY
8344               .type = ARM_CP_CONST,
8345               .resetvalue = cpu->isar.id_aa64pfr0
8346 #else
8347               .type = ARM_CP_NO_RAW,
8348               .accessfn = access_aa64_tid3,
8349               .readfn = id_aa64pfr0_read,
8350               .writefn = arm_cp_write_ignore
8351 #endif
8352             },
8353             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8354               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8355               .access = PL1_R, .type = ARM_CP_CONST,
8356               .accessfn = access_aa64_tid3,
8357               .resetvalue = cpu->isar.id_aa64pfr1},
8358             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8359               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8360               .access = PL1_R, .type = ARM_CP_CONST,
8361               .accessfn = access_aa64_tid3,
8362               .resetvalue = 0 },
8363             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8364               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8365               .access = PL1_R, .type = ARM_CP_CONST,
8366               .accessfn = access_aa64_tid3,
8367               .resetvalue = 0 },
8368             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
8369               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8370               .access = PL1_R, .type = ARM_CP_CONST,
8371               .accessfn = access_aa64_tid3,
8372               .resetvalue = cpu->isar.id_aa64zfr0 },
8373             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
8374               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8375               .access = PL1_R, .type = ARM_CP_CONST,
8376               .accessfn = access_aa64_tid3,
8377               .resetvalue = cpu->isar.id_aa64smfr0 },
8378             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8379               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8380               .access = PL1_R, .type = ARM_CP_CONST,
8381               .accessfn = access_aa64_tid3,
8382               .resetvalue = 0 },
8383             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8384               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8385               .access = PL1_R, .type = ARM_CP_CONST,
8386               .accessfn = access_aa64_tid3,
8387               .resetvalue = 0 },
8388             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8389               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8390               .access = PL1_R, .type = ARM_CP_CONST,
8391               .accessfn = access_aa64_tid3,
8392               .resetvalue = cpu->isar.id_aa64dfr0 },
8393             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8394               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8395               .access = PL1_R, .type = ARM_CP_CONST,
8396               .accessfn = access_aa64_tid3,
8397               .resetvalue = cpu->isar.id_aa64dfr1 },
8398             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8399               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8400               .access = PL1_R, .type = ARM_CP_CONST,
8401               .accessfn = access_aa64_tid3,
8402               .resetvalue = 0 },
8403             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8404               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8405               .access = PL1_R, .type = ARM_CP_CONST,
8406               .accessfn = access_aa64_tid3,
8407               .resetvalue = 0 },
8408             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8409               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8410               .access = PL1_R, .type = ARM_CP_CONST,
8411               .accessfn = access_aa64_tid3,
8412               .resetvalue = cpu->id_aa64afr0 },
8413             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8414               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8415               .access = PL1_R, .type = ARM_CP_CONST,
8416               .accessfn = access_aa64_tid3,
8417               .resetvalue = cpu->id_aa64afr1 },
8418             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8419               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8420               .access = PL1_R, .type = ARM_CP_CONST,
8421               .accessfn = access_aa64_tid3,
8422               .resetvalue = 0 },
8423             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8424               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8425               .access = PL1_R, .type = ARM_CP_CONST,
8426               .accessfn = access_aa64_tid3,
8427               .resetvalue = 0 },
8428             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8429               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8430               .access = PL1_R, .type = ARM_CP_CONST,
8431               .accessfn = access_aa64_tid3,
8432               .resetvalue = cpu->isar.id_aa64isar0 },
8433             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8434               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8435               .access = PL1_R, .type = ARM_CP_CONST,
8436               .accessfn = access_aa64_tid3,
8437               .resetvalue = cpu->isar.id_aa64isar1 },
8438             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8439               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8440               .access = PL1_R, .type = ARM_CP_CONST,
8441               .accessfn = access_aa64_tid3,
8442               .resetvalue = cpu->isar.id_aa64isar2 },
8443             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8444               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8445               .access = PL1_R, .type = ARM_CP_CONST,
8446               .accessfn = access_aa64_tid3,
8447               .resetvalue = 0 },
8448             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8449               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8450               .access = PL1_R, .type = ARM_CP_CONST,
8451               .accessfn = access_aa64_tid3,
8452               .resetvalue = 0 },
8453             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8454               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8455               .access = PL1_R, .type = ARM_CP_CONST,
8456               .accessfn = access_aa64_tid3,
8457               .resetvalue = 0 },
8458             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8459               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8460               .access = PL1_R, .type = ARM_CP_CONST,
8461               .accessfn = access_aa64_tid3,
8462               .resetvalue = 0 },
8463             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8464               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8465               .access = PL1_R, .type = ARM_CP_CONST,
8466               .accessfn = access_aa64_tid3,
8467               .resetvalue = 0 },
8468             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8469               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8470               .access = PL1_R, .type = ARM_CP_CONST,
8471               .accessfn = access_aa64_tid3,
8472               .resetvalue = cpu->isar.id_aa64mmfr0 },
8473             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8474               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8475               .access = PL1_R, .type = ARM_CP_CONST,
8476               .accessfn = access_aa64_tid3,
8477               .resetvalue = cpu->isar.id_aa64mmfr1 },
8478             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8479               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8480               .access = PL1_R, .type = ARM_CP_CONST,
8481               .accessfn = access_aa64_tid3,
8482               .resetvalue = cpu->isar.id_aa64mmfr2 },
8483             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8484               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8485               .access = PL1_R, .type = ARM_CP_CONST,
8486               .accessfn = access_aa64_tid3,
8487               .resetvalue = 0 },
8488             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8489               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8490               .access = PL1_R, .type = ARM_CP_CONST,
8491               .accessfn = access_aa64_tid3,
8492               .resetvalue = 0 },
8493             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8494               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8495               .access = PL1_R, .type = ARM_CP_CONST,
8496               .accessfn = access_aa64_tid3,
8497               .resetvalue = 0 },
8498             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8499               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8500               .access = PL1_R, .type = ARM_CP_CONST,
8501               .accessfn = access_aa64_tid3,
8502               .resetvalue = 0 },
8503             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8504               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8505               .access = PL1_R, .type = ARM_CP_CONST,
8506               .accessfn = access_aa64_tid3,
8507               .resetvalue = 0 },
8508             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8509               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8510               .access = PL1_R, .type = ARM_CP_CONST,
8511               .accessfn = access_aa64_tid3,
8512               .resetvalue = cpu->isar.mvfr0 },
8513             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8514               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8515               .access = PL1_R, .type = ARM_CP_CONST,
8516               .accessfn = access_aa64_tid3,
8517               .resetvalue = cpu->isar.mvfr1 },
8518             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8519               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8520               .access = PL1_R, .type = ARM_CP_CONST,
8521               .accessfn = access_aa64_tid3,
8522               .resetvalue = cpu->isar.mvfr2 },
8523             /*
8524              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8525              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8526              * as RAZ, since it is in the "reserved for future ID
8527              * registers, RAZ" part of the AArch32 encoding space.
8528              */
8529             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8530               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8531               .access = PL1_R, .type = ARM_CP_CONST,
8532               .accessfn = access_aa64_tid3,
8533               .resetvalue = 0 },
8534             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8535               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8536               .access = PL1_R, .type = ARM_CP_CONST,
8537               .accessfn = access_aa64_tid3,
8538               .resetvalue = 0 },
8539             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8540               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8541               .access = PL1_R, .type = ARM_CP_CONST,
8542               .accessfn = access_aa64_tid3,
8543               .resetvalue = 0 },
8544             /*
8545              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8546              * they're also RAZ for AArch64, and in v8 are gradually
8547              * being filled with AArch64-view-of-AArch32-ID-register
8548              * for new ID registers.
8549              */
8550             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8551               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8552               .access = PL1_R, .type = ARM_CP_CONST,
8553               .accessfn = access_aa64_tid3,
8554               .resetvalue = 0 },
8555             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8556               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8557               .access = PL1_R, .type = ARM_CP_CONST,
8558               .accessfn = access_aa64_tid3,
8559               .resetvalue = cpu->isar.id_pfr2 },
8560             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8561               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8562               .access = PL1_R, .type = ARM_CP_CONST,
8563               .accessfn = access_aa64_tid3,
8564               .resetvalue = cpu->isar.id_dfr1 },
8565             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8566               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8567               .access = PL1_R, .type = ARM_CP_CONST,
8568               .accessfn = access_aa64_tid3,
8569               .resetvalue = cpu->isar.id_mmfr5 },
8570             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8571               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8572               .access = PL1_R, .type = ARM_CP_CONST,
8573               .accessfn = access_aa64_tid3,
8574               .resetvalue = 0 },
8575             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8576               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8577               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8578               .fgt = FGT_PMCEIDN_EL0,
8579               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8580             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8581               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8582               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8583               .fgt = FGT_PMCEIDN_EL0,
8584               .resetvalue = cpu->pmceid0 },
8585             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8586               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8587               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8588               .fgt = FGT_PMCEIDN_EL0,
8589               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8590             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8591               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8592               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8593               .fgt = FGT_PMCEIDN_EL0,
8594               .resetvalue = cpu->pmceid1 },
8595         };
8596 #ifdef CONFIG_USER_ONLY
8597         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8598             { .name = "ID_AA64PFR0_EL1",
8599               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8600                                R_ID_AA64PFR0_ADVSIMD_MASK |
8601                                R_ID_AA64PFR0_SVE_MASK |
8602                                R_ID_AA64PFR0_DIT_MASK,
8603               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8604                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8605             { .name = "ID_AA64PFR1_EL1",
8606               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8607                                R_ID_AA64PFR1_SSBS_MASK |
8608                                R_ID_AA64PFR1_MTE_MASK |
8609                                R_ID_AA64PFR1_SME_MASK },
8610             { .name = "ID_AA64PFR*_EL1_RESERVED",
8611               .is_glob = true },
8612             { .name = "ID_AA64ZFR0_EL1",
8613               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8614                                R_ID_AA64ZFR0_AES_MASK |
8615                                R_ID_AA64ZFR0_BITPERM_MASK |
8616                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8617                                R_ID_AA64ZFR0_SHA3_MASK |
8618                                R_ID_AA64ZFR0_SM4_MASK |
8619                                R_ID_AA64ZFR0_I8MM_MASK |
8620                                R_ID_AA64ZFR0_F32MM_MASK |
8621                                R_ID_AA64ZFR0_F64MM_MASK },
8622             { .name = "ID_AA64SMFR0_EL1",
8623               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8624                                R_ID_AA64SMFR0_BI32I32_MASK |
8625                                R_ID_AA64SMFR0_B16F32_MASK |
8626                                R_ID_AA64SMFR0_F16F32_MASK |
8627                                R_ID_AA64SMFR0_I8I32_MASK |
8628                                R_ID_AA64SMFR0_F16F16_MASK |
8629                                R_ID_AA64SMFR0_B16B16_MASK |
8630                                R_ID_AA64SMFR0_I16I32_MASK |
8631                                R_ID_AA64SMFR0_F64F64_MASK |
8632                                R_ID_AA64SMFR0_I16I64_MASK |
8633                                R_ID_AA64SMFR0_SMEVER_MASK |
8634                                R_ID_AA64SMFR0_FA64_MASK },
8635             { .name = "ID_AA64MMFR0_EL1",
8636               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8637               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8638                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8639             { .name = "ID_AA64MMFR1_EL1",
8640               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8641             { .name = "ID_AA64MMFR2_EL1",
8642               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8643             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8644               .is_glob = true },
8645             { .name = "ID_AA64DFR0_EL1",
8646               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8647             { .name = "ID_AA64DFR1_EL1" },
8648             { .name = "ID_AA64DFR*_EL1_RESERVED",
8649               .is_glob = true },
8650             { .name = "ID_AA64AFR*",
8651               .is_glob = true },
8652             { .name = "ID_AA64ISAR0_EL1",
8653               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8654                                R_ID_AA64ISAR0_SHA1_MASK |
8655                                R_ID_AA64ISAR0_SHA2_MASK |
8656                                R_ID_AA64ISAR0_CRC32_MASK |
8657                                R_ID_AA64ISAR0_ATOMIC_MASK |
8658                                R_ID_AA64ISAR0_RDM_MASK |
8659                                R_ID_AA64ISAR0_SHA3_MASK |
8660                                R_ID_AA64ISAR0_SM3_MASK |
8661                                R_ID_AA64ISAR0_SM4_MASK |
8662                                R_ID_AA64ISAR0_DP_MASK |
8663                                R_ID_AA64ISAR0_FHM_MASK |
8664                                R_ID_AA64ISAR0_TS_MASK |
8665                                R_ID_AA64ISAR0_RNDR_MASK },
8666             { .name = "ID_AA64ISAR1_EL1",
8667               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8668                                R_ID_AA64ISAR1_APA_MASK |
8669                                R_ID_AA64ISAR1_API_MASK |
8670                                R_ID_AA64ISAR1_JSCVT_MASK |
8671                                R_ID_AA64ISAR1_FCMA_MASK |
8672                                R_ID_AA64ISAR1_LRCPC_MASK |
8673                                R_ID_AA64ISAR1_GPA_MASK |
8674                                R_ID_AA64ISAR1_GPI_MASK |
8675                                R_ID_AA64ISAR1_FRINTTS_MASK |
8676                                R_ID_AA64ISAR1_SB_MASK |
8677                                R_ID_AA64ISAR1_BF16_MASK |
8678                                R_ID_AA64ISAR1_DGH_MASK |
8679                                R_ID_AA64ISAR1_I8MM_MASK },
8680             { .name = "ID_AA64ISAR2_EL1",
8681               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8682                                R_ID_AA64ISAR2_RPRES_MASK |
8683                                R_ID_AA64ISAR2_GPA3_MASK |
8684                                R_ID_AA64ISAR2_APA3_MASK |
8685                                R_ID_AA64ISAR2_MOPS_MASK |
8686                                R_ID_AA64ISAR2_BC_MASK |
8687                                R_ID_AA64ISAR2_RPRFM_MASK |
8688                                R_ID_AA64ISAR2_CSSC_MASK },
8689             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8690               .is_glob = true },
8691         };
8692         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8693 #endif
8694         /*
8695          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8696          * TODO: For RMR, a write with bit 1 set should do something with
8697          * cpu_reset(). In the meantime, "the bit is strictly a request",
8698          * so we are in spec just ignoring writes.
8699          */
8700         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8701             !arm_feature(env, ARM_FEATURE_EL2)) {
8702             ARMCPRegInfo el1_reset_regs[] = {
8703                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8704                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8705                   .access = PL1_R,
8706                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8707                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8708                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8709                   .access = PL1_RW, .type = ARM_CP_CONST,
8710                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8711             };
8712             define_arm_cp_regs(cpu, el1_reset_regs);
8713         }
8714         define_arm_cp_regs(cpu, v8_idregs);
8715         define_arm_cp_regs(cpu, v8_cp_reginfo);
8716 
8717         for (i = 4; i < 16; i++) {
8718             /*
8719              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8720              * For pre-v8 cores there are RAZ patterns for these in
8721              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8722              * v8 extends the "must RAZ" part of the ID register space
8723              * to also cover c0, 0, c{8-15}, {0-7}.
8724              * These are STATE_AA32 because in the AArch64 sysreg space
8725              * c4-c7 is where the AArch64 ID registers live (and we've
8726              * already defined those in v8_idregs[]), and c8-c15 are not
8727              * "must RAZ" for AArch64.
8728              */
8729             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8730             ARMCPRegInfo v8_aa32_raz_idregs = {
8731                 .name = name,
8732                 .state = ARM_CP_STATE_AA32,
8733                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8734                 .access = PL1_R, .type = ARM_CP_CONST,
8735                 .accessfn = access_aa64_tid3,
8736                 .resetvalue = 0 };
8737             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8738         }
8739     }
8740 
8741     /*
8742      * Register the base EL2 cpregs.
8743      * Pre v8, these registers are implemented only as part of the
8744      * Virtualization Extensions (EL2 present).  Beginning with v8,
8745      * if EL2 is missing but EL3 is enabled, mostly these become
8746      * RES0 from EL3, with some specific exceptions.
8747      */
8748     if (arm_feature(env, ARM_FEATURE_EL2)
8749         || (arm_feature(env, ARM_FEATURE_EL3)
8750             && arm_feature(env, ARM_FEATURE_V8))) {
8751         uint64_t vmpidr_def = mpidr_read_val(env);
8752         ARMCPRegInfo vpidr_regs[] = {
8753             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8754               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8755               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8756               .resetvalue = cpu->midr,
8757               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8758               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8759             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8760               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8761               .access = PL2_RW, .resetvalue = cpu->midr,
8762               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8763               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8764             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8765               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8766               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8767               .resetvalue = vmpidr_def,
8768               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8769               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8770             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8771               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8772               .access = PL2_RW, .resetvalue = vmpidr_def,
8773               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8774               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8775         };
8776         /*
8777          * The only field of MDCR_EL2 that has a defined architectural reset
8778          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8779          */
8780         ARMCPRegInfo mdcr_el2 = {
8781             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8782             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8783             .writefn = mdcr_el2_write,
8784             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8785             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8786         };
8787         define_one_arm_cp_reg(cpu, &mdcr_el2);
8788         define_arm_cp_regs(cpu, vpidr_regs);
8789         define_arm_cp_regs(cpu, el2_cp_reginfo);
8790         if (arm_feature(env, ARM_FEATURE_V8)) {
8791             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8792         }
8793         if (cpu_isar_feature(aa64_sel2, cpu)) {
8794             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8795         }
8796         /*
8797          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8798          * See commentary near RMR_EL1.
8799          */
8800         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8801             static const ARMCPRegInfo el2_reset_regs[] = {
8802                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8803                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8804                   .access = PL2_R,
8805                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8806                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8807                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8808                   .access = PL2_R,
8809                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8810                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
8811                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
8812                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8813             };
8814             define_arm_cp_regs(cpu, el2_reset_regs);
8815         }
8816     }
8817 
8818     /* Register the base EL3 cpregs. */
8819     if (arm_feature(env, ARM_FEATURE_EL3)) {
8820         define_arm_cp_regs(cpu, el3_cp_reginfo);
8821         ARMCPRegInfo el3_regs[] = {
8822             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8823               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8824               .access = PL3_R,
8825               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
8826             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
8827               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
8828               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8829             { .name = "RMR", .state = ARM_CP_STATE_AA32,
8830               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8831               .access = PL3_RW, .type = ARM_CP_CONST,
8832               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
8833             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8834               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8835               .access = PL3_RW,
8836               .raw_writefn = raw_write, .writefn = sctlr_write,
8837               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8838               .resetvalue = cpu->reset_sctlr },
8839         };
8840 
8841         define_arm_cp_regs(cpu, el3_regs);
8842     }
8843     /*
8844      * The behaviour of NSACR is sufficiently various that we don't
8845      * try to describe it in a single reginfo:
8846      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8847      *     reads as constant 0xc00 from NS EL1 and NS EL2
8848      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8849      *  if v7 without EL3, register doesn't exist
8850      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8851      */
8852     if (arm_feature(env, ARM_FEATURE_EL3)) {
8853         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8854             static const ARMCPRegInfo nsacr = {
8855                 .name = "NSACR", .type = ARM_CP_CONST,
8856                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8857                 .access = PL1_RW, .accessfn = nsacr_access,
8858                 .resetvalue = 0xc00
8859             };
8860             define_one_arm_cp_reg(cpu, &nsacr);
8861         } else {
8862             static const ARMCPRegInfo nsacr = {
8863                 .name = "NSACR",
8864                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8865                 .access = PL3_RW | PL1_R,
8866                 .resetvalue = 0,
8867                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8868             };
8869             define_one_arm_cp_reg(cpu, &nsacr);
8870         }
8871     } else {
8872         if (arm_feature(env, ARM_FEATURE_V8)) {
8873             static const ARMCPRegInfo nsacr = {
8874                 .name = "NSACR", .type = ARM_CP_CONST,
8875                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8876                 .access = PL1_R,
8877                 .resetvalue = 0xc00
8878             };
8879             define_one_arm_cp_reg(cpu, &nsacr);
8880         }
8881     }
8882 
8883     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8884         if (arm_feature(env, ARM_FEATURE_V6)) {
8885             /* PMSAv6 not implemented */
8886             assert(arm_feature(env, ARM_FEATURE_V7));
8887             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8888             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8889         } else {
8890             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8891         }
8892     } else {
8893         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8894         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8895         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8896         if (cpu_isar_feature(aa32_hpd, cpu)) {
8897             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8898         }
8899     }
8900     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8901         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8902     }
8903     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8904         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8905     }
8906     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8907         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8908     }
8909     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8910         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8911     }
8912     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8913         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8914     }
8915     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8916         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8917     }
8918     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8919         define_arm_cp_regs(cpu, omap_cp_reginfo);
8920     }
8921     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8922         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8923     }
8924     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8925         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8926     }
8927     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8928         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8929     }
8930     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8931         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8932     }
8933     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8934         define_arm_cp_regs(cpu, jazelle_regs);
8935     }
8936     /*
8937      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8938      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8939      * be read-only (ie write causes UNDEF exception).
8940      */
8941     {
8942         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8943             /*
8944              * Pre-v8 MIDR space.
8945              * Note that the MIDR isn't a simple constant register because
8946              * of the TI925 behaviour where writes to another register can
8947              * cause the MIDR value to change.
8948              *
8949              * Unimplemented registers in the c15 0 0 0 space default to
8950              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8951              * and friends override accordingly.
8952              */
8953             { .name = "MIDR",
8954               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8955               .access = PL1_R, .resetvalue = cpu->midr,
8956               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8957               .readfn = midr_read,
8958               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8959               .type = ARM_CP_OVERRIDE },
8960             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8961             { .name = "DUMMY",
8962               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8963               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8964             { .name = "DUMMY",
8965               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8966               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8967             { .name = "DUMMY",
8968               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8969               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8970             { .name = "DUMMY",
8971               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8972               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8973             { .name = "DUMMY",
8974               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8975               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8976         };
8977         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8978             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8979               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8980               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8981               .fgt = FGT_MIDR_EL1,
8982               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8983               .readfn = midr_read },
8984             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8985             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8986               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8987               .access = PL1_R, .resetvalue = cpu->midr },
8988             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8989               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8990               .access = PL1_R,
8991               .accessfn = access_aa64_tid1,
8992               .fgt = FGT_REVIDR_EL1,
8993               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8994         };
8995         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8996             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8997             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8998             .access = PL1_R, .resetvalue = cpu->midr
8999         };
9000         ARMCPRegInfo id_cp_reginfo[] = {
9001             /* These are common to v8 and pre-v8 */
9002             { .name = "CTR",
9003               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
9004               .access = PL1_R, .accessfn = ctr_el0_access,
9005               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9006             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9007               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9008               .access = PL0_R, .accessfn = ctr_el0_access,
9009               .fgt = FGT_CTR_EL0,
9010               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9011             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9012             { .name = "TCMTR",
9013               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
9014               .access = PL1_R,
9015               .accessfn = access_aa32_tid1,
9016               .type = ARM_CP_CONST, .resetvalue = 0 },
9017         };
9018         /* TLBTR is specific to VMSA */
9019         ARMCPRegInfo id_tlbtr_reginfo = {
9020               .name = "TLBTR",
9021               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
9022               .access = PL1_R,
9023               .accessfn = access_aa32_tid1,
9024               .type = ARM_CP_CONST, .resetvalue = 0,
9025         };
9026         /* MPUIR is specific to PMSA V6+ */
9027         ARMCPRegInfo id_mpuir_reginfo = {
9028               .name = "MPUIR",
9029               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9030               .access = PL1_R, .type = ARM_CP_CONST,
9031               .resetvalue = cpu->pmsav7_dregion << 8
9032         };
9033         /* HMPUIR is specific to PMSA V8 */
9034         ARMCPRegInfo id_hmpuir_reginfo = {
9035             .name = "HMPUIR",
9036             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9037             .access = PL2_R, .type = ARM_CP_CONST,
9038             .resetvalue = cpu->pmsav8r_hdregion
9039         };
9040         static const ARMCPRegInfo crn0_wi_reginfo = {
9041             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9042             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9043             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9044         };
9045 #ifdef CONFIG_USER_ONLY
9046         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
9047             { .name = "MIDR_EL1",
9048               .exported_bits = R_MIDR_EL1_REVISION_MASK |
9049                                R_MIDR_EL1_PARTNUM_MASK |
9050                                R_MIDR_EL1_ARCHITECTURE_MASK |
9051                                R_MIDR_EL1_VARIANT_MASK |
9052                                R_MIDR_EL1_IMPLEMENTER_MASK },
9053             { .name = "REVIDR_EL1" },
9054         };
9055         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9056 #endif
9057         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9058             arm_feature(env, ARM_FEATURE_STRONGARM)) {
9059             size_t i;
9060             /*
9061              * Register the blanket "writes ignored" value first to cover the
9062              * whole space. Then update the specific ID registers to allow write
9063              * access, so that they ignore writes rather than causing them to
9064              * UNDEF.
9065              */
9066             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
9067             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9068                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
9069             }
9070             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9071                 id_cp_reginfo[i].access = PL1_RW;
9072             }
9073             id_mpuir_reginfo.access = PL1_RW;
9074             id_tlbtr_reginfo.access = PL1_RW;
9075         }
9076         if (arm_feature(env, ARM_FEATURE_V8)) {
9077             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
9078             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9079                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9080             }
9081         } else {
9082             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9083         }
9084         define_arm_cp_regs(cpu, id_cp_reginfo);
9085         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9086             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
9087         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9088                    arm_feature(env, ARM_FEATURE_V8)) {
9089             uint32_t i = 0;
9090             char *tmp_string;
9091 
9092             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9093             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9094             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9095 
9096             /* Register alias is only valid for first 32 indexes */
9097             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9098                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9099                 uint8_t opc1 = extract32(i, 4, 1);
9100                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9101 
9102                 tmp_string = g_strdup_printf("PRBAR%u", i);
9103                 ARMCPRegInfo tmp_prbarn_reginfo = {
9104                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9105                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9106                     .access = PL1_RW, .resetvalue = 0,
9107                     .accessfn = access_tvm_trvm,
9108                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9109                 };
9110                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9111                 g_free(tmp_string);
9112 
9113                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9114                 tmp_string = g_strdup_printf("PRLAR%u", i);
9115                 ARMCPRegInfo tmp_prlarn_reginfo = {
9116                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9117                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9118                     .access = PL1_RW, .resetvalue = 0,
9119                     .accessfn = access_tvm_trvm,
9120                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9121                 };
9122                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9123                 g_free(tmp_string);
9124             }
9125 
9126             /* Register alias is only valid for first 32 indexes */
9127             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9128                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9129                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9130                 uint8_t opc2 = extract32(i, 0, 1) << 2;
9131 
9132                 tmp_string = g_strdup_printf("HPRBAR%u", i);
9133                 ARMCPRegInfo tmp_hprbarn_reginfo = {
9134                     .name = tmp_string,
9135                     .type = ARM_CP_NO_RAW,
9136                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9137                     .access = PL2_RW, .resetvalue = 0,
9138                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9139                 };
9140                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9141                 g_free(tmp_string);
9142 
9143                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9144                 tmp_string = g_strdup_printf("HPRLAR%u", i);
9145                 ARMCPRegInfo tmp_hprlarn_reginfo = {
9146                     .name = tmp_string,
9147                     .type = ARM_CP_NO_RAW,
9148                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9149                     .access = PL2_RW, .resetvalue = 0,
9150                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9151                 };
9152                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9153                 g_free(tmp_string);
9154             }
9155         } else if (arm_feature(env, ARM_FEATURE_V7)) {
9156             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9157         }
9158     }
9159 
9160     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
9161         ARMCPRegInfo mpidr_cp_reginfo[] = {
9162             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9163               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
9164               .fgt = FGT_MPIDR_EL1,
9165               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
9166         };
9167 #ifdef CONFIG_USER_ONLY
9168         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
9169             { .name = "MPIDR_EL1",
9170               .fixed_bits = 0x0000000080000000 },
9171         };
9172         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9173 #endif
9174         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9175     }
9176 
9177     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
9178         ARMCPRegInfo auxcr_reginfo[] = {
9179             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9180               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
9181               .access = PL1_RW, .accessfn = access_tacr,
9182               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
9183             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9184               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9185               .access = PL2_RW, .type = ARM_CP_CONST,
9186               .resetvalue = 0 },
9187             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9188               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9189               .access = PL3_RW, .type = ARM_CP_CONST,
9190               .resetvalue = 0 },
9191         };
9192         define_arm_cp_regs(cpu, auxcr_reginfo);
9193         if (cpu_isar_feature(aa32_ac2, cpu)) {
9194             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
9195         }
9196     }
9197 
9198     if (arm_feature(env, ARM_FEATURE_CBAR)) {
9199         /*
9200          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9201          * There are two flavours:
9202          *  (1) older 32-bit only cores have a simple 32-bit CBAR
9203          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9204          *      32-bit register visible to AArch32 at a different encoding
9205          *      to the "flavour 1" register and with the bits rearranged to
9206          *      be able to squash a 64-bit address into the 32-bit view.
9207          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9208          * in future if we support AArch32-only configs of some of the
9209          * AArch64 cores we might need to add a specific feature flag
9210          * to indicate cores with "flavour 2" CBAR.
9211          */
9212         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9213             /* 32 bit view is [31:18] 0...0 [43:32]. */
9214             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
9215                 | extract64(cpu->reset_cbar, 32, 12);
9216             ARMCPRegInfo cbar_reginfo[] = {
9217                 { .name = "CBAR",
9218                   .type = ARM_CP_CONST,
9219                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
9220                   .access = PL1_R, .resetvalue = cbar32 },
9221                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
9222                   .type = ARM_CP_CONST,
9223                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
9224                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
9225             };
9226             /* We don't implement a r/w 64 bit CBAR currently */
9227             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
9228             define_arm_cp_regs(cpu, cbar_reginfo);
9229         } else {
9230             ARMCPRegInfo cbar = {
9231                 .name = "CBAR",
9232                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
9233                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
9234                 .fieldoffset = offsetof(CPUARMState,
9235                                         cp15.c15_config_base_address)
9236             };
9237             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
9238                 cbar.access = PL1_R;
9239                 cbar.fieldoffset = 0;
9240                 cbar.type = ARM_CP_CONST;
9241             }
9242             define_one_arm_cp_reg(cpu, &cbar);
9243         }
9244     }
9245 
9246     if (arm_feature(env, ARM_FEATURE_VBAR)) {
9247         static const ARMCPRegInfo vbar_cp_reginfo[] = {
9248             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
9249               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
9250               .access = PL1_RW, .writefn = vbar_write,
9251               .fgt = FGT_VBAR_EL1,
9252               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
9253                                      offsetof(CPUARMState, cp15.vbar_ns) },
9254               .resetvalue = 0 },
9255         };
9256         define_arm_cp_regs(cpu, vbar_cp_reginfo);
9257     }
9258 
9259     /* Generic registers whose values depend on the implementation */
9260     {
9261         ARMCPRegInfo sctlr = {
9262             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
9263             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
9264             .access = PL1_RW, .accessfn = access_tvm_trvm,
9265             .fgt = FGT_SCTLR_EL1,
9266             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9267                                    offsetof(CPUARMState, cp15.sctlr_ns) },
9268             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9269             .raw_writefn = raw_write,
9270         };
9271         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9272             /*
9273              * Normally we would always end the TB on an SCTLR write, but Linux
9274              * arch/arm/mach-pxa/sleep.S expects two instructions following
9275              * an MMU enable to execute from cache.  Imitate this behaviour.
9276              */
9277             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9278         }
9279         define_one_arm_cp_reg(cpu, &sctlr);
9280 
9281         if (arm_feature(env, ARM_FEATURE_PMSA) &&
9282             arm_feature(env, ARM_FEATURE_V8)) {
9283             ARMCPRegInfo vsctlr = {
9284                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9285                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9286                 .access = PL2_RW, .resetvalue = 0x0,
9287                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9288             };
9289             define_one_arm_cp_reg(cpu, &vsctlr);
9290         }
9291     }
9292 
9293     if (cpu_isar_feature(aa64_lor, cpu)) {
9294         define_arm_cp_regs(cpu, lor_reginfo);
9295     }
9296     if (cpu_isar_feature(aa64_pan, cpu)) {
9297         define_one_arm_cp_reg(cpu, &pan_reginfo);
9298     }
9299 #ifndef CONFIG_USER_ONLY
9300     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9301         define_arm_cp_regs(cpu, ats1e1_reginfo);
9302     }
9303     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9304         define_arm_cp_regs(cpu, ats1cp_reginfo);
9305     }
9306 #endif
9307     if (cpu_isar_feature(aa64_uao, cpu)) {
9308         define_one_arm_cp_reg(cpu, &uao_reginfo);
9309     }
9310 
9311     if (cpu_isar_feature(aa64_dit, cpu)) {
9312         define_one_arm_cp_reg(cpu, &dit_reginfo);
9313     }
9314     if (cpu_isar_feature(aa64_ssbs, cpu)) {
9315         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9316     }
9317     if (cpu_isar_feature(any_ras, cpu)) {
9318         define_arm_cp_regs(cpu, minimal_ras_reginfo);
9319     }
9320 
9321     if (cpu_isar_feature(aa64_vh, cpu) ||
9322         cpu_isar_feature(aa64_debugv8p2, cpu)) {
9323         define_one_arm_cp_reg(cpu, &contextidr_el2);
9324     }
9325     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9326         define_arm_cp_regs(cpu, vhe_reginfo);
9327     }
9328 
9329     if (cpu_isar_feature(aa64_sve, cpu)) {
9330         define_arm_cp_regs(cpu, zcr_reginfo);
9331     }
9332 
9333     if (cpu_isar_feature(aa64_hcx, cpu)) {
9334         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9335     }
9336 
9337 #ifdef TARGET_AARCH64
9338     if (cpu_isar_feature(aa64_sme, cpu)) {
9339         define_arm_cp_regs(cpu, sme_reginfo);
9340     }
9341     if (cpu_isar_feature(aa64_pauth, cpu)) {
9342         define_arm_cp_regs(cpu, pauth_reginfo);
9343     }
9344     if (cpu_isar_feature(aa64_rndr, cpu)) {
9345         define_arm_cp_regs(cpu, rndr_reginfo);
9346     }
9347     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9348         define_arm_cp_regs(cpu, tlbirange_reginfo);
9349     }
9350     if (cpu_isar_feature(aa64_tlbios, cpu)) {
9351         define_arm_cp_regs(cpu, tlbios_reginfo);
9352     }
9353     /* Data Cache clean instructions up to PoP */
9354     if (cpu_isar_feature(aa64_dcpop, cpu)) {
9355         define_one_arm_cp_reg(cpu, dcpop_reg);
9356 
9357         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9358             define_one_arm_cp_reg(cpu, dcpodp_reg);
9359         }
9360     }
9361 
9362     /*
9363      * If full MTE is enabled, add all of the system registers.
9364      * If only "instructions available at EL0" are enabled,
9365      * then define only a RAZ/WI version of PSTATE.TCO.
9366      */
9367     if (cpu_isar_feature(aa64_mte, cpu)) {
9368         ARMCPRegInfo gmid_reginfo = {
9369             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
9370             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
9371             .access = PL1_R, .accessfn = access_aa64_tid5,
9372             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
9373         };
9374         define_one_arm_cp_reg(cpu, &gmid_reginfo);
9375         define_arm_cp_regs(cpu, mte_reginfo);
9376         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9377     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9378         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
9379         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9380     }
9381 
9382     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9383         define_arm_cp_regs(cpu, scxtnum_reginfo);
9384     }
9385 
9386     if (cpu_isar_feature(aa64_fgt, cpu)) {
9387         define_arm_cp_regs(cpu, fgt_reginfo);
9388     }
9389 
9390     if (cpu_isar_feature(aa64_rme, cpu)) {
9391         define_arm_cp_regs(cpu, rme_reginfo);
9392         if (cpu_isar_feature(aa64_mte, cpu)) {
9393             define_arm_cp_regs(cpu, rme_mte_reginfo);
9394         }
9395     }
9396 #endif
9397 
9398     if (cpu_isar_feature(any_predinv, cpu)) {
9399         define_arm_cp_regs(cpu, predinv_reginfo);
9400     }
9401 
9402     if (cpu_isar_feature(any_ccidx, cpu)) {
9403         define_arm_cp_regs(cpu, ccsidr2_reginfo);
9404     }
9405 
9406 #ifndef CONFIG_USER_ONLY
9407     /*
9408      * Register redirections and aliases must be done last,
9409      * after the registers from the other extensions have been defined.
9410      */
9411     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9412         define_arm_vh_e2h_redirects_aliases(cpu);
9413     }
9414 #endif
9415 }
9416 
9417 /* Sort alphabetically by type name, except for "any". */
9418 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
9419 {
9420     ObjectClass *class_a = (ObjectClass *)a;
9421     ObjectClass *class_b = (ObjectClass *)b;
9422     const char *name_a, *name_b;
9423 
9424     name_a = object_class_get_name(class_a);
9425     name_b = object_class_get_name(class_b);
9426     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
9427         return 1;
9428     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
9429         return -1;
9430     } else {
9431         return strcmp(name_a, name_b);
9432     }
9433 }
9434 
9435 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
9436 {
9437     ObjectClass *oc = data;
9438     CPUClass *cc = CPU_CLASS(oc);
9439     const char *typename;
9440     char *name;
9441 
9442     typename = object_class_get_name(oc);
9443     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
9444     if (cc->deprecation_note) {
9445         qemu_printf("  %s (deprecated)\n", name);
9446     } else {
9447         qemu_printf("  %s\n", name);
9448     }
9449     g_free(name);
9450 }
9451 
9452 void arm_cpu_list(void)
9453 {
9454     GSList *list;
9455 
9456     list = object_class_get_list(TYPE_ARM_CPU, false);
9457     list = g_slist_sort(list, arm_cpu_list_compare);
9458     qemu_printf("Available CPUs:\n");
9459     g_slist_foreach(list, arm_cpu_list_entry, NULL);
9460     g_slist_free(list);
9461 }
9462 
9463 /*
9464  * Private utility function for define_one_arm_cp_reg_with_opaque():
9465  * add a single reginfo struct to the hash table.
9466  */
9467 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9468                                    void *opaque, CPState state,
9469                                    CPSecureState secstate,
9470                                    int crm, int opc1, int opc2,
9471                                    const char *name)
9472 {
9473     CPUARMState *env = &cpu->env;
9474     uint32_t key;
9475     ARMCPRegInfo *r2;
9476     bool is64 = r->type & ARM_CP_64BIT;
9477     bool ns = secstate & ARM_CP_SECSTATE_NS;
9478     int cp = r->cp;
9479     size_t name_len;
9480     bool make_const;
9481 
9482     switch (state) {
9483     case ARM_CP_STATE_AA32:
9484         /* We assume it is a cp15 register if the .cp field is left unset. */
9485         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9486             cp = 15;
9487         }
9488         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9489         break;
9490     case ARM_CP_STATE_AA64:
9491         /*
9492          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9493          * cp == 0 as equivalent to the value for "standard guest-visible
9494          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9495          * in their AArch64 view (the .cp value may be non-zero for the
9496          * benefit of the AArch32 view).
9497          */
9498         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9499             cp = CP_REG_ARM64_SYSREG_CP;
9500         }
9501         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9502         break;
9503     default:
9504         g_assert_not_reached();
9505     }
9506 
9507     /* Overriding of an existing definition must be explicitly requested. */
9508     if (!(r->type & ARM_CP_OVERRIDE)) {
9509         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9510         if (oldreg) {
9511             assert(oldreg->type & ARM_CP_OVERRIDE);
9512         }
9513     }
9514 
9515     /*
9516      * Eliminate registers that are not present because the EL is missing.
9517      * Doing this here makes it easier to put all registers for a given
9518      * feature into the same ARMCPRegInfo array and define them all at once.
9519      */
9520     make_const = false;
9521     if (arm_feature(env, ARM_FEATURE_EL3)) {
9522         /*
9523          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9524          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9525          */
9526         int min_el = ctz32(r->access) / 2;
9527         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9528             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9529                 return;
9530             }
9531             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9532         }
9533     } else {
9534         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9535                                  ? PL2_RW : PL1_RW);
9536         if ((r->access & max_el) == 0) {
9537             return;
9538         }
9539     }
9540 
9541     /* Combine cpreg and name into one allocation. */
9542     name_len = strlen(name) + 1;
9543     r2 = g_malloc(sizeof(*r2) + name_len);
9544     *r2 = *r;
9545     r2->name = memcpy(r2 + 1, name, name_len);
9546 
9547     /*
9548      * Update fields to match the instantiation, overwiting wildcards
9549      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9550      */
9551     r2->cp = cp;
9552     r2->crm = crm;
9553     r2->opc1 = opc1;
9554     r2->opc2 = opc2;
9555     r2->state = state;
9556     r2->secure = secstate;
9557     if (opaque) {
9558         r2->opaque = opaque;
9559     }
9560 
9561     if (make_const) {
9562         /* This should not have been a very special register to begin. */
9563         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9564         assert(old_special == 0 || old_special == ARM_CP_NOP);
9565         /*
9566          * Set the special function to CONST, retaining the other flags.
9567          * This is important for e.g. ARM_CP_SVE so that we still
9568          * take the SVE trap if CPTR_EL3.EZ == 0.
9569          */
9570         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9571         /*
9572          * Usually, these registers become RES0, but there are a few
9573          * special cases like VPIDR_EL2 which have a constant non-zero
9574          * value with writes ignored.
9575          */
9576         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9577             r2->resetvalue = 0;
9578         }
9579         /*
9580          * ARM_CP_CONST has precedence, so removing the callbacks and
9581          * offsets are not strictly necessary, but it is potentially
9582          * less confusing to debug later.
9583          */
9584         r2->readfn = NULL;
9585         r2->writefn = NULL;
9586         r2->raw_readfn = NULL;
9587         r2->raw_writefn = NULL;
9588         r2->resetfn = NULL;
9589         r2->fieldoffset = 0;
9590         r2->bank_fieldoffsets[0] = 0;
9591         r2->bank_fieldoffsets[1] = 0;
9592     } else {
9593         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9594 
9595         if (isbanked) {
9596             /*
9597              * Register is banked (using both entries in array).
9598              * Overwriting fieldoffset as the array is only used to define
9599              * banked registers but later only fieldoffset is used.
9600              */
9601             r2->fieldoffset = r->bank_fieldoffsets[ns];
9602         }
9603         if (state == ARM_CP_STATE_AA32) {
9604             if (isbanked) {
9605                 /*
9606                  * If the register is banked then we don't need to migrate or
9607                  * reset the 32-bit instance in certain cases:
9608                  *
9609                  * 1) If the register has both 32-bit and 64-bit instances
9610                  *    then we can count on the 64-bit instance taking care
9611                  *    of the non-secure bank.
9612                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9613                  *    version taking care of the secure bank.  This requires
9614                  *    that separate 32 and 64-bit definitions are provided.
9615                  */
9616                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9617                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9618                     r2->type |= ARM_CP_ALIAS;
9619                 }
9620             } else if ((secstate != r->secure) && !ns) {
9621                 /*
9622                  * The register is not banked so we only want to allow
9623                  * migration of the non-secure instance.
9624                  */
9625                 r2->type |= ARM_CP_ALIAS;
9626             }
9627 
9628             if (HOST_BIG_ENDIAN &&
9629                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9630                 r2->fieldoffset += sizeof(uint32_t);
9631             }
9632         }
9633     }
9634 
9635     /*
9636      * By convention, for wildcarded registers only the first
9637      * entry is used for migration; the others are marked as
9638      * ALIAS so we don't try to transfer the register
9639      * multiple times. Special registers (ie NOP/WFI) are
9640      * never migratable and not even raw-accessible.
9641      */
9642     if (r2->type & ARM_CP_SPECIAL_MASK) {
9643         r2->type |= ARM_CP_NO_RAW;
9644     }
9645     if (((r->crm == CP_ANY) && crm != 0) ||
9646         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9647         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9648         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9649     }
9650 
9651     /*
9652      * Check that raw accesses are either forbidden or handled. Note that
9653      * we can't assert this earlier because the setup of fieldoffset for
9654      * banked registers has to be done first.
9655      */
9656     if (!(r2->type & ARM_CP_NO_RAW)) {
9657         assert(!raw_accessors_invalid(r2));
9658     }
9659 
9660     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9661 }
9662 
9663 
9664 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9665                                        const ARMCPRegInfo *r, void *opaque)
9666 {
9667     /*
9668      * Define implementations of coprocessor registers.
9669      * We store these in a hashtable because typically
9670      * there are less than 150 registers in a space which
9671      * is 16*16*16*8*8 = 262144 in size.
9672      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9673      * If a register is defined twice then the second definition is
9674      * used, so this can be used to define some generic registers and
9675      * then override them with implementation specific variations.
9676      * At least one of the original and the second definition should
9677      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9678      * against accidental use.
9679      *
9680      * The state field defines whether the register is to be
9681      * visible in the AArch32 or AArch64 execution state. If the
9682      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9683      * reginfo structure for the AArch32 view, which sees the lower
9684      * 32 bits of the 64 bit register.
9685      *
9686      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9687      * be wildcarded. AArch64 registers are always considered to be 64
9688      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9689      * the register, if any.
9690      */
9691     int crm, opc1, opc2;
9692     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9693     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9694     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9695     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9696     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9697     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9698     CPState state;
9699 
9700     /* 64 bit registers have only CRm and Opc1 fields */
9701     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9702     /* op0 only exists in the AArch64 encodings */
9703     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9704     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9705     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9706     /*
9707      * This API is only for Arm's system coprocessors (14 and 15) or
9708      * (M-profile or v7A-and-earlier only) for implementation defined
9709      * coprocessors in the range 0..7.  Our decode assumes this, since
9710      * 8..13 can be used for other insns including VFP and Neon. See
9711      * valid_cp() in translate.c.  Assert here that we haven't tried
9712      * to use an invalid coprocessor number.
9713      */
9714     switch (r->state) {
9715     case ARM_CP_STATE_BOTH:
9716         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9717         if (r->cp == 0) {
9718             break;
9719         }
9720         /* fall through */
9721     case ARM_CP_STATE_AA32:
9722         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9723             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9724             assert(r->cp >= 14 && r->cp <= 15);
9725         } else {
9726             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9727         }
9728         break;
9729     case ARM_CP_STATE_AA64:
9730         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9731         break;
9732     default:
9733         g_assert_not_reached();
9734     }
9735     /*
9736      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9737      * encodes a minimum access level for the register. We roll this
9738      * runtime check into our general permission check code, so check
9739      * here that the reginfo's specified permissions are strict enough
9740      * to encompass the generic architectural permission check.
9741      */
9742     if (r->state != ARM_CP_STATE_AA32) {
9743         CPAccessRights mask;
9744         switch (r->opc1) {
9745         case 0:
9746             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9747             mask = PL0U_R | PL1_RW;
9748             break;
9749         case 1: case 2:
9750             /* min_EL EL1 */
9751             mask = PL1_RW;
9752             break;
9753         case 3:
9754             /* min_EL EL0 */
9755             mask = PL0_RW;
9756             break;
9757         case 4:
9758         case 5:
9759             /* min_EL EL2 */
9760             mask = PL2_RW;
9761             break;
9762         case 6:
9763             /* min_EL EL3 */
9764             mask = PL3_RW;
9765             break;
9766         case 7:
9767             /* min_EL EL1, secure mode only (we don't check the latter) */
9768             mask = PL1_RW;
9769             break;
9770         default:
9771             /* broken reginfo with out-of-range opc1 */
9772             g_assert_not_reached();
9773         }
9774         /* assert our permissions are not too lax (stricter is fine) */
9775         assert((r->access & ~mask) == 0);
9776     }
9777 
9778     /*
9779      * Check that the register definition has enough info to handle
9780      * reads and writes if they are permitted.
9781      */
9782     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9783         if (r->access & PL3_R) {
9784             assert((r->fieldoffset ||
9785                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9786                    r->readfn);
9787         }
9788         if (r->access & PL3_W) {
9789             assert((r->fieldoffset ||
9790                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9791                    r->writefn);
9792         }
9793     }
9794 
9795     for (crm = crmmin; crm <= crmmax; crm++) {
9796         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9797             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9798                 for (state = ARM_CP_STATE_AA32;
9799                      state <= ARM_CP_STATE_AA64; state++) {
9800                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9801                         continue;
9802                     }
9803                     if (state == ARM_CP_STATE_AA32) {
9804                         /*
9805                          * Under AArch32 CP registers can be common
9806                          * (same for secure and non-secure world) or banked.
9807                          */
9808                         char *name;
9809 
9810                         switch (r->secure) {
9811                         case ARM_CP_SECSTATE_S:
9812                         case ARM_CP_SECSTATE_NS:
9813                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9814                                                    r->secure, crm, opc1, opc2,
9815                                                    r->name);
9816                             break;
9817                         case ARM_CP_SECSTATE_BOTH:
9818                             name = g_strdup_printf("%s_S", r->name);
9819                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9820                                                    ARM_CP_SECSTATE_S,
9821                                                    crm, opc1, opc2, name);
9822                             g_free(name);
9823                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9824                                                    ARM_CP_SECSTATE_NS,
9825                                                    crm, opc1, opc2, r->name);
9826                             break;
9827                         default:
9828                             g_assert_not_reached();
9829                         }
9830                     } else {
9831                         /*
9832                          * AArch64 registers get mapped to non-secure instance
9833                          * of AArch32
9834                          */
9835                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9836                                                ARM_CP_SECSTATE_NS,
9837                                                crm, opc1, opc2, r->name);
9838                     }
9839                 }
9840             }
9841         }
9842     }
9843 }
9844 
9845 /* Define a whole list of registers */
9846 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9847                                         void *opaque, size_t len)
9848 {
9849     size_t i;
9850     for (i = 0; i < len; ++i) {
9851         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9852     }
9853 }
9854 
9855 /*
9856  * Modify ARMCPRegInfo for access from userspace.
9857  *
9858  * This is a data driven modification directed by
9859  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9860  * user-space cannot alter any values and dynamic values pertaining to
9861  * execution state are hidden from user space view anyway.
9862  */
9863 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9864                                  const ARMCPRegUserSpaceInfo *mods,
9865                                  size_t mods_len)
9866 {
9867     for (size_t mi = 0; mi < mods_len; ++mi) {
9868         const ARMCPRegUserSpaceInfo *m = mods + mi;
9869         GPatternSpec *pat = NULL;
9870 
9871         if (m->is_glob) {
9872             pat = g_pattern_spec_new(m->name);
9873         }
9874         for (size_t ri = 0; ri < regs_len; ++ri) {
9875             ARMCPRegInfo *r = regs + ri;
9876 
9877             if (pat && g_pattern_match_string(pat, r->name)) {
9878                 r->type = ARM_CP_CONST;
9879                 r->access = PL0U_R;
9880                 r->resetvalue = 0;
9881                 /* continue */
9882             } else if (strcmp(r->name, m->name) == 0) {
9883                 r->type = ARM_CP_CONST;
9884                 r->access = PL0U_R;
9885                 r->resetvalue &= m->exported_bits;
9886                 r->resetvalue |= m->fixed_bits;
9887                 break;
9888             }
9889         }
9890         if (pat) {
9891             g_pattern_spec_free(pat);
9892         }
9893     }
9894 }
9895 
9896 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9897 {
9898     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9899 }
9900 
9901 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9902                          uint64_t value)
9903 {
9904     /* Helper coprocessor write function for write-ignore registers */
9905 }
9906 
9907 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9908 {
9909     /* Helper coprocessor write function for read-as-zero registers */
9910     return 0;
9911 }
9912 
9913 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9914 {
9915     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9916 }
9917 
9918 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9919 {
9920     /*
9921      * Return true if it is not valid for us to switch to
9922      * this CPU mode (ie all the UNPREDICTABLE cases in
9923      * the ARM ARM CPSRWriteByInstr pseudocode).
9924      */
9925 
9926     /* Changes to or from Hyp via MSR and CPS are illegal. */
9927     if (write_type == CPSRWriteByInstr &&
9928         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9929          mode == ARM_CPU_MODE_HYP)) {
9930         return 1;
9931     }
9932 
9933     switch (mode) {
9934     case ARM_CPU_MODE_USR:
9935         return 0;
9936     case ARM_CPU_MODE_SYS:
9937     case ARM_CPU_MODE_SVC:
9938     case ARM_CPU_MODE_ABT:
9939     case ARM_CPU_MODE_UND:
9940     case ARM_CPU_MODE_IRQ:
9941     case ARM_CPU_MODE_FIQ:
9942         /*
9943          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9944          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9945          */
9946         /*
9947          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9948          * and CPS are treated as illegal mode changes.
9949          */
9950         if (write_type == CPSRWriteByInstr &&
9951             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9952             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9953             return 1;
9954         }
9955         return 0;
9956     case ARM_CPU_MODE_HYP:
9957         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9958     case ARM_CPU_MODE_MON:
9959         return arm_current_el(env) < 3;
9960     default:
9961         return 1;
9962     }
9963 }
9964 
9965 uint32_t cpsr_read(CPUARMState *env)
9966 {
9967     int ZF;
9968     ZF = (env->ZF == 0);
9969     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9970         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9971         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9972         | ((env->condexec_bits & 0xfc) << 8)
9973         | (env->GE << 16) | (env->daif & CPSR_AIF);
9974 }
9975 
9976 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9977                 CPSRWriteType write_type)
9978 {
9979     uint32_t changed_daif;
9980     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9981         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9982 
9983     if (mask & CPSR_NZCV) {
9984         env->ZF = (~val) & CPSR_Z;
9985         env->NF = val;
9986         env->CF = (val >> 29) & 1;
9987         env->VF = (val << 3) & 0x80000000;
9988     }
9989     if (mask & CPSR_Q) {
9990         env->QF = ((val & CPSR_Q) != 0);
9991     }
9992     if (mask & CPSR_T) {
9993         env->thumb = ((val & CPSR_T) != 0);
9994     }
9995     if (mask & CPSR_IT_0_1) {
9996         env->condexec_bits &= ~3;
9997         env->condexec_bits |= (val >> 25) & 3;
9998     }
9999     if (mask & CPSR_IT_2_7) {
10000         env->condexec_bits &= 3;
10001         env->condexec_bits |= (val >> 8) & 0xfc;
10002     }
10003     if (mask & CPSR_GE) {
10004         env->GE = (val >> 16) & 0xf;
10005     }
10006 
10007     /*
10008      * In a V7 implementation that includes the security extensions but does
10009      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10010      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10011      * bits respectively.
10012      *
10013      * In a V8 implementation, it is permitted for privileged software to
10014      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10015      */
10016     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
10017         arm_feature(env, ARM_FEATURE_EL3) &&
10018         !arm_feature(env, ARM_FEATURE_EL2) &&
10019         !arm_is_secure(env)) {
10020 
10021         changed_daif = (env->daif ^ val) & mask;
10022 
10023         if (changed_daif & CPSR_A) {
10024             /*
10025              * Check to see if we are allowed to change the masking of async
10026              * abort exceptions from a non-secure state.
10027              */
10028             if (!(env->cp15.scr_el3 & SCR_AW)) {
10029                 qemu_log_mask(LOG_GUEST_ERROR,
10030                               "Ignoring attempt to switch CPSR_A flag from "
10031                               "non-secure world with SCR.AW bit clear\n");
10032                 mask &= ~CPSR_A;
10033             }
10034         }
10035 
10036         if (changed_daif & CPSR_F) {
10037             /*
10038              * Check to see if we are allowed to change the masking of FIQ
10039              * exceptions from a non-secure state.
10040              */
10041             if (!(env->cp15.scr_el3 & SCR_FW)) {
10042                 qemu_log_mask(LOG_GUEST_ERROR,
10043                               "Ignoring attempt to switch CPSR_F flag from "
10044                               "non-secure world with SCR.FW bit clear\n");
10045                 mask &= ~CPSR_F;
10046             }
10047 
10048             /*
10049              * Check whether non-maskable FIQ (NMFI) support is enabled.
10050              * If this bit is set software is not allowed to mask
10051              * FIQs, but is allowed to set CPSR_F to 0.
10052              */
10053             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10054                 (val & CPSR_F)) {
10055                 qemu_log_mask(LOG_GUEST_ERROR,
10056                               "Ignoring attempt to enable CPSR_F flag "
10057                               "(non-maskable FIQ [NMFI] support enabled)\n");
10058                 mask &= ~CPSR_F;
10059             }
10060         }
10061     }
10062 
10063     env->daif &= ~(CPSR_AIF & mask);
10064     env->daif |= val & CPSR_AIF & mask;
10065 
10066     if (write_type != CPSRWriteRaw &&
10067         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
10068         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
10069             /*
10070              * Note that we can only get here in USR mode if this is a
10071              * gdb stub write; for this case we follow the architectural
10072              * behaviour for guest writes in USR mode of ignoring an attempt
10073              * to switch mode. (Those are caught by translate.c for writes
10074              * triggered by guest instructions.)
10075              */
10076             mask &= ~CPSR_M;
10077         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
10078             /*
10079              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
10080              * v7, and has defined behaviour in v8:
10081              *  + leave CPSR.M untouched
10082              *  + allow changes to the other CPSR fields
10083              *  + set PSTATE.IL
10084              * For user changes via the GDB stub, we don't set PSTATE.IL,
10085              * as this would be unnecessarily harsh for a user error.
10086              */
10087             mask &= ~CPSR_M;
10088             if (write_type != CPSRWriteByGDBStub &&
10089                 arm_feature(env, ARM_FEATURE_V8)) {
10090                 mask |= CPSR_IL;
10091                 val |= CPSR_IL;
10092             }
10093             qemu_log_mask(LOG_GUEST_ERROR,
10094                           "Illegal AArch32 mode switch attempt from %s to %s\n",
10095                           aarch32_mode_name(env->uncached_cpsr),
10096                           aarch32_mode_name(val));
10097         } else {
10098             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10099                           write_type == CPSRWriteExceptionReturn ?
10100                           "Exception return from AArch32" :
10101                           "AArch32 mode switch from",
10102                           aarch32_mode_name(env->uncached_cpsr),
10103                           aarch32_mode_name(val), env->regs[15]);
10104             switch_mode(env, val & CPSR_M);
10105         }
10106     }
10107     mask &= ~CACHED_CPSR_BITS;
10108     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
10109     if (tcg_enabled() && rebuild_hflags) {
10110         arm_rebuild_hflags(env);
10111     }
10112 }
10113 
10114 /* Sign/zero extend */
10115 uint32_t HELPER(sxtb16)(uint32_t x)
10116 {
10117     uint32_t res;
10118     res = (uint16_t)(int8_t)x;
10119     res |= (uint32_t)(int8_t)(x >> 16) << 16;
10120     return res;
10121 }
10122 
10123 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
10124 {
10125     /*
10126      * Take a division-by-zero exception if necessary; otherwise return
10127      * to get the usual non-trapping division behaviour (result of 0)
10128      */
10129     if (arm_feature(env, ARM_FEATURE_M)
10130         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
10131         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
10132     }
10133 }
10134 
10135 uint32_t HELPER(uxtb16)(uint32_t x)
10136 {
10137     uint32_t res;
10138     res = (uint16_t)(uint8_t)x;
10139     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
10140     return res;
10141 }
10142 
10143 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
10144 {
10145     if (den == 0) {
10146         handle_possible_div0_trap(env, GETPC());
10147         return 0;
10148     }
10149     if (num == INT_MIN && den == -1) {
10150         return INT_MIN;
10151     }
10152     return num / den;
10153 }
10154 
10155 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
10156 {
10157     if (den == 0) {
10158         handle_possible_div0_trap(env, GETPC());
10159         return 0;
10160     }
10161     return num / den;
10162 }
10163 
10164 uint32_t HELPER(rbit)(uint32_t x)
10165 {
10166     return revbit32(x);
10167 }
10168 
10169 #ifdef CONFIG_USER_ONLY
10170 
10171 static void switch_mode(CPUARMState *env, int mode)
10172 {
10173     ARMCPU *cpu = env_archcpu(env);
10174 
10175     if (mode != ARM_CPU_MODE_USR) {
10176         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10177     }
10178 }
10179 
10180 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10181                                  uint32_t cur_el, bool secure)
10182 {
10183     return 1;
10184 }
10185 
10186 void aarch64_sync_64_to_32(CPUARMState *env)
10187 {
10188     g_assert_not_reached();
10189 }
10190 
10191 #else
10192 
10193 static void switch_mode(CPUARMState *env, int mode)
10194 {
10195     int old_mode;
10196     int i;
10197 
10198     old_mode = env->uncached_cpsr & CPSR_M;
10199     if (mode == old_mode) {
10200         return;
10201     }
10202 
10203     if (old_mode == ARM_CPU_MODE_FIQ) {
10204         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10205         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
10206     } else if (mode == ARM_CPU_MODE_FIQ) {
10207         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10208         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
10209     }
10210 
10211     i = bank_number(old_mode);
10212     env->banked_r13[i] = env->regs[13];
10213     env->banked_spsr[i] = env->spsr;
10214 
10215     i = bank_number(mode);
10216     env->regs[13] = env->banked_r13[i];
10217     env->spsr = env->banked_spsr[i];
10218 
10219     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10220     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
10221 }
10222 
10223 /*
10224  * Physical Interrupt Target EL Lookup Table
10225  *
10226  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10227  *
10228  * The below multi-dimensional table is used for looking up the target
10229  * exception level given numerous condition criteria.  Specifically, the
10230  * target EL is based on SCR and HCR routing controls as well as the
10231  * currently executing EL and secure state.
10232  *
10233  *    Dimensions:
10234  *    target_el_table[2][2][2][2][2][4]
10235  *                    |  |  |  |  |  +--- Current EL
10236  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
10237  *                    |  |  |  +--------- HCR mask override
10238  *                    |  |  +------------ SCR exec state control
10239  *                    |  +--------------- SCR mask override
10240  *                    +------------------ 32-bit(0)/64-bit(1) EL3
10241  *
10242  *    The table values are as such:
10243  *    0-3 = EL0-EL3
10244  *     -1 = Cannot occur
10245  *
10246  * The ARM ARM target EL table includes entries indicating that an "exception
10247  * is not taken".  The two cases where this is applicable are:
10248  *    1) An exception is taken from EL3 but the SCR does not have the exception
10249  *    routed to EL3.
10250  *    2) An exception is taken from EL2 but the HCR does not have the exception
10251  *    routed to EL2.
10252  * In these two cases, the below table contain a target of EL1.  This value is
10253  * returned as it is expected that the consumer of the table data will check
10254  * for "target EL >= current EL" to ensure the exception is not taken.
10255  *
10256  *            SCR     HCR
10257  *         64  EA     AMO                 From
10258  *        BIT IRQ     IMO      Non-secure         Secure
10259  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
10260  */
10261 static const int8_t target_el_table[2][2][2][2][2][4] = {
10262     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10263        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
10264       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
10265        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
10266      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10267        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
10268       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
10269        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
10270     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
10271        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
10272       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
10273        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
10274      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
10275        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
10276       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
10277        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
10278 };
10279 
10280 /*
10281  * Determine the target EL for physical exceptions
10282  */
10283 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10284                                  uint32_t cur_el, bool secure)
10285 {
10286     CPUARMState *env = cpu_env(cs);
10287     bool rw;
10288     bool scr;
10289     bool hcr;
10290     int target_el;
10291     /* Is the highest EL AArch64? */
10292     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10293     uint64_t hcr_el2;
10294 
10295     if (arm_feature(env, ARM_FEATURE_EL3)) {
10296         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10297     } else {
10298         /*
10299          * Either EL2 is the highest EL (and so the EL2 register width
10300          * is given by is64); or there is no EL2 or EL3, in which case
10301          * the value of 'rw' does not affect the table lookup anyway.
10302          */
10303         rw = is64;
10304     }
10305 
10306     hcr_el2 = arm_hcr_el2_eff(env);
10307     switch (excp_idx) {
10308     case EXCP_IRQ:
10309         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
10310         hcr = hcr_el2 & HCR_IMO;
10311         break;
10312     case EXCP_FIQ:
10313         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
10314         hcr = hcr_el2 & HCR_FMO;
10315         break;
10316     default:
10317         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
10318         hcr = hcr_el2 & HCR_AMO;
10319         break;
10320     };
10321 
10322     /*
10323      * For these purposes, TGE and AMO/IMO/FMO both force the
10324      * interrupt to EL2.  Fold TGE into the bit extracted above.
10325      */
10326     hcr |= (hcr_el2 & HCR_TGE) != 0;
10327 
10328     /* Perform a table-lookup for the target EL given the current state */
10329     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10330 
10331     assert(target_el > 0);
10332 
10333     return target_el;
10334 }
10335 
10336 void arm_log_exception(CPUState *cs)
10337 {
10338     int idx = cs->exception_index;
10339 
10340     if (qemu_loglevel_mask(CPU_LOG_INT)) {
10341         const char *exc = NULL;
10342         static const char * const excnames[] = {
10343             [EXCP_UDEF] = "Undefined Instruction",
10344             [EXCP_SWI] = "SVC",
10345             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10346             [EXCP_DATA_ABORT] = "Data Abort",
10347             [EXCP_IRQ] = "IRQ",
10348             [EXCP_FIQ] = "FIQ",
10349             [EXCP_BKPT] = "Breakpoint",
10350             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10351             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10352             [EXCP_HVC] = "Hypervisor Call",
10353             [EXCP_HYP_TRAP] = "Hypervisor Trap",
10354             [EXCP_SMC] = "Secure Monitor Call",
10355             [EXCP_VIRQ] = "Virtual IRQ",
10356             [EXCP_VFIQ] = "Virtual FIQ",
10357             [EXCP_SEMIHOST] = "Semihosting call",
10358             [EXCP_NOCP] = "v7M NOCP UsageFault",
10359             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10360             [EXCP_STKOF] = "v8M STKOF UsageFault",
10361             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10362             [EXCP_LSERR] = "v8M LSERR UsageFault",
10363             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
10364             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
10365             [EXCP_VSERR] = "Virtual SERR",
10366             [EXCP_GPC] = "Granule Protection Check",
10367         };
10368 
10369         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10370             exc = excnames[idx];
10371         }
10372         if (!exc) {
10373             exc = "unknown";
10374         }
10375         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10376                       idx, exc, cs->cpu_index);
10377     }
10378 }
10379 
10380 /*
10381  * Function used to synchronize QEMU's AArch64 register set with AArch32
10382  * register set.  This is necessary when switching between AArch32 and AArch64
10383  * execution state.
10384  */
10385 void aarch64_sync_32_to_64(CPUARMState *env)
10386 {
10387     int i;
10388     uint32_t mode = env->uncached_cpsr & CPSR_M;
10389 
10390     /* We can blanket copy R[0:7] to X[0:7] */
10391     for (i = 0; i < 8; i++) {
10392         env->xregs[i] = env->regs[i];
10393     }
10394 
10395     /*
10396      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10397      * Otherwise, they come from the banked user regs.
10398      */
10399     if (mode == ARM_CPU_MODE_FIQ) {
10400         for (i = 8; i < 13; i++) {
10401             env->xregs[i] = env->usr_regs[i - 8];
10402         }
10403     } else {
10404         for (i = 8; i < 13; i++) {
10405             env->xregs[i] = env->regs[i];
10406         }
10407     }
10408 
10409     /*
10410      * Registers x13-x23 are the various mode SP and FP registers. Registers
10411      * r13 and r14 are only copied if we are in that mode, otherwise we copy
10412      * from the mode banked register.
10413      */
10414     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10415         env->xregs[13] = env->regs[13];
10416         env->xregs[14] = env->regs[14];
10417     } else {
10418         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10419         /* HYP is an exception in that it is copied from r14 */
10420         if (mode == ARM_CPU_MODE_HYP) {
10421             env->xregs[14] = env->regs[14];
10422         } else {
10423             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
10424         }
10425     }
10426 
10427     if (mode == ARM_CPU_MODE_HYP) {
10428         env->xregs[15] = env->regs[13];
10429     } else {
10430         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
10431     }
10432 
10433     if (mode == ARM_CPU_MODE_IRQ) {
10434         env->xregs[16] = env->regs[14];
10435         env->xregs[17] = env->regs[13];
10436     } else {
10437         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10438         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10439     }
10440 
10441     if (mode == ARM_CPU_MODE_SVC) {
10442         env->xregs[18] = env->regs[14];
10443         env->xregs[19] = env->regs[13];
10444     } else {
10445         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10446         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10447     }
10448 
10449     if (mode == ARM_CPU_MODE_ABT) {
10450         env->xregs[20] = env->regs[14];
10451         env->xregs[21] = env->regs[13];
10452     } else {
10453         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10454         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10455     }
10456 
10457     if (mode == ARM_CPU_MODE_UND) {
10458         env->xregs[22] = env->regs[14];
10459         env->xregs[23] = env->regs[13];
10460     } else {
10461         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10462         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10463     }
10464 
10465     /*
10466      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10467      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10468      * FIQ bank for r8-r14.
10469      */
10470     if (mode == ARM_CPU_MODE_FIQ) {
10471         for (i = 24; i < 31; i++) {
10472             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10473         }
10474     } else {
10475         for (i = 24; i < 29; i++) {
10476             env->xregs[i] = env->fiq_regs[i - 24];
10477         }
10478         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10479         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10480     }
10481 
10482     env->pc = env->regs[15];
10483 }
10484 
10485 /*
10486  * Function used to synchronize QEMU's AArch32 register set with AArch64
10487  * register set.  This is necessary when switching between AArch32 and AArch64
10488  * execution state.
10489  */
10490 void aarch64_sync_64_to_32(CPUARMState *env)
10491 {
10492     int i;
10493     uint32_t mode = env->uncached_cpsr & CPSR_M;
10494 
10495     /* We can blanket copy X[0:7] to R[0:7] */
10496     for (i = 0; i < 8; i++) {
10497         env->regs[i] = env->xregs[i];
10498     }
10499 
10500     /*
10501      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10502      * Otherwise, we copy x8-x12 into the banked user regs.
10503      */
10504     if (mode == ARM_CPU_MODE_FIQ) {
10505         for (i = 8; i < 13; i++) {
10506             env->usr_regs[i - 8] = env->xregs[i];
10507         }
10508     } else {
10509         for (i = 8; i < 13; i++) {
10510             env->regs[i] = env->xregs[i];
10511         }
10512     }
10513 
10514     /*
10515      * Registers r13 & r14 depend on the current mode.
10516      * If we are in a given mode, we copy the corresponding x registers to r13
10517      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10518      * for the mode.
10519      */
10520     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10521         env->regs[13] = env->xregs[13];
10522         env->regs[14] = env->xregs[14];
10523     } else {
10524         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10525 
10526         /*
10527          * HYP is an exception in that it does not have its own banked r14 but
10528          * shares the USR r14
10529          */
10530         if (mode == ARM_CPU_MODE_HYP) {
10531             env->regs[14] = env->xregs[14];
10532         } else {
10533             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10534         }
10535     }
10536 
10537     if (mode == ARM_CPU_MODE_HYP) {
10538         env->regs[13] = env->xregs[15];
10539     } else {
10540         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10541     }
10542 
10543     if (mode == ARM_CPU_MODE_IRQ) {
10544         env->regs[14] = env->xregs[16];
10545         env->regs[13] = env->xregs[17];
10546     } else {
10547         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10548         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10549     }
10550 
10551     if (mode == ARM_CPU_MODE_SVC) {
10552         env->regs[14] = env->xregs[18];
10553         env->regs[13] = env->xregs[19];
10554     } else {
10555         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10556         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10557     }
10558 
10559     if (mode == ARM_CPU_MODE_ABT) {
10560         env->regs[14] = env->xregs[20];
10561         env->regs[13] = env->xregs[21];
10562     } else {
10563         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10564         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10565     }
10566 
10567     if (mode == ARM_CPU_MODE_UND) {
10568         env->regs[14] = env->xregs[22];
10569         env->regs[13] = env->xregs[23];
10570     } else {
10571         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10572         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10573     }
10574 
10575     /*
10576      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10577      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10578      * FIQ bank for r8-r14.
10579      */
10580     if (mode == ARM_CPU_MODE_FIQ) {
10581         for (i = 24; i < 31; i++) {
10582             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10583         }
10584     } else {
10585         for (i = 24; i < 29; i++) {
10586             env->fiq_regs[i - 24] = env->xregs[i];
10587         }
10588         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10589         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10590     }
10591 
10592     env->regs[15] = env->pc;
10593 }
10594 
10595 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10596                                    uint32_t mask, uint32_t offset,
10597                                    uint32_t newpc)
10598 {
10599     int new_el;
10600 
10601     /* Change the CPU state so as to actually take the exception. */
10602     switch_mode(env, new_mode);
10603 
10604     /*
10605      * For exceptions taken to AArch32 we must clear the SS bit in both
10606      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10607      */
10608     env->pstate &= ~PSTATE_SS;
10609     env->spsr = cpsr_read(env);
10610     /* Clear IT bits.  */
10611     env->condexec_bits = 0;
10612     /* Switch to the new mode, and to the correct instruction set.  */
10613     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10614 
10615     /* This must be after mode switching. */
10616     new_el = arm_current_el(env);
10617 
10618     /* Set new mode endianness */
10619     env->uncached_cpsr &= ~CPSR_E;
10620     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10621         env->uncached_cpsr |= CPSR_E;
10622     }
10623     /* J and IL must always be cleared for exception entry */
10624     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10625     env->daif |= mask;
10626 
10627     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10628         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10629             env->uncached_cpsr |= CPSR_SSBS;
10630         } else {
10631             env->uncached_cpsr &= ~CPSR_SSBS;
10632         }
10633     }
10634 
10635     if (new_mode == ARM_CPU_MODE_HYP) {
10636         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10637         env->elr_el[2] = env->regs[15];
10638     } else {
10639         /* CPSR.PAN is normally preserved preserved unless...  */
10640         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10641             switch (new_el) {
10642             case 3:
10643                 if (!arm_is_secure_below_el3(env)) {
10644                     /* ... the target is EL3, from non-secure state.  */
10645                     env->uncached_cpsr &= ~CPSR_PAN;
10646                     break;
10647                 }
10648                 /* ... the target is EL3, from secure state ... */
10649                 /* fall through */
10650             case 1:
10651                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10652                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10653                     env->uncached_cpsr |= CPSR_PAN;
10654                 }
10655                 break;
10656             }
10657         }
10658         /*
10659          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10660          * and we should just guard the thumb mode on V4
10661          */
10662         if (arm_feature(env, ARM_FEATURE_V4T)) {
10663             env->thumb =
10664                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10665         }
10666         env->regs[14] = env->regs[15] + offset;
10667     }
10668     env->regs[15] = newpc;
10669 
10670     if (tcg_enabled()) {
10671         arm_rebuild_hflags(env);
10672     }
10673 }
10674 
10675 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10676 {
10677     /*
10678      * Handle exception entry to Hyp mode; this is sufficiently
10679      * different to entry to other AArch32 modes that we handle it
10680      * separately here.
10681      *
10682      * The vector table entry used is always the 0x14 Hyp mode entry point,
10683      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10684      * The offset applied to the preferred return address is always zero
10685      * (see DDI0487C.a section G1.12.3).
10686      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10687      */
10688     uint32_t addr, mask;
10689     ARMCPU *cpu = ARM_CPU(cs);
10690     CPUARMState *env = &cpu->env;
10691 
10692     switch (cs->exception_index) {
10693     case EXCP_UDEF:
10694         addr = 0x04;
10695         break;
10696     case EXCP_SWI:
10697         addr = 0x08;
10698         break;
10699     case EXCP_BKPT:
10700         /* Fall through to prefetch abort.  */
10701     case EXCP_PREFETCH_ABORT:
10702         env->cp15.ifar_s = env->exception.vaddress;
10703         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10704                       (uint32_t)env->exception.vaddress);
10705         addr = 0x0c;
10706         break;
10707     case EXCP_DATA_ABORT:
10708         env->cp15.dfar_s = env->exception.vaddress;
10709         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10710                       (uint32_t)env->exception.vaddress);
10711         addr = 0x10;
10712         break;
10713     case EXCP_IRQ:
10714         addr = 0x18;
10715         break;
10716     case EXCP_FIQ:
10717         addr = 0x1c;
10718         break;
10719     case EXCP_HVC:
10720         addr = 0x08;
10721         break;
10722     case EXCP_HYP_TRAP:
10723         addr = 0x14;
10724         break;
10725     default:
10726         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10727     }
10728 
10729     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10730         if (!arm_feature(env, ARM_FEATURE_V8)) {
10731             /*
10732              * QEMU syndrome values are v8-style. v7 has the IL bit
10733              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10734              * If this is a v7 CPU, squash the IL bit in those cases.
10735              */
10736             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10737                 (cs->exception_index == EXCP_DATA_ABORT &&
10738                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10739                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10740                 env->exception.syndrome &= ~ARM_EL_IL;
10741             }
10742         }
10743         env->cp15.esr_el[2] = env->exception.syndrome;
10744     }
10745 
10746     if (arm_current_el(env) != 2 && addr < 0x14) {
10747         addr = 0x14;
10748     }
10749 
10750     mask = 0;
10751     if (!(env->cp15.scr_el3 & SCR_EA)) {
10752         mask |= CPSR_A;
10753     }
10754     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10755         mask |= CPSR_I;
10756     }
10757     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10758         mask |= CPSR_F;
10759     }
10760 
10761     addr += env->cp15.hvbar;
10762 
10763     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10764 }
10765 
10766 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10767 {
10768     ARMCPU *cpu = ARM_CPU(cs);
10769     CPUARMState *env = &cpu->env;
10770     uint32_t addr;
10771     uint32_t mask;
10772     int new_mode;
10773     uint32_t offset;
10774     uint32_t moe;
10775 
10776     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10777     switch (syn_get_ec(env->exception.syndrome)) {
10778     case EC_BREAKPOINT:
10779     case EC_BREAKPOINT_SAME_EL:
10780         moe = 1;
10781         break;
10782     case EC_WATCHPOINT:
10783     case EC_WATCHPOINT_SAME_EL:
10784         moe = 10;
10785         break;
10786     case EC_AA32_BKPT:
10787         moe = 3;
10788         break;
10789     case EC_VECTORCATCH:
10790         moe = 5;
10791         break;
10792     default:
10793         moe = 0;
10794         break;
10795     }
10796 
10797     if (moe) {
10798         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10799     }
10800 
10801     if (env->exception.target_el == 2) {
10802         arm_cpu_do_interrupt_aarch32_hyp(cs);
10803         return;
10804     }
10805 
10806     switch (cs->exception_index) {
10807     case EXCP_UDEF:
10808         new_mode = ARM_CPU_MODE_UND;
10809         addr = 0x04;
10810         mask = CPSR_I;
10811         if (env->thumb) {
10812             offset = 2;
10813         } else {
10814             offset = 4;
10815         }
10816         break;
10817     case EXCP_SWI:
10818         new_mode = ARM_CPU_MODE_SVC;
10819         addr = 0x08;
10820         mask = CPSR_I;
10821         /* The PC already points to the next instruction.  */
10822         offset = 0;
10823         break;
10824     case EXCP_BKPT:
10825         /* Fall through to prefetch abort.  */
10826     case EXCP_PREFETCH_ABORT:
10827         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10828         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10829         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10830                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10831         new_mode = ARM_CPU_MODE_ABT;
10832         addr = 0x0c;
10833         mask = CPSR_A | CPSR_I;
10834         offset = 4;
10835         break;
10836     case EXCP_DATA_ABORT:
10837         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10838         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10839         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10840                       env->exception.fsr,
10841                       (uint32_t)env->exception.vaddress);
10842         new_mode = ARM_CPU_MODE_ABT;
10843         addr = 0x10;
10844         mask = CPSR_A | CPSR_I;
10845         offset = 8;
10846         break;
10847     case EXCP_IRQ:
10848         new_mode = ARM_CPU_MODE_IRQ;
10849         addr = 0x18;
10850         /* Disable IRQ and imprecise data aborts.  */
10851         mask = CPSR_A | CPSR_I;
10852         offset = 4;
10853         if (env->cp15.scr_el3 & SCR_IRQ) {
10854             /* IRQ routed to monitor mode */
10855             new_mode = ARM_CPU_MODE_MON;
10856             mask |= CPSR_F;
10857         }
10858         break;
10859     case EXCP_FIQ:
10860         new_mode = ARM_CPU_MODE_FIQ;
10861         addr = 0x1c;
10862         /* Disable FIQ, IRQ and imprecise data aborts.  */
10863         mask = CPSR_A | CPSR_I | CPSR_F;
10864         if (env->cp15.scr_el3 & SCR_FIQ) {
10865             /* FIQ routed to monitor mode */
10866             new_mode = ARM_CPU_MODE_MON;
10867         }
10868         offset = 4;
10869         break;
10870     case EXCP_VIRQ:
10871         new_mode = ARM_CPU_MODE_IRQ;
10872         addr = 0x18;
10873         /* Disable IRQ and imprecise data aborts.  */
10874         mask = CPSR_A | CPSR_I;
10875         offset = 4;
10876         break;
10877     case EXCP_VFIQ:
10878         new_mode = ARM_CPU_MODE_FIQ;
10879         addr = 0x1c;
10880         /* Disable FIQ, IRQ and imprecise data aborts.  */
10881         mask = CPSR_A | CPSR_I | CPSR_F;
10882         offset = 4;
10883         break;
10884     case EXCP_VSERR:
10885         {
10886             /*
10887              * Note that this is reported as a data abort, but the DFAR
10888              * has an UNKNOWN value.  Construct the SError syndrome from
10889              * AET and ExT fields.
10890              */
10891             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10892 
10893             if (extended_addresses_enabled(env)) {
10894                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10895             } else {
10896                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10897             }
10898             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10899             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10900             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10901                           env->exception.fsr);
10902 
10903             new_mode = ARM_CPU_MODE_ABT;
10904             addr = 0x10;
10905             mask = CPSR_A | CPSR_I;
10906             offset = 8;
10907         }
10908         break;
10909     case EXCP_SMC:
10910         new_mode = ARM_CPU_MODE_MON;
10911         addr = 0x08;
10912         mask = CPSR_A | CPSR_I | CPSR_F;
10913         offset = 0;
10914         break;
10915     default:
10916         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10917         return; /* Never happens.  Keep compiler happy.  */
10918     }
10919 
10920     if (new_mode == ARM_CPU_MODE_MON) {
10921         addr += env->cp15.mvbar;
10922     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10923         /* High vectors. When enabled, base address cannot be remapped. */
10924         addr += 0xffff0000;
10925     } else {
10926         /*
10927          * ARM v7 architectures provide a vector base address register to remap
10928          * the interrupt vector table.
10929          * This register is only followed in non-monitor mode, and is banked.
10930          * Note: only bits 31:5 are valid.
10931          */
10932         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10933     }
10934 
10935     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10936         env->cp15.scr_el3 &= ~SCR_NS;
10937     }
10938 
10939     take_aarch32_exception(env, new_mode, mask, offset, addr);
10940 }
10941 
10942 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10943 {
10944     /*
10945      * Return the register number of the AArch64 view of the AArch32
10946      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10947      * be that of the AArch32 mode the exception came from.
10948      */
10949     int mode = env->uncached_cpsr & CPSR_M;
10950 
10951     switch (aarch32_reg) {
10952     case 0 ... 7:
10953         return aarch32_reg;
10954     case 8 ... 12:
10955         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10956     case 13:
10957         switch (mode) {
10958         case ARM_CPU_MODE_USR:
10959         case ARM_CPU_MODE_SYS:
10960             return 13;
10961         case ARM_CPU_MODE_HYP:
10962             return 15;
10963         case ARM_CPU_MODE_IRQ:
10964             return 17;
10965         case ARM_CPU_MODE_SVC:
10966             return 19;
10967         case ARM_CPU_MODE_ABT:
10968             return 21;
10969         case ARM_CPU_MODE_UND:
10970             return 23;
10971         case ARM_CPU_MODE_FIQ:
10972             return 29;
10973         default:
10974             g_assert_not_reached();
10975         }
10976     case 14:
10977         switch (mode) {
10978         case ARM_CPU_MODE_USR:
10979         case ARM_CPU_MODE_SYS:
10980         case ARM_CPU_MODE_HYP:
10981             return 14;
10982         case ARM_CPU_MODE_IRQ:
10983             return 16;
10984         case ARM_CPU_MODE_SVC:
10985             return 18;
10986         case ARM_CPU_MODE_ABT:
10987             return 20;
10988         case ARM_CPU_MODE_UND:
10989             return 22;
10990         case ARM_CPU_MODE_FIQ:
10991             return 30;
10992         default:
10993             g_assert_not_reached();
10994         }
10995     case 15:
10996         return 31;
10997     default:
10998         g_assert_not_reached();
10999     }
11000 }
11001 
11002 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11003 {
11004     uint32_t ret = cpsr_read(env);
11005 
11006     /* Move DIT to the correct location for SPSR_ELx */
11007     if (ret & CPSR_DIT) {
11008         ret &= ~CPSR_DIT;
11009         ret |= PSTATE_DIT;
11010     }
11011     /* Merge PSTATE.SS into SPSR_ELx */
11012     ret |= env->pstate & PSTATE_SS;
11013 
11014     return ret;
11015 }
11016 
11017 static bool syndrome_is_sync_extabt(uint32_t syndrome)
11018 {
11019     /* Return true if this syndrome value is a synchronous external abort */
11020     switch (syn_get_ec(syndrome)) {
11021     case EC_INSNABORT:
11022     case EC_INSNABORT_SAME_EL:
11023     case EC_DATAABORT:
11024     case EC_DATAABORT_SAME_EL:
11025         /* Look at fault status code for all the synchronous ext abort cases */
11026         switch (syndrome & 0x3f) {
11027         case 0x10:
11028         case 0x13:
11029         case 0x14:
11030         case 0x15:
11031         case 0x16:
11032         case 0x17:
11033             return true;
11034         default:
11035             return false;
11036         }
11037     default:
11038         return false;
11039     }
11040 }
11041 
11042 /* Handle exception entry to a target EL which is using AArch64 */
11043 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
11044 {
11045     ARMCPU *cpu = ARM_CPU(cs);
11046     CPUARMState *env = &cpu->env;
11047     unsigned int new_el = env->exception.target_el;
11048     target_ulong addr = env->cp15.vbar_el[new_el];
11049     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
11050     unsigned int old_mode;
11051     unsigned int cur_el = arm_current_el(env);
11052     int rt;
11053 
11054     if (tcg_enabled()) {
11055         /*
11056          * Note that new_el can never be 0.  If cur_el is 0, then
11057          * el0_a64 is is_a64(), else el0_a64 is ignored.
11058          */
11059         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11060     }
11061 
11062     if (cur_el < new_el) {
11063         /*
11064          * Entry vector offset depends on whether the implemented EL
11065          * immediately lower than the target level is using AArch32 or AArch64
11066          */
11067         bool is_aa64;
11068         uint64_t hcr;
11069 
11070         switch (new_el) {
11071         case 3:
11072             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11073             break;
11074         case 2:
11075             hcr = arm_hcr_el2_eff(env);
11076             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11077                 is_aa64 = (hcr & HCR_RW) != 0;
11078                 break;
11079             }
11080             /* fall through */
11081         case 1:
11082             is_aa64 = is_a64(env);
11083             break;
11084         default:
11085             g_assert_not_reached();
11086         }
11087 
11088         if (is_aa64) {
11089             addr += 0x400;
11090         } else {
11091             addr += 0x600;
11092         }
11093     } else if (pstate_read(env) & PSTATE_SP) {
11094         addr += 0x200;
11095     }
11096 
11097     switch (cs->exception_index) {
11098     case EXCP_GPC:
11099         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11100                       env->cp15.mfar_el3);
11101         /* fall through */
11102     case EXCP_PREFETCH_ABORT:
11103     case EXCP_DATA_ABORT:
11104         /*
11105          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11106          * to be taken to the SError vector entrypoint.
11107          */
11108         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11109             syndrome_is_sync_extabt(env->exception.syndrome)) {
11110             addr += 0x180;
11111         }
11112         env->cp15.far_el[new_el] = env->exception.vaddress;
11113         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11114                       env->cp15.far_el[new_el]);
11115         /* fall through */
11116     case EXCP_BKPT:
11117     case EXCP_UDEF:
11118     case EXCP_SWI:
11119     case EXCP_HVC:
11120     case EXCP_HYP_TRAP:
11121     case EXCP_SMC:
11122         switch (syn_get_ec(env->exception.syndrome)) {
11123         case EC_ADVSIMDFPACCESSTRAP:
11124             /*
11125              * QEMU internal FP/SIMD syndromes from AArch32 include the
11126              * TA and coproc fields which are only exposed if the exception
11127              * is taken to AArch32 Hyp mode. Mask them out to get a valid
11128              * AArch64 format syndrome.
11129              */
11130             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
11131             break;
11132         case EC_CP14RTTRAP:
11133         case EC_CP15RTTRAP:
11134         case EC_CP14DTTRAP:
11135             /*
11136              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11137              * the raw register field from the insn; when taking this to
11138              * AArch64 we must convert it to the AArch64 view of the register
11139              * number. Notice that we read a 4-bit AArch32 register number and
11140              * write back a 5-bit AArch64 one.
11141              */
11142             rt = extract32(env->exception.syndrome, 5, 4);
11143             rt = aarch64_regnum(env, rt);
11144             env->exception.syndrome = deposit32(env->exception.syndrome,
11145                                                 5, 5, rt);
11146             break;
11147         case EC_CP15RRTTRAP:
11148         case EC_CP14RRTTRAP:
11149             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11150             rt = extract32(env->exception.syndrome, 5, 4);
11151             rt = aarch64_regnum(env, rt);
11152             env->exception.syndrome = deposit32(env->exception.syndrome,
11153                                                 5, 5, rt);
11154             rt = extract32(env->exception.syndrome, 10, 4);
11155             rt = aarch64_regnum(env, rt);
11156             env->exception.syndrome = deposit32(env->exception.syndrome,
11157                                                 10, 5, rt);
11158             break;
11159         }
11160         env->cp15.esr_el[new_el] = env->exception.syndrome;
11161         break;
11162     case EXCP_IRQ:
11163     case EXCP_VIRQ:
11164         addr += 0x80;
11165         break;
11166     case EXCP_FIQ:
11167     case EXCP_VFIQ:
11168         addr += 0x100;
11169         break;
11170     case EXCP_VSERR:
11171         addr += 0x180;
11172         /* Construct the SError syndrome from IDS and ISS fields. */
11173         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11174         env->cp15.esr_el[new_el] = env->exception.syndrome;
11175         break;
11176     default:
11177         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11178     }
11179 
11180     if (is_a64(env)) {
11181         old_mode = pstate_read(env);
11182         aarch64_save_sp(env, arm_current_el(env));
11183         env->elr_el[new_el] = env->pc;
11184     } else {
11185         old_mode = cpsr_read_for_spsr_elx(env);
11186         env->elr_el[new_el] = env->regs[15];
11187 
11188         aarch64_sync_32_to_64(env);
11189 
11190         env->condexec_bits = 0;
11191     }
11192     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11193 
11194     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11195                   env->elr_el[new_el]);
11196 
11197     if (cpu_isar_feature(aa64_pan, cpu)) {
11198         /* The value of PSTATE.PAN is normally preserved, except when ... */
11199         new_mode |= old_mode & PSTATE_PAN;
11200         switch (new_el) {
11201         case 2:
11202             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
11203             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11204                 != (HCR_E2H | HCR_TGE)) {
11205                 break;
11206             }
11207             /* fall through */
11208         case 1:
11209             /* ... the target is EL1 ... */
11210             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
11211             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11212                 new_mode |= PSTATE_PAN;
11213             }
11214             break;
11215         }
11216     }
11217     if (cpu_isar_feature(aa64_mte, cpu)) {
11218         new_mode |= PSTATE_TCO;
11219     }
11220 
11221     if (cpu_isar_feature(aa64_ssbs, cpu)) {
11222         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11223             new_mode |= PSTATE_SSBS;
11224         } else {
11225             new_mode &= ~PSTATE_SSBS;
11226         }
11227     }
11228 
11229     pstate_write(env, PSTATE_DAIF | new_mode);
11230     env->aarch64 = true;
11231     aarch64_restore_sp(env, new_el);
11232 
11233     if (tcg_enabled()) {
11234         helper_rebuild_hflags_a64(env, new_el);
11235     }
11236 
11237     env->pc = addr;
11238 
11239     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11240                   new_el, env->pc, pstate_read(env));
11241 }
11242 
11243 /*
11244  * Do semihosting call and set the appropriate return value. All the
11245  * permission and validity checks have been done at translate time.
11246  *
11247  * We only see semihosting exceptions in TCG only as they are not
11248  * trapped to the hypervisor in KVM.
11249  */
11250 #ifdef CONFIG_TCG
11251 static void tcg_handle_semihosting(CPUState *cs)
11252 {
11253     ARMCPU *cpu = ARM_CPU(cs);
11254     CPUARMState *env = &cpu->env;
11255 
11256     if (is_a64(env)) {
11257         qemu_log_mask(CPU_LOG_INT,
11258                       "...handling as semihosting call 0x%" PRIx64 "\n",
11259                       env->xregs[0]);
11260         do_common_semihosting(cs);
11261         env->pc += 4;
11262     } else {
11263         qemu_log_mask(CPU_LOG_INT,
11264                       "...handling as semihosting call 0x%x\n",
11265                       env->regs[0]);
11266         do_common_semihosting(cs);
11267         env->regs[15] += env->thumb ? 2 : 4;
11268     }
11269 }
11270 #endif
11271 
11272 /*
11273  * Handle a CPU exception for A and R profile CPUs.
11274  * Do any appropriate logging, handle PSCI calls, and then hand off
11275  * to the AArch64-entry or AArch32-entry function depending on the
11276  * target exception level's register width.
11277  *
11278  * Note: this is used for both TCG (as the do_interrupt tcg op),
11279  *       and KVM to re-inject guest debug exceptions, and to
11280  *       inject a Synchronous-External-Abort.
11281  */
11282 void arm_cpu_do_interrupt(CPUState *cs)
11283 {
11284     ARMCPU *cpu = ARM_CPU(cs);
11285     CPUARMState *env = &cpu->env;
11286     unsigned int new_el = env->exception.target_el;
11287 
11288     assert(!arm_feature(env, ARM_FEATURE_M));
11289 
11290     arm_log_exception(cs);
11291     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11292                   new_el);
11293     if (qemu_loglevel_mask(CPU_LOG_INT)
11294         && !excp_is_internal(cs->exception_index)) {
11295         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
11296                       syn_get_ec(env->exception.syndrome),
11297                       env->exception.syndrome);
11298     }
11299 
11300     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
11301         arm_handle_psci_call(cpu);
11302         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11303         return;
11304     }
11305 
11306     /*
11307      * Semihosting semantics depend on the register width of the code
11308      * that caused the exception, not the target exception level, so
11309      * must be handled here.
11310      */
11311 #ifdef CONFIG_TCG
11312     if (cs->exception_index == EXCP_SEMIHOST) {
11313         tcg_handle_semihosting(cs);
11314         return;
11315     }
11316 #endif
11317 
11318     /*
11319      * Hooks may change global state so BQL should be held, also the
11320      * BQL needs to be held for any modification of
11321      * cs->interrupt_request.
11322      */
11323     g_assert(qemu_mutex_iothread_locked());
11324 
11325     arm_call_pre_el_change_hook(cpu);
11326 
11327     assert(!excp_is_internal(cs->exception_index));
11328     if (arm_el_is_aa64(env, new_el)) {
11329         arm_cpu_do_interrupt_aarch64(cs);
11330     } else {
11331         arm_cpu_do_interrupt_aarch32(cs);
11332     }
11333 
11334     arm_call_el_change_hook(cpu);
11335 
11336     if (!kvm_enabled()) {
11337         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11338     }
11339 }
11340 #endif /* !CONFIG_USER_ONLY */
11341 
11342 uint64_t arm_sctlr(CPUARMState *env, int el)
11343 {
11344     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11345     if (el == 0) {
11346         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
11347         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
11348     }
11349     return env->cp15.sctlr_el[el];
11350 }
11351 
11352 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11353 {
11354     if (regime_has_2_ranges(mmu_idx)) {
11355         return extract64(tcr, 37, 2);
11356     } else if (regime_is_stage2(mmu_idx)) {
11357         return 0; /* VTCR_EL2 */
11358     } else {
11359         /* Replicate the single TBI bit so we always have 2 bits.  */
11360         return extract32(tcr, 20, 1) * 3;
11361     }
11362 }
11363 
11364 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11365 {
11366     if (regime_has_2_ranges(mmu_idx)) {
11367         return extract64(tcr, 51, 2);
11368     } else if (regime_is_stage2(mmu_idx)) {
11369         return 0; /* VTCR_EL2 */
11370     } else {
11371         /* Replicate the single TBID bit so we always have 2 bits.  */
11372         return extract32(tcr, 29, 1) * 3;
11373     }
11374 }
11375 
11376 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11377 {
11378     if (regime_has_2_ranges(mmu_idx)) {
11379         return extract64(tcr, 57, 2);
11380     } else {
11381         /* Replicate the single TCMA bit so we always have 2 bits.  */
11382         return extract32(tcr, 30, 1) * 3;
11383     }
11384 }
11385 
11386 static ARMGranuleSize tg0_to_gran_size(int tg)
11387 {
11388     switch (tg) {
11389     case 0:
11390         return Gran4K;
11391     case 1:
11392         return Gran64K;
11393     case 2:
11394         return Gran16K;
11395     default:
11396         return GranInvalid;
11397     }
11398 }
11399 
11400 static ARMGranuleSize tg1_to_gran_size(int tg)
11401 {
11402     switch (tg) {
11403     case 1:
11404         return Gran16K;
11405     case 2:
11406         return Gran4K;
11407     case 3:
11408         return Gran64K;
11409     default:
11410         return GranInvalid;
11411     }
11412 }
11413 
11414 static inline bool have4k(ARMCPU *cpu, bool stage2)
11415 {
11416     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11417         : cpu_isar_feature(aa64_tgran4, cpu);
11418 }
11419 
11420 static inline bool have16k(ARMCPU *cpu, bool stage2)
11421 {
11422     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11423         : cpu_isar_feature(aa64_tgran16, cpu);
11424 }
11425 
11426 static inline bool have64k(ARMCPU *cpu, bool stage2)
11427 {
11428     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11429         : cpu_isar_feature(aa64_tgran64, cpu);
11430 }
11431 
11432 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11433                                          bool stage2)
11434 {
11435     switch (gran) {
11436     case Gran4K:
11437         if (have4k(cpu, stage2)) {
11438             return gran;
11439         }
11440         break;
11441     case Gran16K:
11442         if (have16k(cpu, stage2)) {
11443             return gran;
11444         }
11445         break;
11446     case Gran64K:
11447         if (have64k(cpu, stage2)) {
11448             return gran;
11449         }
11450         break;
11451     case GranInvalid:
11452         break;
11453     }
11454     /*
11455      * If the guest selects a granule size that isn't implemented,
11456      * the architecture requires that we behave as if it selected one
11457      * that is (with an IMPDEF choice of which one to pick). We choose
11458      * to implement the smallest supported granule size.
11459      */
11460     if (have4k(cpu, stage2)) {
11461         return Gran4K;
11462     }
11463     if (have16k(cpu, stage2)) {
11464         return Gran16K;
11465     }
11466     assert(have64k(cpu, stage2));
11467     return Gran64K;
11468 }
11469 
11470 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11471                                    ARMMMUIdx mmu_idx, bool data,
11472                                    bool el1_is_aa32)
11473 {
11474     uint64_t tcr = regime_tcr(env, mmu_idx);
11475     bool epd, hpd, tsz_oob, ds, ha, hd;
11476     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11477     ARMGranuleSize gran;
11478     ARMCPU *cpu = env_archcpu(env);
11479     bool stage2 = regime_is_stage2(mmu_idx);
11480 
11481     if (!regime_has_2_ranges(mmu_idx)) {
11482         select = 0;
11483         tsz = extract32(tcr, 0, 6);
11484         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11485         if (stage2) {
11486             /* VTCR_EL2 */
11487             hpd = false;
11488         } else {
11489             hpd = extract32(tcr, 24, 1);
11490         }
11491         epd = false;
11492         sh = extract32(tcr, 12, 2);
11493         ps = extract32(tcr, 16, 3);
11494         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11495         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11496         ds = extract64(tcr, 32, 1);
11497     } else {
11498         bool e0pd;
11499 
11500         /*
11501          * Bit 55 is always between the two regions, and is canonical for
11502          * determining if address tagging is enabled.
11503          */
11504         select = extract64(va, 55, 1);
11505         if (!select) {
11506             tsz = extract32(tcr, 0, 6);
11507             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11508             epd = extract32(tcr, 7, 1);
11509             sh = extract32(tcr, 12, 2);
11510             hpd = extract64(tcr, 41, 1);
11511             e0pd = extract64(tcr, 55, 1);
11512         } else {
11513             tsz = extract32(tcr, 16, 6);
11514             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11515             epd = extract32(tcr, 23, 1);
11516             sh = extract32(tcr, 28, 2);
11517             hpd = extract64(tcr, 42, 1);
11518             e0pd = extract64(tcr, 56, 1);
11519         }
11520         ps = extract64(tcr, 32, 3);
11521         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11522         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11523         ds = extract64(tcr, 59, 1);
11524 
11525         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11526             regime_is_user(env, mmu_idx)) {
11527             epd = true;
11528         }
11529     }
11530 
11531     gran = sanitize_gran_size(cpu, gran, stage2);
11532 
11533     if (cpu_isar_feature(aa64_st, cpu)) {
11534         max_tsz = 48 - (gran == Gran64K);
11535     } else {
11536         max_tsz = 39;
11537     }
11538 
11539     /*
11540      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11541      * adjust the effective value of DS, as documented.
11542      */
11543     min_tsz = 16;
11544     if (gran == Gran64K) {
11545         if (cpu_isar_feature(aa64_lva, cpu)) {
11546             min_tsz = 12;
11547         }
11548         ds = false;
11549     } else if (ds) {
11550         if (regime_is_stage2(mmu_idx)) {
11551             if (gran == Gran16K) {
11552                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11553             } else {
11554                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11555             }
11556         } else {
11557             if (gran == Gran16K) {
11558                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11559             } else {
11560                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11561             }
11562         }
11563         if (ds) {
11564             min_tsz = 12;
11565         }
11566     }
11567 
11568     if (stage2 && el1_is_aa32) {
11569         /*
11570          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11571          * are loosened: a configured IPA of 40 bits is permitted even if
11572          * the implemented PA is less than that (and so a 40 bit IPA would
11573          * fault for an AArch64 EL1). See R_DTLMN.
11574          */
11575         min_tsz = MIN(min_tsz, 24);
11576     }
11577 
11578     if (tsz > max_tsz) {
11579         tsz = max_tsz;
11580         tsz_oob = true;
11581     } else if (tsz < min_tsz) {
11582         tsz = min_tsz;
11583         tsz_oob = true;
11584     } else {
11585         tsz_oob = false;
11586     }
11587 
11588     /* Present TBI as a composite with TBID.  */
11589     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11590     if (!data) {
11591         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11592     }
11593     tbi = (tbi >> select) & 1;
11594 
11595     return (ARMVAParameters) {
11596         .tsz = tsz,
11597         .ps = ps,
11598         .sh = sh,
11599         .select = select,
11600         .tbi = tbi,
11601         .epd = epd,
11602         .hpd = hpd,
11603         .tsz_oob = tsz_oob,
11604         .ds = ds,
11605         .ha = ha,
11606         .hd = ha && hd,
11607         .gran = gran,
11608     };
11609 }
11610 
11611 /*
11612  * Note that signed overflow is undefined in C.  The following routines are
11613  * careful to use unsigned types where modulo arithmetic is required.
11614  * Failure to do so _will_ break on newer gcc.
11615  */
11616 
11617 /* Signed saturating arithmetic.  */
11618 
11619 /* Perform 16-bit signed saturating addition.  */
11620 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11621 {
11622     uint16_t res;
11623 
11624     res = a + b;
11625     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11626         if (a & 0x8000) {
11627             res = 0x8000;
11628         } else {
11629             res = 0x7fff;
11630         }
11631     }
11632     return res;
11633 }
11634 
11635 /* Perform 8-bit signed saturating addition.  */
11636 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11637 {
11638     uint8_t res;
11639 
11640     res = a + b;
11641     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11642         if (a & 0x80) {
11643             res = 0x80;
11644         } else {
11645             res = 0x7f;
11646         }
11647     }
11648     return res;
11649 }
11650 
11651 /* Perform 16-bit signed saturating subtraction.  */
11652 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11653 {
11654     uint16_t res;
11655 
11656     res = a - b;
11657     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11658         if (a & 0x8000) {
11659             res = 0x8000;
11660         } else {
11661             res = 0x7fff;
11662         }
11663     }
11664     return res;
11665 }
11666 
11667 /* Perform 8-bit signed saturating subtraction.  */
11668 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11669 {
11670     uint8_t res;
11671 
11672     res = a - b;
11673     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11674         if (a & 0x80) {
11675             res = 0x80;
11676         } else {
11677             res = 0x7f;
11678         }
11679     }
11680     return res;
11681 }
11682 
11683 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11684 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11685 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
11686 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
11687 #define PFX q
11688 
11689 #include "op_addsub.h"
11690 
11691 /* Unsigned saturating arithmetic.  */
11692 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11693 {
11694     uint16_t res;
11695     res = a + b;
11696     if (res < a) {
11697         res = 0xffff;
11698     }
11699     return res;
11700 }
11701 
11702 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11703 {
11704     if (a > b) {
11705         return a - b;
11706     } else {
11707         return 0;
11708     }
11709 }
11710 
11711 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11712 {
11713     uint8_t res;
11714     res = a + b;
11715     if (res < a) {
11716         res = 0xff;
11717     }
11718     return res;
11719 }
11720 
11721 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11722 {
11723     if (a > b) {
11724         return a - b;
11725     } else {
11726         return 0;
11727     }
11728 }
11729 
11730 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11731 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11732 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
11733 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
11734 #define PFX uq
11735 
11736 #include "op_addsub.h"
11737 
11738 /* Signed modulo arithmetic.  */
11739 #define SARITH16(a, b, n, op) do { \
11740     int32_t sum; \
11741     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
11742     RESULT(sum, n, 16); \
11743     if (sum >= 0) \
11744         ge |= 3 << (n * 2); \
11745     } while (0)
11746 
11747 #define SARITH8(a, b, n, op) do { \
11748     int32_t sum; \
11749     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
11750     RESULT(sum, n, 8); \
11751     if (sum >= 0) \
11752         ge |= 1 << n; \
11753     } while (0)
11754 
11755 
11756 #define ADD16(a, b, n) SARITH16(a, b, n, +)
11757 #define SUB16(a, b, n) SARITH16(a, b, n, -)
11758 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
11759 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
11760 #define PFX s
11761 #define ARITH_GE
11762 
11763 #include "op_addsub.h"
11764 
11765 /* Unsigned modulo arithmetic.  */
11766 #define ADD16(a, b, n) do { \
11767     uint32_t sum; \
11768     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11769     RESULT(sum, n, 16); \
11770     if ((sum >> 16) == 1) \
11771         ge |= 3 << (n * 2); \
11772     } while (0)
11773 
11774 #define ADD8(a, b, n) do { \
11775     uint32_t sum; \
11776     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11777     RESULT(sum, n, 8); \
11778     if ((sum >> 8) == 1) \
11779         ge |= 1 << n; \
11780     } while (0)
11781 
11782 #define SUB16(a, b, n) do { \
11783     uint32_t sum; \
11784     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11785     RESULT(sum, n, 16); \
11786     if ((sum >> 16) == 0) \
11787         ge |= 3 << (n * 2); \
11788     } while (0)
11789 
11790 #define SUB8(a, b, n) do { \
11791     uint32_t sum; \
11792     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11793     RESULT(sum, n, 8); \
11794     if ((sum >> 8) == 0) \
11795         ge |= 1 << n; \
11796     } while (0)
11797 
11798 #define PFX u
11799 #define ARITH_GE
11800 
11801 #include "op_addsub.h"
11802 
11803 /* Halved signed arithmetic.  */
11804 #define ADD16(a, b, n) \
11805   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11806 #define SUB16(a, b, n) \
11807   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11808 #define ADD8(a, b, n) \
11809   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11810 #define SUB8(a, b, n) \
11811   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11812 #define PFX sh
11813 
11814 #include "op_addsub.h"
11815 
11816 /* Halved unsigned arithmetic.  */
11817 #define ADD16(a, b, n) \
11818   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11819 #define SUB16(a, b, n) \
11820   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11821 #define ADD8(a, b, n) \
11822   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11823 #define SUB8(a, b, n) \
11824   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11825 #define PFX uh
11826 
11827 #include "op_addsub.h"
11828 
11829 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11830 {
11831     if (a > b) {
11832         return a - b;
11833     } else {
11834         return b - a;
11835     }
11836 }
11837 
11838 /* Unsigned sum of absolute byte differences.  */
11839 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11840 {
11841     uint32_t sum;
11842     sum = do_usad(a, b);
11843     sum += do_usad(a >> 8, b >> 8);
11844     sum += do_usad(a >> 16, b >> 16);
11845     sum += do_usad(a >> 24, b >> 24);
11846     return sum;
11847 }
11848 
11849 /* For ARMv6 SEL instruction.  */
11850 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11851 {
11852     uint32_t mask;
11853 
11854     mask = 0;
11855     if (flags & 1) {
11856         mask |= 0xff;
11857     }
11858     if (flags & 2) {
11859         mask |= 0xff00;
11860     }
11861     if (flags & 4) {
11862         mask |= 0xff0000;
11863     }
11864     if (flags & 8) {
11865         mask |= 0xff000000;
11866     }
11867     return (a & mask) | (b & ~mask);
11868 }
11869 
11870 /*
11871  * CRC helpers.
11872  * The upper bytes of val (above the number specified by 'bytes') must have
11873  * been zeroed out by the caller.
11874  */
11875 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11876 {
11877     uint8_t buf[4];
11878 
11879     stl_le_p(buf, val);
11880 
11881     /* zlib crc32 converts the accumulator and output to one's complement.  */
11882     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11883 }
11884 
11885 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11886 {
11887     uint8_t buf[4];
11888 
11889     stl_le_p(buf, val);
11890 
11891     /* Linux crc32c converts the output to one's complement.  */
11892     return crc32c(acc, buf, bytes) ^ 0xffffffff;
11893 }
11894 
11895 /*
11896  * Return the exception level to which FP-disabled exceptions should
11897  * be taken, or 0 if FP is enabled.
11898  */
11899 int fp_exception_el(CPUARMState *env, int cur_el)
11900 {
11901 #ifndef CONFIG_USER_ONLY
11902     uint64_t hcr_el2;
11903 
11904     /*
11905      * CPACR and the CPTR registers don't exist before v6, so FP is
11906      * always accessible
11907      */
11908     if (!arm_feature(env, ARM_FEATURE_V6)) {
11909         return 0;
11910     }
11911 
11912     if (arm_feature(env, ARM_FEATURE_M)) {
11913         /* CPACR can cause a NOCP UsageFault taken to current security state */
11914         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11915             return 1;
11916         }
11917 
11918         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11919             if (!extract32(env->v7m.nsacr, 10, 1)) {
11920                 /* FP insns cause a NOCP UsageFault taken to Secure */
11921                 return 3;
11922             }
11923         }
11924 
11925         return 0;
11926     }
11927 
11928     hcr_el2 = arm_hcr_el2_eff(env);
11929 
11930     /*
11931      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11932      * 0, 2 : trap EL0 and EL1/PL1 accesses
11933      * 1    : trap only EL0 accesses
11934      * 3    : trap no accesses
11935      * This register is ignored if E2H+TGE are both set.
11936      */
11937     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11938         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11939 
11940         switch (fpen) {
11941         case 1:
11942             if (cur_el != 0) {
11943                 break;
11944             }
11945             /* fall through */
11946         case 0:
11947         case 2:
11948             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11949             if (!arm_el_is_aa64(env, 3)
11950                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11951                 return 3;
11952             }
11953             if (cur_el <= 1) {
11954                 return 1;
11955             }
11956             break;
11957         }
11958     }
11959 
11960     /*
11961      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11962      * to control non-secure access to the FPU. It doesn't have any
11963      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11964      */
11965     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11966          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11967         if (!extract32(env->cp15.nsacr, 10, 1)) {
11968             /* FP insns act as UNDEF */
11969             return cur_el == 2 ? 2 : 1;
11970         }
11971     }
11972 
11973     /*
11974      * CPTR_EL2 is present in v7VE or v8, and changes format
11975      * with HCR_EL2.E2H (regardless of TGE).
11976      */
11977     if (cur_el <= 2) {
11978         if (hcr_el2 & HCR_E2H) {
11979             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11980             case 1:
11981                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11982                     break;
11983                 }
11984                 /* fall through */
11985             case 0:
11986             case 2:
11987                 return 2;
11988             }
11989         } else if (arm_is_el2_enabled(env)) {
11990             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11991                 return 2;
11992             }
11993         }
11994     }
11995 
11996     /* CPTR_EL3 : present in v8 */
11997     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11998         /* Trap all FP ops to EL3 */
11999         return 3;
12000     }
12001 #endif
12002     return 0;
12003 }
12004 
12005 /* Return the exception level we're running at if this is our mmu_idx */
12006 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12007 {
12008     if (mmu_idx & ARM_MMU_IDX_M) {
12009         return mmu_idx & ARM_MMU_IDX_M_PRIV;
12010     }
12011 
12012     switch (mmu_idx) {
12013     case ARMMMUIdx_E10_0:
12014     case ARMMMUIdx_E20_0:
12015         return 0;
12016     case ARMMMUIdx_E10_1:
12017     case ARMMMUIdx_E10_1_PAN:
12018         return 1;
12019     case ARMMMUIdx_E2:
12020     case ARMMMUIdx_E20_2:
12021     case ARMMMUIdx_E20_2_PAN:
12022         return 2;
12023     case ARMMMUIdx_E3:
12024         return 3;
12025     default:
12026         g_assert_not_reached();
12027     }
12028 }
12029 
12030 #ifndef CONFIG_TCG
12031 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12032 {
12033     g_assert_not_reached();
12034 }
12035 #endif
12036 
12037 static bool arm_pan_enabled(CPUARMState *env)
12038 {
12039     if (is_a64(env)) {
12040         return env->pstate & PSTATE_PAN;
12041     } else {
12042         return env->uncached_cpsr & CPSR_PAN;
12043     }
12044 }
12045 
12046 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12047 {
12048     ARMMMUIdx idx;
12049     uint64_t hcr;
12050 
12051     if (arm_feature(env, ARM_FEATURE_M)) {
12052         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12053     }
12054 
12055     /* See ARM pseudo-function ELIsInHost.  */
12056     switch (el) {
12057     case 0:
12058         hcr = arm_hcr_el2_eff(env);
12059         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12060             idx = ARMMMUIdx_E20_0;
12061         } else {
12062             idx = ARMMMUIdx_E10_0;
12063         }
12064         break;
12065     case 1:
12066         if (arm_pan_enabled(env)) {
12067             idx = ARMMMUIdx_E10_1_PAN;
12068         } else {
12069             idx = ARMMMUIdx_E10_1;
12070         }
12071         break;
12072     case 2:
12073         /* Note that TGE does not apply at EL2.  */
12074         if (arm_hcr_el2_eff(env) & HCR_E2H) {
12075             if (arm_pan_enabled(env)) {
12076                 idx = ARMMMUIdx_E20_2_PAN;
12077             } else {
12078                 idx = ARMMMUIdx_E20_2;
12079             }
12080         } else {
12081             idx = ARMMMUIdx_E2;
12082         }
12083         break;
12084     case 3:
12085         return ARMMMUIdx_E3;
12086     default:
12087         g_assert_not_reached();
12088     }
12089 
12090     return idx;
12091 }
12092 
12093 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12094 {
12095     return arm_mmu_idx_el(env, arm_current_el(env));
12096 }
12097 
12098 static bool mve_no_pred(CPUARMState *env)
12099 {
12100     /*
12101      * Return true if there is definitely no predication of MVE
12102      * instructions by VPR or LTPSIZE. (Returning false even if there
12103      * isn't any predication is OK; generated code will just be
12104      * a little worse.)
12105      * If the CPU does not implement MVE then this TB flag is always 0.
12106      *
12107      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12108      * logic in gen_update_fp_context() needs to be updated to match.
12109      *
12110      * We do not include the effect of the ECI bits here -- they are
12111      * tracked in other TB flags. This simplifies the logic for
12112      * "when did we emit code that changes the MVE_NO_PRED TB flag
12113      * and thus need to end the TB?".
12114      */
12115     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12116         return false;
12117     }
12118     if (env->v7m.vpr) {
12119         return false;
12120     }
12121     if (env->v7m.ltpsize < 4) {
12122         return false;
12123     }
12124     return true;
12125 }
12126 
12127 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12128                           uint64_t *cs_base, uint32_t *pflags)
12129 {
12130     CPUARMTBFlags flags;
12131 
12132     assert_hflags_rebuild_correctly(env);
12133     flags = env->hflags;
12134 
12135     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12136         *pc = env->pc;
12137         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12138             DP_TBFLAG_A64(flags, BTYPE, env->btype);
12139         }
12140     } else {
12141         *pc = env->regs[15];
12142 
12143         if (arm_feature(env, ARM_FEATURE_M)) {
12144             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12145                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12146                 != env->v7m.secure) {
12147                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12148             }
12149 
12150             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12151                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12152                  (env->v7m.secure &&
12153                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12154                 /*
12155                  * ASPEN is set, but FPCA/SFPA indicate that there is no
12156                  * active FP context; we must create a new FP context before
12157                  * executing any FP insn.
12158                  */
12159                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12160             }
12161 
12162             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12163             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12164                 DP_TBFLAG_M32(flags, LSPACT, 1);
12165             }
12166 
12167             if (mve_no_pred(env)) {
12168                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12169             }
12170         } else {
12171             /*
12172              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12173              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12174              */
12175             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12176                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12177             } else {
12178                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12179                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12180             }
12181             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12182                 DP_TBFLAG_A32(flags, VFPEN, 1);
12183             }
12184         }
12185 
12186         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12187         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12188     }
12189 
12190     /*
12191      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12192      * states defined in the ARM ARM for software singlestep:
12193      *  SS_ACTIVE   PSTATE.SS   State
12194      *     0            x       Inactive (the TB flag for SS is always 0)
12195      *     1            0       Active-pending
12196      *     1            1       Active-not-pending
12197      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12198      */
12199     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12200         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12201     }
12202 
12203     *pflags = flags.flags;
12204     *cs_base = flags.flags2;
12205 }
12206 
12207 #ifdef TARGET_AARCH64
12208 /*
12209  * The manual says that when SVE is enabled and VQ is widened the
12210  * implementation is allowed to zero the previously inaccessible
12211  * portion of the registers.  The corollary to that is that when
12212  * SVE is enabled and VQ is narrowed we are also allowed to zero
12213  * the now inaccessible portion of the registers.
12214  *
12215  * The intent of this is that no predicate bit beyond VQ is ever set.
12216  * Which means that some operations on predicate registers themselves
12217  * may operate on full uint64_t or even unrolled across the maximum
12218  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
12219  * may well be cheaper than conditionals to restrict the operation
12220  * to the relevant portion of a uint16_t[16].
12221  */
12222 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12223 {
12224     int i, j;
12225     uint64_t pmask;
12226 
12227     assert(vq >= 1 && vq <= ARM_MAX_VQ);
12228     assert(vq <= env_archcpu(env)->sve_max_vq);
12229 
12230     /* Zap the high bits of the zregs.  */
12231     for (i = 0; i < 32; i++) {
12232         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12233     }
12234 
12235     /* Zap the high bits of the pregs and ffr.  */
12236     pmask = 0;
12237     if (vq & 3) {
12238         pmask = ~(-1ULL << (16 * (vq & 3)));
12239     }
12240     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12241         for (i = 0; i < 17; ++i) {
12242             env->vfp.pregs[i].p[j] &= pmask;
12243         }
12244         pmask = 0;
12245     }
12246 }
12247 
12248 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12249 {
12250     int exc_el;
12251 
12252     if (sm) {
12253         exc_el = sme_exception_el(env, el);
12254     } else {
12255         exc_el = sve_exception_el(env, el);
12256     }
12257     if (exc_el) {
12258         return 0; /* disabled */
12259     }
12260     return sve_vqm1_for_el_sm(env, el, sm);
12261 }
12262 
12263 /*
12264  * Notice a change in SVE vector size when changing EL.
12265  */
12266 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12267                            int new_el, bool el0_a64)
12268 {
12269     ARMCPU *cpu = env_archcpu(env);
12270     int old_len, new_len;
12271     bool old_a64, new_a64, sm;
12272 
12273     /* Nothing to do if no SVE.  */
12274     if (!cpu_isar_feature(aa64_sve, cpu)) {
12275         return;
12276     }
12277 
12278     /* Nothing to do if FP is disabled in either EL.  */
12279     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12280         return;
12281     }
12282 
12283     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12284     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12285 
12286     /*
12287      * Both AArch64.TakeException and AArch64.ExceptionReturn
12288      * invoke ResetSVEState when taking an exception from, or
12289      * returning to, AArch32 state when PSTATE.SM is enabled.
12290      */
12291     sm = FIELD_EX64(env->svcr, SVCR, SM);
12292     if (old_a64 != new_a64 && sm) {
12293         arm_reset_sve_state(env);
12294         return;
12295     }
12296 
12297     /*
12298      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12299      * at ELx, or not available because the EL is in AArch32 state, then
12300      * for all purposes other than a direct read, the ZCR_ELx.LEN field
12301      * has an effective value of 0".
12302      *
12303      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12304      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12305      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
12306      * we already have the correct register contents when encountering the
12307      * vq0->vq0 transition between EL0->EL1.
12308      */
12309     old_len = new_len = 0;
12310     if (old_a64) {
12311         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12312     }
12313     if (new_a64) {
12314         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12315     }
12316 
12317     /* When changing vector length, clear inaccessible state.  */
12318     if (new_len < old_len) {
12319         aarch64_sve_narrow_vq(env, new_len + 1);
12320     }
12321 }
12322 #endif
12323 
12324 #ifndef CONFIG_USER_ONLY
12325 ARMSecuritySpace arm_security_space(CPUARMState *env)
12326 {
12327     if (arm_feature(env, ARM_FEATURE_M)) {
12328         return arm_secure_to_space(env->v7m.secure);
12329     }
12330 
12331     /*
12332      * If EL3 is not supported then the secure state is implementation
12333      * defined, in which case QEMU defaults to non-secure.
12334      */
12335     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12336         return ARMSS_NonSecure;
12337     }
12338 
12339     /* Check for AArch64 EL3 or AArch32 Mon. */
12340     if (is_a64(env)) {
12341         if (extract32(env->pstate, 2, 2) == 3) {
12342             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
12343                 return ARMSS_Root;
12344             } else {
12345                 return ARMSS_Secure;
12346             }
12347         }
12348     } else {
12349         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
12350             return ARMSS_Secure;
12351         }
12352     }
12353 
12354     return arm_security_space_below_el3(env);
12355 }
12356 
12357 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
12358 {
12359     assert(!arm_feature(env, ARM_FEATURE_M));
12360 
12361     /*
12362      * If EL3 is not supported then the secure state is implementation
12363      * defined, in which case QEMU defaults to non-secure.
12364      */
12365     if (!arm_feature(env, ARM_FEATURE_EL3)) {
12366         return ARMSS_NonSecure;
12367     }
12368 
12369     /*
12370      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
12371      * Ignoring NSE when !NS retains consistency without having to
12372      * modify other predicates.
12373      */
12374     if (!(env->cp15.scr_el3 & SCR_NS)) {
12375         return ARMSS_Secure;
12376     } else if (env->cp15.scr_el3 & SCR_NSE) {
12377         return ARMSS_Realm;
12378     } else {
12379         return ARMSS_NonSecure;
12380     }
12381 }
12382 #endif /* !CONFIG_USER_ONLY */
12383