xref: /openbmc/qemu/target/arm/helper.c (revision 0af312b6edd231e1c8d0dec12494a80bc39ac761)
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 "target/arm/idau.h"
13 #include "trace.h"
14 #include "cpu.h"
15 #include "internals.h"
16 #include "exec/helper-proto.h"
17 #include "qemu/host-utils.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/timer.h"
20 #include "qemu/bitops.h"
21 #include "qemu/crc32c.h"
22 #include "qemu/qemu-print.h"
23 #include "exec/exec-all.h"
24 #include <zlib.h> /* For crc32 */
25 #include "hw/irq.h"
26 #include "semihosting/semihost.h"
27 #include "sysemu/cpus.h"
28 #include "sysemu/cpu-timers.h"
29 #include "sysemu/kvm.h"
30 #include "sysemu/tcg.h"
31 #include "qemu/range.h"
32 #include "qapi/qapi-commands-machine-target.h"
33 #include "qapi/error.h"
34 #include "qemu/guest-random.h"
35 #ifdef CONFIG_TCG
36 #include "arm_ldst.h"
37 #include "exec/cpu_ldst.h"
38 #include "semihosting/common-semi.h"
39 #endif
40 
41 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
42 #define PMCR_NUM_COUNTERS 4 /* QEMU IMPDEF choice */
43 
44 #ifndef CONFIG_USER_ONLY
45 
46 static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
47                                MMUAccessType access_type, ARMMMUIdx mmu_idx,
48                                bool s1_is_el0,
49                                hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
50                                target_ulong *page_size_ptr,
51                                ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
52     __attribute__((nonnull));
53 #endif
54 
55 static void switch_mode(CPUARMState *env, int mode);
56 static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx);
57 
58 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
59 {
60     assert(ri->fieldoffset);
61     if (cpreg_field_is_64bit(ri)) {
62         return CPREG_FIELD64(env, ri);
63     } else {
64         return CPREG_FIELD32(env, ri);
65     }
66 }
67 
68 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
69                       uint64_t value)
70 {
71     assert(ri->fieldoffset);
72     if (cpreg_field_is_64bit(ri)) {
73         CPREG_FIELD64(env, ri) = value;
74     } else {
75         CPREG_FIELD32(env, ri) = value;
76     }
77 }
78 
79 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
80 {
81     return (char *)env + ri->fieldoffset;
82 }
83 
84 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
85 {
86     /* Raw read of a coprocessor register (as needed for migration, etc). */
87     if (ri->type & ARM_CP_CONST) {
88         return ri->resetvalue;
89     } else if (ri->raw_readfn) {
90         return ri->raw_readfn(env, ri);
91     } else if (ri->readfn) {
92         return ri->readfn(env, ri);
93     } else {
94         return raw_read(env, ri);
95     }
96 }
97 
98 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
99                              uint64_t v)
100 {
101     /* Raw write of a coprocessor register (as needed for migration, etc).
102      * Note that constant registers are treated as write-ignored; the
103      * caller should check for success by whether a readback gives the
104      * value written.
105      */
106     if (ri->type & ARM_CP_CONST) {
107         return;
108     } else if (ri->raw_writefn) {
109         ri->raw_writefn(env, ri, v);
110     } else if (ri->writefn) {
111         ri->writefn(env, ri, v);
112     } else {
113         raw_write(env, ri, v);
114     }
115 }
116 
117 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
118 {
119    /* Return true if the regdef would cause an assertion if you called
120     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
121     * program bug for it not to have the NO_RAW flag).
122     * NB that returning false here doesn't necessarily mean that calling
123     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
124     * read/write access functions which are safe for raw use" from "has
125     * read/write access functions which have side effects but has forgotten
126     * to provide raw access functions".
127     * The tests here line up with the conditions in read/write_raw_cp_reg()
128     * and assertions in raw_read()/raw_write().
129     */
130     if ((ri->type & ARM_CP_CONST) ||
131         ri->fieldoffset ||
132         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
133         return false;
134     }
135     return true;
136 }
137 
138 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
139 {
140     /* Write the coprocessor state from cpu->env to the (index,value) list. */
141     int i;
142     bool ok = true;
143 
144     for (i = 0; i < cpu->cpreg_array_len; i++) {
145         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
146         const ARMCPRegInfo *ri;
147         uint64_t newval;
148 
149         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
150         if (!ri) {
151             ok = false;
152             continue;
153         }
154         if (ri->type & ARM_CP_NO_RAW) {
155             continue;
156         }
157 
158         newval = read_raw_cp_reg(&cpu->env, ri);
159         if (kvm_sync) {
160             /*
161              * Only sync if the previous list->cpustate sync succeeded.
162              * Rather than tracking the success/failure state for every
163              * item in the list, we just recheck "does the raw write we must
164              * have made in write_list_to_cpustate() read back OK" here.
165              */
166             uint64_t oldval = cpu->cpreg_values[i];
167 
168             if (oldval == newval) {
169                 continue;
170             }
171 
172             write_raw_cp_reg(&cpu->env, ri, oldval);
173             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
174                 continue;
175             }
176 
177             write_raw_cp_reg(&cpu->env, ri, newval);
178         }
179         cpu->cpreg_values[i] = newval;
180     }
181     return ok;
182 }
183 
184 bool write_list_to_cpustate(ARMCPU *cpu)
185 {
186     int i;
187     bool ok = true;
188 
189     for (i = 0; i < cpu->cpreg_array_len; i++) {
190         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
191         uint64_t v = cpu->cpreg_values[i];
192         const ARMCPRegInfo *ri;
193 
194         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
195         if (!ri) {
196             ok = false;
197             continue;
198         }
199         if (ri->type & ARM_CP_NO_RAW) {
200             continue;
201         }
202         /* Write value and confirm it reads back as written
203          * (to catch read-only registers and partially read-only
204          * registers where the incoming migration value doesn't match)
205          */
206         write_raw_cp_reg(&cpu->env, ri, v);
207         if (read_raw_cp_reg(&cpu->env, ri) != v) {
208             ok = false;
209         }
210     }
211     return ok;
212 }
213 
214 static void add_cpreg_to_list(gpointer key, gpointer opaque)
215 {
216     ARMCPU *cpu = opaque;
217     uint64_t regidx;
218     const ARMCPRegInfo *ri;
219 
220     regidx = *(uint32_t *)key;
221     ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
222 
223     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
224         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
225         /* The value array need not be initialized at this point */
226         cpu->cpreg_array_len++;
227     }
228 }
229 
230 static void count_cpreg(gpointer key, gpointer opaque)
231 {
232     ARMCPU *cpu = opaque;
233     uint64_t regidx;
234     const ARMCPRegInfo *ri;
235 
236     regidx = *(uint32_t *)key;
237     ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
238 
239     if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
240         cpu->cpreg_array_len++;
241     }
242 }
243 
244 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
245 {
246     uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
247     uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
248 
249     if (aidx > bidx) {
250         return 1;
251     }
252     if (aidx < bidx) {
253         return -1;
254     }
255     return 0;
256 }
257 
258 void init_cpreg_list(ARMCPU *cpu)
259 {
260     /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
261      * Note that we require cpreg_tuples[] to be sorted by key ID.
262      */
263     GList *keys;
264     int arraylen;
265 
266     keys = g_hash_table_get_keys(cpu->cp_regs);
267     keys = g_list_sort(keys, cpreg_key_compare);
268 
269     cpu->cpreg_array_len = 0;
270 
271     g_list_foreach(keys, count_cpreg, cpu);
272 
273     arraylen = cpu->cpreg_array_len;
274     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
275     cpu->cpreg_values = g_new(uint64_t, arraylen);
276     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
277     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
278     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
279     cpu->cpreg_array_len = 0;
280 
281     g_list_foreach(keys, add_cpreg_to_list, cpu);
282 
283     assert(cpu->cpreg_array_len == arraylen);
284 
285     g_list_free(keys);
286 }
287 
288 /*
289  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
290  */
291 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
292                                         const ARMCPRegInfo *ri,
293                                         bool isread)
294 {
295     if (!is_a64(env) && arm_current_el(env) == 3 &&
296         arm_is_secure_below_el3(env)) {
297         return CP_ACCESS_TRAP_UNCATEGORIZED;
298     }
299     return CP_ACCESS_OK;
300 }
301 
302 /* Some secure-only AArch32 registers trap to EL3 if used from
303  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
304  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
305  * We assume that the .access field is set to PL1_RW.
306  */
307 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
308                                             const ARMCPRegInfo *ri,
309                                             bool isread)
310 {
311     if (arm_current_el(env) == 3) {
312         return CP_ACCESS_OK;
313     }
314     if (arm_is_secure_below_el3(env)) {
315         if (env->cp15.scr_el3 & SCR_EEL2) {
316             return CP_ACCESS_TRAP_EL2;
317         }
318         return CP_ACCESS_TRAP_EL3;
319     }
320     /* This will be EL1 NS and EL2 NS, which just UNDEF */
321     return CP_ACCESS_TRAP_UNCATEGORIZED;
322 }
323 
324 static uint64_t arm_mdcr_el2_eff(CPUARMState *env)
325 {
326     return arm_is_el2_enabled(env) ? env->cp15.mdcr_el2 : 0;
327 }
328 
329 /* Check for traps to "powerdown debug" registers, which are controlled
330  * by MDCR.TDOSA
331  */
332 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
333                                    bool isread)
334 {
335     int el = arm_current_el(env);
336     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
337     bool mdcr_el2_tdosa = (mdcr_el2 & MDCR_TDOSA) || (mdcr_el2 & MDCR_TDE) ||
338         (arm_hcr_el2_eff(env) & HCR_TGE);
339 
340     if (el < 2 && mdcr_el2_tdosa) {
341         return CP_ACCESS_TRAP_EL2;
342     }
343     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
344         return CP_ACCESS_TRAP_EL3;
345     }
346     return CP_ACCESS_OK;
347 }
348 
349 /* Check for traps to "debug ROM" registers, which are controlled
350  * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
351  */
352 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
353                                   bool isread)
354 {
355     int el = arm_current_el(env);
356     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
357     bool mdcr_el2_tdra = (mdcr_el2 & MDCR_TDRA) || (mdcr_el2 & MDCR_TDE) ||
358         (arm_hcr_el2_eff(env) & HCR_TGE);
359 
360     if (el < 2 && mdcr_el2_tdra) {
361         return CP_ACCESS_TRAP_EL2;
362     }
363     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
364         return CP_ACCESS_TRAP_EL3;
365     }
366     return CP_ACCESS_OK;
367 }
368 
369 /* Check for traps to general debug registers, which are controlled
370  * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
371  */
372 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
373                                   bool isread)
374 {
375     int el = arm_current_el(env);
376     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
377     bool mdcr_el2_tda = (mdcr_el2 & MDCR_TDA) || (mdcr_el2 & MDCR_TDE) ||
378         (arm_hcr_el2_eff(env) & HCR_TGE);
379 
380     if (el < 2 && mdcr_el2_tda) {
381         return CP_ACCESS_TRAP_EL2;
382     }
383     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
384         return CP_ACCESS_TRAP_EL3;
385     }
386     return CP_ACCESS_OK;
387 }
388 
389 /* Check for traps to performance monitor registers, which are controlled
390  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
391  */
392 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
393                                  bool isread)
394 {
395     int el = arm_current_el(env);
396     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
397 
398     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
399         return CP_ACCESS_TRAP_EL2;
400     }
401     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
402         return CP_ACCESS_TRAP_EL3;
403     }
404     return CP_ACCESS_OK;
405 }
406 
407 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
408 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
409                                       bool isread)
410 {
411     if (arm_current_el(env) == 1) {
412         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
413         if (arm_hcr_el2_eff(env) & trap) {
414             return CP_ACCESS_TRAP_EL2;
415         }
416     }
417     return CP_ACCESS_OK;
418 }
419 
420 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
421 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
422                                  bool isread)
423 {
424     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
425         return CP_ACCESS_TRAP_EL2;
426     }
427     return CP_ACCESS_OK;
428 }
429 
430 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
431 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
432                                   bool isread)
433 {
434     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
435         return CP_ACCESS_TRAP_EL2;
436     }
437     return CP_ACCESS_OK;
438 }
439 
440 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
441 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
442                                   bool isread)
443 {
444     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
445         return CP_ACCESS_TRAP_EL2;
446     }
447     return CP_ACCESS_OK;
448 }
449 
450 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
451 {
452     ARMCPU *cpu = env_archcpu(env);
453 
454     raw_write(env, ri, value);
455     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
456 }
457 
458 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
459 {
460     ARMCPU *cpu = env_archcpu(env);
461 
462     if (raw_read(env, ri) != value) {
463         /* Unlike real hardware the qemu TLB uses virtual addresses,
464          * not modified virtual addresses, so this causes a TLB flush.
465          */
466         tlb_flush(CPU(cpu));
467         raw_write(env, ri, value);
468     }
469 }
470 
471 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
472                              uint64_t value)
473 {
474     ARMCPU *cpu = env_archcpu(env);
475 
476     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
477         && !extended_addresses_enabled(env)) {
478         /* For VMSA (when not using the LPAE long descriptor page table
479          * format) this register includes the ASID, so do a TLB flush.
480          * For PMSA it is purely a process ID and no action is needed.
481          */
482         tlb_flush(CPU(cpu));
483     }
484     raw_write(env, ri, value);
485 }
486 
487 /* IS variants of TLB operations must affect all cores */
488 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
489                              uint64_t value)
490 {
491     CPUState *cs = env_cpu(env);
492 
493     tlb_flush_all_cpus_synced(cs);
494 }
495 
496 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
497                              uint64_t value)
498 {
499     CPUState *cs = env_cpu(env);
500 
501     tlb_flush_all_cpus_synced(cs);
502 }
503 
504 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
505                              uint64_t value)
506 {
507     CPUState *cs = env_cpu(env);
508 
509     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
510 }
511 
512 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
513                              uint64_t value)
514 {
515     CPUState *cs = env_cpu(env);
516 
517     tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
518 }
519 
520 /*
521  * Non-IS variants of TLB operations are upgraded to
522  * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
523  * force broadcast of these operations.
524  */
525 static bool tlb_force_broadcast(CPUARMState *env)
526 {
527     return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
528 }
529 
530 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
531                           uint64_t value)
532 {
533     /* Invalidate all (TLBIALL) */
534     CPUState *cs = env_cpu(env);
535 
536     if (tlb_force_broadcast(env)) {
537         tlb_flush_all_cpus_synced(cs);
538     } else {
539         tlb_flush(cs);
540     }
541 }
542 
543 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
544                           uint64_t value)
545 {
546     /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
547     CPUState *cs = env_cpu(env);
548 
549     value &= TARGET_PAGE_MASK;
550     if (tlb_force_broadcast(env)) {
551         tlb_flush_page_all_cpus_synced(cs, value);
552     } else {
553         tlb_flush_page(cs, value);
554     }
555 }
556 
557 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
558                            uint64_t value)
559 {
560     /* Invalidate by ASID (TLBIASID) */
561     CPUState *cs = env_cpu(env);
562 
563     if (tlb_force_broadcast(env)) {
564         tlb_flush_all_cpus_synced(cs);
565     } else {
566         tlb_flush(cs);
567     }
568 }
569 
570 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
571                            uint64_t value)
572 {
573     /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
574     CPUState *cs = env_cpu(env);
575 
576     value &= TARGET_PAGE_MASK;
577     if (tlb_force_broadcast(env)) {
578         tlb_flush_page_all_cpus_synced(cs, value);
579     } else {
580         tlb_flush_page(cs, value);
581     }
582 }
583 
584 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
585                                uint64_t value)
586 {
587     CPUState *cs = env_cpu(env);
588 
589     tlb_flush_by_mmuidx(cs,
590                         ARMMMUIdxBit_E10_1 |
591                         ARMMMUIdxBit_E10_1_PAN |
592                         ARMMMUIdxBit_E10_0);
593 }
594 
595 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
596                                   uint64_t value)
597 {
598     CPUState *cs = env_cpu(env);
599 
600     tlb_flush_by_mmuidx_all_cpus_synced(cs,
601                                         ARMMMUIdxBit_E10_1 |
602                                         ARMMMUIdxBit_E10_1_PAN |
603                                         ARMMMUIdxBit_E10_0);
604 }
605 
606 
607 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
608                               uint64_t value)
609 {
610     CPUState *cs = env_cpu(env);
611 
612     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
613 }
614 
615 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
616                                  uint64_t value)
617 {
618     CPUState *cs = env_cpu(env);
619 
620     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
621 }
622 
623 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
624                               uint64_t value)
625 {
626     CPUState *cs = env_cpu(env);
627     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
628 
629     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
630 }
631 
632 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
633                                  uint64_t value)
634 {
635     CPUState *cs = env_cpu(env);
636     uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
637 
638     tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
639                                              ARMMMUIdxBit_E2);
640 }
641 
642 static const ARMCPRegInfo cp_reginfo[] = {
643     /* Define the secure and non-secure FCSE identifier CP registers
644      * separately because there is no secure bank in V8 (no _EL3).  This allows
645      * the secure register to be properly reset and migrated. There is also no
646      * v8 EL1 version of the register so the non-secure instance stands alone.
647      */
648     { .name = "FCSEIDR",
649       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
650       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
651       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
652       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
653     { .name = "FCSEIDR_S",
654       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
655       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
656       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
657       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
658     /* Define the secure and non-secure context identifier CP registers
659      * separately because there is no secure bank in V8 (no _EL3).  This allows
660      * the secure register to be properly reset and migrated.  In the
661      * non-secure case, the 32-bit register will have reset and migration
662      * disabled during registration as it is handled by the 64-bit instance.
663      */
664     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
665       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
666       .access = PL1_RW, .accessfn = access_tvm_trvm,
667       .secure = ARM_CP_SECSTATE_NS,
668       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
669       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
670     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
671       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
672       .access = PL1_RW, .accessfn = access_tvm_trvm,
673       .secure = ARM_CP_SECSTATE_S,
674       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
675       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
676     REGINFO_SENTINEL
677 };
678 
679 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
680     /* NB: Some of these registers exist in v8 but with more precise
681      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
682      */
683     /* MMU Domain access control / MPU write buffer control */
684     { .name = "DACR",
685       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
686       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
687       .writefn = dacr_write, .raw_writefn = raw_write,
688       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
689                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
690     /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
691      * For v6 and v5, these mappings are overly broad.
692      */
693     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
694       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
695     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
696       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
697     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
698       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
699     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
700       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
701     /* Cache maintenance ops; some of this space may be overridden later. */
702     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
703       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
704       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
705     REGINFO_SENTINEL
706 };
707 
708 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
709     /* Not all pre-v6 cores implemented this WFI, so this is slightly
710      * over-broad.
711      */
712     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
713       .access = PL1_W, .type = ARM_CP_WFI },
714     REGINFO_SENTINEL
715 };
716 
717 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
718     /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
719      * is UNPREDICTABLE; we choose to NOP as most implementations do).
720      */
721     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
722       .access = PL1_W, .type = ARM_CP_WFI },
723     /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
724      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
725      * OMAPCP will override this space.
726      */
727     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
728       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
729       .resetvalue = 0 },
730     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
731       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
732       .resetvalue = 0 },
733     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
734     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
735       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
736       .resetvalue = 0 },
737     /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
738      * implementing it as RAZ means the "debug architecture version" bits
739      * will read as a reserved value, which should cause Linux to not try
740      * to use the debug hardware.
741      */
742     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
743       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
744     /* MMU TLB control. Note that the wildcarding means we cover not just
745      * the unified TLB ops but also the dside/iside/inner-shareable variants.
746      */
747     { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
748       .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
749       .type = ARM_CP_NO_RAW },
750     { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
751       .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
752       .type = ARM_CP_NO_RAW },
753     { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
754       .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
755       .type = ARM_CP_NO_RAW },
756     { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
757       .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
758       .type = ARM_CP_NO_RAW },
759     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
760       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
761     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
762       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
763     REGINFO_SENTINEL
764 };
765 
766 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
767                         uint64_t value)
768 {
769     uint32_t mask = 0;
770 
771     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
772     if (!arm_feature(env, ARM_FEATURE_V8)) {
773         /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
774          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
775          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
776          */
777         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
778             /* VFP coprocessor: cp10 & cp11 [23:20] */
779             mask |= (1 << 31) | (1 << 30) | (0xf << 20);
780 
781             if (!arm_feature(env, ARM_FEATURE_NEON)) {
782                 /* ASEDIS [31] bit is RAO/WI */
783                 value |= (1 << 31);
784             }
785 
786             /* VFPv3 and upwards with NEON implement 32 double precision
787              * registers (D0-D31).
788              */
789             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
790                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
791                 value |= (1 << 30);
792             }
793         }
794         value &= mask;
795     }
796 
797     /*
798      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
799      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
800      */
801     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
802         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
803         value &= ~(0xf << 20);
804         value |= env->cp15.cpacr_el1 & (0xf << 20);
805     }
806 
807     env->cp15.cpacr_el1 = value;
808 }
809 
810 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
811 {
812     /*
813      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
814      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
815      */
816     uint64_t value = env->cp15.cpacr_el1;
817 
818     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
819         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
820         value &= ~(0xf << 20);
821     }
822     return value;
823 }
824 
825 
826 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
827 {
828     /* Call cpacr_write() so that we reset with the correct RAO bits set
829      * for our CPU features.
830      */
831     cpacr_write(env, ri, 0);
832 }
833 
834 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
835                                    bool isread)
836 {
837     if (arm_feature(env, ARM_FEATURE_V8)) {
838         /* Check if CPACR accesses are to be trapped to EL2 */
839         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
840             (env->cp15.cptr_el[2] & CPTR_TCPAC)) {
841             return CP_ACCESS_TRAP_EL2;
842         /* Check if CPACR accesses are to be trapped to EL3 */
843         } else if (arm_current_el(env) < 3 &&
844                    (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
845             return CP_ACCESS_TRAP_EL3;
846         }
847     }
848 
849     return CP_ACCESS_OK;
850 }
851 
852 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
853                                   bool isread)
854 {
855     /* Check if CPTR accesses are set to trap to EL3 */
856     if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
857         return CP_ACCESS_TRAP_EL3;
858     }
859 
860     return CP_ACCESS_OK;
861 }
862 
863 static const ARMCPRegInfo v6_cp_reginfo[] = {
864     /* prefetch by MVA in v6, NOP in v7 */
865     { .name = "MVA_prefetch",
866       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
867       .access = PL1_W, .type = ARM_CP_NOP },
868     /* We need to break the TB after ISB to execute self-modifying code
869      * correctly and also to take any pending interrupts immediately.
870      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
871      */
872     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
873       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
874     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
875       .access = PL0_W, .type = ARM_CP_NOP },
876     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
877       .access = PL0_W, .type = ARM_CP_NOP },
878     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
879       .access = PL1_RW, .accessfn = access_tvm_trvm,
880       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
881                              offsetof(CPUARMState, cp15.ifar_ns) },
882       .resetvalue = 0, },
883     /* Watchpoint Fault Address Register : should actually only be present
884      * for 1136, 1176, 11MPCore.
885      */
886     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
887       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
888     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
889       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
890       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
891       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
892     REGINFO_SENTINEL
893 };
894 
895 typedef struct pm_event {
896     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
897     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
898     bool (*supported)(CPUARMState *);
899     /*
900      * Retrieve the current count of the underlying event. The programmed
901      * counters hold a difference from the return value from this function
902      */
903     uint64_t (*get_count)(CPUARMState *);
904     /*
905      * Return how many nanoseconds it will take (at a minimum) for count events
906      * to occur. A negative value indicates the counter will never overflow, or
907      * that the counter has otherwise arranged for the overflow bit to be set
908      * and the PMU interrupt to be raised on overflow.
909      */
910     int64_t (*ns_per_count)(uint64_t);
911 } pm_event;
912 
913 static bool event_always_supported(CPUARMState *env)
914 {
915     return true;
916 }
917 
918 static uint64_t swinc_get_count(CPUARMState *env)
919 {
920     /*
921      * SW_INCR events are written directly to the pmevcntr's by writes to
922      * PMSWINC, so there is no underlying count maintained by the PMU itself
923      */
924     return 0;
925 }
926 
927 static int64_t swinc_ns_per(uint64_t ignored)
928 {
929     return -1;
930 }
931 
932 /*
933  * Return the underlying cycle count for the PMU cycle counters. If we're in
934  * usermode, simply return 0.
935  */
936 static uint64_t cycles_get_count(CPUARMState *env)
937 {
938 #ifndef CONFIG_USER_ONLY
939     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
940                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
941 #else
942     return cpu_get_host_ticks();
943 #endif
944 }
945 
946 #ifndef CONFIG_USER_ONLY
947 static int64_t cycles_ns_per(uint64_t cycles)
948 {
949     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
950 }
951 
952 static bool instructions_supported(CPUARMState *env)
953 {
954     return icount_enabled() == 1; /* Precise instruction counting */
955 }
956 
957 static uint64_t instructions_get_count(CPUARMState *env)
958 {
959     return (uint64_t)icount_get_raw();
960 }
961 
962 static int64_t instructions_ns_per(uint64_t icount)
963 {
964     return icount_to_ns((int64_t)icount);
965 }
966 #endif
967 
968 static bool pmu_8_1_events_supported(CPUARMState *env)
969 {
970     /* For events which are supported in any v8.1 PMU */
971     return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
972 }
973 
974 static bool pmu_8_4_events_supported(CPUARMState *env)
975 {
976     /* For events which are supported in any v8.1 PMU */
977     return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
978 }
979 
980 static uint64_t zero_event_get_count(CPUARMState *env)
981 {
982     /* For events which on QEMU never fire, so their count is always zero */
983     return 0;
984 }
985 
986 static int64_t zero_event_ns_per(uint64_t cycles)
987 {
988     /* An event which never fires can never overflow */
989     return -1;
990 }
991 
992 static const pm_event pm_events[] = {
993     { .number = 0x000, /* SW_INCR */
994       .supported = event_always_supported,
995       .get_count = swinc_get_count,
996       .ns_per_count = swinc_ns_per,
997     },
998 #ifndef CONFIG_USER_ONLY
999     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1000       .supported = instructions_supported,
1001       .get_count = instructions_get_count,
1002       .ns_per_count = instructions_ns_per,
1003     },
1004     { .number = 0x011, /* CPU_CYCLES, Cycle */
1005       .supported = event_always_supported,
1006       .get_count = cycles_get_count,
1007       .ns_per_count = cycles_ns_per,
1008     },
1009 #endif
1010     { .number = 0x023, /* STALL_FRONTEND */
1011       .supported = pmu_8_1_events_supported,
1012       .get_count = zero_event_get_count,
1013       .ns_per_count = zero_event_ns_per,
1014     },
1015     { .number = 0x024, /* STALL_BACKEND */
1016       .supported = pmu_8_1_events_supported,
1017       .get_count = zero_event_get_count,
1018       .ns_per_count = zero_event_ns_per,
1019     },
1020     { .number = 0x03c, /* STALL */
1021       .supported = pmu_8_4_events_supported,
1022       .get_count = zero_event_get_count,
1023       .ns_per_count = zero_event_ns_per,
1024     },
1025 };
1026 
1027 /*
1028  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1029  * events (i.e. the statistical profiling extension), this implementation
1030  * should first be updated to something sparse instead of the current
1031  * supported_event_map[] array.
1032  */
1033 #define MAX_EVENT_ID 0x3c
1034 #define UNSUPPORTED_EVENT UINT16_MAX
1035 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1036 
1037 /*
1038  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1039  * of ARM event numbers to indices in our pm_events array.
1040  *
1041  * Note: Events in the 0x40XX range are not currently supported.
1042  */
1043 void pmu_init(ARMCPU *cpu)
1044 {
1045     unsigned int i;
1046 
1047     /*
1048      * Empty supported_event_map and cpu->pmceid[01] before adding supported
1049      * events to them
1050      */
1051     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1052         supported_event_map[i] = UNSUPPORTED_EVENT;
1053     }
1054     cpu->pmceid0 = 0;
1055     cpu->pmceid1 = 0;
1056 
1057     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1058         const pm_event *cnt = &pm_events[i];
1059         assert(cnt->number <= MAX_EVENT_ID);
1060         /* We do not currently support events in the 0x40xx range */
1061         assert(cnt->number <= 0x3f);
1062 
1063         if (cnt->supported(&cpu->env)) {
1064             supported_event_map[cnt->number] = i;
1065             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1066             if (cnt->number & 0x20) {
1067                 cpu->pmceid1 |= event_mask;
1068             } else {
1069                 cpu->pmceid0 |= event_mask;
1070             }
1071         }
1072     }
1073 }
1074 
1075 /*
1076  * Check at runtime whether a PMU event is supported for the current machine
1077  */
1078 static bool event_supported(uint16_t number)
1079 {
1080     if (number > MAX_EVENT_ID) {
1081         return false;
1082     }
1083     return supported_event_map[number] != UNSUPPORTED_EVENT;
1084 }
1085 
1086 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1087                                    bool isread)
1088 {
1089     /* Performance monitor registers user accessibility is controlled
1090      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1091      * trapping to EL2 or EL3 for other accesses.
1092      */
1093     int el = arm_current_el(env);
1094     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1095 
1096     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1097         return CP_ACCESS_TRAP;
1098     }
1099     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1100         return CP_ACCESS_TRAP_EL2;
1101     }
1102     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1103         return CP_ACCESS_TRAP_EL3;
1104     }
1105 
1106     return CP_ACCESS_OK;
1107 }
1108 
1109 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1110                                            const ARMCPRegInfo *ri,
1111                                            bool isread)
1112 {
1113     /* ER: event counter read trap control */
1114     if (arm_feature(env, ARM_FEATURE_V8)
1115         && arm_current_el(env) == 0
1116         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1117         && isread) {
1118         return CP_ACCESS_OK;
1119     }
1120 
1121     return pmreg_access(env, ri, isread);
1122 }
1123 
1124 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1125                                          const ARMCPRegInfo *ri,
1126                                          bool isread)
1127 {
1128     /* SW: software increment write trap control */
1129     if (arm_feature(env, ARM_FEATURE_V8)
1130         && arm_current_el(env) == 0
1131         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1132         && !isread) {
1133         return CP_ACCESS_OK;
1134     }
1135 
1136     return pmreg_access(env, ri, isread);
1137 }
1138 
1139 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1140                                         const ARMCPRegInfo *ri,
1141                                         bool isread)
1142 {
1143     /* ER: event counter read trap control */
1144     if (arm_feature(env, ARM_FEATURE_V8)
1145         && arm_current_el(env) == 0
1146         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1147         return CP_ACCESS_OK;
1148     }
1149 
1150     return pmreg_access(env, ri, isread);
1151 }
1152 
1153 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1154                                          const ARMCPRegInfo *ri,
1155                                          bool isread)
1156 {
1157     /* CR: cycle counter read trap control */
1158     if (arm_feature(env, ARM_FEATURE_V8)
1159         && arm_current_el(env) == 0
1160         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1161         && isread) {
1162         return CP_ACCESS_OK;
1163     }
1164 
1165     return pmreg_access(env, ri, isread);
1166 }
1167 
1168 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1169  * the current EL, security state, and register configuration.
1170  */
1171 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1172 {
1173     uint64_t filter;
1174     bool e, p, u, nsk, nsu, nsh, m;
1175     bool enabled, prohibited, filtered;
1176     bool secure = arm_is_secure(env);
1177     int el = arm_current_el(env);
1178     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1179     uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1180 
1181     if (!arm_feature(env, ARM_FEATURE_PMU)) {
1182         return false;
1183     }
1184 
1185     if (!arm_feature(env, ARM_FEATURE_EL2) ||
1186             (counter < hpmn || counter == 31)) {
1187         e = env->cp15.c9_pmcr & PMCRE;
1188     } else {
1189         e = mdcr_el2 & MDCR_HPME;
1190     }
1191     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1192 
1193     if (!secure) {
1194         if (el == 2 && (counter < hpmn || counter == 31)) {
1195             prohibited = mdcr_el2 & MDCR_HPMD;
1196         } else {
1197             prohibited = false;
1198         }
1199     } else {
1200         prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1201            !(env->cp15.mdcr_el3 & MDCR_SPME);
1202     }
1203 
1204     if (prohibited && counter == 31) {
1205         prohibited = env->cp15.c9_pmcr & PMCRDP;
1206     }
1207 
1208     if (counter == 31) {
1209         filter = env->cp15.pmccfiltr_el0;
1210     } else {
1211         filter = env->cp15.c14_pmevtyper[counter];
1212     }
1213 
1214     p   = filter & PMXEVTYPER_P;
1215     u   = filter & PMXEVTYPER_U;
1216     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1217     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1218     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1219     m   = arm_el_is_aa64(env, 1) &&
1220               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1221 
1222     if (el == 0) {
1223         filtered = secure ? u : u != nsu;
1224     } else if (el == 1) {
1225         filtered = secure ? p : p != nsk;
1226     } else if (el == 2) {
1227         filtered = !nsh;
1228     } else { /* EL3 */
1229         filtered = m != p;
1230     }
1231 
1232     if (counter != 31) {
1233         /*
1234          * If not checking PMCCNTR, ensure the counter is setup to an event we
1235          * support
1236          */
1237         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1238         if (!event_supported(event)) {
1239             return false;
1240         }
1241     }
1242 
1243     return enabled && !prohibited && !filtered;
1244 }
1245 
1246 static void pmu_update_irq(CPUARMState *env)
1247 {
1248     ARMCPU *cpu = env_archcpu(env);
1249     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1250             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1251 }
1252 
1253 /*
1254  * Ensure c15_ccnt is the guest-visible count so that operations such as
1255  * enabling/disabling the counter or filtering, modifying the count itself,
1256  * etc. can be done logically. This is essentially a no-op if the counter is
1257  * not enabled at the time of the call.
1258  */
1259 static void pmccntr_op_start(CPUARMState *env)
1260 {
1261     uint64_t cycles = cycles_get_count(env);
1262 
1263     if (pmu_counter_enabled(env, 31)) {
1264         uint64_t eff_cycles = cycles;
1265         if (env->cp15.c9_pmcr & PMCRD) {
1266             /* Increment once every 64 processor clock cycles */
1267             eff_cycles /= 64;
1268         }
1269 
1270         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1271 
1272         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1273                                  1ull << 63 : 1ull << 31;
1274         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1275             env->cp15.c9_pmovsr |= (1 << 31);
1276             pmu_update_irq(env);
1277         }
1278 
1279         env->cp15.c15_ccnt = new_pmccntr;
1280     }
1281     env->cp15.c15_ccnt_delta = cycles;
1282 }
1283 
1284 /*
1285  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1286  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1287  * pmccntr_op_start.
1288  */
1289 static void pmccntr_op_finish(CPUARMState *env)
1290 {
1291     if (pmu_counter_enabled(env, 31)) {
1292 #ifndef CONFIG_USER_ONLY
1293         /* Calculate when the counter will next overflow */
1294         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1295         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1296             remaining_cycles = (uint32_t)remaining_cycles;
1297         }
1298         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1299 
1300         if (overflow_in > 0) {
1301             int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1302                 overflow_in;
1303             ARMCPU *cpu = env_archcpu(env);
1304             timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1305         }
1306 #endif
1307 
1308         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1309         if (env->cp15.c9_pmcr & PMCRD) {
1310             /* Increment once every 64 processor clock cycles */
1311             prev_cycles /= 64;
1312         }
1313         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1314     }
1315 }
1316 
1317 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1318 {
1319 
1320     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1321     uint64_t count = 0;
1322     if (event_supported(event)) {
1323         uint16_t event_idx = supported_event_map[event];
1324         count = pm_events[event_idx].get_count(env);
1325     }
1326 
1327     if (pmu_counter_enabled(env, counter)) {
1328         uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1329 
1330         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1331             env->cp15.c9_pmovsr |= (1 << counter);
1332             pmu_update_irq(env);
1333         }
1334         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1335     }
1336     env->cp15.c14_pmevcntr_delta[counter] = count;
1337 }
1338 
1339 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1340 {
1341     if (pmu_counter_enabled(env, counter)) {
1342 #ifndef CONFIG_USER_ONLY
1343         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1344         uint16_t event_idx = supported_event_map[event];
1345         uint64_t delta = UINT32_MAX -
1346             (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1347         int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1348 
1349         if (overflow_in > 0) {
1350             int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1351                 overflow_in;
1352             ARMCPU *cpu = env_archcpu(env);
1353             timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1354         }
1355 #endif
1356 
1357         env->cp15.c14_pmevcntr_delta[counter] -=
1358             env->cp15.c14_pmevcntr[counter];
1359     }
1360 }
1361 
1362 void pmu_op_start(CPUARMState *env)
1363 {
1364     unsigned int i;
1365     pmccntr_op_start(env);
1366     for (i = 0; i < pmu_num_counters(env); i++) {
1367         pmevcntr_op_start(env, i);
1368     }
1369 }
1370 
1371 void pmu_op_finish(CPUARMState *env)
1372 {
1373     unsigned int i;
1374     pmccntr_op_finish(env);
1375     for (i = 0; i < pmu_num_counters(env); i++) {
1376         pmevcntr_op_finish(env, i);
1377     }
1378 }
1379 
1380 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1381 {
1382     pmu_op_start(&cpu->env);
1383 }
1384 
1385 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1386 {
1387     pmu_op_finish(&cpu->env);
1388 }
1389 
1390 void arm_pmu_timer_cb(void *opaque)
1391 {
1392     ARMCPU *cpu = opaque;
1393 
1394     /*
1395      * Update all the counter values based on the current underlying counts,
1396      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1397      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1398      * counter may expire.
1399      */
1400     pmu_op_start(&cpu->env);
1401     pmu_op_finish(&cpu->env);
1402 }
1403 
1404 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1405                        uint64_t value)
1406 {
1407     pmu_op_start(env);
1408 
1409     if (value & PMCRC) {
1410         /* The counter has been reset */
1411         env->cp15.c15_ccnt = 0;
1412     }
1413 
1414     if (value & PMCRP) {
1415         unsigned int i;
1416         for (i = 0; i < pmu_num_counters(env); i++) {
1417             env->cp15.c14_pmevcntr[i] = 0;
1418         }
1419     }
1420 
1421     env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1422     env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
1423 
1424     pmu_op_finish(env);
1425 }
1426 
1427 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1428                           uint64_t value)
1429 {
1430     unsigned int i;
1431     for (i = 0; i < pmu_num_counters(env); i++) {
1432         /* Increment a counter's count iff: */
1433         if ((value & (1 << i)) && /* counter's bit is set */
1434                 /* counter is enabled and not filtered */
1435                 pmu_counter_enabled(env, i) &&
1436                 /* counter is SW_INCR */
1437                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1438             pmevcntr_op_start(env, i);
1439 
1440             /*
1441              * Detect if this write causes an overflow since we can't predict
1442              * PMSWINC overflows like we can for other events
1443              */
1444             uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1445 
1446             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1447                 env->cp15.c9_pmovsr |= (1 << i);
1448                 pmu_update_irq(env);
1449             }
1450 
1451             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1452 
1453             pmevcntr_op_finish(env, i);
1454         }
1455     }
1456 }
1457 
1458 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1459 {
1460     uint64_t ret;
1461     pmccntr_op_start(env);
1462     ret = env->cp15.c15_ccnt;
1463     pmccntr_op_finish(env);
1464     return ret;
1465 }
1466 
1467 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1468                          uint64_t value)
1469 {
1470     /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1471      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1472      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1473      * accessed.
1474      */
1475     env->cp15.c9_pmselr = value & 0x1f;
1476 }
1477 
1478 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1479                         uint64_t value)
1480 {
1481     pmccntr_op_start(env);
1482     env->cp15.c15_ccnt = value;
1483     pmccntr_op_finish(env);
1484 }
1485 
1486 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1487                             uint64_t value)
1488 {
1489     uint64_t cur_val = pmccntr_read(env, NULL);
1490 
1491     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1492 }
1493 
1494 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1495                             uint64_t value)
1496 {
1497     pmccntr_op_start(env);
1498     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1499     pmccntr_op_finish(env);
1500 }
1501 
1502 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1503                             uint64_t value)
1504 {
1505     pmccntr_op_start(env);
1506     /* M is not accessible from AArch32 */
1507     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1508         (value & PMCCFILTR);
1509     pmccntr_op_finish(env);
1510 }
1511 
1512 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1513 {
1514     /* M is not visible in AArch32 */
1515     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1516 }
1517 
1518 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1519                             uint64_t value)
1520 {
1521     value &= pmu_counter_mask(env);
1522     env->cp15.c9_pmcnten |= value;
1523 }
1524 
1525 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1526                              uint64_t value)
1527 {
1528     value &= pmu_counter_mask(env);
1529     env->cp15.c9_pmcnten &= ~value;
1530 }
1531 
1532 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1533                          uint64_t value)
1534 {
1535     value &= pmu_counter_mask(env);
1536     env->cp15.c9_pmovsr &= ~value;
1537     pmu_update_irq(env);
1538 }
1539 
1540 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1541                          uint64_t value)
1542 {
1543     value &= pmu_counter_mask(env);
1544     env->cp15.c9_pmovsr |= value;
1545     pmu_update_irq(env);
1546 }
1547 
1548 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1549                              uint64_t value, const uint8_t counter)
1550 {
1551     if (counter == 31) {
1552         pmccfiltr_write(env, ri, value);
1553     } else if (counter < pmu_num_counters(env)) {
1554         pmevcntr_op_start(env, counter);
1555 
1556         /*
1557          * If this counter's event type is changing, store the current
1558          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1559          * pmevcntr_op_finish has the correct baseline when it converts back to
1560          * a delta.
1561          */
1562         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1563             PMXEVTYPER_EVTCOUNT;
1564         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1565         if (old_event != new_event) {
1566             uint64_t count = 0;
1567             if (event_supported(new_event)) {
1568                 uint16_t event_idx = supported_event_map[new_event];
1569                 count = pm_events[event_idx].get_count(env);
1570             }
1571             env->cp15.c14_pmevcntr_delta[counter] = count;
1572         }
1573 
1574         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1575         pmevcntr_op_finish(env, counter);
1576     }
1577     /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1578      * PMSELR value is equal to or greater than the number of implemented
1579      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1580      */
1581 }
1582 
1583 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1584                                const uint8_t counter)
1585 {
1586     if (counter == 31) {
1587         return env->cp15.pmccfiltr_el0;
1588     } else if (counter < pmu_num_counters(env)) {
1589         return env->cp15.c14_pmevtyper[counter];
1590     } else {
1591       /*
1592        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1593        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1594        */
1595         return 0;
1596     }
1597 }
1598 
1599 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1600                               uint64_t value)
1601 {
1602     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1603     pmevtyper_write(env, ri, value, counter);
1604 }
1605 
1606 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1607                                uint64_t value)
1608 {
1609     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1610     env->cp15.c14_pmevtyper[counter] = value;
1611 
1612     /*
1613      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1614      * pmu_op_finish calls when loading saved state for a migration. Because
1615      * we're potentially updating the type of event here, the value written to
1616      * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1617      * different counter type. Therefore, we need to set this value to the
1618      * current count for the counter type we're writing so that pmu_op_finish
1619      * has the correct count for its calculation.
1620      */
1621     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1622     if (event_supported(event)) {
1623         uint16_t event_idx = supported_event_map[event];
1624         env->cp15.c14_pmevcntr_delta[counter] =
1625             pm_events[event_idx].get_count(env);
1626     }
1627 }
1628 
1629 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1630 {
1631     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1632     return pmevtyper_read(env, ri, counter);
1633 }
1634 
1635 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636                              uint64_t value)
1637 {
1638     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1639 }
1640 
1641 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1642 {
1643     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1644 }
1645 
1646 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1647                              uint64_t value, uint8_t counter)
1648 {
1649     if (counter < pmu_num_counters(env)) {
1650         pmevcntr_op_start(env, counter);
1651         env->cp15.c14_pmevcntr[counter] = value;
1652         pmevcntr_op_finish(env, counter);
1653     }
1654     /*
1655      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1656      * are CONSTRAINED UNPREDICTABLE.
1657      */
1658 }
1659 
1660 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1661                               uint8_t counter)
1662 {
1663     if (counter < pmu_num_counters(env)) {
1664         uint64_t ret;
1665         pmevcntr_op_start(env, counter);
1666         ret = env->cp15.c14_pmevcntr[counter];
1667         pmevcntr_op_finish(env, counter);
1668         return ret;
1669     } else {
1670       /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1671        * are CONSTRAINED UNPREDICTABLE. */
1672         return 0;
1673     }
1674 }
1675 
1676 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1677                              uint64_t value)
1678 {
1679     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1680     pmevcntr_write(env, ri, value, counter);
1681 }
1682 
1683 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1684 {
1685     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1686     return pmevcntr_read(env, ri, counter);
1687 }
1688 
1689 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1690                              uint64_t value)
1691 {
1692     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1693     assert(counter < pmu_num_counters(env));
1694     env->cp15.c14_pmevcntr[counter] = value;
1695     pmevcntr_write(env, ri, value, counter);
1696 }
1697 
1698 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1699 {
1700     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1701     assert(counter < pmu_num_counters(env));
1702     return env->cp15.c14_pmevcntr[counter];
1703 }
1704 
1705 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1706                              uint64_t value)
1707 {
1708     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1709 }
1710 
1711 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1712 {
1713     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1714 }
1715 
1716 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1717                             uint64_t value)
1718 {
1719     if (arm_feature(env, ARM_FEATURE_V8)) {
1720         env->cp15.c9_pmuserenr = value & 0xf;
1721     } else {
1722         env->cp15.c9_pmuserenr = value & 1;
1723     }
1724 }
1725 
1726 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1727                              uint64_t value)
1728 {
1729     /* We have no event counters so only the C bit can be changed */
1730     value &= pmu_counter_mask(env);
1731     env->cp15.c9_pminten |= value;
1732     pmu_update_irq(env);
1733 }
1734 
1735 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1736                              uint64_t value)
1737 {
1738     value &= pmu_counter_mask(env);
1739     env->cp15.c9_pminten &= ~value;
1740     pmu_update_irq(env);
1741 }
1742 
1743 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1744                        uint64_t value)
1745 {
1746     /* Note that even though the AArch64 view of this register has bits
1747      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1748      * architectural requirements for bits which are RES0 only in some
1749      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1750      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1751      */
1752     raw_write(env, ri, value & ~0x1FULL);
1753 }
1754 
1755 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1756 {
1757     /* Begin with base v8.0 state.  */
1758     uint32_t valid_mask = 0x3fff;
1759     ARMCPU *cpu = env_archcpu(env);
1760 
1761     if (ri->state == ARM_CP_STATE_AA64) {
1762         if (arm_feature(env, ARM_FEATURE_AARCH64) &&
1763             !cpu_isar_feature(aa64_aa32_el1, cpu)) {
1764                 value |= SCR_FW | SCR_AW;   /* these two bits are RES1.  */
1765         }
1766         valid_mask &= ~SCR_NET;
1767 
1768         if (cpu_isar_feature(aa64_lor, cpu)) {
1769             valid_mask |= SCR_TLOR;
1770         }
1771         if (cpu_isar_feature(aa64_pauth, cpu)) {
1772             valid_mask |= SCR_API | SCR_APK;
1773         }
1774         if (cpu_isar_feature(aa64_sel2, cpu)) {
1775             valid_mask |= SCR_EEL2;
1776         }
1777         if (cpu_isar_feature(aa64_mte, cpu)) {
1778             valid_mask |= SCR_ATA;
1779         }
1780     } else {
1781         valid_mask &= ~(SCR_RW | SCR_ST);
1782     }
1783 
1784     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1785         valid_mask &= ~SCR_HCE;
1786 
1787         /* On ARMv7, SMD (or SCD as it is called in v7) is only
1788          * supported if EL2 exists. The bit is UNK/SBZP when
1789          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1790          * when EL2 is unavailable.
1791          * On ARMv8, this bit is always available.
1792          */
1793         if (arm_feature(env, ARM_FEATURE_V7) &&
1794             !arm_feature(env, ARM_FEATURE_V8)) {
1795             valid_mask &= ~SCR_SMD;
1796         }
1797     }
1798 
1799     /* Clear all-context RES0 bits.  */
1800     value &= valid_mask;
1801     raw_write(env, ri, value);
1802 }
1803 
1804 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1805 {
1806     /*
1807      * scr_write will set the RES1 bits on an AArch64-only CPU.
1808      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1809      */
1810     scr_write(env, ri, 0);
1811 }
1812 
1813 static CPAccessResult access_aa64_tid2(CPUARMState *env,
1814                                        const ARMCPRegInfo *ri,
1815                                        bool isread)
1816 {
1817     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1818         return CP_ACCESS_TRAP_EL2;
1819     }
1820 
1821     return CP_ACCESS_OK;
1822 }
1823 
1824 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1825 {
1826     ARMCPU *cpu = env_archcpu(env);
1827 
1828     /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1829      * bank
1830      */
1831     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1832                                         ri->secure & ARM_CP_SECSTATE_S);
1833 
1834     return cpu->ccsidr[index];
1835 }
1836 
1837 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1838                          uint64_t value)
1839 {
1840     raw_write(env, ri, value & 0xf);
1841 }
1842 
1843 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1844 {
1845     CPUState *cs = env_cpu(env);
1846     bool el1 = arm_current_el(env) == 1;
1847     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1848     uint64_t ret = 0;
1849 
1850     if (hcr_el2 & HCR_IMO) {
1851         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1852             ret |= CPSR_I;
1853         }
1854     } else {
1855         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1856             ret |= CPSR_I;
1857         }
1858     }
1859 
1860     if (hcr_el2 & HCR_FMO) {
1861         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1862             ret |= CPSR_F;
1863         }
1864     } else {
1865         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1866             ret |= CPSR_F;
1867         }
1868     }
1869 
1870     /* External aborts are not possible in QEMU so A bit is always clear */
1871     return ret;
1872 }
1873 
1874 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1875                                        bool isread)
1876 {
1877     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1878         return CP_ACCESS_TRAP_EL2;
1879     }
1880 
1881     return CP_ACCESS_OK;
1882 }
1883 
1884 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1885                                        bool isread)
1886 {
1887     if (arm_feature(env, ARM_FEATURE_V8)) {
1888         return access_aa64_tid1(env, ri, isread);
1889     }
1890 
1891     return CP_ACCESS_OK;
1892 }
1893 
1894 static const ARMCPRegInfo v7_cp_reginfo[] = {
1895     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1896     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1897       .access = PL1_W, .type = ARM_CP_NOP },
1898     /* Performance monitors are implementation defined in v7,
1899      * but with an ARM recommended set of registers, which we
1900      * follow.
1901      *
1902      * Performance registers fall into three categories:
1903      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1904      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1905      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1906      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1907      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1908      */
1909     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1910       .access = PL0_RW, .type = ARM_CP_ALIAS,
1911       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1912       .writefn = pmcntenset_write,
1913       .accessfn = pmreg_access,
1914       .raw_writefn = raw_write },
1915     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1916       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1917       .access = PL0_RW, .accessfn = pmreg_access,
1918       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1919       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1920     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1921       .access = PL0_RW,
1922       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1923       .accessfn = pmreg_access,
1924       .writefn = pmcntenclr_write,
1925       .type = ARM_CP_ALIAS },
1926     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1927       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1928       .access = PL0_RW, .accessfn = pmreg_access,
1929       .type = ARM_CP_ALIAS,
1930       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1931       .writefn = pmcntenclr_write },
1932     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1933       .access = PL0_RW, .type = ARM_CP_IO,
1934       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1935       .accessfn = pmreg_access,
1936       .writefn = pmovsr_write,
1937       .raw_writefn = raw_write },
1938     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1939       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1940       .access = PL0_RW, .accessfn = pmreg_access,
1941       .type = ARM_CP_ALIAS | ARM_CP_IO,
1942       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1943       .writefn = pmovsr_write,
1944       .raw_writefn = raw_write },
1945     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1946       .access = PL0_W, .accessfn = pmreg_access_swinc,
1947       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1948       .writefn = pmswinc_write },
1949     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1950       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1951       .access = PL0_W, .accessfn = pmreg_access_swinc,
1952       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1953       .writefn = pmswinc_write },
1954     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1955       .access = PL0_RW, .type = ARM_CP_ALIAS,
1956       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1957       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1958       .raw_writefn = raw_write},
1959     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1960       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1961       .access = PL0_RW, .accessfn = pmreg_access_selr,
1962       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1963       .writefn = pmselr_write, .raw_writefn = raw_write, },
1964     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1965       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1966       .readfn = pmccntr_read, .writefn = pmccntr_write32,
1967       .accessfn = pmreg_access_ccntr },
1968     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1969       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1970       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1971       .type = ARM_CP_IO,
1972       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1973       .readfn = pmccntr_read, .writefn = pmccntr_write,
1974       .raw_readfn = raw_read, .raw_writefn = raw_write, },
1975     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1976       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1977       .access = PL0_RW, .accessfn = pmreg_access,
1978       .type = ARM_CP_ALIAS | ARM_CP_IO,
1979       .resetvalue = 0, },
1980     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1981       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1982       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1983       .access = PL0_RW, .accessfn = pmreg_access,
1984       .type = ARM_CP_IO,
1985       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1986       .resetvalue = 0, },
1987     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1988       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1989       .accessfn = pmreg_access,
1990       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1991     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1992       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1993       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1994       .accessfn = pmreg_access,
1995       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1996     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1997       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1998       .accessfn = pmreg_access_xevcntr,
1999       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2000     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2001       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2002       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2003       .accessfn = pmreg_access_xevcntr,
2004       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2005     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2006       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2007       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2008       .resetvalue = 0,
2009       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2010     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2011       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2012       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2013       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2014       .resetvalue = 0,
2015       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2016     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2017       .access = PL1_RW, .accessfn = access_tpm,
2018       .type = ARM_CP_ALIAS | ARM_CP_IO,
2019       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2020       .resetvalue = 0,
2021       .writefn = pmintenset_write, .raw_writefn = raw_write },
2022     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2023       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2024       .access = PL1_RW, .accessfn = access_tpm,
2025       .type = ARM_CP_IO,
2026       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2027       .writefn = pmintenset_write, .raw_writefn = raw_write,
2028       .resetvalue = 0x0 },
2029     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2030       .access = PL1_RW, .accessfn = access_tpm,
2031       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2032       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2033       .writefn = pmintenclr_write, },
2034     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2035       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2036       .access = PL1_RW, .accessfn = access_tpm,
2037       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2038       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2039       .writefn = pmintenclr_write },
2040     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2041       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2042       .access = PL1_R,
2043       .accessfn = access_aa64_tid2,
2044       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2045     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2046       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2047       .access = PL1_RW,
2048       .accessfn = access_aa64_tid2,
2049       .writefn = csselr_write, .resetvalue = 0,
2050       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2051                              offsetof(CPUARMState, cp15.csselr_ns) } },
2052     /* Auxiliary ID register: this actually has an IMPDEF value but for now
2053      * just RAZ for all cores:
2054      */
2055     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2056       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2057       .access = PL1_R, .type = ARM_CP_CONST,
2058       .accessfn = access_aa64_tid1,
2059       .resetvalue = 0 },
2060     /* Auxiliary fault status registers: these also are IMPDEF, and we
2061      * choose to RAZ/WI for all cores.
2062      */
2063     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2064       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2065       .access = PL1_RW, .accessfn = access_tvm_trvm,
2066       .type = ARM_CP_CONST, .resetvalue = 0 },
2067     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2068       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2069       .access = PL1_RW, .accessfn = access_tvm_trvm,
2070       .type = ARM_CP_CONST, .resetvalue = 0 },
2071     /* MAIR can just read-as-written because we don't implement caches
2072      * and so don't need to care about memory attributes.
2073      */
2074     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2075       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2076       .access = PL1_RW, .accessfn = access_tvm_trvm,
2077       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2078       .resetvalue = 0 },
2079     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2080       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2081       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2082       .resetvalue = 0 },
2083     /* For non-long-descriptor page tables these are PRRR and NMRR;
2084      * regardless they still act as reads-as-written for QEMU.
2085      */
2086      /* MAIR0/1 are defined separately from their 64-bit counterpart which
2087       * allows them to assign the correct fieldoffset based on the endianness
2088       * handled in the field definitions.
2089       */
2090     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2091       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2092       .access = PL1_RW, .accessfn = access_tvm_trvm,
2093       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2094                              offsetof(CPUARMState, cp15.mair0_ns) },
2095       .resetfn = arm_cp_reset_ignore },
2096     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2097       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2098       .access = PL1_RW, .accessfn = access_tvm_trvm,
2099       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2100                              offsetof(CPUARMState, cp15.mair1_ns) },
2101       .resetfn = arm_cp_reset_ignore },
2102     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2103       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2104       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2105     /* 32 bit ITLB invalidates */
2106     { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2107       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2108       .writefn = tlbiall_write },
2109     { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2110       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2111       .writefn = tlbimva_write },
2112     { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2113       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2114       .writefn = tlbiasid_write },
2115     /* 32 bit DTLB invalidates */
2116     { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2117       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2118       .writefn = tlbiall_write },
2119     { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2120       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2121       .writefn = tlbimva_write },
2122     { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2123       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2124       .writefn = tlbiasid_write },
2125     /* 32 bit TLB invalidates */
2126     { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2127       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2128       .writefn = tlbiall_write },
2129     { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2130       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2131       .writefn = tlbimva_write },
2132     { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2133       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2134       .writefn = tlbiasid_write },
2135     { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2136       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2137       .writefn = tlbimvaa_write },
2138     REGINFO_SENTINEL
2139 };
2140 
2141 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2142     /* 32 bit TLB invalidates, Inner Shareable */
2143     { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2144       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2145       .writefn = tlbiall_is_write },
2146     { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2147       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2148       .writefn = tlbimva_is_write },
2149     { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2150       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2151       .writefn = tlbiasid_is_write },
2152     { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2153       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2154       .writefn = tlbimvaa_is_write },
2155     REGINFO_SENTINEL
2156 };
2157 
2158 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2159     /* PMOVSSET is not implemented in v7 before v7ve */
2160     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2161       .access = PL0_RW, .accessfn = pmreg_access,
2162       .type = ARM_CP_ALIAS | ARM_CP_IO,
2163       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2164       .writefn = pmovsset_write,
2165       .raw_writefn = raw_write },
2166     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2167       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2168       .access = PL0_RW, .accessfn = pmreg_access,
2169       .type = ARM_CP_ALIAS | ARM_CP_IO,
2170       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2171       .writefn = pmovsset_write,
2172       .raw_writefn = raw_write },
2173     REGINFO_SENTINEL
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     REGINFO_SENTINEL
2215 };
2216 
2217 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2218     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2219       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2220       .access = PL0_RW,
2221       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2222     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2223       .access = PL0_RW,
2224       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2225                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2226       .resetfn = arm_cp_reset_ignore },
2227     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2228       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2229       .access = PL0_R|PL1_W,
2230       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2231       .resetvalue = 0},
2232     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2233       .access = PL0_R|PL1_W,
2234       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2235                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2236       .resetfn = arm_cp_reset_ignore },
2237     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2238       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2239       .access = PL1_RW,
2240       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2241     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2242       .access = PL1_RW,
2243       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2244                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2245       .resetvalue = 0 },
2246     REGINFO_SENTINEL
2247 };
2248 
2249 #ifndef CONFIG_USER_ONLY
2250 
2251 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2252                                        bool isread)
2253 {
2254     /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2255      * Writable only at the highest implemented exception level.
2256      */
2257     int el = arm_current_el(env);
2258     uint64_t hcr;
2259     uint32_t cntkctl;
2260 
2261     switch (el) {
2262     case 0:
2263         hcr = arm_hcr_el2_eff(env);
2264         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2265             cntkctl = env->cp15.cnthctl_el2;
2266         } else {
2267             cntkctl = env->cp15.c14_cntkctl;
2268         }
2269         if (!extract32(cntkctl, 0, 2)) {
2270             return CP_ACCESS_TRAP;
2271         }
2272         break;
2273     case 1:
2274         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2275             arm_is_secure_below_el3(env)) {
2276             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2277             return CP_ACCESS_TRAP_UNCATEGORIZED;
2278         }
2279         break;
2280     case 2:
2281     case 3:
2282         break;
2283     }
2284 
2285     if (!isread && el < arm_highest_el(env)) {
2286         return CP_ACCESS_TRAP_UNCATEGORIZED;
2287     }
2288 
2289     return CP_ACCESS_OK;
2290 }
2291 
2292 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2293                                         bool isread)
2294 {
2295     unsigned int cur_el = arm_current_el(env);
2296     bool has_el2 = arm_is_el2_enabled(env);
2297     uint64_t hcr = arm_hcr_el2_eff(env);
2298 
2299     switch (cur_el) {
2300     case 0:
2301         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2302         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2303             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2304                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2305         }
2306 
2307         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2308         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2309             return CP_ACCESS_TRAP;
2310         }
2311 
2312         /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2313         if (hcr & HCR_E2H) {
2314             if (timeridx == GTIMER_PHYS &&
2315                 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2316                 return CP_ACCESS_TRAP_EL2;
2317             }
2318         } else {
2319             /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2320             if (has_el2 && timeridx == GTIMER_PHYS &&
2321                 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2322                 return CP_ACCESS_TRAP_EL2;
2323             }
2324         }
2325         break;
2326 
2327     case 1:
2328         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2329         if (has_el2 && timeridx == GTIMER_PHYS &&
2330             (hcr & HCR_E2H
2331              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2332              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2333             return CP_ACCESS_TRAP_EL2;
2334         }
2335         break;
2336     }
2337     return CP_ACCESS_OK;
2338 }
2339 
2340 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2341                                       bool isread)
2342 {
2343     unsigned int cur_el = arm_current_el(env);
2344     bool has_el2 = arm_is_el2_enabled(env);
2345     uint64_t hcr = arm_hcr_el2_eff(env);
2346 
2347     switch (cur_el) {
2348     case 0:
2349         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2350             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2351             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2352                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2353         }
2354 
2355         /*
2356          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2357          * EL0 if EL0[PV]TEN is zero.
2358          */
2359         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2360             return CP_ACCESS_TRAP;
2361         }
2362         /* fall through */
2363 
2364     case 1:
2365         if (has_el2 && timeridx == GTIMER_PHYS) {
2366             if (hcr & HCR_E2H) {
2367                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2368                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2369                     return CP_ACCESS_TRAP_EL2;
2370                 }
2371             } else {
2372                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2373                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2374                     return CP_ACCESS_TRAP_EL2;
2375                 }
2376             }
2377         }
2378         break;
2379     }
2380     return CP_ACCESS_OK;
2381 }
2382 
2383 static CPAccessResult gt_pct_access(CPUARMState *env,
2384                                     const ARMCPRegInfo *ri,
2385                                     bool isread)
2386 {
2387     return gt_counter_access(env, GTIMER_PHYS, isread);
2388 }
2389 
2390 static CPAccessResult gt_vct_access(CPUARMState *env,
2391                                     const ARMCPRegInfo *ri,
2392                                     bool isread)
2393 {
2394     return gt_counter_access(env, GTIMER_VIRT, isread);
2395 }
2396 
2397 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2398                                        bool isread)
2399 {
2400     return gt_timer_access(env, GTIMER_PHYS, isread);
2401 }
2402 
2403 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2404                                        bool isread)
2405 {
2406     return gt_timer_access(env, GTIMER_VIRT, isread);
2407 }
2408 
2409 static CPAccessResult gt_stimer_access(CPUARMState *env,
2410                                        const ARMCPRegInfo *ri,
2411                                        bool isread)
2412 {
2413     /* The AArch64 register view of the secure physical timer is
2414      * always accessible from EL3, and configurably accessible from
2415      * Secure EL1.
2416      */
2417     switch (arm_current_el(env)) {
2418     case 1:
2419         if (!arm_is_secure(env)) {
2420             return CP_ACCESS_TRAP;
2421         }
2422         if (!(env->cp15.scr_el3 & SCR_ST)) {
2423             return CP_ACCESS_TRAP_EL3;
2424         }
2425         return CP_ACCESS_OK;
2426     case 0:
2427     case 2:
2428         return CP_ACCESS_TRAP;
2429     case 3:
2430         return CP_ACCESS_OK;
2431     default:
2432         g_assert_not_reached();
2433     }
2434 }
2435 
2436 static uint64_t gt_get_countervalue(CPUARMState *env)
2437 {
2438     ARMCPU *cpu = env_archcpu(env);
2439 
2440     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2441 }
2442 
2443 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2444 {
2445     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2446 
2447     if (gt->ctl & 1) {
2448         /* Timer enabled: calculate and set current ISTATUS, irq, and
2449          * reset timer to when ISTATUS next has to change
2450          */
2451         uint64_t offset = timeridx == GTIMER_VIRT ?
2452                                       cpu->env.cp15.cntvoff_el2 : 0;
2453         uint64_t count = gt_get_countervalue(&cpu->env);
2454         /* Note that this must be unsigned 64 bit arithmetic: */
2455         int istatus = count - offset >= gt->cval;
2456         uint64_t nexttick;
2457         int irqstate;
2458 
2459         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2460 
2461         irqstate = (istatus && !(gt->ctl & 2));
2462         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2463 
2464         if (istatus) {
2465             /* Next transition is when count rolls back over to zero */
2466             nexttick = UINT64_MAX;
2467         } else {
2468             /* Next transition is when we hit cval */
2469             nexttick = gt->cval + offset;
2470         }
2471         /* Note that the desired next expiry time might be beyond the
2472          * signed-64-bit range of a QEMUTimer -- in this case we just
2473          * set the timer for as far in the future as possible. When the
2474          * timer expires we will reset the timer for any remaining period.
2475          */
2476         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2477             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2478         } else {
2479             timer_mod(cpu->gt_timer[timeridx], nexttick);
2480         }
2481         trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2482     } else {
2483         /* Timer disabled: ISTATUS and timer output always clear */
2484         gt->ctl &= ~4;
2485         qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2486         timer_del(cpu->gt_timer[timeridx]);
2487         trace_arm_gt_recalc_disabled(timeridx);
2488     }
2489 }
2490 
2491 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2492                            int timeridx)
2493 {
2494     ARMCPU *cpu = env_archcpu(env);
2495 
2496     timer_del(cpu->gt_timer[timeridx]);
2497 }
2498 
2499 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2500 {
2501     return gt_get_countervalue(env);
2502 }
2503 
2504 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2505 {
2506     uint64_t hcr;
2507 
2508     switch (arm_current_el(env)) {
2509     case 2:
2510         hcr = arm_hcr_el2_eff(env);
2511         if (hcr & HCR_E2H) {
2512             return 0;
2513         }
2514         break;
2515     case 0:
2516         hcr = arm_hcr_el2_eff(env);
2517         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2518             return 0;
2519         }
2520         break;
2521     }
2522 
2523     return env->cp15.cntvoff_el2;
2524 }
2525 
2526 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2527 {
2528     return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2529 }
2530 
2531 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2532                           int timeridx,
2533                           uint64_t value)
2534 {
2535     trace_arm_gt_cval_write(timeridx, value);
2536     env->cp15.c14_timer[timeridx].cval = value;
2537     gt_recalc_timer(env_archcpu(env), timeridx);
2538 }
2539 
2540 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2541                              int timeridx)
2542 {
2543     uint64_t offset = 0;
2544 
2545     switch (timeridx) {
2546     case GTIMER_VIRT:
2547     case GTIMER_HYPVIRT:
2548         offset = gt_virt_cnt_offset(env);
2549         break;
2550     }
2551 
2552     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2553                       (gt_get_countervalue(env) - offset));
2554 }
2555 
2556 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2557                           int timeridx,
2558                           uint64_t value)
2559 {
2560     uint64_t offset = 0;
2561 
2562     switch (timeridx) {
2563     case GTIMER_VIRT:
2564     case GTIMER_HYPVIRT:
2565         offset = gt_virt_cnt_offset(env);
2566         break;
2567     }
2568 
2569     trace_arm_gt_tval_write(timeridx, value);
2570     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2571                                          sextract64(value, 0, 32);
2572     gt_recalc_timer(env_archcpu(env), timeridx);
2573 }
2574 
2575 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2576                          int timeridx,
2577                          uint64_t value)
2578 {
2579     ARMCPU *cpu = env_archcpu(env);
2580     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2581 
2582     trace_arm_gt_ctl_write(timeridx, value);
2583     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2584     if ((oldval ^ value) & 1) {
2585         /* Enable toggled */
2586         gt_recalc_timer(cpu, timeridx);
2587     } else if ((oldval ^ value) & 2) {
2588         /* IMASK toggled: don't need to recalculate,
2589          * just set the interrupt line based on ISTATUS
2590          */
2591         int irqstate = (oldval & 4) && !(value & 2);
2592 
2593         trace_arm_gt_imask_toggle(timeridx, irqstate);
2594         qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2595     }
2596 }
2597 
2598 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2599 {
2600     gt_timer_reset(env, ri, GTIMER_PHYS);
2601 }
2602 
2603 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2604                                uint64_t value)
2605 {
2606     gt_cval_write(env, ri, GTIMER_PHYS, value);
2607 }
2608 
2609 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2610 {
2611     return gt_tval_read(env, ri, GTIMER_PHYS);
2612 }
2613 
2614 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2615                                uint64_t value)
2616 {
2617     gt_tval_write(env, ri, GTIMER_PHYS, value);
2618 }
2619 
2620 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2621                               uint64_t value)
2622 {
2623     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2624 }
2625 
2626 static int gt_phys_redir_timeridx(CPUARMState *env)
2627 {
2628     switch (arm_mmu_idx(env)) {
2629     case ARMMMUIdx_E20_0:
2630     case ARMMMUIdx_E20_2:
2631     case ARMMMUIdx_E20_2_PAN:
2632     case ARMMMUIdx_SE20_0:
2633     case ARMMMUIdx_SE20_2:
2634     case ARMMMUIdx_SE20_2_PAN:
2635         return GTIMER_HYP;
2636     default:
2637         return GTIMER_PHYS;
2638     }
2639 }
2640 
2641 static int gt_virt_redir_timeridx(CPUARMState *env)
2642 {
2643     switch (arm_mmu_idx(env)) {
2644     case ARMMMUIdx_E20_0:
2645     case ARMMMUIdx_E20_2:
2646     case ARMMMUIdx_E20_2_PAN:
2647     case ARMMMUIdx_SE20_0:
2648     case ARMMMUIdx_SE20_2:
2649     case ARMMMUIdx_SE20_2_PAN:
2650         return GTIMER_HYPVIRT;
2651     default:
2652         return GTIMER_VIRT;
2653     }
2654 }
2655 
2656 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2657                                         const ARMCPRegInfo *ri)
2658 {
2659     int timeridx = gt_phys_redir_timeridx(env);
2660     return env->cp15.c14_timer[timeridx].cval;
2661 }
2662 
2663 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2664                                      uint64_t value)
2665 {
2666     int timeridx = gt_phys_redir_timeridx(env);
2667     gt_cval_write(env, ri, timeridx, value);
2668 }
2669 
2670 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2671                                         const ARMCPRegInfo *ri)
2672 {
2673     int timeridx = gt_phys_redir_timeridx(env);
2674     return gt_tval_read(env, ri, timeridx);
2675 }
2676 
2677 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2678                                      uint64_t value)
2679 {
2680     int timeridx = gt_phys_redir_timeridx(env);
2681     gt_tval_write(env, ri, timeridx, value);
2682 }
2683 
2684 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2685                                        const ARMCPRegInfo *ri)
2686 {
2687     int timeridx = gt_phys_redir_timeridx(env);
2688     return env->cp15.c14_timer[timeridx].ctl;
2689 }
2690 
2691 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2692                                     uint64_t value)
2693 {
2694     int timeridx = gt_phys_redir_timeridx(env);
2695     gt_ctl_write(env, ri, timeridx, value);
2696 }
2697 
2698 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2699 {
2700     gt_timer_reset(env, ri, GTIMER_VIRT);
2701 }
2702 
2703 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2704                                uint64_t value)
2705 {
2706     gt_cval_write(env, ri, GTIMER_VIRT, value);
2707 }
2708 
2709 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2710 {
2711     return gt_tval_read(env, ri, GTIMER_VIRT);
2712 }
2713 
2714 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2715                                uint64_t value)
2716 {
2717     gt_tval_write(env, ri, GTIMER_VIRT, value);
2718 }
2719 
2720 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2721                               uint64_t value)
2722 {
2723     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2724 }
2725 
2726 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2727                               uint64_t value)
2728 {
2729     ARMCPU *cpu = env_archcpu(env);
2730 
2731     trace_arm_gt_cntvoff_write(value);
2732     raw_write(env, ri, value);
2733     gt_recalc_timer(cpu, GTIMER_VIRT);
2734 }
2735 
2736 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2737                                         const ARMCPRegInfo *ri)
2738 {
2739     int timeridx = gt_virt_redir_timeridx(env);
2740     return env->cp15.c14_timer[timeridx].cval;
2741 }
2742 
2743 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2744                                      uint64_t value)
2745 {
2746     int timeridx = gt_virt_redir_timeridx(env);
2747     gt_cval_write(env, ri, timeridx, value);
2748 }
2749 
2750 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2751                                         const ARMCPRegInfo *ri)
2752 {
2753     int timeridx = gt_virt_redir_timeridx(env);
2754     return gt_tval_read(env, ri, timeridx);
2755 }
2756 
2757 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2758                                      uint64_t value)
2759 {
2760     int timeridx = gt_virt_redir_timeridx(env);
2761     gt_tval_write(env, ri, timeridx, value);
2762 }
2763 
2764 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2765                                        const ARMCPRegInfo *ri)
2766 {
2767     int timeridx = gt_virt_redir_timeridx(env);
2768     return env->cp15.c14_timer[timeridx].ctl;
2769 }
2770 
2771 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2772                                     uint64_t value)
2773 {
2774     int timeridx = gt_virt_redir_timeridx(env);
2775     gt_ctl_write(env, ri, timeridx, value);
2776 }
2777 
2778 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2779 {
2780     gt_timer_reset(env, ri, GTIMER_HYP);
2781 }
2782 
2783 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2784                               uint64_t value)
2785 {
2786     gt_cval_write(env, ri, GTIMER_HYP, value);
2787 }
2788 
2789 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2790 {
2791     return gt_tval_read(env, ri, GTIMER_HYP);
2792 }
2793 
2794 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2795                               uint64_t value)
2796 {
2797     gt_tval_write(env, ri, GTIMER_HYP, value);
2798 }
2799 
2800 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2801                               uint64_t value)
2802 {
2803     gt_ctl_write(env, ri, GTIMER_HYP, value);
2804 }
2805 
2806 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2807 {
2808     gt_timer_reset(env, ri, GTIMER_SEC);
2809 }
2810 
2811 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2812                               uint64_t value)
2813 {
2814     gt_cval_write(env, ri, GTIMER_SEC, value);
2815 }
2816 
2817 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2818 {
2819     return gt_tval_read(env, ri, GTIMER_SEC);
2820 }
2821 
2822 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2823                               uint64_t value)
2824 {
2825     gt_tval_write(env, ri, GTIMER_SEC, value);
2826 }
2827 
2828 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2829                               uint64_t value)
2830 {
2831     gt_ctl_write(env, ri, GTIMER_SEC, value);
2832 }
2833 
2834 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2835 {
2836     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2837 }
2838 
2839 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2840                              uint64_t value)
2841 {
2842     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2843 }
2844 
2845 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2846 {
2847     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2848 }
2849 
2850 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2851                              uint64_t value)
2852 {
2853     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2854 }
2855 
2856 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2857                             uint64_t value)
2858 {
2859     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2860 }
2861 
2862 void arm_gt_ptimer_cb(void *opaque)
2863 {
2864     ARMCPU *cpu = opaque;
2865 
2866     gt_recalc_timer(cpu, GTIMER_PHYS);
2867 }
2868 
2869 void arm_gt_vtimer_cb(void *opaque)
2870 {
2871     ARMCPU *cpu = opaque;
2872 
2873     gt_recalc_timer(cpu, GTIMER_VIRT);
2874 }
2875 
2876 void arm_gt_htimer_cb(void *opaque)
2877 {
2878     ARMCPU *cpu = opaque;
2879 
2880     gt_recalc_timer(cpu, GTIMER_HYP);
2881 }
2882 
2883 void arm_gt_stimer_cb(void *opaque)
2884 {
2885     ARMCPU *cpu = opaque;
2886 
2887     gt_recalc_timer(cpu, GTIMER_SEC);
2888 }
2889 
2890 void arm_gt_hvtimer_cb(void *opaque)
2891 {
2892     ARMCPU *cpu = opaque;
2893 
2894     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2895 }
2896 
2897 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2898 {
2899     ARMCPU *cpu = env_archcpu(env);
2900 
2901     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2902 }
2903 
2904 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2905     /* Note that CNTFRQ is purely reads-as-written for the benefit
2906      * of software; writing it doesn't actually change the timer frequency.
2907      * Our reset value matches the fixed frequency we implement the timer at.
2908      */
2909     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2910       .type = ARM_CP_ALIAS,
2911       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2912       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2913     },
2914     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2915       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2916       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2917       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2918       .resetfn = arm_gt_cntfrq_reset,
2919     },
2920     /* overall control: mostly access permissions */
2921     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2922       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2923       .access = PL1_RW,
2924       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2925       .resetvalue = 0,
2926     },
2927     /* per-timer control */
2928     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2929       .secure = ARM_CP_SECSTATE_NS,
2930       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2931       .accessfn = gt_ptimer_access,
2932       .fieldoffset = offsetoflow32(CPUARMState,
2933                                    cp15.c14_timer[GTIMER_PHYS].ctl),
2934       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2935       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2936     },
2937     { .name = "CNTP_CTL_S",
2938       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2939       .secure = ARM_CP_SECSTATE_S,
2940       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2941       .accessfn = gt_ptimer_access,
2942       .fieldoffset = offsetoflow32(CPUARMState,
2943                                    cp15.c14_timer[GTIMER_SEC].ctl),
2944       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2945     },
2946     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2947       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2948       .type = ARM_CP_IO, .access = PL0_RW,
2949       .accessfn = gt_ptimer_access,
2950       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2951       .resetvalue = 0,
2952       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2953       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2954     },
2955     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2956       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2957       .accessfn = gt_vtimer_access,
2958       .fieldoffset = offsetoflow32(CPUARMState,
2959                                    cp15.c14_timer[GTIMER_VIRT].ctl),
2960       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2961       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2962     },
2963     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2964       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2965       .type = ARM_CP_IO, .access = PL0_RW,
2966       .accessfn = gt_vtimer_access,
2967       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2968       .resetvalue = 0,
2969       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2970       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2971     },
2972     /* TimerValue views: a 32 bit downcounting view of the underlying state */
2973     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2974       .secure = ARM_CP_SECSTATE_NS,
2975       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2976       .accessfn = gt_ptimer_access,
2977       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2978     },
2979     { .name = "CNTP_TVAL_S",
2980       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2981       .secure = ARM_CP_SECSTATE_S,
2982       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2983       .accessfn = gt_ptimer_access,
2984       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2985     },
2986     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2987       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2988       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2989       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2990       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2991     },
2992     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2993       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2994       .accessfn = gt_vtimer_access,
2995       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
2996     },
2997     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2998       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2999       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3000       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3001       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3002     },
3003     /* The counter itself */
3004     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3005       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3006       .accessfn = gt_pct_access,
3007       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3008     },
3009     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3010       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3011       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3012       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3013     },
3014     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3015       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3016       .accessfn = gt_vct_access,
3017       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3018     },
3019     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3020       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3021       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3022       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3023     },
3024     /* Comparison value, indicating when the timer goes off */
3025     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3026       .secure = ARM_CP_SECSTATE_NS,
3027       .access = PL0_RW,
3028       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3029       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3030       .accessfn = gt_ptimer_access,
3031       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3032       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3033     },
3034     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3035       .secure = ARM_CP_SECSTATE_S,
3036       .access = PL0_RW,
3037       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3038       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3039       .accessfn = gt_ptimer_access,
3040       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3041     },
3042     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3043       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3044       .access = PL0_RW,
3045       .type = ARM_CP_IO,
3046       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3047       .resetvalue = 0, .accessfn = gt_ptimer_access,
3048       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3049       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3050     },
3051     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3052       .access = PL0_RW,
3053       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3054       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3055       .accessfn = gt_vtimer_access,
3056       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3057       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3058     },
3059     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3060       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3061       .access = PL0_RW,
3062       .type = ARM_CP_IO,
3063       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3064       .resetvalue = 0, .accessfn = gt_vtimer_access,
3065       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3066       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3067     },
3068     /* Secure timer -- this is actually restricted to only EL3
3069      * and configurably Secure-EL1 via the accessfn.
3070      */
3071     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3072       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3073       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3074       .accessfn = gt_stimer_access,
3075       .readfn = gt_sec_tval_read,
3076       .writefn = gt_sec_tval_write,
3077       .resetfn = gt_sec_timer_reset,
3078     },
3079     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3080       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3081       .type = ARM_CP_IO, .access = PL1_RW,
3082       .accessfn = gt_stimer_access,
3083       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3084       .resetvalue = 0,
3085       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3086     },
3087     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3088       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3089       .type = ARM_CP_IO, .access = PL1_RW,
3090       .accessfn = gt_stimer_access,
3091       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3092       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3093     },
3094     REGINFO_SENTINEL
3095 };
3096 
3097 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3098                                  bool isread)
3099 {
3100     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3101         return CP_ACCESS_TRAP;
3102     }
3103     return CP_ACCESS_OK;
3104 }
3105 
3106 #else
3107 
3108 /* In user-mode most of the generic timer registers are inaccessible
3109  * however modern kernels (4.12+) allow access to cntvct_el0
3110  */
3111 
3112 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3113 {
3114     ARMCPU *cpu = env_archcpu(env);
3115 
3116     /* Currently we have no support for QEMUTimer in linux-user so we
3117      * can't call gt_get_countervalue(env), instead we directly
3118      * call the lower level functions.
3119      */
3120     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3121 }
3122 
3123 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3124     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3125       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3126       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3127       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3128       .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3129     },
3130     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3131       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3132       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3133       .readfn = gt_virt_cnt_read,
3134     },
3135     REGINFO_SENTINEL
3136 };
3137 
3138 #endif
3139 
3140 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3141 {
3142     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3143         raw_write(env, ri, value);
3144     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3145         raw_write(env, ri, value & 0xfffff6ff);
3146     } else {
3147         raw_write(env, ri, value & 0xfffff1ff);
3148     }
3149 }
3150 
3151 #ifndef CONFIG_USER_ONLY
3152 /* get_phys_addr() isn't present for user-mode-only targets */
3153 
3154 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3155                                  bool isread)
3156 {
3157     if (ri->opc2 & 4) {
3158         /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3159          * Secure EL1 (which can only happen if EL3 is AArch64).
3160          * They are simply UNDEF if executed from NS EL1.
3161          * They function normally from EL2 or EL3.
3162          */
3163         if (arm_current_el(env) == 1) {
3164             if (arm_is_secure_below_el3(env)) {
3165                 if (env->cp15.scr_el3 & SCR_EEL2) {
3166                     return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3167                 }
3168                 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3169             }
3170             return CP_ACCESS_TRAP_UNCATEGORIZED;
3171         }
3172     }
3173     return CP_ACCESS_OK;
3174 }
3175 
3176 #ifdef CONFIG_TCG
3177 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3178                              MMUAccessType access_type, ARMMMUIdx mmu_idx)
3179 {
3180     hwaddr phys_addr;
3181     target_ulong page_size;
3182     int prot;
3183     bool ret;
3184     uint64_t par64;
3185     bool format64 = false;
3186     MemTxAttrs attrs = {};
3187     ARMMMUFaultInfo fi = {};
3188     ARMCacheAttrs cacheattrs = {};
3189 
3190     ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
3191                         &prot, &page_size, &fi, &cacheattrs);
3192 
3193     if (ret) {
3194         /*
3195          * Some kinds of translation fault must cause exceptions rather
3196          * than being reported in the PAR.
3197          */
3198         int current_el = arm_current_el(env);
3199         int target_el;
3200         uint32_t syn, fsr, fsc;
3201         bool take_exc = false;
3202 
3203         if (fi.s1ptw && current_el == 1
3204             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3205             /*
3206              * Synchronous stage 2 fault on an access made as part of the
3207              * translation table walk for AT S1E0* or AT S1E1* insn
3208              * executed from NS EL1. If this is a synchronous external abort
3209              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3210              * to EL3. Otherwise the fault is taken as an exception to EL2,
3211              * and HPFAR_EL2 holds the faulting IPA.
3212              */
3213             if (fi.type == ARMFault_SyncExternalOnWalk &&
3214                 (env->cp15.scr_el3 & SCR_EA)) {
3215                 target_el = 3;
3216             } else {
3217                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3218                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3219                     env->cp15.hpfar_el2 |= HPFAR_NS;
3220                 }
3221                 target_el = 2;
3222             }
3223             take_exc = true;
3224         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3225             /*
3226              * Synchronous external aborts during a translation table walk
3227              * are taken as Data Abort exceptions.
3228              */
3229             if (fi.stage2) {
3230                 if (current_el == 3) {
3231                     target_el = 3;
3232                 } else {
3233                     target_el = 2;
3234                 }
3235             } else {
3236                 target_el = exception_target_el(env);
3237             }
3238             take_exc = true;
3239         }
3240 
3241         if (take_exc) {
3242             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3243             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3244                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3245                 fsr = arm_fi_to_lfsc(&fi);
3246                 fsc = extract32(fsr, 0, 6);
3247             } else {
3248                 fsr = arm_fi_to_sfsc(&fi);
3249                 fsc = 0x3f;
3250             }
3251             /*
3252              * Report exception with ESR indicating a fault due to a
3253              * translation table walk for a cache maintenance instruction.
3254              */
3255             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3256                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3257             env->exception.vaddress = value;
3258             env->exception.fsr = fsr;
3259             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3260         }
3261     }
3262 
3263     if (is_a64(env)) {
3264         format64 = true;
3265     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3266         /*
3267          * ATS1Cxx:
3268          * * TTBCR.EAE determines whether the result is returned using the
3269          *   32-bit or the 64-bit PAR format
3270          * * Instructions executed in Hyp mode always use the 64bit format
3271          *
3272          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3273          * * The Non-secure TTBCR.EAE bit is set to 1
3274          * * The implementation includes EL2, and the value of HCR.VM is 1
3275          *
3276          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3277          *
3278          * ATS1Hx always uses the 64bit format.
3279          */
3280         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3281 
3282         if (arm_feature(env, ARM_FEATURE_EL2)) {
3283             if (mmu_idx == ARMMMUIdx_E10_0 ||
3284                 mmu_idx == ARMMMUIdx_E10_1 ||
3285                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3286                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3287             } else {
3288                 format64 |= arm_current_el(env) == 2;
3289             }
3290         }
3291     }
3292 
3293     if (format64) {
3294         /* Create a 64-bit PAR */
3295         par64 = (1 << 11); /* LPAE bit always set */
3296         if (!ret) {
3297             par64 |= phys_addr & ~0xfffULL;
3298             if (!attrs.secure) {
3299                 par64 |= (1 << 9); /* NS */
3300             }
3301             par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3302             par64 |= cacheattrs.shareability << 7; /* SH */
3303         } else {
3304             uint32_t fsr = arm_fi_to_lfsc(&fi);
3305 
3306             par64 |= 1; /* F */
3307             par64 |= (fsr & 0x3f) << 1; /* FS */
3308             if (fi.stage2) {
3309                 par64 |= (1 << 9); /* S */
3310             }
3311             if (fi.s1ptw) {
3312                 par64 |= (1 << 8); /* PTW */
3313             }
3314         }
3315     } else {
3316         /* fsr is a DFSR/IFSR value for the short descriptor
3317          * translation table format (with WnR always clear).
3318          * Convert it to a 32-bit PAR.
3319          */
3320         if (!ret) {
3321             /* We do not set any attribute bits in the PAR */
3322             if (page_size == (1 << 24)
3323                 && arm_feature(env, ARM_FEATURE_V7)) {
3324                 par64 = (phys_addr & 0xff000000) | (1 << 1);
3325             } else {
3326                 par64 = phys_addr & 0xfffff000;
3327             }
3328             if (!attrs.secure) {
3329                 par64 |= (1 << 9); /* NS */
3330             }
3331         } else {
3332             uint32_t fsr = arm_fi_to_sfsc(&fi);
3333 
3334             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3335                     ((fsr & 0xf) << 1) | 1;
3336         }
3337     }
3338     return par64;
3339 }
3340 #endif /* CONFIG_TCG */
3341 
3342 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3343 {
3344 #ifdef CONFIG_TCG
3345     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3346     uint64_t par64;
3347     ARMMMUIdx mmu_idx;
3348     int el = arm_current_el(env);
3349     bool secure = arm_is_secure_below_el3(env);
3350 
3351     switch (ri->opc2 & 6) {
3352     case 0:
3353         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3354         switch (el) {
3355         case 3:
3356             mmu_idx = ARMMMUIdx_SE3;
3357             break;
3358         case 2:
3359             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3360             /* fall through */
3361         case 1:
3362             if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3363                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3364                            : ARMMMUIdx_Stage1_E1_PAN);
3365             } else {
3366                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3367             }
3368             break;
3369         default:
3370             g_assert_not_reached();
3371         }
3372         break;
3373     case 2:
3374         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3375         switch (el) {
3376         case 3:
3377             mmu_idx = ARMMMUIdx_SE10_0;
3378             break;
3379         case 2:
3380             g_assert(!secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3381             mmu_idx = ARMMMUIdx_Stage1_E0;
3382             break;
3383         case 1:
3384             mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3385             break;
3386         default:
3387             g_assert_not_reached();
3388         }
3389         break;
3390     case 4:
3391         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3392         mmu_idx = ARMMMUIdx_E10_1;
3393         break;
3394     case 6:
3395         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3396         mmu_idx = ARMMMUIdx_E10_0;
3397         break;
3398     default:
3399         g_assert_not_reached();
3400     }
3401 
3402     par64 = do_ats_write(env, value, access_type, mmu_idx);
3403 
3404     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3405 #else
3406     /* Handled by hardware accelerator. */
3407     g_assert_not_reached();
3408 #endif /* CONFIG_TCG */
3409 }
3410 
3411 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3412                         uint64_t value)
3413 {
3414 #ifdef CONFIG_TCG
3415     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3416     uint64_t par64;
3417 
3418     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
3419 
3420     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3421 #else
3422     /* Handled by hardware accelerator. */
3423     g_assert_not_reached();
3424 #endif /* CONFIG_TCG */
3425 }
3426 
3427 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3428                                      bool isread)
3429 {
3430     if (arm_current_el(env) == 3 &&
3431         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3432         return CP_ACCESS_TRAP;
3433     }
3434     return CP_ACCESS_OK;
3435 }
3436 
3437 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3438                         uint64_t value)
3439 {
3440 #ifdef CONFIG_TCG
3441     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3442     ARMMMUIdx mmu_idx;
3443     int secure = arm_is_secure_below_el3(env);
3444 
3445     switch (ri->opc2 & 6) {
3446     case 0:
3447         switch (ri->opc1) {
3448         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3449             if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3450                 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3451                            : ARMMMUIdx_Stage1_E1_PAN);
3452             } else {
3453                 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3454             }
3455             break;
3456         case 4: /* AT S1E2R, AT S1E2W */
3457             mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
3458             break;
3459         case 6: /* AT S1E3R, AT S1E3W */
3460             mmu_idx = ARMMMUIdx_SE3;
3461             break;
3462         default:
3463             g_assert_not_reached();
3464         }
3465         break;
3466     case 2: /* AT S1E0R, AT S1E0W */
3467         mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3468         break;
3469     case 4: /* AT S12E1R, AT S12E1W */
3470         mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
3471         break;
3472     case 6: /* AT S12E0R, AT S12E0W */
3473         mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
3474         break;
3475     default:
3476         g_assert_not_reached();
3477     }
3478 
3479     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3480 #else
3481     /* Handled by hardware accelerator. */
3482     g_assert_not_reached();
3483 #endif /* CONFIG_TCG */
3484 }
3485 #endif
3486 
3487 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3488     { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3489       .access = PL1_RW, .resetvalue = 0,
3490       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3491                              offsetoflow32(CPUARMState, cp15.par_ns) },
3492       .writefn = par_write },
3493 #ifndef CONFIG_USER_ONLY
3494     /* This underdecoding is safe because the reginfo is NO_RAW. */
3495     { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3496       .access = PL1_W, .accessfn = ats_access,
3497       .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3498 #endif
3499     REGINFO_SENTINEL
3500 };
3501 
3502 /* Return basic MPU access permission bits.  */
3503 static uint32_t simple_mpu_ap_bits(uint32_t val)
3504 {
3505     uint32_t ret;
3506     uint32_t mask;
3507     int i;
3508     ret = 0;
3509     mask = 3;
3510     for (i = 0; i < 16; i += 2) {
3511         ret |= (val >> i) & mask;
3512         mask <<= 2;
3513     }
3514     return ret;
3515 }
3516 
3517 /* Pad basic MPU access permission bits to extended format.  */
3518 static uint32_t extended_mpu_ap_bits(uint32_t val)
3519 {
3520     uint32_t ret;
3521     uint32_t mask;
3522     int i;
3523     ret = 0;
3524     mask = 3;
3525     for (i = 0; i < 16; i += 2) {
3526         ret |= (val & mask) << i;
3527         mask <<= 2;
3528     }
3529     return ret;
3530 }
3531 
3532 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3533                                  uint64_t value)
3534 {
3535     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3536 }
3537 
3538 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3539 {
3540     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3541 }
3542 
3543 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3544                                  uint64_t value)
3545 {
3546     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3547 }
3548 
3549 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3550 {
3551     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3552 }
3553 
3554 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3555 {
3556     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3557 
3558     if (!u32p) {
3559         return 0;
3560     }
3561 
3562     u32p += env->pmsav7.rnr[M_REG_NS];
3563     return *u32p;
3564 }
3565 
3566 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3567                          uint64_t value)
3568 {
3569     ARMCPU *cpu = env_archcpu(env);
3570     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3571 
3572     if (!u32p) {
3573         return;
3574     }
3575 
3576     u32p += env->pmsav7.rnr[M_REG_NS];
3577     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3578     *u32p = value;
3579 }
3580 
3581 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3582                               uint64_t value)
3583 {
3584     ARMCPU *cpu = env_archcpu(env);
3585     uint32_t nrgs = cpu->pmsav7_dregion;
3586 
3587     if (value >= nrgs) {
3588         qemu_log_mask(LOG_GUEST_ERROR,
3589                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3590                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3591         return;
3592     }
3593 
3594     raw_write(env, ri, value);
3595 }
3596 
3597 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3598     /* Reset for all these registers is handled in arm_cpu_reset(),
3599      * because the PMSAv7 is also used by M-profile CPUs, which do
3600      * not register cpregs but still need the state to be reset.
3601      */
3602     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3603       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3604       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3605       .readfn = pmsav7_read, .writefn = pmsav7_write,
3606       .resetfn = arm_cp_reset_ignore },
3607     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3608       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3609       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3610       .readfn = pmsav7_read, .writefn = pmsav7_write,
3611       .resetfn = arm_cp_reset_ignore },
3612     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3613       .access = PL1_RW, .type = ARM_CP_NO_RAW,
3614       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3615       .readfn = pmsav7_read, .writefn = pmsav7_write,
3616       .resetfn = arm_cp_reset_ignore },
3617     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3618       .access = PL1_RW,
3619       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3620       .writefn = pmsav7_rgnr_write,
3621       .resetfn = arm_cp_reset_ignore },
3622     REGINFO_SENTINEL
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     REGINFO_SENTINEL
3674 };
3675 
3676 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3677                                  uint64_t value)
3678 {
3679     TCR *tcr = raw_ptr(env, ri);
3680     int maskshift = extract32(value, 0, 3);
3681 
3682     if (!arm_feature(env, ARM_FEATURE_V8)) {
3683         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3684             /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3685              * using Long-desciptor translation table format */
3686             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3687         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3688             /* In an implementation that includes the Security Extensions
3689              * TTBCR has additional fields PD0 [4] and PD1 [5] for
3690              * Short-descriptor translation table format.
3691              */
3692             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3693         } else {
3694             value &= TTBCR_N;
3695         }
3696     }
3697 
3698     /* Update the masks corresponding to the TCR bank being written
3699      * Note that we always calculate mask and base_mask, but
3700      * they are only used for short-descriptor tables (ie if EAE is 0);
3701      * for long-descriptor tables the TCR fields are used differently
3702      * and the mask and base_mask values are meaningless.
3703      */
3704     tcr->raw_tcr = value;
3705     tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3706     tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
3707 }
3708 
3709 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3710                              uint64_t value)
3711 {
3712     ARMCPU *cpu = env_archcpu(env);
3713     TCR *tcr = raw_ptr(env, ri);
3714 
3715     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3716         /* With LPAE the TTBCR could result in a change of ASID
3717          * via the TTBCR.A1 bit, so do a TLB flush.
3718          */
3719         tlb_flush(CPU(cpu));
3720     }
3721     /* Preserve the high half of TCR_EL1, set via TTBCR2.  */
3722     value = deposit64(tcr->raw_tcr, 0, 32, value);
3723     vmsa_ttbcr_raw_write(env, ri, value);
3724 }
3725 
3726 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3727 {
3728     TCR *tcr = raw_ptr(env, ri);
3729 
3730     /* Reset both the TCR as well as the masks corresponding to the bank of
3731      * the TCR being reset.
3732      */
3733     tcr->raw_tcr = 0;
3734     tcr->mask = 0;
3735     tcr->base_mask = 0xffffc000u;
3736 }
3737 
3738 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3739                                uint64_t value)
3740 {
3741     ARMCPU *cpu = env_archcpu(env);
3742     TCR *tcr = raw_ptr(env, ri);
3743 
3744     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3745     tlb_flush(CPU(cpu));
3746     tcr->raw_tcr = value;
3747 }
3748 
3749 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3750                             uint64_t value)
3751 {
3752     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
3753     if (cpreg_field_is_64bit(ri) &&
3754         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3755         ARMCPU *cpu = env_archcpu(env);
3756         tlb_flush(CPU(cpu));
3757     }
3758     raw_write(env, ri, value);
3759 }
3760 
3761 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3762                                     uint64_t value)
3763 {
3764     /*
3765      * If we are running with E2&0 regime, then an ASID is active.
3766      * Flush if that might be changing.  Note we're not checking
3767      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3768      * holds the active ASID, only checking the field that might.
3769      */
3770     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3771         (arm_hcr_el2_eff(env) & HCR_E2H)) {
3772         uint16_t mask = ARMMMUIdxBit_E20_2 |
3773                         ARMMMUIdxBit_E20_2_PAN |
3774                         ARMMMUIdxBit_E20_0;
3775 
3776         if (arm_is_secure_below_el3(env)) {
3777             mask >>= ARM_MMU_IDX_A_NS;
3778         }
3779 
3780         tlb_flush_by_mmuidx(env_cpu(env), mask);
3781     }
3782     raw_write(env, ri, value);
3783 }
3784 
3785 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3786                         uint64_t value)
3787 {
3788     ARMCPU *cpu = env_archcpu(env);
3789     CPUState *cs = CPU(cpu);
3790 
3791     /*
3792      * A change in VMID to the stage2 page table (Stage2) invalidates
3793      * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3794      */
3795     if (raw_read(env, ri) != value) {
3796         uint16_t mask = ARMMMUIdxBit_E10_1 |
3797                         ARMMMUIdxBit_E10_1_PAN |
3798                         ARMMMUIdxBit_E10_0;
3799 
3800         if (arm_is_secure_below_el3(env)) {
3801             mask >>= ARM_MMU_IDX_A_NS;
3802         }
3803 
3804         tlb_flush_by_mmuidx(cs, mask);
3805         raw_write(env, ri, value);
3806     }
3807 }
3808 
3809 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3810     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3811       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
3812       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3813                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3814     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3815       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3816       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3817                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3818     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3819       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3820       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3821                              offsetof(CPUARMState, cp15.dfar_ns) } },
3822     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3823       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3824       .access = PL1_RW, .accessfn = access_tvm_trvm,
3825       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3826       .resetvalue = 0, },
3827     REGINFO_SENTINEL
3828 };
3829 
3830 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3831     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3832       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3833       .access = PL1_RW, .accessfn = access_tvm_trvm,
3834       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3835     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3836       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3837       .access = PL1_RW, .accessfn = access_tvm_trvm,
3838       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3839       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3840                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
3841     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3842       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3843       .access = PL1_RW, .accessfn = access_tvm_trvm,
3844       .writefn = vmsa_ttbr_write, .resetvalue = 0,
3845       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3846                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
3847     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3848       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3849       .access = PL1_RW, .accessfn = access_tvm_trvm,
3850       .writefn = vmsa_tcr_el12_write,
3851       .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3852       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3853     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3854       .access = PL1_RW, .accessfn = access_tvm_trvm,
3855       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3856       .raw_writefn = vmsa_ttbcr_raw_write,
3857       /* No offsetoflow32 -- pass the entire TCR to writefn/raw_writefn. */
3858       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.tcr_el[3]),
3859                              offsetof(CPUARMState, cp15.tcr_el[1])} },
3860     REGINFO_SENTINEL
3861 };
3862 
3863 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3864  * qemu tlbs nor adjusting cached masks.
3865  */
3866 static const ARMCPRegInfo ttbcr2_reginfo = {
3867     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3868     .access = PL1_RW, .accessfn = access_tvm_trvm,
3869     .type = ARM_CP_ALIAS,
3870     .bank_fieldoffsets = {
3871         offsetofhigh32(CPUARMState, cp15.tcr_el[3].raw_tcr),
3872         offsetofhigh32(CPUARMState, cp15.tcr_el[1].raw_tcr),
3873     },
3874 };
3875 
3876 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3877                                 uint64_t value)
3878 {
3879     env->cp15.c15_ticonfig = value & 0xe7;
3880     /* The OS_TYPE bit in this register changes the reported CPUID! */
3881     env->cp15.c0_cpuid = (value & (1 << 5)) ?
3882         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3883 }
3884 
3885 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3886                                 uint64_t value)
3887 {
3888     env->cp15.c15_threadid = value & 0xffff;
3889 }
3890 
3891 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3892                            uint64_t value)
3893 {
3894     /* Wait-for-interrupt (deprecated) */
3895     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3896 }
3897 
3898 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3899                                   uint64_t value)
3900 {
3901     /* On OMAP there are registers indicating the max/min index of dcache lines
3902      * containing a dirty line; cache flush operations have to reset these.
3903      */
3904     env->cp15.c15_i_max = 0x000;
3905     env->cp15.c15_i_min = 0xff0;
3906 }
3907 
3908 static const ARMCPRegInfo omap_cp_reginfo[] = {
3909     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3910       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3911       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3912       .resetvalue = 0, },
3913     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3914       .access = PL1_RW, .type = ARM_CP_NOP },
3915     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3916       .access = PL1_RW,
3917       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3918       .writefn = omap_ticonfig_write },
3919     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3920       .access = PL1_RW,
3921       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3922     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3923       .access = PL1_RW, .resetvalue = 0xff0,
3924       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3925     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3926       .access = PL1_RW,
3927       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3928       .writefn = omap_threadid_write },
3929     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3930       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3931       .type = ARM_CP_NO_RAW,
3932       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3933     /* TODO: Peripheral port remap register:
3934      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3935      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3936      * when MMU is off.
3937      */
3938     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3939       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3940       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3941       .writefn = omap_cachemaint_write },
3942     { .name = "C9", .cp = 15, .crn = 9,
3943       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3944       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3945     REGINFO_SENTINEL
3946 };
3947 
3948 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3949                               uint64_t value)
3950 {
3951     env->cp15.c15_cpar = value & 0x3fff;
3952 }
3953 
3954 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3955     { .name = "XSCALE_CPAR",
3956       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3957       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3958       .writefn = xscale_cpar_write, },
3959     { .name = "XSCALE_AUXCR",
3960       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3961       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3962       .resetvalue = 0, },
3963     /* XScale specific cache-lockdown: since we have no cache we NOP these
3964      * and hope the guest does not really rely on cache behaviour.
3965      */
3966     { .name = "XSCALE_LOCK_ICACHE_LINE",
3967       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3968       .access = PL1_W, .type = ARM_CP_NOP },
3969     { .name = "XSCALE_UNLOCK_ICACHE",
3970       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3971       .access = PL1_W, .type = ARM_CP_NOP },
3972     { .name = "XSCALE_DCACHE_LOCK",
3973       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3974       .access = PL1_RW, .type = ARM_CP_NOP },
3975     { .name = "XSCALE_UNLOCK_DCACHE",
3976       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3977       .access = PL1_W, .type = ARM_CP_NOP },
3978     REGINFO_SENTINEL
3979 };
3980 
3981 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3982     /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3983      * implementation of this implementation-defined space.
3984      * Ideally this should eventually disappear in favour of actually
3985      * implementing the correct behaviour for all cores.
3986      */
3987     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3988       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3989       .access = PL1_RW,
3990       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3991       .resetvalue = 0 },
3992     REGINFO_SENTINEL
3993 };
3994 
3995 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3996     /* Cache status: RAZ because we have no cache so it's always clean */
3997     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3998       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3999       .resetvalue = 0 },
4000     REGINFO_SENTINEL
4001 };
4002 
4003 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4004     /* We never have a a block transfer operation in progress */
4005     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4006       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4007       .resetvalue = 0 },
4008     /* The cache ops themselves: these all NOP for QEMU */
4009     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4010       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4011     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4012       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4013     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4014       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4015     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4016       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4017     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4018       .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4019     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4020       .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4021     REGINFO_SENTINEL
4022 };
4023 
4024 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4025     /* The cache test-and-clean instructions always return (1 << 30)
4026      * to indicate that there are no dirty cache lines.
4027      */
4028     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4029       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4030       .resetvalue = (1 << 30) },
4031     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4032       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4033       .resetvalue = (1 << 30) },
4034     REGINFO_SENTINEL
4035 };
4036 
4037 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4038     /* Ignore ReadBuffer accesses */
4039     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4040       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4041       .access = PL1_RW, .resetvalue = 0,
4042       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4043     REGINFO_SENTINEL
4044 };
4045 
4046 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4047 {
4048     unsigned int cur_el = arm_current_el(env);
4049 
4050     if (arm_is_el2_enabled(env) && cur_el == 1) {
4051         return env->cp15.vpidr_el2;
4052     }
4053     return raw_read(env, ri);
4054 }
4055 
4056 static uint64_t mpidr_read_val(CPUARMState *env)
4057 {
4058     ARMCPU *cpu = env_archcpu(env);
4059     uint64_t mpidr = cpu->mp_affinity;
4060 
4061     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4062         mpidr |= (1U << 31);
4063         /* Cores which are uniprocessor (non-coherent)
4064          * but still implement the MP extensions set
4065          * bit 30. (For instance, Cortex-R5).
4066          */
4067         if (cpu->mp_is_up) {
4068             mpidr |= (1u << 30);
4069         }
4070     }
4071     return mpidr;
4072 }
4073 
4074 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4075 {
4076     unsigned int cur_el = arm_current_el(env);
4077 
4078     if (arm_is_el2_enabled(env) && cur_el == 1) {
4079         return env->cp15.vmpidr_el2;
4080     }
4081     return mpidr_read_val(env);
4082 }
4083 
4084 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4085     /* NOP AMAIR0/1 */
4086     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4087       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4088       .access = PL1_RW, .accessfn = access_tvm_trvm,
4089       .type = ARM_CP_CONST, .resetvalue = 0 },
4090     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4091     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4092       .access = PL1_RW, .accessfn = access_tvm_trvm,
4093       .type = ARM_CP_CONST, .resetvalue = 0 },
4094     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4095       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4096       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4097                              offsetof(CPUARMState, cp15.par_ns)} },
4098     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4099       .access = PL1_RW, .accessfn = access_tvm_trvm,
4100       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4101       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4102                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4103       .writefn = vmsa_ttbr_write, },
4104     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4105       .access = PL1_RW, .accessfn = access_tvm_trvm,
4106       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4107       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4108                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4109       .writefn = vmsa_ttbr_write, },
4110     REGINFO_SENTINEL
4111 };
4112 
4113 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4114 {
4115     return vfp_get_fpcr(env);
4116 }
4117 
4118 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4119                             uint64_t value)
4120 {
4121     vfp_set_fpcr(env, value);
4122 }
4123 
4124 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4125 {
4126     return vfp_get_fpsr(env);
4127 }
4128 
4129 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4130                             uint64_t value)
4131 {
4132     vfp_set_fpsr(env, value);
4133 }
4134 
4135 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4136                                        bool isread)
4137 {
4138     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4139         return CP_ACCESS_TRAP;
4140     }
4141     return CP_ACCESS_OK;
4142 }
4143 
4144 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4145                             uint64_t value)
4146 {
4147     env->daif = value & PSTATE_DAIF;
4148 }
4149 
4150 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4151 {
4152     return env->pstate & PSTATE_PAN;
4153 }
4154 
4155 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4156                            uint64_t value)
4157 {
4158     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4159 }
4160 
4161 static const ARMCPRegInfo pan_reginfo = {
4162     .name = "PAN", .state = ARM_CP_STATE_AA64,
4163     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4164     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4165     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4166 };
4167 
4168 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4169 {
4170     return env->pstate & PSTATE_UAO;
4171 }
4172 
4173 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4174                            uint64_t value)
4175 {
4176     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4177 }
4178 
4179 static const ARMCPRegInfo uao_reginfo = {
4180     .name = "UAO", .state = ARM_CP_STATE_AA64,
4181     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4182     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4183     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4184 };
4185 
4186 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4187 {
4188     return env->pstate & PSTATE_DIT;
4189 }
4190 
4191 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4192                            uint64_t value)
4193 {
4194     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4195 }
4196 
4197 static const ARMCPRegInfo dit_reginfo = {
4198     .name = "DIT", .state = ARM_CP_STATE_AA64,
4199     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4200     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4201     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4202 };
4203 
4204 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4205 {
4206     return env->pstate & PSTATE_SSBS;
4207 }
4208 
4209 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4210                            uint64_t value)
4211 {
4212     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4213 }
4214 
4215 static const ARMCPRegInfo ssbs_reginfo = {
4216     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4217     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4218     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4219     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4220 };
4221 
4222 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4223                                               const ARMCPRegInfo *ri,
4224                                               bool isread)
4225 {
4226     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4227     switch (arm_current_el(env)) {
4228     case 0:
4229         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4230         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4231             return CP_ACCESS_TRAP;
4232         }
4233         /* fall through */
4234     case 1:
4235         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4236         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4237             return CP_ACCESS_TRAP_EL2;
4238         }
4239         break;
4240     }
4241     return CP_ACCESS_OK;
4242 }
4243 
4244 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4245                                               const ARMCPRegInfo *ri,
4246                                               bool isread)
4247 {
4248     /* Cache invalidate/clean to Point of Unification... */
4249     switch (arm_current_el(env)) {
4250     case 0:
4251         /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set.  */
4252         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4253             return CP_ACCESS_TRAP;
4254         }
4255         /* fall through */
4256     case 1:
4257         /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set.  */
4258         if (arm_hcr_el2_eff(env) & HCR_TPU) {
4259             return CP_ACCESS_TRAP_EL2;
4260         }
4261         break;
4262     }
4263     return CP_ACCESS_OK;
4264 }
4265 
4266 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4267  * Page D4-1736 (DDI0487A.b)
4268  */
4269 
4270 static int vae1_tlbmask(CPUARMState *env)
4271 {
4272     uint64_t hcr = arm_hcr_el2_eff(env);
4273     uint16_t mask;
4274 
4275     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4276         mask = ARMMMUIdxBit_E20_2 |
4277                ARMMMUIdxBit_E20_2_PAN |
4278                ARMMMUIdxBit_E20_0;
4279     } else {
4280         mask = ARMMMUIdxBit_E10_1 |
4281                ARMMMUIdxBit_E10_1_PAN |
4282                ARMMMUIdxBit_E10_0;
4283     }
4284 
4285     if (arm_is_secure_below_el3(env)) {
4286         mask >>= ARM_MMU_IDX_A_NS;
4287     }
4288 
4289     return mask;
4290 }
4291 
4292 /* Return 56 if TBI is enabled, 64 otherwise. */
4293 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4294                               uint64_t addr)
4295 {
4296     uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
4297     int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4298     int select = extract64(addr, 55, 1);
4299 
4300     return (tbi >> select) & 1 ? 56 : 64;
4301 }
4302 
4303 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4304 {
4305     uint64_t hcr = arm_hcr_el2_eff(env);
4306     ARMMMUIdx mmu_idx;
4307 
4308     /* Only the regime of the mmu_idx below is significant. */
4309     if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4310         mmu_idx = ARMMMUIdx_E20_0;
4311     } else {
4312         mmu_idx = ARMMMUIdx_E10_0;
4313     }
4314 
4315     if (arm_is_secure_below_el3(env)) {
4316         mmu_idx &= ~ARM_MMU_IDX_A_NS;
4317     }
4318 
4319     return tlbbits_for_regime(env, mmu_idx, addr);
4320 }
4321 
4322 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4323                                       uint64_t value)
4324 {
4325     CPUState *cs = env_cpu(env);
4326     int mask = vae1_tlbmask(env);
4327 
4328     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4329 }
4330 
4331 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4332                                     uint64_t value)
4333 {
4334     CPUState *cs = env_cpu(env);
4335     int mask = vae1_tlbmask(env);
4336 
4337     if (tlb_force_broadcast(env)) {
4338         tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4339     } else {
4340         tlb_flush_by_mmuidx(cs, mask);
4341     }
4342 }
4343 
4344 static int alle1_tlbmask(CPUARMState *env)
4345 {
4346     /*
4347      * Note that the 'ALL' scope must invalidate both stage 1 and
4348      * stage 2 translations, whereas most other scopes only invalidate
4349      * stage 1 translations.
4350      */
4351     if (arm_is_secure_below_el3(env)) {
4352         return ARMMMUIdxBit_SE10_1 |
4353                ARMMMUIdxBit_SE10_1_PAN |
4354                ARMMMUIdxBit_SE10_0;
4355     } else {
4356         return ARMMMUIdxBit_E10_1 |
4357                ARMMMUIdxBit_E10_1_PAN |
4358                ARMMMUIdxBit_E10_0;
4359     }
4360 }
4361 
4362 static int e2_tlbmask(CPUARMState *env)
4363 {
4364     if (arm_is_secure_below_el3(env)) {
4365         return ARMMMUIdxBit_SE20_0 |
4366                ARMMMUIdxBit_SE20_2 |
4367                ARMMMUIdxBit_SE20_2_PAN |
4368                ARMMMUIdxBit_SE2;
4369     } else {
4370         return ARMMMUIdxBit_E20_0 |
4371                ARMMMUIdxBit_E20_2 |
4372                ARMMMUIdxBit_E20_2_PAN |
4373                ARMMMUIdxBit_E2;
4374     }
4375 }
4376 
4377 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4378                                   uint64_t value)
4379 {
4380     CPUState *cs = env_cpu(env);
4381     int mask = alle1_tlbmask(env);
4382 
4383     tlb_flush_by_mmuidx(cs, mask);
4384 }
4385 
4386 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4387                                   uint64_t value)
4388 {
4389     CPUState *cs = env_cpu(env);
4390     int mask = e2_tlbmask(env);
4391 
4392     tlb_flush_by_mmuidx(cs, mask);
4393 }
4394 
4395 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4396                                   uint64_t value)
4397 {
4398     ARMCPU *cpu = env_archcpu(env);
4399     CPUState *cs = CPU(cpu);
4400 
4401     tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
4402 }
4403 
4404 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4405                                     uint64_t value)
4406 {
4407     CPUState *cs = env_cpu(env);
4408     int mask = alle1_tlbmask(env);
4409 
4410     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4411 }
4412 
4413 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4414                                     uint64_t value)
4415 {
4416     CPUState *cs = env_cpu(env);
4417     int mask = e2_tlbmask(env);
4418 
4419     tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4420 }
4421 
4422 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4423                                     uint64_t value)
4424 {
4425     CPUState *cs = env_cpu(env);
4426 
4427     tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
4428 }
4429 
4430 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4431                                  uint64_t value)
4432 {
4433     /* Invalidate by VA, EL2
4434      * Currently handles both VAE2 and VALE2, since we don't support
4435      * flush-last-level-only.
4436      */
4437     CPUState *cs = env_cpu(env);
4438     int mask = e2_tlbmask(env);
4439     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4440 
4441     tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4442 }
4443 
4444 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4445                                  uint64_t value)
4446 {
4447     /* Invalidate by VA, EL3
4448      * Currently handles both VAE3 and VALE3, since we don't support
4449      * flush-last-level-only.
4450      */
4451     ARMCPU *cpu = env_archcpu(env);
4452     CPUState *cs = CPU(cpu);
4453     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4454 
4455     tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
4456 }
4457 
4458 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4459                                    uint64_t value)
4460 {
4461     CPUState *cs = env_cpu(env);
4462     int mask = vae1_tlbmask(env);
4463     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4464     int bits = vae1_tlbbits(env, pageaddr);
4465 
4466     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4467 }
4468 
4469 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4470                                  uint64_t value)
4471 {
4472     /* Invalidate by VA, EL1&0 (AArch64 version).
4473      * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4474      * since we don't support flush-for-specific-ASID-only or
4475      * flush-last-level-only.
4476      */
4477     CPUState *cs = env_cpu(env);
4478     int mask = vae1_tlbmask(env);
4479     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4480     int bits = vae1_tlbbits(env, pageaddr);
4481 
4482     if (tlb_force_broadcast(env)) {
4483         tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4484     } else {
4485         tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4486     }
4487 }
4488 
4489 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4490                                    uint64_t value)
4491 {
4492     CPUState *cs = env_cpu(env);
4493     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4494     bool secure = arm_is_secure_below_el3(env);
4495     int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
4496     int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
4497                                   pageaddr);
4498 
4499     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4500 }
4501 
4502 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4503                                    uint64_t value)
4504 {
4505     CPUState *cs = env_cpu(env);
4506     uint64_t pageaddr = sextract64(value << 12, 0, 56);
4507     int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
4508 
4509     tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4510                                                   ARMMMUIdxBit_SE3, bits);
4511 }
4512 
4513 #ifdef TARGET_AARCH64
4514 static uint64_t tlbi_aa64_range_get_length(CPUARMState *env,
4515                                            uint64_t value)
4516 {
4517     unsigned int page_shift;
4518     unsigned int page_size_granule;
4519     uint64_t num;
4520     uint64_t scale;
4521     uint64_t exponent;
4522     uint64_t length;
4523 
4524     num = extract64(value, 39, 5);
4525     scale = extract64(value, 44, 2);
4526     page_size_granule = extract64(value, 46, 2);
4527 
4528     if (page_size_granule == 0) {
4529         qemu_log_mask(LOG_GUEST_ERROR, "Invalid page size granule %d\n",
4530                       page_size_granule);
4531         return 0;
4532     }
4533 
4534     page_shift = (page_size_granule - 1) * 2 + 12;
4535 
4536     exponent = (5 * scale) + 1;
4537     length = (num + 1) << (exponent + page_shift);
4538 
4539     return length;
4540 }
4541 
4542 static uint64_t tlbi_aa64_range_get_base(CPUARMState *env, uint64_t value,
4543                                         bool two_ranges)
4544 {
4545     /* TODO: ARMv8.7 FEAT_LPA2 */
4546     uint64_t pageaddr;
4547 
4548     if (two_ranges) {
4549         pageaddr = sextract64(value, 0, 37) << TARGET_PAGE_BITS;
4550     } else {
4551         pageaddr = extract64(value, 0, 37) << TARGET_PAGE_BITS;
4552     }
4553 
4554     return pageaddr;
4555 }
4556 
4557 static void do_rvae_write(CPUARMState *env, uint64_t value,
4558                           int idxmap, bool synced)
4559 {
4560     ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4561     bool two_ranges = regime_has_2_ranges(one_idx);
4562     uint64_t baseaddr, length;
4563     int bits;
4564 
4565     baseaddr = tlbi_aa64_range_get_base(env, value, two_ranges);
4566     length = tlbi_aa64_range_get_length(env, value);
4567     bits = tlbbits_for_regime(env, one_idx, baseaddr);
4568 
4569     if (synced) {
4570         tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4571                                                   baseaddr,
4572                                                   length,
4573                                                   idxmap,
4574                                                   bits);
4575     } else {
4576         tlb_flush_range_by_mmuidx(env_cpu(env), baseaddr,
4577                                   length, idxmap, bits);
4578     }
4579 }
4580 
4581 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4582                                   const ARMCPRegInfo *ri,
4583                                   uint64_t value)
4584 {
4585     /*
4586      * Invalidate by VA range, EL1&0.
4587      * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4588      * since we don't support flush-for-specific-ASID-only or
4589      * flush-last-level-only.
4590      */
4591 
4592     do_rvae_write(env, value, vae1_tlbmask(env),
4593                   tlb_force_broadcast(env));
4594 }
4595 
4596 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4597                                     const ARMCPRegInfo *ri,
4598                                     uint64_t value)
4599 {
4600     /*
4601      * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4602      * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4603      * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4604      * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4605      * shareable specific flushes.
4606      */
4607 
4608     do_rvae_write(env, value, vae1_tlbmask(env), true);
4609 }
4610 
4611 static int vae2_tlbmask(CPUARMState *env)
4612 {
4613     return (arm_is_secure_below_el3(env)
4614             ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4615 }
4616 
4617 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4618                                   const ARMCPRegInfo *ri,
4619                                   uint64_t value)
4620 {
4621     /*
4622      * Invalidate by VA range, EL2.
4623      * Currently handles all of RVAE2 and RVALE2,
4624      * since we don't support flush-for-specific-ASID-only or
4625      * flush-last-level-only.
4626      */
4627 
4628     do_rvae_write(env, value, vae2_tlbmask(env),
4629                   tlb_force_broadcast(env));
4630 
4631 
4632 }
4633 
4634 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4635                                     const ARMCPRegInfo *ri,
4636                                     uint64_t value)
4637 {
4638     /*
4639      * Invalidate by VA range, Inner/Outer Shareable, EL2.
4640      * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4641      * since we don't support flush-for-specific-ASID-only,
4642      * flush-last-level-only or inner/outer shareable specific flushes.
4643      */
4644 
4645     do_rvae_write(env, value, vae2_tlbmask(env), true);
4646 
4647 }
4648 
4649 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4650                                   const ARMCPRegInfo *ri,
4651                                   uint64_t value)
4652 {
4653     /*
4654      * Invalidate by VA range, EL3.
4655      * Currently handles all of RVAE3 and RVALE3,
4656      * since we don't support flush-for-specific-ASID-only or
4657      * flush-last-level-only.
4658      */
4659 
4660     do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4661                   tlb_force_broadcast(env));
4662 }
4663 
4664 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4665                                     const ARMCPRegInfo *ri,
4666                                     uint64_t value)
4667 {
4668     /*
4669      * Invalidate by VA range, EL3, Inner/Outer Shareable.
4670      * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4671      * since we don't support flush-for-specific-ASID-only,
4672      * flush-last-level-only or inner/outer specific flushes.
4673      */
4674 
4675     do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4676 }
4677 #endif
4678 
4679 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4680                                       bool isread)
4681 {
4682     int cur_el = arm_current_el(env);
4683 
4684     if (cur_el < 2) {
4685         uint64_t hcr = arm_hcr_el2_eff(env);
4686 
4687         if (cur_el == 0) {
4688             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4689                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4690                     return CP_ACCESS_TRAP_EL2;
4691                 }
4692             } else {
4693                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4694                     return CP_ACCESS_TRAP;
4695                 }
4696                 if (hcr & HCR_TDZ) {
4697                     return CP_ACCESS_TRAP_EL2;
4698                 }
4699             }
4700         } else if (hcr & HCR_TDZ) {
4701             return CP_ACCESS_TRAP_EL2;
4702         }
4703     }
4704     return CP_ACCESS_OK;
4705 }
4706 
4707 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4708 {
4709     ARMCPU *cpu = env_archcpu(env);
4710     int dzp_bit = 1 << 4;
4711 
4712     /* DZP indicates whether DC ZVA access is allowed */
4713     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4714         dzp_bit = 0;
4715     }
4716     return cpu->dcz_blocksize | dzp_bit;
4717 }
4718 
4719 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4720                                     bool isread)
4721 {
4722     if (!(env->pstate & PSTATE_SP)) {
4723         /* Access to SP_EL0 is undefined if it's being used as
4724          * the stack pointer.
4725          */
4726         return CP_ACCESS_TRAP_UNCATEGORIZED;
4727     }
4728     return CP_ACCESS_OK;
4729 }
4730 
4731 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4732 {
4733     return env->pstate & PSTATE_SP;
4734 }
4735 
4736 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4737 {
4738     update_spsel(env, val);
4739 }
4740 
4741 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4742                         uint64_t value)
4743 {
4744     ARMCPU *cpu = env_archcpu(env);
4745 
4746     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4747         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4748         value &= ~SCTLR_M;
4749     }
4750 
4751     /* ??? Lots of these bits are not implemented.  */
4752 
4753     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4754         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4755             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4756         } else {
4757             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4758                        SCTLR_ATA0 | SCTLR_ATA);
4759         }
4760     }
4761 
4762     if (raw_read(env, ri) == value) {
4763         /* Skip the TLB flush if nothing actually changed; Linux likes
4764          * to do a lot of pointless SCTLR writes.
4765          */
4766         return;
4767     }
4768 
4769     raw_write(env, ri, value);
4770 
4771     /* This may enable/disable the MMU, so do a TLB flush.  */
4772     tlb_flush(CPU(cpu));
4773 
4774     if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4775         /*
4776          * Normally we would always end the TB on an SCTLR write; see the
4777          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4778          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4779          * of hflags from the translator, so do it here.
4780          */
4781         arm_rebuild_hflags(env);
4782     }
4783 }
4784 
4785 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4786                                      bool isread)
4787 {
4788     if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
4789         return CP_ACCESS_TRAP_FP_EL2;
4790     }
4791     if (env->cp15.cptr_el[3] & CPTR_TFP) {
4792         return CP_ACCESS_TRAP_FP_EL3;
4793     }
4794     return CP_ACCESS_OK;
4795 }
4796 
4797 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4798                        uint64_t value)
4799 {
4800     env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4801 }
4802 
4803 static const ARMCPRegInfo v8_cp_reginfo[] = {
4804     /* Minimal set of EL0-visible registers. This will need to be expanded
4805      * significantly for system emulation of AArch64 CPUs.
4806      */
4807     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4808       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4809       .access = PL0_RW, .type = ARM_CP_NZCV },
4810     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4811       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4812       .type = ARM_CP_NO_RAW,
4813       .access = PL0_RW, .accessfn = aa64_daif_access,
4814       .fieldoffset = offsetof(CPUARMState, daif),
4815       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4816     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4817       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4818       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4819       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4820     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4821       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4822       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4823       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4824     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4825       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4826       .access = PL0_R, .type = ARM_CP_NO_RAW,
4827       .readfn = aa64_dczid_read },
4828     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4829       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4830       .access = PL0_W, .type = ARM_CP_DC_ZVA,
4831 #ifndef CONFIG_USER_ONLY
4832       /* Avoid overhead of an access check that always passes in user-mode */
4833       .accessfn = aa64_zva_access,
4834 #endif
4835     },
4836     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4837       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4838       .access = PL1_R, .type = ARM_CP_CURRENTEL },
4839     /* Cache ops: all NOPs since we don't emulate caches */
4840     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4841       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4842       .access = PL1_W, .type = ARM_CP_NOP,
4843       .accessfn = aa64_cacheop_pou_access },
4844     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4845       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4846       .access = PL1_W, .type = ARM_CP_NOP,
4847       .accessfn = aa64_cacheop_pou_access },
4848     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4849       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4850       .access = PL0_W, .type = ARM_CP_NOP,
4851       .accessfn = aa64_cacheop_pou_access },
4852     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4853       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4854       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4855       .type = ARM_CP_NOP },
4856     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4857       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4858       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4859     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4860       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4861       .access = PL0_W, .type = ARM_CP_NOP,
4862       .accessfn = aa64_cacheop_poc_access },
4863     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4864       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4865       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4866     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4867       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4868       .access = PL0_W, .type = ARM_CP_NOP,
4869       .accessfn = aa64_cacheop_pou_access },
4870     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4871       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4872       .access = PL0_W, .type = ARM_CP_NOP,
4873       .accessfn = aa64_cacheop_poc_access },
4874     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4875       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4876       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4877     /* TLBI operations */
4878     { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4879       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4880       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4881       .writefn = tlbi_aa64_vmalle1is_write },
4882     { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4883       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4884       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4885       .writefn = tlbi_aa64_vae1is_write },
4886     { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4887       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4888       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4889       .writefn = tlbi_aa64_vmalle1is_write },
4890     { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4891       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4892       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4893       .writefn = tlbi_aa64_vae1is_write },
4894     { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4895       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4896       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4897       .writefn = tlbi_aa64_vae1is_write },
4898     { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4899       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4900       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4901       .writefn = tlbi_aa64_vae1is_write },
4902     { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4903       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4904       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4905       .writefn = tlbi_aa64_vmalle1_write },
4906     { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4907       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4908       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4909       .writefn = tlbi_aa64_vae1_write },
4910     { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4911       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4912       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4913       .writefn = tlbi_aa64_vmalle1_write },
4914     { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4915       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4916       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4917       .writefn = tlbi_aa64_vae1_write },
4918     { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4919       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4920       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4921       .writefn = tlbi_aa64_vae1_write },
4922     { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4923       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4924       .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4925       .writefn = tlbi_aa64_vae1_write },
4926     { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4927       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4928       .access = PL2_W, .type = ARM_CP_NOP },
4929     { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4930       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4931       .access = PL2_W, .type = ARM_CP_NOP },
4932     { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4933       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4934       .access = PL2_W, .type = ARM_CP_NO_RAW,
4935       .writefn = tlbi_aa64_alle1is_write },
4936     { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4937       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4938       .access = PL2_W, .type = ARM_CP_NO_RAW,
4939       .writefn = tlbi_aa64_alle1is_write },
4940     { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4941       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4942       .access = PL2_W, .type = ARM_CP_NOP },
4943     { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4944       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4945       .access = PL2_W, .type = ARM_CP_NOP },
4946     { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4947       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4948       .access = PL2_W, .type = ARM_CP_NO_RAW,
4949       .writefn = tlbi_aa64_alle1_write },
4950     { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4951       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4952       .access = PL2_W, .type = ARM_CP_NO_RAW,
4953       .writefn = tlbi_aa64_alle1is_write },
4954 #ifndef CONFIG_USER_ONLY
4955     /* 64 bit address translation operations */
4956     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4957       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4958       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4959       .writefn = ats_write64 },
4960     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4961       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4962       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4963       .writefn = ats_write64 },
4964     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4965       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4966       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4967       .writefn = ats_write64 },
4968     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4969       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4970       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4971       .writefn = ats_write64 },
4972     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4973       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4974       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4975       .writefn = ats_write64 },
4976     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4977       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4978       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4979       .writefn = ats_write64 },
4980     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4981       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4982       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4983       .writefn = ats_write64 },
4984     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4985       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4986       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4987       .writefn = ats_write64 },
4988     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4989     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4990       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4991       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4992       .writefn = ats_write64 },
4993     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4994       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4995       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4996       .writefn = ats_write64 },
4997     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4998       .type = ARM_CP_ALIAS,
4999       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5000       .access = PL1_RW, .resetvalue = 0,
5001       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5002       .writefn = par_write },
5003 #endif
5004     /* TLB invalidate last level of translation table walk */
5005     { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5006       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5007       .writefn = tlbimva_is_write },
5008     { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5009       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5010       .writefn = tlbimvaa_is_write },
5011     { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5012       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5013       .writefn = tlbimva_write },
5014     { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5015       .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5016       .writefn = tlbimvaa_write },
5017     { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5018       .type = ARM_CP_NO_RAW, .access = PL2_W,
5019       .writefn = tlbimva_hyp_write },
5020     { .name = "TLBIMVALHIS",
5021       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5022       .type = ARM_CP_NO_RAW, .access = PL2_W,
5023       .writefn = tlbimva_hyp_is_write },
5024     { .name = "TLBIIPAS2",
5025       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5026       .type = ARM_CP_NOP, .access = PL2_W },
5027     { .name = "TLBIIPAS2IS",
5028       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5029       .type = ARM_CP_NOP, .access = PL2_W },
5030     { .name = "TLBIIPAS2L",
5031       .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5032       .type = ARM_CP_NOP, .access = PL2_W },
5033     { .name = "TLBIIPAS2LIS",
5034       .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5035       .type = ARM_CP_NOP, .access = PL2_W },
5036     /* 32 bit cache operations */
5037     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5038       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5039     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5040       .type = ARM_CP_NOP, .access = PL1_W },
5041     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5042       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5043     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5044       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5045     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5046       .type = ARM_CP_NOP, .access = PL1_W },
5047     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5048       .type = ARM_CP_NOP, .access = PL1_W },
5049     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5050       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5051     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5052       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5053     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5054       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5055     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5056       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5057     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5058       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5059     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5060       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5061     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5062       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5063     /* MMU Domain access control / MPU write buffer control */
5064     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5065       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5066       .writefn = dacr_write, .raw_writefn = raw_write,
5067       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5068                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5069     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5070       .type = ARM_CP_ALIAS,
5071       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5072       .access = PL1_RW,
5073       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5074     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5075       .type = ARM_CP_ALIAS,
5076       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5077       .access = PL1_RW,
5078       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5079     /* We rely on the access checks not allowing the guest to write to the
5080      * state field when SPSel indicates that it's being used as the stack
5081      * pointer.
5082      */
5083     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5084       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5085       .access = PL1_RW, .accessfn = sp_el0_access,
5086       .type = ARM_CP_ALIAS,
5087       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5088     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5089       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5090       .access = PL2_RW, .type = ARM_CP_ALIAS,
5091       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5092     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5093       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5094       .type = ARM_CP_NO_RAW,
5095       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5096     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5097       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5098       .type = ARM_CP_ALIAS,
5099       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
5100       .access = PL2_RW, .accessfn = fpexc32_access },
5101     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5102       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5103       .access = PL2_RW, .resetvalue = 0,
5104       .writefn = dacr_write, .raw_writefn = raw_write,
5105       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5106     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5107       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5108       .access = PL2_RW, .resetvalue = 0,
5109       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5110     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5111       .type = ARM_CP_ALIAS,
5112       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5113       .access = PL2_RW,
5114       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5115     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5116       .type = ARM_CP_ALIAS,
5117       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5118       .access = PL2_RW,
5119       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5120     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5121       .type = ARM_CP_ALIAS,
5122       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5123       .access = PL2_RW,
5124       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5125     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5126       .type = ARM_CP_ALIAS,
5127       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5128       .access = PL2_RW,
5129       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5130     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5131       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5132       .resetvalue = 0,
5133       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5134     { .name = "SDCR", .type = ARM_CP_ALIAS,
5135       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5136       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5137       .writefn = sdcr_write,
5138       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5139     REGINFO_SENTINEL
5140 };
5141 
5142 /* Used to describe the behaviour of EL2 regs when EL2 does not exist.  */
5143 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
5144     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5145       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5146       .access = PL2_RW,
5147       .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
5148     { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
5149       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5150       .access = PL2_RW,
5151       .type = ARM_CP_CONST, .resetvalue = 0 },
5152     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5153       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5154       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5155     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5156       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5157       .access = PL2_RW,
5158       .type = ARM_CP_CONST, .resetvalue = 0 },
5159     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5160       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5161       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5162     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5163       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5164       .access = PL2_RW, .type = ARM_CP_CONST,
5165       .resetvalue = 0 },
5166     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5167       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5168       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5169     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5170       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5171       .access = PL2_RW, .type = ARM_CP_CONST,
5172       .resetvalue = 0 },
5173     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5174       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5175       .access = PL2_RW, .type = ARM_CP_CONST,
5176       .resetvalue = 0 },
5177     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5178       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5179       .access = PL2_RW, .type = ARM_CP_CONST,
5180       .resetvalue = 0 },
5181     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5182       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5183       .access = PL2_RW, .type = ARM_CP_CONST,
5184       .resetvalue = 0 },
5185     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5186       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5187       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5188     { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
5189       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5190       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5191       .type = ARM_CP_CONST, .resetvalue = 0 },
5192     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5193       .cp = 15, .opc1 = 6, .crm = 2,
5194       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5195       .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
5196     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5197       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5198       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5199     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5200       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5201       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5202     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5203       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5204       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5205     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5206       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5207       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5208     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5209       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5210       .resetvalue = 0 },
5211     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5212       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5213       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5214     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5215       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5216       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5217     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5218       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5219       .resetvalue = 0 },
5220     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5221       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5222       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5223     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5224       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5225       .resetvalue = 0 },
5226     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5227       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5228       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5229     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5230       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5231       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5232     { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5233       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5234       .access = PL2_RW, .accessfn = access_tda,
5235       .type = ARM_CP_CONST, .resetvalue = 0 },
5236     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
5237       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5238       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5239       .type = ARM_CP_CONST, .resetvalue = 0 },
5240     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5241       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5242       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5243     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5244       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5245       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5246     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5247       .type = ARM_CP_CONST,
5248       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5249       .access = PL2_RW, .resetvalue = 0 },
5250     REGINFO_SENTINEL
5251 };
5252 
5253 /* Ditto, but for registers which exist in ARMv8 but not v7 */
5254 static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
5255     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5256       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5257       .access = PL2_RW,
5258       .type = ARM_CP_CONST, .resetvalue = 0 },
5259     REGINFO_SENTINEL
5260 };
5261 
5262 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5263 {
5264     ARMCPU *cpu = env_archcpu(env);
5265 
5266     if (arm_feature(env, ARM_FEATURE_V8)) {
5267         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5268     } else {
5269         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5270     }
5271 
5272     if (arm_feature(env, ARM_FEATURE_EL3)) {
5273         valid_mask &= ~HCR_HCD;
5274     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5275         /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5276          * However, if we're using the SMC PSCI conduit then QEMU is
5277          * effectively acting like EL3 firmware and so the guest at
5278          * EL2 should retain the ability to prevent EL1 from being
5279          * able to make SMC calls into the ersatz firmware, so in
5280          * that case HCR.TSC should be read/write.
5281          */
5282         valid_mask &= ~HCR_TSC;
5283     }
5284 
5285     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5286         if (cpu_isar_feature(aa64_vh, cpu)) {
5287             valid_mask |= HCR_E2H;
5288         }
5289         if (cpu_isar_feature(aa64_lor, cpu)) {
5290             valid_mask |= HCR_TLOR;
5291         }
5292         if (cpu_isar_feature(aa64_pauth, cpu)) {
5293             valid_mask |= HCR_API | HCR_APK;
5294         }
5295         if (cpu_isar_feature(aa64_mte, cpu)) {
5296             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5297         }
5298     }
5299 
5300     /* Clear RES0 bits.  */
5301     value &= valid_mask;
5302 
5303     /*
5304      * These bits change the MMU setup:
5305      * HCR_VM enables stage 2 translation
5306      * HCR_PTW forbids certain page-table setups
5307      * HCR_DC disables stage1 and enables stage2 translation
5308      * HCR_DCT enables tagging on (disabled) stage1 translation
5309      */
5310     if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT)) {
5311         tlb_flush(CPU(cpu));
5312     }
5313     env->cp15.hcr_el2 = value;
5314 
5315     /*
5316      * Updates to VI and VF require us to update the status of
5317      * virtual interrupts, which are the logical OR of these bits
5318      * and the state of the input lines from the GIC. (This requires
5319      * that we have the iothread lock, which is done by marking the
5320      * reginfo structs as ARM_CP_IO.)
5321      * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5322      * possible for it to be taken immediately, because VIRQ and
5323      * VFIQ are masked unless running at EL0 or EL1, and HCR
5324      * can only be written at EL2.
5325      */
5326     g_assert(qemu_mutex_iothread_locked());
5327     arm_cpu_update_virq(cpu);
5328     arm_cpu_update_vfiq(cpu);
5329 }
5330 
5331 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5332 {
5333     do_hcr_write(env, value, 0);
5334 }
5335 
5336 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5337                           uint64_t value)
5338 {
5339     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5340     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5341     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5342 }
5343 
5344 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5345                          uint64_t value)
5346 {
5347     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5348     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5349     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5350 }
5351 
5352 /*
5353  * Return the effective value of HCR_EL2.
5354  * Bits that are not included here:
5355  * RW       (read from SCR_EL3.RW as needed)
5356  */
5357 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5358 {
5359     uint64_t ret = env->cp15.hcr_el2;
5360 
5361     if (!arm_is_el2_enabled(env)) {
5362         /*
5363          * "This register has no effect if EL2 is not enabled in the
5364          * current Security state".  This is ARMv8.4-SecEL2 speak for
5365          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5366          *
5367          * Prior to that, the language was "In an implementation that
5368          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5369          * as if this field is 0 for all purposes other than a direct
5370          * read or write access of HCR_EL2".  With lots of enumeration
5371          * on a per-field basis.  In current QEMU, this is condition
5372          * is arm_is_secure_below_el3.
5373          *
5374          * Since the v8.4 language applies to the entire register, and
5375          * appears to be backward compatible, use that.
5376          */
5377         return 0;
5378     }
5379 
5380     /*
5381      * For a cpu that supports both aarch64 and aarch32, we can set bits
5382      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5383      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5384      */
5385     if (!arm_el_is_aa64(env, 2)) {
5386         uint64_t aa32_valid;
5387 
5388         /*
5389          * These bits are up-to-date as of ARMv8.6.
5390          * For HCR, it's easiest to list just the 2 bits that are invalid.
5391          * For HCR2, list those that are valid.
5392          */
5393         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5394         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5395                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5396         ret &= aa32_valid;
5397     }
5398 
5399     if (ret & HCR_TGE) {
5400         /* These bits are up-to-date as of ARMv8.6.  */
5401         if (ret & HCR_E2H) {
5402             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5403                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5404                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5405                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5406                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5407                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5408         } else {
5409             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5410         }
5411         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5412                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5413                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5414                  HCR_TLOR);
5415     }
5416 
5417     return ret;
5418 }
5419 
5420 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5421                            uint64_t value)
5422 {
5423     /*
5424      * For A-profile AArch32 EL3, if NSACR.CP10
5425      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5426      */
5427     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5428         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5429         value &= ~(0x3 << 10);
5430         value |= env->cp15.cptr_el[2] & (0x3 << 10);
5431     }
5432     env->cp15.cptr_el[2] = value;
5433 }
5434 
5435 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5436 {
5437     /*
5438      * For A-profile AArch32 EL3, if NSACR.CP10
5439      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5440      */
5441     uint64_t value = env->cp15.cptr_el[2];
5442 
5443     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5444         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5445         value |= 0x3 << 10;
5446     }
5447     return value;
5448 }
5449 
5450 static const ARMCPRegInfo el2_cp_reginfo[] = {
5451     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5452       .type = ARM_CP_IO,
5453       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5454       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5455       .writefn = hcr_write },
5456     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5457       .type = ARM_CP_ALIAS | ARM_CP_IO,
5458       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5459       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5460       .writefn = hcr_writelow },
5461     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5462       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5463       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5464     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5465       .type = ARM_CP_ALIAS,
5466       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5467       .access = PL2_RW,
5468       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5469     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5470       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5471       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5472     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5473       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5474       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5475     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5476       .type = ARM_CP_ALIAS,
5477       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5478       .access = PL2_RW,
5479       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5480     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5481       .type = ARM_CP_ALIAS,
5482       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5483       .access = PL2_RW,
5484       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5485     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5486       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5487       .access = PL2_RW, .writefn = vbar_write,
5488       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5489       .resetvalue = 0 },
5490     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5491       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5492       .access = PL3_RW, .type = ARM_CP_ALIAS,
5493       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5494     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5495       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5496       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5497       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5498       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5499     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5500       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5501       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5502       .resetvalue = 0 },
5503     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5504       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5505       .access = PL2_RW, .type = ARM_CP_ALIAS,
5506       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5507     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5508       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5509       .access = PL2_RW, .type = ARM_CP_CONST,
5510       .resetvalue = 0 },
5511     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5512     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5513       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5514       .access = PL2_RW, .type = ARM_CP_CONST,
5515       .resetvalue = 0 },
5516     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5517       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5518       .access = PL2_RW, .type = ARM_CP_CONST,
5519       .resetvalue = 0 },
5520     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5521       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5522       .access = PL2_RW, .type = ARM_CP_CONST,
5523       .resetvalue = 0 },
5524     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5525       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5526       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5527       /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
5528       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5529     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5530       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5531       .type = ARM_CP_ALIAS,
5532       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5533       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5534     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5535       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5536       .access = PL2_RW,
5537       /* no .writefn needed as this can't cause an ASID change;
5538        * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5539        */
5540       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5541     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5542       .cp = 15, .opc1 = 6, .crm = 2,
5543       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5544       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5545       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5546       .writefn = vttbr_write },
5547     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5548       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5549       .access = PL2_RW, .writefn = vttbr_write,
5550       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5551     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5552       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5553       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5554       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5555     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5556       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5557       .access = PL2_RW, .resetvalue = 0,
5558       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5559     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5560       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5561       .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5562       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5563     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5564       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5565       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5566     { .name = "TLBIALLNSNH",
5567       .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5568       .type = ARM_CP_NO_RAW, .access = PL2_W,
5569       .writefn = tlbiall_nsnh_write },
5570     { .name = "TLBIALLNSNHIS",
5571       .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5572       .type = ARM_CP_NO_RAW, .access = PL2_W,
5573       .writefn = tlbiall_nsnh_is_write },
5574     { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5575       .type = ARM_CP_NO_RAW, .access = PL2_W,
5576       .writefn = tlbiall_hyp_write },
5577     { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5578       .type = ARM_CP_NO_RAW, .access = PL2_W,
5579       .writefn = tlbiall_hyp_is_write },
5580     { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5581       .type = ARM_CP_NO_RAW, .access = PL2_W,
5582       .writefn = tlbimva_hyp_write },
5583     { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5584       .type = ARM_CP_NO_RAW, .access = PL2_W,
5585       .writefn = tlbimva_hyp_is_write },
5586     { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5587       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5588       .type = ARM_CP_NO_RAW, .access = PL2_W,
5589       .writefn = tlbi_aa64_alle2_write },
5590     { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5591       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5592       .type = ARM_CP_NO_RAW, .access = PL2_W,
5593       .writefn = tlbi_aa64_vae2_write },
5594     { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5595       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5596       .access = PL2_W, .type = ARM_CP_NO_RAW,
5597       .writefn = tlbi_aa64_vae2_write },
5598     { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5599       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5600       .access = PL2_W, .type = ARM_CP_NO_RAW,
5601       .writefn = tlbi_aa64_alle2is_write },
5602     { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5603       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5604       .type = ARM_CP_NO_RAW, .access = PL2_W,
5605       .writefn = tlbi_aa64_vae2is_write },
5606     { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5607       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5608       .access = PL2_W, .type = ARM_CP_NO_RAW,
5609       .writefn = tlbi_aa64_vae2is_write },
5610 #ifndef CONFIG_USER_ONLY
5611     /* Unlike the other EL2-related AT operations, these must
5612      * UNDEF from EL3 if EL2 is not implemented, which is why we
5613      * define them here rather than with the rest of the AT ops.
5614      */
5615     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5616       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5617       .access = PL2_W, .accessfn = at_s1e2_access,
5618       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
5619     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5620       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5621       .access = PL2_W, .accessfn = at_s1e2_access,
5622       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
5623     /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5624      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5625      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5626      * to behave as if SCR.NS was 1.
5627      */
5628     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5629       .access = PL2_W,
5630       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5631     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5632       .access = PL2_W,
5633       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5634     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5635       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5636       /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5637        * reset values as IMPDEF. We choose to reset to 3 to comply with
5638        * both ARMv7 and ARMv8.
5639        */
5640       .access = PL2_RW, .resetvalue = 3,
5641       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5642     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5643       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5644       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5645       .writefn = gt_cntvoff_write,
5646       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5647     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5648       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5649       .writefn = gt_cntvoff_write,
5650       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5651     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5652       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5653       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5654       .type = ARM_CP_IO, .access = PL2_RW,
5655       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5656     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5657       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5658       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5659       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5660     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5661       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5662       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5663       .resetfn = gt_hyp_timer_reset,
5664       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5665     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5666       .type = ARM_CP_IO,
5667       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5668       .access = PL2_RW,
5669       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5670       .resetvalue = 0,
5671       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5672 #endif
5673     /* The only field of MDCR_EL2 that has a defined architectural reset value
5674      * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
5675      */
5676     { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5677       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5678       .access = PL2_RW, .resetvalue = PMCR_NUM_COUNTERS,
5679       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
5680     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5681       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5682       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5683       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5684     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5685       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5686       .access = PL2_RW,
5687       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5688     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5689       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5690       .access = PL2_RW,
5691       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5692     REGINFO_SENTINEL
5693 };
5694 
5695 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5696     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5697       .type = ARM_CP_ALIAS | ARM_CP_IO,
5698       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5699       .access = PL2_RW,
5700       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5701       .writefn = hcr_writehigh },
5702     REGINFO_SENTINEL
5703 };
5704 
5705 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5706                                   bool isread)
5707 {
5708     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5709         return CP_ACCESS_OK;
5710     }
5711     return CP_ACCESS_TRAP_UNCATEGORIZED;
5712 }
5713 
5714 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5715     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5716       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5717       .access = PL2_RW, .accessfn = sel2_access,
5718       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5719     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5720       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5721       .access = PL2_RW, .accessfn = sel2_access,
5722       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5723     REGINFO_SENTINEL
5724 };
5725 
5726 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5727                                    bool isread)
5728 {
5729     /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5730      * At Secure EL1 it traps to EL3 or EL2.
5731      */
5732     if (arm_current_el(env) == 3) {
5733         return CP_ACCESS_OK;
5734     }
5735     if (arm_is_secure_below_el3(env)) {
5736         if (env->cp15.scr_el3 & SCR_EEL2) {
5737             return CP_ACCESS_TRAP_EL2;
5738         }
5739         return CP_ACCESS_TRAP_EL3;
5740     }
5741     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5742     if (isread) {
5743         return CP_ACCESS_OK;
5744     }
5745     return CP_ACCESS_TRAP_UNCATEGORIZED;
5746 }
5747 
5748 static const ARMCPRegInfo el3_cp_reginfo[] = {
5749     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5750       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5751       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5752       .resetfn = scr_reset, .writefn = scr_write },
5753     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5754       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5755       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5756       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5757       .writefn = scr_write },
5758     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5759       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5760       .access = PL3_RW, .resetvalue = 0,
5761       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5762     { .name = "SDER",
5763       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5764       .access = PL3_RW, .resetvalue = 0,
5765       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5766     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5767       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5768       .writefn = vbar_write, .resetvalue = 0,
5769       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5770     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5771       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5772       .access = PL3_RW, .resetvalue = 0,
5773       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5774     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5775       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5776       .access = PL3_RW,
5777       /* no .writefn needed as this can't cause an ASID change;
5778        * we must provide a .raw_writefn and .resetfn because we handle
5779        * reset and migration for the AArch32 TTBCR(S), which might be
5780        * using mask and base_mask.
5781        */
5782       .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
5783       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5784     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5785       .type = ARM_CP_ALIAS,
5786       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5787       .access = PL3_RW,
5788       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5789     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5790       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5791       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5792     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5793       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5794       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5795     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5796       .type = ARM_CP_ALIAS,
5797       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5798       .access = PL3_RW,
5799       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5800     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5801       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5802       .access = PL3_RW, .writefn = vbar_write,
5803       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5804       .resetvalue = 0 },
5805     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5806       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5807       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5808       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5809     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5810       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5811       .access = PL3_RW, .resetvalue = 0,
5812       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5813     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5814       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5815       .access = PL3_RW, .type = ARM_CP_CONST,
5816       .resetvalue = 0 },
5817     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5818       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5819       .access = PL3_RW, .type = ARM_CP_CONST,
5820       .resetvalue = 0 },
5821     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5822       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5823       .access = PL3_RW, .type = ARM_CP_CONST,
5824       .resetvalue = 0 },
5825     { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5826       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5827       .access = PL3_W, .type = ARM_CP_NO_RAW,
5828       .writefn = tlbi_aa64_alle3is_write },
5829     { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5830       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5831       .access = PL3_W, .type = ARM_CP_NO_RAW,
5832       .writefn = tlbi_aa64_vae3is_write },
5833     { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5834       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5835       .access = PL3_W, .type = ARM_CP_NO_RAW,
5836       .writefn = tlbi_aa64_vae3is_write },
5837     { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5838       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5839       .access = PL3_W, .type = ARM_CP_NO_RAW,
5840       .writefn = tlbi_aa64_alle3_write },
5841     { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5842       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5843       .access = PL3_W, .type = ARM_CP_NO_RAW,
5844       .writefn = tlbi_aa64_vae3_write },
5845     { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5846       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5847       .access = PL3_W, .type = ARM_CP_NO_RAW,
5848       .writefn = tlbi_aa64_vae3_write },
5849     REGINFO_SENTINEL
5850 };
5851 
5852 #ifndef CONFIG_USER_ONLY
5853 /* Test if system register redirection is to occur in the current state.  */
5854 static bool redirect_for_e2h(CPUARMState *env)
5855 {
5856     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5857 }
5858 
5859 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5860 {
5861     CPReadFn *readfn;
5862 
5863     if (redirect_for_e2h(env)) {
5864         /* Switch to the saved EL2 version of the register.  */
5865         ri = ri->opaque;
5866         readfn = ri->readfn;
5867     } else {
5868         readfn = ri->orig_readfn;
5869     }
5870     if (readfn == NULL) {
5871         readfn = raw_read;
5872     }
5873     return readfn(env, ri);
5874 }
5875 
5876 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5877                           uint64_t value)
5878 {
5879     CPWriteFn *writefn;
5880 
5881     if (redirect_for_e2h(env)) {
5882         /* Switch to the saved EL2 version of the register.  */
5883         ri = ri->opaque;
5884         writefn = ri->writefn;
5885     } else {
5886         writefn = ri->orig_writefn;
5887     }
5888     if (writefn == NULL) {
5889         writefn = raw_write;
5890     }
5891     writefn(env, ri, value);
5892 }
5893 
5894 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5895 {
5896     struct E2HAlias {
5897         uint32_t src_key, dst_key, new_key;
5898         const char *src_name, *dst_name, *new_name;
5899         bool (*feature)(const ARMISARegisters *id);
5900     };
5901 
5902 #define K(op0, op1, crn, crm, op2) \
5903     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5904 
5905     static const struct E2HAlias aliases[] = {
5906         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
5907           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5908         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
5909           "CPACR", "CPTR_EL2", "CPACR_EL12" },
5910         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
5911           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5912         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
5913           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5914         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
5915           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5916         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
5917           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5918         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
5919           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5920         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
5921           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5922         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
5923           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5924         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
5925           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5926         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
5927           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5928         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5929           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5930         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5931           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5932         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5933           "VBAR", "VBAR_EL2", "VBAR_EL12" },
5934         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5935           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5936         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5937           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5938 
5939         /*
5940          * Note that redirection of ZCR is mentioned in the description
5941          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5942          * not in the summary table.
5943          */
5944         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
5945           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5946 
5947         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
5948           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5949 
5950         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5951         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5952     };
5953 #undef K
5954 
5955     size_t i;
5956 
5957     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5958         const struct E2HAlias *a = &aliases[i];
5959         ARMCPRegInfo *src_reg, *dst_reg;
5960 
5961         if (a->feature && !a->feature(&cpu->isar)) {
5962             continue;
5963         }
5964 
5965         src_reg = g_hash_table_lookup(cpu->cp_regs, &a->src_key);
5966         dst_reg = g_hash_table_lookup(cpu->cp_regs, &a->dst_key);
5967         g_assert(src_reg != NULL);
5968         g_assert(dst_reg != NULL);
5969 
5970         /* Cross-compare names to detect typos in the keys.  */
5971         g_assert(strcmp(src_reg->name, a->src_name) == 0);
5972         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5973 
5974         /* None of the core system registers use opaque; we will.  */
5975         g_assert(src_reg->opaque == NULL);
5976 
5977         /* Create alias before redirection so we dup the right data. */
5978         if (a->new_key) {
5979             ARMCPRegInfo *new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5980             uint32_t *new_key = g_memdup(&a->new_key, sizeof(uint32_t));
5981             bool ok;
5982 
5983             new_reg->name = a->new_name;
5984             new_reg->type |= ARM_CP_ALIAS;
5985             /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
5986             new_reg->access &= PL2_RW | PL3_RW;
5987 
5988             ok = g_hash_table_insert(cpu->cp_regs, new_key, new_reg);
5989             g_assert(ok);
5990         }
5991 
5992         src_reg->opaque = dst_reg;
5993         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5994         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5995         if (!src_reg->raw_readfn) {
5996             src_reg->raw_readfn = raw_read;
5997         }
5998         if (!src_reg->raw_writefn) {
5999             src_reg->raw_writefn = raw_write;
6000         }
6001         src_reg->readfn = el2_e2h_read;
6002         src_reg->writefn = el2_e2h_write;
6003     }
6004 }
6005 #endif
6006 
6007 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6008                                      bool isread)
6009 {
6010     int cur_el = arm_current_el(env);
6011 
6012     if (cur_el < 2) {
6013         uint64_t hcr = arm_hcr_el2_eff(env);
6014 
6015         if (cur_el == 0) {
6016             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6017                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6018                     return CP_ACCESS_TRAP_EL2;
6019                 }
6020             } else {
6021                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6022                     return CP_ACCESS_TRAP;
6023                 }
6024                 if (hcr & HCR_TID2) {
6025                     return CP_ACCESS_TRAP_EL2;
6026                 }
6027             }
6028         } else if (hcr & HCR_TID2) {
6029             return CP_ACCESS_TRAP_EL2;
6030         }
6031     }
6032 
6033     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6034         return CP_ACCESS_TRAP_EL2;
6035     }
6036 
6037     return CP_ACCESS_OK;
6038 }
6039 
6040 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
6041                         uint64_t value)
6042 {
6043     /* Writes to OSLAR_EL1 may update the OS lock status, which can be
6044      * read via a bit in OSLSR_EL1.
6045      */
6046     int oslock;
6047 
6048     if (ri->state == ARM_CP_STATE_AA32) {
6049         oslock = (value == 0xC5ACCE55);
6050     } else {
6051         oslock = value & 1;
6052     }
6053 
6054     env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
6055 }
6056 
6057 static const ARMCPRegInfo debug_cp_reginfo[] = {
6058     /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
6059      * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
6060      * unlike DBGDRAR it is never accessible from EL0.
6061      * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
6062      * accessor.
6063      */
6064     { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
6065       .access = PL0_R, .accessfn = access_tdra,
6066       .type = ARM_CP_CONST, .resetvalue = 0 },
6067     { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
6068       .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
6069       .access = PL1_R, .accessfn = access_tdra,
6070       .type = ARM_CP_CONST, .resetvalue = 0 },
6071     { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
6072       .access = PL0_R, .accessfn = access_tdra,
6073       .type = ARM_CP_CONST, .resetvalue = 0 },
6074     /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
6075     { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
6076       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
6077       .access = PL1_RW, .accessfn = access_tda,
6078       .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
6079       .resetvalue = 0 },
6080     /*
6081      * MDCCSR_EL0[30:29] map to EDSCR[30:29].  Simply RAZ as the external
6082      * Debug Communication Channel is not implemented.
6083      */
6084     { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_AA64,
6085       .opc0 = 2, .opc1 = 3, .crn = 0, .crm = 1, .opc2 = 0,
6086       .access = PL0_R, .accessfn = access_tda,
6087       .type = ARM_CP_CONST, .resetvalue = 0 },
6088     /*
6089      * DBGDSCRint[15,12,5:2] map to MDSCR_EL1[15,12,5:2].  Map all bits as
6090      * it is unlikely a guest will care.
6091      * We don't implement the configurable EL0 access.
6092      */
6093     { .name = "DBGDSCRint", .state = ARM_CP_STATE_AA32,
6094       .cp = 14, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
6095       .type = ARM_CP_ALIAS,
6096       .access = PL1_R, .accessfn = access_tda,
6097       .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
6098     { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
6099       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
6100       .access = PL1_W, .type = ARM_CP_NO_RAW,
6101       .accessfn = access_tdosa,
6102       .writefn = oslar_write },
6103     { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
6104       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
6105       .access = PL1_R, .resetvalue = 10,
6106       .accessfn = access_tdosa,
6107       .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
6108     /* Dummy OSDLR_EL1: 32-bit Linux will read this */
6109     { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
6110       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
6111       .access = PL1_RW, .accessfn = access_tdosa,
6112       .type = ARM_CP_NOP },
6113     /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
6114      * implement vector catch debug events yet.
6115      */
6116     { .name = "DBGVCR",
6117       .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
6118       .access = PL1_RW, .accessfn = access_tda,
6119       .type = ARM_CP_NOP },
6120     /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
6121      * to save and restore a 32-bit guest's DBGVCR)
6122      */
6123     { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
6124       .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
6125       .access = PL2_RW, .accessfn = access_tda,
6126       .type = ARM_CP_NOP },
6127     /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
6128      * Channel but Linux may try to access this register. The 32-bit
6129      * alias is DBGDCCINT.
6130      */
6131     { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
6132       .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6133       .access = PL1_RW, .accessfn = access_tda,
6134       .type = ARM_CP_NOP },
6135     REGINFO_SENTINEL
6136 };
6137 
6138 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6139     /* 64 bit access versions of the (dummy) debug registers */
6140     { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6141       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6142     { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6143       .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6144     REGINFO_SENTINEL
6145 };
6146 
6147 /* Return the exception level to which exceptions should be taken
6148  * via SVEAccessTrap.  If an exception should be routed through
6149  * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
6150  * take care of raising that exception.
6151  * C.f. the ARM pseudocode function CheckSVEEnabled.
6152  */
6153 int sve_exception_el(CPUARMState *env, int el)
6154 {
6155 #ifndef CONFIG_USER_ONLY
6156     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
6157 
6158     if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
6159         /* Check CPACR.ZEN.  */
6160         switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
6161         case 1:
6162             if (el != 0) {
6163                 break;
6164             }
6165             /* fall through */
6166         case 0:
6167         case 2:
6168             /* route_to_el2 */
6169             return hcr_el2 & HCR_TGE ? 2 : 1;
6170         }
6171 
6172         /* Check CPACR.FPEN.  */
6173         switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
6174         case 1:
6175             if (el != 0) {
6176                 break;
6177             }
6178             /* fall through */
6179         case 0:
6180         case 2:
6181             return 0;
6182         }
6183     }
6184 
6185     /*
6186      * CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE).
6187      */
6188     if (el <= 2) {
6189         if (hcr_el2 & HCR_E2H) {
6190             /* Check CPTR_EL2.ZEN.  */
6191             switch (extract32(env->cp15.cptr_el[2], 16, 2)) {
6192             case 1:
6193                 if (el != 0 || !(hcr_el2 & HCR_TGE)) {
6194                     break;
6195                 }
6196                 /* fall through */
6197             case 0:
6198             case 2:
6199                 return 2;
6200             }
6201 
6202             /* Check CPTR_EL2.FPEN.  */
6203             switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
6204             case 1:
6205                 if (el == 2 || !(hcr_el2 & HCR_TGE)) {
6206                     break;
6207                 }
6208                 /* fall through */
6209             case 0:
6210             case 2:
6211                 return 0;
6212             }
6213         } else if (arm_is_el2_enabled(env)) {
6214             if (env->cp15.cptr_el[2] & CPTR_TZ) {
6215                 return 2;
6216             }
6217             if (env->cp15.cptr_el[2] & CPTR_TFP) {
6218                 return 0;
6219             }
6220         }
6221     }
6222 
6223     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6224     if (arm_feature(env, ARM_FEATURE_EL3)
6225         && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
6226         return 3;
6227     }
6228 #endif
6229     return 0;
6230 }
6231 
6232 uint32_t aarch64_sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
6233 {
6234     uint32_t end_len;
6235 
6236     start_len = MIN(start_len, ARM_MAX_VQ - 1);
6237     end_len = start_len;
6238 
6239     if (!test_bit(start_len, cpu->sve_vq_map)) {
6240         end_len = find_last_bit(cpu->sve_vq_map, start_len);
6241         assert(end_len < start_len);
6242     }
6243     return end_len;
6244 }
6245 
6246 /*
6247  * Given that SVE is enabled, return the vector length for EL.
6248  */
6249 uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
6250 {
6251     ARMCPU *cpu = env_archcpu(env);
6252     uint32_t zcr_len = cpu->sve_max_vq - 1;
6253 
6254     if (el <= 1 &&
6255         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
6256         zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6257     }
6258     if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6259         zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6260     }
6261     if (arm_feature(env, ARM_FEATURE_EL3)) {
6262         zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6263     }
6264 
6265     return aarch64_sve_zcr_get_valid_len(cpu, zcr_len);
6266 }
6267 
6268 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6269                       uint64_t value)
6270 {
6271     int cur_el = arm_current_el(env);
6272     int old_len = sve_zcr_len_for_el(env, cur_el);
6273     int new_len;
6274 
6275     /* Bits other than [3:0] are RAZ/WI.  */
6276     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6277     raw_write(env, ri, value & 0xf);
6278 
6279     /*
6280      * Because we arrived here, we know both FP and SVE are enabled;
6281      * otherwise we would have trapped access to the ZCR_ELn register.
6282      */
6283     new_len = sve_zcr_len_for_el(env, cur_el);
6284     if (new_len < old_len) {
6285         aarch64_sve_narrow_vq(env, new_len + 1);
6286     }
6287 }
6288 
6289 static const ARMCPRegInfo zcr_el1_reginfo = {
6290     .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6291     .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6292     .access = PL1_RW, .type = ARM_CP_SVE,
6293     .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6294     .writefn = zcr_write, .raw_writefn = raw_write
6295 };
6296 
6297 static const ARMCPRegInfo zcr_el2_reginfo = {
6298     .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6299     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6300     .access = PL2_RW, .type = ARM_CP_SVE,
6301     .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6302     .writefn = zcr_write, .raw_writefn = raw_write
6303 };
6304 
6305 static const ARMCPRegInfo zcr_no_el2_reginfo = {
6306     .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6307     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6308     .access = PL2_RW, .type = ARM_CP_SVE,
6309     .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
6310 };
6311 
6312 static const ARMCPRegInfo zcr_el3_reginfo = {
6313     .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6314     .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6315     .access = PL3_RW, .type = ARM_CP_SVE,
6316     .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6317     .writefn = zcr_write, .raw_writefn = raw_write
6318 };
6319 
6320 void hw_watchpoint_update(ARMCPU *cpu, int n)
6321 {
6322     CPUARMState *env = &cpu->env;
6323     vaddr len = 0;
6324     vaddr wvr = env->cp15.dbgwvr[n];
6325     uint64_t wcr = env->cp15.dbgwcr[n];
6326     int mask;
6327     int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6328 
6329     if (env->cpu_watchpoint[n]) {
6330         cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6331         env->cpu_watchpoint[n] = NULL;
6332     }
6333 
6334     if (!extract64(wcr, 0, 1)) {
6335         /* E bit clear : watchpoint disabled */
6336         return;
6337     }
6338 
6339     switch (extract64(wcr, 3, 2)) {
6340     case 0:
6341         /* LSC 00 is reserved and must behave as if the wp is disabled */
6342         return;
6343     case 1:
6344         flags |= BP_MEM_READ;
6345         break;
6346     case 2:
6347         flags |= BP_MEM_WRITE;
6348         break;
6349     case 3:
6350         flags |= BP_MEM_ACCESS;
6351         break;
6352     }
6353 
6354     /* Attempts to use both MASK and BAS fields simultaneously are
6355      * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6356      * thus generating a watchpoint for every byte in the masked region.
6357      */
6358     mask = extract64(wcr, 24, 4);
6359     if (mask == 1 || mask == 2) {
6360         /* Reserved values of MASK; we must act as if the mask value was
6361          * some non-reserved value, or as if the watchpoint were disabled.
6362          * We choose the latter.
6363          */
6364         return;
6365     } else if (mask) {
6366         /* Watchpoint covers an aligned area up to 2GB in size */
6367         len = 1ULL << mask;
6368         /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6369          * whether the watchpoint fires when the unmasked bits match; we opt
6370          * to generate the exceptions.
6371          */
6372         wvr &= ~(len - 1);
6373     } else {
6374         /* Watchpoint covers bytes defined by the byte address select bits */
6375         int bas = extract64(wcr, 5, 8);
6376         int basstart;
6377 
6378         if (extract64(wvr, 2, 1)) {
6379             /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6380              * ignored, and BAS[3:0] define which bytes to watch.
6381              */
6382             bas &= 0xf;
6383         }
6384 
6385         if (bas == 0) {
6386             /* This must act as if the watchpoint is disabled */
6387             return;
6388         }
6389 
6390         /* The BAS bits are supposed to be programmed to indicate a contiguous
6391          * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6392          * we fire for each byte in the word/doubleword addressed by the WVR.
6393          * We choose to ignore any non-zero bits after the first range of 1s.
6394          */
6395         basstart = ctz32(bas);
6396         len = cto32(bas >> basstart);
6397         wvr += basstart;
6398     }
6399 
6400     cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6401                           &env->cpu_watchpoint[n]);
6402 }
6403 
6404 void hw_watchpoint_update_all(ARMCPU *cpu)
6405 {
6406     int i;
6407     CPUARMState *env = &cpu->env;
6408 
6409     /* Completely clear out existing QEMU watchpoints and our array, to
6410      * avoid possible stale entries following migration load.
6411      */
6412     cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6413     memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6414 
6415     for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6416         hw_watchpoint_update(cpu, i);
6417     }
6418 }
6419 
6420 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6421                          uint64_t value)
6422 {
6423     ARMCPU *cpu = env_archcpu(env);
6424     int i = ri->crm;
6425 
6426     /*
6427      * Bits [1:0] are RES0.
6428      *
6429      * It is IMPLEMENTATION DEFINED whether [63:49] ([63:53] with FEAT_LVA)
6430      * are hardwired to the value of bit [48] ([52] with FEAT_LVA), or if
6431      * they contain the value written.  It is CONSTRAINED UNPREDICTABLE
6432      * whether the RESS bits are ignored when comparing an address.
6433      *
6434      * Therefore we are allowed to compare the entire register, which lets
6435      * us avoid considering whether or not FEAT_LVA is actually enabled.
6436      */
6437     value &= ~3ULL;
6438 
6439     raw_write(env, ri, value);
6440     hw_watchpoint_update(cpu, i);
6441 }
6442 
6443 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6444                          uint64_t value)
6445 {
6446     ARMCPU *cpu = env_archcpu(env);
6447     int i = ri->crm;
6448 
6449     raw_write(env, ri, value);
6450     hw_watchpoint_update(cpu, i);
6451 }
6452 
6453 void hw_breakpoint_update(ARMCPU *cpu, int n)
6454 {
6455     CPUARMState *env = &cpu->env;
6456     uint64_t bvr = env->cp15.dbgbvr[n];
6457     uint64_t bcr = env->cp15.dbgbcr[n];
6458     vaddr addr;
6459     int bt;
6460     int flags = BP_CPU;
6461 
6462     if (env->cpu_breakpoint[n]) {
6463         cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6464         env->cpu_breakpoint[n] = NULL;
6465     }
6466 
6467     if (!extract64(bcr, 0, 1)) {
6468         /* E bit clear : watchpoint disabled */
6469         return;
6470     }
6471 
6472     bt = extract64(bcr, 20, 4);
6473 
6474     switch (bt) {
6475     case 4: /* unlinked address mismatch (reserved if AArch64) */
6476     case 5: /* linked address mismatch (reserved if AArch64) */
6477         qemu_log_mask(LOG_UNIMP,
6478                       "arm: address mismatch breakpoint types not implemented\n");
6479         return;
6480     case 0: /* unlinked address match */
6481     case 1: /* linked address match */
6482     {
6483         /*
6484          * Bits [1:0] are RES0.
6485          *
6486          * It is IMPLEMENTATION DEFINED whether bits [63:49]
6487          * ([63:53] for FEAT_LVA) are hardwired to a copy of the sign bit
6488          * of the VA field ([48] or [52] for FEAT_LVA), or whether the
6489          * value is read as written.  It is CONSTRAINED UNPREDICTABLE
6490          * whether the RESS bits are ignored when comparing an address.
6491          * Therefore we are allowed to compare the entire register, which
6492          * lets us avoid considering whether FEAT_LVA is actually enabled.
6493          *
6494          * The BAS field is used to allow setting breakpoints on 16-bit
6495          * wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6496          * a bp will fire if the addresses covered by the bp and the addresses
6497          * covered by the insn overlap but the insn doesn't start at the
6498          * start of the bp address range. We choose to require the insn and
6499          * the bp to have the same address. The constraints on writing to
6500          * BAS enforced in dbgbcr_write mean we have only four cases:
6501          *  0b0000  => no breakpoint
6502          *  0b0011  => breakpoint on addr
6503          *  0b1100  => breakpoint on addr + 2
6504          *  0b1111  => breakpoint on addr
6505          * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6506          */
6507         int bas = extract64(bcr, 5, 4);
6508         addr = bvr & ~3ULL;
6509         if (bas == 0) {
6510             return;
6511         }
6512         if (bas == 0xc) {
6513             addr += 2;
6514         }
6515         break;
6516     }
6517     case 2: /* unlinked context ID match */
6518     case 8: /* unlinked VMID match (reserved if no EL2) */
6519     case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6520         qemu_log_mask(LOG_UNIMP,
6521                       "arm: unlinked context breakpoint types not implemented\n");
6522         return;
6523     case 9: /* linked VMID match (reserved if no EL2) */
6524     case 11: /* linked context ID and VMID match (reserved if no EL2) */
6525     case 3: /* linked context ID match */
6526     default:
6527         /* We must generate no events for Linked context matches (unless
6528          * they are linked to by some other bp/wp, which is handled in
6529          * updates for the linking bp/wp). We choose to also generate no events
6530          * for reserved values.
6531          */
6532         return;
6533     }
6534 
6535     cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6536 }
6537 
6538 void hw_breakpoint_update_all(ARMCPU *cpu)
6539 {
6540     int i;
6541     CPUARMState *env = &cpu->env;
6542 
6543     /* Completely clear out existing QEMU breakpoints and our array, to
6544      * avoid possible stale entries following migration load.
6545      */
6546     cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6547     memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6548 
6549     for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6550         hw_breakpoint_update(cpu, i);
6551     }
6552 }
6553 
6554 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6555                          uint64_t value)
6556 {
6557     ARMCPU *cpu = env_archcpu(env);
6558     int i = ri->crm;
6559 
6560     raw_write(env, ri, value);
6561     hw_breakpoint_update(cpu, i);
6562 }
6563 
6564 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6565                          uint64_t value)
6566 {
6567     ARMCPU *cpu = env_archcpu(env);
6568     int i = ri->crm;
6569 
6570     /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6571      * copy of BAS[0].
6572      */
6573     value = deposit64(value, 6, 1, extract64(value, 5, 1));
6574     value = deposit64(value, 8, 1, extract64(value, 7, 1));
6575 
6576     raw_write(env, ri, value);
6577     hw_breakpoint_update(cpu, i);
6578 }
6579 
6580 static void define_debug_regs(ARMCPU *cpu)
6581 {
6582     /* Define v7 and v8 architectural debug registers.
6583      * These are just dummy implementations for now.
6584      */
6585     int i;
6586     int wrps, brps, ctx_cmps;
6587 
6588     /*
6589      * The Arm ARM says DBGDIDR is optional and deprecated if EL1 cannot
6590      * use AArch32.  Given that bit 15 is RES1, if the value is 0 then
6591      * the register must not exist for this cpu.
6592      */
6593     if (cpu->isar.dbgdidr != 0) {
6594         ARMCPRegInfo dbgdidr = {
6595             .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0,
6596             .opc1 = 0, .opc2 = 0,
6597             .access = PL0_R, .accessfn = access_tda,
6598             .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
6599         };
6600         define_one_arm_cp_reg(cpu, &dbgdidr);
6601     }
6602 
6603     /* Note that all these register fields hold "number of Xs minus 1". */
6604     brps = arm_num_brps(cpu);
6605     wrps = arm_num_wrps(cpu);
6606     ctx_cmps = arm_num_ctx_cmps(cpu);
6607 
6608     assert(ctx_cmps <= brps);
6609 
6610     define_arm_cp_regs(cpu, debug_cp_reginfo);
6611 
6612     if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6613         define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6614     }
6615 
6616     for (i = 0; i < brps; i++) {
6617         ARMCPRegInfo dbgregs[] = {
6618             { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6619               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
6620               .access = PL1_RW, .accessfn = access_tda,
6621               .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6622               .writefn = dbgbvr_write, .raw_writefn = raw_write
6623             },
6624             { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6625               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
6626               .access = PL1_RW, .accessfn = access_tda,
6627               .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6628               .writefn = dbgbcr_write, .raw_writefn = raw_write
6629             },
6630             REGINFO_SENTINEL
6631         };
6632         define_arm_cp_regs(cpu, dbgregs);
6633     }
6634 
6635     for (i = 0; i < wrps; i++) {
6636         ARMCPRegInfo dbgregs[] = {
6637             { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6638               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
6639               .access = PL1_RW, .accessfn = access_tda,
6640               .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6641               .writefn = dbgwvr_write, .raw_writefn = raw_write
6642             },
6643             { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6644               .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
6645               .access = PL1_RW, .accessfn = access_tda,
6646               .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6647               .writefn = dbgwcr_write, .raw_writefn = raw_write
6648             },
6649             REGINFO_SENTINEL
6650         };
6651         define_arm_cp_regs(cpu, dbgregs);
6652     }
6653 }
6654 
6655 static void define_pmu_regs(ARMCPU *cpu)
6656 {
6657     /*
6658      * v7 performance monitor control register: same implementor
6659      * field as main ID register, and we implement four counters in
6660      * addition to the cycle count register.
6661      */
6662     unsigned int i, pmcrn = PMCR_NUM_COUNTERS;
6663     ARMCPRegInfo pmcr = {
6664         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6665         .access = PL0_RW,
6666         .type = ARM_CP_IO | ARM_CP_ALIAS,
6667         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6668         .accessfn = pmreg_access, .writefn = pmcr_write,
6669         .raw_writefn = raw_write,
6670     };
6671     ARMCPRegInfo pmcr64 = {
6672         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6673         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6674         .access = PL0_RW, .accessfn = pmreg_access,
6675         .type = ARM_CP_IO,
6676         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6677         .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6678                       PMCRLC,
6679         .writefn = pmcr_write, .raw_writefn = raw_write,
6680     };
6681     define_one_arm_cp_reg(cpu, &pmcr);
6682     define_one_arm_cp_reg(cpu, &pmcr64);
6683     for (i = 0; i < pmcrn; i++) {
6684         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6685         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6686         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6687         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6688         ARMCPRegInfo pmev_regs[] = {
6689             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6690               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6691               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6692               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6693               .accessfn = pmreg_access },
6694             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6695               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6696               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6697               .type = ARM_CP_IO,
6698               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6699               .raw_readfn = pmevcntr_rawread,
6700               .raw_writefn = pmevcntr_rawwrite },
6701             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6702               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6703               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6704               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6705               .accessfn = pmreg_access },
6706             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6707               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6708               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6709               .type = ARM_CP_IO,
6710               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6711               .raw_writefn = pmevtyper_rawwrite },
6712             REGINFO_SENTINEL
6713         };
6714         define_arm_cp_regs(cpu, pmev_regs);
6715         g_free(pmevcntr_name);
6716         g_free(pmevcntr_el0_name);
6717         g_free(pmevtyper_name);
6718         g_free(pmevtyper_el0_name);
6719     }
6720     if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
6721         ARMCPRegInfo v81_pmu_regs[] = {
6722             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6723               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6724               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6725               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6726             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6727               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6728               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6729               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6730             REGINFO_SENTINEL
6731         };
6732         define_arm_cp_regs(cpu, v81_pmu_regs);
6733     }
6734     if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6735         static const ARMCPRegInfo v84_pmmir = {
6736             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6737             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6738             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6739             .resetvalue = 0
6740         };
6741         define_one_arm_cp_reg(cpu, &v84_pmmir);
6742     }
6743 }
6744 
6745 /* We don't know until after realize whether there's a GICv3
6746  * attached, and that is what registers the gicv3 sysregs.
6747  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6748  * at runtime.
6749  */
6750 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6751 {
6752     ARMCPU *cpu = env_archcpu(env);
6753     uint64_t pfr1 = cpu->isar.id_pfr1;
6754 
6755     if (env->gicv3state) {
6756         pfr1 |= 1 << 28;
6757     }
6758     return pfr1;
6759 }
6760 
6761 #ifndef CONFIG_USER_ONLY
6762 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6763 {
6764     ARMCPU *cpu = env_archcpu(env);
6765     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6766 
6767     if (env->gicv3state) {
6768         pfr0 |= 1 << 24;
6769     }
6770     return pfr0;
6771 }
6772 #endif
6773 
6774 /* Shared logic between LORID and the rest of the LOR* registers.
6775  * Secure state exclusion has already been dealt with.
6776  */
6777 static CPAccessResult access_lor_ns(CPUARMState *env,
6778                                     const ARMCPRegInfo *ri, bool isread)
6779 {
6780     int el = arm_current_el(env);
6781 
6782     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6783         return CP_ACCESS_TRAP_EL2;
6784     }
6785     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6786         return CP_ACCESS_TRAP_EL3;
6787     }
6788     return CP_ACCESS_OK;
6789 }
6790 
6791 static CPAccessResult access_lor_other(CPUARMState *env,
6792                                        const ARMCPRegInfo *ri, bool isread)
6793 {
6794     if (arm_is_secure_below_el3(env)) {
6795         /* Access denied in secure mode.  */
6796         return CP_ACCESS_TRAP;
6797     }
6798     return access_lor_ns(env, ri, isread);
6799 }
6800 
6801 /*
6802  * A trivial implementation of ARMv8.1-LOR leaves all of these
6803  * registers fixed at 0, which indicates that there are zero
6804  * supported Limited Ordering regions.
6805  */
6806 static const ARMCPRegInfo lor_reginfo[] = {
6807     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6808       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6809       .access = PL1_RW, .accessfn = access_lor_other,
6810       .type = ARM_CP_CONST, .resetvalue = 0 },
6811     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6812       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6813       .access = PL1_RW, .accessfn = access_lor_other,
6814       .type = ARM_CP_CONST, .resetvalue = 0 },
6815     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6816       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6817       .access = PL1_RW, .accessfn = access_lor_other,
6818       .type = ARM_CP_CONST, .resetvalue = 0 },
6819     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6820       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6821       .access = PL1_RW, .accessfn = access_lor_other,
6822       .type = ARM_CP_CONST, .resetvalue = 0 },
6823     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6824       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6825       .access = PL1_R, .accessfn = access_lor_ns,
6826       .type = ARM_CP_CONST, .resetvalue = 0 },
6827     REGINFO_SENTINEL
6828 };
6829 
6830 #ifdef TARGET_AARCH64
6831 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6832                                    bool isread)
6833 {
6834     int el = arm_current_el(env);
6835 
6836     if (el < 2 &&
6837         arm_feature(env, ARM_FEATURE_EL2) &&
6838         !(arm_hcr_el2_eff(env) & HCR_APK)) {
6839         return CP_ACCESS_TRAP_EL2;
6840     }
6841     if (el < 3 &&
6842         arm_feature(env, ARM_FEATURE_EL3) &&
6843         !(env->cp15.scr_el3 & SCR_APK)) {
6844         return CP_ACCESS_TRAP_EL3;
6845     }
6846     return CP_ACCESS_OK;
6847 }
6848 
6849 static const ARMCPRegInfo pauth_reginfo[] = {
6850     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6851       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6852       .access = PL1_RW, .accessfn = access_pauth,
6853       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6854     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6855       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6856       .access = PL1_RW, .accessfn = access_pauth,
6857       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6858     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6859       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6860       .access = PL1_RW, .accessfn = access_pauth,
6861       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6862     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6863       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6864       .access = PL1_RW, .accessfn = access_pauth,
6865       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6866     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6867       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6868       .access = PL1_RW, .accessfn = access_pauth,
6869       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6870     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6871       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6872       .access = PL1_RW, .accessfn = access_pauth,
6873       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6874     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6875       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6876       .access = PL1_RW, .accessfn = access_pauth,
6877       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6878     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6879       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6880       .access = PL1_RW, .accessfn = access_pauth,
6881       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6882     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6883       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6884       .access = PL1_RW, .accessfn = access_pauth,
6885       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6886     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6887       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6888       .access = PL1_RW, .accessfn = access_pauth,
6889       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6890     REGINFO_SENTINEL
6891 };
6892 
6893 static const ARMCPRegInfo tlbirange_reginfo[] = {
6894     { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6895       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6896       .access = PL1_W, .type = ARM_CP_NO_RAW,
6897       .writefn = tlbi_aa64_rvae1is_write },
6898     { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6899       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6900       .access = PL1_W, .type = ARM_CP_NO_RAW,
6901       .writefn = tlbi_aa64_rvae1is_write },
6902    { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6903       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6904       .access = PL1_W, .type = ARM_CP_NO_RAW,
6905       .writefn = tlbi_aa64_rvae1is_write },
6906     { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6907       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6908       .access = PL1_W, .type = ARM_CP_NO_RAW,
6909       .writefn = tlbi_aa64_rvae1is_write },
6910     { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6911       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6912       .access = PL1_W, .type = ARM_CP_NO_RAW,
6913       .writefn = tlbi_aa64_rvae1is_write },
6914     { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6915       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6916       .access = PL1_W, .type = ARM_CP_NO_RAW,
6917       .writefn = tlbi_aa64_rvae1is_write },
6918    { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6919       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6920       .access = PL1_W, .type = ARM_CP_NO_RAW,
6921       .writefn = tlbi_aa64_rvae1is_write },
6922     { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6923       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6924       .access = PL1_W, .type = ARM_CP_NO_RAW,
6925       .writefn = tlbi_aa64_rvae1is_write },
6926     { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6927       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6928       .access = PL1_W, .type = ARM_CP_NO_RAW,
6929       .writefn = tlbi_aa64_rvae1_write },
6930     { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6931       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6932       .access = PL1_W, .type = ARM_CP_NO_RAW,
6933       .writefn = tlbi_aa64_rvae1_write },
6934    { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6935       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6936       .access = PL1_W, .type = ARM_CP_NO_RAW,
6937       .writefn = tlbi_aa64_rvae1_write },
6938     { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6939       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6940       .access = PL1_W, .type = ARM_CP_NO_RAW,
6941       .writefn = tlbi_aa64_rvae1_write },
6942     { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6943       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6944       .access = PL2_W, .type = ARM_CP_NOP },
6945     { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6946       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6947       .access = PL2_W, .type = ARM_CP_NOP },
6948     { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6949       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
6950       .access = PL2_W, .type = ARM_CP_NO_RAW,
6951       .writefn = tlbi_aa64_rvae2is_write },
6952    { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6953       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
6954       .access = PL2_W, .type = ARM_CP_NO_RAW,
6955       .writefn = tlbi_aa64_rvae2is_write },
6956     { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6957       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6958       .access = PL2_W, .type = ARM_CP_NOP },
6959    { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6960       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6961       .access = PL2_W, .type = ARM_CP_NOP },
6962    { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6963       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
6964       .access = PL2_W, .type = ARM_CP_NO_RAW,
6965       .writefn = tlbi_aa64_rvae2is_write },
6966    { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6967       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
6968       .access = PL2_W, .type = ARM_CP_NO_RAW,
6969       .writefn = tlbi_aa64_rvae2is_write },
6970     { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6971       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
6972       .access = PL2_W, .type = ARM_CP_NO_RAW,
6973       .writefn = tlbi_aa64_rvae2_write },
6974    { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6975       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
6976       .access = PL2_W, .type = ARM_CP_NO_RAW,
6977       .writefn = tlbi_aa64_rvae2_write },
6978    { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6979       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6980       .access = PL3_W, .type = ARM_CP_NO_RAW,
6981       .writefn = tlbi_aa64_rvae3is_write },
6982    { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6983       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6984       .access = PL3_W, .type = ARM_CP_NO_RAW,
6985       .writefn = tlbi_aa64_rvae3is_write },
6986    { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6987       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6988       .access = PL3_W, .type = ARM_CP_NO_RAW,
6989       .writefn = tlbi_aa64_rvae3is_write },
6990    { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6991       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6992       .access = PL3_W, .type = ARM_CP_NO_RAW,
6993       .writefn = tlbi_aa64_rvae3is_write },
6994    { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6995       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6996       .access = PL3_W, .type = ARM_CP_NO_RAW,
6997       .writefn = tlbi_aa64_rvae3_write },
6998    { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6999       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7000       .access = PL3_W, .type = ARM_CP_NO_RAW,
7001       .writefn = tlbi_aa64_rvae3_write },
7002     REGINFO_SENTINEL
7003 };
7004 
7005 static const ARMCPRegInfo tlbios_reginfo[] = {
7006     { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7007       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7008       .access = PL1_W, .type = ARM_CP_NO_RAW,
7009       .writefn = tlbi_aa64_vmalle1is_write },
7010     { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7011       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7012       .access = PL1_W, .type = ARM_CP_NO_RAW,
7013       .writefn = tlbi_aa64_vae1is_write },
7014     { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7015       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7016       .access = PL1_W, .type = ARM_CP_NO_RAW,
7017       .writefn = tlbi_aa64_vmalle1is_write },
7018     { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7019       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7020       .access = PL1_W, .type = ARM_CP_NO_RAW,
7021       .writefn = tlbi_aa64_vae1is_write },
7022     { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7023       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7024       .access = PL1_W, .type = ARM_CP_NO_RAW,
7025       .writefn = tlbi_aa64_vae1is_write },
7026     { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7027       .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7028       .access = PL1_W, .type = ARM_CP_NO_RAW,
7029       .writefn = tlbi_aa64_vae1is_write },
7030     { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7031       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7032       .access = PL2_W, .type = ARM_CP_NO_RAW,
7033       .writefn = tlbi_aa64_alle2is_write },
7034     { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7035       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7036       .access = PL2_W, .type = ARM_CP_NO_RAW,
7037       .writefn = tlbi_aa64_vae2is_write },
7038    { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7039       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7040       .access = PL2_W, .type = ARM_CP_NO_RAW,
7041       .writefn = tlbi_aa64_alle1is_write },
7042     { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7043       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7044       .access = PL2_W, .type = ARM_CP_NO_RAW,
7045       .writefn = tlbi_aa64_vae2is_write },
7046     { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7047       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7048       .access = PL2_W, .type = ARM_CP_NO_RAW,
7049       .writefn = tlbi_aa64_alle1is_write },
7050     { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7051       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7052       .access = PL2_W, .type = ARM_CP_NOP },
7053     { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7054       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7055       .access = PL2_W, .type = ARM_CP_NOP },
7056     { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7057       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7058       .access = PL2_W, .type = ARM_CP_NOP },
7059     { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7060       .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7061       .access = PL2_W, .type = ARM_CP_NOP },
7062     { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7063       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7064       .access = PL3_W, .type = ARM_CP_NO_RAW,
7065       .writefn = tlbi_aa64_alle3is_write },
7066     { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7067       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7068       .access = PL3_W, .type = ARM_CP_NO_RAW,
7069       .writefn = tlbi_aa64_vae3is_write },
7070     { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7071       .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7072       .access = PL3_W, .type = ARM_CP_NO_RAW,
7073       .writefn = tlbi_aa64_vae3is_write },
7074     REGINFO_SENTINEL
7075 };
7076 
7077 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7078 {
7079     Error *err = NULL;
7080     uint64_t ret;
7081 
7082     /* Success sets NZCV = 0000.  */
7083     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7084 
7085     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7086         /*
7087          * ??? Failed, for unknown reasons in the crypto subsystem.
7088          * The best we can do is log the reason and return the
7089          * timed-out indication to the guest.  There is no reason
7090          * we know to expect this failure to be transitory, so the
7091          * guest may well hang retrying the operation.
7092          */
7093         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7094                       ri->name, error_get_pretty(err));
7095         error_free(err);
7096 
7097         env->ZF = 0; /* NZCF = 0100 */
7098         return 0;
7099     }
7100     return ret;
7101 }
7102 
7103 /* We do not support re-seeding, so the two registers operate the same.  */
7104 static const ARMCPRegInfo rndr_reginfo[] = {
7105     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7106       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7107       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7108       .access = PL0_R, .readfn = rndr_readfn },
7109     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7110       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7111       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7112       .access = PL0_R, .readfn = rndr_readfn },
7113     REGINFO_SENTINEL
7114 };
7115 
7116 #ifndef CONFIG_USER_ONLY
7117 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7118                           uint64_t value)
7119 {
7120     ARMCPU *cpu = env_archcpu(env);
7121     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7122     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7123     uint64_t vaddr_in = (uint64_t) value;
7124     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7125     void *haddr;
7126     int mem_idx = cpu_mmu_index(env, false);
7127 
7128     /* This won't be crossing page boundaries */
7129     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7130     if (haddr) {
7131 
7132         ram_addr_t offset;
7133         MemoryRegion *mr;
7134 
7135         /* RCU lock is already being held */
7136         mr = memory_region_from_host(haddr, &offset);
7137 
7138         if (mr) {
7139             memory_region_writeback(mr, offset, dline_size);
7140         }
7141     }
7142 }
7143 
7144 static const ARMCPRegInfo dcpop_reg[] = {
7145     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7146       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7147       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7148       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7149     REGINFO_SENTINEL
7150 };
7151 
7152 static const ARMCPRegInfo dcpodp_reg[] = {
7153     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7154       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7155       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7156       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7157     REGINFO_SENTINEL
7158 };
7159 #endif /*CONFIG_USER_ONLY*/
7160 
7161 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7162                                        bool isread)
7163 {
7164     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7165         return CP_ACCESS_TRAP_EL2;
7166     }
7167 
7168     return CP_ACCESS_OK;
7169 }
7170 
7171 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7172                                  bool isread)
7173 {
7174     int el = arm_current_el(env);
7175 
7176     if (el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
7177         uint64_t hcr = arm_hcr_el2_eff(env);
7178         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7179             return CP_ACCESS_TRAP_EL2;
7180         }
7181     }
7182     if (el < 3 &&
7183         arm_feature(env, ARM_FEATURE_EL3) &&
7184         !(env->cp15.scr_el3 & SCR_ATA)) {
7185         return CP_ACCESS_TRAP_EL3;
7186     }
7187     return CP_ACCESS_OK;
7188 }
7189 
7190 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7191 {
7192     return env->pstate & PSTATE_TCO;
7193 }
7194 
7195 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7196 {
7197     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7198 }
7199 
7200 static const ARMCPRegInfo mte_reginfo[] = {
7201     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7202       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7203       .access = PL1_RW, .accessfn = access_mte,
7204       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7205     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7206       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7207       .access = PL1_RW, .accessfn = access_mte,
7208       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7209     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7210       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7211       .access = PL2_RW, .accessfn = access_mte,
7212       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7213     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7214       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7215       .access = PL3_RW,
7216       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7217     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7218       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7219       .access = PL1_RW, .accessfn = access_mte,
7220       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7221     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7222       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7223       .access = PL1_RW, .accessfn = access_mte,
7224       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7225     { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7226       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7227       .access = PL1_R, .accessfn = access_aa64_tid5,
7228       .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7229     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7230       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7231       .type = ARM_CP_NO_RAW,
7232       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7233     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7234       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7235       .type = ARM_CP_NOP, .access = PL1_W,
7236       .accessfn = aa64_cacheop_poc_access },
7237     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7238       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7239       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7240     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7241       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7242       .type = ARM_CP_NOP, .access = PL1_W,
7243       .accessfn = aa64_cacheop_poc_access },
7244     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7245       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7246       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7247     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7248       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7249       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7250     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7251       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7252       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7253     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7254       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7255       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7256     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7257       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7258       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7259     REGINFO_SENTINEL
7260 };
7261 
7262 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7263     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7264       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7265       .type = ARM_CP_CONST, .access = PL0_RW, },
7266     REGINFO_SENTINEL
7267 };
7268 
7269 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7270     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7271       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7272       .type = ARM_CP_NOP, .access = PL0_W,
7273       .accessfn = aa64_cacheop_poc_access },
7274     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7275       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7276       .type = ARM_CP_NOP, .access = PL0_W,
7277       .accessfn = aa64_cacheop_poc_access },
7278     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7279       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7280       .type = ARM_CP_NOP, .access = PL0_W,
7281       .accessfn = aa64_cacheop_poc_access },
7282     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7283       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7284       .type = ARM_CP_NOP, .access = PL0_W,
7285       .accessfn = aa64_cacheop_poc_access },
7286     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7287       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7288       .type = ARM_CP_NOP, .access = PL0_W,
7289       .accessfn = aa64_cacheop_poc_access },
7290     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7291       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7292       .type = ARM_CP_NOP, .access = PL0_W,
7293       .accessfn = aa64_cacheop_poc_access },
7294     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7295       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7296       .type = ARM_CP_NOP, .access = PL0_W,
7297       .accessfn = aa64_cacheop_poc_access },
7298     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7299       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7300       .type = ARM_CP_NOP, .access = PL0_W,
7301       .accessfn = aa64_cacheop_poc_access },
7302     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7303       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7304       .access = PL0_W, .type = ARM_CP_DC_GVA,
7305 #ifndef CONFIG_USER_ONLY
7306       /* Avoid overhead of an access check that always passes in user-mode */
7307       .accessfn = aa64_zva_access,
7308 #endif
7309     },
7310     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7311       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7312       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7313 #ifndef CONFIG_USER_ONLY
7314       /* Avoid overhead of an access check that always passes in user-mode */
7315       .accessfn = aa64_zva_access,
7316 #endif
7317     },
7318     REGINFO_SENTINEL
7319 };
7320 
7321 #endif
7322 
7323 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7324                                      bool isread)
7325 {
7326     int el = arm_current_el(env);
7327 
7328     if (el == 0) {
7329         uint64_t sctlr = arm_sctlr(env, el);
7330         if (!(sctlr & SCTLR_EnRCTX)) {
7331             return CP_ACCESS_TRAP;
7332         }
7333     } else if (el == 1) {
7334         uint64_t hcr = arm_hcr_el2_eff(env);
7335         if (hcr & HCR_NV) {
7336             return CP_ACCESS_TRAP_EL2;
7337         }
7338     }
7339     return CP_ACCESS_OK;
7340 }
7341 
7342 static const ARMCPRegInfo predinv_reginfo[] = {
7343     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7344       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7345       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7346     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7347       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7348       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7349     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7350       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7351       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7352     /*
7353      * Note the AArch32 opcodes have a different OPC1.
7354      */
7355     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7356       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7357       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7358     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7359       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7360       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7361     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7362       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7363       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7364     REGINFO_SENTINEL
7365 };
7366 
7367 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7368 {
7369     /* Read the high 32 bits of the current CCSIDR */
7370     return extract64(ccsidr_read(env, ri), 32, 32);
7371 }
7372 
7373 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7374     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7375       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7376       .access = PL1_R,
7377       .accessfn = access_aa64_tid2,
7378       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7379     REGINFO_SENTINEL
7380 };
7381 
7382 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7383                                        bool isread)
7384 {
7385     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7386         return CP_ACCESS_TRAP_EL2;
7387     }
7388 
7389     return CP_ACCESS_OK;
7390 }
7391 
7392 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7393                                        bool isread)
7394 {
7395     if (arm_feature(env, ARM_FEATURE_V8)) {
7396         return access_aa64_tid3(env, ri, isread);
7397     }
7398 
7399     return CP_ACCESS_OK;
7400 }
7401 
7402 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7403                                      bool isread)
7404 {
7405     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7406         return CP_ACCESS_TRAP_EL2;
7407     }
7408 
7409     return CP_ACCESS_OK;
7410 }
7411 
7412 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7413                                         const ARMCPRegInfo *ri, bool isread)
7414 {
7415     /*
7416      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7417      * in v7A, not in v8A.
7418      */
7419     if (!arm_feature(env, ARM_FEATURE_V8) &&
7420         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7421         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7422         return CP_ACCESS_TRAP_EL2;
7423     }
7424     return CP_ACCESS_OK;
7425 }
7426 
7427 static const ARMCPRegInfo jazelle_regs[] = {
7428     { .name = "JIDR",
7429       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7430       .access = PL1_R, .accessfn = access_jazelle,
7431       .type = ARM_CP_CONST, .resetvalue = 0 },
7432     { .name = "JOSCR",
7433       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7434       .accessfn = access_joscr_jmcr,
7435       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7436     { .name = "JMCR",
7437       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7438       .accessfn = access_joscr_jmcr,
7439       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7440     REGINFO_SENTINEL
7441 };
7442 
7443 static const ARMCPRegInfo vhe_reginfo[] = {
7444     { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7445       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7446       .access = PL2_RW,
7447       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
7448     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7449       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7450       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7451       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7452 #ifndef CONFIG_USER_ONLY
7453     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7454       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7455       .fieldoffset =
7456         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7457       .type = ARM_CP_IO, .access = PL2_RW,
7458       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7459     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7460       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7461       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7462       .resetfn = gt_hv_timer_reset,
7463       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7464     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7465       .type = ARM_CP_IO,
7466       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7467       .access = PL2_RW,
7468       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7469       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7470     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7471       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7472       .type = ARM_CP_IO | ARM_CP_ALIAS,
7473       .access = PL2_RW, .accessfn = e2h_access,
7474       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7475       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7476     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7477       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7478       .type = ARM_CP_IO | ARM_CP_ALIAS,
7479       .access = PL2_RW, .accessfn = e2h_access,
7480       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7481       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7482     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7483       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7484       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7485       .access = PL2_RW, .accessfn = e2h_access,
7486       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7487     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7488       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7489       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7490       .access = PL2_RW, .accessfn = e2h_access,
7491       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7492     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7493       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7494       .type = ARM_CP_IO | ARM_CP_ALIAS,
7495       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7496       .access = PL2_RW, .accessfn = e2h_access,
7497       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7498     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7499       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7500       .type = ARM_CP_IO | ARM_CP_ALIAS,
7501       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7502       .access = PL2_RW, .accessfn = e2h_access,
7503       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7504 #endif
7505     REGINFO_SENTINEL
7506 };
7507 
7508 #ifndef CONFIG_USER_ONLY
7509 static const ARMCPRegInfo ats1e1_reginfo[] = {
7510     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7511       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7512       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7513       .writefn = ats_write64 },
7514     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7515       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7516       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7517       .writefn = ats_write64 },
7518     REGINFO_SENTINEL
7519 };
7520 
7521 static const ARMCPRegInfo ats1cp_reginfo[] = {
7522     { .name = "ATS1CPRP",
7523       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7524       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7525       .writefn = ats_write },
7526     { .name = "ATS1CPWP",
7527       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7528       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7529       .writefn = ats_write },
7530     REGINFO_SENTINEL
7531 };
7532 #endif
7533 
7534 /*
7535  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7536  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7537  * is non-zero, which is never for ARMv7, optionally in ARMv8
7538  * and mandatorily for ARMv8.2 and up.
7539  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7540  * implementation is RAZ/WI we can ignore this detail, as we
7541  * do for ACTLR.
7542  */
7543 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7544     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7545       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7546       .access = PL1_RW, .accessfn = access_tacr,
7547       .type = ARM_CP_CONST, .resetvalue = 0 },
7548     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7549       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7550       .access = PL2_RW, .type = ARM_CP_CONST,
7551       .resetvalue = 0 },
7552     REGINFO_SENTINEL
7553 };
7554 
7555 void register_cp_regs_for_features(ARMCPU *cpu)
7556 {
7557     /* Register all the coprocessor registers based on feature bits */
7558     CPUARMState *env = &cpu->env;
7559     if (arm_feature(env, ARM_FEATURE_M)) {
7560         /* M profile has no coprocessor registers */
7561         return;
7562     }
7563 
7564     define_arm_cp_regs(cpu, cp_reginfo);
7565     if (!arm_feature(env, ARM_FEATURE_V8)) {
7566         /* Must go early as it is full of wildcards that may be
7567          * overridden by later definitions.
7568          */
7569         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7570     }
7571 
7572     if (arm_feature(env, ARM_FEATURE_V6)) {
7573         /* The ID registers all have impdef reset values */
7574         ARMCPRegInfo v6_idregs[] = {
7575             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7576               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7577               .access = PL1_R, .type = ARM_CP_CONST,
7578               .accessfn = access_aa32_tid3,
7579               .resetvalue = cpu->isar.id_pfr0 },
7580             /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7581              * the value of the GIC field until after we define these regs.
7582              */
7583             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7584               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7585               .access = PL1_R, .type = ARM_CP_NO_RAW,
7586               .accessfn = access_aa32_tid3,
7587               .readfn = id_pfr1_read,
7588               .writefn = arm_cp_write_ignore },
7589             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7590               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7591               .access = PL1_R, .type = ARM_CP_CONST,
7592               .accessfn = access_aa32_tid3,
7593               .resetvalue = cpu->isar.id_dfr0 },
7594             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7595               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7596               .access = PL1_R, .type = ARM_CP_CONST,
7597               .accessfn = access_aa32_tid3,
7598               .resetvalue = cpu->id_afr0 },
7599             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7600               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7601               .access = PL1_R, .type = ARM_CP_CONST,
7602               .accessfn = access_aa32_tid3,
7603               .resetvalue = cpu->isar.id_mmfr0 },
7604             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7605               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7606               .access = PL1_R, .type = ARM_CP_CONST,
7607               .accessfn = access_aa32_tid3,
7608               .resetvalue = cpu->isar.id_mmfr1 },
7609             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7610               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7611               .access = PL1_R, .type = ARM_CP_CONST,
7612               .accessfn = access_aa32_tid3,
7613               .resetvalue = cpu->isar.id_mmfr2 },
7614             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7615               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7616               .access = PL1_R, .type = ARM_CP_CONST,
7617               .accessfn = access_aa32_tid3,
7618               .resetvalue = cpu->isar.id_mmfr3 },
7619             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7620               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7621               .access = PL1_R, .type = ARM_CP_CONST,
7622               .accessfn = access_aa32_tid3,
7623               .resetvalue = cpu->isar.id_isar0 },
7624             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7625               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7626               .access = PL1_R, .type = ARM_CP_CONST,
7627               .accessfn = access_aa32_tid3,
7628               .resetvalue = cpu->isar.id_isar1 },
7629             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7630               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7631               .access = PL1_R, .type = ARM_CP_CONST,
7632               .accessfn = access_aa32_tid3,
7633               .resetvalue = cpu->isar.id_isar2 },
7634             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7635               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7636               .access = PL1_R, .type = ARM_CP_CONST,
7637               .accessfn = access_aa32_tid3,
7638               .resetvalue = cpu->isar.id_isar3 },
7639             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7640               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7641               .access = PL1_R, .type = ARM_CP_CONST,
7642               .accessfn = access_aa32_tid3,
7643               .resetvalue = cpu->isar.id_isar4 },
7644             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7645               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7646               .access = PL1_R, .type = ARM_CP_CONST,
7647               .accessfn = access_aa32_tid3,
7648               .resetvalue = cpu->isar.id_isar5 },
7649             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7650               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7651               .access = PL1_R, .type = ARM_CP_CONST,
7652               .accessfn = access_aa32_tid3,
7653               .resetvalue = cpu->isar.id_mmfr4 },
7654             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7655               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7656               .access = PL1_R, .type = ARM_CP_CONST,
7657               .accessfn = access_aa32_tid3,
7658               .resetvalue = cpu->isar.id_isar6 },
7659             REGINFO_SENTINEL
7660         };
7661         define_arm_cp_regs(cpu, v6_idregs);
7662         define_arm_cp_regs(cpu, v6_cp_reginfo);
7663     } else {
7664         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7665     }
7666     if (arm_feature(env, ARM_FEATURE_V6K)) {
7667         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7668     }
7669     if (arm_feature(env, ARM_FEATURE_V7MP) &&
7670         !arm_feature(env, ARM_FEATURE_PMSA)) {
7671         define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7672     }
7673     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7674         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7675     }
7676     if (arm_feature(env, ARM_FEATURE_V7)) {
7677         ARMCPRegInfo clidr = {
7678             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7679             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7680             .access = PL1_R, .type = ARM_CP_CONST,
7681             .accessfn = access_aa64_tid2,
7682             .resetvalue = cpu->clidr
7683         };
7684         define_one_arm_cp_reg(cpu, &clidr);
7685         define_arm_cp_regs(cpu, v7_cp_reginfo);
7686         define_debug_regs(cpu);
7687         define_pmu_regs(cpu);
7688     } else {
7689         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7690     }
7691     if (arm_feature(env, ARM_FEATURE_V8)) {
7692         /* AArch64 ID registers, which all have impdef reset values.
7693          * Note that within the ID register ranges the unused slots
7694          * must all RAZ, not UNDEF; future architecture versions may
7695          * define new registers here.
7696          */
7697         ARMCPRegInfo v8_idregs[] = {
7698             /*
7699              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7700              * emulation because we don't know the right value for the
7701              * GIC field until after we define these regs.
7702              */
7703             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7704               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7705               .access = PL1_R,
7706 #ifdef CONFIG_USER_ONLY
7707               .type = ARM_CP_CONST,
7708               .resetvalue = cpu->isar.id_aa64pfr0
7709 #else
7710               .type = ARM_CP_NO_RAW,
7711               .accessfn = access_aa64_tid3,
7712               .readfn = id_aa64pfr0_read,
7713               .writefn = arm_cp_write_ignore
7714 #endif
7715             },
7716             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7717               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7718               .access = PL1_R, .type = ARM_CP_CONST,
7719               .accessfn = access_aa64_tid3,
7720               .resetvalue = cpu->isar.id_aa64pfr1},
7721             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7722               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7723               .access = PL1_R, .type = ARM_CP_CONST,
7724               .accessfn = access_aa64_tid3,
7725               .resetvalue = 0 },
7726             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7727               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7728               .access = PL1_R, .type = ARM_CP_CONST,
7729               .accessfn = access_aa64_tid3,
7730               .resetvalue = 0 },
7731             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7732               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7733               .access = PL1_R, .type = ARM_CP_CONST,
7734               .accessfn = access_aa64_tid3,
7735               .resetvalue = cpu->isar.id_aa64zfr0 },
7736             { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7737               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7738               .access = PL1_R, .type = ARM_CP_CONST,
7739               .accessfn = access_aa64_tid3,
7740               .resetvalue = 0 },
7741             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7742               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7743               .access = PL1_R, .type = ARM_CP_CONST,
7744               .accessfn = access_aa64_tid3,
7745               .resetvalue = 0 },
7746             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7747               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7748               .access = PL1_R, .type = ARM_CP_CONST,
7749               .accessfn = access_aa64_tid3,
7750               .resetvalue = 0 },
7751             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7752               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7753               .access = PL1_R, .type = ARM_CP_CONST,
7754               .accessfn = access_aa64_tid3,
7755               .resetvalue = cpu->isar.id_aa64dfr0 },
7756             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7757               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7758               .access = PL1_R, .type = ARM_CP_CONST,
7759               .accessfn = access_aa64_tid3,
7760               .resetvalue = cpu->isar.id_aa64dfr1 },
7761             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7762               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7763               .access = PL1_R, .type = ARM_CP_CONST,
7764               .accessfn = access_aa64_tid3,
7765               .resetvalue = 0 },
7766             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7767               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7768               .access = PL1_R, .type = ARM_CP_CONST,
7769               .accessfn = access_aa64_tid3,
7770               .resetvalue = 0 },
7771             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7772               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7773               .access = PL1_R, .type = ARM_CP_CONST,
7774               .accessfn = access_aa64_tid3,
7775               .resetvalue = cpu->id_aa64afr0 },
7776             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7777               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7778               .access = PL1_R, .type = ARM_CP_CONST,
7779               .accessfn = access_aa64_tid3,
7780               .resetvalue = cpu->id_aa64afr1 },
7781             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7782               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7783               .access = PL1_R, .type = ARM_CP_CONST,
7784               .accessfn = access_aa64_tid3,
7785               .resetvalue = 0 },
7786             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7787               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7788               .access = PL1_R, .type = ARM_CP_CONST,
7789               .accessfn = access_aa64_tid3,
7790               .resetvalue = 0 },
7791             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7792               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7793               .access = PL1_R, .type = ARM_CP_CONST,
7794               .accessfn = access_aa64_tid3,
7795               .resetvalue = cpu->isar.id_aa64isar0 },
7796             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7797               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7798               .access = PL1_R, .type = ARM_CP_CONST,
7799               .accessfn = access_aa64_tid3,
7800               .resetvalue = cpu->isar.id_aa64isar1 },
7801             { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7802               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7803               .access = PL1_R, .type = ARM_CP_CONST,
7804               .accessfn = access_aa64_tid3,
7805               .resetvalue = 0 },
7806             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7807               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7808               .access = PL1_R, .type = ARM_CP_CONST,
7809               .accessfn = access_aa64_tid3,
7810               .resetvalue = 0 },
7811             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7812               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7813               .access = PL1_R, .type = ARM_CP_CONST,
7814               .accessfn = access_aa64_tid3,
7815               .resetvalue = 0 },
7816             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7817               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7818               .access = PL1_R, .type = ARM_CP_CONST,
7819               .accessfn = access_aa64_tid3,
7820               .resetvalue = 0 },
7821             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7822               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7823               .access = PL1_R, .type = ARM_CP_CONST,
7824               .accessfn = access_aa64_tid3,
7825               .resetvalue = 0 },
7826             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7827               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7828               .access = PL1_R, .type = ARM_CP_CONST,
7829               .accessfn = access_aa64_tid3,
7830               .resetvalue = 0 },
7831             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7832               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7833               .access = PL1_R, .type = ARM_CP_CONST,
7834               .accessfn = access_aa64_tid3,
7835               .resetvalue = cpu->isar.id_aa64mmfr0 },
7836             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7837               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7838               .access = PL1_R, .type = ARM_CP_CONST,
7839               .accessfn = access_aa64_tid3,
7840               .resetvalue = cpu->isar.id_aa64mmfr1 },
7841             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7842               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7843               .access = PL1_R, .type = ARM_CP_CONST,
7844               .accessfn = access_aa64_tid3,
7845               .resetvalue = cpu->isar.id_aa64mmfr2 },
7846             { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7847               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7848               .access = PL1_R, .type = ARM_CP_CONST,
7849               .accessfn = access_aa64_tid3,
7850               .resetvalue = 0 },
7851             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7852               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7853               .access = PL1_R, .type = ARM_CP_CONST,
7854               .accessfn = access_aa64_tid3,
7855               .resetvalue = 0 },
7856             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7857               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7858               .access = PL1_R, .type = ARM_CP_CONST,
7859               .accessfn = access_aa64_tid3,
7860               .resetvalue = 0 },
7861             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7862               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7863               .access = PL1_R, .type = ARM_CP_CONST,
7864               .accessfn = access_aa64_tid3,
7865               .resetvalue = 0 },
7866             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7867               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7868               .access = PL1_R, .type = ARM_CP_CONST,
7869               .accessfn = access_aa64_tid3,
7870               .resetvalue = 0 },
7871             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7872               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7873               .access = PL1_R, .type = ARM_CP_CONST,
7874               .accessfn = access_aa64_tid3,
7875               .resetvalue = cpu->isar.mvfr0 },
7876             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7877               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7878               .access = PL1_R, .type = ARM_CP_CONST,
7879               .accessfn = access_aa64_tid3,
7880               .resetvalue = cpu->isar.mvfr1 },
7881             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7882               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7883               .access = PL1_R, .type = ARM_CP_CONST,
7884               .accessfn = access_aa64_tid3,
7885               .resetvalue = cpu->isar.mvfr2 },
7886             { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7887               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7888               .access = PL1_R, .type = ARM_CP_CONST,
7889               .accessfn = access_aa64_tid3,
7890               .resetvalue = 0 },
7891             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7892               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7893               .access = PL1_R, .type = ARM_CP_CONST,
7894               .accessfn = access_aa64_tid3,
7895               .resetvalue = cpu->isar.id_pfr2 },
7896             { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7897               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7898               .access = PL1_R, .type = ARM_CP_CONST,
7899               .accessfn = access_aa64_tid3,
7900               .resetvalue = 0 },
7901             { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7902               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7903               .access = PL1_R, .type = ARM_CP_CONST,
7904               .accessfn = access_aa64_tid3,
7905               .resetvalue = 0 },
7906             { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7907               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7908               .access = PL1_R, .type = ARM_CP_CONST,
7909               .accessfn = access_aa64_tid3,
7910               .resetvalue = 0 },
7911             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7912               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7913               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7914               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7915             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7916               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7917               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7918               .resetvalue = cpu->pmceid0 },
7919             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7920               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7921               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7922               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7923             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7924               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7925               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7926               .resetvalue = cpu->pmceid1 },
7927             REGINFO_SENTINEL
7928         };
7929 #ifdef CONFIG_USER_ONLY
7930         ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7931             { .name = "ID_AA64PFR0_EL1",
7932               .exported_bits = 0x000f000f00ff0000,
7933               .fixed_bits    = 0x0000000000000011 },
7934             { .name = "ID_AA64PFR1_EL1",
7935               .exported_bits = 0x00000000000000f0 },
7936             { .name = "ID_AA64PFR*_EL1_RESERVED",
7937               .is_glob = true                     },
7938             { .name = "ID_AA64ZFR0_EL1"           },
7939             { .name = "ID_AA64MMFR0_EL1",
7940               .fixed_bits    = 0x00000000ff000000 },
7941             { .name = "ID_AA64MMFR1_EL1"          },
7942             { .name = "ID_AA64MMFR*_EL1_RESERVED",
7943               .is_glob = true                     },
7944             { .name = "ID_AA64DFR0_EL1",
7945               .fixed_bits    = 0x0000000000000006 },
7946             { .name = "ID_AA64DFR1_EL1"           },
7947             { .name = "ID_AA64DFR*_EL1_RESERVED",
7948               .is_glob = true                     },
7949             { .name = "ID_AA64AFR*",
7950               .is_glob = true                     },
7951             { .name = "ID_AA64ISAR0_EL1",
7952               .exported_bits = 0x00fffffff0fffff0 },
7953             { .name = "ID_AA64ISAR1_EL1",
7954               .exported_bits = 0x000000f0ffffffff },
7955             { .name = "ID_AA64ISAR*_EL1_RESERVED",
7956               .is_glob = true                     },
7957             REGUSERINFO_SENTINEL
7958         };
7959         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7960 #endif
7961         /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7962         if (!arm_feature(env, ARM_FEATURE_EL3) &&
7963             !arm_feature(env, ARM_FEATURE_EL2)) {
7964             ARMCPRegInfo rvbar = {
7965                 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7966                 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7967                 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
7968             };
7969             define_one_arm_cp_reg(cpu, &rvbar);
7970         }
7971         define_arm_cp_regs(cpu, v8_idregs);
7972         define_arm_cp_regs(cpu, v8_cp_reginfo);
7973     }
7974     if (arm_feature(env, ARM_FEATURE_EL2)) {
7975         uint64_t vmpidr_def = mpidr_read_val(env);
7976         ARMCPRegInfo vpidr_regs[] = {
7977             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7978               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7979               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7980               .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7981               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
7982             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7983               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7984               .access = PL2_RW, .resetvalue = cpu->midr,
7985               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7986             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7987               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7988               .access = PL2_RW, .accessfn = access_el3_aa32ns,
7989               .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7990               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
7991             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7992               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7993               .access = PL2_RW,
7994               .resetvalue = vmpidr_def,
7995               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
7996             REGINFO_SENTINEL
7997         };
7998         define_arm_cp_regs(cpu, vpidr_regs);
7999         define_arm_cp_regs(cpu, el2_cp_reginfo);
8000         if (arm_feature(env, ARM_FEATURE_V8)) {
8001             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8002         }
8003         if (cpu_isar_feature(aa64_sel2, cpu)) {
8004             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8005         }
8006         /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
8007         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8008             ARMCPRegInfo rvbar = {
8009                 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8010                 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8011                 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
8012             };
8013             define_one_arm_cp_reg(cpu, &rvbar);
8014         }
8015     } else {
8016         /* If EL2 is missing but higher ELs are enabled, we need to
8017          * register the no_el2 reginfos.
8018          */
8019         if (arm_feature(env, ARM_FEATURE_EL3)) {
8020             /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
8021              * of MIDR_EL1 and MPIDR_EL1.
8022              */
8023             ARMCPRegInfo vpidr_regs[] = {
8024                 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
8025                   .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8026                   .access = PL2_RW, .accessfn = access_el3_aa32ns,
8027                   .type = ARM_CP_CONST, .resetvalue = cpu->midr,
8028                   .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8029                 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
8030                   .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8031                   .access = PL2_RW, .accessfn = access_el3_aa32ns,
8032                   .type = ARM_CP_NO_RAW,
8033                   .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
8034                 REGINFO_SENTINEL
8035             };
8036             define_arm_cp_regs(cpu, vpidr_regs);
8037             define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
8038             if (arm_feature(env, ARM_FEATURE_V8)) {
8039                 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
8040             }
8041         }
8042     }
8043     if (arm_feature(env, ARM_FEATURE_EL3)) {
8044         define_arm_cp_regs(cpu, el3_cp_reginfo);
8045         ARMCPRegInfo el3_regs[] = {
8046             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8047               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8048               .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
8049             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8050               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8051               .access = PL3_RW,
8052               .raw_writefn = raw_write, .writefn = sctlr_write,
8053               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8054               .resetvalue = cpu->reset_sctlr },
8055             REGINFO_SENTINEL
8056         };
8057 
8058         define_arm_cp_regs(cpu, el3_regs);
8059     }
8060     /* The behaviour of NSACR is sufficiently various that we don't
8061      * try to describe it in a single reginfo:
8062      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8063      *     reads as constant 0xc00 from NS EL1 and NS EL2
8064      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8065      *  if v7 without EL3, register doesn't exist
8066      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8067      */
8068     if (arm_feature(env, ARM_FEATURE_EL3)) {
8069         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8070             ARMCPRegInfo nsacr = {
8071                 .name = "NSACR", .type = ARM_CP_CONST,
8072                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8073                 .access = PL1_RW, .accessfn = nsacr_access,
8074                 .resetvalue = 0xc00
8075             };
8076             define_one_arm_cp_reg(cpu, &nsacr);
8077         } else {
8078             ARMCPRegInfo nsacr = {
8079                 .name = "NSACR",
8080                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8081                 .access = PL3_RW | PL1_R,
8082                 .resetvalue = 0,
8083                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8084             };
8085             define_one_arm_cp_reg(cpu, &nsacr);
8086         }
8087     } else {
8088         if (arm_feature(env, ARM_FEATURE_V8)) {
8089             ARMCPRegInfo nsacr = {
8090                 .name = "NSACR", .type = ARM_CP_CONST,
8091                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8092                 .access = PL1_R,
8093                 .resetvalue = 0xc00
8094             };
8095             define_one_arm_cp_reg(cpu, &nsacr);
8096         }
8097     }
8098 
8099     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8100         if (arm_feature(env, ARM_FEATURE_V6)) {
8101             /* PMSAv6 not implemented */
8102             assert(arm_feature(env, ARM_FEATURE_V7));
8103             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8104             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8105         } else {
8106             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8107         }
8108     } else {
8109         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8110         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8111         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8112         if (cpu_isar_feature(aa32_hpd, cpu)) {
8113             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8114         }
8115     }
8116     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8117         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8118     }
8119     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8120         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8121     }
8122     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8123         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8124     }
8125     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8126         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8127     }
8128     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8129         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8130     }
8131     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8132         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8133     }
8134     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8135         define_arm_cp_regs(cpu, omap_cp_reginfo);
8136     }
8137     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8138         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8139     }
8140     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8141         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8142     }
8143     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8144         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8145     }
8146     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8147         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8148     }
8149     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8150         define_arm_cp_regs(cpu, jazelle_regs);
8151     }
8152     /* Slightly awkwardly, the OMAP and StrongARM cores need all of
8153      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8154      * be read-only (ie write causes UNDEF exception).
8155      */
8156     {
8157         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8158             /* Pre-v8 MIDR space.
8159              * Note that the MIDR isn't a simple constant register because
8160              * of the TI925 behaviour where writes to another register can
8161              * cause the MIDR value to change.
8162              *
8163              * Unimplemented registers in the c15 0 0 0 space default to
8164              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8165              * and friends override accordingly.
8166              */
8167             { .name = "MIDR",
8168               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8169               .access = PL1_R, .resetvalue = cpu->midr,
8170               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8171               .readfn = midr_read,
8172               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8173               .type = ARM_CP_OVERRIDE },
8174             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8175             { .name = "DUMMY",
8176               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8177               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8178             { .name = "DUMMY",
8179               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8180               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8181             { .name = "DUMMY",
8182               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8183               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8184             { .name = "DUMMY",
8185               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8186               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8187             { .name = "DUMMY",
8188               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8189               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8190             REGINFO_SENTINEL
8191         };
8192         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8193             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8194               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8195               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8196               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8197               .readfn = midr_read },
8198             /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8199             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8200               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8201               .access = PL1_R, .resetvalue = cpu->midr },
8202             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8203               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8204               .access = PL1_R, .resetvalue = cpu->midr },
8205             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8206               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8207               .access = PL1_R,
8208               .accessfn = access_aa64_tid1,
8209               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8210             REGINFO_SENTINEL
8211         };
8212         ARMCPRegInfo id_cp_reginfo[] = {
8213             /* These are common to v8 and pre-v8 */
8214             { .name = "CTR",
8215               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8216               .access = PL1_R, .accessfn = ctr_el0_access,
8217               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8218             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8219               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8220               .access = PL0_R, .accessfn = ctr_el0_access,
8221               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8222             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8223             { .name = "TCMTR",
8224               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8225               .access = PL1_R,
8226               .accessfn = access_aa32_tid1,
8227               .type = ARM_CP_CONST, .resetvalue = 0 },
8228             REGINFO_SENTINEL
8229         };
8230         /* TLBTR is specific to VMSA */
8231         ARMCPRegInfo id_tlbtr_reginfo = {
8232               .name = "TLBTR",
8233               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8234               .access = PL1_R,
8235               .accessfn = access_aa32_tid1,
8236               .type = ARM_CP_CONST, .resetvalue = 0,
8237         };
8238         /* MPUIR is specific to PMSA V6+ */
8239         ARMCPRegInfo id_mpuir_reginfo = {
8240               .name = "MPUIR",
8241               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8242               .access = PL1_R, .type = ARM_CP_CONST,
8243               .resetvalue = cpu->pmsav7_dregion << 8
8244         };
8245         ARMCPRegInfo crn0_wi_reginfo = {
8246             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8247             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8248             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8249         };
8250 #ifdef CONFIG_USER_ONLY
8251         ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8252             { .name = "MIDR_EL1",
8253               .exported_bits = 0x00000000ffffffff },
8254             { .name = "REVIDR_EL1"                },
8255             REGUSERINFO_SENTINEL
8256         };
8257         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8258 #endif
8259         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8260             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8261             ARMCPRegInfo *r;
8262             /* Register the blanket "writes ignored" value first to cover the
8263              * whole space. Then update the specific ID registers to allow write
8264              * access, so that they ignore writes rather than causing them to
8265              * UNDEF.
8266              */
8267             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8268             for (r = id_pre_v8_midr_cp_reginfo;
8269                  r->type != ARM_CP_SENTINEL; r++) {
8270                 r->access = PL1_RW;
8271             }
8272             for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
8273                 r->access = PL1_RW;
8274             }
8275             id_mpuir_reginfo.access = PL1_RW;
8276             id_tlbtr_reginfo.access = PL1_RW;
8277         }
8278         if (arm_feature(env, ARM_FEATURE_V8)) {
8279             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8280         } else {
8281             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8282         }
8283         define_arm_cp_regs(cpu, id_cp_reginfo);
8284         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8285             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8286         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8287             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8288         }
8289     }
8290 
8291     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8292         ARMCPRegInfo mpidr_cp_reginfo[] = {
8293             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8294               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8295               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8296             REGINFO_SENTINEL
8297         };
8298 #ifdef CONFIG_USER_ONLY
8299         ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8300             { .name = "MPIDR_EL1",
8301               .fixed_bits = 0x0000000080000000 },
8302             REGUSERINFO_SENTINEL
8303         };
8304         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8305 #endif
8306         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8307     }
8308 
8309     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8310         ARMCPRegInfo auxcr_reginfo[] = {
8311             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8312               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8313               .access = PL1_RW, .accessfn = access_tacr,
8314               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8315             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8316               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8317               .access = PL2_RW, .type = ARM_CP_CONST,
8318               .resetvalue = 0 },
8319             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8320               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8321               .access = PL3_RW, .type = ARM_CP_CONST,
8322               .resetvalue = 0 },
8323             REGINFO_SENTINEL
8324         };
8325         define_arm_cp_regs(cpu, auxcr_reginfo);
8326         if (cpu_isar_feature(aa32_ac2, cpu)) {
8327             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8328         }
8329     }
8330 
8331     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8332         /*
8333          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8334          * There are two flavours:
8335          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8336          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8337          *      32-bit register visible to AArch32 at a different encoding
8338          *      to the "flavour 1" register and with the bits rearranged to
8339          *      be able to squash a 64-bit address into the 32-bit view.
8340          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8341          * in future if we support AArch32-only configs of some of the
8342          * AArch64 cores we might need to add a specific feature flag
8343          * to indicate cores with "flavour 2" CBAR.
8344          */
8345         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8346             /* 32 bit view is [31:18] 0...0 [43:32]. */
8347             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8348                 | extract64(cpu->reset_cbar, 32, 12);
8349             ARMCPRegInfo cbar_reginfo[] = {
8350                 { .name = "CBAR",
8351                   .type = ARM_CP_CONST,
8352                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8353                   .access = PL1_R, .resetvalue = cbar32 },
8354                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8355                   .type = ARM_CP_CONST,
8356                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8357                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8358                 REGINFO_SENTINEL
8359             };
8360             /* We don't implement a r/w 64 bit CBAR currently */
8361             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8362             define_arm_cp_regs(cpu, cbar_reginfo);
8363         } else {
8364             ARMCPRegInfo cbar = {
8365                 .name = "CBAR",
8366                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8367                 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8368                 .fieldoffset = offsetof(CPUARMState,
8369                                         cp15.c15_config_base_address)
8370             };
8371             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8372                 cbar.access = PL1_R;
8373                 cbar.fieldoffset = 0;
8374                 cbar.type = ARM_CP_CONST;
8375             }
8376             define_one_arm_cp_reg(cpu, &cbar);
8377         }
8378     }
8379 
8380     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8381         ARMCPRegInfo vbar_cp_reginfo[] = {
8382             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8383               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8384               .access = PL1_RW, .writefn = vbar_write,
8385               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8386                                      offsetof(CPUARMState, cp15.vbar_ns) },
8387               .resetvalue = 0 },
8388             REGINFO_SENTINEL
8389         };
8390         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8391     }
8392 
8393     /* Generic registers whose values depend on the implementation */
8394     {
8395         ARMCPRegInfo sctlr = {
8396             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8397             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8398             .access = PL1_RW, .accessfn = access_tvm_trvm,
8399             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8400                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8401             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8402             .raw_writefn = raw_write,
8403         };
8404         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8405             /* Normally we would always end the TB on an SCTLR write, but Linux
8406              * arch/arm/mach-pxa/sleep.S expects two instructions following
8407              * an MMU enable to execute from cache.  Imitate this behaviour.
8408              */
8409             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8410         }
8411         define_one_arm_cp_reg(cpu, &sctlr);
8412     }
8413 
8414     if (cpu_isar_feature(aa64_lor, cpu)) {
8415         define_arm_cp_regs(cpu, lor_reginfo);
8416     }
8417     if (cpu_isar_feature(aa64_pan, cpu)) {
8418         define_one_arm_cp_reg(cpu, &pan_reginfo);
8419     }
8420 #ifndef CONFIG_USER_ONLY
8421     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8422         define_arm_cp_regs(cpu, ats1e1_reginfo);
8423     }
8424     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8425         define_arm_cp_regs(cpu, ats1cp_reginfo);
8426     }
8427 #endif
8428     if (cpu_isar_feature(aa64_uao, cpu)) {
8429         define_one_arm_cp_reg(cpu, &uao_reginfo);
8430     }
8431 
8432     if (cpu_isar_feature(aa64_dit, cpu)) {
8433         define_one_arm_cp_reg(cpu, &dit_reginfo);
8434     }
8435     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8436         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8437     }
8438 
8439     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8440         define_arm_cp_regs(cpu, vhe_reginfo);
8441     }
8442 
8443     if (cpu_isar_feature(aa64_sve, cpu)) {
8444         define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
8445         if (arm_feature(env, ARM_FEATURE_EL2)) {
8446             define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
8447         } else {
8448             define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
8449         }
8450         if (arm_feature(env, ARM_FEATURE_EL3)) {
8451             define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
8452         }
8453     }
8454 
8455 #ifdef TARGET_AARCH64
8456     if (cpu_isar_feature(aa64_pauth, cpu)) {
8457         define_arm_cp_regs(cpu, pauth_reginfo);
8458     }
8459     if (cpu_isar_feature(aa64_rndr, cpu)) {
8460         define_arm_cp_regs(cpu, rndr_reginfo);
8461     }
8462     if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8463         define_arm_cp_regs(cpu, tlbirange_reginfo);
8464     }
8465     if (cpu_isar_feature(aa64_tlbios, cpu)) {
8466         define_arm_cp_regs(cpu, tlbios_reginfo);
8467     }
8468 #ifndef CONFIG_USER_ONLY
8469     /* Data Cache clean instructions up to PoP */
8470     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8471         define_one_arm_cp_reg(cpu, dcpop_reg);
8472 
8473         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8474             define_one_arm_cp_reg(cpu, dcpodp_reg);
8475         }
8476     }
8477 #endif /*CONFIG_USER_ONLY*/
8478 
8479     /*
8480      * If full MTE is enabled, add all of the system registers.
8481      * If only "instructions available at EL0" are enabled,
8482      * then define only a RAZ/WI version of PSTATE.TCO.
8483      */
8484     if (cpu_isar_feature(aa64_mte, cpu)) {
8485         define_arm_cp_regs(cpu, mte_reginfo);
8486         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8487     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8488         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8489         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8490     }
8491 #endif
8492 
8493     if (cpu_isar_feature(any_predinv, cpu)) {
8494         define_arm_cp_regs(cpu, predinv_reginfo);
8495     }
8496 
8497     if (cpu_isar_feature(any_ccidx, cpu)) {
8498         define_arm_cp_regs(cpu, ccsidr2_reginfo);
8499     }
8500 
8501 #ifndef CONFIG_USER_ONLY
8502     /*
8503      * Register redirections and aliases must be done last,
8504      * after the registers from the other extensions have been defined.
8505      */
8506     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8507         define_arm_vh_e2h_redirects_aliases(cpu);
8508     }
8509 #endif
8510 }
8511 
8512 /* Sort alphabetically by type name, except for "any". */
8513 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8514 {
8515     ObjectClass *class_a = (ObjectClass *)a;
8516     ObjectClass *class_b = (ObjectClass *)b;
8517     const char *name_a, *name_b;
8518 
8519     name_a = object_class_get_name(class_a);
8520     name_b = object_class_get_name(class_b);
8521     if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8522         return 1;
8523     } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8524         return -1;
8525     } else {
8526         return strcmp(name_a, name_b);
8527     }
8528 }
8529 
8530 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8531 {
8532     ObjectClass *oc = data;
8533     const char *typename;
8534     char *name;
8535 
8536     typename = object_class_get_name(oc);
8537     name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8538     qemu_printf("  %s\n", name);
8539     g_free(name);
8540 }
8541 
8542 void arm_cpu_list(void)
8543 {
8544     GSList *list;
8545 
8546     list = object_class_get_list(TYPE_ARM_CPU, false);
8547     list = g_slist_sort(list, arm_cpu_list_compare);
8548     qemu_printf("Available CPUs:\n");
8549     g_slist_foreach(list, arm_cpu_list_entry, NULL);
8550     g_slist_free(list);
8551 }
8552 
8553 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8554 {
8555     ObjectClass *oc = data;
8556     CpuDefinitionInfoList **cpu_list = user_data;
8557     CpuDefinitionInfo *info;
8558     const char *typename;
8559 
8560     typename = object_class_get_name(oc);
8561     info = g_malloc0(sizeof(*info));
8562     info->name = g_strndup(typename,
8563                            strlen(typename) - strlen("-" TYPE_ARM_CPU));
8564     info->q_typename = g_strdup(typename);
8565 
8566     QAPI_LIST_PREPEND(*cpu_list, info);
8567 }
8568 
8569 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8570 {
8571     CpuDefinitionInfoList *cpu_list = NULL;
8572     GSList *list;
8573 
8574     list = object_class_get_list(TYPE_ARM_CPU, false);
8575     g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8576     g_slist_free(list);
8577 
8578     return cpu_list;
8579 }
8580 
8581 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8582                                    void *opaque, int state, int secstate,
8583                                    int crm, int opc1, int opc2,
8584                                    const char *name)
8585 {
8586     /* Private utility function for define_one_arm_cp_reg_with_opaque():
8587      * add a single reginfo struct to the hash table.
8588      */
8589     uint32_t *key = g_new(uint32_t, 1);
8590     ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
8591     int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
8592     int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
8593 
8594     r2->name = g_strdup(name);
8595     /* Reset the secure state to the specific incoming state.  This is
8596      * necessary as the register may have been defined with both states.
8597      */
8598     r2->secure = secstate;
8599 
8600     if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8601         /* Register is banked (using both entries in array).
8602          * Overwriting fieldoffset as the array is only used to define
8603          * banked registers but later only fieldoffset is used.
8604          */
8605         r2->fieldoffset = r->bank_fieldoffsets[ns];
8606     }
8607 
8608     if (state == ARM_CP_STATE_AA32) {
8609         if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8610             /* If the register is banked then we don't need to migrate or
8611              * reset the 32-bit instance in certain cases:
8612              *
8613              * 1) If the register has both 32-bit and 64-bit instances then we
8614              *    can count on the 64-bit instance taking care of the
8615              *    non-secure bank.
8616              * 2) If ARMv8 is enabled then we can count on a 64-bit version
8617              *    taking care of the secure bank.  This requires that separate
8618              *    32 and 64-bit definitions are provided.
8619              */
8620             if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8621                 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
8622                 r2->type |= ARM_CP_ALIAS;
8623             }
8624         } else if ((secstate != r->secure) && !ns) {
8625             /* The register is not banked so we only want to allow migration of
8626              * the non-secure instance.
8627              */
8628             r2->type |= ARM_CP_ALIAS;
8629         }
8630 
8631         if (r->state == ARM_CP_STATE_BOTH) {
8632             /* We assume it is a cp15 register if the .cp field is left unset.
8633              */
8634             if (r2->cp == 0) {
8635                 r2->cp = 15;
8636             }
8637 
8638 #ifdef HOST_WORDS_BIGENDIAN
8639             if (r2->fieldoffset) {
8640                 r2->fieldoffset += sizeof(uint32_t);
8641             }
8642 #endif
8643         }
8644     }
8645     if (state == ARM_CP_STATE_AA64) {
8646         /* To allow abbreviation of ARMCPRegInfo
8647          * definitions, we treat cp == 0 as equivalent to
8648          * the value for "standard guest-visible sysreg".
8649          * STATE_BOTH definitions are also always "standard
8650          * sysreg" in their AArch64 view (the .cp value may
8651          * be non-zero for the benefit of the AArch32 view).
8652          */
8653         if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8654             r2->cp = CP_REG_ARM64_SYSREG_CP;
8655         }
8656         *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
8657                                   r2->opc0, opc1, opc2);
8658     } else {
8659         *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
8660     }
8661     if (opaque) {
8662         r2->opaque = opaque;
8663     }
8664     /* reginfo passed to helpers is correct for the actual access,
8665      * and is never ARM_CP_STATE_BOTH:
8666      */
8667     r2->state = state;
8668     /* Make sure reginfo passed to helpers for wildcarded regs
8669      * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
8670      */
8671     r2->crm = crm;
8672     r2->opc1 = opc1;
8673     r2->opc2 = opc2;
8674     /* By convention, for wildcarded registers only the first
8675      * entry is used for migration; the others are marked as
8676      * ALIAS so we don't try to transfer the register
8677      * multiple times. Special registers (ie NOP/WFI) are
8678      * never migratable and not even raw-accessible.
8679      */
8680     if ((r->type & ARM_CP_SPECIAL)) {
8681         r2->type |= ARM_CP_NO_RAW;
8682     }
8683     if (((r->crm == CP_ANY) && crm != 0) ||
8684         ((r->opc1 == CP_ANY) && opc1 != 0) ||
8685         ((r->opc2 == CP_ANY) && opc2 != 0)) {
8686         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8687     }
8688 
8689     /* Check that raw accesses are either forbidden or handled. Note that
8690      * we can't assert this earlier because the setup of fieldoffset for
8691      * banked registers has to be done first.
8692      */
8693     if (!(r2->type & ARM_CP_NO_RAW)) {
8694         assert(!raw_accessors_invalid(r2));
8695     }
8696 
8697     /* Overriding of an existing definition must be explicitly
8698      * requested.
8699      */
8700     if (!(r->type & ARM_CP_OVERRIDE)) {
8701         ARMCPRegInfo *oldreg;
8702         oldreg = g_hash_table_lookup(cpu->cp_regs, key);
8703         if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
8704             fprintf(stderr, "Register redefined: cp=%d %d bit "
8705                     "crn=%d crm=%d opc1=%d opc2=%d, "
8706                     "was %s, now %s\n", r2->cp, 32 + 32 * is64,
8707                     r2->crn, r2->crm, r2->opc1, r2->opc2,
8708                     oldreg->name, r2->name);
8709             g_assert_not_reached();
8710         }
8711     }
8712     g_hash_table_insert(cpu->cp_regs, key, r2);
8713 }
8714 
8715 
8716 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8717                                        const ARMCPRegInfo *r, void *opaque)
8718 {
8719     /* Define implementations of coprocessor registers.
8720      * We store these in a hashtable because typically
8721      * there are less than 150 registers in a space which
8722      * is 16*16*16*8*8 = 262144 in size.
8723      * Wildcarding is supported for the crm, opc1 and opc2 fields.
8724      * If a register is defined twice then the second definition is
8725      * used, so this can be used to define some generic registers and
8726      * then override them with implementation specific variations.
8727      * At least one of the original and the second definition should
8728      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8729      * against accidental use.
8730      *
8731      * The state field defines whether the register is to be
8732      * visible in the AArch32 or AArch64 execution state. If the
8733      * state is set to ARM_CP_STATE_BOTH then we synthesise a
8734      * reginfo structure for the AArch32 view, which sees the lower
8735      * 32 bits of the 64 bit register.
8736      *
8737      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8738      * be wildcarded. AArch64 registers are always considered to be 64
8739      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8740      * the register, if any.
8741      */
8742     int crm, opc1, opc2, state;
8743     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8744     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8745     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8746     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8747     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8748     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8749     /* 64 bit registers have only CRm and Opc1 fields */
8750     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8751     /* op0 only exists in the AArch64 encodings */
8752     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8753     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8754     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8755     /*
8756      * This API is only for Arm's system coprocessors (14 and 15) or
8757      * (M-profile or v7A-and-earlier only) for implementation defined
8758      * coprocessors in the range 0..7.  Our decode assumes this, since
8759      * 8..13 can be used for other insns including VFP and Neon. See
8760      * valid_cp() in translate.c.  Assert here that we haven't tried
8761      * to use an invalid coprocessor number.
8762      */
8763     switch (r->state) {
8764     case ARM_CP_STATE_BOTH:
8765         /* 0 has a special meaning, but otherwise the same rules as AA32. */
8766         if (r->cp == 0) {
8767             break;
8768         }
8769         /* fall through */
8770     case ARM_CP_STATE_AA32:
8771         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8772             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8773             assert(r->cp >= 14 && r->cp <= 15);
8774         } else {
8775             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8776         }
8777         break;
8778     case ARM_CP_STATE_AA64:
8779         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8780         break;
8781     default:
8782         g_assert_not_reached();
8783     }
8784     /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8785      * encodes a minimum access level for the register. We roll this
8786      * runtime check into our general permission check code, so check
8787      * here that the reginfo's specified permissions are strict enough
8788      * to encompass the generic architectural permission check.
8789      */
8790     if (r->state != ARM_CP_STATE_AA32) {
8791         int mask = 0;
8792         switch (r->opc1) {
8793         case 0:
8794             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8795             mask = PL0U_R | PL1_RW;
8796             break;
8797         case 1: case 2:
8798             /* min_EL EL1 */
8799             mask = PL1_RW;
8800             break;
8801         case 3:
8802             /* min_EL EL0 */
8803             mask = PL0_RW;
8804             break;
8805         case 4:
8806         case 5:
8807             /* min_EL EL2 */
8808             mask = PL2_RW;
8809             break;
8810         case 6:
8811             /* min_EL EL3 */
8812             mask = PL3_RW;
8813             break;
8814         case 7:
8815             /* min_EL EL1, secure mode only (we don't check the latter) */
8816             mask = PL1_RW;
8817             break;
8818         default:
8819             /* broken reginfo with out-of-range opc1 */
8820             assert(false);
8821             break;
8822         }
8823         /* assert our permissions are not too lax (stricter is fine) */
8824         assert((r->access & ~mask) == 0);
8825     }
8826 
8827     /* Check that the register definition has enough info to handle
8828      * reads and writes if they are permitted.
8829      */
8830     if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
8831         if (r->access & PL3_R) {
8832             assert((r->fieldoffset ||
8833                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8834                    r->readfn);
8835         }
8836         if (r->access & PL3_W) {
8837             assert((r->fieldoffset ||
8838                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8839                    r->writefn);
8840         }
8841     }
8842     /* Bad type field probably means missing sentinel at end of reg list */
8843     assert(cptype_valid(r->type));
8844     for (crm = crmmin; crm <= crmmax; crm++) {
8845         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8846             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
8847                 for (state = ARM_CP_STATE_AA32;
8848                      state <= ARM_CP_STATE_AA64; state++) {
8849                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8850                         continue;
8851                     }
8852                     if (state == ARM_CP_STATE_AA32) {
8853                         /* Under AArch32 CP registers can be common
8854                          * (same for secure and non-secure world) or banked.
8855                          */
8856                         char *name;
8857 
8858                         switch (r->secure) {
8859                         case ARM_CP_SECSTATE_S:
8860                         case ARM_CP_SECSTATE_NS:
8861                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8862                                                    r->secure, crm, opc1, opc2,
8863                                                    r->name);
8864                             break;
8865                         default:
8866                             name = g_strdup_printf("%s_S", r->name);
8867                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8868                                                    ARM_CP_SECSTATE_S,
8869                                                    crm, opc1, opc2, name);
8870                             g_free(name);
8871                             add_cpreg_to_hashtable(cpu, r, opaque, state,
8872                                                    ARM_CP_SECSTATE_NS,
8873                                                    crm, opc1, opc2, r->name);
8874                             break;
8875                         }
8876                     } else {
8877                         /* AArch64 registers get mapped to non-secure instance
8878                          * of AArch32 */
8879                         add_cpreg_to_hashtable(cpu, r, opaque, state,
8880                                                ARM_CP_SECSTATE_NS,
8881                                                crm, opc1, opc2, r->name);
8882                     }
8883                 }
8884             }
8885         }
8886     }
8887 }
8888 
8889 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
8890                                     const ARMCPRegInfo *regs, void *opaque)
8891 {
8892     /* Define a whole list of registers */
8893     const ARMCPRegInfo *r;
8894     for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8895         define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
8896     }
8897 }
8898 
8899 /*
8900  * Modify ARMCPRegInfo for access from userspace.
8901  *
8902  * This is a data driven modification directed by
8903  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8904  * user-space cannot alter any values and dynamic values pertaining to
8905  * execution state are hidden from user space view anyway.
8906  */
8907 void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
8908 {
8909     const ARMCPRegUserSpaceInfo *m;
8910     ARMCPRegInfo *r;
8911 
8912     for (m = mods; m->name; m++) {
8913         GPatternSpec *pat = NULL;
8914         if (m->is_glob) {
8915             pat = g_pattern_spec_new(m->name);
8916         }
8917         for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8918             if (pat && g_pattern_match_string(pat, r->name)) {
8919                 r->type = ARM_CP_CONST;
8920                 r->access = PL0U_R;
8921                 r->resetvalue = 0;
8922                 /* continue */
8923             } else if (strcmp(r->name, m->name) == 0) {
8924                 r->type = ARM_CP_CONST;
8925                 r->access = PL0U_R;
8926                 r->resetvalue &= m->exported_bits;
8927                 r->resetvalue |= m->fixed_bits;
8928                 break;
8929             }
8930         }
8931         if (pat) {
8932             g_pattern_spec_free(pat);
8933         }
8934     }
8935 }
8936 
8937 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8938 {
8939     return g_hash_table_lookup(cpregs, &encoded_cp);
8940 }
8941 
8942 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8943                          uint64_t value)
8944 {
8945     /* Helper coprocessor write function for write-ignore registers */
8946 }
8947 
8948 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8949 {
8950     /* Helper coprocessor write function for read-as-zero registers */
8951     return 0;
8952 }
8953 
8954 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8955 {
8956     /* Helper coprocessor reset function for do-nothing-on-reset registers */
8957 }
8958 
8959 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
8960 {
8961     /* Return true if it is not valid for us to switch to
8962      * this CPU mode (ie all the UNPREDICTABLE cases in
8963      * the ARM ARM CPSRWriteByInstr pseudocode).
8964      */
8965 
8966     /* Changes to or from Hyp via MSR and CPS are illegal. */
8967     if (write_type == CPSRWriteByInstr &&
8968         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8969          mode == ARM_CPU_MODE_HYP)) {
8970         return 1;
8971     }
8972 
8973     switch (mode) {
8974     case ARM_CPU_MODE_USR:
8975         return 0;
8976     case ARM_CPU_MODE_SYS:
8977     case ARM_CPU_MODE_SVC:
8978     case ARM_CPU_MODE_ABT:
8979     case ARM_CPU_MODE_UND:
8980     case ARM_CPU_MODE_IRQ:
8981     case ARM_CPU_MODE_FIQ:
8982         /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8983          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8984          */
8985         /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8986          * and CPS are treated as illegal mode changes.
8987          */
8988         if (write_type == CPSRWriteByInstr &&
8989             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
8990             (arm_hcr_el2_eff(env) & HCR_TGE)) {
8991             return 1;
8992         }
8993         return 0;
8994     case ARM_CPU_MODE_HYP:
8995         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
8996     case ARM_CPU_MODE_MON:
8997         return arm_current_el(env) < 3;
8998     default:
8999         return 1;
9000     }
9001 }
9002 
9003 uint32_t cpsr_read(CPUARMState *env)
9004 {
9005     int ZF;
9006     ZF = (env->ZF == 0);
9007     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9008         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9009         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9010         | ((env->condexec_bits & 0xfc) << 8)
9011         | (env->GE << 16) | (env->daif & CPSR_AIF);
9012 }
9013 
9014 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9015                 CPSRWriteType write_type)
9016 {
9017     uint32_t changed_daif;
9018     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9019         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9020 
9021     if (mask & CPSR_NZCV) {
9022         env->ZF = (~val) & CPSR_Z;
9023         env->NF = val;
9024         env->CF = (val >> 29) & 1;
9025         env->VF = (val << 3) & 0x80000000;
9026     }
9027     if (mask & CPSR_Q)
9028         env->QF = ((val & CPSR_Q) != 0);
9029     if (mask & CPSR_T)
9030         env->thumb = ((val & CPSR_T) != 0);
9031     if (mask & CPSR_IT_0_1) {
9032         env->condexec_bits &= ~3;
9033         env->condexec_bits |= (val >> 25) & 3;
9034     }
9035     if (mask & CPSR_IT_2_7) {
9036         env->condexec_bits &= 3;
9037         env->condexec_bits |= (val >> 8) & 0xfc;
9038     }
9039     if (mask & CPSR_GE) {
9040         env->GE = (val >> 16) & 0xf;
9041     }
9042 
9043     /* In a V7 implementation that includes the security extensions but does
9044      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9045      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9046      * bits respectively.
9047      *
9048      * In a V8 implementation, it is permitted for privileged software to
9049      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9050      */
9051     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9052         arm_feature(env, ARM_FEATURE_EL3) &&
9053         !arm_feature(env, ARM_FEATURE_EL2) &&
9054         !arm_is_secure(env)) {
9055 
9056         changed_daif = (env->daif ^ val) & mask;
9057 
9058         if (changed_daif & CPSR_A) {
9059             /* Check to see if we are allowed to change the masking of async
9060              * abort exceptions from a non-secure state.
9061              */
9062             if (!(env->cp15.scr_el3 & SCR_AW)) {
9063                 qemu_log_mask(LOG_GUEST_ERROR,
9064                               "Ignoring attempt to switch CPSR_A flag from "
9065                               "non-secure world with SCR.AW bit clear\n");
9066                 mask &= ~CPSR_A;
9067             }
9068         }
9069 
9070         if (changed_daif & CPSR_F) {
9071             /* Check to see if we are allowed to change the masking of FIQ
9072              * exceptions from a non-secure state.
9073              */
9074             if (!(env->cp15.scr_el3 & SCR_FW)) {
9075                 qemu_log_mask(LOG_GUEST_ERROR,
9076                               "Ignoring attempt to switch CPSR_F flag from "
9077                               "non-secure world with SCR.FW bit clear\n");
9078                 mask &= ~CPSR_F;
9079             }
9080 
9081             /* Check whether non-maskable FIQ (NMFI) support is enabled.
9082              * If this bit is set software is not allowed to mask
9083              * FIQs, but is allowed to set CPSR_F to 0.
9084              */
9085             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9086                 (val & CPSR_F)) {
9087                 qemu_log_mask(LOG_GUEST_ERROR,
9088                               "Ignoring attempt to enable CPSR_F flag "
9089                               "(non-maskable FIQ [NMFI] support enabled)\n");
9090                 mask &= ~CPSR_F;
9091             }
9092         }
9093     }
9094 
9095     env->daif &= ~(CPSR_AIF & mask);
9096     env->daif |= val & CPSR_AIF & mask;
9097 
9098     if (write_type != CPSRWriteRaw &&
9099         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9100         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9101             /* Note that we can only get here in USR mode if this is a
9102              * gdb stub write; for this case we follow the architectural
9103              * behaviour for guest writes in USR mode of ignoring an attempt
9104              * to switch mode. (Those are caught by translate.c for writes
9105              * triggered by guest instructions.)
9106              */
9107             mask &= ~CPSR_M;
9108         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9109             /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9110              * v7, and has defined behaviour in v8:
9111              *  + leave CPSR.M untouched
9112              *  + allow changes to the other CPSR fields
9113              *  + set PSTATE.IL
9114              * For user changes via the GDB stub, we don't set PSTATE.IL,
9115              * as this would be unnecessarily harsh for a user error.
9116              */
9117             mask &= ~CPSR_M;
9118             if (write_type != CPSRWriteByGDBStub &&
9119                 arm_feature(env, ARM_FEATURE_V8)) {
9120                 mask |= CPSR_IL;
9121                 val |= CPSR_IL;
9122             }
9123             qemu_log_mask(LOG_GUEST_ERROR,
9124                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9125                           aarch32_mode_name(env->uncached_cpsr),
9126                           aarch32_mode_name(val));
9127         } else {
9128             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9129                           write_type == CPSRWriteExceptionReturn ?
9130                           "Exception return from AArch32" :
9131                           "AArch32 mode switch from",
9132                           aarch32_mode_name(env->uncached_cpsr),
9133                           aarch32_mode_name(val), env->regs[15]);
9134             switch_mode(env, val & CPSR_M);
9135         }
9136     }
9137     mask &= ~CACHED_CPSR_BITS;
9138     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9139     if (rebuild_hflags) {
9140         arm_rebuild_hflags(env);
9141     }
9142 }
9143 
9144 /* Sign/zero extend */
9145 uint32_t HELPER(sxtb16)(uint32_t x)
9146 {
9147     uint32_t res;
9148     res = (uint16_t)(int8_t)x;
9149     res |= (uint32_t)(int8_t)(x >> 16) << 16;
9150     return res;
9151 }
9152 
9153 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9154 {
9155     /*
9156      * Take a division-by-zero exception if necessary; otherwise return
9157      * to get the usual non-trapping division behaviour (result of 0)
9158      */
9159     if (arm_feature(env, ARM_FEATURE_M)
9160         && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9161         raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9162     }
9163 }
9164 
9165 uint32_t HELPER(uxtb16)(uint32_t x)
9166 {
9167     uint32_t res;
9168     res = (uint16_t)(uint8_t)x;
9169     res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9170     return res;
9171 }
9172 
9173 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9174 {
9175     if (den == 0) {
9176         handle_possible_div0_trap(env, GETPC());
9177         return 0;
9178     }
9179     if (num == INT_MIN && den == -1) {
9180         return INT_MIN;
9181     }
9182     return num / den;
9183 }
9184 
9185 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9186 {
9187     if (den == 0) {
9188         handle_possible_div0_trap(env, GETPC());
9189         return 0;
9190     }
9191     return num / den;
9192 }
9193 
9194 uint32_t HELPER(rbit)(uint32_t x)
9195 {
9196     return revbit32(x);
9197 }
9198 
9199 #ifdef CONFIG_USER_ONLY
9200 
9201 static void switch_mode(CPUARMState *env, int mode)
9202 {
9203     ARMCPU *cpu = env_archcpu(env);
9204 
9205     if (mode != ARM_CPU_MODE_USR) {
9206         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9207     }
9208 }
9209 
9210 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9211                                  uint32_t cur_el, bool secure)
9212 {
9213     return 1;
9214 }
9215 
9216 void aarch64_sync_64_to_32(CPUARMState *env)
9217 {
9218     g_assert_not_reached();
9219 }
9220 
9221 #else
9222 
9223 static void switch_mode(CPUARMState *env, int mode)
9224 {
9225     int old_mode;
9226     int i;
9227 
9228     old_mode = env->uncached_cpsr & CPSR_M;
9229     if (mode == old_mode)
9230         return;
9231 
9232     if (old_mode == ARM_CPU_MODE_FIQ) {
9233         memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9234         memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9235     } else if (mode == ARM_CPU_MODE_FIQ) {
9236         memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9237         memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9238     }
9239 
9240     i = bank_number(old_mode);
9241     env->banked_r13[i] = env->regs[13];
9242     env->banked_spsr[i] = env->spsr;
9243 
9244     i = bank_number(mode);
9245     env->regs[13] = env->banked_r13[i];
9246     env->spsr = env->banked_spsr[i];
9247 
9248     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9249     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9250 }
9251 
9252 /* Physical Interrupt Target EL Lookup Table
9253  *
9254  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9255  *
9256  * The below multi-dimensional table is used for looking up the target
9257  * exception level given numerous condition criteria.  Specifically, the
9258  * target EL is based on SCR and HCR routing controls as well as the
9259  * currently executing EL and secure state.
9260  *
9261  *    Dimensions:
9262  *    target_el_table[2][2][2][2][2][4]
9263  *                    |  |  |  |  |  +--- Current EL
9264  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9265  *                    |  |  |  +--------- HCR mask override
9266  *                    |  |  +------------ SCR exec state control
9267  *                    |  +--------------- SCR mask override
9268  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9269  *
9270  *    The table values are as such:
9271  *    0-3 = EL0-EL3
9272  *     -1 = Cannot occur
9273  *
9274  * The ARM ARM target EL table includes entries indicating that an "exception
9275  * is not taken".  The two cases where this is applicable are:
9276  *    1) An exception is taken from EL3 but the SCR does not have the exception
9277  *    routed to EL3.
9278  *    2) An exception is taken from EL2 but the HCR does not have the exception
9279  *    routed to EL2.
9280  * In these two cases, the below table contain a target of EL1.  This value is
9281  * returned as it is expected that the consumer of the table data will check
9282  * for "target EL >= current EL" to ensure the exception is not taken.
9283  *
9284  *            SCR     HCR
9285  *         64  EA     AMO                 From
9286  *        BIT IRQ     IMO      Non-secure         Secure
9287  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9288  */
9289 static const int8_t target_el_table[2][2][2][2][2][4] = {
9290     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9291        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9292       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9293        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9294      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9295        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9296       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9297        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9298     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9299        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9300       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9301        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9302      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9303        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9304       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9305        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9306 };
9307 
9308 /*
9309  * Determine the target EL for physical exceptions
9310  */
9311 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9312                                  uint32_t cur_el, bool secure)
9313 {
9314     CPUARMState *env = cs->env_ptr;
9315     bool rw;
9316     bool scr;
9317     bool hcr;
9318     int target_el;
9319     /* Is the highest EL AArch64? */
9320     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9321     uint64_t hcr_el2;
9322 
9323     if (arm_feature(env, ARM_FEATURE_EL3)) {
9324         rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9325     } else {
9326         /* Either EL2 is the highest EL (and so the EL2 register width
9327          * is given by is64); or there is no EL2 or EL3, in which case
9328          * the value of 'rw' does not affect the table lookup anyway.
9329          */
9330         rw = is64;
9331     }
9332 
9333     hcr_el2 = arm_hcr_el2_eff(env);
9334     switch (excp_idx) {
9335     case EXCP_IRQ:
9336         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9337         hcr = hcr_el2 & HCR_IMO;
9338         break;
9339     case EXCP_FIQ:
9340         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9341         hcr = hcr_el2 & HCR_FMO;
9342         break;
9343     default:
9344         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9345         hcr = hcr_el2 & HCR_AMO;
9346         break;
9347     };
9348 
9349     /*
9350      * For these purposes, TGE and AMO/IMO/FMO both force the
9351      * interrupt to EL2.  Fold TGE into the bit extracted above.
9352      */
9353     hcr |= (hcr_el2 & HCR_TGE) != 0;
9354 
9355     /* Perform a table-lookup for the target EL given the current state */
9356     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9357 
9358     assert(target_el > 0);
9359 
9360     return target_el;
9361 }
9362 
9363 void arm_log_exception(CPUState *cs)
9364 {
9365     int idx = cs->exception_index;
9366 
9367     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9368         const char *exc = NULL;
9369         static const char * const excnames[] = {
9370             [EXCP_UDEF] = "Undefined Instruction",
9371             [EXCP_SWI] = "SVC",
9372             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9373             [EXCP_DATA_ABORT] = "Data Abort",
9374             [EXCP_IRQ] = "IRQ",
9375             [EXCP_FIQ] = "FIQ",
9376             [EXCP_BKPT] = "Breakpoint",
9377             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9378             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9379             [EXCP_HVC] = "Hypervisor Call",
9380             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9381             [EXCP_SMC] = "Secure Monitor Call",
9382             [EXCP_VIRQ] = "Virtual IRQ",
9383             [EXCP_VFIQ] = "Virtual FIQ",
9384             [EXCP_SEMIHOST] = "Semihosting call",
9385             [EXCP_NOCP] = "v7M NOCP UsageFault",
9386             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9387             [EXCP_STKOF] = "v8M STKOF UsageFault",
9388             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9389             [EXCP_LSERR] = "v8M LSERR UsageFault",
9390             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9391             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9392         };
9393 
9394         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9395             exc = excnames[idx];
9396         }
9397         if (!exc) {
9398             exc = "unknown";
9399         }
9400         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9401                       idx, exc, cs->cpu_index);
9402     }
9403 }
9404 
9405 /*
9406  * Function used to synchronize QEMU's AArch64 register set with AArch32
9407  * register set.  This is necessary when switching between AArch32 and AArch64
9408  * execution state.
9409  */
9410 void aarch64_sync_32_to_64(CPUARMState *env)
9411 {
9412     int i;
9413     uint32_t mode = env->uncached_cpsr & CPSR_M;
9414 
9415     /* We can blanket copy R[0:7] to X[0:7] */
9416     for (i = 0; i < 8; i++) {
9417         env->xregs[i] = env->regs[i];
9418     }
9419 
9420     /*
9421      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9422      * Otherwise, they come from the banked user regs.
9423      */
9424     if (mode == ARM_CPU_MODE_FIQ) {
9425         for (i = 8; i < 13; i++) {
9426             env->xregs[i] = env->usr_regs[i - 8];
9427         }
9428     } else {
9429         for (i = 8; i < 13; i++) {
9430             env->xregs[i] = env->regs[i];
9431         }
9432     }
9433 
9434     /*
9435      * Registers x13-x23 are the various mode SP and FP registers. Registers
9436      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9437      * from the mode banked register.
9438      */
9439     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9440         env->xregs[13] = env->regs[13];
9441         env->xregs[14] = env->regs[14];
9442     } else {
9443         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9444         /* HYP is an exception in that it is copied from r14 */
9445         if (mode == ARM_CPU_MODE_HYP) {
9446             env->xregs[14] = env->regs[14];
9447         } else {
9448             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9449         }
9450     }
9451 
9452     if (mode == ARM_CPU_MODE_HYP) {
9453         env->xregs[15] = env->regs[13];
9454     } else {
9455         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9456     }
9457 
9458     if (mode == ARM_CPU_MODE_IRQ) {
9459         env->xregs[16] = env->regs[14];
9460         env->xregs[17] = env->regs[13];
9461     } else {
9462         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9463         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9464     }
9465 
9466     if (mode == ARM_CPU_MODE_SVC) {
9467         env->xregs[18] = env->regs[14];
9468         env->xregs[19] = env->regs[13];
9469     } else {
9470         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9471         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9472     }
9473 
9474     if (mode == ARM_CPU_MODE_ABT) {
9475         env->xregs[20] = env->regs[14];
9476         env->xregs[21] = env->regs[13];
9477     } else {
9478         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9479         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9480     }
9481 
9482     if (mode == ARM_CPU_MODE_UND) {
9483         env->xregs[22] = env->regs[14];
9484         env->xregs[23] = env->regs[13];
9485     } else {
9486         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9487         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9488     }
9489 
9490     /*
9491      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9492      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
9493      * FIQ bank for r8-r14.
9494      */
9495     if (mode == ARM_CPU_MODE_FIQ) {
9496         for (i = 24; i < 31; i++) {
9497             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
9498         }
9499     } else {
9500         for (i = 24; i < 29; i++) {
9501             env->xregs[i] = env->fiq_regs[i - 24];
9502         }
9503         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9504         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9505     }
9506 
9507     env->pc = env->regs[15];
9508 }
9509 
9510 /*
9511  * Function used to synchronize QEMU's AArch32 register set with AArch64
9512  * register set.  This is necessary when switching between AArch32 and AArch64
9513  * execution state.
9514  */
9515 void aarch64_sync_64_to_32(CPUARMState *env)
9516 {
9517     int i;
9518     uint32_t mode = env->uncached_cpsr & CPSR_M;
9519 
9520     /* We can blanket copy X[0:7] to R[0:7] */
9521     for (i = 0; i < 8; i++) {
9522         env->regs[i] = env->xregs[i];
9523     }
9524 
9525     /*
9526      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9527      * Otherwise, we copy x8-x12 into the banked user regs.
9528      */
9529     if (mode == ARM_CPU_MODE_FIQ) {
9530         for (i = 8; i < 13; i++) {
9531             env->usr_regs[i - 8] = env->xregs[i];
9532         }
9533     } else {
9534         for (i = 8; i < 13; i++) {
9535             env->regs[i] = env->xregs[i];
9536         }
9537     }
9538 
9539     /*
9540      * Registers r13 & r14 depend on the current mode.
9541      * If we are in a given mode, we copy the corresponding x registers to r13
9542      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
9543      * for the mode.
9544      */
9545     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9546         env->regs[13] = env->xregs[13];
9547         env->regs[14] = env->xregs[14];
9548     } else {
9549         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9550 
9551         /*
9552          * HYP is an exception in that it does not have its own banked r14 but
9553          * shares the USR r14
9554          */
9555         if (mode == ARM_CPU_MODE_HYP) {
9556             env->regs[14] = env->xregs[14];
9557         } else {
9558             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9559         }
9560     }
9561 
9562     if (mode == ARM_CPU_MODE_HYP) {
9563         env->regs[13] = env->xregs[15];
9564     } else {
9565         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9566     }
9567 
9568     if (mode == ARM_CPU_MODE_IRQ) {
9569         env->regs[14] = env->xregs[16];
9570         env->regs[13] = env->xregs[17];
9571     } else {
9572         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9573         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9574     }
9575 
9576     if (mode == ARM_CPU_MODE_SVC) {
9577         env->regs[14] = env->xregs[18];
9578         env->regs[13] = env->xregs[19];
9579     } else {
9580         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9581         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9582     }
9583 
9584     if (mode == ARM_CPU_MODE_ABT) {
9585         env->regs[14] = env->xregs[20];
9586         env->regs[13] = env->xregs[21];
9587     } else {
9588         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9589         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9590     }
9591 
9592     if (mode == ARM_CPU_MODE_UND) {
9593         env->regs[14] = env->xregs[22];
9594         env->regs[13] = env->xregs[23];
9595     } else {
9596         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9597         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9598     }
9599 
9600     /* Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
9601      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
9602      * FIQ bank for r8-r14.
9603      */
9604     if (mode == ARM_CPU_MODE_FIQ) {
9605         for (i = 24; i < 31; i++) {
9606             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
9607         }
9608     } else {
9609         for (i = 24; i < 29; i++) {
9610             env->fiq_regs[i - 24] = env->xregs[i];
9611         }
9612         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9613         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9614     }
9615 
9616     env->regs[15] = env->pc;
9617 }
9618 
9619 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9620                                    uint32_t mask, uint32_t offset,
9621                                    uint32_t newpc)
9622 {
9623     int new_el;
9624 
9625     /* Change the CPU state so as to actually take the exception. */
9626     switch_mode(env, new_mode);
9627 
9628     /*
9629      * For exceptions taken to AArch32 we must clear the SS bit in both
9630      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9631      */
9632     env->pstate &= ~PSTATE_SS;
9633     env->spsr = cpsr_read(env);
9634     /* Clear IT bits.  */
9635     env->condexec_bits = 0;
9636     /* Switch to the new mode, and to the correct instruction set.  */
9637     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9638 
9639     /* This must be after mode switching. */
9640     new_el = arm_current_el(env);
9641 
9642     /* Set new mode endianness */
9643     env->uncached_cpsr &= ~CPSR_E;
9644     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9645         env->uncached_cpsr |= CPSR_E;
9646     }
9647     /* J and IL must always be cleared for exception entry */
9648     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9649     env->daif |= mask;
9650 
9651     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9652         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9653             env->uncached_cpsr |= CPSR_SSBS;
9654         } else {
9655             env->uncached_cpsr &= ~CPSR_SSBS;
9656         }
9657     }
9658 
9659     if (new_mode == ARM_CPU_MODE_HYP) {
9660         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9661         env->elr_el[2] = env->regs[15];
9662     } else {
9663         /* CPSR.PAN is normally preserved preserved unless...  */
9664         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9665             switch (new_el) {
9666             case 3:
9667                 if (!arm_is_secure_below_el3(env)) {
9668                     /* ... the target is EL3, from non-secure state.  */
9669                     env->uncached_cpsr &= ~CPSR_PAN;
9670                     break;
9671                 }
9672                 /* ... the target is EL3, from secure state ... */
9673                 /* fall through */
9674             case 1:
9675                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
9676                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9677                     env->uncached_cpsr |= CPSR_PAN;
9678                 }
9679                 break;
9680             }
9681         }
9682         /*
9683          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9684          * and we should just guard the thumb mode on V4
9685          */
9686         if (arm_feature(env, ARM_FEATURE_V4T)) {
9687             env->thumb =
9688                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9689         }
9690         env->regs[14] = env->regs[15] + offset;
9691     }
9692     env->regs[15] = newpc;
9693     arm_rebuild_hflags(env);
9694 }
9695 
9696 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9697 {
9698     /*
9699      * Handle exception entry to Hyp mode; this is sufficiently
9700      * different to entry to other AArch32 modes that we handle it
9701      * separately here.
9702      *
9703      * The vector table entry used is always the 0x14 Hyp mode entry point,
9704      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9705      * The offset applied to the preferred return address is always zero
9706      * (see DDI0487C.a section G1.12.3).
9707      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9708      */
9709     uint32_t addr, mask;
9710     ARMCPU *cpu = ARM_CPU(cs);
9711     CPUARMState *env = &cpu->env;
9712 
9713     switch (cs->exception_index) {
9714     case EXCP_UDEF:
9715         addr = 0x04;
9716         break;
9717     case EXCP_SWI:
9718         addr = 0x08;
9719         break;
9720     case EXCP_BKPT:
9721         /* Fall through to prefetch abort.  */
9722     case EXCP_PREFETCH_ABORT:
9723         env->cp15.ifar_s = env->exception.vaddress;
9724         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9725                       (uint32_t)env->exception.vaddress);
9726         addr = 0x0c;
9727         break;
9728     case EXCP_DATA_ABORT:
9729         env->cp15.dfar_s = env->exception.vaddress;
9730         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9731                       (uint32_t)env->exception.vaddress);
9732         addr = 0x10;
9733         break;
9734     case EXCP_IRQ:
9735         addr = 0x18;
9736         break;
9737     case EXCP_FIQ:
9738         addr = 0x1c;
9739         break;
9740     case EXCP_HVC:
9741         addr = 0x08;
9742         break;
9743     case EXCP_HYP_TRAP:
9744         addr = 0x14;
9745         break;
9746     default:
9747         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9748     }
9749 
9750     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9751         if (!arm_feature(env, ARM_FEATURE_V8)) {
9752             /*
9753              * QEMU syndrome values are v8-style. v7 has the IL bit
9754              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9755              * If this is a v7 CPU, squash the IL bit in those cases.
9756              */
9757             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9758                 (cs->exception_index == EXCP_DATA_ABORT &&
9759                  !(env->exception.syndrome & ARM_EL_ISV)) ||
9760                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9761                 env->exception.syndrome &= ~ARM_EL_IL;
9762             }
9763         }
9764         env->cp15.esr_el[2] = env->exception.syndrome;
9765     }
9766 
9767     if (arm_current_el(env) != 2 && addr < 0x14) {
9768         addr = 0x14;
9769     }
9770 
9771     mask = 0;
9772     if (!(env->cp15.scr_el3 & SCR_EA)) {
9773         mask |= CPSR_A;
9774     }
9775     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9776         mask |= CPSR_I;
9777     }
9778     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9779         mask |= CPSR_F;
9780     }
9781 
9782     addr += env->cp15.hvbar;
9783 
9784     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9785 }
9786 
9787 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9788 {
9789     ARMCPU *cpu = ARM_CPU(cs);
9790     CPUARMState *env = &cpu->env;
9791     uint32_t addr;
9792     uint32_t mask;
9793     int new_mode;
9794     uint32_t offset;
9795     uint32_t moe;
9796 
9797     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9798     switch (syn_get_ec(env->exception.syndrome)) {
9799     case EC_BREAKPOINT:
9800     case EC_BREAKPOINT_SAME_EL:
9801         moe = 1;
9802         break;
9803     case EC_WATCHPOINT:
9804     case EC_WATCHPOINT_SAME_EL:
9805         moe = 10;
9806         break;
9807     case EC_AA32_BKPT:
9808         moe = 3;
9809         break;
9810     case EC_VECTORCATCH:
9811         moe = 5;
9812         break;
9813     default:
9814         moe = 0;
9815         break;
9816     }
9817 
9818     if (moe) {
9819         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9820     }
9821 
9822     if (env->exception.target_el == 2) {
9823         arm_cpu_do_interrupt_aarch32_hyp(cs);
9824         return;
9825     }
9826 
9827     switch (cs->exception_index) {
9828     case EXCP_UDEF:
9829         new_mode = ARM_CPU_MODE_UND;
9830         addr = 0x04;
9831         mask = CPSR_I;
9832         if (env->thumb)
9833             offset = 2;
9834         else
9835             offset = 4;
9836         break;
9837     case EXCP_SWI:
9838         new_mode = ARM_CPU_MODE_SVC;
9839         addr = 0x08;
9840         mask = CPSR_I;
9841         /* The PC already points to the next instruction.  */
9842         offset = 0;
9843         break;
9844     case EXCP_BKPT:
9845         /* Fall through to prefetch abort.  */
9846     case EXCP_PREFETCH_ABORT:
9847         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9848         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9849         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9850                       env->exception.fsr, (uint32_t)env->exception.vaddress);
9851         new_mode = ARM_CPU_MODE_ABT;
9852         addr = 0x0c;
9853         mask = CPSR_A | CPSR_I;
9854         offset = 4;
9855         break;
9856     case EXCP_DATA_ABORT:
9857         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9858         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9859         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9860                       env->exception.fsr,
9861                       (uint32_t)env->exception.vaddress);
9862         new_mode = ARM_CPU_MODE_ABT;
9863         addr = 0x10;
9864         mask = CPSR_A | CPSR_I;
9865         offset = 8;
9866         break;
9867     case EXCP_IRQ:
9868         new_mode = ARM_CPU_MODE_IRQ;
9869         addr = 0x18;
9870         /* Disable IRQ and imprecise data aborts.  */
9871         mask = CPSR_A | CPSR_I;
9872         offset = 4;
9873         if (env->cp15.scr_el3 & SCR_IRQ) {
9874             /* IRQ routed to monitor mode */
9875             new_mode = ARM_CPU_MODE_MON;
9876             mask |= CPSR_F;
9877         }
9878         break;
9879     case EXCP_FIQ:
9880         new_mode = ARM_CPU_MODE_FIQ;
9881         addr = 0x1c;
9882         /* Disable FIQ, IRQ and imprecise data aborts.  */
9883         mask = CPSR_A | CPSR_I | CPSR_F;
9884         if (env->cp15.scr_el3 & SCR_FIQ) {
9885             /* FIQ routed to monitor mode */
9886             new_mode = ARM_CPU_MODE_MON;
9887         }
9888         offset = 4;
9889         break;
9890     case EXCP_VIRQ:
9891         new_mode = ARM_CPU_MODE_IRQ;
9892         addr = 0x18;
9893         /* Disable IRQ and imprecise data aborts.  */
9894         mask = CPSR_A | CPSR_I;
9895         offset = 4;
9896         break;
9897     case EXCP_VFIQ:
9898         new_mode = ARM_CPU_MODE_FIQ;
9899         addr = 0x1c;
9900         /* Disable FIQ, IRQ and imprecise data aborts.  */
9901         mask = CPSR_A | CPSR_I | CPSR_F;
9902         offset = 4;
9903         break;
9904     case EXCP_SMC:
9905         new_mode = ARM_CPU_MODE_MON;
9906         addr = 0x08;
9907         mask = CPSR_A | CPSR_I | CPSR_F;
9908         offset = 0;
9909         break;
9910     default:
9911         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9912         return; /* Never happens.  Keep compiler happy.  */
9913     }
9914 
9915     if (new_mode == ARM_CPU_MODE_MON) {
9916         addr += env->cp15.mvbar;
9917     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9918         /* High vectors. When enabled, base address cannot be remapped. */
9919         addr += 0xffff0000;
9920     } else {
9921         /* ARM v7 architectures provide a vector base address register to remap
9922          * the interrupt vector table.
9923          * This register is only followed in non-monitor mode, and is banked.
9924          * Note: only bits 31:5 are valid.
9925          */
9926         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
9927     }
9928 
9929     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9930         env->cp15.scr_el3 &= ~SCR_NS;
9931     }
9932 
9933     take_aarch32_exception(env, new_mode, mask, offset, addr);
9934 }
9935 
9936 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9937 {
9938     /*
9939      * Return the register number of the AArch64 view of the AArch32
9940      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9941      * be that of the AArch32 mode the exception came from.
9942      */
9943     int mode = env->uncached_cpsr & CPSR_M;
9944 
9945     switch (aarch32_reg) {
9946     case 0 ... 7:
9947         return aarch32_reg;
9948     case 8 ... 12:
9949         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9950     case 13:
9951         switch (mode) {
9952         case ARM_CPU_MODE_USR:
9953         case ARM_CPU_MODE_SYS:
9954             return 13;
9955         case ARM_CPU_MODE_HYP:
9956             return 15;
9957         case ARM_CPU_MODE_IRQ:
9958             return 17;
9959         case ARM_CPU_MODE_SVC:
9960             return 19;
9961         case ARM_CPU_MODE_ABT:
9962             return 21;
9963         case ARM_CPU_MODE_UND:
9964             return 23;
9965         case ARM_CPU_MODE_FIQ:
9966             return 29;
9967         default:
9968             g_assert_not_reached();
9969         }
9970     case 14:
9971         switch (mode) {
9972         case ARM_CPU_MODE_USR:
9973         case ARM_CPU_MODE_SYS:
9974         case ARM_CPU_MODE_HYP:
9975             return 14;
9976         case ARM_CPU_MODE_IRQ:
9977             return 16;
9978         case ARM_CPU_MODE_SVC:
9979             return 18;
9980         case ARM_CPU_MODE_ABT:
9981             return 20;
9982         case ARM_CPU_MODE_UND:
9983             return 22;
9984         case ARM_CPU_MODE_FIQ:
9985             return 30;
9986         default:
9987             g_assert_not_reached();
9988         }
9989     case 15:
9990         return 31;
9991     default:
9992         g_assert_not_reached();
9993     }
9994 }
9995 
9996 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
9997 {
9998     uint32_t ret = cpsr_read(env);
9999 
10000     /* Move DIT to the correct location for SPSR_ELx */
10001     if (ret & CPSR_DIT) {
10002         ret &= ~CPSR_DIT;
10003         ret |= PSTATE_DIT;
10004     }
10005     /* Merge PSTATE.SS into SPSR_ELx */
10006     ret |= env->pstate & PSTATE_SS;
10007 
10008     return ret;
10009 }
10010 
10011 /* Handle exception entry to a target EL which is using AArch64 */
10012 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10013 {
10014     ARMCPU *cpu = ARM_CPU(cs);
10015     CPUARMState *env = &cpu->env;
10016     unsigned int new_el = env->exception.target_el;
10017     target_ulong addr = env->cp15.vbar_el[new_el];
10018     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10019     unsigned int old_mode;
10020     unsigned int cur_el = arm_current_el(env);
10021     int rt;
10022 
10023     /*
10024      * Note that new_el can never be 0.  If cur_el is 0, then
10025      * el0_a64 is is_a64(), else el0_a64 is ignored.
10026      */
10027     aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10028 
10029     if (cur_el < new_el) {
10030         /* Entry vector offset depends on whether the implemented EL
10031          * immediately lower than the target level is using AArch32 or AArch64
10032          */
10033         bool is_aa64;
10034         uint64_t hcr;
10035 
10036         switch (new_el) {
10037         case 3:
10038             is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10039             break;
10040         case 2:
10041             hcr = arm_hcr_el2_eff(env);
10042             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10043                 is_aa64 = (hcr & HCR_RW) != 0;
10044                 break;
10045             }
10046             /* fall through */
10047         case 1:
10048             is_aa64 = is_a64(env);
10049             break;
10050         default:
10051             g_assert_not_reached();
10052         }
10053 
10054         if (is_aa64) {
10055             addr += 0x400;
10056         } else {
10057             addr += 0x600;
10058         }
10059     } else if (pstate_read(env) & PSTATE_SP) {
10060         addr += 0x200;
10061     }
10062 
10063     switch (cs->exception_index) {
10064     case EXCP_PREFETCH_ABORT:
10065     case EXCP_DATA_ABORT:
10066         env->cp15.far_el[new_el] = env->exception.vaddress;
10067         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10068                       env->cp15.far_el[new_el]);
10069         /* fall through */
10070     case EXCP_BKPT:
10071     case EXCP_UDEF:
10072     case EXCP_SWI:
10073     case EXCP_HVC:
10074     case EXCP_HYP_TRAP:
10075     case EXCP_SMC:
10076         switch (syn_get_ec(env->exception.syndrome)) {
10077         case EC_ADVSIMDFPACCESSTRAP:
10078             /*
10079              * QEMU internal FP/SIMD syndromes from AArch32 include the
10080              * TA and coproc fields which are only exposed if the exception
10081              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10082              * AArch64 format syndrome.
10083              */
10084             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10085             break;
10086         case EC_CP14RTTRAP:
10087         case EC_CP15RTTRAP:
10088         case EC_CP14DTTRAP:
10089             /*
10090              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10091              * the raw register field from the insn; when taking this to
10092              * AArch64 we must convert it to the AArch64 view of the register
10093              * number. Notice that we read a 4-bit AArch32 register number and
10094              * write back a 5-bit AArch64 one.
10095              */
10096             rt = extract32(env->exception.syndrome, 5, 4);
10097             rt = aarch64_regnum(env, rt);
10098             env->exception.syndrome = deposit32(env->exception.syndrome,
10099                                                 5, 5, rt);
10100             break;
10101         case EC_CP15RRTTRAP:
10102         case EC_CP14RRTTRAP:
10103             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10104             rt = extract32(env->exception.syndrome, 5, 4);
10105             rt = aarch64_regnum(env, rt);
10106             env->exception.syndrome = deposit32(env->exception.syndrome,
10107                                                 5, 5, rt);
10108             rt = extract32(env->exception.syndrome, 10, 4);
10109             rt = aarch64_regnum(env, rt);
10110             env->exception.syndrome = deposit32(env->exception.syndrome,
10111                                                 10, 5, rt);
10112             break;
10113         }
10114         env->cp15.esr_el[new_el] = env->exception.syndrome;
10115         break;
10116     case EXCP_IRQ:
10117     case EXCP_VIRQ:
10118         addr += 0x80;
10119         break;
10120     case EXCP_FIQ:
10121     case EXCP_VFIQ:
10122         addr += 0x100;
10123         break;
10124     default:
10125         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10126     }
10127 
10128     if (is_a64(env)) {
10129         old_mode = pstate_read(env);
10130         aarch64_save_sp(env, arm_current_el(env));
10131         env->elr_el[new_el] = env->pc;
10132     } else {
10133         old_mode = cpsr_read_for_spsr_elx(env);
10134         env->elr_el[new_el] = env->regs[15];
10135 
10136         aarch64_sync_32_to_64(env);
10137 
10138         env->condexec_bits = 0;
10139     }
10140     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10141 
10142     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10143                   env->elr_el[new_el]);
10144 
10145     if (cpu_isar_feature(aa64_pan, cpu)) {
10146         /* The value of PSTATE.PAN is normally preserved, except when ... */
10147         new_mode |= old_mode & PSTATE_PAN;
10148         switch (new_el) {
10149         case 2:
10150             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10151             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10152                 != (HCR_E2H | HCR_TGE)) {
10153                 break;
10154             }
10155             /* fall through */
10156         case 1:
10157             /* ... the target is EL1 ... */
10158             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10159             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10160                 new_mode |= PSTATE_PAN;
10161             }
10162             break;
10163         }
10164     }
10165     if (cpu_isar_feature(aa64_mte, cpu)) {
10166         new_mode |= PSTATE_TCO;
10167     }
10168 
10169     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10170         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10171             new_mode |= PSTATE_SSBS;
10172         } else {
10173             new_mode &= ~PSTATE_SSBS;
10174         }
10175     }
10176 
10177     pstate_write(env, PSTATE_DAIF | new_mode);
10178     env->aarch64 = 1;
10179     aarch64_restore_sp(env, new_el);
10180     helper_rebuild_hflags_a64(env, new_el);
10181 
10182     env->pc = addr;
10183 
10184     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10185                   new_el, env->pc, pstate_read(env));
10186 }
10187 
10188 /*
10189  * Do semihosting call and set the appropriate return value. All the
10190  * permission and validity checks have been done at translate time.
10191  *
10192  * We only see semihosting exceptions in TCG only as they are not
10193  * trapped to the hypervisor in KVM.
10194  */
10195 #ifdef CONFIG_TCG
10196 static void handle_semihosting(CPUState *cs)
10197 {
10198     ARMCPU *cpu = ARM_CPU(cs);
10199     CPUARMState *env = &cpu->env;
10200 
10201     if (is_a64(env)) {
10202         qemu_log_mask(CPU_LOG_INT,
10203                       "...handling as semihosting call 0x%" PRIx64 "\n",
10204                       env->xregs[0]);
10205         env->xregs[0] = do_common_semihosting(cs);
10206         env->pc += 4;
10207     } else {
10208         qemu_log_mask(CPU_LOG_INT,
10209                       "...handling as semihosting call 0x%x\n",
10210                       env->regs[0]);
10211         env->regs[0] = do_common_semihosting(cs);
10212         env->regs[15] += env->thumb ? 2 : 4;
10213     }
10214 }
10215 #endif
10216 
10217 /* Handle a CPU exception for A and R profile CPUs.
10218  * Do any appropriate logging, handle PSCI calls, and then hand off
10219  * to the AArch64-entry or AArch32-entry function depending on the
10220  * target exception level's register width.
10221  *
10222  * Note: this is used for both TCG (as the do_interrupt tcg op),
10223  *       and KVM to re-inject guest debug exceptions, and to
10224  *       inject a Synchronous-External-Abort.
10225  */
10226 void arm_cpu_do_interrupt(CPUState *cs)
10227 {
10228     ARMCPU *cpu = ARM_CPU(cs);
10229     CPUARMState *env = &cpu->env;
10230     unsigned int new_el = env->exception.target_el;
10231 
10232     assert(!arm_feature(env, ARM_FEATURE_M));
10233 
10234     arm_log_exception(cs);
10235     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10236                   new_el);
10237     if (qemu_loglevel_mask(CPU_LOG_INT)
10238         && !excp_is_internal(cs->exception_index)) {
10239         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10240                       syn_get_ec(env->exception.syndrome),
10241                       env->exception.syndrome);
10242     }
10243 
10244     if (arm_is_psci_call(cpu, cs->exception_index)) {
10245         arm_handle_psci_call(cpu);
10246         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10247         return;
10248     }
10249 
10250     /*
10251      * Semihosting semantics depend on the register width of the code
10252      * that caused the exception, not the target exception level, so
10253      * must be handled here.
10254      */
10255 #ifdef CONFIG_TCG
10256     if (cs->exception_index == EXCP_SEMIHOST) {
10257         handle_semihosting(cs);
10258         return;
10259     }
10260 #endif
10261 
10262     /* Hooks may change global state so BQL should be held, also the
10263      * BQL needs to be held for any modification of
10264      * cs->interrupt_request.
10265      */
10266     g_assert(qemu_mutex_iothread_locked());
10267 
10268     arm_call_pre_el_change_hook(cpu);
10269 
10270     assert(!excp_is_internal(cs->exception_index));
10271     if (arm_el_is_aa64(env, new_el)) {
10272         arm_cpu_do_interrupt_aarch64(cs);
10273     } else {
10274         arm_cpu_do_interrupt_aarch32(cs);
10275     }
10276 
10277     arm_call_el_change_hook(cpu);
10278 
10279     if (!kvm_enabled()) {
10280         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10281     }
10282 }
10283 #endif /* !CONFIG_USER_ONLY */
10284 
10285 uint64_t arm_sctlr(CPUARMState *env, int el)
10286 {
10287     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10288     if (el == 0) {
10289         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10290         el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10291              ? 2 : 1;
10292     }
10293     return env->cp15.sctlr_el[el];
10294 }
10295 
10296 /* Return the SCTLR value which controls this address translation regime */
10297 static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
10298 {
10299     return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
10300 }
10301 
10302 #ifndef CONFIG_USER_ONLY
10303 
10304 /* Return true if the specified stage of address translation is disabled */
10305 static inline bool regime_translation_disabled(CPUARMState *env,
10306                                                ARMMMUIdx mmu_idx)
10307 {
10308     uint64_t hcr_el2;
10309 
10310     if (arm_feature(env, ARM_FEATURE_M)) {
10311         switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
10312                 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
10313         case R_V7M_MPU_CTRL_ENABLE_MASK:
10314             /* Enabled, but not for HardFault and NMI */
10315             return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
10316         case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
10317             /* Enabled for all cases */
10318             return false;
10319         case 0:
10320         default:
10321             /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
10322              * we warned about that in armv7m_nvic.c when the guest set it.
10323              */
10324             return true;
10325         }
10326     }
10327 
10328     hcr_el2 = arm_hcr_el2_eff(env);
10329 
10330     if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10331         /* HCR.DC means HCR.VM behaves as 1 */
10332         return (hcr_el2 & (HCR_DC | HCR_VM)) == 0;
10333     }
10334 
10335     if (hcr_el2 & HCR_TGE) {
10336         /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
10337         if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
10338             return true;
10339         }
10340     }
10341 
10342     if ((hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
10343         /* HCR.DC means SCTLR_EL1.M behaves as 0 */
10344         return true;
10345     }
10346 
10347     return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
10348 }
10349 
10350 static inline bool regime_translation_big_endian(CPUARMState *env,
10351                                                  ARMMMUIdx mmu_idx)
10352 {
10353     return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
10354 }
10355 
10356 /* Return the TTBR associated with this translation regime */
10357 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
10358                                    int ttbrn)
10359 {
10360     if (mmu_idx == ARMMMUIdx_Stage2) {
10361         return env->cp15.vttbr_el2;
10362     }
10363     if (mmu_idx == ARMMMUIdx_Stage2_S) {
10364         return env->cp15.vsttbr_el2;
10365     }
10366     if (ttbrn == 0) {
10367         return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
10368     } else {
10369         return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
10370     }
10371 }
10372 
10373 #endif /* !CONFIG_USER_ONLY */
10374 
10375 /* Convert a possible stage1+2 MMU index into the appropriate
10376  * stage 1 MMU index
10377  */
10378 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
10379 {
10380     switch (mmu_idx) {
10381     case ARMMMUIdx_SE10_0:
10382         return ARMMMUIdx_Stage1_SE0;
10383     case ARMMMUIdx_SE10_1:
10384         return ARMMMUIdx_Stage1_SE1;
10385     case ARMMMUIdx_SE10_1_PAN:
10386         return ARMMMUIdx_Stage1_SE1_PAN;
10387     case ARMMMUIdx_E10_0:
10388         return ARMMMUIdx_Stage1_E0;
10389     case ARMMMUIdx_E10_1:
10390         return ARMMMUIdx_Stage1_E1;
10391     case ARMMMUIdx_E10_1_PAN:
10392         return ARMMMUIdx_Stage1_E1_PAN;
10393     default:
10394         return mmu_idx;
10395     }
10396 }
10397 
10398 /* Return true if the translation regime is using LPAE format page tables */
10399 static inline bool regime_using_lpae_format(CPUARMState *env,
10400                                             ARMMMUIdx mmu_idx)
10401 {
10402     int el = regime_el(env, mmu_idx);
10403     if (el == 2 || arm_el_is_aa64(env, el)) {
10404         return true;
10405     }
10406     if (arm_feature(env, ARM_FEATURE_LPAE)
10407         && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
10408         return true;
10409     }
10410     return false;
10411 }
10412 
10413 /* Returns true if the stage 1 translation regime is using LPAE format page
10414  * tables. Used when raising alignment exceptions, whose FSR changes depending
10415  * on whether the long or short descriptor format is in use. */
10416 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
10417 {
10418     mmu_idx = stage_1_mmu_idx(mmu_idx);
10419 
10420     return regime_using_lpae_format(env, mmu_idx);
10421 }
10422 
10423 #ifndef CONFIG_USER_ONLY
10424 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
10425 {
10426     switch (mmu_idx) {
10427     case ARMMMUIdx_SE10_0:
10428     case ARMMMUIdx_E20_0:
10429     case ARMMMUIdx_SE20_0:
10430     case ARMMMUIdx_Stage1_E0:
10431     case ARMMMUIdx_Stage1_SE0:
10432     case ARMMMUIdx_MUser:
10433     case ARMMMUIdx_MSUser:
10434     case ARMMMUIdx_MUserNegPri:
10435     case ARMMMUIdx_MSUserNegPri:
10436         return true;
10437     default:
10438         return false;
10439     case ARMMMUIdx_E10_0:
10440     case ARMMMUIdx_E10_1:
10441     case ARMMMUIdx_E10_1_PAN:
10442         g_assert_not_reached();
10443     }
10444 }
10445 
10446 /* Translate section/page access permissions to page
10447  * R/W protection flags
10448  *
10449  * @env:         CPUARMState
10450  * @mmu_idx:     MMU index indicating required translation regime
10451  * @ap:          The 3-bit access permissions (AP[2:0])
10452  * @domain_prot: The 2-bit domain access permissions
10453  */
10454 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
10455                                 int ap, int domain_prot)
10456 {
10457     bool is_user = regime_is_user(env, mmu_idx);
10458 
10459     if (domain_prot == 3) {
10460         return PAGE_READ | PAGE_WRITE;
10461     }
10462 
10463     switch (ap) {
10464     case 0:
10465         if (arm_feature(env, ARM_FEATURE_V7)) {
10466             return 0;
10467         }
10468         switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
10469         case SCTLR_S:
10470             return is_user ? 0 : PAGE_READ;
10471         case SCTLR_R:
10472             return PAGE_READ;
10473         default:
10474             return 0;
10475         }
10476     case 1:
10477         return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10478     case 2:
10479         if (is_user) {
10480             return PAGE_READ;
10481         } else {
10482             return PAGE_READ | PAGE_WRITE;
10483         }
10484     case 3:
10485         return PAGE_READ | PAGE_WRITE;
10486     case 4: /* Reserved.  */
10487         return 0;
10488     case 5:
10489         return is_user ? 0 : PAGE_READ;
10490     case 6:
10491         return PAGE_READ;
10492     case 7:
10493         if (!arm_feature(env, ARM_FEATURE_V6K)) {
10494             return 0;
10495         }
10496         return PAGE_READ;
10497     default:
10498         g_assert_not_reached();
10499     }
10500 }
10501 
10502 /* Translate section/page access permissions to page
10503  * R/W protection flags.
10504  *
10505  * @ap:      The 2-bit simple AP (AP[2:1])
10506  * @is_user: TRUE if accessing from PL0
10507  */
10508 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
10509 {
10510     switch (ap) {
10511     case 0:
10512         return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10513     case 1:
10514         return PAGE_READ | PAGE_WRITE;
10515     case 2:
10516         return is_user ? 0 : PAGE_READ;
10517     case 3:
10518         return PAGE_READ;
10519     default:
10520         g_assert_not_reached();
10521     }
10522 }
10523 
10524 static inline int
10525 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10526 {
10527     return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10528 }
10529 
10530 /* Translate S2 section/page access permissions to protection flags
10531  *
10532  * @env:     CPUARMState
10533  * @s2ap:    The 2-bit stage2 access permissions (S2AP)
10534  * @xn:      XN (execute-never) bits
10535  * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
10536  */
10537 static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
10538 {
10539     int prot = 0;
10540 
10541     if (s2ap & 1) {
10542         prot |= PAGE_READ;
10543     }
10544     if (s2ap & 2) {
10545         prot |= PAGE_WRITE;
10546     }
10547 
10548     if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
10549         switch (xn) {
10550         case 0:
10551             prot |= PAGE_EXEC;
10552             break;
10553         case 1:
10554             if (s1_is_el0) {
10555                 prot |= PAGE_EXEC;
10556             }
10557             break;
10558         case 2:
10559             break;
10560         case 3:
10561             if (!s1_is_el0) {
10562                 prot |= PAGE_EXEC;
10563             }
10564             break;
10565         default:
10566             g_assert_not_reached();
10567         }
10568     } else {
10569         if (!extract32(xn, 1, 1)) {
10570             if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10571                 prot |= PAGE_EXEC;
10572             }
10573         }
10574     }
10575     return prot;
10576 }
10577 
10578 /* Translate section/page access permissions to protection flags
10579  *
10580  * @env:     CPUARMState
10581  * @mmu_idx: MMU index indicating required translation regime
10582  * @is_aa64: TRUE if AArch64
10583  * @ap:      The 2-bit simple AP (AP[2:1])
10584  * @ns:      NS (non-secure) bit
10585  * @xn:      XN (execute-never) bit
10586  * @pxn:     PXN (privileged execute-never) bit
10587  */
10588 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10589                       int ap, int ns, int xn, int pxn)
10590 {
10591     bool is_user = regime_is_user(env, mmu_idx);
10592     int prot_rw, user_rw;
10593     bool have_wxn;
10594     int wxn = 0;
10595 
10596     assert(mmu_idx != ARMMMUIdx_Stage2);
10597     assert(mmu_idx != ARMMMUIdx_Stage2_S);
10598 
10599     user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10600     if (is_user) {
10601         prot_rw = user_rw;
10602     } else {
10603         if (user_rw && regime_is_pan(env, mmu_idx)) {
10604             /* PAN forbids data accesses but doesn't affect insn fetch */
10605             prot_rw = 0;
10606         } else {
10607             prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
10608         }
10609     }
10610 
10611     if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10612         return prot_rw;
10613     }
10614 
10615     /* TODO have_wxn should be replaced with
10616      *   ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10617      * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10618      * compatible processors have EL2, which is required for [U]WXN.
10619      */
10620     have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10621 
10622     if (have_wxn) {
10623         wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10624     }
10625 
10626     if (is_aa64) {
10627         if (regime_has_2_ranges(mmu_idx) && !is_user) {
10628             xn = pxn || (user_rw & PAGE_WRITE);
10629         }
10630     } else if (arm_feature(env, ARM_FEATURE_V7)) {
10631         switch (regime_el(env, mmu_idx)) {
10632         case 1:
10633         case 3:
10634             if (is_user) {
10635                 xn = xn || !(user_rw & PAGE_READ);
10636             } else {
10637                 int uwxn = 0;
10638                 if (have_wxn) {
10639                     uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10640                 }
10641                 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10642                      (uwxn && (user_rw & PAGE_WRITE));
10643             }
10644             break;
10645         case 2:
10646             break;
10647         }
10648     } else {
10649         xn = wxn = 0;
10650     }
10651 
10652     if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10653         return prot_rw;
10654     }
10655     return prot_rw | PAGE_EXEC;
10656 }
10657 
10658 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10659                                      uint32_t *table, uint32_t address)
10660 {
10661     /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
10662     TCR *tcr = regime_tcr(env, mmu_idx);
10663 
10664     if (address & tcr->mask) {
10665         if (tcr->raw_tcr & TTBCR_PD1) {
10666             /* Translation table walk disabled for TTBR1 */
10667             return false;
10668         }
10669         *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
10670     } else {
10671         if (tcr->raw_tcr & TTBCR_PD0) {
10672             /* Translation table walk disabled for TTBR0 */
10673             return false;
10674         }
10675         *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
10676     }
10677     *table |= (address >> 18) & 0x3ffc;
10678     return true;
10679 }
10680 
10681 /* Translate a S1 pagetable walk through S2 if needed.  */
10682 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
10683                                hwaddr addr, bool *is_secure,
10684                                ARMMMUFaultInfo *fi)
10685 {
10686     if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
10687         !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
10688         target_ulong s2size;
10689         hwaddr s2pa;
10690         int s2prot;
10691         int ret;
10692         ARMMMUIdx s2_mmu_idx = *is_secure ? ARMMMUIdx_Stage2_S
10693                                           : ARMMMUIdx_Stage2;
10694         ARMCacheAttrs cacheattrs = {};
10695         MemTxAttrs txattrs = {};
10696 
10697         ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, s2_mmu_idx, false,
10698                                  &s2pa, &txattrs, &s2prot, &s2size, fi,
10699                                  &cacheattrs);
10700         if (ret) {
10701             assert(fi->type != ARMFault_None);
10702             fi->s2addr = addr;
10703             fi->stage2 = true;
10704             fi->s1ptw = true;
10705             fi->s1ns = !*is_secure;
10706             return ~0;
10707         }
10708         if ((arm_hcr_el2_eff(env) & HCR_PTW) &&
10709             (cacheattrs.attrs & 0xf0) == 0) {
10710             /*
10711              * PTW set and S1 walk touched S2 Device memory:
10712              * generate Permission fault.
10713              */
10714             fi->type = ARMFault_Permission;
10715             fi->s2addr = addr;
10716             fi->stage2 = true;
10717             fi->s1ptw = true;
10718             fi->s1ns = !*is_secure;
10719             return ~0;
10720         }
10721 
10722         if (arm_is_secure_below_el3(env)) {
10723             /* Check if page table walk is to secure or non-secure PA space. */
10724             if (*is_secure) {
10725                 *is_secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
10726             } else {
10727                 *is_secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
10728             }
10729         } else {
10730             assert(!*is_secure);
10731         }
10732 
10733         addr = s2pa;
10734     }
10735     return addr;
10736 }
10737 
10738 /* All loads done in the course of a page table walk go through here. */
10739 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
10740                             ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
10741 {
10742     ARMCPU *cpu = ARM_CPU(cs);
10743     CPUARMState *env = &cpu->env;
10744     MemTxAttrs attrs = {};
10745     MemTxResult result = MEMTX_OK;
10746     AddressSpace *as;
10747     uint32_t data;
10748 
10749     addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
10750     attrs.secure = is_secure;
10751     as = arm_addressspace(cs, attrs);
10752     if (fi->s1ptw) {
10753         return 0;
10754     }
10755     if (regime_translation_big_endian(env, mmu_idx)) {
10756         data = address_space_ldl_be(as, addr, attrs, &result);
10757     } else {
10758         data = address_space_ldl_le(as, addr, attrs, &result);
10759     }
10760     if (result == MEMTX_OK) {
10761         return data;
10762     }
10763     fi->type = ARMFault_SyncExternalOnWalk;
10764     fi->ea = arm_extabort_type(result);
10765     return 0;
10766 }
10767 
10768 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
10769                             ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
10770 {
10771     ARMCPU *cpu = ARM_CPU(cs);
10772     CPUARMState *env = &cpu->env;
10773     MemTxAttrs attrs = {};
10774     MemTxResult result = MEMTX_OK;
10775     AddressSpace *as;
10776     uint64_t data;
10777 
10778     addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
10779     attrs.secure = is_secure;
10780     as = arm_addressspace(cs, attrs);
10781     if (fi->s1ptw) {
10782         return 0;
10783     }
10784     if (regime_translation_big_endian(env, mmu_idx)) {
10785         data = address_space_ldq_be(as, addr, attrs, &result);
10786     } else {
10787         data = address_space_ldq_le(as, addr, attrs, &result);
10788     }
10789     if (result == MEMTX_OK) {
10790         return data;
10791     }
10792     fi->type = ARMFault_SyncExternalOnWalk;
10793     fi->ea = arm_extabort_type(result);
10794     return 0;
10795 }
10796 
10797 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
10798                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
10799                              hwaddr *phys_ptr, int *prot,
10800                              target_ulong *page_size,
10801                              ARMMMUFaultInfo *fi)
10802 {
10803     CPUState *cs = env_cpu(env);
10804     int level = 1;
10805     uint32_t table;
10806     uint32_t desc;
10807     int type;
10808     int ap;
10809     int domain = 0;
10810     int domain_prot;
10811     hwaddr phys_addr;
10812     uint32_t dacr;
10813 
10814     /* Pagetable walk.  */
10815     /* Lookup l1 descriptor.  */
10816     if (!get_level1_table_address(env, mmu_idx, &table, address)) {
10817         /* Section translation fault if page walk is disabled by PD0 or PD1 */
10818         fi->type = ARMFault_Translation;
10819         goto do_fault;
10820     }
10821     desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10822                        mmu_idx, fi);
10823     if (fi->type != ARMFault_None) {
10824         goto do_fault;
10825     }
10826     type = (desc & 3);
10827     domain = (desc >> 5) & 0x0f;
10828     if (regime_el(env, mmu_idx) == 1) {
10829         dacr = env->cp15.dacr_ns;
10830     } else {
10831         dacr = env->cp15.dacr_s;
10832     }
10833     domain_prot = (dacr >> (domain * 2)) & 3;
10834     if (type == 0) {
10835         /* Section translation fault.  */
10836         fi->type = ARMFault_Translation;
10837         goto do_fault;
10838     }
10839     if (type != 2) {
10840         level = 2;
10841     }
10842     if (domain_prot == 0 || domain_prot == 2) {
10843         fi->type = ARMFault_Domain;
10844         goto do_fault;
10845     }
10846     if (type == 2) {
10847         /* 1Mb section.  */
10848         phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10849         ap = (desc >> 10) & 3;
10850         *page_size = 1024 * 1024;
10851     } else {
10852         /* Lookup l2 entry.  */
10853         if (type == 1) {
10854             /* Coarse pagetable.  */
10855             table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10856         } else {
10857             /* Fine pagetable.  */
10858             table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10859         }
10860         desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10861                            mmu_idx, fi);
10862         if (fi->type != ARMFault_None) {
10863             goto do_fault;
10864         }
10865         switch (desc & 3) {
10866         case 0: /* Page translation fault.  */
10867             fi->type = ARMFault_Translation;
10868             goto do_fault;
10869         case 1: /* 64k page.  */
10870             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10871             ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
10872             *page_size = 0x10000;
10873             break;
10874         case 2: /* 4k page.  */
10875             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10876             ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
10877             *page_size = 0x1000;
10878             break;
10879         case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
10880             if (type == 1) {
10881                 /* ARMv6/XScale extended small page format */
10882                 if (arm_feature(env, ARM_FEATURE_XSCALE)
10883                     || arm_feature(env, ARM_FEATURE_V6)) {
10884                     phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10885                     *page_size = 0x1000;
10886                 } else {
10887                     /* UNPREDICTABLE in ARMv5; we choose to take a
10888                      * page translation fault.
10889                      */
10890                     fi->type = ARMFault_Translation;
10891                     goto do_fault;
10892                 }
10893             } else {
10894                 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
10895                 *page_size = 0x400;
10896             }
10897             ap = (desc >> 4) & 3;
10898             break;
10899         default:
10900             /* Never happens, but compiler isn't smart enough to tell.  */
10901             abort();
10902         }
10903     }
10904     *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10905     *prot |= *prot ? PAGE_EXEC : 0;
10906     if (!(*prot & (1 << access_type))) {
10907         /* Access permission fault.  */
10908         fi->type = ARMFault_Permission;
10909         goto do_fault;
10910     }
10911     *phys_ptr = phys_addr;
10912     return false;
10913 do_fault:
10914     fi->domain = domain;
10915     fi->level = level;
10916     return true;
10917 }
10918 
10919 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
10920                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
10921                              hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
10922                              target_ulong *page_size, ARMMMUFaultInfo *fi)
10923 {
10924     CPUState *cs = env_cpu(env);
10925     ARMCPU *cpu = env_archcpu(env);
10926     int level = 1;
10927     uint32_t table;
10928     uint32_t desc;
10929     uint32_t xn;
10930     uint32_t pxn = 0;
10931     int type;
10932     int ap;
10933     int domain = 0;
10934     int domain_prot;
10935     hwaddr phys_addr;
10936     uint32_t dacr;
10937     bool ns;
10938 
10939     /* Pagetable walk.  */
10940     /* Lookup l1 descriptor.  */
10941     if (!get_level1_table_address(env, mmu_idx, &table, address)) {
10942         /* Section translation fault if page walk is disabled by PD0 or PD1 */
10943         fi->type = ARMFault_Translation;
10944         goto do_fault;
10945     }
10946     desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10947                        mmu_idx, fi);
10948     if (fi->type != ARMFault_None) {
10949         goto do_fault;
10950     }
10951     type = (desc & 3);
10952     if (type == 0 || (type == 3 && !cpu_isar_feature(aa32_pxn, cpu))) {
10953         /* Section translation fault, or attempt to use the encoding
10954          * which is Reserved on implementations without PXN.
10955          */
10956         fi->type = ARMFault_Translation;
10957         goto do_fault;
10958     }
10959     if ((type == 1) || !(desc & (1 << 18))) {
10960         /* Page or Section.  */
10961         domain = (desc >> 5) & 0x0f;
10962     }
10963     if (regime_el(env, mmu_idx) == 1) {
10964         dacr = env->cp15.dacr_ns;
10965     } else {
10966         dacr = env->cp15.dacr_s;
10967     }
10968     if (type == 1) {
10969         level = 2;
10970     }
10971     domain_prot = (dacr >> (domain * 2)) & 3;
10972     if (domain_prot == 0 || domain_prot == 2) {
10973         /* Section or Page domain fault */
10974         fi->type = ARMFault_Domain;
10975         goto do_fault;
10976     }
10977     if (type != 1) {
10978         if (desc & (1 << 18)) {
10979             /* Supersection.  */
10980             phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
10981             phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10982             phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
10983             *page_size = 0x1000000;
10984         } else {
10985             /* Section.  */
10986             phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10987             *page_size = 0x100000;
10988         }
10989         ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10990         xn = desc & (1 << 4);
10991         pxn = desc & 1;
10992         ns = extract32(desc, 19, 1);
10993     } else {
10994         if (cpu_isar_feature(aa32_pxn, cpu)) {
10995             pxn = (desc >> 2) & 1;
10996         }
10997         ns = extract32(desc, 3, 1);
10998         /* Lookup l2 entry.  */
10999         table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
11000         desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
11001                            mmu_idx, fi);
11002         if (fi->type != ARMFault_None) {
11003             goto do_fault;
11004         }
11005         ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
11006         switch (desc & 3) {
11007         case 0: /* Page translation fault.  */
11008             fi->type = ARMFault_Translation;
11009             goto do_fault;
11010         case 1: /* 64k page.  */
11011             phys_addr = (desc & 0xffff0000) | (address & 0xffff);
11012             xn = desc & (1 << 15);
11013             *page_size = 0x10000;
11014             break;
11015         case 2: case 3: /* 4k page.  */
11016             phys_addr = (desc & 0xfffff000) | (address & 0xfff);
11017             xn = desc & 1;
11018             *page_size = 0x1000;
11019             break;
11020         default:
11021             /* Never happens, but compiler isn't smart enough to tell.  */
11022             abort();
11023         }
11024     }
11025     if (domain_prot == 3) {
11026         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11027     } else {
11028         if (pxn && !regime_is_user(env, mmu_idx)) {
11029             xn = 1;
11030         }
11031         if (xn && access_type == MMU_INST_FETCH) {
11032             fi->type = ARMFault_Permission;
11033             goto do_fault;
11034         }
11035 
11036         if (arm_feature(env, ARM_FEATURE_V6K) &&
11037                 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
11038             /* The simplified model uses AP[0] as an access control bit.  */
11039             if ((ap & 1) == 0) {
11040                 /* Access flag fault.  */
11041                 fi->type = ARMFault_AccessFlag;
11042                 goto do_fault;
11043             }
11044             *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
11045         } else {
11046             *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
11047         }
11048         if (*prot && !xn) {
11049             *prot |= PAGE_EXEC;
11050         }
11051         if (!(*prot & (1 << access_type))) {
11052             /* Access permission fault.  */
11053             fi->type = ARMFault_Permission;
11054             goto do_fault;
11055         }
11056     }
11057     if (ns) {
11058         /* The NS bit will (as required by the architecture) have no effect if
11059          * the CPU doesn't support TZ or this is a non-secure translation
11060          * regime, because the attribute will already be non-secure.
11061          */
11062         attrs->secure = false;
11063     }
11064     *phys_ptr = phys_addr;
11065     return false;
11066 do_fault:
11067     fi->domain = domain;
11068     fi->level = level;
11069     return true;
11070 }
11071 
11072 /*
11073  * check_s2_mmu_setup
11074  * @cpu:        ARMCPU
11075  * @is_aa64:    True if the translation regime is in AArch64 state
11076  * @startlevel: Suggested starting level
11077  * @inputsize:  Bitsize of IPAs
11078  * @stride:     Page-table stride (See the ARM ARM)
11079  *
11080  * Returns true if the suggested S2 translation parameters are OK and
11081  * false otherwise.
11082  */
11083 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
11084                                int inputsize, int stride, int outputsize)
11085 {
11086     const int grainsize = stride + 3;
11087     int startsizecheck;
11088 
11089     /* Negative levels are never allowed.  */
11090     if (level < 0) {
11091         return false;
11092     }
11093 
11094     startsizecheck = inputsize - ((3 - level) * stride + grainsize);
11095     if (startsizecheck < 1 || startsizecheck > stride + 4) {
11096         return false;
11097     }
11098 
11099     if (is_aa64) {
11100         switch (stride) {
11101         case 13: /* 64KB Pages.  */
11102             if (level == 0 || (level == 1 && outputsize <= 42)) {
11103                 return false;
11104             }
11105             break;
11106         case 11: /* 16KB Pages.  */
11107             if (level == 0 || (level == 1 && outputsize <= 40)) {
11108                 return false;
11109             }
11110             break;
11111         case 9: /* 4KB Pages.  */
11112             if (level == 0 && outputsize <= 42) {
11113                 return false;
11114             }
11115             break;
11116         default:
11117             g_assert_not_reached();
11118         }
11119 
11120         /* Inputsize checks.  */
11121         if (inputsize > outputsize &&
11122             (arm_el_is_aa64(&cpu->env, 1) || inputsize > 40)) {
11123             /* This is CONSTRAINED UNPREDICTABLE and we choose to fault.  */
11124             return false;
11125         }
11126     } else {
11127         /* AArch32 only supports 4KB pages. Assert on that.  */
11128         assert(stride == 9);
11129 
11130         if (level == 0) {
11131             return false;
11132         }
11133     }
11134     return true;
11135 }
11136 
11137 /* Translate from the 4-bit stage 2 representation of
11138  * memory attributes (without cache-allocation hints) to
11139  * the 8-bit representation of the stage 1 MAIR registers
11140  * (which includes allocation hints).
11141  *
11142  * ref: shared/translation/attrs/S2AttrDecode()
11143  *      .../S2ConvertAttrsHints()
11144  */
11145 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
11146 {
11147     uint8_t hiattr = extract32(s2attrs, 2, 2);
11148     uint8_t loattr = extract32(s2attrs, 0, 2);
11149     uint8_t hihint = 0, lohint = 0;
11150 
11151     if (hiattr != 0) { /* normal memory */
11152         if (arm_hcr_el2_eff(env) & HCR_CD) { /* cache disabled */
11153             hiattr = loattr = 1; /* non-cacheable */
11154         } else {
11155             if (hiattr != 1) { /* Write-through or write-back */
11156                 hihint = 3; /* RW allocate */
11157             }
11158             if (loattr != 1) { /* Write-through or write-back */
11159                 lohint = 3; /* RW allocate */
11160             }
11161         }
11162     }
11163 
11164     return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
11165 }
11166 #endif /* !CONFIG_USER_ONLY */
11167 
11168 /* This mapping is common between ID_AA64MMFR0.PARANGE and TCR_ELx.{I}PS. */
11169 static const uint8_t pamax_map[] = {
11170     [0] = 32,
11171     [1] = 36,
11172     [2] = 40,
11173     [3] = 42,
11174     [4] = 44,
11175     [5] = 48,
11176 };
11177 
11178 /* The cpu-specific constant value of PAMax; also used by hw/arm/virt. */
11179 unsigned int arm_pamax(ARMCPU *cpu)
11180 {
11181     unsigned int parange =
11182         FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11183 
11184     /*
11185      * id_aa64mmfr0 is a read-only register so values outside of the
11186      * supported mappings can be considered an implementation error.
11187      */
11188     assert(parange < ARRAY_SIZE(pamax_map));
11189     return pamax_map[parange];
11190 }
11191 
11192 static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11193 {
11194     if (regime_has_2_ranges(mmu_idx)) {
11195         return extract64(tcr, 37, 2);
11196     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11197         return 0; /* VTCR_EL2 */
11198     } else {
11199         /* Replicate the single TBI bit so we always have 2 bits.  */
11200         return extract32(tcr, 20, 1) * 3;
11201     }
11202 }
11203 
11204 static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11205 {
11206     if (regime_has_2_ranges(mmu_idx)) {
11207         return extract64(tcr, 51, 2);
11208     } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11209         return 0; /* VTCR_EL2 */
11210     } else {
11211         /* Replicate the single TBID bit so we always have 2 bits.  */
11212         return extract32(tcr, 29, 1) * 3;
11213     }
11214 }
11215 
11216 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11217 {
11218     if (regime_has_2_ranges(mmu_idx)) {
11219         return extract64(tcr, 57, 2);
11220     } else {
11221         /* Replicate the single TCMA bit so we always have 2 bits.  */
11222         return extract32(tcr, 30, 1) * 3;
11223     }
11224 }
11225 
11226 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11227                                    ARMMMUIdx mmu_idx, bool data)
11228 {
11229     uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11230     bool epd, hpd, using16k, using64k, tsz_oob;
11231     int select, tsz, tbi, max_tsz, min_tsz, ps;
11232 
11233     if (!regime_has_2_ranges(mmu_idx)) {
11234         select = 0;
11235         tsz = extract32(tcr, 0, 6);
11236         using64k = extract32(tcr, 14, 1);
11237         using16k = extract32(tcr, 15, 1);
11238         if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11239             /* VTCR_EL2 */
11240             hpd = false;
11241         } else {
11242             hpd = extract32(tcr, 24, 1);
11243         }
11244         epd = false;
11245         ps = extract32(tcr, 16, 3);
11246     } else {
11247         /*
11248          * Bit 55 is always between the two regions, and is canonical for
11249          * determining if address tagging is enabled.
11250          */
11251         select = extract64(va, 55, 1);
11252         if (!select) {
11253             tsz = extract32(tcr, 0, 6);
11254             epd = extract32(tcr, 7, 1);
11255             using64k = extract32(tcr, 14, 1);
11256             using16k = extract32(tcr, 15, 1);
11257             hpd = extract64(tcr, 41, 1);
11258         } else {
11259             int tg = extract32(tcr, 30, 2);
11260             using16k = tg == 1;
11261             using64k = tg == 3;
11262             tsz = extract32(tcr, 16, 6);
11263             epd = extract32(tcr, 23, 1);
11264             hpd = extract64(tcr, 42, 1);
11265         }
11266         ps = extract64(tcr, 32, 3);
11267     }
11268 
11269     if (cpu_isar_feature(aa64_st, env_archcpu(env))) {
11270         max_tsz = 48 - using64k;
11271     } else {
11272         max_tsz = 39;
11273     }
11274 
11275     min_tsz = 16;
11276     if (using64k) {
11277         if (cpu_isar_feature(aa64_lva, env_archcpu(env))) {
11278             min_tsz = 12;
11279         }
11280     }
11281     /* TODO: FEAT_LPA2 */
11282 
11283     if (tsz > max_tsz) {
11284         tsz = max_tsz;
11285         tsz_oob = true;
11286     } else if (tsz < min_tsz) {
11287         tsz = min_tsz;
11288         tsz_oob = true;
11289     } else {
11290         tsz_oob = false;
11291     }
11292 
11293     /* Present TBI as a composite with TBID.  */
11294     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11295     if (!data) {
11296         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11297     }
11298     tbi = (tbi >> select) & 1;
11299 
11300     return (ARMVAParameters) {
11301         .tsz = tsz,
11302         .ps = ps,
11303         .select = select,
11304         .tbi = tbi,
11305         .epd = epd,
11306         .hpd = hpd,
11307         .using16k = using16k,
11308         .using64k = using64k,
11309         .tsz_oob = tsz_oob,
11310     };
11311 }
11312 
11313 #ifndef CONFIG_USER_ONLY
11314 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
11315                                           ARMMMUIdx mmu_idx)
11316 {
11317     uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11318     uint32_t el = regime_el(env, mmu_idx);
11319     int select, tsz;
11320     bool epd, hpd;
11321 
11322     assert(mmu_idx != ARMMMUIdx_Stage2_S);
11323 
11324     if (mmu_idx == ARMMMUIdx_Stage2) {
11325         /* VTCR */
11326         bool sext = extract32(tcr, 4, 1);
11327         bool sign = extract32(tcr, 3, 1);
11328 
11329         /*
11330          * If the sign-extend bit is not the same as t0sz[3], the result
11331          * is unpredictable. Flag this as a guest error.
11332          */
11333         if (sign != sext) {
11334             qemu_log_mask(LOG_GUEST_ERROR,
11335                           "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
11336         }
11337         tsz = sextract32(tcr, 0, 4) + 8;
11338         select = 0;
11339         hpd = false;
11340         epd = false;
11341     } else if (el == 2) {
11342         /* HTCR */
11343         tsz = extract32(tcr, 0, 3);
11344         select = 0;
11345         hpd = extract64(tcr, 24, 1);
11346         epd = false;
11347     } else {
11348         int t0sz = extract32(tcr, 0, 3);
11349         int t1sz = extract32(tcr, 16, 3);
11350 
11351         if (t1sz == 0) {
11352             select = va > (0xffffffffu >> t0sz);
11353         } else {
11354             /* Note that we will detect errors later.  */
11355             select = va >= ~(0xffffffffu >> t1sz);
11356         }
11357         if (!select) {
11358             tsz = t0sz;
11359             epd = extract32(tcr, 7, 1);
11360             hpd = extract64(tcr, 41, 1);
11361         } else {
11362             tsz = t1sz;
11363             epd = extract32(tcr, 23, 1);
11364             hpd = extract64(tcr, 42, 1);
11365         }
11366         /* For aarch32, hpd0 is not enabled without t2e as well.  */
11367         hpd &= extract32(tcr, 6, 1);
11368     }
11369 
11370     return (ARMVAParameters) {
11371         .tsz = tsz,
11372         .select = select,
11373         .epd = epd,
11374         .hpd = hpd,
11375     };
11376 }
11377 
11378 /**
11379  * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
11380  *
11381  * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11382  * prot and page_size may not be filled in, and the populated fsr value provides
11383  * information on why the translation aborted, in the format of a long-format
11384  * DFSR/IFSR fault register, with the following caveats:
11385  *  * the WnR bit is never set (the caller must do this).
11386  *
11387  * @env: CPUARMState
11388  * @address: virtual address to get physical address for
11389  * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
11390  * @mmu_idx: MMU index indicating required translation regime
11391  * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table
11392  *             walk), must be true if this is stage 2 of a stage 1+2 walk for an
11393  *             EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored.
11394  * @phys_ptr: set to the physical address corresponding to the virtual address
11395  * @attrs: set to the memory transaction attributes to use
11396  * @prot: set to the permissions for the page containing phys_ptr
11397  * @page_size_ptr: set to the size of the page containing phys_ptr
11398  * @fi: set to fault info if the translation fails
11399  * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
11400  */
11401 static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
11402                                MMUAccessType access_type, ARMMMUIdx mmu_idx,
11403                                bool s1_is_el0,
11404                                hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
11405                                target_ulong *page_size_ptr,
11406                                ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
11407 {
11408     ARMCPU *cpu = env_archcpu(env);
11409     CPUState *cs = CPU(cpu);
11410     /* Read an LPAE long-descriptor translation table. */
11411     ARMFaultType fault_type = ARMFault_Translation;
11412     uint32_t level;
11413     ARMVAParameters param;
11414     uint64_t ttbr;
11415     hwaddr descaddr, indexmask, indexmask_grainsize;
11416     uint32_t tableattrs;
11417     target_ulong page_size;
11418     uint32_t attrs;
11419     int32_t stride;
11420     int addrsize, inputsize, outputsize;
11421     TCR *tcr = regime_tcr(env, mmu_idx);
11422     int ap, ns, xn, pxn;
11423     uint32_t el = regime_el(env, mmu_idx);
11424     uint64_t descaddrmask;
11425     bool aarch64 = arm_el_is_aa64(env, el);
11426     bool guarded = false;
11427 
11428     /* TODO: This code does not support shareability levels. */
11429     if (aarch64) {
11430         int ps;
11431 
11432         param = aa64_va_parameters(env, address, mmu_idx,
11433                                    access_type != MMU_INST_FETCH);
11434         level = 0;
11435 
11436         /*
11437          * If TxSZ is programmed to a value larger than the maximum,
11438          * or smaller than the effective minimum, it is IMPLEMENTATION
11439          * DEFINED whether we behave as if the field were programmed
11440          * within bounds, or if a level 0 Translation fault is generated.
11441          *
11442          * With FEAT_LVA, fault on less than minimum becomes required,
11443          * so our choice is to always raise the fault.
11444          */
11445         if (param.tsz_oob) {
11446             fault_type = ARMFault_Translation;
11447             goto do_fault;
11448         }
11449 
11450         addrsize = 64 - 8 * param.tbi;
11451         inputsize = 64 - param.tsz;
11452 
11453         /*
11454          * Bound PS by PARANGE to find the effective output address size.
11455          * ID_AA64MMFR0 is a read-only register so values outside of the
11456          * supported mappings can be considered an implementation error.
11457          */
11458         ps = FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11459         ps = MIN(ps, param.ps);
11460         assert(ps < ARRAY_SIZE(pamax_map));
11461         outputsize = pamax_map[ps];
11462     } else {
11463         param = aa32_va_parameters(env, address, mmu_idx);
11464         level = 1;
11465         addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
11466         inputsize = addrsize - param.tsz;
11467         outputsize = 40;
11468     }
11469 
11470     /*
11471      * We determined the region when collecting the parameters, but we
11472      * have not yet validated that the address is valid for the region.
11473      * Extract the top bits and verify that they all match select.
11474      *
11475      * For aa32, if inputsize == addrsize, then we have selected the
11476      * region by exclusion in aa32_va_parameters and there is no more
11477      * validation to do here.
11478      */
11479     if (inputsize < addrsize) {
11480         target_ulong top_bits = sextract64(address, inputsize,
11481                                            addrsize - inputsize);
11482         if (-top_bits != param.select) {
11483             /* The gap between the two regions is a Translation fault */
11484             fault_type = ARMFault_Translation;
11485             goto do_fault;
11486         }
11487     }
11488 
11489     if (param.using64k) {
11490         stride = 13;
11491     } else if (param.using16k) {
11492         stride = 11;
11493     } else {
11494         stride = 9;
11495     }
11496 
11497     /* Note that QEMU ignores shareability and cacheability attributes,
11498      * so we don't need to do anything with the SH, ORGN, IRGN fields
11499      * in the TTBCR.  Similarly, TTBCR:A1 selects whether we get the
11500      * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
11501      * implement any ASID-like capability so we can ignore it (instead
11502      * we will always flush the TLB any time the ASID is changed).
11503      */
11504     ttbr = regime_ttbr(env, mmu_idx, param.select);
11505 
11506     /* Here we should have set up all the parameters for the translation:
11507      * inputsize, ttbr, epd, stride, tbi
11508      */
11509 
11510     if (param.epd) {
11511         /* Translation table walk disabled => Translation fault on TLB miss
11512          * Note: This is always 0 on 64-bit EL2 and EL3.
11513          */
11514         goto do_fault;
11515     }
11516 
11517     if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
11518         /* The starting level depends on the virtual address size (which can
11519          * be up to 48 bits) and the translation granule size. It indicates
11520          * the number of strides (stride bits at a time) needed to
11521          * consume the bits of the input address. In the pseudocode this is:
11522          *  level = 4 - RoundUp((inputsize - grainsize) / stride)
11523          * where their 'inputsize' is our 'inputsize', 'grainsize' is
11524          * our 'stride + 3' and 'stride' is our 'stride'.
11525          * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
11526          * = 4 - (inputsize - stride - 3 + stride - 1) / stride
11527          * = 4 - (inputsize - 4) / stride;
11528          */
11529         level = 4 - (inputsize - 4) / stride;
11530     } else {
11531         /* For stage 2 translations the starting level is specified by the
11532          * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
11533          */
11534         uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
11535         uint32_t startlevel;
11536         bool ok;
11537 
11538         if (!aarch64 || stride == 9) {
11539             /* AArch32 or 4KB pages */
11540             startlevel = 2 - sl0;
11541 
11542             if (cpu_isar_feature(aa64_st, cpu)) {
11543                 startlevel &= 3;
11544             }
11545         } else {
11546             /* 16KB or 64KB pages */
11547             startlevel = 3 - sl0;
11548         }
11549 
11550         /* Check that the starting level is valid. */
11551         ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
11552                                 inputsize, stride, outputsize);
11553         if (!ok) {
11554             fault_type = ARMFault_Translation;
11555             goto do_fault;
11556         }
11557         level = startlevel;
11558     }
11559 
11560     indexmask_grainsize = MAKE_64BIT_MASK(0, stride + 3);
11561     indexmask = MAKE_64BIT_MASK(0, inputsize - (stride * (4 - level)));
11562 
11563     /* Now we can extract the actual base address from the TTBR */
11564     descaddr = extract64(ttbr, 0, 48);
11565 
11566     /*
11567      * If the base address is out of range, raise AddressSizeFault.
11568      * In the pseudocode, this is !IsZero(baseregister<47:outputsize>),
11569      * but we've just cleared the bits above 47, so simplify the test.
11570      */
11571     if (descaddr >> outputsize) {
11572         level = 0;
11573         fault_type = ARMFault_AddressSize;
11574         goto do_fault;
11575     }
11576 
11577     /*
11578      * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
11579      * and also to mask out CnP (bit 0) which could validly be non-zero.
11580      */
11581     descaddr &= ~indexmask;
11582 
11583     /*
11584      * For AArch32, the address field in the descriptor goes up to bit 39
11585      * for both v7 and v8.  However, for v8 the SBZ bits [47:40] must be 0
11586      * or an AddressSize fault is raised.  So for v8 we extract those SBZ
11587      * bits as part of the address, which will be checked via outputsize.
11588      * For AArch64, the address field always goes up to bit 47 (with extra
11589      * bits for FEAT_LPA placed elsewhere).  AArch64 implies v8.
11590      */
11591     if (arm_feature(env, ARM_FEATURE_V8)) {
11592         descaddrmask = MAKE_64BIT_MASK(0, 48);
11593     } else {
11594         descaddrmask = MAKE_64BIT_MASK(0, 40);
11595     }
11596     descaddrmask &= ~indexmask_grainsize;
11597 
11598     /* Secure accesses start with the page table in secure memory and
11599      * can be downgraded to non-secure at any step. Non-secure accesses
11600      * remain non-secure. We implement this by just ORing in the NSTable/NS
11601      * bits at each step.
11602      */
11603     tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
11604     for (;;) {
11605         uint64_t descriptor;
11606         bool nstable;
11607 
11608         descaddr |= (address >> (stride * (4 - level))) & indexmask;
11609         descaddr &= ~7ULL;
11610         nstable = extract32(tableattrs, 4, 1);
11611         descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
11612         if (fi->type != ARMFault_None) {
11613             goto do_fault;
11614         }
11615 
11616         if (!(descriptor & 1) ||
11617             (!(descriptor & 2) && (level == 3))) {
11618             /* Invalid, or the Reserved level 3 encoding */
11619             goto do_fault;
11620         }
11621 
11622         descaddr = descriptor & descaddrmask;
11623         if (descaddr >> outputsize) {
11624             fault_type = ARMFault_AddressSize;
11625             goto do_fault;
11626         }
11627 
11628         if ((descriptor & 2) && (level < 3)) {
11629             /* Table entry. The top five bits are attributes which may
11630              * propagate down through lower levels of the table (and
11631              * which are all arranged so that 0 means "no effect", so
11632              * we can gather them up by ORing in the bits at each level).
11633              */
11634             tableattrs |= extract64(descriptor, 59, 5);
11635             level++;
11636             indexmask = indexmask_grainsize;
11637             continue;
11638         }
11639         /* Block entry at level 1 or 2, or page entry at level 3.
11640          * These are basically the same thing, although the number
11641          * of bits we pull in from the vaddr varies.
11642          */
11643         page_size = (1ULL << ((stride * (4 - level)) + 3));
11644         descaddr |= (address & (page_size - 1));
11645         /* Extract attributes from the descriptor */
11646         attrs = extract64(descriptor, 2, 10)
11647             | (extract64(descriptor, 52, 12) << 10);
11648 
11649         if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11650             /* Stage 2 table descriptors do not include any attribute fields */
11651             break;
11652         }
11653         /* Merge in attributes from table descriptors */
11654         attrs |= nstable << 3; /* NS */
11655         guarded = extract64(descriptor, 50, 1);  /* GP */
11656         if (param.hpd) {
11657             /* HPD disables all the table attributes except NSTable.  */
11658             break;
11659         }
11660         attrs |= extract32(tableattrs, 0, 2) << 11;     /* XN, PXN */
11661         /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11662          * means "force PL1 access only", which means forcing AP[1] to 0.
11663          */
11664         attrs &= ~(extract32(tableattrs, 2, 1) << 4);   /* !APT[0] => AP[1] */
11665         attrs |= extract32(tableattrs, 3, 1) << 5;      /* APT[1] => AP[2] */
11666         break;
11667     }
11668     /* Here descaddr is the final physical address, and attributes
11669      * are all in attrs.
11670      */
11671     fault_type = ARMFault_AccessFlag;
11672     if ((attrs & (1 << 8)) == 0) {
11673         /* Access flag */
11674         goto do_fault;
11675     }
11676 
11677     ap = extract32(attrs, 4, 2);
11678 
11679     if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11680         ns = mmu_idx == ARMMMUIdx_Stage2;
11681         xn = extract32(attrs, 11, 2);
11682         *prot = get_S2prot(env, ap, xn, s1_is_el0);
11683     } else {
11684         ns = extract32(attrs, 3, 1);
11685         xn = extract32(attrs, 12, 1);
11686         pxn = extract32(attrs, 11, 1);
11687         *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
11688     }
11689 
11690     fault_type = ARMFault_Permission;
11691     if (!(*prot & (1 << access_type))) {
11692         goto do_fault;
11693     }
11694 
11695     if (ns) {
11696         /* The NS bit will (as required by the architecture) have no effect if
11697          * the CPU doesn't support TZ or this is a non-secure translation
11698          * regime, because the attribute will already be non-secure.
11699          */
11700         txattrs->secure = false;
11701     }
11702     /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB.  */
11703     if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
11704         arm_tlb_bti_gp(txattrs) = true;
11705     }
11706 
11707     if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11708         cacheattrs->attrs = convert_stage2_attrs(env, extract32(attrs, 0, 4));
11709     } else {
11710         /* Index into MAIR registers for cache attributes */
11711         uint8_t attrindx = extract32(attrs, 0, 3);
11712         uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11713         assert(attrindx <= 7);
11714         cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
11715     }
11716     cacheattrs->shareability = extract32(attrs, 6, 2);
11717 
11718     *phys_ptr = descaddr;
11719     *page_size_ptr = page_size;
11720     return false;
11721 
11722 do_fault:
11723     fi->type = fault_type;
11724     fi->level = level;
11725     /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2.  */
11726     fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2 ||
11727                                mmu_idx == ARMMMUIdx_Stage2_S);
11728     fi->s1ns = mmu_idx == ARMMMUIdx_Stage2;
11729     return true;
11730 }
11731 
11732 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11733                                                 ARMMMUIdx mmu_idx,
11734                                                 int32_t address, int *prot)
11735 {
11736     if (!arm_feature(env, ARM_FEATURE_M)) {
11737         *prot = PAGE_READ | PAGE_WRITE;
11738         switch (address) {
11739         case 0xF0000000 ... 0xFFFFFFFF:
11740             if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11741                 /* hivecs execing is ok */
11742                 *prot |= PAGE_EXEC;
11743             }
11744             break;
11745         case 0x00000000 ... 0x7FFFFFFF:
11746             *prot |= PAGE_EXEC;
11747             break;
11748         }
11749     } else {
11750         /* Default system address map for M profile cores.
11751          * The architecture specifies which regions are execute-never;
11752          * at the MPU level no other checks are defined.
11753          */
11754         switch (address) {
11755         case 0x00000000 ... 0x1fffffff: /* ROM */
11756         case 0x20000000 ... 0x3fffffff: /* SRAM */
11757         case 0x60000000 ... 0x7fffffff: /* RAM */
11758         case 0x80000000 ... 0x9fffffff: /* RAM */
11759             *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11760             break;
11761         case 0x40000000 ... 0x5fffffff: /* Peripheral */
11762         case 0xa0000000 ... 0xbfffffff: /* Device */
11763         case 0xc0000000 ... 0xdfffffff: /* Device */
11764         case 0xe0000000 ... 0xffffffff: /* System */
11765             *prot = PAGE_READ | PAGE_WRITE;
11766             break;
11767         default:
11768             g_assert_not_reached();
11769         }
11770     }
11771 }
11772 
11773 static bool pmsav7_use_background_region(ARMCPU *cpu,
11774                                          ARMMMUIdx mmu_idx, bool is_user)
11775 {
11776     /* Return true if we should use the default memory map as a
11777      * "background" region if there are no hits against any MPU regions.
11778      */
11779     CPUARMState *env = &cpu->env;
11780 
11781     if (is_user) {
11782         return false;
11783     }
11784 
11785     if (arm_feature(env, ARM_FEATURE_M)) {
11786         return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11787             & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
11788     } else {
11789         return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11790     }
11791 }
11792 
11793 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11794 {
11795     /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11796     return arm_feature(env, ARM_FEATURE_M) &&
11797         extract32(address, 20, 12) == 0xe00;
11798 }
11799 
11800 static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11801 {
11802     /* True if address is in the M profile system region
11803      * 0xe0000000 - 0xffffffff
11804      */
11805     return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11806 }
11807 
11808 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
11809                                  MMUAccessType access_type, ARMMMUIdx mmu_idx,
11810                                  hwaddr *phys_ptr, int *prot,
11811                                  target_ulong *page_size,
11812                                  ARMMMUFaultInfo *fi)
11813 {
11814     ARMCPU *cpu = env_archcpu(env);
11815     int n;
11816     bool is_user = regime_is_user(env, mmu_idx);
11817 
11818     *phys_ptr = address;
11819     *page_size = TARGET_PAGE_SIZE;
11820     *prot = 0;
11821 
11822     if (regime_translation_disabled(env, mmu_idx) ||
11823         m_is_ppb_region(env, address)) {
11824         /* MPU disabled or M profile PPB access: use default memory map.
11825          * The other case which uses the default memory map in the
11826          * v7M ARM ARM pseudocode is exception vector reads from the vector
11827          * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11828          * which always does a direct read using address_space_ldl(), rather
11829          * than going via this function, so we don't need to check that here.
11830          */
11831         get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11832     } else { /* MPU enabled */
11833         for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11834             /* region search */
11835             uint32_t base = env->pmsav7.drbar[n];
11836             uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11837             uint32_t rmask;
11838             bool srdis = false;
11839 
11840             if (!(env->pmsav7.drsr[n] & 0x1)) {
11841                 continue;
11842             }
11843 
11844             if (!rsize) {
11845                 qemu_log_mask(LOG_GUEST_ERROR,
11846                               "DRSR[%d]: Rsize field cannot be 0\n", n);
11847                 continue;
11848             }
11849             rsize++;
11850             rmask = (1ull << rsize) - 1;
11851 
11852             if (base & rmask) {
11853                 qemu_log_mask(LOG_GUEST_ERROR,
11854                               "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11855                               "to DRSR region size, mask = 0x%" PRIx32 "\n",
11856                               n, base, rmask);
11857                 continue;
11858             }
11859 
11860             if (address < base || address > base + rmask) {
11861                 /*
11862                  * Address not in this region. We must check whether the
11863                  * region covers addresses in the same page as our address.
11864                  * In that case we must not report a size that covers the
11865                  * whole page for a subsequent hit against a different MPU
11866                  * region or the background region, because it would result in
11867                  * incorrect TLB hits for subsequent accesses to addresses that
11868                  * are in this MPU region.
11869                  */
11870                 if (ranges_overlap(base, rmask,
11871                                    address & TARGET_PAGE_MASK,
11872                                    TARGET_PAGE_SIZE)) {
11873                     *page_size = 1;
11874                 }
11875                 continue;
11876             }
11877 
11878             /* Region matched */
11879 
11880             if (rsize >= 8) { /* no subregions for regions < 256 bytes */
11881                 int i, snd;
11882                 uint32_t srdis_mask;
11883 
11884                 rsize -= 3; /* sub region size (power of 2) */
11885                 snd = ((address - base) >> rsize) & 0x7;
11886                 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
11887 
11888                 srdis_mask = srdis ? 0x3 : 0x0;
11889                 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
11890                     /* This will check in groups of 2, 4 and then 8, whether
11891                      * the subregion bits are consistent. rsize is incremented
11892                      * back up to give the region size, considering consistent
11893                      * adjacent subregions as one region. Stop testing if rsize
11894                      * is already big enough for an entire QEMU page.
11895                      */
11896                     int snd_rounded = snd & ~(i - 1);
11897                     uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
11898                                                      snd_rounded + 8, i);
11899                     if (srdis_mask ^ srdis_multi) {
11900                         break;
11901                     }
11902                     srdis_mask = (srdis_mask << i) | srdis_mask;
11903                     rsize++;
11904                 }
11905             }
11906             if (srdis) {
11907                 continue;
11908             }
11909             if (rsize < TARGET_PAGE_BITS) {
11910                 *page_size = 1 << rsize;
11911             }
11912             break;
11913         }
11914 
11915         if (n == -1) { /* no hits */
11916             if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11917                 /* background fault */
11918                 fi->type = ARMFault_Background;
11919                 return true;
11920             }
11921             get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11922         } else { /* a MPU hit! */
11923             uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
11924             uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
11925 
11926             if (m_is_system_region(env, address)) {
11927                 /* System space is always execute never */
11928                 xn = 1;
11929             }
11930 
11931             if (is_user) { /* User mode AP bit decoding */
11932                 switch (ap) {
11933                 case 0:
11934                 case 1:
11935                 case 5:
11936                     break; /* no access */
11937                 case 3:
11938                     *prot |= PAGE_WRITE;
11939                     /* fall through */
11940                 case 2:
11941                 case 6:
11942                     *prot |= PAGE_READ | PAGE_EXEC;
11943                     break;
11944                 case 7:
11945                     /* for v7M, same as 6; for R profile a reserved value */
11946                     if (arm_feature(env, ARM_FEATURE_M)) {
11947                         *prot |= PAGE_READ | PAGE_EXEC;
11948                         break;
11949                     }
11950                     /* fall through */
11951                 default:
11952                     qemu_log_mask(LOG_GUEST_ERROR,
11953                                   "DRACR[%d]: Bad value for AP bits: 0x%"
11954                                   PRIx32 "\n", n, ap);
11955                 }
11956             } else { /* Priv. mode AP bits decoding */
11957                 switch (ap) {
11958                 case 0:
11959                     break; /* no access */
11960                 case 1:
11961                 case 2:
11962                 case 3:
11963                     *prot |= PAGE_WRITE;
11964                     /* fall through */
11965                 case 5:
11966                 case 6:
11967                     *prot |= PAGE_READ | PAGE_EXEC;
11968                     break;
11969                 case 7:
11970                     /* for v7M, same as 6; for R profile a reserved value */
11971                     if (arm_feature(env, ARM_FEATURE_M)) {
11972                         *prot |= PAGE_READ | PAGE_EXEC;
11973                         break;
11974                     }
11975                     /* fall through */
11976                 default:
11977                     qemu_log_mask(LOG_GUEST_ERROR,
11978                                   "DRACR[%d]: Bad value for AP bits: 0x%"
11979                                   PRIx32 "\n", n, ap);
11980                 }
11981             }
11982 
11983             /* execute never */
11984             if (xn) {
11985                 *prot &= ~PAGE_EXEC;
11986             }
11987         }
11988     }
11989 
11990     fi->type = ARMFault_Permission;
11991     fi->level = 1;
11992     return !(*prot & (1 << access_type));
11993 }
11994 
11995 static bool v8m_is_sau_exempt(CPUARMState *env,
11996                               uint32_t address, MMUAccessType access_type)
11997 {
11998     /* The architecture specifies that certain address ranges are
11999      * exempt from v8M SAU/IDAU checks.
12000      */
12001     return
12002         (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
12003         (address >= 0xe0000000 && address <= 0xe0002fff) ||
12004         (address >= 0xe000e000 && address <= 0xe000efff) ||
12005         (address >= 0xe002e000 && address <= 0xe002efff) ||
12006         (address >= 0xe0040000 && address <= 0xe0041fff) ||
12007         (address >= 0xe00ff000 && address <= 0xe00fffff);
12008 }
12009 
12010 void v8m_security_lookup(CPUARMState *env, uint32_t address,
12011                                 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12012                                 V8M_SAttributes *sattrs)
12013 {
12014     /* Look up the security attributes for this address. Compare the
12015      * pseudocode SecurityCheck() function.
12016      * We assume the caller has zero-initialized *sattrs.
12017      */
12018     ARMCPU *cpu = env_archcpu(env);
12019     int r;
12020     bool idau_exempt = false, idau_ns = true, idau_nsc = true;
12021     int idau_region = IREGION_NOTVALID;
12022     uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12023     uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
12024 
12025     if (cpu->idau) {
12026         IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
12027         IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
12028 
12029         iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
12030                    &idau_nsc);
12031     }
12032 
12033     if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
12034         /* 0xf0000000..0xffffffff is always S for insn fetches */
12035         return;
12036     }
12037 
12038     if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
12039         sattrs->ns = !regime_is_secure(env, mmu_idx);
12040         return;
12041     }
12042 
12043     if (idau_region != IREGION_NOTVALID) {
12044         sattrs->irvalid = true;
12045         sattrs->iregion = idau_region;
12046     }
12047 
12048     switch (env->sau.ctrl & 3) {
12049     case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
12050         break;
12051     case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
12052         sattrs->ns = true;
12053         break;
12054     default: /* SAU.ENABLE == 1 */
12055         for (r = 0; r < cpu->sau_sregion; r++) {
12056             if (env->sau.rlar[r] & 1) {
12057                 uint32_t base = env->sau.rbar[r] & ~0x1f;
12058                 uint32_t limit = env->sau.rlar[r] | 0x1f;
12059 
12060                 if (base <= address && limit >= address) {
12061                     if (base > addr_page_base || limit < addr_page_limit) {
12062                         sattrs->subpage = true;
12063                     }
12064                     if (sattrs->srvalid) {
12065                         /* If we hit in more than one region then we must report
12066                          * as Secure, not NS-Callable, with no valid region
12067                          * number info.
12068                          */
12069                         sattrs->ns = false;
12070                         sattrs->nsc = false;
12071                         sattrs->sregion = 0;
12072                         sattrs->srvalid = false;
12073                         break;
12074                     } else {
12075                         if (env->sau.rlar[r] & 2) {
12076                             sattrs->nsc = true;
12077                         } else {
12078                             sattrs->ns = true;
12079                         }
12080                         sattrs->srvalid = true;
12081                         sattrs->sregion = r;
12082                     }
12083                 } else {
12084                     /*
12085                      * Address not in this region. We must check whether the
12086                      * region covers addresses in the same page as our address.
12087                      * In that case we must not report a size that covers the
12088                      * whole page for a subsequent hit against a different MPU
12089                      * region or the background region, because it would result
12090                      * in incorrect TLB hits for subsequent accesses to
12091                      * addresses that are in this MPU region.
12092                      */
12093                     if (limit >= base &&
12094                         ranges_overlap(base, limit - base + 1,
12095                                        addr_page_base,
12096                                        TARGET_PAGE_SIZE)) {
12097                         sattrs->subpage = true;
12098                     }
12099                 }
12100             }
12101         }
12102         break;
12103     }
12104 
12105     /*
12106      * The IDAU will override the SAU lookup results if it specifies
12107      * higher security than the SAU does.
12108      */
12109     if (!idau_ns) {
12110         if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
12111             sattrs->ns = false;
12112             sattrs->nsc = idau_nsc;
12113         }
12114     }
12115 }
12116 
12117 bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
12118                               MMUAccessType access_type, ARMMMUIdx mmu_idx,
12119                               hwaddr *phys_ptr, MemTxAttrs *txattrs,
12120                               int *prot, bool *is_subpage,
12121                               ARMMMUFaultInfo *fi, uint32_t *mregion)
12122 {
12123     /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
12124      * that a full phys-to-virt translation does).
12125      * mregion is (if not NULL) set to the region number which matched,
12126      * or -1 if no region number is returned (MPU off, address did not
12127      * hit a region, address hit in multiple regions).
12128      * We set is_subpage to true if the region hit doesn't cover the
12129      * entire TARGET_PAGE the address is within.
12130      */
12131     ARMCPU *cpu = env_archcpu(env);
12132     bool is_user = regime_is_user(env, mmu_idx);
12133     uint32_t secure = regime_is_secure(env, mmu_idx);
12134     int n;
12135     int matchregion = -1;
12136     bool hit = false;
12137     uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12138     uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
12139 
12140     *is_subpage = false;
12141     *phys_ptr = address;
12142     *prot = 0;
12143     if (mregion) {
12144         *mregion = -1;
12145     }
12146 
12147     /* Unlike the ARM ARM pseudocode, we don't need to check whether this
12148      * was an exception vector read from the vector table (which is always
12149      * done using the default system address map), because those accesses
12150      * are done in arm_v7m_load_vector(), which always does a direct
12151      * read using address_space_ldl(), rather than going via this function.
12152      */
12153     if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
12154         hit = true;
12155     } else if (m_is_ppb_region(env, address)) {
12156         hit = true;
12157     } else {
12158         if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
12159             hit = true;
12160         }
12161 
12162         for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
12163             /* region search */
12164             /* Note that the base address is bits [31:5] from the register
12165              * with bits [4:0] all zeroes, but the limit address is bits
12166              * [31:5] from the register with bits [4:0] all ones.
12167              */
12168             uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
12169             uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
12170 
12171             if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
12172                 /* Region disabled */
12173                 continue;
12174             }
12175 
12176             if (address < base || address > limit) {
12177                 /*
12178                  * Address not in this region. We must check whether the
12179                  * region covers addresses in the same page as our address.
12180                  * In that case we must not report a size that covers the
12181                  * whole page for a subsequent hit against a different MPU
12182                  * region or the background region, because it would result in
12183                  * incorrect TLB hits for subsequent accesses to addresses that
12184                  * are in this MPU region.
12185                  */
12186                 if (limit >= base &&
12187                     ranges_overlap(base, limit - base + 1,
12188                                    addr_page_base,
12189                                    TARGET_PAGE_SIZE)) {
12190                     *is_subpage = true;
12191                 }
12192                 continue;
12193             }
12194 
12195             if (base > addr_page_base || limit < addr_page_limit) {
12196                 *is_subpage = true;
12197             }
12198 
12199             if (matchregion != -1) {
12200                 /* Multiple regions match -- always a failure (unlike
12201                  * PMSAv7 where highest-numbered-region wins)
12202                  */
12203                 fi->type = ARMFault_Permission;
12204                 fi->level = 1;
12205                 return true;
12206             }
12207 
12208             matchregion = n;
12209             hit = true;
12210         }
12211     }
12212 
12213     if (!hit) {
12214         /* background fault */
12215         fi->type = ARMFault_Background;
12216         return true;
12217     }
12218 
12219     if (matchregion == -1) {
12220         /* hit using the background region */
12221         get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
12222     } else {
12223         uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
12224         uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
12225         bool pxn = false;
12226 
12227         if (arm_feature(env, ARM_FEATURE_V8_1M)) {
12228             pxn = extract32(env->pmsav8.rlar[secure][matchregion], 4, 1);
12229         }
12230 
12231         if (m_is_system_region(env, address)) {
12232             /* System space is always execute never */
12233             xn = 1;
12234         }
12235 
12236         *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
12237         if (*prot && !xn && !(pxn && !is_user)) {
12238             *prot |= PAGE_EXEC;
12239         }
12240         /* We don't need to look the attribute up in the MAIR0/MAIR1
12241          * registers because that only tells us about cacheability.
12242          */
12243         if (mregion) {
12244             *mregion = matchregion;
12245         }
12246     }
12247 
12248     fi->type = ARMFault_Permission;
12249     fi->level = 1;
12250     return !(*prot & (1 << access_type));
12251 }
12252 
12253 
12254 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
12255                                  MMUAccessType access_type, ARMMMUIdx mmu_idx,
12256                                  hwaddr *phys_ptr, MemTxAttrs *txattrs,
12257                                  int *prot, target_ulong *page_size,
12258                                  ARMMMUFaultInfo *fi)
12259 {
12260     uint32_t secure = regime_is_secure(env, mmu_idx);
12261     V8M_SAttributes sattrs = {};
12262     bool ret;
12263     bool mpu_is_subpage;
12264 
12265     if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
12266         v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
12267         if (access_type == MMU_INST_FETCH) {
12268             /* Instruction fetches always use the MMU bank and the
12269              * transaction attribute determined by the fetch address,
12270              * regardless of CPU state. This is painful for QEMU
12271              * to handle, because it would mean we need to encode
12272              * into the mmu_idx not just the (user, negpri) information
12273              * for the current security state but also that for the
12274              * other security state, which would balloon the number
12275              * of mmu_idx values needed alarmingly.
12276              * Fortunately we can avoid this because it's not actually
12277              * possible to arbitrarily execute code from memory with
12278              * the wrong security attribute: it will always generate
12279              * an exception of some kind or another, apart from the
12280              * special case of an NS CPU executing an SG instruction
12281              * in S&NSC memory. So we always just fail the translation
12282              * here and sort things out in the exception handler
12283              * (including possibly emulating an SG instruction).
12284              */
12285             if (sattrs.ns != !secure) {
12286                 if (sattrs.nsc) {
12287                     fi->type = ARMFault_QEMU_NSCExec;
12288                 } else {
12289                     fi->type = ARMFault_QEMU_SFault;
12290                 }
12291                 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
12292                 *phys_ptr = address;
12293                 *prot = 0;
12294                 return true;
12295             }
12296         } else {
12297             /* For data accesses we always use the MMU bank indicated
12298              * by the current CPU state, but the security attributes
12299              * might downgrade a secure access to nonsecure.
12300              */
12301             if (sattrs.ns) {
12302                 txattrs->secure = false;
12303             } else if (!secure) {
12304                 /* NS access to S memory must fault.
12305                  * Architecturally we should first check whether the
12306                  * MPU information for this address indicates that we
12307                  * are doing an unaligned access to Device memory, which
12308                  * should generate a UsageFault instead. QEMU does not
12309                  * currently check for that kind of unaligned access though.
12310                  * If we added it we would need to do so as a special case
12311                  * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
12312                  */
12313                 fi->type = ARMFault_QEMU_SFault;
12314                 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
12315                 *phys_ptr = address;
12316                 *prot = 0;
12317                 return true;
12318             }
12319         }
12320     }
12321 
12322     ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
12323                             txattrs, prot, &mpu_is_subpage, fi, NULL);
12324     *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
12325     return ret;
12326 }
12327 
12328 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
12329                                  MMUAccessType access_type, ARMMMUIdx mmu_idx,
12330                                  hwaddr *phys_ptr, int *prot,
12331                                  ARMMMUFaultInfo *fi)
12332 {
12333     int n;
12334     uint32_t mask;
12335     uint32_t base;
12336     bool is_user = regime_is_user(env, mmu_idx);
12337 
12338     if (regime_translation_disabled(env, mmu_idx)) {
12339         /* MPU disabled.  */
12340         *phys_ptr = address;
12341         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12342         return false;
12343     }
12344 
12345     *phys_ptr = address;
12346     for (n = 7; n >= 0; n--) {
12347         base = env->cp15.c6_region[n];
12348         if ((base & 1) == 0) {
12349             continue;
12350         }
12351         mask = 1 << ((base >> 1) & 0x1f);
12352         /* Keep this shift separate from the above to avoid an
12353            (undefined) << 32.  */
12354         mask = (mask << 1) - 1;
12355         if (((base ^ address) & ~mask) == 0) {
12356             break;
12357         }
12358     }
12359     if (n < 0) {
12360         fi->type = ARMFault_Background;
12361         return true;
12362     }
12363 
12364     if (access_type == MMU_INST_FETCH) {
12365         mask = env->cp15.pmsav5_insn_ap;
12366     } else {
12367         mask = env->cp15.pmsav5_data_ap;
12368     }
12369     mask = (mask >> (n * 4)) & 0xf;
12370     switch (mask) {
12371     case 0:
12372         fi->type = ARMFault_Permission;
12373         fi->level = 1;
12374         return true;
12375     case 1:
12376         if (is_user) {
12377             fi->type = ARMFault_Permission;
12378             fi->level = 1;
12379             return true;
12380         }
12381         *prot = PAGE_READ | PAGE_WRITE;
12382         break;
12383     case 2:
12384         *prot = PAGE_READ;
12385         if (!is_user) {
12386             *prot |= PAGE_WRITE;
12387         }
12388         break;
12389     case 3:
12390         *prot = PAGE_READ | PAGE_WRITE;
12391         break;
12392     case 5:
12393         if (is_user) {
12394             fi->type = ARMFault_Permission;
12395             fi->level = 1;
12396             return true;
12397         }
12398         *prot = PAGE_READ;
12399         break;
12400     case 6:
12401         *prot = PAGE_READ;
12402         break;
12403     default:
12404         /* Bad permission.  */
12405         fi->type = ARMFault_Permission;
12406         fi->level = 1;
12407         return true;
12408     }
12409     *prot |= PAGE_EXEC;
12410     return false;
12411 }
12412 
12413 /* Combine either inner or outer cacheability attributes for normal
12414  * memory, according to table D4-42 and pseudocode procedure
12415  * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
12416  *
12417  * NB: only stage 1 includes allocation hints (RW bits), leading to
12418  * some asymmetry.
12419  */
12420 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
12421 {
12422     if (s1 == 4 || s2 == 4) {
12423         /* non-cacheable has precedence */
12424         return 4;
12425     } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
12426         /* stage 1 write-through takes precedence */
12427         return s1;
12428     } else if (extract32(s2, 2, 2) == 2) {
12429         /* stage 2 write-through takes precedence, but the allocation hint
12430          * is still taken from stage 1
12431          */
12432         return (2 << 2) | extract32(s1, 0, 2);
12433     } else { /* write-back */
12434         return s1;
12435     }
12436 }
12437 
12438 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
12439  * and CombineS1S2Desc()
12440  *
12441  * @s1:      Attributes from stage 1 walk
12442  * @s2:      Attributes from stage 2 walk
12443  */
12444 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
12445 {
12446     uint8_t s1lo, s2lo, s1hi, s2hi;
12447     ARMCacheAttrs ret;
12448     bool tagged = false;
12449 
12450     if (s1.attrs == 0xf0) {
12451         tagged = true;
12452         s1.attrs = 0xff;
12453     }
12454 
12455     s1lo = extract32(s1.attrs, 0, 4);
12456     s2lo = extract32(s2.attrs, 0, 4);
12457     s1hi = extract32(s1.attrs, 4, 4);
12458     s2hi = extract32(s2.attrs, 4, 4);
12459 
12460     /* Combine shareability attributes (table D4-43) */
12461     if (s1.shareability == 2 || s2.shareability == 2) {
12462         /* if either are outer-shareable, the result is outer-shareable */
12463         ret.shareability = 2;
12464     } else if (s1.shareability == 3 || s2.shareability == 3) {
12465         /* if either are inner-shareable, the result is inner-shareable */
12466         ret.shareability = 3;
12467     } else {
12468         /* both non-shareable */
12469         ret.shareability = 0;
12470     }
12471 
12472     /* Combine memory type and cacheability attributes */
12473     if (s1hi == 0 || s2hi == 0) {
12474         /* Device has precedence over normal */
12475         if (s1lo == 0 || s2lo == 0) {
12476             /* nGnRnE has precedence over anything */
12477             ret.attrs = 0;
12478         } else if (s1lo == 4 || s2lo == 4) {
12479             /* non-Reordering has precedence over Reordering */
12480             ret.attrs = 4;  /* nGnRE */
12481         } else if (s1lo == 8 || s2lo == 8) {
12482             /* non-Gathering has precedence over Gathering */
12483             ret.attrs = 8;  /* nGRE */
12484         } else {
12485             ret.attrs = 0xc; /* GRE */
12486         }
12487 
12488         /* Any location for which the resultant memory type is any
12489          * type of Device memory is always treated as Outer Shareable.
12490          */
12491         ret.shareability = 2;
12492     } else { /* Normal memory */
12493         /* Outer/inner cacheability combine independently */
12494         ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
12495                   | combine_cacheattr_nibble(s1lo, s2lo);
12496 
12497         if (ret.attrs == 0x44) {
12498             /* Any location for which the resultant memory type is Normal
12499              * Inner Non-cacheable, Outer Non-cacheable is always treated
12500              * as Outer Shareable.
12501              */
12502             ret.shareability = 2;
12503         }
12504     }
12505 
12506     /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
12507     if (tagged && ret.attrs == 0xff) {
12508         ret.attrs = 0xf0;
12509     }
12510 
12511     return ret;
12512 }
12513 
12514 
12515 /* get_phys_addr - get the physical address for this virtual address
12516  *
12517  * Find the physical address corresponding to the given virtual address,
12518  * by doing a translation table walk on MMU based systems or using the
12519  * MPU state on MPU based systems.
12520  *
12521  * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
12522  * prot and page_size may not be filled in, and the populated fsr value provides
12523  * information on why the translation aborted, in the format of a
12524  * DFSR/IFSR fault register, with the following caveats:
12525  *  * we honour the short vs long DFSR format differences.
12526  *  * the WnR bit is never set (the caller must do this).
12527  *  * for PSMAv5 based systems we don't bother to return a full FSR format
12528  *    value.
12529  *
12530  * @env: CPUARMState
12531  * @address: virtual address to get physical address for
12532  * @access_type: 0 for read, 1 for write, 2 for execute
12533  * @mmu_idx: MMU index indicating required translation regime
12534  * @phys_ptr: set to the physical address corresponding to the virtual address
12535  * @attrs: set to the memory transaction attributes to use
12536  * @prot: set to the permissions for the page containing phys_ptr
12537  * @page_size: set to the size of the page containing phys_ptr
12538  * @fi: set to fault info if the translation fails
12539  * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
12540  */
12541 bool get_phys_addr(CPUARMState *env, target_ulong address,
12542                    MMUAccessType access_type, ARMMMUIdx mmu_idx,
12543                    hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
12544                    target_ulong *page_size,
12545                    ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
12546 {
12547     ARMMMUIdx s1_mmu_idx = stage_1_mmu_idx(mmu_idx);
12548 
12549     if (mmu_idx != s1_mmu_idx) {
12550         /* Call ourselves recursively to do the stage 1 and then stage 2
12551          * translations if mmu_idx is a two-stage regime.
12552          */
12553         if (arm_feature(env, ARM_FEATURE_EL2)) {
12554             hwaddr ipa;
12555             int s2_prot;
12556             int ret;
12557             ARMCacheAttrs cacheattrs2 = {};
12558             ARMMMUIdx s2_mmu_idx;
12559             bool is_el0;
12560 
12561             ret = get_phys_addr(env, address, access_type, s1_mmu_idx, &ipa,
12562                                 attrs, prot, page_size, fi, cacheattrs);
12563 
12564             /* If S1 fails or S2 is disabled, return early.  */
12565             if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
12566                 *phys_ptr = ipa;
12567                 return ret;
12568             }
12569 
12570             s2_mmu_idx = attrs->secure ? ARMMMUIdx_Stage2_S : ARMMMUIdx_Stage2;
12571             is_el0 = mmu_idx == ARMMMUIdx_E10_0 || mmu_idx == ARMMMUIdx_SE10_0;
12572 
12573             /* S1 is done. Now do S2 translation.  */
12574             ret = get_phys_addr_lpae(env, ipa, access_type, s2_mmu_idx, is_el0,
12575                                      phys_ptr, attrs, &s2_prot,
12576                                      page_size, fi, &cacheattrs2);
12577             fi->s2addr = ipa;
12578             /* Combine the S1 and S2 perms.  */
12579             *prot &= s2_prot;
12580 
12581             /* If S2 fails, return early.  */
12582             if (ret) {
12583                 return ret;
12584             }
12585 
12586             /* Combine the S1 and S2 cache attributes. */
12587             if (arm_hcr_el2_eff(env) & HCR_DC) {
12588                 /*
12589                  * HCR.DC forces the first stage attributes to
12590                  *  Normal Non-Shareable,
12591                  *  Inner Write-Back Read-Allocate Write-Allocate,
12592                  *  Outer Write-Back Read-Allocate Write-Allocate.
12593                  * Do not overwrite Tagged within attrs.
12594                  */
12595                 if (cacheattrs->attrs != 0xf0) {
12596                     cacheattrs->attrs = 0xff;
12597                 }
12598                 cacheattrs->shareability = 0;
12599             }
12600             *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
12601 
12602             /* Check if IPA translates to secure or non-secure PA space. */
12603             if (arm_is_secure_below_el3(env)) {
12604                 if (attrs->secure) {
12605                     attrs->secure =
12606                         !(env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW));
12607                 } else {
12608                     attrs->secure =
12609                         !((env->cp15.vtcr_el2.raw_tcr & (VTCR_NSA | VTCR_NSW))
12610                         || (env->cp15.vstcr_el2.raw_tcr & VSTCR_SA));
12611                 }
12612             }
12613             return 0;
12614         } else {
12615             /*
12616              * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
12617              */
12618             mmu_idx = stage_1_mmu_idx(mmu_idx);
12619         }
12620     }
12621 
12622     /* The page table entries may downgrade secure to non-secure, but
12623      * cannot upgrade an non-secure translation regime's attributes
12624      * to secure.
12625      */
12626     attrs->secure = regime_is_secure(env, mmu_idx);
12627     attrs->user = regime_is_user(env, mmu_idx);
12628 
12629     /* Fast Context Switch Extension. This doesn't exist at all in v8.
12630      * In v7 and earlier it affects all stage 1 translations.
12631      */
12632     if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
12633         && !arm_feature(env, ARM_FEATURE_V8)) {
12634         if (regime_el(env, mmu_idx) == 3) {
12635             address += env->cp15.fcseidr_s;
12636         } else {
12637             address += env->cp15.fcseidr_ns;
12638         }
12639     }
12640 
12641     if (arm_feature(env, ARM_FEATURE_PMSA)) {
12642         bool ret;
12643         *page_size = TARGET_PAGE_SIZE;
12644 
12645         if (arm_feature(env, ARM_FEATURE_V8)) {
12646             /* PMSAv8 */
12647             ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
12648                                        phys_ptr, attrs, prot, page_size, fi);
12649         } else if (arm_feature(env, ARM_FEATURE_V7)) {
12650             /* PMSAv7 */
12651             ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
12652                                        phys_ptr, prot, page_size, fi);
12653         } else {
12654             /* Pre-v7 MPU */
12655             ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
12656                                        phys_ptr, prot, fi);
12657         }
12658         qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
12659                       " mmu_idx %u -> %s (prot %c%c%c)\n",
12660                       access_type == MMU_DATA_LOAD ? "reading" :
12661                       (access_type == MMU_DATA_STORE ? "writing" : "execute"),
12662                       (uint32_t)address, mmu_idx,
12663                       ret ? "Miss" : "Hit",
12664                       *prot & PAGE_READ ? 'r' : '-',
12665                       *prot & PAGE_WRITE ? 'w' : '-',
12666                       *prot & PAGE_EXEC ? 'x' : '-');
12667 
12668         return ret;
12669     }
12670 
12671     /* Definitely a real MMU, not an MPU */
12672 
12673     if (regime_translation_disabled(env, mmu_idx)) {
12674         uint64_t hcr;
12675         uint8_t memattr;
12676 
12677         /*
12678          * MMU disabled.  S1 addresses within aa64 translation regimes are
12679          * still checked for bounds -- see AArch64.TranslateAddressS1Off.
12680          */
12681         if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
12682             int r_el = regime_el(env, mmu_idx);
12683             if (arm_el_is_aa64(env, r_el)) {
12684                 int pamax = arm_pamax(env_archcpu(env));
12685                 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
12686                 int addrtop, tbi;
12687 
12688                 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12689                 if (access_type == MMU_INST_FETCH) {
12690                     tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12691                 }
12692                 tbi = (tbi >> extract64(address, 55, 1)) & 1;
12693                 addrtop = (tbi ? 55 : 63);
12694 
12695                 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
12696                     fi->type = ARMFault_AddressSize;
12697                     fi->level = 0;
12698                     fi->stage2 = false;
12699                     return 1;
12700                 }
12701 
12702                 /*
12703                  * When TBI is disabled, we've just validated that all of the
12704                  * bits above PAMax are zero, so logically we only need to
12705                  * clear the top byte for TBI.  But it's clearer to follow
12706                  * the pseudocode set of addrdesc.paddress.
12707                  */
12708                 address = extract64(address, 0, 52);
12709             }
12710         }
12711         *phys_ptr = address;
12712         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12713         *page_size = TARGET_PAGE_SIZE;
12714 
12715         /* Fill in cacheattr a-la AArch64.TranslateAddressS1Off. */
12716         hcr = arm_hcr_el2_eff(env);
12717         cacheattrs->shareability = 0;
12718         if (hcr & HCR_DC) {
12719             if (hcr & HCR_DCT) {
12720                 memattr = 0xf0;  /* Tagged, Normal, WB, RWA */
12721             } else {
12722                 memattr = 0xff;  /* Normal, WB, RWA */
12723             }
12724         } else if (access_type == MMU_INST_FETCH) {
12725             if (regime_sctlr(env, mmu_idx) & SCTLR_I) {
12726                 memattr = 0xee;  /* Normal, WT, RA, NT */
12727             } else {
12728                 memattr = 0x44;  /* Normal, NC, No */
12729             }
12730             cacheattrs->shareability = 2; /* outer sharable */
12731         } else {
12732             memattr = 0x00;      /* Device, nGnRnE */
12733         }
12734         cacheattrs->attrs = memattr;
12735         return 0;
12736     }
12737 
12738     if (regime_using_lpae_format(env, mmu_idx)) {
12739         return get_phys_addr_lpae(env, address, access_type, mmu_idx, false,
12740                                   phys_ptr, attrs, prot, page_size,
12741                                   fi, cacheattrs);
12742     } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
12743         return get_phys_addr_v6(env, address, access_type, mmu_idx,
12744                                 phys_ptr, attrs, prot, page_size, fi);
12745     } else {
12746         return get_phys_addr_v5(env, address, access_type, mmu_idx,
12747                                     phys_ptr, prot, page_size, fi);
12748     }
12749 }
12750 
12751 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12752                                          MemTxAttrs *attrs)
12753 {
12754     ARMCPU *cpu = ARM_CPU(cs);
12755     CPUARMState *env = &cpu->env;
12756     hwaddr phys_addr;
12757     target_ulong page_size;
12758     int prot;
12759     bool ret;
12760     ARMMMUFaultInfo fi = {};
12761     ARMMMUIdx mmu_idx = arm_mmu_idx(env);
12762     ARMCacheAttrs cacheattrs = {};
12763 
12764     *attrs = (MemTxAttrs) {};
12765 
12766     ret = get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &phys_addr,
12767                         attrs, &prot, &page_size, &fi, &cacheattrs);
12768 
12769     if (ret) {
12770         return -1;
12771     }
12772     return phys_addr;
12773 }
12774 
12775 #endif
12776 
12777 /* Note that signed overflow is undefined in C.  The following routines are
12778    careful to use unsigned types where modulo arithmetic is required.
12779    Failure to do so _will_ break on newer gcc.  */
12780 
12781 /* Signed saturating arithmetic.  */
12782 
12783 /* Perform 16-bit signed saturating addition.  */
12784 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12785 {
12786     uint16_t res;
12787 
12788     res = a + b;
12789     if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12790         if (a & 0x8000)
12791             res = 0x8000;
12792         else
12793             res = 0x7fff;
12794     }
12795     return res;
12796 }
12797 
12798 /* Perform 8-bit signed saturating addition.  */
12799 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12800 {
12801     uint8_t res;
12802 
12803     res = a + b;
12804     if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12805         if (a & 0x80)
12806             res = 0x80;
12807         else
12808             res = 0x7f;
12809     }
12810     return res;
12811 }
12812 
12813 /* Perform 16-bit signed saturating subtraction.  */
12814 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12815 {
12816     uint16_t res;
12817 
12818     res = a - b;
12819     if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12820         if (a & 0x8000)
12821             res = 0x8000;
12822         else
12823             res = 0x7fff;
12824     }
12825     return res;
12826 }
12827 
12828 /* Perform 8-bit signed saturating subtraction.  */
12829 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12830 {
12831     uint8_t res;
12832 
12833     res = a - b;
12834     if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12835         if (a & 0x80)
12836             res = 0x80;
12837         else
12838             res = 0x7f;
12839     }
12840     return res;
12841 }
12842 
12843 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12844 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12845 #define ADD8(a, b, n)  RESULT(add8_sat(a, b), n, 8);
12846 #define SUB8(a, b, n)  RESULT(sub8_sat(a, b), n, 8);
12847 #define PFX q
12848 
12849 #include "op_addsub.h"
12850 
12851 /* Unsigned saturating arithmetic.  */
12852 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
12853 {
12854     uint16_t res;
12855     res = a + b;
12856     if (res < a)
12857         res = 0xffff;
12858     return res;
12859 }
12860 
12861 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
12862 {
12863     if (a > b)
12864         return a - b;
12865     else
12866         return 0;
12867 }
12868 
12869 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12870 {
12871     uint8_t res;
12872     res = a + b;
12873     if (res < a)
12874         res = 0xff;
12875     return res;
12876 }
12877 
12878 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12879 {
12880     if (a > b)
12881         return a - b;
12882     else
12883         return 0;
12884 }
12885 
12886 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12887 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12888 #define ADD8(a, b, n)  RESULT(add8_usat(a, b), n, 8);
12889 #define SUB8(a, b, n)  RESULT(sub8_usat(a, b), n, 8);
12890 #define PFX uq
12891 
12892 #include "op_addsub.h"
12893 
12894 /* Signed modulo arithmetic.  */
12895 #define SARITH16(a, b, n, op) do { \
12896     int32_t sum; \
12897     sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
12898     RESULT(sum, n, 16); \
12899     if (sum >= 0) \
12900         ge |= 3 << (n * 2); \
12901     } while(0)
12902 
12903 #define SARITH8(a, b, n, op) do { \
12904     int32_t sum; \
12905     sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
12906     RESULT(sum, n, 8); \
12907     if (sum >= 0) \
12908         ge |= 1 << n; \
12909     } while(0)
12910 
12911 
12912 #define ADD16(a, b, n) SARITH16(a, b, n, +)
12913 #define SUB16(a, b, n) SARITH16(a, b, n, -)
12914 #define ADD8(a, b, n)  SARITH8(a, b, n, +)
12915 #define SUB8(a, b, n)  SARITH8(a, b, n, -)
12916 #define PFX s
12917 #define ARITH_GE
12918 
12919 #include "op_addsub.h"
12920 
12921 /* Unsigned modulo arithmetic.  */
12922 #define ADD16(a, b, n) do { \
12923     uint32_t sum; \
12924     sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12925     RESULT(sum, n, 16); \
12926     if ((sum >> 16) == 1) \
12927         ge |= 3 << (n * 2); \
12928     } while(0)
12929 
12930 #define ADD8(a, b, n) do { \
12931     uint32_t sum; \
12932     sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12933     RESULT(sum, n, 8); \
12934     if ((sum >> 8) == 1) \
12935         ge |= 1 << n; \
12936     } while(0)
12937 
12938 #define SUB16(a, b, n) do { \
12939     uint32_t sum; \
12940     sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12941     RESULT(sum, n, 16); \
12942     if ((sum >> 16) == 0) \
12943         ge |= 3 << (n * 2); \
12944     } while(0)
12945 
12946 #define SUB8(a, b, n) do { \
12947     uint32_t sum; \
12948     sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12949     RESULT(sum, n, 8); \
12950     if ((sum >> 8) == 0) \
12951         ge |= 1 << n; \
12952     } while(0)
12953 
12954 #define PFX u
12955 #define ARITH_GE
12956 
12957 #include "op_addsub.h"
12958 
12959 /* Halved signed arithmetic.  */
12960 #define ADD16(a, b, n) \
12961   RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12962 #define SUB16(a, b, n) \
12963   RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12964 #define ADD8(a, b, n) \
12965   RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12966 #define SUB8(a, b, n) \
12967   RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12968 #define PFX sh
12969 
12970 #include "op_addsub.h"
12971 
12972 /* Halved unsigned arithmetic.  */
12973 #define ADD16(a, b, n) \
12974   RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12975 #define SUB16(a, b, n) \
12976   RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12977 #define ADD8(a, b, n) \
12978   RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12979 #define SUB8(a, b, n) \
12980   RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12981 #define PFX uh
12982 
12983 #include "op_addsub.h"
12984 
12985 static inline uint8_t do_usad(uint8_t a, uint8_t b)
12986 {
12987     if (a > b)
12988         return a - b;
12989     else
12990         return b - a;
12991 }
12992 
12993 /* Unsigned sum of absolute byte differences.  */
12994 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12995 {
12996     uint32_t sum;
12997     sum = do_usad(a, b);
12998     sum += do_usad(a >> 8, b >> 8);
12999     sum += do_usad(a >> 16, b >> 16);
13000     sum += do_usad(a >> 24, b >> 24);
13001     return sum;
13002 }
13003 
13004 /* For ARMv6 SEL instruction.  */
13005 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
13006 {
13007     uint32_t mask;
13008 
13009     mask = 0;
13010     if (flags & 1)
13011         mask |= 0xff;
13012     if (flags & 2)
13013         mask |= 0xff00;
13014     if (flags & 4)
13015         mask |= 0xff0000;
13016     if (flags & 8)
13017         mask |= 0xff000000;
13018     return (a & mask) | (b & ~mask);
13019 }
13020 
13021 /* CRC helpers.
13022  * The upper bytes of val (above the number specified by 'bytes') must have
13023  * been zeroed out by the caller.
13024  */
13025 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
13026 {
13027     uint8_t buf[4];
13028 
13029     stl_le_p(buf, val);
13030 
13031     /* zlib crc32 converts the accumulator and output to one's complement.  */
13032     return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
13033 }
13034 
13035 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
13036 {
13037     uint8_t buf[4];
13038 
13039     stl_le_p(buf, val);
13040 
13041     /* Linux crc32c converts the output to one's complement.  */
13042     return crc32c(acc, buf, bytes) ^ 0xffffffff;
13043 }
13044 
13045 /* Return the exception level to which FP-disabled exceptions should
13046  * be taken, or 0 if FP is enabled.
13047  */
13048 int fp_exception_el(CPUARMState *env, int cur_el)
13049 {
13050 #ifndef CONFIG_USER_ONLY
13051     uint64_t hcr_el2;
13052 
13053     /* CPACR and the CPTR registers don't exist before v6, so FP is
13054      * always accessible
13055      */
13056     if (!arm_feature(env, ARM_FEATURE_V6)) {
13057         return 0;
13058     }
13059 
13060     if (arm_feature(env, ARM_FEATURE_M)) {
13061         /* CPACR can cause a NOCP UsageFault taken to current security state */
13062         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
13063             return 1;
13064         }
13065 
13066         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
13067             if (!extract32(env->v7m.nsacr, 10, 1)) {
13068                 /* FP insns cause a NOCP UsageFault taken to Secure */
13069                 return 3;
13070             }
13071         }
13072 
13073         return 0;
13074     }
13075 
13076     hcr_el2 = arm_hcr_el2_eff(env);
13077 
13078     /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
13079      * 0, 2 : trap EL0 and EL1/PL1 accesses
13080      * 1    : trap only EL0 accesses
13081      * 3    : trap no accesses
13082      * This register is ignored if E2H+TGE are both set.
13083      */
13084     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
13085         int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
13086 
13087         switch (fpen) {
13088         case 0:
13089         case 2:
13090             if (cur_el == 0 || cur_el == 1) {
13091                 /* Trap to PL1, which might be EL1 or EL3 */
13092                 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
13093                     return 3;
13094                 }
13095                 return 1;
13096             }
13097             if (cur_el == 3 && !is_a64(env)) {
13098                 /* Secure PL1 running at EL3 */
13099                 return 3;
13100             }
13101             break;
13102         case 1:
13103             if (cur_el == 0) {
13104                 return 1;
13105             }
13106             break;
13107         case 3:
13108             break;
13109         }
13110     }
13111 
13112     /*
13113      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
13114      * to control non-secure access to the FPU. It doesn't have any
13115      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
13116      */
13117     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
13118          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
13119         if (!extract32(env->cp15.nsacr, 10, 1)) {
13120             /* FP insns act as UNDEF */
13121             return cur_el == 2 ? 2 : 1;
13122         }
13123     }
13124 
13125     /*
13126      * CPTR_EL2 is present in v7VE or v8, and changes format
13127      * with HCR_EL2.E2H (regardless of TGE).
13128      */
13129     if (cur_el <= 2) {
13130         if (hcr_el2 & HCR_E2H) {
13131             /* Check CPTR_EL2.FPEN.  */
13132             switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
13133             case 1:
13134                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
13135                     break;
13136                 }
13137                 /* fall through */
13138             case 0:
13139             case 2:
13140                 return 2;
13141             }
13142         } else if (arm_is_el2_enabled(env)) {
13143             if (env->cp15.cptr_el[2] & CPTR_TFP) {
13144                 return 2;
13145             }
13146         }
13147     }
13148 
13149     /* CPTR_EL3 : present in v8 */
13150     if (env->cp15.cptr_el[3] & CPTR_TFP) {
13151         /* Trap all FP ops to EL3 */
13152         return 3;
13153     }
13154 #endif
13155     return 0;
13156 }
13157 
13158 /* Return the exception level we're running at if this is our mmu_idx */
13159 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
13160 {
13161     if (mmu_idx & ARM_MMU_IDX_M) {
13162         return mmu_idx & ARM_MMU_IDX_M_PRIV;
13163     }
13164 
13165     switch (mmu_idx) {
13166     case ARMMMUIdx_E10_0:
13167     case ARMMMUIdx_E20_0:
13168     case ARMMMUIdx_SE10_0:
13169     case ARMMMUIdx_SE20_0:
13170         return 0;
13171     case ARMMMUIdx_E10_1:
13172     case ARMMMUIdx_E10_1_PAN:
13173     case ARMMMUIdx_SE10_1:
13174     case ARMMMUIdx_SE10_1_PAN:
13175         return 1;
13176     case ARMMMUIdx_E2:
13177     case ARMMMUIdx_E20_2:
13178     case ARMMMUIdx_E20_2_PAN:
13179     case ARMMMUIdx_SE2:
13180     case ARMMMUIdx_SE20_2:
13181     case ARMMMUIdx_SE20_2_PAN:
13182         return 2;
13183     case ARMMMUIdx_SE3:
13184         return 3;
13185     default:
13186         g_assert_not_reached();
13187     }
13188 }
13189 
13190 #ifndef CONFIG_TCG
13191 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
13192 {
13193     g_assert_not_reached();
13194 }
13195 #endif
13196 
13197 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
13198 {
13199     ARMMMUIdx idx;
13200     uint64_t hcr;
13201 
13202     if (arm_feature(env, ARM_FEATURE_M)) {
13203         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
13204     }
13205 
13206     /* See ARM pseudo-function ELIsInHost.  */
13207     switch (el) {
13208     case 0:
13209         hcr = arm_hcr_el2_eff(env);
13210         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
13211             idx = ARMMMUIdx_E20_0;
13212         } else {
13213             idx = ARMMMUIdx_E10_0;
13214         }
13215         break;
13216     case 1:
13217         if (env->pstate & PSTATE_PAN) {
13218             idx = ARMMMUIdx_E10_1_PAN;
13219         } else {
13220             idx = ARMMMUIdx_E10_1;
13221         }
13222         break;
13223     case 2:
13224         /* Note that TGE does not apply at EL2.  */
13225         if (arm_hcr_el2_eff(env) & HCR_E2H) {
13226             if (env->pstate & PSTATE_PAN) {
13227                 idx = ARMMMUIdx_E20_2_PAN;
13228             } else {
13229                 idx = ARMMMUIdx_E20_2;
13230             }
13231         } else {
13232             idx = ARMMMUIdx_E2;
13233         }
13234         break;
13235     case 3:
13236         return ARMMMUIdx_SE3;
13237     default:
13238         g_assert_not_reached();
13239     }
13240 
13241     if (arm_is_secure_below_el3(env)) {
13242         idx &= ~ARM_MMU_IDX_A_NS;
13243     }
13244 
13245     return idx;
13246 }
13247 
13248 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
13249 {
13250     return arm_mmu_idx_el(env, arm_current_el(env));
13251 }
13252 
13253 #ifndef CONFIG_USER_ONLY
13254 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
13255 {
13256     return stage_1_mmu_idx(arm_mmu_idx(env));
13257 }
13258 #endif
13259 
13260 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
13261                                            ARMMMUIdx mmu_idx,
13262                                            CPUARMTBFlags flags)
13263 {
13264     DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
13265     DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
13266 
13267     if (arm_singlestep_active(env)) {
13268         DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
13269     }
13270     return flags;
13271 }
13272 
13273 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
13274                                               ARMMMUIdx mmu_idx,
13275                                               CPUARMTBFlags flags)
13276 {
13277     bool sctlr_b = arm_sctlr_b(env);
13278 
13279     if (sctlr_b) {
13280         DP_TBFLAG_A32(flags, SCTLR__B, 1);
13281     }
13282     if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
13283         DP_TBFLAG_ANY(flags, BE_DATA, 1);
13284     }
13285     DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
13286 
13287     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13288 }
13289 
13290 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
13291                                         ARMMMUIdx mmu_idx)
13292 {
13293     CPUARMTBFlags flags = {};
13294     uint32_t ccr = env->v7m.ccr[env->v7m.secure];
13295 
13296     /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
13297     if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
13298         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13299     }
13300 
13301     if (arm_v7m_is_handler_mode(env)) {
13302         DP_TBFLAG_M32(flags, HANDLER, 1);
13303     }
13304 
13305     /*
13306      * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
13307      * is suppressing them because the requested execution priority
13308      * is less than 0.
13309      */
13310     if (arm_feature(env, ARM_FEATURE_V8) &&
13311         !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
13312           (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
13313         DP_TBFLAG_M32(flags, STACKCHECK, 1);
13314     }
13315 
13316     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
13317 }
13318 
13319 static CPUARMTBFlags rebuild_hflags_aprofile(CPUARMState *env)
13320 {
13321     CPUARMTBFlags flags = {};
13322 
13323     DP_TBFLAG_ANY(flags, DEBUG_TARGET_EL, arm_debug_target_el(env));
13324     return flags;
13325 }
13326 
13327 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
13328                                         ARMMMUIdx mmu_idx)
13329 {
13330     CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
13331     int el = arm_current_el(env);
13332 
13333     if (arm_sctlr(env, el) & SCTLR_A) {
13334         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13335     }
13336 
13337     if (arm_el_is_aa64(env, 1)) {
13338         DP_TBFLAG_A32(flags, VFPEN, 1);
13339     }
13340 
13341     if (el < 2 && env->cp15.hstr_el2 &&
13342         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
13343         DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
13344     }
13345 
13346     if (env->uncached_cpsr & CPSR_IL) {
13347         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13348     }
13349 
13350     return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
13351 }
13352 
13353 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
13354                                         ARMMMUIdx mmu_idx)
13355 {
13356     CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
13357     ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
13358     uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
13359     uint64_t sctlr;
13360     int tbii, tbid;
13361 
13362     DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
13363 
13364     /* Get control bits for tagged addresses.  */
13365     tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
13366     tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
13367 
13368     DP_TBFLAG_A64(flags, TBII, tbii);
13369     DP_TBFLAG_A64(flags, TBID, tbid);
13370 
13371     if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
13372         int sve_el = sve_exception_el(env, el);
13373         uint32_t zcr_len;
13374 
13375         /*
13376          * If SVE is disabled, but FP is enabled,
13377          * then the effective len is 0.
13378          */
13379         if (sve_el != 0 && fp_el == 0) {
13380             zcr_len = 0;
13381         } else {
13382             zcr_len = sve_zcr_len_for_el(env, el);
13383         }
13384         DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
13385         DP_TBFLAG_A64(flags, ZCR_LEN, zcr_len);
13386     }
13387 
13388     sctlr = regime_sctlr(env, stage1);
13389 
13390     if (sctlr & SCTLR_A) {
13391         DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13392     }
13393 
13394     if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
13395         DP_TBFLAG_ANY(flags, BE_DATA, 1);
13396     }
13397 
13398     if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
13399         /*
13400          * In order to save space in flags, we record only whether
13401          * pauth is "inactive", meaning all insns are implemented as
13402          * a nop, or "active" when some action must be performed.
13403          * The decision of which action to take is left to a helper.
13404          */
13405         if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
13406             DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
13407         }
13408     }
13409 
13410     if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
13411         /* Note that SCTLR_EL[23].BT == SCTLR_BT1.  */
13412         if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
13413             DP_TBFLAG_A64(flags, BT, 1);
13414         }
13415     }
13416 
13417     /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
13418     if (!(env->pstate & PSTATE_UAO)) {
13419         switch (mmu_idx) {
13420         case ARMMMUIdx_E10_1:
13421         case ARMMMUIdx_E10_1_PAN:
13422         case ARMMMUIdx_SE10_1:
13423         case ARMMMUIdx_SE10_1_PAN:
13424             /* TODO: ARMv8.3-NV */
13425             DP_TBFLAG_A64(flags, UNPRIV, 1);
13426             break;
13427         case ARMMMUIdx_E20_2:
13428         case ARMMMUIdx_E20_2_PAN:
13429         case ARMMMUIdx_SE20_2:
13430         case ARMMMUIdx_SE20_2_PAN:
13431             /*
13432              * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
13433              * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
13434              */
13435             if (env->cp15.hcr_el2 & HCR_TGE) {
13436                 DP_TBFLAG_A64(flags, UNPRIV, 1);
13437             }
13438             break;
13439         default:
13440             break;
13441         }
13442     }
13443 
13444     if (env->pstate & PSTATE_IL) {
13445         DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13446     }
13447 
13448     if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
13449         /*
13450          * Set MTE_ACTIVE if any access may be Checked, and leave clear
13451          * if all accesses must be Unchecked:
13452          * 1) If no TBI, then there are no tags in the address to check,
13453          * 2) If Tag Check Override, then all accesses are Unchecked,
13454          * 3) If Tag Check Fail == 0, then Checked access have no effect,
13455          * 4) If no Allocation Tag Access, then all accesses are Unchecked.
13456          */
13457         if (allocation_tag_access_enabled(env, el, sctlr)) {
13458             DP_TBFLAG_A64(flags, ATA, 1);
13459             if (tbid
13460                 && !(env->pstate & PSTATE_TCO)
13461                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
13462                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
13463             }
13464         }
13465         /* And again for unprivileged accesses, if required.  */
13466         if (EX_TBFLAG_A64(flags, UNPRIV)
13467             && tbid
13468             && !(env->pstate & PSTATE_TCO)
13469             && (sctlr & SCTLR_TCF0)
13470             && allocation_tag_access_enabled(env, 0, sctlr)) {
13471             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
13472         }
13473         /* Cache TCMA as well as TBI. */
13474         DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
13475     }
13476 
13477     return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13478 }
13479 
13480 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
13481 {
13482     int el = arm_current_el(env);
13483     int fp_el = fp_exception_el(env, el);
13484     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13485 
13486     if (is_a64(env)) {
13487         return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13488     } else if (arm_feature(env, ARM_FEATURE_M)) {
13489         return rebuild_hflags_m32(env, fp_el, mmu_idx);
13490     } else {
13491         return rebuild_hflags_a32(env, fp_el, mmu_idx);
13492     }
13493 }
13494 
13495 void arm_rebuild_hflags(CPUARMState *env)
13496 {
13497     env->hflags = rebuild_hflags_internal(env);
13498 }
13499 
13500 /*
13501  * If we have triggered a EL state change we can't rely on the
13502  * translator having passed it to us, we need to recompute.
13503  */
13504 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
13505 {
13506     int el = arm_current_el(env);
13507     int fp_el = fp_exception_el(env, el);
13508     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13509 
13510     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13511 }
13512 
13513 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
13514 {
13515     int fp_el = fp_exception_el(env, el);
13516     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13517 
13518     env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13519 }
13520 
13521 /*
13522  * If we have triggered a EL state change we can't rely on the
13523  * translator having passed it to us, we need to recompute.
13524  */
13525 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
13526 {
13527     int el = arm_current_el(env);
13528     int fp_el = fp_exception_el(env, el);
13529     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13530     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13531 }
13532 
13533 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
13534 {
13535     int fp_el = fp_exception_el(env, el);
13536     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13537 
13538     env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13539 }
13540 
13541 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
13542 {
13543     int fp_el = fp_exception_el(env, el);
13544     ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13545 
13546     env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13547 }
13548 
13549 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
13550 {
13551 #ifdef CONFIG_DEBUG_TCG
13552     CPUARMTBFlags c = env->hflags;
13553     CPUARMTBFlags r = rebuild_hflags_internal(env);
13554 
13555     if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
13556         fprintf(stderr, "TCG hflags mismatch "
13557                         "(current:(0x%08x,0x" TARGET_FMT_lx ")"
13558                         " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
13559                 c.flags, c.flags2, r.flags, r.flags2);
13560         abort();
13561     }
13562 #endif
13563 }
13564 
13565 static bool mve_no_pred(CPUARMState *env)
13566 {
13567     /*
13568      * Return true if there is definitely no predication of MVE
13569      * instructions by VPR or LTPSIZE. (Returning false even if there
13570      * isn't any predication is OK; generated code will just be
13571      * a little worse.)
13572      * If the CPU does not implement MVE then this TB flag is always 0.
13573      *
13574      * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
13575      * logic in gen_update_fp_context() needs to be updated to match.
13576      *
13577      * We do not include the effect of the ECI bits here -- they are
13578      * tracked in other TB flags. This simplifies the logic for
13579      * "when did we emit code that changes the MVE_NO_PRED TB flag
13580      * and thus need to end the TB?".
13581      */
13582     if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
13583         return false;
13584     }
13585     if (env->v7m.vpr) {
13586         return false;
13587     }
13588     if (env->v7m.ltpsize < 4) {
13589         return false;
13590     }
13591     return true;
13592 }
13593 
13594 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
13595                           target_ulong *cs_base, uint32_t *pflags)
13596 {
13597     CPUARMTBFlags flags;
13598 
13599     assert_hflags_rebuild_correctly(env);
13600     flags = env->hflags;
13601 
13602     if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
13603         *pc = env->pc;
13604         if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
13605             DP_TBFLAG_A64(flags, BTYPE, env->btype);
13606         }
13607     } else {
13608         *pc = env->regs[15];
13609 
13610         if (arm_feature(env, ARM_FEATURE_M)) {
13611             if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
13612                 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
13613                 != env->v7m.secure) {
13614                 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
13615             }
13616 
13617             if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
13618                 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
13619                  (env->v7m.secure &&
13620                   !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
13621                 /*
13622                  * ASPEN is set, but FPCA/SFPA indicate that there is no
13623                  * active FP context; we must create a new FP context before
13624                  * executing any FP insn.
13625                  */
13626                 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
13627             }
13628 
13629             bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
13630             if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
13631                 DP_TBFLAG_M32(flags, LSPACT, 1);
13632             }
13633 
13634             if (mve_no_pred(env)) {
13635                 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
13636             }
13637         } else {
13638             /*
13639              * Note that XSCALE_CPAR shares bits with VECSTRIDE.
13640              * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
13641              */
13642             if (arm_feature(env, ARM_FEATURE_XSCALE)) {
13643                 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
13644             } else {
13645                 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
13646                 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
13647             }
13648             if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
13649                 DP_TBFLAG_A32(flags, VFPEN, 1);
13650             }
13651         }
13652 
13653         DP_TBFLAG_AM32(flags, THUMB, env->thumb);
13654         DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
13655     }
13656 
13657     /*
13658      * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
13659      * states defined in the ARM ARM for software singlestep:
13660      *  SS_ACTIVE   PSTATE.SS   State
13661      *     0            x       Inactive (the TB flag for SS is always 0)
13662      *     1            0       Active-pending
13663      *     1            1       Active-not-pending
13664      * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
13665      */
13666     if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
13667         DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
13668     }
13669 
13670     *pflags = flags.flags;
13671     *cs_base = flags.flags2;
13672 }
13673 
13674 #ifdef TARGET_AARCH64
13675 /*
13676  * The manual says that when SVE is enabled and VQ is widened the
13677  * implementation is allowed to zero the previously inaccessible
13678  * portion of the registers.  The corollary to that is that when
13679  * SVE is enabled and VQ is narrowed we are also allowed to zero
13680  * the now inaccessible portion of the registers.
13681  *
13682  * The intent of this is that no predicate bit beyond VQ is ever set.
13683  * Which means that some operations on predicate registers themselves
13684  * may operate on full uint64_t or even unrolled across the maximum
13685  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
13686  * may well be cheaper than conditionals to restrict the operation
13687  * to the relevant portion of a uint16_t[16].
13688  */
13689 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
13690 {
13691     int i, j;
13692     uint64_t pmask;
13693 
13694     assert(vq >= 1 && vq <= ARM_MAX_VQ);
13695     assert(vq <= env_archcpu(env)->sve_max_vq);
13696 
13697     /* Zap the high bits of the zregs.  */
13698     for (i = 0; i < 32; i++) {
13699         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
13700     }
13701 
13702     /* Zap the high bits of the pregs and ffr.  */
13703     pmask = 0;
13704     if (vq & 3) {
13705         pmask = ~(-1ULL << (16 * (vq & 3)));
13706     }
13707     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13708         for (i = 0; i < 17; ++i) {
13709             env->vfp.pregs[i].p[j] &= pmask;
13710         }
13711         pmask = 0;
13712     }
13713 }
13714 
13715 /*
13716  * Notice a change in SVE vector size when changing EL.
13717  */
13718 void aarch64_sve_change_el(CPUARMState *env, int old_el,
13719                            int new_el, bool el0_a64)
13720 {
13721     ARMCPU *cpu = env_archcpu(env);
13722     int old_len, new_len;
13723     bool old_a64, new_a64;
13724 
13725     /* Nothing to do if no SVE.  */
13726     if (!cpu_isar_feature(aa64_sve, cpu)) {
13727         return;
13728     }
13729 
13730     /* Nothing to do if FP is disabled in either EL.  */
13731     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
13732         return;
13733     }
13734 
13735     /*
13736      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
13737      * at ELx, or not available because the EL is in AArch32 state, then
13738      * for all purposes other than a direct read, the ZCR_ELx.LEN field
13739      * has an effective value of 0".
13740      *
13741      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
13742      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
13743      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
13744      * we already have the correct register contents when encountering the
13745      * vq0->vq0 transition between EL0->EL1.
13746      */
13747     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
13748     old_len = (old_a64 && !sve_exception_el(env, old_el)
13749                ? sve_zcr_len_for_el(env, old_el) : 0);
13750     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
13751     new_len = (new_a64 && !sve_exception_el(env, new_el)
13752                ? sve_zcr_len_for_el(env, new_el) : 0);
13753 
13754     /* When changing vector length, clear inaccessible state.  */
13755     if (new_len < old_len) {
13756         aarch64_sve_narrow_vq(env, new_len + 1);
13757     }
13758 }
13759 #endif
13760