xref: /openbmc/qemu/target/arm/helper.c (revision ffe98631)
1 /*
2  * ARM generic helpers.
3  *
4  * This code is licensed under the GNU GPL v2 or later.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "trace.h"
12 #include "cpu.h"
13 #include "internals.h"
14 #include "exec/helper-proto.h"
15 #include "qemu/main-loop.h"
16 #include "qemu/timer.h"
17 #include "qemu/bitops.h"
18 #include "qemu/crc32c.h"
19 #include "qemu/qemu-print.h"
20 #include "exec/exec-all.h"
21 #include <zlib.h> /* For crc32 */
22 #include "hw/irq.h"
23 #include "sysemu/cpu-timers.h"
24 #include "sysemu/kvm.h"
25 #include "qapi/qapi-commands-machine-target.h"
26 #include "qapi/error.h"
27 #include "qemu/guest-random.h"
28 #ifdef CONFIG_TCG
29 #include "semihosting/common-semi.h"
30 #endif
31 #include "cpregs.h"
32 
33 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
34 
35 static void switch_mode(CPUARMState *env, int mode);
36 
37 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
38 {
39     assert(ri->fieldoffset);
40     if (cpreg_field_is_64bit(ri)) {
41         return CPREG_FIELD64(env, ri);
42     } else {
43         return CPREG_FIELD32(env, ri);
44     }
45 }
46 
47 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
48 {
49     assert(ri->fieldoffset);
50     if (cpreg_field_is_64bit(ri)) {
51         CPREG_FIELD64(env, ri) = value;
52     } else {
53         CPREG_FIELD32(env, ri) = value;
54     }
55 }
56 
57 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
58 {
59     return (char *)env + ri->fieldoffset;
60 }
61 
62 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
63 {
64     /* Raw read of a coprocessor register (as needed for migration, etc). */
65     if (ri->type & ARM_CP_CONST) {
66         return ri->resetvalue;
67     } else if (ri->raw_readfn) {
68         return ri->raw_readfn(env, ri);
69     } else if (ri->readfn) {
70         return ri->readfn(env, ri);
71     } else {
72         return raw_read(env, ri);
73     }
74 }
75 
76 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
77                              uint64_t v)
78 {
79     /*
80      * Raw write of a coprocessor register (as needed for migration, etc).
81      * Note that constant registers are treated as write-ignored; the
82      * caller should check for success by whether a readback gives the
83      * value written.
84      */
85     if (ri->type & ARM_CP_CONST) {
86         return;
87     } else if (ri->raw_writefn) {
88         ri->raw_writefn(env, ri, v);
89     } else if (ri->writefn) {
90         ri->writefn(env, ri, v);
91     } else {
92         raw_write(env, ri, v);
93     }
94 }
95 
96 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
97 {
98    /*
99     * Return true if the regdef would cause an assertion if you called
100     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
101     * program bug for it not to have the NO_RAW flag).
102     * NB that returning false here doesn't necessarily mean that calling
103     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
104     * read/write access functions which are safe for raw use" from "has
105     * read/write access functions which have side effects but has forgotten
106     * to provide raw access functions".
107     * The tests here line up with the conditions in read/write_raw_cp_reg()
108     * and assertions in raw_read()/raw_write().
109     */
110     if ((ri->type & ARM_CP_CONST) ||
111         ri->fieldoffset ||
112         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
113         return false;
114     }
115     return true;
116 }
117 
118 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
119 {
120     /* Write the coprocessor state from cpu->env to the (index,value) list. */
121     int i;
122     bool ok = true;
123 
124     for (i = 0; i < cpu->cpreg_array_len; i++) {
125         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
126         const ARMCPRegInfo *ri;
127         uint64_t newval;
128 
129         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
130         if (!ri) {
131             ok = false;
132             continue;
133         }
134         if (ri->type & ARM_CP_NO_RAW) {
135             continue;
136         }
137 
138         newval = read_raw_cp_reg(&cpu->env, ri);
139         if (kvm_sync) {
140             /*
141              * Only sync if the previous list->cpustate sync succeeded.
142              * Rather than tracking the success/failure state for every
143              * item in the list, we just recheck "does the raw write we must
144              * have made in write_list_to_cpustate() read back OK" here.
145              */
146             uint64_t oldval = cpu->cpreg_values[i];
147 
148             if (oldval == newval) {
149                 continue;
150             }
151 
152             write_raw_cp_reg(&cpu->env, ri, oldval);
153             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
154                 continue;
155             }
156 
157             write_raw_cp_reg(&cpu->env, ri, newval);
158         }
159         cpu->cpreg_values[i] = newval;
160     }
161     return ok;
162 }
163 
164 bool write_list_to_cpustate(ARMCPU *cpu)
165 {
166     int i;
167     bool ok = true;
168 
169     for (i = 0; i < cpu->cpreg_array_len; i++) {
170         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
171         uint64_t v = cpu->cpreg_values[i];
172         const ARMCPRegInfo *ri;
173 
174         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
175         if (!ri) {
176             ok = false;
177             continue;
178         }
179         if (ri->type & ARM_CP_NO_RAW) {
180             continue;
181         }
182         /*
183          * Write value and confirm it reads back as written
184          * (to catch read-only registers and partially read-only
185          * registers where the incoming migration value doesn't match)
186          */
187         write_raw_cp_reg(&cpu->env, ri, v);
188         if (read_raw_cp_reg(&cpu->env, ri) != v) {
189             ok = false;
190         }
191     }
192     return ok;
193 }
194 
195 static void add_cpreg_to_list(gpointer key, gpointer opaque)
196 {
197     ARMCPU *cpu = opaque;
198     uint32_t regidx = (uintptr_t)key;
199     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
200 
201     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
202         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
203         /* The value array need not be initialized at this point */
204         cpu->cpreg_array_len++;
205     }
206 }
207 
208 static void count_cpreg(gpointer key, gpointer opaque)
209 {
210     ARMCPU *cpu = opaque;
211     const ARMCPRegInfo *ri;
212 
213     ri = g_hash_table_lookup(cpu->cp_regs, key);
214 
215     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
216         cpu->cpreg_array_len++;
217     }
218 }
219 
220 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
221 {
222     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
223     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
224 
225     if (aidx > bidx) {
226         return 1;
227     }
228     if (aidx < bidx) {
229         return -1;
230     }
231     return 0;
232 }
233 
234 void init_cpreg_list(ARMCPU *cpu)
235 {
236     /*
237      * Initialise the cpreg_tuples[] array based on the cp_regs hash.
238      * Note that we require cpreg_tuples[] to be sorted by key ID.
239      */
240     GList *keys;
241     int arraylen;
242 
243     keys = g_hash_table_get_keys(cpu->cp_regs);
244     keys = g_list_sort(keys, cpreg_key_compare);
245 
246     cpu->cpreg_array_len = 0;
247 
248     g_list_foreach(keys, count_cpreg, cpu);
249 
250     arraylen = cpu->cpreg_array_len;
251     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
252     cpu->cpreg_values = g_new(uint64_t, arraylen);
253     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
254     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
255     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
256     cpu->cpreg_array_len = 0;
257 
258     g_list_foreach(keys, add_cpreg_to_list, cpu);
259 
260     assert(cpu->cpreg_array_len == arraylen);
261 
262     g_list_free(keys);
263 }
264 
265 /*
266  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
267  */
268 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
269                                         const ARMCPRegInfo *ri,
270                                         bool isread)
271 {
272     if (!is_a64(env) && arm_current_el(env) == 3 &&
273         arm_is_secure_below_el3(env)) {
274         return CP_ACCESS_TRAP_UNCATEGORIZED;
275     }
276     return CP_ACCESS_OK;
277 }
278 
279 /*
280  * Some secure-only AArch32 registers trap to EL3 if used from
281  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
282  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
283  * We assume that the .access field is set to PL1_RW.
284  */
285 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
286                                             const ARMCPRegInfo *ri,
287                                             bool isread)
288 {
289     if (arm_current_el(env) == 3) {
290         return CP_ACCESS_OK;
291     }
292     if (arm_is_secure_below_el3(env)) {
293         if (env->cp15.scr_el3 & SCR_EEL2) {
294             return CP_ACCESS_TRAP_EL2;
295         }
296         return CP_ACCESS_TRAP_EL3;
297     }
298     /* This will be EL1 NS and EL2 NS, which just UNDEF */
299     return CP_ACCESS_TRAP_UNCATEGORIZED;
300 }
301 
302 /*
303  * Check for traps to performance monitor registers, which are controlled
304  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
305  */
306 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
307                                  bool isread)
308 {
309     int el = arm_current_el(env);
310     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
311 
312     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
313         return CP_ACCESS_TRAP_EL2;
314     }
315     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
316         return CP_ACCESS_TRAP_EL3;
317     }
318     return CP_ACCESS_OK;
319 }
320 
321 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
322 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
323                                       bool isread)
324 {
325     if (arm_current_el(env) == 1) {
326         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
327         if (arm_hcr_el2_eff(env) & trap) {
328             return CP_ACCESS_TRAP_EL2;
329         }
330     }
331     return CP_ACCESS_OK;
332 }
333 
334 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
335 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
336                                  bool isread)
337 {
338     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
339         return CP_ACCESS_TRAP_EL2;
340     }
341     return CP_ACCESS_OK;
342 }
343 
344 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
345 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
346                                   bool isread)
347 {
348     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
349         return CP_ACCESS_TRAP_EL2;
350     }
351     return CP_ACCESS_OK;
352 }
353 
354 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
355 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
356                                   bool isread)
357 {
358     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
359         return CP_ACCESS_TRAP_EL2;
360     }
361     return CP_ACCESS_OK;
362 }
363 
364 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
365 static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
366                                     bool isread)
367 {
368     if (arm_current_el(env) == 1 &&
369         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
370         return CP_ACCESS_TRAP_EL2;
371     }
372     return CP_ACCESS_OK;
373 }
374 
375 #ifdef TARGET_AARCH64
376 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
377 static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
378                                     bool isread)
379 {
380     if (arm_current_el(env) == 1 &&
381         (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
382         return CP_ACCESS_TRAP_EL2;
383     }
384     return CP_ACCESS_OK;
385 }
386 #endif
387 
388 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
389 {
390     ARMCPU *cpu = env_archcpu(env);
391 
392     raw_write(env, ri, value);
393     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
394 }
395 
396 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
397 {
398     ARMCPU *cpu = env_archcpu(env);
399 
400     if (raw_read(env, ri) != value) {
401         /*
402          * Unlike real hardware the qemu TLB uses virtual addresses,
403          * not modified virtual addresses, so this causes a TLB flush.
404          */
405         tlb_flush(CPU(cpu));
406         raw_write(env, ri, value);
407     }
408 }
409 
410 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
411                              uint64_t value)
412 {
413     ARMCPU *cpu = env_archcpu(env);
414 
415     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
416         && !extended_addresses_enabled(env)) {
417         /*
418          * For VMSA (when not using the LPAE long descriptor page table
419          * format) this register includes the ASID, so do a TLB flush.
420          * For PMSA it is purely a process ID and no action is needed.
421          */
422         tlb_flush(CPU(cpu));
423     }
424     raw_write(env, ri, value);
425 }
426 
427 static int alle1_tlbmask(CPUARMState *env)
428 {
429     /*
430      * Note that the 'ALL' scope must invalidate both stage 1 and
431      * stage 2 translations, whereas most other scopes only invalidate
432      * stage 1 translations.
433      */
434     return (ARMMMUIdxBit_E10_1 |
435             ARMMMUIdxBit_E10_1_PAN |
436             ARMMMUIdxBit_E10_0 |
437             ARMMMUIdxBit_Stage2 |
438             ARMMMUIdxBit_Stage2_S);
439 }
440 
441 
442 /* IS variants of TLB operations must affect all cores */
443 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
444                              uint64_t value)
445 {
446     CPUState *cs = env_cpu(env);
447 
448     tlb_flush_all_cpus_synced(cs);
449 }
450 
451 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
452                              uint64_t value)
453 {
454     CPUState *cs = env_cpu(env);
455 
456     tlb_flush_all_cpus_synced(cs);
457 }
458 
459 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
460                              uint64_t value)
461 {
462     CPUState *cs = env_cpu(env);
463 
464     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
465 }
466 
467 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
468                              uint64_t value)
469 {
470     CPUState *cs = env_cpu(env);
471 
472     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
473 }
474 
475 /*
476  * Non-IS variants of TLB operations are upgraded to
477  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
478  * force broadcast of these operations.
479  */
480 static bool tlb_force_broadcast(CPUARMState *env)
481 {
482     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
483 }
484 
485 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
486                           uint64_t value)
487 {
488     /* Invalidate all (TLBIALL) */
489     CPUState *cs = env_cpu(env);
490 
491     if (tlb_force_broadcast(env)) {
492         tlb_flush_all_cpus_synced(cs);
493     } else {
494         tlb_flush(cs);
495     }
496 }
497 
498 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
499                           uint64_t value)
500 {
501     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
502     CPUState *cs = env_cpu(env);
503 
504     value &= TARGET_PAGE_MASK;
505     if (tlb_force_broadcast(env)) {
506         tlb_flush_page_all_cpus_synced(cs, value);
507     } else {
508         tlb_flush_page(cs, value);
509     }
510 }
511 
512 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
513                            uint64_t value)
514 {
515     /* Invalidate by ASID (TLBIASID) */
516     CPUState *cs = env_cpu(env);
517 
518     if (tlb_force_broadcast(env)) {
519         tlb_flush_all_cpus_synced(cs);
520     } else {
521         tlb_flush(cs);
522     }
523 }
524 
525 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
526                            uint64_t value)
527 {
528     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
529     CPUState *cs = env_cpu(env);
530 
531     value &= TARGET_PAGE_MASK;
532     if (tlb_force_broadcast(env)) {
533         tlb_flush_page_all_cpus_synced(cs, value);
534     } else {
535         tlb_flush_page(cs, value);
536     }
537 }
538 
539 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
540                                uint64_t value)
541 {
542     CPUState *cs = env_cpu(env);
543 
544     tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
545 }
546 
547 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
548                                   uint64_t value)
549 {
550     CPUState *cs = env_cpu(env);
551 
552     tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
553 }
554 
555 
556 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
557                               uint64_t value)
558 {
559     CPUState *cs = env_cpu(env);
560 
561     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
562 }
563 
564 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
565                                  uint64_t value)
566 {
567     CPUState *cs = env_cpu(env);
568 
569     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
570 }
571 
572 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
573                               uint64_t value)
574 {
575     CPUState *cs = env_cpu(env);
576     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
577 
578     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
579 }
580 
581 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
582                                  uint64_t value)
583 {
584     CPUState *cs = env_cpu(env);
585     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
586 
587     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
588                                              ARMMMUIdxBit_E2);
589 }
590 
591 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
592                                 uint64_t value)
593 {
594     CPUState *cs = env_cpu(env);
595     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
596 
597     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
598 }
599 
600 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
601                                 uint64_t value)
602 {
603     CPUState *cs = env_cpu(env);
604     uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
605 
606     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
607 }
608 
609 static const ARMCPRegInfo cp_reginfo[] = {
610     /*
611      * Define the secure and non-secure FCSE identifier CP registers
612      * separately because there is no secure bank in V8 (no _EL3).  This allows
613      * the secure register to be properly reset and migrated. There is also no
614      * v8 EL1 version of the register so the non-secure instance stands alone.
615      */
616     { .name = "FCSEIDR",
617       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
618       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
619       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
620       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
621     { .name = "FCSEIDR_S",
622       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
623       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
624       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
625       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
626     /*
627      * Define the secure and non-secure context identifier CP registers
628      * separately because there is no secure bank in V8 (no _EL3).  This allows
629      * the secure register to be properly reset and migrated.  In the
630      * non-secure case, the 32-bit register will have reset and migration
631      * disabled during registration as it is handled by the 64-bit instance.
632      */
633     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
634       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
635       .access = PL1_RW, .accessfn = access_tvm_trvm,
636       .secure = ARM_CP_SECSTATE_NS,
637       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
638       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
639     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
640       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
641       .access = PL1_RW, .accessfn = access_tvm_trvm,
642       .secure = ARM_CP_SECSTATE_S,
643       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
644       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
645 };
646 
647 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
648     /*
649      * NB: Some of these registers exist in v8 but with more precise
650      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
651      */
652     /* MMU Domain access control / MPU write buffer control */
653     { .name = "DACR",
654       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
655       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
656       .writefn = dacr_write, .raw_writefn = raw_write,
657       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
658                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
659     /*
660      * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
661      * For v6 and v5, these mappings are overly broad.
662      */
663     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
664       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
665     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
666       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
667     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
668       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
669     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
670       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
671     /* Cache maintenance ops; some of this space may be overridden later. */
672     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
673       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
674       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
675 };
676 
677 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
678     /*
679      * Not all pre-v6 cores implemented this WFI, so this is slightly
680      * over-broad.
681      */
682     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
683       .access = PL1_W, .type = ARM_CP_WFI },
684 };
685 
686 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
687     /*
688      * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
689      * is UNPREDICTABLE; we choose to NOP as most implementations do).
690      */
691     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
692       .access = PL1_W, .type = ARM_CP_WFI },
693     /*
694      * L1 cache lockdown. Not architectural in v6 and earlier but in practice
695      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
696      * OMAPCP will override this space.
697      */
698     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
699       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
700       .resetvalue = 0 },
701     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
702       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
703       .resetvalue = 0 },
704     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
705     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
706       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
707       .resetvalue = 0 },
708     /*
709      * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
710      * implementing it as RAZ means the "debug architecture version" bits
711      * will read as a reserved value, which should cause Linux to not try
712      * to use the debug hardware.
713      */
714     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
715       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
716     /*
717      * MMU TLB control. Note that the wildcarding means we cover not just
718      * the unified TLB ops but also the dside/iside/inner-shareable variants.
719      */
720     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
721       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
722       .type = ARM_CP_NO_RAW },
723     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
724       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
725       .type = ARM_CP_NO_RAW },
726     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
727       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
728       .type = ARM_CP_NO_RAW },
729     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
730       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
731       .type = ARM_CP_NO_RAW },
732     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
733       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
734     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
735       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
736 };
737 
738 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
739                         uint64_t value)
740 {
741     uint32_t mask = 0;
742 
743     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
744     if (!arm_feature(env, ARM_FEATURE_V8)) {
745         /*
746          * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
747          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
748          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
749          */
750         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
751             /* VFP coprocessor: cp10 & cp11 [23:20] */
752             mask |= R_CPACR_ASEDIS_MASK |
753                     R_CPACR_D32DIS_MASK |
754                     R_CPACR_CP11_MASK |
755                     R_CPACR_CP10_MASK;
756 
757             if (!arm_feature(env, ARM_FEATURE_NEON)) {
758                 /* ASEDIS [31] bit is RAO/WI */
759                 value |= R_CPACR_ASEDIS_MASK;
760             }
761 
762             /*
763              * VFPv3 and upwards with NEON implement 32 double precision
764              * registers (D0-D31).
765              */
766             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
767                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
768                 value |= R_CPACR_D32DIS_MASK;
769             }
770         }
771         value &= mask;
772     }
773 
774     /*
775      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
776      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
777      */
778     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
779         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
780         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
781         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
782     }
783 
784     env->cp15.cpacr_el1 = value;
785 }
786 
787 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
788 {
789     /*
790      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
791      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
792      */
793     uint64_t value = env->cp15.cpacr_el1;
794 
795     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
796         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
797         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
798     }
799     return value;
800 }
801 
802 
803 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
804 {
805     /*
806      * Call cpacr_write() so that we reset with the correct RAO bits set
807      * for our CPU features.
808      */
809     cpacr_write(env, ri, 0);
810 }
811 
812 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
813                                    bool isread)
814 {
815     if (arm_feature(env, ARM_FEATURE_V8)) {
816         /* Check if CPACR accesses are to be trapped to EL2 */
817         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
818             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
819             return CP_ACCESS_TRAP_EL2;
820         /* Check if CPACR accesses are to be trapped to EL3 */
821         } else if (arm_current_el(env) < 3 &&
822                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
823             return CP_ACCESS_TRAP_EL3;
824         }
825     }
826 
827     return CP_ACCESS_OK;
828 }
829 
830 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
831                                   bool isread)
832 {
833     /* Check if CPTR accesses are set to trap to EL3 */
834     if (arm_current_el(env) == 2 &&
835         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
836         return CP_ACCESS_TRAP_EL3;
837     }
838 
839     return CP_ACCESS_OK;
840 }
841 
842 static const ARMCPRegInfo v6_cp_reginfo[] = {
843     /* prefetch by MVA in v6, NOP in v7 */
844     { .name = "MVA_prefetch",
845       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
846       .access = PL1_W, .type = ARM_CP_NOP },
847     /*
848      * We need to break the TB after ISB to execute self-modifying code
849      * correctly and also to take any pending interrupts immediately.
850      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
851      */
852     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
853       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
854     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
855       .access = PL0_W, .type = ARM_CP_NOP },
856     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
857       .access = PL0_W, .type = ARM_CP_NOP },
858     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
859       .access = PL1_RW, .accessfn = access_tvm_trvm,
860       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
861                              offsetof(CPUARMState, cp15.ifar_ns) },
862       .resetvalue = 0, },
863     /*
864      * Watchpoint Fault Address Register : should actually only be present
865      * for 1136, 1176, 11MPCore.
866      */
867     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
868       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
869     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
870       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
871       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
872       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
873 };
874 
875 typedef struct pm_event {
876     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
877     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
878     bool (*supported)(CPUARMState *);
879     /*
880      * Retrieve the current count of the underlying event. The programmed
881      * counters hold a difference from the return value from this function
882      */
883     uint64_t (*get_count)(CPUARMState *);
884     /*
885      * Return how many nanoseconds it will take (at a minimum) for count events
886      * to occur. A negative value indicates the counter will never overflow, or
887      * that the counter has otherwise arranged for the overflow bit to be set
888      * and the PMU interrupt to be raised on overflow.
889      */
890     int64_t (*ns_per_count)(uint64_t);
891 } pm_event;
892 
893 static bool event_always_supported(CPUARMState *env)
894 {
895     return true;
896 }
897 
898 static uint64_t swinc_get_count(CPUARMState *env)
899 {
900     /*
901      * SW_INCR events are written directly to the pmevcntr's by writes to
902      * PMSWINC, so there is no underlying count maintained by the PMU itself
903      */
904     return 0;
905 }
906 
907 static int64_t swinc_ns_per(uint64_t ignored)
908 {
909     return -1;
910 }
911 
912 /*
913  * Return the underlying cycle count for the PMU cycle counters. If we're in
914  * usermode, simply return 0.
915  */
916 static uint64_t cycles_get_count(CPUARMState *env)
917 {
918 #ifndef CONFIG_USER_ONLY
919     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
920                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
921 #else
922     return cpu_get_host_ticks();
923 #endif
924 }
925 
926 #ifndef CONFIG_USER_ONLY
927 static int64_t cycles_ns_per(uint64_t cycles)
928 {
929     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
930 }
931 
932 static bool instructions_supported(CPUARMState *env)
933 {
934     return icount_enabled() == 1; /* Precise instruction counting */
935 }
936 
937 static uint64_t instructions_get_count(CPUARMState *env)
938 {
939     return (uint64_t)icount_get_raw();
940 }
941 
942 static int64_t instructions_ns_per(uint64_t icount)
943 {
944     return icount_to_ns((int64_t)icount);
945 }
946 #endif
947 
948 static bool pmuv3p1_events_supported(CPUARMState *env)
949 {
950     /* For events which are supported in any v8.1 PMU */
951     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
952 }
953 
954 static bool pmuv3p4_events_supported(CPUARMState *env)
955 {
956     /* For events which are supported in any v8.1 PMU */
957     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
958 }
959 
960 static uint64_t zero_event_get_count(CPUARMState *env)
961 {
962     /* For events which on QEMU never fire, so their count is always zero */
963     return 0;
964 }
965 
966 static int64_t zero_event_ns_per(uint64_t cycles)
967 {
968     /* An event which never fires can never overflow */
969     return -1;
970 }
971 
972 static const pm_event pm_events[] = {
973     { .number = 0x000, /* SW_INCR */
974       .supported = event_always_supported,
975       .get_count = swinc_get_count,
976       .ns_per_count = swinc_ns_per,
977     },
978 #ifndef CONFIG_USER_ONLY
979     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
980       .supported = instructions_supported,
981       .get_count = instructions_get_count,
982       .ns_per_count = instructions_ns_per,
983     },
984     { .number = 0x011, /* CPU_CYCLES, Cycle */
985       .supported = event_always_supported,
986       .get_count = cycles_get_count,
987       .ns_per_count = cycles_ns_per,
988     },
989 #endif
990     { .number = 0x023, /* STALL_FRONTEND */
991       .supported = pmuv3p1_events_supported,
992       .get_count = zero_event_get_count,
993       .ns_per_count = zero_event_ns_per,
994     },
995     { .number = 0x024, /* STALL_BACKEND */
996       .supported = pmuv3p1_events_supported,
997       .get_count = zero_event_get_count,
998       .ns_per_count = zero_event_ns_per,
999     },
1000     { .number = 0x03c, /* STALL */
1001       .supported = pmuv3p4_events_supported,
1002       .get_count = zero_event_get_count,
1003       .ns_per_count = zero_event_ns_per,
1004     },
1005 };
1006 
1007 /*
1008  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1009  * events (i.e. the statistical profiling extension), this implementation
1010  * should first be updated to something sparse instead of the current
1011  * supported_event_map[] array.
1012  */
1013 #define MAX_EVENT_ID 0x3c
1014 #define UNSUPPORTED_EVENT UINT16_MAX
1015 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1016 
1017 /*
1018  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1019  * of ARM event numbers to indices in our pm_events array.
1020  *
1021  * Note: Events in the 0x40XX range are not currently supported.
1022  */
1023 void pmu_init(ARMCPU *cpu)
1024 {
1025     unsigned int i;
1026 
1027     /*
1028      * Empty supported_event_map and cpu->pmceid[01] before adding supported
1029      * events to them
1030      */
1031     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1032         supported_event_map[i] = UNSUPPORTED_EVENT;
1033     }
1034     cpu->pmceid0 = 0;
1035     cpu->pmceid1 = 0;
1036 
1037     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1038         const pm_event *cnt = &pm_events[i];
1039         assert(cnt->number <= MAX_EVENT_ID);
1040         /* We do not currently support events in the 0x40xx range */
1041         assert(cnt->number <= 0x3f);
1042 
1043         if (cnt->supported(&cpu->env)) {
1044             supported_event_map[cnt->number] = i;
1045             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1046             if (cnt->number & 0x20) {
1047                 cpu->pmceid1 |= event_mask;
1048             } else {
1049                 cpu->pmceid0 |= event_mask;
1050             }
1051         }
1052     }
1053 }
1054 
1055 /*
1056  * Check at runtime whether a PMU event is supported for the current machine
1057  */
1058 static bool event_supported(uint16_t number)
1059 {
1060     if (number > MAX_EVENT_ID) {
1061         return false;
1062     }
1063     return supported_event_map[number] != UNSUPPORTED_EVENT;
1064 }
1065 
1066 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1067                                    bool isread)
1068 {
1069     /*
1070      * Performance monitor registers user accessibility is controlled
1071      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1072      * trapping to EL2 or EL3 for other accesses.
1073      */
1074     int el = arm_current_el(env);
1075     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1076 
1077     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1078         return CP_ACCESS_TRAP;
1079     }
1080     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1081         return CP_ACCESS_TRAP_EL2;
1082     }
1083     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1084         return CP_ACCESS_TRAP_EL3;
1085     }
1086 
1087     return CP_ACCESS_OK;
1088 }
1089 
1090 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1091                                            const ARMCPRegInfo *ri,
1092                                            bool isread)
1093 {
1094     /* ER: event counter read trap control */
1095     if (arm_feature(env, ARM_FEATURE_V8)
1096         && arm_current_el(env) == 0
1097         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1098         && isread) {
1099         return CP_ACCESS_OK;
1100     }
1101 
1102     return pmreg_access(env, ri, isread);
1103 }
1104 
1105 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1106                                          const ARMCPRegInfo *ri,
1107                                          bool isread)
1108 {
1109     /* SW: software increment write trap control */
1110     if (arm_feature(env, ARM_FEATURE_V8)
1111         && arm_current_el(env) == 0
1112         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1113         && !isread) {
1114         return CP_ACCESS_OK;
1115     }
1116 
1117     return pmreg_access(env, ri, isread);
1118 }
1119 
1120 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1121                                         const ARMCPRegInfo *ri,
1122                                         bool isread)
1123 {
1124     /* ER: event counter read trap control */
1125     if (arm_feature(env, ARM_FEATURE_V8)
1126         && arm_current_el(env) == 0
1127         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1128         return CP_ACCESS_OK;
1129     }
1130 
1131     return pmreg_access(env, ri, isread);
1132 }
1133 
1134 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1135                                          const ARMCPRegInfo *ri,
1136                                          bool isread)
1137 {
1138     /* CR: cycle counter read trap control */
1139     if (arm_feature(env, ARM_FEATURE_V8)
1140         && arm_current_el(env) == 0
1141         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1142         && isread) {
1143         return CP_ACCESS_OK;
1144     }
1145 
1146     return pmreg_access(env, ri, isread);
1147 }
1148 
1149 /*
1150  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1151  * We use these to decide whether we need to wrap a write to MDCR_EL2
1152  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1153  */
1154 #define MDCR_EL2_PMU_ENABLE_BITS \
1155     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1156 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1157 
1158 /*
1159  * Returns true if the counter (pass 31 for PMCCNTR) should count events using
1160  * the current EL, security state, and register configuration.
1161  */
1162 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1163 {
1164     uint64_t filter;
1165     bool e, p, u, nsk, nsu, nsh, m;
1166     bool enabled, prohibited = false, filtered;
1167     bool secure = arm_is_secure(env);
1168     int el = arm_current_el(env);
1169     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1170     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1171 
1172     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1173         return false;
1174     }
1175 
1176     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1177             (counter < hpmn || counter == 31)) {
1178         e = env->cp15.c9_pmcr & PMCRE;
1179     } else {
1180         e = mdcr_el2 & MDCR_HPME;
1181     }
1182     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1183 
1184     /* Is event counting prohibited? */
1185     if (el == 2 && (counter < hpmn || counter == 31)) {
1186         prohibited = mdcr_el2 & MDCR_HPMD;
1187     }
1188     if (secure) {
1189         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1190     }
1191 
1192     if (counter == 31) {
1193         /*
1194          * The cycle counter defaults to running. PMCR.DP says "disable
1195          * the cycle counter when event counting is prohibited".
1196          * Some MDCR bits disable the cycle counter specifically.
1197          */
1198         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1199         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1200             if (secure) {
1201                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1202             }
1203             if (el == 2) {
1204                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1205             }
1206         }
1207     }
1208 
1209     if (counter == 31) {
1210         filter = env->cp15.pmccfiltr_el0;
1211     } else {
1212         filter = env->cp15.c14_pmevtyper[counter];
1213     }
1214 
1215     p   = filter & PMXEVTYPER_P;
1216     u   = filter & PMXEVTYPER_U;
1217     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1218     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1219     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1220     m   = arm_el_is_aa64(env, 1) &&
1221               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1222 
1223     if (el == 0) {
1224         filtered = secure ? u : u != nsu;
1225     } else if (el == 1) {
1226         filtered = secure ? p : p != nsk;
1227     } else if (el == 2) {
1228         filtered = !nsh;
1229     } else { /* EL3 */
1230         filtered = m != p;
1231     }
1232 
1233     if (counter != 31) {
1234         /*
1235          * If not checking PMCCNTR, ensure the counter is setup to an event we
1236          * support
1237          */
1238         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1239         if (!event_supported(event)) {
1240             return false;
1241         }
1242     }
1243 
1244     return enabled && !prohibited && !filtered;
1245 }
1246 
1247 static void pmu_update_irq(CPUARMState *env)
1248 {
1249     ARMCPU *cpu = env_archcpu(env);
1250     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1251             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1252 }
1253 
1254 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1255 {
1256     /*
1257      * Return true if the clock divider is enabled and the cycle counter
1258      * is supposed to tick only once every 64 clock cycles. This is
1259      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1260      * (64-bit) cycle counter PMCR.D has no effect.
1261      */
1262     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1263 }
1264 
1265 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1266 {
1267     /* Return true if the specified event counter is configured to be 64 bit */
1268 
1269     /* This isn't intended to be used with the cycle counter */
1270     assert(counter < 31);
1271 
1272     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1273         return false;
1274     }
1275 
1276     if (arm_feature(env, ARM_FEATURE_EL2)) {
1277         /*
1278          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1279          * current security state, so we don't use arm_mdcr_el2_eff() here.
1280          */
1281         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1282         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1283 
1284         if (hpmn != 0 && counter >= hpmn) {
1285             return hlp;
1286         }
1287     }
1288     return env->cp15.c9_pmcr & PMCRLP;
1289 }
1290 
1291 /*
1292  * Ensure c15_ccnt is the guest-visible count so that operations such as
1293  * enabling/disabling the counter or filtering, modifying the count itself,
1294  * etc. can be done logically. This is essentially a no-op if the counter is
1295  * not enabled at the time of the call.
1296  */
1297 static void pmccntr_op_start(CPUARMState *env)
1298 {
1299     uint64_t cycles = cycles_get_count(env);
1300 
1301     if (pmu_counter_enabled(env, 31)) {
1302         uint64_t eff_cycles = cycles;
1303         if (pmccntr_clockdiv_enabled(env)) {
1304             eff_cycles /= 64;
1305         }
1306 
1307         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1308 
1309         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1310                                  1ull << 63 : 1ull << 31;
1311         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1312             env->cp15.c9_pmovsr |= (1ULL << 31);
1313             pmu_update_irq(env);
1314         }
1315 
1316         env->cp15.c15_ccnt = new_pmccntr;
1317     }
1318     env->cp15.c15_ccnt_delta = cycles;
1319 }
1320 
1321 /*
1322  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1323  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1324  * pmccntr_op_start.
1325  */
1326 static void pmccntr_op_finish(CPUARMState *env)
1327 {
1328     if (pmu_counter_enabled(env, 31)) {
1329 #ifndef CONFIG_USER_ONLY
1330         /* Calculate when the counter will next overflow */
1331         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1332         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1333             remaining_cycles = (uint32_t)remaining_cycles;
1334         }
1335         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1336 
1337         if (overflow_in > 0) {
1338             int64_t overflow_at;
1339 
1340             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1341                                  overflow_in, &overflow_at)) {
1342                 ARMCPU *cpu = env_archcpu(env);
1343                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1344             }
1345         }
1346 #endif
1347 
1348         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1349         if (pmccntr_clockdiv_enabled(env)) {
1350             prev_cycles /= 64;
1351         }
1352         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1353     }
1354 }
1355 
1356 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1357 {
1358 
1359     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1360     uint64_t count = 0;
1361     if (event_supported(event)) {
1362         uint16_t event_idx = supported_event_map[event];
1363         count = pm_events[event_idx].get_count(env);
1364     }
1365 
1366     if (pmu_counter_enabled(env, counter)) {
1367         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1368         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1369             1ULL << 63 : 1ULL << 31;
1370 
1371         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1372             env->cp15.c9_pmovsr |= (1 << counter);
1373             pmu_update_irq(env);
1374         }
1375         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1376     }
1377     env->cp15.c14_pmevcntr_delta[counter] = count;
1378 }
1379 
1380 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1381 {
1382     if (pmu_counter_enabled(env, counter)) {
1383 #ifndef CONFIG_USER_ONLY
1384         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1385         uint16_t event_idx = supported_event_map[event];
1386         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1387         int64_t overflow_in;
1388 
1389         if (!pmevcntr_is_64_bit(env, counter)) {
1390             delta = (uint32_t)delta;
1391         }
1392         overflow_in = pm_events[event_idx].ns_per_count(delta);
1393 
1394         if (overflow_in > 0) {
1395             int64_t overflow_at;
1396 
1397             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1398                                  overflow_in, &overflow_at)) {
1399                 ARMCPU *cpu = env_archcpu(env);
1400                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1401             }
1402         }
1403 #endif
1404 
1405         env->cp15.c14_pmevcntr_delta[counter] -=
1406             env->cp15.c14_pmevcntr[counter];
1407     }
1408 }
1409 
1410 void pmu_op_start(CPUARMState *env)
1411 {
1412     unsigned int i;
1413     pmccntr_op_start(env);
1414     for (i = 0; i < pmu_num_counters(env); i++) {
1415         pmevcntr_op_start(env, i);
1416     }
1417 }
1418 
1419 void pmu_op_finish(CPUARMState *env)
1420 {
1421     unsigned int i;
1422     pmccntr_op_finish(env);
1423     for (i = 0; i < pmu_num_counters(env); i++) {
1424         pmevcntr_op_finish(env, i);
1425     }
1426 }
1427 
1428 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1429 {
1430     pmu_op_start(&cpu->env);
1431 }
1432 
1433 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1434 {
1435     pmu_op_finish(&cpu->env);
1436 }
1437 
1438 void arm_pmu_timer_cb(void *opaque)
1439 {
1440     ARMCPU *cpu = opaque;
1441 
1442     /*
1443      * Update all the counter values based on the current underlying counts,
1444      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1445      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1446      * counter may expire.
1447      */
1448     pmu_op_start(&cpu->env);
1449     pmu_op_finish(&cpu->env);
1450 }
1451 
1452 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1453                        uint64_t value)
1454 {
1455     pmu_op_start(env);
1456 
1457     if (value & PMCRC) {
1458         /* The counter has been reset */
1459         env->cp15.c15_ccnt = 0;
1460     }
1461 
1462     if (value & PMCRP) {
1463         unsigned int i;
1464         for (i = 0; i < pmu_num_counters(env); i++) {
1465             env->cp15.c14_pmevcntr[i] = 0;
1466         }
1467     }
1468 
1469     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1470     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1471 
1472     pmu_op_finish(env);
1473 }
1474 
1475 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1476                           uint64_t value)
1477 {
1478     unsigned int i;
1479     uint64_t overflow_mask, new_pmswinc;
1480 
1481     for (i = 0; i < pmu_num_counters(env); i++) {
1482         /* Increment a counter's count iff: */
1483         if ((value & (1 << i)) && /* counter's bit is set */
1484                 /* counter is enabled and not filtered */
1485                 pmu_counter_enabled(env, i) &&
1486                 /* counter is SW_INCR */
1487                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1488             pmevcntr_op_start(env, i);
1489 
1490             /*
1491              * Detect if this write causes an overflow since we can't predict
1492              * PMSWINC overflows like we can for other events
1493              */
1494             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1495 
1496             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1497                 1ULL << 63 : 1ULL << 31;
1498 
1499             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1500                 env->cp15.c9_pmovsr |= (1 << i);
1501                 pmu_update_irq(env);
1502             }
1503 
1504             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1505 
1506             pmevcntr_op_finish(env, i);
1507         }
1508     }
1509 }
1510 
1511 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1512 {
1513     uint64_t ret;
1514     pmccntr_op_start(env);
1515     ret = env->cp15.c15_ccnt;
1516     pmccntr_op_finish(env);
1517     return ret;
1518 }
1519 
1520 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1521                          uint64_t value)
1522 {
1523     /*
1524      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1525      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1526      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1527      * accessed.
1528      */
1529     env->cp15.c9_pmselr = value & 0x1f;
1530 }
1531 
1532 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1533                         uint64_t value)
1534 {
1535     pmccntr_op_start(env);
1536     env->cp15.c15_ccnt = value;
1537     pmccntr_op_finish(env);
1538 }
1539 
1540 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1541                             uint64_t value)
1542 {
1543     uint64_t cur_val = pmccntr_read(env, NULL);
1544 
1545     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1546 }
1547 
1548 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1549                             uint64_t value)
1550 {
1551     pmccntr_op_start(env);
1552     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1553     pmccntr_op_finish(env);
1554 }
1555 
1556 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1557                             uint64_t value)
1558 {
1559     pmccntr_op_start(env);
1560     /* M is not accessible from AArch32 */
1561     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1562         (value & PMCCFILTR);
1563     pmccntr_op_finish(env);
1564 }
1565 
1566 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1567 {
1568     /* M is not visible in AArch32 */
1569     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1570 }
1571 
1572 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1573                             uint64_t value)
1574 {
1575     pmu_op_start(env);
1576     value &= pmu_counter_mask(env);
1577     env->cp15.c9_pmcnten |= value;
1578     pmu_op_finish(env);
1579 }
1580 
1581 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1582                              uint64_t value)
1583 {
1584     pmu_op_start(env);
1585     value &= pmu_counter_mask(env);
1586     env->cp15.c9_pmcnten &= ~value;
1587     pmu_op_finish(env);
1588 }
1589 
1590 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1591                          uint64_t value)
1592 {
1593     value &= pmu_counter_mask(env);
1594     env->cp15.c9_pmovsr &= ~value;
1595     pmu_update_irq(env);
1596 }
1597 
1598 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1599                          uint64_t value)
1600 {
1601     value &= pmu_counter_mask(env);
1602     env->cp15.c9_pmovsr |= value;
1603     pmu_update_irq(env);
1604 }
1605 
1606 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1607                              uint64_t value, const uint8_t counter)
1608 {
1609     if (counter == 31) {
1610         pmccfiltr_write(env, ri, value);
1611     } else if (counter < pmu_num_counters(env)) {
1612         pmevcntr_op_start(env, counter);
1613 
1614         /*
1615          * If this counter's event type is changing, store the current
1616          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1617          * pmevcntr_op_finish has the correct baseline when it converts back to
1618          * a delta.
1619          */
1620         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1621             PMXEVTYPER_EVTCOUNT;
1622         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1623         if (old_event != new_event) {
1624             uint64_t count = 0;
1625             if (event_supported(new_event)) {
1626                 uint16_t event_idx = supported_event_map[new_event];
1627                 count = pm_events[event_idx].get_count(env);
1628             }
1629             env->cp15.c14_pmevcntr_delta[counter] = count;
1630         }
1631 
1632         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1633         pmevcntr_op_finish(env, counter);
1634     }
1635     /*
1636      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1637      * PMSELR value is equal to or greater than the number of implemented
1638      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1639      */
1640 }
1641 
1642 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1643                                const uint8_t counter)
1644 {
1645     if (counter == 31) {
1646         return env->cp15.pmccfiltr_el0;
1647     } else if (counter < pmu_num_counters(env)) {
1648         return env->cp15.c14_pmevtyper[counter];
1649     } else {
1650       /*
1651        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1652        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1653        */
1654         return 0;
1655     }
1656 }
1657 
1658 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1659                               uint64_t value)
1660 {
1661     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1662     pmevtyper_write(env, ri, value, counter);
1663 }
1664 
1665 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1666                                uint64_t value)
1667 {
1668     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1669     env->cp15.c14_pmevtyper[counter] = value;
1670 
1671     /*
1672      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1673      * pmu_op_finish calls when loading saved state for a migration. Because
1674      * we're potentially updating the type of event here, the value written to
1675      * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1676      * different counter type. Therefore, we need to set this value to the
1677      * current count for the counter type we're writing so that pmu_op_finish
1678      * has the correct count for its calculation.
1679      */
1680     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1681     if (event_supported(event)) {
1682         uint16_t event_idx = supported_event_map[event];
1683         env->cp15.c14_pmevcntr_delta[counter] =
1684             pm_events[event_idx].get_count(env);
1685     }
1686 }
1687 
1688 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1689 {
1690     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1691     return pmevtyper_read(env, ri, counter);
1692 }
1693 
1694 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1695                              uint64_t value)
1696 {
1697     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1698 }
1699 
1700 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1701 {
1702     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1703 }
1704 
1705 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1706                              uint64_t value, uint8_t counter)
1707 {
1708     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1709         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1710         value &= MAKE_64BIT_MASK(0, 32);
1711     }
1712     if (counter < pmu_num_counters(env)) {
1713         pmevcntr_op_start(env, counter);
1714         env->cp15.c14_pmevcntr[counter] = value;
1715         pmevcntr_op_finish(env, counter);
1716     }
1717     /*
1718      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1719      * are CONSTRAINED UNPREDICTABLE.
1720      */
1721 }
1722 
1723 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1724                               uint8_t counter)
1725 {
1726     if (counter < pmu_num_counters(env)) {
1727         uint64_t ret;
1728         pmevcntr_op_start(env, counter);
1729         ret = env->cp15.c14_pmevcntr[counter];
1730         pmevcntr_op_finish(env, counter);
1731         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1732             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1733             ret &= MAKE_64BIT_MASK(0, 32);
1734         }
1735         return ret;
1736     } else {
1737       /*
1738        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1739        * are CONSTRAINED UNPREDICTABLE.
1740        */
1741         return 0;
1742     }
1743 }
1744 
1745 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1746                              uint64_t value)
1747 {
1748     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1749     pmevcntr_write(env, ri, value, counter);
1750 }
1751 
1752 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1753 {
1754     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1755     return pmevcntr_read(env, ri, counter);
1756 }
1757 
1758 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1759                              uint64_t value)
1760 {
1761     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1762     assert(counter < pmu_num_counters(env));
1763     env->cp15.c14_pmevcntr[counter] = value;
1764     pmevcntr_write(env, ri, value, counter);
1765 }
1766 
1767 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1768 {
1769     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1770     assert(counter < pmu_num_counters(env));
1771     return env->cp15.c14_pmevcntr[counter];
1772 }
1773 
1774 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1775                              uint64_t value)
1776 {
1777     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1778 }
1779 
1780 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1781 {
1782     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1783 }
1784 
1785 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1786                             uint64_t value)
1787 {
1788     if (arm_feature(env, ARM_FEATURE_V8)) {
1789         env->cp15.c9_pmuserenr = value & 0xf;
1790     } else {
1791         env->cp15.c9_pmuserenr = value & 1;
1792     }
1793 }
1794 
1795 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1796                              uint64_t value)
1797 {
1798     /* We have no event counters so only the C bit can be changed */
1799     value &= pmu_counter_mask(env);
1800     env->cp15.c9_pminten |= value;
1801     pmu_update_irq(env);
1802 }
1803 
1804 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1805                              uint64_t value)
1806 {
1807     value &= pmu_counter_mask(env);
1808     env->cp15.c9_pminten &= ~value;
1809     pmu_update_irq(env);
1810 }
1811 
1812 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1813                        uint64_t value)
1814 {
1815     /*
1816      * Note that even though the AArch64 view of this register has bits
1817      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1818      * architectural requirements for bits which are RES0 only in some
1819      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1820      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1821      */
1822     raw_write(env, ri, value & ~0x1FULL);
1823 }
1824 
1825 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1826 {
1827     /* Begin with base v8.0 state.  */
1828     uint64_t valid_mask = 0x3fff;
1829     ARMCPU *cpu = env_archcpu(env);
1830     uint64_t changed;
1831 
1832     /*
1833      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1834      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1835      * Instead, choose the format based on the mode of EL3.
1836      */
1837     if (arm_el_is_aa64(env, 3)) {
1838         value |= SCR_FW | SCR_AW;      /* RES1 */
1839         valid_mask &= ~SCR_NET;        /* RES0 */
1840 
1841         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1842             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1843             value |= SCR_RW;           /* RAO/WI */
1844         }
1845         if (cpu_isar_feature(aa64_ras, cpu)) {
1846             valid_mask |= SCR_TERR;
1847         }
1848         if (cpu_isar_feature(aa64_lor, cpu)) {
1849             valid_mask |= SCR_TLOR;
1850         }
1851         if (cpu_isar_feature(aa64_pauth, cpu)) {
1852             valid_mask |= SCR_API | SCR_APK;
1853         }
1854         if (cpu_isar_feature(aa64_sel2, cpu)) {
1855             valid_mask |= SCR_EEL2;
1856         }
1857         if (cpu_isar_feature(aa64_mte, cpu)) {
1858             valid_mask |= SCR_ATA;
1859         }
1860         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1861             valid_mask |= SCR_ENSCXT;
1862         }
1863         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1864             valid_mask |= SCR_EASE | SCR_NMEA;
1865         }
1866         if (cpu_isar_feature(aa64_sme, cpu)) {
1867             valid_mask |= SCR_ENTP2;
1868         }
1869         if (cpu_isar_feature(aa64_hcx, cpu)) {
1870             valid_mask |= SCR_HXEN;
1871         }
1872     } else {
1873         valid_mask &= ~(SCR_RW | SCR_ST);
1874         if (cpu_isar_feature(aa32_ras, cpu)) {
1875             valid_mask |= SCR_TERR;
1876         }
1877     }
1878 
1879     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1880         valid_mask &= ~SCR_HCE;
1881 
1882         /*
1883          * On ARMv7, SMD (or SCD as it is called in v7) is only
1884          * supported if EL2 exists. The bit is UNK/SBZP when
1885          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1886          * when EL2 is unavailable.
1887          * On ARMv8, this bit is always available.
1888          */
1889         if (arm_feature(env, ARM_FEATURE_V7) &&
1890             !arm_feature(env, ARM_FEATURE_V8)) {
1891             valid_mask &= ~SCR_SMD;
1892         }
1893     }
1894 
1895     /* Clear all-context RES0 bits.  */
1896     value &= valid_mask;
1897     changed = env->cp15.scr_el3 ^ value;
1898     env->cp15.scr_el3 = value;
1899 
1900     /*
1901      * If SCR_EL3.NS changes, i.e. arm_is_secure_below_el3, then
1902      * we must invalidate all TLBs below EL3.
1903      */
1904     if (changed & SCR_NS) {
1905         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1906                                            ARMMMUIdxBit_E20_0 |
1907                                            ARMMMUIdxBit_E10_1 |
1908                                            ARMMMUIdxBit_E20_2 |
1909                                            ARMMMUIdxBit_E10_1_PAN |
1910                                            ARMMMUIdxBit_E20_2_PAN |
1911                                            ARMMMUIdxBit_E2));
1912     }
1913 }
1914 
1915 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1916 {
1917     /*
1918      * scr_write will set the RES1 bits on an AArch64-only CPU.
1919      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1920      */
1921     scr_write(env, ri, 0);
1922 }
1923 
1924 static CPAccessResult access_tid4(CPUARMState *env,
1925                                   const ARMCPRegInfo *ri,
1926                                   bool isread)
1927 {
1928     if (arm_current_el(env) == 1 &&
1929         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1930         return CP_ACCESS_TRAP_EL2;
1931     }
1932 
1933     return CP_ACCESS_OK;
1934 }
1935 
1936 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1937 {
1938     ARMCPU *cpu = env_archcpu(env);
1939 
1940     /*
1941      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1942      * bank
1943      */
1944     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1945                                         ri->secure & ARM_CP_SECSTATE_S);
1946 
1947     return cpu->ccsidr[index];
1948 }
1949 
1950 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1951                          uint64_t value)
1952 {
1953     raw_write(env, ri, value & 0xf);
1954 }
1955 
1956 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1957 {
1958     CPUState *cs = env_cpu(env);
1959     bool el1 = arm_current_el(env) == 1;
1960     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1961     uint64_t ret = 0;
1962 
1963     if (hcr_el2 & HCR_IMO) {
1964         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1965             ret |= CPSR_I;
1966         }
1967     } else {
1968         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1969             ret |= CPSR_I;
1970         }
1971     }
1972 
1973     if (hcr_el2 & HCR_FMO) {
1974         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1975             ret |= CPSR_F;
1976         }
1977     } else {
1978         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1979             ret |= CPSR_F;
1980         }
1981     }
1982 
1983     if (hcr_el2 & HCR_AMO) {
1984         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1985             ret |= CPSR_A;
1986         }
1987     }
1988 
1989     return ret;
1990 }
1991 
1992 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1993                                        bool isread)
1994 {
1995     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1996         return CP_ACCESS_TRAP_EL2;
1997     }
1998 
1999     return CP_ACCESS_OK;
2000 }
2001 
2002 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2003                                        bool isread)
2004 {
2005     if (arm_feature(env, ARM_FEATURE_V8)) {
2006         return access_aa64_tid1(env, ri, isread);
2007     }
2008 
2009     return CP_ACCESS_OK;
2010 }
2011 
2012 static const ARMCPRegInfo v7_cp_reginfo[] = {
2013     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2014     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2015       .access = PL1_W, .type = ARM_CP_NOP },
2016     /*
2017      * Performance monitors are implementation defined in v7,
2018      * but with an ARM recommended set of registers, which we
2019      * follow.
2020      *
2021      * Performance registers fall into three categories:
2022      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2023      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2024      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2025      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2026      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2027      */
2028     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2029       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2030       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2031       .writefn = pmcntenset_write,
2032       .accessfn = pmreg_access,
2033       .raw_writefn = raw_write },
2034     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2035       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2036       .access = PL0_RW, .accessfn = pmreg_access,
2037       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2038       .writefn = pmcntenset_write, .raw_writefn = raw_write },
2039     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2040       .access = PL0_RW,
2041       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2042       .accessfn = pmreg_access,
2043       .writefn = pmcntenclr_write,
2044       .type = ARM_CP_ALIAS | ARM_CP_IO },
2045     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2046       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2047       .access = PL0_RW, .accessfn = pmreg_access,
2048       .type = ARM_CP_ALIAS | ARM_CP_IO,
2049       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2050       .writefn = pmcntenclr_write },
2051     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2052       .access = PL0_RW, .type = ARM_CP_IO,
2053       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2054       .accessfn = pmreg_access,
2055       .writefn = pmovsr_write,
2056       .raw_writefn = raw_write },
2057     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2058       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2059       .access = PL0_RW, .accessfn = pmreg_access,
2060       .type = ARM_CP_ALIAS | ARM_CP_IO,
2061       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2062       .writefn = pmovsr_write,
2063       .raw_writefn = raw_write },
2064     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2065       .access = PL0_W, .accessfn = pmreg_access_swinc,
2066       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2067       .writefn = pmswinc_write },
2068     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2069       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2070       .access = PL0_W, .accessfn = pmreg_access_swinc,
2071       .type = ARM_CP_NO_RAW | ARM_CP_IO,
2072       .writefn = pmswinc_write },
2073     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2074       .access = PL0_RW, .type = ARM_CP_ALIAS,
2075       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2076       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2077       .raw_writefn = raw_write},
2078     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2079       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2080       .access = PL0_RW, .accessfn = pmreg_access_selr,
2081       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2082       .writefn = pmselr_write, .raw_writefn = raw_write, },
2083     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2084       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2085       .readfn = pmccntr_read, .writefn = pmccntr_write32,
2086       .accessfn = pmreg_access_ccntr },
2087     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2088       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2089       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2090       .type = ARM_CP_IO,
2091       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2092       .readfn = pmccntr_read, .writefn = pmccntr_write,
2093       .raw_readfn = raw_read, .raw_writefn = raw_write, },
2094     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2095       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2096       .access = PL0_RW, .accessfn = pmreg_access,
2097       .type = ARM_CP_ALIAS | ARM_CP_IO,
2098       .resetvalue = 0, },
2099     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2100       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2101       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2102       .access = PL0_RW, .accessfn = pmreg_access,
2103       .type = ARM_CP_IO,
2104       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2105       .resetvalue = 0, },
2106     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2107       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2108       .accessfn = pmreg_access,
2109       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2110     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2111       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2112       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2113       .accessfn = pmreg_access,
2114       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2115     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2116       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2117       .accessfn = pmreg_access_xevcntr,
2118       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2119     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2120       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2121       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2122       .accessfn = pmreg_access_xevcntr,
2123       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2124     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2125       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2126       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2127       .resetvalue = 0,
2128       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2129     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2130       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2131       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2132       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2133       .resetvalue = 0,
2134       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2135     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2136       .access = PL1_RW, .accessfn = access_tpm,
2137       .type = ARM_CP_ALIAS | ARM_CP_IO,
2138       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2139       .resetvalue = 0,
2140       .writefn = pmintenset_write, .raw_writefn = raw_write },
2141     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2142       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2143       .access = PL1_RW, .accessfn = access_tpm,
2144       .type = ARM_CP_IO,
2145       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2146       .writefn = pmintenset_write, .raw_writefn = raw_write,
2147       .resetvalue = 0x0 },
2148     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2149       .access = PL1_RW, .accessfn = access_tpm,
2150       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2151       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2152       .writefn = pmintenclr_write, },
2153     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2154       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2155       .access = PL1_RW, .accessfn = access_tpm,
2156       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2157       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2158       .writefn = pmintenclr_write },
2159     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2160       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2161       .access = PL1_R,
2162       .accessfn = access_tid4,
2163       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2164     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2165       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2166       .access = PL1_RW,
2167       .accessfn = access_tid4,
2168       .writefn = csselr_write, .resetvalue = 0,
2169       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2170                              offsetof(CPUARMState, cp15.csselr_ns) } },
2171     /*
2172      * Auxiliary ID register: this actually has an IMPDEF value but for now
2173      * just RAZ for all cores:
2174      */
2175     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2176       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2177       .access = PL1_R, .type = ARM_CP_CONST,
2178       .accessfn = access_aa64_tid1,
2179       .resetvalue = 0 },
2180     /*
2181      * Auxiliary fault status registers: these also are IMPDEF, and we
2182      * choose to RAZ/WI for all cores.
2183      */
2184     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2185       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2186       .access = PL1_RW, .accessfn = access_tvm_trvm,
2187       .type = ARM_CP_CONST, .resetvalue = 0 },
2188     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2189       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2190       .access = PL1_RW, .accessfn = access_tvm_trvm,
2191       .type = ARM_CP_CONST, .resetvalue = 0 },
2192     /*
2193      * MAIR can just read-as-written because we don't implement caches
2194      * and so don't need to care about memory attributes.
2195      */
2196     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2197       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2198       .access = PL1_RW, .accessfn = access_tvm_trvm,
2199       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2200       .resetvalue = 0 },
2201     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2202       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2203       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2204       .resetvalue = 0 },
2205     /*
2206      * For non-long-descriptor page tables these are PRRR and NMRR;
2207      * regardless they still act as reads-as-written for QEMU.
2208      */
2209      /*
2210       * MAIR0/1 are defined separately from their 64-bit counterpart which
2211       * allows them to assign the correct fieldoffset based on the endianness
2212       * handled in the field definitions.
2213       */
2214     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2215       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2216       .access = PL1_RW, .accessfn = access_tvm_trvm,
2217       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2218                              offsetof(CPUARMState, cp15.mair0_ns) },
2219       .resetfn = arm_cp_reset_ignore },
2220     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2221       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2222       .access = PL1_RW, .accessfn = access_tvm_trvm,
2223       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2224                              offsetof(CPUARMState, cp15.mair1_ns) },
2225       .resetfn = arm_cp_reset_ignore },
2226     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2227       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2228       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2229     /* 32 bit ITLB invalidates */
2230     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2231       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2232       .writefn = tlbiall_write },
2233     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2234       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2235       .writefn = tlbimva_write },
2236     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2237       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2238       .writefn = tlbiasid_write },
2239     /* 32 bit DTLB invalidates */
2240     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2241       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2242       .writefn = tlbiall_write },
2243     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2244       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2245       .writefn = tlbimva_write },
2246     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2247       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2248       .writefn = tlbiasid_write },
2249     /* 32 bit TLB invalidates */
2250     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2251       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2252       .writefn = tlbiall_write },
2253     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2254       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2255       .writefn = tlbimva_write },
2256     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2257       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2258       .writefn = tlbiasid_write },
2259     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2260       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2261       .writefn = tlbimvaa_write },
2262 };
2263 
2264 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2265     /* 32 bit TLB invalidates, Inner Shareable */
2266     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2267       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2268       .writefn = tlbiall_is_write },
2269     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2270       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2271       .writefn = tlbimva_is_write },
2272     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2273       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2274       .writefn = tlbiasid_is_write },
2275     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2276       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2277       .writefn = tlbimvaa_is_write },
2278 };
2279 
2280 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2281     /* PMOVSSET is not implemented in v7 before v7ve */
2282     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2283       .access = PL0_RW, .accessfn = pmreg_access,
2284       .type = ARM_CP_ALIAS | ARM_CP_IO,
2285       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2286       .writefn = pmovsset_write,
2287       .raw_writefn = raw_write },
2288     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2289       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2290       .access = PL0_RW, .accessfn = pmreg_access,
2291       .type = ARM_CP_ALIAS | ARM_CP_IO,
2292       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2293       .writefn = pmovsset_write,
2294       .raw_writefn = raw_write },
2295 };
2296 
2297 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2298                         uint64_t value)
2299 {
2300     value &= 1;
2301     env->teecr = value;
2302 }
2303 
2304 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2305                                    bool isread)
2306 {
2307     /*
2308      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2309      * at all, so we don't need to check whether we're v8A.
2310      */
2311     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2312         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2313         return CP_ACCESS_TRAP_EL2;
2314     }
2315     return CP_ACCESS_OK;
2316 }
2317 
2318 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2319                                     bool isread)
2320 {
2321     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2322         return CP_ACCESS_TRAP;
2323     }
2324     return teecr_access(env, ri, isread);
2325 }
2326 
2327 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2328     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2329       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2330       .resetvalue = 0,
2331       .writefn = teecr_write, .accessfn = teecr_access },
2332     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2333       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2334       .accessfn = teehbr_access, .resetvalue = 0 },
2335 };
2336 
2337 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2338     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2339       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2340       .access = PL0_RW,
2341       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2342     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2343       .access = PL0_RW,
2344       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2345                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2346       .resetfn = arm_cp_reset_ignore },
2347     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2348       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2349       .access = PL0_R | PL1_W,
2350       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2351       .resetvalue = 0},
2352     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2353       .access = PL0_R | PL1_W,
2354       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2355                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2356       .resetfn = arm_cp_reset_ignore },
2357     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2358       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2359       .access = PL1_RW,
2360       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2361     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2362       .access = PL1_RW,
2363       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2364                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2365       .resetvalue = 0 },
2366 };
2367 
2368 #ifndef CONFIG_USER_ONLY
2369 
2370 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2371                                        bool isread)
2372 {
2373     /*
2374      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2375      * Writable only at the highest implemented exception level.
2376      */
2377     int el = arm_current_el(env);
2378     uint64_t hcr;
2379     uint32_t cntkctl;
2380 
2381     switch (el) {
2382     case 0:
2383         hcr = arm_hcr_el2_eff(env);
2384         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2385             cntkctl = env->cp15.cnthctl_el2;
2386         } else {
2387             cntkctl = env->cp15.c14_cntkctl;
2388         }
2389         if (!extract32(cntkctl, 0, 2)) {
2390             return CP_ACCESS_TRAP;
2391         }
2392         break;
2393     case 1:
2394         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2395             arm_is_secure_below_el3(env)) {
2396             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2397             return CP_ACCESS_TRAP_UNCATEGORIZED;
2398         }
2399         break;
2400     case 2:
2401     case 3:
2402         break;
2403     }
2404 
2405     if (!isread && el < arm_highest_el(env)) {
2406         return CP_ACCESS_TRAP_UNCATEGORIZED;
2407     }
2408 
2409     return CP_ACCESS_OK;
2410 }
2411 
2412 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2413                                         bool isread)
2414 {
2415     unsigned int cur_el = arm_current_el(env);
2416     bool has_el2 = arm_is_el2_enabled(env);
2417     uint64_t hcr = arm_hcr_el2_eff(env);
2418 
2419     switch (cur_el) {
2420     case 0:
2421         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2422         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2423             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2424                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2425         }
2426 
2427         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2428         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2429             return CP_ACCESS_TRAP;
2430         }
2431 
2432         /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2433         if (hcr & HCR_E2H) {
2434             if (timeridx == GTIMER_PHYS &&
2435                 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2436                 return CP_ACCESS_TRAP_EL2;
2437             }
2438         } else {
2439             /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2440             if (has_el2 && timeridx == GTIMER_PHYS &&
2441                 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2442                 return CP_ACCESS_TRAP_EL2;
2443             }
2444         }
2445         break;
2446 
2447     case 1:
2448         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2449         if (has_el2 && timeridx == GTIMER_PHYS &&
2450             (hcr & HCR_E2H
2451              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2452              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2453             return CP_ACCESS_TRAP_EL2;
2454         }
2455         break;
2456     }
2457     return CP_ACCESS_OK;
2458 }
2459 
2460 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2461                                       bool isread)
2462 {
2463     unsigned int cur_el = arm_current_el(env);
2464     bool has_el2 = arm_is_el2_enabled(env);
2465     uint64_t hcr = arm_hcr_el2_eff(env);
2466 
2467     switch (cur_el) {
2468     case 0:
2469         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2470             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2471             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2472                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2473         }
2474 
2475         /*
2476          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2477          * EL0 if EL0[PV]TEN is zero.
2478          */
2479         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2480             return CP_ACCESS_TRAP;
2481         }
2482         /* fall through */
2483 
2484     case 1:
2485         if (has_el2 && timeridx == GTIMER_PHYS) {
2486             if (hcr & HCR_E2H) {
2487                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2488                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2489                     return CP_ACCESS_TRAP_EL2;
2490                 }
2491             } else {
2492                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2493                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2494                     return CP_ACCESS_TRAP_EL2;
2495                 }
2496             }
2497         }
2498         break;
2499     }
2500     return CP_ACCESS_OK;
2501 }
2502 
2503 static CPAccessResult gt_pct_access(CPUARMState *env,
2504                                     const ARMCPRegInfo *ri,
2505                                     bool isread)
2506 {
2507     return gt_counter_access(env, GTIMER_PHYS, isread);
2508 }
2509 
2510 static CPAccessResult gt_vct_access(CPUARMState *env,
2511                                     const ARMCPRegInfo *ri,
2512                                     bool isread)
2513 {
2514     return gt_counter_access(env, GTIMER_VIRT, isread);
2515 }
2516 
2517 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2518                                        bool isread)
2519 {
2520     return gt_timer_access(env, GTIMER_PHYS, isread);
2521 }
2522 
2523 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2524                                        bool isread)
2525 {
2526     return gt_timer_access(env, GTIMER_VIRT, isread);
2527 }
2528 
2529 static CPAccessResult gt_stimer_access(CPUARMState *env,
2530                                        const ARMCPRegInfo *ri,
2531                                        bool isread)
2532 {
2533     /*
2534      * The AArch64 register view of the secure physical timer is
2535      * always accessible from EL3, and configurably accessible from
2536      * Secure EL1.
2537      */
2538     switch (arm_current_el(env)) {
2539     case 1:
2540         if (!arm_is_secure(env)) {
2541             return CP_ACCESS_TRAP;
2542         }
2543         if (!(env->cp15.scr_el3 & SCR_ST)) {
2544             return CP_ACCESS_TRAP_EL3;
2545         }
2546         return CP_ACCESS_OK;
2547     case 0:
2548     case 2:
2549         return CP_ACCESS_TRAP;
2550     case 3:
2551         return CP_ACCESS_OK;
2552     default:
2553         g_assert_not_reached();
2554     }
2555 }
2556 
2557 static uint64_t gt_get_countervalue(CPUARMState *env)
2558 {
2559     ARMCPU *cpu = env_archcpu(env);
2560 
2561     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2562 }
2563 
2564 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2565 {
2566     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2567 
2568     if (gt->ctl & 1) {
2569         /*
2570          * Timer enabled: calculate and set current ISTATUS, irq, and
2571          * reset timer to when ISTATUS next has to change
2572          */
2573         uint64_t offset = timeridx == GTIMER_VIRT ?
2574                                       cpu->env.cp15.cntvoff_el2 : 0;
2575         uint64_t count = gt_get_countervalue(&cpu->env);
2576         /* Note that this must be unsigned 64 bit arithmetic: */
2577         int istatus = count - offset >= gt->cval;
2578         uint64_t nexttick;
2579         int irqstate;
2580 
2581         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2582 
2583         irqstate = (istatus && !(gt->ctl & 2));
2584         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2585 
2586         if (istatus) {
2587             /* Next transition is when count rolls back over to zero */
2588             nexttick = UINT64_MAX;
2589         } else {
2590             /* Next transition is when we hit cval */
2591             nexttick = gt->cval + offset;
2592         }
2593         /*
2594          * Note that the desired next expiry time might be beyond the
2595          * signed-64-bit range of a QEMUTimer -- in this case we just
2596          * set the timer for as far in the future as possible. When the
2597          * timer expires we will reset the timer for any remaining period.
2598          */
2599         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2600             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2601         } else {
2602             timer_mod(cpu->gt_timer[timeridx], nexttick);
2603         }
2604         trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2605     } else {
2606         /* Timer disabled: ISTATUS and timer output always clear */
2607         gt->ctl &= ~4;
2608         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2609         timer_del(cpu->gt_timer[timeridx]);
2610         trace_arm_gt_recalc_disabled(timeridx);
2611     }
2612 }
2613 
2614 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2615                            int timeridx)
2616 {
2617     ARMCPU *cpu = env_archcpu(env);
2618 
2619     timer_del(cpu->gt_timer[timeridx]);
2620 }
2621 
2622 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2623 {
2624     return gt_get_countervalue(env);
2625 }
2626 
2627 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2628 {
2629     uint64_t hcr;
2630 
2631     switch (arm_current_el(env)) {
2632     case 2:
2633         hcr = arm_hcr_el2_eff(env);
2634         if (hcr & HCR_E2H) {
2635             return 0;
2636         }
2637         break;
2638     case 0:
2639         hcr = arm_hcr_el2_eff(env);
2640         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2641             return 0;
2642         }
2643         break;
2644     }
2645 
2646     return env->cp15.cntvoff_el2;
2647 }
2648 
2649 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2650 {
2651     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2652 }
2653 
2654 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2655                           int timeridx,
2656                           uint64_t value)
2657 {
2658     trace_arm_gt_cval_write(timeridx, value);
2659     env->cp15.c14_timer[timeridx].cval = value;
2660     gt_recalc_timer(env_archcpu(env), timeridx);
2661 }
2662 
2663 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2664                              int timeridx)
2665 {
2666     uint64_t offset = 0;
2667 
2668     switch (timeridx) {
2669     case GTIMER_VIRT:
2670     case GTIMER_HYPVIRT:
2671         offset = gt_virt_cnt_offset(env);
2672         break;
2673     }
2674 
2675     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2676                       (gt_get_countervalue(env) - offset));
2677 }
2678 
2679 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2680                           int timeridx,
2681                           uint64_t value)
2682 {
2683     uint64_t offset = 0;
2684 
2685     switch (timeridx) {
2686     case GTIMER_VIRT:
2687     case GTIMER_HYPVIRT:
2688         offset = gt_virt_cnt_offset(env);
2689         break;
2690     }
2691 
2692     trace_arm_gt_tval_write(timeridx, value);
2693     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2694                                          sextract64(value, 0, 32);
2695     gt_recalc_timer(env_archcpu(env), timeridx);
2696 }
2697 
2698 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2699                          int timeridx,
2700                          uint64_t value)
2701 {
2702     ARMCPU *cpu = env_archcpu(env);
2703     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2704 
2705     trace_arm_gt_ctl_write(timeridx, value);
2706     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2707     if ((oldval ^ value) & 1) {
2708         /* Enable toggled */
2709         gt_recalc_timer(cpu, timeridx);
2710     } else if ((oldval ^ value) & 2) {
2711         /*
2712          * IMASK toggled: don't need to recalculate,
2713          * just set the interrupt line based on ISTATUS
2714          */
2715         int irqstate = (oldval & 4) && !(value & 2);
2716 
2717         trace_arm_gt_imask_toggle(timeridx, irqstate);
2718         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2719     }
2720 }
2721 
2722 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2723 {
2724     gt_timer_reset(env, ri, GTIMER_PHYS);
2725 }
2726 
2727 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2728                                uint64_t value)
2729 {
2730     gt_cval_write(env, ri, GTIMER_PHYS, value);
2731 }
2732 
2733 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2734 {
2735     return gt_tval_read(env, ri, GTIMER_PHYS);
2736 }
2737 
2738 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2739                                uint64_t value)
2740 {
2741     gt_tval_write(env, ri, GTIMER_PHYS, value);
2742 }
2743 
2744 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2745                               uint64_t value)
2746 {
2747     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2748 }
2749 
2750 static int gt_phys_redir_timeridx(CPUARMState *env)
2751 {
2752     switch (arm_mmu_idx(env)) {
2753     case ARMMMUIdx_E20_0:
2754     case ARMMMUIdx_E20_2:
2755     case ARMMMUIdx_E20_2_PAN:
2756         return GTIMER_HYP;
2757     default:
2758         return GTIMER_PHYS;
2759     }
2760 }
2761 
2762 static int gt_virt_redir_timeridx(CPUARMState *env)
2763 {
2764     switch (arm_mmu_idx(env)) {
2765     case ARMMMUIdx_E20_0:
2766     case ARMMMUIdx_E20_2:
2767     case ARMMMUIdx_E20_2_PAN:
2768         return GTIMER_HYPVIRT;
2769     default:
2770         return GTIMER_VIRT;
2771     }
2772 }
2773 
2774 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2775                                         const ARMCPRegInfo *ri)
2776 {
2777     int timeridx = gt_phys_redir_timeridx(env);
2778     return env->cp15.c14_timer[timeridx].cval;
2779 }
2780 
2781 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2782                                      uint64_t value)
2783 {
2784     int timeridx = gt_phys_redir_timeridx(env);
2785     gt_cval_write(env, ri, timeridx, value);
2786 }
2787 
2788 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2789                                         const ARMCPRegInfo *ri)
2790 {
2791     int timeridx = gt_phys_redir_timeridx(env);
2792     return gt_tval_read(env, ri, timeridx);
2793 }
2794 
2795 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2796                                      uint64_t value)
2797 {
2798     int timeridx = gt_phys_redir_timeridx(env);
2799     gt_tval_write(env, ri, timeridx, value);
2800 }
2801 
2802 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2803                                        const ARMCPRegInfo *ri)
2804 {
2805     int timeridx = gt_phys_redir_timeridx(env);
2806     return env->cp15.c14_timer[timeridx].ctl;
2807 }
2808 
2809 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2810                                     uint64_t value)
2811 {
2812     int timeridx = gt_phys_redir_timeridx(env);
2813     gt_ctl_write(env, ri, timeridx, value);
2814 }
2815 
2816 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2817 {
2818     gt_timer_reset(env, ri, GTIMER_VIRT);
2819 }
2820 
2821 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2822                                uint64_t value)
2823 {
2824     gt_cval_write(env, ri, GTIMER_VIRT, value);
2825 }
2826 
2827 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2828 {
2829     return gt_tval_read(env, ri, GTIMER_VIRT);
2830 }
2831 
2832 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2833                                uint64_t value)
2834 {
2835     gt_tval_write(env, ri, GTIMER_VIRT, value);
2836 }
2837 
2838 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2839                               uint64_t value)
2840 {
2841     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2842 }
2843 
2844 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2845                               uint64_t value)
2846 {
2847     ARMCPU *cpu = env_archcpu(env);
2848 
2849     trace_arm_gt_cntvoff_write(value);
2850     raw_write(env, ri, value);
2851     gt_recalc_timer(cpu, GTIMER_VIRT);
2852 }
2853 
2854 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2855                                         const ARMCPRegInfo *ri)
2856 {
2857     int timeridx = gt_virt_redir_timeridx(env);
2858     return env->cp15.c14_timer[timeridx].cval;
2859 }
2860 
2861 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2862                                      uint64_t value)
2863 {
2864     int timeridx = gt_virt_redir_timeridx(env);
2865     gt_cval_write(env, ri, timeridx, value);
2866 }
2867 
2868 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2869                                         const ARMCPRegInfo *ri)
2870 {
2871     int timeridx = gt_virt_redir_timeridx(env);
2872     return gt_tval_read(env, ri, timeridx);
2873 }
2874 
2875 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2876                                      uint64_t value)
2877 {
2878     int timeridx = gt_virt_redir_timeridx(env);
2879     gt_tval_write(env, ri, timeridx, value);
2880 }
2881 
2882 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2883                                        const ARMCPRegInfo *ri)
2884 {
2885     int timeridx = gt_virt_redir_timeridx(env);
2886     return env->cp15.c14_timer[timeridx].ctl;
2887 }
2888 
2889 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2890                                     uint64_t value)
2891 {
2892     int timeridx = gt_virt_redir_timeridx(env);
2893     gt_ctl_write(env, ri, timeridx, value);
2894 }
2895 
2896 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2897 {
2898     gt_timer_reset(env, ri, GTIMER_HYP);
2899 }
2900 
2901 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2902                               uint64_t value)
2903 {
2904     gt_cval_write(env, ri, GTIMER_HYP, value);
2905 }
2906 
2907 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2908 {
2909     return gt_tval_read(env, ri, GTIMER_HYP);
2910 }
2911 
2912 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2913                               uint64_t value)
2914 {
2915     gt_tval_write(env, ri, GTIMER_HYP, value);
2916 }
2917 
2918 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2919                               uint64_t value)
2920 {
2921     gt_ctl_write(env, ri, GTIMER_HYP, value);
2922 }
2923 
2924 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2925 {
2926     gt_timer_reset(env, ri, GTIMER_SEC);
2927 }
2928 
2929 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2930                               uint64_t value)
2931 {
2932     gt_cval_write(env, ri, GTIMER_SEC, value);
2933 }
2934 
2935 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2936 {
2937     return gt_tval_read(env, ri, GTIMER_SEC);
2938 }
2939 
2940 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2941                               uint64_t value)
2942 {
2943     gt_tval_write(env, ri, GTIMER_SEC, value);
2944 }
2945 
2946 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2947                               uint64_t value)
2948 {
2949     gt_ctl_write(env, ri, GTIMER_SEC, value);
2950 }
2951 
2952 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2953 {
2954     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2955 }
2956 
2957 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2958                              uint64_t value)
2959 {
2960     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2961 }
2962 
2963 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2964 {
2965     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2966 }
2967 
2968 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2969                              uint64_t value)
2970 {
2971     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2972 }
2973 
2974 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2975                             uint64_t value)
2976 {
2977     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2978 }
2979 
2980 void arm_gt_ptimer_cb(void *opaque)
2981 {
2982     ARMCPU *cpu = opaque;
2983 
2984     gt_recalc_timer(cpu, GTIMER_PHYS);
2985 }
2986 
2987 void arm_gt_vtimer_cb(void *opaque)
2988 {
2989     ARMCPU *cpu = opaque;
2990 
2991     gt_recalc_timer(cpu, GTIMER_VIRT);
2992 }
2993 
2994 void arm_gt_htimer_cb(void *opaque)
2995 {
2996     ARMCPU *cpu = opaque;
2997 
2998     gt_recalc_timer(cpu, GTIMER_HYP);
2999 }
3000 
3001 void arm_gt_stimer_cb(void *opaque)
3002 {
3003     ARMCPU *cpu = opaque;
3004 
3005     gt_recalc_timer(cpu, GTIMER_SEC);
3006 }
3007 
3008 void arm_gt_hvtimer_cb(void *opaque)
3009 {
3010     ARMCPU *cpu = opaque;
3011 
3012     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3013 }
3014 
3015 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3016 {
3017     ARMCPU *cpu = env_archcpu(env);
3018 
3019     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3020 }
3021 
3022 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3023     /*
3024      * Note that CNTFRQ is purely reads-as-written for the benefit
3025      * of software; writing it doesn't actually change the timer frequency.
3026      * Our reset value matches the fixed frequency we implement the timer at.
3027      */
3028     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3029       .type = ARM_CP_ALIAS,
3030       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3031       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3032     },
3033     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3034       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3035       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3036       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3037       .resetfn = arm_gt_cntfrq_reset,
3038     },
3039     /* overall control: mostly access permissions */
3040     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3041       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3042       .access = PL1_RW,
3043       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3044       .resetvalue = 0,
3045     },
3046     /* per-timer control */
3047     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3048       .secure = ARM_CP_SECSTATE_NS,
3049       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3050       .accessfn = gt_ptimer_access,
3051       .fieldoffset = offsetoflow32(CPUARMState,
3052                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3053       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3054       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3055     },
3056     { .name = "CNTP_CTL_S",
3057       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3058       .secure = ARM_CP_SECSTATE_S,
3059       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3060       .accessfn = gt_ptimer_access,
3061       .fieldoffset = offsetoflow32(CPUARMState,
3062                                    cp15.c14_timer[GTIMER_SEC].ctl),
3063       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3064     },
3065     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3066       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3067       .type = ARM_CP_IO, .access = PL0_RW,
3068       .accessfn = gt_ptimer_access,
3069       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3070       .resetvalue = 0,
3071       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3072       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3073     },
3074     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3075       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3076       .accessfn = gt_vtimer_access,
3077       .fieldoffset = offsetoflow32(CPUARMState,
3078                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3079       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3080       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3081     },
3082     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3083       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3084       .type = ARM_CP_IO, .access = PL0_RW,
3085       .accessfn = gt_vtimer_access,
3086       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3087       .resetvalue = 0,
3088       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3089       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3090     },
3091     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3092     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3093       .secure = ARM_CP_SECSTATE_NS,
3094       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3095       .accessfn = gt_ptimer_access,
3096       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3097     },
3098     { .name = "CNTP_TVAL_S",
3099       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3100       .secure = ARM_CP_SECSTATE_S,
3101       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3102       .accessfn = gt_ptimer_access,
3103       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3104     },
3105     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3106       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3107       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3108       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3109       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3110     },
3111     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3112       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3113       .accessfn = gt_vtimer_access,
3114       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3115     },
3116     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3117       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3118       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3119       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3120       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3121     },
3122     /* The counter itself */
3123     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3124       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3125       .accessfn = gt_pct_access,
3126       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3127     },
3128     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3129       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3130       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3131       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3132     },
3133     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3134       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3135       .accessfn = gt_vct_access,
3136       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3137     },
3138     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3139       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3140       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3141       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3142     },
3143     /* Comparison value, indicating when the timer goes off */
3144     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3145       .secure = ARM_CP_SECSTATE_NS,
3146       .access = PL0_RW,
3147       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3148       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3149       .accessfn = gt_ptimer_access,
3150       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3151       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3152     },
3153     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3154       .secure = ARM_CP_SECSTATE_S,
3155       .access = PL0_RW,
3156       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3157       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3158       .accessfn = gt_ptimer_access,
3159       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3160     },
3161     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3162       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3163       .access = PL0_RW,
3164       .type = ARM_CP_IO,
3165       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3166       .resetvalue = 0, .accessfn = gt_ptimer_access,
3167       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3168       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3169     },
3170     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3171       .access = PL0_RW,
3172       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3173       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3174       .accessfn = gt_vtimer_access,
3175       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3176       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3177     },
3178     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3179       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3180       .access = PL0_RW,
3181       .type = ARM_CP_IO,
3182       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3183       .resetvalue = 0, .accessfn = gt_vtimer_access,
3184       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3185       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3186     },
3187     /*
3188      * Secure timer -- this is actually restricted to only EL3
3189      * and configurably Secure-EL1 via the accessfn.
3190      */
3191     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3192       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3193       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3194       .accessfn = gt_stimer_access,
3195       .readfn = gt_sec_tval_read,
3196       .writefn = gt_sec_tval_write,
3197       .resetfn = gt_sec_timer_reset,
3198     },
3199     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3200       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3201       .type = ARM_CP_IO, .access = PL1_RW,
3202       .accessfn = gt_stimer_access,
3203       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3204       .resetvalue = 0,
3205       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3206     },
3207     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3208       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3209       .type = ARM_CP_IO, .access = PL1_RW,
3210       .accessfn = gt_stimer_access,
3211       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3212       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3213     },
3214 };
3215 
3216 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3217                                  bool isread)
3218 {
3219     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3220         return CP_ACCESS_TRAP;
3221     }
3222     return CP_ACCESS_OK;
3223 }
3224 
3225 #else
3226 
3227 /*
3228  * In user-mode most of the generic timer registers are inaccessible
3229  * however modern kernels (4.12+) allow access to cntvct_el0
3230  */
3231 
3232 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3233 {
3234     ARMCPU *cpu = env_archcpu(env);
3235 
3236     /*
3237      * Currently we have no support for QEMUTimer in linux-user so we
3238      * can't call gt_get_countervalue(env), instead we directly
3239      * call the lower level functions.
3240      */
3241     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3242 }
3243 
3244 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3245     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3246       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3247       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3248       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3249       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3250     },
3251     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3252       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3253       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3254       .readfn = gt_virt_cnt_read,
3255     },
3256 };
3257 
3258 #endif
3259 
3260 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3261 {
3262     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3263         raw_write(env, ri, value);
3264     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3265         raw_write(env, ri, value & 0xfffff6ff);
3266     } else {
3267         raw_write(env, ri, value & 0xfffff1ff);
3268     }
3269 }
3270 
3271 #ifndef CONFIG_USER_ONLY
3272 /* get_phys_addr() isn't present for user-mode-only targets */
3273 
3274 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3275                                  bool isread)
3276 {
3277     if (ri->opc2 & 4) {
3278         /*
3279          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3280          * Secure EL1 (which can only happen if EL3 is AArch64).
3281          * They are simply UNDEF if executed from NS EL1.
3282          * They function normally from EL2 or EL3.
3283          */
3284         if (arm_current_el(env) == 1) {
3285             if (arm_is_secure_below_el3(env)) {
3286                 if (env->cp15.scr_el3 & SCR_EEL2) {
3287                     return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3288                 }
3289                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3290             }
3291             return CP_ACCESS_TRAP_UNCATEGORIZED;
3292         }
3293     }
3294     return CP_ACCESS_OK;
3295 }
3296 
3297 #ifdef CONFIG_TCG
3298 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3299                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3300                              bool is_secure)
3301 {
3302     bool ret;
3303     uint64_t par64;
3304     bool format64 = false;
3305     ARMMMUFaultInfo fi = {};
3306     GetPhysAddrResult res = {};
3307 
3308     ret = get_phys_addr_with_secure(env, value, access_type, mmu_idx,
3309                                     is_secure, &res, &fi);
3310 
3311     /*
3312      * ATS operations only do S1 or S1+S2 translations, so we never
3313      * have to deal with the ARMCacheAttrs format for S2 only.
3314      */
3315     assert(!res.cacheattrs.is_s2_format);
3316 
3317     if (ret) {
3318         /*
3319          * Some kinds of translation fault must cause exceptions rather
3320          * than being reported in the PAR.
3321          */
3322         int current_el = arm_current_el(env);
3323         int target_el;
3324         uint32_t syn, fsr, fsc;
3325         bool take_exc = false;
3326 
3327         if (fi.s1ptw && current_el == 1
3328             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3329             /*
3330              * Synchronous stage 2 fault on an access made as part of the
3331              * translation table walk for AT S1E0* or AT S1E1* insn
3332              * executed from NS EL1. If this is a synchronous external abort
3333              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3334              * to EL3. Otherwise the fault is taken as an exception to EL2,
3335              * and HPFAR_EL2 holds the faulting IPA.
3336              */
3337             if (fi.type == ARMFault_SyncExternalOnWalk &&
3338                 (env->cp15.scr_el3 & SCR_EA)) {
3339                 target_el = 3;
3340             } else {
3341                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3342                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3343                     env->cp15.hpfar_el2 |= HPFAR_NS;
3344                 }
3345                 target_el = 2;
3346             }
3347             take_exc = true;
3348         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3349             /*
3350              * Synchronous external aborts during a translation table walk
3351              * are taken as Data Abort exceptions.
3352              */
3353             if (fi.stage2) {
3354                 if (current_el == 3) {
3355                     target_el = 3;
3356                 } else {
3357                     target_el = 2;
3358                 }
3359             } else {
3360                 target_el = exception_target_el(env);
3361             }
3362             take_exc = true;
3363         }
3364 
3365         if (take_exc) {
3366             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3367             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3368                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3369                 fsr = arm_fi_to_lfsc(&fi);
3370                 fsc = extract32(fsr, 0, 6);
3371             } else {
3372                 fsr = arm_fi_to_sfsc(&fi);
3373                 fsc = 0x3f;
3374             }
3375             /*
3376              * Report exception with ESR indicating a fault due to a
3377              * translation table walk for a cache maintenance instruction.
3378              */
3379             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3380                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3381             env->exception.vaddress = value;
3382             env->exception.fsr = fsr;
3383             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3384         }
3385     }
3386 
3387     if (is_a64(env)) {
3388         format64 = true;
3389     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3390         /*
3391          * ATS1Cxx:
3392          * * TTBCR.EAE determines whether the result is returned using the
3393          *   32-bit or the 64-bit PAR format
3394          * * Instructions executed in Hyp mode always use the 64bit format
3395          *
3396          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3397          * * The Non-secure TTBCR.EAE bit is set to 1
3398          * * The implementation includes EL2, and the value of HCR.VM is 1
3399          *
3400          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3401          *
3402          * ATS1Hx always uses the 64bit format.
3403          */
3404         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3405 
3406         if (arm_feature(env, ARM_FEATURE_EL2)) {
3407             if (mmu_idx == ARMMMUIdx_E10_0 ||
3408                 mmu_idx == ARMMMUIdx_E10_1 ||
3409                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3410                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3411             } else {
3412                 format64 |= arm_current_el(env) == 2;
3413             }
3414         }
3415     }
3416 
3417     if (format64) {
3418         /* Create a 64-bit PAR */
3419         par64 = (1 << 11); /* LPAE bit always set */
3420         if (!ret) {
3421             par64 |= res.f.phys_addr & ~0xfffULL;
3422             if (!res.f.attrs.secure) {
3423                 par64 |= (1 << 9); /* NS */
3424             }
3425             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3426             par64 |= res.cacheattrs.shareability << 7; /* SH */
3427         } else {
3428             uint32_t fsr = arm_fi_to_lfsc(&fi);
3429 
3430             par64 |= 1; /* F */
3431             par64 |= (fsr & 0x3f) << 1; /* FS */
3432             if (fi.stage2) {
3433                 par64 |= (1 << 9); /* S */
3434             }
3435             if (fi.s1ptw) {
3436                 par64 |= (1 << 8); /* PTW */
3437             }
3438         }
3439     } else {
3440         /*
3441          * fsr is a DFSR/IFSR value for the short descriptor
3442          * translation table format (with WnR always clear).
3443          * Convert it to a 32-bit PAR.
3444          */
3445         if (!ret) {
3446             /* We do not set any attribute bits in the PAR */
3447             if (res.f.lg_page_size == 24
3448                 && arm_feature(env, ARM_FEATURE_V7)) {
3449                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3450             } else {
3451                 par64 = res.f.phys_addr & 0xfffff000;
3452             }
3453             if (!res.f.attrs.secure) {
3454                 par64 |= (1 << 9); /* NS */
3455             }
3456         } else {
3457             uint32_t fsr = arm_fi_to_sfsc(&fi);
3458 
3459             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3460                     ((fsr & 0xf) << 1) | 1;
3461         }
3462     }
3463     return par64;
3464 }
3465 #endif /* CONFIG_TCG */
3466 
3467 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3468 {
3469 #ifdef CONFIG_TCG
3470     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3471     uint64_t par64;
3472     ARMMMUIdx mmu_idx;
3473     int el = arm_current_el(env);
3474     bool secure = arm_is_secure_below_el3(env);
3475 
3476     switch (ri->opc2 & 6) {
3477     case 0:
3478         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3479         switch (el) {
3480         case 3:
3481             mmu_idx = ARMMMUIdx_E3;
3482             secure = true;
3483             break;
3484         case 2:
3485             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3486             /* fall through */
3487         case 1:
3488             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3489                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3490             } else {
3491                 mmu_idx = ARMMMUIdx_Stage1_E1;
3492             }
3493             break;
3494         default:
3495             g_assert_not_reached();
3496         }
3497         break;
3498     case 2:
3499         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3500         switch (el) {
3501         case 3:
3502             mmu_idx = ARMMMUIdx_E10_0;
3503             secure = true;
3504             break;
3505         case 2:
3506             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3507             mmu_idx = ARMMMUIdx_Stage1_E0;
3508             break;
3509         case 1:
3510             mmu_idx = ARMMMUIdx_Stage1_E0;
3511             break;
3512         default:
3513             g_assert_not_reached();
3514         }
3515         break;
3516     case 4:
3517         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3518         mmu_idx = ARMMMUIdx_E10_1;
3519         secure = false;
3520         break;
3521     case 6:
3522         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3523         mmu_idx = ARMMMUIdx_E10_0;
3524         secure = false;
3525         break;
3526     default:
3527         g_assert_not_reached();
3528     }
3529 
3530     par64 = do_ats_write(env, value, access_type, mmu_idx, secure);
3531 
3532     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3533 #else
3534     /* Handled by hardware accelerator. */
3535     g_assert_not_reached();
3536 #endif /* CONFIG_TCG */
3537 }
3538 
3539 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3540                         uint64_t value)
3541 {
3542 #ifdef CONFIG_TCG
3543     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3544     uint64_t par64;
3545 
3546     /* There is no SecureEL2 for AArch32. */
3547     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, false);
3548 
3549     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3550 #else
3551     /* Handled by hardware accelerator. */
3552     g_assert_not_reached();
3553 #endif /* CONFIG_TCG */
3554 }
3555 
3556 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3557                                      bool isread)
3558 {
3559     if (arm_current_el(env) == 3 &&
3560         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3561         return CP_ACCESS_TRAP;
3562     }
3563     return CP_ACCESS_OK;
3564 }
3565 
3566 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3567                         uint64_t value)
3568 {
3569 #ifdef CONFIG_TCG
3570     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3571     ARMMMUIdx mmu_idx;
3572     int secure = arm_is_secure_below_el3(env);
3573     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3574     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3575 
3576     switch (ri->opc2 & 6) {
3577     case 0:
3578         switch (ri->opc1) {
3579         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3580             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3581                 mmu_idx = regime_e20 ?
3582                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3583             } else {
3584                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3585             }
3586             break;
3587         case 4: /* AT S1E2R, AT S1E2W */
3588             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3589             break;
3590         case 6: /* AT S1E3R, AT S1E3W */
3591             mmu_idx = ARMMMUIdx_E3;
3592             secure = true;
3593             break;
3594         default:
3595             g_assert_not_reached();
3596         }
3597         break;
3598     case 2: /* AT S1E0R, AT S1E0W */
3599         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3600         break;
3601     case 4: /* AT S12E1R, AT S12E1W */
3602         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3603         break;
3604     case 6: /* AT S12E0R, AT S12E0W */
3605         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3606         break;
3607     default:
3608         g_assert_not_reached();
3609     }
3610 
3611     env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3612                                        mmu_idx, secure);
3613 #else
3614     /* Handled by hardware accelerator. */
3615     g_assert_not_reached();
3616 #endif /* CONFIG_TCG */
3617 }
3618 #endif
3619 
3620 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3621     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3622       .access = PL1_RW, .resetvalue = 0,
3623       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3624                              offsetoflow32(CPUARMState, cp15.par_ns) },
3625       .writefn = par_write },
3626 #ifndef CONFIG_USER_ONLY
3627     /* This underdecoding is safe because the reginfo is NO_RAW. */
3628     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3629       .access = PL1_W, .accessfn = ats_access,
3630       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3631 #endif
3632 };
3633 
3634 /* Return basic MPU access permission bits.  */
3635 static uint32_t simple_mpu_ap_bits(uint32_t val)
3636 {
3637     uint32_t ret;
3638     uint32_t mask;
3639     int i;
3640     ret = 0;
3641     mask = 3;
3642     for (i = 0; i < 16; i += 2) {
3643         ret |= (val >> i) & mask;
3644         mask <<= 2;
3645     }
3646     return ret;
3647 }
3648 
3649 /* Pad basic MPU access permission bits to extended format.  */
3650 static uint32_t extended_mpu_ap_bits(uint32_t val)
3651 {
3652     uint32_t ret;
3653     uint32_t mask;
3654     int i;
3655     ret = 0;
3656     mask = 3;
3657     for (i = 0; i < 16; i += 2) {
3658         ret |= (val & mask) << i;
3659         mask <<= 2;
3660     }
3661     return ret;
3662 }
3663 
3664 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3665                                  uint64_t value)
3666 {
3667     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3668 }
3669 
3670 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3671 {
3672     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3673 }
3674 
3675 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3676                                  uint64_t value)
3677 {
3678     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3679 }
3680 
3681 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3682 {
3683     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3684 }
3685 
3686 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3687 {
3688     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3689 
3690     if (!u32p) {
3691         return 0;
3692     }
3693 
3694     u32p += env->pmsav7.rnr[M_REG_NS];
3695     return *u32p;
3696 }
3697 
3698 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3699                          uint64_t value)
3700 {
3701     ARMCPU *cpu = env_archcpu(env);
3702     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3703 
3704     if (!u32p) {
3705         return;
3706     }
3707 
3708     u32p += env->pmsav7.rnr[M_REG_NS];
3709     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3710     *u32p = value;
3711 }
3712 
3713 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3714                               uint64_t value)
3715 {
3716     ARMCPU *cpu = env_archcpu(env);
3717     uint32_t nrgs = cpu->pmsav7_dregion;
3718 
3719     if (value >= nrgs) {
3720         qemu_log_mask(LOG_GUEST_ERROR,
3721                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3722                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3723         return;
3724     }
3725 
3726     raw_write(env, ri, value);
3727 }
3728 
3729 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3730                           uint64_t value)
3731 {
3732     ARMCPU *cpu = env_archcpu(env);
3733 
3734     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3735     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3736 }
3737 
3738 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3739 {
3740     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3741 }
3742 
3743 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3744                           uint64_t value)
3745 {
3746     ARMCPU *cpu = env_archcpu(env);
3747 
3748     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3749     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3750 }
3751 
3752 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3753 {
3754     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3755 }
3756 
3757 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3758                            uint64_t value)
3759 {
3760     ARMCPU *cpu = env_archcpu(env);
3761 
3762     /*
3763      * Ignore writes that would select not implemented region.
3764      * This is architecturally UNPREDICTABLE.
3765      */
3766     if (value >= cpu->pmsav7_dregion) {
3767         return;
3768     }
3769 
3770     env->pmsav7.rnr[M_REG_NS] = value;
3771 }
3772 
3773 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3774                           uint64_t value)
3775 {
3776     ARMCPU *cpu = env_archcpu(env);
3777 
3778     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3779     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3780 }
3781 
3782 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3783 {
3784     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3785 }
3786 
3787 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3788                           uint64_t value)
3789 {
3790     ARMCPU *cpu = env_archcpu(env);
3791 
3792     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3793     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3794 }
3795 
3796 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3797 {
3798     return env->pmsav8.hprlar[env->pmsav8.hprselr];
3799 }
3800 
3801 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3802                           uint64_t value)
3803 {
3804     uint32_t n;
3805     uint32_t bit;
3806     ARMCPU *cpu = env_archcpu(env);
3807 
3808     /* Ignore writes to unimplemented regions */
3809     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3810     value &= MAKE_64BIT_MASK(0, rmax);
3811 
3812     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3813 
3814     /* Register alias is only valid for first 32 indexes */
3815     for (n = 0; n < rmax; ++n) {
3816         bit = extract32(value, n, 1);
3817         env->pmsav8.hprlar[n] = deposit32(
3818                     env->pmsav8.hprlar[n], 0, 1, bit);
3819     }
3820 }
3821 
3822 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3823 {
3824     uint32_t n;
3825     uint32_t result = 0x0;
3826     ARMCPU *cpu = env_archcpu(env);
3827 
3828     /* Register alias is only valid for first 32 indexes */
3829     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3830         if (env->pmsav8.hprlar[n] & 0x1) {
3831             result |= (0x1 << n);
3832         }
3833     }
3834     return result;
3835 }
3836 
3837 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3838                            uint64_t value)
3839 {
3840     ARMCPU *cpu = env_archcpu(env);
3841 
3842     /*
3843      * Ignore writes that would select not implemented region.
3844      * This is architecturally UNPREDICTABLE.
3845      */
3846     if (value >= cpu->pmsav8r_hdregion) {
3847         return;
3848     }
3849 
3850     env->pmsav8.hprselr = value;
3851 }
3852 
3853 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
3854                           uint64_t value)
3855 {
3856     ARMCPU *cpu = env_archcpu(env);
3857     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3858                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3859 
3860     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3861 
3862     if (ri->opc1 & 4) {
3863         if (index >= cpu->pmsav8r_hdregion) {
3864             return;
3865         }
3866         if (ri->opc2 & 0x1) {
3867             env->pmsav8.hprlar[index] = value;
3868         } else {
3869             env->pmsav8.hprbar[index] = value;
3870         }
3871     } else {
3872         if (index >= cpu->pmsav7_dregion) {
3873             return;
3874         }
3875         if (ri->opc2 & 0x1) {
3876             env->pmsav8.rlar[M_REG_NS][index] = value;
3877         } else {
3878             env->pmsav8.rbar[M_REG_NS][index] = value;
3879         }
3880     }
3881 }
3882 
3883 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
3884 {
3885     ARMCPU *cpu = env_archcpu(env);
3886     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3887                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3888 
3889     if (ri->opc1 & 4) {
3890         if (index >= cpu->pmsav8r_hdregion) {
3891             return 0x0;
3892         }
3893         if (ri->opc2 & 0x1) {
3894             return env->pmsav8.hprlar[index];
3895         } else {
3896             return env->pmsav8.hprbar[index];
3897         }
3898     } else {
3899         if (index >= cpu->pmsav7_dregion) {
3900             return 0x0;
3901         }
3902         if (ri->opc2 & 0x1) {
3903             return env->pmsav8.rlar[M_REG_NS][index];
3904         } else {
3905             return env->pmsav8.rbar[M_REG_NS][index];
3906         }
3907     }
3908 }
3909 
3910 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
3911     { .name = "PRBAR",
3912       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
3913       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3914       .accessfn = access_tvm_trvm,
3915       .readfn = prbar_read, .writefn = prbar_write },
3916     { .name = "PRLAR",
3917       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
3918       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3919       .accessfn = access_tvm_trvm,
3920       .readfn = prlar_read, .writefn = prlar_write },
3921     { .name = "PRSELR", .resetvalue = 0,
3922       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
3923       .access = PL1_RW, .accessfn = access_tvm_trvm,
3924       .writefn = prselr_write,
3925       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
3926     { .name = "HPRBAR", .resetvalue = 0,
3927       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
3928       .access = PL2_RW, .type = ARM_CP_NO_RAW,
3929       .readfn = hprbar_read, .writefn = hprbar_write },
3930     { .name = "HPRLAR",
3931       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
3932       .access = PL2_RW, .type = ARM_CP_NO_RAW,
3933       .readfn = hprlar_read, .writefn = hprlar_write },
3934     { .name = "HPRSELR", .resetvalue = 0,
3935       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
3936       .access = PL2_RW,
3937       .writefn = hprselr_write,
3938       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
3939     { .name = "HPRENR",
3940       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
3941       .access = PL2_RW, .type = ARM_CP_NO_RAW,
3942       .readfn = hprenr_read, .writefn = hprenr_write },
3943 };
3944 
3945 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3946     /*
3947      * Reset for all these registers is handled in arm_cpu_reset(),
3948      * because the PMSAv7 is also used by M-profile CPUs, which do
3949      * not register cpregs but still need the state to be reset.
3950      */
3951     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3952       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3953       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3954       .readfn = pmsav7_read, .writefn = pmsav7_write,
3955       .resetfn = arm_cp_reset_ignore },
3956     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3957       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3958       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3959       .readfn = pmsav7_read, .writefn = pmsav7_write,
3960       .resetfn = arm_cp_reset_ignore },
3961     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3962       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3963       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3964       .readfn = pmsav7_read, .writefn = pmsav7_write,
3965       .resetfn = arm_cp_reset_ignore },
3966     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3967       .access = PL1_RW,
3968       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3969       .writefn = pmsav7_rgnr_write,
3970       .resetfn = arm_cp_reset_ignore },
3971 };
3972 
3973 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3974     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3975       .access = PL1_RW, .type = ARM_CP_ALIAS,
3976       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3977       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3978     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3979       .access = PL1_RW, .type = ARM_CP_ALIAS,
3980       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3981       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3982     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3983       .access = PL1_RW,
3984       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3985       .resetvalue = 0, },
3986     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3987       .access = PL1_RW,
3988       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3989       .resetvalue = 0, },
3990     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3991       .access = PL1_RW,
3992       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3993     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3994       .access = PL1_RW,
3995       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3996     /* Protection region base and size registers */
3997     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3998       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3999       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4000     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4001       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4002       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4003     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4004       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4005       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4006     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4007       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4008       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4009     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4010       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4011       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4012     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4013       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4014       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4015     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4016       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4017       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4018     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4019       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4020       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4021 };
4022 
4023 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4024                              uint64_t value)
4025 {
4026     ARMCPU *cpu = env_archcpu(env);
4027 
4028     if (!arm_feature(env, ARM_FEATURE_V8)) {
4029         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4030             /*
4031              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4032              * using Long-descriptor translation table format
4033              */
4034             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4035         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4036             /*
4037              * In an implementation that includes the Security Extensions
4038              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4039              * Short-descriptor translation table format.
4040              */
4041             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4042         } else {
4043             value &= TTBCR_N;
4044         }
4045     }
4046 
4047     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4048         /*
4049          * With LPAE the TTBCR could result in a change of ASID
4050          * via the TTBCR.A1 bit, so do a TLB flush.
4051          */
4052         tlb_flush(CPU(cpu));
4053     }
4054     raw_write(env, ri, value);
4055 }
4056 
4057 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4058                                uint64_t value)
4059 {
4060     ARMCPU *cpu = env_archcpu(env);
4061 
4062     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4063     tlb_flush(CPU(cpu));
4064     raw_write(env, ri, value);
4065 }
4066 
4067 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4068                             uint64_t value)
4069 {
4070     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4071     if (cpreg_field_is_64bit(ri) &&
4072         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4073         ARMCPU *cpu = env_archcpu(env);
4074         tlb_flush(CPU(cpu));
4075     }
4076     raw_write(env, ri, value);
4077 }
4078 
4079 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4080                                     uint64_t value)
4081 {
4082     /*
4083      * If we are running with E2&0 regime, then an ASID is active.
4084      * Flush if that might be changing.  Note we're not checking
4085      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4086      * holds the active ASID, only checking the field that might.
4087      */
4088     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4089         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4090         uint16_t mask = ARMMMUIdxBit_E20_2 |
4091                         ARMMMUIdxBit_E20_2_PAN |
4092                         ARMMMUIdxBit_E20_0;
4093         tlb_flush_by_mmuidx(env_cpu(env), mask);
4094     }
4095     raw_write(env, ri, value);
4096 }
4097 
4098 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4099                         uint64_t value)
4100 {
4101     ARMCPU *cpu = env_archcpu(env);
4102     CPUState *cs = CPU(cpu);
4103 
4104     /*
4105      * A change in VMID to the stage2 page table (Stage2) invalidates
4106      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4107      */
4108     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4109         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4110     }
4111     raw_write(env, ri, value);
4112 }
4113 
4114 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4115     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4116       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4117       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4118                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4119     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4120       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4121       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4122                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4123     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4124       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4125       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4126                              offsetof(CPUARMState, cp15.dfar_ns) } },
4127     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4128       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4129       .access = PL1_RW, .accessfn = access_tvm_trvm,
4130       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4131       .resetvalue = 0, },
4132 };
4133 
4134 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4135     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4136       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4137       .access = PL1_RW, .accessfn = access_tvm_trvm,
4138       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4139     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4140       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4141       .access = PL1_RW, .accessfn = access_tvm_trvm,
4142       .writefn = vmsa_ttbr_write, .resetvalue = 0,
4143       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4144                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4145     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4146       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4147       .access = PL1_RW, .accessfn = access_tvm_trvm,
4148       .writefn = vmsa_ttbr_write, .resetvalue = 0,
4149       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4150                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4151     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4152       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4153       .access = PL1_RW, .accessfn = access_tvm_trvm,
4154       .writefn = vmsa_tcr_el12_write,
4155       .raw_writefn = raw_write,
4156       .resetvalue = 0,
4157       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4158     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4159       .access = PL1_RW, .accessfn = access_tvm_trvm,
4160       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4161       .raw_writefn = raw_write,
4162       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4163                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4164 };
4165 
4166 /*
4167  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4168  * qemu tlbs nor adjusting cached masks.
4169  */
4170 static const ARMCPRegInfo ttbcr2_reginfo = {
4171     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4172     .access = PL1_RW, .accessfn = access_tvm_trvm,
4173     .type = ARM_CP_ALIAS,
4174     .bank_fieldoffsets = {
4175         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4176         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4177     },
4178 };
4179 
4180 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4181                                 uint64_t value)
4182 {
4183     env->cp15.c15_ticonfig = value & 0xe7;
4184     /* The OS_TYPE bit in this register changes the reported CPUID! */
4185     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4186         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4187 }
4188 
4189 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4190                                 uint64_t value)
4191 {
4192     env->cp15.c15_threadid = value & 0xffff;
4193 }
4194 
4195 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4196                            uint64_t value)
4197 {
4198     /* Wait-for-interrupt (deprecated) */
4199     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4200 }
4201 
4202 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4203                                   uint64_t value)
4204 {
4205     /*
4206      * On OMAP there are registers indicating the max/min index of dcache lines
4207      * containing a dirty line; cache flush operations have to reset these.
4208      */
4209     env->cp15.c15_i_max = 0x000;
4210     env->cp15.c15_i_min = 0xff0;
4211 }
4212 
4213 static const ARMCPRegInfo omap_cp_reginfo[] = {
4214     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4215       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4216       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4217       .resetvalue = 0, },
4218     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4219       .access = PL1_RW, .type = ARM_CP_NOP },
4220     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4221       .access = PL1_RW,
4222       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4223       .writefn = omap_ticonfig_write },
4224     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4225       .access = PL1_RW,
4226       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4227     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4228       .access = PL1_RW, .resetvalue = 0xff0,
4229       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4230     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4231       .access = PL1_RW,
4232       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4233       .writefn = omap_threadid_write },
4234     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4235       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4236       .type = ARM_CP_NO_RAW,
4237       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4238     /*
4239      * TODO: Peripheral port remap register:
4240      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4241      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4242      * when MMU is off.
4243      */
4244     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4245       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4246       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4247       .writefn = omap_cachemaint_write },
4248     { .name = "C9", .cp = 15, .crn = 9,
4249       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4250       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4251 };
4252 
4253 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4254                               uint64_t value)
4255 {
4256     env->cp15.c15_cpar = value & 0x3fff;
4257 }
4258 
4259 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4260     { .name = "XSCALE_CPAR",
4261       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4262       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4263       .writefn = xscale_cpar_write, },
4264     { .name = "XSCALE_AUXCR",
4265       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4266       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4267       .resetvalue = 0, },
4268     /*
4269      * XScale specific cache-lockdown: since we have no cache we NOP these
4270      * and hope the guest does not really rely on cache behaviour.
4271      */
4272     { .name = "XSCALE_LOCK_ICACHE_LINE",
4273       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4274       .access = PL1_W, .type = ARM_CP_NOP },
4275     { .name = "XSCALE_UNLOCK_ICACHE",
4276       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4277       .access = PL1_W, .type = ARM_CP_NOP },
4278     { .name = "XSCALE_DCACHE_LOCK",
4279       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4280       .access = PL1_RW, .type = ARM_CP_NOP },
4281     { .name = "XSCALE_UNLOCK_DCACHE",
4282       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4283       .access = PL1_W, .type = ARM_CP_NOP },
4284 };
4285 
4286 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4287     /*
4288      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4289      * implementation of this implementation-defined space.
4290      * Ideally this should eventually disappear in favour of actually
4291      * implementing the correct behaviour for all cores.
4292      */
4293     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4294       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4295       .access = PL1_RW,
4296       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4297       .resetvalue = 0 },
4298 };
4299 
4300 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4301     /* Cache status: RAZ because we have no cache so it's always clean */
4302     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4303       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4304       .resetvalue = 0 },
4305 };
4306 
4307 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4308     /* We never have a block transfer operation in progress */
4309     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4310       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4311       .resetvalue = 0 },
4312     /* The cache ops themselves: these all NOP for QEMU */
4313     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4314       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4315     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4316       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4317     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4318       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4319     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4320       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4321     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4322       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4323     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4324       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4325 };
4326 
4327 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4328     /*
4329      * The cache test-and-clean instructions always return (1 << 30)
4330      * to indicate that there are no dirty cache lines.
4331      */
4332     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4333       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4334       .resetvalue = (1 << 30) },
4335     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4336       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4337       .resetvalue = (1 << 30) },
4338 };
4339 
4340 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4341     /* Ignore ReadBuffer accesses */
4342     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4343       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4344       .access = PL1_RW, .resetvalue = 0,
4345       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4346 };
4347 
4348 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4349 {
4350     unsigned int cur_el = arm_current_el(env);
4351 
4352     if (arm_is_el2_enabled(env) && cur_el == 1) {
4353         return env->cp15.vpidr_el2;
4354     }
4355     return raw_read(env, ri);
4356 }
4357 
4358 static uint64_t mpidr_read_val(CPUARMState *env)
4359 {
4360     ARMCPU *cpu = env_archcpu(env);
4361     uint64_t mpidr = cpu->mp_affinity;
4362 
4363     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4364         mpidr |= (1U << 31);
4365         /*
4366          * Cores which are uniprocessor (non-coherent)
4367          * but still implement the MP extensions set
4368          * bit 30. (For instance, Cortex-R5).
4369          */
4370         if (cpu->mp_is_up) {
4371             mpidr |= (1u << 30);
4372         }
4373     }
4374     return mpidr;
4375 }
4376 
4377 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4378 {
4379     unsigned int cur_el = arm_current_el(env);
4380 
4381     if (arm_is_el2_enabled(env) && cur_el == 1) {
4382         return env->cp15.vmpidr_el2;
4383     }
4384     return mpidr_read_val(env);
4385 }
4386 
4387 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4388     /* NOP AMAIR0/1 */
4389     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4390       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4391       .access = PL1_RW, .accessfn = access_tvm_trvm,
4392       .type = ARM_CP_CONST, .resetvalue = 0 },
4393     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4394     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4395       .access = PL1_RW, .accessfn = access_tvm_trvm,
4396       .type = ARM_CP_CONST, .resetvalue = 0 },
4397     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4398       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4399       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4400                              offsetof(CPUARMState, cp15.par_ns)} },
4401     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4402       .access = PL1_RW, .accessfn = access_tvm_trvm,
4403       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4404       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4405                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4406       .writefn = vmsa_ttbr_write, },
4407     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4408       .access = PL1_RW, .accessfn = access_tvm_trvm,
4409       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4410       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4411                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4412       .writefn = vmsa_ttbr_write, },
4413 };
4414 
4415 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4416 {
4417     return vfp_get_fpcr(env);
4418 }
4419 
4420 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4421                             uint64_t value)
4422 {
4423     vfp_set_fpcr(env, value);
4424 }
4425 
4426 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4427 {
4428     return vfp_get_fpsr(env);
4429 }
4430 
4431 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4432                             uint64_t value)
4433 {
4434     vfp_set_fpsr(env, value);
4435 }
4436 
4437 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4438                                        bool isread)
4439 {
4440     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4441         return CP_ACCESS_TRAP;
4442     }
4443     return CP_ACCESS_OK;
4444 }
4445 
4446 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4447                             uint64_t value)
4448 {
4449     env->daif = value & PSTATE_DAIF;
4450 }
4451 
4452 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4453 {
4454     return env->pstate & PSTATE_PAN;
4455 }
4456 
4457 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4458                            uint64_t value)
4459 {
4460     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4461 }
4462 
4463 static const ARMCPRegInfo pan_reginfo = {
4464     .name = "PAN", .state = ARM_CP_STATE_AA64,
4465     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4466     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4467     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4468 };
4469 
4470 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4471 {
4472     return env->pstate & PSTATE_UAO;
4473 }
4474 
4475 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4476                            uint64_t value)
4477 {
4478     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4479 }
4480 
4481 static const ARMCPRegInfo uao_reginfo = {
4482     .name = "UAO", .state = ARM_CP_STATE_AA64,
4483     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4484     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4485     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4486 };
4487 
4488 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4489 {
4490     return env->pstate & PSTATE_DIT;
4491 }
4492 
4493 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4494                            uint64_t value)
4495 {
4496     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4497 }
4498 
4499 static const ARMCPRegInfo dit_reginfo = {
4500     .name = "DIT", .state = ARM_CP_STATE_AA64,
4501     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4502     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4503     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4504 };
4505 
4506 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4507 {
4508     return env->pstate & PSTATE_SSBS;
4509 }
4510 
4511 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4512                            uint64_t value)
4513 {
4514     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4515 }
4516 
4517 static const ARMCPRegInfo ssbs_reginfo = {
4518     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4519     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4520     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4521     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4522 };
4523 
4524 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4525                                               const ARMCPRegInfo *ri,
4526                                               bool isread)
4527 {
4528     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4529     switch (arm_current_el(env)) {
4530     case 0:
4531         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4532         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4533             return CP_ACCESS_TRAP;
4534         }
4535         /* fall through */
4536     case 1:
4537         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4538         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4539             return CP_ACCESS_TRAP_EL2;
4540         }
4541         break;
4542     }
4543     return CP_ACCESS_OK;
4544 }
4545 
4546 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4547 {
4548     /* Cache invalidate/clean to Point of Unification... */
4549     switch (arm_current_el(env)) {
4550     case 0:
4551         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4552         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4553             return CP_ACCESS_TRAP;
4554         }
4555         /* fall through */
4556     case 1:
4557         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4558         if (arm_hcr_el2_eff(env) & hcrflags) {
4559             return CP_ACCESS_TRAP_EL2;
4560         }
4561         break;
4562     }
4563     return CP_ACCESS_OK;
4564 }
4565 
4566 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4567                                    bool isread)
4568 {
4569     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4570 }
4571 
4572 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4573                                   bool isread)
4574 {
4575     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4576 }
4577 
4578 /*
4579  * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4580  * Page D4-1736 (DDI0487A.b)
4581  */
4582 
4583 static int vae1_tlbmask(CPUARMState *env)
4584 {
4585     uint64_t hcr = arm_hcr_el2_eff(env);
4586     uint16_t mask;
4587 
4588     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4589         mask = ARMMMUIdxBit_E20_2 |
4590                ARMMMUIdxBit_E20_2_PAN |
4591                ARMMMUIdxBit_E20_0;
4592     } else {
4593         mask = ARMMMUIdxBit_E10_1 |
4594                ARMMMUIdxBit_E10_1_PAN |
4595                ARMMMUIdxBit_E10_0;
4596     }
4597     return mask;
4598 }
4599 
4600 /* Return 56 if TBI is enabled, 64 otherwise. */
4601 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4602                               uint64_t addr)
4603 {
4604     uint64_t tcr = regime_tcr(env, mmu_idx);
4605     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4606     int select = extract64(addr, 55, 1);
4607 
4608     return (tbi >> select) & 1 ? 56 : 64;
4609 }
4610 
4611 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4612 {
4613     uint64_t hcr = arm_hcr_el2_eff(env);
4614     ARMMMUIdx mmu_idx;
4615 
4616     /* Only the regime of the mmu_idx below is significant. */
4617     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4618         mmu_idx = ARMMMUIdx_E20_0;
4619     } else {
4620         mmu_idx = ARMMMUIdx_E10_0;
4621     }
4622 
4623     return tlbbits_for_regime(env, mmu_idx, addr);
4624 }
4625 
4626 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4627                                       uint64_t value)
4628 {
4629     CPUState *cs = env_cpu(env);
4630     int mask = vae1_tlbmask(env);
4631 
4632     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4633 }
4634 
4635 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4636                                     uint64_t value)
4637 {
4638     CPUState *cs = env_cpu(env);
4639     int mask = vae1_tlbmask(env);
4640 
4641     if (tlb_force_broadcast(env)) {
4642         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4643     } else {
4644         tlb_flush_by_mmuidx(cs, mask);
4645     }
4646 }
4647 
4648 static int e2_tlbmask(CPUARMState *env)
4649 {
4650     return (ARMMMUIdxBit_E20_0 |
4651             ARMMMUIdxBit_E20_2 |
4652             ARMMMUIdxBit_E20_2_PAN |
4653             ARMMMUIdxBit_E2);
4654 }
4655 
4656 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4657                                   uint64_t value)
4658 {
4659     CPUState *cs = env_cpu(env);
4660     int mask = alle1_tlbmask(env);
4661 
4662     tlb_flush_by_mmuidx(cs, mask);
4663 }
4664 
4665 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4666                                   uint64_t value)
4667 {
4668     CPUState *cs = env_cpu(env);
4669     int mask = e2_tlbmask(env);
4670 
4671     tlb_flush_by_mmuidx(cs, mask);
4672 }
4673 
4674 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4675                                   uint64_t value)
4676 {
4677     ARMCPU *cpu = env_archcpu(env);
4678     CPUState *cs = CPU(cpu);
4679 
4680     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4681 }
4682 
4683 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4684                                     uint64_t value)
4685 {
4686     CPUState *cs = env_cpu(env);
4687     int mask = alle1_tlbmask(env);
4688 
4689     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4690 }
4691 
4692 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4693                                     uint64_t value)
4694 {
4695     CPUState *cs = env_cpu(env);
4696     int mask = e2_tlbmask(env);
4697 
4698     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4699 }
4700 
4701 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4702                                     uint64_t value)
4703 {
4704     CPUState *cs = env_cpu(env);
4705 
4706     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4707 }
4708 
4709 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4710                                  uint64_t value)
4711 {
4712     /*
4713      * Invalidate by VA, EL2
4714      * Currently handles both VAE2 and VALE2, since we don't support
4715      * flush-last-level-only.
4716      */
4717     CPUState *cs = env_cpu(env);
4718     int mask = e2_tlbmask(env);
4719     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4720 
4721     tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4722 }
4723 
4724 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4725                                  uint64_t value)
4726 {
4727     /*
4728      * Invalidate by VA, EL3
4729      * Currently handles both VAE3 and VALE3, since we don't support
4730      * flush-last-level-only.
4731      */
4732     ARMCPU *cpu = env_archcpu(env);
4733     CPUState *cs = CPU(cpu);
4734     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4735 
4736     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4737 }
4738 
4739 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4740                                    uint64_t value)
4741 {
4742     CPUState *cs = env_cpu(env);
4743     int mask = vae1_tlbmask(env);
4744     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4745     int bits = vae1_tlbbits(env, pageaddr);
4746 
4747     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4748 }
4749 
4750 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4751                                  uint64_t value)
4752 {
4753     /*
4754      * Invalidate by VA, EL1&0 (AArch64 version).
4755      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4756      * since we don't support flush-for-specific-ASID-only or
4757      * flush-last-level-only.
4758      */
4759     CPUState *cs = env_cpu(env);
4760     int mask = vae1_tlbmask(env);
4761     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4762     int bits = vae1_tlbbits(env, pageaddr);
4763 
4764     if (tlb_force_broadcast(env)) {
4765         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4766     } else {
4767         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4768     }
4769 }
4770 
4771 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4772                                    uint64_t value)
4773 {
4774     CPUState *cs = env_cpu(env);
4775     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4776     int bits = tlbbits_for_regime(env, ARMMMUIdx_E2, pageaddr);
4777 
4778     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4779                                                   ARMMMUIdxBit_E2, bits);
4780 }
4781 
4782 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4783                                    uint64_t value)
4784 {
4785     CPUState *cs = env_cpu(env);
4786     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4787     int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
4788 
4789     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4790                                                   ARMMMUIdxBit_E3, bits);
4791 }
4792 
4793 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4794 {
4795     /*
4796      * The MSB of value is the NS field, which only applies if SEL2
4797      * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4798      */
4799     return (value >= 0
4800             && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4801             && arm_is_secure_below_el3(env)
4802             ? ARMMMUIdxBit_Stage2_S
4803             : ARMMMUIdxBit_Stage2);
4804 }
4805 
4806 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4807                                     uint64_t value)
4808 {
4809     CPUState *cs = env_cpu(env);
4810     int mask = ipas2e1_tlbmask(env, value);
4811     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4812 
4813     if (tlb_force_broadcast(env)) {
4814         tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4815     } else {
4816         tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4817     }
4818 }
4819 
4820 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4821                                       uint64_t value)
4822 {
4823     CPUState *cs = env_cpu(env);
4824     int mask = ipas2e1_tlbmask(env, value);
4825     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4826 
4827     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4828 }
4829 
4830 #ifdef TARGET_AARCH64
4831 typedef struct {
4832     uint64_t base;
4833     uint64_t length;
4834 } TLBIRange;
4835 
4836 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4837 {
4838     /*
4839      * Note that the TLBI range TG field encoding differs from both
4840      * TG0 and TG1 encodings.
4841      */
4842     switch (tg) {
4843     case 1:
4844         return Gran4K;
4845     case 2:
4846         return Gran16K;
4847     case 3:
4848         return Gran64K;
4849     default:
4850         return GranInvalid;
4851     }
4852 }
4853 
4854 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4855                                      uint64_t value)
4856 {
4857     unsigned int page_size_granule, page_shift, num, scale, exponent;
4858     /* Extract one bit to represent the va selector in use. */
4859     uint64_t select = sextract64(value, 36, 1);
4860     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4861     TLBIRange ret = { };
4862     ARMGranuleSize gran;
4863 
4864     page_size_granule = extract64(value, 46, 2);
4865     gran = tlbi_range_tg_to_gran_size(page_size_granule);
4866 
4867     /* The granule encoded in value must match the granule in use. */
4868     if (gran != param.gran) {
4869         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4870                       page_size_granule);
4871         return ret;
4872     }
4873 
4874     page_shift = arm_granule_bits(gran);
4875     num = extract64(value, 39, 5);
4876     scale = extract64(value, 44, 2);
4877     exponent = (5 * scale) + 1;
4878 
4879     ret.length = (num + 1) << (exponent + page_shift);
4880 
4881     if (param.select) {
4882         ret.base = sextract64(value, 0, 37);
4883     } else {
4884         ret.base = extract64(value, 0, 37);
4885     }
4886     if (param.ds) {
4887         /*
4888          * With DS=1, BaseADDR is always shifted 16 so that it is able
4889          * to address all 52 va bits.  The input address is perforce
4890          * aligned on a 64k boundary regardless of translation granule.
4891          */
4892         page_shift = 16;
4893     }
4894     ret.base <<= page_shift;
4895 
4896     return ret;
4897 }
4898 
4899 static void do_rvae_write(CPUARMState *env, uint64_t value,
4900                           int idxmap, bool synced)
4901 {
4902     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4903     TLBIRange range;
4904     int bits;
4905 
4906     range = tlbi_aa64_get_range(env, one_idx, value);
4907     bits = tlbbits_for_regime(env, one_idx, range.base);
4908 
4909     if (synced) {
4910         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4911                                                   range.base,
4912                                                   range.length,
4913                                                   idxmap,
4914                                                   bits);
4915     } else {
4916         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4917                                   range.length, idxmap, bits);
4918     }
4919 }
4920 
4921 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4922                                   const ARMCPRegInfo *ri,
4923                                   uint64_t value)
4924 {
4925     /*
4926      * Invalidate by VA range, EL1&0.
4927      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4928      * since we don't support flush-for-specific-ASID-only or
4929      * flush-last-level-only.
4930      */
4931 
4932     do_rvae_write(env, value, vae1_tlbmask(env),
4933                   tlb_force_broadcast(env));
4934 }
4935 
4936 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4937                                     const ARMCPRegInfo *ri,
4938                                     uint64_t value)
4939 {
4940     /*
4941      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4942      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4943      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4944      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4945      * shareable specific flushes.
4946      */
4947 
4948     do_rvae_write(env, value, vae1_tlbmask(env), true);
4949 }
4950 
4951 static int vae2_tlbmask(CPUARMState *env)
4952 {
4953     return ARMMMUIdxBit_E2;
4954 }
4955 
4956 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4957                                   const ARMCPRegInfo *ri,
4958                                   uint64_t value)
4959 {
4960     /*
4961      * Invalidate by VA range, EL2.
4962      * Currently handles all of RVAE2 and RVALE2,
4963      * since we don't support flush-for-specific-ASID-only or
4964      * flush-last-level-only.
4965      */
4966 
4967     do_rvae_write(env, value, vae2_tlbmask(env),
4968                   tlb_force_broadcast(env));
4969 
4970 
4971 }
4972 
4973 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4974                                     const ARMCPRegInfo *ri,
4975                                     uint64_t value)
4976 {
4977     /*
4978      * Invalidate by VA range, Inner/Outer Shareable, EL2.
4979      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4980      * since we don't support flush-for-specific-ASID-only,
4981      * flush-last-level-only or inner/outer shareable specific flushes.
4982      */
4983 
4984     do_rvae_write(env, value, vae2_tlbmask(env), true);
4985 
4986 }
4987 
4988 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4989                                   const ARMCPRegInfo *ri,
4990                                   uint64_t value)
4991 {
4992     /*
4993      * Invalidate by VA range, EL3.
4994      * Currently handles all of RVAE3 and RVALE3,
4995      * since we don't support flush-for-specific-ASID-only or
4996      * flush-last-level-only.
4997      */
4998 
4999     do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5000 }
5001 
5002 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5003                                     const ARMCPRegInfo *ri,
5004                                     uint64_t value)
5005 {
5006     /*
5007      * Invalidate by VA range, EL3, Inner/Outer Shareable.
5008      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5009      * since we don't support flush-for-specific-ASID-only,
5010      * flush-last-level-only or inner/outer specific flushes.
5011      */
5012 
5013     do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5014 }
5015 
5016 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5017                                      uint64_t value)
5018 {
5019     do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5020                   tlb_force_broadcast(env));
5021 }
5022 
5023 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5024                                        const ARMCPRegInfo *ri,
5025                                        uint64_t value)
5026 {
5027     do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5028 }
5029 #endif
5030 
5031 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5032                                       bool isread)
5033 {
5034     int cur_el = arm_current_el(env);
5035 
5036     if (cur_el < 2) {
5037         uint64_t hcr = arm_hcr_el2_eff(env);
5038 
5039         if (cur_el == 0) {
5040             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5041                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5042                     return CP_ACCESS_TRAP_EL2;
5043                 }
5044             } else {
5045                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5046                     return CP_ACCESS_TRAP;
5047                 }
5048                 if (hcr & HCR_TDZ) {
5049                     return CP_ACCESS_TRAP_EL2;
5050                 }
5051             }
5052         } else if (hcr & HCR_TDZ) {
5053             return CP_ACCESS_TRAP_EL2;
5054         }
5055     }
5056     return CP_ACCESS_OK;
5057 }
5058 
5059 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5060 {
5061     ARMCPU *cpu = env_archcpu(env);
5062     int dzp_bit = 1 << 4;
5063 
5064     /* DZP indicates whether DC ZVA access is allowed */
5065     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5066         dzp_bit = 0;
5067     }
5068     return cpu->dcz_blocksize | dzp_bit;
5069 }
5070 
5071 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5072                                     bool isread)
5073 {
5074     if (!(env->pstate & PSTATE_SP)) {
5075         /*
5076          * Access to SP_EL0 is undefined if it's being used as
5077          * the stack pointer.
5078          */
5079         return CP_ACCESS_TRAP_UNCATEGORIZED;
5080     }
5081     return CP_ACCESS_OK;
5082 }
5083 
5084 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5085 {
5086     return env->pstate & PSTATE_SP;
5087 }
5088 
5089 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5090 {
5091     update_spsel(env, val);
5092 }
5093 
5094 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5095                         uint64_t value)
5096 {
5097     ARMCPU *cpu = env_archcpu(env);
5098 
5099     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5100         /* M bit is RAZ/WI for PMSA with no MPU implemented */
5101         value &= ~SCTLR_M;
5102     }
5103 
5104     /* ??? Lots of these bits are not implemented.  */
5105 
5106     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5107         if (ri->opc1 == 6) { /* SCTLR_EL3 */
5108             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5109         } else {
5110             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5111                        SCTLR_ATA0 | SCTLR_ATA);
5112         }
5113     }
5114 
5115     if (raw_read(env, ri) == value) {
5116         /*
5117          * Skip the TLB flush if nothing actually changed; Linux likes
5118          * to do a lot of pointless SCTLR writes.
5119          */
5120         return;
5121     }
5122 
5123     raw_write(env, ri, value);
5124 
5125     /* This may enable/disable the MMU, so do a TLB flush.  */
5126     tlb_flush(CPU(cpu));
5127 
5128     if (ri->type & ARM_CP_SUPPRESS_TB_END) {
5129         /*
5130          * Normally we would always end the TB on an SCTLR write; see the
5131          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5132          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5133          * of hflags from the translator, so do it here.
5134          */
5135         arm_rebuild_hflags(env);
5136     }
5137 }
5138 
5139 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5140                            uint64_t value)
5141 {
5142     /*
5143      * Some MDCR_EL3 bits affect whether PMU counters are running:
5144      * if we are trying to change any of those then we must
5145      * bracket this update with PMU start/finish calls.
5146      */
5147     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5148 
5149     if (pmu_op) {
5150         pmu_op_start(env);
5151     }
5152     env->cp15.mdcr_el3 = value;
5153     if (pmu_op) {
5154         pmu_op_finish(env);
5155     }
5156 }
5157 
5158 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5159                        uint64_t value)
5160 {
5161     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5162     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5163 }
5164 
5165 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5166                            uint64_t value)
5167 {
5168     /*
5169      * Some MDCR_EL2 bits affect whether PMU counters are running:
5170      * if we are trying to change any of those then we must
5171      * bracket this update with PMU start/finish calls.
5172      */
5173     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5174 
5175     if (pmu_op) {
5176         pmu_op_start(env);
5177     }
5178     env->cp15.mdcr_el2 = value;
5179     if (pmu_op) {
5180         pmu_op_finish(env);
5181     }
5182 }
5183 
5184 static const ARMCPRegInfo v8_cp_reginfo[] = {
5185     /*
5186      * Minimal set of EL0-visible registers. This will need to be expanded
5187      * significantly for system emulation of AArch64 CPUs.
5188      */
5189     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5190       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5191       .access = PL0_RW, .type = ARM_CP_NZCV },
5192     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5193       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5194       .type = ARM_CP_NO_RAW,
5195       .access = PL0_RW, .accessfn = aa64_daif_access,
5196       .fieldoffset = offsetof(CPUARMState, daif),
5197       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5198     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5199       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5200       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5201       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5202     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5203       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5204       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5205       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5206     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5207       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5208       .access = PL0_R, .type = ARM_CP_NO_RAW,
5209       .readfn = aa64_dczid_read },
5210     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5211       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5212       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5213 #ifndef CONFIG_USER_ONLY
5214       /* Avoid overhead of an access check that always passes in user-mode */
5215       .accessfn = aa64_zva_access,
5216 #endif
5217     },
5218     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5219       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5220       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5221     /* Cache ops: all NOPs since we don't emulate caches */
5222     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5223       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5224       .access = PL1_W, .type = ARM_CP_NOP,
5225       .accessfn = access_ticab },
5226     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5227       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5228       .access = PL1_W, .type = ARM_CP_NOP,
5229       .accessfn = access_tocu },
5230     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5231       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5232       .access = PL0_W, .type = ARM_CP_NOP,
5233       .accessfn = access_tocu },
5234     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5235       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5236       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5237       .type = ARM_CP_NOP },
5238     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5239       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5240       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5241     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5242       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5243       .access = PL0_W, .type = ARM_CP_NOP,
5244       .accessfn = aa64_cacheop_poc_access },
5245     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5246       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5247       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5248     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5249       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5250       .access = PL0_W, .type = ARM_CP_NOP,
5251       .accessfn = access_tocu },
5252     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5253       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5254       .access = PL0_W, .type = ARM_CP_NOP,
5255       .accessfn = aa64_cacheop_poc_access },
5256     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5257       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5258       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5259     /* TLBI operations */
5260     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5261       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5262       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5263       .writefn = tlbi_aa64_vmalle1is_write },
5264     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5265       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5266       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5267       .writefn = tlbi_aa64_vae1is_write },
5268     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5269       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5270       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5271       .writefn = tlbi_aa64_vmalle1is_write },
5272     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5273       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5274       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5275       .writefn = tlbi_aa64_vae1is_write },
5276     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5277       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5278       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5279       .writefn = tlbi_aa64_vae1is_write },
5280     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5281       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5282       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5283       .writefn = tlbi_aa64_vae1is_write },
5284     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5285       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5286       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5287       .writefn = tlbi_aa64_vmalle1_write },
5288     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5289       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5290       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5291       .writefn = tlbi_aa64_vae1_write },
5292     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5293       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5294       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5295       .writefn = tlbi_aa64_vmalle1_write },
5296     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5297       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5298       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5299       .writefn = tlbi_aa64_vae1_write },
5300     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5301       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5302       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5303       .writefn = tlbi_aa64_vae1_write },
5304     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5305       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5306       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5307       .writefn = tlbi_aa64_vae1_write },
5308     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5309       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5310       .access = PL2_W, .type = ARM_CP_NO_RAW,
5311       .writefn = tlbi_aa64_ipas2e1is_write },
5312     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5313       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5314       .access = PL2_W, .type = ARM_CP_NO_RAW,
5315       .writefn = tlbi_aa64_ipas2e1is_write },
5316     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5317       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5318       .access = PL2_W, .type = ARM_CP_NO_RAW,
5319       .writefn = tlbi_aa64_alle1is_write },
5320     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5321       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5322       .access = PL2_W, .type = ARM_CP_NO_RAW,
5323       .writefn = tlbi_aa64_alle1is_write },
5324     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5325       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5326       .access = PL2_W, .type = ARM_CP_NO_RAW,
5327       .writefn = tlbi_aa64_ipas2e1_write },
5328     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5329       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5330       .access = PL2_W, .type = ARM_CP_NO_RAW,
5331       .writefn = tlbi_aa64_ipas2e1_write },
5332     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5333       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5334       .access = PL2_W, .type = ARM_CP_NO_RAW,
5335       .writefn = tlbi_aa64_alle1_write },
5336     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5337       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5338       .access = PL2_W, .type = ARM_CP_NO_RAW,
5339       .writefn = tlbi_aa64_alle1is_write },
5340 #ifndef CONFIG_USER_ONLY
5341     /* 64 bit address translation operations */
5342     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5343       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5344       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5345       .writefn = ats_write64 },
5346     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5347       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5348       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5349       .writefn = ats_write64 },
5350     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5351       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5352       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5353       .writefn = ats_write64 },
5354     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5355       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5356       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5357       .writefn = ats_write64 },
5358     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5359       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5360       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5361       .writefn = ats_write64 },
5362     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5363       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5364       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5365       .writefn = ats_write64 },
5366     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5367       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5368       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5369       .writefn = ats_write64 },
5370     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5371       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5372       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5373       .writefn = ats_write64 },
5374     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5375     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5376       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5377       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5378       .writefn = ats_write64 },
5379     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5380       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5381       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5382       .writefn = ats_write64 },
5383     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5384       .type = ARM_CP_ALIAS,
5385       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5386       .access = PL1_RW, .resetvalue = 0,
5387       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5388       .writefn = par_write },
5389 #endif
5390     /* TLB invalidate last level of translation table walk */
5391     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5392       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5393       .writefn = tlbimva_is_write },
5394     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5395       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5396       .writefn = tlbimvaa_is_write },
5397     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5398       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5399       .writefn = tlbimva_write },
5400     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5401       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5402       .writefn = tlbimvaa_write },
5403     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5404       .type = ARM_CP_NO_RAW, .access = PL2_W,
5405       .writefn = tlbimva_hyp_write },
5406     { .name = "TLBIMVALHIS",
5407       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5408       .type = ARM_CP_NO_RAW, .access = PL2_W,
5409       .writefn = tlbimva_hyp_is_write },
5410     { .name = "TLBIIPAS2",
5411       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5412       .type = ARM_CP_NO_RAW, .access = PL2_W,
5413       .writefn = tlbiipas2_hyp_write },
5414     { .name = "TLBIIPAS2IS",
5415       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5416       .type = ARM_CP_NO_RAW, .access = PL2_W,
5417       .writefn = tlbiipas2is_hyp_write },
5418     { .name = "TLBIIPAS2L",
5419       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5420       .type = ARM_CP_NO_RAW, .access = PL2_W,
5421       .writefn = tlbiipas2_hyp_write },
5422     { .name = "TLBIIPAS2LIS",
5423       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5424       .type = ARM_CP_NO_RAW, .access = PL2_W,
5425       .writefn = tlbiipas2is_hyp_write },
5426     /* 32 bit cache operations */
5427     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5428       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5429     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5430       .type = ARM_CP_NOP, .access = PL1_W },
5431     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5432       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5433     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5434       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5435     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5436       .type = ARM_CP_NOP, .access = PL1_W },
5437     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5438       .type = ARM_CP_NOP, .access = PL1_W },
5439     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5440       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5441     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5442       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5443     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5444       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5445     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5446       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5447     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5448       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5449     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5450       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5451     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5452       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5453     /* MMU Domain access control / MPU write buffer control */
5454     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5455       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5456       .writefn = dacr_write, .raw_writefn = raw_write,
5457       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5458                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5459     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5460       .type = ARM_CP_ALIAS,
5461       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5462       .access = PL1_RW,
5463       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5464     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5465       .type = ARM_CP_ALIAS,
5466       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5467       .access = PL1_RW,
5468       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5469     /*
5470      * We rely on the access checks not allowing the guest to write to the
5471      * state field when SPSel indicates that it's being used as the stack
5472      * pointer.
5473      */
5474     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5475       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5476       .access = PL1_RW, .accessfn = sp_el0_access,
5477       .type = ARM_CP_ALIAS,
5478       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5479     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5480       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5481       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5482       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5483     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5484       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5485       .type = ARM_CP_NO_RAW,
5486       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5487     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5488       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5489       .access = PL2_RW,
5490       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5491       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5492     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5493       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5494       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5495       .writefn = dacr_write, .raw_writefn = raw_write,
5496       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5497     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5498       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5499       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5500       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5501     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5502       .type = ARM_CP_ALIAS,
5503       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5504       .access = PL2_RW,
5505       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5506     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5507       .type = ARM_CP_ALIAS,
5508       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5509       .access = PL2_RW,
5510       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5511     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5512       .type = ARM_CP_ALIAS,
5513       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5514       .access = PL2_RW,
5515       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5516     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5517       .type = ARM_CP_ALIAS,
5518       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5519       .access = PL2_RW,
5520       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5521     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5522       .type = ARM_CP_IO,
5523       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5524       .resetvalue = 0,
5525       .access = PL3_RW,
5526       .writefn = mdcr_el3_write,
5527       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5528     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5529       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5530       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5531       .writefn = sdcr_write,
5532       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5533 };
5534 
5535 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5536 {
5537     ARMCPU *cpu = env_archcpu(env);
5538 
5539     if (arm_feature(env, ARM_FEATURE_V8)) {
5540         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5541     } else {
5542         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5543     }
5544 
5545     if (arm_feature(env, ARM_FEATURE_EL3)) {
5546         valid_mask &= ~HCR_HCD;
5547     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5548         /*
5549          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5550          * However, if we're using the SMC PSCI conduit then QEMU is
5551          * effectively acting like EL3 firmware and so the guest at
5552          * EL2 should retain the ability to prevent EL1 from being
5553          * able to make SMC calls into the ersatz firmware, so in
5554          * that case HCR.TSC should be read/write.
5555          */
5556         valid_mask &= ~HCR_TSC;
5557     }
5558 
5559     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5560         if (cpu_isar_feature(aa64_vh, cpu)) {
5561             valid_mask |= HCR_E2H;
5562         }
5563         if (cpu_isar_feature(aa64_ras, cpu)) {
5564             valid_mask |= HCR_TERR | HCR_TEA;
5565         }
5566         if (cpu_isar_feature(aa64_lor, cpu)) {
5567             valid_mask |= HCR_TLOR;
5568         }
5569         if (cpu_isar_feature(aa64_pauth, cpu)) {
5570             valid_mask |= HCR_API | HCR_APK;
5571         }
5572         if (cpu_isar_feature(aa64_mte, cpu)) {
5573             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5574         }
5575         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5576             valid_mask |= HCR_ENSCXT;
5577         }
5578         if (cpu_isar_feature(aa64_fwb, cpu)) {
5579             valid_mask |= HCR_FWB;
5580         }
5581     }
5582 
5583     if (cpu_isar_feature(any_evt, cpu)) {
5584         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5585     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5586         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5587     }
5588 
5589     /* Clear RES0 bits.  */
5590     value &= valid_mask;
5591 
5592     /*
5593      * These bits change the MMU setup:
5594      * HCR_VM enables stage 2 translation
5595      * HCR_PTW forbids certain page-table setups
5596      * HCR_DC disables stage1 and enables stage2 translation
5597      * HCR_DCT enables tagging on (disabled) stage1 translation
5598      * HCR_FWB changes the interpretation of stage2 descriptor bits
5599      */
5600     if ((env->cp15.hcr_el2 ^ value) &
5601         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5602         tlb_flush(CPU(cpu));
5603     }
5604     env->cp15.hcr_el2 = value;
5605 
5606     /*
5607      * Updates to VI and VF require us to update the status of
5608      * virtual interrupts, which are the logical OR of these bits
5609      * and the state of the input lines from the GIC. (This requires
5610      * that we have the iothread lock, which is done by marking the
5611      * reginfo structs as ARM_CP_IO.)
5612      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5613      * possible for it to be taken immediately, because VIRQ and
5614      * VFIQ are masked unless running at EL0 or EL1, and HCR
5615      * can only be written at EL2.
5616      */
5617     g_assert(qemu_mutex_iothread_locked());
5618     arm_cpu_update_virq(cpu);
5619     arm_cpu_update_vfiq(cpu);
5620     arm_cpu_update_vserr(cpu);
5621 }
5622 
5623 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5624 {
5625     do_hcr_write(env, value, 0);
5626 }
5627 
5628 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5629                           uint64_t value)
5630 {
5631     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5632     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5633     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5634 }
5635 
5636 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5637                          uint64_t value)
5638 {
5639     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5640     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5641     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5642 }
5643 
5644 /*
5645  * Return the effective value of HCR_EL2, at the given security state.
5646  * Bits that are not included here:
5647  * RW       (read from SCR_EL3.RW as needed)
5648  */
5649 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, bool secure)
5650 {
5651     uint64_t ret = env->cp15.hcr_el2;
5652 
5653     if (!arm_is_el2_enabled_secstate(env, secure)) {
5654         /*
5655          * "This register has no effect if EL2 is not enabled in the
5656          * current Security state".  This is ARMv8.4-SecEL2 speak for
5657          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5658          *
5659          * Prior to that, the language was "In an implementation that
5660          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5661          * as if this field is 0 for all purposes other than a direct
5662          * read or write access of HCR_EL2".  With lots of enumeration
5663          * on a per-field basis.  In current QEMU, this is condition
5664          * is arm_is_secure_below_el3.
5665          *
5666          * Since the v8.4 language applies to the entire register, and
5667          * appears to be backward compatible, use that.
5668          */
5669         return 0;
5670     }
5671 
5672     /*
5673      * For a cpu that supports both aarch64 and aarch32, we can set bits
5674      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5675      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5676      */
5677     if (!arm_el_is_aa64(env, 2)) {
5678         uint64_t aa32_valid;
5679 
5680         /*
5681          * These bits are up-to-date as of ARMv8.6.
5682          * For HCR, it's easiest to list just the 2 bits that are invalid.
5683          * For HCR2, list those that are valid.
5684          */
5685         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5686         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5687                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5688         ret &= aa32_valid;
5689     }
5690 
5691     if (ret & HCR_TGE) {
5692         /* These bits are up-to-date as of ARMv8.6.  */
5693         if (ret & HCR_E2H) {
5694             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5695                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5696                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5697                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5698                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5699                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5700         } else {
5701             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5702         }
5703         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5704                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5705                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5706                  HCR_TLOR);
5707     }
5708 
5709     return ret;
5710 }
5711 
5712 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5713 {
5714     return arm_hcr_el2_eff_secstate(env, arm_is_secure_below_el3(env));
5715 }
5716 
5717 /*
5718  * Corresponds to ARM pseudocode function ELIsInHost().
5719  */
5720 bool el_is_in_host(CPUARMState *env, int el)
5721 {
5722     uint64_t mask;
5723 
5724     /*
5725      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5726      * Perform the simplest bit tests first, and validate EL2 afterward.
5727      */
5728     if (el & 1) {
5729         return false; /* EL1 or EL3 */
5730     }
5731 
5732     /*
5733      * Note that hcr_write() checks isar_feature_aa64_vh(),
5734      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5735      */
5736     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5737     if ((env->cp15.hcr_el2 & mask) != mask) {
5738         return false;
5739     }
5740 
5741     /* TGE and/or E2H set: double check those bits are currently legal. */
5742     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5743 }
5744 
5745 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5746                        uint64_t value)
5747 {
5748     uint64_t valid_mask = 0;
5749 
5750     /* No features adding bits to HCRX are implemented. */
5751 
5752     /* Clear RES0 bits.  */
5753     env->cp15.hcrx_el2 = value & valid_mask;
5754 }
5755 
5756 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5757                                   bool isread)
5758 {
5759     if (arm_current_el(env) < 3
5760         && arm_feature(env, ARM_FEATURE_EL3)
5761         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5762         return CP_ACCESS_TRAP_EL3;
5763     }
5764     return CP_ACCESS_OK;
5765 }
5766 
5767 static const ARMCPRegInfo hcrx_el2_reginfo = {
5768     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5769     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5770     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5771     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5772 };
5773 
5774 /* Return the effective value of HCRX_EL2.  */
5775 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5776 {
5777     /*
5778      * The bits in this register behave as 0 for all purposes other than
5779      * direct reads of the register if:
5780      *   - EL2 is not enabled in the current security state,
5781      *   - SCR_EL3.HXEn is 0.
5782      */
5783     if (!arm_is_el2_enabled(env)
5784         || (arm_feature(env, ARM_FEATURE_EL3)
5785             && !(env->cp15.scr_el3 & SCR_HXEN))) {
5786         return 0;
5787     }
5788     return env->cp15.hcrx_el2;
5789 }
5790 
5791 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5792                            uint64_t value)
5793 {
5794     /*
5795      * For A-profile AArch32 EL3, if NSACR.CP10
5796      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5797      */
5798     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5799         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5800         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5801         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5802     }
5803     env->cp15.cptr_el[2] = value;
5804 }
5805 
5806 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5807 {
5808     /*
5809      * For A-profile AArch32 EL3, if NSACR.CP10
5810      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5811      */
5812     uint64_t value = env->cp15.cptr_el[2];
5813 
5814     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5815         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5816         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5817     }
5818     return value;
5819 }
5820 
5821 static const ARMCPRegInfo el2_cp_reginfo[] = {
5822     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5823       .type = ARM_CP_IO,
5824       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5825       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5826       .writefn = hcr_write },
5827     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5828       .type = ARM_CP_ALIAS | ARM_CP_IO,
5829       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5830       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5831       .writefn = hcr_writelow },
5832     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5833       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5834       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5835     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5836       .type = ARM_CP_ALIAS,
5837       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5838       .access = PL2_RW,
5839       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5840     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5841       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5842       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5843     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5844       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5845       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5846     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5847       .type = ARM_CP_ALIAS,
5848       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5849       .access = PL2_RW,
5850       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5851     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5852       .type = ARM_CP_ALIAS,
5853       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5854       .access = PL2_RW,
5855       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5856     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5857       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5858       .access = PL2_RW, .writefn = vbar_write,
5859       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5860       .resetvalue = 0 },
5861     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5862       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5863       .access = PL3_RW, .type = ARM_CP_ALIAS,
5864       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5865     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5866       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5867       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5868       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5869       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5870     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5871       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5872       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5873       .resetvalue = 0 },
5874     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5875       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5876       .access = PL2_RW, .type = ARM_CP_ALIAS,
5877       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5878     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5879       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5880       .access = PL2_RW, .type = ARM_CP_CONST,
5881       .resetvalue = 0 },
5882     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5883     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5884       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5885       .access = PL2_RW, .type = ARM_CP_CONST,
5886       .resetvalue = 0 },
5887     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5888       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5889       .access = PL2_RW, .type = ARM_CP_CONST,
5890       .resetvalue = 0 },
5891     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5892       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5893       .access = PL2_RW, .type = ARM_CP_CONST,
5894       .resetvalue = 0 },
5895     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5896       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5897       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5898       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5899     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5900       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5901       .type = ARM_CP_ALIAS,
5902       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5903       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5904     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5905       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5906       .access = PL2_RW,
5907       /* no .writefn needed as this can't cause an ASID change */
5908       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5909     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5910       .cp = 15, .opc1 = 6, .crm = 2,
5911       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5912       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5913       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5914       .writefn = vttbr_write },
5915     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5916       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5917       .access = PL2_RW, .writefn = vttbr_write,
5918       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5919     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5920       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5921       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5922       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5923     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5924       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5925       .access = PL2_RW, .resetvalue = 0,
5926       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5927     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5928       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5929       .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5930       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5931     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5932       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5933       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5934     { .name = "TLBIALLNSNH",
5935       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5936       .type = ARM_CP_NO_RAW, .access = PL2_W,
5937       .writefn = tlbiall_nsnh_write },
5938     { .name = "TLBIALLNSNHIS",
5939       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5940       .type = ARM_CP_NO_RAW, .access = PL2_W,
5941       .writefn = tlbiall_nsnh_is_write },
5942     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5943       .type = ARM_CP_NO_RAW, .access = PL2_W,
5944       .writefn = tlbiall_hyp_write },
5945     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5946       .type = ARM_CP_NO_RAW, .access = PL2_W,
5947       .writefn = tlbiall_hyp_is_write },
5948     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5949       .type = ARM_CP_NO_RAW, .access = PL2_W,
5950       .writefn = tlbimva_hyp_write },
5951     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5952       .type = ARM_CP_NO_RAW, .access = PL2_W,
5953       .writefn = tlbimva_hyp_is_write },
5954     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5955       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5956       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5957       .writefn = tlbi_aa64_alle2_write },
5958     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5959       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5960       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5961       .writefn = tlbi_aa64_vae2_write },
5962     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5963       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5964       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5965       .writefn = tlbi_aa64_vae2_write },
5966     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5967       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5968       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5969       .writefn = tlbi_aa64_alle2is_write },
5970     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5971       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5972       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5973       .writefn = tlbi_aa64_vae2is_write },
5974     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5975       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5976       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5977       .writefn = tlbi_aa64_vae2is_write },
5978 #ifndef CONFIG_USER_ONLY
5979     /*
5980      * Unlike the other EL2-related AT operations, these must
5981      * UNDEF from EL3 if EL2 is not implemented, which is why we
5982      * define them here rather than with the rest of the AT ops.
5983      */
5984     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5985       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5986       .access = PL2_W, .accessfn = at_s1e2_access,
5987       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5988       .writefn = ats_write64 },
5989     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5990       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5991       .access = PL2_W, .accessfn = at_s1e2_access,
5992       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5993       .writefn = ats_write64 },
5994     /*
5995      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5996      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5997      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5998      * to behave as if SCR.NS was 1.
5999      */
6000     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6001       .access = PL2_W,
6002       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6003     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6004       .access = PL2_W,
6005       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6006     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6007       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6008       /*
6009        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6010        * reset values as IMPDEF. We choose to reset to 3 to comply with
6011        * both ARMv7 and ARMv8.
6012        */
6013       .access = PL2_RW, .resetvalue = 3,
6014       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6015     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6016       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6017       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6018       .writefn = gt_cntvoff_write,
6019       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6020     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6021       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6022       .writefn = gt_cntvoff_write,
6023       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6024     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6025       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6026       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6027       .type = ARM_CP_IO, .access = PL2_RW,
6028       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6029     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6030       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6031       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6032       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6033     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6034       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6035       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6036       .resetfn = gt_hyp_timer_reset,
6037       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6038     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6039       .type = ARM_CP_IO,
6040       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6041       .access = PL2_RW,
6042       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6043       .resetvalue = 0,
6044       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6045 #endif
6046     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6047       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6048       .access = PL2_RW, .accessfn = access_el3_aa32ns,
6049       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6050     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6051       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6052       .access = PL2_RW,
6053       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6054     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6055       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6056       .access = PL2_RW,
6057       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6058 };
6059 
6060 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6061     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6062       .type = ARM_CP_ALIAS | ARM_CP_IO,
6063       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6064       .access = PL2_RW,
6065       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6066       .writefn = hcr_writehigh },
6067 };
6068 
6069 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6070                                   bool isread)
6071 {
6072     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6073         return CP_ACCESS_OK;
6074     }
6075     return CP_ACCESS_TRAP_UNCATEGORIZED;
6076 }
6077 
6078 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6079     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6080       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6081       .access = PL2_RW, .accessfn = sel2_access,
6082       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6083     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6084       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6085       .access = PL2_RW, .accessfn = sel2_access,
6086       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6087 };
6088 
6089 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6090                                    bool isread)
6091 {
6092     /*
6093      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6094      * At Secure EL1 it traps to EL3 or EL2.
6095      */
6096     if (arm_current_el(env) == 3) {
6097         return CP_ACCESS_OK;
6098     }
6099     if (arm_is_secure_below_el3(env)) {
6100         if (env->cp15.scr_el3 & SCR_EEL2) {
6101             return CP_ACCESS_TRAP_EL2;
6102         }
6103         return CP_ACCESS_TRAP_EL3;
6104     }
6105     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6106     if (isread) {
6107         return CP_ACCESS_OK;
6108     }
6109     return CP_ACCESS_TRAP_UNCATEGORIZED;
6110 }
6111 
6112 static const ARMCPRegInfo el3_cp_reginfo[] = {
6113     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6114       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6115       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6116       .resetfn = scr_reset, .writefn = scr_write },
6117     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6118       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6119       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6120       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6121       .writefn = scr_write },
6122     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6123       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6124       .access = PL3_RW, .resetvalue = 0,
6125       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6126     { .name = "SDER",
6127       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6128       .access = PL3_RW, .resetvalue = 0,
6129       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6130     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6131       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6132       .writefn = vbar_write, .resetvalue = 0,
6133       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6134     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6135       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6136       .access = PL3_RW, .resetvalue = 0,
6137       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6138     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6139       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6140       .access = PL3_RW,
6141       /* no .writefn needed as this can't cause an ASID change */
6142       .resetvalue = 0,
6143       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6144     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6145       .type = ARM_CP_ALIAS,
6146       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6147       .access = PL3_RW,
6148       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6149     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6150       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6151       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6152     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6153       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6154       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6155     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6156       .type = ARM_CP_ALIAS,
6157       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6158       .access = PL3_RW,
6159       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6160     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6161       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6162       .access = PL3_RW, .writefn = vbar_write,
6163       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6164       .resetvalue = 0 },
6165     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6166       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6167       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6168       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6169     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6170       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6171       .access = PL3_RW, .resetvalue = 0,
6172       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6173     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6174       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6175       .access = PL3_RW, .type = ARM_CP_CONST,
6176       .resetvalue = 0 },
6177     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6178       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6179       .access = PL3_RW, .type = ARM_CP_CONST,
6180       .resetvalue = 0 },
6181     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6182       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6183       .access = PL3_RW, .type = ARM_CP_CONST,
6184       .resetvalue = 0 },
6185     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6186       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6187       .access = PL3_W, .type = ARM_CP_NO_RAW,
6188       .writefn = tlbi_aa64_alle3is_write },
6189     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6190       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6191       .access = PL3_W, .type = ARM_CP_NO_RAW,
6192       .writefn = tlbi_aa64_vae3is_write },
6193     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6194       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6195       .access = PL3_W, .type = ARM_CP_NO_RAW,
6196       .writefn = tlbi_aa64_vae3is_write },
6197     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6198       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6199       .access = PL3_W, .type = ARM_CP_NO_RAW,
6200       .writefn = tlbi_aa64_alle3_write },
6201     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6202       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6203       .access = PL3_W, .type = ARM_CP_NO_RAW,
6204       .writefn = tlbi_aa64_vae3_write },
6205     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6206       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6207       .access = PL3_W, .type = ARM_CP_NO_RAW,
6208       .writefn = tlbi_aa64_vae3_write },
6209 };
6210 
6211 #ifndef CONFIG_USER_ONLY
6212 /* Test if system register redirection is to occur in the current state.  */
6213 static bool redirect_for_e2h(CPUARMState *env)
6214 {
6215     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6216 }
6217 
6218 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6219 {
6220     CPReadFn *readfn;
6221 
6222     if (redirect_for_e2h(env)) {
6223         /* Switch to the saved EL2 version of the register.  */
6224         ri = ri->opaque;
6225         readfn = ri->readfn;
6226     } else {
6227         readfn = ri->orig_readfn;
6228     }
6229     if (readfn == NULL) {
6230         readfn = raw_read;
6231     }
6232     return readfn(env, ri);
6233 }
6234 
6235 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6236                           uint64_t value)
6237 {
6238     CPWriteFn *writefn;
6239 
6240     if (redirect_for_e2h(env)) {
6241         /* Switch to the saved EL2 version of the register.  */
6242         ri = ri->opaque;
6243         writefn = ri->writefn;
6244     } else {
6245         writefn = ri->orig_writefn;
6246     }
6247     if (writefn == NULL) {
6248         writefn = raw_write;
6249     }
6250     writefn(env, ri, value);
6251 }
6252 
6253 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6254 {
6255     struct E2HAlias {
6256         uint32_t src_key, dst_key, new_key;
6257         const char *src_name, *dst_name, *new_name;
6258         bool (*feature)(const ARMISARegisters *id);
6259     };
6260 
6261 #define K(op0, op1, crn, crm, op2) \
6262     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6263 
6264     static const struct E2HAlias aliases[] = {
6265         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6266           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6267         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6268           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6269         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6270           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6271         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6272           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6273         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6274           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6275         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6276           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6277         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6278           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6279         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6280           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6281         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6282           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6283         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6284           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6285         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6286           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6287         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6288           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6289         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6290           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6291         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6292           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6293         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6294           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6295         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6296           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6297 
6298         /*
6299          * Note that redirection of ZCR is mentioned in the description
6300          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6301          * not in the summary table.
6302          */
6303         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6304           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6305         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6306           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6307 
6308         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6309           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6310 
6311         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6312           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6313           isar_feature_aa64_scxtnum },
6314 
6315         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6316         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6317     };
6318 #undef K
6319 
6320     size_t i;
6321 
6322     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6323         const struct E2HAlias *a = &aliases[i];
6324         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6325         bool ok;
6326 
6327         if (a->feature && !a->feature(&cpu->isar)) {
6328             continue;
6329         }
6330 
6331         src_reg = g_hash_table_lookup(cpu->cp_regs,
6332                                       (gpointer)(uintptr_t)a->src_key);
6333         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6334                                       (gpointer)(uintptr_t)a->dst_key);
6335         g_assert(src_reg != NULL);
6336         g_assert(dst_reg != NULL);
6337 
6338         /* Cross-compare names to detect typos in the keys.  */
6339         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6340         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6341 
6342         /* None of the core system registers use opaque; we will.  */
6343         g_assert(src_reg->opaque == NULL);
6344 
6345         /* Create alias before redirection so we dup the right data. */
6346         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6347 
6348         new_reg->name = a->new_name;
6349         new_reg->type |= ARM_CP_ALIAS;
6350         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6351         new_reg->access &= PL2_RW | PL3_RW;
6352 
6353         ok = g_hash_table_insert(cpu->cp_regs,
6354                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6355         g_assert(ok);
6356 
6357         src_reg->opaque = dst_reg;
6358         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6359         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6360         if (!src_reg->raw_readfn) {
6361             src_reg->raw_readfn = raw_read;
6362         }
6363         if (!src_reg->raw_writefn) {
6364             src_reg->raw_writefn = raw_write;
6365         }
6366         src_reg->readfn = el2_e2h_read;
6367         src_reg->writefn = el2_e2h_write;
6368     }
6369 }
6370 #endif
6371 
6372 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6373                                      bool isread)
6374 {
6375     int cur_el = arm_current_el(env);
6376 
6377     if (cur_el < 2) {
6378         uint64_t hcr = arm_hcr_el2_eff(env);
6379 
6380         if (cur_el == 0) {
6381             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6382                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6383                     return CP_ACCESS_TRAP_EL2;
6384                 }
6385             } else {
6386                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6387                     return CP_ACCESS_TRAP;
6388                 }
6389                 if (hcr & HCR_TID2) {
6390                     return CP_ACCESS_TRAP_EL2;
6391                 }
6392             }
6393         } else if (hcr & HCR_TID2) {
6394             return CP_ACCESS_TRAP_EL2;
6395         }
6396     }
6397 
6398     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6399         return CP_ACCESS_TRAP_EL2;
6400     }
6401 
6402     return CP_ACCESS_OK;
6403 }
6404 
6405 /*
6406  * Check for traps to RAS registers, which are controlled
6407  * by HCR_EL2.TERR and SCR_EL3.TERR.
6408  */
6409 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6410                                   bool isread)
6411 {
6412     int el = arm_current_el(env);
6413 
6414     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6415         return CP_ACCESS_TRAP_EL2;
6416     }
6417     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6418         return CP_ACCESS_TRAP_EL3;
6419     }
6420     return CP_ACCESS_OK;
6421 }
6422 
6423 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6424 {
6425     int el = arm_current_el(env);
6426 
6427     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6428         return env->cp15.vdisr_el2;
6429     }
6430     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6431         return 0; /* RAZ/WI */
6432     }
6433     return env->cp15.disr_el1;
6434 }
6435 
6436 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6437 {
6438     int el = arm_current_el(env);
6439 
6440     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6441         env->cp15.vdisr_el2 = val;
6442         return;
6443     }
6444     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6445         return; /* RAZ/WI */
6446     }
6447     env->cp15.disr_el1 = val;
6448 }
6449 
6450 /*
6451  * Minimal RAS implementation with no Error Records.
6452  * Which means that all of the Error Record registers:
6453  *   ERXADDR_EL1
6454  *   ERXCTLR_EL1
6455  *   ERXFR_EL1
6456  *   ERXMISC0_EL1
6457  *   ERXMISC1_EL1
6458  *   ERXMISC2_EL1
6459  *   ERXMISC3_EL1
6460  *   ERXPFGCDN_EL1  (RASv1p1)
6461  *   ERXPFGCTL_EL1  (RASv1p1)
6462  *   ERXPFGF_EL1    (RASv1p1)
6463  *   ERXSTATUS_EL1
6464  * and
6465  *   ERRSELR_EL1
6466  * may generate UNDEFINED, which is the effect we get by not
6467  * listing them at all.
6468  */
6469 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6470     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6471       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6472       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6473       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6474     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6475       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6476       .access = PL1_R, .accessfn = access_terr,
6477       .type = ARM_CP_CONST, .resetvalue = 0 },
6478     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6479       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6480       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6481     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6482       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6483       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6484 };
6485 
6486 /*
6487  * Return the exception level to which exceptions should be taken
6488  * via SVEAccessTrap.  This excludes the check for whether the exception
6489  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6490  * be found by testing 0 < fp_exception_el < sve_exception_el.
6491  *
6492  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6493  * pseudocode does *not* separate out the FP trap checks, but has them
6494  * all in one function.
6495  */
6496 int sve_exception_el(CPUARMState *env, int el)
6497 {
6498 #ifndef CONFIG_USER_ONLY
6499     if (el <= 1 && !el_is_in_host(env, el)) {
6500         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6501         case 1:
6502             if (el != 0) {
6503                 break;
6504             }
6505             /* fall through */
6506         case 0:
6507         case 2:
6508             return 1;
6509         }
6510     }
6511 
6512     if (el <= 2 && arm_is_el2_enabled(env)) {
6513         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6514         if (env->cp15.hcr_el2 & HCR_E2H) {
6515             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6516             case 1:
6517                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6518                     break;
6519                 }
6520                 /* fall through */
6521             case 0:
6522             case 2:
6523                 return 2;
6524             }
6525         } else {
6526             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6527                 return 2;
6528             }
6529         }
6530     }
6531 
6532     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6533     if (arm_feature(env, ARM_FEATURE_EL3)
6534         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6535         return 3;
6536     }
6537 #endif
6538     return 0;
6539 }
6540 
6541 /*
6542  * Return the exception level to which exceptions should be taken for SME.
6543  * C.f. the ARM pseudocode function CheckSMEAccess.
6544  */
6545 int sme_exception_el(CPUARMState *env, int el)
6546 {
6547 #ifndef CONFIG_USER_ONLY
6548     if (el <= 1 && !el_is_in_host(env, el)) {
6549         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6550         case 1:
6551             if (el != 0) {
6552                 break;
6553             }
6554             /* fall through */
6555         case 0:
6556         case 2:
6557             return 1;
6558         }
6559     }
6560 
6561     if (el <= 2 && arm_is_el2_enabled(env)) {
6562         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6563         if (env->cp15.hcr_el2 & HCR_E2H) {
6564             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6565             case 1:
6566                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6567                     break;
6568                 }
6569                 /* fall through */
6570             case 0:
6571             case 2:
6572                 return 2;
6573             }
6574         } else {
6575             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6576                 return 2;
6577             }
6578         }
6579     }
6580 
6581     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6582     if (arm_feature(env, ARM_FEATURE_EL3)
6583         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6584         return 3;
6585     }
6586 #endif
6587     return 0;
6588 }
6589 
6590 /* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6591 static bool sme_fa64(CPUARMState *env, int el)
6592 {
6593     if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6594         return false;
6595     }
6596 
6597     if (el <= 1 && !el_is_in_host(env, el)) {
6598         if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6599             return false;
6600         }
6601     }
6602     if (el <= 2 && arm_is_el2_enabled(env)) {
6603         if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6604             return false;
6605         }
6606     }
6607     if (arm_feature(env, ARM_FEATURE_EL3)) {
6608         if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6609             return false;
6610         }
6611     }
6612 
6613     return true;
6614 }
6615 
6616 /*
6617  * Given that SVE is enabled, return the vector length for EL.
6618  */
6619 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6620 {
6621     ARMCPU *cpu = env_archcpu(env);
6622     uint64_t *cr = env->vfp.zcr_el;
6623     uint32_t map = cpu->sve_vq.map;
6624     uint32_t len = ARM_MAX_VQ - 1;
6625 
6626     if (sm) {
6627         cr = env->vfp.smcr_el;
6628         map = cpu->sme_vq.map;
6629     }
6630 
6631     if (el <= 1 && !el_is_in_host(env, el)) {
6632         len = MIN(len, 0xf & (uint32_t)cr[1]);
6633     }
6634     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6635         len = MIN(len, 0xf & (uint32_t)cr[2]);
6636     }
6637     if (arm_feature(env, ARM_FEATURE_EL3)) {
6638         len = MIN(len, 0xf & (uint32_t)cr[3]);
6639     }
6640 
6641     map &= MAKE_64BIT_MASK(0, len + 1);
6642     if (map != 0) {
6643         return 31 - clz32(map);
6644     }
6645 
6646     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6647     assert(sm);
6648     return ctz32(cpu->sme_vq.map);
6649 }
6650 
6651 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6652 {
6653     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6654 }
6655 
6656 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6657                       uint64_t value)
6658 {
6659     int cur_el = arm_current_el(env);
6660     int old_len = sve_vqm1_for_el(env, cur_el);
6661     int new_len;
6662 
6663     /* Bits other than [3:0] are RAZ/WI.  */
6664     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6665     raw_write(env, ri, value & 0xf);
6666 
6667     /*
6668      * Because we arrived here, we know both FP and SVE are enabled;
6669      * otherwise we would have trapped access to the ZCR_ELn register.
6670      */
6671     new_len = sve_vqm1_for_el(env, cur_el);
6672     if (new_len < old_len) {
6673         aarch64_sve_narrow_vq(env, new_len + 1);
6674     }
6675 }
6676 
6677 static const ARMCPRegInfo zcr_reginfo[] = {
6678     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6679       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6680       .access = PL1_RW, .type = ARM_CP_SVE,
6681       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6682       .writefn = zcr_write, .raw_writefn = raw_write },
6683     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6684       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6685       .access = PL2_RW, .type = ARM_CP_SVE,
6686       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6687       .writefn = zcr_write, .raw_writefn = raw_write },
6688     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6689       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6690       .access = PL3_RW, .type = ARM_CP_SVE,
6691       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6692       .writefn = zcr_write, .raw_writefn = raw_write },
6693 };
6694 
6695 #ifdef TARGET_AARCH64
6696 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6697                                     bool isread)
6698 {
6699     int el = arm_current_el(env);
6700 
6701     if (el == 0) {
6702         uint64_t sctlr = arm_sctlr(env, el);
6703         if (!(sctlr & SCTLR_EnTP2)) {
6704             return CP_ACCESS_TRAP;
6705         }
6706     }
6707     /* TODO: FEAT_FGT */
6708     if (el < 3
6709         && arm_feature(env, ARM_FEATURE_EL3)
6710         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6711         return CP_ACCESS_TRAP_EL3;
6712     }
6713     return CP_ACCESS_OK;
6714 }
6715 
6716 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6717                                  bool isread)
6718 {
6719     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6720     if (arm_current_el(env) < 3
6721         && arm_feature(env, ARM_FEATURE_EL3)
6722         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6723         return CP_ACCESS_TRAP_EL3;
6724     }
6725     return CP_ACCESS_OK;
6726 }
6727 
6728 /* ResetSVEState */
6729 static void arm_reset_sve_state(CPUARMState *env)
6730 {
6731     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6732     /* Recall that FFR is stored as pregs[16]. */
6733     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6734     vfp_set_fpcr(env, 0x0800009f);
6735 }
6736 
6737 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6738 {
6739     uint64_t change = (env->svcr ^ new) & mask;
6740 
6741     if (change == 0) {
6742         return;
6743     }
6744     env->svcr ^= change;
6745 
6746     if (change & R_SVCR_SM_MASK) {
6747         arm_reset_sve_state(env);
6748     }
6749 
6750     /*
6751      * ResetSMEState.
6752      *
6753      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
6754      * on enable: while disabled, the storage is inaccessible and the
6755      * value does not matter.  We're not saving the storage in vmstate
6756      * when disabled either.
6757      */
6758     if (change & new & R_SVCR_ZA_MASK) {
6759         memset(env->zarray, 0, sizeof(env->zarray));
6760     }
6761 
6762     arm_rebuild_hflags(env);
6763 }
6764 
6765 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6766                        uint64_t value)
6767 {
6768     aarch64_set_svcr(env, value, -1);
6769 }
6770 
6771 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6772                        uint64_t value)
6773 {
6774     int cur_el = arm_current_el(env);
6775     int old_len = sve_vqm1_for_el(env, cur_el);
6776     int new_len;
6777 
6778     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6779     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6780     raw_write(env, ri, value);
6781 
6782     /*
6783      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6784      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6785      * current values for simplicity.  But for QEMU internals, we must still
6786      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6787      * above aarch64_sve_narrow_vq.
6788      */
6789     new_len = sve_vqm1_for_el(env, cur_el);
6790     if (new_len < old_len) {
6791         aarch64_sve_narrow_vq(env, new_len + 1);
6792     }
6793 }
6794 
6795 static const ARMCPRegInfo sme_reginfo[] = {
6796     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6797       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6798       .access = PL0_RW, .accessfn = access_tpidr2,
6799       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6800     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6801       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6802       .access = PL0_RW, .type = ARM_CP_SME,
6803       .fieldoffset = offsetof(CPUARMState, svcr),
6804       .writefn = svcr_write, .raw_writefn = raw_write },
6805     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6806       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6807       .access = PL1_RW, .type = ARM_CP_SME,
6808       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6809       .writefn = smcr_write, .raw_writefn = raw_write },
6810     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6811       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6812       .access = PL2_RW, .type = ARM_CP_SME,
6813       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6814       .writefn = smcr_write, .raw_writefn = raw_write },
6815     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6816       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6817       .access = PL3_RW, .type = ARM_CP_SME,
6818       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6819       .writefn = smcr_write, .raw_writefn = raw_write },
6820     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6821       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6822       .access = PL1_R, .accessfn = access_aa64_tid1,
6823       /*
6824        * IMPLEMENTOR = 0 (software)
6825        * REVISION    = 0 (implementation defined)
6826        * SMPS        = 0 (no streaming execution priority in QEMU)
6827        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6828        */
6829       .type = ARM_CP_CONST, .resetvalue = 0, },
6830     /*
6831      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6832      */
6833     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6834       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6835       .access = PL1_RW, .accessfn = access_esm,
6836       .type = ARM_CP_CONST, .resetvalue = 0 },
6837     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6838       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6839       .access = PL2_RW, .accessfn = access_esm,
6840       .type = ARM_CP_CONST, .resetvalue = 0 },
6841 };
6842 #endif /* TARGET_AARCH64 */
6843 
6844 static void define_pmu_regs(ARMCPU *cpu)
6845 {
6846     /*
6847      * v7 performance monitor control register: same implementor
6848      * field as main ID register, and we implement four counters in
6849      * addition to the cycle count register.
6850      */
6851     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6852     ARMCPRegInfo pmcr = {
6853         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6854         .access = PL0_RW,
6855         .type = ARM_CP_IO | ARM_CP_ALIAS,
6856         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6857         .accessfn = pmreg_access, .writefn = pmcr_write,
6858         .raw_writefn = raw_write,
6859     };
6860     ARMCPRegInfo pmcr64 = {
6861         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6862         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6863         .access = PL0_RW, .accessfn = pmreg_access,
6864         .type = ARM_CP_IO,
6865         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6866         .resetvalue = cpu->isar.reset_pmcr_el0,
6867         .writefn = pmcr_write, .raw_writefn = raw_write,
6868     };
6869 
6870     define_one_arm_cp_reg(cpu, &pmcr);
6871     define_one_arm_cp_reg(cpu, &pmcr64);
6872     for (i = 0; i < pmcrn; i++) {
6873         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6874         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6875         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6876         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6877         ARMCPRegInfo pmev_regs[] = {
6878             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6879               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6880               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6881               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6882               .accessfn = pmreg_access_xevcntr },
6883             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6884               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6885               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6886               .type = ARM_CP_IO,
6887               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6888               .raw_readfn = pmevcntr_rawread,
6889               .raw_writefn = pmevcntr_rawwrite },
6890             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6891               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6892               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6893               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6894               .accessfn = pmreg_access },
6895             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6896               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6897               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6898               .type = ARM_CP_IO,
6899               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6900               .raw_writefn = pmevtyper_rawwrite },
6901         };
6902         define_arm_cp_regs(cpu, pmev_regs);
6903         g_free(pmevcntr_name);
6904         g_free(pmevcntr_el0_name);
6905         g_free(pmevtyper_name);
6906         g_free(pmevtyper_el0_name);
6907     }
6908     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6909         ARMCPRegInfo v81_pmu_regs[] = {
6910             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6911               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6912               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6913               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6914             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6915               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6916               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6917               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6918         };
6919         define_arm_cp_regs(cpu, v81_pmu_regs);
6920     }
6921     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6922         static const ARMCPRegInfo v84_pmmir = {
6923             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6924             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6925             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6926             .resetvalue = 0
6927         };
6928         define_one_arm_cp_reg(cpu, &v84_pmmir);
6929     }
6930 }
6931 
6932 /*
6933  * We don't know until after realize whether there's a GICv3
6934  * attached, and that is what registers the gicv3 sysregs.
6935  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6936  * at runtime.
6937  */
6938 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6939 {
6940     ARMCPU *cpu = env_archcpu(env);
6941     uint64_t pfr1 = cpu->isar.id_pfr1;
6942 
6943     if (env->gicv3state) {
6944         pfr1 |= 1 << 28;
6945     }
6946     return pfr1;
6947 }
6948 
6949 #ifndef CONFIG_USER_ONLY
6950 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6951 {
6952     ARMCPU *cpu = env_archcpu(env);
6953     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6954 
6955     if (env->gicv3state) {
6956         pfr0 |= 1 << 24;
6957     }
6958     return pfr0;
6959 }
6960 #endif
6961 
6962 /*
6963  * Shared logic between LORID and the rest of the LOR* registers.
6964  * Secure state exclusion has already been dealt with.
6965  */
6966 static CPAccessResult access_lor_ns(CPUARMState *env,
6967                                     const ARMCPRegInfo *ri, bool isread)
6968 {
6969     int el = arm_current_el(env);
6970 
6971     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6972         return CP_ACCESS_TRAP_EL2;
6973     }
6974     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6975         return CP_ACCESS_TRAP_EL3;
6976     }
6977     return CP_ACCESS_OK;
6978 }
6979 
6980 static CPAccessResult access_lor_other(CPUARMState *env,
6981                                        const ARMCPRegInfo *ri, bool isread)
6982 {
6983     if (arm_is_secure_below_el3(env)) {
6984         /* Access denied in secure mode.  */
6985         return CP_ACCESS_TRAP;
6986     }
6987     return access_lor_ns(env, ri, isread);
6988 }
6989 
6990 /*
6991  * A trivial implementation of ARMv8.1-LOR leaves all of these
6992  * registers fixed at 0, which indicates that there are zero
6993  * supported Limited Ordering regions.
6994  */
6995 static const ARMCPRegInfo lor_reginfo[] = {
6996     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6997       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6998       .access = PL1_RW, .accessfn = access_lor_other,
6999       .type = ARM_CP_CONST, .resetvalue = 0 },
7000     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7001       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7002       .access = PL1_RW, .accessfn = access_lor_other,
7003       .type = ARM_CP_CONST, .resetvalue = 0 },
7004     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7005       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7006       .access = PL1_RW, .accessfn = access_lor_other,
7007       .type = ARM_CP_CONST, .resetvalue = 0 },
7008     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7009       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7010       .access = PL1_RW, .accessfn = access_lor_other,
7011       .type = ARM_CP_CONST, .resetvalue = 0 },
7012     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7013       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7014       .access = PL1_R, .accessfn = access_lor_ns,
7015       .type = ARM_CP_CONST, .resetvalue = 0 },
7016 };
7017 
7018 #ifdef TARGET_AARCH64
7019 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7020                                    bool isread)
7021 {
7022     int el = arm_current_el(env);
7023 
7024     if (el < 2 &&
7025         arm_is_el2_enabled(env) &&
7026         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7027         return CP_ACCESS_TRAP_EL2;
7028     }
7029     if (el < 3 &&
7030         arm_feature(env, ARM_FEATURE_EL3) &&
7031         !(env->cp15.scr_el3 & SCR_APK)) {
7032         return CP_ACCESS_TRAP_EL3;
7033     }
7034     return CP_ACCESS_OK;
7035 }
7036 
7037 static const ARMCPRegInfo pauth_reginfo[] = {
7038     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7039       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7040       .access = PL1_RW, .accessfn = access_pauth,
7041       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7042     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7043       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7044       .access = PL1_RW, .accessfn = access_pauth,
7045       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7046     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7047       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7048       .access = PL1_RW, .accessfn = access_pauth,
7049       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7050     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7051       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7052       .access = PL1_RW, .accessfn = access_pauth,
7053       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7054     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7055       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7056       .access = PL1_RW, .accessfn = access_pauth,
7057       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7058     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7059       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7060       .access = PL1_RW, .accessfn = access_pauth,
7061       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7062     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7063       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7064       .access = PL1_RW, .accessfn = access_pauth,
7065       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7066     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7067       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7068       .access = PL1_RW, .accessfn = access_pauth,
7069       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7070     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7071       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7072       .access = PL1_RW, .accessfn = access_pauth,
7073       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7074     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7075       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7076       .access = PL1_RW, .accessfn = access_pauth,
7077       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7078 };
7079 
7080 static const ARMCPRegInfo tlbirange_reginfo[] = {
7081     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7082       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7083       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7084       .writefn = tlbi_aa64_rvae1is_write },
7085     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7086       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7087       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7088       .writefn = tlbi_aa64_rvae1is_write },
7089    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7090       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7091       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7092       .writefn = tlbi_aa64_rvae1is_write },
7093     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7094       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7095       .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7096       .writefn = tlbi_aa64_rvae1is_write },
7097     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7098       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7099       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7100       .writefn = tlbi_aa64_rvae1is_write },
7101     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7102       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7103       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7104       .writefn = tlbi_aa64_rvae1is_write },
7105    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7106       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7107       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7108       .writefn = tlbi_aa64_rvae1is_write },
7109     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7110       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7111       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7112       .writefn = tlbi_aa64_rvae1is_write },
7113     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7114       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7115       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7116       .writefn = tlbi_aa64_rvae1_write },
7117     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7118       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7119       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7120       .writefn = tlbi_aa64_rvae1_write },
7121    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7122       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7123       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7124       .writefn = tlbi_aa64_rvae1_write },
7125     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7126       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7127       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7128       .writefn = tlbi_aa64_rvae1_write },
7129     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7130       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7131       .access = PL2_W, .type = ARM_CP_NO_RAW,
7132       .writefn = tlbi_aa64_ripas2e1is_write },
7133     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7134       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7135       .access = PL2_W, .type = ARM_CP_NO_RAW,
7136       .writefn = tlbi_aa64_ripas2e1is_write },
7137     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7138       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7139       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7140       .writefn = tlbi_aa64_rvae2is_write },
7141    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7142       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7143       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7144       .writefn = tlbi_aa64_rvae2is_write },
7145     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7146       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7147       .access = PL2_W, .type = ARM_CP_NO_RAW,
7148       .writefn = tlbi_aa64_ripas2e1_write },
7149     { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7150       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7151       .access = PL2_W, .type = ARM_CP_NO_RAW,
7152       .writefn = tlbi_aa64_ripas2e1_write },
7153    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7154       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7155       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7156       .writefn = tlbi_aa64_rvae2is_write },
7157    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7158       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7159       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7160       .writefn = tlbi_aa64_rvae2is_write },
7161     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7162       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7163       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7164       .writefn = tlbi_aa64_rvae2_write },
7165    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7166       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7167       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7168       .writefn = tlbi_aa64_rvae2_write },
7169    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7170       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7171       .access = PL3_W, .type = ARM_CP_NO_RAW,
7172       .writefn = tlbi_aa64_rvae3is_write },
7173    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7174       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7175       .access = PL3_W, .type = ARM_CP_NO_RAW,
7176       .writefn = tlbi_aa64_rvae3is_write },
7177    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7178       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7179       .access = PL3_W, .type = ARM_CP_NO_RAW,
7180       .writefn = tlbi_aa64_rvae3is_write },
7181    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7182       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7183       .access = PL3_W, .type = ARM_CP_NO_RAW,
7184       .writefn = tlbi_aa64_rvae3is_write },
7185    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7186       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7187       .access = PL3_W, .type = ARM_CP_NO_RAW,
7188       .writefn = tlbi_aa64_rvae3_write },
7189    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7190       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7191       .access = PL3_W, .type = ARM_CP_NO_RAW,
7192       .writefn = tlbi_aa64_rvae3_write },
7193 };
7194 
7195 static const ARMCPRegInfo tlbios_reginfo[] = {
7196     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7197       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7198       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7199       .writefn = tlbi_aa64_vmalle1is_write },
7200     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7201       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7202       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7203       .writefn = tlbi_aa64_vae1is_write },
7204     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7205       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7206       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7207       .writefn = tlbi_aa64_vmalle1is_write },
7208     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7209       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7210       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7211       .writefn = tlbi_aa64_vae1is_write },
7212     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7213       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7214       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7215       .writefn = tlbi_aa64_vae1is_write },
7216     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7217       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7218       .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7219       .writefn = tlbi_aa64_vae1is_write },
7220     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7221       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7222       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7223       .writefn = tlbi_aa64_alle2is_write },
7224     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7225       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7226       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7227       .writefn = tlbi_aa64_vae2is_write },
7228    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7229       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7230       .access = PL2_W, .type = ARM_CP_NO_RAW,
7231       .writefn = tlbi_aa64_alle1is_write },
7232     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7233       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7234       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7235       .writefn = tlbi_aa64_vae2is_write },
7236     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7237       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7238       .access = PL2_W, .type = ARM_CP_NO_RAW,
7239       .writefn = tlbi_aa64_alle1is_write },
7240     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7241       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7242       .access = PL2_W, .type = ARM_CP_NOP },
7243     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7244       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7245       .access = PL2_W, .type = ARM_CP_NOP },
7246     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7247       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7248       .access = PL2_W, .type = ARM_CP_NOP },
7249     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7250       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7251       .access = PL2_W, .type = ARM_CP_NOP },
7252     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7253       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7254       .access = PL3_W, .type = ARM_CP_NO_RAW,
7255       .writefn = tlbi_aa64_alle3is_write },
7256     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7257       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7258       .access = PL3_W, .type = ARM_CP_NO_RAW,
7259       .writefn = tlbi_aa64_vae3is_write },
7260     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7261       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7262       .access = PL3_W, .type = ARM_CP_NO_RAW,
7263       .writefn = tlbi_aa64_vae3is_write },
7264 };
7265 
7266 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7267 {
7268     Error *err = NULL;
7269     uint64_t ret;
7270 
7271     /* Success sets NZCV = 0000.  */
7272     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7273 
7274     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7275         /*
7276          * ??? Failed, for unknown reasons in the crypto subsystem.
7277          * The best we can do is log the reason and return the
7278          * timed-out indication to the guest.  There is no reason
7279          * we know to expect this failure to be transitory, so the
7280          * guest may well hang retrying the operation.
7281          */
7282         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7283                       ri->name, error_get_pretty(err));
7284         error_free(err);
7285 
7286         env->ZF = 0; /* NZCF = 0100 */
7287         return 0;
7288     }
7289     return ret;
7290 }
7291 
7292 /* We do not support re-seeding, so the two registers operate the same.  */
7293 static const ARMCPRegInfo rndr_reginfo[] = {
7294     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7295       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7296       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7297       .access = PL0_R, .readfn = rndr_readfn },
7298     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7299       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7300       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7301       .access = PL0_R, .readfn = rndr_readfn },
7302 };
7303 
7304 #ifndef CONFIG_USER_ONLY
7305 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7306                           uint64_t value)
7307 {
7308     ARMCPU *cpu = env_archcpu(env);
7309     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7310     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7311     uint64_t vaddr_in = (uint64_t) value;
7312     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7313     void *haddr;
7314     int mem_idx = cpu_mmu_index(env, false);
7315 
7316     /* This won't be crossing page boundaries */
7317     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7318     if (haddr) {
7319 
7320         ram_addr_t offset;
7321         MemoryRegion *mr;
7322 
7323         /* RCU lock is already being held */
7324         mr = memory_region_from_host(haddr, &offset);
7325 
7326         if (mr) {
7327             memory_region_writeback(mr, offset, dline_size);
7328         }
7329     }
7330 }
7331 
7332 static const ARMCPRegInfo dcpop_reg[] = {
7333     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7334       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7335       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7336       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7337 };
7338 
7339 static const ARMCPRegInfo dcpodp_reg[] = {
7340     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7341       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7342       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7343       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7344 };
7345 #endif /*CONFIG_USER_ONLY*/
7346 
7347 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7348                                        bool isread)
7349 {
7350     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7351         return CP_ACCESS_TRAP_EL2;
7352     }
7353 
7354     return CP_ACCESS_OK;
7355 }
7356 
7357 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7358                                  bool isread)
7359 {
7360     int el = arm_current_el(env);
7361 
7362     if (el < 2 && arm_is_el2_enabled(env)) {
7363         uint64_t hcr = arm_hcr_el2_eff(env);
7364         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7365             return CP_ACCESS_TRAP_EL2;
7366         }
7367     }
7368     if (el < 3 &&
7369         arm_feature(env, ARM_FEATURE_EL3) &&
7370         !(env->cp15.scr_el3 & SCR_ATA)) {
7371         return CP_ACCESS_TRAP_EL3;
7372     }
7373     return CP_ACCESS_OK;
7374 }
7375 
7376 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7377 {
7378     return env->pstate & PSTATE_TCO;
7379 }
7380 
7381 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7382 {
7383     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7384 }
7385 
7386 static const ARMCPRegInfo mte_reginfo[] = {
7387     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7388       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7389       .access = PL1_RW, .accessfn = access_mte,
7390       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7391     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7392       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7393       .access = PL1_RW, .accessfn = access_mte,
7394       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7395     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7396       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7397       .access = PL2_RW, .accessfn = access_mte,
7398       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7399     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7400       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7401       .access = PL3_RW,
7402       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7403     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7404       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7405       .access = PL1_RW, .accessfn = access_mte,
7406       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7407     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7408       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7409       .access = PL1_RW, .accessfn = access_mte,
7410       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7411     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7412       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7413       .access = PL1_R, .accessfn = access_aa64_tid5,
7414       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7415     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7416       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7417       .type = ARM_CP_NO_RAW,
7418       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7419     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7420       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7421       .type = ARM_CP_NOP, .access = PL1_W,
7422       .accessfn = aa64_cacheop_poc_access },
7423     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7424       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7425       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7426     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7427       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7428       .type = ARM_CP_NOP, .access = PL1_W,
7429       .accessfn = aa64_cacheop_poc_access },
7430     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7431       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7432       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7433     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7434       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7435       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7436     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7437       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7438       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7439     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7440       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7441       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7442     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7443       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7444       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7445 };
7446 
7447 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7448     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7449       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7450       .type = ARM_CP_CONST, .access = PL0_RW, },
7451 };
7452 
7453 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7454     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7455       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7456       .type = ARM_CP_NOP, .access = PL0_W,
7457       .accessfn = aa64_cacheop_poc_access },
7458     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7459       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7460       .type = ARM_CP_NOP, .access = PL0_W,
7461       .accessfn = aa64_cacheop_poc_access },
7462     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7463       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7464       .type = ARM_CP_NOP, .access = PL0_W,
7465       .accessfn = aa64_cacheop_poc_access },
7466     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7467       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7468       .type = ARM_CP_NOP, .access = PL0_W,
7469       .accessfn = aa64_cacheop_poc_access },
7470     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7471       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7472       .type = ARM_CP_NOP, .access = PL0_W,
7473       .accessfn = aa64_cacheop_poc_access },
7474     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7475       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7476       .type = ARM_CP_NOP, .access = PL0_W,
7477       .accessfn = aa64_cacheop_poc_access },
7478     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7479       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7480       .type = ARM_CP_NOP, .access = PL0_W,
7481       .accessfn = aa64_cacheop_poc_access },
7482     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7483       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7484       .type = ARM_CP_NOP, .access = PL0_W,
7485       .accessfn = aa64_cacheop_poc_access },
7486     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7487       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7488       .access = PL0_W, .type = ARM_CP_DC_GVA,
7489 #ifndef CONFIG_USER_ONLY
7490       /* Avoid overhead of an access check that always passes in user-mode */
7491       .accessfn = aa64_zva_access,
7492 #endif
7493     },
7494     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7495       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7496       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7497 #ifndef CONFIG_USER_ONLY
7498       /* Avoid overhead of an access check that always passes in user-mode */
7499       .accessfn = aa64_zva_access,
7500 #endif
7501     },
7502 };
7503 
7504 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7505                                      bool isread)
7506 {
7507     uint64_t hcr = arm_hcr_el2_eff(env);
7508     int el = arm_current_el(env);
7509 
7510     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7511         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7512             if (hcr & HCR_TGE) {
7513                 return CP_ACCESS_TRAP_EL2;
7514             }
7515             return CP_ACCESS_TRAP;
7516         }
7517     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7518         return CP_ACCESS_TRAP_EL2;
7519     }
7520     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7521         return CP_ACCESS_TRAP_EL2;
7522     }
7523     if (el < 3
7524         && arm_feature(env, ARM_FEATURE_EL3)
7525         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7526         return CP_ACCESS_TRAP_EL3;
7527     }
7528     return CP_ACCESS_OK;
7529 }
7530 
7531 static const ARMCPRegInfo scxtnum_reginfo[] = {
7532     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7533       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7534       .access = PL0_RW, .accessfn = access_scxtnum,
7535       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7536     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7537       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7538       .access = PL1_RW, .accessfn = access_scxtnum,
7539       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7540     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7541       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7542       .access = PL2_RW, .accessfn = access_scxtnum,
7543       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7544     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7545       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7546       .access = PL3_RW,
7547       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7548 };
7549 #endif /* TARGET_AARCH64 */
7550 
7551 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7552                                      bool isread)
7553 {
7554     int el = arm_current_el(env);
7555 
7556     if (el == 0) {
7557         uint64_t sctlr = arm_sctlr(env, el);
7558         if (!(sctlr & SCTLR_EnRCTX)) {
7559             return CP_ACCESS_TRAP;
7560         }
7561     } else if (el == 1) {
7562         uint64_t hcr = arm_hcr_el2_eff(env);
7563         if (hcr & HCR_NV) {
7564             return CP_ACCESS_TRAP_EL2;
7565         }
7566     }
7567     return CP_ACCESS_OK;
7568 }
7569 
7570 static const ARMCPRegInfo predinv_reginfo[] = {
7571     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7572       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7573       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7574     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7575       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7576       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7577     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7578       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7579       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7580     /*
7581      * Note the AArch32 opcodes have a different OPC1.
7582      */
7583     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7584       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7585       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7586     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7587       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7588       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7589     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7590       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7591       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7592 };
7593 
7594 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7595 {
7596     /* Read the high 32 bits of the current CCSIDR */
7597     return extract64(ccsidr_read(env, ri), 32, 32);
7598 }
7599 
7600 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7601     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7602       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7603       .access = PL1_R,
7604       .accessfn = access_tid4,
7605       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7606 };
7607 
7608 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7609                                        bool isread)
7610 {
7611     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7612         return CP_ACCESS_TRAP_EL2;
7613     }
7614 
7615     return CP_ACCESS_OK;
7616 }
7617 
7618 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7619                                        bool isread)
7620 {
7621     if (arm_feature(env, ARM_FEATURE_V8)) {
7622         return access_aa64_tid3(env, ri, isread);
7623     }
7624 
7625     return CP_ACCESS_OK;
7626 }
7627 
7628 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7629                                      bool isread)
7630 {
7631     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7632         return CP_ACCESS_TRAP_EL2;
7633     }
7634 
7635     return CP_ACCESS_OK;
7636 }
7637 
7638 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7639                                         const ARMCPRegInfo *ri, bool isread)
7640 {
7641     /*
7642      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7643      * in v7A, not in v8A.
7644      */
7645     if (!arm_feature(env, ARM_FEATURE_V8) &&
7646         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7647         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7648         return CP_ACCESS_TRAP_EL2;
7649     }
7650     return CP_ACCESS_OK;
7651 }
7652 
7653 static const ARMCPRegInfo jazelle_regs[] = {
7654     { .name = "JIDR",
7655       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7656       .access = PL1_R, .accessfn = access_jazelle,
7657       .type = ARM_CP_CONST, .resetvalue = 0 },
7658     { .name = "JOSCR",
7659       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7660       .accessfn = access_joscr_jmcr,
7661       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7662     { .name = "JMCR",
7663       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7664       .accessfn = access_joscr_jmcr,
7665       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7666 };
7667 
7668 static const ARMCPRegInfo contextidr_el2 = {
7669     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7670     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7671     .access = PL2_RW,
7672     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7673 };
7674 
7675 static const ARMCPRegInfo vhe_reginfo[] = {
7676     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7677       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7678       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7679       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7680 #ifndef CONFIG_USER_ONLY
7681     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7682       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7683       .fieldoffset =
7684         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7685       .type = ARM_CP_IO, .access = PL2_RW,
7686       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7687     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7688       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7689       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7690       .resetfn = gt_hv_timer_reset,
7691       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7692     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7693       .type = ARM_CP_IO,
7694       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7695       .access = PL2_RW,
7696       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7697       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7698     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7699       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7700       .type = ARM_CP_IO | ARM_CP_ALIAS,
7701       .access = PL2_RW, .accessfn = e2h_access,
7702       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7703       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7704     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7705       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7706       .type = ARM_CP_IO | ARM_CP_ALIAS,
7707       .access = PL2_RW, .accessfn = e2h_access,
7708       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7709       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7710     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7711       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7712       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7713       .access = PL2_RW, .accessfn = e2h_access,
7714       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7715     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7716       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7717       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7718       .access = PL2_RW, .accessfn = e2h_access,
7719       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7720     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7721       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7722       .type = ARM_CP_IO | ARM_CP_ALIAS,
7723       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7724       .access = PL2_RW, .accessfn = e2h_access,
7725       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7726     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7727       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7728       .type = ARM_CP_IO | ARM_CP_ALIAS,
7729       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7730       .access = PL2_RW, .accessfn = e2h_access,
7731       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7732 #endif
7733 };
7734 
7735 #ifndef CONFIG_USER_ONLY
7736 static const ARMCPRegInfo ats1e1_reginfo[] = {
7737     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7738       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7739       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7740       .writefn = ats_write64 },
7741     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7742       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7743       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7744       .writefn = ats_write64 },
7745 };
7746 
7747 static const ARMCPRegInfo ats1cp_reginfo[] = {
7748     { .name = "ATS1CPRP",
7749       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7750       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7751       .writefn = ats_write },
7752     { .name = "ATS1CPWP",
7753       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7754       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7755       .writefn = ats_write },
7756 };
7757 #endif
7758 
7759 /*
7760  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7761  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7762  * is non-zero, which is never for ARMv7, optionally in ARMv8
7763  * and mandatorily for ARMv8.2 and up.
7764  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7765  * implementation is RAZ/WI we can ignore this detail, as we
7766  * do for ACTLR.
7767  */
7768 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7769     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7770       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7771       .access = PL1_RW, .accessfn = access_tacr,
7772       .type = ARM_CP_CONST, .resetvalue = 0 },
7773     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7774       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7775       .access = PL2_RW, .type = ARM_CP_CONST,
7776       .resetvalue = 0 },
7777 };
7778 
7779 void register_cp_regs_for_features(ARMCPU *cpu)
7780 {
7781     /* Register all the coprocessor registers based on feature bits */
7782     CPUARMState *env = &cpu->env;
7783     if (arm_feature(env, ARM_FEATURE_M)) {
7784         /* M profile has no coprocessor registers */
7785         return;
7786     }
7787 
7788     define_arm_cp_regs(cpu, cp_reginfo);
7789     if (!arm_feature(env, ARM_FEATURE_V8)) {
7790         /*
7791          * Must go early as it is full of wildcards that may be
7792          * overridden by later definitions.
7793          */
7794         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7795     }
7796 
7797     if (arm_feature(env, ARM_FEATURE_V6)) {
7798         /* The ID registers all have impdef reset values */
7799         ARMCPRegInfo v6_idregs[] = {
7800             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7801               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7802               .access = PL1_R, .type = ARM_CP_CONST,
7803               .accessfn = access_aa32_tid3,
7804               .resetvalue = cpu->isar.id_pfr0 },
7805             /*
7806              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7807              * the value of the GIC field until after we define these regs.
7808              */
7809             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7810               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7811               .access = PL1_R, .type = ARM_CP_NO_RAW,
7812               .accessfn = access_aa32_tid3,
7813               .readfn = id_pfr1_read,
7814               .writefn = arm_cp_write_ignore },
7815             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7816               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7817               .access = PL1_R, .type = ARM_CP_CONST,
7818               .accessfn = access_aa32_tid3,
7819               .resetvalue = cpu->isar.id_dfr0 },
7820             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7821               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7822               .access = PL1_R, .type = ARM_CP_CONST,
7823               .accessfn = access_aa32_tid3,
7824               .resetvalue = cpu->id_afr0 },
7825             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7826               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7827               .access = PL1_R, .type = ARM_CP_CONST,
7828               .accessfn = access_aa32_tid3,
7829               .resetvalue = cpu->isar.id_mmfr0 },
7830             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7831               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7832               .access = PL1_R, .type = ARM_CP_CONST,
7833               .accessfn = access_aa32_tid3,
7834               .resetvalue = cpu->isar.id_mmfr1 },
7835             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7836               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7837               .access = PL1_R, .type = ARM_CP_CONST,
7838               .accessfn = access_aa32_tid3,
7839               .resetvalue = cpu->isar.id_mmfr2 },
7840             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7841               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7842               .access = PL1_R, .type = ARM_CP_CONST,
7843               .accessfn = access_aa32_tid3,
7844               .resetvalue = cpu->isar.id_mmfr3 },
7845             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7846               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7847               .access = PL1_R, .type = ARM_CP_CONST,
7848               .accessfn = access_aa32_tid3,
7849               .resetvalue = cpu->isar.id_isar0 },
7850             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7851               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7852               .access = PL1_R, .type = ARM_CP_CONST,
7853               .accessfn = access_aa32_tid3,
7854               .resetvalue = cpu->isar.id_isar1 },
7855             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7856               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7857               .access = PL1_R, .type = ARM_CP_CONST,
7858               .accessfn = access_aa32_tid3,
7859               .resetvalue = cpu->isar.id_isar2 },
7860             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7861               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7862               .access = PL1_R, .type = ARM_CP_CONST,
7863               .accessfn = access_aa32_tid3,
7864               .resetvalue = cpu->isar.id_isar3 },
7865             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7866               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7867               .access = PL1_R, .type = ARM_CP_CONST,
7868               .accessfn = access_aa32_tid3,
7869               .resetvalue = cpu->isar.id_isar4 },
7870             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7871               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7872               .access = PL1_R, .type = ARM_CP_CONST,
7873               .accessfn = access_aa32_tid3,
7874               .resetvalue = cpu->isar.id_isar5 },
7875             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7876               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7877               .access = PL1_R, .type = ARM_CP_CONST,
7878               .accessfn = access_aa32_tid3,
7879               .resetvalue = cpu->isar.id_mmfr4 },
7880             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7881               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7882               .access = PL1_R, .type = ARM_CP_CONST,
7883               .accessfn = access_aa32_tid3,
7884               .resetvalue = cpu->isar.id_isar6 },
7885         };
7886         define_arm_cp_regs(cpu, v6_idregs);
7887         define_arm_cp_regs(cpu, v6_cp_reginfo);
7888     } else {
7889         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7890     }
7891     if (arm_feature(env, ARM_FEATURE_V6K)) {
7892         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7893     }
7894     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7895         !arm_feature(env, ARM_FEATURE_PMSA)) {
7896         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7897     }
7898     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7899         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7900     }
7901     if (arm_feature(env, ARM_FEATURE_V7)) {
7902         ARMCPRegInfo clidr = {
7903             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7904             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7905             .access = PL1_R, .type = ARM_CP_CONST,
7906             .accessfn = access_tid4,
7907             .resetvalue = cpu->clidr
7908         };
7909         define_one_arm_cp_reg(cpu, &clidr);
7910         define_arm_cp_regs(cpu, v7_cp_reginfo);
7911         define_debug_regs(cpu);
7912         define_pmu_regs(cpu);
7913     } else {
7914         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7915     }
7916     if (arm_feature(env, ARM_FEATURE_V8)) {
7917         /*
7918          * v8 ID registers, which all have impdef reset values.
7919          * Note that within the ID register ranges the unused slots
7920          * must all RAZ, not UNDEF; future architecture versions may
7921          * define new registers here.
7922          * ID registers which are AArch64 views of the AArch32 ID registers
7923          * which already existed in v6 and v7 are handled elsewhere,
7924          * in v6_idregs[].
7925          */
7926         int i;
7927         ARMCPRegInfo v8_idregs[] = {
7928             /*
7929              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7930              * emulation because we don't know the right value for the
7931              * GIC field until after we define these regs.
7932              */
7933             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7934               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7935               .access = PL1_R,
7936 #ifdef CONFIG_USER_ONLY
7937               .type = ARM_CP_CONST,
7938               .resetvalue = cpu->isar.id_aa64pfr0
7939 #else
7940               .type = ARM_CP_NO_RAW,
7941               .accessfn = access_aa64_tid3,
7942               .readfn = id_aa64pfr0_read,
7943               .writefn = arm_cp_write_ignore
7944 #endif
7945             },
7946             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7947               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7948               .access = PL1_R, .type = ARM_CP_CONST,
7949               .accessfn = access_aa64_tid3,
7950               .resetvalue = cpu->isar.id_aa64pfr1},
7951             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7952               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7953               .access = PL1_R, .type = ARM_CP_CONST,
7954               .accessfn = access_aa64_tid3,
7955               .resetvalue = 0 },
7956             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7957               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7958               .access = PL1_R, .type = ARM_CP_CONST,
7959               .accessfn = access_aa64_tid3,
7960               .resetvalue = 0 },
7961             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7962               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7963               .access = PL1_R, .type = ARM_CP_CONST,
7964               .accessfn = access_aa64_tid3,
7965               .resetvalue = cpu->isar.id_aa64zfr0 },
7966             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7967               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7968               .access = PL1_R, .type = ARM_CP_CONST,
7969               .accessfn = access_aa64_tid3,
7970               .resetvalue = cpu->isar.id_aa64smfr0 },
7971             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7972               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7973               .access = PL1_R, .type = ARM_CP_CONST,
7974               .accessfn = access_aa64_tid3,
7975               .resetvalue = 0 },
7976             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7977               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7978               .access = PL1_R, .type = ARM_CP_CONST,
7979               .accessfn = access_aa64_tid3,
7980               .resetvalue = 0 },
7981             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7982               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7983               .access = PL1_R, .type = ARM_CP_CONST,
7984               .accessfn = access_aa64_tid3,
7985               .resetvalue = cpu->isar.id_aa64dfr0 },
7986             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7987               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7988               .access = PL1_R, .type = ARM_CP_CONST,
7989               .accessfn = access_aa64_tid3,
7990               .resetvalue = cpu->isar.id_aa64dfr1 },
7991             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7992               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7993               .access = PL1_R, .type = ARM_CP_CONST,
7994               .accessfn = access_aa64_tid3,
7995               .resetvalue = 0 },
7996             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7997               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7998               .access = PL1_R, .type = ARM_CP_CONST,
7999               .accessfn = access_aa64_tid3,
8000               .resetvalue = 0 },
8001             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8002               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8003               .access = PL1_R, .type = ARM_CP_CONST,
8004               .accessfn = access_aa64_tid3,
8005               .resetvalue = cpu->id_aa64afr0 },
8006             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8007               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8008               .access = PL1_R, .type = ARM_CP_CONST,
8009               .accessfn = access_aa64_tid3,
8010               .resetvalue = cpu->id_aa64afr1 },
8011             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8012               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8013               .access = PL1_R, .type = ARM_CP_CONST,
8014               .accessfn = access_aa64_tid3,
8015               .resetvalue = 0 },
8016             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8017               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8018               .access = PL1_R, .type = ARM_CP_CONST,
8019               .accessfn = access_aa64_tid3,
8020               .resetvalue = 0 },
8021             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8022               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8023               .access = PL1_R, .type = ARM_CP_CONST,
8024               .accessfn = access_aa64_tid3,
8025               .resetvalue = cpu->isar.id_aa64isar0 },
8026             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8027               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8028               .access = PL1_R, .type = ARM_CP_CONST,
8029               .accessfn = access_aa64_tid3,
8030               .resetvalue = cpu->isar.id_aa64isar1 },
8031             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8032               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8033               .access = PL1_R, .type = ARM_CP_CONST,
8034               .accessfn = access_aa64_tid3,
8035               .resetvalue = 0 },
8036             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8037               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8038               .access = PL1_R, .type = ARM_CP_CONST,
8039               .accessfn = access_aa64_tid3,
8040               .resetvalue = 0 },
8041             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8042               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8043               .access = PL1_R, .type = ARM_CP_CONST,
8044               .accessfn = access_aa64_tid3,
8045               .resetvalue = 0 },
8046             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8047               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8048               .access = PL1_R, .type = ARM_CP_CONST,
8049               .accessfn = access_aa64_tid3,
8050               .resetvalue = 0 },
8051             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8052               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8053               .access = PL1_R, .type = ARM_CP_CONST,
8054               .accessfn = access_aa64_tid3,
8055               .resetvalue = 0 },
8056             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8057               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8058               .access = PL1_R, .type = ARM_CP_CONST,
8059               .accessfn = access_aa64_tid3,
8060               .resetvalue = 0 },
8061             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8062               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8063               .access = PL1_R, .type = ARM_CP_CONST,
8064               .accessfn = access_aa64_tid3,
8065               .resetvalue = cpu->isar.id_aa64mmfr0 },
8066             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8067               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8068               .access = PL1_R, .type = ARM_CP_CONST,
8069               .accessfn = access_aa64_tid3,
8070               .resetvalue = cpu->isar.id_aa64mmfr1 },
8071             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8072               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8073               .access = PL1_R, .type = ARM_CP_CONST,
8074               .accessfn = access_aa64_tid3,
8075               .resetvalue = cpu->isar.id_aa64mmfr2 },
8076             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8077               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8078               .access = PL1_R, .type = ARM_CP_CONST,
8079               .accessfn = access_aa64_tid3,
8080               .resetvalue = 0 },
8081             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8082               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8083               .access = PL1_R, .type = ARM_CP_CONST,
8084               .accessfn = access_aa64_tid3,
8085               .resetvalue = 0 },
8086             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8087               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8088               .access = PL1_R, .type = ARM_CP_CONST,
8089               .accessfn = access_aa64_tid3,
8090               .resetvalue = 0 },
8091             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8092               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8093               .access = PL1_R, .type = ARM_CP_CONST,
8094               .accessfn = access_aa64_tid3,
8095               .resetvalue = 0 },
8096             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8097               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8098               .access = PL1_R, .type = ARM_CP_CONST,
8099               .accessfn = access_aa64_tid3,
8100               .resetvalue = 0 },
8101             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8102               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8103               .access = PL1_R, .type = ARM_CP_CONST,
8104               .accessfn = access_aa64_tid3,
8105               .resetvalue = cpu->isar.mvfr0 },
8106             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8107               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8108               .access = PL1_R, .type = ARM_CP_CONST,
8109               .accessfn = access_aa64_tid3,
8110               .resetvalue = cpu->isar.mvfr1 },
8111             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8112               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8113               .access = PL1_R, .type = ARM_CP_CONST,
8114               .accessfn = access_aa64_tid3,
8115               .resetvalue = cpu->isar.mvfr2 },
8116             /*
8117              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8118              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8119              * as RAZ, since it is in the "reserved for future ID
8120              * registers, RAZ" part of the AArch32 encoding space.
8121              */
8122             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8123               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8124               .access = PL1_R, .type = ARM_CP_CONST,
8125               .accessfn = access_aa64_tid3,
8126               .resetvalue = 0 },
8127             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8128               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8129               .access = PL1_R, .type = ARM_CP_CONST,
8130               .accessfn = access_aa64_tid3,
8131               .resetvalue = 0 },
8132             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8133               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8134               .access = PL1_R, .type = ARM_CP_CONST,
8135               .accessfn = access_aa64_tid3,
8136               .resetvalue = 0 },
8137             /*
8138              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8139              * they're also RAZ for AArch64, and in v8 are gradually
8140              * being filled with AArch64-view-of-AArch32-ID-register
8141              * for new ID registers.
8142              */
8143             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8144               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8145               .access = PL1_R, .type = ARM_CP_CONST,
8146               .accessfn = access_aa64_tid3,
8147               .resetvalue = 0 },
8148             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8149               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8150               .access = PL1_R, .type = ARM_CP_CONST,
8151               .accessfn = access_aa64_tid3,
8152               .resetvalue = cpu->isar.id_pfr2 },
8153             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8154               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8155               .access = PL1_R, .type = ARM_CP_CONST,
8156               .accessfn = access_aa64_tid3,
8157               .resetvalue = cpu->isar.id_dfr1 },
8158             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8159               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8160               .access = PL1_R, .type = ARM_CP_CONST,
8161               .accessfn = access_aa64_tid3,
8162               .resetvalue = cpu->isar.id_mmfr5 },
8163             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8164               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8165               .access = PL1_R, .type = ARM_CP_CONST,
8166               .accessfn = access_aa64_tid3,
8167               .resetvalue = 0 },
8168             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8169               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8170               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8171               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8172             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8173               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8174               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8175               .resetvalue = cpu->pmceid0 },
8176             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8177               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8178               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8179               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8180             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8181               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8182               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8183               .resetvalue = cpu->pmceid1 },
8184         };
8185 #ifdef CONFIG_USER_ONLY
8186         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8187             { .name = "ID_AA64PFR0_EL1",
8188               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8189                                R_ID_AA64PFR0_ADVSIMD_MASK |
8190                                R_ID_AA64PFR0_SVE_MASK |
8191                                R_ID_AA64PFR0_DIT_MASK,
8192               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8193                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8194             { .name = "ID_AA64PFR1_EL1",
8195               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8196                                R_ID_AA64PFR1_SSBS_MASK |
8197                                R_ID_AA64PFR1_MTE_MASK |
8198                                R_ID_AA64PFR1_SME_MASK },
8199             { .name = "ID_AA64PFR*_EL1_RESERVED",
8200               .is_glob = true },
8201             { .name = "ID_AA64ZFR0_EL1",
8202               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8203                                R_ID_AA64ZFR0_AES_MASK |
8204                                R_ID_AA64ZFR0_BITPERM_MASK |
8205                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8206                                R_ID_AA64ZFR0_SHA3_MASK |
8207                                R_ID_AA64ZFR0_SM4_MASK |
8208                                R_ID_AA64ZFR0_I8MM_MASK |
8209                                R_ID_AA64ZFR0_F32MM_MASK |
8210                                R_ID_AA64ZFR0_F64MM_MASK },
8211             { .name = "ID_AA64SMFR0_EL1",
8212               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8213                                R_ID_AA64SMFR0_B16F32_MASK |
8214                                R_ID_AA64SMFR0_F16F32_MASK |
8215                                R_ID_AA64SMFR0_I8I32_MASK |
8216                                R_ID_AA64SMFR0_F64F64_MASK |
8217                                R_ID_AA64SMFR0_I16I64_MASK |
8218                                R_ID_AA64SMFR0_FA64_MASK },
8219             { .name = "ID_AA64MMFR0_EL1",
8220               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8221               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8222                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8223             { .name = "ID_AA64MMFR1_EL1",
8224               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8225             { .name = "ID_AA64MMFR2_EL1",
8226               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8227             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8228               .is_glob = true },
8229             { .name = "ID_AA64DFR0_EL1",
8230               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8231             { .name = "ID_AA64DFR1_EL1" },
8232             { .name = "ID_AA64DFR*_EL1_RESERVED",
8233               .is_glob = true },
8234             { .name = "ID_AA64AFR*",
8235               .is_glob = true },
8236             { .name = "ID_AA64ISAR0_EL1",
8237               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8238                                R_ID_AA64ISAR0_SHA1_MASK |
8239                                R_ID_AA64ISAR0_SHA2_MASK |
8240                                R_ID_AA64ISAR0_CRC32_MASK |
8241                                R_ID_AA64ISAR0_ATOMIC_MASK |
8242                                R_ID_AA64ISAR0_RDM_MASK |
8243                                R_ID_AA64ISAR0_SHA3_MASK |
8244                                R_ID_AA64ISAR0_SM3_MASK |
8245                                R_ID_AA64ISAR0_SM4_MASK |
8246                                R_ID_AA64ISAR0_DP_MASK |
8247                                R_ID_AA64ISAR0_FHM_MASK |
8248                                R_ID_AA64ISAR0_TS_MASK |
8249                                R_ID_AA64ISAR0_RNDR_MASK },
8250             { .name = "ID_AA64ISAR1_EL1",
8251               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8252                                R_ID_AA64ISAR1_APA_MASK |
8253                                R_ID_AA64ISAR1_API_MASK |
8254                                R_ID_AA64ISAR1_JSCVT_MASK |
8255                                R_ID_AA64ISAR1_FCMA_MASK |
8256                                R_ID_AA64ISAR1_LRCPC_MASK |
8257                                R_ID_AA64ISAR1_GPA_MASK |
8258                                R_ID_AA64ISAR1_GPI_MASK |
8259                                R_ID_AA64ISAR1_FRINTTS_MASK |
8260                                R_ID_AA64ISAR1_SB_MASK |
8261                                R_ID_AA64ISAR1_BF16_MASK |
8262                                R_ID_AA64ISAR1_DGH_MASK |
8263                                R_ID_AA64ISAR1_I8MM_MASK },
8264             { .name = "ID_AA64ISAR2_EL1",
8265               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8266                                R_ID_AA64ISAR2_RPRES_MASK |
8267                                R_ID_AA64ISAR2_GPA3_MASK |
8268                                R_ID_AA64ISAR2_APA3_MASK },
8269             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8270               .is_glob = true },
8271         };
8272         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8273 #endif
8274         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
8275         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8276             !arm_feature(env, ARM_FEATURE_EL2)) {
8277             ARMCPRegInfo rvbar = {
8278                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8279                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8280                 .access = PL1_R,
8281                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8282             };
8283             define_one_arm_cp_reg(cpu, &rvbar);
8284         }
8285         define_arm_cp_regs(cpu, v8_idregs);
8286         define_arm_cp_regs(cpu, v8_cp_reginfo);
8287 
8288         for (i = 4; i < 16; i++) {
8289             /*
8290              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8291              * For pre-v8 cores there are RAZ patterns for these in
8292              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8293              * v8 extends the "must RAZ" part of the ID register space
8294              * to also cover c0, 0, c{8-15}, {0-7}.
8295              * These are STATE_AA32 because in the AArch64 sysreg space
8296              * c4-c7 is where the AArch64 ID registers live (and we've
8297              * already defined those in v8_idregs[]), and c8-c15 are not
8298              * "must RAZ" for AArch64.
8299              */
8300             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8301             ARMCPRegInfo v8_aa32_raz_idregs = {
8302                 .name = name,
8303                 .state = ARM_CP_STATE_AA32,
8304                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8305                 .access = PL1_R, .type = ARM_CP_CONST,
8306                 .accessfn = access_aa64_tid3,
8307                 .resetvalue = 0 };
8308             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8309         }
8310     }
8311 
8312     /*
8313      * Register the base EL2 cpregs.
8314      * Pre v8, these registers are implemented only as part of the
8315      * Virtualization Extensions (EL2 present).  Beginning with v8,
8316      * if EL2 is missing but EL3 is enabled, mostly these become
8317      * RES0 from EL3, with some specific exceptions.
8318      */
8319     if (arm_feature(env, ARM_FEATURE_EL2)
8320         || (arm_feature(env, ARM_FEATURE_EL3)
8321             && arm_feature(env, ARM_FEATURE_V8))) {
8322         uint64_t vmpidr_def = mpidr_read_val(env);
8323         ARMCPRegInfo vpidr_regs[] = {
8324             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8325               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8326               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8327               .resetvalue = cpu->midr,
8328               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8329               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8330             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8331               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8332               .access = PL2_RW, .resetvalue = cpu->midr,
8333               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8334               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8335             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8336               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8337               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8338               .resetvalue = vmpidr_def,
8339               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8340               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8341             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8342               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8343               .access = PL2_RW, .resetvalue = vmpidr_def,
8344               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8345               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8346         };
8347         /*
8348          * The only field of MDCR_EL2 that has a defined architectural reset
8349          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8350          */
8351         ARMCPRegInfo mdcr_el2 = {
8352             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8353             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8354             .writefn = mdcr_el2_write,
8355             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8356             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8357         };
8358         define_one_arm_cp_reg(cpu, &mdcr_el2);
8359         define_arm_cp_regs(cpu, vpidr_regs);
8360         define_arm_cp_regs(cpu, el2_cp_reginfo);
8361         if (arm_feature(env, ARM_FEATURE_V8)) {
8362             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8363         }
8364         if (cpu_isar_feature(aa64_sel2, cpu)) {
8365             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8366         }
8367         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
8368         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8369             ARMCPRegInfo rvbar[] = {
8370                 {
8371                     .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8372                     .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8373                     .access = PL2_R,
8374                     .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8375                 },
8376                 {   .name = "RVBAR", .type = ARM_CP_ALIAS,
8377                     .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8378                     .access = PL2_R,
8379                     .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8380                 },
8381             };
8382             define_arm_cp_regs(cpu, rvbar);
8383         }
8384     }
8385 
8386     /* Register the base EL3 cpregs. */
8387     if (arm_feature(env, ARM_FEATURE_EL3)) {
8388         define_arm_cp_regs(cpu, el3_cp_reginfo);
8389         ARMCPRegInfo el3_regs[] = {
8390             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8391               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8392               .access = PL3_R,
8393               .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8394             },
8395             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8396               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8397               .access = PL3_RW,
8398               .raw_writefn = raw_write, .writefn = sctlr_write,
8399               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8400               .resetvalue = cpu->reset_sctlr },
8401         };
8402 
8403         define_arm_cp_regs(cpu, el3_regs);
8404     }
8405     /*
8406      * The behaviour of NSACR is sufficiently various that we don't
8407      * try to describe it in a single reginfo:
8408      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8409      *     reads as constant 0xc00 from NS EL1 and NS EL2
8410      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8411      *  if v7 without EL3, register doesn't exist
8412      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8413      */
8414     if (arm_feature(env, ARM_FEATURE_EL3)) {
8415         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8416             static const ARMCPRegInfo nsacr = {
8417                 .name = "NSACR", .type = ARM_CP_CONST,
8418                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8419                 .access = PL1_RW, .accessfn = nsacr_access,
8420                 .resetvalue = 0xc00
8421             };
8422             define_one_arm_cp_reg(cpu, &nsacr);
8423         } else {
8424             static const ARMCPRegInfo nsacr = {
8425                 .name = "NSACR",
8426                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8427                 .access = PL3_RW | PL1_R,
8428                 .resetvalue = 0,
8429                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8430             };
8431             define_one_arm_cp_reg(cpu, &nsacr);
8432         }
8433     } else {
8434         if (arm_feature(env, ARM_FEATURE_V8)) {
8435             static const ARMCPRegInfo nsacr = {
8436                 .name = "NSACR", .type = ARM_CP_CONST,
8437                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8438                 .access = PL1_R,
8439                 .resetvalue = 0xc00
8440             };
8441             define_one_arm_cp_reg(cpu, &nsacr);
8442         }
8443     }
8444 
8445     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8446         if (arm_feature(env, ARM_FEATURE_V6)) {
8447             /* PMSAv6 not implemented */
8448             assert(arm_feature(env, ARM_FEATURE_V7));
8449             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8450             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8451         } else {
8452             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8453         }
8454     } else {
8455         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8456         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8457         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8458         if (cpu_isar_feature(aa32_hpd, cpu)) {
8459             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8460         }
8461     }
8462     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8463         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8464     }
8465     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8466         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8467     }
8468     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8469         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8470     }
8471     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8472         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8473     }
8474     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8475         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8476     }
8477     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8478         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8479     }
8480     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8481         define_arm_cp_regs(cpu, omap_cp_reginfo);
8482     }
8483     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8484         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8485     }
8486     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8487         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8488     }
8489     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8490         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8491     }
8492     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8493         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8494     }
8495     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8496         define_arm_cp_regs(cpu, jazelle_regs);
8497     }
8498     /*
8499      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8500      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8501      * be read-only (ie write causes UNDEF exception).
8502      */
8503     {
8504         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8505             /*
8506              * Pre-v8 MIDR space.
8507              * Note that the MIDR isn't a simple constant register because
8508              * of the TI925 behaviour where writes to another register can
8509              * cause the MIDR value to change.
8510              *
8511              * Unimplemented registers in the c15 0 0 0 space default to
8512              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8513              * and friends override accordingly.
8514              */
8515             { .name = "MIDR",
8516               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8517               .access = PL1_R, .resetvalue = cpu->midr,
8518               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8519               .readfn = midr_read,
8520               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8521               .type = ARM_CP_OVERRIDE },
8522             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8523             { .name = "DUMMY",
8524               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8525               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8526             { .name = "DUMMY",
8527               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8528               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8529             { .name = "DUMMY",
8530               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8531               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8532             { .name = "DUMMY",
8533               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8534               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8535             { .name = "DUMMY",
8536               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8537               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8538         };
8539         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8540             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8541               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8542               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8543               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8544               .readfn = midr_read },
8545             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8546             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8547               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8548               .access = PL1_R, .resetvalue = cpu->midr },
8549             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8550               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8551               .access = PL1_R,
8552               .accessfn = access_aa64_tid1,
8553               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8554         };
8555         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8556             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8557             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8558             .access = PL1_R, .resetvalue = cpu->midr
8559         };
8560         ARMCPRegInfo id_cp_reginfo[] = {
8561             /* These are common to v8 and pre-v8 */
8562             { .name = "CTR",
8563               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8564               .access = PL1_R, .accessfn = ctr_el0_access,
8565               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8566             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8567               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8568               .access = PL0_R, .accessfn = ctr_el0_access,
8569               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8570             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8571             { .name = "TCMTR",
8572               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8573               .access = PL1_R,
8574               .accessfn = access_aa32_tid1,
8575               .type = ARM_CP_CONST, .resetvalue = 0 },
8576         };
8577         /* TLBTR is specific to VMSA */
8578         ARMCPRegInfo id_tlbtr_reginfo = {
8579               .name = "TLBTR",
8580               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8581               .access = PL1_R,
8582               .accessfn = access_aa32_tid1,
8583               .type = ARM_CP_CONST, .resetvalue = 0,
8584         };
8585         /* MPUIR is specific to PMSA V6+ */
8586         ARMCPRegInfo id_mpuir_reginfo = {
8587               .name = "MPUIR",
8588               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8589               .access = PL1_R, .type = ARM_CP_CONST,
8590               .resetvalue = cpu->pmsav7_dregion << 8
8591         };
8592         /* HMPUIR is specific to PMSA V8 */
8593         ARMCPRegInfo id_hmpuir_reginfo = {
8594             .name = "HMPUIR",
8595             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8596             .access = PL2_R, .type = ARM_CP_CONST,
8597             .resetvalue = cpu->pmsav8r_hdregion
8598         };
8599         static const ARMCPRegInfo crn0_wi_reginfo = {
8600             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8601             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8602             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8603         };
8604 #ifdef CONFIG_USER_ONLY
8605         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8606             { .name = "MIDR_EL1",
8607               .exported_bits = R_MIDR_EL1_REVISION_MASK |
8608                                R_MIDR_EL1_PARTNUM_MASK |
8609                                R_MIDR_EL1_ARCHITECTURE_MASK |
8610                                R_MIDR_EL1_VARIANT_MASK |
8611                                R_MIDR_EL1_IMPLEMENTER_MASK },
8612             { .name = "REVIDR_EL1" },
8613         };
8614         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8615 #endif
8616         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8617             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8618             size_t i;
8619             /*
8620              * Register the blanket "writes ignored" value first to cover the
8621              * whole space. Then update the specific ID registers to allow write
8622              * access, so that they ignore writes rather than causing them to
8623              * UNDEF.
8624              */
8625             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8626             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8627                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8628             }
8629             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8630                 id_cp_reginfo[i].access = PL1_RW;
8631             }
8632             id_mpuir_reginfo.access = PL1_RW;
8633             id_tlbtr_reginfo.access = PL1_RW;
8634         }
8635         if (arm_feature(env, ARM_FEATURE_V8)) {
8636             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8637             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8638                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8639             }
8640         } else {
8641             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8642         }
8643         define_arm_cp_regs(cpu, id_cp_reginfo);
8644         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8645             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8646         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8647                    arm_feature(env, ARM_FEATURE_V8)) {
8648             uint32_t i = 0;
8649             char *tmp_string;
8650 
8651             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8652             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8653             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8654 
8655             /* Register alias is only valid for first 32 indexes */
8656             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8657                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8658                 uint8_t opc1 = extract32(i, 4, 1);
8659                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8660 
8661                 tmp_string = g_strdup_printf("PRBAR%u", i);
8662                 ARMCPRegInfo tmp_prbarn_reginfo = {
8663                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8664                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8665                     .access = PL1_RW, .resetvalue = 0,
8666                     .accessfn = access_tvm_trvm,
8667                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8668                 };
8669                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8670                 g_free(tmp_string);
8671 
8672                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8673                 tmp_string = g_strdup_printf("PRLAR%u", i);
8674                 ARMCPRegInfo tmp_prlarn_reginfo = {
8675                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8676                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8677                     .access = PL1_RW, .resetvalue = 0,
8678                     .accessfn = access_tvm_trvm,
8679                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8680                 };
8681                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8682                 g_free(tmp_string);
8683             }
8684 
8685             /* Register alias is only valid for first 32 indexes */
8686             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8687                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8688                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8689                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8690 
8691                 tmp_string = g_strdup_printf("HPRBAR%u", i);
8692                 ARMCPRegInfo tmp_hprbarn_reginfo = {
8693                     .name = tmp_string,
8694                     .type = ARM_CP_NO_RAW,
8695                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8696                     .access = PL2_RW, .resetvalue = 0,
8697                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8698                 };
8699                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8700                 g_free(tmp_string);
8701 
8702                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8703                 tmp_string = g_strdup_printf("HPRLAR%u", i);
8704                 ARMCPRegInfo tmp_hprlarn_reginfo = {
8705                     .name = tmp_string,
8706                     .type = ARM_CP_NO_RAW,
8707                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8708                     .access = PL2_RW, .resetvalue = 0,
8709                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8710                 };
8711                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8712                 g_free(tmp_string);
8713             }
8714         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8715             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8716         }
8717     }
8718 
8719     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8720         ARMCPRegInfo mpidr_cp_reginfo[] = {
8721             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8722               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8723               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8724         };
8725 #ifdef CONFIG_USER_ONLY
8726         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8727             { .name = "MPIDR_EL1",
8728               .fixed_bits = 0x0000000080000000 },
8729         };
8730         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8731 #endif
8732         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8733     }
8734 
8735     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8736         ARMCPRegInfo auxcr_reginfo[] = {
8737             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8738               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8739               .access = PL1_RW, .accessfn = access_tacr,
8740               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8741             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8742               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8743               .access = PL2_RW, .type = ARM_CP_CONST,
8744               .resetvalue = 0 },
8745             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8746               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8747               .access = PL3_RW, .type = ARM_CP_CONST,
8748               .resetvalue = 0 },
8749         };
8750         define_arm_cp_regs(cpu, auxcr_reginfo);
8751         if (cpu_isar_feature(aa32_ac2, cpu)) {
8752             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8753         }
8754     }
8755 
8756     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8757         /*
8758          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8759          * There are two flavours:
8760          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8761          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8762          *      32-bit register visible to AArch32 at a different encoding
8763          *      to the "flavour 1" register and with the bits rearranged to
8764          *      be able to squash a 64-bit address into the 32-bit view.
8765          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8766          * in future if we support AArch32-only configs of some of the
8767          * AArch64 cores we might need to add a specific feature flag
8768          * to indicate cores with "flavour 2" CBAR.
8769          */
8770         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8771             /* 32 bit view is [31:18] 0...0 [43:32]. */
8772             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8773                 | extract64(cpu->reset_cbar, 32, 12);
8774             ARMCPRegInfo cbar_reginfo[] = {
8775                 { .name = "CBAR",
8776                   .type = ARM_CP_CONST,
8777                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8778                   .access = PL1_R, .resetvalue = cbar32 },
8779                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8780                   .type = ARM_CP_CONST,
8781                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8782                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8783             };
8784             /* We don't implement a r/w 64 bit CBAR currently */
8785             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8786             define_arm_cp_regs(cpu, cbar_reginfo);
8787         } else {
8788             ARMCPRegInfo cbar = {
8789                 .name = "CBAR",
8790                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8791                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
8792                 .fieldoffset = offsetof(CPUARMState,
8793                                         cp15.c15_config_base_address)
8794             };
8795             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8796                 cbar.access = PL1_R;
8797                 cbar.fieldoffset = 0;
8798                 cbar.type = ARM_CP_CONST;
8799             }
8800             define_one_arm_cp_reg(cpu, &cbar);
8801         }
8802     }
8803 
8804     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8805         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8806             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8807               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8808               .access = PL1_RW, .writefn = vbar_write,
8809               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8810                                      offsetof(CPUARMState, cp15.vbar_ns) },
8811               .resetvalue = 0 },
8812         };
8813         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8814     }
8815 
8816     /* Generic registers whose values depend on the implementation */
8817     {
8818         ARMCPRegInfo sctlr = {
8819             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8820             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8821             .access = PL1_RW, .accessfn = access_tvm_trvm,
8822             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8823                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8824             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8825             .raw_writefn = raw_write,
8826         };
8827         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8828             /*
8829              * Normally we would always end the TB on an SCTLR write, but Linux
8830              * arch/arm/mach-pxa/sleep.S expects two instructions following
8831              * an MMU enable to execute from cache.  Imitate this behaviour.
8832              */
8833             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8834         }
8835         define_one_arm_cp_reg(cpu, &sctlr);
8836 
8837         if (arm_feature(env, ARM_FEATURE_PMSA) &&
8838             arm_feature(env, ARM_FEATURE_V8)) {
8839             ARMCPRegInfo vsctlr = {
8840                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8841                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8842                 .access = PL2_RW, .resetvalue = 0x0,
8843                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8844             };
8845             define_one_arm_cp_reg(cpu, &vsctlr);
8846         }
8847     }
8848 
8849     if (cpu_isar_feature(aa64_lor, cpu)) {
8850         define_arm_cp_regs(cpu, lor_reginfo);
8851     }
8852     if (cpu_isar_feature(aa64_pan, cpu)) {
8853         define_one_arm_cp_reg(cpu, &pan_reginfo);
8854     }
8855 #ifndef CONFIG_USER_ONLY
8856     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8857         define_arm_cp_regs(cpu, ats1e1_reginfo);
8858     }
8859     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8860         define_arm_cp_regs(cpu, ats1cp_reginfo);
8861     }
8862 #endif
8863     if (cpu_isar_feature(aa64_uao, cpu)) {
8864         define_one_arm_cp_reg(cpu, &uao_reginfo);
8865     }
8866 
8867     if (cpu_isar_feature(aa64_dit, cpu)) {
8868         define_one_arm_cp_reg(cpu, &dit_reginfo);
8869     }
8870     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8871         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8872     }
8873     if (cpu_isar_feature(any_ras, cpu)) {
8874         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8875     }
8876 
8877     if (cpu_isar_feature(aa64_vh, cpu) ||
8878         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8879         define_one_arm_cp_reg(cpu, &contextidr_el2);
8880     }
8881     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8882         define_arm_cp_regs(cpu, vhe_reginfo);
8883     }
8884 
8885     if (cpu_isar_feature(aa64_sve, cpu)) {
8886         define_arm_cp_regs(cpu, zcr_reginfo);
8887     }
8888 
8889     if (cpu_isar_feature(aa64_hcx, cpu)) {
8890         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8891     }
8892 
8893 #ifdef TARGET_AARCH64
8894     if (cpu_isar_feature(aa64_sme, cpu)) {
8895         define_arm_cp_regs(cpu, sme_reginfo);
8896     }
8897     if (cpu_isar_feature(aa64_pauth, cpu)) {
8898         define_arm_cp_regs(cpu, pauth_reginfo);
8899     }
8900     if (cpu_isar_feature(aa64_rndr, cpu)) {
8901         define_arm_cp_regs(cpu, rndr_reginfo);
8902     }
8903     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8904         define_arm_cp_regs(cpu, tlbirange_reginfo);
8905     }
8906     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8907         define_arm_cp_regs(cpu, tlbios_reginfo);
8908     }
8909 #ifndef CONFIG_USER_ONLY
8910     /* Data Cache clean instructions up to PoP */
8911     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8912         define_one_arm_cp_reg(cpu, dcpop_reg);
8913 
8914         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8915             define_one_arm_cp_reg(cpu, dcpodp_reg);
8916         }
8917     }
8918 #endif /*CONFIG_USER_ONLY*/
8919 
8920     /*
8921      * If full MTE is enabled, add all of the system registers.
8922      * If only "instructions available at EL0" are enabled,
8923      * then define only a RAZ/WI version of PSTATE.TCO.
8924      */
8925     if (cpu_isar_feature(aa64_mte, cpu)) {
8926         define_arm_cp_regs(cpu, mte_reginfo);
8927         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8928     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8929         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8930         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8931     }
8932 
8933     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8934         define_arm_cp_regs(cpu, scxtnum_reginfo);
8935     }
8936 #endif
8937 
8938     if (cpu_isar_feature(any_predinv, cpu)) {
8939         define_arm_cp_regs(cpu, predinv_reginfo);
8940     }
8941 
8942     if (cpu_isar_feature(any_ccidx, cpu)) {
8943         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8944     }
8945 
8946 #ifndef CONFIG_USER_ONLY
8947     /*
8948      * Register redirections and aliases must be done last,
8949      * after the registers from the other extensions have been defined.
8950      */
8951     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8952         define_arm_vh_e2h_redirects_aliases(cpu);
8953     }
8954 #endif
8955 }
8956 
8957 /* Sort alphabetically by type name, except for "any". */
8958 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8959 {
8960     ObjectClass *class_a = (ObjectClass *)a;
8961     ObjectClass *class_b = (ObjectClass *)b;
8962     const char *name_a, *name_b;
8963 
8964     name_a = object_class_get_name(class_a);
8965     name_b = object_class_get_name(class_b);
8966     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8967         return 1;
8968     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8969         return -1;
8970     } else {
8971         return strcmp(name_a, name_b);
8972     }
8973 }
8974 
8975 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8976 {
8977     ObjectClass *oc = data;
8978     CPUClass *cc = CPU_CLASS(oc);
8979     const char *typename;
8980     char *name;
8981 
8982     typename = object_class_get_name(oc);
8983     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8984     if (cc->deprecation_note) {
8985         qemu_printf("  %s (deprecated)\n", name);
8986     } else {
8987         qemu_printf("  %s\n", name);
8988     }
8989     g_free(name);
8990 }
8991 
8992 void arm_cpu_list(void)
8993 {
8994     GSList *list;
8995 
8996     list = object_class_get_list(TYPE_ARM_CPU, false);
8997     list = g_slist_sort(list, arm_cpu_list_compare);
8998     qemu_printf("Available CPUs:\n");
8999     g_slist_foreach(list, arm_cpu_list_entry, NULL);
9000     g_slist_free(list);
9001 }
9002 
9003 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
9004 {
9005     ObjectClass *oc = data;
9006     CpuDefinitionInfoList **cpu_list = user_data;
9007     CpuDefinitionInfo *info;
9008     const char *typename;
9009 
9010     typename = object_class_get_name(oc);
9011     info = g_malloc0(sizeof(*info));
9012     info->name = g_strndup(typename,
9013                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
9014     info->q_typename = g_strdup(typename);
9015 
9016     QAPI_LIST_PREPEND(*cpu_list, info);
9017 }
9018 
9019 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
9020 {
9021     CpuDefinitionInfoList *cpu_list = NULL;
9022     GSList *list;
9023 
9024     list = object_class_get_list(TYPE_ARM_CPU, false);
9025     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
9026     g_slist_free(list);
9027 
9028     return cpu_list;
9029 }
9030 
9031 /*
9032  * Private utility function for define_one_arm_cp_reg_with_opaque():
9033  * add a single reginfo struct to the hash table.
9034  */
9035 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9036                                    void *opaque, CPState state,
9037                                    CPSecureState secstate,
9038                                    int crm, int opc1, int opc2,
9039                                    const char *name)
9040 {
9041     CPUARMState *env = &cpu->env;
9042     uint32_t key;
9043     ARMCPRegInfo *r2;
9044     bool is64 = r->type & ARM_CP_64BIT;
9045     bool ns = secstate & ARM_CP_SECSTATE_NS;
9046     int cp = r->cp;
9047     size_t name_len;
9048     bool make_const;
9049 
9050     switch (state) {
9051     case ARM_CP_STATE_AA32:
9052         /* We assume it is a cp15 register if the .cp field is left unset. */
9053         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9054             cp = 15;
9055         }
9056         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9057         break;
9058     case ARM_CP_STATE_AA64:
9059         /*
9060          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9061          * cp == 0 as equivalent to the value for "standard guest-visible
9062          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9063          * in their AArch64 view (the .cp value may be non-zero for the
9064          * benefit of the AArch32 view).
9065          */
9066         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9067             cp = CP_REG_ARM64_SYSREG_CP;
9068         }
9069         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9070         break;
9071     default:
9072         g_assert_not_reached();
9073     }
9074 
9075     /* Overriding of an existing definition must be explicitly requested. */
9076     if (!(r->type & ARM_CP_OVERRIDE)) {
9077         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9078         if (oldreg) {
9079             assert(oldreg->type & ARM_CP_OVERRIDE);
9080         }
9081     }
9082 
9083     /*
9084      * Eliminate registers that are not present because the EL is missing.
9085      * Doing this here makes it easier to put all registers for a given
9086      * feature into the same ARMCPRegInfo array and define them all at once.
9087      */
9088     make_const = false;
9089     if (arm_feature(env, ARM_FEATURE_EL3)) {
9090         /*
9091          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9092          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9093          */
9094         int min_el = ctz32(r->access) / 2;
9095         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9096             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9097                 return;
9098             }
9099             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9100         }
9101     } else {
9102         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9103                                  ? PL2_RW : PL1_RW);
9104         if ((r->access & max_el) == 0) {
9105             return;
9106         }
9107     }
9108 
9109     /* Combine cpreg and name into one allocation. */
9110     name_len = strlen(name) + 1;
9111     r2 = g_malloc(sizeof(*r2) + name_len);
9112     *r2 = *r;
9113     r2->name = memcpy(r2 + 1, name, name_len);
9114 
9115     /*
9116      * Update fields to match the instantiation, overwiting wildcards
9117      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9118      */
9119     r2->cp = cp;
9120     r2->crm = crm;
9121     r2->opc1 = opc1;
9122     r2->opc2 = opc2;
9123     r2->state = state;
9124     r2->secure = secstate;
9125     if (opaque) {
9126         r2->opaque = opaque;
9127     }
9128 
9129     if (make_const) {
9130         /* This should not have been a very special register to begin. */
9131         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9132         assert(old_special == 0 || old_special == ARM_CP_NOP);
9133         /*
9134          * Set the special function to CONST, retaining the other flags.
9135          * This is important for e.g. ARM_CP_SVE so that we still
9136          * take the SVE trap if CPTR_EL3.EZ == 0.
9137          */
9138         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9139         /*
9140          * Usually, these registers become RES0, but there are a few
9141          * special cases like VPIDR_EL2 which have a constant non-zero
9142          * value with writes ignored.
9143          */
9144         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9145             r2->resetvalue = 0;
9146         }
9147         /*
9148          * ARM_CP_CONST has precedence, so removing the callbacks and
9149          * offsets are not strictly necessary, but it is potentially
9150          * less confusing to debug later.
9151          */
9152         r2->readfn = NULL;
9153         r2->writefn = NULL;
9154         r2->raw_readfn = NULL;
9155         r2->raw_writefn = NULL;
9156         r2->resetfn = NULL;
9157         r2->fieldoffset = 0;
9158         r2->bank_fieldoffsets[0] = 0;
9159         r2->bank_fieldoffsets[1] = 0;
9160     } else {
9161         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9162 
9163         if (isbanked) {
9164             /*
9165              * Register is banked (using both entries in array).
9166              * Overwriting fieldoffset as the array is only used to define
9167              * banked registers but later only fieldoffset is used.
9168              */
9169             r2->fieldoffset = r->bank_fieldoffsets[ns];
9170         }
9171         if (state == ARM_CP_STATE_AA32) {
9172             if (isbanked) {
9173                 /*
9174                  * If the register is banked then we don't need to migrate or
9175                  * reset the 32-bit instance in certain cases:
9176                  *
9177                  * 1) If the register has both 32-bit and 64-bit instances
9178                  *    then we can count on the 64-bit instance taking care
9179                  *    of the non-secure bank.
9180                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9181                  *    version taking care of the secure bank.  This requires
9182                  *    that separate 32 and 64-bit definitions are provided.
9183                  */
9184                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9185                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9186                     r2->type |= ARM_CP_ALIAS;
9187                 }
9188             } else if ((secstate != r->secure) && !ns) {
9189                 /*
9190                  * The register is not banked so we only want to allow
9191                  * migration of the non-secure instance.
9192                  */
9193                 r2->type |= ARM_CP_ALIAS;
9194             }
9195 
9196             if (HOST_BIG_ENDIAN &&
9197                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9198                 r2->fieldoffset += sizeof(uint32_t);
9199             }
9200         }
9201     }
9202 
9203     /*
9204      * By convention, for wildcarded registers only the first
9205      * entry is used for migration; the others are marked as
9206      * ALIAS so we don't try to transfer the register
9207      * multiple times. Special registers (ie NOP/WFI) are
9208      * never migratable and not even raw-accessible.
9209      */
9210     if (r2->type & ARM_CP_SPECIAL_MASK) {
9211         r2->type |= ARM_CP_NO_RAW;
9212     }
9213     if (((r->crm == CP_ANY) && crm != 0) ||
9214         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9215         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9216         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9217     }
9218 
9219     /*
9220      * Check that raw accesses are either forbidden or handled. Note that
9221      * we can't assert this earlier because the setup of fieldoffset for
9222      * banked registers has to be done first.
9223      */
9224     if (!(r2->type & ARM_CP_NO_RAW)) {
9225         assert(!raw_accessors_invalid(r2));
9226     }
9227 
9228     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9229 }
9230 
9231 
9232 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9233                                        const ARMCPRegInfo *r, void *opaque)
9234 {
9235     /*
9236      * Define implementations of coprocessor registers.
9237      * We store these in a hashtable because typically
9238      * there are less than 150 registers in a space which
9239      * is 16*16*16*8*8 = 262144 in size.
9240      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9241      * If a register is defined twice then the second definition is
9242      * used, so this can be used to define some generic registers and
9243      * then override them with implementation specific variations.
9244      * At least one of the original and the second definition should
9245      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9246      * against accidental use.
9247      *
9248      * The state field defines whether the register is to be
9249      * visible in the AArch32 or AArch64 execution state. If the
9250      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9251      * reginfo structure for the AArch32 view, which sees the lower
9252      * 32 bits of the 64 bit register.
9253      *
9254      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9255      * be wildcarded. AArch64 registers are always considered to be 64
9256      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9257      * the register, if any.
9258      */
9259     int crm, opc1, opc2;
9260     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9261     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9262     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9263     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9264     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9265     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9266     CPState state;
9267 
9268     /* 64 bit registers have only CRm and Opc1 fields */
9269     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9270     /* op0 only exists in the AArch64 encodings */
9271     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9272     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9273     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9274     /*
9275      * This API is only for Arm's system coprocessors (14 and 15) or
9276      * (M-profile or v7A-and-earlier only) for implementation defined
9277      * coprocessors in the range 0..7.  Our decode assumes this, since
9278      * 8..13 can be used for other insns including VFP and Neon. See
9279      * valid_cp() in translate.c.  Assert here that we haven't tried
9280      * to use an invalid coprocessor number.
9281      */
9282     switch (r->state) {
9283     case ARM_CP_STATE_BOTH:
9284         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9285         if (r->cp == 0) {
9286             break;
9287         }
9288         /* fall through */
9289     case ARM_CP_STATE_AA32:
9290         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9291             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9292             assert(r->cp >= 14 && r->cp <= 15);
9293         } else {
9294             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9295         }
9296         break;
9297     case ARM_CP_STATE_AA64:
9298         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9299         break;
9300     default:
9301         g_assert_not_reached();
9302     }
9303     /*
9304      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9305      * encodes a minimum access level for the register. We roll this
9306      * runtime check into our general permission check code, so check
9307      * here that the reginfo's specified permissions are strict enough
9308      * to encompass the generic architectural permission check.
9309      */
9310     if (r->state != ARM_CP_STATE_AA32) {
9311         CPAccessRights mask;
9312         switch (r->opc1) {
9313         case 0:
9314             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9315             mask = PL0U_R | PL1_RW;
9316             break;
9317         case 1: case 2:
9318             /* min_EL EL1 */
9319             mask = PL1_RW;
9320             break;
9321         case 3:
9322             /* min_EL EL0 */
9323             mask = PL0_RW;
9324             break;
9325         case 4:
9326         case 5:
9327             /* min_EL EL2 */
9328             mask = PL2_RW;
9329             break;
9330         case 6:
9331             /* min_EL EL3 */
9332             mask = PL3_RW;
9333             break;
9334         case 7:
9335             /* min_EL EL1, secure mode only (we don't check the latter) */
9336             mask = PL1_RW;
9337             break;
9338         default:
9339             /* broken reginfo with out-of-range opc1 */
9340             g_assert_not_reached();
9341         }
9342         /* assert our permissions are not too lax (stricter is fine) */
9343         assert((r->access & ~mask) == 0);
9344     }
9345 
9346     /*
9347      * Check that the register definition has enough info to handle
9348      * reads and writes if they are permitted.
9349      */
9350     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9351         if (r->access & PL3_R) {
9352             assert((r->fieldoffset ||
9353                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9354                    r->readfn);
9355         }
9356         if (r->access & PL3_W) {
9357             assert((r->fieldoffset ||
9358                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9359                    r->writefn);
9360         }
9361     }
9362 
9363     for (crm = crmmin; crm <= crmmax; crm++) {
9364         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9365             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9366                 for (state = ARM_CP_STATE_AA32;
9367                      state <= ARM_CP_STATE_AA64; state++) {
9368                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9369                         continue;
9370                     }
9371                     if (state == ARM_CP_STATE_AA32) {
9372                         /*
9373                          * Under AArch32 CP registers can be common
9374                          * (same for secure and non-secure world) or banked.
9375                          */
9376                         char *name;
9377 
9378                         switch (r->secure) {
9379                         case ARM_CP_SECSTATE_S:
9380                         case ARM_CP_SECSTATE_NS:
9381                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9382                                                    r->secure, crm, opc1, opc2,
9383                                                    r->name);
9384                             break;
9385                         case ARM_CP_SECSTATE_BOTH:
9386                             name = g_strdup_printf("%s_S", r->name);
9387                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9388                                                    ARM_CP_SECSTATE_S,
9389                                                    crm, opc1, opc2, name);
9390                             g_free(name);
9391                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9392                                                    ARM_CP_SECSTATE_NS,
9393                                                    crm, opc1, opc2, r->name);
9394                             break;
9395                         default:
9396                             g_assert_not_reached();
9397                         }
9398                     } else {
9399                         /*
9400                          * AArch64 registers get mapped to non-secure instance
9401                          * of AArch32
9402                          */
9403                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9404                                                ARM_CP_SECSTATE_NS,
9405                                                crm, opc1, opc2, r->name);
9406                     }
9407                 }
9408             }
9409         }
9410     }
9411 }
9412 
9413 /* Define a whole list of registers */
9414 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9415                                         void *opaque, size_t len)
9416 {
9417     size_t i;
9418     for (i = 0; i < len; ++i) {
9419         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9420     }
9421 }
9422 
9423 /*
9424  * Modify ARMCPRegInfo for access from userspace.
9425  *
9426  * This is a data driven modification directed by
9427  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9428  * user-space cannot alter any values and dynamic values pertaining to
9429  * execution state are hidden from user space view anyway.
9430  */
9431 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9432                                  const ARMCPRegUserSpaceInfo *mods,
9433                                  size_t mods_len)
9434 {
9435     for (size_t mi = 0; mi < mods_len; ++mi) {
9436         const ARMCPRegUserSpaceInfo *m = mods + mi;
9437         GPatternSpec *pat = NULL;
9438 
9439         if (m->is_glob) {
9440             pat = g_pattern_spec_new(m->name);
9441         }
9442         for (size_t ri = 0; ri < regs_len; ++ri) {
9443             ARMCPRegInfo *r = regs + ri;
9444 
9445             if (pat && g_pattern_match_string(pat, r->name)) {
9446                 r->type = ARM_CP_CONST;
9447                 r->access = PL0U_R;
9448                 r->resetvalue = 0;
9449                 /* continue */
9450             } else if (strcmp(r->name, m->name) == 0) {
9451                 r->type = ARM_CP_CONST;
9452                 r->access = PL0U_R;
9453                 r->resetvalue &= m->exported_bits;
9454                 r->resetvalue |= m->fixed_bits;
9455                 break;
9456             }
9457         }
9458         if (pat) {
9459             g_pattern_spec_free(pat);
9460         }
9461     }
9462 }
9463 
9464 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9465 {
9466     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9467 }
9468 
9469 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9470                          uint64_t value)
9471 {
9472     /* Helper coprocessor write function for write-ignore registers */
9473 }
9474 
9475 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9476 {
9477     /* Helper coprocessor write function for read-as-zero registers */
9478     return 0;
9479 }
9480 
9481 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9482 {
9483     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9484 }
9485 
9486 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9487 {
9488     /*
9489      * Return true if it is not valid for us to switch to
9490      * this CPU mode (ie all the UNPREDICTABLE cases in
9491      * the ARM ARM CPSRWriteByInstr pseudocode).
9492      */
9493 
9494     /* Changes to or from Hyp via MSR and CPS are illegal. */
9495     if (write_type == CPSRWriteByInstr &&
9496         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9497          mode == ARM_CPU_MODE_HYP)) {
9498         return 1;
9499     }
9500 
9501     switch (mode) {
9502     case ARM_CPU_MODE_USR:
9503         return 0;
9504     case ARM_CPU_MODE_SYS:
9505     case ARM_CPU_MODE_SVC:
9506     case ARM_CPU_MODE_ABT:
9507     case ARM_CPU_MODE_UND:
9508     case ARM_CPU_MODE_IRQ:
9509     case ARM_CPU_MODE_FIQ:
9510         /*
9511          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9512          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9513          */
9514         /*
9515          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9516          * and CPS are treated as illegal mode changes.
9517          */
9518         if (write_type == CPSRWriteByInstr &&
9519             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9520             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9521             return 1;
9522         }
9523         return 0;
9524     case ARM_CPU_MODE_HYP:
9525         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9526     case ARM_CPU_MODE_MON:
9527         return arm_current_el(env) < 3;
9528     default:
9529         return 1;
9530     }
9531 }
9532 
9533 uint32_t cpsr_read(CPUARMState *env)
9534 {
9535     int ZF;
9536     ZF = (env->ZF == 0);
9537     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9538         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9539         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9540         | ((env->condexec_bits & 0xfc) << 8)
9541         | (env->GE << 16) | (env->daif & CPSR_AIF);
9542 }
9543 
9544 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9545                 CPSRWriteType write_type)
9546 {
9547     uint32_t changed_daif;
9548     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9549         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9550 
9551     if (mask & CPSR_NZCV) {
9552         env->ZF = (~val) & CPSR_Z;
9553         env->NF = val;
9554         env->CF = (val >> 29) & 1;
9555         env->VF = (val << 3) & 0x80000000;
9556     }
9557     if (mask & CPSR_Q) {
9558         env->QF = ((val & CPSR_Q) != 0);
9559     }
9560     if (mask & CPSR_T) {
9561         env->thumb = ((val & CPSR_T) != 0);
9562     }
9563     if (mask & CPSR_IT_0_1) {
9564         env->condexec_bits &= ~3;
9565         env->condexec_bits |= (val >> 25) & 3;
9566     }
9567     if (mask & CPSR_IT_2_7) {
9568         env->condexec_bits &= 3;
9569         env->condexec_bits |= (val >> 8) & 0xfc;
9570     }
9571     if (mask & CPSR_GE) {
9572         env->GE = (val >> 16) & 0xf;
9573     }
9574 
9575     /*
9576      * In a V7 implementation that includes the security extensions but does
9577      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9578      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9579      * bits respectively.
9580      *
9581      * In a V8 implementation, it is permitted for privileged software to
9582      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9583      */
9584     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9585         arm_feature(env, ARM_FEATURE_EL3) &&
9586         !arm_feature(env, ARM_FEATURE_EL2) &&
9587         !arm_is_secure(env)) {
9588 
9589         changed_daif = (env->daif ^ val) & mask;
9590 
9591         if (changed_daif & CPSR_A) {
9592             /*
9593              * Check to see if we are allowed to change the masking of async
9594              * abort exceptions from a non-secure state.
9595              */
9596             if (!(env->cp15.scr_el3 & SCR_AW)) {
9597                 qemu_log_mask(LOG_GUEST_ERROR,
9598                               "Ignoring attempt to switch CPSR_A flag from "
9599                               "non-secure world with SCR.AW bit clear\n");
9600                 mask &= ~CPSR_A;
9601             }
9602         }
9603 
9604         if (changed_daif & CPSR_F) {
9605             /*
9606              * Check to see if we are allowed to change the masking of FIQ
9607              * exceptions from a non-secure state.
9608              */
9609             if (!(env->cp15.scr_el3 & SCR_FW)) {
9610                 qemu_log_mask(LOG_GUEST_ERROR,
9611                               "Ignoring attempt to switch CPSR_F flag from "
9612                               "non-secure world with SCR.FW bit clear\n");
9613                 mask &= ~CPSR_F;
9614             }
9615 
9616             /*
9617              * Check whether non-maskable FIQ (NMFI) support is enabled.
9618              * If this bit is set software is not allowed to mask
9619              * FIQs, but is allowed to set CPSR_F to 0.
9620              */
9621             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9622                 (val & CPSR_F)) {
9623                 qemu_log_mask(LOG_GUEST_ERROR,
9624                               "Ignoring attempt to enable CPSR_F flag "
9625                               "(non-maskable FIQ [NMFI] support enabled)\n");
9626                 mask &= ~CPSR_F;
9627             }
9628         }
9629     }
9630 
9631     env->daif &= ~(CPSR_AIF & mask);
9632     env->daif |= val & CPSR_AIF & mask;
9633 
9634     if (write_type != CPSRWriteRaw &&
9635         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9636         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9637             /*
9638              * Note that we can only get here in USR mode if this is a
9639              * gdb stub write; for this case we follow the architectural
9640              * behaviour for guest writes in USR mode of ignoring an attempt
9641              * to switch mode. (Those are caught by translate.c for writes
9642              * triggered by guest instructions.)
9643              */
9644             mask &= ~CPSR_M;
9645         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9646             /*
9647              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9648              * v7, and has defined behaviour in v8:
9649              *  + leave CPSR.M untouched
9650              *  + allow changes to the other CPSR fields
9651              *  + set PSTATE.IL
9652              * For user changes via the GDB stub, we don't set PSTATE.IL,
9653              * as this would be unnecessarily harsh for a user error.
9654              */
9655             mask &= ~CPSR_M;
9656             if (write_type != CPSRWriteByGDBStub &&
9657                 arm_feature(env, ARM_FEATURE_V8)) {
9658                 mask |= CPSR_IL;
9659                 val |= CPSR_IL;
9660             }
9661             qemu_log_mask(LOG_GUEST_ERROR,
9662                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9663                           aarch32_mode_name(env->uncached_cpsr),
9664                           aarch32_mode_name(val));
9665         } else {
9666             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9667                           write_type == CPSRWriteExceptionReturn ?
9668                           "Exception return from AArch32" :
9669                           "AArch32 mode switch from",
9670                           aarch32_mode_name(env->uncached_cpsr),
9671                           aarch32_mode_name(val), env->regs[15]);
9672             switch_mode(env, val & CPSR_M);
9673         }
9674     }
9675     mask &= ~CACHED_CPSR_BITS;
9676     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9677     if (rebuild_hflags) {
9678         arm_rebuild_hflags(env);
9679     }
9680 }
9681 
9682 /* Sign/zero extend */
9683 uint32_t HELPER(sxtb16)(uint32_t x)
9684 {
9685     uint32_t res;
9686     res = (uint16_t)(int8_t)x;
9687     res |= (uint32_t)(int8_t)(x >> 16) << 16;
9688     return res;
9689 }
9690 
9691 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9692 {
9693     /*
9694      * Take a division-by-zero exception if necessary; otherwise return
9695      * to get the usual non-trapping division behaviour (result of 0)
9696      */
9697     if (arm_feature(env, ARM_FEATURE_M)
9698         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9699         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9700     }
9701 }
9702 
9703 uint32_t HELPER(uxtb16)(uint32_t x)
9704 {
9705     uint32_t res;
9706     res = (uint16_t)(uint8_t)x;
9707     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9708     return res;
9709 }
9710 
9711 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9712 {
9713     if (den == 0) {
9714         handle_possible_div0_trap(env, GETPC());
9715         return 0;
9716     }
9717     if (num == INT_MIN && den == -1) {
9718         return INT_MIN;
9719     }
9720     return num / den;
9721 }
9722 
9723 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9724 {
9725     if (den == 0) {
9726         handle_possible_div0_trap(env, GETPC());
9727         return 0;
9728     }
9729     return num / den;
9730 }
9731 
9732 uint32_t HELPER(rbit)(uint32_t x)
9733 {
9734     return revbit32(x);
9735 }
9736 
9737 #ifdef CONFIG_USER_ONLY
9738 
9739 static void switch_mode(CPUARMState *env, int mode)
9740 {
9741     ARMCPU *cpu = env_archcpu(env);
9742 
9743     if (mode != ARM_CPU_MODE_USR) {
9744         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9745     }
9746 }
9747 
9748 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9749                                  uint32_t cur_el, bool secure)
9750 {
9751     return 1;
9752 }
9753 
9754 void aarch64_sync_64_to_32(CPUARMState *env)
9755 {
9756     g_assert_not_reached();
9757 }
9758 
9759 #else
9760 
9761 static void switch_mode(CPUARMState *env, int mode)
9762 {
9763     int old_mode;
9764     int i;
9765 
9766     old_mode = env->uncached_cpsr & CPSR_M;
9767     if (mode == old_mode) {
9768         return;
9769     }
9770 
9771     if (old_mode == ARM_CPU_MODE_FIQ) {
9772         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9773         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9774     } else if (mode == ARM_CPU_MODE_FIQ) {
9775         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9776         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9777     }
9778 
9779     i = bank_number(old_mode);
9780     env->banked_r13[i] = env->regs[13];
9781     env->banked_spsr[i] = env->spsr;
9782 
9783     i = bank_number(mode);
9784     env->regs[13] = env->banked_r13[i];
9785     env->spsr = env->banked_spsr[i];
9786 
9787     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9788     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9789 }
9790 
9791 /*
9792  * Physical Interrupt Target EL Lookup Table
9793  *
9794  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9795  *
9796  * The below multi-dimensional table is used for looking up the target
9797  * exception level given numerous condition criteria.  Specifically, the
9798  * target EL is based on SCR and HCR routing controls as well as the
9799  * currently executing EL and secure state.
9800  *
9801  *    Dimensions:
9802  *    target_el_table[2][2][2][2][2][4]
9803  *                    |  |  |  |  |  +--- Current EL
9804  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9805  *                    |  |  |  +--------- HCR mask override
9806  *                    |  |  +------------ SCR exec state control
9807  *                    |  +--------------- SCR mask override
9808  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9809  *
9810  *    The table values are as such:
9811  *    0-3 = EL0-EL3
9812  *     -1 = Cannot occur
9813  *
9814  * The ARM ARM target EL table includes entries indicating that an "exception
9815  * is not taken".  The two cases where this is applicable are:
9816  *    1) An exception is taken from EL3 but the SCR does not have the exception
9817  *    routed to EL3.
9818  *    2) An exception is taken from EL2 but the HCR does not have the exception
9819  *    routed to EL2.
9820  * In these two cases, the below table contain a target of EL1.  This value is
9821  * returned as it is expected that the consumer of the table data will check
9822  * for "target EL >= current EL" to ensure the exception is not taken.
9823  *
9824  *            SCR     HCR
9825  *         64  EA     AMO                 From
9826  *        BIT IRQ     IMO      Non-secure         Secure
9827  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9828  */
9829 static const int8_t target_el_table[2][2][2][2][2][4] = {
9830     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9831        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9832       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9833        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9834      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9835        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9836       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9837        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9838     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9839        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9840       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9841        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9842      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9843        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9844       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9845        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9846 };
9847 
9848 /*
9849  * Determine the target EL for physical exceptions
9850  */
9851 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9852                                  uint32_t cur_el, bool secure)
9853 {
9854     CPUARMState *env = cs->env_ptr;
9855     bool rw;
9856     bool scr;
9857     bool hcr;
9858     int target_el;
9859     /* Is the highest EL AArch64? */
9860     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9861     uint64_t hcr_el2;
9862 
9863     if (arm_feature(env, ARM_FEATURE_EL3)) {
9864         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9865     } else {
9866         /*
9867          * Either EL2 is the highest EL (and so the EL2 register width
9868          * is given by is64); or there is no EL2 or EL3, in which case
9869          * the value of 'rw' does not affect the table lookup anyway.
9870          */
9871         rw = is64;
9872     }
9873 
9874     hcr_el2 = arm_hcr_el2_eff(env);
9875     switch (excp_idx) {
9876     case EXCP_IRQ:
9877         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9878         hcr = hcr_el2 & HCR_IMO;
9879         break;
9880     case EXCP_FIQ:
9881         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9882         hcr = hcr_el2 & HCR_FMO;
9883         break;
9884     default:
9885         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9886         hcr = hcr_el2 & HCR_AMO;
9887         break;
9888     };
9889 
9890     /*
9891      * For these purposes, TGE and AMO/IMO/FMO both force the
9892      * interrupt to EL2.  Fold TGE into the bit extracted above.
9893      */
9894     hcr |= (hcr_el2 & HCR_TGE) != 0;
9895 
9896     /* Perform a table-lookup for the target EL given the current state */
9897     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9898 
9899     assert(target_el > 0);
9900 
9901     return target_el;
9902 }
9903 
9904 void arm_log_exception(CPUState *cs)
9905 {
9906     int idx = cs->exception_index;
9907 
9908     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9909         const char *exc = NULL;
9910         static const char * const excnames[] = {
9911             [EXCP_UDEF] = "Undefined Instruction",
9912             [EXCP_SWI] = "SVC",
9913             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9914             [EXCP_DATA_ABORT] = "Data Abort",
9915             [EXCP_IRQ] = "IRQ",
9916             [EXCP_FIQ] = "FIQ",
9917             [EXCP_BKPT] = "Breakpoint",
9918             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9919             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9920             [EXCP_HVC] = "Hypervisor Call",
9921             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9922             [EXCP_SMC] = "Secure Monitor Call",
9923             [EXCP_VIRQ] = "Virtual IRQ",
9924             [EXCP_VFIQ] = "Virtual FIQ",
9925             [EXCP_SEMIHOST] = "Semihosting call",
9926             [EXCP_NOCP] = "v7M NOCP UsageFault",
9927             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9928             [EXCP_STKOF] = "v8M STKOF UsageFault",
9929             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9930             [EXCP_LSERR] = "v8M LSERR UsageFault",
9931             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9932             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9933             [EXCP_VSERR] = "Virtual SERR",
9934         };
9935 
9936         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9937             exc = excnames[idx];
9938         }
9939         if (!exc) {
9940             exc = "unknown";
9941         }
9942         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9943                       idx, exc, cs->cpu_index);
9944     }
9945 }
9946 
9947 /*
9948  * Function used to synchronize QEMU's AArch64 register set with AArch32
9949  * register set.  This is necessary when switching between AArch32 and AArch64
9950  * execution state.
9951  */
9952 void aarch64_sync_32_to_64(CPUARMState *env)
9953 {
9954     int i;
9955     uint32_t mode = env->uncached_cpsr & CPSR_M;
9956 
9957     /* We can blanket copy R[0:7] to X[0:7] */
9958     for (i = 0; i < 8; i++) {
9959         env->xregs[i] = env->regs[i];
9960     }
9961 
9962     /*
9963      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9964      * Otherwise, they come from the banked user regs.
9965      */
9966     if (mode == ARM_CPU_MODE_FIQ) {
9967         for (i = 8; i < 13; i++) {
9968             env->xregs[i] = env->usr_regs[i - 8];
9969         }
9970     } else {
9971         for (i = 8; i < 13; i++) {
9972             env->xregs[i] = env->regs[i];
9973         }
9974     }
9975 
9976     /*
9977      * Registers x13-x23 are the various mode SP and FP registers. Registers
9978      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9979      * from the mode banked register.
9980      */
9981     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9982         env->xregs[13] = env->regs[13];
9983         env->xregs[14] = env->regs[14];
9984     } else {
9985         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9986         /* HYP is an exception in that it is copied from r14 */
9987         if (mode == ARM_CPU_MODE_HYP) {
9988             env->xregs[14] = env->regs[14];
9989         } else {
9990             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9991         }
9992     }
9993 
9994     if (mode == ARM_CPU_MODE_HYP) {
9995         env->xregs[15] = env->regs[13];
9996     } else {
9997         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9998     }
9999 
10000     if (mode == ARM_CPU_MODE_IRQ) {
10001         env->xregs[16] = env->regs[14];
10002         env->xregs[17] = env->regs[13];
10003     } else {
10004         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10005         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10006     }
10007 
10008     if (mode == ARM_CPU_MODE_SVC) {
10009         env->xregs[18] = env->regs[14];
10010         env->xregs[19] = env->regs[13];
10011     } else {
10012         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10013         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10014     }
10015 
10016     if (mode == ARM_CPU_MODE_ABT) {
10017         env->xregs[20] = env->regs[14];
10018         env->xregs[21] = env->regs[13];
10019     } else {
10020         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10021         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10022     }
10023 
10024     if (mode == ARM_CPU_MODE_UND) {
10025         env->xregs[22] = env->regs[14];
10026         env->xregs[23] = env->regs[13];
10027     } else {
10028         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10029         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10030     }
10031 
10032     /*
10033      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10034      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10035      * FIQ bank for r8-r14.
10036      */
10037     if (mode == ARM_CPU_MODE_FIQ) {
10038         for (i = 24; i < 31; i++) {
10039             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10040         }
10041     } else {
10042         for (i = 24; i < 29; i++) {
10043             env->xregs[i] = env->fiq_regs[i - 24];
10044         }
10045         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10046         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10047     }
10048 
10049     env->pc = env->regs[15];
10050 }
10051 
10052 /*
10053  * Function used to synchronize QEMU's AArch32 register set with AArch64
10054  * register set.  This is necessary when switching between AArch32 and AArch64
10055  * execution state.
10056  */
10057 void aarch64_sync_64_to_32(CPUARMState *env)
10058 {
10059     int i;
10060     uint32_t mode = env->uncached_cpsr & CPSR_M;
10061 
10062     /* We can blanket copy X[0:7] to R[0:7] */
10063     for (i = 0; i < 8; i++) {
10064         env->regs[i] = env->xregs[i];
10065     }
10066 
10067     /*
10068      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10069      * Otherwise, we copy x8-x12 into the banked user regs.
10070      */
10071     if (mode == ARM_CPU_MODE_FIQ) {
10072         for (i = 8; i < 13; i++) {
10073             env->usr_regs[i - 8] = env->xregs[i];
10074         }
10075     } else {
10076         for (i = 8; i < 13; i++) {
10077             env->regs[i] = env->xregs[i];
10078         }
10079     }
10080 
10081     /*
10082      * Registers r13 & r14 depend on the current mode.
10083      * If we are in a given mode, we copy the corresponding x registers to r13
10084      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10085      * for the mode.
10086      */
10087     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10088         env->regs[13] = env->xregs[13];
10089         env->regs[14] = env->xregs[14];
10090     } else {
10091         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10092 
10093         /*
10094          * HYP is an exception in that it does not have its own banked r14 but
10095          * shares the USR r14
10096          */
10097         if (mode == ARM_CPU_MODE_HYP) {
10098             env->regs[14] = env->xregs[14];
10099         } else {
10100             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10101         }
10102     }
10103 
10104     if (mode == ARM_CPU_MODE_HYP) {
10105         env->regs[13] = env->xregs[15];
10106     } else {
10107         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10108     }
10109 
10110     if (mode == ARM_CPU_MODE_IRQ) {
10111         env->regs[14] = env->xregs[16];
10112         env->regs[13] = env->xregs[17];
10113     } else {
10114         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10115         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10116     }
10117 
10118     if (mode == ARM_CPU_MODE_SVC) {
10119         env->regs[14] = env->xregs[18];
10120         env->regs[13] = env->xregs[19];
10121     } else {
10122         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10123         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10124     }
10125 
10126     if (mode == ARM_CPU_MODE_ABT) {
10127         env->regs[14] = env->xregs[20];
10128         env->regs[13] = env->xregs[21];
10129     } else {
10130         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10131         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10132     }
10133 
10134     if (mode == ARM_CPU_MODE_UND) {
10135         env->regs[14] = env->xregs[22];
10136         env->regs[13] = env->xregs[23];
10137     } else {
10138         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10139         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10140     }
10141 
10142     /*
10143      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10144      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10145      * FIQ bank for r8-r14.
10146      */
10147     if (mode == ARM_CPU_MODE_FIQ) {
10148         for (i = 24; i < 31; i++) {
10149             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10150         }
10151     } else {
10152         for (i = 24; i < 29; i++) {
10153             env->fiq_regs[i - 24] = env->xregs[i];
10154         }
10155         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10156         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10157     }
10158 
10159     env->regs[15] = env->pc;
10160 }
10161 
10162 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10163                                    uint32_t mask, uint32_t offset,
10164                                    uint32_t newpc)
10165 {
10166     int new_el;
10167 
10168     /* Change the CPU state so as to actually take the exception. */
10169     switch_mode(env, new_mode);
10170 
10171     /*
10172      * For exceptions taken to AArch32 we must clear the SS bit in both
10173      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10174      */
10175     env->pstate &= ~PSTATE_SS;
10176     env->spsr = cpsr_read(env);
10177     /* Clear IT bits.  */
10178     env->condexec_bits = 0;
10179     /* Switch to the new mode, and to the correct instruction set.  */
10180     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10181 
10182     /* This must be after mode switching. */
10183     new_el = arm_current_el(env);
10184 
10185     /* Set new mode endianness */
10186     env->uncached_cpsr &= ~CPSR_E;
10187     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10188         env->uncached_cpsr |= CPSR_E;
10189     }
10190     /* J and IL must always be cleared for exception entry */
10191     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10192     env->daif |= mask;
10193 
10194     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10195         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10196             env->uncached_cpsr |= CPSR_SSBS;
10197         } else {
10198             env->uncached_cpsr &= ~CPSR_SSBS;
10199         }
10200     }
10201 
10202     if (new_mode == ARM_CPU_MODE_HYP) {
10203         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10204         env->elr_el[2] = env->regs[15];
10205     } else {
10206         /* CPSR.PAN is normally preserved preserved unless...  */
10207         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10208             switch (new_el) {
10209             case 3:
10210                 if (!arm_is_secure_below_el3(env)) {
10211                     /* ... the target is EL3, from non-secure state.  */
10212                     env->uncached_cpsr &= ~CPSR_PAN;
10213                     break;
10214                 }
10215                 /* ... the target is EL3, from secure state ... */
10216                 /* fall through */
10217             case 1:
10218                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10219                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10220                     env->uncached_cpsr |= CPSR_PAN;
10221                 }
10222                 break;
10223             }
10224         }
10225         /*
10226          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10227          * and we should just guard the thumb mode on V4
10228          */
10229         if (arm_feature(env, ARM_FEATURE_V4T)) {
10230             env->thumb =
10231                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10232         }
10233         env->regs[14] = env->regs[15] + offset;
10234     }
10235     env->regs[15] = newpc;
10236     arm_rebuild_hflags(env);
10237 }
10238 
10239 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10240 {
10241     /*
10242      * Handle exception entry to Hyp mode; this is sufficiently
10243      * different to entry to other AArch32 modes that we handle it
10244      * separately here.
10245      *
10246      * The vector table entry used is always the 0x14 Hyp mode entry point,
10247      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10248      * The offset applied to the preferred return address is always zero
10249      * (see DDI0487C.a section G1.12.3).
10250      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10251      */
10252     uint32_t addr, mask;
10253     ARMCPU *cpu = ARM_CPU(cs);
10254     CPUARMState *env = &cpu->env;
10255 
10256     switch (cs->exception_index) {
10257     case EXCP_UDEF:
10258         addr = 0x04;
10259         break;
10260     case EXCP_SWI:
10261         addr = 0x08;
10262         break;
10263     case EXCP_BKPT:
10264         /* Fall through to prefetch abort.  */
10265     case EXCP_PREFETCH_ABORT:
10266         env->cp15.ifar_s = env->exception.vaddress;
10267         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10268                       (uint32_t)env->exception.vaddress);
10269         addr = 0x0c;
10270         break;
10271     case EXCP_DATA_ABORT:
10272         env->cp15.dfar_s = env->exception.vaddress;
10273         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10274                       (uint32_t)env->exception.vaddress);
10275         addr = 0x10;
10276         break;
10277     case EXCP_IRQ:
10278         addr = 0x18;
10279         break;
10280     case EXCP_FIQ:
10281         addr = 0x1c;
10282         break;
10283     case EXCP_HVC:
10284         addr = 0x08;
10285         break;
10286     case EXCP_HYP_TRAP:
10287         addr = 0x14;
10288         break;
10289     default:
10290         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10291     }
10292 
10293     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10294         if (!arm_feature(env, ARM_FEATURE_V8)) {
10295             /*
10296              * QEMU syndrome values are v8-style. v7 has the IL bit
10297              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10298              * If this is a v7 CPU, squash the IL bit in those cases.
10299              */
10300             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10301                 (cs->exception_index == EXCP_DATA_ABORT &&
10302                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10303                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10304                 env->exception.syndrome &= ~ARM_EL_IL;
10305             }
10306         }
10307         env->cp15.esr_el[2] = env->exception.syndrome;
10308     }
10309 
10310     if (arm_current_el(env) != 2 && addr < 0x14) {
10311         addr = 0x14;
10312     }
10313 
10314     mask = 0;
10315     if (!(env->cp15.scr_el3 & SCR_EA)) {
10316         mask |= CPSR_A;
10317     }
10318     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10319         mask |= CPSR_I;
10320     }
10321     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10322         mask |= CPSR_F;
10323     }
10324 
10325     addr += env->cp15.hvbar;
10326 
10327     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10328 }
10329 
10330 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10331 {
10332     ARMCPU *cpu = ARM_CPU(cs);
10333     CPUARMState *env = &cpu->env;
10334     uint32_t addr;
10335     uint32_t mask;
10336     int new_mode;
10337     uint32_t offset;
10338     uint32_t moe;
10339 
10340     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10341     switch (syn_get_ec(env->exception.syndrome)) {
10342     case EC_BREAKPOINT:
10343     case EC_BREAKPOINT_SAME_EL:
10344         moe = 1;
10345         break;
10346     case EC_WATCHPOINT:
10347     case EC_WATCHPOINT_SAME_EL:
10348         moe = 10;
10349         break;
10350     case EC_AA32_BKPT:
10351         moe = 3;
10352         break;
10353     case EC_VECTORCATCH:
10354         moe = 5;
10355         break;
10356     default:
10357         moe = 0;
10358         break;
10359     }
10360 
10361     if (moe) {
10362         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10363     }
10364 
10365     if (env->exception.target_el == 2) {
10366         arm_cpu_do_interrupt_aarch32_hyp(cs);
10367         return;
10368     }
10369 
10370     switch (cs->exception_index) {
10371     case EXCP_UDEF:
10372         new_mode = ARM_CPU_MODE_UND;
10373         addr = 0x04;
10374         mask = CPSR_I;
10375         if (env->thumb) {
10376             offset = 2;
10377         } else {
10378             offset = 4;
10379         }
10380         break;
10381     case EXCP_SWI:
10382         new_mode = ARM_CPU_MODE_SVC;
10383         addr = 0x08;
10384         mask = CPSR_I;
10385         /* The PC already points to the next instruction.  */
10386         offset = 0;
10387         break;
10388     case EXCP_BKPT:
10389         /* Fall through to prefetch abort.  */
10390     case EXCP_PREFETCH_ABORT:
10391         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10392         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10393         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10394                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10395         new_mode = ARM_CPU_MODE_ABT;
10396         addr = 0x0c;
10397         mask = CPSR_A | CPSR_I;
10398         offset = 4;
10399         break;
10400     case EXCP_DATA_ABORT:
10401         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10402         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10403         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10404                       env->exception.fsr,
10405                       (uint32_t)env->exception.vaddress);
10406         new_mode = ARM_CPU_MODE_ABT;
10407         addr = 0x10;
10408         mask = CPSR_A | CPSR_I;
10409         offset = 8;
10410         break;
10411     case EXCP_IRQ:
10412         new_mode = ARM_CPU_MODE_IRQ;
10413         addr = 0x18;
10414         /* Disable IRQ and imprecise data aborts.  */
10415         mask = CPSR_A | CPSR_I;
10416         offset = 4;
10417         if (env->cp15.scr_el3 & SCR_IRQ) {
10418             /* IRQ routed to monitor mode */
10419             new_mode = ARM_CPU_MODE_MON;
10420             mask |= CPSR_F;
10421         }
10422         break;
10423     case EXCP_FIQ:
10424         new_mode = ARM_CPU_MODE_FIQ;
10425         addr = 0x1c;
10426         /* Disable FIQ, IRQ and imprecise data aborts.  */
10427         mask = CPSR_A | CPSR_I | CPSR_F;
10428         if (env->cp15.scr_el3 & SCR_FIQ) {
10429             /* FIQ routed to monitor mode */
10430             new_mode = ARM_CPU_MODE_MON;
10431         }
10432         offset = 4;
10433         break;
10434     case EXCP_VIRQ:
10435         new_mode = ARM_CPU_MODE_IRQ;
10436         addr = 0x18;
10437         /* Disable IRQ and imprecise data aborts.  */
10438         mask = CPSR_A | CPSR_I;
10439         offset = 4;
10440         break;
10441     case EXCP_VFIQ:
10442         new_mode = ARM_CPU_MODE_FIQ;
10443         addr = 0x1c;
10444         /* Disable FIQ, IRQ and imprecise data aborts.  */
10445         mask = CPSR_A | CPSR_I | CPSR_F;
10446         offset = 4;
10447         break;
10448     case EXCP_VSERR:
10449         {
10450             /*
10451              * Note that this is reported as a data abort, but the DFAR
10452              * has an UNKNOWN value.  Construct the SError syndrome from
10453              * AET and ExT fields.
10454              */
10455             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10456 
10457             if (extended_addresses_enabled(env)) {
10458                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10459             } else {
10460                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10461             }
10462             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10463             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10464             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10465                           env->exception.fsr);
10466 
10467             new_mode = ARM_CPU_MODE_ABT;
10468             addr = 0x10;
10469             mask = CPSR_A | CPSR_I;
10470             offset = 8;
10471         }
10472         break;
10473     case EXCP_SMC:
10474         new_mode = ARM_CPU_MODE_MON;
10475         addr = 0x08;
10476         mask = CPSR_A | CPSR_I | CPSR_F;
10477         offset = 0;
10478         break;
10479     default:
10480         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10481         return; /* Never happens.  Keep compiler happy.  */
10482     }
10483 
10484     if (new_mode == ARM_CPU_MODE_MON) {
10485         addr += env->cp15.mvbar;
10486     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10487         /* High vectors. When enabled, base address cannot be remapped. */
10488         addr += 0xffff0000;
10489     } else {
10490         /*
10491          * ARM v7 architectures provide a vector base address register to remap
10492          * the interrupt vector table.
10493          * This register is only followed in non-monitor mode, and is banked.
10494          * Note: only bits 31:5 are valid.
10495          */
10496         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10497     }
10498 
10499     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10500         env->cp15.scr_el3 &= ~SCR_NS;
10501     }
10502 
10503     take_aarch32_exception(env, new_mode, mask, offset, addr);
10504 }
10505 
10506 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10507 {
10508     /*
10509      * Return the register number of the AArch64 view of the AArch32
10510      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10511      * be that of the AArch32 mode the exception came from.
10512      */
10513     int mode = env->uncached_cpsr & CPSR_M;
10514 
10515     switch (aarch32_reg) {
10516     case 0 ... 7:
10517         return aarch32_reg;
10518     case 8 ... 12:
10519         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10520     case 13:
10521         switch (mode) {
10522         case ARM_CPU_MODE_USR:
10523         case ARM_CPU_MODE_SYS:
10524             return 13;
10525         case ARM_CPU_MODE_HYP:
10526             return 15;
10527         case ARM_CPU_MODE_IRQ:
10528             return 17;
10529         case ARM_CPU_MODE_SVC:
10530             return 19;
10531         case ARM_CPU_MODE_ABT:
10532             return 21;
10533         case ARM_CPU_MODE_UND:
10534             return 23;
10535         case ARM_CPU_MODE_FIQ:
10536             return 29;
10537         default:
10538             g_assert_not_reached();
10539         }
10540     case 14:
10541         switch (mode) {
10542         case ARM_CPU_MODE_USR:
10543         case ARM_CPU_MODE_SYS:
10544         case ARM_CPU_MODE_HYP:
10545             return 14;
10546         case ARM_CPU_MODE_IRQ:
10547             return 16;
10548         case ARM_CPU_MODE_SVC:
10549             return 18;
10550         case ARM_CPU_MODE_ABT:
10551             return 20;
10552         case ARM_CPU_MODE_UND:
10553             return 22;
10554         case ARM_CPU_MODE_FIQ:
10555             return 30;
10556         default:
10557             g_assert_not_reached();
10558         }
10559     case 15:
10560         return 31;
10561     default:
10562         g_assert_not_reached();
10563     }
10564 }
10565 
10566 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10567 {
10568     uint32_t ret = cpsr_read(env);
10569 
10570     /* Move DIT to the correct location for SPSR_ELx */
10571     if (ret & CPSR_DIT) {
10572         ret &= ~CPSR_DIT;
10573         ret |= PSTATE_DIT;
10574     }
10575     /* Merge PSTATE.SS into SPSR_ELx */
10576     ret |= env->pstate & PSTATE_SS;
10577 
10578     return ret;
10579 }
10580 
10581 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10582 {
10583     /* Return true if this syndrome value is a synchronous external abort */
10584     switch (syn_get_ec(syndrome)) {
10585     case EC_INSNABORT:
10586     case EC_INSNABORT_SAME_EL:
10587     case EC_DATAABORT:
10588     case EC_DATAABORT_SAME_EL:
10589         /* Look at fault status code for all the synchronous ext abort cases */
10590         switch (syndrome & 0x3f) {
10591         case 0x10:
10592         case 0x13:
10593         case 0x14:
10594         case 0x15:
10595         case 0x16:
10596         case 0x17:
10597             return true;
10598         default:
10599             return false;
10600         }
10601     default:
10602         return false;
10603     }
10604 }
10605 
10606 /* Handle exception entry to a target EL which is using AArch64 */
10607 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10608 {
10609     ARMCPU *cpu = ARM_CPU(cs);
10610     CPUARMState *env = &cpu->env;
10611     unsigned int new_el = env->exception.target_el;
10612     target_ulong addr = env->cp15.vbar_el[new_el];
10613     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10614     unsigned int old_mode;
10615     unsigned int cur_el = arm_current_el(env);
10616     int rt;
10617 
10618     /*
10619      * Note that new_el can never be 0.  If cur_el is 0, then
10620      * el0_a64 is is_a64(), else el0_a64 is ignored.
10621      */
10622     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10623 
10624     if (cur_el < new_el) {
10625         /*
10626          * Entry vector offset depends on whether the implemented EL
10627          * immediately lower than the target level is using AArch32 or AArch64
10628          */
10629         bool is_aa64;
10630         uint64_t hcr;
10631 
10632         switch (new_el) {
10633         case 3:
10634             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10635             break;
10636         case 2:
10637             hcr = arm_hcr_el2_eff(env);
10638             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10639                 is_aa64 = (hcr & HCR_RW) != 0;
10640                 break;
10641             }
10642             /* fall through */
10643         case 1:
10644             is_aa64 = is_a64(env);
10645             break;
10646         default:
10647             g_assert_not_reached();
10648         }
10649 
10650         if (is_aa64) {
10651             addr += 0x400;
10652         } else {
10653             addr += 0x600;
10654         }
10655     } else if (pstate_read(env) & PSTATE_SP) {
10656         addr += 0x200;
10657     }
10658 
10659     switch (cs->exception_index) {
10660     case EXCP_PREFETCH_ABORT:
10661     case EXCP_DATA_ABORT:
10662         /*
10663          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10664          * to be taken to the SError vector entrypoint.
10665          */
10666         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10667             syndrome_is_sync_extabt(env->exception.syndrome)) {
10668             addr += 0x180;
10669         }
10670         env->cp15.far_el[new_el] = env->exception.vaddress;
10671         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10672                       env->cp15.far_el[new_el]);
10673         /* fall through */
10674     case EXCP_BKPT:
10675     case EXCP_UDEF:
10676     case EXCP_SWI:
10677     case EXCP_HVC:
10678     case EXCP_HYP_TRAP:
10679     case EXCP_SMC:
10680         switch (syn_get_ec(env->exception.syndrome)) {
10681         case EC_ADVSIMDFPACCESSTRAP:
10682             /*
10683              * QEMU internal FP/SIMD syndromes from AArch32 include the
10684              * TA and coproc fields which are only exposed if the exception
10685              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10686              * AArch64 format syndrome.
10687              */
10688             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10689             break;
10690         case EC_CP14RTTRAP:
10691         case EC_CP15RTTRAP:
10692         case EC_CP14DTTRAP:
10693             /*
10694              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10695              * the raw register field from the insn; when taking this to
10696              * AArch64 we must convert it to the AArch64 view of the register
10697              * number. Notice that we read a 4-bit AArch32 register number and
10698              * write back a 5-bit AArch64 one.
10699              */
10700             rt = extract32(env->exception.syndrome, 5, 4);
10701             rt = aarch64_regnum(env, rt);
10702             env->exception.syndrome = deposit32(env->exception.syndrome,
10703                                                 5, 5, rt);
10704             break;
10705         case EC_CP15RRTTRAP:
10706         case EC_CP14RRTTRAP:
10707             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10708             rt = extract32(env->exception.syndrome, 5, 4);
10709             rt = aarch64_regnum(env, rt);
10710             env->exception.syndrome = deposit32(env->exception.syndrome,
10711                                                 5, 5, rt);
10712             rt = extract32(env->exception.syndrome, 10, 4);
10713             rt = aarch64_regnum(env, rt);
10714             env->exception.syndrome = deposit32(env->exception.syndrome,
10715                                                 10, 5, rt);
10716             break;
10717         }
10718         env->cp15.esr_el[new_el] = env->exception.syndrome;
10719         break;
10720     case EXCP_IRQ:
10721     case EXCP_VIRQ:
10722         addr += 0x80;
10723         break;
10724     case EXCP_FIQ:
10725     case EXCP_VFIQ:
10726         addr += 0x100;
10727         break;
10728     case EXCP_VSERR:
10729         addr += 0x180;
10730         /* Construct the SError syndrome from IDS and ISS fields. */
10731         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10732         env->cp15.esr_el[new_el] = env->exception.syndrome;
10733         break;
10734     default:
10735         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10736     }
10737 
10738     if (is_a64(env)) {
10739         old_mode = pstate_read(env);
10740         aarch64_save_sp(env, arm_current_el(env));
10741         env->elr_el[new_el] = env->pc;
10742     } else {
10743         old_mode = cpsr_read_for_spsr_elx(env);
10744         env->elr_el[new_el] = env->regs[15];
10745 
10746         aarch64_sync_32_to_64(env);
10747 
10748         env->condexec_bits = 0;
10749     }
10750     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10751 
10752     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10753                   env->elr_el[new_el]);
10754 
10755     if (cpu_isar_feature(aa64_pan, cpu)) {
10756         /* The value of PSTATE.PAN is normally preserved, except when ... */
10757         new_mode |= old_mode & PSTATE_PAN;
10758         switch (new_el) {
10759         case 2:
10760             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10761             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10762                 != (HCR_E2H | HCR_TGE)) {
10763                 break;
10764             }
10765             /* fall through */
10766         case 1:
10767             /* ... the target is EL1 ... */
10768             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10769             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10770                 new_mode |= PSTATE_PAN;
10771             }
10772             break;
10773         }
10774     }
10775     if (cpu_isar_feature(aa64_mte, cpu)) {
10776         new_mode |= PSTATE_TCO;
10777     }
10778 
10779     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10780         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10781             new_mode |= PSTATE_SSBS;
10782         } else {
10783             new_mode &= ~PSTATE_SSBS;
10784         }
10785     }
10786 
10787     pstate_write(env, PSTATE_DAIF | new_mode);
10788     env->aarch64 = true;
10789     aarch64_restore_sp(env, new_el);
10790     helper_rebuild_hflags_a64(env, new_el);
10791 
10792     env->pc = addr;
10793 
10794     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10795                   new_el, env->pc, pstate_read(env));
10796 }
10797 
10798 /*
10799  * Do semihosting call and set the appropriate return value. All the
10800  * permission and validity checks have been done at translate time.
10801  *
10802  * We only see semihosting exceptions in TCG only as they are not
10803  * trapped to the hypervisor in KVM.
10804  */
10805 #ifdef CONFIG_TCG
10806 static void handle_semihosting(CPUState *cs)
10807 {
10808     ARMCPU *cpu = ARM_CPU(cs);
10809     CPUARMState *env = &cpu->env;
10810 
10811     if (is_a64(env)) {
10812         qemu_log_mask(CPU_LOG_INT,
10813                       "...handling as semihosting call 0x%" PRIx64 "\n",
10814                       env->xregs[0]);
10815         do_common_semihosting(cs);
10816         env->pc += 4;
10817     } else {
10818         qemu_log_mask(CPU_LOG_INT,
10819                       "...handling as semihosting call 0x%x\n",
10820                       env->regs[0]);
10821         do_common_semihosting(cs);
10822         env->regs[15] += env->thumb ? 2 : 4;
10823     }
10824 }
10825 #endif
10826 
10827 /*
10828  * Handle a CPU exception for A and R profile CPUs.
10829  * Do any appropriate logging, handle PSCI calls, and then hand off
10830  * to the AArch64-entry or AArch32-entry function depending on the
10831  * target exception level's register width.
10832  *
10833  * Note: this is used for both TCG (as the do_interrupt tcg op),
10834  *       and KVM to re-inject guest debug exceptions, and to
10835  *       inject a Synchronous-External-Abort.
10836  */
10837 void arm_cpu_do_interrupt(CPUState *cs)
10838 {
10839     ARMCPU *cpu = ARM_CPU(cs);
10840     CPUARMState *env = &cpu->env;
10841     unsigned int new_el = env->exception.target_el;
10842 
10843     assert(!arm_feature(env, ARM_FEATURE_M));
10844 
10845     arm_log_exception(cs);
10846     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10847                   new_el);
10848     if (qemu_loglevel_mask(CPU_LOG_INT)
10849         && !excp_is_internal(cs->exception_index)) {
10850         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10851                       syn_get_ec(env->exception.syndrome),
10852                       env->exception.syndrome);
10853     }
10854 
10855     if (arm_is_psci_call(cpu, cs->exception_index)) {
10856         arm_handle_psci_call(cpu);
10857         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10858         return;
10859     }
10860 
10861     /*
10862      * Semihosting semantics depend on the register width of the code
10863      * that caused the exception, not the target exception level, so
10864      * must be handled here.
10865      */
10866 #ifdef CONFIG_TCG
10867     if (cs->exception_index == EXCP_SEMIHOST) {
10868         handle_semihosting(cs);
10869         return;
10870     }
10871 #endif
10872 
10873     /*
10874      * Hooks may change global state so BQL should be held, also the
10875      * BQL needs to be held for any modification of
10876      * cs->interrupt_request.
10877      */
10878     g_assert(qemu_mutex_iothread_locked());
10879 
10880     arm_call_pre_el_change_hook(cpu);
10881 
10882     assert(!excp_is_internal(cs->exception_index));
10883     if (arm_el_is_aa64(env, new_el)) {
10884         arm_cpu_do_interrupt_aarch64(cs);
10885     } else {
10886         arm_cpu_do_interrupt_aarch32(cs);
10887     }
10888 
10889     arm_call_el_change_hook(cpu);
10890 
10891     if (!kvm_enabled()) {
10892         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10893     }
10894 }
10895 #endif /* !CONFIG_USER_ONLY */
10896 
10897 uint64_t arm_sctlr(CPUARMState *env, int el)
10898 {
10899     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10900     if (el == 0) {
10901         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10902         el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
10903     }
10904     return env->cp15.sctlr_el[el];
10905 }
10906 
10907 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10908 {
10909     if (regime_has_2_ranges(mmu_idx)) {
10910         return extract64(tcr, 37, 2);
10911     } else if (regime_is_stage2(mmu_idx)) {
10912         return 0; /* VTCR_EL2 */
10913     } else {
10914         /* Replicate the single TBI bit so we always have 2 bits.  */
10915         return extract32(tcr, 20, 1) * 3;
10916     }
10917 }
10918 
10919 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10920 {
10921     if (regime_has_2_ranges(mmu_idx)) {
10922         return extract64(tcr, 51, 2);
10923     } else if (regime_is_stage2(mmu_idx)) {
10924         return 0; /* VTCR_EL2 */
10925     } else {
10926         /* Replicate the single TBID bit so we always have 2 bits.  */
10927         return extract32(tcr, 29, 1) * 3;
10928     }
10929 }
10930 
10931 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10932 {
10933     if (regime_has_2_ranges(mmu_idx)) {
10934         return extract64(tcr, 57, 2);
10935     } else {
10936         /* Replicate the single TCMA bit so we always have 2 bits.  */
10937         return extract32(tcr, 30, 1) * 3;
10938     }
10939 }
10940 
10941 static ARMGranuleSize tg0_to_gran_size(int tg)
10942 {
10943     switch (tg) {
10944     case 0:
10945         return Gran4K;
10946     case 1:
10947         return Gran64K;
10948     case 2:
10949         return Gran16K;
10950     default:
10951         return GranInvalid;
10952     }
10953 }
10954 
10955 static ARMGranuleSize tg1_to_gran_size(int tg)
10956 {
10957     switch (tg) {
10958     case 1:
10959         return Gran16K;
10960     case 2:
10961         return Gran4K;
10962     case 3:
10963         return Gran64K;
10964     default:
10965         return GranInvalid;
10966     }
10967 }
10968 
10969 static inline bool have4k(ARMCPU *cpu, bool stage2)
10970 {
10971     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
10972         : cpu_isar_feature(aa64_tgran4, cpu);
10973 }
10974 
10975 static inline bool have16k(ARMCPU *cpu, bool stage2)
10976 {
10977     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
10978         : cpu_isar_feature(aa64_tgran16, cpu);
10979 }
10980 
10981 static inline bool have64k(ARMCPU *cpu, bool stage2)
10982 {
10983     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
10984         : cpu_isar_feature(aa64_tgran64, cpu);
10985 }
10986 
10987 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
10988                                          bool stage2)
10989 {
10990     switch (gran) {
10991     case Gran4K:
10992         if (have4k(cpu, stage2)) {
10993             return gran;
10994         }
10995         break;
10996     case Gran16K:
10997         if (have16k(cpu, stage2)) {
10998             return gran;
10999         }
11000         break;
11001     case Gran64K:
11002         if (have64k(cpu, stage2)) {
11003             return gran;
11004         }
11005         break;
11006     case GranInvalid:
11007         break;
11008     }
11009     /*
11010      * If the guest selects a granule size that isn't implemented,
11011      * the architecture requires that we behave as if it selected one
11012      * that is (with an IMPDEF choice of which one to pick). We choose
11013      * to implement the smallest supported granule size.
11014      */
11015     if (have4k(cpu, stage2)) {
11016         return Gran4K;
11017     }
11018     if (have16k(cpu, stage2)) {
11019         return Gran16K;
11020     }
11021     assert(have64k(cpu, stage2));
11022     return Gran64K;
11023 }
11024 
11025 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11026                                    ARMMMUIdx mmu_idx, bool data)
11027 {
11028     uint64_t tcr = regime_tcr(env, mmu_idx);
11029     bool epd, hpd, tsz_oob, ds, ha, hd;
11030     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11031     ARMGranuleSize gran;
11032     ARMCPU *cpu = env_archcpu(env);
11033     bool stage2 = regime_is_stage2(mmu_idx);
11034 
11035     if (!regime_has_2_ranges(mmu_idx)) {
11036         select = 0;
11037         tsz = extract32(tcr, 0, 6);
11038         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11039         if (stage2) {
11040             /* VTCR_EL2 */
11041             hpd = false;
11042         } else {
11043             hpd = extract32(tcr, 24, 1);
11044         }
11045         epd = false;
11046         sh = extract32(tcr, 12, 2);
11047         ps = extract32(tcr, 16, 3);
11048         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11049         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11050         ds = extract64(tcr, 32, 1);
11051     } else {
11052         bool e0pd;
11053 
11054         /*
11055          * Bit 55 is always between the two regions, and is canonical for
11056          * determining if address tagging is enabled.
11057          */
11058         select = extract64(va, 55, 1);
11059         if (!select) {
11060             tsz = extract32(tcr, 0, 6);
11061             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11062             epd = extract32(tcr, 7, 1);
11063             sh = extract32(tcr, 12, 2);
11064             hpd = extract64(tcr, 41, 1);
11065             e0pd = extract64(tcr, 55, 1);
11066         } else {
11067             tsz = extract32(tcr, 16, 6);
11068             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11069             epd = extract32(tcr, 23, 1);
11070             sh = extract32(tcr, 28, 2);
11071             hpd = extract64(tcr, 42, 1);
11072             e0pd = extract64(tcr, 56, 1);
11073         }
11074         ps = extract64(tcr, 32, 3);
11075         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11076         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11077         ds = extract64(tcr, 59, 1);
11078 
11079         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11080             regime_is_user(env, mmu_idx)) {
11081             epd = true;
11082         }
11083     }
11084 
11085     gran = sanitize_gran_size(cpu, gran, stage2);
11086 
11087     if (cpu_isar_feature(aa64_st, cpu)) {
11088         max_tsz = 48 - (gran == Gran64K);
11089     } else {
11090         max_tsz = 39;
11091     }
11092 
11093     /*
11094      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11095      * adjust the effective value of DS, as documented.
11096      */
11097     min_tsz = 16;
11098     if (gran == Gran64K) {
11099         if (cpu_isar_feature(aa64_lva, cpu)) {
11100             min_tsz = 12;
11101         }
11102         ds = false;
11103     } else if (ds) {
11104         if (regime_is_stage2(mmu_idx)) {
11105             if (gran == Gran16K) {
11106                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11107             } else {
11108                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11109             }
11110         } else {
11111             if (gran == Gran16K) {
11112                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11113             } else {
11114                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11115             }
11116         }
11117         if (ds) {
11118             min_tsz = 12;
11119         }
11120     }
11121 
11122     if (tsz > max_tsz) {
11123         tsz = max_tsz;
11124         tsz_oob = true;
11125     } else if (tsz < min_tsz) {
11126         tsz = min_tsz;
11127         tsz_oob = true;
11128     } else {
11129         tsz_oob = false;
11130     }
11131 
11132     /* Present TBI as a composite with TBID.  */
11133     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11134     if (!data) {
11135         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11136     }
11137     tbi = (tbi >> select) & 1;
11138 
11139     return (ARMVAParameters) {
11140         .tsz = tsz,
11141         .ps = ps,
11142         .sh = sh,
11143         .select = select,
11144         .tbi = tbi,
11145         .epd = epd,
11146         .hpd = hpd,
11147         .tsz_oob = tsz_oob,
11148         .ds = ds,
11149         .ha = ha,
11150         .hd = ha && hd,
11151         .gran = gran,
11152     };
11153 }
11154 
11155 /*
11156  * Note that signed overflow is undefined in C.  The following routines are
11157  * careful to use unsigned types where modulo arithmetic is required.
11158  * Failure to do so _will_ break on newer gcc.
11159  */
11160 
11161 /* Signed saturating arithmetic.  */
11162 
11163 /* Perform 16-bit signed saturating addition.  */
11164 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11165 {
11166     uint16_t res;
11167 
11168     res = a + b;
11169     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11170         if (a & 0x8000) {
11171             res = 0x8000;
11172         } else {
11173             res = 0x7fff;
11174         }
11175     }
11176     return res;
11177 }
11178 
11179 /* Perform 8-bit signed saturating addition.  */
11180 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11181 {
11182     uint8_t res;
11183 
11184     res = a + b;
11185     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11186         if (a & 0x80) {
11187             res = 0x80;
11188         } else {
11189             res = 0x7f;
11190         }
11191     }
11192     return res;
11193 }
11194 
11195 /* Perform 16-bit signed saturating subtraction.  */
11196 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11197 {
11198     uint16_t res;
11199 
11200     res = a - b;
11201     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11202         if (a & 0x8000) {
11203             res = 0x8000;
11204         } else {
11205             res = 0x7fff;
11206         }
11207     }
11208     return res;
11209 }
11210 
11211 /* Perform 8-bit signed saturating subtraction.  */
11212 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11213 {
11214     uint8_t res;
11215 
11216     res = a - b;
11217     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11218         if (a & 0x80) {
11219             res = 0x80;
11220         } else {
11221             res = 0x7f;
11222         }
11223     }
11224     return res;
11225 }
11226 
11227 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11228 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11229 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
11230 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
11231 #define PFX q
11232 
11233 #include "op_addsub.h"
11234 
11235 /* Unsigned saturating arithmetic.  */
11236 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11237 {
11238     uint16_t res;
11239     res = a + b;
11240     if (res < a) {
11241         res = 0xffff;
11242     }
11243     return res;
11244 }
11245 
11246 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11247 {
11248     if (a > b) {
11249         return a - b;
11250     } else {
11251         return 0;
11252     }
11253 }
11254 
11255 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11256 {
11257     uint8_t res;
11258     res = a + b;
11259     if (res < a) {
11260         res = 0xff;
11261     }
11262     return res;
11263 }
11264 
11265 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11266 {
11267     if (a > b) {
11268         return a - b;
11269     } else {
11270         return 0;
11271     }
11272 }
11273 
11274 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11275 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11276 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
11277 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
11278 #define PFX uq
11279 
11280 #include "op_addsub.h"
11281 
11282 /* Signed modulo arithmetic.  */
11283 #define SARITH16(a, b, n, op) do { \
11284     int32_t sum; \
11285     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
11286     RESULT(sum, n, 16); \
11287     if (sum >= 0) \
11288         ge |= 3 << (n * 2); \
11289     } while (0)
11290 
11291 #define SARITH8(a, b, n, op) do { \
11292     int32_t sum; \
11293     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
11294     RESULT(sum, n, 8); \
11295     if (sum >= 0) \
11296         ge |= 1 << n; \
11297     } while (0)
11298 
11299 
11300 #define ADD16(a, b, n) SARITH16(a, b, n, +)
11301 #define SUB16(a, b, n) SARITH16(a, b, n, -)
11302 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
11303 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
11304 #define PFX s
11305 #define ARITH_GE
11306 
11307 #include "op_addsub.h"
11308 
11309 /* Unsigned modulo arithmetic.  */
11310 #define ADD16(a, b, n) do { \
11311     uint32_t sum; \
11312     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11313     RESULT(sum, n, 16); \
11314     if ((sum >> 16) == 1) \
11315         ge |= 3 << (n * 2); \
11316     } while (0)
11317 
11318 #define ADD8(a, b, n) do { \
11319     uint32_t sum; \
11320     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11321     RESULT(sum, n, 8); \
11322     if ((sum >> 8) == 1) \
11323         ge |= 1 << n; \
11324     } while (0)
11325 
11326 #define SUB16(a, b, n) do { \
11327     uint32_t sum; \
11328     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11329     RESULT(sum, n, 16); \
11330     if ((sum >> 16) == 0) \
11331         ge |= 3 << (n * 2); \
11332     } while (0)
11333 
11334 #define SUB8(a, b, n) do { \
11335     uint32_t sum; \
11336     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11337     RESULT(sum, n, 8); \
11338     if ((sum >> 8) == 0) \
11339         ge |= 1 << n; \
11340     } while (0)
11341 
11342 #define PFX u
11343 #define ARITH_GE
11344 
11345 #include "op_addsub.h"
11346 
11347 /* Halved signed arithmetic.  */
11348 #define ADD16(a, b, n) \
11349   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11350 #define SUB16(a, b, n) \
11351   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11352 #define ADD8(a, b, n) \
11353   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11354 #define SUB8(a, b, n) \
11355   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11356 #define PFX sh
11357 
11358 #include "op_addsub.h"
11359 
11360 /* Halved unsigned arithmetic.  */
11361 #define ADD16(a, b, n) \
11362   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11363 #define SUB16(a, b, n) \
11364   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11365 #define ADD8(a, b, n) \
11366   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11367 #define SUB8(a, b, n) \
11368   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11369 #define PFX uh
11370 
11371 #include "op_addsub.h"
11372 
11373 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11374 {
11375     if (a > b) {
11376         return a - b;
11377     } else {
11378         return b - a;
11379     }
11380 }
11381 
11382 /* Unsigned sum of absolute byte differences.  */
11383 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11384 {
11385     uint32_t sum;
11386     sum = do_usad(a, b);
11387     sum += do_usad(a >> 8, b >> 8);
11388     sum += do_usad(a >> 16, b >> 16);
11389     sum += do_usad(a >> 24, b >> 24);
11390     return sum;
11391 }
11392 
11393 /* For ARMv6 SEL instruction.  */
11394 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11395 {
11396     uint32_t mask;
11397 
11398     mask = 0;
11399     if (flags & 1) {
11400         mask |= 0xff;
11401     }
11402     if (flags & 2) {
11403         mask |= 0xff00;
11404     }
11405     if (flags & 4) {
11406         mask |= 0xff0000;
11407     }
11408     if (flags & 8) {
11409         mask |= 0xff000000;
11410     }
11411     return (a & mask) | (b & ~mask);
11412 }
11413 
11414 /*
11415  * CRC helpers.
11416  * The upper bytes of val (above the number specified by 'bytes') must have
11417  * been zeroed out by the caller.
11418  */
11419 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11420 {
11421     uint8_t buf[4];
11422 
11423     stl_le_p(buf, val);
11424 
11425     /* zlib crc32 converts the accumulator and output to one's complement.  */
11426     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11427 }
11428 
11429 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11430 {
11431     uint8_t buf[4];
11432 
11433     stl_le_p(buf, val);
11434 
11435     /* Linux crc32c converts the output to one's complement.  */
11436     return crc32c(acc, buf, bytes) ^ 0xffffffff;
11437 }
11438 
11439 /*
11440  * Return the exception level to which FP-disabled exceptions should
11441  * be taken, or 0 if FP is enabled.
11442  */
11443 int fp_exception_el(CPUARMState *env, int cur_el)
11444 {
11445 #ifndef CONFIG_USER_ONLY
11446     uint64_t hcr_el2;
11447 
11448     /*
11449      * CPACR and the CPTR registers don't exist before v6, so FP is
11450      * always accessible
11451      */
11452     if (!arm_feature(env, ARM_FEATURE_V6)) {
11453         return 0;
11454     }
11455 
11456     if (arm_feature(env, ARM_FEATURE_M)) {
11457         /* CPACR can cause a NOCP UsageFault taken to current security state */
11458         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11459             return 1;
11460         }
11461 
11462         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11463             if (!extract32(env->v7m.nsacr, 10, 1)) {
11464                 /* FP insns cause a NOCP UsageFault taken to Secure */
11465                 return 3;
11466             }
11467         }
11468 
11469         return 0;
11470     }
11471 
11472     hcr_el2 = arm_hcr_el2_eff(env);
11473 
11474     /*
11475      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11476      * 0, 2 : trap EL0 and EL1/PL1 accesses
11477      * 1    : trap only EL0 accesses
11478      * 3    : trap no accesses
11479      * This register is ignored if E2H+TGE are both set.
11480      */
11481     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11482         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11483 
11484         switch (fpen) {
11485         case 1:
11486             if (cur_el != 0) {
11487                 break;
11488             }
11489             /* fall through */
11490         case 0:
11491         case 2:
11492             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11493             if (!arm_el_is_aa64(env, 3)
11494                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11495                 return 3;
11496             }
11497             if (cur_el <= 1) {
11498                 return 1;
11499             }
11500             break;
11501         }
11502     }
11503 
11504     /*
11505      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11506      * to control non-secure access to the FPU. It doesn't have any
11507      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11508      */
11509     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11510          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11511         if (!extract32(env->cp15.nsacr, 10, 1)) {
11512             /* FP insns act as UNDEF */
11513             return cur_el == 2 ? 2 : 1;
11514         }
11515     }
11516 
11517     /*
11518      * CPTR_EL2 is present in v7VE or v8, and changes format
11519      * with HCR_EL2.E2H (regardless of TGE).
11520      */
11521     if (cur_el <= 2) {
11522         if (hcr_el2 & HCR_E2H) {
11523             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11524             case 1:
11525                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11526                     break;
11527                 }
11528                 /* fall through */
11529             case 0:
11530             case 2:
11531                 return 2;
11532             }
11533         } else if (arm_is_el2_enabled(env)) {
11534             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11535                 return 2;
11536             }
11537         }
11538     }
11539 
11540     /* CPTR_EL3 : present in v8 */
11541     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11542         /* Trap all FP ops to EL3 */
11543         return 3;
11544     }
11545 #endif
11546     return 0;
11547 }
11548 
11549 /* Return the exception level we're running at if this is our mmu_idx */
11550 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11551 {
11552     if (mmu_idx & ARM_MMU_IDX_M) {
11553         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11554     }
11555 
11556     switch (mmu_idx) {
11557     case ARMMMUIdx_E10_0:
11558     case ARMMMUIdx_E20_0:
11559         return 0;
11560     case ARMMMUIdx_E10_1:
11561     case ARMMMUIdx_E10_1_PAN:
11562         return 1;
11563     case ARMMMUIdx_E2:
11564     case ARMMMUIdx_E20_2:
11565     case ARMMMUIdx_E20_2_PAN:
11566         return 2;
11567     case ARMMMUIdx_E3:
11568         return 3;
11569     default:
11570         g_assert_not_reached();
11571     }
11572 }
11573 
11574 #ifndef CONFIG_TCG
11575 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11576 {
11577     g_assert_not_reached();
11578 }
11579 #endif
11580 
11581 static bool arm_pan_enabled(CPUARMState *env)
11582 {
11583     if (is_a64(env)) {
11584         return env->pstate & PSTATE_PAN;
11585     } else {
11586         return env->uncached_cpsr & CPSR_PAN;
11587     }
11588 }
11589 
11590 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11591 {
11592     ARMMMUIdx idx;
11593     uint64_t hcr;
11594 
11595     if (arm_feature(env, ARM_FEATURE_M)) {
11596         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11597     }
11598 
11599     /* See ARM pseudo-function ELIsInHost.  */
11600     switch (el) {
11601     case 0:
11602         hcr = arm_hcr_el2_eff(env);
11603         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11604             idx = ARMMMUIdx_E20_0;
11605         } else {
11606             idx = ARMMMUIdx_E10_0;
11607         }
11608         break;
11609     case 1:
11610         if (arm_pan_enabled(env)) {
11611             idx = ARMMMUIdx_E10_1_PAN;
11612         } else {
11613             idx = ARMMMUIdx_E10_1;
11614         }
11615         break;
11616     case 2:
11617         /* Note that TGE does not apply at EL2.  */
11618         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11619             if (arm_pan_enabled(env)) {
11620                 idx = ARMMMUIdx_E20_2_PAN;
11621             } else {
11622                 idx = ARMMMUIdx_E20_2;
11623             }
11624         } else {
11625             idx = ARMMMUIdx_E2;
11626         }
11627         break;
11628     case 3:
11629         return ARMMMUIdx_E3;
11630     default:
11631         g_assert_not_reached();
11632     }
11633 
11634     return idx;
11635 }
11636 
11637 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11638 {
11639     return arm_mmu_idx_el(env, arm_current_el(env));
11640 }
11641 
11642 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
11643                                            ARMMMUIdx mmu_idx,
11644                                            CPUARMTBFlags flags)
11645 {
11646     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
11647     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
11648 
11649     if (arm_singlestep_active(env)) {
11650         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
11651     }
11652     return flags;
11653 }
11654 
11655 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11656                                               ARMMMUIdx mmu_idx,
11657                                               CPUARMTBFlags flags)
11658 {
11659     bool sctlr_b = arm_sctlr_b(env);
11660 
11661     if (sctlr_b) {
11662         DP_TBFLAG_A32(flags, SCTLR__B, 1);
11663     }
11664     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
11665         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11666     }
11667     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
11668 
11669     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11670 }
11671 
11672 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11673                                         ARMMMUIdx mmu_idx)
11674 {
11675     CPUARMTBFlags flags = {};
11676     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11677 
11678     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11679     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11680         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11681     }
11682 
11683     if (arm_v7m_is_handler_mode(env)) {
11684         DP_TBFLAG_M32(flags, HANDLER, 1);
11685     }
11686 
11687     /*
11688      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11689      * is suppressing them because the requested execution priority
11690      * is less than 0.
11691      */
11692     if (arm_feature(env, ARM_FEATURE_V8) &&
11693         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11694           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11695         DP_TBFLAG_M32(flags, STACKCHECK, 1);
11696     }
11697 
11698     if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
11699         DP_TBFLAG_M32(flags, SECURE, 1);
11700     }
11701 
11702     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11703 }
11704 
11705 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11706                                         ARMMMUIdx mmu_idx)
11707 {
11708     CPUARMTBFlags flags = {};
11709     int el = arm_current_el(env);
11710 
11711     if (arm_sctlr(env, el) & SCTLR_A) {
11712         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11713     }
11714 
11715     if (arm_el_is_aa64(env, 1)) {
11716         DP_TBFLAG_A32(flags, VFPEN, 1);
11717     }
11718 
11719     if (el < 2 && env->cp15.hstr_el2 &&
11720         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11721         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
11722     }
11723 
11724     if (env->uncached_cpsr & CPSR_IL) {
11725         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11726     }
11727 
11728     /*
11729      * The SME exception we are testing for is raised via
11730      * AArch64.CheckFPAdvSIMDEnabled(), as called from
11731      * AArch32.CheckAdvSIMDOrFPEnabled().
11732      */
11733     if (el == 0
11734         && FIELD_EX64(env->svcr, SVCR, SM)
11735         && (!arm_is_el2_enabled(env)
11736             || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
11737         && arm_el_is_aa64(env, 1)
11738         && !sme_fa64(env, el)) {
11739         DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
11740     }
11741 
11742     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11743 }
11744 
11745 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11746                                         ARMMMUIdx mmu_idx)
11747 {
11748     CPUARMTBFlags flags = {};
11749     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11750     uint64_t tcr = regime_tcr(env, mmu_idx);
11751     uint64_t sctlr;
11752     int tbii, tbid;
11753 
11754     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
11755 
11756     /* Get control bits for tagged addresses.  */
11757     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11758     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
11759 
11760     DP_TBFLAG_A64(flags, TBII, tbii);
11761     DP_TBFLAG_A64(flags, TBID, tbid);
11762 
11763     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11764         int sve_el = sve_exception_el(env, el);
11765 
11766         /*
11767          * If either FP or SVE are disabled, translator does not need len.
11768          * If SVE EL > FP EL, FP exception has precedence, and translator
11769          * does not need SVE EL.  Save potential re-translations by forcing
11770          * the unneeded data to zero.
11771          */
11772         if (fp_el != 0) {
11773             if (sve_el > fp_el) {
11774                 sve_el = 0;
11775             }
11776         } else if (sve_el == 0) {
11777             DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
11778         }
11779         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
11780     }
11781     if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
11782         int sme_el = sme_exception_el(env, el);
11783         bool sm = FIELD_EX64(env->svcr, SVCR, SM);
11784 
11785         DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11786         if (sme_el == 0) {
11787             /* Similarly, do not compute SVL if SME is disabled. */
11788             int svl = sve_vqm1_for_el_sm(env, el, true);
11789             DP_TBFLAG_A64(flags, SVL, svl);
11790             if (sm) {
11791                 /* If SVE is disabled, we will not have set VL above. */
11792                 DP_TBFLAG_A64(flags, VL, svl);
11793             }
11794         }
11795         if (sm) {
11796             DP_TBFLAG_A64(flags, PSTATE_SM, 1);
11797             DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
11798         }
11799         DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
11800     }
11801 
11802     sctlr = regime_sctlr(env, stage1);
11803 
11804     if (sctlr & SCTLR_A) {
11805         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11806     }
11807 
11808     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11809         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11810     }
11811 
11812     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11813         /*
11814          * In order to save space in flags, we record only whether
11815          * pauth is "inactive", meaning all insns are implemented as
11816          * a nop, or "active" when some action must be performed.
11817          * The decision of which action to take is left to a helper.
11818          */
11819         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11820             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11821         }
11822     }
11823 
11824     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11825         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
11826         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11827             DP_TBFLAG_A64(flags, BT, 1);
11828         }
11829     }
11830 
11831     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11832     if (!(env->pstate & PSTATE_UAO)) {
11833         switch (mmu_idx) {
11834         case ARMMMUIdx_E10_1:
11835         case ARMMMUIdx_E10_1_PAN:
11836             /* TODO: ARMv8.3-NV */
11837             DP_TBFLAG_A64(flags, UNPRIV, 1);
11838             break;
11839         case ARMMMUIdx_E20_2:
11840         case ARMMMUIdx_E20_2_PAN:
11841             /*
11842              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11843              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11844              */
11845             if (env->cp15.hcr_el2 & HCR_TGE) {
11846                 DP_TBFLAG_A64(flags, UNPRIV, 1);
11847             }
11848             break;
11849         default:
11850             break;
11851         }
11852     }
11853 
11854     if (env->pstate & PSTATE_IL) {
11855         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11856     }
11857 
11858     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11859         /*
11860          * Set MTE_ACTIVE if any access may be Checked, and leave clear
11861          * if all accesses must be Unchecked:
11862          * 1) If no TBI, then there are no tags in the address to check,
11863          * 2) If Tag Check Override, then all accesses are Unchecked,
11864          * 3) If Tag Check Fail == 0, then Checked access have no effect,
11865          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11866          */
11867         if (allocation_tag_access_enabled(env, el, sctlr)) {
11868             DP_TBFLAG_A64(flags, ATA, 1);
11869             if (tbid
11870                 && !(env->pstate & PSTATE_TCO)
11871                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11872                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11873             }
11874         }
11875         /* And again for unprivileged accesses, if required.  */
11876         if (EX_TBFLAG_A64(flags, UNPRIV)
11877             && tbid
11878             && !(env->pstate & PSTATE_TCO)
11879             && (sctlr & SCTLR_TCF0)
11880             && allocation_tag_access_enabled(env, 0, sctlr)) {
11881             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11882         }
11883         /* Cache TCMA as well as TBI. */
11884         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11885     }
11886 
11887     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11888 }
11889 
11890 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11891 {
11892     int el = arm_current_el(env);
11893     int fp_el = fp_exception_el(env, el);
11894     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11895 
11896     if (is_a64(env)) {
11897         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11898     } else if (arm_feature(env, ARM_FEATURE_M)) {
11899         return rebuild_hflags_m32(env, fp_el, mmu_idx);
11900     } else {
11901         return rebuild_hflags_a32(env, fp_el, mmu_idx);
11902     }
11903 }
11904 
11905 void arm_rebuild_hflags(CPUARMState *env)
11906 {
11907     env->hflags = rebuild_hflags_internal(env);
11908 }
11909 
11910 /*
11911  * If we have triggered a EL state change we can't rely on the
11912  * translator having passed it to us, we need to recompute.
11913  */
11914 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11915 {
11916     int el = arm_current_el(env);
11917     int fp_el = fp_exception_el(env, el);
11918     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11919 
11920     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11921 }
11922 
11923 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11924 {
11925     int fp_el = fp_exception_el(env, el);
11926     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11927 
11928     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11929 }
11930 
11931 /*
11932  * If we have triggered a EL state change we can't rely on the
11933  * translator having passed it to us, we need to recompute.
11934  */
11935 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11936 {
11937     int el = arm_current_el(env);
11938     int fp_el = fp_exception_el(env, el);
11939     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11940     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11941 }
11942 
11943 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11944 {
11945     int fp_el = fp_exception_el(env, el);
11946     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11947 
11948     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11949 }
11950 
11951 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11952 {
11953     int fp_el = fp_exception_el(env, el);
11954     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11955 
11956     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11957 }
11958 
11959 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11960 {
11961 #ifdef CONFIG_DEBUG_TCG
11962     CPUARMTBFlags c = env->hflags;
11963     CPUARMTBFlags r = rebuild_hflags_internal(env);
11964 
11965     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11966         fprintf(stderr, "TCG hflags mismatch "
11967                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11968                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11969                 c.flags, c.flags2, r.flags, r.flags2);
11970         abort();
11971     }
11972 #endif
11973 }
11974 
11975 static bool mve_no_pred(CPUARMState *env)
11976 {
11977     /*
11978      * Return true if there is definitely no predication of MVE
11979      * instructions by VPR or LTPSIZE. (Returning false even if there
11980      * isn't any predication is OK; generated code will just be
11981      * a little worse.)
11982      * If the CPU does not implement MVE then this TB flag is always 0.
11983      *
11984      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11985      * logic in gen_update_fp_context() needs to be updated to match.
11986      *
11987      * We do not include the effect of the ECI bits here -- they are
11988      * tracked in other TB flags. This simplifies the logic for
11989      * "when did we emit code that changes the MVE_NO_PRED TB flag
11990      * and thus need to end the TB?".
11991      */
11992     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11993         return false;
11994     }
11995     if (env->v7m.vpr) {
11996         return false;
11997     }
11998     if (env->v7m.ltpsize < 4) {
11999         return false;
12000     }
12001     return true;
12002 }
12003 
12004 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12005                           target_ulong *cs_base, uint32_t *pflags)
12006 {
12007     CPUARMTBFlags flags;
12008 
12009     assert_hflags_rebuild_correctly(env);
12010     flags = env->hflags;
12011 
12012     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12013         *pc = env->pc;
12014         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12015             DP_TBFLAG_A64(flags, BTYPE, env->btype);
12016         }
12017     } else {
12018         *pc = env->regs[15];
12019 
12020         if (arm_feature(env, ARM_FEATURE_M)) {
12021             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12022                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12023                 != env->v7m.secure) {
12024                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12025             }
12026 
12027             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12028                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12029                  (env->v7m.secure &&
12030                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12031                 /*
12032                  * ASPEN is set, but FPCA/SFPA indicate that there is no
12033                  * active FP context; we must create a new FP context before
12034                  * executing any FP insn.
12035                  */
12036                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12037             }
12038 
12039             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12040             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12041                 DP_TBFLAG_M32(flags, LSPACT, 1);
12042             }
12043 
12044             if (mve_no_pred(env)) {
12045                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12046             }
12047         } else {
12048             /*
12049              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12050              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12051              */
12052             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12053                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12054             } else {
12055                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12056                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12057             }
12058             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12059                 DP_TBFLAG_A32(flags, VFPEN, 1);
12060             }
12061         }
12062 
12063         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12064         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12065     }
12066 
12067     /*
12068      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12069      * states defined in the ARM ARM for software singlestep:
12070      *  SS_ACTIVE   PSTATE.SS   State
12071      *     0            x       Inactive (the TB flag for SS is always 0)
12072      *     1            0       Active-pending
12073      *     1            1       Active-not-pending
12074      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12075      */
12076     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12077         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12078     }
12079 
12080     *pflags = flags.flags;
12081     *cs_base = flags.flags2;
12082 }
12083 
12084 #ifdef TARGET_AARCH64
12085 /*
12086  * The manual says that when SVE is enabled and VQ is widened the
12087  * implementation is allowed to zero the previously inaccessible
12088  * portion of the registers.  The corollary to that is that when
12089  * SVE is enabled and VQ is narrowed we are also allowed to zero
12090  * the now inaccessible portion of the registers.
12091  *
12092  * The intent of this is that no predicate bit beyond VQ is ever set.
12093  * Which means that some operations on predicate registers themselves
12094  * may operate on full uint64_t or even unrolled across the maximum
12095  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
12096  * may well be cheaper than conditionals to restrict the operation
12097  * to the relevant portion of a uint16_t[16].
12098  */
12099 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12100 {
12101     int i, j;
12102     uint64_t pmask;
12103 
12104     assert(vq >= 1 && vq <= ARM_MAX_VQ);
12105     assert(vq <= env_archcpu(env)->sve_max_vq);
12106 
12107     /* Zap the high bits of the zregs.  */
12108     for (i = 0; i < 32; i++) {
12109         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12110     }
12111 
12112     /* Zap the high bits of the pregs and ffr.  */
12113     pmask = 0;
12114     if (vq & 3) {
12115         pmask = ~(-1ULL << (16 * (vq & 3)));
12116     }
12117     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12118         for (i = 0; i < 17; ++i) {
12119             env->vfp.pregs[i].p[j] &= pmask;
12120         }
12121         pmask = 0;
12122     }
12123 }
12124 
12125 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12126 {
12127     int exc_el;
12128 
12129     if (sm) {
12130         exc_el = sme_exception_el(env, el);
12131     } else {
12132         exc_el = sve_exception_el(env, el);
12133     }
12134     if (exc_el) {
12135         return 0; /* disabled */
12136     }
12137     return sve_vqm1_for_el_sm(env, el, sm);
12138 }
12139 
12140 /*
12141  * Notice a change in SVE vector size when changing EL.
12142  */
12143 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12144                            int new_el, bool el0_a64)
12145 {
12146     ARMCPU *cpu = env_archcpu(env);
12147     int old_len, new_len;
12148     bool old_a64, new_a64, sm;
12149 
12150     /* Nothing to do if no SVE.  */
12151     if (!cpu_isar_feature(aa64_sve, cpu)) {
12152         return;
12153     }
12154 
12155     /* Nothing to do if FP is disabled in either EL.  */
12156     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12157         return;
12158     }
12159 
12160     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12161     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12162 
12163     /*
12164      * Both AArch64.TakeException and AArch64.ExceptionReturn
12165      * invoke ResetSVEState when taking an exception from, or
12166      * returning to, AArch32 state when PSTATE.SM is enabled.
12167      */
12168     sm = FIELD_EX64(env->svcr, SVCR, SM);
12169     if (old_a64 != new_a64 && sm) {
12170         arm_reset_sve_state(env);
12171         return;
12172     }
12173 
12174     /*
12175      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12176      * at ELx, or not available because the EL is in AArch32 state, then
12177      * for all purposes other than a direct read, the ZCR_ELx.LEN field
12178      * has an effective value of 0".
12179      *
12180      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12181      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12182      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
12183      * we already have the correct register contents when encountering the
12184      * vq0->vq0 transition between EL0->EL1.
12185      */
12186     old_len = new_len = 0;
12187     if (old_a64) {
12188         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12189     }
12190     if (new_a64) {
12191         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12192     }
12193 
12194     /* When changing vector length, clear inaccessible state.  */
12195     if (new_len < old_len) {
12196         aarch64_sve_narrow_vq(env, new_len + 1);
12197     }
12198 }
12199 #endif
12200