xref: /qemu/target/arm/helper.c (revision 118d4ed0)
1 /*
2  * ARM generic helpers.
3  *
4  * This code is licensed under the GNU GPL v2 or later.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qemu/units.h"
11 #include "qemu/log.h"
12 #include "trace.h"
13 #include "cpu.h"
14 #include "internals.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/host-utils.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/timer.h"
19 #include "qemu/bitops.h"
20 #include "qemu/crc32c.h"
21 #include "qemu/qemu-print.h"
22 #include "exec/exec-all.h"
23 #include <zlib.h> /* For crc32 */
24 #include "hw/irq.h"
25 #include "semihosting/semihost.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/cpu-timers.h"
28 #include "sysemu/kvm.h"
29 #include "qemu/range.h"
30 #include "qapi/qapi-commands-machine-target.h"
31 #include "qapi/error.h"
32 #include "qemu/guest-random.h"
33 #ifdef CONFIG_TCG
34 #include "arm_ldst.h"
35 #include "exec/cpu_ldst.h"
36 #include "semihosting/common-semi.h"
37 #endif
38 #include "cpregs.h"
39 
40 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
41 
42 static void switch_mode(CPUARMState *env, int mode);
43 
44 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
45 {
46     assert(ri->fieldoffset);
47     if (cpreg_field_is_64bit(ri)) {
48         return CPREG_FIELD64(env, ri);
49     } else {
50         return CPREG_FIELD32(env, ri);
51     }
52 }
53 
54 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
55                       uint64_t value)
56 {
57     assert(ri->fieldoffset);
58     if (cpreg_field_is_64bit(ri)) {
59         CPREG_FIELD64(env, ri) = value;
60     } else {
61         CPREG_FIELD32(env, ri) = value;
62     }
63 }
64 
65 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
66 {
67     return (char *)env + ri->fieldoffset;
68 }
69 
70 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
71 {
72     /* Raw read of a coprocessor register (as needed for migration, etc). */
73     if (ri->type & ARM_CP_CONST) {
74         return ri->resetvalue;
75     } else if (ri->raw_readfn) {
76         return ri->raw_readfn(env, ri);
77     } else if (ri->readfn) {
78         return ri->readfn(env, ri);
79     } else {
80         return raw_read(env, ri);
81     }
82 }
83 
84 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
85                              uint64_t v)
86 {
87     /* Raw write of a coprocessor register (as needed for migration, etc).
88      * Note that constant registers are treated as write-ignored; the
89      * caller should check for success by whether a readback gives the
90      * value written.
91      */
92     if (ri->type & ARM_CP_CONST) {
93         return;
94     } else if (ri->raw_writefn) {
95         ri->raw_writefn(env, ri, v);
96     } else if (ri->writefn) {
97         ri->writefn(env, ri, v);
98     } else {
99         raw_write(env, ri, v);
100     }
101 }
102 
103 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
104 {
105    /* Return true if the regdef would cause an assertion if you called
106     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
107     * program bug for it not to have the NO_RAW flag).
108     * NB that returning false here doesn't necessarily mean that calling
109     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
110     * read/write access functions which are safe for raw use" from "has
111     * read/write access functions which have side effects but has forgotten
112     * to provide raw access functions".
113     * The tests here line up with the conditions in read/write_raw_cp_reg()
114     * and assertions in raw_read()/raw_write().
115     */
116     if ((ri->type & ARM_CP_CONST) ||
117         ri->fieldoffset ||
118         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
119         return false;
120     }
121     return true;
122 }
123 
124 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
125 {
126     /* Write the coprocessor state from cpu->env to the (index,value) list. */
127     int i;
128     bool ok = true;
129 
130     for (i = 0; i < cpu->cpreg_array_len; i++) {
131         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
132         const ARMCPRegInfo *ri;
133         uint64_t newval;
134 
135         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
136         if (!ri) {
137             ok = false;
138             continue;
139         }
140         if (ri->type & ARM_CP_NO_RAW) {
141             continue;
142         }
143 
144         newval = read_raw_cp_reg(&cpu->env, ri);
145         if (kvm_sync) {
146             /*
147              * Only sync if the previous list->cpustate sync succeeded.
148              * Rather than tracking the success/failure state for every
149              * item in the list, we just recheck "does the raw write we must
150              * have made in write_list_to_cpustate() read back OK" here.
151              */
152             uint64_t oldval = cpu->cpreg_values[i];
153 
154             if (oldval == newval) {
155                 continue;
156             }
157 
158             write_raw_cp_reg(&cpu->env, ri, oldval);
159             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
160                 continue;
161             }
162 
163             write_raw_cp_reg(&cpu->env, ri, newval);
164         }
165         cpu->cpreg_values[i] = newval;
166     }
167     return ok;
168 }
169 
170 bool write_list_to_cpustate(ARMCPU *cpu)
171 {
172     int i;
173     bool ok = true;
174 
175     for (i = 0; i < cpu->cpreg_array_len; i++) {
176         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
177         uint64_t v = cpu->cpreg_values[i];
178         const ARMCPRegInfo *ri;
179 
180         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
181         if (!ri) {
182             ok = false;
183             continue;
184         }
185         if (ri->type & ARM_CP_NO_RAW) {
186             continue;
187         }
188         /* Write value and confirm it reads back as written
189          * (to catch read-only registers and partially read-only
190          * registers where the incoming migration value doesn't match)
191          */
192         write_raw_cp_reg(&cpu->env, ri, v);
193         if (read_raw_cp_reg(&cpu->env, ri) != v) {
194             ok = false;
195         }
196     }
197     return ok;
198 }
199 
200 static void add_cpreg_to_list(gpointer key, gpointer opaque)
201 {
202     ARMCPU *cpu = opaque;
203     uint32_t regidx = (uintptr_t)key;
204     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
205 
206     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
207         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
208         /* The value array need not be initialized at this point */
209         cpu->cpreg_array_len++;
210     }
211 }
212 
213 static void count_cpreg(gpointer key, gpointer opaque)
214 {
215     ARMCPU *cpu = opaque;
216     const ARMCPRegInfo *ri;
217 
218     ri = g_hash_table_lookup(cpu->cp_regs, key);
219 
220     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
221         cpu->cpreg_array_len++;
222     }
223 }
224 
225 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
226 {
227     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
228     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
229 
230     if (aidx > bidx) {
231         return 1;
232     }
233     if (aidx < bidx) {
234         return -1;
235     }
236     return 0;
237 }
238 
239 void init_cpreg_list(ARMCPU *cpu)
240 {
241     /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
242      * Note that we require cpreg_tuples[] to be sorted by key ID.
243      */
244     GList *keys;
245     int arraylen;
246 
247     keys = g_hash_table_get_keys(cpu->cp_regs);
248     keys = g_list_sort(keys, cpreg_key_compare);
249 
250     cpu->cpreg_array_len = 0;
251 
252     g_list_foreach(keys, count_cpreg, cpu);
253 
254     arraylen = cpu->cpreg_array_len;
255     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
256     cpu->cpreg_values = g_new(uint64_t, arraylen);
257     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
258     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
259     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
260     cpu->cpreg_array_len = 0;
261 
262     g_list_foreach(keys, add_cpreg_to_list, cpu);
263 
264     assert(cpu->cpreg_array_len == arraylen);
265 
266     g_list_free(keys);
267 }
268 
269 /*
270  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
271  */
272 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
273                                         const ARMCPRegInfo *ri,
274                                         bool isread)
275 {
276     if (!is_a64(env) && arm_current_el(env) == 3 &&
277         arm_is_secure_below_el3(env)) {
278         return CP_ACCESS_TRAP_UNCATEGORIZED;
279     }
280     return CP_ACCESS_OK;
281 }
282 
283 /* Some secure-only AArch32 registers trap to EL3 if used from
284  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
285  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
286  * We assume that the .access field is set to PL1_RW.
287  */
288 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
289                                             const ARMCPRegInfo *ri,
290                                             bool isread)
291 {
292     if (arm_current_el(env) == 3) {
293         return CP_ACCESS_OK;
294     }
295     if (arm_is_secure_below_el3(env)) {
296         if (env->cp15.scr_el3 & SCR_EEL2) {
297             return CP_ACCESS_TRAP_EL2;
298         }
299         return CP_ACCESS_TRAP_EL3;
300     }
301     /* This will be EL1 NS and EL2 NS, which just UNDEF */
302     return CP_ACCESS_TRAP_UNCATEGORIZED;
303 }
304 
305 static uint64_t arm_mdcr_el2_eff(CPUARMState *env)
306 {
307     return arm_is_el2_enabled(env) ? env->cp15.mdcr_el2 : 0;
308 }
309 
310 /* Check for traps to "powerdown debug" registers, which are controlled
311  * by MDCR.TDOSA
312  */
313 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
314                                    bool isread)
315 {
316     int el = arm_current_el(env);
317     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
318     bool mdcr_el2_tdosa = (mdcr_el2 & MDCR_TDOSA) || (mdcr_el2 & MDCR_TDE) ||
319         (arm_hcr_el2_eff(env) & HCR_TGE);
320 
321     if (el < 2 && mdcr_el2_tdosa) {
322         return CP_ACCESS_TRAP_EL2;
323     }
324     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
325         return CP_ACCESS_TRAP_EL3;
326     }
327     return CP_ACCESS_OK;
328 }
329 
330 /* Check for traps to "debug ROM" registers, which are controlled
331  * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
332  */
333 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
334                                   bool isread)
335 {
336     int el = arm_current_el(env);
337     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
338     bool mdcr_el2_tdra = (mdcr_el2 & MDCR_TDRA) || (mdcr_el2 & MDCR_TDE) ||
339         (arm_hcr_el2_eff(env) & HCR_TGE);
340 
341     if (el < 2 && mdcr_el2_tdra) {
342         return CP_ACCESS_TRAP_EL2;
343     }
344     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
345         return CP_ACCESS_TRAP_EL3;
346     }
347     return CP_ACCESS_OK;
348 }
349 
350 /* Check for traps to general debug registers, which are controlled
351  * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
352  */
353 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
354                                   bool isread)
355 {
356     int el = arm_current_el(env);
357     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
358     bool mdcr_el2_tda = (mdcr_el2 & MDCR_TDA) || (mdcr_el2 & MDCR_TDE) ||
359         (arm_hcr_el2_eff(env) & HCR_TGE);
360 
361     if (el < 2 && mdcr_el2_tda) {
362         return CP_ACCESS_TRAP_EL2;
363     }
364     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
365         return CP_ACCESS_TRAP_EL3;
366     }
367     return CP_ACCESS_OK;
368 }
369 
370 /* Check for traps to performance monitor registers, which are controlled
371  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
372  */
373 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
374                                  bool isread)
375 {
376     int el = arm_current_el(env);
377     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
378 
379     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
380         return CP_ACCESS_TRAP_EL2;
381     }
382     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
383         return CP_ACCESS_TRAP_EL3;
384     }
385     return CP_ACCESS_OK;
386 }
387 
388 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
389 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
390                                       bool isread)
391 {
392     if (arm_current_el(env) == 1) {
393         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
394         if (arm_hcr_el2_eff(env) & trap) {
395             return CP_ACCESS_TRAP_EL2;
396         }
397     }
398     return CP_ACCESS_OK;
399 }
400 
401 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
402 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
403                                  bool isread)
404 {
405     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
406         return CP_ACCESS_TRAP_EL2;
407     }
408     return CP_ACCESS_OK;
409 }
410 
411 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
412 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
413                                   bool isread)
414 {
415     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
416         return CP_ACCESS_TRAP_EL2;
417     }
418     return CP_ACCESS_OK;
419 }
420 
421 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
422 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
423                                   bool isread)
424 {
425     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
426         return CP_ACCESS_TRAP_EL2;
427     }
428     return CP_ACCESS_OK;
429 }
430 
431 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
432 {
433     ARMCPU *cpu = env_archcpu(env);
434 
435     raw_write(env, ri, value);
436     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
437 }
438 
439 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
440 {
441     ARMCPU *cpu = env_archcpu(env);
442 
443     if (raw_read(env, ri) != value) {
444         /* Unlike real hardware the qemu TLB uses virtual addresses,
445          * not modified virtual addresses, so this causes a TLB flush.
446          */
447         tlb_flush(CPU(cpu));
448         raw_write(env, ri, value);
449     }
450 }
451 
452 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
453                              uint64_t value)
454 {
455     ARMCPU *cpu = env_archcpu(env);
456 
457     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
458         && !extended_addresses_enabled(env)) {
459         /* For VMSA (when not using the LPAE long descriptor page table
460          * format) this register includes the ASID, so do a TLB flush.
461          * For PMSA it is purely a process ID and no action is needed.
462          */
463         tlb_flush(CPU(cpu));
464     }
465     raw_write(env, ri, value);
466 }
467 
468 /* IS variants of TLB operations must affect all cores */
469 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
470                              uint64_t value)
471 {
472     CPUState *cs = env_cpu(env);
473 
474     tlb_flush_all_cpus_synced(cs);
475 }
476 
477 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
478                              uint64_t value)
479 {
480     CPUState *cs = env_cpu(env);
481 
482     tlb_flush_all_cpus_synced(cs);
483 }
484 
485 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
486                              uint64_t value)
487 {
488     CPUState *cs = env_cpu(env);
489 
490     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
491 }
492 
493 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
494                              uint64_t value)
495 {
496     CPUState *cs = env_cpu(env);
497 
498     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
499 }
500 
501 /*
502  * Non-IS variants of TLB operations are upgraded to
503  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
504  * force broadcast of these operations.
505  */
506 static bool tlb_force_broadcast(CPUARMState *env)
507 {
508     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
509 }
510 
511 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
512                           uint64_t value)
513 {
514     /* Invalidate all (TLBIALL) */
515     CPUState *cs = env_cpu(env);
516 
517     if (tlb_force_broadcast(env)) {
518         tlb_flush_all_cpus_synced(cs);
519     } else {
520         tlb_flush(cs);
521     }
522 }
523 
524 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
525                           uint64_t value)
526 {
527     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
528     CPUState *cs = env_cpu(env);
529 
530     value &= TARGET_PAGE_MASK;
531     if (tlb_force_broadcast(env)) {
532         tlb_flush_page_all_cpus_synced(cs, value);
533     } else {
534         tlb_flush_page(cs, value);
535     }
536 }
537 
538 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
539                            uint64_t value)
540 {
541     /* Invalidate by ASID (TLBIASID) */
542     CPUState *cs = env_cpu(env);
543 
544     if (tlb_force_broadcast(env)) {
545         tlb_flush_all_cpus_synced(cs);
546     } else {
547         tlb_flush(cs);
548     }
549 }
550 
551 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
552                            uint64_t value)
553 {
554     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
555     CPUState *cs = env_cpu(env);
556 
557     value &= TARGET_PAGE_MASK;
558     if (tlb_force_broadcast(env)) {
559         tlb_flush_page_all_cpus_synced(cs, value);
560     } else {
561         tlb_flush_page(cs, value);
562     }
563 }
564 
565 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
566                                uint64_t value)
567 {
568     CPUState *cs = env_cpu(env);
569 
570     tlb_flush_by_mmuidx(cs,
571                         ARMMMUIdxBit_E10_1 |
572                         ARMMMUIdxBit_E10_1_PAN |
573                         ARMMMUIdxBit_E10_0);
574 }
575 
576 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
577                                   uint64_t value)
578 {
579     CPUState *cs = env_cpu(env);
580 
581     tlb_flush_by_mmuidx_all_cpus_synced(cs,
582                                         ARMMMUIdxBit_E10_1 |
583                                         ARMMMUIdxBit_E10_1_PAN |
584                                         ARMMMUIdxBit_E10_0);
585 }
586 
587 
588 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
589                               uint64_t value)
590 {
591     CPUState *cs = env_cpu(env);
592 
593     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
594 }
595 
596 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
597                                  uint64_t value)
598 {
599     CPUState *cs = env_cpu(env);
600 
601     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
602 }
603 
604 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
605                               uint64_t value)
606 {
607     CPUState *cs = env_cpu(env);
608     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
609 
610     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
611 }
612 
613 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
614                                  uint64_t value)
615 {
616     CPUState *cs = env_cpu(env);
617     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
618 
619     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
620                                              ARMMMUIdxBit_E2);
621 }
622 
623 static const ARMCPRegInfo cp_reginfo[] = {
624     /* Define the secure and non-secure FCSE identifier CP registers
625      * separately because there is no secure bank in V8 (no _EL3).  This allows
626      * the secure register to be properly reset and migrated. There is also no
627      * v8 EL1 version of the register so the non-secure instance stands alone.
628      */
629     { .name = "FCSEIDR",
630       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
631       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
632       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
633       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
634     { .name = "FCSEIDR_S",
635       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
636       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
637       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
638       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
639     /* Define the secure and non-secure context identifier CP registers
640      * separately because there is no secure bank in V8 (no _EL3).  This allows
641      * the secure register to be properly reset and migrated.  In the
642      * non-secure case, the 32-bit register will have reset and migration
643      * disabled during registration as it is handled by the 64-bit instance.
644      */
645     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
646       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
647       .access = PL1_RW, .accessfn = access_tvm_trvm,
648       .secure = ARM_CP_SECSTATE_NS,
649       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
650       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
651     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
652       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
653       .access = PL1_RW, .accessfn = access_tvm_trvm,
654       .secure = ARM_CP_SECSTATE_S,
655       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
656       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
657 };
658 
659 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
660     /* NB: Some of these registers exist in v8 but with more precise
661      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
662      */
663     /* MMU Domain access control / MPU write buffer control */
664     { .name = "DACR",
665       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
666       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
667       .writefn = dacr_write, .raw_writefn = raw_write,
668       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
669                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
670     /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
671      * For v6 and v5, these mappings are overly broad.
672      */
673     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
674       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
675     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
676       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
677     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
678       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
679     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
680       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
681     /* Cache maintenance ops; some of this space may be overridden later. */
682     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
683       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
684       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
685 };
686 
687 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
688     /* Not all pre-v6 cores implemented this WFI, so this is slightly
689      * over-broad.
690      */
691     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
692       .access = PL1_W, .type = ARM_CP_WFI },
693 };
694 
695 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
696     /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
697      * is UNPREDICTABLE; we choose to NOP as most implementations do).
698      */
699     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
700       .access = PL1_W, .type = ARM_CP_WFI },
701     /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
702      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
703      * OMAPCP will override this space.
704      */
705     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
706       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
707       .resetvalue = 0 },
708     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
709       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
710       .resetvalue = 0 },
711     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
712     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
713       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
714       .resetvalue = 0 },
715     /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
716      * implementing it as RAZ means the "debug architecture version" bits
717      * will read as a reserved value, which should cause Linux to not try
718      * to use the debug hardware.
719      */
720     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
721       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
722     /* MMU TLB control. Note that the wildcarding means we cover not just
723      * the unified TLB ops but also the dside/iside/inner-shareable variants.
724      */
725     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
726       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
727       .type = ARM_CP_NO_RAW },
728     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
729       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
730       .type = ARM_CP_NO_RAW },
731     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
732       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
733       .type = ARM_CP_NO_RAW },
734     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
735       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
736       .type = ARM_CP_NO_RAW },
737     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
738       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
739     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
740       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
741 };
742 
743 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
744                         uint64_t value)
745 {
746     uint32_t mask = 0;
747 
748     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
749     if (!arm_feature(env, ARM_FEATURE_V8)) {
750         /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
751          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
752          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
753          */
754         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
755             /* VFP coprocessor: cp10 & cp11 [23:20] */
756             mask |= R_CPACR_ASEDIS_MASK |
757                     R_CPACR_D32DIS_MASK |
758                     R_CPACR_CP11_MASK |
759                     R_CPACR_CP10_MASK;
760 
761             if (!arm_feature(env, ARM_FEATURE_NEON)) {
762                 /* ASEDIS [31] bit is RAO/WI */
763                 value |= R_CPACR_ASEDIS_MASK;
764             }
765 
766             /* VFPv3 and upwards with NEON implement 32 double precision
767              * registers (D0-D31).
768              */
769             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
770                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
771                 value |= R_CPACR_D32DIS_MASK;
772             }
773         }
774         value &= mask;
775     }
776 
777     /*
778      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
779      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
780      */
781     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
782         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
783         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
784         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
785     }
786 
787     env->cp15.cpacr_el1 = value;
788 }
789 
790 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
791 {
792     /*
793      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
794      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
795      */
796     uint64_t value = env->cp15.cpacr_el1;
797 
798     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
799         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
800         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
801     }
802     return value;
803 }
804 
805 
806 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
807 {
808     /* Call cpacr_write() so that we reset with the correct RAO bits set
809      * for our CPU features.
810      */
811     cpacr_write(env, ri, 0);
812 }
813 
814 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
815                                    bool isread)
816 {
817     if (arm_feature(env, ARM_FEATURE_V8)) {
818         /* Check if CPACR accesses are to be trapped to EL2 */
819         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
820             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
821             return CP_ACCESS_TRAP_EL2;
822         /* Check if CPACR accesses are to be trapped to EL3 */
823         } else if (arm_current_el(env) < 3 &&
824                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
825             return CP_ACCESS_TRAP_EL3;
826         }
827     }
828 
829     return CP_ACCESS_OK;
830 }
831 
832 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
833                                   bool isread)
834 {
835     /* Check if CPTR accesses are set to trap to EL3 */
836     if (arm_current_el(env) == 2 &&
837         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
838         return CP_ACCESS_TRAP_EL3;
839     }
840 
841     return CP_ACCESS_OK;
842 }
843 
844 static const ARMCPRegInfo v6_cp_reginfo[] = {
845     /* prefetch by MVA in v6, NOP in v7 */
846     { .name = "MVA_prefetch",
847       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
848       .access = PL1_W, .type = ARM_CP_NOP },
849     /* We need to break the TB after ISB to execute self-modifying code
850      * correctly and also to take any pending interrupts immediately.
851      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
852      */
853     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
854       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
855     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
856       .access = PL0_W, .type = ARM_CP_NOP },
857     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
858       .access = PL0_W, .type = ARM_CP_NOP },
859     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
860       .access = PL1_RW, .accessfn = access_tvm_trvm,
861       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
862                              offsetof(CPUARMState, cp15.ifar_ns) },
863       .resetvalue = 0, },
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 pmu_8_1_events_supported(CPUARMState *env)
949 {
950     /* For events which are supported in any v8.1 PMU */
951     return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
952 }
953 
954 static bool pmu_8_4_events_supported(CPUARMState *env)
955 {
956     /* For events which are supported in any v8.1 PMU */
957     return cpu_isar_feature(any_pmu_8_4, 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 = pmu_8_1_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 = pmu_8_1_events_supported,
997       .get_count = zero_event_get_count,
998       .ns_per_count = zero_event_ns_per,
999     },
1000     { .number = 0x03c, /* STALL */
1001       .supported = pmu_8_4_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     /* Performance monitor registers user accessibility is controlled
1070      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1071      * trapping to EL2 or EL3 for other accesses.
1072      */
1073     int el = arm_current_el(env);
1074     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1075 
1076     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1077         return CP_ACCESS_TRAP;
1078     }
1079     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1080         return CP_ACCESS_TRAP_EL2;
1081     }
1082     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1083         return CP_ACCESS_TRAP_EL3;
1084     }
1085 
1086     return CP_ACCESS_OK;
1087 }
1088 
1089 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1090                                            const ARMCPRegInfo *ri,
1091                                            bool isread)
1092 {
1093     /* ER: event counter read trap control */
1094     if (arm_feature(env, ARM_FEATURE_V8)
1095         && arm_current_el(env) == 0
1096         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1097         && isread) {
1098         return CP_ACCESS_OK;
1099     }
1100 
1101     return pmreg_access(env, ri, isread);
1102 }
1103 
1104 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1105                                          const ARMCPRegInfo *ri,
1106                                          bool isread)
1107 {
1108     /* SW: software increment write trap control */
1109     if (arm_feature(env, ARM_FEATURE_V8)
1110         && arm_current_el(env) == 0
1111         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1112         && !isread) {
1113         return CP_ACCESS_OK;
1114     }
1115 
1116     return pmreg_access(env, ri, isread);
1117 }
1118 
1119 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1120                                         const ARMCPRegInfo *ri,
1121                                         bool isread)
1122 {
1123     /* ER: event counter read trap control */
1124     if (arm_feature(env, ARM_FEATURE_V8)
1125         && arm_current_el(env) == 0
1126         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1127         return CP_ACCESS_OK;
1128     }
1129 
1130     return pmreg_access(env, ri, isread);
1131 }
1132 
1133 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1134                                          const ARMCPRegInfo *ri,
1135                                          bool isread)
1136 {
1137     /* CR: cycle counter read trap control */
1138     if (arm_feature(env, ARM_FEATURE_V8)
1139         && arm_current_el(env) == 0
1140         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1141         && isread) {
1142         return CP_ACCESS_OK;
1143     }
1144 
1145     return pmreg_access(env, ri, isread);
1146 }
1147 
1148 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1149  * the current EL, security state, and register configuration.
1150  */
1151 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1152 {
1153     uint64_t filter;
1154     bool e, p, u, nsk, nsu, nsh, m;
1155     bool enabled, prohibited, filtered;
1156     bool secure = arm_is_secure(env);
1157     int el = arm_current_el(env);
1158     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1159     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1160 
1161     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1162         return false;
1163     }
1164 
1165     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1166             (counter < hpmn || counter == 31)) {
1167         e = env->cp15.c9_pmcr & PMCRE;
1168     } else {
1169         e = mdcr_el2 & MDCR_HPME;
1170     }
1171     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1172 
1173     if (!secure) {
1174         if (el == 2 && (counter < hpmn || counter == 31)) {
1175             prohibited = mdcr_el2 & MDCR_HPMD;
1176         } else {
1177             prohibited = false;
1178         }
1179     } else {
1180         prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1181            !(env->cp15.mdcr_el3 & MDCR_SPME);
1182     }
1183 
1184     if (prohibited && counter == 31) {
1185         prohibited = env->cp15.c9_pmcr & PMCRDP;
1186     }
1187 
1188     if (counter == 31) {
1189         filter = env->cp15.pmccfiltr_el0;
1190     } else {
1191         filter = env->cp15.c14_pmevtyper[counter];
1192     }
1193 
1194     p   = filter & PMXEVTYPER_P;
1195     u   = filter & PMXEVTYPER_U;
1196     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1197     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1198     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1199     m   = arm_el_is_aa64(env, 1) &&
1200               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1201 
1202     if (el == 0) {
1203         filtered = secure ? u : u != nsu;
1204     } else if (el == 1) {
1205         filtered = secure ? p : p != nsk;
1206     } else if (el == 2) {
1207         filtered = !nsh;
1208     } else { /* EL3 */
1209         filtered = m != p;
1210     }
1211 
1212     if (counter != 31) {
1213         /*
1214          * If not checking PMCCNTR, ensure the counter is setup to an event we
1215          * support
1216          */
1217         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1218         if (!event_supported(event)) {
1219             return false;
1220         }
1221     }
1222 
1223     return enabled && !prohibited && !filtered;
1224 }
1225 
1226 static void pmu_update_irq(CPUARMState *env)
1227 {
1228     ARMCPU *cpu = env_archcpu(env);
1229     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1230             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1231 }
1232 
1233 /*
1234  * Ensure c15_ccnt is the guest-visible count so that operations such as
1235  * enabling/disabling the counter or filtering, modifying the count itself,
1236  * etc. can be done logically. This is essentially a no-op if the counter is
1237  * not enabled at the time of the call.
1238  */
1239 static void pmccntr_op_start(CPUARMState *env)
1240 {
1241     uint64_t cycles = cycles_get_count(env);
1242 
1243     if (pmu_counter_enabled(env, 31)) {
1244         uint64_t eff_cycles = cycles;
1245         if (env->cp15.c9_pmcr & PMCRD) {
1246             /* Increment once every 64 processor clock cycles */
1247             eff_cycles /= 64;
1248         }
1249 
1250         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1251 
1252         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1253                                  1ull << 63 : 1ull << 31;
1254         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1255             env->cp15.c9_pmovsr |= (1 << 31);
1256             pmu_update_irq(env);
1257         }
1258 
1259         env->cp15.c15_ccnt = new_pmccntr;
1260     }
1261     env->cp15.c15_ccnt_delta = cycles;
1262 }
1263 
1264 /*
1265  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1266  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1267  * pmccntr_op_start.
1268  */
1269 static void pmccntr_op_finish(CPUARMState *env)
1270 {
1271     if (pmu_counter_enabled(env, 31)) {
1272 #ifndef CONFIG_USER_ONLY
1273         /* Calculate when the counter will next overflow */
1274         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1275         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1276             remaining_cycles = (uint32_t)remaining_cycles;
1277         }
1278         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1279 
1280         if (overflow_in > 0) {
1281             int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1282                 overflow_in;
1283             ARMCPU *cpu = env_archcpu(env);
1284             timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1285         }
1286 #endif
1287 
1288         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1289         if (env->cp15.c9_pmcr & PMCRD) {
1290             /* Increment once every 64 processor clock cycles */
1291             prev_cycles /= 64;
1292         }
1293         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1294     }
1295 }
1296 
1297 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1298 {
1299 
1300     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1301     uint64_t count = 0;
1302     if (event_supported(event)) {
1303         uint16_t event_idx = supported_event_map[event];
1304         count = pm_events[event_idx].get_count(env);
1305     }
1306 
1307     if (pmu_counter_enabled(env, counter)) {
1308         uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1309 
1310         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1311             env->cp15.c9_pmovsr |= (1 << counter);
1312             pmu_update_irq(env);
1313         }
1314         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1315     }
1316     env->cp15.c14_pmevcntr_delta[counter] = count;
1317 }
1318 
1319 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1320 {
1321     if (pmu_counter_enabled(env, counter)) {
1322 #ifndef CONFIG_USER_ONLY
1323         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1324         uint16_t event_idx = supported_event_map[event];
1325         uint64_t delta = UINT32_MAX -
1326             (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1327         int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1328 
1329         if (overflow_in > 0) {
1330             int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1331                 overflow_in;
1332             ARMCPU *cpu = env_archcpu(env);
1333             timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1334         }
1335 #endif
1336 
1337         env->cp15.c14_pmevcntr_delta[counter] -=
1338             env->cp15.c14_pmevcntr[counter];
1339     }
1340 }
1341 
1342 void pmu_op_start(CPUARMState *env)
1343 {
1344     unsigned int i;
1345     pmccntr_op_start(env);
1346     for (i = 0; i < pmu_num_counters(env); i++) {
1347         pmevcntr_op_start(env, i);
1348     }
1349 }
1350 
1351 void pmu_op_finish(CPUARMState *env)
1352 {
1353     unsigned int i;
1354     pmccntr_op_finish(env);
1355     for (i = 0; i < pmu_num_counters(env); i++) {
1356         pmevcntr_op_finish(env, i);
1357     }
1358 }
1359 
1360 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1361 {
1362     pmu_op_start(&cpu->env);
1363 }
1364 
1365 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1366 {
1367     pmu_op_finish(&cpu->env);
1368 }
1369 
1370 void arm_pmu_timer_cb(void *opaque)
1371 {
1372     ARMCPU *cpu = opaque;
1373 
1374     /*
1375      * Update all the counter values based on the current underlying counts,
1376      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1377      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1378      * counter may expire.
1379      */
1380     pmu_op_start(&cpu->env);
1381     pmu_op_finish(&cpu->env);
1382 }
1383 
1384 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1385                        uint64_t value)
1386 {
1387     pmu_op_start(env);
1388 
1389     if (value & PMCRC) {
1390         /* The counter has been reset */
1391         env->cp15.c15_ccnt = 0;
1392     }
1393 
1394     if (value & PMCRP) {
1395         unsigned int i;
1396         for (i = 0; i < pmu_num_counters(env); i++) {
1397             env->cp15.c14_pmevcntr[i] = 0;
1398         }
1399     }
1400 
1401     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1402     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1403 
1404     pmu_op_finish(env);
1405 }
1406 
1407 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1408                           uint64_t value)
1409 {
1410     unsigned int i;
1411     for (i = 0; i < pmu_num_counters(env); i++) {
1412         /* Increment a counter's count iff: */
1413         if ((value & (1 << i)) && /* counter's bit is set */
1414                 /* counter is enabled and not filtered */
1415                 pmu_counter_enabled(env, i) &&
1416                 /* counter is SW_INCR */
1417                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1418             pmevcntr_op_start(env, i);
1419 
1420             /*
1421              * Detect if this write causes an overflow since we can't predict
1422              * PMSWINC overflows like we can for other events
1423              */
1424             uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1425 
1426             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1427                 env->cp15.c9_pmovsr |= (1 << i);
1428                 pmu_update_irq(env);
1429             }
1430 
1431             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1432 
1433             pmevcntr_op_finish(env, i);
1434         }
1435     }
1436 }
1437 
1438 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1439 {
1440     uint64_t ret;
1441     pmccntr_op_start(env);
1442     ret = env->cp15.c15_ccnt;
1443     pmccntr_op_finish(env);
1444     return ret;
1445 }
1446 
1447 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1448                          uint64_t value)
1449 {
1450     /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1451      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1452      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1453      * accessed.
1454      */
1455     env->cp15.c9_pmselr = value & 0x1f;
1456 }
1457 
1458 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1459                         uint64_t value)
1460 {
1461     pmccntr_op_start(env);
1462     env->cp15.c15_ccnt = value;
1463     pmccntr_op_finish(env);
1464 }
1465 
1466 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1467                             uint64_t value)
1468 {
1469     uint64_t cur_val = pmccntr_read(env, NULL);
1470 
1471     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1472 }
1473 
1474 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1475                             uint64_t value)
1476 {
1477     pmccntr_op_start(env);
1478     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1479     pmccntr_op_finish(env);
1480 }
1481 
1482 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1483                             uint64_t value)
1484 {
1485     pmccntr_op_start(env);
1486     /* M is not accessible from AArch32 */
1487     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1488         (value & PMCCFILTR);
1489     pmccntr_op_finish(env);
1490 }
1491 
1492 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1493 {
1494     /* M is not visible in AArch32 */
1495     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1496 }
1497 
1498 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1499                             uint64_t value)
1500 {
1501     value &= pmu_counter_mask(env);
1502     env->cp15.c9_pmcnten |= value;
1503 }
1504 
1505 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1506                              uint64_t value)
1507 {
1508     value &= pmu_counter_mask(env);
1509     env->cp15.c9_pmcnten &= ~value;
1510 }
1511 
1512 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1513                          uint64_t value)
1514 {
1515     value &= pmu_counter_mask(env);
1516     env->cp15.c9_pmovsr &= ~value;
1517     pmu_update_irq(env);
1518 }
1519 
1520 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1521                          uint64_t value)
1522 {
1523     value &= pmu_counter_mask(env);
1524     env->cp15.c9_pmovsr |= value;
1525     pmu_update_irq(env);
1526 }
1527 
1528 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1529                              uint64_t value, const uint8_t counter)
1530 {
1531     if (counter == 31) {
1532         pmccfiltr_write(env, ri, value);
1533     } else if (counter < pmu_num_counters(env)) {
1534         pmevcntr_op_start(env, counter);
1535 
1536         /*
1537          * If this counter's event type is changing, store the current
1538          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1539          * pmevcntr_op_finish has the correct baseline when it converts back to
1540          * a delta.
1541          */
1542         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1543             PMXEVTYPER_EVTCOUNT;
1544         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1545         if (old_event != new_event) {
1546             uint64_t count = 0;
1547             if (event_supported(new_event)) {
1548                 uint16_t event_idx = supported_event_map[new_event];
1549                 count = pm_events[event_idx].get_count(env);
1550             }
1551             env->cp15.c14_pmevcntr_delta[counter] = count;
1552         }
1553 
1554         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1555         pmevcntr_op_finish(env, counter);
1556     }
1557     /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1558      * PMSELR value is equal to or greater than the number of implemented
1559      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1560      */
1561 }
1562 
1563 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1564                                const uint8_t counter)
1565 {
1566     if (counter == 31) {
1567         return env->cp15.pmccfiltr_el0;
1568     } else if (counter < pmu_num_counters(env)) {
1569         return env->cp15.c14_pmevtyper[counter];
1570     } else {
1571       /*
1572        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1573        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1574        */
1575         return 0;
1576     }
1577 }
1578 
1579 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1580                               uint64_t value)
1581 {
1582     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1583     pmevtyper_write(env, ri, value, counter);
1584 }
1585 
1586 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1587                                uint64_t value)
1588 {
1589     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1590     env->cp15.c14_pmevtyper[counter] = value;
1591 
1592     /*
1593      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1594      * pmu_op_finish calls when loading saved state for a migration. Because
1595      * we're potentially updating the type of event here, the value written to
1596      * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1597      * different counter type. Therefore, we need to set this value to the
1598      * current count for the counter type we're writing so that pmu_op_finish
1599      * has the correct count for its calculation.
1600      */
1601     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1602     if (event_supported(event)) {
1603         uint16_t event_idx = supported_event_map[event];
1604         env->cp15.c14_pmevcntr_delta[counter] =
1605             pm_events[event_idx].get_count(env);
1606     }
1607 }
1608 
1609 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1610 {
1611     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1612     return pmevtyper_read(env, ri, counter);
1613 }
1614 
1615 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1616                              uint64_t value)
1617 {
1618     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1619 }
1620 
1621 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1622 {
1623     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1624 }
1625 
1626 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1627                              uint64_t value, uint8_t counter)
1628 {
1629     if (counter < pmu_num_counters(env)) {
1630         pmevcntr_op_start(env, counter);
1631         env->cp15.c14_pmevcntr[counter] = value;
1632         pmevcntr_op_finish(env, counter);
1633     }
1634     /*
1635      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1636      * are CONSTRAINED UNPREDICTABLE.
1637      */
1638 }
1639 
1640 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1641                               uint8_t counter)
1642 {
1643     if (counter < pmu_num_counters(env)) {
1644         uint64_t ret;
1645         pmevcntr_op_start(env, counter);
1646         ret = env->cp15.c14_pmevcntr[counter];
1647         pmevcntr_op_finish(env, counter);
1648         return ret;
1649     } else {
1650       /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1651        * are CONSTRAINED UNPREDICTABLE. */
1652         return 0;
1653     }
1654 }
1655 
1656 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1657                              uint64_t value)
1658 {
1659     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1660     pmevcntr_write(env, ri, value, counter);
1661 }
1662 
1663 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1664 {
1665     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1666     return pmevcntr_read(env, ri, counter);
1667 }
1668 
1669 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1670                              uint64_t value)
1671 {
1672     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1673     assert(counter < pmu_num_counters(env));
1674     env->cp15.c14_pmevcntr[counter] = value;
1675     pmevcntr_write(env, ri, value, counter);
1676 }
1677 
1678 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1679 {
1680     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1681     assert(counter < pmu_num_counters(env));
1682     return env->cp15.c14_pmevcntr[counter];
1683 }
1684 
1685 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1686                              uint64_t value)
1687 {
1688     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1689 }
1690 
1691 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1692 {
1693     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1694 }
1695 
1696 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1697                             uint64_t value)
1698 {
1699     if (arm_feature(env, ARM_FEATURE_V8)) {
1700         env->cp15.c9_pmuserenr = value & 0xf;
1701     } else {
1702         env->cp15.c9_pmuserenr = value & 1;
1703     }
1704 }
1705 
1706 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1707                              uint64_t value)
1708 {
1709     /* We have no event counters so only the C bit can be changed */
1710     value &= pmu_counter_mask(env);
1711     env->cp15.c9_pminten |= value;
1712     pmu_update_irq(env);
1713 }
1714 
1715 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1716                              uint64_t value)
1717 {
1718     value &= pmu_counter_mask(env);
1719     env->cp15.c9_pminten &= ~value;
1720     pmu_update_irq(env);
1721 }
1722 
1723 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1724                        uint64_t value)
1725 {
1726     /* Note that even though the AArch64 view of this register has bits
1727      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1728      * architectural requirements for bits which are RES0 only in some
1729      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1730      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1731      */
1732     raw_write(env, ri, value & ~0x1FULL);
1733 }
1734 
1735 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1736 {
1737     /* Begin with base v8.0 state.  */
1738     uint32_t valid_mask = 0x3fff;
1739     ARMCPU *cpu = env_archcpu(env);
1740 
1741     /*
1742      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1743      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1744      * Instead, choose the format based on the mode of EL3.
1745      */
1746     if (arm_el_is_aa64(env, 3)) {
1747         value |= SCR_FW | SCR_AW;      /* RES1 */
1748         valid_mask &= ~SCR_NET;        /* RES0 */
1749 
1750         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1751             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1752             value |= SCR_RW;           /* RAO/WI */
1753         }
1754         if (cpu_isar_feature(aa64_ras, cpu)) {
1755             valid_mask |= SCR_TERR;
1756         }
1757         if (cpu_isar_feature(aa64_lor, cpu)) {
1758             valid_mask |= SCR_TLOR;
1759         }
1760         if (cpu_isar_feature(aa64_pauth, cpu)) {
1761             valid_mask |= SCR_API | SCR_APK;
1762         }
1763         if (cpu_isar_feature(aa64_sel2, cpu)) {
1764             valid_mask |= SCR_EEL2;
1765         }
1766         if (cpu_isar_feature(aa64_mte, cpu)) {
1767             valid_mask |= SCR_ATA;
1768         }
1769         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1770             valid_mask |= SCR_ENSCXT;
1771         }
1772         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1773             valid_mask |= SCR_EASE | SCR_NMEA;
1774         }
1775     } else {
1776         valid_mask &= ~(SCR_RW | SCR_ST);
1777         if (cpu_isar_feature(aa32_ras, cpu)) {
1778             valid_mask |= SCR_TERR;
1779         }
1780     }
1781 
1782     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1783         valid_mask &= ~SCR_HCE;
1784 
1785         /* On ARMv7, SMD (or SCD as it is called in v7) is only
1786          * supported if EL2 exists. The bit is UNK/SBZP when
1787          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1788          * when EL2 is unavailable.
1789          * On ARMv8, this bit is always available.
1790          */
1791         if (arm_feature(env, ARM_FEATURE_V7) &&
1792             !arm_feature(env, ARM_FEATURE_V8)) {
1793             valid_mask &= ~SCR_SMD;
1794         }
1795     }
1796 
1797     /* Clear all-context RES0 bits.  */
1798     value &= valid_mask;
1799     raw_write(env, ri, value);
1800 }
1801 
1802 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1803 {
1804     /*
1805      * scr_write will set the RES1 bits on an AArch64-only CPU.
1806      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1807      */
1808     scr_write(env, ri, 0);
1809 }
1810 
1811 static CPAccessResult access_aa64_tid2(CPUARMState *env,
1812                                        const ARMCPRegInfo *ri,
1813                                        bool isread)
1814 {
1815     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1816         return CP_ACCESS_TRAP_EL2;
1817     }
1818 
1819     return CP_ACCESS_OK;
1820 }
1821 
1822 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1823 {
1824     ARMCPU *cpu = env_archcpu(env);
1825 
1826     /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1827      * bank
1828      */
1829     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1830                                         ri->secure & ARM_CP_SECSTATE_S);
1831 
1832     return cpu->ccsidr[index];
1833 }
1834 
1835 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1836                          uint64_t value)
1837 {
1838     raw_write(env, ri, value & 0xf);
1839 }
1840 
1841 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1842 {
1843     CPUState *cs = env_cpu(env);
1844     bool el1 = arm_current_el(env) == 1;
1845     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1846     uint64_t ret = 0;
1847 
1848     if (hcr_el2 & HCR_IMO) {
1849         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1850             ret |= CPSR_I;
1851         }
1852     } else {
1853         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1854             ret |= CPSR_I;
1855         }
1856     }
1857 
1858     if (hcr_el2 & HCR_FMO) {
1859         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1860             ret |= CPSR_F;
1861         }
1862     } else {
1863         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1864             ret |= CPSR_F;
1865         }
1866     }
1867 
1868     if (hcr_el2 & HCR_AMO) {
1869         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1870             ret |= CPSR_A;
1871         }
1872     }
1873 
1874     return ret;
1875 }
1876 
1877 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1878                                        bool isread)
1879 {
1880     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1881         return CP_ACCESS_TRAP_EL2;
1882     }
1883 
1884     return CP_ACCESS_OK;
1885 }
1886 
1887 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1888                                        bool isread)
1889 {
1890     if (arm_feature(env, ARM_FEATURE_V8)) {
1891         return access_aa64_tid1(env, ri, isread);
1892     }
1893 
1894     return CP_ACCESS_OK;
1895 }
1896 
1897 static const ARMCPRegInfo v7_cp_reginfo[] = {
1898     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1899     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1900       .access = PL1_W, .type = ARM_CP_NOP },
1901     /* Performance monitors are implementation defined in v7,
1902      * but with an ARM recommended set of registers, which we
1903      * follow.
1904      *
1905      * Performance registers fall into three categories:
1906      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1907      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1908      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1909      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1910      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1911      */
1912     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1913       .access = PL0_RW, .type = ARM_CP_ALIAS,
1914       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1915       .writefn = pmcntenset_write,
1916       .accessfn = pmreg_access,
1917       .raw_writefn = raw_write },
1918     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1919       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1920       .access = PL0_RW, .accessfn = pmreg_access,
1921       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1922       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1923     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1924       .access = PL0_RW,
1925       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1926       .accessfn = pmreg_access,
1927       .writefn = pmcntenclr_write,
1928       .type = ARM_CP_ALIAS },
1929     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1930       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1931       .access = PL0_RW, .accessfn = pmreg_access,
1932       .type = ARM_CP_ALIAS,
1933       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1934       .writefn = pmcntenclr_write },
1935     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1936       .access = PL0_RW, .type = ARM_CP_IO,
1937       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1938       .accessfn = pmreg_access,
1939       .writefn = pmovsr_write,
1940       .raw_writefn = raw_write },
1941     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1942       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1943       .access = PL0_RW, .accessfn = pmreg_access,
1944       .type = ARM_CP_ALIAS | ARM_CP_IO,
1945       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1946       .writefn = pmovsr_write,
1947       .raw_writefn = raw_write },
1948     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1949       .access = PL0_W, .accessfn = pmreg_access_swinc,
1950       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1951       .writefn = pmswinc_write },
1952     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1953       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1954       .access = PL0_W, .accessfn = pmreg_access_swinc,
1955       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1956       .writefn = pmswinc_write },
1957     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1958       .access = PL0_RW, .type = ARM_CP_ALIAS,
1959       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1960       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1961       .raw_writefn = raw_write},
1962     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1963       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1964       .access = PL0_RW, .accessfn = pmreg_access_selr,
1965       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1966       .writefn = pmselr_write, .raw_writefn = raw_write, },
1967     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1968       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1969       .readfn = pmccntr_read, .writefn = pmccntr_write32,
1970       .accessfn = pmreg_access_ccntr },
1971     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1972       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1973       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1974       .type = ARM_CP_IO,
1975       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1976       .readfn = pmccntr_read, .writefn = pmccntr_write,
1977       .raw_readfn = raw_read, .raw_writefn = raw_write, },
1978     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1979       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1980       .access = PL0_RW, .accessfn = pmreg_access,
1981       .type = ARM_CP_ALIAS | ARM_CP_IO,
1982       .resetvalue = 0, },
1983     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1984       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1985       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1986       .access = PL0_RW, .accessfn = pmreg_access,
1987       .type = ARM_CP_IO,
1988       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1989       .resetvalue = 0, },
1990     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1991       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1992       .accessfn = pmreg_access,
1993       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1994     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1995       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1996       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1997       .accessfn = pmreg_access,
1998       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1999     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2000       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2001       .accessfn = pmreg_access_xevcntr,
2002       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2003     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2004       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2005       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2006       .accessfn = pmreg_access_xevcntr,
2007       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2008     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2009       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2010       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2011       .resetvalue = 0,
2012       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2013     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2014       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2015       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2016       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2017       .resetvalue = 0,
2018       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2019     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2020       .access = PL1_RW, .accessfn = access_tpm,
2021       .type = ARM_CP_ALIAS | ARM_CP_IO,
2022       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2023       .resetvalue = 0,
2024       .writefn = pmintenset_write, .raw_writefn = raw_write },
2025     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2026       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2027       .access = PL1_RW, .accessfn = access_tpm,
2028       .type = ARM_CP_IO,
2029       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2030       .writefn = pmintenset_write, .raw_writefn = raw_write,
2031       .resetvalue = 0x0 },
2032     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2033       .access = PL1_RW, .accessfn = access_tpm,
2034       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2035       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2036       .writefn = pmintenclr_write, },
2037     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2038       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2039       .access = PL1_RW, .accessfn = access_tpm,
2040       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2041       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2042       .writefn = pmintenclr_write },
2043     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2044       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2045       .access = PL1_R,
2046       .accessfn = access_aa64_tid2,
2047       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2048     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2049       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2050       .access = PL1_RW,
2051       .accessfn = access_aa64_tid2,
2052       .writefn = csselr_write, .resetvalue = 0,
2053       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2054                              offsetof(CPUARMState, cp15.csselr_ns) } },
2055     /* Auxiliary ID register: this actually has an IMPDEF value but for now
2056      * just RAZ for all cores:
2057      */
2058     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2059       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2060       .access = PL1_R, .type = ARM_CP_CONST,
2061       .accessfn = access_aa64_tid1,
2062       .resetvalue = 0 },
2063     /* Auxiliary fault status registers: these also are IMPDEF, and we
2064      * choose to RAZ/WI for all cores.
2065      */
2066     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2067       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2068       .access = PL1_RW, .accessfn = access_tvm_trvm,
2069       .type = ARM_CP_CONST, .resetvalue = 0 },
2070     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2071       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2072       .access = PL1_RW, .accessfn = access_tvm_trvm,
2073       .type = ARM_CP_CONST, .resetvalue = 0 },
2074     /* MAIR can just read-as-written because we don't implement caches
2075      * and so don't need to care about memory attributes.
2076      */
2077     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2078       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2079       .access = PL1_RW, .accessfn = access_tvm_trvm,
2080       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2081       .resetvalue = 0 },
2082     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2083       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2084       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2085       .resetvalue = 0 },
2086     /* For non-long-descriptor page tables these are PRRR and NMRR;
2087      * regardless they still act as reads-as-written for QEMU.
2088      */
2089      /* MAIR0/1 are defined separately from their 64-bit counterpart which
2090       * allows them to assign the correct fieldoffset based on the endianness
2091       * handled in the field definitions.
2092       */
2093     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2094       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2095       .access = PL1_RW, .accessfn = access_tvm_trvm,
2096       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2097                              offsetof(CPUARMState, cp15.mair0_ns) },
2098       .resetfn = arm_cp_reset_ignore },
2099     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2100       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2101       .access = PL1_RW, .accessfn = access_tvm_trvm,
2102       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2103                              offsetof(CPUARMState, cp15.mair1_ns) },
2104       .resetfn = arm_cp_reset_ignore },
2105     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2106       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2107       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2108     /* 32 bit ITLB invalidates */
2109     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2110       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2111       .writefn = tlbiall_write },
2112     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2113       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2114       .writefn = tlbimva_write },
2115     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2116       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2117       .writefn = tlbiasid_write },
2118     /* 32 bit DTLB invalidates */
2119     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2120       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2121       .writefn = tlbiall_write },
2122     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2123       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2124       .writefn = tlbimva_write },
2125     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2126       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2127       .writefn = tlbiasid_write },
2128     /* 32 bit TLB invalidates */
2129     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2130       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2131       .writefn = tlbiall_write },
2132     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2133       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2134       .writefn = tlbimva_write },
2135     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2136       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2137       .writefn = tlbiasid_write },
2138     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2139       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2140       .writefn = tlbimvaa_write },
2141 };
2142 
2143 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2144     /* 32 bit TLB invalidates, Inner Shareable */
2145     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2146       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2147       .writefn = tlbiall_is_write },
2148     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2149       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2150       .writefn = tlbimva_is_write },
2151     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2152       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2153       .writefn = tlbiasid_is_write },
2154     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2155       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2156       .writefn = tlbimvaa_is_write },
2157 };
2158 
2159 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2160     /* PMOVSSET is not implemented in v7 before v7ve */
2161     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2162       .access = PL0_RW, .accessfn = pmreg_access,
2163       .type = ARM_CP_ALIAS | ARM_CP_IO,
2164       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2165       .writefn = pmovsset_write,
2166       .raw_writefn = raw_write },
2167     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2168       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2169       .access = PL0_RW, .accessfn = pmreg_access,
2170       .type = ARM_CP_ALIAS | ARM_CP_IO,
2171       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2172       .writefn = pmovsset_write,
2173       .raw_writefn = raw_write },
2174 };
2175 
2176 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2177                         uint64_t value)
2178 {
2179     value &= 1;
2180     env->teecr = value;
2181 }
2182 
2183 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2184                                    bool isread)
2185 {
2186     /*
2187      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2188      * at all, so we don't need to check whether we're v8A.
2189      */
2190     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2191         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2192         return CP_ACCESS_TRAP_EL2;
2193     }
2194     return CP_ACCESS_OK;
2195 }
2196 
2197 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2198                                     bool isread)
2199 {
2200     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2201         return CP_ACCESS_TRAP;
2202     }
2203     return teecr_access(env, ri, isread);
2204 }
2205 
2206 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2207     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2208       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2209       .resetvalue = 0,
2210       .writefn = teecr_write, .accessfn = teecr_access },
2211     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2212       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2213       .accessfn = teehbr_access, .resetvalue = 0 },
2214 };
2215 
2216 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2217     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2218       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2219       .access = PL0_RW,
2220       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2221     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2222       .access = PL0_RW,
2223       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2224                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2225       .resetfn = arm_cp_reset_ignore },
2226     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2227       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2228       .access = PL0_R|PL1_W,
2229       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2230       .resetvalue = 0},
2231     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2232       .access = PL0_R|PL1_W,
2233       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2234                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2235       .resetfn = arm_cp_reset_ignore },
2236     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2237       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2238       .access = PL1_RW,
2239       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2240     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2241       .access = PL1_RW,
2242       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2243                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2244       .resetvalue = 0 },
2245 };
2246 
2247 #ifndef CONFIG_USER_ONLY
2248 
2249 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2250                                        bool isread)
2251 {
2252     /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2253      * Writable only at the highest implemented exception level.
2254      */
2255     int el = arm_current_el(env);
2256     uint64_t hcr;
2257     uint32_t cntkctl;
2258 
2259     switch (el) {
2260     case 0:
2261         hcr = arm_hcr_el2_eff(env);
2262         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2263             cntkctl = env->cp15.cnthctl_el2;
2264         } else {
2265             cntkctl = env->cp15.c14_cntkctl;
2266         }
2267         if (!extract32(cntkctl, 0, 2)) {
2268             return CP_ACCESS_TRAP;
2269         }
2270         break;
2271     case 1:
2272         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2273             arm_is_secure_below_el3(env)) {
2274             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2275             return CP_ACCESS_TRAP_UNCATEGORIZED;
2276         }
2277         break;
2278     case 2:
2279     case 3:
2280         break;
2281     }
2282 
2283     if (!isread && el < arm_highest_el(env)) {
2284         return CP_ACCESS_TRAP_UNCATEGORIZED;
2285     }
2286 
2287     return CP_ACCESS_OK;
2288 }
2289 
2290 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2291                                         bool isread)
2292 {
2293     unsigned int cur_el = arm_current_el(env);
2294     bool has_el2 = arm_is_el2_enabled(env);
2295     uint64_t hcr = arm_hcr_el2_eff(env);
2296 
2297     switch (cur_el) {
2298     case 0:
2299         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2300         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2301             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2302                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2303         }
2304 
2305         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2306         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2307             return CP_ACCESS_TRAP;
2308         }
2309 
2310         /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2311         if (hcr & HCR_E2H) {
2312             if (timeridx == GTIMER_PHYS &&
2313                 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2314                 return CP_ACCESS_TRAP_EL2;
2315             }
2316         } else {
2317             /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2318             if (has_el2 && timeridx == GTIMER_PHYS &&
2319                 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2320                 return CP_ACCESS_TRAP_EL2;
2321             }
2322         }
2323         break;
2324 
2325     case 1:
2326         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2327         if (has_el2 && timeridx == GTIMER_PHYS &&
2328             (hcr & HCR_E2H
2329              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2330              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2331             return CP_ACCESS_TRAP_EL2;
2332         }
2333         break;
2334     }
2335     return CP_ACCESS_OK;
2336 }
2337 
2338 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2339                                       bool isread)
2340 {
2341     unsigned int cur_el = arm_current_el(env);
2342     bool has_el2 = arm_is_el2_enabled(env);
2343     uint64_t hcr = arm_hcr_el2_eff(env);
2344 
2345     switch (cur_el) {
2346     case 0:
2347         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2348             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2349             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2350                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2351         }
2352 
2353         /*
2354          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2355          * EL0 if EL0[PV]TEN is zero.
2356          */
2357         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2358             return CP_ACCESS_TRAP;
2359         }
2360         /* fall through */
2361 
2362     case 1:
2363         if (has_el2 && timeridx == GTIMER_PHYS) {
2364             if (hcr & HCR_E2H) {
2365                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2366                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2367                     return CP_ACCESS_TRAP_EL2;
2368                 }
2369             } else {
2370                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2371                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2372                     return CP_ACCESS_TRAP_EL2;
2373                 }
2374             }
2375         }
2376         break;
2377     }
2378     return CP_ACCESS_OK;
2379 }
2380 
2381 static CPAccessResult gt_pct_access(CPUARMState *env,
2382                                     const ARMCPRegInfo *ri,
2383                                     bool isread)
2384 {
2385     return gt_counter_access(env, GTIMER_PHYS, isread);
2386 }
2387 
2388 static CPAccessResult gt_vct_access(CPUARMState *env,
2389                                     const ARMCPRegInfo *ri,
2390                                     bool isread)
2391 {
2392     return gt_counter_access(env, GTIMER_VIRT, isread);
2393 }
2394 
2395 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2396                                        bool isread)
2397 {
2398     return gt_timer_access(env, GTIMER_PHYS, isread);
2399 }
2400 
2401 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2402                                        bool isread)
2403 {
2404     return gt_timer_access(env, GTIMER_VIRT, isread);
2405 }
2406 
2407 static CPAccessResult gt_stimer_access(CPUARMState *env,
2408                                        const ARMCPRegInfo *ri,
2409                                        bool isread)
2410 {
2411     /* The AArch64 register view of the secure physical timer is
2412      * always accessible from EL3, and configurably accessible from
2413      * Secure EL1.
2414      */
2415     switch (arm_current_el(env)) {
2416     case 1:
2417         if (!arm_is_secure(env)) {
2418             return CP_ACCESS_TRAP;
2419         }
2420         if (!(env->cp15.scr_el3 & SCR_ST)) {
2421             return CP_ACCESS_TRAP_EL3;
2422         }
2423         return CP_ACCESS_OK;
2424     case 0:
2425     case 2:
2426         return CP_ACCESS_TRAP;
2427     case 3:
2428         return CP_ACCESS_OK;
2429     default:
2430         g_assert_not_reached();
2431     }
2432 }
2433 
2434 static uint64_t gt_get_countervalue(CPUARMState *env)
2435 {
2436     ARMCPU *cpu = env_archcpu(env);
2437 
2438     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2439 }
2440 
2441 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2442 {
2443     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2444 
2445     if (gt->ctl & 1) {
2446         /* Timer enabled: calculate and set current ISTATUS, irq, and
2447          * reset timer to when ISTATUS next has to change
2448          */
2449         uint64_t offset = timeridx == GTIMER_VIRT ?
2450                                       cpu->env.cp15.cntvoff_el2 : 0;
2451         uint64_t count = gt_get_countervalue(&cpu->env);
2452         /* Note that this must be unsigned 64 bit arithmetic: */
2453         int istatus = count - offset >= gt->cval;
2454         uint64_t nexttick;
2455         int irqstate;
2456 
2457         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2458 
2459         irqstate = (istatus && !(gt->ctl & 2));
2460         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2461 
2462         if (istatus) {
2463             /* Next transition is when count rolls back over to zero */
2464             nexttick = UINT64_MAX;
2465         } else {
2466             /* Next transition is when we hit cval */
2467             nexttick = gt->cval + offset;
2468         }
2469         /* Note that the desired next expiry time might be beyond the
2470          * signed-64-bit range of a QEMUTimer -- in this case we just
2471          * set the timer for as far in the future as possible. When the
2472          * timer expires we will reset the timer for any remaining period.
2473          */
2474         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2475             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2476         } else {
2477             timer_mod(cpu->gt_timer[timeridx], nexttick);
2478         }
2479         trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2480     } else {
2481         /* Timer disabled: ISTATUS and timer output always clear */
2482         gt->ctl &= ~4;
2483         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2484         timer_del(cpu->gt_timer[timeridx]);
2485         trace_arm_gt_recalc_disabled(timeridx);
2486     }
2487 }
2488 
2489 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2490                            int timeridx)
2491 {
2492     ARMCPU *cpu = env_archcpu(env);
2493 
2494     timer_del(cpu->gt_timer[timeridx]);
2495 }
2496 
2497 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2498 {
2499     return gt_get_countervalue(env);
2500 }
2501 
2502 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2503 {
2504     uint64_t hcr;
2505 
2506     switch (arm_current_el(env)) {
2507     case 2:
2508         hcr = arm_hcr_el2_eff(env);
2509         if (hcr & HCR_E2H) {
2510             return 0;
2511         }
2512         break;
2513     case 0:
2514         hcr = arm_hcr_el2_eff(env);
2515         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2516             return 0;
2517         }
2518         break;
2519     }
2520 
2521     return env->cp15.cntvoff_el2;
2522 }
2523 
2524 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2525 {
2526     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2527 }
2528 
2529 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2530                           int timeridx,
2531                           uint64_t value)
2532 {
2533     trace_arm_gt_cval_write(timeridx, value);
2534     env->cp15.c14_timer[timeridx].cval = value;
2535     gt_recalc_timer(env_archcpu(env), timeridx);
2536 }
2537 
2538 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2539                              int timeridx)
2540 {
2541     uint64_t offset = 0;
2542 
2543     switch (timeridx) {
2544     case GTIMER_VIRT:
2545     case GTIMER_HYPVIRT:
2546         offset = gt_virt_cnt_offset(env);
2547         break;
2548     }
2549 
2550     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2551                       (gt_get_countervalue(env) - offset));
2552 }
2553 
2554 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2555                           int timeridx,
2556                           uint64_t value)
2557 {
2558     uint64_t offset = 0;
2559 
2560     switch (timeridx) {
2561     case GTIMER_VIRT:
2562     case GTIMER_HYPVIRT:
2563         offset = gt_virt_cnt_offset(env);
2564         break;
2565     }
2566 
2567     trace_arm_gt_tval_write(timeridx, value);
2568     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2569                                          sextract64(value, 0, 32);
2570     gt_recalc_timer(env_archcpu(env), timeridx);
2571 }
2572 
2573 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2574                          int timeridx,
2575                          uint64_t value)
2576 {
2577     ARMCPU *cpu = env_archcpu(env);
2578     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2579 
2580     trace_arm_gt_ctl_write(timeridx, value);
2581     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2582     if ((oldval ^ value) & 1) {
2583         /* Enable toggled */
2584         gt_recalc_timer(cpu, timeridx);
2585     } else if ((oldval ^ value) & 2) {
2586         /* IMASK toggled: don't need to recalculate,
2587          * just set the interrupt line based on ISTATUS
2588          */
2589         int irqstate = (oldval & 4) && !(value & 2);
2590 
2591         trace_arm_gt_imask_toggle(timeridx, irqstate);
2592         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2593     }
2594 }
2595 
2596 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2597 {
2598     gt_timer_reset(env, ri, GTIMER_PHYS);
2599 }
2600 
2601 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2602                                uint64_t value)
2603 {
2604     gt_cval_write(env, ri, GTIMER_PHYS, value);
2605 }
2606 
2607 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2608 {
2609     return gt_tval_read(env, ri, GTIMER_PHYS);
2610 }
2611 
2612 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2613                                uint64_t value)
2614 {
2615     gt_tval_write(env, ri, GTIMER_PHYS, value);
2616 }
2617 
2618 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2619                               uint64_t value)
2620 {
2621     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2622 }
2623 
2624 static int gt_phys_redir_timeridx(CPUARMState *env)
2625 {
2626     switch (arm_mmu_idx(env)) {
2627     case ARMMMUIdx_E20_0:
2628     case ARMMMUIdx_E20_2:
2629     case ARMMMUIdx_E20_2_PAN:
2630     case ARMMMUIdx_SE20_0:
2631     case ARMMMUIdx_SE20_2:
2632     case ARMMMUIdx_SE20_2_PAN:
2633         return GTIMER_HYP;
2634     default:
2635         return GTIMER_PHYS;
2636     }
2637 }
2638 
2639 static int gt_virt_redir_timeridx(CPUARMState *env)
2640 {
2641     switch (arm_mmu_idx(env)) {
2642     case ARMMMUIdx_E20_0:
2643     case ARMMMUIdx_E20_2:
2644     case ARMMMUIdx_E20_2_PAN:
2645     case ARMMMUIdx_SE20_0:
2646     case ARMMMUIdx_SE20_2:
2647     case ARMMMUIdx_SE20_2_PAN:
2648         return GTIMER_HYPVIRT;
2649     default:
2650         return GTIMER_VIRT;
2651     }
2652 }
2653 
2654 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2655                                         const ARMCPRegInfo *ri)
2656 {
2657     int timeridx = gt_phys_redir_timeridx(env);
2658     return env->cp15.c14_timer[timeridx].cval;
2659 }
2660 
2661 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2662                                      uint64_t value)
2663 {
2664     int timeridx = gt_phys_redir_timeridx(env);
2665     gt_cval_write(env, ri, timeridx, value);
2666 }
2667 
2668 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2669                                         const ARMCPRegInfo *ri)
2670 {
2671     int timeridx = gt_phys_redir_timeridx(env);
2672     return gt_tval_read(env, ri, timeridx);
2673 }
2674 
2675 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2676                                      uint64_t value)
2677 {
2678     int timeridx = gt_phys_redir_timeridx(env);
2679     gt_tval_write(env, ri, timeridx, value);
2680 }
2681 
2682 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2683                                        const ARMCPRegInfo *ri)
2684 {
2685     int timeridx = gt_phys_redir_timeridx(env);
2686     return env->cp15.c14_timer[timeridx].ctl;
2687 }
2688 
2689 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2690                                     uint64_t value)
2691 {
2692     int timeridx = gt_phys_redir_timeridx(env);
2693     gt_ctl_write(env, ri, timeridx, value);
2694 }
2695 
2696 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2697 {
2698     gt_timer_reset(env, ri, GTIMER_VIRT);
2699 }
2700 
2701 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2702                                uint64_t value)
2703 {
2704     gt_cval_write(env, ri, GTIMER_VIRT, value);
2705 }
2706 
2707 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2708 {
2709     return gt_tval_read(env, ri, GTIMER_VIRT);
2710 }
2711 
2712 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2713                                uint64_t value)
2714 {
2715     gt_tval_write(env, ri, GTIMER_VIRT, value);
2716 }
2717 
2718 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2719                               uint64_t value)
2720 {
2721     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2722 }
2723 
2724 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2725                               uint64_t value)
2726 {
2727     ARMCPU *cpu = env_archcpu(env);
2728 
2729     trace_arm_gt_cntvoff_write(value);
2730     raw_write(env, ri, value);
2731     gt_recalc_timer(cpu, GTIMER_VIRT);
2732 }
2733 
2734 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2735                                         const ARMCPRegInfo *ri)
2736 {
2737     int timeridx = gt_virt_redir_timeridx(env);
2738     return env->cp15.c14_timer[timeridx].cval;
2739 }
2740 
2741 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2742                                      uint64_t value)
2743 {
2744     int timeridx = gt_virt_redir_timeridx(env);
2745     gt_cval_write(env, ri, timeridx, value);
2746 }
2747 
2748 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2749                                         const ARMCPRegInfo *ri)
2750 {
2751     int timeridx = gt_virt_redir_timeridx(env);
2752     return gt_tval_read(env, ri, timeridx);
2753 }
2754 
2755 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2756                                      uint64_t value)
2757 {
2758     int timeridx = gt_virt_redir_timeridx(env);
2759     gt_tval_write(env, ri, timeridx, value);
2760 }
2761 
2762 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2763                                        const ARMCPRegInfo *ri)
2764 {
2765     int timeridx = gt_virt_redir_timeridx(env);
2766     return env->cp15.c14_timer[timeridx].ctl;
2767 }
2768 
2769 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2770                                     uint64_t value)
2771 {
2772     int timeridx = gt_virt_redir_timeridx(env);
2773     gt_ctl_write(env, ri, timeridx, value);
2774 }
2775 
2776 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2777 {
2778     gt_timer_reset(env, ri, GTIMER_HYP);
2779 }
2780 
2781 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2782                               uint64_t value)
2783 {
2784     gt_cval_write(env, ri, GTIMER_HYP, value);
2785 }
2786 
2787 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2788 {
2789     return gt_tval_read(env, ri, GTIMER_HYP);
2790 }
2791 
2792 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2793                               uint64_t value)
2794 {
2795     gt_tval_write(env, ri, GTIMER_HYP, value);
2796 }
2797 
2798 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2799                               uint64_t value)
2800 {
2801     gt_ctl_write(env, ri, GTIMER_HYP, value);
2802 }
2803 
2804 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2805 {
2806     gt_timer_reset(env, ri, GTIMER_SEC);
2807 }
2808 
2809 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2810                               uint64_t value)
2811 {
2812     gt_cval_write(env, ri, GTIMER_SEC, value);
2813 }
2814 
2815 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2816 {
2817     return gt_tval_read(env, ri, GTIMER_SEC);
2818 }
2819 
2820 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2821                               uint64_t value)
2822 {
2823     gt_tval_write(env, ri, GTIMER_SEC, value);
2824 }
2825 
2826 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2827                               uint64_t value)
2828 {
2829     gt_ctl_write(env, ri, GTIMER_SEC, value);
2830 }
2831 
2832 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2833 {
2834     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2835 }
2836 
2837 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2838                              uint64_t value)
2839 {
2840     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2841 }
2842 
2843 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2844 {
2845     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2846 }
2847 
2848 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2849                              uint64_t value)
2850 {
2851     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2852 }
2853 
2854 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2855                             uint64_t value)
2856 {
2857     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2858 }
2859 
2860 void arm_gt_ptimer_cb(void *opaque)
2861 {
2862     ARMCPU *cpu = opaque;
2863 
2864     gt_recalc_timer(cpu, GTIMER_PHYS);
2865 }
2866 
2867 void arm_gt_vtimer_cb(void *opaque)
2868 {
2869     ARMCPU *cpu = opaque;
2870 
2871     gt_recalc_timer(cpu, GTIMER_VIRT);
2872 }
2873 
2874 void arm_gt_htimer_cb(void *opaque)
2875 {
2876     ARMCPU *cpu = opaque;
2877 
2878     gt_recalc_timer(cpu, GTIMER_HYP);
2879 }
2880 
2881 void arm_gt_stimer_cb(void *opaque)
2882 {
2883     ARMCPU *cpu = opaque;
2884 
2885     gt_recalc_timer(cpu, GTIMER_SEC);
2886 }
2887 
2888 void arm_gt_hvtimer_cb(void *opaque)
2889 {
2890     ARMCPU *cpu = opaque;
2891 
2892     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2893 }
2894 
2895 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2896 {
2897     ARMCPU *cpu = env_archcpu(env);
2898 
2899     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2900 }
2901 
2902 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2903     /* Note that CNTFRQ is purely reads-as-written for the benefit
2904      * of software; writing it doesn't actually change the timer frequency.
2905      * Our reset value matches the fixed frequency we implement the timer at.
2906      */
2907     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2908       .type = ARM_CP_ALIAS,
2909       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2910       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2911     },
2912     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2913       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2914       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2915       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2916       .resetfn = arm_gt_cntfrq_reset,
2917     },
2918     /* overall control: mostly access permissions */
2919     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2920       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2921       .access = PL1_RW,
2922       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2923       .resetvalue = 0,
2924     },
2925     /* per-timer control */
2926     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2927       .secure = ARM_CP_SECSTATE_NS,
2928       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2929       .accessfn = gt_ptimer_access,
2930       .fieldoffset = offsetoflow32(CPUARMState,
2931                                    cp15.c14_timer[GTIMER_PHYS].ctl),
2932       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2933       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2934     },
2935     { .name = "CNTP_CTL_S",
2936       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2937       .secure = ARM_CP_SECSTATE_S,
2938       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2939       .accessfn = gt_ptimer_access,
2940       .fieldoffset = offsetoflow32(CPUARMState,
2941                                    cp15.c14_timer[GTIMER_SEC].ctl),
2942       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2943     },
2944     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2945       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2946       .type = ARM_CP_IO, .access = PL0_RW,
2947       .accessfn = gt_ptimer_access,
2948       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2949       .resetvalue = 0,
2950       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2951       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2952     },
2953     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2954       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2955       .accessfn = gt_vtimer_access,
2956       .fieldoffset = offsetoflow32(CPUARMState,
2957                                    cp15.c14_timer[GTIMER_VIRT].ctl),
2958       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2959       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2960     },
2961     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2962       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2963       .type = ARM_CP_IO, .access = PL0_RW,
2964       .accessfn = gt_vtimer_access,
2965       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2966       .resetvalue = 0,
2967       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2968       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2969     },
2970     /* TimerValue views: a 32 bit downcounting view of the underlying state */
2971     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2972       .secure = ARM_CP_SECSTATE_NS,
2973       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2974       .accessfn = gt_ptimer_access,
2975       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2976     },
2977     { .name = "CNTP_TVAL_S",
2978       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2979       .secure = ARM_CP_SECSTATE_S,
2980       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2981       .accessfn = gt_ptimer_access,
2982       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2983     },
2984     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2985       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2986       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2987       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2988       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2989     },
2990     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2991       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2992       .accessfn = gt_vtimer_access,
2993       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
2994     },
2995     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2996       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2997       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2998       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2999       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3000     },
3001     /* The counter itself */
3002     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3003       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3004       .accessfn = gt_pct_access,
3005       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3006     },
3007     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3008       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3009       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3010       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3011     },
3012     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3013       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3014       .accessfn = gt_vct_access,
3015       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3016     },
3017     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3018       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3019       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3020       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3021     },
3022     /* Comparison value, indicating when the timer goes off */
3023     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3024       .secure = ARM_CP_SECSTATE_NS,
3025       .access = PL0_RW,
3026       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3027       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3028       .accessfn = gt_ptimer_access,
3029       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3030       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3031     },
3032     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3033       .secure = ARM_CP_SECSTATE_S,
3034       .access = PL0_RW,
3035       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3036       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3037       .accessfn = gt_ptimer_access,
3038       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3039     },
3040     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3041       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3042       .access = PL0_RW,
3043       .type = ARM_CP_IO,
3044       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3045       .resetvalue = 0, .accessfn = gt_ptimer_access,
3046       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3047       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3048     },
3049     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3050       .access = PL0_RW,
3051       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3052       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3053       .accessfn = gt_vtimer_access,
3054       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3055       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3056     },
3057     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3058       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3059       .access = PL0_RW,
3060       .type = ARM_CP_IO,
3061       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3062       .resetvalue = 0, .accessfn = gt_vtimer_access,
3063       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3064       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3065     },
3066     /* Secure timer -- this is actually restricted to only EL3
3067      * and configurably Secure-EL1 via the accessfn.
3068      */
3069     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3070       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3071       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3072       .accessfn = gt_stimer_access,
3073       .readfn = gt_sec_tval_read,
3074       .writefn = gt_sec_tval_write,
3075       .resetfn = gt_sec_timer_reset,
3076     },
3077     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3078       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3079       .type = ARM_CP_IO, .access = PL1_RW,
3080       .accessfn = gt_stimer_access,
3081       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3082       .resetvalue = 0,
3083       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3084     },
3085     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3086       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3087       .type = ARM_CP_IO, .access = PL1_RW,
3088       .accessfn = gt_stimer_access,
3089       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3090       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3091     },
3092 };
3093 
3094 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3095                                  bool isread)
3096 {
3097     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3098         return CP_ACCESS_TRAP;
3099     }
3100     return CP_ACCESS_OK;
3101 }
3102 
3103 #else
3104 
3105 /* In user-mode most of the generic timer registers are inaccessible
3106  * however modern kernels (4.12+) allow access to cntvct_el0
3107  */
3108 
3109 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3110 {
3111     ARMCPU *cpu = env_archcpu(env);
3112 
3113     /* Currently we have no support for QEMUTimer in linux-user so we
3114      * can't call gt_get_countervalue(env), instead we directly
3115      * call the lower level functions.
3116      */
3117     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3118 }
3119 
3120 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3121     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3122       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3123       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3124       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3125       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3126     },
3127     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3128       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3129       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3130       .readfn = gt_virt_cnt_read,
3131     },
3132 };
3133 
3134 #endif
3135 
3136 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3137 {
3138     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3139         raw_write(env, ri, value);
3140     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3141         raw_write(env, ri, value & 0xfffff6ff);
3142     } else {
3143         raw_write(env, ri, value & 0xfffff1ff);
3144     }
3145 }
3146 
3147 #ifndef CONFIG_USER_ONLY
3148 /* get_phys_addr() isn't present for user-mode-only targets */
3149 
3150 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3151                                  bool isread)
3152 {
3153     if (ri->opc2 & 4) {
3154         /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3155          * Secure EL1 (which can only happen if EL3 is AArch64).
3156          * They are simply UNDEF if executed from NS EL1.
3157          * They function normally from EL2 or EL3.
3158          */
3159         if (arm_current_el(env) == 1) {
3160             if (arm_is_secure_below_el3(env)) {
3161                 if (env->cp15.scr_el3 & SCR_EEL2) {
3162                     return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3163                 }
3164                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3165             }
3166             return CP_ACCESS_TRAP_UNCATEGORIZED;
3167         }
3168     }
3169     return CP_ACCESS_OK;
3170 }
3171 
3172 #ifdef CONFIG_TCG
3173 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3174                              MMUAccessType access_type, ARMMMUIdx mmu_idx)
3175 {
3176     hwaddr phys_addr;
3177     target_ulong page_size;
3178     int prot;
3179     bool ret;
3180     uint64_t par64;
3181     bool format64 = false;
3182     MemTxAttrs attrs = {};
3183     ARMMMUFaultInfo fi = {};
3184     ARMCacheAttrs cacheattrs = {};
3185 
3186     ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
3187                         &prot, &page_size, &fi, &cacheattrs);
3188 
3189     /*
3190      * ATS operations only do S1 or S1+S2 translations, so we never
3191      * have to deal with the ARMCacheAttrs format for S2 only.
3192      */
3193     assert(!cacheattrs.is_s2_format);
3194 
3195     if (ret) {
3196         /*
3197          * Some kinds of translation fault must cause exceptions rather
3198          * than being reported in the PAR.
3199          */
3200         int current_el = arm_current_el(env);
3201         int target_el;
3202         uint32_t syn, fsr, fsc;
3203         bool take_exc = false;
3204 
3205         if (fi.s1ptw && current_el == 1
3206             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3207             /*
3208              * Synchronous stage 2 fault on an access made as part of the
3209              * translation table walk for AT S1E0* or AT S1E1* insn
3210              * executed from NS EL1. If this is a synchronous external abort
3211              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3212              * to EL3. Otherwise the fault is taken as an exception to EL2,
3213              * and HPFAR_EL2 holds the faulting IPA.
3214              */
3215             if (fi.type == ARMFault_SyncExternalOnWalk &&
3216                 (env->cp15.scr_el3 & SCR_EA)) {
3217                 target_el = 3;
3218             } else {
3219                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3220                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3221                     env->cp15.hpfar_el2 |= HPFAR_NS;
3222                 }
3223                 target_el = 2;
3224             }
3225             take_exc = true;
3226         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3227             /*
3228              * Synchronous external aborts during a translation table walk
3229              * are taken as Data Abort exceptions.
3230              */
3231             if (fi.stage2) {
3232                 if (current_el == 3) {
3233                     target_el = 3;
3234                 } else {
3235                     target_el = 2;
3236                 }
3237             } else {
3238                 target_el = exception_target_el(env);
3239             }
3240             take_exc = true;
3241         }
3242 
3243         if (take_exc) {
3244             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3245             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3246                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3247                 fsr = arm_fi_to_lfsc(&fi);
3248                 fsc = extract32(fsr, 0, 6);
3249             } else {
3250                 fsr = arm_fi_to_sfsc(&fi);
3251                 fsc = 0x3f;
3252             }
3253             /*
3254              * Report exception with ESR indicating a fault due to a
3255              * translation table walk for a cache maintenance instruction.
3256              */
3257             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3258                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3259             env->exception.vaddress = value;
3260             env->exception.fsr = fsr;
3261             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3262         }
3263     }
3264 
3265     if (is_a64(env)) {
3266         format64 = true;
3267     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3268         /*
3269          * ATS1Cxx:
3270          * * TTBCR.EAE determines whether the result is returned using the
3271          *   32-bit or the 64-bit PAR format
3272          * * Instructions executed in Hyp mode always use the 64bit format
3273          *
3274          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3275          * * The Non-secure TTBCR.EAE bit is set to 1
3276          * * The implementation includes EL2, and the value of HCR.VM is 1
3277          *
3278          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3279          *
3280          * ATS1Hx always uses the 64bit format.
3281          */
3282         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3283 
3284         if (arm_feature(env, ARM_FEATURE_EL2)) {
3285             if (mmu_idx == ARMMMUIdx_E10_0 ||
3286                 mmu_idx == ARMMMUIdx_E10_1 ||
3287                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3288                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3289             } else {
3290                 format64 |= arm_current_el(env) == 2;
3291             }
3292         }
3293     }
3294 
3295     if (format64) {
3296         /* Create a 64-bit PAR */
3297         par64 = (1 << 11); /* LPAE bit always set */
3298         if (!ret) {
3299             par64 |= phys_addr & ~0xfffULL;
3300             if (!attrs.secure) {
3301                 par64 |= (1 << 9); /* NS */
3302             }
3303             par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3304             par64 |= cacheattrs.shareability << 7; /* SH */
3305         } else {
3306             uint32_t fsr = arm_fi_to_lfsc(&fi);
3307 
3308             par64 |= 1; /* F */
3309             par64 |= (fsr & 0x3f) << 1; /* FS */
3310             if (fi.stage2) {
3311                 par64 |= (1 << 9); /* S */
3312             }
3313             if (fi.s1ptw) {
3314                 par64 |= (1 << 8); /* PTW */
3315             }
3316         }
3317     } else {
3318         /* fsr is a DFSR/IFSR value for the short descriptor
3319          * translation table format (with WnR always clear).
3320          * Convert it to a 32-bit PAR.
3321          */
3322         if (!ret) {
3323             /* We do not set any attribute bits in the PAR */
3324             if (page_size == (1 << 24)
3325                 && arm_feature(env, ARM_FEATURE_V7)) {
3326                 par64 = (phys_addr & 0xff000000) | (1 << 1);
3327             } else {
3328                 par64 = phys_addr & 0xfffff000;
3329             }
3330             if (!attrs.secure) {
3331                 par64 |= (1 << 9); /* NS */
3332             }
3333         } else {
3334             uint32_t fsr = arm_fi_to_sfsc(&fi);
3335 
3336             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3337                     ((fsr & 0xf) << 1) | 1;
3338         }
3339     }
3340     return par64;
3341 }
3342 #endif /* CONFIG_TCG */
3343 
3344 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3345 {
3346 #ifdef CONFIG_TCG
3347     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3348     uint64_t par64;
3349     ARMMMUIdx mmu_idx;
3350     int el = arm_current_el(env);
3351     bool secure = arm_is_secure_below_el3(env);
3352 
3353     switch (ri->opc2 & 6) {
3354     case 0:
3355         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3356         switch (el) {
3357         case 3:
3358             mmu_idx = ARMMMUIdx_SE3;
3359             break;
3360         case 2:
3361             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3362             /* fall through */
3363         case 1:
3364             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3365                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3366                            : ARMMMUIdx_Stage1_E1_PAN);
3367             } else {
3368                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3369             }
3370             break;
3371         default:
3372             g_assert_not_reached();
3373         }
3374         break;
3375     case 2:
3376         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3377         switch (el) {
3378         case 3:
3379             mmu_idx = ARMMMUIdx_SE10_0;
3380             break;
3381         case 2:
3382             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3383             mmu_idx = ARMMMUIdx_Stage1_E0;
3384             break;
3385         case 1:
3386             mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3387             break;
3388         default:
3389             g_assert_not_reached();
3390         }
3391         break;
3392     case 4:
3393         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3394         mmu_idx = ARMMMUIdx_E10_1;
3395         break;
3396     case 6:
3397         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3398         mmu_idx = ARMMMUIdx_E10_0;
3399         break;
3400     default:
3401         g_assert_not_reached();
3402     }
3403 
3404     par64 = do_ats_write(env, value, access_type, mmu_idx);
3405 
3406     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3407 #else
3408     /* Handled by hardware accelerator. */
3409     g_assert_not_reached();
3410 #endif /* CONFIG_TCG */
3411 }
3412 
3413 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3414                         uint64_t value)
3415 {
3416 #ifdef CONFIG_TCG
3417     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3418     uint64_t par64;
3419 
3420     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
3421 
3422     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3423 #else
3424     /* Handled by hardware accelerator. */
3425     g_assert_not_reached();
3426 #endif /* CONFIG_TCG */
3427 }
3428 
3429 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3430                                      bool isread)
3431 {
3432     if (arm_current_el(env) == 3 &&
3433         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3434         return CP_ACCESS_TRAP;
3435     }
3436     return CP_ACCESS_OK;
3437 }
3438 
3439 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3440                         uint64_t value)
3441 {
3442 #ifdef CONFIG_TCG
3443     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3444     ARMMMUIdx mmu_idx;
3445     int secure = arm_is_secure_below_el3(env);
3446 
3447     switch (ri->opc2 & 6) {
3448     case 0:
3449         switch (ri->opc1) {
3450         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3451             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3452                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3453                            : ARMMMUIdx_Stage1_E1_PAN);
3454             } else {
3455                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3456             }
3457             break;
3458         case 4: /* AT S1E2R, AT S1E2W */
3459             mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
3460             break;
3461         case 6: /* AT S1E3R, AT S1E3W */
3462             mmu_idx = ARMMMUIdx_SE3;
3463             break;
3464         default:
3465             g_assert_not_reached();
3466         }
3467         break;
3468     case 2: /* AT S1E0R, AT S1E0W */
3469         mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3470         break;
3471     case 4: /* AT S12E1R, AT S12E1W */
3472         mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
3473         break;
3474     case 6: /* AT S12E0R, AT S12E0W */
3475         mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
3476         break;
3477     default:
3478         g_assert_not_reached();
3479     }
3480 
3481     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3482 #else
3483     /* Handled by hardware accelerator. */
3484     g_assert_not_reached();
3485 #endif /* CONFIG_TCG */
3486 }
3487 #endif
3488 
3489 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3490     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3491       .access = PL1_RW, .resetvalue = 0,
3492       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3493                              offsetoflow32(CPUARMState, cp15.par_ns) },
3494       .writefn = par_write },
3495 #ifndef CONFIG_USER_ONLY
3496     /* This underdecoding is safe because the reginfo is NO_RAW. */
3497     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3498       .access = PL1_W, .accessfn = ats_access,
3499       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3500 #endif
3501 };
3502 
3503 /* Return basic MPU access permission bits.  */
3504 static uint32_t simple_mpu_ap_bits(uint32_t val)
3505 {
3506     uint32_t ret;
3507     uint32_t mask;
3508     int i;
3509     ret = 0;
3510     mask = 3;
3511     for (i = 0; i < 16; i += 2) {
3512         ret |= (val >> i) & mask;
3513         mask <<= 2;
3514     }
3515     return ret;
3516 }
3517 
3518 /* Pad basic MPU access permission bits to extended format.  */
3519 static uint32_t extended_mpu_ap_bits(uint32_t val)
3520 {
3521     uint32_t ret;
3522     uint32_t mask;
3523     int i;
3524     ret = 0;
3525     mask = 3;
3526     for (i = 0; i < 16; i += 2) {
3527         ret |= (val & mask) << i;
3528         mask <<= 2;
3529     }
3530     return ret;
3531 }
3532 
3533 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3534                                  uint64_t value)
3535 {
3536     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3537 }
3538 
3539 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3540 {
3541     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3542 }
3543 
3544 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3545                                  uint64_t value)
3546 {
3547     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3548 }
3549 
3550 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3551 {
3552     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3553 }
3554 
3555 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3556 {
3557     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3558 
3559     if (!u32p) {
3560         return 0;
3561     }
3562 
3563     u32p += env->pmsav7.rnr[M_REG_NS];
3564     return *u32p;
3565 }
3566 
3567 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3568                          uint64_t value)
3569 {
3570     ARMCPU *cpu = env_archcpu(env);
3571     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3572 
3573     if (!u32p) {
3574         return;
3575     }
3576 
3577     u32p += env->pmsav7.rnr[M_REG_NS];
3578     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3579     *u32p = value;
3580 }
3581 
3582 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3583                               uint64_t value)
3584 {
3585     ARMCPU *cpu = env_archcpu(env);
3586     uint32_t nrgs = cpu->pmsav7_dregion;
3587 
3588     if (value >= nrgs) {
3589         qemu_log_mask(LOG_GUEST_ERROR,
3590                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3591                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3592         return;
3593     }
3594 
3595     raw_write(env, ri, value);
3596 }
3597 
3598 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3599     /* Reset for all these registers is handled in arm_cpu_reset(),
3600      * because the PMSAv7 is also used by M-profile CPUs, which do
3601      * not register cpregs but still need the state to be reset.
3602      */
3603     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3604       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3605       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3606       .readfn = pmsav7_read, .writefn = pmsav7_write,
3607       .resetfn = arm_cp_reset_ignore },
3608     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3609       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3610       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3611       .readfn = pmsav7_read, .writefn = pmsav7_write,
3612       .resetfn = arm_cp_reset_ignore },
3613     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3614       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3615       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3616       .readfn = pmsav7_read, .writefn = pmsav7_write,
3617       .resetfn = arm_cp_reset_ignore },
3618     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3619       .access = PL1_RW,
3620       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3621       .writefn = pmsav7_rgnr_write,
3622       .resetfn = arm_cp_reset_ignore },
3623 };
3624 
3625 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3626     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3627       .access = PL1_RW, .type = ARM_CP_ALIAS,
3628       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3629       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3630     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3631       .access = PL1_RW, .type = ARM_CP_ALIAS,
3632       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3633       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3634     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3635       .access = PL1_RW,
3636       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3637       .resetvalue = 0, },
3638     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3639       .access = PL1_RW,
3640       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3641       .resetvalue = 0, },
3642     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3643       .access = PL1_RW,
3644       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3645     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3646       .access = PL1_RW,
3647       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3648     /* Protection region base and size registers */
3649     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3650       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3651       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3652     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3653       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3654       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3655     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3656       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3657       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3658     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3659       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3660       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3661     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3662       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3663       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3664     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3665       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3666       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3667     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3668       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3669       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3670     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3671       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3672       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3673 };
3674 
3675 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3676                                  uint64_t value)
3677 {
3678     TCR *tcr = raw_ptr(env, ri);
3679     int maskshift = extract32(value, 0, 3);
3680 
3681     if (!arm_feature(env, ARM_FEATURE_V8)) {
3682         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3683             /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3684              * using Long-desciptor translation table format */
3685             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3686         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3687             /* In an implementation that includes the Security Extensions
3688              * TTBCR has additional fields PD0 [4] and PD1 [5] for
3689              * Short-descriptor translation table format.
3690              */
3691             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3692         } else {
3693             value &= TTBCR_N;
3694         }
3695     }
3696 
3697     /* Update the masks corresponding to the TCR bank being written
3698      * Note that we always calculate mask and base_mask, but
3699      * they are only used for short-descriptor tables (ie if EAE is 0);
3700      * for long-descriptor tables the TCR fields are used differently
3701      * and the mask and base_mask values are meaningless.
3702      */
3703     tcr->raw_tcr = value;
3704     tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3705     tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
3706 }
3707 
3708 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3709                              uint64_t value)
3710 {
3711     ARMCPU *cpu = env_archcpu(env);
3712     TCR *tcr = raw_ptr(env, ri);
3713 
3714     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3715         /* With LPAE the TTBCR could result in a change of ASID
3716          * via the TTBCR.A1 bit, so do a TLB flush.
3717          */
3718         tlb_flush(CPU(cpu));
3719     }
3720     /* Preserve the high half of TCR_EL1, set via TTBCR2.  */
3721     value = deposit64(tcr->raw_tcr, 0, 32, value);
3722     vmsa_ttbcr_raw_write(env, ri, value);
3723 }
3724 
3725 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3726 {
3727     TCR *tcr = raw_ptr(env, ri);
3728 
3729     /* Reset both the TCR as well as the masks corresponding to the bank of
3730      * the TCR being reset.
3731      */
3732     tcr->raw_tcr = 0;
3733     tcr->mask = 0;
3734     tcr->base_mask = 0xffffc000u;
3735 }
3736 
3737 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3738                                uint64_t value)
3739 {
3740     ARMCPU *cpu = env_archcpu(env);
3741     TCR *tcr = raw_ptr(env, ri);
3742 
3743     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3744     tlb_flush(CPU(cpu));
3745     tcr->raw_tcr = value;
3746 }
3747 
3748 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3749                             uint64_t value)
3750 {
3751     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
3752     if (cpreg_field_is_64bit(ri) &&
3753         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3754         ARMCPU *cpu = env_archcpu(env);
3755         tlb_flush(CPU(cpu));
3756     }
3757     raw_write(env, ri, value);
3758 }
3759 
3760 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3761                                     uint64_t value)
3762 {
3763     /*
3764      * If we are running with E2&0 regime, then an ASID is active.
3765      * Flush if that might be changing.  Note we're not checking
3766      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3767      * holds the active ASID, only checking the field that might.
3768      */
3769     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3770         (arm_hcr_el2_eff(env) & HCR_E2H)) {
3771         uint16_t mask = ARMMMUIdxBit_E20_2 |
3772                         ARMMMUIdxBit_E20_2_PAN |
3773                         ARMMMUIdxBit_E20_0;
3774 
3775         if (arm_is_secure_below_el3(env)) {
3776             mask >>= ARM_MMU_IDX_A_NS;
3777         }
3778 
3779         tlb_flush_by_mmuidx(env_cpu(env), mask);
3780     }
3781     raw_write(env, ri, value);
3782 }
3783 
3784 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3785                         uint64_t value)
3786 {
3787     ARMCPU *cpu = env_archcpu(env);
3788     CPUState *cs = CPU(cpu);
3789 
3790     /*
3791      * A change in VMID to the stage2 page table (Stage2) invalidates
3792      * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3793      */
3794     if (raw_read(env, ri) != value) {
3795         uint16_t mask = ARMMMUIdxBit_E10_1 |
3796                         ARMMMUIdxBit_E10_1_PAN |
3797                         ARMMMUIdxBit_E10_0;
3798 
3799         if (arm_is_secure_below_el3(env)) {
3800             mask >>= ARM_MMU_IDX_A_NS;
3801         }
3802 
3803         tlb_flush_by_mmuidx(cs, mask);
3804         raw_write(env, ri, value);
3805     }
3806 }
3807 
3808 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3809     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3810       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
3811       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3812                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3813     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3814       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3815       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3816                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3817     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3818       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3819       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3820                              offsetof(CPUARMState, cp15.dfar_ns) } },
3821     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3822       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3823       .access = PL1_RW, .accessfn = access_tvm_trvm,
3824       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3825       .resetvalue = 0, },
3826 };
3827 
3828 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3829     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3830       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3831       .access = PL1_RW, .accessfn = access_tvm_trvm,
3832       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3833     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3834       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3835       .access = PL1_RW, .accessfn = access_tvm_trvm,
3836       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3837       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3838                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
3839     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3840       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3841       .access = PL1_RW, .accessfn = access_tvm_trvm,
3842       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3843       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3844                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
3845     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3846       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3847       .access = PL1_RW, .accessfn = access_tvm_trvm,
3848       .writefn = vmsa_tcr_el12_write,
3849       .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3850       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3851     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3852       .access = PL1_RW, .accessfn = access_tvm_trvm,
3853       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3854       .raw_writefn = vmsa_ttbcr_raw_write,
3855       /* No offsetoflow32 -- pass the entire TCR to writefn/raw_writefn. */
3856       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.tcr_el[3]),
3857                              offsetof(CPUARMState, cp15.tcr_el[1])} },
3858 };
3859 
3860 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3861  * qemu tlbs nor adjusting cached masks.
3862  */
3863 static const ARMCPRegInfo ttbcr2_reginfo = {
3864     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3865     .access = PL1_RW, .accessfn = access_tvm_trvm,
3866     .type = ARM_CP_ALIAS,
3867     .bank_fieldoffsets = {
3868         offsetofhigh32(CPUARMState, cp15.tcr_el[3].raw_tcr),
3869         offsetofhigh32(CPUARMState, cp15.tcr_el[1].raw_tcr),
3870     },
3871 };
3872 
3873 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3874                                 uint64_t value)
3875 {
3876     env->cp15.c15_ticonfig = value & 0xe7;
3877     /* The OS_TYPE bit in this register changes the reported CPUID! */
3878     env->cp15.c0_cpuid = (value & (1 << 5)) ?
3879         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3880 }
3881 
3882 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3883                                 uint64_t value)
3884 {
3885     env->cp15.c15_threadid = value & 0xffff;
3886 }
3887 
3888 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3889                            uint64_t value)
3890 {
3891     /* Wait-for-interrupt (deprecated) */
3892     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3893 }
3894 
3895 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3896                                   uint64_t value)
3897 {
3898     /* On OMAP there are registers indicating the max/min index of dcache lines
3899      * containing a dirty line; cache flush operations have to reset these.
3900      */
3901     env->cp15.c15_i_max = 0x000;
3902     env->cp15.c15_i_min = 0xff0;
3903 }
3904 
3905 static const ARMCPRegInfo omap_cp_reginfo[] = {
3906     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3907       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3908       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3909       .resetvalue = 0, },
3910     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3911       .access = PL1_RW, .type = ARM_CP_NOP },
3912     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3913       .access = PL1_RW,
3914       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3915       .writefn = omap_ticonfig_write },
3916     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3917       .access = PL1_RW,
3918       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3919     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3920       .access = PL1_RW, .resetvalue = 0xff0,
3921       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3922     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3923       .access = PL1_RW,
3924       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3925       .writefn = omap_threadid_write },
3926     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3927       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3928       .type = ARM_CP_NO_RAW,
3929       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3930     /* TODO: Peripheral port remap register:
3931      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3932      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3933      * when MMU is off.
3934      */
3935     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3936       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3937       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3938       .writefn = omap_cachemaint_write },
3939     { .name = "C9", .cp = 15, .crn = 9,
3940       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3941       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3942 };
3943 
3944 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3945                               uint64_t value)
3946 {
3947     env->cp15.c15_cpar = value & 0x3fff;
3948 }
3949 
3950 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3951     { .name = "XSCALE_CPAR",
3952       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3953       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3954       .writefn = xscale_cpar_write, },
3955     { .name = "XSCALE_AUXCR",
3956       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3957       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3958       .resetvalue = 0, },
3959     /* XScale specific cache-lockdown: since we have no cache we NOP these
3960      * and hope the guest does not really rely on cache behaviour.
3961      */
3962     { .name = "XSCALE_LOCK_ICACHE_LINE",
3963       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3964       .access = PL1_W, .type = ARM_CP_NOP },
3965     { .name = "XSCALE_UNLOCK_ICACHE",
3966       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3967       .access = PL1_W, .type = ARM_CP_NOP },
3968     { .name = "XSCALE_DCACHE_LOCK",
3969       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3970       .access = PL1_RW, .type = ARM_CP_NOP },
3971     { .name = "XSCALE_UNLOCK_DCACHE",
3972       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3973       .access = PL1_W, .type = ARM_CP_NOP },
3974 };
3975 
3976 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3977     /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3978      * implementation of this implementation-defined space.
3979      * Ideally this should eventually disappear in favour of actually
3980      * implementing the correct behaviour for all cores.
3981      */
3982     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3983       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3984       .access = PL1_RW,
3985       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3986       .resetvalue = 0 },
3987 };
3988 
3989 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3990     /* Cache status: RAZ because we have no cache so it's always clean */
3991     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3992       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3993       .resetvalue = 0 },
3994 };
3995 
3996 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3997     /* We never have a a block transfer operation in progress */
3998     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
3999       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4000       .resetvalue = 0 },
4001     /* The cache ops themselves: these all NOP for QEMU */
4002     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4003       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4004     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4005       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4006     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4007       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4008     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4009       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4010     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4011       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4012     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4013       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4014 };
4015 
4016 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4017     /* The cache test-and-clean instructions always return (1 << 30)
4018      * to indicate that there are no dirty cache lines.
4019      */
4020     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4021       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4022       .resetvalue = (1 << 30) },
4023     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4024       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4025       .resetvalue = (1 << 30) },
4026 };
4027 
4028 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4029     /* Ignore ReadBuffer accesses */
4030     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4031       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4032       .access = PL1_RW, .resetvalue = 0,
4033       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4034 };
4035 
4036 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4037 {
4038     unsigned int cur_el = arm_current_el(env);
4039 
4040     if (arm_is_el2_enabled(env) && cur_el == 1) {
4041         return env->cp15.vpidr_el2;
4042     }
4043     return raw_read(env, ri);
4044 }
4045 
4046 static uint64_t mpidr_read_val(CPUARMState *env)
4047 {
4048     ARMCPU *cpu = env_archcpu(env);
4049     uint64_t mpidr = cpu->mp_affinity;
4050 
4051     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4052         mpidr |= (1U << 31);
4053         /* Cores which are uniprocessor (non-coherent)
4054          * but still implement the MP extensions set
4055          * bit 30. (For instance, Cortex-R5).
4056          */
4057         if (cpu->mp_is_up) {
4058             mpidr |= (1u << 30);
4059         }
4060     }
4061     return mpidr;
4062 }
4063 
4064 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4065 {
4066     unsigned int cur_el = arm_current_el(env);
4067 
4068     if (arm_is_el2_enabled(env) && cur_el == 1) {
4069         return env->cp15.vmpidr_el2;
4070     }
4071     return mpidr_read_val(env);
4072 }
4073 
4074 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4075     /* NOP AMAIR0/1 */
4076     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4077       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4078       .access = PL1_RW, .accessfn = access_tvm_trvm,
4079       .type = ARM_CP_CONST, .resetvalue = 0 },
4080     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4081     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4082       .access = PL1_RW, .accessfn = access_tvm_trvm,
4083       .type = ARM_CP_CONST, .resetvalue = 0 },
4084     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4085       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4086       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4087                              offsetof(CPUARMState, cp15.par_ns)} },
4088     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4089       .access = PL1_RW, .accessfn = access_tvm_trvm,
4090       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4091       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4092                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4093       .writefn = vmsa_ttbr_write, },
4094     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4095       .access = PL1_RW, .accessfn = access_tvm_trvm,
4096       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4097       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4098                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4099       .writefn = vmsa_ttbr_write, },
4100 };
4101 
4102 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4103 {
4104     return vfp_get_fpcr(env);
4105 }
4106 
4107 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4108                             uint64_t value)
4109 {
4110     vfp_set_fpcr(env, value);
4111 }
4112 
4113 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4114 {
4115     return vfp_get_fpsr(env);
4116 }
4117 
4118 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4119                             uint64_t value)
4120 {
4121     vfp_set_fpsr(env, value);
4122 }
4123 
4124 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4125                                        bool isread)
4126 {
4127     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4128         return CP_ACCESS_TRAP;
4129     }
4130     return CP_ACCESS_OK;
4131 }
4132 
4133 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4134                             uint64_t value)
4135 {
4136     env->daif = value & PSTATE_DAIF;
4137 }
4138 
4139 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4140 {
4141     return env->pstate & PSTATE_PAN;
4142 }
4143 
4144 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4145                            uint64_t value)
4146 {
4147     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4148 }
4149 
4150 static const ARMCPRegInfo pan_reginfo = {
4151     .name = "PAN", .state = ARM_CP_STATE_AA64,
4152     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4153     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4154     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4155 };
4156 
4157 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4158 {
4159     return env->pstate & PSTATE_UAO;
4160 }
4161 
4162 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4163                            uint64_t value)
4164 {
4165     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4166 }
4167 
4168 static const ARMCPRegInfo uao_reginfo = {
4169     .name = "UAO", .state = ARM_CP_STATE_AA64,
4170     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4171     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4172     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4173 };
4174 
4175 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4176 {
4177     return env->pstate & PSTATE_DIT;
4178 }
4179 
4180 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4181                            uint64_t value)
4182 {
4183     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4184 }
4185 
4186 static const ARMCPRegInfo dit_reginfo = {
4187     .name = "DIT", .state = ARM_CP_STATE_AA64,
4188     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4189     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4190     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4191 };
4192 
4193 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4194 {
4195     return env->pstate & PSTATE_SSBS;
4196 }
4197 
4198 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4199                            uint64_t value)
4200 {
4201     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4202 }
4203 
4204 static const ARMCPRegInfo ssbs_reginfo = {
4205     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4206     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4207     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4208     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4209 };
4210 
4211 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4212                                               const ARMCPRegInfo *ri,
4213                                               bool isread)
4214 {
4215     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4216     switch (arm_current_el(env)) {
4217     case 0:
4218         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4219         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4220             return CP_ACCESS_TRAP;
4221         }
4222         /* fall through */
4223     case 1:
4224         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4225         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4226             return CP_ACCESS_TRAP_EL2;
4227         }
4228         break;
4229     }
4230     return CP_ACCESS_OK;
4231 }
4232 
4233 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4234                                               const ARMCPRegInfo *ri,
4235                                               bool isread)
4236 {
4237     /* Cache invalidate/clean to Point of Unification... */
4238     switch (arm_current_el(env)) {
4239     case 0:
4240         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4241         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4242             return CP_ACCESS_TRAP;
4243         }
4244         /* fall through */
4245     case 1:
4246         /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set.  */
4247         if (arm_hcr_el2_eff(env) & HCR_TPU) {
4248             return CP_ACCESS_TRAP_EL2;
4249         }
4250         break;
4251     }
4252     return CP_ACCESS_OK;
4253 }
4254 
4255 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4256  * Page D4-1736 (DDI0487A.b)
4257  */
4258 
4259 static int vae1_tlbmask(CPUARMState *env)
4260 {
4261     uint64_t hcr = arm_hcr_el2_eff(env);
4262     uint16_t mask;
4263 
4264     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4265         mask = ARMMMUIdxBit_E20_2 |
4266                ARMMMUIdxBit_E20_2_PAN |
4267                ARMMMUIdxBit_E20_0;
4268     } else {
4269         mask = ARMMMUIdxBit_E10_1 |
4270                ARMMMUIdxBit_E10_1_PAN |
4271                ARMMMUIdxBit_E10_0;
4272     }
4273 
4274     if (arm_is_secure_below_el3(env)) {
4275         mask >>= ARM_MMU_IDX_A_NS;
4276     }
4277 
4278     return mask;
4279 }
4280 
4281 /* Return 56 if TBI is enabled, 64 otherwise. */
4282 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4283                               uint64_t addr)
4284 {
4285     uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
4286     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4287     int select = extract64(addr, 55, 1);
4288 
4289     return (tbi >> select) & 1 ? 56 : 64;
4290 }
4291 
4292 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4293 {
4294     uint64_t hcr = arm_hcr_el2_eff(env);
4295     ARMMMUIdx mmu_idx;
4296 
4297     /* Only the regime of the mmu_idx below is significant. */
4298     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4299         mmu_idx = ARMMMUIdx_E20_0;
4300     } else {
4301         mmu_idx = ARMMMUIdx_E10_0;
4302     }
4303 
4304     if (arm_is_secure_below_el3(env)) {
4305         mmu_idx &= ~ARM_MMU_IDX_A_NS;
4306     }
4307 
4308     return tlbbits_for_regime(env, mmu_idx, addr);
4309 }
4310 
4311 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4312                                       uint64_t value)
4313 {
4314     CPUState *cs = env_cpu(env);
4315     int mask = vae1_tlbmask(env);
4316 
4317     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4318 }
4319 
4320 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4321                                     uint64_t value)
4322 {
4323     CPUState *cs = env_cpu(env);
4324     int mask = vae1_tlbmask(env);
4325 
4326     if (tlb_force_broadcast(env)) {
4327         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4328     } else {
4329         tlb_flush_by_mmuidx(cs, mask);
4330     }
4331 }
4332 
4333 static int alle1_tlbmask(CPUARMState *env)
4334 {
4335     /*
4336      * Note that the 'ALL' scope must invalidate both stage 1 and
4337      * stage 2 translations, whereas most other scopes only invalidate
4338      * stage 1 translations.
4339      */
4340     if (arm_is_secure_below_el3(env)) {
4341         return ARMMMUIdxBit_SE10_1 |
4342                ARMMMUIdxBit_SE10_1_PAN |
4343                ARMMMUIdxBit_SE10_0;
4344     } else {
4345         return ARMMMUIdxBit_E10_1 |
4346                ARMMMUIdxBit_E10_1_PAN |
4347                ARMMMUIdxBit_E10_0;
4348     }
4349 }
4350 
4351 static int e2_tlbmask(CPUARMState *env)
4352 {
4353     if (arm_is_secure_below_el3(env)) {
4354         return ARMMMUIdxBit_SE20_0 |
4355                ARMMMUIdxBit_SE20_2 |
4356                ARMMMUIdxBit_SE20_2_PAN |
4357                ARMMMUIdxBit_SE2;
4358     } else {
4359         return ARMMMUIdxBit_E20_0 |
4360                ARMMMUIdxBit_E20_2 |
4361                ARMMMUIdxBit_E20_2_PAN |
4362                ARMMMUIdxBit_E2;
4363     }
4364 }
4365 
4366 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4367                                   uint64_t value)
4368 {
4369     CPUState *cs = env_cpu(env);
4370     int mask = alle1_tlbmask(env);
4371 
4372     tlb_flush_by_mmuidx(cs, mask);
4373 }
4374 
4375 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4376                                   uint64_t value)
4377 {
4378     CPUState *cs = env_cpu(env);
4379     int mask = e2_tlbmask(env);
4380 
4381     tlb_flush_by_mmuidx(cs, mask);
4382 }
4383 
4384 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4385                                   uint64_t value)
4386 {
4387     ARMCPU *cpu = env_archcpu(env);
4388     CPUState *cs = CPU(cpu);
4389 
4390     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
4391 }
4392 
4393 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4394                                     uint64_t value)
4395 {
4396     CPUState *cs = env_cpu(env);
4397     int mask = alle1_tlbmask(env);
4398 
4399     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4400 }
4401 
4402 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4403                                     uint64_t value)
4404 {
4405     CPUState *cs = env_cpu(env);
4406     int mask = e2_tlbmask(env);
4407 
4408     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4409 }
4410 
4411 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4412                                     uint64_t value)
4413 {
4414     CPUState *cs = env_cpu(env);
4415 
4416     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
4417 }
4418 
4419 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4420                                  uint64_t value)
4421 {
4422     /* Invalidate by VA, EL2
4423      * Currently handles both VAE2 and VALE2, since we don't support
4424      * flush-last-level-only.
4425      */
4426     CPUState *cs = env_cpu(env);
4427     int mask = e2_tlbmask(env);
4428     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4429 
4430     tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4431 }
4432 
4433 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4434                                  uint64_t value)
4435 {
4436     /* Invalidate by VA, EL3
4437      * Currently handles both VAE3 and VALE3, since we don't support
4438      * flush-last-level-only.
4439      */
4440     ARMCPU *cpu = env_archcpu(env);
4441     CPUState *cs = CPU(cpu);
4442     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4443 
4444     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
4445 }
4446 
4447 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4448                                    uint64_t value)
4449 {
4450     CPUState *cs = env_cpu(env);
4451     int mask = vae1_tlbmask(env);
4452     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4453     int bits = vae1_tlbbits(env, pageaddr);
4454 
4455     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4456 }
4457 
4458 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4459                                  uint64_t value)
4460 {
4461     /* Invalidate by VA, EL1&0 (AArch64 version).
4462      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4463      * since we don't support flush-for-specific-ASID-only or
4464      * flush-last-level-only.
4465      */
4466     CPUState *cs = env_cpu(env);
4467     int mask = vae1_tlbmask(env);
4468     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4469     int bits = vae1_tlbbits(env, pageaddr);
4470 
4471     if (tlb_force_broadcast(env)) {
4472         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4473     } else {
4474         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4475     }
4476 }
4477 
4478 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4479                                    uint64_t value)
4480 {
4481     CPUState *cs = env_cpu(env);
4482     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4483     bool secure = arm_is_secure_below_el3(env);
4484     int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
4485     int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
4486                                   pageaddr);
4487 
4488     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4489 }
4490 
4491 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4492                                    uint64_t value)
4493 {
4494     CPUState *cs = env_cpu(env);
4495     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4496     int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
4497 
4498     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4499                                                   ARMMMUIdxBit_SE3, bits);
4500 }
4501 
4502 #ifdef TARGET_AARCH64
4503 typedef struct {
4504     uint64_t base;
4505     uint64_t length;
4506 } TLBIRange;
4507 
4508 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4509                                      uint64_t value)
4510 {
4511     unsigned int page_size_granule, page_shift, num, scale, exponent;
4512     /* Extract one bit to represent the va selector in use. */
4513     uint64_t select = sextract64(value, 36, 1);
4514     ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4515     TLBIRange ret = { };
4516 
4517     page_size_granule = extract64(value, 46, 2);
4518 
4519     /* The granule encoded in value must match the granule in use. */
4520     if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4521         qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4522                       page_size_granule);
4523         return ret;
4524     }
4525 
4526     page_shift = (page_size_granule - 1) * 2 + 12;
4527     num = extract64(value, 39, 5);
4528     scale = extract64(value, 44, 2);
4529     exponent = (5 * scale) + 1;
4530 
4531     ret.length = (num + 1) << (exponent + page_shift);
4532 
4533     if (param.select) {
4534         ret.base = sextract64(value, 0, 37);
4535     } else {
4536         ret.base = extract64(value, 0, 37);
4537     }
4538     if (param.ds) {
4539         /*
4540          * With DS=1, BaseADDR is always shifted 16 so that it is able
4541          * to address all 52 va bits.  The input address is perforce
4542          * aligned on a 64k boundary regardless of translation granule.
4543          */
4544         page_shift = 16;
4545     }
4546     ret.base <<= page_shift;
4547 
4548     return ret;
4549 }
4550 
4551 static void do_rvae_write(CPUARMState *env, uint64_t value,
4552                           int idxmap, bool synced)
4553 {
4554     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4555     TLBIRange range;
4556     int bits;
4557 
4558     range = tlbi_aa64_get_range(env, one_idx, value);
4559     bits = tlbbits_for_regime(env, one_idx, range.base);
4560 
4561     if (synced) {
4562         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4563                                                   range.base,
4564                                                   range.length,
4565                                                   idxmap,
4566                                                   bits);
4567     } else {
4568         tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4569                                   range.length, idxmap, bits);
4570     }
4571 }
4572 
4573 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4574                                   const ARMCPRegInfo *ri,
4575                                   uint64_t value)
4576 {
4577     /*
4578      * Invalidate by VA range, EL1&0.
4579      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4580      * since we don't support flush-for-specific-ASID-only or
4581      * flush-last-level-only.
4582      */
4583 
4584     do_rvae_write(env, value, vae1_tlbmask(env),
4585                   tlb_force_broadcast(env));
4586 }
4587 
4588 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4589                                     const ARMCPRegInfo *ri,
4590                                     uint64_t value)
4591 {
4592     /*
4593      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4594      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4595      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4596      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4597      * shareable specific flushes.
4598      */
4599 
4600     do_rvae_write(env, value, vae1_tlbmask(env), true);
4601 }
4602 
4603 static int vae2_tlbmask(CPUARMState *env)
4604 {
4605     return (arm_is_secure_below_el3(env)
4606             ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4607 }
4608 
4609 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4610                                   const ARMCPRegInfo *ri,
4611                                   uint64_t value)
4612 {
4613     /*
4614      * Invalidate by VA range, EL2.
4615      * Currently handles all of RVAE2 and RVALE2,
4616      * since we don't support flush-for-specific-ASID-only or
4617      * flush-last-level-only.
4618      */
4619 
4620     do_rvae_write(env, value, vae2_tlbmask(env),
4621                   tlb_force_broadcast(env));
4622 
4623 
4624 }
4625 
4626 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4627                                     const ARMCPRegInfo *ri,
4628                                     uint64_t value)
4629 {
4630     /*
4631      * Invalidate by VA range, Inner/Outer Shareable, EL2.
4632      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4633      * since we don't support flush-for-specific-ASID-only,
4634      * flush-last-level-only or inner/outer shareable specific flushes.
4635      */
4636 
4637     do_rvae_write(env, value, vae2_tlbmask(env), true);
4638 
4639 }
4640 
4641 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4642                                   const ARMCPRegInfo *ri,
4643                                   uint64_t value)
4644 {
4645     /*
4646      * Invalidate by VA range, EL3.
4647      * Currently handles all of RVAE3 and RVALE3,
4648      * since we don't support flush-for-specific-ASID-only or
4649      * flush-last-level-only.
4650      */
4651 
4652     do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4653                   tlb_force_broadcast(env));
4654 }
4655 
4656 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4657                                     const ARMCPRegInfo *ri,
4658                                     uint64_t value)
4659 {
4660     /*
4661      * Invalidate by VA range, EL3, Inner/Outer Shareable.
4662      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4663      * since we don't support flush-for-specific-ASID-only,
4664      * flush-last-level-only or inner/outer specific flushes.
4665      */
4666 
4667     do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4668 }
4669 #endif
4670 
4671 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4672                                       bool isread)
4673 {
4674     int cur_el = arm_current_el(env);
4675 
4676     if (cur_el < 2) {
4677         uint64_t hcr = arm_hcr_el2_eff(env);
4678 
4679         if (cur_el == 0) {
4680             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4681                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4682                     return CP_ACCESS_TRAP_EL2;
4683                 }
4684             } else {
4685                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4686                     return CP_ACCESS_TRAP;
4687                 }
4688                 if (hcr & HCR_TDZ) {
4689                     return CP_ACCESS_TRAP_EL2;
4690                 }
4691             }
4692         } else if (hcr & HCR_TDZ) {
4693             return CP_ACCESS_TRAP_EL2;
4694         }
4695     }
4696     return CP_ACCESS_OK;
4697 }
4698 
4699 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4700 {
4701     ARMCPU *cpu = env_archcpu(env);
4702     int dzp_bit = 1 << 4;
4703 
4704     /* DZP indicates whether DC ZVA access is allowed */
4705     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4706         dzp_bit = 0;
4707     }
4708     return cpu->dcz_blocksize | dzp_bit;
4709 }
4710 
4711 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4712                                     bool isread)
4713 {
4714     if (!(env->pstate & PSTATE_SP)) {
4715         /* Access to SP_EL0 is undefined if it's being used as
4716          * the stack pointer.
4717          */
4718         return CP_ACCESS_TRAP_UNCATEGORIZED;
4719     }
4720     return CP_ACCESS_OK;
4721 }
4722 
4723 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4724 {
4725     return env->pstate & PSTATE_SP;
4726 }
4727 
4728 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4729 {
4730     update_spsel(env, val);
4731 }
4732 
4733 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4734                         uint64_t value)
4735 {
4736     ARMCPU *cpu = env_archcpu(env);
4737 
4738     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4739         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4740         value &= ~SCTLR_M;
4741     }
4742 
4743     /* ??? Lots of these bits are not implemented.  */
4744 
4745     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4746         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4747             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4748         } else {
4749             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4750                        SCTLR_ATA0 | SCTLR_ATA);
4751         }
4752     }
4753 
4754     if (raw_read(env, ri) == value) {
4755         /* Skip the TLB flush if nothing actually changed; Linux likes
4756          * to do a lot of pointless SCTLR writes.
4757          */
4758         return;
4759     }
4760 
4761     raw_write(env, ri, value);
4762 
4763     /* This may enable/disable the MMU, so do a TLB flush.  */
4764     tlb_flush(CPU(cpu));
4765 
4766     if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4767         /*
4768          * Normally we would always end the TB on an SCTLR write; see the
4769          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4770          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4771          * of hflags from the translator, so do it here.
4772          */
4773         arm_rebuild_hflags(env);
4774     }
4775 }
4776 
4777 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4778                        uint64_t value)
4779 {
4780     env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4781 }
4782 
4783 static const ARMCPRegInfo v8_cp_reginfo[] = {
4784     /* Minimal set of EL0-visible registers. This will need to be expanded
4785      * significantly for system emulation of AArch64 CPUs.
4786      */
4787     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4788       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4789       .access = PL0_RW, .type = ARM_CP_NZCV },
4790     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4791       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4792       .type = ARM_CP_NO_RAW,
4793       .access = PL0_RW, .accessfn = aa64_daif_access,
4794       .fieldoffset = offsetof(CPUARMState, daif),
4795       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4796     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4797       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4798       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4799       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4800     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4801       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4802       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4803       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4804     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4805       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4806       .access = PL0_R, .type = ARM_CP_NO_RAW,
4807       .readfn = aa64_dczid_read },
4808     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4809       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4810       .access = PL0_W, .type = ARM_CP_DC_ZVA,
4811 #ifndef CONFIG_USER_ONLY
4812       /* Avoid overhead of an access check that always passes in user-mode */
4813       .accessfn = aa64_zva_access,
4814 #endif
4815     },
4816     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4817       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4818       .access = PL1_R, .type = ARM_CP_CURRENTEL },
4819     /* Cache ops: all NOPs since we don't emulate caches */
4820     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4821       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4822       .access = PL1_W, .type = ARM_CP_NOP,
4823       .accessfn = aa64_cacheop_pou_access },
4824     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4825       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4826       .access = PL1_W, .type = ARM_CP_NOP,
4827       .accessfn = aa64_cacheop_pou_access },
4828     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4829       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4830       .access = PL0_W, .type = ARM_CP_NOP,
4831       .accessfn = aa64_cacheop_pou_access },
4832     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4833       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4834       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4835       .type = ARM_CP_NOP },
4836     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4837       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4838       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4839     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4840       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4841       .access = PL0_W, .type = ARM_CP_NOP,
4842       .accessfn = aa64_cacheop_poc_access },
4843     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4844       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4845       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4846     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4847       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4848       .access = PL0_W, .type = ARM_CP_NOP,
4849       .accessfn = aa64_cacheop_pou_access },
4850     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4851       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4852       .access = PL0_W, .type = ARM_CP_NOP,
4853       .accessfn = aa64_cacheop_poc_access },
4854     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4855       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4856       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4857     /* TLBI operations */
4858     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4859       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4860       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4861       .writefn = tlbi_aa64_vmalle1is_write },
4862     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4863       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4864       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4865       .writefn = tlbi_aa64_vae1is_write },
4866     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4867       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4868       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4869       .writefn = tlbi_aa64_vmalle1is_write },
4870     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4871       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4872       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4873       .writefn = tlbi_aa64_vae1is_write },
4874     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4875       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4876       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4877       .writefn = tlbi_aa64_vae1is_write },
4878     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4879       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4880       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4881       .writefn = tlbi_aa64_vae1is_write },
4882     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4883       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4884       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4885       .writefn = tlbi_aa64_vmalle1_write },
4886     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4887       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4888       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4889       .writefn = tlbi_aa64_vae1_write },
4890     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4891       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4892       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4893       .writefn = tlbi_aa64_vmalle1_write },
4894     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4895       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4896       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4897       .writefn = tlbi_aa64_vae1_write },
4898     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4899       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4900       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4901       .writefn = tlbi_aa64_vae1_write },
4902     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4903       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4904       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4905       .writefn = tlbi_aa64_vae1_write },
4906     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4907       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4908       .access = PL2_W, .type = ARM_CP_NOP },
4909     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4910       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4911       .access = PL2_W, .type = ARM_CP_NOP },
4912     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4913       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4914       .access = PL2_W, .type = ARM_CP_NO_RAW,
4915       .writefn = tlbi_aa64_alle1is_write },
4916     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4917       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4918       .access = PL2_W, .type = ARM_CP_NO_RAW,
4919       .writefn = tlbi_aa64_alle1is_write },
4920     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4921       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4922       .access = PL2_W, .type = ARM_CP_NOP },
4923     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4924       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4925       .access = PL2_W, .type = ARM_CP_NOP },
4926     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4927       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4928       .access = PL2_W, .type = ARM_CP_NO_RAW,
4929       .writefn = tlbi_aa64_alle1_write },
4930     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4931       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4932       .access = PL2_W, .type = ARM_CP_NO_RAW,
4933       .writefn = tlbi_aa64_alle1is_write },
4934 #ifndef CONFIG_USER_ONLY
4935     /* 64 bit address translation operations */
4936     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4937       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4938       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4939       .writefn = ats_write64 },
4940     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4941       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4942       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4943       .writefn = ats_write64 },
4944     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4945       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4946       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4947       .writefn = ats_write64 },
4948     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4949       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4950       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4951       .writefn = ats_write64 },
4952     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4953       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4954       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4955       .writefn = ats_write64 },
4956     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4957       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4958       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4959       .writefn = ats_write64 },
4960     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4961       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4962       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4963       .writefn = ats_write64 },
4964     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4965       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4966       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4967       .writefn = ats_write64 },
4968     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4969     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4970       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4971       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4972       .writefn = ats_write64 },
4973     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4974       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4975       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4976       .writefn = ats_write64 },
4977     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4978       .type = ARM_CP_ALIAS,
4979       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4980       .access = PL1_RW, .resetvalue = 0,
4981       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4982       .writefn = par_write },
4983 #endif
4984     /* TLB invalidate last level of translation table walk */
4985     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4986       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4987       .writefn = tlbimva_is_write },
4988     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4989       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4990       .writefn = tlbimvaa_is_write },
4991     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4992       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4993       .writefn = tlbimva_write },
4994     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4995       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4996       .writefn = tlbimvaa_write },
4997     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4998       .type = ARM_CP_NO_RAW, .access = PL2_W,
4999       .writefn = tlbimva_hyp_write },
5000     { .name = "TLBIMVALHIS",
5001       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5002       .type = ARM_CP_NO_RAW, .access = PL2_W,
5003       .writefn = tlbimva_hyp_is_write },
5004     { .name = "TLBIIPAS2",
5005       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5006       .type = ARM_CP_NOP, .access = PL2_W },
5007     { .name = "TLBIIPAS2IS",
5008       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5009       .type = ARM_CP_NOP, .access = PL2_W },
5010     { .name = "TLBIIPAS2L",
5011       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5012       .type = ARM_CP_NOP, .access = PL2_W },
5013     { .name = "TLBIIPAS2LIS",
5014       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5015       .type = ARM_CP_NOP, .access = PL2_W },
5016     /* 32 bit cache operations */
5017     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5018       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5019     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5020       .type = ARM_CP_NOP, .access = PL1_W },
5021     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5022       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5023     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5024       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5025     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5026       .type = ARM_CP_NOP, .access = PL1_W },
5027     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5028       .type = ARM_CP_NOP, .access = PL1_W },
5029     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5030       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5031     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5032       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5033     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5034       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5035     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5036       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5037     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5038       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5039     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5040       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5041     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5042       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5043     /* MMU Domain access control / MPU write buffer control */
5044     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5045       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5046       .writefn = dacr_write, .raw_writefn = raw_write,
5047       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5048                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5049     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5050       .type = ARM_CP_ALIAS,
5051       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5052       .access = PL1_RW,
5053       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5054     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5055       .type = ARM_CP_ALIAS,
5056       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5057       .access = PL1_RW,
5058       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5059     /* We rely on the access checks not allowing the guest to write to the
5060      * state field when SPSel indicates that it's being used as the stack
5061      * pointer.
5062      */
5063     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5064       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5065       .access = PL1_RW, .accessfn = sp_el0_access,
5066       .type = ARM_CP_ALIAS,
5067       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5068     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5069       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5070       .access = PL2_RW, .type = ARM_CP_ALIAS,
5071       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5072     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5073       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5074       .type = ARM_CP_NO_RAW,
5075       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5076     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5077       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5078       .access = PL2_RW,
5079       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5080       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5081     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5082       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5083       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5084       .writefn = dacr_write, .raw_writefn = raw_write,
5085       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5086     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5087       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5088       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5089       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5090     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5091       .type = ARM_CP_ALIAS,
5092       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5093       .access = PL2_RW,
5094       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5095     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5096       .type = ARM_CP_ALIAS,
5097       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5098       .access = PL2_RW,
5099       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5100     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5101       .type = ARM_CP_ALIAS,
5102       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5103       .access = PL2_RW,
5104       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5105     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5106       .type = ARM_CP_ALIAS,
5107       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5108       .access = PL2_RW,
5109       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5110     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5111       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5112       .resetvalue = 0,
5113       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5114     { .name = "SDCR", .type = ARM_CP_ALIAS,
5115       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5116       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5117       .writefn = sdcr_write,
5118       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5119 };
5120 
5121 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5122 {
5123     ARMCPU *cpu = env_archcpu(env);
5124 
5125     if (arm_feature(env, ARM_FEATURE_V8)) {
5126         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5127     } else {
5128         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5129     }
5130 
5131     if (arm_feature(env, ARM_FEATURE_EL3)) {
5132         valid_mask &= ~HCR_HCD;
5133     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5134         /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5135          * However, if we're using the SMC PSCI conduit then QEMU is
5136          * effectively acting like EL3 firmware and so the guest at
5137          * EL2 should retain the ability to prevent EL1 from being
5138          * able to make SMC calls into the ersatz firmware, so in
5139          * that case HCR.TSC should be read/write.
5140          */
5141         valid_mask &= ~HCR_TSC;
5142     }
5143 
5144     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5145         if (cpu_isar_feature(aa64_vh, cpu)) {
5146             valid_mask |= HCR_E2H;
5147         }
5148         if (cpu_isar_feature(aa64_ras, cpu)) {
5149             valid_mask |= HCR_TERR | HCR_TEA;
5150         }
5151         if (cpu_isar_feature(aa64_lor, cpu)) {
5152             valid_mask |= HCR_TLOR;
5153         }
5154         if (cpu_isar_feature(aa64_pauth, cpu)) {
5155             valid_mask |= HCR_API | HCR_APK;
5156         }
5157         if (cpu_isar_feature(aa64_mte, cpu)) {
5158             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5159         }
5160         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5161             valid_mask |= HCR_ENSCXT;
5162         }
5163         if (cpu_isar_feature(aa64_fwb, cpu)) {
5164             valid_mask |= HCR_FWB;
5165         }
5166     }
5167 
5168     /* Clear RES0 bits.  */
5169     value &= valid_mask;
5170 
5171     /*
5172      * These bits change the MMU setup:
5173      * HCR_VM enables stage 2 translation
5174      * HCR_PTW forbids certain page-table setups
5175      * HCR_DC disables stage1 and enables stage2 translation
5176      * HCR_DCT enables tagging on (disabled) stage1 translation
5177      * HCR_FWB changes the interpretation of stage2 descriptor bits
5178      */
5179     if ((env->cp15.hcr_el2 ^ value) &
5180         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5181         tlb_flush(CPU(cpu));
5182     }
5183     env->cp15.hcr_el2 = value;
5184 
5185     /*
5186      * Updates to VI and VF require us to update the status of
5187      * virtual interrupts, which are the logical OR of these bits
5188      * and the state of the input lines from the GIC. (This requires
5189      * that we have the iothread lock, which is done by marking the
5190      * reginfo structs as ARM_CP_IO.)
5191      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5192      * possible for it to be taken immediately, because VIRQ and
5193      * VFIQ are masked unless running at EL0 or EL1, and HCR
5194      * can only be written at EL2.
5195      */
5196     g_assert(qemu_mutex_iothread_locked());
5197     arm_cpu_update_virq(cpu);
5198     arm_cpu_update_vfiq(cpu);
5199     arm_cpu_update_vserr(cpu);
5200 }
5201 
5202 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5203 {
5204     do_hcr_write(env, value, 0);
5205 }
5206 
5207 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5208                           uint64_t value)
5209 {
5210     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5211     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5212     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5213 }
5214 
5215 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5216                          uint64_t value)
5217 {
5218     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5219     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5220     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5221 }
5222 
5223 /*
5224  * Return the effective value of HCR_EL2.
5225  * Bits that are not included here:
5226  * RW       (read from SCR_EL3.RW as needed)
5227  */
5228 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5229 {
5230     uint64_t ret = env->cp15.hcr_el2;
5231 
5232     if (!arm_is_el2_enabled(env)) {
5233         /*
5234          * "This register has no effect if EL2 is not enabled in the
5235          * current Security state".  This is ARMv8.4-SecEL2 speak for
5236          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5237          *
5238          * Prior to that, the language was "In an implementation that
5239          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5240          * as if this field is 0 for all purposes other than a direct
5241          * read or write access of HCR_EL2".  With lots of enumeration
5242          * on a per-field basis.  In current QEMU, this is condition
5243          * is arm_is_secure_below_el3.
5244          *
5245          * Since the v8.4 language applies to the entire register, and
5246          * appears to be backward compatible, use that.
5247          */
5248         return 0;
5249     }
5250 
5251     /*
5252      * For a cpu that supports both aarch64 and aarch32, we can set bits
5253      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5254      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5255      */
5256     if (!arm_el_is_aa64(env, 2)) {
5257         uint64_t aa32_valid;
5258 
5259         /*
5260          * These bits are up-to-date as of ARMv8.6.
5261          * For HCR, it's easiest to list just the 2 bits that are invalid.
5262          * For HCR2, list those that are valid.
5263          */
5264         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5265         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5266                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5267         ret &= aa32_valid;
5268     }
5269 
5270     if (ret & HCR_TGE) {
5271         /* These bits are up-to-date as of ARMv8.6.  */
5272         if (ret & HCR_E2H) {
5273             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5274                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5275                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5276                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5277                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5278                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5279         } else {
5280             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5281         }
5282         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5283                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5284                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5285                  HCR_TLOR);
5286     }
5287 
5288     return ret;
5289 }
5290 
5291 /*
5292  * Corresponds to ARM pseudocode function ELIsInHost().
5293  */
5294 bool el_is_in_host(CPUARMState *env, int el)
5295 {
5296     uint64_t mask;
5297 
5298     /*
5299      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5300      * Perform the simplest bit tests first, and validate EL2 afterward.
5301      */
5302     if (el & 1) {
5303         return false; /* EL1 or EL3 */
5304     }
5305 
5306     /*
5307      * Note that hcr_write() checks isar_feature_aa64_vh(),
5308      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5309      */
5310     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5311     if ((env->cp15.hcr_el2 & mask) != mask) {
5312         return false;
5313     }
5314 
5315     /* TGE and/or E2H set: double check those bits are currently legal. */
5316     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5317 }
5318 
5319 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5320                        uint64_t value)
5321 {
5322     uint64_t valid_mask = 0;
5323 
5324     /* No features adding bits to HCRX are implemented. */
5325 
5326     /* Clear RES0 bits.  */
5327     env->cp15.hcrx_el2 = value & valid_mask;
5328 }
5329 
5330 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5331                                   bool isread)
5332 {
5333     if (arm_current_el(env) < 3
5334         && arm_feature(env, ARM_FEATURE_EL3)
5335         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5336         return CP_ACCESS_TRAP_EL3;
5337     }
5338     return CP_ACCESS_OK;
5339 }
5340 
5341 static const ARMCPRegInfo hcrx_el2_reginfo = {
5342     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5343     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5344     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5345     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5346 };
5347 
5348 /* Return the effective value of HCRX_EL2.  */
5349 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5350 {
5351     /*
5352      * The bits in this register behave as 0 for all purposes other than
5353      * direct reads of the register if:
5354      *   - EL2 is not enabled in the current security state,
5355      *   - SCR_EL3.HXEn is 0.
5356      */
5357     if (!arm_is_el2_enabled(env)
5358         || (arm_feature(env, ARM_FEATURE_EL3)
5359             && !(env->cp15.scr_el3 & SCR_HXEN))) {
5360         return 0;
5361     }
5362     return env->cp15.hcrx_el2;
5363 }
5364 
5365 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5366                            uint64_t value)
5367 {
5368     /*
5369      * For A-profile AArch32 EL3, if NSACR.CP10
5370      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5371      */
5372     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5373         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5374         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5375         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5376     }
5377     env->cp15.cptr_el[2] = value;
5378 }
5379 
5380 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5381 {
5382     /*
5383      * For A-profile AArch32 EL3, if NSACR.CP10
5384      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5385      */
5386     uint64_t value = env->cp15.cptr_el[2];
5387 
5388     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5389         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5390         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5391     }
5392     return value;
5393 }
5394 
5395 static const ARMCPRegInfo el2_cp_reginfo[] = {
5396     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5397       .type = ARM_CP_IO,
5398       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5399       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5400       .writefn = hcr_write },
5401     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5402       .type = ARM_CP_ALIAS | ARM_CP_IO,
5403       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5404       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5405       .writefn = hcr_writelow },
5406     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5407       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5408       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5409     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5410       .type = ARM_CP_ALIAS,
5411       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5412       .access = PL2_RW,
5413       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5414     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5415       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5416       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5417     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5418       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5419       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5420     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5421       .type = ARM_CP_ALIAS,
5422       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5423       .access = PL2_RW,
5424       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5425     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5426       .type = ARM_CP_ALIAS,
5427       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5428       .access = PL2_RW,
5429       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5430     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5431       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5432       .access = PL2_RW, .writefn = vbar_write,
5433       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5434       .resetvalue = 0 },
5435     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5436       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5437       .access = PL3_RW, .type = ARM_CP_ALIAS,
5438       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5439     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5440       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5441       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5442       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5443       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5444     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5445       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5446       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5447       .resetvalue = 0 },
5448     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5449       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5450       .access = PL2_RW, .type = ARM_CP_ALIAS,
5451       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5452     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5453       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5454       .access = PL2_RW, .type = ARM_CP_CONST,
5455       .resetvalue = 0 },
5456     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5457     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5458       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5459       .access = PL2_RW, .type = ARM_CP_CONST,
5460       .resetvalue = 0 },
5461     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5462       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5463       .access = PL2_RW, .type = ARM_CP_CONST,
5464       .resetvalue = 0 },
5465     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5466       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5467       .access = PL2_RW, .type = ARM_CP_CONST,
5468       .resetvalue = 0 },
5469     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5470       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5471       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5472       /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
5473       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5474     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5475       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5476       .type = ARM_CP_ALIAS,
5477       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5478       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5479     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5480       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5481       .access = PL2_RW,
5482       /* no .writefn needed as this can't cause an ASID change;
5483        * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5484        */
5485       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5486     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5487       .cp = 15, .opc1 = 6, .crm = 2,
5488       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5489       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5490       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5491       .writefn = vttbr_write },
5492     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5493       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5494       .access = PL2_RW, .writefn = vttbr_write,
5495       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5496     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5497       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5498       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5499       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5500     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5501       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5502       .access = PL2_RW, .resetvalue = 0,
5503       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5504     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5505       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5506       .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5507       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5508     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5509       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5510       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5511     { .name = "TLBIALLNSNH",
5512       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5513       .type = ARM_CP_NO_RAW, .access = PL2_W,
5514       .writefn = tlbiall_nsnh_write },
5515     { .name = "TLBIALLNSNHIS",
5516       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5517       .type = ARM_CP_NO_RAW, .access = PL2_W,
5518       .writefn = tlbiall_nsnh_is_write },
5519     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5520       .type = ARM_CP_NO_RAW, .access = PL2_W,
5521       .writefn = tlbiall_hyp_write },
5522     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5523       .type = ARM_CP_NO_RAW, .access = PL2_W,
5524       .writefn = tlbiall_hyp_is_write },
5525     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5526       .type = ARM_CP_NO_RAW, .access = PL2_W,
5527       .writefn = tlbimva_hyp_write },
5528     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5529       .type = ARM_CP_NO_RAW, .access = PL2_W,
5530       .writefn = tlbimva_hyp_is_write },
5531     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5532       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5533       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5534       .writefn = tlbi_aa64_alle2_write },
5535     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5536       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5537       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5538       .writefn = tlbi_aa64_vae2_write },
5539     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5540       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5541       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5542       .writefn = tlbi_aa64_vae2_write },
5543     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5544       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5545       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5546       .writefn = tlbi_aa64_alle2is_write },
5547     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5548       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5549       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5550       .writefn = tlbi_aa64_vae2is_write },
5551     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5552       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5553       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5554       .writefn = tlbi_aa64_vae2is_write },
5555 #ifndef CONFIG_USER_ONLY
5556     /* Unlike the other EL2-related AT operations, these must
5557      * UNDEF from EL3 if EL2 is not implemented, which is why we
5558      * define them here rather than with the rest of the AT ops.
5559      */
5560     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5561       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5562       .access = PL2_W, .accessfn = at_s1e2_access,
5563       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5564       .writefn = ats_write64 },
5565     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5566       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5567       .access = PL2_W, .accessfn = at_s1e2_access,
5568       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5569       .writefn = ats_write64 },
5570     /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5571      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5572      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5573      * to behave as if SCR.NS was 1.
5574      */
5575     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5576       .access = PL2_W,
5577       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5578     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5579       .access = PL2_W,
5580       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5581     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5582       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5583       /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5584        * reset values as IMPDEF. We choose to reset to 3 to comply with
5585        * both ARMv7 and ARMv8.
5586        */
5587       .access = PL2_RW, .resetvalue = 3,
5588       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5589     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5590       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5591       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5592       .writefn = gt_cntvoff_write,
5593       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5594     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5595       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5596       .writefn = gt_cntvoff_write,
5597       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5598     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5599       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5600       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5601       .type = ARM_CP_IO, .access = PL2_RW,
5602       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5603     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5604       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5605       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5606       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5607     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5608       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5609       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5610       .resetfn = gt_hyp_timer_reset,
5611       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5612     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5613       .type = ARM_CP_IO,
5614       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5615       .access = PL2_RW,
5616       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5617       .resetvalue = 0,
5618       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5619 #endif
5620     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5621       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5622       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5623       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5624     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5625       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5626       .access = PL2_RW,
5627       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5628     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5629       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5630       .access = PL2_RW,
5631       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5632 };
5633 
5634 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5635     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5636       .type = ARM_CP_ALIAS | ARM_CP_IO,
5637       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5638       .access = PL2_RW,
5639       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5640       .writefn = hcr_writehigh },
5641 };
5642 
5643 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5644                                   bool isread)
5645 {
5646     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5647         return CP_ACCESS_OK;
5648     }
5649     return CP_ACCESS_TRAP_UNCATEGORIZED;
5650 }
5651 
5652 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5653     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5654       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5655       .access = PL2_RW, .accessfn = sel2_access,
5656       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5657     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5658       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5659       .access = PL2_RW, .accessfn = sel2_access,
5660       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5661 };
5662 
5663 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5664                                    bool isread)
5665 {
5666     /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5667      * At Secure EL1 it traps to EL3 or EL2.
5668      */
5669     if (arm_current_el(env) == 3) {
5670         return CP_ACCESS_OK;
5671     }
5672     if (arm_is_secure_below_el3(env)) {
5673         if (env->cp15.scr_el3 & SCR_EEL2) {
5674             return CP_ACCESS_TRAP_EL2;
5675         }
5676         return CP_ACCESS_TRAP_EL3;
5677     }
5678     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5679     if (isread) {
5680         return CP_ACCESS_OK;
5681     }
5682     return CP_ACCESS_TRAP_UNCATEGORIZED;
5683 }
5684 
5685 static const ARMCPRegInfo el3_cp_reginfo[] = {
5686     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5687       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5688       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5689       .resetfn = scr_reset, .writefn = scr_write },
5690     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5691       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5692       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5693       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5694       .writefn = scr_write },
5695     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5696       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5697       .access = PL3_RW, .resetvalue = 0,
5698       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5699     { .name = "SDER",
5700       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5701       .access = PL3_RW, .resetvalue = 0,
5702       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5703     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5704       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5705       .writefn = vbar_write, .resetvalue = 0,
5706       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5707     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5708       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5709       .access = PL3_RW, .resetvalue = 0,
5710       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5711     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5712       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5713       .access = PL3_RW,
5714       /* no .writefn needed as this can't cause an ASID change;
5715        * we must provide a .raw_writefn and .resetfn because we handle
5716        * reset and migration for the AArch32 TTBCR(S), which might be
5717        * using mask and base_mask.
5718        */
5719       .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
5720       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5721     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5722       .type = ARM_CP_ALIAS,
5723       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5724       .access = PL3_RW,
5725       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5726     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5727       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5728       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5729     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5730       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5731       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5732     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5733       .type = ARM_CP_ALIAS,
5734       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5735       .access = PL3_RW,
5736       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5737     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5738       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5739       .access = PL3_RW, .writefn = vbar_write,
5740       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5741       .resetvalue = 0 },
5742     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5743       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5744       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5745       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5746     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5747       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5748       .access = PL3_RW, .resetvalue = 0,
5749       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5750     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5751       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5752       .access = PL3_RW, .type = ARM_CP_CONST,
5753       .resetvalue = 0 },
5754     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5755       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5756       .access = PL3_RW, .type = ARM_CP_CONST,
5757       .resetvalue = 0 },
5758     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5759       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5760       .access = PL3_RW, .type = ARM_CP_CONST,
5761       .resetvalue = 0 },
5762     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5763       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5764       .access = PL3_W, .type = ARM_CP_NO_RAW,
5765       .writefn = tlbi_aa64_alle3is_write },
5766     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5767       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5768       .access = PL3_W, .type = ARM_CP_NO_RAW,
5769       .writefn = tlbi_aa64_vae3is_write },
5770     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5771       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5772       .access = PL3_W, .type = ARM_CP_NO_RAW,
5773       .writefn = tlbi_aa64_vae3is_write },
5774     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5775       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5776       .access = PL3_W, .type = ARM_CP_NO_RAW,
5777       .writefn = tlbi_aa64_alle3_write },
5778     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5779       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5780       .access = PL3_W, .type = ARM_CP_NO_RAW,
5781       .writefn = tlbi_aa64_vae3_write },
5782     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5783       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5784       .access = PL3_W, .type = ARM_CP_NO_RAW,
5785       .writefn = tlbi_aa64_vae3_write },
5786 };
5787 
5788 #ifndef CONFIG_USER_ONLY
5789 /* Test if system register redirection is to occur in the current state.  */
5790 static bool redirect_for_e2h(CPUARMState *env)
5791 {
5792     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5793 }
5794 
5795 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5796 {
5797     CPReadFn *readfn;
5798 
5799     if (redirect_for_e2h(env)) {
5800         /* Switch to the saved EL2 version of the register.  */
5801         ri = ri->opaque;
5802         readfn = ri->readfn;
5803     } else {
5804         readfn = ri->orig_readfn;
5805     }
5806     if (readfn == NULL) {
5807         readfn = raw_read;
5808     }
5809     return readfn(env, ri);
5810 }
5811 
5812 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5813                           uint64_t value)
5814 {
5815     CPWriteFn *writefn;
5816 
5817     if (redirect_for_e2h(env)) {
5818         /* Switch to the saved EL2 version of the register.  */
5819         ri = ri->opaque;
5820         writefn = ri->writefn;
5821     } else {
5822         writefn = ri->orig_writefn;
5823     }
5824     if (writefn == NULL) {
5825         writefn = raw_write;
5826     }
5827     writefn(env, ri, value);
5828 }
5829 
5830 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5831 {
5832     struct E2HAlias {
5833         uint32_t src_key, dst_key, new_key;
5834         const char *src_name, *dst_name, *new_name;
5835         bool (*feature)(const ARMISARegisters *id);
5836     };
5837 
5838 #define K(op0, op1, crn, crm, op2) \
5839     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5840 
5841     static const struct E2HAlias aliases[] = {
5842         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
5843           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5844         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
5845           "CPACR", "CPTR_EL2", "CPACR_EL12" },
5846         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
5847           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5848         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
5849           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5850         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
5851           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5852         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
5853           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5854         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
5855           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5856         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
5857           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5858         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
5859           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5860         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
5861           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5862         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
5863           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5864         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5865           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5866         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5867           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5868         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5869           "VBAR", "VBAR_EL2", "VBAR_EL12" },
5870         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5871           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5872         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5873           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5874 
5875         /*
5876          * Note that redirection of ZCR is mentioned in the description
5877          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5878          * not in the summary table.
5879          */
5880         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
5881           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5882         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
5883           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
5884 
5885         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
5886           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5887 
5888         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5889           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5890           isar_feature_aa64_scxtnum },
5891 
5892         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5893         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5894     };
5895 #undef K
5896 
5897     size_t i;
5898 
5899     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5900         const struct E2HAlias *a = &aliases[i];
5901         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
5902         bool ok;
5903 
5904         if (a->feature && !a->feature(&cpu->isar)) {
5905             continue;
5906         }
5907 
5908         src_reg = g_hash_table_lookup(cpu->cp_regs,
5909                                       (gpointer)(uintptr_t)a->src_key);
5910         dst_reg = g_hash_table_lookup(cpu->cp_regs,
5911                                       (gpointer)(uintptr_t)a->dst_key);
5912         g_assert(src_reg != NULL);
5913         g_assert(dst_reg != NULL);
5914 
5915         /* Cross-compare names to detect typos in the keys.  */
5916         g_assert(strcmp(src_reg->name, a->src_name) == 0);
5917         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5918 
5919         /* None of the core system registers use opaque; we will.  */
5920         g_assert(src_reg->opaque == NULL);
5921 
5922         /* Create alias before redirection so we dup the right data. */
5923         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5924 
5925         new_reg->name = a->new_name;
5926         new_reg->type |= ARM_CP_ALIAS;
5927         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
5928         new_reg->access &= PL2_RW | PL3_RW;
5929 
5930         ok = g_hash_table_insert(cpu->cp_regs,
5931                                  (gpointer)(uintptr_t)a->new_key, new_reg);
5932         g_assert(ok);
5933 
5934         src_reg->opaque = dst_reg;
5935         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5936         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5937         if (!src_reg->raw_readfn) {
5938             src_reg->raw_readfn = raw_read;
5939         }
5940         if (!src_reg->raw_writefn) {
5941             src_reg->raw_writefn = raw_write;
5942         }
5943         src_reg->readfn = el2_e2h_read;
5944         src_reg->writefn = el2_e2h_write;
5945     }
5946 }
5947 #endif
5948 
5949 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5950                                      bool isread)
5951 {
5952     int cur_el = arm_current_el(env);
5953 
5954     if (cur_el < 2) {
5955         uint64_t hcr = arm_hcr_el2_eff(env);
5956 
5957         if (cur_el == 0) {
5958             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5959                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5960                     return CP_ACCESS_TRAP_EL2;
5961                 }
5962             } else {
5963                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5964                     return CP_ACCESS_TRAP;
5965                 }
5966                 if (hcr & HCR_TID2) {
5967                     return CP_ACCESS_TRAP_EL2;
5968                 }
5969             }
5970         } else if (hcr & HCR_TID2) {
5971             return CP_ACCESS_TRAP_EL2;
5972         }
5973     }
5974 
5975     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5976         return CP_ACCESS_TRAP_EL2;
5977     }
5978 
5979     return CP_ACCESS_OK;
5980 }
5981 
5982 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5983                         uint64_t value)
5984 {
5985     /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5986      * read via a bit in OSLSR_EL1.
5987      */
5988     int oslock;
5989 
5990     if (ri->state == ARM_CP_STATE_AA32) {
5991         oslock = (value == 0xC5ACCE55);
5992     } else {
5993         oslock = value & 1;
5994     }
5995 
5996     env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5997 }
5998 
5999 static const ARMCPRegInfo debug_cp_reginfo[] = {
6000     /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
6001      * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
6002      * unlike DBGDRAR it is never accessible from EL0.
6003      * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
6004      * accessor.
6005      */
6006     { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
6007       .access = PL0_R, .accessfn = access_tdra,
6008       .type = ARM_CP_CONST, .resetvalue = 0 },
6009     { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
6010       .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
6011       .access = PL1_R, .accessfn = access_tdra,
6012       .type = ARM_CP_CONST, .resetvalue = 0 },
6013     { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
6014       .access = PL0_R, .accessfn = access_tdra,
6015       .type = ARM_CP_CONST, .resetvalue = 0 },
6016     /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
6017     { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
6018       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
6019       .access = PL1_RW, .accessfn = access_tda,
6020       .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
6021       .resetvalue = 0 },
6022     /*
6023      * MDCCSR_EL0[30:29] map to EDSCR[30:29].  Simply RAZ as the external
6024      * Debug Communication Channel is not implemented.
6025      */
6026     { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_AA64,
6027       .opc0 = 2, .opc1 = 3, .crn = 0, .crm = 1, .opc2 = 0,
6028       .access = PL0_R, .accessfn = access_tda,
6029       .type = ARM_CP_CONST, .resetvalue = 0 },
6030     /*
6031      * DBGDSCRint[15,12,5:2] map to MDSCR_EL1[15,12,5:2].  Map all bits as
6032      * it is unlikely a guest will care.
6033      * We don't implement the configurable EL0 access.
6034      */
6035     { .name = "DBGDSCRint", .state = ARM_CP_STATE_AA32,
6036       .cp = 14, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
6037       .type = ARM_CP_ALIAS,
6038       .access = PL1_R, .accessfn = access_tda,
6039       .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
6040     { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
6041       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
6042       .access = PL1_W, .type = ARM_CP_NO_RAW,
6043       .accessfn = access_tdosa,
6044       .writefn = oslar_write },
6045     { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
6046       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
6047       .access = PL1_R, .resetvalue = 10,
6048       .accessfn = access_tdosa,
6049       .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
6050     /* Dummy OSDLR_EL1: 32-bit Linux will read this */
6051     { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
6052       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
6053       .access = PL1_RW, .accessfn = access_tdosa,
6054       .type = ARM_CP_NOP },
6055     /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
6056      * implement vector catch debug events yet.
6057      */
6058     { .name = "DBGVCR",
6059       .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
6060       .access = PL1_RW, .accessfn = access_tda,
6061       .type = ARM_CP_NOP },
6062     /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
6063      * to save and restore a 32-bit guest's DBGVCR)
6064      */
6065     { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
6066       .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
6067       .access = PL2_RW, .accessfn = access_tda,
6068       .type = ARM_CP_NOP | ARM_CP_EL3_NO_EL2_KEEP },
6069     /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
6070      * Channel but Linux may try to access this register. The 32-bit
6071      * alias is DBGDCCINT.
6072      */
6073     { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
6074       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6075       .access = PL1_RW, .accessfn = access_tda,
6076       .type = ARM_CP_NOP },
6077 };
6078 
6079 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6080     /* 64 bit access versions of the (dummy) debug registers */
6081     { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6082       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6083     { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6084       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6085 };
6086 
6087 /*
6088  * Check for traps to RAS registers, which are controlled
6089  * by HCR_EL2.TERR and SCR_EL3.TERR.
6090  */
6091 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6092                                   bool isread)
6093 {
6094     int el = arm_current_el(env);
6095 
6096     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6097         return CP_ACCESS_TRAP_EL2;
6098     }
6099     if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6100         return CP_ACCESS_TRAP_EL3;
6101     }
6102     return CP_ACCESS_OK;
6103 }
6104 
6105 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6106 {
6107     int el = arm_current_el(env);
6108 
6109     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6110         return env->cp15.vdisr_el2;
6111     }
6112     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6113         return 0; /* RAZ/WI */
6114     }
6115     return env->cp15.disr_el1;
6116 }
6117 
6118 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6119 {
6120     int el = arm_current_el(env);
6121 
6122     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6123         env->cp15.vdisr_el2 = val;
6124         return;
6125     }
6126     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6127         return; /* RAZ/WI */
6128     }
6129     env->cp15.disr_el1 = val;
6130 }
6131 
6132 /*
6133  * Minimal RAS implementation with no Error Records.
6134  * Which means that all of the Error Record registers:
6135  *   ERXADDR_EL1
6136  *   ERXCTLR_EL1
6137  *   ERXFR_EL1
6138  *   ERXMISC0_EL1
6139  *   ERXMISC1_EL1
6140  *   ERXMISC2_EL1
6141  *   ERXMISC3_EL1
6142  *   ERXPFGCDN_EL1  (RASv1p1)
6143  *   ERXPFGCTL_EL1  (RASv1p1)
6144  *   ERXPFGF_EL1    (RASv1p1)
6145  *   ERXSTATUS_EL1
6146  * and
6147  *   ERRSELR_EL1
6148  * may generate UNDEFINED, which is the effect we get by not
6149  * listing them at all.
6150  */
6151 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6152     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6153       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6154       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6155       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6156     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6157       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6158       .access = PL1_R, .accessfn = access_terr,
6159       .type = ARM_CP_CONST, .resetvalue = 0 },
6160     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6161       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6162       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6163     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6164       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6165       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6166 };
6167 
6168 /*
6169  * Return the exception level to which exceptions should be taken
6170  * via SVEAccessTrap.  This excludes the check for whether the exception
6171  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6172  * be found by testing 0 < fp_exception_el < sve_exception_el.
6173  *
6174  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6175  * pseudocode does *not* separate out the FP trap checks, but has them
6176  * all in one function.
6177  */
6178 int sve_exception_el(CPUARMState *env, int el)
6179 {
6180 #ifndef CONFIG_USER_ONLY
6181     if (el <= 1 && !el_is_in_host(env, el)) {
6182         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6183         case 1:
6184             if (el != 0) {
6185                 break;
6186             }
6187             /* fall through */
6188         case 0:
6189         case 2:
6190             return 1;
6191         }
6192     }
6193 
6194     if (el <= 2 && arm_is_el2_enabled(env)) {
6195         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6196         if (env->cp15.hcr_el2 & HCR_E2H) {
6197             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6198             case 1:
6199                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6200                     break;
6201                 }
6202                 /* fall through */
6203             case 0:
6204             case 2:
6205                 return 2;
6206             }
6207         } else {
6208             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6209                 return 2;
6210             }
6211         }
6212     }
6213 
6214     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6215     if (arm_feature(env, ARM_FEATURE_EL3)
6216         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6217         return 3;
6218     }
6219 #endif
6220     return 0;
6221 }
6222 
6223 /*
6224  * Return the exception level to which exceptions should be taken for SME.
6225  * C.f. the ARM pseudocode function CheckSMEAccess.
6226  */
6227 int sme_exception_el(CPUARMState *env, int el)
6228 {
6229 #ifndef CONFIG_USER_ONLY
6230     if (el <= 1 && !el_is_in_host(env, el)) {
6231         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6232         case 1:
6233             if (el != 0) {
6234                 break;
6235             }
6236             /* fall through */
6237         case 0:
6238         case 2:
6239             return 1;
6240         }
6241     }
6242 
6243     if (el <= 2 && arm_is_el2_enabled(env)) {
6244         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6245         if (env->cp15.hcr_el2 & HCR_E2H) {
6246             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6247             case 1:
6248                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6249                     break;
6250                 }
6251                 /* fall through */
6252             case 0:
6253             case 2:
6254                 return 2;
6255             }
6256         } else {
6257             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6258                 return 2;
6259             }
6260         }
6261     }
6262 
6263     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6264     if (arm_feature(env, ARM_FEATURE_EL3)
6265         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6266         return 3;
6267     }
6268 #endif
6269     return 0;
6270 }
6271 
6272 /*
6273  * Given that SVE is enabled, return the vector length for EL.
6274  */
6275 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6276 {
6277     ARMCPU *cpu = env_archcpu(env);
6278     uint64_t *cr = env->vfp.zcr_el;
6279     uint32_t map = cpu->sve_vq.map;
6280     uint32_t len = ARM_MAX_VQ - 1;
6281 
6282     if (sm) {
6283         cr = env->vfp.smcr_el;
6284         map = cpu->sme_vq.map;
6285     }
6286 
6287     if (el <= 1 && !el_is_in_host(env, el)) {
6288         len = MIN(len, 0xf & (uint32_t)cr[1]);
6289     }
6290     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6291         len = MIN(len, 0xf & (uint32_t)cr[2]);
6292     }
6293     if (arm_feature(env, ARM_FEATURE_EL3)) {
6294         len = MIN(len, 0xf & (uint32_t)cr[3]);
6295     }
6296 
6297     map &= MAKE_64BIT_MASK(0, len + 1);
6298     if (map != 0) {
6299         return 31 - clz32(map);
6300     }
6301 
6302     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6303     assert(sm);
6304     return ctz32(cpu->sme_vq.map);
6305 }
6306 
6307 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6308 {
6309     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6310 }
6311 
6312 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6313                       uint64_t value)
6314 {
6315     int cur_el = arm_current_el(env);
6316     int old_len = sve_vqm1_for_el(env, cur_el);
6317     int new_len;
6318 
6319     /* Bits other than [3:0] are RAZ/WI.  */
6320     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6321     raw_write(env, ri, value & 0xf);
6322 
6323     /*
6324      * Because we arrived here, we know both FP and SVE are enabled;
6325      * otherwise we would have trapped access to the ZCR_ELn register.
6326      */
6327     new_len = sve_vqm1_for_el(env, cur_el);
6328     if (new_len < old_len) {
6329         aarch64_sve_narrow_vq(env, new_len + 1);
6330     }
6331 }
6332 
6333 static const ARMCPRegInfo zcr_reginfo[] = {
6334     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6335       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6336       .access = PL1_RW, .type = ARM_CP_SVE,
6337       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6338       .writefn = zcr_write, .raw_writefn = raw_write },
6339     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6340       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6341       .access = PL2_RW, .type = ARM_CP_SVE,
6342       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6343       .writefn = zcr_write, .raw_writefn = raw_write },
6344     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6345       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6346       .access = PL3_RW, .type = ARM_CP_SVE,
6347       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6348       .writefn = zcr_write, .raw_writefn = raw_write },
6349 };
6350 
6351 #ifdef TARGET_AARCH64
6352 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6353                                     bool isread)
6354 {
6355     int el = arm_current_el(env);
6356 
6357     if (el == 0) {
6358         uint64_t sctlr = arm_sctlr(env, el);
6359         if (!(sctlr & SCTLR_EnTP2)) {
6360             return CP_ACCESS_TRAP;
6361         }
6362     }
6363     /* TODO: FEAT_FGT */
6364     if (el < 3
6365         && arm_feature(env, ARM_FEATURE_EL3)
6366         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6367         return CP_ACCESS_TRAP_EL3;
6368     }
6369     return CP_ACCESS_OK;
6370 }
6371 
6372 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6373                                  bool isread)
6374 {
6375     /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6376     if (arm_current_el(env) < 3
6377         && arm_feature(env, ARM_FEATURE_EL3)
6378         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6379         return CP_ACCESS_TRAP_EL3;
6380     }
6381     return CP_ACCESS_OK;
6382 }
6383 
6384 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6385                        uint64_t value)
6386 {
6387     helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6388     helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6389     arm_rebuild_hflags(env);
6390 }
6391 
6392 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6393                        uint64_t value)
6394 {
6395     int cur_el = arm_current_el(env);
6396     int old_len = sve_vqm1_for_el(env, cur_el);
6397     int new_len;
6398 
6399     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6400     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6401     raw_write(env, ri, value);
6402 
6403     /*
6404      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6405      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6406      * current values for simplicity.  But for QEMU internals, we must still
6407      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6408      * above aarch64_sve_narrow_vq.
6409      */
6410     new_len = sve_vqm1_for_el(env, cur_el);
6411     if (new_len < old_len) {
6412         aarch64_sve_narrow_vq(env, new_len + 1);
6413     }
6414 }
6415 
6416 static const ARMCPRegInfo sme_reginfo[] = {
6417     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6418       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6419       .access = PL0_RW, .accessfn = access_tpidr2,
6420       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6421     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6422       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6423       .access = PL0_RW, .type = ARM_CP_SME,
6424       .fieldoffset = offsetof(CPUARMState, svcr),
6425       .writefn = svcr_write, .raw_writefn = raw_write },
6426     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6427       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6428       .access = PL1_RW, .type = ARM_CP_SME,
6429       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6430       .writefn = smcr_write, .raw_writefn = raw_write },
6431     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6432       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6433       .access = PL2_RW, .type = ARM_CP_SME,
6434       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6435       .writefn = smcr_write, .raw_writefn = raw_write },
6436     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6437       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6438       .access = PL3_RW, .type = ARM_CP_SME,
6439       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6440       .writefn = smcr_write, .raw_writefn = raw_write },
6441     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6442       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6443       .access = PL1_R, .accessfn = access_aa64_tid1,
6444       /*
6445        * IMPLEMENTOR = 0 (software)
6446        * REVISION    = 0 (implementation defined)
6447        * SMPS        = 0 (no streaming execution priority in QEMU)
6448        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6449        */
6450       .type = ARM_CP_CONST, .resetvalue = 0, },
6451     /*
6452      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6453      */
6454     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6455       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6456       .access = PL1_RW, .accessfn = access_esm,
6457       .type = ARM_CP_CONST, .resetvalue = 0 },
6458     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6459       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6460       .access = PL2_RW, .accessfn = access_esm,
6461       .type = ARM_CP_CONST, .resetvalue = 0 },
6462 };
6463 #endif /* TARGET_AARCH64 */
6464 
6465 void hw_watchpoint_update(ARMCPU *cpu, int n)
6466 {
6467     CPUARMState *env = &cpu->env;
6468     vaddr len = 0;
6469     vaddr wvr = env->cp15.dbgwvr[n];
6470     uint64_t wcr = env->cp15.dbgwcr[n];
6471     int mask;
6472     int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6473 
6474     if (env->cpu_watchpoint[n]) {
6475         cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6476         env->cpu_watchpoint[n] = NULL;
6477     }
6478 
6479     if (!FIELD_EX64(wcr, DBGWCR, E)) {
6480         /* E bit clear : watchpoint disabled */
6481         return;
6482     }
6483 
6484     switch (FIELD_EX64(wcr, DBGWCR, LSC)) {
6485     case 0:
6486         /* LSC 00 is reserved and must behave as if the wp is disabled */
6487         return;
6488     case 1:
6489         flags |= BP_MEM_READ;
6490         break;
6491     case 2:
6492         flags |= BP_MEM_WRITE;
6493         break;
6494     case 3:
6495         flags |= BP_MEM_ACCESS;
6496         break;
6497     }
6498 
6499     /* Attempts to use both MASK and BAS fields simultaneously are
6500      * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6501      * thus generating a watchpoint for every byte in the masked region.
6502      */
6503     mask = FIELD_EX64(wcr, DBGWCR, MASK);
6504     if (mask == 1 || mask == 2) {
6505         /* Reserved values of MASK; we must act as if the mask value was
6506          * some non-reserved value, or as if the watchpoint were disabled.
6507          * We choose the latter.
6508          */
6509         return;
6510     } else if (mask) {
6511         /* Watchpoint covers an aligned area up to 2GB in size */
6512         len = 1ULL << mask;
6513         /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6514          * whether the watchpoint fires when the unmasked bits match; we opt
6515          * to generate the exceptions.
6516          */
6517         wvr &= ~(len - 1);
6518     } else {
6519         /* Watchpoint covers bytes defined by the byte address select bits */
6520         int bas = FIELD_EX64(wcr, DBGWCR, BAS);
6521         int basstart;
6522 
6523         if (extract64(wvr, 2, 1)) {
6524             /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6525              * ignored, and BAS[3:0] define which bytes to watch.
6526              */
6527             bas &= 0xf;
6528         }
6529 
6530         if (bas == 0) {
6531             /* This must act as if the watchpoint is disabled */
6532             return;
6533         }
6534 
6535         /* The BAS bits are supposed to be programmed to indicate a contiguous
6536          * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6537          * we fire for each byte in the word/doubleword addressed by the WVR.
6538          * We choose to ignore any non-zero bits after the first range of 1s.
6539          */
6540         basstart = ctz32(bas);
6541         len = cto32(bas >> basstart);
6542         wvr += basstart;
6543     }
6544 
6545     cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6546                           &env->cpu_watchpoint[n]);
6547 }
6548 
6549 void hw_watchpoint_update_all(ARMCPU *cpu)
6550 {
6551     int i;
6552     CPUARMState *env = &cpu->env;
6553 
6554     /* Completely clear out existing QEMU watchpoints and our array, to
6555      * avoid possible stale entries following migration load.
6556      */
6557     cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6558     memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6559 
6560     for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6561         hw_watchpoint_update(cpu, i);
6562     }
6563 }
6564 
6565 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6566                          uint64_t value)
6567 {
6568     ARMCPU *cpu = env_archcpu(env);
6569     int i = ri->crm;
6570 
6571     /*
6572      * Bits [1:0] are RES0.
6573      *
6574      * It is IMPLEMENTATION DEFINED whether [63:49] ([63:53] with FEAT_LVA)
6575      * are hardwired to the value of bit [48] ([52] with FEAT_LVA), or if
6576      * they contain the value written.  It is CONSTRAINED UNPREDICTABLE
6577      * whether the RESS bits are ignored when comparing an address.
6578      *
6579      * Therefore we are allowed to compare the entire register, which lets
6580      * us avoid considering whether or not FEAT_LVA is actually enabled.
6581      */
6582     value &= ~3ULL;
6583 
6584     raw_write(env, ri, value);
6585     hw_watchpoint_update(cpu, i);
6586 }
6587 
6588 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6589                          uint64_t value)
6590 {
6591     ARMCPU *cpu = env_archcpu(env);
6592     int i = ri->crm;
6593 
6594     raw_write(env, ri, value);
6595     hw_watchpoint_update(cpu, i);
6596 }
6597 
6598 void hw_breakpoint_update(ARMCPU *cpu, int n)
6599 {
6600     CPUARMState *env = &cpu->env;
6601     uint64_t bvr = env->cp15.dbgbvr[n];
6602     uint64_t bcr = env->cp15.dbgbcr[n];
6603     vaddr addr;
6604     int bt;
6605     int flags = BP_CPU;
6606 
6607     if (env->cpu_breakpoint[n]) {
6608         cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6609         env->cpu_breakpoint[n] = NULL;
6610     }
6611 
6612     if (!extract64(bcr, 0, 1)) {
6613         /* E bit clear : watchpoint disabled */
6614         return;
6615     }
6616 
6617     bt = extract64(bcr, 20, 4);
6618 
6619     switch (bt) {
6620     case 4: /* unlinked address mismatch (reserved if AArch64) */
6621     case 5: /* linked address mismatch (reserved if AArch64) */
6622         qemu_log_mask(LOG_UNIMP,
6623                       "arm: address mismatch breakpoint types not implemented\n");
6624         return;
6625     case 0: /* unlinked address match */
6626     case 1: /* linked address match */
6627     {
6628         /*
6629          * Bits [1:0] are RES0.
6630          *
6631          * It is IMPLEMENTATION DEFINED whether bits [63:49]
6632          * ([63:53] for FEAT_LVA) are hardwired to a copy of the sign bit
6633          * of the VA field ([48] or [52] for FEAT_LVA), or whether the
6634          * value is read as written.  It is CONSTRAINED UNPREDICTABLE
6635          * whether the RESS bits are ignored when comparing an address.
6636          * Therefore we are allowed to compare the entire register, which
6637          * lets us avoid considering whether FEAT_LVA is actually enabled.
6638          *
6639          * The BAS field is used to allow setting breakpoints on 16-bit
6640          * wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6641          * a bp will fire if the addresses covered by the bp and the addresses
6642          * covered by the insn overlap but the insn doesn't start at the
6643          * start of the bp address range. We choose to require the insn and
6644          * the bp to have the same address. The constraints on writing to
6645          * BAS enforced in dbgbcr_write mean we have only four cases:
6646          *  0b0000  => no breakpoint
6647          *  0b0011  => breakpoint on addr
6648          *  0b1100  => breakpoint on addr + 2
6649          *  0b1111  => breakpoint on addr
6650          * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6651          */
6652         int bas = extract64(bcr, 5, 4);
6653         addr = bvr & ~3ULL;
6654         if (bas == 0) {
6655             return;
6656         }
6657         if (bas == 0xc) {
6658             addr += 2;
6659         }
6660         break;
6661     }
6662     case 2: /* unlinked context ID match */
6663     case 8: /* unlinked VMID match (reserved if no EL2) */
6664     case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6665         qemu_log_mask(LOG_UNIMP,
6666                       "arm: unlinked context breakpoint types not implemented\n");
6667         return;
6668     case 9: /* linked VMID match (reserved if no EL2) */
6669     case 11: /* linked context ID and VMID match (reserved if no EL2) */
6670     case 3: /* linked context ID match */
6671     default:
6672         /* We must generate no events for Linked context matches (unless
6673          * they are linked to by some other bp/wp, which is handled in
6674          * updates for the linking bp/wp). We choose to also generate no events
6675          * for reserved values.
6676          */
6677         return;
6678     }
6679 
6680     cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6681 }
6682 
6683 void hw_breakpoint_update_all(ARMCPU *cpu)
6684 {
6685     int i;
6686     CPUARMState *env = &cpu->env;
6687 
6688     /* Completely clear out existing QEMU breakpoints and our array, to
6689      * avoid possible stale entries following migration load.
6690      */
6691     cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6692     memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6693 
6694     for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6695         hw_breakpoint_update(cpu, i);
6696     }
6697 }
6698 
6699 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6700                          uint64_t value)
6701 {
6702     ARMCPU *cpu = env_archcpu(env);
6703     int i = ri->crm;
6704 
6705     raw_write(env, ri, value);
6706     hw_breakpoint_update(cpu, i);
6707 }
6708 
6709 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6710                          uint64_t value)
6711 {
6712     ARMCPU *cpu = env_archcpu(env);
6713     int i = ri->crm;
6714 
6715     /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6716      * copy of BAS[0].
6717      */
6718     value = deposit64(value, 6, 1, extract64(value, 5, 1));
6719     value = deposit64(value, 8, 1, extract64(value, 7, 1));
6720 
6721     raw_write(env, ri, value);
6722     hw_breakpoint_update(cpu, i);
6723 }
6724 
6725 static void define_debug_regs(ARMCPU *cpu)
6726 {
6727     /* Define v7 and v8 architectural debug registers.
6728      * These are just dummy implementations for now.
6729      */
6730     int i;
6731     int wrps, brps, ctx_cmps;
6732 
6733     /*
6734      * The Arm ARM says DBGDIDR is optional and deprecated if EL1 cannot
6735      * use AArch32.  Given that bit 15 is RES1, if the value is 0 then
6736      * the register must not exist for this cpu.
6737      */
6738     if (cpu->isar.dbgdidr != 0) {
6739         ARMCPRegInfo dbgdidr = {
6740             .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0,
6741             .opc1 = 0, .opc2 = 0,
6742             .access = PL0_R, .accessfn = access_tda,
6743             .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
6744         };
6745         define_one_arm_cp_reg(cpu, &dbgdidr);
6746     }
6747 
6748     brps = arm_num_brps(cpu);
6749     wrps = arm_num_wrps(cpu);
6750     ctx_cmps = arm_num_ctx_cmps(cpu);
6751 
6752     assert(ctx_cmps <= brps);
6753 
6754     define_arm_cp_regs(cpu, debug_cp_reginfo);
6755 
6756     if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6757         define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6758     }
6759 
6760     for (i = 0; i < brps; i++) {
6761         char *dbgbvr_el1_name = g_strdup_printf("DBGBVR%d_EL1", i);
6762         char *dbgbcr_el1_name = g_strdup_printf("DBGBCR%d_EL1", i);
6763         ARMCPRegInfo dbgregs[] = {
6764             { .name = dbgbvr_el1_name, .state = ARM_CP_STATE_BOTH,
6765               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
6766               .access = PL1_RW, .accessfn = access_tda,
6767               .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6768               .writefn = dbgbvr_write, .raw_writefn = raw_write
6769             },
6770             { .name = dbgbcr_el1_name, .state = ARM_CP_STATE_BOTH,
6771               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
6772               .access = PL1_RW, .accessfn = access_tda,
6773               .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6774               .writefn = dbgbcr_write, .raw_writefn = raw_write
6775             },
6776         };
6777         define_arm_cp_regs(cpu, dbgregs);
6778         g_free(dbgbvr_el1_name);
6779         g_free(dbgbcr_el1_name);
6780     }
6781 
6782     for (i = 0; i < wrps; i++) {
6783         char *dbgwvr_el1_name = g_strdup_printf("DBGWVR%d_EL1", i);
6784         char *dbgwcr_el1_name = g_strdup_printf("DBGWCR%d_EL1", i);
6785         ARMCPRegInfo dbgregs[] = {
6786             { .name = dbgwvr_el1_name, .state = ARM_CP_STATE_BOTH,
6787               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
6788               .access = PL1_RW, .accessfn = access_tda,
6789               .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6790               .writefn = dbgwvr_write, .raw_writefn = raw_write
6791             },
6792             { .name = dbgwcr_el1_name, .state = ARM_CP_STATE_BOTH,
6793               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
6794               .access = PL1_RW, .accessfn = access_tda,
6795               .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6796               .writefn = dbgwcr_write, .raw_writefn = raw_write
6797             },
6798         };
6799         define_arm_cp_regs(cpu, dbgregs);
6800         g_free(dbgwvr_el1_name);
6801         g_free(dbgwcr_el1_name);
6802     }
6803 }
6804 
6805 static void define_pmu_regs(ARMCPU *cpu)
6806 {
6807     /*
6808      * v7 performance monitor control register: same implementor
6809      * field as main ID register, and we implement four counters in
6810      * addition to the cycle count register.
6811      */
6812     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6813     ARMCPRegInfo pmcr = {
6814         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6815         .access = PL0_RW,
6816         .type = ARM_CP_IO | ARM_CP_ALIAS,
6817         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6818         .accessfn = pmreg_access, .writefn = pmcr_write,
6819         .raw_writefn = raw_write,
6820     };
6821     ARMCPRegInfo pmcr64 = {
6822         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6823         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6824         .access = PL0_RW, .accessfn = pmreg_access,
6825         .type = ARM_CP_IO,
6826         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6827         .resetvalue = cpu->isar.reset_pmcr_el0,
6828         .writefn = pmcr_write, .raw_writefn = raw_write,
6829     };
6830 
6831     define_one_arm_cp_reg(cpu, &pmcr);
6832     define_one_arm_cp_reg(cpu, &pmcr64);
6833     for (i = 0; i < pmcrn; i++) {
6834         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6835         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6836         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6837         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6838         ARMCPRegInfo pmev_regs[] = {
6839             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6840               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6841               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6842               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6843               .accessfn = pmreg_access_xevcntr },
6844             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6845               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6846               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6847               .type = ARM_CP_IO,
6848               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6849               .raw_readfn = pmevcntr_rawread,
6850               .raw_writefn = pmevcntr_rawwrite },
6851             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6852               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6853               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6854               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6855               .accessfn = pmreg_access },
6856             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6857               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6858               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6859               .type = ARM_CP_IO,
6860               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6861               .raw_writefn = pmevtyper_rawwrite },
6862         };
6863         define_arm_cp_regs(cpu, pmev_regs);
6864         g_free(pmevcntr_name);
6865         g_free(pmevcntr_el0_name);
6866         g_free(pmevtyper_name);
6867         g_free(pmevtyper_el0_name);
6868     }
6869     if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
6870         ARMCPRegInfo v81_pmu_regs[] = {
6871             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6872               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6873               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6874               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6875             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6876               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6877               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6878               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6879         };
6880         define_arm_cp_regs(cpu, v81_pmu_regs);
6881     }
6882     if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6883         static const ARMCPRegInfo v84_pmmir = {
6884             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6885             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6886             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6887             .resetvalue = 0
6888         };
6889         define_one_arm_cp_reg(cpu, &v84_pmmir);
6890     }
6891 }
6892 
6893 /* We don't know until after realize whether there's a GICv3
6894  * attached, and that is what registers the gicv3 sysregs.
6895  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6896  * at runtime.
6897  */
6898 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6899 {
6900     ARMCPU *cpu = env_archcpu(env);
6901     uint64_t pfr1 = cpu->isar.id_pfr1;
6902 
6903     if (env->gicv3state) {
6904         pfr1 |= 1 << 28;
6905     }
6906     return pfr1;
6907 }
6908 
6909 #ifndef CONFIG_USER_ONLY
6910 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6911 {
6912     ARMCPU *cpu = env_archcpu(env);
6913     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6914 
6915     if (env->gicv3state) {
6916         pfr0 |= 1 << 24;
6917     }
6918     return pfr0;
6919 }
6920 #endif
6921 
6922 /* Shared logic between LORID and the rest of the LOR* registers.
6923  * Secure state exclusion has already been dealt with.
6924  */
6925 static CPAccessResult access_lor_ns(CPUARMState *env,
6926                                     const ARMCPRegInfo *ri, bool isread)
6927 {
6928     int el = arm_current_el(env);
6929 
6930     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6931         return CP_ACCESS_TRAP_EL2;
6932     }
6933     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6934         return CP_ACCESS_TRAP_EL3;
6935     }
6936     return CP_ACCESS_OK;
6937 }
6938 
6939 static CPAccessResult access_lor_other(CPUARMState *env,
6940                                        const ARMCPRegInfo *ri, bool isread)
6941 {
6942     if (arm_is_secure_below_el3(env)) {
6943         /* Access denied in secure mode.  */
6944         return CP_ACCESS_TRAP;
6945     }
6946     return access_lor_ns(env, ri, isread);
6947 }
6948 
6949 /*
6950  * A trivial implementation of ARMv8.1-LOR leaves all of these
6951  * registers fixed at 0, which indicates that there are zero
6952  * supported Limited Ordering regions.
6953  */
6954 static const ARMCPRegInfo lor_reginfo[] = {
6955     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6956       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6957       .access = PL1_RW, .accessfn = access_lor_other,
6958       .type = ARM_CP_CONST, .resetvalue = 0 },
6959     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6960       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6961       .access = PL1_RW, .accessfn = access_lor_other,
6962       .type = ARM_CP_CONST, .resetvalue = 0 },
6963     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6964       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6965       .access = PL1_RW, .accessfn = access_lor_other,
6966       .type = ARM_CP_CONST, .resetvalue = 0 },
6967     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6968       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6969       .access = PL1_RW, .accessfn = access_lor_other,
6970       .type = ARM_CP_CONST, .resetvalue = 0 },
6971     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6972       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6973       .access = PL1_R, .accessfn = access_lor_ns,
6974       .type = ARM_CP_CONST, .resetvalue = 0 },
6975 };
6976 
6977 #ifdef TARGET_AARCH64
6978 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6979                                    bool isread)
6980 {
6981     int el = arm_current_el(env);
6982 
6983     if (el < 2 &&
6984         arm_is_el2_enabled(env) &&
6985         !(arm_hcr_el2_eff(env) & HCR_APK)) {
6986         return CP_ACCESS_TRAP_EL2;
6987     }
6988     if (el < 3 &&
6989         arm_feature(env, ARM_FEATURE_EL3) &&
6990         !(env->cp15.scr_el3 & SCR_APK)) {
6991         return CP_ACCESS_TRAP_EL3;
6992     }
6993     return CP_ACCESS_OK;
6994 }
6995 
6996 static const ARMCPRegInfo pauth_reginfo[] = {
6997     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6998       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6999       .access = PL1_RW, .accessfn = access_pauth,
7000       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7001     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7002       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7003       .access = PL1_RW, .accessfn = access_pauth,
7004       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7005     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7006       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7007       .access = PL1_RW, .accessfn = access_pauth,
7008       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7009     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7010       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7011       .access = PL1_RW, .accessfn = access_pauth,
7012       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7013     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7014       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7015       .access = PL1_RW, .accessfn = access_pauth,
7016       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7017     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7018       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7019       .access = PL1_RW, .accessfn = access_pauth,
7020       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7021     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7022       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7023       .access = PL1_RW, .accessfn = access_pauth,
7024       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7025     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7026       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7027       .access = PL1_RW, .accessfn = access_pauth,
7028       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7029     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7030       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7031       .access = PL1_RW, .accessfn = access_pauth,
7032       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7033     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7034       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7035       .access = PL1_RW, .accessfn = access_pauth,
7036       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7037 };
7038 
7039 static const ARMCPRegInfo tlbirange_reginfo[] = {
7040     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7041       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7042       .access = PL1_W, .type = ARM_CP_NO_RAW,
7043       .writefn = tlbi_aa64_rvae1is_write },
7044     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7045       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7046       .access = PL1_W, .type = ARM_CP_NO_RAW,
7047       .writefn = tlbi_aa64_rvae1is_write },
7048    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7049       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7050       .access = PL1_W, .type = ARM_CP_NO_RAW,
7051       .writefn = tlbi_aa64_rvae1is_write },
7052     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7053       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7054       .access = PL1_W, .type = ARM_CP_NO_RAW,
7055       .writefn = tlbi_aa64_rvae1is_write },
7056     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7057       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7058       .access = PL1_W, .type = ARM_CP_NO_RAW,
7059       .writefn = tlbi_aa64_rvae1is_write },
7060     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7061       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7062       .access = PL1_W, .type = ARM_CP_NO_RAW,
7063       .writefn = tlbi_aa64_rvae1is_write },
7064    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7065       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7066       .access = PL1_W, .type = ARM_CP_NO_RAW,
7067       .writefn = tlbi_aa64_rvae1is_write },
7068     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7069       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7070       .access = PL1_W, .type = ARM_CP_NO_RAW,
7071       .writefn = tlbi_aa64_rvae1is_write },
7072     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7073       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7074       .access = PL1_W, .type = ARM_CP_NO_RAW,
7075       .writefn = tlbi_aa64_rvae1_write },
7076     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7077       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7078       .access = PL1_W, .type = ARM_CP_NO_RAW,
7079       .writefn = tlbi_aa64_rvae1_write },
7080    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7081       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7082       .access = PL1_W, .type = ARM_CP_NO_RAW,
7083       .writefn = tlbi_aa64_rvae1_write },
7084     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7085       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7086       .access = PL1_W, .type = ARM_CP_NO_RAW,
7087       .writefn = tlbi_aa64_rvae1_write },
7088     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7089       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7090       .access = PL2_W, .type = ARM_CP_NOP },
7091     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7092       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7093       .access = PL2_W, .type = ARM_CP_NOP },
7094     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7095       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7096       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7097       .writefn = tlbi_aa64_rvae2is_write },
7098    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7099       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7100       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7101       .writefn = tlbi_aa64_rvae2is_write },
7102     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7103       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7104       .access = PL2_W, .type = ARM_CP_NOP },
7105    { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7106       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7107       .access = PL2_W, .type = ARM_CP_NOP },
7108    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7109       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7110       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7111       .writefn = tlbi_aa64_rvae2is_write },
7112    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7113       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7114       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7115       .writefn = tlbi_aa64_rvae2is_write },
7116     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7117       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7118       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7119       .writefn = tlbi_aa64_rvae2_write },
7120    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7121       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7122       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7123       .writefn = tlbi_aa64_rvae2_write },
7124    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7125       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7126       .access = PL3_W, .type = ARM_CP_NO_RAW,
7127       .writefn = tlbi_aa64_rvae3is_write },
7128    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7129       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7130       .access = PL3_W, .type = ARM_CP_NO_RAW,
7131       .writefn = tlbi_aa64_rvae3is_write },
7132    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7133       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7134       .access = PL3_W, .type = ARM_CP_NO_RAW,
7135       .writefn = tlbi_aa64_rvae3is_write },
7136    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7137       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7138       .access = PL3_W, .type = ARM_CP_NO_RAW,
7139       .writefn = tlbi_aa64_rvae3is_write },
7140    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7141       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7142       .access = PL3_W, .type = ARM_CP_NO_RAW,
7143       .writefn = tlbi_aa64_rvae3_write },
7144    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7145       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7146       .access = PL3_W, .type = ARM_CP_NO_RAW,
7147       .writefn = tlbi_aa64_rvae3_write },
7148 };
7149 
7150 static const ARMCPRegInfo tlbios_reginfo[] = {
7151     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7152       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7153       .access = PL1_W, .type = ARM_CP_NO_RAW,
7154       .writefn = tlbi_aa64_vmalle1is_write },
7155     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7156       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7157       .access = PL1_W, .type = ARM_CP_NO_RAW,
7158       .writefn = tlbi_aa64_vae1is_write },
7159     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7160       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7161       .access = PL1_W, .type = ARM_CP_NO_RAW,
7162       .writefn = tlbi_aa64_vmalle1is_write },
7163     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7164       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7165       .access = PL1_W, .type = ARM_CP_NO_RAW,
7166       .writefn = tlbi_aa64_vae1is_write },
7167     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7168       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7169       .access = PL1_W, .type = ARM_CP_NO_RAW,
7170       .writefn = tlbi_aa64_vae1is_write },
7171     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7172       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7173       .access = PL1_W, .type = ARM_CP_NO_RAW,
7174       .writefn = tlbi_aa64_vae1is_write },
7175     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7176       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7177       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7178       .writefn = tlbi_aa64_alle2is_write },
7179     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7180       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7181       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7182       .writefn = tlbi_aa64_vae2is_write },
7183    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7184       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7185       .access = PL2_W, .type = ARM_CP_NO_RAW,
7186       .writefn = tlbi_aa64_alle1is_write },
7187     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7188       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7189       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7190       .writefn = tlbi_aa64_vae2is_write },
7191     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7192       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7193       .access = PL2_W, .type = ARM_CP_NO_RAW,
7194       .writefn = tlbi_aa64_alle1is_write },
7195     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7196       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7197       .access = PL2_W, .type = ARM_CP_NOP },
7198     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7199       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7200       .access = PL2_W, .type = ARM_CP_NOP },
7201     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7202       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7203       .access = PL2_W, .type = ARM_CP_NOP },
7204     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7205       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7206       .access = PL2_W, .type = ARM_CP_NOP },
7207     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7208       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7209       .access = PL3_W, .type = ARM_CP_NO_RAW,
7210       .writefn = tlbi_aa64_alle3is_write },
7211     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7212       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7213       .access = PL3_W, .type = ARM_CP_NO_RAW,
7214       .writefn = tlbi_aa64_vae3is_write },
7215     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7216       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7217       .access = PL3_W, .type = ARM_CP_NO_RAW,
7218       .writefn = tlbi_aa64_vae3is_write },
7219 };
7220 
7221 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7222 {
7223     Error *err = NULL;
7224     uint64_t ret;
7225 
7226     /* Success sets NZCV = 0000.  */
7227     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7228 
7229     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7230         /*
7231          * ??? Failed, for unknown reasons in the crypto subsystem.
7232          * The best we can do is log the reason and return the
7233          * timed-out indication to the guest.  There is no reason
7234          * we know to expect this failure to be transitory, so the
7235          * guest may well hang retrying the operation.
7236          */
7237         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7238                       ri->name, error_get_pretty(err));
7239         error_free(err);
7240 
7241         env->ZF = 0; /* NZCF = 0100 */
7242         return 0;
7243     }
7244     return ret;
7245 }
7246 
7247 /* We do not support re-seeding, so the two registers operate the same.  */
7248 static const ARMCPRegInfo rndr_reginfo[] = {
7249     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7250       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7251       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7252       .access = PL0_R, .readfn = rndr_readfn },
7253     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7254       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7255       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7256       .access = PL0_R, .readfn = rndr_readfn },
7257 };
7258 
7259 #ifndef CONFIG_USER_ONLY
7260 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7261                           uint64_t value)
7262 {
7263     ARMCPU *cpu = env_archcpu(env);
7264     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7265     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7266     uint64_t vaddr_in = (uint64_t) value;
7267     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7268     void *haddr;
7269     int mem_idx = cpu_mmu_index(env, false);
7270 
7271     /* This won't be crossing page boundaries */
7272     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7273     if (haddr) {
7274 
7275         ram_addr_t offset;
7276         MemoryRegion *mr;
7277 
7278         /* RCU lock is already being held */
7279         mr = memory_region_from_host(haddr, &offset);
7280 
7281         if (mr) {
7282             memory_region_writeback(mr, offset, dline_size);
7283         }
7284     }
7285 }
7286 
7287 static const ARMCPRegInfo dcpop_reg[] = {
7288     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7289       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7290       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7291       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7292 };
7293 
7294 static const ARMCPRegInfo dcpodp_reg[] = {
7295     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7296       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7297       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7298       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7299 };
7300 #endif /*CONFIG_USER_ONLY*/
7301 
7302 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7303                                        bool isread)
7304 {
7305     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7306         return CP_ACCESS_TRAP_EL2;
7307     }
7308 
7309     return CP_ACCESS_OK;
7310 }
7311 
7312 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7313                                  bool isread)
7314 {
7315     int el = arm_current_el(env);
7316 
7317     if (el < 2 && arm_is_el2_enabled(env)) {
7318         uint64_t hcr = arm_hcr_el2_eff(env);
7319         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7320             return CP_ACCESS_TRAP_EL2;
7321         }
7322     }
7323     if (el < 3 &&
7324         arm_feature(env, ARM_FEATURE_EL3) &&
7325         !(env->cp15.scr_el3 & SCR_ATA)) {
7326         return CP_ACCESS_TRAP_EL3;
7327     }
7328     return CP_ACCESS_OK;
7329 }
7330 
7331 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7332 {
7333     return env->pstate & PSTATE_TCO;
7334 }
7335 
7336 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7337 {
7338     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7339 }
7340 
7341 static const ARMCPRegInfo mte_reginfo[] = {
7342     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7343       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7344       .access = PL1_RW, .accessfn = access_mte,
7345       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7346     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7347       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7348       .access = PL1_RW, .accessfn = access_mte,
7349       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7350     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7351       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7352       .access = PL2_RW, .accessfn = access_mte,
7353       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7354     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7355       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7356       .access = PL3_RW,
7357       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7358     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7359       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7360       .access = PL1_RW, .accessfn = access_mte,
7361       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7362     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7363       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7364       .access = PL1_RW, .accessfn = access_mte,
7365       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7366     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7367       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7368       .access = PL1_R, .accessfn = access_aa64_tid5,
7369       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7370     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7371       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7372       .type = ARM_CP_NO_RAW,
7373       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7374     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7375       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7376       .type = ARM_CP_NOP, .access = PL1_W,
7377       .accessfn = aa64_cacheop_poc_access },
7378     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7379       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7380       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7381     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7382       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7383       .type = ARM_CP_NOP, .access = PL1_W,
7384       .accessfn = aa64_cacheop_poc_access },
7385     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7386       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7387       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7388     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7389       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7390       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7391     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7392       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7393       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7394     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7395       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7396       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7397     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7398       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7399       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7400 };
7401 
7402 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7403     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7404       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7405       .type = ARM_CP_CONST, .access = PL0_RW, },
7406 };
7407 
7408 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7409     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7410       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7411       .type = ARM_CP_NOP, .access = PL0_W,
7412       .accessfn = aa64_cacheop_poc_access },
7413     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7414       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7415       .type = ARM_CP_NOP, .access = PL0_W,
7416       .accessfn = aa64_cacheop_poc_access },
7417     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7418       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7419       .type = ARM_CP_NOP, .access = PL0_W,
7420       .accessfn = aa64_cacheop_poc_access },
7421     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7422       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7423       .type = ARM_CP_NOP, .access = PL0_W,
7424       .accessfn = aa64_cacheop_poc_access },
7425     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7426       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7427       .type = ARM_CP_NOP, .access = PL0_W,
7428       .accessfn = aa64_cacheop_poc_access },
7429     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7430       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7431       .type = ARM_CP_NOP, .access = PL0_W,
7432       .accessfn = aa64_cacheop_poc_access },
7433     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7434       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7435       .type = ARM_CP_NOP, .access = PL0_W,
7436       .accessfn = aa64_cacheop_poc_access },
7437     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7438       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7439       .type = ARM_CP_NOP, .access = PL0_W,
7440       .accessfn = aa64_cacheop_poc_access },
7441     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7442       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7443       .access = PL0_W, .type = ARM_CP_DC_GVA,
7444 #ifndef CONFIG_USER_ONLY
7445       /* Avoid overhead of an access check that always passes in user-mode */
7446       .accessfn = aa64_zva_access,
7447 #endif
7448     },
7449     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7450       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7451       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7452 #ifndef CONFIG_USER_ONLY
7453       /* Avoid overhead of an access check that always passes in user-mode */
7454       .accessfn = aa64_zva_access,
7455 #endif
7456     },
7457 };
7458 
7459 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7460                                      bool isread)
7461 {
7462     uint64_t hcr = arm_hcr_el2_eff(env);
7463     int el = arm_current_el(env);
7464 
7465     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7466         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7467             if (hcr & HCR_TGE) {
7468                 return CP_ACCESS_TRAP_EL2;
7469             }
7470             return CP_ACCESS_TRAP;
7471         }
7472     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7473         return CP_ACCESS_TRAP_EL2;
7474     }
7475     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7476         return CP_ACCESS_TRAP_EL2;
7477     }
7478     if (el < 3
7479         && arm_feature(env, ARM_FEATURE_EL3)
7480         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7481         return CP_ACCESS_TRAP_EL3;
7482     }
7483     return CP_ACCESS_OK;
7484 }
7485 
7486 static const ARMCPRegInfo scxtnum_reginfo[] = {
7487     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7488       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7489       .access = PL0_RW, .accessfn = access_scxtnum,
7490       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7491     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7492       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7493       .access = PL1_RW, .accessfn = access_scxtnum,
7494       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7495     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7496       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7497       .access = PL2_RW, .accessfn = access_scxtnum,
7498       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7499     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7500       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7501       .access = PL3_RW,
7502       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7503 };
7504 #endif /* TARGET_AARCH64 */
7505 
7506 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7507                                      bool isread)
7508 {
7509     int el = arm_current_el(env);
7510 
7511     if (el == 0) {
7512         uint64_t sctlr = arm_sctlr(env, el);
7513         if (!(sctlr & SCTLR_EnRCTX)) {
7514             return CP_ACCESS_TRAP;
7515         }
7516     } else if (el == 1) {
7517         uint64_t hcr = arm_hcr_el2_eff(env);
7518         if (hcr & HCR_NV) {
7519             return CP_ACCESS_TRAP_EL2;
7520         }
7521     }
7522     return CP_ACCESS_OK;
7523 }
7524 
7525 static const ARMCPRegInfo predinv_reginfo[] = {
7526     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7527       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7528       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7529     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7530       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7531       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7532     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7533       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7534       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7535     /*
7536      * Note the AArch32 opcodes have a different OPC1.
7537      */
7538     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7539       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7540       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7541     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7542       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7543       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7544     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7545       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7546       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7547 };
7548 
7549 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7550 {
7551     /* Read the high 32 bits of the current CCSIDR */
7552     return extract64(ccsidr_read(env, ri), 32, 32);
7553 }
7554 
7555 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7556     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7557       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7558       .access = PL1_R,
7559       .accessfn = access_aa64_tid2,
7560       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7561 };
7562 
7563 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7564                                        bool isread)
7565 {
7566     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7567         return CP_ACCESS_TRAP_EL2;
7568     }
7569 
7570     return CP_ACCESS_OK;
7571 }
7572 
7573 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7574                                        bool isread)
7575 {
7576     if (arm_feature(env, ARM_FEATURE_V8)) {
7577         return access_aa64_tid3(env, ri, isread);
7578     }
7579 
7580     return CP_ACCESS_OK;
7581 }
7582 
7583 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7584                                      bool isread)
7585 {
7586     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7587         return CP_ACCESS_TRAP_EL2;
7588     }
7589 
7590     return CP_ACCESS_OK;
7591 }
7592 
7593 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7594                                         const ARMCPRegInfo *ri, bool isread)
7595 {
7596     /*
7597      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7598      * in v7A, not in v8A.
7599      */
7600     if (!arm_feature(env, ARM_FEATURE_V8) &&
7601         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7602         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7603         return CP_ACCESS_TRAP_EL2;
7604     }
7605     return CP_ACCESS_OK;
7606 }
7607 
7608 static const ARMCPRegInfo jazelle_regs[] = {
7609     { .name = "JIDR",
7610       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7611       .access = PL1_R, .accessfn = access_jazelle,
7612       .type = ARM_CP_CONST, .resetvalue = 0 },
7613     { .name = "JOSCR",
7614       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7615       .accessfn = access_joscr_jmcr,
7616       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7617     { .name = "JMCR",
7618       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7619       .accessfn = access_joscr_jmcr,
7620       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7621 };
7622 
7623 static const ARMCPRegInfo contextidr_el2 = {
7624     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7625     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7626     .access = PL2_RW,
7627     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7628 };
7629 
7630 static const ARMCPRegInfo vhe_reginfo[] = {
7631     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7632       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7633       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7634       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7635 #ifndef CONFIG_USER_ONLY
7636     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7637       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7638       .fieldoffset =
7639         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7640       .type = ARM_CP_IO, .access = PL2_RW,
7641       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7642     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7643       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7644       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7645       .resetfn = gt_hv_timer_reset,
7646       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7647     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7648       .type = ARM_CP_IO,
7649       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7650       .access = PL2_RW,
7651       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7652       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7653     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7654       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7655       .type = ARM_CP_IO | ARM_CP_ALIAS,
7656       .access = PL2_RW, .accessfn = e2h_access,
7657       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7658       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7659     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7660       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7661       .type = ARM_CP_IO | ARM_CP_ALIAS,
7662       .access = PL2_RW, .accessfn = e2h_access,
7663       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7664       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7665     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7666       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7667       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7668       .access = PL2_RW, .accessfn = e2h_access,
7669       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7670     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7671       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7672       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7673       .access = PL2_RW, .accessfn = e2h_access,
7674       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7675     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7676       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7677       .type = ARM_CP_IO | ARM_CP_ALIAS,
7678       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7679       .access = PL2_RW, .accessfn = e2h_access,
7680       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7681     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7682       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7683       .type = ARM_CP_IO | ARM_CP_ALIAS,
7684       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7685       .access = PL2_RW, .accessfn = e2h_access,
7686       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7687 #endif
7688 };
7689 
7690 #ifndef CONFIG_USER_ONLY
7691 static const ARMCPRegInfo ats1e1_reginfo[] = {
7692     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7693       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7694       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7695       .writefn = ats_write64 },
7696     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7697       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7698       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7699       .writefn = ats_write64 },
7700 };
7701 
7702 static const ARMCPRegInfo ats1cp_reginfo[] = {
7703     { .name = "ATS1CPRP",
7704       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7705       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7706       .writefn = ats_write },
7707     { .name = "ATS1CPWP",
7708       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7709       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7710       .writefn = ats_write },
7711 };
7712 #endif
7713 
7714 /*
7715  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7716  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7717  * is non-zero, which is never for ARMv7, optionally in ARMv8
7718  * and mandatorily for ARMv8.2 and up.
7719  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7720  * implementation is RAZ/WI we can ignore this detail, as we
7721  * do for ACTLR.
7722  */
7723 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7724     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7725       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7726       .access = PL1_RW, .accessfn = access_tacr,
7727       .type = ARM_CP_CONST, .resetvalue = 0 },
7728     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7729       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7730       .access = PL2_RW, .type = ARM_CP_CONST,
7731       .resetvalue = 0 },
7732 };
7733 
7734 void register_cp_regs_for_features(ARMCPU *cpu)
7735 {
7736     /* Register all the coprocessor registers based on feature bits */
7737     CPUARMState *env = &cpu->env;
7738     if (arm_feature(env, ARM_FEATURE_M)) {
7739         /* M profile has no coprocessor registers */
7740         return;
7741     }
7742 
7743     define_arm_cp_regs(cpu, cp_reginfo);
7744     if (!arm_feature(env, ARM_FEATURE_V8)) {
7745         /* Must go early as it is full of wildcards that may be
7746          * overridden by later definitions.
7747          */
7748         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7749     }
7750 
7751     if (arm_feature(env, ARM_FEATURE_V6)) {
7752         /* The ID registers all have impdef reset values */
7753         ARMCPRegInfo v6_idregs[] = {
7754             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7755               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7756               .access = PL1_R, .type = ARM_CP_CONST,
7757               .accessfn = access_aa32_tid3,
7758               .resetvalue = cpu->isar.id_pfr0 },
7759             /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7760              * the value of the GIC field until after we define these regs.
7761              */
7762             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7763               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7764               .access = PL1_R, .type = ARM_CP_NO_RAW,
7765               .accessfn = access_aa32_tid3,
7766               .readfn = id_pfr1_read,
7767               .writefn = arm_cp_write_ignore },
7768             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7769               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7770               .access = PL1_R, .type = ARM_CP_CONST,
7771               .accessfn = access_aa32_tid3,
7772               .resetvalue = cpu->isar.id_dfr0 },
7773             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7774               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7775               .access = PL1_R, .type = ARM_CP_CONST,
7776               .accessfn = access_aa32_tid3,
7777               .resetvalue = cpu->id_afr0 },
7778             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7779               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7780               .access = PL1_R, .type = ARM_CP_CONST,
7781               .accessfn = access_aa32_tid3,
7782               .resetvalue = cpu->isar.id_mmfr0 },
7783             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7784               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7785               .access = PL1_R, .type = ARM_CP_CONST,
7786               .accessfn = access_aa32_tid3,
7787               .resetvalue = cpu->isar.id_mmfr1 },
7788             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7789               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7790               .access = PL1_R, .type = ARM_CP_CONST,
7791               .accessfn = access_aa32_tid3,
7792               .resetvalue = cpu->isar.id_mmfr2 },
7793             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7794               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7795               .access = PL1_R, .type = ARM_CP_CONST,
7796               .accessfn = access_aa32_tid3,
7797               .resetvalue = cpu->isar.id_mmfr3 },
7798             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7799               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7800               .access = PL1_R, .type = ARM_CP_CONST,
7801               .accessfn = access_aa32_tid3,
7802               .resetvalue = cpu->isar.id_isar0 },
7803             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7804               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7805               .access = PL1_R, .type = ARM_CP_CONST,
7806               .accessfn = access_aa32_tid3,
7807               .resetvalue = cpu->isar.id_isar1 },
7808             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7809               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7810               .access = PL1_R, .type = ARM_CP_CONST,
7811               .accessfn = access_aa32_tid3,
7812               .resetvalue = cpu->isar.id_isar2 },
7813             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7814               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7815               .access = PL1_R, .type = ARM_CP_CONST,
7816               .accessfn = access_aa32_tid3,
7817               .resetvalue = cpu->isar.id_isar3 },
7818             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7819               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7820               .access = PL1_R, .type = ARM_CP_CONST,
7821               .accessfn = access_aa32_tid3,
7822               .resetvalue = cpu->isar.id_isar4 },
7823             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7824               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7825               .access = PL1_R, .type = ARM_CP_CONST,
7826               .accessfn = access_aa32_tid3,
7827               .resetvalue = cpu->isar.id_isar5 },
7828             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7829               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7830               .access = PL1_R, .type = ARM_CP_CONST,
7831               .accessfn = access_aa32_tid3,
7832               .resetvalue = cpu->isar.id_mmfr4 },
7833             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7834               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7835               .access = PL1_R, .type = ARM_CP_CONST,
7836               .accessfn = access_aa32_tid3,
7837               .resetvalue = cpu->isar.id_isar6 },
7838         };
7839         define_arm_cp_regs(cpu, v6_idregs);
7840         define_arm_cp_regs(cpu, v6_cp_reginfo);
7841     } else {
7842         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7843     }
7844     if (arm_feature(env, ARM_FEATURE_V6K)) {
7845         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7846     }
7847     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7848         !arm_feature(env, ARM_FEATURE_PMSA)) {
7849         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7850     }
7851     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7852         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7853     }
7854     if (arm_feature(env, ARM_FEATURE_V7)) {
7855         ARMCPRegInfo clidr = {
7856             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7857             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7858             .access = PL1_R, .type = ARM_CP_CONST,
7859             .accessfn = access_aa64_tid2,
7860             .resetvalue = cpu->clidr
7861         };
7862         define_one_arm_cp_reg(cpu, &clidr);
7863         define_arm_cp_regs(cpu, v7_cp_reginfo);
7864         define_debug_regs(cpu);
7865         define_pmu_regs(cpu);
7866     } else {
7867         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7868     }
7869     if (arm_feature(env, ARM_FEATURE_V8)) {
7870         /* AArch64 ID registers, which all have impdef reset values.
7871          * Note that within the ID register ranges the unused slots
7872          * must all RAZ, not UNDEF; future architecture versions may
7873          * define new registers here.
7874          */
7875         ARMCPRegInfo v8_idregs[] = {
7876             /*
7877              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7878              * emulation because we don't know the right value for the
7879              * GIC field until after we define these regs.
7880              */
7881             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7882               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7883               .access = PL1_R,
7884 #ifdef CONFIG_USER_ONLY
7885               .type = ARM_CP_CONST,
7886               .resetvalue = cpu->isar.id_aa64pfr0
7887 #else
7888               .type = ARM_CP_NO_RAW,
7889               .accessfn = access_aa64_tid3,
7890               .readfn = id_aa64pfr0_read,
7891               .writefn = arm_cp_write_ignore
7892 #endif
7893             },
7894             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7895               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7896               .access = PL1_R, .type = ARM_CP_CONST,
7897               .accessfn = access_aa64_tid3,
7898               .resetvalue = cpu->isar.id_aa64pfr1},
7899             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7900               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7901               .access = PL1_R, .type = ARM_CP_CONST,
7902               .accessfn = access_aa64_tid3,
7903               .resetvalue = 0 },
7904             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7905               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7906               .access = PL1_R, .type = ARM_CP_CONST,
7907               .accessfn = access_aa64_tid3,
7908               .resetvalue = 0 },
7909             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7910               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7911               .access = PL1_R, .type = ARM_CP_CONST,
7912               .accessfn = access_aa64_tid3,
7913               .resetvalue = cpu->isar.id_aa64zfr0 },
7914             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7915               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7916               .access = PL1_R, .type = ARM_CP_CONST,
7917               .accessfn = access_aa64_tid3,
7918               .resetvalue = cpu->isar.id_aa64smfr0 },
7919             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7920               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7921               .access = PL1_R, .type = ARM_CP_CONST,
7922               .accessfn = access_aa64_tid3,
7923               .resetvalue = 0 },
7924             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7925               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7926               .access = PL1_R, .type = ARM_CP_CONST,
7927               .accessfn = access_aa64_tid3,
7928               .resetvalue = 0 },
7929             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7930               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7931               .access = PL1_R, .type = ARM_CP_CONST,
7932               .accessfn = access_aa64_tid3,
7933               .resetvalue = cpu->isar.id_aa64dfr0 },
7934             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7935               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7936               .access = PL1_R, .type = ARM_CP_CONST,
7937               .accessfn = access_aa64_tid3,
7938               .resetvalue = cpu->isar.id_aa64dfr1 },
7939             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7940               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7941               .access = PL1_R, .type = ARM_CP_CONST,
7942               .accessfn = access_aa64_tid3,
7943               .resetvalue = 0 },
7944             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7945               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7946               .access = PL1_R, .type = ARM_CP_CONST,
7947               .accessfn = access_aa64_tid3,
7948               .resetvalue = 0 },
7949             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7950               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7951               .access = PL1_R, .type = ARM_CP_CONST,
7952               .accessfn = access_aa64_tid3,
7953               .resetvalue = cpu->id_aa64afr0 },
7954             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7955               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7956               .access = PL1_R, .type = ARM_CP_CONST,
7957               .accessfn = access_aa64_tid3,
7958               .resetvalue = cpu->id_aa64afr1 },
7959             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7960               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7961               .access = PL1_R, .type = ARM_CP_CONST,
7962               .accessfn = access_aa64_tid3,
7963               .resetvalue = 0 },
7964             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7965               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7966               .access = PL1_R, .type = ARM_CP_CONST,
7967               .accessfn = access_aa64_tid3,
7968               .resetvalue = 0 },
7969             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7970               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7971               .access = PL1_R, .type = ARM_CP_CONST,
7972               .accessfn = access_aa64_tid3,
7973               .resetvalue = cpu->isar.id_aa64isar0 },
7974             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7975               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7976               .access = PL1_R, .type = ARM_CP_CONST,
7977               .accessfn = access_aa64_tid3,
7978               .resetvalue = cpu->isar.id_aa64isar1 },
7979             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7980               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7981               .access = PL1_R, .type = ARM_CP_CONST,
7982               .accessfn = access_aa64_tid3,
7983               .resetvalue = 0 },
7984             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7985               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7986               .access = PL1_R, .type = ARM_CP_CONST,
7987               .accessfn = access_aa64_tid3,
7988               .resetvalue = 0 },
7989             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7990               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7991               .access = PL1_R, .type = ARM_CP_CONST,
7992               .accessfn = access_aa64_tid3,
7993               .resetvalue = 0 },
7994             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7995               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7996               .access = PL1_R, .type = ARM_CP_CONST,
7997               .accessfn = access_aa64_tid3,
7998               .resetvalue = 0 },
7999             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8000               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8001               .access = PL1_R, .type = ARM_CP_CONST,
8002               .accessfn = access_aa64_tid3,
8003               .resetvalue = 0 },
8004             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8005               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8006               .access = PL1_R, .type = ARM_CP_CONST,
8007               .accessfn = access_aa64_tid3,
8008               .resetvalue = 0 },
8009             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8010               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8011               .access = PL1_R, .type = ARM_CP_CONST,
8012               .accessfn = access_aa64_tid3,
8013               .resetvalue = cpu->isar.id_aa64mmfr0 },
8014             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8015               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8016               .access = PL1_R, .type = ARM_CP_CONST,
8017               .accessfn = access_aa64_tid3,
8018               .resetvalue = cpu->isar.id_aa64mmfr1 },
8019             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8020               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8021               .access = PL1_R, .type = ARM_CP_CONST,
8022               .accessfn = access_aa64_tid3,
8023               .resetvalue = cpu->isar.id_aa64mmfr2 },
8024             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8025               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8026               .access = PL1_R, .type = ARM_CP_CONST,
8027               .accessfn = access_aa64_tid3,
8028               .resetvalue = 0 },
8029             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8030               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8031               .access = PL1_R, .type = ARM_CP_CONST,
8032               .accessfn = access_aa64_tid3,
8033               .resetvalue = 0 },
8034             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8035               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8036               .access = PL1_R, .type = ARM_CP_CONST,
8037               .accessfn = access_aa64_tid3,
8038               .resetvalue = 0 },
8039             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8040               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8041               .access = PL1_R, .type = ARM_CP_CONST,
8042               .accessfn = access_aa64_tid3,
8043               .resetvalue = 0 },
8044             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8045               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8046               .access = PL1_R, .type = ARM_CP_CONST,
8047               .accessfn = access_aa64_tid3,
8048               .resetvalue = 0 },
8049             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8050               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8051               .access = PL1_R, .type = ARM_CP_CONST,
8052               .accessfn = access_aa64_tid3,
8053               .resetvalue = cpu->isar.mvfr0 },
8054             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8055               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8056               .access = PL1_R, .type = ARM_CP_CONST,
8057               .accessfn = access_aa64_tid3,
8058               .resetvalue = cpu->isar.mvfr1 },
8059             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8060               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8061               .access = PL1_R, .type = ARM_CP_CONST,
8062               .accessfn = access_aa64_tid3,
8063               .resetvalue = cpu->isar.mvfr2 },
8064             { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8065               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8066               .access = PL1_R, .type = ARM_CP_CONST,
8067               .accessfn = access_aa64_tid3,
8068               .resetvalue = 0 },
8069             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8070               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8071               .access = PL1_R, .type = ARM_CP_CONST,
8072               .accessfn = access_aa64_tid3,
8073               .resetvalue = cpu->isar.id_pfr2 },
8074             { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8075               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8076               .access = PL1_R, .type = ARM_CP_CONST,
8077               .accessfn = access_aa64_tid3,
8078               .resetvalue = 0 },
8079             { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8080               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8081               .access = PL1_R, .type = ARM_CP_CONST,
8082               .accessfn = access_aa64_tid3,
8083               .resetvalue = 0 },
8084             { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8085               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8086               .access = PL1_R, .type = ARM_CP_CONST,
8087               .accessfn = access_aa64_tid3,
8088               .resetvalue = 0 },
8089             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8090               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8091               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8092               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8093             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8094               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8095               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8096               .resetvalue = cpu->pmceid0 },
8097             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8098               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8099               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8100               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8101             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8102               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8103               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8104               .resetvalue = cpu->pmceid1 },
8105         };
8106 #ifdef CONFIG_USER_ONLY
8107         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8108             { .name = "ID_AA64PFR0_EL1",
8109               .exported_bits = 0x000f000f00ff0000,
8110               .fixed_bits    = 0x0000000000000011 },
8111             { .name = "ID_AA64PFR1_EL1",
8112               .exported_bits = 0x00000000000000f0 },
8113             { .name = "ID_AA64PFR*_EL1_RESERVED",
8114               .is_glob = true                     },
8115             { .name = "ID_AA64ZFR0_EL1"           },
8116             { .name = "ID_AA64MMFR0_EL1",
8117               .fixed_bits    = 0x00000000ff000000 },
8118             { .name = "ID_AA64MMFR1_EL1"          },
8119             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8120               .is_glob = true                     },
8121             { .name = "ID_AA64DFR0_EL1",
8122               .fixed_bits    = 0x0000000000000006 },
8123             { .name = "ID_AA64DFR1_EL1"           },
8124             { .name = "ID_AA64DFR*_EL1_RESERVED",
8125               .is_glob = true                     },
8126             { .name = "ID_AA64AFR*",
8127               .is_glob = true                     },
8128             { .name = "ID_AA64ISAR0_EL1",
8129               .exported_bits = 0x00fffffff0fffff0 },
8130             { .name = "ID_AA64ISAR1_EL1",
8131               .exported_bits = 0x000000f0ffffffff },
8132             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8133               .is_glob = true                     },
8134         };
8135         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8136 #endif
8137         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
8138         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8139             !arm_feature(env, ARM_FEATURE_EL2)) {
8140             ARMCPRegInfo rvbar = {
8141                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
8142                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8143                 .access = PL1_R,
8144                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8145             };
8146             define_one_arm_cp_reg(cpu, &rvbar);
8147         }
8148         define_arm_cp_regs(cpu, v8_idregs);
8149         define_arm_cp_regs(cpu, v8_cp_reginfo);
8150     }
8151 
8152     /*
8153      * Register the base EL2 cpregs.
8154      * Pre v8, these registers are implemented only as part of the
8155      * Virtualization Extensions (EL2 present).  Beginning with v8,
8156      * if EL2 is missing but EL3 is enabled, mostly these become
8157      * RES0 from EL3, with some specific exceptions.
8158      */
8159     if (arm_feature(env, ARM_FEATURE_EL2)
8160         || (arm_feature(env, ARM_FEATURE_EL3)
8161             && arm_feature(env, ARM_FEATURE_V8))) {
8162         uint64_t vmpidr_def = mpidr_read_val(env);
8163         ARMCPRegInfo vpidr_regs[] = {
8164             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8165               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8166               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8167               .resetvalue = cpu->midr,
8168               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8169               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8170             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8171               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8172               .access = PL2_RW, .resetvalue = cpu->midr,
8173               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8174               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8175             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8176               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8177               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8178               .resetvalue = vmpidr_def,
8179               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8180               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8181             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8182               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8183               .access = PL2_RW, .resetvalue = vmpidr_def,
8184               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8185               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8186         };
8187         /*
8188          * The only field of MDCR_EL2 that has a defined architectural reset
8189          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8190          */
8191         ARMCPRegInfo mdcr_el2 = {
8192             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
8193             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8194             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8195             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8196         };
8197         define_one_arm_cp_reg(cpu, &mdcr_el2);
8198         define_arm_cp_regs(cpu, vpidr_regs);
8199         define_arm_cp_regs(cpu, el2_cp_reginfo);
8200         if (arm_feature(env, ARM_FEATURE_V8)) {
8201             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8202         }
8203         if (cpu_isar_feature(aa64_sel2, cpu)) {
8204             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8205         }
8206         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
8207         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8208             ARMCPRegInfo rvbar = {
8209                 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8210                 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8211                 .access = PL2_R,
8212                 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8213             };
8214             define_one_arm_cp_reg(cpu, &rvbar);
8215         }
8216     }
8217 
8218     /* Register the base EL3 cpregs. */
8219     if (arm_feature(env, ARM_FEATURE_EL3)) {
8220         define_arm_cp_regs(cpu, el3_cp_reginfo);
8221         ARMCPRegInfo el3_regs[] = {
8222             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8223               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8224               .access = PL3_R,
8225               .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8226             },
8227             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8228               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8229               .access = PL3_RW,
8230               .raw_writefn = raw_write, .writefn = sctlr_write,
8231               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8232               .resetvalue = cpu->reset_sctlr },
8233         };
8234 
8235         define_arm_cp_regs(cpu, el3_regs);
8236     }
8237     /* The behaviour of NSACR is sufficiently various that we don't
8238      * try to describe it in a single reginfo:
8239      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8240      *     reads as constant 0xc00 from NS EL1 and NS EL2
8241      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8242      *  if v7 without EL3, register doesn't exist
8243      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8244      */
8245     if (arm_feature(env, ARM_FEATURE_EL3)) {
8246         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8247             static const ARMCPRegInfo nsacr = {
8248                 .name = "NSACR", .type = ARM_CP_CONST,
8249                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8250                 .access = PL1_RW, .accessfn = nsacr_access,
8251                 .resetvalue = 0xc00
8252             };
8253             define_one_arm_cp_reg(cpu, &nsacr);
8254         } else {
8255             static const ARMCPRegInfo nsacr = {
8256                 .name = "NSACR",
8257                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8258                 .access = PL3_RW | PL1_R,
8259                 .resetvalue = 0,
8260                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8261             };
8262             define_one_arm_cp_reg(cpu, &nsacr);
8263         }
8264     } else {
8265         if (arm_feature(env, ARM_FEATURE_V8)) {
8266             static const ARMCPRegInfo nsacr = {
8267                 .name = "NSACR", .type = ARM_CP_CONST,
8268                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8269                 .access = PL1_R,
8270                 .resetvalue = 0xc00
8271             };
8272             define_one_arm_cp_reg(cpu, &nsacr);
8273         }
8274     }
8275 
8276     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8277         if (arm_feature(env, ARM_FEATURE_V6)) {
8278             /* PMSAv6 not implemented */
8279             assert(arm_feature(env, ARM_FEATURE_V7));
8280             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8281             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8282         } else {
8283             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8284         }
8285     } else {
8286         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8287         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8288         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8289         if (cpu_isar_feature(aa32_hpd, cpu)) {
8290             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8291         }
8292     }
8293     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8294         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8295     }
8296     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8297         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8298     }
8299     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8300         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8301     }
8302     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8303         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8304     }
8305     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8306         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8307     }
8308     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8309         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8310     }
8311     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8312         define_arm_cp_regs(cpu, omap_cp_reginfo);
8313     }
8314     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8315         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8316     }
8317     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8318         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8319     }
8320     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8321         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8322     }
8323     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8324         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8325     }
8326     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8327         define_arm_cp_regs(cpu, jazelle_regs);
8328     }
8329     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
8330      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8331      * be read-only (ie write causes UNDEF exception).
8332      */
8333     {
8334         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8335             /* Pre-v8 MIDR space.
8336              * Note that the MIDR isn't a simple constant register because
8337              * of the TI925 behaviour where writes to another register can
8338              * cause the MIDR value to change.
8339              *
8340              * Unimplemented registers in the c15 0 0 0 space default to
8341              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8342              * and friends override accordingly.
8343              */
8344             { .name = "MIDR",
8345               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8346               .access = PL1_R, .resetvalue = cpu->midr,
8347               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8348               .readfn = midr_read,
8349               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8350               .type = ARM_CP_OVERRIDE },
8351             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8352             { .name = "DUMMY",
8353               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8354               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8355             { .name = "DUMMY",
8356               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8357               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8358             { .name = "DUMMY",
8359               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8360               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8361             { .name = "DUMMY",
8362               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8363               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8364             { .name = "DUMMY",
8365               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8366               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8367         };
8368         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8369             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8370               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8371               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8372               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8373               .readfn = midr_read },
8374             /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8375             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8376               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8377               .access = PL1_R, .resetvalue = cpu->midr },
8378             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8379               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8380               .access = PL1_R, .resetvalue = cpu->midr },
8381             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8382               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8383               .access = PL1_R,
8384               .accessfn = access_aa64_tid1,
8385               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8386         };
8387         ARMCPRegInfo id_cp_reginfo[] = {
8388             /* These are common to v8 and pre-v8 */
8389             { .name = "CTR",
8390               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8391               .access = PL1_R, .accessfn = ctr_el0_access,
8392               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8393             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8394               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8395               .access = PL0_R, .accessfn = ctr_el0_access,
8396               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8397             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8398             { .name = "TCMTR",
8399               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8400               .access = PL1_R,
8401               .accessfn = access_aa32_tid1,
8402               .type = ARM_CP_CONST, .resetvalue = 0 },
8403         };
8404         /* TLBTR is specific to VMSA */
8405         ARMCPRegInfo id_tlbtr_reginfo = {
8406               .name = "TLBTR",
8407               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8408               .access = PL1_R,
8409               .accessfn = access_aa32_tid1,
8410               .type = ARM_CP_CONST, .resetvalue = 0,
8411         };
8412         /* MPUIR is specific to PMSA V6+ */
8413         ARMCPRegInfo id_mpuir_reginfo = {
8414               .name = "MPUIR",
8415               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8416               .access = PL1_R, .type = ARM_CP_CONST,
8417               .resetvalue = cpu->pmsav7_dregion << 8
8418         };
8419         static const ARMCPRegInfo crn0_wi_reginfo = {
8420             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8421             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8422             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8423         };
8424 #ifdef CONFIG_USER_ONLY
8425         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8426             { .name = "MIDR_EL1",
8427               .exported_bits = 0x00000000ffffffff },
8428             { .name = "REVIDR_EL1"                },
8429         };
8430         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8431 #endif
8432         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8433             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8434             size_t i;
8435             /* Register the blanket "writes ignored" value first to cover the
8436              * whole space. Then update the specific ID registers to allow write
8437              * access, so that they ignore writes rather than causing them to
8438              * UNDEF.
8439              */
8440             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8441             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8442                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8443             }
8444             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8445                 id_cp_reginfo[i].access = PL1_RW;
8446             }
8447             id_mpuir_reginfo.access = PL1_RW;
8448             id_tlbtr_reginfo.access = PL1_RW;
8449         }
8450         if (arm_feature(env, ARM_FEATURE_V8)) {
8451             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8452         } else {
8453             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8454         }
8455         define_arm_cp_regs(cpu, id_cp_reginfo);
8456         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8457             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8458         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8459             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8460         }
8461     }
8462 
8463     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8464         ARMCPRegInfo mpidr_cp_reginfo[] = {
8465             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8466               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8467               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8468         };
8469 #ifdef CONFIG_USER_ONLY
8470         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8471             { .name = "MPIDR_EL1",
8472               .fixed_bits = 0x0000000080000000 },
8473         };
8474         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8475 #endif
8476         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8477     }
8478 
8479     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8480         ARMCPRegInfo auxcr_reginfo[] = {
8481             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8482               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8483               .access = PL1_RW, .accessfn = access_tacr,
8484               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8485             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8486               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8487               .access = PL2_RW, .type = ARM_CP_CONST,
8488               .resetvalue = 0 },
8489             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8490               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8491               .access = PL3_RW, .type = ARM_CP_CONST,
8492               .resetvalue = 0 },
8493         };
8494         define_arm_cp_regs(cpu, auxcr_reginfo);
8495         if (cpu_isar_feature(aa32_ac2, cpu)) {
8496             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8497         }
8498     }
8499 
8500     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8501         /*
8502          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8503          * There are two flavours:
8504          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8505          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8506          *      32-bit register visible to AArch32 at a different encoding
8507          *      to the "flavour 1" register and with the bits rearranged to
8508          *      be able to squash a 64-bit address into the 32-bit view.
8509          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8510          * in future if we support AArch32-only configs of some of the
8511          * AArch64 cores we might need to add a specific feature flag
8512          * to indicate cores with "flavour 2" CBAR.
8513          */
8514         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8515             /* 32 bit view is [31:18] 0...0 [43:32]. */
8516             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8517                 | extract64(cpu->reset_cbar, 32, 12);
8518             ARMCPRegInfo cbar_reginfo[] = {
8519                 { .name = "CBAR",
8520                   .type = ARM_CP_CONST,
8521                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8522                   .access = PL1_R, .resetvalue = cbar32 },
8523                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8524                   .type = ARM_CP_CONST,
8525                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8526                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8527             };
8528             /* We don't implement a r/w 64 bit CBAR currently */
8529             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8530             define_arm_cp_regs(cpu, cbar_reginfo);
8531         } else {
8532             ARMCPRegInfo cbar = {
8533                 .name = "CBAR",
8534                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8535                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8536                 .fieldoffset = offsetof(CPUARMState,
8537                                         cp15.c15_config_base_address)
8538             };
8539             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8540                 cbar.access = PL1_R;
8541                 cbar.fieldoffset = 0;
8542                 cbar.type = ARM_CP_CONST;
8543             }
8544             define_one_arm_cp_reg(cpu, &cbar);
8545         }
8546     }
8547 
8548     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8549         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8550             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8551               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8552               .access = PL1_RW, .writefn = vbar_write,
8553               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8554                                      offsetof(CPUARMState, cp15.vbar_ns) },
8555               .resetvalue = 0 },
8556         };
8557         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8558     }
8559 
8560     /* Generic registers whose values depend on the implementation */
8561     {
8562         ARMCPRegInfo sctlr = {
8563             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8564             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8565             .access = PL1_RW, .accessfn = access_tvm_trvm,
8566             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8567                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8568             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8569             .raw_writefn = raw_write,
8570         };
8571         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8572             /* Normally we would always end the TB on an SCTLR write, but Linux
8573              * arch/arm/mach-pxa/sleep.S expects two instructions following
8574              * an MMU enable to execute from cache.  Imitate this behaviour.
8575              */
8576             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8577         }
8578         define_one_arm_cp_reg(cpu, &sctlr);
8579     }
8580 
8581     if (cpu_isar_feature(aa64_lor, cpu)) {
8582         define_arm_cp_regs(cpu, lor_reginfo);
8583     }
8584     if (cpu_isar_feature(aa64_pan, cpu)) {
8585         define_one_arm_cp_reg(cpu, &pan_reginfo);
8586     }
8587 #ifndef CONFIG_USER_ONLY
8588     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8589         define_arm_cp_regs(cpu, ats1e1_reginfo);
8590     }
8591     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8592         define_arm_cp_regs(cpu, ats1cp_reginfo);
8593     }
8594 #endif
8595     if (cpu_isar_feature(aa64_uao, cpu)) {
8596         define_one_arm_cp_reg(cpu, &uao_reginfo);
8597     }
8598 
8599     if (cpu_isar_feature(aa64_dit, cpu)) {
8600         define_one_arm_cp_reg(cpu, &dit_reginfo);
8601     }
8602     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8603         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8604     }
8605     if (cpu_isar_feature(any_ras, cpu)) {
8606         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8607     }
8608 
8609     if (cpu_isar_feature(aa64_vh, cpu) ||
8610         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8611         define_one_arm_cp_reg(cpu, &contextidr_el2);
8612     }
8613     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8614         define_arm_cp_regs(cpu, vhe_reginfo);
8615     }
8616 
8617     if (cpu_isar_feature(aa64_sve, cpu)) {
8618         define_arm_cp_regs(cpu, zcr_reginfo);
8619     }
8620 
8621     if (cpu_isar_feature(aa64_hcx, cpu)) {
8622         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8623     }
8624 
8625 #ifdef TARGET_AARCH64
8626     if (cpu_isar_feature(aa64_sme, cpu)) {
8627         define_arm_cp_regs(cpu, sme_reginfo);
8628     }
8629     if (cpu_isar_feature(aa64_pauth, cpu)) {
8630         define_arm_cp_regs(cpu, pauth_reginfo);
8631     }
8632     if (cpu_isar_feature(aa64_rndr, cpu)) {
8633         define_arm_cp_regs(cpu, rndr_reginfo);
8634     }
8635     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8636         define_arm_cp_regs(cpu, tlbirange_reginfo);
8637     }
8638     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8639         define_arm_cp_regs(cpu, tlbios_reginfo);
8640     }
8641 #ifndef CONFIG_USER_ONLY
8642     /* Data Cache clean instructions up to PoP */
8643     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8644         define_one_arm_cp_reg(cpu, dcpop_reg);
8645 
8646         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8647             define_one_arm_cp_reg(cpu, dcpodp_reg);
8648         }
8649     }
8650 #endif /*CONFIG_USER_ONLY*/
8651 
8652     /*
8653      * If full MTE is enabled, add all of the system registers.
8654      * If only "instructions available at EL0" are enabled,
8655      * then define only a RAZ/WI version of PSTATE.TCO.
8656      */
8657     if (cpu_isar_feature(aa64_mte, cpu)) {
8658         define_arm_cp_regs(cpu, mte_reginfo);
8659         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8660     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8661         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8662         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8663     }
8664 
8665     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8666         define_arm_cp_regs(cpu, scxtnum_reginfo);
8667     }
8668 #endif
8669 
8670     if (cpu_isar_feature(any_predinv, cpu)) {
8671         define_arm_cp_regs(cpu, predinv_reginfo);
8672     }
8673 
8674     if (cpu_isar_feature(any_ccidx, cpu)) {
8675         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8676     }
8677 
8678 #ifndef CONFIG_USER_ONLY
8679     /*
8680      * Register redirections and aliases must be done last,
8681      * after the registers from the other extensions have been defined.
8682      */
8683     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8684         define_arm_vh_e2h_redirects_aliases(cpu);
8685     }
8686 #endif
8687 }
8688 
8689 /* Sort alphabetically by type name, except for "any". */
8690 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8691 {
8692     ObjectClass *class_a = (ObjectClass *)a;
8693     ObjectClass *class_b = (ObjectClass *)b;
8694     const char *name_a, *name_b;
8695 
8696     name_a = object_class_get_name(class_a);
8697     name_b = object_class_get_name(class_b);
8698     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8699         return 1;
8700     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8701         return -1;
8702     } else {
8703         return strcmp(name_a, name_b);
8704     }
8705 }
8706 
8707 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8708 {
8709     ObjectClass *oc = data;
8710     const char *typename;
8711     char *name;
8712 
8713     typename = object_class_get_name(oc);
8714     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8715     qemu_printf("  %s\n", name);
8716     g_free(name);
8717 }
8718 
8719 void arm_cpu_list(void)
8720 {
8721     GSList *list;
8722 
8723     list = object_class_get_list(TYPE_ARM_CPU, false);
8724     list = g_slist_sort(list, arm_cpu_list_compare);
8725     qemu_printf("Available CPUs:\n");
8726     g_slist_foreach(list, arm_cpu_list_entry, NULL);
8727     g_slist_free(list);
8728 }
8729 
8730 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8731 {
8732     ObjectClass *oc = data;
8733     CpuDefinitionInfoList **cpu_list = user_data;
8734     CpuDefinitionInfo *info;
8735     const char *typename;
8736 
8737     typename = object_class_get_name(oc);
8738     info = g_malloc0(sizeof(*info));
8739     info->name = g_strndup(typename,
8740                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
8741     info->q_typename = g_strdup(typename);
8742 
8743     QAPI_LIST_PREPEND(*cpu_list, info);
8744 }
8745 
8746 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8747 {
8748     CpuDefinitionInfoList *cpu_list = NULL;
8749     GSList *list;
8750 
8751     list = object_class_get_list(TYPE_ARM_CPU, false);
8752     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8753     g_slist_free(list);
8754 
8755     return cpu_list;
8756 }
8757 
8758 /*
8759  * Private utility function for define_one_arm_cp_reg_with_opaque():
8760  * add a single reginfo struct to the hash table.
8761  */
8762 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8763                                    void *opaque, CPState state,
8764                                    CPSecureState secstate,
8765                                    int crm, int opc1, int opc2,
8766                                    const char *name)
8767 {
8768     CPUARMState *env = &cpu->env;
8769     uint32_t key;
8770     ARMCPRegInfo *r2;
8771     bool is64 = r->type & ARM_CP_64BIT;
8772     bool ns = secstate & ARM_CP_SECSTATE_NS;
8773     int cp = r->cp;
8774     size_t name_len;
8775     bool make_const;
8776 
8777     switch (state) {
8778     case ARM_CP_STATE_AA32:
8779         /* We assume it is a cp15 register if the .cp field is left unset. */
8780         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8781             cp = 15;
8782         }
8783         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8784         break;
8785     case ARM_CP_STATE_AA64:
8786         /*
8787          * To allow abbreviation of ARMCPRegInfo definitions, we treat
8788          * cp == 0 as equivalent to the value for "standard guest-visible
8789          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
8790          * in their AArch64 view (the .cp value may be non-zero for the
8791          * benefit of the AArch32 view).
8792          */
8793         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8794             cp = CP_REG_ARM64_SYSREG_CP;
8795         }
8796         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8797         break;
8798     default:
8799         g_assert_not_reached();
8800     }
8801 
8802     /* Overriding of an existing definition must be explicitly requested. */
8803     if (!(r->type & ARM_CP_OVERRIDE)) {
8804         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8805         if (oldreg) {
8806             assert(oldreg->type & ARM_CP_OVERRIDE);
8807         }
8808     }
8809 
8810     /*
8811      * Eliminate registers that are not present because the EL is missing.
8812      * Doing this here makes it easier to put all registers for a given
8813      * feature into the same ARMCPRegInfo array and define them all at once.
8814      */
8815     make_const = false;
8816     if (arm_feature(env, ARM_FEATURE_EL3)) {
8817         /*
8818          * An EL2 register without EL2 but with EL3 is (usually) RES0.
8819          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8820          */
8821         int min_el = ctz32(r->access) / 2;
8822         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8823             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8824                 return;
8825             }
8826             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8827         }
8828     } else {
8829         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8830                                  ? PL2_RW : PL1_RW);
8831         if ((r->access & max_el) == 0) {
8832             return;
8833         }
8834     }
8835 
8836     /* Combine cpreg and name into one allocation. */
8837     name_len = strlen(name) + 1;
8838     r2 = g_malloc(sizeof(*r2) + name_len);
8839     *r2 = *r;
8840     r2->name = memcpy(r2 + 1, name, name_len);
8841 
8842     /*
8843      * Update fields to match the instantiation, overwiting wildcards
8844      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8845      */
8846     r2->cp = cp;
8847     r2->crm = crm;
8848     r2->opc1 = opc1;
8849     r2->opc2 = opc2;
8850     r2->state = state;
8851     r2->secure = secstate;
8852     if (opaque) {
8853         r2->opaque = opaque;
8854     }
8855 
8856     if (make_const) {
8857         /* This should not have been a very special register to begin. */
8858         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8859         assert(old_special == 0 || old_special == ARM_CP_NOP);
8860         /*
8861          * Set the special function to CONST, retaining the other flags.
8862          * This is important for e.g. ARM_CP_SVE so that we still
8863          * take the SVE trap if CPTR_EL3.EZ == 0.
8864          */
8865         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8866         /*
8867          * Usually, these registers become RES0, but there are a few
8868          * special cases like VPIDR_EL2 which have a constant non-zero
8869          * value with writes ignored.
8870          */
8871         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8872             r2->resetvalue = 0;
8873         }
8874         /*
8875          * ARM_CP_CONST has precedence, so removing the callbacks and
8876          * offsets are not strictly necessary, but it is potentially
8877          * less confusing to debug later.
8878          */
8879         r2->readfn = NULL;
8880         r2->writefn = NULL;
8881         r2->raw_readfn = NULL;
8882         r2->raw_writefn = NULL;
8883         r2->resetfn = NULL;
8884         r2->fieldoffset = 0;
8885         r2->bank_fieldoffsets[0] = 0;
8886         r2->bank_fieldoffsets[1] = 0;
8887     } else {
8888         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8889 
8890         if (isbanked) {
8891             /*
8892              * Register is banked (using both entries in array).
8893              * Overwriting fieldoffset as the array is only used to define
8894              * banked registers but later only fieldoffset is used.
8895              */
8896             r2->fieldoffset = r->bank_fieldoffsets[ns];
8897         }
8898         if (state == ARM_CP_STATE_AA32) {
8899             if (isbanked) {
8900                 /*
8901                  * If the register is banked then we don't need to migrate or
8902                  * reset the 32-bit instance in certain cases:
8903                  *
8904                  * 1) If the register has both 32-bit and 64-bit instances
8905                  *    then we can count on the 64-bit instance taking care
8906                  *    of the non-secure bank.
8907                  * 2) If ARMv8 is enabled then we can count on a 64-bit
8908                  *    version taking care of the secure bank.  This requires
8909                  *    that separate 32 and 64-bit definitions are provided.
8910                  */
8911                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8912                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8913                     r2->type |= ARM_CP_ALIAS;
8914                 }
8915             } else if ((secstate != r->secure) && !ns) {
8916                 /*
8917                  * The register is not banked so we only want to allow
8918                  * migration of the non-secure instance.
8919                  */
8920                 r2->type |= ARM_CP_ALIAS;
8921             }
8922 
8923             if (HOST_BIG_ENDIAN &&
8924                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8925                 r2->fieldoffset += sizeof(uint32_t);
8926             }
8927         }
8928     }
8929 
8930     /*
8931      * By convention, for wildcarded registers only the first
8932      * entry is used for migration; the others are marked as
8933      * ALIAS so we don't try to transfer the register
8934      * multiple times. Special registers (ie NOP/WFI) are
8935      * never migratable and not even raw-accessible.
8936      */
8937     if (r2->type & ARM_CP_SPECIAL_MASK) {
8938         r2->type |= ARM_CP_NO_RAW;
8939     }
8940     if (((r->crm == CP_ANY) && crm != 0) ||
8941         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8942         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8943         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8944     }
8945 
8946     /*
8947      * Check that raw accesses are either forbidden or handled. Note that
8948      * we can't assert this earlier because the setup of fieldoffset for
8949      * banked registers has to be done first.
8950      */
8951     if (!(r2->type & ARM_CP_NO_RAW)) {
8952         assert(!raw_accessors_invalid(r2));
8953     }
8954 
8955     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
8956 }
8957 
8958 
8959 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8960                                        const ARMCPRegInfo *r, void *opaque)
8961 {
8962     /* Define implementations of coprocessor registers.
8963      * We store these in a hashtable because typically
8964      * there are less than 150 registers in a space which
8965      * is 16*16*16*8*8 = 262144 in size.
8966      * Wildcarding is supported for the crm, opc1 and opc2 fields.
8967      * If a register is defined twice then the second definition is
8968      * used, so this can be used to define some generic registers and
8969      * then override them with implementation specific variations.
8970      * At least one of the original and the second definition should
8971      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8972      * against accidental use.
8973      *
8974      * The state field defines whether the register is to be
8975      * visible in the AArch32 or AArch64 execution state. If the
8976      * state is set to ARM_CP_STATE_BOTH then we synthesise a
8977      * reginfo structure for the AArch32 view, which sees the lower
8978      * 32 bits of the 64 bit register.
8979      *
8980      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8981      * be wildcarded. AArch64 registers are always considered to be 64
8982      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8983      * the register, if any.
8984      */
8985     int crm, opc1, opc2;
8986     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8987     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8988     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8989     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8990     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8991     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8992     CPState state;
8993 
8994     /* 64 bit registers have only CRm and Opc1 fields */
8995     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8996     /* op0 only exists in the AArch64 encodings */
8997     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8998     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8999     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9000     /*
9001      * This API is only for Arm's system coprocessors (14 and 15) or
9002      * (M-profile or v7A-and-earlier only) for implementation defined
9003      * coprocessors in the range 0..7.  Our decode assumes this, since
9004      * 8..13 can be used for other insns including VFP and Neon. See
9005      * valid_cp() in translate.c.  Assert here that we haven't tried
9006      * to use an invalid coprocessor number.
9007      */
9008     switch (r->state) {
9009     case ARM_CP_STATE_BOTH:
9010         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9011         if (r->cp == 0) {
9012             break;
9013         }
9014         /* fall through */
9015     case ARM_CP_STATE_AA32:
9016         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9017             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9018             assert(r->cp >= 14 && r->cp <= 15);
9019         } else {
9020             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9021         }
9022         break;
9023     case ARM_CP_STATE_AA64:
9024         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9025         break;
9026     default:
9027         g_assert_not_reached();
9028     }
9029     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
9030      * encodes a minimum access level for the register. We roll this
9031      * runtime check into our general permission check code, so check
9032      * here that the reginfo's specified permissions are strict enough
9033      * to encompass the generic architectural permission check.
9034      */
9035     if (r->state != ARM_CP_STATE_AA32) {
9036         CPAccessRights mask;
9037         switch (r->opc1) {
9038         case 0:
9039             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9040             mask = PL0U_R | PL1_RW;
9041             break;
9042         case 1: case 2:
9043             /* min_EL EL1 */
9044             mask = PL1_RW;
9045             break;
9046         case 3:
9047             /* min_EL EL0 */
9048             mask = PL0_RW;
9049             break;
9050         case 4:
9051         case 5:
9052             /* min_EL EL2 */
9053             mask = PL2_RW;
9054             break;
9055         case 6:
9056             /* min_EL EL3 */
9057             mask = PL3_RW;
9058             break;
9059         case 7:
9060             /* min_EL EL1, secure mode only (we don't check the latter) */
9061             mask = PL1_RW;
9062             break;
9063         default:
9064             /* broken reginfo with out-of-range opc1 */
9065             g_assert_not_reached();
9066         }
9067         /* assert our permissions are not too lax (stricter is fine) */
9068         assert((r->access & ~mask) == 0);
9069     }
9070 
9071     /* Check that the register definition has enough info to handle
9072      * reads and writes if they are permitted.
9073      */
9074     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9075         if (r->access & PL3_R) {
9076             assert((r->fieldoffset ||
9077                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9078                    r->readfn);
9079         }
9080         if (r->access & PL3_W) {
9081             assert((r->fieldoffset ||
9082                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9083                    r->writefn);
9084         }
9085     }
9086 
9087     for (crm = crmmin; crm <= crmmax; crm++) {
9088         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9089             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9090                 for (state = ARM_CP_STATE_AA32;
9091                      state <= ARM_CP_STATE_AA64; state++) {
9092                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9093                         continue;
9094                     }
9095                     if (state == ARM_CP_STATE_AA32) {
9096                         /* Under AArch32 CP registers can be common
9097                          * (same for secure and non-secure world) or banked.
9098                          */
9099                         char *name;
9100 
9101                         switch (r->secure) {
9102                         case ARM_CP_SECSTATE_S:
9103                         case ARM_CP_SECSTATE_NS:
9104                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9105                                                    r->secure, crm, opc1, opc2,
9106                                                    r->name);
9107                             break;
9108                         case ARM_CP_SECSTATE_BOTH:
9109                             name = g_strdup_printf("%s_S", r->name);
9110                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9111                                                    ARM_CP_SECSTATE_S,
9112                                                    crm, opc1, opc2, name);
9113                             g_free(name);
9114                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9115                                                    ARM_CP_SECSTATE_NS,
9116                                                    crm, opc1, opc2, r->name);
9117                             break;
9118                         default:
9119                             g_assert_not_reached();
9120                         }
9121                     } else {
9122                         /* AArch64 registers get mapped to non-secure instance
9123                          * of AArch32 */
9124                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9125                                                ARM_CP_SECSTATE_NS,
9126                                                crm, opc1, opc2, r->name);
9127                     }
9128                 }
9129             }
9130         }
9131     }
9132 }
9133 
9134 /* Define a whole list of registers */
9135 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9136                                         void *opaque, size_t len)
9137 {
9138     size_t i;
9139     for (i = 0; i < len; ++i) {
9140         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9141     }
9142 }
9143 
9144 /*
9145  * Modify ARMCPRegInfo for access from userspace.
9146  *
9147  * This is a data driven modification directed by
9148  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9149  * user-space cannot alter any values and dynamic values pertaining to
9150  * execution state are hidden from user space view anyway.
9151  */
9152 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9153                                  const ARMCPRegUserSpaceInfo *mods,
9154                                  size_t mods_len)
9155 {
9156     for (size_t mi = 0; mi < mods_len; ++mi) {
9157         const ARMCPRegUserSpaceInfo *m = mods + mi;
9158         GPatternSpec *pat = NULL;
9159 
9160         if (m->is_glob) {
9161             pat = g_pattern_spec_new(m->name);
9162         }
9163         for (size_t ri = 0; ri < regs_len; ++ri) {
9164             ARMCPRegInfo *r = regs + ri;
9165 
9166             if (pat && g_pattern_match_string(pat, r->name)) {
9167                 r->type = ARM_CP_CONST;
9168                 r->access = PL0U_R;
9169                 r->resetvalue = 0;
9170                 /* continue */
9171             } else if (strcmp(r->name, m->name) == 0) {
9172                 r->type = ARM_CP_CONST;
9173                 r->access = PL0U_R;
9174                 r->resetvalue &= m->exported_bits;
9175                 r->resetvalue |= m->fixed_bits;
9176                 break;
9177             }
9178         }
9179         if (pat) {
9180             g_pattern_spec_free(pat);
9181         }
9182     }
9183 }
9184 
9185 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9186 {
9187     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9188 }
9189 
9190 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9191                          uint64_t value)
9192 {
9193     /* Helper coprocessor write function for write-ignore registers */
9194 }
9195 
9196 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9197 {
9198     /* Helper coprocessor write function for read-as-zero registers */
9199     return 0;
9200 }
9201 
9202 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9203 {
9204     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9205 }
9206 
9207 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9208 {
9209     /* Return true if it is not valid for us to switch to
9210      * this CPU mode (ie all the UNPREDICTABLE cases in
9211      * the ARM ARM CPSRWriteByInstr pseudocode).
9212      */
9213 
9214     /* Changes to or from Hyp via MSR and CPS are illegal. */
9215     if (write_type == CPSRWriteByInstr &&
9216         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9217          mode == ARM_CPU_MODE_HYP)) {
9218         return 1;
9219     }
9220 
9221     switch (mode) {
9222     case ARM_CPU_MODE_USR:
9223         return 0;
9224     case ARM_CPU_MODE_SYS:
9225     case ARM_CPU_MODE_SVC:
9226     case ARM_CPU_MODE_ABT:
9227     case ARM_CPU_MODE_UND:
9228     case ARM_CPU_MODE_IRQ:
9229     case ARM_CPU_MODE_FIQ:
9230         /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
9231          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9232          */
9233         /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9234          * and CPS are treated as illegal mode changes.
9235          */
9236         if (write_type == CPSRWriteByInstr &&
9237             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9238             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9239             return 1;
9240         }
9241         return 0;
9242     case ARM_CPU_MODE_HYP:
9243         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9244     case ARM_CPU_MODE_MON:
9245         return arm_current_el(env) < 3;
9246     default:
9247         return 1;
9248     }
9249 }
9250 
9251 uint32_t cpsr_read(CPUARMState *env)
9252 {
9253     int ZF;
9254     ZF = (env->ZF == 0);
9255     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9256         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9257         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9258         | ((env->condexec_bits & 0xfc) << 8)
9259         | (env->GE << 16) | (env->daif & CPSR_AIF);
9260 }
9261 
9262 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9263                 CPSRWriteType write_type)
9264 {
9265     uint32_t changed_daif;
9266     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9267         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9268 
9269     if (mask & CPSR_NZCV) {
9270         env->ZF = (~val) & CPSR_Z;
9271         env->NF = val;
9272         env->CF = (val >> 29) & 1;
9273         env->VF = (val << 3) & 0x80000000;
9274     }
9275     if (mask & CPSR_Q)
9276         env->QF = ((val & CPSR_Q) != 0);
9277     if (mask & CPSR_T)
9278         env->thumb = ((val & CPSR_T) != 0);
9279     if (mask & CPSR_IT_0_1) {
9280         env->condexec_bits &= ~3;
9281         env->condexec_bits |= (val >> 25) & 3;
9282     }
9283     if (mask & CPSR_IT_2_7) {
9284         env->condexec_bits &= 3;
9285         env->condexec_bits |= (val >> 8) & 0xfc;
9286     }
9287     if (mask & CPSR_GE) {
9288         env->GE = (val >> 16) & 0xf;
9289     }
9290 
9291     /* In a V7 implementation that includes the security extensions but does
9292      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9293      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9294      * bits respectively.
9295      *
9296      * In a V8 implementation, it is permitted for privileged software to
9297      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9298      */
9299     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9300         arm_feature(env, ARM_FEATURE_EL3) &&
9301         !arm_feature(env, ARM_FEATURE_EL2) &&
9302         !arm_is_secure(env)) {
9303 
9304         changed_daif = (env->daif ^ val) & mask;
9305 
9306         if (changed_daif & CPSR_A) {
9307             /* Check to see if we are allowed to change the masking of async
9308              * abort exceptions from a non-secure state.
9309              */
9310             if (!(env->cp15.scr_el3 & SCR_AW)) {
9311                 qemu_log_mask(LOG_GUEST_ERROR,
9312                               "Ignoring attempt to switch CPSR_A flag from "
9313                               "non-secure world with SCR.AW bit clear\n");
9314                 mask &= ~CPSR_A;
9315             }
9316         }
9317 
9318         if (changed_daif & CPSR_F) {
9319             /* Check to see if we are allowed to change the masking of FIQ
9320              * exceptions from a non-secure state.
9321              */
9322             if (!(env->cp15.scr_el3 & SCR_FW)) {
9323                 qemu_log_mask(LOG_GUEST_ERROR,
9324                               "Ignoring attempt to switch CPSR_F flag from "
9325                               "non-secure world with SCR.FW bit clear\n");
9326                 mask &= ~CPSR_F;
9327             }
9328 
9329             /* Check whether non-maskable FIQ (NMFI) support is enabled.
9330              * If this bit is set software is not allowed to mask
9331              * FIQs, but is allowed to set CPSR_F to 0.
9332              */
9333             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9334                 (val & CPSR_F)) {
9335                 qemu_log_mask(LOG_GUEST_ERROR,
9336                               "Ignoring attempt to enable CPSR_F flag "
9337                               "(non-maskable FIQ [NMFI] support enabled)\n");
9338                 mask &= ~CPSR_F;
9339             }
9340         }
9341     }
9342 
9343     env->daif &= ~(CPSR_AIF & mask);
9344     env->daif |= val & CPSR_AIF & mask;
9345 
9346     if (write_type != CPSRWriteRaw &&
9347         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9348         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9349             /* Note that we can only get here in USR mode if this is a
9350              * gdb stub write; for this case we follow the architectural
9351              * behaviour for guest writes in USR mode of ignoring an attempt
9352              * to switch mode. (Those are caught by translate.c for writes
9353              * triggered by guest instructions.)
9354              */
9355             mask &= ~CPSR_M;
9356         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9357             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9358              * v7, and has defined behaviour in v8:
9359              *  + leave CPSR.M untouched
9360              *  + allow changes to the other CPSR fields
9361              *  + set PSTATE.IL
9362              * For user changes via the GDB stub, we don't set PSTATE.IL,
9363              * as this would be unnecessarily harsh for a user error.
9364              */
9365             mask &= ~CPSR_M;
9366             if (write_type != CPSRWriteByGDBStub &&
9367                 arm_feature(env, ARM_FEATURE_V8)) {
9368                 mask |= CPSR_IL;
9369                 val |= CPSR_IL;
9370             }
9371             qemu_log_mask(LOG_GUEST_ERROR,
9372                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9373                           aarch32_mode_name(env->uncached_cpsr),
9374                           aarch32_mode_name(val));
9375         } else {
9376             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9377                           write_type == CPSRWriteExceptionReturn ?
9378                           "Exception return from AArch32" :
9379                           "AArch32 mode switch from",
9380                           aarch32_mode_name(env->uncached_cpsr),
9381                           aarch32_mode_name(val), env->regs[15]);
9382             switch_mode(env, val & CPSR_M);
9383         }
9384     }
9385     mask &= ~CACHED_CPSR_BITS;
9386     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9387     if (rebuild_hflags) {
9388         arm_rebuild_hflags(env);
9389     }
9390 }
9391 
9392 /* Sign/zero extend */
9393 uint32_t HELPER(sxtb16)(uint32_t x)
9394 {
9395     uint32_t res;
9396     res = (uint16_t)(int8_t)x;
9397     res |= (uint32_t)(int8_t)(x >> 16) << 16;
9398     return res;
9399 }
9400 
9401 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9402 {
9403     /*
9404      * Take a division-by-zero exception if necessary; otherwise return
9405      * to get the usual non-trapping division behaviour (result of 0)
9406      */
9407     if (arm_feature(env, ARM_FEATURE_M)
9408         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9409         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9410     }
9411 }
9412 
9413 uint32_t HELPER(uxtb16)(uint32_t x)
9414 {
9415     uint32_t res;
9416     res = (uint16_t)(uint8_t)x;
9417     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9418     return res;
9419 }
9420 
9421 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9422 {
9423     if (den == 0) {
9424         handle_possible_div0_trap(env, GETPC());
9425         return 0;
9426     }
9427     if (num == INT_MIN && den == -1) {
9428         return INT_MIN;
9429     }
9430     return num / den;
9431 }
9432 
9433 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9434 {
9435     if (den == 0) {
9436         handle_possible_div0_trap(env, GETPC());
9437         return 0;
9438     }
9439     return num / den;
9440 }
9441 
9442 uint32_t HELPER(rbit)(uint32_t x)
9443 {
9444     return revbit32(x);
9445 }
9446 
9447 #ifdef CONFIG_USER_ONLY
9448 
9449 static void switch_mode(CPUARMState *env, int mode)
9450 {
9451     ARMCPU *cpu = env_archcpu(env);
9452 
9453     if (mode != ARM_CPU_MODE_USR) {
9454         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9455     }
9456 }
9457 
9458 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9459                                  uint32_t cur_el, bool secure)
9460 {
9461     return 1;
9462 }
9463 
9464 void aarch64_sync_64_to_32(CPUARMState *env)
9465 {
9466     g_assert_not_reached();
9467 }
9468 
9469 #else
9470 
9471 static void switch_mode(CPUARMState *env, int mode)
9472 {
9473     int old_mode;
9474     int i;
9475 
9476     old_mode = env->uncached_cpsr & CPSR_M;
9477     if (mode == old_mode)
9478         return;
9479 
9480     if (old_mode == ARM_CPU_MODE_FIQ) {
9481         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9482         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9483     } else if (mode == ARM_CPU_MODE_FIQ) {
9484         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9485         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9486     }
9487 
9488     i = bank_number(old_mode);
9489     env->banked_r13[i] = env->regs[13];
9490     env->banked_spsr[i] = env->spsr;
9491 
9492     i = bank_number(mode);
9493     env->regs[13] = env->banked_r13[i];
9494     env->spsr = env->banked_spsr[i];
9495 
9496     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9497     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9498 }
9499 
9500 /* Physical Interrupt Target EL Lookup Table
9501  *
9502  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9503  *
9504  * The below multi-dimensional table is used for looking up the target
9505  * exception level given numerous condition criteria.  Specifically, the
9506  * target EL is based on SCR and HCR routing controls as well as the
9507  * currently executing EL and secure state.
9508  *
9509  *    Dimensions:
9510  *    target_el_table[2][2][2][2][2][4]
9511  *                    |  |  |  |  |  +--- Current EL
9512  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9513  *                    |  |  |  +--------- HCR mask override
9514  *                    |  |  +------------ SCR exec state control
9515  *                    |  +--------------- SCR mask override
9516  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9517  *
9518  *    The table values are as such:
9519  *    0-3 = EL0-EL3
9520  *     -1 = Cannot occur
9521  *
9522  * The ARM ARM target EL table includes entries indicating that an "exception
9523  * is not taken".  The two cases where this is applicable are:
9524  *    1) An exception is taken from EL3 but the SCR does not have the exception
9525  *    routed to EL3.
9526  *    2) An exception is taken from EL2 but the HCR does not have the exception
9527  *    routed to EL2.
9528  * In these two cases, the below table contain a target of EL1.  This value is
9529  * returned as it is expected that the consumer of the table data will check
9530  * for "target EL >= current EL" to ensure the exception is not taken.
9531  *
9532  *            SCR     HCR
9533  *         64  EA     AMO                 From
9534  *        BIT IRQ     IMO      Non-secure         Secure
9535  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9536  */
9537 static const int8_t target_el_table[2][2][2][2][2][4] = {
9538     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9539        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9540       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9541        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9542      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9543        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9544       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9545        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9546     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9547        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9548       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9549        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9550      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9551        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9552       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9553        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9554 };
9555 
9556 /*
9557  * Determine the target EL for physical exceptions
9558  */
9559 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9560                                  uint32_t cur_el, bool secure)
9561 {
9562     CPUARMState *env = cs->env_ptr;
9563     bool rw;
9564     bool scr;
9565     bool hcr;
9566     int target_el;
9567     /* Is the highest EL AArch64? */
9568     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9569     uint64_t hcr_el2;
9570 
9571     if (arm_feature(env, ARM_FEATURE_EL3)) {
9572         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9573     } else {
9574         /* Either EL2 is the highest EL (and so the EL2 register width
9575          * is given by is64); or there is no EL2 or EL3, in which case
9576          * the value of 'rw' does not affect the table lookup anyway.
9577          */
9578         rw = is64;
9579     }
9580 
9581     hcr_el2 = arm_hcr_el2_eff(env);
9582     switch (excp_idx) {
9583     case EXCP_IRQ:
9584         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9585         hcr = hcr_el2 & HCR_IMO;
9586         break;
9587     case EXCP_FIQ:
9588         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9589         hcr = hcr_el2 & HCR_FMO;
9590         break;
9591     default:
9592         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9593         hcr = hcr_el2 & HCR_AMO;
9594         break;
9595     };
9596 
9597     /*
9598      * For these purposes, TGE and AMO/IMO/FMO both force the
9599      * interrupt to EL2.  Fold TGE into the bit extracted above.
9600      */
9601     hcr |= (hcr_el2 & HCR_TGE) != 0;
9602 
9603     /* Perform a table-lookup for the target EL given the current state */
9604     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9605 
9606     assert(target_el > 0);
9607 
9608     return target_el;
9609 }
9610 
9611 void arm_log_exception(CPUState *cs)
9612 {
9613     int idx = cs->exception_index;
9614 
9615     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9616         const char *exc = NULL;
9617         static const char * const excnames[] = {
9618             [EXCP_UDEF] = "Undefined Instruction",
9619             [EXCP_SWI] = "SVC",
9620             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9621             [EXCP_DATA_ABORT] = "Data Abort",
9622             [EXCP_IRQ] = "IRQ",
9623             [EXCP_FIQ] = "FIQ",
9624             [EXCP_BKPT] = "Breakpoint",
9625             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9626             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9627             [EXCP_HVC] = "Hypervisor Call",
9628             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9629             [EXCP_SMC] = "Secure Monitor Call",
9630             [EXCP_VIRQ] = "Virtual IRQ",
9631             [EXCP_VFIQ] = "Virtual FIQ",
9632             [EXCP_SEMIHOST] = "Semihosting call",
9633             [EXCP_NOCP] = "v7M NOCP UsageFault",
9634             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9635             [EXCP_STKOF] = "v8M STKOF UsageFault",
9636             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9637             [EXCP_LSERR] = "v8M LSERR UsageFault",
9638             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9639             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9640             [EXCP_VSERR] = "Virtual SERR",
9641         };
9642 
9643         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9644             exc = excnames[idx];
9645         }
9646         if (!exc) {
9647             exc = "unknown";
9648         }
9649         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9650                       idx, exc, cs->cpu_index);
9651     }
9652 }
9653 
9654 /*
9655  * Function used to synchronize QEMU's AArch64 register set with AArch32
9656  * register set.  This is necessary when switching between AArch32 and AArch64
9657  * execution state.
9658  */
9659 void aarch64_sync_32_to_64(CPUARMState *env)
9660 {
9661     int i;
9662     uint32_t mode = env->uncached_cpsr & CPSR_M;
9663 
9664     /* We can blanket copy R[0:7] to X[0:7] */
9665     for (i = 0; i < 8; i++) {
9666         env->xregs[i] = env->regs[i];
9667     }
9668 
9669     /*
9670      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9671      * Otherwise, they come from the banked user regs.
9672      */
9673     if (mode == ARM_CPU_MODE_FIQ) {
9674         for (i = 8; i < 13; i++) {
9675             env->xregs[i] = env->usr_regs[i - 8];
9676         }
9677     } else {
9678         for (i = 8; i < 13; i++) {
9679             env->xregs[i] = env->regs[i];
9680         }
9681     }
9682 
9683     /*
9684      * Registers x13-x23 are the various mode SP and FP registers. Registers
9685      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9686      * from the mode banked register.
9687      */
9688     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9689         env->xregs[13] = env->regs[13];
9690         env->xregs[14] = env->regs[14];
9691     } else {
9692         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9693         /* HYP is an exception in that it is copied from r14 */
9694         if (mode == ARM_CPU_MODE_HYP) {
9695             env->xregs[14] = env->regs[14];
9696         } else {
9697             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9698         }
9699     }
9700 
9701     if (mode == ARM_CPU_MODE_HYP) {
9702         env->xregs[15] = env->regs[13];
9703     } else {
9704         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9705     }
9706 
9707     if (mode == ARM_CPU_MODE_IRQ) {
9708         env->xregs[16] = env->regs[14];
9709         env->xregs[17] = env->regs[13];
9710     } else {
9711         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9712         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9713     }
9714 
9715     if (mode == ARM_CPU_MODE_SVC) {
9716         env->xregs[18] = env->regs[14];
9717         env->xregs[19] = env->regs[13];
9718     } else {
9719         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9720         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9721     }
9722 
9723     if (mode == ARM_CPU_MODE_ABT) {
9724         env->xregs[20] = env->regs[14];
9725         env->xregs[21] = env->regs[13];
9726     } else {
9727         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9728         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9729     }
9730 
9731     if (mode == ARM_CPU_MODE_UND) {
9732         env->xregs[22] = env->regs[14];
9733         env->xregs[23] = env->regs[13];
9734     } else {
9735         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9736         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9737     }
9738 
9739     /*
9740      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9741      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9742      * FIQ bank for r8-r14.
9743      */
9744     if (mode == ARM_CPU_MODE_FIQ) {
9745         for (i = 24; i < 31; i++) {
9746             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9747         }
9748     } else {
9749         for (i = 24; i < 29; i++) {
9750             env->xregs[i] = env->fiq_regs[i - 24];
9751         }
9752         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9753         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9754     }
9755 
9756     env->pc = env->regs[15];
9757 }
9758 
9759 /*
9760  * Function used to synchronize QEMU's AArch32 register set with AArch64
9761  * register set.  This is necessary when switching between AArch32 and AArch64
9762  * execution state.
9763  */
9764 void aarch64_sync_64_to_32(CPUARMState *env)
9765 {
9766     int i;
9767     uint32_t mode = env->uncached_cpsr & CPSR_M;
9768 
9769     /* We can blanket copy X[0:7] to R[0:7] */
9770     for (i = 0; i < 8; i++) {
9771         env->regs[i] = env->xregs[i];
9772     }
9773 
9774     /*
9775      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9776      * Otherwise, we copy x8-x12 into the banked user regs.
9777      */
9778     if (mode == ARM_CPU_MODE_FIQ) {
9779         for (i = 8; i < 13; i++) {
9780             env->usr_regs[i - 8] = env->xregs[i];
9781         }
9782     } else {
9783         for (i = 8; i < 13; i++) {
9784             env->regs[i] = env->xregs[i];
9785         }
9786     }
9787 
9788     /*
9789      * Registers r13 & r14 depend on the current mode.
9790      * If we are in a given mode, we copy the corresponding x registers to r13
9791      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9792      * for the mode.
9793      */
9794     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9795         env->regs[13] = env->xregs[13];
9796         env->regs[14] = env->xregs[14];
9797     } else {
9798         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9799 
9800         /*
9801          * HYP is an exception in that it does not have its own banked r14 but
9802          * shares the USR r14
9803          */
9804         if (mode == ARM_CPU_MODE_HYP) {
9805             env->regs[14] = env->xregs[14];
9806         } else {
9807             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9808         }
9809     }
9810 
9811     if (mode == ARM_CPU_MODE_HYP) {
9812         env->regs[13] = env->xregs[15];
9813     } else {
9814         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9815     }
9816 
9817     if (mode == ARM_CPU_MODE_IRQ) {
9818         env->regs[14] = env->xregs[16];
9819         env->regs[13] = env->xregs[17];
9820     } else {
9821         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9822         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9823     }
9824 
9825     if (mode == ARM_CPU_MODE_SVC) {
9826         env->regs[14] = env->xregs[18];
9827         env->regs[13] = env->xregs[19];
9828     } else {
9829         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9830         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9831     }
9832 
9833     if (mode == ARM_CPU_MODE_ABT) {
9834         env->regs[14] = env->xregs[20];
9835         env->regs[13] = env->xregs[21];
9836     } else {
9837         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9838         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9839     }
9840 
9841     if (mode == ARM_CPU_MODE_UND) {
9842         env->regs[14] = env->xregs[22];
9843         env->regs[13] = env->xregs[23];
9844     } else {
9845         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9846         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9847     }
9848 
9849     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9850      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9851      * FIQ bank for r8-r14.
9852      */
9853     if (mode == ARM_CPU_MODE_FIQ) {
9854         for (i = 24; i < 31; i++) {
9855             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9856         }
9857     } else {
9858         for (i = 24; i < 29; i++) {
9859             env->fiq_regs[i - 24] = env->xregs[i];
9860         }
9861         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9862         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9863     }
9864 
9865     env->regs[15] = env->pc;
9866 }
9867 
9868 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9869                                    uint32_t mask, uint32_t offset,
9870                                    uint32_t newpc)
9871 {
9872     int new_el;
9873 
9874     /* Change the CPU state so as to actually take the exception. */
9875     switch_mode(env, new_mode);
9876 
9877     /*
9878      * For exceptions taken to AArch32 we must clear the SS bit in both
9879      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9880      */
9881     env->pstate &= ~PSTATE_SS;
9882     env->spsr = cpsr_read(env);
9883     /* Clear IT bits.  */
9884     env->condexec_bits = 0;
9885     /* Switch to the new mode, and to the correct instruction set.  */
9886     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9887 
9888     /* This must be after mode switching. */
9889     new_el = arm_current_el(env);
9890 
9891     /* Set new mode endianness */
9892     env->uncached_cpsr &= ~CPSR_E;
9893     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9894         env->uncached_cpsr |= CPSR_E;
9895     }
9896     /* J and IL must always be cleared for exception entry */
9897     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9898     env->daif |= mask;
9899 
9900     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9901         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9902             env->uncached_cpsr |= CPSR_SSBS;
9903         } else {
9904             env->uncached_cpsr &= ~CPSR_SSBS;
9905         }
9906     }
9907 
9908     if (new_mode == ARM_CPU_MODE_HYP) {
9909         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9910         env->elr_el[2] = env->regs[15];
9911     } else {
9912         /* CPSR.PAN is normally preserved preserved unless...  */
9913         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9914             switch (new_el) {
9915             case 3:
9916                 if (!arm_is_secure_below_el3(env)) {
9917                     /* ... the target is EL3, from non-secure state.  */
9918                     env->uncached_cpsr &= ~CPSR_PAN;
9919                     break;
9920                 }
9921                 /* ... the target is EL3, from secure state ... */
9922                 /* fall through */
9923             case 1:
9924                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9925                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9926                     env->uncached_cpsr |= CPSR_PAN;
9927                 }
9928                 break;
9929             }
9930         }
9931         /*
9932          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9933          * and we should just guard the thumb mode on V4
9934          */
9935         if (arm_feature(env, ARM_FEATURE_V4T)) {
9936             env->thumb =
9937                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9938         }
9939         env->regs[14] = env->regs[15] + offset;
9940     }
9941     env->regs[15] = newpc;
9942     arm_rebuild_hflags(env);
9943 }
9944 
9945 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9946 {
9947     /*
9948      * Handle exception entry to Hyp mode; this is sufficiently
9949      * different to entry to other AArch32 modes that we handle it
9950      * separately here.
9951      *
9952      * The vector table entry used is always the 0x14 Hyp mode entry point,
9953      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9954      * The offset applied to the preferred return address is always zero
9955      * (see DDI0487C.a section G1.12.3).
9956      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9957      */
9958     uint32_t addr, mask;
9959     ARMCPU *cpu = ARM_CPU(cs);
9960     CPUARMState *env = &cpu->env;
9961 
9962     switch (cs->exception_index) {
9963     case EXCP_UDEF:
9964         addr = 0x04;
9965         break;
9966     case EXCP_SWI:
9967         addr = 0x08;
9968         break;
9969     case EXCP_BKPT:
9970         /* Fall through to prefetch abort.  */
9971     case EXCP_PREFETCH_ABORT:
9972         env->cp15.ifar_s = env->exception.vaddress;
9973         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9974                       (uint32_t)env->exception.vaddress);
9975         addr = 0x0c;
9976         break;
9977     case EXCP_DATA_ABORT:
9978         env->cp15.dfar_s = env->exception.vaddress;
9979         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9980                       (uint32_t)env->exception.vaddress);
9981         addr = 0x10;
9982         break;
9983     case EXCP_IRQ:
9984         addr = 0x18;
9985         break;
9986     case EXCP_FIQ:
9987         addr = 0x1c;
9988         break;
9989     case EXCP_HVC:
9990         addr = 0x08;
9991         break;
9992     case EXCP_HYP_TRAP:
9993         addr = 0x14;
9994         break;
9995     default:
9996         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9997     }
9998 
9999     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10000         if (!arm_feature(env, ARM_FEATURE_V8)) {
10001             /*
10002              * QEMU syndrome values are v8-style. v7 has the IL bit
10003              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10004              * If this is a v7 CPU, squash the IL bit in those cases.
10005              */
10006             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10007                 (cs->exception_index == EXCP_DATA_ABORT &&
10008                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10009                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10010                 env->exception.syndrome &= ~ARM_EL_IL;
10011             }
10012         }
10013         env->cp15.esr_el[2] = env->exception.syndrome;
10014     }
10015 
10016     if (arm_current_el(env) != 2 && addr < 0x14) {
10017         addr = 0x14;
10018     }
10019 
10020     mask = 0;
10021     if (!(env->cp15.scr_el3 & SCR_EA)) {
10022         mask |= CPSR_A;
10023     }
10024     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10025         mask |= CPSR_I;
10026     }
10027     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10028         mask |= CPSR_F;
10029     }
10030 
10031     addr += env->cp15.hvbar;
10032 
10033     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10034 }
10035 
10036 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10037 {
10038     ARMCPU *cpu = ARM_CPU(cs);
10039     CPUARMState *env = &cpu->env;
10040     uint32_t addr;
10041     uint32_t mask;
10042     int new_mode;
10043     uint32_t offset;
10044     uint32_t moe;
10045 
10046     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10047     switch (syn_get_ec(env->exception.syndrome)) {
10048     case EC_BREAKPOINT:
10049     case EC_BREAKPOINT_SAME_EL:
10050         moe = 1;
10051         break;
10052     case EC_WATCHPOINT:
10053     case EC_WATCHPOINT_SAME_EL:
10054         moe = 10;
10055         break;
10056     case EC_AA32_BKPT:
10057         moe = 3;
10058         break;
10059     case EC_VECTORCATCH:
10060         moe = 5;
10061         break;
10062     default:
10063         moe = 0;
10064         break;
10065     }
10066 
10067     if (moe) {
10068         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10069     }
10070 
10071     if (env->exception.target_el == 2) {
10072         arm_cpu_do_interrupt_aarch32_hyp(cs);
10073         return;
10074     }
10075 
10076     switch (cs->exception_index) {
10077     case EXCP_UDEF:
10078         new_mode = ARM_CPU_MODE_UND;
10079         addr = 0x04;
10080         mask = CPSR_I;
10081         if (env->thumb)
10082             offset = 2;
10083         else
10084             offset = 4;
10085         break;
10086     case EXCP_SWI:
10087         new_mode = ARM_CPU_MODE_SVC;
10088         addr = 0x08;
10089         mask = CPSR_I;
10090         /* The PC already points to the next instruction.  */
10091         offset = 0;
10092         break;
10093     case EXCP_BKPT:
10094         /* Fall through to prefetch abort.  */
10095     case EXCP_PREFETCH_ABORT:
10096         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10097         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10098         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10099                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10100         new_mode = ARM_CPU_MODE_ABT;
10101         addr = 0x0c;
10102         mask = CPSR_A | CPSR_I;
10103         offset = 4;
10104         break;
10105     case EXCP_DATA_ABORT:
10106         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10107         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10108         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10109                       env->exception.fsr,
10110                       (uint32_t)env->exception.vaddress);
10111         new_mode = ARM_CPU_MODE_ABT;
10112         addr = 0x10;
10113         mask = CPSR_A | CPSR_I;
10114         offset = 8;
10115         break;
10116     case EXCP_IRQ:
10117         new_mode = ARM_CPU_MODE_IRQ;
10118         addr = 0x18;
10119         /* Disable IRQ and imprecise data aborts.  */
10120         mask = CPSR_A | CPSR_I;
10121         offset = 4;
10122         if (env->cp15.scr_el3 & SCR_IRQ) {
10123             /* IRQ routed to monitor mode */
10124             new_mode = ARM_CPU_MODE_MON;
10125             mask |= CPSR_F;
10126         }
10127         break;
10128     case EXCP_FIQ:
10129         new_mode = ARM_CPU_MODE_FIQ;
10130         addr = 0x1c;
10131         /* Disable FIQ, IRQ and imprecise data aborts.  */
10132         mask = CPSR_A | CPSR_I | CPSR_F;
10133         if (env->cp15.scr_el3 & SCR_FIQ) {
10134             /* FIQ routed to monitor mode */
10135             new_mode = ARM_CPU_MODE_MON;
10136         }
10137         offset = 4;
10138         break;
10139     case EXCP_VIRQ:
10140         new_mode = ARM_CPU_MODE_IRQ;
10141         addr = 0x18;
10142         /* Disable IRQ and imprecise data aborts.  */
10143         mask = CPSR_A | CPSR_I;
10144         offset = 4;
10145         break;
10146     case EXCP_VFIQ:
10147         new_mode = ARM_CPU_MODE_FIQ;
10148         addr = 0x1c;
10149         /* Disable FIQ, IRQ and imprecise data aborts.  */
10150         mask = CPSR_A | CPSR_I | CPSR_F;
10151         offset = 4;
10152         break;
10153     case EXCP_VSERR:
10154         {
10155             /*
10156              * Note that this is reported as a data abort, but the DFAR
10157              * has an UNKNOWN value.  Construct the SError syndrome from
10158              * AET and ExT fields.
10159              */
10160             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10161 
10162             if (extended_addresses_enabled(env)) {
10163                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10164             } else {
10165                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10166             }
10167             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10168             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10169             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10170                           env->exception.fsr);
10171 
10172             new_mode = ARM_CPU_MODE_ABT;
10173             addr = 0x10;
10174             mask = CPSR_A | CPSR_I;
10175             offset = 8;
10176         }
10177         break;
10178     case EXCP_SMC:
10179         new_mode = ARM_CPU_MODE_MON;
10180         addr = 0x08;
10181         mask = CPSR_A | CPSR_I | CPSR_F;
10182         offset = 0;
10183         break;
10184     default:
10185         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10186         return; /* Never happens.  Keep compiler happy.  */
10187     }
10188 
10189     if (new_mode == ARM_CPU_MODE_MON) {
10190         addr += env->cp15.mvbar;
10191     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10192         /* High vectors. When enabled, base address cannot be remapped. */
10193         addr += 0xffff0000;
10194     } else {
10195         /* ARM v7 architectures provide a vector base address register to remap
10196          * the interrupt vector table.
10197          * This register is only followed in non-monitor mode, and is banked.
10198          * Note: only bits 31:5 are valid.
10199          */
10200         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10201     }
10202 
10203     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10204         env->cp15.scr_el3 &= ~SCR_NS;
10205     }
10206 
10207     take_aarch32_exception(env, new_mode, mask, offset, addr);
10208 }
10209 
10210 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10211 {
10212     /*
10213      * Return the register number of the AArch64 view of the AArch32
10214      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10215      * be that of the AArch32 mode the exception came from.
10216      */
10217     int mode = env->uncached_cpsr & CPSR_M;
10218 
10219     switch (aarch32_reg) {
10220     case 0 ... 7:
10221         return aarch32_reg;
10222     case 8 ... 12:
10223         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10224     case 13:
10225         switch (mode) {
10226         case ARM_CPU_MODE_USR:
10227         case ARM_CPU_MODE_SYS:
10228             return 13;
10229         case ARM_CPU_MODE_HYP:
10230             return 15;
10231         case ARM_CPU_MODE_IRQ:
10232             return 17;
10233         case ARM_CPU_MODE_SVC:
10234             return 19;
10235         case ARM_CPU_MODE_ABT:
10236             return 21;
10237         case ARM_CPU_MODE_UND:
10238             return 23;
10239         case ARM_CPU_MODE_FIQ:
10240             return 29;
10241         default:
10242             g_assert_not_reached();
10243         }
10244     case 14:
10245         switch (mode) {
10246         case ARM_CPU_MODE_USR:
10247         case ARM_CPU_MODE_SYS:
10248         case ARM_CPU_MODE_HYP:
10249             return 14;
10250         case ARM_CPU_MODE_IRQ:
10251             return 16;
10252         case ARM_CPU_MODE_SVC:
10253             return 18;
10254         case ARM_CPU_MODE_ABT:
10255             return 20;
10256         case ARM_CPU_MODE_UND:
10257             return 22;
10258         case ARM_CPU_MODE_FIQ:
10259             return 30;
10260         default:
10261             g_assert_not_reached();
10262         }
10263     case 15:
10264         return 31;
10265     default:
10266         g_assert_not_reached();
10267     }
10268 }
10269 
10270 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10271 {
10272     uint32_t ret = cpsr_read(env);
10273 
10274     /* Move DIT to the correct location for SPSR_ELx */
10275     if (ret & CPSR_DIT) {
10276         ret &= ~CPSR_DIT;
10277         ret |= PSTATE_DIT;
10278     }
10279     /* Merge PSTATE.SS into SPSR_ELx */
10280     ret |= env->pstate & PSTATE_SS;
10281 
10282     return ret;
10283 }
10284 
10285 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10286 {
10287     /* Return true if this syndrome value is a synchronous external abort */
10288     switch (syn_get_ec(syndrome)) {
10289     case EC_INSNABORT:
10290     case EC_INSNABORT_SAME_EL:
10291     case EC_DATAABORT:
10292     case EC_DATAABORT_SAME_EL:
10293         /* Look at fault status code for all the synchronous ext abort cases */
10294         switch (syndrome & 0x3f) {
10295         case 0x10:
10296         case 0x13:
10297         case 0x14:
10298         case 0x15:
10299         case 0x16:
10300         case 0x17:
10301             return true;
10302         default:
10303             return false;
10304         }
10305     default:
10306         return false;
10307     }
10308 }
10309 
10310 /* Handle exception entry to a target EL which is using AArch64 */
10311 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10312 {
10313     ARMCPU *cpu = ARM_CPU(cs);
10314     CPUARMState *env = &cpu->env;
10315     unsigned int new_el = env->exception.target_el;
10316     target_ulong addr = env->cp15.vbar_el[new_el];
10317     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10318     unsigned int old_mode;
10319     unsigned int cur_el = arm_current_el(env);
10320     int rt;
10321 
10322     /*
10323      * Note that new_el can never be 0.  If cur_el is 0, then
10324      * el0_a64 is is_a64(), else el0_a64 is ignored.
10325      */
10326     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10327 
10328     if (cur_el < new_el) {
10329         /* Entry vector offset depends on whether the implemented EL
10330          * immediately lower than the target level is using AArch32 or AArch64
10331          */
10332         bool is_aa64;
10333         uint64_t hcr;
10334 
10335         switch (new_el) {
10336         case 3:
10337             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10338             break;
10339         case 2:
10340             hcr = arm_hcr_el2_eff(env);
10341             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10342                 is_aa64 = (hcr & HCR_RW) != 0;
10343                 break;
10344             }
10345             /* fall through */
10346         case 1:
10347             is_aa64 = is_a64(env);
10348             break;
10349         default:
10350             g_assert_not_reached();
10351         }
10352 
10353         if (is_aa64) {
10354             addr += 0x400;
10355         } else {
10356             addr += 0x600;
10357         }
10358     } else if (pstate_read(env) & PSTATE_SP) {
10359         addr += 0x200;
10360     }
10361 
10362     switch (cs->exception_index) {
10363     case EXCP_PREFETCH_ABORT:
10364     case EXCP_DATA_ABORT:
10365         /*
10366          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10367          * to be taken to the SError vector entrypoint.
10368          */
10369         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10370             syndrome_is_sync_extabt(env->exception.syndrome)) {
10371             addr += 0x180;
10372         }
10373         env->cp15.far_el[new_el] = env->exception.vaddress;
10374         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10375                       env->cp15.far_el[new_el]);
10376         /* fall through */
10377     case EXCP_BKPT:
10378     case EXCP_UDEF:
10379     case EXCP_SWI:
10380     case EXCP_HVC:
10381     case EXCP_HYP_TRAP:
10382     case EXCP_SMC:
10383         switch (syn_get_ec(env->exception.syndrome)) {
10384         case EC_ADVSIMDFPACCESSTRAP:
10385             /*
10386              * QEMU internal FP/SIMD syndromes from AArch32 include the
10387              * TA and coproc fields which are only exposed if the exception
10388              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10389              * AArch64 format syndrome.
10390              */
10391             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10392             break;
10393         case EC_CP14RTTRAP:
10394         case EC_CP15RTTRAP:
10395         case EC_CP14DTTRAP:
10396             /*
10397              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10398              * the raw register field from the insn; when taking this to
10399              * AArch64 we must convert it to the AArch64 view of the register
10400              * number. Notice that we read a 4-bit AArch32 register number and
10401              * write back a 5-bit AArch64 one.
10402              */
10403             rt = extract32(env->exception.syndrome, 5, 4);
10404             rt = aarch64_regnum(env, rt);
10405             env->exception.syndrome = deposit32(env->exception.syndrome,
10406                                                 5, 5, rt);
10407             break;
10408         case EC_CP15RRTTRAP:
10409         case EC_CP14RRTTRAP:
10410             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10411             rt = extract32(env->exception.syndrome, 5, 4);
10412             rt = aarch64_regnum(env, rt);
10413             env->exception.syndrome = deposit32(env->exception.syndrome,
10414                                                 5, 5, rt);
10415             rt = extract32(env->exception.syndrome, 10, 4);
10416             rt = aarch64_regnum(env, rt);
10417             env->exception.syndrome = deposit32(env->exception.syndrome,
10418                                                 10, 5, rt);
10419             break;
10420         }
10421         env->cp15.esr_el[new_el] = env->exception.syndrome;
10422         break;
10423     case EXCP_IRQ:
10424     case EXCP_VIRQ:
10425         addr += 0x80;
10426         break;
10427     case EXCP_FIQ:
10428     case EXCP_VFIQ:
10429         addr += 0x100;
10430         break;
10431     case EXCP_VSERR:
10432         addr += 0x180;
10433         /* Construct the SError syndrome from IDS and ISS fields. */
10434         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10435         env->cp15.esr_el[new_el] = env->exception.syndrome;
10436         break;
10437     default:
10438         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10439     }
10440 
10441     if (is_a64(env)) {
10442         old_mode = pstate_read(env);
10443         aarch64_save_sp(env, arm_current_el(env));
10444         env->elr_el[new_el] = env->pc;
10445     } else {
10446         old_mode = cpsr_read_for_spsr_elx(env);
10447         env->elr_el[new_el] = env->regs[15];
10448 
10449         aarch64_sync_32_to_64(env);
10450 
10451         env->condexec_bits = 0;
10452     }
10453     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10454 
10455     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10456                   env->elr_el[new_el]);
10457 
10458     if (cpu_isar_feature(aa64_pan, cpu)) {
10459         /* The value of PSTATE.PAN is normally preserved, except when ... */
10460         new_mode |= old_mode & PSTATE_PAN;
10461         switch (new_el) {
10462         case 2:
10463             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10464             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10465                 != (HCR_E2H | HCR_TGE)) {
10466                 break;
10467             }
10468             /* fall through */
10469         case 1:
10470             /* ... the target is EL1 ... */
10471             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10472             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10473                 new_mode |= PSTATE_PAN;
10474             }
10475             break;
10476         }
10477     }
10478     if (cpu_isar_feature(aa64_mte, cpu)) {
10479         new_mode |= PSTATE_TCO;
10480     }
10481 
10482     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10483         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10484             new_mode |= PSTATE_SSBS;
10485         } else {
10486             new_mode &= ~PSTATE_SSBS;
10487         }
10488     }
10489 
10490     pstate_write(env, PSTATE_DAIF | new_mode);
10491     env->aarch64 = true;
10492     aarch64_restore_sp(env, new_el);
10493     helper_rebuild_hflags_a64(env, new_el);
10494 
10495     env->pc = addr;
10496 
10497     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10498                   new_el, env->pc, pstate_read(env));
10499 }
10500 
10501 /*
10502  * Do semihosting call and set the appropriate return value. All the
10503  * permission and validity checks have been done at translate time.
10504  *
10505  * We only see semihosting exceptions in TCG only as they are not
10506  * trapped to the hypervisor in KVM.
10507  */
10508 #ifdef CONFIG_TCG
10509 static void handle_semihosting(CPUState *cs)
10510 {
10511     ARMCPU *cpu = ARM_CPU(cs);
10512     CPUARMState *env = &cpu->env;
10513 
10514     if (is_a64(env)) {
10515         qemu_log_mask(CPU_LOG_INT,
10516                       "...handling as semihosting call 0x%" PRIx64 "\n",
10517                       env->xregs[0]);
10518         do_common_semihosting(cs);
10519         env->pc += 4;
10520     } else {
10521         qemu_log_mask(CPU_LOG_INT,
10522                       "...handling as semihosting call 0x%x\n",
10523                       env->regs[0]);
10524         do_common_semihosting(cs);
10525         env->regs[15] += env->thumb ? 2 : 4;
10526     }
10527 }
10528 #endif
10529 
10530 /* Handle a CPU exception for A and R profile CPUs.
10531  * Do any appropriate logging, handle PSCI calls, and then hand off
10532  * to the AArch64-entry or AArch32-entry function depending on the
10533  * target exception level's register width.
10534  *
10535  * Note: this is used for both TCG (as the do_interrupt tcg op),
10536  *       and KVM to re-inject guest debug exceptions, and to
10537  *       inject a Synchronous-External-Abort.
10538  */
10539 void arm_cpu_do_interrupt(CPUState *cs)
10540 {
10541     ARMCPU *cpu = ARM_CPU(cs);
10542     CPUARMState *env = &cpu->env;
10543     unsigned int new_el = env->exception.target_el;
10544 
10545     assert(!arm_feature(env, ARM_FEATURE_M));
10546 
10547     arm_log_exception(cs);
10548     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10549                   new_el);
10550     if (qemu_loglevel_mask(CPU_LOG_INT)
10551         && !excp_is_internal(cs->exception_index)) {
10552         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10553                       syn_get_ec(env->exception.syndrome),
10554                       env->exception.syndrome);
10555     }
10556 
10557     if (arm_is_psci_call(cpu, cs->exception_index)) {
10558         arm_handle_psci_call(cpu);
10559         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10560         return;
10561     }
10562 
10563     /*
10564      * Semihosting semantics depend on the register width of the code
10565      * that caused the exception, not the target exception level, so
10566      * must be handled here.
10567      */
10568 #ifdef CONFIG_TCG
10569     if (cs->exception_index == EXCP_SEMIHOST) {
10570         handle_semihosting(cs);
10571         return;
10572     }
10573 #endif
10574 
10575     /* Hooks may change global state so BQL should be held, also the
10576      * BQL needs to be held for any modification of
10577      * cs->interrupt_request.
10578      */
10579     g_assert(qemu_mutex_iothread_locked());
10580 
10581     arm_call_pre_el_change_hook(cpu);
10582 
10583     assert(!excp_is_internal(cs->exception_index));
10584     if (arm_el_is_aa64(env, new_el)) {
10585         arm_cpu_do_interrupt_aarch64(cs);
10586     } else {
10587         arm_cpu_do_interrupt_aarch32(cs);
10588     }
10589 
10590     arm_call_el_change_hook(cpu);
10591 
10592     if (!kvm_enabled()) {
10593         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10594     }
10595 }
10596 #endif /* !CONFIG_USER_ONLY */
10597 
10598 uint64_t arm_sctlr(CPUARMState *env, int el)
10599 {
10600     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10601     if (el == 0) {
10602         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10603         el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10604              ? 2 : 1;
10605     }
10606     return env->cp15.sctlr_el[el];
10607 }
10608 
10609 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10610 {
10611     if (regime_has_2_ranges(mmu_idx)) {
10612         return extract64(tcr, 37, 2);
10613     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10614         return 0; /* VTCR_EL2 */
10615     } else {
10616         /* Replicate the single TBI bit so we always have 2 bits.  */
10617         return extract32(tcr, 20, 1) * 3;
10618     }
10619 }
10620 
10621 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10622 {
10623     if (regime_has_2_ranges(mmu_idx)) {
10624         return extract64(tcr, 51, 2);
10625     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10626         return 0; /* VTCR_EL2 */
10627     } else {
10628         /* Replicate the single TBID bit so we always have 2 bits.  */
10629         return extract32(tcr, 29, 1) * 3;
10630     }
10631 }
10632 
10633 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10634 {
10635     if (regime_has_2_ranges(mmu_idx)) {
10636         return extract64(tcr, 57, 2);
10637     } else {
10638         /* Replicate the single TCMA bit so we always have 2 bits.  */
10639         return extract32(tcr, 30, 1) * 3;
10640     }
10641 }
10642 
10643 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10644                                    ARMMMUIdx mmu_idx, bool data)
10645 {
10646     uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10647     bool epd, hpd, using16k, using64k, tsz_oob, ds;
10648     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10649     ARMCPU *cpu = env_archcpu(env);
10650 
10651     if (!regime_has_2_ranges(mmu_idx)) {
10652         select = 0;
10653         tsz = extract32(tcr, 0, 6);
10654         using64k = extract32(tcr, 14, 1);
10655         using16k = extract32(tcr, 15, 1);
10656         if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10657             /* VTCR_EL2 */
10658             hpd = false;
10659         } else {
10660             hpd = extract32(tcr, 24, 1);
10661         }
10662         epd = false;
10663         sh = extract32(tcr, 12, 2);
10664         ps = extract32(tcr, 16, 3);
10665         ds = extract64(tcr, 32, 1);
10666     } else {
10667         /*
10668          * Bit 55 is always between the two regions, and is canonical for
10669          * determining if address tagging is enabled.
10670          */
10671         select = extract64(va, 55, 1);
10672         if (!select) {
10673             tsz = extract32(tcr, 0, 6);
10674             epd = extract32(tcr, 7, 1);
10675             sh = extract32(tcr, 12, 2);
10676             using64k = extract32(tcr, 14, 1);
10677             using16k = extract32(tcr, 15, 1);
10678             hpd = extract64(tcr, 41, 1);
10679         } else {
10680             int tg = extract32(tcr, 30, 2);
10681             using16k = tg == 1;
10682             using64k = tg == 3;
10683             tsz = extract32(tcr, 16, 6);
10684             epd = extract32(tcr, 23, 1);
10685             sh = extract32(tcr, 28, 2);
10686             hpd = extract64(tcr, 42, 1);
10687         }
10688         ps = extract64(tcr, 32, 3);
10689         ds = extract64(tcr, 59, 1);
10690     }
10691 
10692     if (cpu_isar_feature(aa64_st, cpu)) {
10693         max_tsz = 48 - using64k;
10694     } else {
10695         max_tsz = 39;
10696     }
10697 
10698     /*
10699      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10700      * adjust the effective value of DS, as documented.
10701      */
10702     min_tsz = 16;
10703     if (using64k) {
10704         if (cpu_isar_feature(aa64_lva, cpu)) {
10705             min_tsz = 12;
10706         }
10707         ds = false;
10708     } else if (ds) {
10709         switch (mmu_idx) {
10710         case ARMMMUIdx_Stage2:
10711         case ARMMMUIdx_Stage2_S:
10712             if (using16k) {
10713                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10714             } else {
10715                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10716             }
10717             break;
10718         default:
10719             if (using16k) {
10720                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10721             } else {
10722                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10723             }
10724             break;
10725         }
10726         if (ds) {
10727             min_tsz = 12;
10728         }
10729     }
10730 
10731     if (tsz > max_tsz) {
10732         tsz = max_tsz;
10733         tsz_oob = true;
10734     } else if (tsz < min_tsz) {
10735         tsz = min_tsz;
10736         tsz_oob = true;
10737     } else {
10738         tsz_oob = false;
10739     }
10740 
10741     /* Present TBI as a composite with TBID.  */
10742     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10743     if (!data) {
10744         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10745     }
10746     tbi = (tbi >> select) & 1;
10747 
10748     return (ARMVAParameters) {
10749         .tsz = tsz,
10750         .ps = ps,
10751         .sh = sh,
10752         .select = select,
10753         .tbi = tbi,
10754         .epd = epd,
10755         .hpd = hpd,
10756         .using16k = using16k,
10757         .using64k = using64k,
10758         .tsz_oob = tsz_oob,
10759         .ds = ds,
10760     };
10761 }
10762 
10763 /* Note that signed overflow is undefined in C.  The following routines are
10764    careful to use unsigned types where modulo arithmetic is required.
10765    Failure to do so _will_ break on newer gcc.  */
10766 
10767 /* Signed saturating arithmetic.  */
10768 
10769 /* Perform 16-bit signed saturating addition.  */
10770 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10771 {
10772     uint16_t res;
10773 
10774     res = a + b;
10775     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10776         if (a & 0x8000)
10777             res = 0x8000;
10778         else
10779             res = 0x7fff;
10780     }
10781     return res;
10782 }
10783 
10784 /* Perform 8-bit signed saturating addition.  */
10785 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10786 {
10787     uint8_t res;
10788 
10789     res = a + b;
10790     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10791         if (a & 0x80)
10792             res = 0x80;
10793         else
10794             res = 0x7f;
10795     }
10796     return res;
10797 }
10798 
10799 /* Perform 16-bit signed saturating subtraction.  */
10800 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10801 {
10802     uint16_t res;
10803 
10804     res = a - b;
10805     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10806         if (a & 0x8000)
10807             res = 0x8000;
10808         else
10809             res = 0x7fff;
10810     }
10811     return res;
10812 }
10813 
10814 /* Perform 8-bit signed saturating subtraction.  */
10815 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10816 {
10817     uint8_t res;
10818 
10819     res = a - b;
10820     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10821         if (a & 0x80)
10822             res = 0x80;
10823         else
10824             res = 0x7f;
10825     }
10826     return res;
10827 }
10828 
10829 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10830 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10831 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
10832 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
10833 #define PFX q
10834 
10835 #include "op_addsub.h"
10836 
10837 /* Unsigned saturating arithmetic.  */
10838 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10839 {
10840     uint16_t res;
10841     res = a + b;
10842     if (res < a)
10843         res = 0xffff;
10844     return res;
10845 }
10846 
10847 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10848 {
10849     if (a > b)
10850         return a - b;
10851     else
10852         return 0;
10853 }
10854 
10855 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10856 {
10857     uint8_t res;
10858     res = a + b;
10859     if (res < a)
10860         res = 0xff;
10861     return res;
10862 }
10863 
10864 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10865 {
10866     if (a > b)
10867         return a - b;
10868     else
10869         return 0;
10870 }
10871 
10872 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10873 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10874 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
10875 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
10876 #define PFX uq
10877 
10878 #include "op_addsub.h"
10879 
10880 /* Signed modulo arithmetic.  */
10881 #define SARITH16(a, b, n, op) do { \
10882     int32_t sum; \
10883     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10884     RESULT(sum, n, 16); \
10885     if (sum >= 0) \
10886         ge |= 3 << (n * 2); \
10887     } while(0)
10888 
10889 #define SARITH8(a, b, n, op) do { \
10890     int32_t sum; \
10891     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10892     RESULT(sum, n, 8); \
10893     if (sum >= 0) \
10894         ge |= 1 << n; \
10895     } while(0)
10896 
10897 
10898 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10899 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10900 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
10901 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
10902 #define PFX s
10903 #define ARITH_GE
10904 
10905 #include "op_addsub.h"
10906 
10907 /* Unsigned modulo arithmetic.  */
10908 #define ADD16(a, b, n) do { \
10909     uint32_t sum; \
10910     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10911     RESULT(sum, n, 16); \
10912     if ((sum >> 16) == 1) \
10913         ge |= 3 << (n * 2); \
10914     } while(0)
10915 
10916 #define ADD8(a, b, n) do { \
10917     uint32_t sum; \
10918     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10919     RESULT(sum, n, 8); \
10920     if ((sum >> 8) == 1) \
10921         ge |= 1 << n; \
10922     } while(0)
10923 
10924 #define SUB16(a, b, n) do { \
10925     uint32_t sum; \
10926     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10927     RESULT(sum, n, 16); \
10928     if ((sum >> 16) == 0) \
10929         ge |= 3 << (n * 2); \
10930     } while(0)
10931 
10932 #define SUB8(a, b, n) do { \
10933     uint32_t sum; \
10934     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10935     RESULT(sum, n, 8); \
10936     if ((sum >> 8) == 0) \
10937         ge |= 1 << n; \
10938     } while(0)
10939 
10940 #define PFX u
10941 #define ARITH_GE
10942 
10943 #include "op_addsub.h"
10944 
10945 /* Halved signed arithmetic.  */
10946 #define ADD16(a, b, n) \
10947   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10948 #define SUB16(a, b, n) \
10949   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10950 #define ADD8(a, b, n) \
10951   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10952 #define SUB8(a, b, n) \
10953   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10954 #define PFX sh
10955 
10956 #include "op_addsub.h"
10957 
10958 /* Halved unsigned arithmetic.  */
10959 #define ADD16(a, b, n) \
10960   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10961 #define SUB16(a, b, n) \
10962   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10963 #define ADD8(a, b, n) \
10964   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10965 #define SUB8(a, b, n) \
10966   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10967 #define PFX uh
10968 
10969 #include "op_addsub.h"
10970 
10971 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10972 {
10973     if (a > b)
10974         return a - b;
10975     else
10976         return b - a;
10977 }
10978 
10979 /* Unsigned sum of absolute byte differences.  */
10980 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10981 {
10982     uint32_t sum;
10983     sum = do_usad(a, b);
10984     sum += do_usad(a >> 8, b >> 8);
10985     sum += do_usad(a >> 16, b >> 16);
10986     sum += do_usad(a >> 24, b >> 24);
10987     return sum;
10988 }
10989 
10990 /* For ARMv6 SEL instruction.  */
10991 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10992 {
10993     uint32_t mask;
10994 
10995     mask = 0;
10996     if (flags & 1)
10997         mask |= 0xff;
10998     if (flags & 2)
10999         mask |= 0xff00;
11000     if (flags & 4)
11001         mask |= 0xff0000;
11002     if (flags & 8)
11003         mask |= 0xff000000;
11004     return (a & mask) | (b & ~mask);
11005 }
11006 
11007 /* CRC helpers.
11008  * The upper bytes of val (above the number specified by 'bytes') must have
11009  * been zeroed out by the caller.
11010  */
11011 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11012 {
11013     uint8_t buf[4];
11014 
11015     stl_le_p(buf, val);
11016 
11017     /* zlib crc32 converts the accumulator and output to one's complement.  */
11018     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11019 }
11020 
11021 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11022 {
11023     uint8_t buf[4];
11024 
11025     stl_le_p(buf, val);
11026 
11027     /* Linux crc32c converts the output to one's complement.  */
11028     return crc32c(acc, buf, bytes) ^ 0xffffffff;
11029 }
11030 
11031 /* Return the exception level to which FP-disabled exceptions should
11032  * be taken, or 0 if FP is enabled.
11033  */
11034 int fp_exception_el(CPUARMState *env, int cur_el)
11035 {
11036 #ifndef CONFIG_USER_ONLY
11037     uint64_t hcr_el2;
11038 
11039     /* CPACR and the CPTR registers don't exist before v6, so FP is
11040      * always accessible
11041      */
11042     if (!arm_feature(env, ARM_FEATURE_V6)) {
11043         return 0;
11044     }
11045 
11046     if (arm_feature(env, ARM_FEATURE_M)) {
11047         /* CPACR can cause a NOCP UsageFault taken to current security state */
11048         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11049             return 1;
11050         }
11051 
11052         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11053             if (!extract32(env->v7m.nsacr, 10, 1)) {
11054                 /* FP insns cause a NOCP UsageFault taken to Secure */
11055                 return 3;
11056             }
11057         }
11058 
11059         return 0;
11060     }
11061 
11062     hcr_el2 = arm_hcr_el2_eff(env);
11063 
11064     /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11065      * 0, 2 : trap EL0 and EL1/PL1 accesses
11066      * 1    : trap only EL0 accesses
11067      * 3    : trap no accesses
11068      * This register is ignored if E2H+TGE are both set.
11069      */
11070     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11071         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11072 
11073         switch (fpen) {
11074         case 1:
11075             if (cur_el != 0) {
11076                 break;
11077             }
11078             /* fall through */
11079         case 0:
11080         case 2:
11081             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11082             if (!arm_el_is_aa64(env, 3)
11083                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11084                 return 3;
11085             }
11086             if (cur_el <= 1) {
11087                 return 1;
11088             }
11089             break;
11090         }
11091     }
11092 
11093     /*
11094      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11095      * to control non-secure access to the FPU. It doesn't have any
11096      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11097      */
11098     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11099          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11100         if (!extract32(env->cp15.nsacr, 10, 1)) {
11101             /* FP insns act as UNDEF */
11102             return cur_el == 2 ? 2 : 1;
11103         }
11104     }
11105 
11106     /*
11107      * CPTR_EL2 is present in v7VE or v8, and changes format
11108      * with HCR_EL2.E2H (regardless of TGE).
11109      */
11110     if (cur_el <= 2) {
11111         if (hcr_el2 & HCR_E2H) {
11112             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11113             case 1:
11114                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11115                     break;
11116                 }
11117                 /* fall through */
11118             case 0:
11119             case 2:
11120                 return 2;
11121             }
11122         } else if (arm_is_el2_enabled(env)) {
11123             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11124                 return 2;
11125             }
11126         }
11127     }
11128 
11129     /* CPTR_EL3 : present in v8 */
11130     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11131         /* Trap all FP ops to EL3 */
11132         return 3;
11133     }
11134 #endif
11135     return 0;
11136 }
11137 
11138 /* Return the exception level we're running at if this is our mmu_idx */
11139 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11140 {
11141     if (mmu_idx & ARM_MMU_IDX_M) {
11142         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11143     }
11144 
11145     switch (mmu_idx) {
11146     case ARMMMUIdx_E10_0:
11147     case ARMMMUIdx_E20_0:
11148     case ARMMMUIdx_SE10_0:
11149     case ARMMMUIdx_SE20_0:
11150         return 0;
11151     case ARMMMUIdx_E10_1:
11152     case ARMMMUIdx_E10_1_PAN:
11153     case ARMMMUIdx_SE10_1:
11154     case ARMMMUIdx_SE10_1_PAN:
11155         return 1;
11156     case ARMMMUIdx_E2:
11157     case ARMMMUIdx_E20_2:
11158     case ARMMMUIdx_E20_2_PAN:
11159     case ARMMMUIdx_SE2:
11160     case ARMMMUIdx_SE20_2:
11161     case ARMMMUIdx_SE20_2_PAN:
11162         return 2;
11163     case ARMMMUIdx_SE3:
11164         return 3;
11165     default:
11166         g_assert_not_reached();
11167     }
11168 }
11169 
11170 #ifndef CONFIG_TCG
11171 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11172 {
11173     g_assert_not_reached();
11174 }
11175 #endif
11176 
11177 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11178 {
11179     ARMMMUIdx idx;
11180     uint64_t hcr;
11181 
11182     if (arm_feature(env, ARM_FEATURE_M)) {
11183         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11184     }
11185 
11186     /* See ARM pseudo-function ELIsInHost.  */
11187     switch (el) {
11188     case 0:
11189         hcr = arm_hcr_el2_eff(env);
11190         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11191             idx = ARMMMUIdx_E20_0;
11192         } else {
11193             idx = ARMMMUIdx_E10_0;
11194         }
11195         break;
11196     case 1:
11197         if (env->pstate & PSTATE_PAN) {
11198             idx = ARMMMUIdx_E10_1_PAN;
11199         } else {
11200             idx = ARMMMUIdx_E10_1;
11201         }
11202         break;
11203     case 2:
11204         /* Note that TGE does not apply at EL2.  */
11205         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11206             if (env->pstate & PSTATE_PAN) {
11207                 idx = ARMMMUIdx_E20_2_PAN;
11208             } else {
11209                 idx = ARMMMUIdx_E20_2;
11210             }
11211         } else {
11212             idx = ARMMMUIdx_E2;
11213         }
11214         break;
11215     case 3:
11216         return ARMMMUIdx_SE3;
11217     default:
11218         g_assert_not_reached();
11219     }
11220 
11221     if (arm_is_secure_below_el3(env)) {
11222         idx &= ~ARM_MMU_IDX_A_NS;
11223     }
11224 
11225     return idx;
11226 }
11227 
11228 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11229 {
11230     return arm_mmu_idx_el(env, arm_current_el(env));
11231 }
11232 
11233 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
11234                                            ARMMMUIdx mmu_idx,
11235                                            CPUARMTBFlags flags)
11236 {
11237     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
11238     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
11239 
11240     if (arm_singlestep_active(env)) {
11241         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
11242     }
11243     return flags;
11244 }
11245 
11246 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11247                                               ARMMMUIdx mmu_idx,
11248                                               CPUARMTBFlags flags)
11249 {
11250     bool sctlr_b = arm_sctlr_b(env);
11251 
11252     if (sctlr_b) {
11253         DP_TBFLAG_A32(flags, SCTLR__B, 1);
11254     }
11255     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
11256         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11257     }
11258     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
11259 
11260     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11261 }
11262 
11263 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11264                                         ARMMMUIdx mmu_idx)
11265 {
11266     CPUARMTBFlags flags = {};
11267     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11268 
11269     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11270     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11271         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11272     }
11273 
11274     if (arm_v7m_is_handler_mode(env)) {
11275         DP_TBFLAG_M32(flags, HANDLER, 1);
11276     }
11277 
11278     /*
11279      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11280      * is suppressing them because the requested execution priority
11281      * is less than 0.
11282      */
11283     if (arm_feature(env, ARM_FEATURE_V8) &&
11284         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11285           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11286         DP_TBFLAG_M32(flags, STACKCHECK, 1);
11287     }
11288 
11289     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11290 }
11291 
11292 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11293                                         ARMMMUIdx mmu_idx)
11294 {
11295     CPUARMTBFlags flags = {};
11296     int el = arm_current_el(env);
11297 
11298     if (arm_sctlr(env, el) & SCTLR_A) {
11299         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11300     }
11301 
11302     if (arm_el_is_aa64(env, 1)) {
11303         DP_TBFLAG_A32(flags, VFPEN, 1);
11304     }
11305 
11306     if (el < 2 && env->cp15.hstr_el2 &&
11307         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11308         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
11309     }
11310 
11311     if (env->uncached_cpsr & CPSR_IL) {
11312         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11313     }
11314 
11315     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11316 }
11317 
11318 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11319                                         ARMMMUIdx mmu_idx)
11320 {
11321     CPUARMTBFlags flags = {};
11322     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11323     uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11324     uint64_t sctlr;
11325     int tbii, tbid;
11326 
11327     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
11328 
11329     /* Get control bits for tagged addresses.  */
11330     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11331     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
11332 
11333     DP_TBFLAG_A64(flags, TBII, tbii);
11334     DP_TBFLAG_A64(flags, TBID, tbid);
11335 
11336     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11337         int sve_el = sve_exception_el(env, el);
11338 
11339         /*
11340          * If either FP or SVE are disabled, translator does not need len.
11341          * If SVE EL > FP EL, FP exception has precedence, and translator
11342          * does not need SVE EL.  Save potential re-translations by forcing
11343          * the unneeded data to zero.
11344          */
11345         if (fp_el != 0) {
11346             if (sve_el > fp_el) {
11347                 sve_el = 0;
11348             }
11349         } else if (sve_el == 0) {
11350             DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
11351         }
11352         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
11353     }
11354     if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
11355         int sme_el = sme_exception_el(env, el);
11356 
11357         DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11358         if (sme_el == 0) {
11359             /* Similarly, do not compute SVL if SME is disabled. */
11360             DP_TBFLAG_A64(flags, SVL, sve_vqm1_for_el_sm(env, el, true));
11361         }
11362         if (FIELD_EX64(env->svcr, SVCR, SM)) {
11363             DP_TBFLAG_A64(flags, PSTATE_SM, 1);
11364         }
11365         DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
11366     }
11367 
11368     sctlr = regime_sctlr(env, stage1);
11369 
11370     if (sctlr & SCTLR_A) {
11371         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11372     }
11373 
11374     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11375         DP_TBFLAG_ANY(flags, BE_DATA, 1);
11376     }
11377 
11378     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11379         /*
11380          * In order to save space in flags, we record only whether
11381          * pauth is "inactive", meaning all insns are implemented as
11382          * a nop, or "active" when some action must be performed.
11383          * The decision of which action to take is left to a helper.
11384          */
11385         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11386             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11387         }
11388     }
11389 
11390     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11391         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
11392         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11393             DP_TBFLAG_A64(flags, BT, 1);
11394         }
11395     }
11396 
11397     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11398     if (!(env->pstate & PSTATE_UAO)) {
11399         switch (mmu_idx) {
11400         case ARMMMUIdx_E10_1:
11401         case ARMMMUIdx_E10_1_PAN:
11402         case ARMMMUIdx_SE10_1:
11403         case ARMMMUIdx_SE10_1_PAN:
11404             /* TODO: ARMv8.3-NV */
11405             DP_TBFLAG_A64(flags, UNPRIV, 1);
11406             break;
11407         case ARMMMUIdx_E20_2:
11408         case ARMMMUIdx_E20_2_PAN:
11409         case ARMMMUIdx_SE20_2:
11410         case ARMMMUIdx_SE20_2_PAN:
11411             /*
11412              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11413              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11414              */
11415             if (env->cp15.hcr_el2 & HCR_TGE) {
11416                 DP_TBFLAG_A64(flags, UNPRIV, 1);
11417             }
11418             break;
11419         default:
11420             break;
11421         }
11422     }
11423 
11424     if (env->pstate & PSTATE_IL) {
11425         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11426     }
11427 
11428     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11429         /*
11430          * Set MTE_ACTIVE if any access may be Checked, and leave clear
11431          * if all accesses must be Unchecked:
11432          * 1) If no TBI, then there are no tags in the address to check,
11433          * 2) If Tag Check Override, then all accesses are Unchecked,
11434          * 3) If Tag Check Fail == 0, then Checked access have no effect,
11435          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11436          */
11437         if (allocation_tag_access_enabled(env, el, sctlr)) {
11438             DP_TBFLAG_A64(flags, ATA, 1);
11439             if (tbid
11440                 && !(env->pstate & PSTATE_TCO)
11441                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11442                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11443             }
11444         }
11445         /* And again for unprivileged accesses, if required.  */
11446         if (EX_TBFLAG_A64(flags, UNPRIV)
11447             && tbid
11448             && !(env->pstate & PSTATE_TCO)
11449             && (sctlr & SCTLR_TCF0)
11450             && allocation_tag_access_enabled(env, 0, sctlr)) {
11451             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11452         }
11453         /* Cache TCMA as well as TBI. */
11454         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11455     }
11456 
11457     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11458 }
11459 
11460 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11461 {
11462     int el = arm_current_el(env);
11463     int fp_el = fp_exception_el(env, el);
11464     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11465 
11466     if (is_a64(env)) {
11467         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11468     } else if (arm_feature(env, ARM_FEATURE_M)) {
11469         return rebuild_hflags_m32(env, fp_el, mmu_idx);
11470     } else {
11471         return rebuild_hflags_a32(env, fp_el, mmu_idx);
11472     }
11473 }
11474 
11475 void arm_rebuild_hflags(CPUARMState *env)
11476 {
11477     env->hflags = rebuild_hflags_internal(env);
11478 }
11479 
11480 /*
11481  * If we have triggered a EL state change we can't rely on the
11482  * translator having passed it to us, we need to recompute.
11483  */
11484 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11485 {
11486     int el = arm_current_el(env);
11487     int fp_el = fp_exception_el(env, el);
11488     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11489 
11490     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11491 }
11492 
11493 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11494 {
11495     int fp_el = fp_exception_el(env, el);
11496     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11497 
11498     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11499 }
11500 
11501 /*
11502  * If we have triggered a EL state change we can't rely on the
11503  * translator having passed it to us, we need to recompute.
11504  */
11505 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11506 {
11507     int el = arm_current_el(env);
11508     int fp_el = fp_exception_el(env, el);
11509     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11510     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11511 }
11512 
11513 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11514 {
11515     int fp_el = fp_exception_el(env, el);
11516     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11517 
11518     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11519 }
11520 
11521 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11522 {
11523     int fp_el = fp_exception_el(env, el);
11524     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11525 
11526     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11527 }
11528 
11529 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11530 {
11531 #ifdef CONFIG_DEBUG_TCG
11532     CPUARMTBFlags c = env->hflags;
11533     CPUARMTBFlags r = rebuild_hflags_internal(env);
11534 
11535     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11536         fprintf(stderr, "TCG hflags mismatch "
11537                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11538                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11539                 c.flags, c.flags2, r.flags, r.flags2);
11540         abort();
11541     }
11542 #endif
11543 }
11544 
11545 static bool mve_no_pred(CPUARMState *env)
11546 {
11547     /*
11548      * Return true if there is definitely no predication of MVE
11549      * instructions by VPR or LTPSIZE. (Returning false even if there
11550      * isn't any predication is OK; generated code will just be
11551      * a little worse.)
11552      * If the CPU does not implement MVE then this TB flag is always 0.
11553      *
11554      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11555      * logic in gen_update_fp_context() needs to be updated to match.
11556      *
11557      * We do not include the effect of the ECI bits here -- they are
11558      * tracked in other TB flags. This simplifies the logic for
11559      * "when did we emit code that changes the MVE_NO_PRED TB flag
11560      * and thus need to end the TB?".
11561      */
11562     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11563         return false;
11564     }
11565     if (env->v7m.vpr) {
11566         return false;
11567     }
11568     if (env->v7m.ltpsize < 4) {
11569         return false;
11570     }
11571     return true;
11572 }
11573 
11574 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11575                           target_ulong *cs_base, uint32_t *pflags)
11576 {
11577     CPUARMTBFlags flags;
11578 
11579     assert_hflags_rebuild_correctly(env);
11580     flags = env->hflags;
11581 
11582     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11583         *pc = env->pc;
11584         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11585             DP_TBFLAG_A64(flags, BTYPE, env->btype);
11586         }
11587     } else {
11588         *pc = env->regs[15];
11589 
11590         if (arm_feature(env, ARM_FEATURE_M)) {
11591             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11592                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11593                 != env->v7m.secure) {
11594                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11595             }
11596 
11597             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11598                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11599                  (env->v7m.secure &&
11600                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11601                 /*
11602                  * ASPEN is set, but FPCA/SFPA indicate that there is no
11603                  * active FP context; we must create a new FP context before
11604                  * executing any FP insn.
11605                  */
11606                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11607             }
11608 
11609             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11610             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11611                 DP_TBFLAG_M32(flags, LSPACT, 1);
11612             }
11613 
11614             if (mve_no_pred(env)) {
11615                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11616             }
11617         } else {
11618             /*
11619              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11620              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11621              */
11622             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11623                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11624             } else {
11625                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11626                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11627             }
11628             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11629                 DP_TBFLAG_A32(flags, VFPEN, 1);
11630             }
11631         }
11632 
11633         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11634         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11635     }
11636 
11637     /*
11638      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11639      * states defined in the ARM ARM for software singlestep:
11640      *  SS_ACTIVE   PSTATE.SS   State
11641      *     0            x       Inactive (the TB flag for SS is always 0)
11642      *     1            0       Active-pending
11643      *     1            1       Active-not-pending
11644      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11645      */
11646     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11647         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11648     }
11649 
11650     *pflags = flags.flags;
11651     *cs_base = flags.flags2;
11652 }
11653 
11654 #ifdef TARGET_AARCH64
11655 /*
11656  * The manual says that when SVE is enabled and VQ is widened the
11657  * implementation is allowed to zero the previously inaccessible
11658  * portion of the registers.  The corollary to that is that when
11659  * SVE is enabled and VQ is narrowed we are also allowed to zero
11660  * the now inaccessible portion of the registers.
11661  *
11662  * The intent of this is that no predicate bit beyond VQ is ever set.
11663  * Which means that some operations on predicate registers themselves
11664  * may operate on full uint64_t or even unrolled across the maximum
11665  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11666  * may well be cheaper than conditionals to restrict the operation
11667  * to the relevant portion of a uint16_t[16].
11668  */
11669 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11670 {
11671     int i, j;
11672     uint64_t pmask;
11673 
11674     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11675     assert(vq <= env_archcpu(env)->sve_max_vq);
11676 
11677     /* Zap the high bits of the zregs.  */
11678     for (i = 0; i < 32; i++) {
11679         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11680     }
11681 
11682     /* Zap the high bits of the pregs and ffr.  */
11683     pmask = 0;
11684     if (vq & 3) {
11685         pmask = ~(-1ULL << (16 * (vq & 3)));
11686     }
11687     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11688         for (i = 0; i < 17; ++i) {
11689             env->vfp.pregs[i].p[j] &= pmask;
11690         }
11691         pmask = 0;
11692     }
11693 }
11694 
11695 /*
11696  * Notice a change in SVE vector size when changing EL.
11697  */
11698 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11699                            int new_el, bool el0_a64)
11700 {
11701     ARMCPU *cpu = env_archcpu(env);
11702     int old_len, new_len;
11703     bool old_a64, new_a64;
11704 
11705     /* Nothing to do if no SVE.  */
11706     if (!cpu_isar_feature(aa64_sve, cpu)) {
11707         return;
11708     }
11709 
11710     /* Nothing to do if FP is disabled in either EL.  */
11711     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11712         return;
11713     }
11714 
11715     /*
11716      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11717      * at ELx, or not available because the EL is in AArch32 state, then
11718      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11719      * has an effective value of 0".
11720      *
11721      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11722      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11723      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11724      * we already have the correct register contents when encountering the
11725      * vq0->vq0 transition between EL0->EL1.
11726      */
11727     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11728     old_len = (old_a64 && !sve_exception_el(env, old_el)
11729                ? sve_vqm1_for_el(env, old_el) : 0);
11730     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11731     new_len = (new_a64 && !sve_exception_el(env, new_el)
11732                ? sve_vqm1_for_el(env, new_el) : 0);
11733 
11734     /* When changing vector length, clear inaccessible state.  */
11735     if (new_len < old_len) {
11736         aarch64_sve_narrow_vq(env, new_len + 1);
11737     }
11738 }
11739 #endif
11740