target/arm: Move CPU state dumping routines to cpu.c
[qemu/ar7.git] / target / arm / helper.c
bloba87fda919148b16135ef8cbec6f69cae608fcca9
1 /*
2 * ARM generic helpers.
4 * This code is licensed under the GNU GPL v2 or later.
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8 #include "qemu/osdep.h"
9 #include "qemu/units.h"
10 #include "target/arm/idau.h"
11 #include "trace.h"
12 #include "cpu.h"
13 #include "internals.h"
14 #include "exec/gdbstub.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/host-utils.h"
17 #include "sysemu/sysemu.h"
18 #include "qemu/bitops.h"
19 #include "qemu/crc32c.h"
20 #include "qemu/qemu-print.h"
21 #include "exec/exec-all.h"
22 #include "exec/cpu_ldst.h"
23 #include "arm_ldst.h"
24 #include <zlib.h> /* For crc32 */
25 #include "hw/semihosting/semihost.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/kvm.h"
28 #include "qemu/range.h"
29 #include "qapi/qapi-commands-target.h"
30 #include "qapi/error.h"
31 #include "qemu/guest-random.h"
33 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
35 #ifndef CONFIG_USER_ONLY
36 /* Cacheability and shareability attributes for a memory access */
37 typedef struct ARMCacheAttrs {
38 unsigned int attrs:8; /* as in the MAIR register encoding */
39 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */
40 } ARMCacheAttrs;
42 static bool get_phys_addr(CPUARMState *env, target_ulong address,
43 MMUAccessType access_type, ARMMMUIdx mmu_idx,
44 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
45 target_ulong *page_size,
46 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
48 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
49 MMUAccessType access_type, ARMMMUIdx mmu_idx,
50 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
51 target_ulong *page_size_ptr,
52 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
54 /* Security attributes for an address, as returned by v8m_security_lookup. */
55 typedef struct V8M_SAttributes {
56 bool subpage; /* true if these attrs don't cover the whole TARGET_PAGE */
57 bool ns;
58 bool nsc;
59 uint8_t sregion;
60 bool srvalid;
61 uint8_t iregion;
62 bool irvalid;
63 } V8M_SAttributes;
65 static void v8m_security_lookup(CPUARMState *env, uint32_t address,
66 MMUAccessType access_type, ARMMMUIdx mmu_idx,
67 V8M_SAttributes *sattrs);
68 #endif
70 static void switch_mode(CPUARMState *env, int mode);
72 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
74 int nregs;
76 /* VFP data registers are always little-endian. */
77 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
78 if (reg < nregs) {
79 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
80 return 8;
82 if (arm_feature(env, ARM_FEATURE_NEON)) {
83 /* Aliases for Q regs. */
84 nregs += 16;
85 if (reg < nregs) {
86 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
87 stq_le_p(buf, q[0]);
88 stq_le_p(buf + 8, q[1]);
89 return 16;
92 switch (reg - nregs) {
93 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
94 case 1: stl_p(buf, vfp_get_fpscr(env)); return 4;
95 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
97 return 0;
100 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
102 int nregs;
104 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
105 if (reg < nregs) {
106 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
107 return 8;
109 if (arm_feature(env, ARM_FEATURE_NEON)) {
110 nregs += 16;
111 if (reg < nregs) {
112 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
113 q[0] = ldq_le_p(buf);
114 q[1] = ldq_le_p(buf + 8);
115 return 16;
118 switch (reg - nregs) {
119 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
120 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4;
121 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
123 return 0;
126 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
128 switch (reg) {
129 case 0 ... 31:
130 /* 128 bit FP register */
132 uint64_t *q = aa64_vfp_qreg(env, reg);
133 stq_le_p(buf, q[0]);
134 stq_le_p(buf + 8, q[1]);
135 return 16;
137 case 32:
138 /* FPSR */
139 stl_p(buf, vfp_get_fpsr(env));
140 return 4;
141 case 33:
142 /* FPCR */
143 stl_p(buf, vfp_get_fpcr(env));
144 return 4;
145 default:
146 return 0;
150 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
152 switch (reg) {
153 case 0 ... 31:
154 /* 128 bit FP register */
156 uint64_t *q = aa64_vfp_qreg(env, reg);
157 q[0] = ldq_le_p(buf);
158 q[1] = ldq_le_p(buf + 8);
159 return 16;
161 case 32:
162 /* FPSR */
163 vfp_set_fpsr(env, ldl_p(buf));
164 return 4;
165 case 33:
166 /* FPCR */
167 vfp_set_fpcr(env, ldl_p(buf));
168 return 4;
169 default:
170 return 0;
174 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
176 assert(ri->fieldoffset);
177 if (cpreg_field_is_64bit(ri)) {
178 return CPREG_FIELD64(env, ri);
179 } else {
180 return CPREG_FIELD32(env, ri);
184 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
185 uint64_t value)
187 assert(ri->fieldoffset);
188 if (cpreg_field_is_64bit(ri)) {
189 CPREG_FIELD64(env, ri) = value;
190 } else {
191 CPREG_FIELD32(env, ri) = value;
195 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
197 return (char *)env + ri->fieldoffset;
200 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
202 /* Raw read of a coprocessor register (as needed for migration, etc). */
203 if (ri->type & ARM_CP_CONST) {
204 return ri->resetvalue;
205 } else if (ri->raw_readfn) {
206 return ri->raw_readfn(env, ri);
207 } else if (ri->readfn) {
208 return ri->readfn(env, ri);
209 } else {
210 return raw_read(env, ri);
214 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
215 uint64_t v)
217 /* Raw write of a coprocessor register (as needed for migration, etc).
218 * Note that constant registers are treated as write-ignored; the
219 * caller should check for success by whether a readback gives the
220 * value written.
222 if (ri->type & ARM_CP_CONST) {
223 return;
224 } else if (ri->raw_writefn) {
225 ri->raw_writefn(env, ri, v);
226 } else if (ri->writefn) {
227 ri->writefn(env, ri, v);
228 } else {
229 raw_write(env, ri, v);
233 static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg)
235 ARMCPU *cpu = env_archcpu(env);
236 const ARMCPRegInfo *ri;
237 uint32_t key;
239 key = cpu->dyn_xml.cpregs_keys[reg];
240 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
241 if (ri) {
242 if (cpreg_field_is_64bit(ri)) {
243 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
244 } else {
245 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
248 return 0;
251 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
253 return 0;
256 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
258 /* Return true if the regdef would cause an assertion if you called
259 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
260 * program bug for it not to have the NO_RAW flag).
261 * NB that returning false here doesn't necessarily mean that calling
262 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
263 * read/write access functions which are safe for raw use" from "has
264 * read/write access functions which have side effects but has forgotten
265 * to provide raw access functions".
266 * The tests here line up with the conditions in read/write_raw_cp_reg()
267 * and assertions in raw_read()/raw_write().
269 if ((ri->type & ARM_CP_CONST) ||
270 ri->fieldoffset ||
271 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
272 return false;
274 return true;
277 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
279 /* Write the coprocessor state from cpu->env to the (index,value) list. */
280 int i;
281 bool ok = true;
283 for (i = 0; i < cpu->cpreg_array_len; i++) {
284 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
285 const ARMCPRegInfo *ri;
286 uint64_t newval;
288 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
289 if (!ri) {
290 ok = false;
291 continue;
293 if (ri->type & ARM_CP_NO_RAW) {
294 continue;
297 newval = read_raw_cp_reg(&cpu->env, ri);
298 if (kvm_sync) {
300 * Only sync if the previous list->cpustate sync succeeded.
301 * Rather than tracking the success/failure state for every
302 * item in the list, we just recheck "does the raw write we must
303 * have made in write_list_to_cpustate() read back OK" here.
305 uint64_t oldval = cpu->cpreg_values[i];
307 if (oldval == newval) {
308 continue;
311 write_raw_cp_reg(&cpu->env, ri, oldval);
312 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
313 continue;
316 write_raw_cp_reg(&cpu->env, ri, newval);
318 cpu->cpreg_values[i] = newval;
320 return ok;
323 bool write_list_to_cpustate(ARMCPU *cpu)
325 int i;
326 bool ok = true;
328 for (i = 0; i < cpu->cpreg_array_len; i++) {
329 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
330 uint64_t v = cpu->cpreg_values[i];
331 const ARMCPRegInfo *ri;
333 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
334 if (!ri) {
335 ok = false;
336 continue;
338 if (ri->type & ARM_CP_NO_RAW) {
339 continue;
341 /* Write value and confirm it reads back as written
342 * (to catch read-only registers and partially read-only
343 * registers where the incoming migration value doesn't match)
345 write_raw_cp_reg(&cpu->env, ri, v);
346 if (read_raw_cp_reg(&cpu->env, ri) != v) {
347 ok = false;
350 return ok;
353 static void add_cpreg_to_list(gpointer key, gpointer opaque)
355 ARMCPU *cpu = opaque;
356 uint64_t regidx;
357 const ARMCPRegInfo *ri;
359 regidx = *(uint32_t *)key;
360 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
362 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
363 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
364 /* The value array need not be initialized at this point */
365 cpu->cpreg_array_len++;
369 static void count_cpreg(gpointer key, gpointer opaque)
371 ARMCPU *cpu = opaque;
372 uint64_t regidx;
373 const ARMCPRegInfo *ri;
375 regidx = *(uint32_t *)key;
376 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
378 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
379 cpu->cpreg_array_len++;
383 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
385 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
386 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
388 if (aidx > bidx) {
389 return 1;
391 if (aidx < bidx) {
392 return -1;
394 return 0;
397 void init_cpreg_list(ARMCPU *cpu)
399 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
400 * Note that we require cpreg_tuples[] to be sorted by key ID.
402 GList *keys;
403 int arraylen;
405 keys = g_hash_table_get_keys(cpu->cp_regs);
406 keys = g_list_sort(keys, cpreg_key_compare);
408 cpu->cpreg_array_len = 0;
410 g_list_foreach(keys, count_cpreg, cpu);
412 arraylen = cpu->cpreg_array_len;
413 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
414 cpu->cpreg_values = g_new(uint64_t, arraylen);
415 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
416 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
417 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
418 cpu->cpreg_array_len = 0;
420 g_list_foreach(keys, add_cpreg_to_list, cpu);
422 assert(cpu->cpreg_array_len == arraylen);
424 g_list_free(keys);
428 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
429 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
431 * access_el3_aa32ns: Used to check AArch32 register views.
432 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
434 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
435 const ARMCPRegInfo *ri,
436 bool isread)
438 bool secure = arm_is_secure_below_el3(env);
440 assert(!arm_el_is_aa64(env, 3));
441 if (secure) {
442 return CP_ACCESS_TRAP_UNCATEGORIZED;
444 return CP_ACCESS_OK;
447 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
448 const ARMCPRegInfo *ri,
449 bool isread)
451 if (!arm_el_is_aa64(env, 3)) {
452 return access_el3_aa32ns(env, ri, isread);
454 return CP_ACCESS_OK;
457 /* Some secure-only AArch32 registers trap to EL3 if used from
458 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
459 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
460 * We assume that the .access field is set to PL1_RW.
462 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
463 const ARMCPRegInfo *ri,
464 bool isread)
466 if (arm_current_el(env) == 3) {
467 return CP_ACCESS_OK;
469 if (arm_is_secure_below_el3(env)) {
470 return CP_ACCESS_TRAP_EL3;
472 /* This will be EL1 NS and EL2 NS, which just UNDEF */
473 return CP_ACCESS_TRAP_UNCATEGORIZED;
476 /* Check for traps to "powerdown debug" registers, which are controlled
477 * by MDCR.TDOSA
479 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
480 bool isread)
482 int el = arm_current_el(env);
483 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) ||
484 (env->cp15.mdcr_el2 & MDCR_TDE) ||
485 (arm_hcr_el2_eff(env) & HCR_TGE);
487 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) {
488 return CP_ACCESS_TRAP_EL2;
490 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
491 return CP_ACCESS_TRAP_EL3;
493 return CP_ACCESS_OK;
496 /* Check for traps to "debug ROM" registers, which are controlled
497 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
499 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
500 bool isread)
502 int el = arm_current_el(env);
503 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) ||
504 (env->cp15.mdcr_el2 & MDCR_TDE) ||
505 (arm_hcr_el2_eff(env) & HCR_TGE);
507 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) {
508 return CP_ACCESS_TRAP_EL2;
510 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
511 return CP_ACCESS_TRAP_EL3;
513 return CP_ACCESS_OK;
516 /* Check for traps to general debug registers, which are controlled
517 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
519 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
520 bool isread)
522 int el = arm_current_el(env);
523 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) ||
524 (env->cp15.mdcr_el2 & MDCR_TDE) ||
525 (arm_hcr_el2_eff(env) & HCR_TGE);
527 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) {
528 return CP_ACCESS_TRAP_EL2;
530 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
531 return CP_ACCESS_TRAP_EL3;
533 return CP_ACCESS_OK;
536 /* Check for traps to performance monitor registers, which are controlled
537 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
539 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
540 bool isread)
542 int el = arm_current_el(env);
544 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
545 && !arm_is_secure_below_el3(env)) {
546 return CP_ACCESS_TRAP_EL2;
548 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
549 return CP_ACCESS_TRAP_EL3;
551 return CP_ACCESS_OK;
554 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
556 ARMCPU *cpu = env_archcpu(env);
558 raw_write(env, ri, value);
559 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
562 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
564 ARMCPU *cpu = env_archcpu(env);
566 if (raw_read(env, ri) != value) {
567 /* Unlike real hardware the qemu TLB uses virtual addresses,
568 * not modified virtual addresses, so this causes a TLB flush.
570 tlb_flush(CPU(cpu));
571 raw_write(env, ri, value);
575 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
576 uint64_t value)
578 ARMCPU *cpu = env_archcpu(env);
580 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
581 && !extended_addresses_enabled(env)) {
582 /* For VMSA (when not using the LPAE long descriptor page table
583 * format) this register includes the ASID, so do a TLB flush.
584 * For PMSA it is purely a process ID and no action is needed.
586 tlb_flush(CPU(cpu));
588 raw_write(env, ri, value);
591 /* IS variants of TLB operations must affect all cores */
592 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
593 uint64_t value)
595 CPUState *cs = env_cpu(env);
597 tlb_flush_all_cpus_synced(cs);
600 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
601 uint64_t value)
603 CPUState *cs = env_cpu(env);
605 tlb_flush_all_cpus_synced(cs);
608 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
609 uint64_t value)
611 CPUState *cs = env_cpu(env);
613 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
616 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
617 uint64_t value)
619 CPUState *cs = env_cpu(env);
621 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
625 * Non-IS variants of TLB operations are upgraded to
626 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to
627 * force broadcast of these operations.
629 static bool tlb_force_broadcast(CPUARMState *env)
631 return (env->cp15.hcr_el2 & HCR_FB) &&
632 arm_current_el(env) == 1 && arm_is_secure_below_el3(env);
635 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
636 uint64_t value)
638 /* Invalidate all (TLBIALL) */
639 ARMCPU *cpu = env_archcpu(env);
641 if (tlb_force_broadcast(env)) {
642 tlbiall_is_write(env, NULL, value);
643 return;
646 tlb_flush(CPU(cpu));
649 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
650 uint64_t value)
652 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
653 ARMCPU *cpu = env_archcpu(env);
655 if (tlb_force_broadcast(env)) {
656 tlbimva_is_write(env, NULL, value);
657 return;
660 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
663 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
664 uint64_t value)
666 /* Invalidate by ASID (TLBIASID) */
667 ARMCPU *cpu = env_archcpu(env);
669 if (tlb_force_broadcast(env)) {
670 tlbiasid_is_write(env, NULL, value);
671 return;
674 tlb_flush(CPU(cpu));
677 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
678 uint64_t value)
680 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
681 ARMCPU *cpu = env_archcpu(env);
683 if (tlb_force_broadcast(env)) {
684 tlbimvaa_is_write(env, NULL, value);
685 return;
688 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
691 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
692 uint64_t value)
694 CPUState *cs = env_cpu(env);
696 tlb_flush_by_mmuidx(cs,
697 ARMMMUIdxBit_S12NSE1 |
698 ARMMMUIdxBit_S12NSE0 |
699 ARMMMUIdxBit_S2NS);
702 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
703 uint64_t value)
705 CPUState *cs = env_cpu(env);
707 tlb_flush_by_mmuidx_all_cpus_synced(cs,
708 ARMMMUIdxBit_S12NSE1 |
709 ARMMMUIdxBit_S12NSE0 |
710 ARMMMUIdxBit_S2NS);
713 static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
714 uint64_t value)
716 /* Invalidate by IPA. This has to invalidate any structures that
717 * contain only stage 2 translation information, but does not need
718 * to apply to structures that contain combined stage 1 and stage 2
719 * translation information.
720 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
722 CPUState *cs = env_cpu(env);
723 uint64_t pageaddr;
725 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
726 return;
729 pageaddr = sextract64(value << 12, 0, 40);
731 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
734 static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
735 uint64_t value)
737 CPUState *cs = env_cpu(env);
738 uint64_t pageaddr;
740 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
741 return;
744 pageaddr = sextract64(value << 12, 0, 40);
746 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
747 ARMMMUIdxBit_S2NS);
750 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
751 uint64_t value)
753 CPUState *cs = env_cpu(env);
755 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
758 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
759 uint64_t value)
761 CPUState *cs = env_cpu(env);
763 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
766 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
767 uint64_t value)
769 CPUState *cs = env_cpu(env);
770 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
772 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
775 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
776 uint64_t value)
778 CPUState *cs = env_cpu(env);
779 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
781 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
782 ARMMMUIdxBit_S1E2);
785 static const ARMCPRegInfo cp_reginfo[] = {
786 /* Define the secure and non-secure FCSE identifier CP registers
787 * separately because there is no secure bank in V8 (no _EL3). This allows
788 * the secure register to be properly reset and migrated. There is also no
789 * v8 EL1 version of the register so the non-secure instance stands alone.
791 { .name = "FCSEIDR",
792 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
793 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
794 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
795 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
796 { .name = "FCSEIDR_S",
797 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
798 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
799 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
800 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
801 /* Define the secure and non-secure context identifier CP registers
802 * separately because there is no secure bank in V8 (no _EL3). This allows
803 * the secure register to be properly reset and migrated. In the
804 * non-secure case, the 32-bit register will have reset and migration
805 * disabled during registration as it is handled by the 64-bit instance.
807 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
808 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
809 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
810 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
811 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
812 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
813 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
814 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
815 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
816 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
817 REGINFO_SENTINEL
820 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
821 /* NB: Some of these registers exist in v8 but with more precise
822 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
824 /* MMU Domain access control / MPU write buffer control */
825 { .name = "DACR",
826 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
827 .access = PL1_RW, .resetvalue = 0,
828 .writefn = dacr_write, .raw_writefn = raw_write,
829 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
830 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
831 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
832 * For v6 and v5, these mappings are overly broad.
834 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
835 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
836 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
837 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
838 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
839 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
840 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
841 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
842 /* Cache maintenance ops; some of this space may be overridden later. */
843 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
844 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
845 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
846 REGINFO_SENTINEL
849 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
850 /* Not all pre-v6 cores implemented this WFI, so this is slightly
851 * over-broad.
853 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
854 .access = PL1_W, .type = ARM_CP_WFI },
855 REGINFO_SENTINEL
858 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
859 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
860 * is UNPREDICTABLE; we choose to NOP as most implementations do).
862 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
863 .access = PL1_W, .type = ARM_CP_WFI },
864 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
865 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
866 * OMAPCP will override this space.
868 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
869 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
870 .resetvalue = 0 },
871 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
872 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
873 .resetvalue = 0 },
874 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
875 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
876 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
877 .resetvalue = 0 },
878 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
879 * implementing it as RAZ means the "debug architecture version" bits
880 * will read as a reserved value, which should cause Linux to not try
881 * to use the debug hardware.
883 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
884 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
885 /* MMU TLB control. Note that the wildcarding means we cover not just
886 * the unified TLB ops but also the dside/iside/inner-shareable variants.
888 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
889 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
890 .type = ARM_CP_NO_RAW },
891 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
892 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
893 .type = ARM_CP_NO_RAW },
894 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
895 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
896 .type = ARM_CP_NO_RAW },
897 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
898 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
899 .type = ARM_CP_NO_RAW },
900 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
901 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
902 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
903 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
904 REGINFO_SENTINEL
907 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
908 uint64_t value)
910 uint32_t mask = 0;
912 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
913 if (!arm_feature(env, ARM_FEATURE_V8)) {
914 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
915 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
916 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
918 if (arm_feature(env, ARM_FEATURE_VFP)) {
919 /* VFP coprocessor: cp10 & cp11 [23:20] */
920 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
922 if (!arm_feature(env, ARM_FEATURE_NEON)) {
923 /* ASEDIS [31] bit is RAO/WI */
924 value |= (1 << 31);
927 /* VFPv3 and upwards with NEON implement 32 double precision
928 * registers (D0-D31).
930 if (!arm_feature(env, ARM_FEATURE_NEON) ||
931 !arm_feature(env, ARM_FEATURE_VFP3)) {
932 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
933 value |= (1 << 30);
936 value &= mask;
940 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
941 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
943 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
944 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
945 value &= ~(0xf << 20);
946 value |= env->cp15.cpacr_el1 & (0xf << 20);
949 env->cp15.cpacr_el1 = value;
952 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
955 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
956 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
958 uint64_t value = env->cp15.cpacr_el1;
960 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
961 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
962 value &= ~(0xf << 20);
964 return value;
968 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
970 /* Call cpacr_write() so that we reset with the correct RAO bits set
971 * for our CPU features.
973 cpacr_write(env, ri, 0);
976 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
977 bool isread)
979 if (arm_feature(env, ARM_FEATURE_V8)) {
980 /* Check if CPACR accesses are to be trapped to EL2 */
981 if (arm_current_el(env) == 1 &&
982 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
983 return CP_ACCESS_TRAP_EL2;
984 /* Check if CPACR accesses are to be trapped to EL3 */
985 } else if (arm_current_el(env) < 3 &&
986 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
987 return CP_ACCESS_TRAP_EL3;
991 return CP_ACCESS_OK;
994 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
995 bool isread)
997 /* Check if CPTR accesses are set to trap to EL3 */
998 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
999 return CP_ACCESS_TRAP_EL3;
1002 return CP_ACCESS_OK;
1005 static const ARMCPRegInfo v6_cp_reginfo[] = {
1006 /* prefetch by MVA in v6, NOP in v7 */
1007 { .name = "MVA_prefetch",
1008 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
1009 .access = PL1_W, .type = ARM_CP_NOP },
1010 /* We need to break the TB after ISB to execute self-modifying code
1011 * correctly and also to take any pending interrupts immediately.
1012 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
1014 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
1015 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
1016 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
1017 .access = PL0_W, .type = ARM_CP_NOP },
1018 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
1019 .access = PL0_W, .type = ARM_CP_NOP },
1020 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
1021 .access = PL1_RW,
1022 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
1023 offsetof(CPUARMState, cp15.ifar_ns) },
1024 .resetvalue = 0, },
1025 /* Watchpoint Fault Address Register : should actually only be present
1026 * for 1136, 1176, 11MPCore.
1028 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1029 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
1030 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
1031 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
1032 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
1033 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
1034 REGINFO_SENTINEL
1037 /* Definitions for the PMU registers */
1038 #define PMCRN_MASK 0xf800
1039 #define PMCRN_SHIFT 11
1040 #define PMCRLC 0x40
1041 #define PMCRDP 0x10
1042 #define PMCRD 0x8
1043 #define PMCRC 0x4
1044 #define PMCRP 0x2
1045 #define PMCRE 0x1
1047 #define PMXEVTYPER_P 0x80000000
1048 #define PMXEVTYPER_U 0x40000000
1049 #define PMXEVTYPER_NSK 0x20000000
1050 #define PMXEVTYPER_NSU 0x10000000
1051 #define PMXEVTYPER_NSH 0x08000000
1052 #define PMXEVTYPER_M 0x04000000
1053 #define PMXEVTYPER_MT 0x02000000
1054 #define PMXEVTYPER_EVTCOUNT 0x0000ffff
1055 #define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
1056 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
1057 PMXEVTYPER_M | PMXEVTYPER_MT | \
1058 PMXEVTYPER_EVTCOUNT)
1060 #define PMCCFILTR 0xf8000000
1061 #define PMCCFILTR_M PMXEVTYPER_M
1062 #define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M)
1064 static inline uint32_t pmu_num_counters(CPUARMState *env)
1066 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1069 /* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1070 static inline uint64_t pmu_counter_mask(CPUARMState *env)
1072 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1075 typedef struct pm_event {
1076 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1077 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1078 bool (*supported)(CPUARMState *);
1080 * Retrieve the current count of the underlying event. The programmed
1081 * counters hold a difference from the return value from this function
1083 uint64_t (*get_count)(CPUARMState *);
1085 * Return how many nanoseconds it will take (at a minimum) for count events
1086 * to occur. A negative value indicates the counter will never overflow, or
1087 * that the counter has otherwise arranged for the overflow bit to be set
1088 * and the PMU interrupt to be raised on overflow.
1090 int64_t (*ns_per_count)(uint64_t);
1091 } pm_event;
1093 static bool event_always_supported(CPUARMState *env)
1095 return true;
1098 static uint64_t swinc_get_count(CPUARMState *env)
1101 * SW_INCR events are written directly to the pmevcntr's by writes to
1102 * PMSWINC, so there is no underlying count maintained by the PMU itself
1104 return 0;
1107 static int64_t swinc_ns_per(uint64_t ignored)
1109 return -1;
1113 * Return the underlying cycle count for the PMU cycle counters. If we're in
1114 * usermode, simply return 0.
1116 static uint64_t cycles_get_count(CPUARMState *env)
1118 #ifndef CONFIG_USER_ONLY
1119 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1120 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1121 #else
1122 return cpu_get_host_ticks();
1123 #endif
1126 #ifndef CONFIG_USER_ONLY
1127 static int64_t cycles_ns_per(uint64_t cycles)
1129 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1132 static bool instructions_supported(CPUARMState *env)
1134 return use_icount == 1 /* Precise instruction counting */;
1137 static uint64_t instructions_get_count(CPUARMState *env)
1139 return (uint64_t)cpu_get_icount_raw();
1142 static int64_t instructions_ns_per(uint64_t icount)
1144 return cpu_icount_to_ns((int64_t)icount);
1146 #endif
1148 static const pm_event pm_events[] = {
1149 { .number = 0x000, /* SW_INCR */
1150 .supported = event_always_supported,
1151 .get_count = swinc_get_count,
1152 .ns_per_count = swinc_ns_per,
1154 #ifndef CONFIG_USER_ONLY
1155 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1156 .supported = instructions_supported,
1157 .get_count = instructions_get_count,
1158 .ns_per_count = instructions_ns_per,
1160 { .number = 0x011, /* CPU_CYCLES, Cycle */
1161 .supported = event_always_supported,
1162 .get_count = cycles_get_count,
1163 .ns_per_count = cycles_ns_per,
1165 #endif
1169 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1170 * events (i.e. the statistical profiling extension), this implementation
1171 * should first be updated to something sparse instead of the current
1172 * supported_event_map[] array.
1174 #define MAX_EVENT_ID 0x11
1175 #define UNSUPPORTED_EVENT UINT16_MAX
1176 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1179 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1180 * of ARM event numbers to indices in our pm_events array.
1182 * Note: Events in the 0x40XX range are not currently supported.
1184 void pmu_init(ARMCPU *cpu)
1186 unsigned int i;
1189 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1190 * events to them
1192 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1193 supported_event_map[i] = UNSUPPORTED_EVENT;
1195 cpu->pmceid0 = 0;
1196 cpu->pmceid1 = 0;
1198 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1199 const pm_event *cnt = &pm_events[i];
1200 assert(cnt->number <= MAX_EVENT_ID);
1201 /* We do not currently support events in the 0x40xx range */
1202 assert(cnt->number <= 0x3f);
1204 if (cnt->supported(&cpu->env)) {
1205 supported_event_map[cnt->number] = i;
1206 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1207 if (cnt->number & 0x20) {
1208 cpu->pmceid1 |= event_mask;
1209 } else {
1210 cpu->pmceid0 |= event_mask;
1217 * Check at runtime whether a PMU event is supported for the current machine
1219 static bool event_supported(uint16_t number)
1221 if (number > MAX_EVENT_ID) {
1222 return false;
1224 return supported_event_map[number] != UNSUPPORTED_EVENT;
1227 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1228 bool isread)
1230 /* Performance monitor registers user accessibility is controlled
1231 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1232 * trapping to EL2 or EL3 for other accesses.
1234 int el = arm_current_el(env);
1236 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1237 return CP_ACCESS_TRAP;
1239 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
1240 && !arm_is_secure_below_el3(env)) {
1241 return CP_ACCESS_TRAP_EL2;
1243 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1244 return CP_ACCESS_TRAP_EL3;
1247 return CP_ACCESS_OK;
1250 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1251 const ARMCPRegInfo *ri,
1252 bool isread)
1254 /* ER: event counter read trap control */
1255 if (arm_feature(env, ARM_FEATURE_V8)
1256 && arm_current_el(env) == 0
1257 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1258 && isread) {
1259 return CP_ACCESS_OK;
1262 return pmreg_access(env, ri, isread);
1265 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1266 const ARMCPRegInfo *ri,
1267 bool isread)
1269 /* SW: software increment write trap control */
1270 if (arm_feature(env, ARM_FEATURE_V8)
1271 && arm_current_el(env) == 0
1272 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1273 && !isread) {
1274 return CP_ACCESS_OK;
1277 return pmreg_access(env, ri, isread);
1280 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1281 const ARMCPRegInfo *ri,
1282 bool isread)
1284 /* ER: event counter read trap control */
1285 if (arm_feature(env, ARM_FEATURE_V8)
1286 && arm_current_el(env) == 0
1287 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1288 return CP_ACCESS_OK;
1291 return pmreg_access(env, ri, isread);
1294 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1295 const ARMCPRegInfo *ri,
1296 bool isread)
1298 /* CR: cycle counter read trap control */
1299 if (arm_feature(env, ARM_FEATURE_V8)
1300 && arm_current_el(env) == 0
1301 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1302 && isread) {
1303 return CP_ACCESS_OK;
1306 return pmreg_access(env, ri, isread);
1309 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1310 * the current EL, security state, and register configuration.
1312 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1314 uint64_t filter;
1315 bool e, p, u, nsk, nsu, nsh, m;
1316 bool enabled, prohibited, filtered;
1317 bool secure = arm_is_secure(env);
1318 int el = arm_current_el(env);
1319 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1321 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1322 return false;
1325 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1326 (counter < hpmn || counter == 31)) {
1327 e = env->cp15.c9_pmcr & PMCRE;
1328 } else {
1329 e = env->cp15.mdcr_el2 & MDCR_HPME;
1331 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1333 if (!secure) {
1334 if (el == 2 && (counter < hpmn || counter == 31)) {
1335 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD;
1336 } else {
1337 prohibited = false;
1339 } else {
1340 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1341 (env->cp15.mdcr_el3 & MDCR_SPME);
1344 if (prohibited && counter == 31) {
1345 prohibited = env->cp15.c9_pmcr & PMCRDP;
1348 if (counter == 31) {
1349 filter = env->cp15.pmccfiltr_el0;
1350 } else {
1351 filter = env->cp15.c14_pmevtyper[counter];
1354 p = filter & PMXEVTYPER_P;
1355 u = filter & PMXEVTYPER_U;
1356 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1357 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1358 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1359 m = arm_el_is_aa64(env, 1) &&
1360 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1362 if (el == 0) {
1363 filtered = secure ? u : u != nsu;
1364 } else if (el == 1) {
1365 filtered = secure ? p : p != nsk;
1366 } else if (el == 2) {
1367 filtered = !nsh;
1368 } else { /* EL3 */
1369 filtered = m != p;
1372 if (counter != 31) {
1374 * If not checking PMCCNTR, ensure the counter is setup to an event we
1375 * support
1377 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1378 if (!event_supported(event)) {
1379 return false;
1383 return enabled && !prohibited && !filtered;
1386 static void pmu_update_irq(CPUARMState *env)
1388 ARMCPU *cpu = env_archcpu(env);
1389 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1390 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1394 * Ensure c15_ccnt is the guest-visible count so that operations such as
1395 * enabling/disabling the counter or filtering, modifying the count itself,
1396 * etc. can be done logically. This is essentially a no-op if the counter is
1397 * not enabled at the time of the call.
1399 static void pmccntr_op_start(CPUARMState *env)
1401 uint64_t cycles = cycles_get_count(env);
1403 if (pmu_counter_enabled(env, 31)) {
1404 uint64_t eff_cycles = cycles;
1405 if (env->cp15.c9_pmcr & PMCRD) {
1406 /* Increment once every 64 processor clock cycles */
1407 eff_cycles /= 64;
1410 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1412 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1413 1ull << 63 : 1ull << 31;
1414 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1415 env->cp15.c9_pmovsr |= (1 << 31);
1416 pmu_update_irq(env);
1419 env->cp15.c15_ccnt = new_pmccntr;
1421 env->cp15.c15_ccnt_delta = cycles;
1425 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1426 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1427 * pmccntr_op_start.
1429 static void pmccntr_op_finish(CPUARMState *env)
1431 if (pmu_counter_enabled(env, 31)) {
1432 #ifndef CONFIG_USER_ONLY
1433 /* Calculate when the counter will next overflow */
1434 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1435 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1436 remaining_cycles = (uint32_t)remaining_cycles;
1438 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1440 if (overflow_in > 0) {
1441 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1442 overflow_in;
1443 ARMCPU *cpu = env_archcpu(env);
1444 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1446 #endif
1448 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1449 if (env->cp15.c9_pmcr & PMCRD) {
1450 /* Increment once every 64 processor clock cycles */
1451 prev_cycles /= 64;
1453 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1457 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1460 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1461 uint64_t count = 0;
1462 if (event_supported(event)) {
1463 uint16_t event_idx = supported_event_map[event];
1464 count = pm_events[event_idx].get_count(env);
1467 if (pmu_counter_enabled(env, counter)) {
1468 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1470 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1471 env->cp15.c9_pmovsr |= (1 << counter);
1472 pmu_update_irq(env);
1474 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1476 env->cp15.c14_pmevcntr_delta[counter] = count;
1479 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1481 if (pmu_counter_enabled(env, counter)) {
1482 #ifndef CONFIG_USER_ONLY
1483 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1484 uint16_t event_idx = supported_event_map[event];
1485 uint64_t delta = UINT32_MAX -
1486 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1487 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1489 if (overflow_in > 0) {
1490 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1491 overflow_in;
1492 ARMCPU *cpu = env_archcpu(env);
1493 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1495 #endif
1497 env->cp15.c14_pmevcntr_delta[counter] -=
1498 env->cp15.c14_pmevcntr[counter];
1502 void pmu_op_start(CPUARMState *env)
1504 unsigned int i;
1505 pmccntr_op_start(env);
1506 for (i = 0; i < pmu_num_counters(env); i++) {
1507 pmevcntr_op_start(env, i);
1511 void pmu_op_finish(CPUARMState *env)
1513 unsigned int i;
1514 pmccntr_op_finish(env);
1515 for (i = 0; i < pmu_num_counters(env); i++) {
1516 pmevcntr_op_finish(env, i);
1520 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1522 pmu_op_start(&cpu->env);
1525 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1527 pmu_op_finish(&cpu->env);
1530 void arm_pmu_timer_cb(void *opaque)
1532 ARMCPU *cpu = opaque;
1535 * Update all the counter values based on the current underlying counts,
1536 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1537 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1538 * counter may expire.
1540 pmu_op_start(&cpu->env);
1541 pmu_op_finish(&cpu->env);
1544 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1545 uint64_t value)
1547 pmu_op_start(env);
1549 if (value & PMCRC) {
1550 /* The counter has been reset */
1551 env->cp15.c15_ccnt = 0;
1554 if (value & PMCRP) {
1555 unsigned int i;
1556 for (i = 0; i < pmu_num_counters(env); i++) {
1557 env->cp15.c14_pmevcntr[i] = 0;
1561 /* only the DP, X, D and E bits are writable */
1562 env->cp15.c9_pmcr &= ~0x39;
1563 env->cp15.c9_pmcr |= (value & 0x39);
1565 pmu_op_finish(env);
1568 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1569 uint64_t value)
1571 unsigned int i;
1572 for (i = 0; i < pmu_num_counters(env); i++) {
1573 /* Increment a counter's count iff: */
1574 if ((value & (1 << i)) && /* counter's bit is set */
1575 /* counter is enabled and not filtered */
1576 pmu_counter_enabled(env, i) &&
1577 /* counter is SW_INCR */
1578 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1579 pmevcntr_op_start(env, i);
1582 * Detect if this write causes an overflow since we can't predict
1583 * PMSWINC overflows like we can for other events
1585 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1587 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1588 env->cp15.c9_pmovsr |= (1 << i);
1589 pmu_update_irq(env);
1592 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1594 pmevcntr_op_finish(env, i);
1599 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1601 uint64_t ret;
1602 pmccntr_op_start(env);
1603 ret = env->cp15.c15_ccnt;
1604 pmccntr_op_finish(env);
1605 return ret;
1608 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1609 uint64_t value)
1611 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1612 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1613 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1614 * accessed.
1616 env->cp15.c9_pmselr = value & 0x1f;
1619 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1620 uint64_t value)
1622 pmccntr_op_start(env);
1623 env->cp15.c15_ccnt = value;
1624 pmccntr_op_finish(env);
1627 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1628 uint64_t value)
1630 uint64_t cur_val = pmccntr_read(env, NULL);
1632 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1635 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636 uint64_t value)
1638 pmccntr_op_start(env);
1639 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1640 pmccntr_op_finish(env);
1643 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1644 uint64_t value)
1646 pmccntr_op_start(env);
1647 /* M is not accessible from AArch32 */
1648 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1649 (value & PMCCFILTR);
1650 pmccntr_op_finish(env);
1653 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1655 /* M is not visible in AArch32 */
1656 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1659 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1660 uint64_t value)
1662 value &= pmu_counter_mask(env);
1663 env->cp15.c9_pmcnten |= value;
1666 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1667 uint64_t value)
1669 value &= pmu_counter_mask(env);
1670 env->cp15.c9_pmcnten &= ~value;
1673 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1674 uint64_t value)
1676 value &= pmu_counter_mask(env);
1677 env->cp15.c9_pmovsr &= ~value;
1678 pmu_update_irq(env);
1681 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1682 uint64_t value)
1684 value &= pmu_counter_mask(env);
1685 env->cp15.c9_pmovsr |= value;
1686 pmu_update_irq(env);
1689 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1690 uint64_t value, const uint8_t counter)
1692 if (counter == 31) {
1693 pmccfiltr_write(env, ri, value);
1694 } else if (counter < pmu_num_counters(env)) {
1695 pmevcntr_op_start(env, counter);
1698 * If this counter's event type is changing, store the current
1699 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1700 * pmevcntr_op_finish has the correct baseline when it converts back to
1701 * a delta.
1703 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1704 PMXEVTYPER_EVTCOUNT;
1705 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1706 if (old_event != new_event) {
1707 uint64_t count = 0;
1708 if (event_supported(new_event)) {
1709 uint16_t event_idx = supported_event_map[new_event];
1710 count = pm_events[event_idx].get_count(env);
1712 env->cp15.c14_pmevcntr_delta[counter] = count;
1715 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1716 pmevcntr_op_finish(env, counter);
1718 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1719 * PMSELR value is equal to or greater than the number of implemented
1720 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1724 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1725 const uint8_t counter)
1727 if (counter == 31) {
1728 return env->cp15.pmccfiltr_el0;
1729 } else if (counter < pmu_num_counters(env)) {
1730 return env->cp15.c14_pmevtyper[counter];
1731 } else {
1733 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1734 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1736 return 0;
1740 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1741 uint64_t value)
1743 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1744 pmevtyper_write(env, ri, value, counter);
1747 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1748 uint64_t value)
1750 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1751 env->cp15.c14_pmevtyper[counter] = value;
1754 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1755 * pmu_op_finish calls when loading saved state for a migration. Because
1756 * we're potentially updating the type of event here, the value written to
1757 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1758 * different counter type. Therefore, we need to set this value to the
1759 * current count for the counter type we're writing so that pmu_op_finish
1760 * has the correct count for its calculation.
1762 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1763 if (event_supported(event)) {
1764 uint16_t event_idx = supported_event_map[event];
1765 env->cp15.c14_pmevcntr_delta[counter] =
1766 pm_events[event_idx].get_count(env);
1770 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1772 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1773 return pmevtyper_read(env, ri, counter);
1776 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1777 uint64_t value)
1779 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1782 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1784 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1787 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1788 uint64_t value, uint8_t counter)
1790 if (counter < pmu_num_counters(env)) {
1791 pmevcntr_op_start(env, counter);
1792 env->cp15.c14_pmevcntr[counter] = value;
1793 pmevcntr_op_finish(env, counter);
1796 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1797 * are CONSTRAINED UNPREDICTABLE.
1801 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1802 uint8_t counter)
1804 if (counter < pmu_num_counters(env)) {
1805 uint64_t ret;
1806 pmevcntr_op_start(env, counter);
1807 ret = env->cp15.c14_pmevcntr[counter];
1808 pmevcntr_op_finish(env, counter);
1809 return ret;
1810 } else {
1811 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1812 * are CONSTRAINED UNPREDICTABLE. */
1813 return 0;
1817 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1818 uint64_t value)
1820 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1821 pmevcntr_write(env, ri, value, counter);
1824 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1826 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1827 return pmevcntr_read(env, ri, counter);
1830 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1831 uint64_t value)
1833 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1834 assert(counter < pmu_num_counters(env));
1835 env->cp15.c14_pmevcntr[counter] = value;
1836 pmevcntr_write(env, ri, value, counter);
1839 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1841 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1842 assert(counter < pmu_num_counters(env));
1843 return env->cp15.c14_pmevcntr[counter];
1846 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1847 uint64_t value)
1849 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1852 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1854 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1857 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1858 uint64_t value)
1860 if (arm_feature(env, ARM_FEATURE_V8)) {
1861 env->cp15.c9_pmuserenr = value & 0xf;
1862 } else {
1863 env->cp15.c9_pmuserenr = value & 1;
1867 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1868 uint64_t value)
1870 /* We have no event counters so only the C bit can be changed */
1871 value &= pmu_counter_mask(env);
1872 env->cp15.c9_pminten |= value;
1873 pmu_update_irq(env);
1876 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1877 uint64_t value)
1879 value &= pmu_counter_mask(env);
1880 env->cp15.c9_pminten &= ~value;
1881 pmu_update_irq(env);
1884 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1885 uint64_t value)
1887 /* Note that even though the AArch64 view of this register has bits
1888 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1889 * architectural requirements for bits which are RES0 only in some
1890 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1891 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1893 raw_write(env, ri, value & ~0x1FULL);
1896 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1898 /* Begin with base v8.0 state. */
1899 uint32_t valid_mask = 0x3fff;
1900 ARMCPU *cpu = env_archcpu(env);
1902 if (arm_el_is_aa64(env, 3)) {
1903 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
1904 valid_mask &= ~SCR_NET;
1905 } else {
1906 valid_mask &= ~(SCR_RW | SCR_ST);
1909 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1910 valid_mask &= ~SCR_HCE;
1912 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1913 * supported if EL2 exists. The bit is UNK/SBZP when
1914 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1915 * when EL2 is unavailable.
1916 * On ARMv8, this bit is always available.
1918 if (arm_feature(env, ARM_FEATURE_V7) &&
1919 !arm_feature(env, ARM_FEATURE_V8)) {
1920 valid_mask &= ~SCR_SMD;
1923 if (cpu_isar_feature(aa64_lor, cpu)) {
1924 valid_mask |= SCR_TLOR;
1926 if (cpu_isar_feature(aa64_pauth, cpu)) {
1927 valid_mask |= SCR_API | SCR_APK;
1930 /* Clear all-context RES0 bits. */
1931 value &= valid_mask;
1932 raw_write(env, ri, value);
1935 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1937 ARMCPU *cpu = env_archcpu(env);
1939 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1940 * bank
1942 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1943 ri->secure & ARM_CP_SECSTATE_S);
1945 return cpu->ccsidr[index];
1948 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1949 uint64_t value)
1951 raw_write(env, ri, value & 0xf);
1954 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1956 CPUState *cs = env_cpu(env);
1957 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1958 uint64_t ret = 0;
1960 if (hcr_el2 & HCR_IMO) {
1961 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1962 ret |= CPSR_I;
1964 } else {
1965 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1966 ret |= CPSR_I;
1970 if (hcr_el2 & HCR_FMO) {
1971 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1972 ret |= CPSR_F;
1974 } else {
1975 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1976 ret |= CPSR_F;
1980 /* External aborts are not possible in QEMU so A bit is always clear */
1981 return ret;
1984 static const ARMCPRegInfo v7_cp_reginfo[] = {
1985 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1986 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1987 .access = PL1_W, .type = ARM_CP_NOP },
1988 /* Performance monitors are implementation defined in v7,
1989 * but with an ARM recommended set of registers, which we
1990 * follow.
1992 * Performance registers fall into three categories:
1993 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1994 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1995 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1996 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1997 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1999 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2000 .access = PL0_RW, .type = ARM_CP_ALIAS,
2001 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2002 .writefn = pmcntenset_write,
2003 .accessfn = pmreg_access,
2004 .raw_writefn = raw_write },
2005 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
2006 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2007 .access = PL0_RW, .accessfn = pmreg_access,
2008 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2009 .writefn = pmcntenset_write, .raw_writefn = raw_write },
2010 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2011 .access = PL0_RW,
2012 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2013 .accessfn = pmreg_access,
2014 .writefn = pmcntenclr_write,
2015 .type = ARM_CP_ALIAS },
2016 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2017 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2018 .access = PL0_RW, .accessfn = pmreg_access,
2019 .type = ARM_CP_ALIAS,
2020 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2021 .writefn = pmcntenclr_write },
2022 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2023 .access = PL0_RW, .type = ARM_CP_IO,
2024 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2025 .accessfn = pmreg_access,
2026 .writefn = pmovsr_write,
2027 .raw_writefn = raw_write },
2028 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2029 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2030 .access = PL0_RW, .accessfn = pmreg_access,
2031 .type = ARM_CP_ALIAS | ARM_CP_IO,
2032 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2033 .writefn = pmovsr_write,
2034 .raw_writefn = raw_write },
2035 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2036 .access = PL0_W, .accessfn = pmreg_access_swinc,
2037 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2038 .writefn = pmswinc_write },
2039 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2040 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2041 .access = PL0_W, .accessfn = pmreg_access_swinc,
2042 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2043 .writefn = pmswinc_write },
2044 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2045 .access = PL0_RW, .type = ARM_CP_ALIAS,
2046 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2047 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2048 .raw_writefn = raw_write},
2049 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2050 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2051 .access = PL0_RW, .accessfn = pmreg_access_selr,
2052 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2053 .writefn = pmselr_write, .raw_writefn = raw_write, },
2054 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2055 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2056 .readfn = pmccntr_read, .writefn = pmccntr_write32,
2057 .accessfn = pmreg_access_ccntr },
2058 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2059 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2060 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2061 .type = ARM_CP_IO,
2062 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2063 .readfn = pmccntr_read, .writefn = pmccntr_write,
2064 .raw_readfn = raw_read, .raw_writefn = raw_write, },
2065 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2066 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2067 .access = PL0_RW, .accessfn = pmreg_access,
2068 .type = ARM_CP_ALIAS | ARM_CP_IO,
2069 .resetvalue = 0, },
2070 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2071 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2072 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2073 .access = PL0_RW, .accessfn = pmreg_access,
2074 .type = ARM_CP_IO,
2075 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2076 .resetvalue = 0, },
2077 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2078 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2079 .accessfn = pmreg_access,
2080 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2081 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2082 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2083 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2084 .accessfn = pmreg_access,
2085 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2086 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2087 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2088 .accessfn = pmreg_access_xevcntr,
2089 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2090 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2091 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2092 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2093 .accessfn = pmreg_access_xevcntr,
2094 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2095 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2096 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2097 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2098 .resetvalue = 0,
2099 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2100 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2101 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2102 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2103 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2104 .resetvalue = 0,
2105 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2106 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2107 .access = PL1_RW, .accessfn = access_tpm,
2108 .type = ARM_CP_ALIAS | ARM_CP_IO,
2109 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2110 .resetvalue = 0,
2111 .writefn = pmintenset_write, .raw_writefn = raw_write },
2112 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2113 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2114 .access = PL1_RW, .accessfn = access_tpm,
2115 .type = ARM_CP_IO,
2116 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2117 .writefn = pmintenset_write, .raw_writefn = raw_write,
2118 .resetvalue = 0x0 },
2119 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2120 .access = PL1_RW, .accessfn = access_tpm,
2121 .type = ARM_CP_ALIAS | ARM_CP_IO,
2122 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2123 .writefn = pmintenclr_write, },
2124 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2125 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2126 .access = PL1_RW, .accessfn = access_tpm,
2127 .type = ARM_CP_ALIAS | ARM_CP_IO,
2128 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2129 .writefn = pmintenclr_write },
2130 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2131 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2132 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2133 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2134 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2135 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
2136 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2137 offsetof(CPUARMState, cp15.csselr_ns) } },
2138 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2139 * just RAZ for all cores:
2141 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2142 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2143 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2144 /* Auxiliary fault status registers: these also are IMPDEF, and we
2145 * choose to RAZ/WI for all cores.
2147 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2148 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2149 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2150 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2151 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2152 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2153 /* MAIR can just read-as-written because we don't implement caches
2154 * and so don't need to care about memory attributes.
2156 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2157 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2158 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2159 .resetvalue = 0 },
2160 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2161 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2162 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2163 .resetvalue = 0 },
2164 /* For non-long-descriptor page tables these are PRRR and NMRR;
2165 * regardless they still act as reads-as-written for QEMU.
2167 /* MAIR0/1 are defined separately from their 64-bit counterpart which
2168 * allows them to assign the correct fieldoffset based on the endianness
2169 * handled in the field definitions.
2171 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2172 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
2173 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2174 offsetof(CPUARMState, cp15.mair0_ns) },
2175 .resetfn = arm_cp_reset_ignore },
2176 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2177 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
2178 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2179 offsetof(CPUARMState, cp15.mair1_ns) },
2180 .resetfn = arm_cp_reset_ignore },
2181 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2182 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2183 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2184 /* 32 bit ITLB invalidates */
2185 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2186 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2187 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2188 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2189 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2190 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2191 /* 32 bit DTLB invalidates */
2192 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2193 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2194 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2195 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2196 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2197 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2198 /* 32 bit TLB invalidates */
2199 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2200 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2201 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2202 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2203 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2204 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2205 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2206 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
2207 REGINFO_SENTINEL
2210 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2211 /* 32 bit TLB invalidates, Inner Shareable */
2212 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2213 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
2214 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2215 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
2216 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2217 .type = ARM_CP_NO_RAW, .access = PL1_W,
2218 .writefn = tlbiasid_is_write },
2219 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2220 .type = ARM_CP_NO_RAW, .access = PL1_W,
2221 .writefn = tlbimvaa_is_write },
2222 REGINFO_SENTINEL
2225 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2226 /* PMOVSSET is not implemented in v7 before v7ve */
2227 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2228 .access = PL0_RW, .accessfn = pmreg_access,
2229 .type = ARM_CP_ALIAS | ARM_CP_IO,
2230 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2231 .writefn = pmovsset_write,
2232 .raw_writefn = raw_write },
2233 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2234 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2235 .access = PL0_RW, .accessfn = pmreg_access,
2236 .type = ARM_CP_ALIAS | ARM_CP_IO,
2237 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2238 .writefn = pmovsset_write,
2239 .raw_writefn = raw_write },
2240 REGINFO_SENTINEL
2243 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2244 uint64_t value)
2246 value &= 1;
2247 env->teecr = value;
2250 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2251 bool isread)
2253 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2254 return CP_ACCESS_TRAP;
2256 return CP_ACCESS_OK;
2259 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2260 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2261 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2262 .resetvalue = 0,
2263 .writefn = teecr_write },
2264 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2265 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2266 .accessfn = teehbr_access, .resetvalue = 0 },
2267 REGINFO_SENTINEL
2270 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2271 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2272 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2273 .access = PL0_RW,
2274 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2275 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2276 .access = PL0_RW,
2277 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2278 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2279 .resetfn = arm_cp_reset_ignore },
2280 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2281 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2282 .access = PL0_R|PL1_W,
2283 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2284 .resetvalue = 0},
2285 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2286 .access = PL0_R|PL1_W,
2287 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2288 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2289 .resetfn = arm_cp_reset_ignore },
2290 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2291 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2292 .access = PL1_RW,
2293 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2294 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2295 .access = PL1_RW,
2296 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2297 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2298 .resetvalue = 0 },
2299 REGINFO_SENTINEL
2302 #ifndef CONFIG_USER_ONLY
2304 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2305 bool isread)
2307 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2308 * Writable only at the highest implemented exception level.
2310 int el = arm_current_el(env);
2312 switch (el) {
2313 case 0:
2314 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
2315 return CP_ACCESS_TRAP;
2317 break;
2318 case 1:
2319 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2320 arm_is_secure_below_el3(env)) {
2321 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2322 return CP_ACCESS_TRAP_UNCATEGORIZED;
2324 break;
2325 case 2:
2326 case 3:
2327 break;
2330 if (!isread && el < arm_highest_el(env)) {
2331 return CP_ACCESS_TRAP_UNCATEGORIZED;
2334 return CP_ACCESS_OK;
2337 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2338 bool isread)
2340 unsigned int cur_el = arm_current_el(env);
2341 bool secure = arm_is_secure(env);
2343 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
2344 if (cur_el == 0 &&
2345 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2346 return CP_ACCESS_TRAP;
2349 if (arm_feature(env, ARM_FEATURE_EL2) &&
2350 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
2351 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
2352 return CP_ACCESS_TRAP_EL2;
2354 return CP_ACCESS_OK;
2357 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2358 bool isread)
2360 unsigned int cur_el = arm_current_el(env);
2361 bool secure = arm_is_secure(env);
2363 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
2364 * EL0[PV]TEN is zero.
2366 if (cur_el == 0 &&
2367 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2368 return CP_ACCESS_TRAP;
2371 if (arm_feature(env, ARM_FEATURE_EL2) &&
2372 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
2373 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2374 return CP_ACCESS_TRAP_EL2;
2376 return CP_ACCESS_OK;
2379 static CPAccessResult gt_pct_access(CPUARMState *env,
2380 const ARMCPRegInfo *ri,
2381 bool isread)
2383 return gt_counter_access(env, GTIMER_PHYS, isread);
2386 static CPAccessResult gt_vct_access(CPUARMState *env,
2387 const ARMCPRegInfo *ri,
2388 bool isread)
2390 return gt_counter_access(env, GTIMER_VIRT, isread);
2393 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2394 bool isread)
2396 return gt_timer_access(env, GTIMER_PHYS, isread);
2399 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2400 bool isread)
2402 return gt_timer_access(env, GTIMER_VIRT, isread);
2405 static CPAccessResult gt_stimer_access(CPUARMState *env,
2406 const ARMCPRegInfo *ri,
2407 bool isread)
2409 /* The AArch64 register view of the secure physical timer is
2410 * always accessible from EL3, and configurably accessible from
2411 * Secure EL1.
2413 switch (arm_current_el(env)) {
2414 case 1:
2415 if (!arm_is_secure(env)) {
2416 return CP_ACCESS_TRAP;
2418 if (!(env->cp15.scr_el3 & SCR_ST)) {
2419 return CP_ACCESS_TRAP_EL3;
2421 return CP_ACCESS_OK;
2422 case 0:
2423 case 2:
2424 return CP_ACCESS_TRAP;
2425 case 3:
2426 return CP_ACCESS_OK;
2427 default:
2428 g_assert_not_reached();
2432 static uint64_t gt_get_countervalue(CPUARMState *env)
2434 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
2437 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2439 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2441 if (gt->ctl & 1) {
2442 /* Timer enabled: calculate and set current ISTATUS, irq, and
2443 * reset timer to when ISTATUS next has to change
2445 uint64_t offset = timeridx == GTIMER_VIRT ?
2446 cpu->env.cp15.cntvoff_el2 : 0;
2447 uint64_t count = gt_get_countervalue(&cpu->env);
2448 /* Note that this must be unsigned 64 bit arithmetic: */
2449 int istatus = count - offset >= gt->cval;
2450 uint64_t nexttick;
2451 int irqstate;
2453 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2455 irqstate = (istatus && !(gt->ctl & 2));
2456 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2458 if (istatus) {
2459 /* Next transition is when count rolls back over to zero */
2460 nexttick = UINT64_MAX;
2461 } else {
2462 /* Next transition is when we hit cval */
2463 nexttick = gt->cval + offset;
2465 /* Note that the desired next expiry time might be beyond the
2466 * signed-64-bit range of a QEMUTimer -- in this case we just
2467 * set the timer for as far in the future as possible. When the
2468 * timer expires we will reset the timer for any remaining period.
2470 if (nexttick > INT64_MAX / GTIMER_SCALE) {
2471 nexttick = INT64_MAX / GTIMER_SCALE;
2473 timer_mod(cpu->gt_timer[timeridx], nexttick);
2474 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2475 } else {
2476 /* Timer disabled: ISTATUS and timer output always clear */
2477 gt->ctl &= ~4;
2478 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2479 timer_del(cpu->gt_timer[timeridx]);
2480 trace_arm_gt_recalc_disabled(timeridx);
2484 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2485 int timeridx)
2487 ARMCPU *cpu = env_archcpu(env);
2489 timer_del(cpu->gt_timer[timeridx]);
2492 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2494 return gt_get_countervalue(env);
2497 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2499 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
2502 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2503 int timeridx,
2504 uint64_t value)
2506 trace_arm_gt_cval_write(timeridx, value);
2507 env->cp15.c14_timer[timeridx].cval = value;
2508 gt_recalc_timer(env_archcpu(env), timeridx);
2511 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2512 int timeridx)
2514 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
2516 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2517 (gt_get_countervalue(env) - offset));
2520 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2521 int timeridx,
2522 uint64_t value)
2524 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
2526 trace_arm_gt_tval_write(timeridx, value);
2527 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2528 sextract64(value, 0, 32);
2529 gt_recalc_timer(env_archcpu(env), timeridx);
2532 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2533 int timeridx,
2534 uint64_t value)
2536 ARMCPU *cpu = env_archcpu(env);
2537 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2539 trace_arm_gt_ctl_write(timeridx, value);
2540 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2541 if ((oldval ^ value) & 1) {
2542 /* Enable toggled */
2543 gt_recalc_timer(cpu, timeridx);
2544 } else if ((oldval ^ value) & 2) {
2545 /* IMASK toggled: don't need to recalculate,
2546 * just set the interrupt line based on ISTATUS
2548 int irqstate = (oldval & 4) && !(value & 2);
2550 trace_arm_gt_imask_toggle(timeridx, irqstate);
2551 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2555 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2557 gt_timer_reset(env, ri, GTIMER_PHYS);
2560 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2561 uint64_t value)
2563 gt_cval_write(env, ri, GTIMER_PHYS, value);
2566 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2568 return gt_tval_read(env, ri, GTIMER_PHYS);
2571 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2572 uint64_t value)
2574 gt_tval_write(env, ri, GTIMER_PHYS, value);
2577 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2578 uint64_t value)
2580 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2583 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2585 gt_timer_reset(env, ri, GTIMER_VIRT);
2588 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2589 uint64_t value)
2591 gt_cval_write(env, ri, GTIMER_VIRT, value);
2594 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2596 return gt_tval_read(env, ri, GTIMER_VIRT);
2599 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2600 uint64_t value)
2602 gt_tval_write(env, ri, GTIMER_VIRT, value);
2605 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2606 uint64_t value)
2608 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2611 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2612 uint64_t value)
2614 ARMCPU *cpu = env_archcpu(env);
2616 trace_arm_gt_cntvoff_write(value);
2617 raw_write(env, ri, value);
2618 gt_recalc_timer(cpu, GTIMER_VIRT);
2621 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2623 gt_timer_reset(env, ri, GTIMER_HYP);
2626 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2627 uint64_t value)
2629 gt_cval_write(env, ri, GTIMER_HYP, value);
2632 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2634 return gt_tval_read(env, ri, GTIMER_HYP);
2637 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2638 uint64_t value)
2640 gt_tval_write(env, ri, GTIMER_HYP, value);
2643 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2644 uint64_t value)
2646 gt_ctl_write(env, ri, GTIMER_HYP, value);
2649 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2651 gt_timer_reset(env, ri, GTIMER_SEC);
2654 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2655 uint64_t value)
2657 gt_cval_write(env, ri, GTIMER_SEC, value);
2660 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2662 return gt_tval_read(env, ri, GTIMER_SEC);
2665 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2666 uint64_t value)
2668 gt_tval_write(env, ri, GTIMER_SEC, value);
2671 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2672 uint64_t value)
2674 gt_ctl_write(env, ri, GTIMER_SEC, value);
2677 void arm_gt_ptimer_cb(void *opaque)
2679 ARMCPU *cpu = opaque;
2681 gt_recalc_timer(cpu, GTIMER_PHYS);
2684 void arm_gt_vtimer_cb(void *opaque)
2686 ARMCPU *cpu = opaque;
2688 gt_recalc_timer(cpu, GTIMER_VIRT);
2691 void arm_gt_htimer_cb(void *opaque)
2693 ARMCPU *cpu = opaque;
2695 gt_recalc_timer(cpu, GTIMER_HYP);
2698 void arm_gt_stimer_cb(void *opaque)
2700 ARMCPU *cpu = opaque;
2702 gt_recalc_timer(cpu, GTIMER_SEC);
2705 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2706 /* Note that CNTFRQ is purely reads-as-written for the benefit
2707 * of software; writing it doesn't actually change the timer frequency.
2708 * Our reset value matches the fixed frequency we implement the timer at.
2710 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2711 .type = ARM_CP_ALIAS,
2712 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2713 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2715 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2716 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2717 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2718 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2719 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
2721 /* overall control: mostly access permissions */
2722 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2723 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2724 .access = PL1_RW,
2725 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2726 .resetvalue = 0,
2728 /* per-timer control */
2729 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2730 .secure = ARM_CP_SECSTATE_NS,
2731 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2732 .accessfn = gt_ptimer_access,
2733 .fieldoffset = offsetoflow32(CPUARMState,
2734 cp15.c14_timer[GTIMER_PHYS].ctl),
2735 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
2737 { .name = "CNTP_CTL_S",
2738 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2739 .secure = ARM_CP_SECSTATE_S,
2740 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2741 .accessfn = gt_ptimer_access,
2742 .fieldoffset = offsetoflow32(CPUARMState,
2743 cp15.c14_timer[GTIMER_SEC].ctl),
2744 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2746 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2747 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2748 .type = ARM_CP_IO, .access = PL0_RW,
2749 .accessfn = gt_ptimer_access,
2750 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2751 .resetvalue = 0,
2752 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
2754 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2755 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2756 .accessfn = gt_vtimer_access,
2757 .fieldoffset = offsetoflow32(CPUARMState,
2758 cp15.c14_timer[GTIMER_VIRT].ctl),
2759 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
2761 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2762 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2763 .type = ARM_CP_IO, .access = PL0_RW,
2764 .accessfn = gt_vtimer_access,
2765 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2766 .resetvalue = 0,
2767 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
2769 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2770 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2771 .secure = ARM_CP_SECSTATE_NS,
2772 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2773 .accessfn = gt_ptimer_access,
2774 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
2776 { .name = "CNTP_TVAL_S",
2777 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2778 .secure = ARM_CP_SECSTATE_S,
2779 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2780 .accessfn = gt_ptimer_access,
2781 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2783 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2784 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2785 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2786 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2787 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
2789 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2790 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2791 .accessfn = gt_vtimer_access,
2792 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2794 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2795 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2796 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2797 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2798 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2800 /* The counter itself */
2801 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
2802 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2803 .accessfn = gt_pct_access,
2804 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2806 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2807 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
2808 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2809 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
2811 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
2812 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2813 .accessfn = gt_vct_access,
2814 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
2816 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2817 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2818 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2819 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
2821 /* Comparison value, indicating when the timer goes off */
2822 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
2823 .secure = ARM_CP_SECSTATE_NS,
2824 .access = PL0_RW,
2825 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2826 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2827 .accessfn = gt_ptimer_access,
2828 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2830 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
2831 .secure = ARM_CP_SECSTATE_S,
2832 .access = PL0_RW,
2833 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2834 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2835 .accessfn = gt_ptimer_access,
2836 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2838 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2839 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2840 .access = PL0_RW,
2841 .type = ARM_CP_IO,
2842 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2843 .resetvalue = 0, .accessfn = gt_ptimer_access,
2844 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2846 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2847 .access = PL0_RW,
2848 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2849 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2850 .accessfn = gt_vtimer_access,
2851 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2853 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2854 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2855 .access = PL0_RW,
2856 .type = ARM_CP_IO,
2857 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2858 .resetvalue = 0, .accessfn = gt_vtimer_access,
2859 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2861 /* Secure timer -- this is actually restricted to only EL3
2862 * and configurably Secure-EL1 via the accessfn.
2864 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2865 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2866 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2867 .accessfn = gt_stimer_access,
2868 .readfn = gt_sec_tval_read,
2869 .writefn = gt_sec_tval_write,
2870 .resetfn = gt_sec_timer_reset,
2872 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2873 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2874 .type = ARM_CP_IO, .access = PL1_RW,
2875 .accessfn = gt_stimer_access,
2876 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2877 .resetvalue = 0,
2878 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2880 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2881 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2882 .type = ARM_CP_IO, .access = PL1_RW,
2883 .accessfn = gt_stimer_access,
2884 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2885 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2887 REGINFO_SENTINEL
2890 #else
2892 /* In user-mode most of the generic timer registers are inaccessible
2893 * however modern kernels (4.12+) allow access to cntvct_el0
2896 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2898 /* Currently we have no support for QEMUTimer in linux-user so we
2899 * can't call gt_get_countervalue(env), instead we directly
2900 * call the lower level functions.
2902 return cpu_get_clock() / GTIMER_SCALE;
2905 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2906 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2907 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2908 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
2909 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2910 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
2912 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2913 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2914 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2915 .readfn = gt_virt_cnt_read,
2917 REGINFO_SENTINEL
2920 #endif
2922 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2924 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2925 raw_write(env, ri, value);
2926 } else if (arm_feature(env, ARM_FEATURE_V7)) {
2927 raw_write(env, ri, value & 0xfffff6ff);
2928 } else {
2929 raw_write(env, ri, value & 0xfffff1ff);
2933 #ifndef CONFIG_USER_ONLY
2934 /* get_phys_addr() isn't present for user-mode-only targets */
2936 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2937 bool isread)
2939 if (ri->opc2 & 4) {
2940 /* The ATS12NSO* operations must trap to EL3 if executed in
2941 * Secure EL1 (which can only happen if EL3 is AArch64).
2942 * They are simply UNDEF if executed from NS EL1.
2943 * They function normally from EL2 or EL3.
2945 if (arm_current_el(env) == 1) {
2946 if (arm_is_secure_below_el3(env)) {
2947 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2949 return CP_ACCESS_TRAP_UNCATEGORIZED;
2952 return CP_ACCESS_OK;
2955 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
2956 MMUAccessType access_type, ARMMMUIdx mmu_idx)
2958 hwaddr phys_addr;
2959 target_ulong page_size;
2960 int prot;
2961 bool ret;
2962 uint64_t par64;
2963 bool format64 = false;
2964 MemTxAttrs attrs = {};
2965 ARMMMUFaultInfo fi = {};
2966 ARMCacheAttrs cacheattrs = {};
2968 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
2969 &prot, &page_size, &fi, &cacheattrs);
2971 if (is_a64(env)) {
2972 format64 = true;
2973 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
2975 * ATS1Cxx:
2976 * * TTBCR.EAE determines whether the result is returned using the
2977 * 32-bit or the 64-bit PAR format
2978 * * Instructions executed in Hyp mode always use the 64bit format
2980 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
2981 * * The Non-secure TTBCR.EAE bit is set to 1
2982 * * The implementation includes EL2, and the value of HCR.VM is 1
2984 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
2986 * ATS1Hx always uses the 64bit format.
2988 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
2990 if (arm_feature(env, ARM_FEATURE_EL2)) {
2991 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
2992 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
2993 } else {
2994 format64 |= arm_current_el(env) == 2;
2999 if (format64) {
3000 /* Create a 64-bit PAR */
3001 par64 = (1 << 11); /* LPAE bit always set */
3002 if (!ret) {
3003 par64 |= phys_addr & ~0xfffULL;
3004 if (!attrs.secure) {
3005 par64 |= (1 << 9); /* NS */
3007 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3008 par64 |= cacheattrs.shareability << 7; /* SH */
3009 } else {
3010 uint32_t fsr = arm_fi_to_lfsc(&fi);
3012 par64 |= 1; /* F */
3013 par64 |= (fsr & 0x3f) << 1; /* FS */
3014 if (fi.stage2) {
3015 par64 |= (1 << 9); /* S */
3017 if (fi.s1ptw) {
3018 par64 |= (1 << 8); /* PTW */
3021 } else {
3022 /* fsr is a DFSR/IFSR value for the short descriptor
3023 * translation table format (with WnR always clear).
3024 * Convert it to a 32-bit PAR.
3026 if (!ret) {
3027 /* We do not set any attribute bits in the PAR */
3028 if (page_size == (1 << 24)
3029 && arm_feature(env, ARM_FEATURE_V7)) {
3030 par64 = (phys_addr & 0xff000000) | (1 << 1);
3031 } else {
3032 par64 = phys_addr & 0xfffff000;
3034 if (!attrs.secure) {
3035 par64 |= (1 << 9); /* NS */
3037 } else {
3038 uint32_t fsr = arm_fi_to_sfsc(&fi);
3040 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3041 ((fsr & 0xf) << 1) | 1;
3044 return par64;
3047 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3049 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3050 uint64_t par64;
3051 ARMMMUIdx mmu_idx;
3052 int el = arm_current_el(env);
3053 bool secure = arm_is_secure_below_el3(env);
3055 switch (ri->opc2 & 6) {
3056 case 0:
3057 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
3058 switch (el) {
3059 case 3:
3060 mmu_idx = ARMMMUIdx_S1E3;
3061 break;
3062 case 2:
3063 mmu_idx = ARMMMUIdx_S1NSE1;
3064 break;
3065 case 1:
3066 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
3067 break;
3068 default:
3069 g_assert_not_reached();
3071 break;
3072 case 2:
3073 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3074 switch (el) {
3075 case 3:
3076 mmu_idx = ARMMMUIdx_S1SE0;
3077 break;
3078 case 2:
3079 mmu_idx = ARMMMUIdx_S1NSE0;
3080 break;
3081 case 1:
3082 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
3083 break;
3084 default:
3085 g_assert_not_reached();
3087 break;
3088 case 4:
3089 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3090 mmu_idx = ARMMMUIdx_S12NSE1;
3091 break;
3092 case 6:
3093 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3094 mmu_idx = ARMMMUIdx_S12NSE0;
3095 break;
3096 default:
3097 g_assert_not_reached();
3100 par64 = do_ats_write(env, value, access_type, mmu_idx);
3102 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3105 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3106 uint64_t value)
3108 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3109 uint64_t par64;
3111 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S1E2);
3113 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3116 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3117 bool isread)
3119 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3120 return CP_ACCESS_TRAP;
3122 return CP_ACCESS_OK;
3125 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3126 uint64_t value)
3128 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3129 ARMMMUIdx mmu_idx;
3130 int secure = arm_is_secure_below_el3(env);
3132 switch (ri->opc2 & 6) {
3133 case 0:
3134 switch (ri->opc1) {
3135 case 0: /* AT S1E1R, AT S1E1W */
3136 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
3137 break;
3138 case 4: /* AT S1E2R, AT S1E2W */
3139 mmu_idx = ARMMMUIdx_S1E2;
3140 break;
3141 case 6: /* AT S1E3R, AT S1E3W */
3142 mmu_idx = ARMMMUIdx_S1E3;
3143 break;
3144 default:
3145 g_assert_not_reached();
3147 break;
3148 case 2: /* AT S1E0R, AT S1E0W */
3149 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
3150 break;
3151 case 4: /* AT S12E1R, AT S12E1W */
3152 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
3153 break;
3154 case 6: /* AT S12E0R, AT S12E0W */
3155 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
3156 break;
3157 default:
3158 g_assert_not_reached();
3161 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3163 #endif
3165 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3166 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3167 .access = PL1_RW, .resetvalue = 0,
3168 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3169 offsetoflow32(CPUARMState, cp15.par_ns) },
3170 .writefn = par_write },
3171 #ifndef CONFIG_USER_ONLY
3172 /* This underdecoding is safe because the reginfo is NO_RAW. */
3173 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3174 .access = PL1_W, .accessfn = ats_access,
3175 .writefn = ats_write, .type = ARM_CP_NO_RAW },
3176 #endif
3177 REGINFO_SENTINEL
3180 /* Return basic MPU access permission bits. */
3181 static uint32_t simple_mpu_ap_bits(uint32_t val)
3183 uint32_t ret;
3184 uint32_t mask;
3185 int i;
3186 ret = 0;
3187 mask = 3;
3188 for (i = 0; i < 16; i += 2) {
3189 ret |= (val >> i) & mask;
3190 mask <<= 2;
3192 return ret;
3195 /* Pad basic MPU access permission bits to extended format. */
3196 static uint32_t extended_mpu_ap_bits(uint32_t val)
3198 uint32_t ret;
3199 uint32_t mask;
3200 int i;
3201 ret = 0;
3202 mask = 3;
3203 for (i = 0; i < 16; i += 2) {
3204 ret |= (val & mask) << i;
3205 mask <<= 2;
3207 return ret;
3210 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3211 uint64_t value)
3213 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3216 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3218 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3221 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3222 uint64_t value)
3224 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3227 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3229 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3232 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3234 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3236 if (!u32p) {
3237 return 0;
3240 u32p += env->pmsav7.rnr[M_REG_NS];
3241 return *u32p;
3244 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3245 uint64_t value)
3247 ARMCPU *cpu = env_archcpu(env);
3248 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3250 if (!u32p) {
3251 return;
3254 u32p += env->pmsav7.rnr[M_REG_NS];
3255 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3256 *u32p = value;
3259 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3260 uint64_t value)
3262 ARMCPU *cpu = env_archcpu(env);
3263 uint32_t nrgs = cpu->pmsav7_dregion;
3265 if (value >= nrgs) {
3266 qemu_log_mask(LOG_GUEST_ERROR,
3267 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3268 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3269 return;
3272 raw_write(env, ri, value);
3275 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3276 /* Reset for all these registers is handled in arm_cpu_reset(),
3277 * because the PMSAv7 is also used by M-profile CPUs, which do
3278 * not register cpregs but still need the state to be reset.
3280 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3281 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3282 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3283 .readfn = pmsav7_read, .writefn = pmsav7_write,
3284 .resetfn = arm_cp_reset_ignore },
3285 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3286 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3287 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3288 .readfn = pmsav7_read, .writefn = pmsav7_write,
3289 .resetfn = arm_cp_reset_ignore },
3290 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3291 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3292 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3293 .readfn = pmsav7_read, .writefn = pmsav7_write,
3294 .resetfn = arm_cp_reset_ignore },
3295 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3296 .access = PL1_RW,
3297 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3298 .writefn = pmsav7_rgnr_write,
3299 .resetfn = arm_cp_reset_ignore },
3300 REGINFO_SENTINEL
3303 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3304 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3305 .access = PL1_RW, .type = ARM_CP_ALIAS,
3306 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3307 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3308 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3309 .access = PL1_RW, .type = ARM_CP_ALIAS,
3310 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3311 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3312 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3313 .access = PL1_RW,
3314 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3315 .resetvalue = 0, },
3316 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3317 .access = PL1_RW,
3318 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3319 .resetvalue = 0, },
3320 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3321 .access = PL1_RW,
3322 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3323 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3324 .access = PL1_RW,
3325 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3326 /* Protection region base and size registers */
3327 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3328 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3329 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3330 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3331 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3332 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3333 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3334 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3335 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3336 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3337 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3338 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3339 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3340 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3341 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3342 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3343 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3344 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3345 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3346 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3347 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3348 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3349 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3350 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3351 REGINFO_SENTINEL
3354 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3355 uint64_t value)
3357 TCR *tcr = raw_ptr(env, ri);
3358 int maskshift = extract32(value, 0, 3);
3360 if (!arm_feature(env, ARM_FEATURE_V8)) {
3361 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3362 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3363 * using Long-desciptor translation table format */
3364 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3365 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3366 /* In an implementation that includes the Security Extensions
3367 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3368 * Short-descriptor translation table format.
3370 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3371 } else {
3372 value &= TTBCR_N;
3376 /* Update the masks corresponding to the TCR bank being written
3377 * Note that we always calculate mask and base_mask, but
3378 * they are only used for short-descriptor tables (ie if EAE is 0);
3379 * for long-descriptor tables the TCR fields are used differently
3380 * and the mask and base_mask values are meaningless.
3382 tcr->raw_tcr = value;
3383 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3384 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
3387 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3388 uint64_t value)
3390 ARMCPU *cpu = env_archcpu(env);
3391 TCR *tcr = raw_ptr(env, ri);
3393 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3394 /* With LPAE the TTBCR could result in a change of ASID
3395 * via the TTBCR.A1 bit, so do a TLB flush.
3397 tlb_flush(CPU(cpu));
3399 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3400 value = deposit64(tcr->raw_tcr, 0, 32, value);
3401 vmsa_ttbcr_raw_write(env, ri, value);
3404 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3406 TCR *tcr = raw_ptr(env, ri);
3408 /* Reset both the TCR as well as the masks corresponding to the bank of
3409 * the TCR being reset.
3411 tcr->raw_tcr = 0;
3412 tcr->mask = 0;
3413 tcr->base_mask = 0xffffc000u;
3416 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3417 uint64_t value)
3419 ARMCPU *cpu = env_archcpu(env);
3420 TCR *tcr = raw_ptr(env, ri);
3422 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3423 tlb_flush(CPU(cpu));
3424 tcr->raw_tcr = value;
3427 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3428 uint64_t value)
3430 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3431 if (cpreg_field_is_64bit(ri) &&
3432 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3433 ARMCPU *cpu = env_archcpu(env);
3434 tlb_flush(CPU(cpu));
3436 raw_write(env, ri, value);
3439 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3440 uint64_t value)
3442 ARMCPU *cpu = env_archcpu(env);
3443 CPUState *cs = CPU(cpu);
3445 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
3446 if (raw_read(env, ri) != value) {
3447 tlb_flush_by_mmuidx(cs,
3448 ARMMMUIdxBit_S12NSE1 |
3449 ARMMMUIdxBit_S12NSE0 |
3450 ARMMMUIdxBit_S2NS);
3451 raw_write(env, ri, value);
3455 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3456 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3457 .access = PL1_RW, .type = ARM_CP_ALIAS,
3458 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3459 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3460 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3461 .access = PL1_RW, .resetvalue = 0,
3462 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3463 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3464 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3465 .access = PL1_RW, .resetvalue = 0,
3466 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3467 offsetof(CPUARMState, cp15.dfar_ns) } },
3468 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3469 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3470 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3471 .resetvalue = 0, },
3472 REGINFO_SENTINEL
3475 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3476 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3477 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3478 .access = PL1_RW,
3479 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3480 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3481 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3482 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3483 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3484 offsetof(CPUARMState, cp15.ttbr0_ns) } },
3485 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3486 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3487 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3488 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3489 offsetof(CPUARMState, cp15.ttbr1_ns) } },
3490 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3491 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3492 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
3493 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3494 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3495 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3496 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3497 .raw_writefn = vmsa_ttbcr_raw_write,
3498 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3499 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
3500 REGINFO_SENTINEL
3503 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3504 * qemu tlbs nor adjusting cached masks.
3506 static const ARMCPRegInfo ttbcr2_reginfo = {
3507 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3508 .access = PL1_RW, .type = ARM_CP_ALIAS,
3509 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3510 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
3513 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3514 uint64_t value)
3516 env->cp15.c15_ticonfig = value & 0xe7;
3517 /* The OS_TYPE bit in this register changes the reported CPUID! */
3518 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3519 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3522 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3523 uint64_t value)
3525 env->cp15.c15_threadid = value & 0xffff;
3528 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3529 uint64_t value)
3531 /* Wait-for-interrupt (deprecated) */
3532 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3535 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3536 uint64_t value)
3538 /* On OMAP there are registers indicating the max/min index of dcache lines
3539 * containing a dirty line; cache flush operations have to reset these.
3541 env->cp15.c15_i_max = 0x000;
3542 env->cp15.c15_i_min = 0xff0;
3545 static const ARMCPRegInfo omap_cp_reginfo[] = {
3546 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3547 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3548 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3549 .resetvalue = 0, },
3550 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3551 .access = PL1_RW, .type = ARM_CP_NOP },
3552 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3553 .access = PL1_RW,
3554 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3555 .writefn = omap_ticonfig_write },
3556 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3557 .access = PL1_RW,
3558 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3559 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3560 .access = PL1_RW, .resetvalue = 0xff0,
3561 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3562 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3563 .access = PL1_RW,
3564 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3565 .writefn = omap_threadid_write },
3566 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3567 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3568 .type = ARM_CP_NO_RAW,
3569 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3570 /* TODO: Peripheral port remap register:
3571 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3572 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3573 * when MMU is off.
3575 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3576 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3577 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3578 .writefn = omap_cachemaint_write },
3579 { .name = "C9", .cp = 15, .crn = 9,
3580 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3581 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3582 REGINFO_SENTINEL
3585 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3586 uint64_t value)
3588 env->cp15.c15_cpar = value & 0x3fff;
3591 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3592 { .name = "XSCALE_CPAR",
3593 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3594 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3595 .writefn = xscale_cpar_write, },
3596 { .name = "XSCALE_AUXCR",
3597 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3598 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3599 .resetvalue = 0, },
3600 /* XScale specific cache-lockdown: since we have no cache we NOP these
3601 * and hope the guest does not really rely on cache behaviour.
3603 { .name = "XSCALE_LOCK_ICACHE_LINE",
3604 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3605 .access = PL1_W, .type = ARM_CP_NOP },
3606 { .name = "XSCALE_UNLOCK_ICACHE",
3607 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3608 .access = PL1_W, .type = ARM_CP_NOP },
3609 { .name = "XSCALE_DCACHE_LOCK",
3610 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3611 .access = PL1_RW, .type = ARM_CP_NOP },
3612 { .name = "XSCALE_UNLOCK_DCACHE",
3613 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3614 .access = PL1_W, .type = ARM_CP_NOP },
3615 REGINFO_SENTINEL
3618 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3619 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3620 * implementation of this implementation-defined space.
3621 * Ideally this should eventually disappear in favour of actually
3622 * implementing the correct behaviour for all cores.
3624 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3625 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3626 .access = PL1_RW,
3627 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3628 .resetvalue = 0 },
3629 REGINFO_SENTINEL
3632 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3633 /* Cache status: RAZ because we have no cache so it's always clean */
3634 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3635 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3636 .resetvalue = 0 },
3637 REGINFO_SENTINEL
3640 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3641 /* We never have a a block transfer operation in progress */
3642 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
3643 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3644 .resetvalue = 0 },
3645 /* The cache ops themselves: these all NOP for QEMU */
3646 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3647 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3648 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3649 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3650 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3651 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3652 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
3653 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3654 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
3655 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3656 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
3657 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3658 REGINFO_SENTINEL
3661 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
3662 /* The cache test-and-clean instructions always return (1 << 30)
3663 * to indicate that there are no dirty cache lines.
3665 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
3666 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3667 .resetvalue = (1 << 30) },
3668 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
3669 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3670 .resetvalue = (1 << 30) },
3671 REGINFO_SENTINEL
3674 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
3675 /* Ignore ReadBuffer accesses */
3676 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
3677 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3678 .access = PL1_RW, .resetvalue = 0,
3679 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
3680 REGINFO_SENTINEL
3683 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3685 ARMCPU *cpu = env_archcpu(env);
3686 unsigned int cur_el = arm_current_el(env);
3687 bool secure = arm_is_secure(env);
3689 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
3690 return env->cp15.vpidr_el2;
3692 return raw_read(env, ri);
3695 static uint64_t mpidr_read_val(CPUARMState *env)
3697 ARMCPU *cpu = env_archcpu(env);
3698 uint64_t mpidr = cpu->mp_affinity;
3700 if (arm_feature(env, ARM_FEATURE_V7MP)) {
3701 mpidr |= (1U << 31);
3702 /* Cores which are uniprocessor (non-coherent)
3703 * but still implement the MP extensions set
3704 * bit 30. (For instance, Cortex-R5).
3706 if (cpu->mp_is_up) {
3707 mpidr |= (1u << 30);
3710 return mpidr;
3713 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3715 unsigned int cur_el = arm_current_el(env);
3716 bool secure = arm_is_secure(env);
3718 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
3719 return env->cp15.vmpidr_el2;
3721 return mpidr_read_val(env);
3724 static const ARMCPRegInfo lpae_cp_reginfo[] = {
3725 /* NOP AMAIR0/1 */
3726 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
3727 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
3728 .access = PL1_RW, .type = ARM_CP_CONST,
3729 .resetvalue = 0 },
3730 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
3731 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
3732 .access = PL1_RW, .type = ARM_CP_CONST,
3733 .resetvalue = 0 },
3734 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
3735 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
3736 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
3737 offsetof(CPUARMState, cp15.par_ns)} },
3738 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
3739 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3740 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3741 offsetof(CPUARMState, cp15.ttbr0_ns) },
3742 .writefn = vmsa_ttbr_write, },
3743 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
3744 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3745 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3746 offsetof(CPUARMState, cp15.ttbr1_ns) },
3747 .writefn = vmsa_ttbr_write, },
3748 REGINFO_SENTINEL
3751 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3753 return vfp_get_fpcr(env);
3756 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3757 uint64_t value)
3759 vfp_set_fpcr(env, value);
3762 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3764 return vfp_get_fpsr(env);
3767 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3768 uint64_t value)
3770 vfp_set_fpsr(env, value);
3773 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
3774 bool isread)
3776 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
3777 return CP_ACCESS_TRAP;
3779 return CP_ACCESS_OK;
3782 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
3783 uint64_t value)
3785 env->daif = value & PSTATE_DAIF;
3788 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3789 const ARMCPRegInfo *ri,
3790 bool isread)
3792 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
3793 * SCTLR_EL1.UCI is set.
3795 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
3796 return CP_ACCESS_TRAP;
3798 return CP_ACCESS_OK;
3801 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
3802 * Page D4-1736 (DDI0487A.b)
3805 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3806 uint64_t value)
3808 CPUState *cs = env_cpu(env);
3809 bool sec = arm_is_secure_below_el3(env);
3811 if (sec) {
3812 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3813 ARMMMUIdxBit_S1SE1 |
3814 ARMMMUIdxBit_S1SE0);
3815 } else {
3816 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3817 ARMMMUIdxBit_S12NSE1 |
3818 ARMMMUIdxBit_S12NSE0);
3822 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3823 uint64_t value)
3825 CPUState *cs = env_cpu(env);
3827 if (tlb_force_broadcast(env)) {
3828 tlbi_aa64_vmalle1is_write(env, NULL, value);
3829 return;
3832 if (arm_is_secure_below_el3(env)) {
3833 tlb_flush_by_mmuidx(cs,
3834 ARMMMUIdxBit_S1SE1 |
3835 ARMMMUIdxBit_S1SE0);
3836 } else {
3837 tlb_flush_by_mmuidx(cs,
3838 ARMMMUIdxBit_S12NSE1 |
3839 ARMMMUIdxBit_S12NSE0);
3843 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3844 uint64_t value)
3846 /* Note that the 'ALL' scope must invalidate both stage 1 and
3847 * stage 2 translations, whereas most other scopes only invalidate
3848 * stage 1 translations.
3850 ARMCPU *cpu = env_archcpu(env);
3851 CPUState *cs = CPU(cpu);
3853 if (arm_is_secure_below_el3(env)) {
3854 tlb_flush_by_mmuidx(cs,
3855 ARMMMUIdxBit_S1SE1 |
3856 ARMMMUIdxBit_S1SE0);
3857 } else {
3858 if (arm_feature(env, ARM_FEATURE_EL2)) {
3859 tlb_flush_by_mmuidx(cs,
3860 ARMMMUIdxBit_S12NSE1 |
3861 ARMMMUIdxBit_S12NSE0 |
3862 ARMMMUIdxBit_S2NS);
3863 } else {
3864 tlb_flush_by_mmuidx(cs,
3865 ARMMMUIdxBit_S12NSE1 |
3866 ARMMMUIdxBit_S12NSE0);
3871 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3872 uint64_t value)
3874 ARMCPU *cpu = env_archcpu(env);
3875 CPUState *cs = CPU(cpu);
3877 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
3880 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3881 uint64_t value)
3883 ARMCPU *cpu = env_archcpu(env);
3884 CPUState *cs = CPU(cpu);
3886 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
3889 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3890 uint64_t value)
3892 /* Note that the 'ALL' scope must invalidate both stage 1 and
3893 * stage 2 translations, whereas most other scopes only invalidate
3894 * stage 1 translations.
3896 CPUState *cs = env_cpu(env);
3897 bool sec = arm_is_secure_below_el3(env);
3898 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
3900 if (sec) {
3901 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3902 ARMMMUIdxBit_S1SE1 |
3903 ARMMMUIdxBit_S1SE0);
3904 } else if (has_el2) {
3905 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3906 ARMMMUIdxBit_S12NSE1 |
3907 ARMMMUIdxBit_S12NSE0 |
3908 ARMMMUIdxBit_S2NS);
3909 } else {
3910 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3911 ARMMMUIdxBit_S12NSE1 |
3912 ARMMMUIdxBit_S12NSE0);
3916 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3917 uint64_t value)
3919 CPUState *cs = env_cpu(env);
3921 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
3924 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3925 uint64_t value)
3927 CPUState *cs = env_cpu(env);
3929 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
3932 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3933 uint64_t value)
3935 /* Invalidate by VA, EL2
3936 * Currently handles both VAE2 and VALE2, since we don't support
3937 * flush-last-level-only.
3939 ARMCPU *cpu = env_archcpu(env);
3940 CPUState *cs = CPU(cpu);
3941 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3943 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
3946 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3947 uint64_t value)
3949 /* Invalidate by VA, EL3
3950 * Currently handles both VAE3 and VALE3, since we don't support
3951 * flush-last-level-only.
3953 ARMCPU *cpu = env_archcpu(env);
3954 CPUState *cs = CPU(cpu);
3955 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3957 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
3960 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3961 uint64_t value)
3963 ARMCPU *cpu = env_archcpu(env);
3964 CPUState *cs = CPU(cpu);
3965 bool sec = arm_is_secure_below_el3(env);
3966 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3968 if (sec) {
3969 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3970 ARMMMUIdxBit_S1SE1 |
3971 ARMMMUIdxBit_S1SE0);
3972 } else {
3973 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3974 ARMMMUIdxBit_S12NSE1 |
3975 ARMMMUIdxBit_S12NSE0);
3979 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3980 uint64_t value)
3982 /* Invalidate by VA, EL1&0 (AArch64 version).
3983 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3984 * since we don't support flush-for-specific-ASID-only or
3985 * flush-last-level-only.
3987 ARMCPU *cpu = env_archcpu(env);
3988 CPUState *cs = CPU(cpu);
3989 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3991 if (tlb_force_broadcast(env)) {
3992 tlbi_aa64_vae1is_write(env, NULL, value);
3993 return;
3996 if (arm_is_secure_below_el3(env)) {
3997 tlb_flush_page_by_mmuidx(cs, pageaddr,
3998 ARMMMUIdxBit_S1SE1 |
3999 ARMMMUIdxBit_S1SE0);
4000 } else {
4001 tlb_flush_page_by_mmuidx(cs, pageaddr,
4002 ARMMMUIdxBit_S12NSE1 |
4003 ARMMMUIdxBit_S12NSE0);
4007 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4008 uint64_t value)
4010 CPUState *cs = env_cpu(env);
4011 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4013 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
4014 ARMMMUIdxBit_S1E2);
4017 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4018 uint64_t value)
4020 CPUState *cs = env_cpu(env);
4021 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4023 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
4024 ARMMMUIdxBit_S1E3);
4027 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4028 uint64_t value)
4030 /* Invalidate by IPA. This has to invalidate any structures that
4031 * contain only stage 2 translation information, but does not need
4032 * to apply to structures that contain combined stage 1 and stage 2
4033 * translation information.
4034 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
4036 ARMCPU *cpu = env_archcpu(env);
4037 CPUState *cs = CPU(cpu);
4038 uint64_t pageaddr;
4040 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4041 return;
4044 pageaddr = sextract64(value << 12, 0, 48);
4046 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
4049 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4050 uint64_t value)
4052 CPUState *cs = env_cpu(env);
4053 uint64_t pageaddr;
4055 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4056 return;
4059 pageaddr = sextract64(value << 12, 0, 48);
4061 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
4062 ARMMMUIdxBit_S2NS);
4065 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4066 bool isread)
4068 /* We don't implement EL2, so the only control on DC ZVA is the
4069 * bit in the SCTLR which can prohibit access for EL0.
4071 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4072 return CP_ACCESS_TRAP;
4074 return CP_ACCESS_OK;
4077 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4079 ARMCPU *cpu = env_archcpu(env);
4080 int dzp_bit = 1 << 4;
4082 /* DZP indicates whether DC ZVA access is allowed */
4083 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4084 dzp_bit = 0;
4086 return cpu->dcz_blocksize | dzp_bit;
4089 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4090 bool isread)
4092 if (!(env->pstate & PSTATE_SP)) {
4093 /* Access to SP_EL0 is undefined if it's being used as
4094 * the stack pointer.
4096 return CP_ACCESS_TRAP_UNCATEGORIZED;
4098 return CP_ACCESS_OK;
4101 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4103 return env->pstate & PSTATE_SP;
4106 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4108 update_spsel(env, val);
4111 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4112 uint64_t value)
4114 ARMCPU *cpu = env_archcpu(env);
4116 if (raw_read(env, ri) == value) {
4117 /* Skip the TLB flush if nothing actually changed; Linux likes
4118 * to do a lot of pointless SCTLR writes.
4120 return;
4123 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4124 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4125 value &= ~SCTLR_M;
4128 raw_write(env, ri, value);
4129 /* ??? Lots of these bits are not implemented. */
4130 /* This may enable/disable the MMU, so do a TLB flush. */
4131 tlb_flush(CPU(cpu));
4134 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4135 bool isread)
4137 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
4138 return CP_ACCESS_TRAP_FP_EL2;
4140 if (env->cp15.cptr_el[3] & CPTR_TFP) {
4141 return CP_ACCESS_TRAP_FP_EL3;
4143 return CP_ACCESS_OK;
4146 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4147 uint64_t value)
4149 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4152 static const ARMCPRegInfo v8_cp_reginfo[] = {
4153 /* Minimal set of EL0-visible registers. This will need to be expanded
4154 * significantly for system emulation of AArch64 CPUs.
4156 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4157 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4158 .access = PL0_RW, .type = ARM_CP_NZCV },
4159 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4160 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4161 .type = ARM_CP_NO_RAW,
4162 .access = PL0_RW, .accessfn = aa64_daif_access,
4163 .fieldoffset = offsetof(CPUARMState, daif),
4164 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4165 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4166 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4167 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4168 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4169 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4170 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4171 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4172 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4173 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4174 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4175 .access = PL0_R, .type = ARM_CP_NO_RAW,
4176 .readfn = aa64_dczid_read },
4177 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4178 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4179 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4180 #ifndef CONFIG_USER_ONLY
4181 /* Avoid overhead of an access check that always passes in user-mode */
4182 .accessfn = aa64_zva_access,
4183 #endif
4185 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4186 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4187 .access = PL1_R, .type = ARM_CP_CURRENTEL },
4188 /* Cache ops: all NOPs since we don't emulate caches */
4189 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4190 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4191 .access = PL1_W, .type = ARM_CP_NOP },
4192 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4193 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4194 .access = PL1_W, .type = ARM_CP_NOP },
4195 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4196 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4197 .access = PL0_W, .type = ARM_CP_NOP,
4198 .accessfn = aa64_cacheop_access },
4199 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4200 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4201 .access = PL1_W, .type = ARM_CP_NOP },
4202 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4203 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4204 .access = PL1_W, .type = ARM_CP_NOP },
4205 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4206 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4207 .access = PL0_W, .type = ARM_CP_NOP,
4208 .accessfn = aa64_cacheop_access },
4209 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4210 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4211 .access = PL1_W, .type = ARM_CP_NOP },
4212 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4213 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4214 .access = PL0_W, .type = ARM_CP_NOP,
4215 .accessfn = aa64_cacheop_access },
4216 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4217 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4218 .access = PL0_W, .type = ARM_CP_NOP,
4219 .accessfn = aa64_cacheop_access },
4220 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4221 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4222 .access = PL1_W, .type = ARM_CP_NOP },
4223 /* TLBI operations */
4224 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4225 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4226 .access = PL1_W, .type = ARM_CP_NO_RAW,
4227 .writefn = tlbi_aa64_vmalle1is_write },
4228 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4229 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4230 .access = PL1_W, .type = ARM_CP_NO_RAW,
4231 .writefn = tlbi_aa64_vae1is_write },
4232 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4233 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4234 .access = PL1_W, .type = ARM_CP_NO_RAW,
4235 .writefn = tlbi_aa64_vmalle1is_write },
4236 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4237 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4238 .access = PL1_W, .type = ARM_CP_NO_RAW,
4239 .writefn = tlbi_aa64_vae1is_write },
4240 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4241 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4242 .access = PL1_W, .type = ARM_CP_NO_RAW,
4243 .writefn = tlbi_aa64_vae1is_write },
4244 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4245 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4246 .access = PL1_W, .type = ARM_CP_NO_RAW,
4247 .writefn = tlbi_aa64_vae1is_write },
4248 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4249 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4250 .access = PL1_W, .type = ARM_CP_NO_RAW,
4251 .writefn = tlbi_aa64_vmalle1_write },
4252 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4253 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4254 .access = PL1_W, .type = ARM_CP_NO_RAW,
4255 .writefn = tlbi_aa64_vae1_write },
4256 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4257 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4258 .access = PL1_W, .type = ARM_CP_NO_RAW,
4259 .writefn = tlbi_aa64_vmalle1_write },
4260 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4261 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4262 .access = PL1_W, .type = ARM_CP_NO_RAW,
4263 .writefn = tlbi_aa64_vae1_write },
4264 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4265 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4266 .access = PL1_W, .type = ARM_CP_NO_RAW,
4267 .writefn = tlbi_aa64_vae1_write },
4268 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4269 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4270 .access = PL1_W, .type = ARM_CP_NO_RAW,
4271 .writefn = tlbi_aa64_vae1_write },
4272 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4273 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4274 .access = PL2_W, .type = ARM_CP_NO_RAW,
4275 .writefn = tlbi_aa64_ipas2e1is_write },
4276 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4277 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4278 .access = PL2_W, .type = ARM_CP_NO_RAW,
4279 .writefn = tlbi_aa64_ipas2e1is_write },
4280 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4281 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4282 .access = PL2_W, .type = ARM_CP_NO_RAW,
4283 .writefn = tlbi_aa64_alle1is_write },
4284 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4285 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4286 .access = PL2_W, .type = ARM_CP_NO_RAW,
4287 .writefn = tlbi_aa64_alle1is_write },
4288 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4289 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4290 .access = PL2_W, .type = ARM_CP_NO_RAW,
4291 .writefn = tlbi_aa64_ipas2e1_write },
4292 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4293 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4294 .access = PL2_W, .type = ARM_CP_NO_RAW,
4295 .writefn = tlbi_aa64_ipas2e1_write },
4296 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4297 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4298 .access = PL2_W, .type = ARM_CP_NO_RAW,
4299 .writefn = tlbi_aa64_alle1_write },
4300 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4301 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4302 .access = PL2_W, .type = ARM_CP_NO_RAW,
4303 .writefn = tlbi_aa64_alle1is_write },
4304 #ifndef CONFIG_USER_ONLY
4305 /* 64 bit address translation operations */
4306 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4307 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4308 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4309 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4310 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4311 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4312 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4313 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4314 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4315 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4316 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4317 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4318 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4319 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4320 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4321 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4322 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4323 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4324 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4325 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4326 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4327 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4328 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4329 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4330 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4331 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4332 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4333 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4334 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4335 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4336 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4337 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4338 .type = ARM_CP_ALIAS,
4339 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4340 .access = PL1_RW, .resetvalue = 0,
4341 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4342 .writefn = par_write },
4343 #endif
4344 /* TLB invalidate last level of translation table walk */
4345 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4346 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
4347 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4348 .type = ARM_CP_NO_RAW, .access = PL1_W,
4349 .writefn = tlbimvaa_is_write },
4350 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4351 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
4352 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4353 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
4354 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4355 .type = ARM_CP_NO_RAW, .access = PL2_W,
4356 .writefn = tlbimva_hyp_write },
4357 { .name = "TLBIMVALHIS",
4358 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4359 .type = ARM_CP_NO_RAW, .access = PL2_W,
4360 .writefn = tlbimva_hyp_is_write },
4361 { .name = "TLBIIPAS2",
4362 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4363 .type = ARM_CP_NO_RAW, .access = PL2_W,
4364 .writefn = tlbiipas2_write },
4365 { .name = "TLBIIPAS2IS",
4366 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4367 .type = ARM_CP_NO_RAW, .access = PL2_W,
4368 .writefn = tlbiipas2_is_write },
4369 { .name = "TLBIIPAS2L",
4370 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4371 .type = ARM_CP_NO_RAW, .access = PL2_W,
4372 .writefn = tlbiipas2_write },
4373 { .name = "TLBIIPAS2LIS",
4374 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4375 .type = ARM_CP_NO_RAW, .access = PL2_W,
4376 .writefn = tlbiipas2_is_write },
4377 /* 32 bit cache operations */
4378 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4379 .type = ARM_CP_NOP, .access = PL1_W },
4380 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4381 .type = ARM_CP_NOP, .access = PL1_W },
4382 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4383 .type = ARM_CP_NOP, .access = PL1_W },
4384 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
4385 .type = ARM_CP_NOP, .access = PL1_W },
4386 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
4387 .type = ARM_CP_NOP, .access = PL1_W },
4388 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
4389 .type = ARM_CP_NOP, .access = PL1_W },
4390 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4391 .type = ARM_CP_NOP, .access = PL1_W },
4392 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4393 .type = ARM_CP_NOP, .access = PL1_W },
4394 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
4395 .type = ARM_CP_NOP, .access = PL1_W },
4396 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4397 .type = ARM_CP_NOP, .access = PL1_W },
4398 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
4399 .type = ARM_CP_NOP, .access = PL1_W },
4400 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
4401 .type = ARM_CP_NOP, .access = PL1_W },
4402 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4403 .type = ARM_CP_NOP, .access = PL1_W },
4404 /* MMU Domain access control / MPU write buffer control */
4405 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
4406 .access = PL1_RW, .resetvalue = 0,
4407 .writefn = dacr_write, .raw_writefn = raw_write,
4408 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
4409 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
4410 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
4411 .type = ARM_CP_ALIAS,
4412 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
4413 .access = PL1_RW,
4414 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
4415 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
4416 .type = ARM_CP_ALIAS,
4417 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
4418 .access = PL1_RW,
4419 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
4420 /* We rely on the access checks not allowing the guest to write to the
4421 * state field when SPSel indicates that it's being used as the stack
4422 * pointer.
4424 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
4425 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
4426 .access = PL1_RW, .accessfn = sp_el0_access,
4427 .type = ARM_CP_ALIAS,
4428 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
4429 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
4430 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
4431 .access = PL2_RW, .type = ARM_CP_ALIAS,
4432 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
4433 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
4434 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
4435 .type = ARM_CP_NO_RAW,
4436 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
4437 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
4438 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
4439 .type = ARM_CP_ALIAS,
4440 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
4441 .access = PL2_RW, .accessfn = fpexc32_access },
4442 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
4443 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
4444 .access = PL2_RW, .resetvalue = 0,
4445 .writefn = dacr_write, .raw_writefn = raw_write,
4446 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
4447 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
4448 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
4449 .access = PL2_RW, .resetvalue = 0,
4450 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
4451 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
4452 .type = ARM_CP_ALIAS,
4453 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
4454 .access = PL2_RW,
4455 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
4456 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
4457 .type = ARM_CP_ALIAS,
4458 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
4459 .access = PL2_RW,
4460 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
4461 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
4462 .type = ARM_CP_ALIAS,
4463 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
4464 .access = PL2_RW,
4465 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
4466 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
4467 .type = ARM_CP_ALIAS,
4468 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
4469 .access = PL2_RW,
4470 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
4471 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
4472 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
4473 .resetvalue = 0,
4474 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
4475 { .name = "SDCR", .type = ARM_CP_ALIAS,
4476 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
4477 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4478 .writefn = sdcr_write,
4479 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
4480 REGINFO_SENTINEL
4483 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4484 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
4485 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
4486 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
4487 .access = PL2_RW,
4488 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
4489 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
4490 .type = ARM_CP_NO_RAW,
4491 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4492 .access = PL2_RW,
4493 .type = ARM_CP_CONST, .resetvalue = 0 },
4494 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
4495 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
4496 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4497 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
4498 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
4499 .access = PL2_RW,
4500 .type = ARM_CP_CONST, .resetvalue = 0 },
4501 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
4502 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
4503 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4504 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
4505 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
4506 .access = PL2_RW, .type = ARM_CP_CONST,
4507 .resetvalue = 0 },
4508 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
4509 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
4510 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4511 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
4512 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
4513 .access = PL2_RW, .type = ARM_CP_CONST,
4514 .resetvalue = 0 },
4515 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
4516 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
4517 .access = PL2_RW, .type = ARM_CP_CONST,
4518 .resetvalue = 0 },
4519 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
4520 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
4521 .access = PL2_RW, .type = ARM_CP_CONST,
4522 .resetvalue = 0 },
4523 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
4524 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
4525 .access = PL2_RW, .type = ARM_CP_CONST,
4526 .resetvalue = 0 },
4527 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
4528 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
4529 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4530 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
4531 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
4532 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4533 .type = ARM_CP_CONST, .resetvalue = 0 },
4534 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
4535 .cp = 15, .opc1 = 6, .crm = 2,
4536 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4537 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
4538 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
4539 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
4540 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4541 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
4542 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
4543 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4544 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4545 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
4546 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4547 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
4548 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
4549 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4550 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
4551 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4552 .resetvalue = 0 },
4553 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
4554 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
4555 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4556 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
4557 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
4558 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4559 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
4560 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4561 .resetvalue = 0 },
4562 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
4563 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
4564 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4565 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4566 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4567 .resetvalue = 0 },
4568 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4569 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
4570 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4571 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4572 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4573 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4574 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4575 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4576 .access = PL2_RW, .accessfn = access_tda,
4577 .type = ARM_CP_CONST, .resetvalue = 0 },
4578 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
4579 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4580 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4581 .type = ARM_CP_CONST, .resetvalue = 0 },
4582 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4583 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4584 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4585 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
4586 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
4587 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4588 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
4589 .type = ARM_CP_CONST,
4590 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
4591 .access = PL2_RW, .resetvalue = 0 },
4592 REGINFO_SENTINEL
4595 /* Ditto, but for registers which exist in ARMv8 but not v7 */
4596 static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
4597 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
4598 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
4599 .access = PL2_RW,
4600 .type = ARM_CP_CONST, .resetvalue = 0 },
4601 REGINFO_SENTINEL
4604 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4606 ARMCPU *cpu = env_archcpu(env);
4607 uint64_t valid_mask = HCR_MASK;
4609 if (arm_feature(env, ARM_FEATURE_EL3)) {
4610 valid_mask &= ~HCR_HCD;
4611 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
4612 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
4613 * However, if we're using the SMC PSCI conduit then QEMU is
4614 * effectively acting like EL3 firmware and so the guest at
4615 * EL2 should retain the ability to prevent EL1 from being
4616 * able to make SMC calls into the ersatz firmware, so in
4617 * that case HCR.TSC should be read/write.
4619 valid_mask &= ~HCR_TSC;
4621 if (cpu_isar_feature(aa64_lor, cpu)) {
4622 valid_mask |= HCR_TLOR;
4624 if (cpu_isar_feature(aa64_pauth, cpu)) {
4625 valid_mask |= HCR_API | HCR_APK;
4628 /* Clear RES0 bits. */
4629 value &= valid_mask;
4631 /* These bits change the MMU setup:
4632 * HCR_VM enables stage 2 translation
4633 * HCR_PTW forbids certain page-table setups
4634 * HCR_DC Disables stage1 and enables stage2 translation
4636 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
4637 tlb_flush(CPU(cpu));
4639 env->cp15.hcr_el2 = value;
4642 * Updates to VI and VF require us to update the status of
4643 * virtual interrupts, which are the logical OR of these bits
4644 * and the state of the input lines from the GIC. (This requires
4645 * that we have the iothread lock, which is done by marking the
4646 * reginfo structs as ARM_CP_IO.)
4647 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
4648 * possible for it to be taken immediately, because VIRQ and
4649 * VFIQ are masked unless running at EL0 or EL1, and HCR
4650 * can only be written at EL2.
4652 g_assert(qemu_mutex_iothread_locked());
4653 arm_cpu_update_virq(cpu);
4654 arm_cpu_update_vfiq(cpu);
4657 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
4658 uint64_t value)
4660 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
4661 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
4662 hcr_write(env, NULL, value);
4665 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
4666 uint64_t value)
4668 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
4669 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
4670 hcr_write(env, NULL, value);
4674 * Return the effective value of HCR_EL2.
4675 * Bits that are not included here:
4676 * RW (read from SCR_EL3.RW as needed)
4678 uint64_t arm_hcr_el2_eff(CPUARMState *env)
4680 uint64_t ret = env->cp15.hcr_el2;
4682 if (arm_is_secure_below_el3(env)) {
4684 * "This register has no effect if EL2 is not enabled in the
4685 * current Security state". This is ARMv8.4-SecEL2 speak for
4686 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
4688 * Prior to that, the language was "In an implementation that
4689 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
4690 * as if this field is 0 for all purposes other than a direct
4691 * read or write access of HCR_EL2". With lots of enumeration
4692 * on a per-field basis. In current QEMU, this is condition
4693 * is arm_is_secure_below_el3.
4695 * Since the v8.4 language applies to the entire register, and
4696 * appears to be backward compatible, use that.
4698 ret = 0;
4699 } else if (ret & HCR_TGE) {
4700 /* These bits are up-to-date as of ARMv8.4. */
4701 if (ret & HCR_E2H) {
4702 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
4703 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
4704 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4705 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE);
4706 } else {
4707 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
4709 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
4710 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
4711 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
4712 HCR_TLOR);
4715 return ret;
4718 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4719 uint64_t value)
4722 * For A-profile AArch32 EL3, if NSACR.CP10
4723 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
4725 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
4726 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
4727 value &= ~(0x3 << 10);
4728 value |= env->cp15.cptr_el[2] & (0x3 << 10);
4730 env->cp15.cptr_el[2] = value;
4733 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
4736 * For A-profile AArch32 EL3, if NSACR.CP10
4737 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
4739 uint64_t value = env->cp15.cptr_el[2];
4741 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
4742 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
4743 value |= 0x3 << 10;
4745 return value;
4748 static const ARMCPRegInfo el2_cp_reginfo[] = {
4749 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
4750 .type = ARM_CP_IO,
4751 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4752 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
4753 .writefn = hcr_write },
4754 { .name = "HCR", .state = ARM_CP_STATE_AA32,
4755 .type = ARM_CP_ALIAS | ARM_CP_IO,
4756 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4757 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
4758 .writefn = hcr_writelow },
4759 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
4760 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
4761 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4762 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
4763 .type = ARM_CP_ALIAS,
4764 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
4765 .access = PL2_RW,
4766 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
4767 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
4768 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
4769 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
4770 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
4771 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
4772 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
4773 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
4774 .type = ARM_CP_ALIAS,
4775 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
4776 .access = PL2_RW,
4777 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
4778 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
4779 .type = ARM_CP_ALIAS,
4780 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
4781 .access = PL2_RW,
4782 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
4783 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
4784 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
4785 .access = PL2_RW, .writefn = vbar_write,
4786 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
4787 .resetvalue = 0 },
4788 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
4789 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
4790 .access = PL3_RW, .type = ARM_CP_ALIAS,
4791 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
4792 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
4793 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
4794 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
4795 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
4796 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
4797 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
4798 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
4799 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
4800 .resetvalue = 0 },
4801 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
4802 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
4803 .access = PL2_RW, .type = ARM_CP_ALIAS,
4804 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
4805 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
4806 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
4807 .access = PL2_RW, .type = ARM_CP_CONST,
4808 .resetvalue = 0 },
4809 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
4810 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
4811 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
4812 .access = PL2_RW, .type = ARM_CP_CONST,
4813 .resetvalue = 0 },
4814 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
4815 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
4816 .access = PL2_RW, .type = ARM_CP_CONST,
4817 .resetvalue = 0 },
4818 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
4819 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
4820 .access = PL2_RW, .type = ARM_CP_CONST,
4821 .resetvalue = 0 },
4822 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
4823 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
4824 .access = PL2_RW,
4825 /* no .writefn needed as this can't cause an ASID change;
4826 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
4828 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
4829 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
4830 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
4831 .type = ARM_CP_ALIAS,
4832 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4833 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
4834 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
4835 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
4836 .access = PL2_RW,
4837 /* no .writefn needed as this can't cause an ASID change;
4838 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
4840 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
4841 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
4842 .cp = 15, .opc1 = 6, .crm = 2,
4843 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4844 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4845 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
4846 .writefn = vttbr_write },
4847 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
4848 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
4849 .access = PL2_RW, .writefn = vttbr_write,
4850 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
4851 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
4852 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
4853 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
4854 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
4855 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4856 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
4857 .access = PL2_RW, .resetvalue = 0,
4858 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
4859 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
4860 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
4861 .access = PL2_RW, .resetvalue = 0,
4862 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
4863 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
4864 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4865 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
4866 { .name = "TLBIALLNSNH",
4867 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4868 .type = ARM_CP_NO_RAW, .access = PL2_W,
4869 .writefn = tlbiall_nsnh_write },
4870 { .name = "TLBIALLNSNHIS",
4871 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4872 .type = ARM_CP_NO_RAW, .access = PL2_W,
4873 .writefn = tlbiall_nsnh_is_write },
4874 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
4875 .type = ARM_CP_NO_RAW, .access = PL2_W,
4876 .writefn = tlbiall_hyp_write },
4877 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
4878 .type = ARM_CP_NO_RAW, .access = PL2_W,
4879 .writefn = tlbiall_hyp_is_write },
4880 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
4881 .type = ARM_CP_NO_RAW, .access = PL2_W,
4882 .writefn = tlbimva_hyp_write },
4883 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
4884 .type = ARM_CP_NO_RAW, .access = PL2_W,
4885 .writefn = tlbimva_hyp_is_write },
4886 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
4887 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
4888 .type = ARM_CP_NO_RAW, .access = PL2_W,
4889 .writefn = tlbi_aa64_alle2_write },
4890 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
4891 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
4892 .type = ARM_CP_NO_RAW, .access = PL2_W,
4893 .writefn = tlbi_aa64_vae2_write },
4894 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
4895 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4896 .access = PL2_W, .type = ARM_CP_NO_RAW,
4897 .writefn = tlbi_aa64_vae2_write },
4898 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
4899 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
4900 .access = PL2_W, .type = ARM_CP_NO_RAW,
4901 .writefn = tlbi_aa64_alle2is_write },
4902 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
4903 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
4904 .type = ARM_CP_NO_RAW, .access = PL2_W,
4905 .writefn = tlbi_aa64_vae2is_write },
4906 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
4907 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4908 .access = PL2_W, .type = ARM_CP_NO_RAW,
4909 .writefn = tlbi_aa64_vae2is_write },
4910 #ifndef CONFIG_USER_ONLY
4911 /* Unlike the other EL2-related AT operations, these must
4912 * UNDEF from EL3 if EL2 is not implemented, which is why we
4913 * define them here rather than with the rest of the AT ops.
4915 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
4916 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
4917 .access = PL2_W, .accessfn = at_s1e2_access,
4918 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4919 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
4920 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
4921 .access = PL2_W, .accessfn = at_s1e2_access,
4922 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4923 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
4924 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
4925 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
4926 * to behave as if SCR.NS was 1.
4928 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
4929 .access = PL2_W,
4930 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
4931 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
4932 .access = PL2_W,
4933 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
4934 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
4935 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
4936 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
4937 * reset values as IMPDEF. We choose to reset to 3 to comply with
4938 * both ARMv7 and ARMv8.
4940 .access = PL2_RW, .resetvalue = 3,
4941 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
4942 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
4943 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
4944 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
4945 .writefn = gt_cntvoff_write,
4946 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
4947 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
4948 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
4949 .writefn = gt_cntvoff_write,
4950 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
4951 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
4952 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
4953 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4954 .type = ARM_CP_IO, .access = PL2_RW,
4955 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4956 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4957 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4958 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
4959 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4960 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4961 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
4962 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
4963 .resetfn = gt_hyp_timer_reset,
4964 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
4965 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4966 .type = ARM_CP_IO,
4967 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4968 .access = PL2_RW,
4969 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
4970 .resetvalue = 0,
4971 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
4972 #endif
4973 /* The only field of MDCR_EL2 that has a defined architectural reset value
4974 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
4975 * don't implement any PMU event counters, so using zero as a reset
4976 * value for MDCR_EL2 is okay
4978 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4979 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4980 .access = PL2_RW, .resetvalue = 0,
4981 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
4982 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
4983 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4984 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4985 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4986 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
4987 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4988 .access = PL2_RW,
4989 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4990 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4991 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4992 .access = PL2_RW,
4993 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
4994 REGINFO_SENTINEL
4997 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
4998 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
4999 .type = ARM_CP_ALIAS | ARM_CP_IO,
5000 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5001 .access = PL2_RW,
5002 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5003 .writefn = hcr_writehigh },
5004 REGINFO_SENTINEL
5007 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5008 bool isread)
5010 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5011 * At Secure EL1 it traps to EL3.
5013 if (arm_current_el(env) == 3) {
5014 return CP_ACCESS_OK;
5016 if (arm_is_secure_below_el3(env)) {
5017 return CP_ACCESS_TRAP_EL3;
5019 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5020 if (isread) {
5021 return CP_ACCESS_OK;
5023 return CP_ACCESS_TRAP_UNCATEGORIZED;
5026 static const ARMCPRegInfo el3_cp_reginfo[] = {
5027 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5028 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5029 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5030 .resetvalue = 0, .writefn = scr_write },
5031 { .name = "SCR", .type = ARM_CP_ALIAS,
5032 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5033 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5034 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5035 .writefn = scr_write },
5036 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5037 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5038 .access = PL3_RW, .resetvalue = 0,
5039 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5040 { .name = "SDER",
5041 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5042 .access = PL3_RW, .resetvalue = 0,
5043 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5044 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5045 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5046 .writefn = vbar_write, .resetvalue = 0,
5047 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5048 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5049 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5050 .access = PL3_RW, .resetvalue = 0,
5051 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5052 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5053 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5054 .access = PL3_RW,
5055 /* no .writefn needed as this can't cause an ASID change;
5056 * we must provide a .raw_writefn and .resetfn because we handle
5057 * reset and migration for the AArch32 TTBCR(S), which might be
5058 * using mask and base_mask.
5060 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
5061 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5062 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5063 .type = ARM_CP_ALIAS,
5064 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5065 .access = PL3_RW,
5066 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5067 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5068 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5069 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5070 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5071 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5072 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5073 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5074 .type = ARM_CP_ALIAS,
5075 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5076 .access = PL3_RW,
5077 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5078 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5079 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5080 .access = PL3_RW, .writefn = vbar_write,
5081 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5082 .resetvalue = 0 },
5083 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5084 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5085 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5086 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5087 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5088 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5089 .access = PL3_RW, .resetvalue = 0,
5090 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5091 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5092 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5093 .access = PL3_RW, .type = ARM_CP_CONST,
5094 .resetvalue = 0 },
5095 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5096 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5097 .access = PL3_RW, .type = ARM_CP_CONST,
5098 .resetvalue = 0 },
5099 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5100 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5101 .access = PL3_RW, .type = ARM_CP_CONST,
5102 .resetvalue = 0 },
5103 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5104 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5105 .access = PL3_W, .type = ARM_CP_NO_RAW,
5106 .writefn = tlbi_aa64_alle3is_write },
5107 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5108 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5109 .access = PL3_W, .type = ARM_CP_NO_RAW,
5110 .writefn = tlbi_aa64_vae3is_write },
5111 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5112 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5113 .access = PL3_W, .type = ARM_CP_NO_RAW,
5114 .writefn = tlbi_aa64_vae3is_write },
5115 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5116 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5117 .access = PL3_W, .type = ARM_CP_NO_RAW,
5118 .writefn = tlbi_aa64_alle3_write },
5119 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5120 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5121 .access = PL3_W, .type = ARM_CP_NO_RAW,
5122 .writefn = tlbi_aa64_vae3_write },
5123 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5124 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5125 .access = PL3_W, .type = ARM_CP_NO_RAW,
5126 .writefn = tlbi_aa64_vae3_write },
5127 REGINFO_SENTINEL
5130 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5131 bool isread)
5133 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
5134 * but the AArch32 CTR has its own reginfo struct)
5136 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5137 return CP_ACCESS_TRAP;
5139 return CP_ACCESS_OK;
5142 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5143 uint64_t value)
5145 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5146 * read via a bit in OSLSR_EL1.
5148 int oslock;
5150 if (ri->state == ARM_CP_STATE_AA32) {
5151 oslock = (value == 0xC5ACCE55);
5152 } else {
5153 oslock = value & 1;
5156 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5159 static const ARMCPRegInfo debug_cp_reginfo[] = {
5160 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
5161 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5162 * unlike DBGDRAR it is never accessible from EL0.
5163 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5164 * accessor.
5166 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
5167 .access = PL0_R, .accessfn = access_tdra,
5168 .type = ARM_CP_CONST, .resetvalue = 0 },
5169 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
5170 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5171 .access = PL1_R, .accessfn = access_tdra,
5172 .type = ARM_CP_CONST, .resetvalue = 0 },
5173 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
5174 .access = PL0_R, .accessfn = access_tdra,
5175 .type = ARM_CP_CONST, .resetvalue = 0 },
5176 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
5177 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
5178 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
5179 .access = PL1_RW, .accessfn = access_tda,
5180 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
5181 .resetvalue = 0 },
5182 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
5183 * We don't implement the configurable EL0 access.
5185 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
5186 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
5187 .type = ARM_CP_ALIAS,
5188 .access = PL1_R, .accessfn = access_tda,
5189 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
5190 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
5191 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
5192 .access = PL1_W, .type = ARM_CP_NO_RAW,
5193 .accessfn = access_tdosa,
5194 .writefn = oslar_write },
5195 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
5196 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
5197 .access = PL1_R, .resetvalue = 10,
5198 .accessfn = access_tdosa,
5199 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5200 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
5201 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
5202 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
5203 .access = PL1_RW, .accessfn = access_tdosa,
5204 .type = ARM_CP_NOP },
5205 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
5206 * implement vector catch debug events yet.
5208 { .name = "DBGVCR",
5209 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
5210 .access = PL1_RW, .accessfn = access_tda,
5211 .type = ARM_CP_NOP },
5212 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
5213 * to save and restore a 32-bit guest's DBGVCR)
5215 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
5216 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
5217 .access = PL2_RW, .accessfn = access_tda,
5218 .type = ARM_CP_NOP },
5219 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
5220 * Channel but Linux may try to access this register. The 32-bit
5221 * alias is DBGDCCINT.
5223 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
5224 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5225 .access = PL1_RW, .accessfn = access_tda,
5226 .type = ARM_CP_NOP },
5227 REGINFO_SENTINEL
5230 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
5231 /* 64 bit access versions of the (dummy) debug registers */
5232 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
5233 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5234 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
5235 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5236 REGINFO_SENTINEL
5239 /* Return the exception level to which exceptions should be taken
5240 * via SVEAccessTrap. If an exception should be routed through
5241 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
5242 * take care of raising that exception.
5243 * C.f. the ARM pseudocode function CheckSVEEnabled.
5245 int sve_exception_el(CPUARMState *env, int el)
5247 #ifndef CONFIG_USER_ONLY
5248 if (el <= 1) {
5249 bool disabled = false;
5251 /* The CPACR.ZEN controls traps to EL1:
5252 * 0, 2 : trap EL0 and EL1 accesses
5253 * 1 : trap only EL0 accesses
5254 * 3 : trap no accesses
5256 if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
5257 disabled = true;
5258 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
5259 disabled = el == 0;
5261 if (disabled) {
5262 /* route_to_el2 */
5263 return (arm_feature(env, ARM_FEATURE_EL2)
5264 && (arm_hcr_el2_eff(env) & HCR_TGE) ? 2 : 1);
5267 /* Check CPACR.FPEN. */
5268 if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
5269 disabled = true;
5270 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
5271 disabled = el == 0;
5273 if (disabled) {
5274 return 0;
5278 /* CPTR_EL2. Since TZ and TFP are positive,
5279 * they will be zero when EL2 is not present.
5281 if (el <= 2 && !arm_is_secure_below_el3(env)) {
5282 if (env->cp15.cptr_el[2] & CPTR_TZ) {
5283 return 2;
5285 if (env->cp15.cptr_el[2] & CPTR_TFP) {
5286 return 0;
5290 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
5291 if (arm_feature(env, ARM_FEATURE_EL3)
5292 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5293 return 3;
5295 #endif
5296 return 0;
5300 * Given that SVE is enabled, return the vector length for EL.
5302 uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
5304 ARMCPU *cpu = env_archcpu(env);
5305 uint32_t zcr_len = cpu->sve_max_vq - 1;
5307 if (el <= 1) {
5308 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
5310 if (el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
5311 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
5313 if (el < 3 && arm_feature(env, ARM_FEATURE_EL3)) {
5314 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
5316 return zcr_len;
5319 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5320 uint64_t value)
5322 int cur_el = arm_current_el(env);
5323 int old_len = sve_zcr_len_for_el(env, cur_el);
5324 int new_len;
5326 /* Bits other than [3:0] are RAZ/WI. */
5327 raw_write(env, ri, value & 0xf);
5330 * Because we arrived here, we know both FP and SVE are enabled;
5331 * otherwise we would have trapped access to the ZCR_ELn register.
5333 new_len = sve_zcr_len_for_el(env, cur_el);
5334 if (new_len < old_len) {
5335 aarch64_sve_narrow_vq(env, new_len + 1);
5339 static const ARMCPRegInfo zcr_el1_reginfo = {
5340 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
5341 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
5342 .access = PL1_RW, .type = ARM_CP_SVE,
5343 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
5344 .writefn = zcr_write, .raw_writefn = raw_write
5347 static const ARMCPRegInfo zcr_el2_reginfo = {
5348 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
5349 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
5350 .access = PL2_RW, .type = ARM_CP_SVE,
5351 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
5352 .writefn = zcr_write, .raw_writefn = raw_write
5355 static const ARMCPRegInfo zcr_no_el2_reginfo = {
5356 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
5357 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
5358 .access = PL2_RW, .type = ARM_CP_SVE,
5359 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
5362 static const ARMCPRegInfo zcr_el3_reginfo = {
5363 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
5364 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
5365 .access = PL3_RW, .type = ARM_CP_SVE,
5366 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
5367 .writefn = zcr_write, .raw_writefn = raw_write
5370 void hw_watchpoint_update(ARMCPU *cpu, int n)
5372 CPUARMState *env = &cpu->env;
5373 vaddr len = 0;
5374 vaddr wvr = env->cp15.dbgwvr[n];
5375 uint64_t wcr = env->cp15.dbgwcr[n];
5376 int mask;
5377 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
5379 if (env->cpu_watchpoint[n]) {
5380 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
5381 env->cpu_watchpoint[n] = NULL;
5384 if (!extract64(wcr, 0, 1)) {
5385 /* E bit clear : watchpoint disabled */
5386 return;
5389 switch (extract64(wcr, 3, 2)) {
5390 case 0:
5391 /* LSC 00 is reserved and must behave as if the wp is disabled */
5392 return;
5393 case 1:
5394 flags |= BP_MEM_READ;
5395 break;
5396 case 2:
5397 flags |= BP_MEM_WRITE;
5398 break;
5399 case 3:
5400 flags |= BP_MEM_ACCESS;
5401 break;
5404 /* Attempts to use both MASK and BAS fields simultaneously are
5405 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
5406 * thus generating a watchpoint for every byte in the masked region.
5408 mask = extract64(wcr, 24, 4);
5409 if (mask == 1 || mask == 2) {
5410 /* Reserved values of MASK; we must act as if the mask value was
5411 * some non-reserved value, or as if the watchpoint were disabled.
5412 * We choose the latter.
5414 return;
5415 } else if (mask) {
5416 /* Watchpoint covers an aligned area up to 2GB in size */
5417 len = 1ULL << mask;
5418 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
5419 * whether the watchpoint fires when the unmasked bits match; we opt
5420 * to generate the exceptions.
5422 wvr &= ~(len - 1);
5423 } else {
5424 /* Watchpoint covers bytes defined by the byte address select bits */
5425 int bas = extract64(wcr, 5, 8);
5426 int basstart;
5428 if (bas == 0) {
5429 /* This must act as if the watchpoint is disabled */
5430 return;
5433 if (extract64(wvr, 2, 1)) {
5434 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
5435 * ignored, and BAS[3:0] define which bytes to watch.
5437 bas &= 0xf;
5439 /* The BAS bits are supposed to be programmed to indicate a contiguous
5440 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
5441 * we fire for each byte in the word/doubleword addressed by the WVR.
5442 * We choose to ignore any non-zero bits after the first range of 1s.
5444 basstart = ctz32(bas);
5445 len = cto32(bas >> basstart);
5446 wvr += basstart;
5449 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
5450 &env->cpu_watchpoint[n]);
5453 void hw_watchpoint_update_all(ARMCPU *cpu)
5455 int i;
5456 CPUARMState *env = &cpu->env;
5458 /* Completely clear out existing QEMU watchpoints and our array, to
5459 * avoid possible stale entries following migration load.
5461 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
5462 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
5464 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
5465 hw_watchpoint_update(cpu, i);
5469 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5470 uint64_t value)
5472 ARMCPU *cpu = env_archcpu(env);
5473 int i = ri->crm;
5475 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
5476 * register reads and behaves as if values written are sign extended.
5477 * Bits [1:0] are RES0.
5479 value = sextract64(value, 0, 49) & ~3ULL;
5481 raw_write(env, ri, value);
5482 hw_watchpoint_update(cpu, i);
5485 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5486 uint64_t value)
5488 ARMCPU *cpu = env_archcpu(env);
5489 int i = ri->crm;
5491 raw_write(env, ri, value);
5492 hw_watchpoint_update(cpu, i);
5495 void hw_breakpoint_update(ARMCPU *cpu, int n)
5497 CPUARMState *env = &cpu->env;
5498 uint64_t bvr = env->cp15.dbgbvr[n];
5499 uint64_t bcr = env->cp15.dbgbcr[n];
5500 vaddr addr;
5501 int bt;
5502 int flags = BP_CPU;
5504 if (env->cpu_breakpoint[n]) {
5505 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
5506 env->cpu_breakpoint[n] = NULL;
5509 if (!extract64(bcr, 0, 1)) {
5510 /* E bit clear : watchpoint disabled */
5511 return;
5514 bt = extract64(bcr, 20, 4);
5516 switch (bt) {
5517 case 4: /* unlinked address mismatch (reserved if AArch64) */
5518 case 5: /* linked address mismatch (reserved if AArch64) */
5519 qemu_log_mask(LOG_UNIMP,
5520 "arm: address mismatch breakpoint types not implemented\n");
5521 return;
5522 case 0: /* unlinked address match */
5523 case 1: /* linked address match */
5525 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
5526 * we behave as if the register was sign extended. Bits [1:0] are
5527 * RES0. The BAS field is used to allow setting breakpoints on 16
5528 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
5529 * a bp will fire if the addresses covered by the bp and the addresses
5530 * covered by the insn overlap but the insn doesn't start at the
5531 * start of the bp address range. We choose to require the insn and
5532 * the bp to have the same address. The constraints on writing to
5533 * BAS enforced in dbgbcr_write mean we have only four cases:
5534 * 0b0000 => no breakpoint
5535 * 0b0011 => breakpoint on addr
5536 * 0b1100 => breakpoint on addr + 2
5537 * 0b1111 => breakpoint on addr
5538 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
5540 int bas = extract64(bcr, 5, 4);
5541 addr = sextract64(bvr, 0, 49) & ~3ULL;
5542 if (bas == 0) {
5543 return;
5545 if (bas == 0xc) {
5546 addr += 2;
5548 break;
5550 case 2: /* unlinked context ID match */
5551 case 8: /* unlinked VMID match (reserved if no EL2) */
5552 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
5553 qemu_log_mask(LOG_UNIMP,
5554 "arm: unlinked context breakpoint types not implemented\n");
5555 return;
5556 case 9: /* linked VMID match (reserved if no EL2) */
5557 case 11: /* linked context ID and VMID match (reserved if no EL2) */
5558 case 3: /* linked context ID match */
5559 default:
5560 /* We must generate no events for Linked context matches (unless
5561 * they are linked to by some other bp/wp, which is handled in
5562 * updates for the linking bp/wp). We choose to also generate no events
5563 * for reserved values.
5565 return;
5568 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
5571 void hw_breakpoint_update_all(ARMCPU *cpu)
5573 int i;
5574 CPUARMState *env = &cpu->env;
5576 /* Completely clear out existing QEMU breakpoints and our array, to
5577 * avoid possible stale entries following migration load.
5579 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
5580 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
5582 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
5583 hw_breakpoint_update(cpu, i);
5587 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5588 uint64_t value)
5590 ARMCPU *cpu = env_archcpu(env);
5591 int i = ri->crm;
5593 raw_write(env, ri, value);
5594 hw_breakpoint_update(cpu, i);
5597 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5598 uint64_t value)
5600 ARMCPU *cpu = env_archcpu(env);
5601 int i = ri->crm;
5603 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
5604 * copy of BAS[0].
5606 value = deposit64(value, 6, 1, extract64(value, 5, 1));
5607 value = deposit64(value, 8, 1, extract64(value, 7, 1));
5609 raw_write(env, ri, value);
5610 hw_breakpoint_update(cpu, i);
5613 static void define_debug_regs(ARMCPU *cpu)
5615 /* Define v7 and v8 architectural debug registers.
5616 * These are just dummy implementations for now.
5618 int i;
5619 int wrps, brps, ctx_cmps;
5620 ARMCPRegInfo dbgdidr = {
5621 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
5622 .access = PL0_R, .accessfn = access_tda,
5623 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
5626 /* Note that all these register fields hold "number of Xs minus 1". */
5627 brps = extract32(cpu->dbgdidr, 24, 4);
5628 wrps = extract32(cpu->dbgdidr, 28, 4);
5629 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
5631 assert(ctx_cmps <= brps);
5633 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
5634 * of the debug registers such as number of breakpoints;
5635 * check that if they both exist then they agree.
5637 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
5638 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
5639 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
5640 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
5643 define_one_arm_cp_reg(cpu, &dbgdidr);
5644 define_arm_cp_regs(cpu, debug_cp_reginfo);
5646 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
5647 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
5650 for (i = 0; i < brps + 1; i++) {
5651 ARMCPRegInfo dbgregs[] = {
5652 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
5653 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
5654 .access = PL1_RW, .accessfn = access_tda,
5655 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
5656 .writefn = dbgbvr_write, .raw_writefn = raw_write
5658 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
5659 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
5660 .access = PL1_RW, .accessfn = access_tda,
5661 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
5662 .writefn = dbgbcr_write, .raw_writefn = raw_write
5664 REGINFO_SENTINEL
5666 define_arm_cp_regs(cpu, dbgregs);
5669 for (i = 0; i < wrps + 1; i++) {
5670 ARMCPRegInfo dbgregs[] = {
5671 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
5672 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
5673 .access = PL1_RW, .accessfn = access_tda,
5674 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
5675 .writefn = dbgwvr_write, .raw_writefn = raw_write
5677 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
5678 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
5679 .access = PL1_RW, .accessfn = access_tda,
5680 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
5681 .writefn = dbgwcr_write, .raw_writefn = raw_write
5683 REGINFO_SENTINEL
5685 define_arm_cp_regs(cpu, dbgregs);
5689 /* We don't know until after realize whether there's a GICv3
5690 * attached, and that is what registers the gicv3 sysregs.
5691 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
5692 * at runtime.
5694 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
5696 ARMCPU *cpu = env_archcpu(env);
5697 uint64_t pfr1 = cpu->id_pfr1;
5699 if (env->gicv3state) {
5700 pfr1 |= 1 << 28;
5702 return pfr1;
5705 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
5707 ARMCPU *cpu = env_archcpu(env);
5708 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
5710 if (env->gicv3state) {
5711 pfr0 |= 1 << 24;
5713 return pfr0;
5716 /* Shared logic between LORID and the rest of the LOR* registers.
5717 * Secure state has already been delt with.
5719 static CPAccessResult access_lor_ns(CPUARMState *env)
5721 int el = arm_current_el(env);
5723 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
5724 return CP_ACCESS_TRAP_EL2;
5726 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
5727 return CP_ACCESS_TRAP_EL3;
5729 return CP_ACCESS_OK;
5732 static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri,
5733 bool isread)
5735 if (arm_is_secure_below_el3(env)) {
5736 /* Access ok in secure mode. */
5737 return CP_ACCESS_OK;
5739 return access_lor_ns(env);
5742 static CPAccessResult access_lor_other(CPUARMState *env,
5743 const ARMCPRegInfo *ri, bool isread)
5745 if (arm_is_secure_below_el3(env)) {
5746 /* Access denied in secure mode. */
5747 return CP_ACCESS_TRAP;
5749 return access_lor_ns(env);
5752 #ifdef TARGET_AARCH64
5753 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
5754 bool isread)
5756 int el = arm_current_el(env);
5758 if (el < 2 &&
5759 arm_feature(env, ARM_FEATURE_EL2) &&
5760 !(arm_hcr_el2_eff(env) & HCR_APK)) {
5761 return CP_ACCESS_TRAP_EL2;
5763 if (el < 3 &&
5764 arm_feature(env, ARM_FEATURE_EL3) &&
5765 !(env->cp15.scr_el3 & SCR_APK)) {
5766 return CP_ACCESS_TRAP_EL3;
5768 return CP_ACCESS_OK;
5771 static const ARMCPRegInfo pauth_reginfo[] = {
5772 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5773 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
5774 .access = PL1_RW, .accessfn = access_pauth,
5775 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
5776 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5777 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
5778 .access = PL1_RW, .accessfn = access_pauth,
5779 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
5780 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5781 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
5782 .access = PL1_RW, .accessfn = access_pauth,
5783 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
5784 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5785 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
5786 .access = PL1_RW, .accessfn = access_pauth,
5787 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
5788 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5789 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
5790 .access = PL1_RW, .accessfn = access_pauth,
5791 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
5792 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5793 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
5794 .access = PL1_RW, .accessfn = access_pauth,
5795 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
5796 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5797 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
5798 .access = PL1_RW, .accessfn = access_pauth,
5799 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
5800 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5801 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
5802 .access = PL1_RW, .accessfn = access_pauth,
5803 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
5804 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5805 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
5806 .access = PL1_RW, .accessfn = access_pauth,
5807 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
5808 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5809 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
5810 .access = PL1_RW, .accessfn = access_pauth,
5811 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
5812 REGINFO_SENTINEL
5815 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
5817 Error *err = NULL;
5818 uint64_t ret;
5820 /* Success sets NZCV = 0000. */
5821 env->NF = env->CF = env->VF = 0, env->ZF = 1;
5823 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
5825 * ??? Failed, for unknown reasons in the crypto subsystem.
5826 * The best we can do is log the reason and return the
5827 * timed-out indication to the guest. There is no reason
5828 * we know to expect this failure to be transitory, so the
5829 * guest may well hang retrying the operation.
5831 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
5832 ri->name, error_get_pretty(err));
5833 error_free(err);
5835 env->ZF = 0; /* NZCF = 0100 */
5836 return 0;
5838 return ret;
5841 /* We do not support re-seeding, so the two registers operate the same. */
5842 static const ARMCPRegInfo rndr_reginfo[] = {
5843 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
5844 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
5845 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
5846 .access = PL0_R, .readfn = rndr_readfn },
5847 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
5848 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
5849 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
5850 .access = PL0_R, .readfn = rndr_readfn },
5851 REGINFO_SENTINEL
5853 #endif
5855 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
5856 bool isread)
5858 int el = arm_current_el(env);
5860 if (el == 0) {
5861 uint64_t sctlr = arm_sctlr(env, el);
5862 if (!(sctlr & SCTLR_EnRCTX)) {
5863 return CP_ACCESS_TRAP;
5865 } else if (el == 1) {
5866 uint64_t hcr = arm_hcr_el2_eff(env);
5867 if (hcr & HCR_NV) {
5868 return CP_ACCESS_TRAP_EL2;
5871 return CP_ACCESS_OK;
5874 static const ARMCPRegInfo predinv_reginfo[] = {
5875 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
5876 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
5877 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5878 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
5879 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
5880 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5881 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
5882 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
5883 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5885 * Note the AArch32 opcodes have a different OPC1.
5887 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
5888 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
5889 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5890 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
5891 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
5892 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5893 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
5894 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
5895 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5896 REGINFO_SENTINEL
5899 void register_cp_regs_for_features(ARMCPU *cpu)
5901 /* Register all the coprocessor registers based on feature bits */
5902 CPUARMState *env = &cpu->env;
5903 if (arm_feature(env, ARM_FEATURE_M)) {
5904 /* M profile has no coprocessor registers */
5905 return;
5908 define_arm_cp_regs(cpu, cp_reginfo);
5909 if (!arm_feature(env, ARM_FEATURE_V8)) {
5910 /* Must go early as it is full of wildcards that may be
5911 * overridden by later definitions.
5913 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
5916 if (arm_feature(env, ARM_FEATURE_V6)) {
5917 /* The ID registers all have impdef reset values */
5918 ARMCPRegInfo v6_idregs[] = {
5919 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
5920 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
5921 .access = PL1_R, .type = ARM_CP_CONST,
5922 .resetvalue = cpu->id_pfr0 },
5923 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
5924 * the value of the GIC field until after we define these regs.
5926 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
5927 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
5928 .access = PL1_R, .type = ARM_CP_NO_RAW,
5929 .readfn = id_pfr1_read,
5930 .writefn = arm_cp_write_ignore },
5931 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
5932 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
5933 .access = PL1_R, .type = ARM_CP_CONST,
5934 .resetvalue = cpu->id_dfr0 },
5935 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
5936 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
5937 .access = PL1_R, .type = ARM_CP_CONST,
5938 .resetvalue = cpu->id_afr0 },
5939 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
5940 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
5941 .access = PL1_R, .type = ARM_CP_CONST,
5942 .resetvalue = cpu->id_mmfr0 },
5943 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
5944 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
5945 .access = PL1_R, .type = ARM_CP_CONST,
5946 .resetvalue = cpu->id_mmfr1 },
5947 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
5948 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
5949 .access = PL1_R, .type = ARM_CP_CONST,
5950 .resetvalue = cpu->id_mmfr2 },
5951 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
5952 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
5953 .access = PL1_R, .type = ARM_CP_CONST,
5954 .resetvalue = cpu->id_mmfr3 },
5955 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
5956 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5957 .access = PL1_R, .type = ARM_CP_CONST,
5958 .resetvalue = cpu->isar.id_isar0 },
5959 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
5960 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
5961 .access = PL1_R, .type = ARM_CP_CONST,
5962 .resetvalue = cpu->isar.id_isar1 },
5963 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
5964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
5965 .access = PL1_R, .type = ARM_CP_CONST,
5966 .resetvalue = cpu->isar.id_isar2 },
5967 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
5968 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
5969 .access = PL1_R, .type = ARM_CP_CONST,
5970 .resetvalue = cpu->isar.id_isar3 },
5971 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
5972 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
5973 .access = PL1_R, .type = ARM_CP_CONST,
5974 .resetvalue = cpu->isar.id_isar4 },
5975 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
5976 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
5977 .access = PL1_R, .type = ARM_CP_CONST,
5978 .resetvalue = cpu->isar.id_isar5 },
5979 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
5980 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
5981 .access = PL1_R, .type = ARM_CP_CONST,
5982 .resetvalue = cpu->id_mmfr4 },
5983 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
5984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
5985 .access = PL1_R, .type = ARM_CP_CONST,
5986 .resetvalue = cpu->isar.id_isar6 },
5987 REGINFO_SENTINEL
5989 define_arm_cp_regs(cpu, v6_idregs);
5990 define_arm_cp_regs(cpu, v6_cp_reginfo);
5991 } else {
5992 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
5994 if (arm_feature(env, ARM_FEATURE_V6K)) {
5995 define_arm_cp_regs(cpu, v6k_cp_reginfo);
5997 if (arm_feature(env, ARM_FEATURE_V7MP) &&
5998 !arm_feature(env, ARM_FEATURE_PMSA)) {
5999 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
6001 if (arm_feature(env, ARM_FEATURE_V7VE)) {
6002 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
6004 if (arm_feature(env, ARM_FEATURE_V7)) {
6005 /* v7 performance monitor control register: same implementor
6006 * field as main ID register, and we implement four counters in
6007 * addition to the cycle count register.
6009 unsigned int i, pmcrn = 4;
6010 ARMCPRegInfo pmcr = {
6011 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6012 .access = PL0_RW,
6013 .type = ARM_CP_IO | ARM_CP_ALIAS,
6014 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6015 .accessfn = pmreg_access, .writefn = pmcr_write,
6016 .raw_writefn = raw_write,
6018 ARMCPRegInfo pmcr64 = {
6019 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6020 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6021 .access = PL0_RW, .accessfn = pmreg_access,
6022 .type = ARM_CP_IO,
6023 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6024 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT),
6025 .writefn = pmcr_write, .raw_writefn = raw_write,
6027 define_one_arm_cp_reg(cpu, &pmcr);
6028 define_one_arm_cp_reg(cpu, &pmcr64);
6029 for (i = 0; i < pmcrn; i++) {
6030 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6031 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6032 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6033 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6034 ARMCPRegInfo pmev_regs[] = {
6035 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6036 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6037 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6038 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6039 .accessfn = pmreg_access },
6040 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6041 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6042 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6043 .type = ARM_CP_IO,
6044 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6045 .raw_readfn = pmevcntr_rawread,
6046 .raw_writefn = pmevcntr_rawwrite },
6047 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6048 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6049 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6050 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6051 .accessfn = pmreg_access },
6052 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6053 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6054 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6055 .type = ARM_CP_IO,
6056 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6057 .raw_writefn = pmevtyper_rawwrite },
6058 REGINFO_SENTINEL
6060 define_arm_cp_regs(cpu, pmev_regs);
6061 g_free(pmevcntr_name);
6062 g_free(pmevcntr_el0_name);
6063 g_free(pmevtyper_name);
6064 g_free(pmevtyper_el0_name);
6066 ARMCPRegInfo clidr = {
6067 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
6068 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
6069 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
6071 define_one_arm_cp_reg(cpu, &clidr);
6072 define_arm_cp_regs(cpu, v7_cp_reginfo);
6073 define_debug_regs(cpu);
6074 } else {
6075 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
6077 if (FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) >= 4 &&
6078 FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) != 0xf) {
6079 ARMCPRegInfo v81_pmu_regs[] = {
6080 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6081 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6082 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6083 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6084 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6085 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6086 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6087 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6088 REGINFO_SENTINEL
6090 define_arm_cp_regs(cpu, v81_pmu_regs);
6092 if (arm_feature(env, ARM_FEATURE_V8)) {
6093 /* AArch64 ID registers, which all have impdef reset values.
6094 * Note that within the ID register ranges the unused slots
6095 * must all RAZ, not UNDEF; future architecture versions may
6096 * define new registers here.
6098 ARMCPRegInfo v8_idregs[] = {
6099 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
6100 * know the right value for the GIC field until after we
6101 * define these regs.
6103 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
6104 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
6105 .access = PL1_R, .type = ARM_CP_NO_RAW,
6106 .readfn = id_aa64pfr0_read,
6107 .writefn = arm_cp_write_ignore },
6108 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
6109 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
6110 .access = PL1_R, .type = ARM_CP_CONST,
6111 .resetvalue = cpu->isar.id_aa64pfr1},
6112 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6113 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
6114 .access = PL1_R, .type = ARM_CP_CONST,
6115 .resetvalue = 0 },
6116 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6117 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
6118 .access = PL1_R, .type = ARM_CP_CONST,
6119 .resetvalue = 0 },
6120 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
6121 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
6122 .access = PL1_R, .type = ARM_CP_CONST,
6123 /* At present, only SVEver == 0 is defined anyway. */
6124 .resetvalue = 0 },
6125 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6126 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
6127 .access = PL1_R, .type = ARM_CP_CONST,
6128 .resetvalue = 0 },
6129 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6130 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
6131 .access = PL1_R, .type = ARM_CP_CONST,
6132 .resetvalue = 0 },
6133 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6134 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
6135 .access = PL1_R, .type = ARM_CP_CONST,
6136 .resetvalue = 0 },
6137 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
6138 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
6139 .access = PL1_R, .type = ARM_CP_CONST,
6140 .resetvalue = cpu->id_aa64dfr0 },
6141 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
6142 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
6143 .access = PL1_R, .type = ARM_CP_CONST,
6144 .resetvalue = cpu->id_aa64dfr1 },
6145 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6146 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
6147 .access = PL1_R, .type = ARM_CP_CONST,
6148 .resetvalue = 0 },
6149 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6150 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
6151 .access = PL1_R, .type = ARM_CP_CONST,
6152 .resetvalue = 0 },
6153 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
6154 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
6155 .access = PL1_R, .type = ARM_CP_CONST,
6156 .resetvalue = cpu->id_aa64afr0 },
6157 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
6158 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
6159 .access = PL1_R, .type = ARM_CP_CONST,
6160 .resetvalue = cpu->id_aa64afr1 },
6161 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6162 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
6163 .access = PL1_R, .type = ARM_CP_CONST,
6164 .resetvalue = 0 },
6165 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6166 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
6167 .access = PL1_R, .type = ARM_CP_CONST,
6168 .resetvalue = 0 },
6169 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
6170 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
6171 .access = PL1_R, .type = ARM_CP_CONST,
6172 .resetvalue = cpu->isar.id_aa64isar0 },
6173 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
6174 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
6175 .access = PL1_R, .type = ARM_CP_CONST,
6176 .resetvalue = cpu->isar.id_aa64isar1 },
6177 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6178 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
6179 .access = PL1_R, .type = ARM_CP_CONST,
6180 .resetvalue = 0 },
6181 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6182 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
6183 .access = PL1_R, .type = ARM_CP_CONST,
6184 .resetvalue = 0 },
6185 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6186 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
6187 .access = PL1_R, .type = ARM_CP_CONST,
6188 .resetvalue = 0 },
6189 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6190 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
6191 .access = PL1_R, .type = ARM_CP_CONST,
6192 .resetvalue = 0 },
6193 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6194 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
6195 .access = PL1_R, .type = ARM_CP_CONST,
6196 .resetvalue = 0 },
6197 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6198 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
6199 .access = PL1_R, .type = ARM_CP_CONST,
6200 .resetvalue = 0 },
6201 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
6202 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
6203 .access = PL1_R, .type = ARM_CP_CONST,
6204 .resetvalue = cpu->isar.id_aa64mmfr0 },
6205 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
6206 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
6207 .access = PL1_R, .type = ARM_CP_CONST,
6208 .resetvalue = cpu->isar.id_aa64mmfr1 },
6209 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6210 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
6211 .access = PL1_R, .type = ARM_CP_CONST,
6212 .resetvalue = 0 },
6213 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6214 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
6215 .access = PL1_R, .type = ARM_CP_CONST,
6216 .resetvalue = 0 },
6217 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6218 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
6219 .access = PL1_R, .type = ARM_CP_CONST,
6220 .resetvalue = 0 },
6221 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6222 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
6223 .access = PL1_R, .type = ARM_CP_CONST,
6224 .resetvalue = 0 },
6225 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6226 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
6227 .access = PL1_R, .type = ARM_CP_CONST,
6228 .resetvalue = 0 },
6229 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6230 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
6231 .access = PL1_R, .type = ARM_CP_CONST,
6232 .resetvalue = 0 },
6233 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
6234 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
6235 .access = PL1_R, .type = ARM_CP_CONST,
6236 .resetvalue = cpu->isar.mvfr0 },
6237 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
6238 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
6239 .access = PL1_R, .type = ARM_CP_CONST,
6240 .resetvalue = cpu->isar.mvfr1 },
6241 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
6242 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
6243 .access = PL1_R, .type = ARM_CP_CONST,
6244 .resetvalue = cpu->isar.mvfr2 },
6245 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6246 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
6247 .access = PL1_R, .type = ARM_CP_CONST,
6248 .resetvalue = 0 },
6249 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6250 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
6251 .access = PL1_R, .type = ARM_CP_CONST,
6252 .resetvalue = 0 },
6253 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6254 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
6255 .access = PL1_R, .type = ARM_CP_CONST,
6256 .resetvalue = 0 },
6257 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6258 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
6259 .access = PL1_R, .type = ARM_CP_CONST,
6260 .resetvalue = 0 },
6261 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6262 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
6263 .access = PL1_R, .type = ARM_CP_CONST,
6264 .resetvalue = 0 },
6265 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
6266 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
6267 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6268 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
6269 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
6270 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
6271 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6272 .resetvalue = cpu->pmceid0 },
6273 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
6274 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
6275 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6276 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
6277 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
6278 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
6279 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6280 .resetvalue = cpu->pmceid1 },
6281 REGINFO_SENTINEL
6283 #ifdef CONFIG_USER_ONLY
6284 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6285 { .name = "ID_AA64PFR0_EL1",
6286 .exported_bits = 0x000f000f00ff0000,
6287 .fixed_bits = 0x0000000000000011 },
6288 { .name = "ID_AA64PFR1_EL1",
6289 .exported_bits = 0x00000000000000f0 },
6290 { .name = "ID_AA64PFR*_EL1_RESERVED",
6291 .is_glob = true },
6292 { .name = "ID_AA64ZFR0_EL1" },
6293 { .name = "ID_AA64MMFR0_EL1",
6294 .fixed_bits = 0x00000000ff000000 },
6295 { .name = "ID_AA64MMFR1_EL1" },
6296 { .name = "ID_AA64MMFR*_EL1_RESERVED",
6297 .is_glob = true },
6298 { .name = "ID_AA64DFR0_EL1",
6299 .fixed_bits = 0x0000000000000006 },
6300 { .name = "ID_AA64DFR1_EL1" },
6301 { .name = "ID_AA64DFR*_EL1_RESERVED",
6302 .is_glob = true },
6303 { .name = "ID_AA64AFR*",
6304 .is_glob = true },
6305 { .name = "ID_AA64ISAR0_EL1",
6306 .exported_bits = 0x00fffffff0fffff0 },
6307 { .name = "ID_AA64ISAR1_EL1",
6308 .exported_bits = 0x000000f0ffffffff },
6309 { .name = "ID_AA64ISAR*_EL1_RESERVED",
6310 .is_glob = true },
6311 REGUSERINFO_SENTINEL
6313 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
6314 #endif
6315 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
6316 if (!arm_feature(env, ARM_FEATURE_EL3) &&
6317 !arm_feature(env, ARM_FEATURE_EL2)) {
6318 ARMCPRegInfo rvbar = {
6319 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
6320 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6321 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
6323 define_one_arm_cp_reg(cpu, &rvbar);
6325 define_arm_cp_regs(cpu, v8_idregs);
6326 define_arm_cp_regs(cpu, v8_cp_reginfo);
6328 if (arm_feature(env, ARM_FEATURE_EL2)) {
6329 uint64_t vmpidr_def = mpidr_read_val(env);
6330 ARMCPRegInfo vpidr_regs[] = {
6331 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
6332 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6333 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6334 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
6335 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
6336 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
6337 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6338 .access = PL2_RW, .resetvalue = cpu->midr,
6339 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
6340 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
6341 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6342 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6343 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
6344 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
6345 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
6346 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6347 .access = PL2_RW,
6348 .resetvalue = vmpidr_def,
6349 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
6350 REGINFO_SENTINEL
6352 define_arm_cp_regs(cpu, vpidr_regs);
6353 define_arm_cp_regs(cpu, el2_cp_reginfo);
6354 if (arm_feature(env, ARM_FEATURE_V8)) {
6355 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
6357 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
6358 if (!arm_feature(env, ARM_FEATURE_EL3)) {
6359 ARMCPRegInfo rvbar = {
6360 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
6361 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
6362 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
6364 define_one_arm_cp_reg(cpu, &rvbar);
6366 } else {
6367 /* If EL2 is missing but higher ELs are enabled, we need to
6368 * register the no_el2 reginfos.
6370 if (arm_feature(env, ARM_FEATURE_EL3)) {
6371 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
6372 * of MIDR_EL1 and MPIDR_EL1.
6374 ARMCPRegInfo vpidr_regs[] = {
6375 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6376 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6377 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
6378 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
6379 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
6380 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6381 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6382 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
6383 .type = ARM_CP_NO_RAW,
6384 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
6385 REGINFO_SENTINEL
6387 define_arm_cp_regs(cpu, vpidr_regs);
6388 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
6389 if (arm_feature(env, ARM_FEATURE_V8)) {
6390 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
6394 if (arm_feature(env, ARM_FEATURE_EL3)) {
6395 define_arm_cp_regs(cpu, el3_cp_reginfo);
6396 ARMCPRegInfo el3_regs[] = {
6397 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
6398 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
6399 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
6400 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
6401 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
6402 .access = PL3_RW,
6403 .raw_writefn = raw_write, .writefn = sctlr_write,
6404 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
6405 .resetvalue = cpu->reset_sctlr },
6406 REGINFO_SENTINEL
6409 define_arm_cp_regs(cpu, el3_regs);
6411 /* The behaviour of NSACR is sufficiently various that we don't
6412 * try to describe it in a single reginfo:
6413 * if EL3 is 64 bit, then trap to EL3 from S EL1,
6414 * reads as constant 0xc00 from NS EL1 and NS EL2
6415 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
6416 * if v7 without EL3, register doesn't exist
6417 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
6419 if (arm_feature(env, ARM_FEATURE_EL3)) {
6420 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6421 ARMCPRegInfo nsacr = {
6422 .name = "NSACR", .type = ARM_CP_CONST,
6423 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6424 .access = PL1_RW, .accessfn = nsacr_access,
6425 .resetvalue = 0xc00
6427 define_one_arm_cp_reg(cpu, &nsacr);
6428 } else {
6429 ARMCPRegInfo nsacr = {
6430 .name = "NSACR",
6431 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6432 .access = PL3_RW | PL1_R,
6433 .resetvalue = 0,
6434 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
6436 define_one_arm_cp_reg(cpu, &nsacr);
6438 } else {
6439 if (arm_feature(env, ARM_FEATURE_V8)) {
6440 ARMCPRegInfo nsacr = {
6441 .name = "NSACR", .type = ARM_CP_CONST,
6442 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6443 .access = PL1_R,
6444 .resetvalue = 0xc00
6446 define_one_arm_cp_reg(cpu, &nsacr);
6450 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6451 if (arm_feature(env, ARM_FEATURE_V6)) {
6452 /* PMSAv6 not implemented */
6453 assert(arm_feature(env, ARM_FEATURE_V7));
6454 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
6455 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
6456 } else {
6457 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
6459 } else {
6460 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
6461 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
6462 /* TTCBR2 is introduced with ARMv8.2-A32HPD. */
6463 if (FIELD_EX32(cpu->id_mmfr4, ID_MMFR4, HPDS) != 0) {
6464 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
6467 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
6468 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
6470 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
6471 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
6473 if (arm_feature(env, ARM_FEATURE_VAPA)) {
6474 define_arm_cp_regs(cpu, vapa_cp_reginfo);
6476 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
6477 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
6479 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
6480 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
6482 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
6483 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
6485 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
6486 define_arm_cp_regs(cpu, omap_cp_reginfo);
6488 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
6489 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
6491 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
6492 define_arm_cp_regs(cpu, xscale_cp_reginfo);
6494 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
6495 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
6497 if (arm_feature(env, ARM_FEATURE_LPAE)) {
6498 define_arm_cp_regs(cpu, lpae_cp_reginfo);
6500 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
6501 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
6502 * be read-only (ie write causes UNDEF exception).
6505 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
6506 /* Pre-v8 MIDR space.
6507 * Note that the MIDR isn't a simple constant register because
6508 * of the TI925 behaviour where writes to another register can
6509 * cause the MIDR value to change.
6511 * Unimplemented registers in the c15 0 0 0 space default to
6512 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
6513 * and friends override accordingly.
6515 { .name = "MIDR",
6516 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
6517 .access = PL1_R, .resetvalue = cpu->midr,
6518 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
6519 .readfn = midr_read,
6520 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
6521 .type = ARM_CP_OVERRIDE },
6522 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
6523 { .name = "DUMMY",
6524 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
6525 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6526 { .name = "DUMMY",
6527 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
6528 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6529 { .name = "DUMMY",
6530 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
6531 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6532 { .name = "DUMMY",
6533 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
6534 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6535 { .name = "DUMMY",
6536 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
6537 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6538 REGINFO_SENTINEL
6540 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
6541 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
6542 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
6543 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
6544 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
6545 .readfn = midr_read },
6546 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
6547 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
6548 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
6549 .access = PL1_R, .resetvalue = cpu->midr },
6550 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
6551 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
6552 .access = PL1_R, .resetvalue = cpu->midr },
6553 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
6554 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
6555 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
6556 REGINFO_SENTINEL
6558 ARMCPRegInfo id_cp_reginfo[] = {
6559 /* These are common to v8 and pre-v8 */
6560 { .name = "CTR",
6561 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
6562 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
6563 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
6564 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
6565 .access = PL0_R, .accessfn = ctr_el0_access,
6566 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
6567 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
6568 { .name = "TCMTR",
6569 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
6570 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6571 REGINFO_SENTINEL
6573 /* TLBTR is specific to VMSA */
6574 ARMCPRegInfo id_tlbtr_reginfo = {
6575 .name = "TLBTR",
6576 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
6577 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
6579 /* MPUIR is specific to PMSA V6+ */
6580 ARMCPRegInfo id_mpuir_reginfo = {
6581 .name = "MPUIR",
6582 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
6583 .access = PL1_R, .type = ARM_CP_CONST,
6584 .resetvalue = cpu->pmsav7_dregion << 8
6586 ARMCPRegInfo crn0_wi_reginfo = {
6587 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
6588 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
6589 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
6591 #ifdef CONFIG_USER_ONLY
6592 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6593 { .name = "MIDR_EL1",
6594 .exported_bits = 0x00000000ffffffff },
6595 { .name = "REVIDR_EL1" },
6596 REGUSERINFO_SENTINEL
6598 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
6599 #endif
6600 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
6601 arm_feature(env, ARM_FEATURE_STRONGARM)) {
6602 ARMCPRegInfo *r;
6603 /* Register the blanket "writes ignored" value first to cover the
6604 * whole space. Then update the specific ID registers to allow write
6605 * access, so that they ignore writes rather than causing them to
6606 * UNDEF.
6608 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
6609 for (r = id_pre_v8_midr_cp_reginfo;
6610 r->type != ARM_CP_SENTINEL; r++) {
6611 r->access = PL1_RW;
6613 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
6614 r->access = PL1_RW;
6616 id_mpuir_reginfo.access = PL1_RW;
6617 id_tlbtr_reginfo.access = PL1_RW;
6619 if (arm_feature(env, ARM_FEATURE_V8)) {
6620 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
6621 } else {
6622 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
6624 define_arm_cp_regs(cpu, id_cp_reginfo);
6625 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
6626 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
6627 } else if (arm_feature(env, ARM_FEATURE_V7)) {
6628 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
6632 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
6633 ARMCPRegInfo mpidr_cp_reginfo[] = {
6634 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
6635 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
6636 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
6637 REGINFO_SENTINEL
6639 #ifdef CONFIG_USER_ONLY
6640 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
6641 { .name = "MPIDR_EL1",
6642 .fixed_bits = 0x0000000080000000 },
6643 REGUSERINFO_SENTINEL
6645 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
6646 #endif
6647 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
6650 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
6651 ARMCPRegInfo auxcr_reginfo[] = {
6652 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
6653 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
6654 .access = PL1_RW, .type = ARM_CP_CONST,
6655 .resetvalue = cpu->reset_auxcr },
6656 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
6657 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
6658 .access = PL2_RW, .type = ARM_CP_CONST,
6659 .resetvalue = 0 },
6660 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
6661 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
6662 .access = PL3_RW, .type = ARM_CP_CONST,
6663 .resetvalue = 0 },
6664 REGINFO_SENTINEL
6666 define_arm_cp_regs(cpu, auxcr_reginfo);
6667 if (arm_feature(env, ARM_FEATURE_V8)) {
6668 /* HACTLR2 maps to ACTLR_EL2[63:32] and is not in ARMv7 */
6669 ARMCPRegInfo hactlr2_reginfo = {
6670 .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
6671 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
6672 .access = PL2_RW, .type = ARM_CP_CONST,
6673 .resetvalue = 0
6675 define_one_arm_cp_reg(cpu, &hactlr2_reginfo);
6679 if (arm_feature(env, ARM_FEATURE_CBAR)) {
6680 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6681 /* 32 bit view is [31:18] 0...0 [43:32]. */
6682 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
6683 | extract64(cpu->reset_cbar, 32, 12);
6684 ARMCPRegInfo cbar_reginfo[] = {
6685 { .name = "CBAR",
6686 .type = ARM_CP_CONST,
6687 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
6688 .access = PL1_R, .resetvalue = cpu->reset_cbar },
6689 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
6690 .type = ARM_CP_CONST,
6691 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
6692 .access = PL1_R, .resetvalue = cbar32 },
6693 REGINFO_SENTINEL
6695 /* We don't implement a r/w 64 bit CBAR currently */
6696 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
6697 define_arm_cp_regs(cpu, cbar_reginfo);
6698 } else {
6699 ARMCPRegInfo cbar = {
6700 .name = "CBAR",
6701 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
6702 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
6703 .fieldoffset = offsetof(CPUARMState,
6704 cp15.c15_config_base_address)
6706 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
6707 cbar.access = PL1_R;
6708 cbar.fieldoffset = 0;
6709 cbar.type = ARM_CP_CONST;
6711 define_one_arm_cp_reg(cpu, &cbar);
6715 if (arm_feature(env, ARM_FEATURE_VBAR)) {
6716 ARMCPRegInfo vbar_cp_reginfo[] = {
6717 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
6718 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
6719 .access = PL1_RW, .writefn = vbar_write,
6720 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
6721 offsetof(CPUARMState, cp15.vbar_ns) },
6722 .resetvalue = 0 },
6723 REGINFO_SENTINEL
6725 define_arm_cp_regs(cpu, vbar_cp_reginfo);
6728 /* Generic registers whose values depend on the implementation */
6730 ARMCPRegInfo sctlr = {
6731 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
6732 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
6733 .access = PL1_RW,
6734 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
6735 offsetof(CPUARMState, cp15.sctlr_ns) },
6736 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
6737 .raw_writefn = raw_write,
6739 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
6740 /* Normally we would always end the TB on an SCTLR write, but Linux
6741 * arch/arm/mach-pxa/sleep.S expects two instructions following
6742 * an MMU enable to execute from cache. Imitate this behaviour.
6744 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
6746 define_one_arm_cp_reg(cpu, &sctlr);
6749 if (cpu_isar_feature(aa64_lor, cpu)) {
6751 * A trivial implementation of ARMv8.1-LOR leaves all of these
6752 * registers fixed at 0, which indicates that there are zero
6753 * supported Limited Ordering regions.
6755 static const ARMCPRegInfo lor_reginfo[] = {
6756 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6757 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6758 .access = PL1_RW, .accessfn = access_lor_other,
6759 .type = ARM_CP_CONST, .resetvalue = 0 },
6760 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6761 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6762 .access = PL1_RW, .accessfn = access_lor_other,
6763 .type = ARM_CP_CONST, .resetvalue = 0 },
6764 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6765 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6766 .access = PL1_RW, .accessfn = access_lor_other,
6767 .type = ARM_CP_CONST, .resetvalue = 0 },
6768 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6769 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6770 .access = PL1_RW, .accessfn = access_lor_other,
6771 .type = ARM_CP_CONST, .resetvalue = 0 },
6772 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6773 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6774 .access = PL1_R, .accessfn = access_lorid,
6775 .type = ARM_CP_CONST, .resetvalue = 0 },
6776 REGINFO_SENTINEL
6778 define_arm_cp_regs(cpu, lor_reginfo);
6781 if (cpu_isar_feature(aa64_sve, cpu)) {
6782 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
6783 if (arm_feature(env, ARM_FEATURE_EL2)) {
6784 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
6785 } else {
6786 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
6788 if (arm_feature(env, ARM_FEATURE_EL3)) {
6789 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
6793 #ifdef TARGET_AARCH64
6794 if (cpu_isar_feature(aa64_pauth, cpu)) {
6795 define_arm_cp_regs(cpu, pauth_reginfo);
6797 if (cpu_isar_feature(aa64_rndr, cpu)) {
6798 define_arm_cp_regs(cpu, rndr_reginfo);
6800 #endif
6803 * While all v8.0 cpus support aarch64, QEMU does have configurations
6804 * that do not set ID_AA64ISAR1, e.g. user-only qemu-arm -cpu max,
6805 * which will set ID_ISAR6.
6807 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)
6808 ? cpu_isar_feature(aa64_predinv, cpu)
6809 : cpu_isar_feature(aa32_predinv, cpu)) {
6810 define_arm_cp_regs(cpu, predinv_reginfo);
6814 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
6816 CPUState *cs = CPU(cpu);
6817 CPUARMState *env = &cpu->env;
6819 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6820 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
6821 aarch64_fpu_gdb_set_reg,
6822 34, "aarch64-fpu.xml", 0);
6823 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
6824 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
6825 51, "arm-neon.xml", 0);
6826 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
6827 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
6828 35, "arm-vfp3.xml", 0);
6829 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
6830 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
6831 19, "arm-vfp.xml", 0);
6833 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
6834 arm_gen_dynamic_xml(cs),
6835 "system-registers.xml", 0);
6838 /* Sort alphabetically by type name, except for "any". */
6839 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
6841 ObjectClass *class_a = (ObjectClass *)a;
6842 ObjectClass *class_b = (ObjectClass *)b;
6843 const char *name_a, *name_b;
6845 name_a = object_class_get_name(class_a);
6846 name_b = object_class_get_name(class_b);
6847 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
6848 return 1;
6849 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
6850 return -1;
6851 } else {
6852 return strcmp(name_a, name_b);
6856 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
6858 ObjectClass *oc = data;
6859 const char *typename;
6860 char *name;
6862 typename = object_class_get_name(oc);
6863 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
6864 qemu_printf(" %s\n", name);
6865 g_free(name);
6868 void arm_cpu_list(void)
6870 GSList *list;
6872 list = object_class_get_list(TYPE_ARM_CPU, false);
6873 list = g_slist_sort(list, arm_cpu_list_compare);
6874 qemu_printf("Available CPUs:\n");
6875 g_slist_foreach(list, arm_cpu_list_entry, NULL);
6876 g_slist_free(list);
6879 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
6881 ObjectClass *oc = data;
6882 CpuDefinitionInfoList **cpu_list = user_data;
6883 CpuDefinitionInfoList *entry;
6884 CpuDefinitionInfo *info;
6885 const char *typename;
6887 typename = object_class_get_name(oc);
6888 info = g_malloc0(sizeof(*info));
6889 info->name = g_strndup(typename,
6890 strlen(typename) - strlen("-" TYPE_ARM_CPU));
6891 info->q_typename = g_strdup(typename);
6893 entry = g_malloc0(sizeof(*entry));
6894 entry->value = info;
6895 entry->next = *cpu_list;
6896 *cpu_list = entry;
6899 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
6901 CpuDefinitionInfoList *cpu_list = NULL;
6902 GSList *list;
6904 list = object_class_get_list(TYPE_ARM_CPU, false);
6905 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
6906 g_slist_free(list);
6908 return cpu_list;
6911 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
6912 void *opaque, int state, int secstate,
6913 int crm, int opc1, int opc2,
6914 const char *name)
6916 /* Private utility function for define_one_arm_cp_reg_with_opaque():
6917 * add a single reginfo struct to the hash table.
6919 uint32_t *key = g_new(uint32_t, 1);
6920 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
6921 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
6922 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
6924 r2->name = g_strdup(name);
6925 /* Reset the secure state to the specific incoming state. This is
6926 * necessary as the register may have been defined with both states.
6928 r2->secure = secstate;
6930 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
6931 /* Register is banked (using both entries in array).
6932 * Overwriting fieldoffset as the array is only used to define
6933 * banked registers but later only fieldoffset is used.
6935 r2->fieldoffset = r->bank_fieldoffsets[ns];
6938 if (state == ARM_CP_STATE_AA32) {
6939 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
6940 /* If the register is banked then we don't need to migrate or
6941 * reset the 32-bit instance in certain cases:
6943 * 1) If the register has both 32-bit and 64-bit instances then we
6944 * can count on the 64-bit instance taking care of the
6945 * non-secure bank.
6946 * 2) If ARMv8 is enabled then we can count on a 64-bit version
6947 * taking care of the secure bank. This requires that separate
6948 * 32 and 64-bit definitions are provided.
6950 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
6951 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
6952 r2->type |= ARM_CP_ALIAS;
6954 } else if ((secstate != r->secure) && !ns) {
6955 /* The register is not banked so we only want to allow migration of
6956 * the non-secure instance.
6958 r2->type |= ARM_CP_ALIAS;
6961 if (r->state == ARM_CP_STATE_BOTH) {
6962 /* We assume it is a cp15 register if the .cp field is left unset.
6964 if (r2->cp == 0) {
6965 r2->cp = 15;
6968 #ifdef HOST_WORDS_BIGENDIAN
6969 if (r2->fieldoffset) {
6970 r2->fieldoffset += sizeof(uint32_t);
6972 #endif
6975 if (state == ARM_CP_STATE_AA64) {
6976 /* To allow abbreviation of ARMCPRegInfo
6977 * definitions, we treat cp == 0 as equivalent to
6978 * the value for "standard guest-visible sysreg".
6979 * STATE_BOTH definitions are also always "standard
6980 * sysreg" in their AArch64 view (the .cp value may
6981 * be non-zero for the benefit of the AArch32 view).
6983 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
6984 r2->cp = CP_REG_ARM64_SYSREG_CP;
6986 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
6987 r2->opc0, opc1, opc2);
6988 } else {
6989 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
6991 if (opaque) {
6992 r2->opaque = opaque;
6994 /* reginfo passed to helpers is correct for the actual access,
6995 * and is never ARM_CP_STATE_BOTH:
6997 r2->state = state;
6998 /* Make sure reginfo passed to helpers for wildcarded regs
6999 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
7001 r2->crm = crm;
7002 r2->opc1 = opc1;
7003 r2->opc2 = opc2;
7004 /* By convention, for wildcarded registers only the first
7005 * entry is used for migration; the others are marked as
7006 * ALIAS so we don't try to transfer the register
7007 * multiple times. Special registers (ie NOP/WFI) are
7008 * never migratable and not even raw-accessible.
7010 if ((r->type & ARM_CP_SPECIAL)) {
7011 r2->type |= ARM_CP_NO_RAW;
7013 if (((r->crm == CP_ANY) && crm != 0) ||
7014 ((r->opc1 == CP_ANY) && opc1 != 0) ||
7015 ((r->opc2 == CP_ANY) && opc2 != 0)) {
7016 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
7019 /* Check that raw accesses are either forbidden or handled. Note that
7020 * we can't assert this earlier because the setup of fieldoffset for
7021 * banked registers has to be done first.
7023 if (!(r2->type & ARM_CP_NO_RAW)) {
7024 assert(!raw_accessors_invalid(r2));
7027 /* Overriding of an existing definition must be explicitly
7028 * requested.
7030 if (!(r->type & ARM_CP_OVERRIDE)) {
7031 ARMCPRegInfo *oldreg;
7032 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
7033 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
7034 fprintf(stderr, "Register redefined: cp=%d %d bit "
7035 "crn=%d crm=%d opc1=%d opc2=%d, "
7036 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
7037 r2->crn, r2->crm, r2->opc1, r2->opc2,
7038 oldreg->name, r2->name);
7039 g_assert_not_reached();
7042 g_hash_table_insert(cpu->cp_regs, key, r2);
7046 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
7047 const ARMCPRegInfo *r, void *opaque)
7049 /* Define implementations of coprocessor registers.
7050 * We store these in a hashtable because typically
7051 * there are less than 150 registers in a space which
7052 * is 16*16*16*8*8 = 262144 in size.
7053 * Wildcarding is supported for the crm, opc1 and opc2 fields.
7054 * If a register is defined twice then the second definition is
7055 * used, so this can be used to define some generic registers and
7056 * then override them with implementation specific variations.
7057 * At least one of the original and the second definition should
7058 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
7059 * against accidental use.
7061 * The state field defines whether the register is to be
7062 * visible in the AArch32 or AArch64 execution state. If the
7063 * state is set to ARM_CP_STATE_BOTH then we synthesise a
7064 * reginfo structure for the AArch32 view, which sees the lower
7065 * 32 bits of the 64 bit register.
7067 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
7068 * be wildcarded. AArch64 registers are always considered to be 64
7069 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
7070 * the register, if any.
7072 int crm, opc1, opc2, state;
7073 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
7074 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
7075 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
7076 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
7077 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
7078 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
7079 /* 64 bit registers have only CRm and Opc1 fields */
7080 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
7081 /* op0 only exists in the AArch64 encodings */
7082 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
7083 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
7084 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
7085 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
7086 * encodes a minimum access level for the register. We roll this
7087 * runtime check into our general permission check code, so check
7088 * here that the reginfo's specified permissions are strict enough
7089 * to encompass the generic architectural permission check.
7091 if (r->state != ARM_CP_STATE_AA32) {
7092 int mask = 0;
7093 switch (r->opc1) {
7094 case 0:
7095 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
7096 mask = PL0U_R | PL1_RW;
7097 break;
7098 case 1: case 2:
7099 /* min_EL EL1 */
7100 mask = PL1_RW;
7101 break;
7102 case 3:
7103 /* min_EL EL0 */
7104 mask = PL0_RW;
7105 break;
7106 case 4:
7107 /* min_EL EL2 */
7108 mask = PL2_RW;
7109 break;
7110 case 5:
7111 /* unallocated encoding, so not possible */
7112 assert(false);
7113 break;
7114 case 6:
7115 /* min_EL EL3 */
7116 mask = PL3_RW;
7117 break;
7118 case 7:
7119 /* min_EL EL1, secure mode only (we don't check the latter) */
7120 mask = PL1_RW;
7121 break;
7122 default:
7123 /* broken reginfo with out-of-range opc1 */
7124 assert(false);
7125 break;
7127 /* assert our permissions are not too lax (stricter is fine) */
7128 assert((r->access & ~mask) == 0);
7131 /* Check that the register definition has enough info to handle
7132 * reads and writes if they are permitted.
7134 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
7135 if (r->access & PL3_R) {
7136 assert((r->fieldoffset ||
7137 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
7138 r->readfn);
7140 if (r->access & PL3_W) {
7141 assert((r->fieldoffset ||
7142 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
7143 r->writefn);
7146 /* Bad type field probably means missing sentinel at end of reg list */
7147 assert(cptype_valid(r->type));
7148 for (crm = crmmin; crm <= crmmax; crm++) {
7149 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
7150 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
7151 for (state = ARM_CP_STATE_AA32;
7152 state <= ARM_CP_STATE_AA64; state++) {
7153 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
7154 continue;
7156 if (state == ARM_CP_STATE_AA32) {
7157 /* Under AArch32 CP registers can be common
7158 * (same for secure and non-secure world) or banked.
7160 char *name;
7162 switch (r->secure) {
7163 case ARM_CP_SECSTATE_S:
7164 case ARM_CP_SECSTATE_NS:
7165 add_cpreg_to_hashtable(cpu, r, opaque, state,
7166 r->secure, crm, opc1, opc2,
7167 r->name);
7168 break;
7169 default:
7170 name = g_strdup_printf("%s_S", r->name);
7171 add_cpreg_to_hashtable(cpu, r, opaque, state,
7172 ARM_CP_SECSTATE_S,
7173 crm, opc1, opc2, name);
7174 g_free(name);
7175 add_cpreg_to_hashtable(cpu, r, opaque, state,
7176 ARM_CP_SECSTATE_NS,
7177 crm, opc1, opc2, r->name);
7178 break;
7180 } else {
7181 /* AArch64 registers get mapped to non-secure instance
7182 * of AArch32 */
7183 add_cpreg_to_hashtable(cpu, r, opaque, state,
7184 ARM_CP_SECSTATE_NS,
7185 crm, opc1, opc2, r->name);
7193 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
7194 const ARMCPRegInfo *regs, void *opaque)
7196 /* Define a whole list of registers */
7197 const ARMCPRegInfo *r;
7198 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
7199 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
7204 * Modify ARMCPRegInfo for access from userspace.
7206 * This is a data driven modification directed by
7207 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
7208 * user-space cannot alter any values and dynamic values pertaining to
7209 * execution state are hidden from user space view anyway.
7211 void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
7213 const ARMCPRegUserSpaceInfo *m;
7214 ARMCPRegInfo *r;
7216 for (m = mods; m->name; m++) {
7217 GPatternSpec *pat = NULL;
7218 if (m->is_glob) {
7219 pat = g_pattern_spec_new(m->name);
7221 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
7222 if (pat && g_pattern_match_string(pat, r->name)) {
7223 r->type = ARM_CP_CONST;
7224 r->access = PL0U_R;
7225 r->resetvalue = 0;
7226 /* continue */
7227 } else if (strcmp(r->name, m->name) == 0) {
7228 r->type = ARM_CP_CONST;
7229 r->access = PL0U_R;
7230 r->resetvalue &= m->exported_bits;
7231 r->resetvalue |= m->fixed_bits;
7232 break;
7235 if (pat) {
7236 g_pattern_spec_free(pat);
7241 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
7243 return g_hash_table_lookup(cpregs, &encoded_cp);
7246 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
7247 uint64_t value)
7249 /* Helper coprocessor write function for write-ignore registers */
7252 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
7254 /* Helper coprocessor write function for read-as-zero registers */
7255 return 0;
7258 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
7260 /* Helper coprocessor reset function for do-nothing-on-reset registers */
7263 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
7265 /* Return true if it is not valid for us to switch to
7266 * this CPU mode (ie all the UNPREDICTABLE cases in
7267 * the ARM ARM CPSRWriteByInstr pseudocode).
7270 /* Changes to or from Hyp via MSR and CPS are illegal. */
7271 if (write_type == CPSRWriteByInstr &&
7272 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
7273 mode == ARM_CPU_MODE_HYP)) {
7274 return 1;
7277 switch (mode) {
7278 case ARM_CPU_MODE_USR:
7279 return 0;
7280 case ARM_CPU_MODE_SYS:
7281 case ARM_CPU_MODE_SVC:
7282 case ARM_CPU_MODE_ABT:
7283 case ARM_CPU_MODE_UND:
7284 case ARM_CPU_MODE_IRQ:
7285 case ARM_CPU_MODE_FIQ:
7286 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
7287 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
7289 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
7290 * and CPS are treated as illegal mode changes.
7292 if (write_type == CPSRWriteByInstr &&
7293 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7294 (arm_hcr_el2_eff(env) & HCR_TGE)) {
7295 return 1;
7297 return 0;
7298 case ARM_CPU_MODE_HYP:
7299 return !arm_feature(env, ARM_FEATURE_EL2)
7300 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env);
7301 case ARM_CPU_MODE_MON:
7302 return arm_current_el(env) < 3;
7303 default:
7304 return 1;
7308 uint32_t cpsr_read(CPUARMState *env)
7310 int ZF;
7311 ZF = (env->ZF == 0);
7312 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
7313 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
7314 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
7315 | ((env->condexec_bits & 0xfc) << 8)
7316 | (env->GE << 16) | (env->daif & CPSR_AIF);
7319 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
7320 CPSRWriteType write_type)
7322 uint32_t changed_daif;
7324 if (mask & CPSR_NZCV) {
7325 env->ZF = (~val) & CPSR_Z;
7326 env->NF = val;
7327 env->CF = (val >> 29) & 1;
7328 env->VF = (val << 3) & 0x80000000;
7330 if (mask & CPSR_Q)
7331 env->QF = ((val & CPSR_Q) != 0);
7332 if (mask & CPSR_T)
7333 env->thumb = ((val & CPSR_T) != 0);
7334 if (mask & CPSR_IT_0_1) {
7335 env->condexec_bits &= ~3;
7336 env->condexec_bits |= (val >> 25) & 3;
7338 if (mask & CPSR_IT_2_7) {
7339 env->condexec_bits &= 3;
7340 env->condexec_bits |= (val >> 8) & 0xfc;
7342 if (mask & CPSR_GE) {
7343 env->GE = (val >> 16) & 0xf;
7346 /* In a V7 implementation that includes the security extensions but does
7347 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
7348 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
7349 * bits respectively.
7351 * In a V8 implementation, it is permitted for privileged software to
7352 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
7354 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
7355 arm_feature(env, ARM_FEATURE_EL3) &&
7356 !arm_feature(env, ARM_FEATURE_EL2) &&
7357 !arm_is_secure(env)) {
7359 changed_daif = (env->daif ^ val) & mask;
7361 if (changed_daif & CPSR_A) {
7362 /* Check to see if we are allowed to change the masking of async
7363 * abort exceptions from a non-secure state.
7365 if (!(env->cp15.scr_el3 & SCR_AW)) {
7366 qemu_log_mask(LOG_GUEST_ERROR,
7367 "Ignoring attempt to switch CPSR_A flag from "
7368 "non-secure world with SCR.AW bit clear\n");
7369 mask &= ~CPSR_A;
7373 if (changed_daif & CPSR_F) {
7374 /* Check to see if we are allowed to change the masking of FIQ
7375 * exceptions from a non-secure state.
7377 if (!(env->cp15.scr_el3 & SCR_FW)) {
7378 qemu_log_mask(LOG_GUEST_ERROR,
7379 "Ignoring attempt to switch CPSR_F flag from "
7380 "non-secure world with SCR.FW bit clear\n");
7381 mask &= ~CPSR_F;
7384 /* Check whether non-maskable FIQ (NMFI) support is enabled.
7385 * If this bit is set software is not allowed to mask
7386 * FIQs, but is allowed to set CPSR_F to 0.
7388 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
7389 (val & CPSR_F)) {
7390 qemu_log_mask(LOG_GUEST_ERROR,
7391 "Ignoring attempt to enable CPSR_F flag "
7392 "(non-maskable FIQ [NMFI] support enabled)\n");
7393 mask &= ~CPSR_F;
7398 env->daif &= ~(CPSR_AIF & mask);
7399 env->daif |= val & CPSR_AIF & mask;
7401 if (write_type != CPSRWriteRaw &&
7402 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
7403 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
7404 /* Note that we can only get here in USR mode if this is a
7405 * gdb stub write; for this case we follow the architectural
7406 * behaviour for guest writes in USR mode of ignoring an attempt
7407 * to switch mode. (Those are caught by translate.c for writes
7408 * triggered by guest instructions.)
7410 mask &= ~CPSR_M;
7411 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
7412 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
7413 * v7, and has defined behaviour in v8:
7414 * + leave CPSR.M untouched
7415 * + allow changes to the other CPSR fields
7416 * + set PSTATE.IL
7417 * For user changes via the GDB stub, we don't set PSTATE.IL,
7418 * as this would be unnecessarily harsh for a user error.
7420 mask &= ~CPSR_M;
7421 if (write_type != CPSRWriteByGDBStub &&
7422 arm_feature(env, ARM_FEATURE_V8)) {
7423 mask |= CPSR_IL;
7424 val |= CPSR_IL;
7426 qemu_log_mask(LOG_GUEST_ERROR,
7427 "Illegal AArch32 mode switch attempt from %s to %s\n",
7428 aarch32_mode_name(env->uncached_cpsr),
7429 aarch32_mode_name(val));
7430 } else {
7431 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
7432 write_type == CPSRWriteExceptionReturn ?
7433 "Exception return from AArch32" :
7434 "AArch32 mode switch from",
7435 aarch32_mode_name(env->uncached_cpsr),
7436 aarch32_mode_name(val), env->regs[15]);
7437 switch_mode(env, val & CPSR_M);
7440 mask &= ~CACHED_CPSR_BITS;
7441 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
7444 /* Sign/zero extend */
7445 uint32_t HELPER(sxtb16)(uint32_t x)
7447 uint32_t res;
7448 res = (uint16_t)(int8_t)x;
7449 res |= (uint32_t)(int8_t)(x >> 16) << 16;
7450 return res;
7453 uint32_t HELPER(uxtb16)(uint32_t x)
7455 uint32_t res;
7456 res = (uint16_t)(uint8_t)x;
7457 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
7458 return res;
7461 int32_t HELPER(sdiv)(int32_t num, int32_t den)
7463 if (den == 0)
7464 return 0;
7465 if (num == INT_MIN && den == -1)
7466 return INT_MIN;
7467 return num / den;
7470 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
7472 if (den == 0)
7473 return 0;
7474 return num / den;
7477 uint32_t HELPER(rbit)(uint32_t x)
7479 return revbit32(x);
7482 #ifdef CONFIG_USER_ONLY
7484 /* These should probably raise undefined insn exceptions. */
7485 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
7487 ARMCPU *cpu = env_archcpu(env);
7489 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
7492 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
7494 ARMCPU *cpu = env_archcpu(env);
7496 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
7497 return 0;
7500 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
7502 /* translate.c should never generate calls here in user-only mode */
7503 g_assert_not_reached();
7506 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
7508 /* translate.c should never generate calls here in user-only mode */
7509 g_assert_not_reached();
7512 void HELPER(v7m_preserve_fp_state)(CPUARMState *env)
7514 /* translate.c should never generate calls here in user-only mode */
7515 g_assert_not_reached();
7518 void HELPER(v7m_vlstm)(CPUARMState *env, uint32_t fptr)
7520 /* translate.c should never generate calls here in user-only mode */
7521 g_assert_not_reached();
7524 void HELPER(v7m_vlldm)(CPUARMState *env, uint32_t fptr)
7526 /* translate.c should never generate calls here in user-only mode */
7527 g_assert_not_reached();
7530 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
7533 * The TT instructions can be used by unprivileged code, but in
7534 * user-only emulation we don't have the MPU.
7535 * Luckily since we know we are NonSecure unprivileged (and that in
7536 * turn means that the A flag wasn't specified), all the bits in the
7537 * register must be zero:
7538 * IREGION: 0 because IRVALID is 0
7539 * IRVALID: 0 because NS
7540 * S: 0 because NS
7541 * NSRW: 0 because NS
7542 * NSR: 0 because NS
7543 * RW: 0 because unpriv and A flag not set
7544 * R: 0 because unpriv and A flag not set
7545 * SRVALID: 0 because NS
7546 * MRVALID: 0 because unpriv and A flag not set
7547 * SREGION: 0 becaus SRVALID is 0
7548 * MREGION: 0 because MRVALID is 0
7550 return 0;
7553 static void switch_mode(CPUARMState *env, int mode)
7555 ARMCPU *cpu = env_archcpu(env);
7557 if (mode != ARM_CPU_MODE_USR) {
7558 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
7562 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
7563 uint32_t cur_el, bool secure)
7565 return 1;
7568 void aarch64_sync_64_to_32(CPUARMState *env)
7570 g_assert_not_reached();
7573 #else
7575 static void switch_mode(CPUARMState *env, int mode)
7577 int old_mode;
7578 int i;
7580 old_mode = env->uncached_cpsr & CPSR_M;
7581 if (mode == old_mode)
7582 return;
7584 if (old_mode == ARM_CPU_MODE_FIQ) {
7585 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
7586 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
7587 } else if (mode == ARM_CPU_MODE_FIQ) {
7588 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
7589 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
7592 i = bank_number(old_mode);
7593 env->banked_r13[i] = env->regs[13];
7594 env->banked_spsr[i] = env->spsr;
7596 i = bank_number(mode);
7597 env->regs[13] = env->banked_r13[i];
7598 env->spsr = env->banked_spsr[i];
7600 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
7601 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
7604 /* Physical Interrupt Target EL Lookup Table
7606 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
7608 * The below multi-dimensional table is used for looking up the target
7609 * exception level given numerous condition criteria. Specifically, the
7610 * target EL is based on SCR and HCR routing controls as well as the
7611 * currently executing EL and secure state.
7613 * Dimensions:
7614 * target_el_table[2][2][2][2][2][4]
7615 * | | | | | +--- Current EL
7616 * | | | | +------ Non-secure(0)/Secure(1)
7617 * | | | +--------- HCR mask override
7618 * | | +------------ SCR exec state control
7619 * | +--------------- SCR mask override
7620 * +------------------ 32-bit(0)/64-bit(1) EL3
7622 * The table values are as such:
7623 * 0-3 = EL0-EL3
7624 * -1 = Cannot occur
7626 * The ARM ARM target EL table includes entries indicating that an "exception
7627 * is not taken". The two cases where this is applicable are:
7628 * 1) An exception is taken from EL3 but the SCR does not have the exception
7629 * routed to EL3.
7630 * 2) An exception is taken from EL2 but the HCR does not have the exception
7631 * routed to EL2.
7632 * In these two cases, the below table contain a target of EL1. This value is
7633 * returned as it is expected that the consumer of the table data will check
7634 * for "target EL >= current EL" to ensure the exception is not taken.
7636 * SCR HCR
7637 * 64 EA AMO From
7638 * BIT IRQ IMO Non-secure Secure
7639 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
7641 static const int8_t target_el_table[2][2][2][2][2][4] = {
7642 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
7643 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
7644 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
7645 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
7646 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
7647 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
7648 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
7649 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
7650 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
7651 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
7652 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
7653 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
7654 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
7655 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
7656 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
7657 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
7661 * Determine the target EL for physical exceptions
7663 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
7664 uint32_t cur_el, bool secure)
7666 CPUARMState *env = cs->env_ptr;
7667 bool rw;
7668 bool scr;
7669 bool hcr;
7670 int target_el;
7671 /* Is the highest EL AArch64? */
7672 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
7673 uint64_t hcr_el2;
7675 if (arm_feature(env, ARM_FEATURE_EL3)) {
7676 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
7677 } else {
7678 /* Either EL2 is the highest EL (and so the EL2 register width
7679 * is given by is64); or there is no EL2 or EL3, in which case
7680 * the value of 'rw' does not affect the table lookup anyway.
7682 rw = is64;
7685 hcr_el2 = arm_hcr_el2_eff(env);
7686 switch (excp_idx) {
7687 case EXCP_IRQ:
7688 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
7689 hcr = hcr_el2 & HCR_IMO;
7690 break;
7691 case EXCP_FIQ:
7692 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
7693 hcr = hcr_el2 & HCR_FMO;
7694 break;
7695 default:
7696 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
7697 hcr = hcr_el2 & HCR_AMO;
7698 break;
7701 /* Perform a table-lookup for the target EL given the current state */
7702 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
7704 assert(target_el > 0);
7706 return target_el;
7710 * Return true if the v7M CPACR permits access to the FPU for the specified
7711 * security state and privilege level.
7713 static bool v7m_cpacr_pass(CPUARMState *env, bool is_secure, bool is_priv)
7715 switch (extract32(env->v7m.cpacr[is_secure], 20, 2)) {
7716 case 0:
7717 case 2: /* UNPREDICTABLE: we treat like 0 */
7718 return false;
7719 case 1:
7720 return is_priv;
7721 case 3:
7722 return true;
7723 default:
7724 g_assert_not_reached();
7729 * What kind of stack write are we doing? This affects how exceptions
7730 * generated during the stacking are treated.
7732 typedef enum StackingMode {
7733 STACK_NORMAL,
7734 STACK_IGNFAULTS,
7735 STACK_LAZYFP,
7736 } StackingMode;
7738 static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value,
7739 ARMMMUIdx mmu_idx, StackingMode mode)
7741 CPUState *cs = CPU(cpu);
7742 CPUARMState *env = &cpu->env;
7743 MemTxAttrs attrs = {};
7744 MemTxResult txres;
7745 target_ulong page_size;
7746 hwaddr physaddr;
7747 int prot;
7748 ARMMMUFaultInfo fi = {};
7749 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
7750 int exc;
7751 bool exc_secure;
7753 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr,
7754 &attrs, &prot, &page_size, &fi, NULL)) {
7755 /* MPU/SAU lookup failed */
7756 if (fi.type == ARMFault_QEMU_SFault) {
7757 if (mode == STACK_LAZYFP) {
7758 qemu_log_mask(CPU_LOG_INT,
7759 "...SecureFault with SFSR.LSPERR "
7760 "during lazy stacking\n");
7761 env->v7m.sfsr |= R_V7M_SFSR_LSPERR_MASK;
7762 } else {
7763 qemu_log_mask(CPU_LOG_INT,
7764 "...SecureFault with SFSR.AUVIOL "
7765 "during stacking\n");
7766 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
7768 env->v7m.sfsr |= R_V7M_SFSR_SFARVALID_MASK;
7769 env->v7m.sfar = addr;
7770 exc = ARMV7M_EXCP_SECURE;
7771 exc_secure = false;
7772 } else {
7773 if (mode == STACK_LAZYFP) {
7774 qemu_log_mask(CPU_LOG_INT,
7775 "...MemManageFault with CFSR.MLSPERR\n");
7776 env->v7m.cfsr[secure] |= R_V7M_CFSR_MLSPERR_MASK;
7777 } else {
7778 qemu_log_mask(CPU_LOG_INT,
7779 "...MemManageFault with CFSR.MSTKERR\n");
7780 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK;
7782 exc = ARMV7M_EXCP_MEM;
7783 exc_secure = secure;
7785 goto pend_fault;
7787 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value,
7788 attrs, &txres);
7789 if (txres != MEMTX_OK) {
7790 /* BusFault trying to write the data */
7791 if (mode == STACK_LAZYFP) {
7792 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.LSPERR\n");
7793 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_LSPERR_MASK;
7794 } else {
7795 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n");
7796 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK;
7798 exc = ARMV7M_EXCP_BUS;
7799 exc_secure = false;
7800 goto pend_fault;
7802 return true;
7804 pend_fault:
7806 * By pending the exception at this point we are making
7807 * the IMPDEF choice "overridden exceptions pended" (see the
7808 * MergeExcInfo() pseudocode). The other choice would be to not
7809 * pend them now and then make a choice about which to throw away
7810 * later if we have two derived exceptions.
7811 * The only case when we must not pend the exception but instead
7812 * throw it away is if we are doing the push of the callee registers
7813 * and we've already generated a derived exception (this is indicated
7814 * by the caller passing STACK_IGNFAULTS). Even in this case we will
7815 * still update the fault status registers.
7817 switch (mode) {
7818 case STACK_NORMAL:
7819 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure);
7820 break;
7821 case STACK_LAZYFP:
7822 armv7m_nvic_set_pending_lazyfp(env->nvic, exc, exc_secure);
7823 break;
7824 case STACK_IGNFAULTS:
7825 break;
7827 return false;
7830 static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr,
7831 ARMMMUIdx mmu_idx)
7833 CPUState *cs = CPU(cpu);
7834 CPUARMState *env = &cpu->env;
7835 MemTxAttrs attrs = {};
7836 MemTxResult txres;
7837 target_ulong page_size;
7838 hwaddr physaddr;
7839 int prot;
7840 ARMMMUFaultInfo fi = {};
7841 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
7842 int exc;
7843 bool exc_secure;
7844 uint32_t value;
7846 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr,
7847 &attrs, &prot, &page_size, &fi, NULL)) {
7848 /* MPU/SAU lookup failed */
7849 if (fi.type == ARMFault_QEMU_SFault) {
7850 qemu_log_mask(CPU_LOG_INT,
7851 "...SecureFault with SFSR.AUVIOL during unstack\n");
7852 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
7853 env->v7m.sfar = addr;
7854 exc = ARMV7M_EXCP_SECURE;
7855 exc_secure = false;
7856 } else {
7857 qemu_log_mask(CPU_LOG_INT,
7858 "...MemManageFault with CFSR.MUNSTKERR\n");
7859 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK;
7860 exc = ARMV7M_EXCP_MEM;
7861 exc_secure = secure;
7863 goto pend_fault;
7866 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr,
7867 attrs, &txres);
7868 if (txres != MEMTX_OK) {
7869 /* BusFault trying to read the data */
7870 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n");
7871 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK;
7872 exc = ARMV7M_EXCP_BUS;
7873 exc_secure = false;
7874 goto pend_fault;
7877 *dest = value;
7878 return true;
7880 pend_fault:
7882 * By pending the exception at this point we are making
7883 * the IMPDEF choice "overridden exceptions pended" (see the
7884 * MergeExcInfo() pseudocode). The other choice would be to not
7885 * pend them now and then make a choice about which to throw away
7886 * later if we have two derived exceptions.
7888 armv7m_nvic_set_pending(env->nvic, exc, exc_secure);
7889 return false;
7892 void HELPER(v7m_preserve_fp_state)(CPUARMState *env)
7895 * Preserve FP state (because LSPACT was set and we are about
7896 * to execute an FP instruction). This corresponds to the
7897 * PreserveFPState() pseudocode.
7898 * We may throw an exception if the stacking fails.
7900 ARMCPU *cpu = env_archcpu(env);
7901 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
7902 bool negpri = !(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_HFRDY_MASK);
7903 bool is_priv = !(env->v7m.fpccr[is_secure] & R_V7M_FPCCR_USER_MASK);
7904 bool splimviol = env->v7m.fpccr[is_secure] & R_V7M_FPCCR_SPLIMVIOL_MASK;
7905 uint32_t fpcar = env->v7m.fpcar[is_secure];
7906 bool stacked_ok = true;
7907 bool ts = is_secure && (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK);
7908 bool take_exception;
7910 /* Take the iothread lock as we are going to touch the NVIC */
7911 qemu_mutex_lock_iothread();
7913 /* Check the background context had access to the FPU */
7914 if (!v7m_cpacr_pass(env, is_secure, is_priv)) {
7915 armv7m_nvic_set_pending_lazyfp(env->nvic, ARMV7M_EXCP_USAGE, is_secure);
7916 env->v7m.cfsr[is_secure] |= R_V7M_CFSR_NOCP_MASK;
7917 stacked_ok = false;
7918 } else if (!is_secure && !extract32(env->v7m.nsacr, 10, 1)) {
7919 armv7m_nvic_set_pending_lazyfp(env->nvic, ARMV7M_EXCP_USAGE, M_REG_S);
7920 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK;
7921 stacked_ok = false;
7924 if (!splimviol && stacked_ok) {
7925 /* We only stack if the stack limit wasn't violated */
7926 int i;
7927 ARMMMUIdx mmu_idx;
7929 mmu_idx = arm_v7m_mmu_idx_all(env, is_secure, is_priv, negpri);
7930 for (i = 0; i < (ts ? 32 : 16); i += 2) {
7931 uint64_t dn = *aa32_vfp_dreg(env, i / 2);
7932 uint32_t faddr = fpcar + 4 * i;
7933 uint32_t slo = extract64(dn, 0, 32);
7934 uint32_t shi = extract64(dn, 32, 32);
7936 if (i >= 16) {
7937 faddr += 8; /* skip the slot for the FPSCR */
7939 stacked_ok = stacked_ok &&
7940 v7m_stack_write(cpu, faddr, slo, mmu_idx, STACK_LAZYFP) &&
7941 v7m_stack_write(cpu, faddr + 4, shi, mmu_idx, STACK_LAZYFP);
7944 stacked_ok = stacked_ok &&
7945 v7m_stack_write(cpu, fpcar + 0x40,
7946 vfp_get_fpscr(env), mmu_idx, STACK_LAZYFP);
7950 * We definitely pended an exception, but it's possible that it
7951 * might not be able to be taken now. If its priority permits us
7952 * to take it now, then we must not update the LSPACT or FP regs,
7953 * but instead jump out to take the exception immediately.
7954 * If it's just pending and won't be taken until the current
7955 * handler exits, then we do update LSPACT and the FP regs.
7957 take_exception = !stacked_ok &&
7958 armv7m_nvic_can_take_pending_exception(env->nvic);
7960 qemu_mutex_unlock_iothread();
7962 if (take_exception) {
7963 raise_exception_ra(env, EXCP_LAZYFP, 0, 1, GETPC());
7966 env->v7m.fpccr[is_secure] &= ~R_V7M_FPCCR_LSPACT_MASK;
7968 if (ts) {
7969 /* Clear s0 to s31 and the FPSCR */
7970 int i;
7972 for (i = 0; i < 32; i += 2) {
7973 *aa32_vfp_dreg(env, i / 2) = 0;
7975 vfp_set_fpscr(env, 0);
7978 * Otherwise s0 to s15 and FPSCR are UNKNOWN; we choose to leave them
7979 * unchanged.
7984 * Write to v7M CONTROL.SPSEL bit for the specified security bank.
7985 * This may change the current stack pointer between Main and Process
7986 * stack pointers if it is done for the CONTROL register for the current
7987 * security state.
7989 static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
7990 bool new_spsel,
7991 bool secstate)
7993 bool old_is_psp = v7m_using_psp(env);
7995 env->v7m.control[secstate] =
7996 deposit32(env->v7m.control[secstate],
7997 R_V7M_CONTROL_SPSEL_SHIFT,
7998 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
8000 if (secstate == env->v7m.secure) {
8001 bool new_is_psp = v7m_using_psp(env);
8002 uint32_t tmp;
8004 if (old_is_psp != new_is_psp) {
8005 tmp = env->v7m.other_sp;
8006 env->v7m.other_sp = env->regs[13];
8007 env->regs[13] = tmp;
8013 * Write to v7M CONTROL.SPSEL bit. This may change the current
8014 * stack pointer between Main and Process stack pointers.
8016 static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
8018 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
8021 void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
8024 * Write a new value to v7m.exception, thus transitioning into or out
8025 * of Handler mode; this may result in a change of active stack pointer.
8027 bool new_is_psp, old_is_psp = v7m_using_psp(env);
8028 uint32_t tmp;
8030 env->v7m.exception = new_exc;
8032 new_is_psp = v7m_using_psp(env);
8034 if (old_is_psp != new_is_psp) {
8035 tmp = env->v7m.other_sp;
8036 env->v7m.other_sp = env->regs[13];
8037 env->regs[13] = tmp;
8041 /* Switch M profile security state between NS and S */
8042 static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
8044 uint32_t new_ss_msp, new_ss_psp;
8046 if (env->v7m.secure == new_secstate) {
8047 return;
8051 * All the banked state is accessed by looking at env->v7m.secure
8052 * except for the stack pointer; rearrange the SP appropriately.
8054 new_ss_msp = env->v7m.other_ss_msp;
8055 new_ss_psp = env->v7m.other_ss_psp;
8057 if (v7m_using_psp(env)) {
8058 env->v7m.other_ss_psp = env->regs[13];
8059 env->v7m.other_ss_msp = env->v7m.other_sp;
8060 } else {
8061 env->v7m.other_ss_msp = env->regs[13];
8062 env->v7m.other_ss_psp = env->v7m.other_sp;
8065 env->v7m.secure = new_secstate;
8067 if (v7m_using_psp(env)) {
8068 env->regs[13] = new_ss_psp;
8069 env->v7m.other_sp = new_ss_msp;
8070 } else {
8071 env->regs[13] = new_ss_msp;
8072 env->v7m.other_sp = new_ss_psp;
8076 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
8079 * Handle v7M BXNS:
8080 * - if the return value is a magic value, do exception return (like BX)
8081 * - otherwise bit 0 of the return value is the target security state
8083 uint32_t min_magic;
8085 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8086 /* Covers FNC_RETURN and EXC_RETURN magic */
8087 min_magic = FNC_RETURN_MIN_MAGIC;
8088 } else {
8089 /* EXC_RETURN magic only */
8090 min_magic = EXC_RETURN_MIN_MAGIC;
8093 if (dest >= min_magic) {
8095 * This is an exception return magic value; put it where
8096 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
8097 * Note that if we ever add gen_ss_advance() singlestep support to
8098 * M profile this should count as an "instruction execution complete"
8099 * event (compare gen_bx_excret_final_code()).
8101 env->regs[15] = dest & ~1;
8102 env->thumb = dest & 1;
8103 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
8104 /* notreached */
8107 /* translate.c should have made BXNS UNDEF unless we're secure */
8108 assert(env->v7m.secure);
8110 if (!(dest & 1)) {
8111 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
8113 switch_v7m_security_state(env, dest & 1);
8114 env->thumb = 1;
8115 env->regs[15] = dest & ~1;
8118 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
8121 * Handle v7M BLXNS:
8122 * - bit 0 of the destination address is the target security state
8125 /* At this point regs[15] is the address just after the BLXNS */
8126 uint32_t nextinst = env->regs[15] | 1;
8127 uint32_t sp = env->regs[13] - 8;
8128 uint32_t saved_psr;
8130 /* translate.c will have made BLXNS UNDEF unless we're secure */
8131 assert(env->v7m.secure);
8133 if (dest & 1) {
8135 * Target is Secure, so this is just a normal BLX,
8136 * except that the low bit doesn't indicate Thumb/not.
8138 env->regs[14] = nextinst;
8139 env->thumb = 1;
8140 env->regs[15] = dest & ~1;
8141 return;
8144 /* Target is non-secure: first push a stack frame */
8145 if (!QEMU_IS_ALIGNED(sp, 8)) {
8146 qemu_log_mask(LOG_GUEST_ERROR,
8147 "BLXNS with misaligned SP is UNPREDICTABLE\n");
8150 if (sp < v7m_sp_limit(env)) {
8151 raise_exception(env, EXCP_STKOF, 0, 1);
8154 saved_psr = env->v7m.exception;
8155 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
8156 saved_psr |= XPSR_SFPA;
8159 /* Note that these stores can throw exceptions on MPU faults */
8160 cpu_stl_data(env, sp, nextinst);
8161 cpu_stl_data(env, sp + 4, saved_psr);
8163 env->regs[13] = sp;
8164 env->regs[14] = 0xfeffffff;
8165 if (arm_v7m_is_handler_mode(env)) {
8167 * Write a dummy value to IPSR, to avoid leaking the current secure
8168 * exception number to non-secure code. This is guaranteed not
8169 * to cause write_v7m_exception() to actually change stacks.
8171 write_v7m_exception(env, 1);
8173 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
8174 switch_v7m_security_state(env, 0);
8175 env->thumb = 1;
8176 env->regs[15] = dest;
8179 static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
8180 bool spsel)
8183 * Return a pointer to the location where we currently store the
8184 * stack pointer for the requested security state and thread mode.
8185 * This pointer will become invalid if the CPU state is updated
8186 * such that the stack pointers are switched around (eg changing
8187 * the SPSEL control bit).
8188 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
8189 * Unlike that pseudocode, we require the caller to pass us in the
8190 * SPSEL control bit value; this is because we also use this
8191 * function in handling of pushing of the callee-saves registers
8192 * part of the v8M stack frame (pseudocode PushCalleeStack()),
8193 * and in the tailchain codepath the SPSEL bit comes from the exception
8194 * return magic LR value from the previous exception. The pseudocode
8195 * opencodes the stack-selection in PushCalleeStack(), but we prefer
8196 * to make this utility function generic enough to do the job.
8198 bool want_psp = threadmode && spsel;
8200 if (secure == env->v7m.secure) {
8201 if (want_psp == v7m_using_psp(env)) {
8202 return &env->regs[13];
8203 } else {
8204 return &env->v7m.other_sp;
8206 } else {
8207 if (want_psp) {
8208 return &env->v7m.other_ss_psp;
8209 } else {
8210 return &env->v7m.other_ss_msp;
8215 static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure,
8216 uint32_t *pvec)
8218 CPUState *cs = CPU(cpu);
8219 CPUARMState *env = &cpu->env;
8220 MemTxResult result;
8221 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4;
8222 uint32_t vector_entry;
8223 MemTxAttrs attrs = {};
8224 ARMMMUIdx mmu_idx;
8225 bool exc_secure;
8227 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true);
8230 * We don't do a get_phys_addr() here because the rules for vector
8231 * loads are special: they always use the default memory map, and
8232 * the default memory map permits reads from all addresses.
8233 * Since there's no easy way to pass through to pmsav8_mpu_lookup()
8234 * that we want this special case which would always say "yes",
8235 * we just do the SAU lookup here followed by a direct physical load.
8237 attrs.secure = targets_secure;
8238 attrs.user = false;
8240 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8241 V8M_SAttributes sattrs = {};
8243 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
8244 if (sattrs.ns) {
8245 attrs.secure = false;
8246 } else if (!targets_secure) {
8247 /* NS access to S memory */
8248 goto load_fail;
8252 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr,
8253 attrs, &result);
8254 if (result != MEMTX_OK) {
8255 goto load_fail;
8257 *pvec = vector_entry;
8258 return true;
8260 load_fail:
8262 * All vector table fetch fails are reported as HardFault, with
8263 * HFSR.VECTTBL and .FORCED set. (FORCED is set because
8264 * technically the underlying exception is a MemManage or BusFault
8265 * that is escalated to HardFault.) This is a terminal exception,
8266 * so we will either take the HardFault immediately or else enter
8267 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()).
8269 exc_secure = targets_secure ||
8270 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
8271 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK;
8272 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure);
8273 return false;
8276 static uint32_t v7m_integrity_sig(CPUARMState *env, uint32_t lr)
8279 * Return the integrity signature value for the callee-saves
8280 * stack frame section. @lr is the exception return payload/LR value
8281 * whose FType bit forms bit 0 of the signature if FP is present.
8283 uint32_t sig = 0xfefa125a;
8285 if (!arm_feature(env, ARM_FEATURE_VFP) || (lr & R_V7M_EXCRET_FTYPE_MASK)) {
8286 sig |= 1;
8288 return sig;
8291 static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain,
8292 bool ignore_faults)
8295 * For v8M, push the callee-saves register part of the stack frame.
8296 * Compare the v8M pseudocode PushCalleeStack().
8297 * In the tailchaining case this may not be the current stack.
8299 CPUARMState *env = &cpu->env;
8300 uint32_t *frame_sp_p;
8301 uint32_t frameptr;
8302 ARMMMUIdx mmu_idx;
8303 bool stacked_ok;
8304 uint32_t limit;
8305 bool want_psp;
8306 uint32_t sig;
8307 StackingMode smode = ignore_faults ? STACK_IGNFAULTS : STACK_NORMAL;
8309 if (dotailchain) {
8310 bool mode = lr & R_V7M_EXCRET_MODE_MASK;
8311 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) ||
8312 !mode;
8314 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv);
8315 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode,
8316 lr & R_V7M_EXCRET_SPSEL_MASK);
8317 want_psp = mode && (lr & R_V7M_EXCRET_SPSEL_MASK);
8318 if (want_psp) {
8319 limit = env->v7m.psplim[M_REG_S];
8320 } else {
8321 limit = env->v7m.msplim[M_REG_S];
8323 } else {
8324 mmu_idx = arm_mmu_idx(env);
8325 frame_sp_p = &env->regs[13];
8326 limit = v7m_sp_limit(env);
8329 frameptr = *frame_sp_p - 0x28;
8330 if (frameptr < limit) {
8332 * Stack limit failure: set SP to the limit value, and generate
8333 * STKOF UsageFault. Stack pushes below the limit must not be
8334 * performed. It is IMPDEF whether pushes above the limit are
8335 * performed; we choose not to.
8337 qemu_log_mask(CPU_LOG_INT,
8338 "...STKOF during callee-saves register stacking\n");
8339 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK;
8340 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
8341 env->v7m.secure);
8342 *frame_sp_p = limit;
8343 return true;
8347 * Write as much of the stack frame as we can. A write failure may
8348 * cause us to pend a derived exception.
8350 sig = v7m_integrity_sig(env, lr);
8351 stacked_ok =
8352 v7m_stack_write(cpu, frameptr, sig, mmu_idx, smode) &&
8353 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx, smode) &&
8354 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx, smode) &&
8355 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx, smode) &&
8356 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx, smode) &&
8357 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx, smode) &&
8358 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx, smode) &&
8359 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx, smode) &&
8360 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx, smode);
8362 /* Update SP regardless of whether any of the stack accesses failed. */
8363 *frame_sp_p = frameptr;
8365 return !stacked_ok;
8368 static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain,
8369 bool ignore_stackfaults)
8372 * Do the "take the exception" parts of exception entry,
8373 * but not the pushing of state to the stack. This is
8374 * similar to the pseudocode ExceptionTaken() function.
8376 CPUARMState *env = &cpu->env;
8377 uint32_t addr;
8378 bool targets_secure;
8379 int exc;
8380 bool push_failed = false;
8382 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure);
8383 qemu_log_mask(CPU_LOG_INT, "...taking pending %s exception %d\n",
8384 targets_secure ? "secure" : "nonsecure", exc);
8386 if (dotailchain) {
8387 /* Sanitize LR FType and PREFIX bits */
8388 if (!arm_feature(env, ARM_FEATURE_VFP)) {
8389 lr |= R_V7M_EXCRET_FTYPE_MASK;
8391 lr = deposit32(lr, 24, 8, 0xff);
8394 if (arm_feature(env, ARM_FEATURE_V8)) {
8395 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
8396 (lr & R_V7M_EXCRET_S_MASK)) {
8398 * The background code (the owner of the registers in the
8399 * exception frame) is Secure. This means it may either already
8400 * have or now needs to push callee-saves registers.
8402 if (targets_secure) {
8403 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
8405 * We took an exception from Secure to NonSecure
8406 * (which means the callee-saved registers got stacked)
8407 * and are now tailchaining to a Secure exception.
8408 * Clear DCRS so eventual return from this Secure
8409 * exception unstacks the callee-saved registers.
8411 lr &= ~R_V7M_EXCRET_DCRS_MASK;
8413 } else {
8415 * We're going to a non-secure exception; push the
8416 * callee-saves registers to the stack now, if they're
8417 * not already saved.
8419 if (lr & R_V7M_EXCRET_DCRS_MASK &&
8420 !(dotailchain && !(lr & R_V7M_EXCRET_ES_MASK))) {
8421 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain,
8422 ignore_stackfaults);
8424 lr |= R_V7M_EXCRET_DCRS_MASK;
8428 lr &= ~R_V7M_EXCRET_ES_MASK;
8429 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8430 lr |= R_V7M_EXCRET_ES_MASK;
8432 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
8433 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
8434 lr |= R_V7M_EXCRET_SPSEL_MASK;
8438 * Clear registers if necessary to prevent non-secure exception
8439 * code being able to see register values from secure code.
8440 * Where register values become architecturally UNKNOWN we leave
8441 * them with their previous values.
8443 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8444 if (!targets_secure) {
8446 * Always clear the caller-saved registers (they have been
8447 * pushed to the stack earlier in v7m_push_stack()).
8448 * Clear callee-saved registers if the background code is
8449 * Secure (in which case these regs were saved in
8450 * v7m_push_callee_stack()).
8452 int i;
8454 for (i = 0; i < 13; i++) {
8455 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
8456 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
8457 env->regs[i] = 0;
8460 /* Clear EAPSR */
8461 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
8466 if (push_failed && !ignore_stackfaults) {
8468 * Derived exception on callee-saves register stacking:
8469 * we might now want to take a different exception which
8470 * targets a different security state, so try again from the top.
8472 qemu_log_mask(CPU_LOG_INT,
8473 "...derived exception on callee-saves register stacking");
8474 v7m_exception_taken(cpu, lr, true, true);
8475 return;
8478 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) {
8479 /* Vector load failed: derived exception */
8480 qemu_log_mask(CPU_LOG_INT, "...derived exception on vector table load");
8481 v7m_exception_taken(cpu, lr, true, true);
8482 return;
8486 * Now we've done everything that might cause a derived exception
8487 * we can go ahead and activate whichever exception we're going to
8488 * take (which might now be the derived exception).
8490 armv7m_nvic_acknowledge_irq(env->nvic);
8492 /* Switch to target security state -- must do this before writing SPSEL */
8493 switch_v7m_security_state(env, targets_secure);
8494 write_v7m_control_spsel(env, 0);
8495 arm_clear_exclusive(env);
8496 /* Clear SFPA and FPCA (has no effect if no FPU) */
8497 env->v7m.control[M_REG_S] &=
8498 ~(R_V7M_CONTROL_FPCA_MASK | R_V7M_CONTROL_SFPA_MASK);
8499 /* Clear IT bits */
8500 env->condexec_bits = 0;
8501 env->regs[14] = lr;
8502 env->regs[15] = addr & 0xfffffffe;
8503 env->thumb = addr & 1;
8506 static void v7m_update_fpccr(CPUARMState *env, uint32_t frameptr,
8507 bool apply_splim)
8510 * Like the pseudocode UpdateFPCCR: save state in FPCAR and FPCCR
8511 * that we will need later in order to do lazy FP reg stacking.
8513 bool is_secure = env->v7m.secure;
8514 void *nvic = env->nvic;
8516 * Some bits are unbanked and live always in fpccr[M_REG_S]; some bits
8517 * are banked and we want to update the bit in the bank for the
8518 * current security state; and in one case we want to specifically
8519 * update the NS banked version of a bit even if we are secure.
8521 uint32_t *fpccr_s = &env->v7m.fpccr[M_REG_S];
8522 uint32_t *fpccr_ns = &env->v7m.fpccr[M_REG_NS];
8523 uint32_t *fpccr = &env->v7m.fpccr[is_secure];
8524 bool hfrdy, bfrdy, mmrdy, ns_ufrdy, s_ufrdy, sfrdy, monrdy;
8526 env->v7m.fpcar[is_secure] = frameptr & ~0x7;
8528 if (apply_splim && arm_feature(env, ARM_FEATURE_V8)) {
8529 bool splimviol;
8530 uint32_t splim = v7m_sp_limit(env);
8531 bool ign = armv7m_nvic_neg_prio_requested(nvic, is_secure) &&
8532 (env->v7m.ccr[is_secure] & R_V7M_CCR_STKOFHFNMIGN_MASK);
8534 splimviol = !ign && frameptr < splim;
8535 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, SPLIMVIOL, splimviol);
8538 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, LSPACT, 1);
8540 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, S, is_secure);
8542 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, USER, arm_current_el(env) == 0);
8544 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, THREAD,
8545 !arm_v7m_is_handler_mode(env));
8547 hfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_HARD, false);
8548 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, HFRDY, hfrdy);
8550 bfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_BUS, false);
8551 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, BFRDY, bfrdy);
8553 mmrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_MEM, is_secure);
8554 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, MMRDY, mmrdy);
8556 ns_ufrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_USAGE, false);
8557 *fpccr_ns = FIELD_DP32(*fpccr_ns, V7M_FPCCR, UFRDY, ns_ufrdy);
8559 monrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_DEBUG, false);
8560 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, MONRDY, monrdy);
8562 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8563 s_ufrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_USAGE, true);
8564 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, UFRDY, s_ufrdy);
8566 sfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_SECURE, false);
8567 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, SFRDY, sfrdy);
8571 void HELPER(v7m_vlstm)(CPUARMState *env, uint32_t fptr)
8573 /* fptr is the value of Rn, the frame pointer we store the FP regs to */
8574 bool s = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
8575 bool lspact = env->v7m.fpccr[s] & R_V7M_FPCCR_LSPACT_MASK;
8577 assert(env->v7m.secure);
8579 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) {
8580 return;
8583 /* Check access to the coprocessor is permitted */
8584 if (!v7m_cpacr_pass(env, true, arm_current_el(env) != 0)) {
8585 raise_exception_ra(env, EXCP_NOCP, 0, 1, GETPC());
8588 if (lspact) {
8589 /* LSPACT should not be active when there is active FP state */
8590 raise_exception_ra(env, EXCP_LSERR, 0, 1, GETPC());
8593 if (fptr & 7) {
8594 raise_exception_ra(env, EXCP_UNALIGNED, 0, 1, GETPC());
8598 * Note that we do not use v7m_stack_write() here, because the
8599 * accesses should not set the FSR bits for stacking errors if they
8600 * fail. (In pseudocode terms, they are AccType_NORMAL, not AccType_STACK
8601 * or AccType_LAZYFP). Faults in cpu_stl_data() will throw exceptions
8602 * and longjmp out.
8604 if (!(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPEN_MASK)) {
8605 bool ts = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK;
8606 int i;
8608 for (i = 0; i < (ts ? 32 : 16); i += 2) {
8609 uint64_t dn = *aa32_vfp_dreg(env, i / 2);
8610 uint32_t faddr = fptr + 4 * i;
8611 uint32_t slo = extract64(dn, 0, 32);
8612 uint32_t shi = extract64(dn, 32, 32);
8614 if (i >= 16) {
8615 faddr += 8; /* skip the slot for the FPSCR */
8617 cpu_stl_data(env, faddr, slo);
8618 cpu_stl_data(env, faddr + 4, shi);
8620 cpu_stl_data(env, fptr + 0x40, vfp_get_fpscr(env));
8623 * If TS is 0 then s0 to s15 and FPSCR are UNKNOWN; we choose to
8624 * leave them unchanged, matching our choice in v7m_preserve_fp_state.
8626 if (ts) {
8627 for (i = 0; i < 32; i += 2) {
8628 *aa32_vfp_dreg(env, i / 2) = 0;
8630 vfp_set_fpscr(env, 0);
8632 } else {
8633 v7m_update_fpccr(env, fptr, false);
8636 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK;
8639 void HELPER(v7m_vlldm)(CPUARMState *env, uint32_t fptr)
8641 /* fptr is the value of Rn, the frame pointer we load the FP regs from */
8642 assert(env->v7m.secure);
8644 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) {
8645 return;
8648 /* Check access to the coprocessor is permitted */
8649 if (!v7m_cpacr_pass(env, true, arm_current_el(env) != 0)) {
8650 raise_exception_ra(env, EXCP_NOCP, 0, 1, GETPC());
8653 if (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK) {
8654 /* State in FP is still valid */
8655 env->v7m.fpccr[M_REG_S] &= ~R_V7M_FPCCR_LSPACT_MASK;
8656 } else {
8657 bool ts = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK;
8658 int i;
8659 uint32_t fpscr;
8661 if (fptr & 7) {
8662 raise_exception_ra(env, EXCP_UNALIGNED, 0, 1, GETPC());
8665 for (i = 0; i < (ts ? 32 : 16); i += 2) {
8666 uint32_t slo, shi;
8667 uint64_t dn;
8668 uint32_t faddr = fptr + 4 * i;
8670 if (i >= 16) {
8671 faddr += 8; /* skip the slot for the FPSCR */
8674 slo = cpu_ldl_data(env, faddr);
8675 shi = cpu_ldl_data(env, faddr + 4);
8677 dn = (uint64_t) shi << 32 | slo;
8678 *aa32_vfp_dreg(env, i / 2) = dn;
8680 fpscr = cpu_ldl_data(env, fptr + 0x40);
8681 vfp_set_fpscr(env, fpscr);
8684 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK;
8687 static bool v7m_push_stack(ARMCPU *cpu)
8690 * Do the "set up stack frame" part of exception entry,
8691 * similar to pseudocode PushStack().
8692 * Return true if we generate a derived exception (and so
8693 * should ignore further stack faults trying to process
8694 * that derived exception.)
8696 bool stacked_ok = true, limitviol = false;
8697 CPUARMState *env = &cpu->env;
8698 uint32_t xpsr = xpsr_read(env);
8699 uint32_t frameptr = env->regs[13];
8700 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
8701 uint32_t framesize;
8702 bool nsacr_cp10 = extract32(env->v7m.nsacr, 10, 1);
8704 if ((env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) &&
8705 (env->v7m.secure || nsacr_cp10)) {
8706 if (env->v7m.secure &&
8707 env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK) {
8708 framesize = 0xa8;
8709 } else {
8710 framesize = 0x68;
8712 } else {
8713 framesize = 0x20;
8716 /* Align stack pointer if the guest wants that */
8717 if ((frameptr & 4) &&
8718 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
8719 frameptr -= 4;
8720 xpsr |= XPSR_SPREALIGN;
8723 xpsr &= ~XPSR_SFPA;
8724 if (env->v7m.secure &&
8725 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) {
8726 xpsr |= XPSR_SFPA;
8729 frameptr -= framesize;
8731 if (arm_feature(env, ARM_FEATURE_V8)) {
8732 uint32_t limit = v7m_sp_limit(env);
8734 if (frameptr < limit) {
8736 * Stack limit failure: set SP to the limit value, and generate
8737 * STKOF UsageFault. Stack pushes below the limit must not be
8738 * performed. It is IMPDEF whether pushes above the limit are
8739 * performed; we choose not to.
8741 qemu_log_mask(CPU_LOG_INT,
8742 "...STKOF during stacking\n");
8743 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK;
8744 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
8745 env->v7m.secure);
8746 env->regs[13] = limit;
8748 * We won't try to perform any further memory accesses but
8749 * we must continue through the following code to check for
8750 * permission faults during FPU state preservation, and we
8751 * must update FPCCR if lazy stacking is enabled.
8753 limitviol = true;
8754 stacked_ok = false;
8759 * Write as much of the stack frame as we can. If we fail a stack
8760 * write this will result in a derived exception being pended
8761 * (which may be taken in preference to the one we started with
8762 * if it has higher priority).
8764 stacked_ok = stacked_ok &&
8765 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, STACK_NORMAL) &&
8766 v7m_stack_write(cpu, frameptr + 4, env->regs[1],
8767 mmu_idx, STACK_NORMAL) &&
8768 v7m_stack_write(cpu, frameptr + 8, env->regs[2],
8769 mmu_idx, STACK_NORMAL) &&
8770 v7m_stack_write(cpu, frameptr + 12, env->regs[3],
8771 mmu_idx, STACK_NORMAL) &&
8772 v7m_stack_write(cpu, frameptr + 16, env->regs[12],
8773 mmu_idx, STACK_NORMAL) &&
8774 v7m_stack_write(cpu, frameptr + 20, env->regs[14],
8775 mmu_idx, STACK_NORMAL) &&
8776 v7m_stack_write(cpu, frameptr + 24, env->regs[15],
8777 mmu_idx, STACK_NORMAL) &&
8778 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, STACK_NORMAL);
8780 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) {
8781 /* FPU is active, try to save its registers */
8782 bool fpccr_s = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
8783 bool lspact = env->v7m.fpccr[fpccr_s] & R_V7M_FPCCR_LSPACT_MASK;
8785 if (lspact && arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8786 qemu_log_mask(CPU_LOG_INT,
8787 "...SecureFault because LSPACT and FPCA both set\n");
8788 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK;
8789 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
8790 } else if (!env->v7m.secure && !nsacr_cp10) {
8791 qemu_log_mask(CPU_LOG_INT,
8792 "...Secure UsageFault with CFSR.NOCP because "
8793 "NSACR.CP10 prevents stacking FP regs\n");
8794 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, M_REG_S);
8795 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK;
8796 } else {
8797 if (!(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPEN_MASK)) {
8798 /* Lazy stacking disabled, save registers now */
8799 int i;
8800 bool cpacr_pass = v7m_cpacr_pass(env, env->v7m.secure,
8801 arm_current_el(env) != 0);
8803 if (stacked_ok && !cpacr_pass) {
8805 * Take UsageFault if CPACR forbids access. The pseudocode
8806 * here does a full CheckCPEnabled() but we know the NSACR
8807 * check can never fail as we have already handled that.
8809 qemu_log_mask(CPU_LOG_INT,
8810 "...UsageFault with CFSR.NOCP because "
8811 "CPACR.CP10 prevents stacking FP regs\n");
8812 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
8813 env->v7m.secure);
8814 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
8815 stacked_ok = false;
8818 for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) {
8819 uint64_t dn = *aa32_vfp_dreg(env, i / 2);
8820 uint32_t faddr = frameptr + 0x20 + 4 * i;
8821 uint32_t slo = extract64(dn, 0, 32);
8822 uint32_t shi = extract64(dn, 32, 32);
8824 if (i >= 16) {
8825 faddr += 8; /* skip the slot for the FPSCR */
8827 stacked_ok = stacked_ok &&
8828 v7m_stack_write(cpu, faddr, slo,
8829 mmu_idx, STACK_NORMAL) &&
8830 v7m_stack_write(cpu, faddr + 4, shi,
8831 mmu_idx, STACK_NORMAL);
8833 stacked_ok = stacked_ok &&
8834 v7m_stack_write(cpu, frameptr + 0x60,
8835 vfp_get_fpscr(env), mmu_idx, STACK_NORMAL);
8836 if (cpacr_pass) {
8837 for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) {
8838 *aa32_vfp_dreg(env, i / 2) = 0;
8840 vfp_set_fpscr(env, 0);
8842 } else {
8843 /* Lazy stacking enabled, save necessary info to stack later */
8844 v7m_update_fpccr(env, frameptr + 0x20, true);
8850 * If we broke a stack limit then SP was already updated earlier;
8851 * otherwise we update SP regardless of whether any of the stack
8852 * accesses failed or we took some other kind of fault.
8854 if (!limitviol) {
8855 env->regs[13] = frameptr;
8858 return !stacked_ok;
8861 static void do_v7m_exception_exit(ARMCPU *cpu)
8863 CPUARMState *env = &cpu->env;
8864 uint32_t excret;
8865 uint32_t xpsr, xpsr_mask;
8866 bool ufault = false;
8867 bool sfault = false;
8868 bool return_to_sp_process;
8869 bool return_to_handler;
8870 bool rettobase = false;
8871 bool exc_secure = false;
8872 bool return_to_secure;
8873 bool ftype;
8874 bool restore_s16_s31;
8877 * If we're not in Handler mode then jumps to magic exception-exit
8878 * addresses don't have magic behaviour. However for the v8M
8879 * security extensions the magic secure-function-return has to
8880 * work in thread mode too, so to avoid doing an extra check in
8881 * the generated code we allow exception-exit magic to also cause the
8882 * internal exception and bring us here in thread mode. Correct code
8883 * will never try to do this (the following insn fetch will always
8884 * fault) so we the overhead of having taken an unnecessary exception
8885 * doesn't matter.
8887 if (!arm_v7m_is_handler_mode(env)) {
8888 return;
8892 * In the spec pseudocode ExceptionReturn() is called directly
8893 * from BXWritePC() and gets the full target PC value including
8894 * bit zero. In QEMU's implementation we treat it as a normal
8895 * jump-to-register (which is then caught later on), and so split
8896 * the target value up between env->regs[15] and env->thumb in
8897 * gen_bx(). Reconstitute it.
8899 excret = env->regs[15];
8900 if (env->thumb) {
8901 excret |= 1;
8904 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
8905 " previous exception %d\n",
8906 excret, env->v7m.exception);
8908 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
8909 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
8910 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
8911 excret);
8914 ftype = excret & R_V7M_EXCRET_FTYPE_MASK;
8916 if (!arm_feature(env, ARM_FEATURE_VFP) && !ftype) {
8917 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero FTYPE in exception "
8918 "exit PC value 0x%" PRIx32 " is UNPREDICTABLE "
8919 "if FPU not present\n",
8920 excret);
8921 ftype = true;
8924 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8926 * EXC_RETURN.ES validation check (R_SMFL). We must do this before
8927 * we pick which FAULTMASK to clear.
8929 if (!env->v7m.secure &&
8930 ((excret & R_V7M_EXCRET_ES_MASK) ||
8931 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
8932 sfault = 1;
8933 /* For all other purposes, treat ES as 0 (R_HXSR) */
8934 excret &= ~R_V7M_EXCRET_ES_MASK;
8936 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
8939 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
8941 * Auto-clear FAULTMASK on return from other than NMI.
8942 * If the security extension is implemented then this only
8943 * happens if the raw execution priority is >= 0; the
8944 * value of the ES bit in the exception return value indicates
8945 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
8947 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8948 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
8949 env->v7m.faultmask[exc_secure] = 0;
8951 } else {
8952 env->v7m.faultmask[M_REG_NS] = 0;
8956 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
8957 exc_secure)) {
8958 case -1:
8959 /* attempt to exit an exception that isn't active */
8960 ufault = true;
8961 break;
8962 case 0:
8963 /* still an irq active now */
8964 break;
8965 case 1:
8967 * We returned to base exception level, no nesting.
8968 * (In the pseudocode this is written using "NestedActivation != 1"
8969 * where we have 'rettobase == false'.)
8971 rettobase = true;
8972 break;
8973 default:
8974 g_assert_not_reached();
8977 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
8978 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
8979 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
8980 (excret & R_V7M_EXCRET_S_MASK);
8982 if (arm_feature(env, ARM_FEATURE_V8)) {
8983 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8985 * UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
8986 * we choose to take the UsageFault.
8988 if ((excret & R_V7M_EXCRET_S_MASK) ||
8989 (excret & R_V7M_EXCRET_ES_MASK) ||
8990 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
8991 ufault = true;
8994 if (excret & R_V7M_EXCRET_RES0_MASK) {
8995 ufault = true;
8997 } else {
8998 /* For v7M we only recognize certain combinations of the low bits */
8999 switch (excret & 0xf) {
9000 case 1: /* Return to Handler */
9001 break;
9002 case 13: /* Return to Thread using Process stack */
9003 case 9: /* Return to Thread using Main stack */
9005 * We only need to check NONBASETHRDENA for v7M, because in
9006 * v8M this bit does not exist (it is RES1).
9008 if (!rettobase &&
9009 !(env->v7m.ccr[env->v7m.secure] &
9010 R_V7M_CCR_NONBASETHRDENA_MASK)) {
9011 ufault = true;
9013 break;
9014 default:
9015 ufault = true;
9020 * Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
9021 * Handler mode (and will be until we write the new XPSR.Interrupt
9022 * field) this does not switch around the current stack pointer.
9023 * We must do this before we do any kind of tailchaining, including
9024 * for the derived exceptions on integrity check failures, or we will
9025 * give the guest an incorrect EXCRET.SPSEL value on exception entry.
9027 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
9030 * Clear scratch FP values left in caller saved registers; this
9031 * must happen before any kind of tail chaining.
9033 if ((env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_CLRONRET_MASK) &&
9034 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK)) {
9035 if (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK) {
9036 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK;
9037 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
9038 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
9039 "stackframe: error during lazy state deactivation\n");
9040 v7m_exception_taken(cpu, excret, true, false);
9041 return;
9042 } else {
9043 /* Clear s0..s15 and FPSCR */
9044 int i;
9046 for (i = 0; i < 16; i += 2) {
9047 *aa32_vfp_dreg(env, i / 2) = 0;
9049 vfp_set_fpscr(env, 0);
9053 if (sfault) {
9054 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
9055 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
9056 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
9057 "stackframe: failed EXC_RETURN.ES validity check\n");
9058 v7m_exception_taken(cpu, excret, true, false);
9059 return;
9062 if (ufault) {
9064 * Bad exception return: instead of popping the exception
9065 * stack, directly take a usage fault on the current stack.
9067 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
9068 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
9069 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
9070 "stackframe: failed exception return integrity check\n");
9071 v7m_exception_taken(cpu, excret, true, false);
9072 return;
9076 * Tailchaining: if there is currently a pending exception that
9077 * is high enough priority to preempt execution at the level we're
9078 * about to return to, then just directly take that exception now,
9079 * avoiding an unstack-and-then-stack. Note that now we have
9080 * deactivated the previous exception by calling armv7m_nvic_complete_irq()
9081 * our current execution priority is already the execution priority we are
9082 * returning to -- none of the state we would unstack or set based on
9083 * the EXCRET value affects it.
9085 if (armv7m_nvic_can_take_pending_exception(env->nvic)) {
9086 qemu_log_mask(CPU_LOG_INT, "...tailchaining to pending exception\n");
9087 v7m_exception_taken(cpu, excret, true, false);
9088 return;
9091 switch_v7m_security_state(env, return_to_secure);
9095 * The stack pointer we should be reading the exception frame from
9096 * depends on bits in the magic exception return type value (and
9097 * for v8M isn't necessarily the stack pointer we will eventually
9098 * end up resuming execution with). Get a pointer to the location
9099 * in the CPU state struct where the SP we need is currently being
9100 * stored; we will use and modify it in place.
9101 * We use this limited C variable scope so we don't accidentally
9102 * use 'frame_sp_p' after we do something that makes it invalid.
9104 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
9105 return_to_secure,
9106 !return_to_handler,
9107 return_to_sp_process);
9108 uint32_t frameptr = *frame_sp_p;
9109 bool pop_ok = true;
9110 ARMMMUIdx mmu_idx;
9111 bool return_to_priv = return_to_handler ||
9112 !(env->v7m.control[return_to_secure] & R_V7M_CONTROL_NPRIV_MASK);
9114 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure,
9115 return_to_priv);
9117 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
9118 arm_feature(env, ARM_FEATURE_V8)) {
9119 qemu_log_mask(LOG_GUEST_ERROR,
9120 "M profile exception return with non-8-aligned SP "
9121 "for destination state is UNPREDICTABLE\n");
9124 /* Do we need to pop callee-saved registers? */
9125 if (return_to_secure &&
9126 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
9127 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
9128 uint32_t actual_sig;
9130 pop_ok = v7m_stack_read(cpu, &actual_sig, frameptr, mmu_idx);
9132 if (pop_ok && v7m_integrity_sig(env, excret) != actual_sig) {
9133 /* Take a SecureFault on the current stack */
9134 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
9135 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
9136 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
9137 "stackframe: failed exception return integrity "
9138 "signature check\n");
9139 v7m_exception_taken(cpu, excret, true, false);
9140 return;
9143 pop_ok = pop_ok &&
9144 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
9145 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) &&
9146 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) &&
9147 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) &&
9148 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) &&
9149 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) &&
9150 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) &&
9151 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx);
9153 frameptr += 0x28;
9156 /* Pop registers */
9157 pop_ok = pop_ok &&
9158 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) &&
9159 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) &&
9160 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) &&
9161 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) &&
9162 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) &&
9163 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) &&
9164 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) &&
9165 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx);
9167 if (!pop_ok) {
9169 * v7m_stack_read() pended a fault, so take it (as a tail
9170 * chained exception on the same stack frame)
9172 qemu_log_mask(CPU_LOG_INT, "...derived exception on unstacking\n");
9173 v7m_exception_taken(cpu, excret, true, false);
9174 return;
9178 * Returning from an exception with a PC with bit 0 set is defined
9179 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
9180 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
9181 * the lsbit, and there are several RTOSes out there which incorrectly
9182 * assume the r15 in the stack frame should be a Thumb-style "lsbit
9183 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
9184 * complain about the badly behaved guest.
9186 if (env->regs[15] & 1) {
9187 env->regs[15] &= ~1U;
9188 if (!arm_feature(env, ARM_FEATURE_V8)) {
9189 qemu_log_mask(LOG_GUEST_ERROR,
9190 "M profile return from interrupt with misaligned "
9191 "PC is UNPREDICTABLE on v7M\n");
9195 if (arm_feature(env, ARM_FEATURE_V8)) {
9197 * For v8M we have to check whether the xPSR exception field
9198 * matches the EXCRET value for return to handler/thread
9199 * before we commit to changing the SP and xPSR.
9201 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
9202 if (return_to_handler != will_be_handler) {
9204 * Take an INVPC UsageFault on the current stack.
9205 * By this point we will have switched to the security state
9206 * for the background state, so this UsageFault will target
9207 * that state.
9209 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
9210 env->v7m.secure);
9211 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
9212 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
9213 "stackframe: failed exception return integrity "
9214 "check\n");
9215 v7m_exception_taken(cpu, excret, true, false);
9216 return;
9220 if (!ftype) {
9221 /* FP present and we need to handle it */
9222 if (!return_to_secure &&
9223 (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK)) {
9224 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
9225 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK;
9226 qemu_log_mask(CPU_LOG_INT,
9227 "...taking SecureFault on existing stackframe: "
9228 "Secure LSPACT set but exception return is "
9229 "not to secure state\n");
9230 v7m_exception_taken(cpu, excret, true, false);
9231 return;
9234 restore_s16_s31 = return_to_secure &&
9235 (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK);
9237 if (env->v7m.fpccr[return_to_secure] & R_V7M_FPCCR_LSPACT_MASK) {
9238 /* State in FPU is still valid, just clear LSPACT */
9239 env->v7m.fpccr[return_to_secure] &= ~R_V7M_FPCCR_LSPACT_MASK;
9240 } else {
9241 int i;
9242 uint32_t fpscr;
9243 bool cpacr_pass, nsacr_pass;
9245 cpacr_pass = v7m_cpacr_pass(env, return_to_secure,
9246 return_to_priv);
9247 nsacr_pass = return_to_secure ||
9248 extract32(env->v7m.nsacr, 10, 1);
9250 if (!cpacr_pass) {
9251 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
9252 return_to_secure);
9253 env->v7m.cfsr[return_to_secure] |= R_V7M_CFSR_NOCP_MASK;
9254 qemu_log_mask(CPU_LOG_INT,
9255 "...taking UsageFault on existing "
9256 "stackframe: CPACR.CP10 prevents unstacking "
9257 "FP regs\n");
9258 v7m_exception_taken(cpu, excret, true, false);
9259 return;
9260 } else if (!nsacr_pass) {
9261 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, true);
9262 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_INVPC_MASK;
9263 qemu_log_mask(CPU_LOG_INT,
9264 "...taking Secure UsageFault on existing "
9265 "stackframe: NSACR.CP10 prevents unstacking "
9266 "FP regs\n");
9267 v7m_exception_taken(cpu, excret, true, false);
9268 return;
9271 for (i = 0; i < (restore_s16_s31 ? 32 : 16); i += 2) {
9272 uint32_t slo, shi;
9273 uint64_t dn;
9274 uint32_t faddr = frameptr + 0x20 + 4 * i;
9276 if (i >= 16) {
9277 faddr += 8; /* Skip the slot for the FPSCR */
9280 pop_ok = pop_ok &&
9281 v7m_stack_read(cpu, &slo, faddr, mmu_idx) &&
9282 v7m_stack_read(cpu, &shi, faddr + 4, mmu_idx);
9284 if (!pop_ok) {
9285 break;
9288 dn = (uint64_t)shi << 32 | slo;
9289 *aa32_vfp_dreg(env, i / 2) = dn;
9291 pop_ok = pop_ok &&
9292 v7m_stack_read(cpu, &fpscr, frameptr + 0x60, mmu_idx);
9293 if (pop_ok) {
9294 vfp_set_fpscr(env, fpscr);
9296 if (!pop_ok) {
9298 * These regs are 0 if security extension present;
9299 * otherwise merely UNKNOWN. We zero always.
9301 for (i = 0; i < (restore_s16_s31 ? 32 : 16); i += 2) {
9302 *aa32_vfp_dreg(env, i / 2) = 0;
9304 vfp_set_fpscr(env, 0);
9308 env->v7m.control[M_REG_S] = FIELD_DP32(env->v7m.control[M_REG_S],
9309 V7M_CONTROL, FPCA, !ftype);
9311 /* Commit to consuming the stack frame */
9312 frameptr += 0x20;
9313 if (!ftype) {
9314 frameptr += 0x48;
9315 if (restore_s16_s31) {
9316 frameptr += 0x40;
9320 * Undo stack alignment (the SPREALIGN bit indicates that the original
9321 * pre-exception SP was not 8-aligned and we added a padding word to
9322 * align it, so we undo this by ORing in the bit that increases it
9323 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
9324 * would work too but a logical OR is how the pseudocode specifies it.)
9326 if (xpsr & XPSR_SPREALIGN) {
9327 frameptr |= 4;
9329 *frame_sp_p = frameptr;
9332 xpsr_mask = ~(XPSR_SPREALIGN | XPSR_SFPA);
9333 if (!arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
9334 xpsr_mask &= ~XPSR_GE;
9336 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
9337 xpsr_write(env, xpsr, xpsr_mask);
9339 if (env->v7m.secure) {
9340 bool sfpa = xpsr & XPSR_SFPA;
9342 env->v7m.control[M_REG_S] = FIELD_DP32(env->v7m.control[M_REG_S],
9343 V7M_CONTROL, SFPA, sfpa);
9347 * The restored xPSR exception field will be zero if we're
9348 * resuming in Thread mode. If that doesn't match what the
9349 * exception return excret specified then this is a UsageFault.
9350 * v7M requires we make this check here; v8M did it earlier.
9352 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
9354 * Take an INVPC UsageFault by pushing the stack again;
9355 * we know we're v7M so this is never a Secure UsageFault.
9357 bool ignore_stackfaults;
9359 assert(!arm_feature(env, ARM_FEATURE_V8));
9360 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
9361 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
9362 ignore_stackfaults = v7m_push_stack(cpu);
9363 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
9364 "failed exception return integrity check\n");
9365 v7m_exception_taken(cpu, excret, false, ignore_stackfaults);
9366 return;
9369 /* Otherwise, we have a successful exception exit. */
9370 arm_clear_exclusive(env);
9371 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
9374 static bool do_v7m_function_return(ARMCPU *cpu)
9377 * v8M security extensions magic function return.
9378 * We may either:
9379 * (1) throw an exception (longjump)
9380 * (2) return true if we successfully handled the function return
9381 * (3) return false if we failed a consistency check and have
9382 * pended a UsageFault that needs to be taken now
9384 * At this point the magic return value is split between env->regs[15]
9385 * and env->thumb. We don't bother to reconstitute it because we don't
9386 * need it (all values are handled the same way).
9388 CPUARMState *env = &cpu->env;
9389 uint32_t newpc, newpsr, newpsr_exc;
9391 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
9394 bool threadmode, spsel;
9395 TCGMemOpIdx oi;
9396 ARMMMUIdx mmu_idx;
9397 uint32_t *frame_sp_p;
9398 uint32_t frameptr;
9400 /* Pull the return address and IPSR from the Secure stack */
9401 threadmode = !arm_v7m_is_handler_mode(env);
9402 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
9404 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
9405 frameptr = *frame_sp_p;
9408 * These loads may throw an exception (for MPU faults). We want to
9409 * do them as secure, so work out what MMU index that is.
9411 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
9412 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx));
9413 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0);
9414 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0);
9416 /* Consistency checks on new IPSR */
9417 newpsr_exc = newpsr & XPSR_EXCP;
9418 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
9419 (env->v7m.exception == 1 && newpsr_exc != 0))) {
9420 /* Pend the fault and tell our caller to take it */
9421 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
9422 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
9423 env->v7m.secure);
9424 qemu_log_mask(CPU_LOG_INT,
9425 "...taking INVPC UsageFault: "
9426 "IPSR consistency check failed\n");
9427 return false;
9430 *frame_sp_p = frameptr + 8;
9433 /* This invalidates frame_sp_p */
9434 switch_v7m_security_state(env, true);
9435 env->v7m.exception = newpsr_exc;
9436 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
9437 if (newpsr & XPSR_SFPA) {
9438 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
9440 xpsr_write(env, 0, XPSR_IT);
9441 env->thumb = newpc & 1;
9442 env->regs[15] = newpc & ~1;
9444 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
9445 return true;
9448 static void arm_log_exception(int idx)
9450 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9451 const char *exc = NULL;
9452 static const char * const excnames[] = {
9453 [EXCP_UDEF] = "Undefined Instruction",
9454 [EXCP_SWI] = "SVC",
9455 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9456 [EXCP_DATA_ABORT] = "Data Abort",
9457 [EXCP_IRQ] = "IRQ",
9458 [EXCP_FIQ] = "FIQ",
9459 [EXCP_BKPT] = "Breakpoint",
9460 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9461 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9462 [EXCP_HVC] = "Hypervisor Call",
9463 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9464 [EXCP_SMC] = "Secure Monitor Call",
9465 [EXCP_VIRQ] = "Virtual IRQ",
9466 [EXCP_VFIQ] = "Virtual FIQ",
9467 [EXCP_SEMIHOST] = "Semihosting call",
9468 [EXCP_NOCP] = "v7M NOCP UsageFault",
9469 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9470 [EXCP_STKOF] = "v8M STKOF UsageFault",
9471 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9472 [EXCP_LSERR] = "v8M LSERR UsageFault",
9473 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9476 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9477 exc = excnames[idx];
9479 if (!exc) {
9480 exc = "unknown";
9482 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
9486 static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
9487 uint32_t addr, uint16_t *insn)
9490 * Load a 16-bit portion of a v7M instruction, returning true on success,
9491 * or false on failure (in which case we will have pended the appropriate
9492 * exception).
9493 * We need to do the instruction fetch's MPU and SAU checks
9494 * like this because there is no MMU index that would allow
9495 * doing the load with a single function call. Instead we must
9496 * first check that the security attributes permit the load
9497 * and that they don't mismatch on the two halves of the instruction,
9498 * and then we do the load as a secure load (ie using the security
9499 * attributes of the address, not the CPU, as architecturally required).
9501 CPUState *cs = CPU(cpu);
9502 CPUARMState *env = &cpu->env;
9503 V8M_SAttributes sattrs = {};
9504 MemTxAttrs attrs = {};
9505 ARMMMUFaultInfo fi = {};
9506 MemTxResult txres;
9507 target_ulong page_size;
9508 hwaddr physaddr;
9509 int prot;
9511 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
9512 if (!sattrs.nsc || sattrs.ns) {
9514 * This must be the second half of the insn, and it straddles a
9515 * region boundary with the second half not being S&NSC.
9517 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
9518 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
9519 qemu_log_mask(CPU_LOG_INT,
9520 "...really SecureFault with SFSR.INVEP\n");
9521 return false;
9523 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
9524 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) {
9525 /* the MPU lookup failed */
9526 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
9527 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
9528 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
9529 return false;
9531 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
9532 attrs, &txres);
9533 if (txres != MEMTX_OK) {
9534 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
9535 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
9536 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
9537 return false;
9539 return true;
9542 static bool v7m_handle_execute_nsc(ARMCPU *cpu)
9545 * Check whether this attempt to execute code in a Secure & NS-Callable
9546 * memory region is for an SG instruction; if so, then emulate the
9547 * effect of the SG instruction and return true. Otherwise pend
9548 * the correct kind of exception and return false.
9550 CPUARMState *env = &cpu->env;
9551 ARMMMUIdx mmu_idx;
9552 uint16_t insn;
9555 * We should never get here unless get_phys_addr_pmsav8() caused
9556 * an exception for NS executing in S&NSC memory.
9558 assert(!env->v7m.secure);
9559 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
9561 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
9562 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
9564 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
9565 return false;
9568 if (!env->thumb) {
9569 goto gen_invep;
9572 if (insn != 0xe97f) {
9574 * Not an SG instruction first half (we choose the IMPDEF
9575 * early-SG-check option).
9577 goto gen_invep;
9580 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
9581 return false;
9584 if (insn != 0xe97f) {
9586 * Not an SG instruction second half (yes, both halves of the SG
9587 * insn have the same hex value)
9589 goto gen_invep;
9593 * OK, we have confirmed that we really have an SG instruction.
9594 * We know we're NS in S memory so don't need to repeat those checks.
9596 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
9597 ", executing it\n", env->regs[15]);
9598 env->regs[14] &= ~1;
9599 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
9600 switch_v7m_security_state(env, true);
9601 xpsr_write(env, 0, XPSR_IT);
9602 env->regs[15] += 4;
9603 return true;
9605 gen_invep:
9606 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
9607 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
9608 qemu_log_mask(CPU_LOG_INT,
9609 "...really SecureFault with SFSR.INVEP\n");
9610 return false;
9613 void arm_v7m_cpu_do_interrupt(CPUState *cs)
9615 ARMCPU *cpu = ARM_CPU(cs);
9616 CPUARMState *env = &cpu->env;
9617 uint32_t lr;
9618 bool ignore_stackfaults;
9620 arm_log_exception(cs->exception_index);
9623 * For exceptions we just mark as pending on the NVIC, and let that
9624 * handle it.
9626 switch (cs->exception_index) {
9627 case EXCP_UDEF:
9628 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
9629 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
9630 break;
9631 case EXCP_NOCP:
9634 * NOCP might be directed to something other than the current
9635 * security state if this fault is because of NSACR; we indicate
9636 * the target security state using exception.target_el.
9638 int target_secstate;
9640 if (env->exception.target_el == 3) {
9641 target_secstate = M_REG_S;
9642 } else {
9643 target_secstate = env->v7m.secure;
9645 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, target_secstate);
9646 env->v7m.cfsr[target_secstate] |= R_V7M_CFSR_NOCP_MASK;
9647 break;
9649 case EXCP_INVSTATE:
9650 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
9651 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
9652 break;
9653 case EXCP_STKOF:
9654 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
9655 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK;
9656 break;
9657 case EXCP_LSERR:
9658 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
9659 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK;
9660 break;
9661 case EXCP_UNALIGNED:
9662 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
9663 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNALIGNED_MASK;
9664 break;
9665 case EXCP_SWI:
9666 /* The PC already points to the next instruction. */
9667 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
9668 break;
9669 case EXCP_PREFETCH_ABORT:
9670 case EXCP_DATA_ABORT:
9672 * Note that for M profile we don't have a guest facing FSR, but
9673 * the env->exception.fsr will be populated by the code that
9674 * raises the fault, in the A profile short-descriptor format.
9676 switch (env->exception.fsr & 0xf) {
9677 case M_FAKE_FSR_NSC_EXEC:
9679 * Exception generated when we try to execute code at an address
9680 * which is marked as Secure & Non-Secure Callable and the CPU
9681 * is in the Non-Secure state. The only instruction which can
9682 * be executed like this is SG (and that only if both halves of
9683 * the SG instruction have the same security attributes.)
9684 * Everything else must generate an INVEP SecureFault, so we
9685 * emulate the SG instruction here.
9687 if (v7m_handle_execute_nsc(cpu)) {
9688 return;
9690 break;
9691 case M_FAKE_FSR_SFAULT:
9693 * Various flavours of SecureFault for attempts to execute or
9694 * access data in the wrong security state.
9696 switch (cs->exception_index) {
9697 case EXCP_PREFETCH_ABORT:
9698 if (env->v7m.secure) {
9699 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
9700 qemu_log_mask(CPU_LOG_INT,
9701 "...really SecureFault with SFSR.INVTRAN\n");
9702 } else {
9703 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
9704 qemu_log_mask(CPU_LOG_INT,
9705 "...really SecureFault with SFSR.INVEP\n");
9707 break;
9708 case EXCP_DATA_ABORT:
9709 /* This must be an NS access to S memory */
9710 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
9711 qemu_log_mask(CPU_LOG_INT,
9712 "...really SecureFault with SFSR.AUVIOL\n");
9713 break;
9715 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
9716 break;
9717 case 0x8: /* External Abort */
9718 switch (cs->exception_index) {
9719 case EXCP_PREFETCH_ABORT:
9720 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
9721 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
9722 break;
9723 case EXCP_DATA_ABORT:
9724 env->v7m.cfsr[M_REG_NS] |=
9725 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
9726 env->v7m.bfar = env->exception.vaddress;
9727 qemu_log_mask(CPU_LOG_INT,
9728 "...with CFSR.PRECISERR and BFAR 0x%x\n",
9729 env->v7m.bfar);
9730 break;
9732 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
9733 break;
9734 default:
9736 * All other FSR values are either MPU faults or "can't happen
9737 * for M profile" cases.
9739 switch (cs->exception_index) {
9740 case EXCP_PREFETCH_ABORT:
9741 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
9742 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
9743 break;
9744 case EXCP_DATA_ABORT:
9745 env->v7m.cfsr[env->v7m.secure] |=
9746 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
9747 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
9748 qemu_log_mask(CPU_LOG_INT,
9749 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
9750 env->v7m.mmfar[env->v7m.secure]);
9751 break;
9753 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
9754 env->v7m.secure);
9755 break;
9757 break;
9758 case EXCP_BKPT:
9759 if (semihosting_enabled()) {
9760 int nr;
9761 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
9762 if (nr == 0xab) {
9763 env->regs[15] += 2;
9764 qemu_log_mask(CPU_LOG_INT,
9765 "...handling as semihosting call 0x%x\n",
9766 env->regs[0]);
9767 env->regs[0] = do_arm_semihosting(env);
9768 return;
9771 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
9772 break;
9773 case EXCP_IRQ:
9774 break;
9775 case EXCP_EXCEPTION_EXIT:
9776 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
9777 /* Must be v8M security extension function return */
9778 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
9779 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
9780 if (do_v7m_function_return(cpu)) {
9781 return;
9783 } else {
9784 do_v7m_exception_exit(cpu);
9785 return;
9787 break;
9788 case EXCP_LAZYFP:
9790 * We already pended the specific exception in the NVIC in the
9791 * v7m_preserve_fp_state() helper function.
9793 break;
9794 default:
9795 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9796 return; /* Never happens. Keep compiler happy. */
9799 if (arm_feature(env, ARM_FEATURE_V8)) {
9800 lr = R_V7M_EXCRET_RES1_MASK |
9801 R_V7M_EXCRET_DCRS_MASK;
9803 * The S bit indicates whether we should return to Secure
9804 * or NonSecure (ie our current state).
9805 * The ES bit indicates whether we're taking this exception
9806 * to Secure or NonSecure (ie our target state). We set it
9807 * later, in v7m_exception_taken().
9808 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
9809 * This corresponds to the ARM ARM pseudocode for v8M setting
9810 * some LR bits in PushStack() and some in ExceptionTaken();
9811 * the distinction matters for the tailchain cases where we
9812 * can take an exception without pushing the stack.
9814 if (env->v7m.secure) {
9815 lr |= R_V7M_EXCRET_S_MASK;
9817 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK)) {
9818 lr |= R_V7M_EXCRET_FTYPE_MASK;
9820 } else {
9821 lr = R_V7M_EXCRET_RES1_MASK |
9822 R_V7M_EXCRET_S_MASK |
9823 R_V7M_EXCRET_DCRS_MASK |
9824 R_V7M_EXCRET_FTYPE_MASK |
9825 R_V7M_EXCRET_ES_MASK;
9826 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
9827 lr |= R_V7M_EXCRET_SPSEL_MASK;
9830 if (!arm_v7m_is_handler_mode(env)) {
9831 lr |= R_V7M_EXCRET_MODE_MASK;
9834 ignore_stackfaults = v7m_push_stack(cpu);
9835 v7m_exception_taken(cpu, lr, false, ignore_stackfaults);
9839 * Function used to synchronize QEMU's AArch64 register set with AArch32
9840 * register set. This is necessary when switching between AArch32 and AArch64
9841 * execution state.
9843 void aarch64_sync_32_to_64(CPUARMState *env)
9845 int i;
9846 uint32_t mode = env->uncached_cpsr & CPSR_M;
9848 /* We can blanket copy R[0:7] to X[0:7] */
9849 for (i = 0; i < 8; i++) {
9850 env->xregs[i] = env->regs[i];
9854 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9855 * Otherwise, they come from the banked user regs.
9857 if (mode == ARM_CPU_MODE_FIQ) {
9858 for (i = 8; i < 13; i++) {
9859 env->xregs[i] = env->usr_regs[i - 8];
9861 } else {
9862 for (i = 8; i < 13; i++) {
9863 env->xregs[i] = env->regs[i];
9868 * Registers x13-x23 are the various mode SP and FP registers. Registers
9869 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9870 * from the mode banked register.
9872 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9873 env->xregs[13] = env->regs[13];
9874 env->xregs[14] = env->regs[14];
9875 } else {
9876 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9877 /* HYP is an exception in that it is copied from r14 */
9878 if (mode == ARM_CPU_MODE_HYP) {
9879 env->xregs[14] = env->regs[14];
9880 } else {
9881 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9885 if (mode == ARM_CPU_MODE_HYP) {
9886 env->xregs[15] = env->regs[13];
9887 } else {
9888 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9891 if (mode == ARM_CPU_MODE_IRQ) {
9892 env->xregs[16] = env->regs[14];
9893 env->xregs[17] = env->regs[13];
9894 } else {
9895 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9896 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9899 if (mode == ARM_CPU_MODE_SVC) {
9900 env->xregs[18] = env->regs[14];
9901 env->xregs[19] = env->regs[13];
9902 } else {
9903 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9904 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9907 if (mode == ARM_CPU_MODE_ABT) {
9908 env->xregs[20] = env->regs[14];
9909 env->xregs[21] = env->regs[13];
9910 } else {
9911 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9912 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9915 if (mode == ARM_CPU_MODE_UND) {
9916 env->xregs[22] = env->regs[14];
9917 env->xregs[23] = env->regs[13];
9918 } else {
9919 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9920 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9924 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9925 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9926 * FIQ bank for r8-r14.
9928 if (mode == ARM_CPU_MODE_FIQ) {
9929 for (i = 24; i < 31; i++) {
9930 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9932 } else {
9933 for (i = 24; i < 29; i++) {
9934 env->xregs[i] = env->fiq_regs[i - 24];
9936 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9937 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9940 env->pc = env->regs[15];
9944 * Function used to synchronize QEMU's AArch32 register set with AArch64
9945 * register set. This is necessary when switching between AArch32 and AArch64
9946 * execution state.
9948 void aarch64_sync_64_to_32(CPUARMState *env)
9950 int i;
9951 uint32_t mode = env->uncached_cpsr & CPSR_M;
9953 /* We can blanket copy X[0:7] to R[0:7] */
9954 for (i = 0; i < 8; i++) {
9955 env->regs[i] = env->xregs[i];
9959 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9960 * Otherwise, we copy x8-x12 into the banked user regs.
9962 if (mode == ARM_CPU_MODE_FIQ) {
9963 for (i = 8; i < 13; i++) {
9964 env->usr_regs[i - 8] = env->xregs[i];
9966 } else {
9967 for (i = 8; i < 13; i++) {
9968 env->regs[i] = env->xregs[i];
9973 * Registers r13 & r14 depend on the current mode.
9974 * If we are in a given mode, we copy the corresponding x registers to r13
9975 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9976 * for the mode.
9978 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9979 env->regs[13] = env->xregs[13];
9980 env->regs[14] = env->xregs[14];
9981 } else {
9982 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9985 * HYP is an exception in that it does not have its own banked r14 but
9986 * shares the USR r14
9988 if (mode == ARM_CPU_MODE_HYP) {
9989 env->regs[14] = env->xregs[14];
9990 } else {
9991 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9995 if (mode == ARM_CPU_MODE_HYP) {
9996 env->regs[13] = env->xregs[15];
9997 } else {
9998 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10001 if (mode == ARM_CPU_MODE_IRQ) {
10002 env->regs[14] = env->xregs[16];
10003 env->regs[13] = env->xregs[17];
10004 } else {
10005 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10006 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10009 if (mode == ARM_CPU_MODE_SVC) {
10010 env->regs[14] = env->xregs[18];
10011 env->regs[13] = env->xregs[19];
10012 } else {
10013 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10014 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10017 if (mode == ARM_CPU_MODE_ABT) {
10018 env->regs[14] = env->xregs[20];
10019 env->regs[13] = env->xregs[21];
10020 } else {
10021 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10022 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10025 if (mode == ARM_CPU_MODE_UND) {
10026 env->regs[14] = env->xregs[22];
10027 env->regs[13] = env->xregs[23];
10028 } else {
10029 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10030 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10033 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
10034 * mode, then we can copy to r8-r14. Otherwise, we copy to the
10035 * FIQ bank for r8-r14.
10037 if (mode == ARM_CPU_MODE_FIQ) {
10038 for (i = 24; i < 31; i++) {
10039 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
10041 } else {
10042 for (i = 24; i < 29; i++) {
10043 env->fiq_regs[i - 24] = env->xregs[i];
10045 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10046 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10049 env->regs[15] = env->pc;
10052 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10053 uint32_t mask, uint32_t offset,
10054 uint32_t newpc)
10056 /* Change the CPU state so as to actually take the exception. */
10057 switch_mode(env, new_mode);
10059 * For exceptions taken to AArch32 we must clear the SS bit in both
10060 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10062 env->uncached_cpsr &= ~PSTATE_SS;
10063 env->spsr = cpsr_read(env);
10064 /* Clear IT bits. */
10065 env->condexec_bits = 0;
10066 /* Switch to the new mode, and to the correct instruction set. */
10067 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10068 /* Set new mode endianness */
10069 env->uncached_cpsr &= ~CPSR_E;
10070 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
10071 env->uncached_cpsr |= CPSR_E;
10073 /* J and IL must always be cleared for exception entry */
10074 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10075 env->daif |= mask;
10077 if (new_mode == ARM_CPU_MODE_HYP) {
10078 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10079 env->elr_el[2] = env->regs[15];
10080 } else {
10082 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10083 * and we should just guard the thumb mode on V4
10085 if (arm_feature(env, ARM_FEATURE_V4T)) {
10086 env->thumb =
10087 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10089 env->regs[14] = env->regs[15] + offset;
10091 env->regs[15] = newpc;
10094 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10097 * Handle exception entry to Hyp mode; this is sufficiently
10098 * different to entry to other AArch32 modes that we handle it
10099 * separately here.
10101 * The vector table entry used is always the 0x14 Hyp mode entry point,
10102 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
10103 * The offset applied to the preferred return address is always zero
10104 * (see DDI0487C.a section G1.12.3).
10105 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10107 uint32_t addr, mask;
10108 ARMCPU *cpu = ARM_CPU(cs);
10109 CPUARMState *env = &cpu->env;
10111 switch (cs->exception_index) {
10112 case EXCP_UDEF:
10113 addr = 0x04;
10114 break;
10115 case EXCP_SWI:
10116 addr = 0x14;
10117 break;
10118 case EXCP_BKPT:
10119 /* Fall through to prefetch abort. */
10120 case EXCP_PREFETCH_ABORT:
10121 env->cp15.ifar_s = env->exception.vaddress;
10122 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10123 (uint32_t)env->exception.vaddress);
10124 addr = 0x0c;
10125 break;
10126 case EXCP_DATA_ABORT:
10127 env->cp15.dfar_s = env->exception.vaddress;
10128 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10129 (uint32_t)env->exception.vaddress);
10130 addr = 0x10;
10131 break;
10132 case EXCP_IRQ:
10133 addr = 0x18;
10134 break;
10135 case EXCP_FIQ:
10136 addr = 0x1c;
10137 break;
10138 case EXCP_HVC:
10139 addr = 0x08;
10140 break;
10141 case EXCP_HYP_TRAP:
10142 addr = 0x14;
10143 default:
10144 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10147 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10148 if (!arm_feature(env, ARM_FEATURE_V8)) {
10150 * QEMU syndrome values are v8-style. v7 has the IL bit
10151 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10152 * If this is a v7 CPU, squash the IL bit in those cases.
10154 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10155 (cs->exception_index == EXCP_DATA_ABORT &&
10156 !(env->exception.syndrome & ARM_EL_ISV)) ||
10157 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10158 env->exception.syndrome &= ~ARM_EL_IL;
10161 env->cp15.esr_el[2] = env->exception.syndrome;
10164 if (arm_current_el(env) != 2 && addr < 0x14) {
10165 addr = 0x14;
10168 mask = 0;
10169 if (!(env->cp15.scr_el3 & SCR_EA)) {
10170 mask |= CPSR_A;
10172 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10173 mask |= CPSR_I;
10175 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10176 mask |= CPSR_F;
10179 addr += env->cp15.hvbar;
10181 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10184 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10186 ARMCPU *cpu = ARM_CPU(cs);
10187 CPUARMState *env = &cpu->env;
10188 uint32_t addr;
10189 uint32_t mask;
10190 int new_mode;
10191 uint32_t offset;
10192 uint32_t moe;
10194 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10195 switch (syn_get_ec(env->exception.syndrome)) {
10196 case EC_BREAKPOINT:
10197 case EC_BREAKPOINT_SAME_EL:
10198 moe = 1;
10199 break;
10200 case EC_WATCHPOINT:
10201 case EC_WATCHPOINT_SAME_EL:
10202 moe = 10;
10203 break;
10204 case EC_AA32_BKPT:
10205 moe = 3;
10206 break;
10207 case EC_VECTORCATCH:
10208 moe = 5;
10209 break;
10210 default:
10211 moe = 0;
10212 break;
10215 if (moe) {
10216 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10219 if (env->exception.target_el == 2) {
10220 arm_cpu_do_interrupt_aarch32_hyp(cs);
10221 return;
10224 switch (cs->exception_index) {
10225 case EXCP_UDEF:
10226 new_mode = ARM_CPU_MODE_UND;
10227 addr = 0x04;
10228 mask = CPSR_I;
10229 if (env->thumb)
10230 offset = 2;
10231 else
10232 offset = 4;
10233 break;
10234 case EXCP_SWI:
10235 new_mode = ARM_CPU_MODE_SVC;
10236 addr = 0x08;
10237 mask = CPSR_I;
10238 /* The PC already points to the next instruction. */
10239 offset = 0;
10240 break;
10241 case EXCP_BKPT:
10242 /* Fall through to prefetch abort. */
10243 case EXCP_PREFETCH_ABORT:
10244 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10245 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10246 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10247 env->exception.fsr, (uint32_t)env->exception.vaddress);
10248 new_mode = ARM_CPU_MODE_ABT;
10249 addr = 0x0c;
10250 mask = CPSR_A | CPSR_I;
10251 offset = 4;
10252 break;
10253 case EXCP_DATA_ABORT:
10254 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10255 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10256 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10257 env->exception.fsr,
10258 (uint32_t)env->exception.vaddress);
10259 new_mode = ARM_CPU_MODE_ABT;
10260 addr = 0x10;
10261 mask = CPSR_A | CPSR_I;
10262 offset = 8;
10263 break;
10264 case EXCP_IRQ:
10265 new_mode = ARM_CPU_MODE_IRQ;
10266 addr = 0x18;
10267 /* Disable IRQ and imprecise data aborts. */
10268 mask = CPSR_A | CPSR_I;
10269 offset = 4;
10270 if (env->cp15.scr_el3 & SCR_IRQ) {
10271 /* IRQ routed to monitor mode */
10272 new_mode = ARM_CPU_MODE_MON;
10273 mask |= CPSR_F;
10275 break;
10276 case EXCP_FIQ:
10277 new_mode = ARM_CPU_MODE_FIQ;
10278 addr = 0x1c;
10279 /* Disable FIQ, IRQ and imprecise data aborts. */
10280 mask = CPSR_A | CPSR_I | CPSR_F;
10281 if (env->cp15.scr_el3 & SCR_FIQ) {
10282 /* FIQ routed to monitor mode */
10283 new_mode = ARM_CPU_MODE_MON;
10285 offset = 4;
10286 break;
10287 case EXCP_VIRQ:
10288 new_mode = ARM_CPU_MODE_IRQ;
10289 addr = 0x18;
10290 /* Disable IRQ and imprecise data aborts. */
10291 mask = CPSR_A | CPSR_I;
10292 offset = 4;
10293 break;
10294 case EXCP_VFIQ:
10295 new_mode = ARM_CPU_MODE_FIQ;
10296 addr = 0x1c;
10297 /* Disable FIQ, IRQ and imprecise data aborts. */
10298 mask = CPSR_A | CPSR_I | CPSR_F;
10299 offset = 4;
10300 break;
10301 case EXCP_SMC:
10302 new_mode = ARM_CPU_MODE_MON;
10303 addr = 0x08;
10304 mask = CPSR_A | CPSR_I | CPSR_F;
10305 offset = 0;
10306 break;
10307 default:
10308 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10309 return; /* Never happens. Keep compiler happy. */
10312 if (new_mode == ARM_CPU_MODE_MON) {
10313 addr += env->cp15.mvbar;
10314 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10315 /* High vectors. When enabled, base address cannot be remapped. */
10316 addr += 0xffff0000;
10317 } else {
10318 /* ARM v7 architectures provide a vector base address register to remap
10319 * the interrupt vector table.
10320 * This register is only followed in non-monitor mode, and is banked.
10321 * Note: only bits 31:5 are valid.
10323 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10326 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10327 env->cp15.scr_el3 &= ~SCR_NS;
10330 take_aarch32_exception(env, new_mode, mask, offset, addr);
10333 /* Handle exception entry to a target EL which is using AArch64 */
10334 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10336 ARMCPU *cpu = ARM_CPU(cs);
10337 CPUARMState *env = &cpu->env;
10338 unsigned int new_el = env->exception.target_el;
10339 target_ulong addr = env->cp15.vbar_el[new_el];
10340 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10341 unsigned int cur_el = arm_current_el(env);
10344 * Note that new_el can never be 0. If cur_el is 0, then
10345 * el0_a64 is is_a64(), else el0_a64 is ignored.
10347 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10349 if (cur_el < new_el) {
10350 /* Entry vector offset depends on whether the implemented EL
10351 * immediately lower than the target level is using AArch32 or AArch64
10353 bool is_aa64;
10355 switch (new_el) {
10356 case 3:
10357 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10358 break;
10359 case 2:
10360 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
10361 break;
10362 case 1:
10363 is_aa64 = is_a64(env);
10364 break;
10365 default:
10366 g_assert_not_reached();
10369 if (is_aa64) {
10370 addr += 0x400;
10371 } else {
10372 addr += 0x600;
10374 } else if (pstate_read(env) & PSTATE_SP) {
10375 addr += 0x200;
10378 switch (cs->exception_index) {
10379 case EXCP_PREFETCH_ABORT:
10380 case EXCP_DATA_ABORT:
10381 env->cp15.far_el[new_el] = env->exception.vaddress;
10382 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10383 env->cp15.far_el[new_el]);
10384 /* fall through */
10385 case EXCP_BKPT:
10386 case EXCP_UDEF:
10387 case EXCP_SWI:
10388 case EXCP_HVC:
10389 case EXCP_HYP_TRAP:
10390 case EXCP_SMC:
10391 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) {
10393 * QEMU internal FP/SIMD syndromes from AArch32 include the
10394 * TA and coproc fields which are only exposed if the exception
10395 * is taken to AArch32 Hyp mode. Mask them out to get a valid
10396 * AArch64 format syndrome.
10398 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10400 env->cp15.esr_el[new_el] = env->exception.syndrome;
10401 break;
10402 case EXCP_IRQ:
10403 case EXCP_VIRQ:
10404 addr += 0x80;
10405 break;
10406 case EXCP_FIQ:
10407 case EXCP_VFIQ:
10408 addr += 0x100;
10409 break;
10410 case EXCP_SEMIHOST:
10411 qemu_log_mask(CPU_LOG_INT,
10412 "...handling as semihosting call 0x%" PRIx64 "\n",
10413 env->xregs[0]);
10414 env->xregs[0] = do_arm_semihosting(env);
10415 return;
10416 default:
10417 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10420 if (is_a64(env)) {
10421 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
10422 aarch64_save_sp(env, arm_current_el(env));
10423 env->elr_el[new_el] = env->pc;
10424 } else {
10425 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
10426 env->elr_el[new_el] = env->regs[15];
10428 aarch64_sync_32_to_64(env);
10430 env->condexec_bits = 0;
10432 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10433 env->elr_el[new_el]);
10435 pstate_write(env, PSTATE_DAIF | new_mode);
10436 env->aarch64 = 1;
10437 aarch64_restore_sp(env, new_el);
10439 env->pc = addr;
10441 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10442 new_el, env->pc, pstate_read(env));
10445 static inline bool check_for_semihosting(CPUState *cs)
10447 /* Check whether this exception is a semihosting call; if so
10448 * then handle it and return true; otherwise return false.
10450 ARMCPU *cpu = ARM_CPU(cs);
10451 CPUARMState *env = &cpu->env;
10453 if (is_a64(env)) {
10454 if (cs->exception_index == EXCP_SEMIHOST) {
10455 /* This is always the 64-bit semihosting exception.
10456 * The "is this usermode" and "is semihosting enabled"
10457 * checks have been done at translate time.
10459 qemu_log_mask(CPU_LOG_INT,
10460 "...handling as semihosting call 0x%" PRIx64 "\n",
10461 env->xregs[0]);
10462 env->xregs[0] = do_arm_semihosting(env);
10463 return true;
10465 return false;
10466 } else {
10467 uint32_t imm;
10469 /* Only intercept calls from privileged modes, to provide some
10470 * semblance of security.
10472 if (cs->exception_index != EXCP_SEMIHOST &&
10473 (!semihosting_enabled() ||
10474 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
10475 return false;
10478 switch (cs->exception_index) {
10479 case EXCP_SEMIHOST:
10480 /* This is always a semihosting call; the "is this usermode"
10481 * and "is semihosting enabled" checks have been done at
10482 * translate time.
10484 break;
10485 case EXCP_SWI:
10486 /* Check for semihosting interrupt. */
10487 if (env->thumb) {
10488 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
10489 & 0xff;
10490 if (imm == 0xab) {
10491 break;
10493 } else {
10494 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
10495 & 0xffffff;
10496 if (imm == 0x123456) {
10497 break;
10500 return false;
10501 case EXCP_BKPT:
10502 /* See if this is a semihosting syscall. */
10503 if (env->thumb) {
10504 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
10505 & 0xff;
10506 if (imm == 0xab) {
10507 env->regs[15] += 2;
10508 break;
10511 return false;
10512 default:
10513 return false;
10516 qemu_log_mask(CPU_LOG_INT,
10517 "...handling as semihosting call 0x%x\n",
10518 env->regs[0]);
10519 env->regs[0] = do_arm_semihosting(env);
10520 return true;
10524 /* Handle a CPU exception for A and R profile CPUs.
10525 * Do any appropriate logging, handle PSCI calls, and then hand off
10526 * to the AArch64-entry or AArch32-entry function depending on the
10527 * target exception level's register width.
10529 void arm_cpu_do_interrupt(CPUState *cs)
10531 ARMCPU *cpu = ARM_CPU(cs);
10532 CPUARMState *env = &cpu->env;
10533 unsigned int new_el = env->exception.target_el;
10535 assert(!arm_feature(env, ARM_FEATURE_M));
10537 arm_log_exception(cs->exception_index);
10538 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10539 new_el);
10540 if (qemu_loglevel_mask(CPU_LOG_INT)
10541 && !excp_is_internal(cs->exception_index)) {
10542 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10543 syn_get_ec(env->exception.syndrome),
10544 env->exception.syndrome);
10547 if (arm_is_psci_call(cpu, cs->exception_index)) {
10548 arm_handle_psci_call(cpu);
10549 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10550 return;
10553 /* Semihosting semantics depend on the register width of the
10554 * code that caused the exception, not the target exception level,
10555 * so must be handled here.
10557 if (check_for_semihosting(cs)) {
10558 return;
10561 /* Hooks may change global state so BQL should be held, also the
10562 * BQL needs to be held for any modification of
10563 * cs->interrupt_request.
10565 g_assert(qemu_mutex_iothread_locked());
10567 arm_call_pre_el_change_hook(cpu);
10569 assert(!excp_is_internal(cs->exception_index));
10570 if (arm_el_is_aa64(env, new_el)) {
10571 arm_cpu_do_interrupt_aarch64(cs);
10572 } else {
10573 arm_cpu_do_interrupt_aarch32(cs);
10576 arm_call_el_change_hook(cpu);
10578 if (!kvm_enabled()) {
10579 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10582 #endif /* !CONFIG_USER_ONLY */
10584 /* Return the exception level which controls this address translation regime */
10585 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
10587 switch (mmu_idx) {
10588 case ARMMMUIdx_S2NS:
10589 case ARMMMUIdx_S1E2:
10590 return 2;
10591 case ARMMMUIdx_S1E3:
10592 return 3;
10593 case ARMMMUIdx_S1SE0:
10594 return arm_el_is_aa64(env, 3) ? 1 : 3;
10595 case ARMMMUIdx_S1SE1:
10596 case ARMMMUIdx_S1NSE0:
10597 case ARMMMUIdx_S1NSE1:
10598 case ARMMMUIdx_MPrivNegPri:
10599 case ARMMMUIdx_MUserNegPri:
10600 case ARMMMUIdx_MPriv:
10601 case ARMMMUIdx_MUser:
10602 case ARMMMUIdx_MSPrivNegPri:
10603 case ARMMMUIdx_MSUserNegPri:
10604 case ARMMMUIdx_MSPriv:
10605 case ARMMMUIdx_MSUser:
10606 return 1;
10607 default:
10608 g_assert_not_reached();
10612 #ifndef CONFIG_USER_ONLY
10614 /* Return the SCTLR value which controls this address translation regime */
10615 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
10617 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
10620 /* Return true if the specified stage of address translation is disabled */
10621 static inline bool regime_translation_disabled(CPUARMState *env,
10622 ARMMMUIdx mmu_idx)
10624 if (arm_feature(env, ARM_FEATURE_M)) {
10625 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
10626 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
10627 case R_V7M_MPU_CTRL_ENABLE_MASK:
10628 /* Enabled, but not for HardFault and NMI */
10629 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
10630 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
10631 /* Enabled for all cases */
10632 return false;
10633 case 0:
10634 default:
10635 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
10636 * we warned about that in armv7m_nvic.c when the guest set it.
10638 return true;
10642 if (mmu_idx == ARMMMUIdx_S2NS) {
10643 /* HCR.DC means HCR.VM behaves as 1 */
10644 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
10647 if (env->cp15.hcr_el2 & HCR_TGE) {
10648 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
10649 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
10650 return true;
10654 if ((env->cp15.hcr_el2 & HCR_DC) &&
10655 (mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1)) {
10656 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
10657 return true;
10660 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
10663 static inline bool regime_translation_big_endian(CPUARMState *env,
10664 ARMMMUIdx mmu_idx)
10666 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
10669 /* Return the TTBR associated with this translation regime */
10670 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
10671 int ttbrn)
10673 if (mmu_idx == ARMMMUIdx_S2NS) {
10674 return env->cp15.vttbr_el2;
10676 if (ttbrn == 0) {
10677 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
10678 } else {
10679 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
10683 #endif /* !CONFIG_USER_ONLY */
10685 /* Return the TCR controlling this translation regime */
10686 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
10688 if (mmu_idx == ARMMMUIdx_S2NS) {
10689 return &env->cp15.vtcr_el2;
10691 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
10694 /* Convert a possible stage1+2 MMU index into the appropriate
10695 * stage 1 MMU index
10697 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
10699 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
10700 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
10702 return mmu_idx;
10705 /* Return true if the translation regime is using LPAE format page tables */
10706 static inline bool regime_using_lpae_format(CPUARMState *env,
10707 ARMMMUIdx mmu_idx)
10709 int el = regime_el(env, mmu_idx);
10710 if (el == 2 || arm_el_is_aa64(env, el)) {
10711 return true;
10713 if (arm_feature(env, ARM_FEATURE_LPAE)
10714 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
10715 return true;
10717 return false;
10720 /* Returns true if the stage 1 translation regime is using LPAE format page
10721 * tables. Used when raising alignment exceptions, whose FSR changes depending
10722 * on whether the long or short descriptor format is in use. */
10723 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
10725 mmu_idx = stage_1_mmu_idx(mmu_idx);
10727 return regime_using_lpae_format(env, mmu_idx);
10730 #ifndef CONFIG_USER_ONLY
10731 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
10733 switch (mmu_idx) {
10734 case ARMMMUIdx_S1SE0:
10735 case ARMMMUIdx_S1NSE0:
10736 case ARMMMUIdx_MUser:
10737 case ARMMMUIdx_MSUser:
10738 case ARMMMUIdx_MUserNegPri:
10739 case ARMMMUIdx_MSUserNegPri:
10740 return true;
10741 default:
10742 return false;
10743 case ARMMMUIdx_S12NSE0:
10744 case ARMMMUIdx_S12NSE1:
10745 g_assert_not_reached();
10749 /* Translate section/page access permissions to page
10750 * R/W protection flags
10752 * @env: CPUARMState
10753 * @mmu_idx: MMU index indicating required translation regime
10754 * @ap: The 3-bit access permissions (AP[2:0])
10755 * @domain_prot: The 2-bit domain access permissions
10757 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
10758 int ap, int domain_prot)
10760 bool is_user = regime_is_user(env, mmu_idx);
10762 if (domain_prot == 3) {
10763 return PAGE_READ | PAGE_WRITE;
10766 switch (ap) {
10767 case 0:
10768 if (arm_feature(env, ARM_FEATURE_V7)) {
10769 return 0;
10771 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
10772 case SCTLR_S:
10773 return is_user ? 0 : PAGE_READ;
10774 case SCTLR_R:
10775 return PAGE_READ;
10776 default:
10777 return 0;
10779 case 1:
10780 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10781 case 2:
10782 if (is_user) {
10783 return PAGE_READ;
10784 } else {
10785 return PAGE_READ | PAGE_WRITE;
10787 case 3:
10788 return PAGE_READ | PAGE_WRITE;
10789 case 4: /* Reserved. */
10790 return 0;
10791 case 5:
10792 return is_user ? 0 : PAGE_READ;
10793 case 6:
10794 return PAGE_READ;
10795 case 7:
10796 if (!arm_feature(env, ARM_FEATURE_V6K)) {
10797 return 0;
10799 return PAGE_READ;
10800 default:
10801 g_assert_not_reached();
10805 /* Translate section/page access permissions to page
10806 * R/W protection flags.
10808 * @ap: The 2-bit simple AP (AP[2:1])
10809 * @is_user: TRUE if accessing from PL0
10811 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
10813 switch (ap) {
10814 case 0:
10815 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10816 case 1:
10817 return PAGE_READ | PAGE_WRITE;
10818 case 2:
10819 return is_user ? 0 : PAGE_READ;
10820 case 3:
10821 return PAGE_READ;
10822 default:
10823 g_assert_not_reached();
10827 static inline int
10828 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10830 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10833 /* Translate S2 section/page access permissions to protection flags
10835 * @env: CPUARMState
10836 * @s2ap: The 2-bit stage2 access permissions (S2AP)
10837 * @xn: XN (execute-never) bit
10839 static int get_S2prot(CPUARMState *env, int s2ap, int xn)
10841 int prot = 0;
10843 if (s2ap & 1) {
10844 prot |= PAGE_READ;
10846 if (s2ap & 2) {
10847 prot |= PAGE_WRITE;
10849 if (!xn) {
10850 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10851 prot |= PAGE_EXEC;
10854 return prot;
10857 /* Translate section/page access permissions to protection flags
10859 * @env: CPUARMState
10860 * @mmu_idx: MMU index indicating required translation regime
10861 * @is_aa64: TRUE if AArch64
10862 * @ap: The 2-bit simple AP (AP[2:1])
10863 * @ns: NS (non-secure) bit
10864 * @xn: XN (execute-never) bit
10865 * @pxn: PXN (privileged execute-never) bit
10867 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10868 int ap, int ns, int xn, int pxn)
10870 bool is_user = regime_is_user(env, mmu_idx);
10871 int prot_rw, user_rw;
10872 bool have_wxn;
10873 int wxn = 0;
10875 assert(mmu_idx != ARMMMUIdx_S2NS);
10877 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10878 if (is_user) {
10879 prot_rw = user_rw;
10880 } else {
10881 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
10884 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10885 return prot_rw;
10888 /* TODO have_wxn should be replaced with
10889 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10890 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10891 * compatible processors have EL2, which is required for [U]WXN.
10893 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10895 if (have_wxn) {
10896 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10899 if (is_aa64) {
10900 switch (regime_el(env, mmu_idx)) {
10901 case 1:
10902 if (!is_user) {
10903 xn = pxn || (user_rw & PAGE_WRITE);
10905 break;
10906 case 2:
10907 case 3:
10908 break;
10910 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10911 switch (regime_el(env, mmu_idx)) {
10912 case 1:
10913 case 3:
10914 if (is_user) {
10915 xn = xn || !(user_rw & PAGE_READ);
10916 } else {
10917 int uwxn = 0;
10918 if (have_wxn) {
10919 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10921 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10922 (uwxn && (user_rw & PAGE_WRITE));
10924 break;
10925 case 2:
10926 break;
10928 } else {
10929 xn = wxn = 0;
10932 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10933 return prot_rw;
10935 return prot_rw | PAGE_EXEC;
10938 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10939 uint32_t *table, uint32_t address)
10941 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
10942 TCR *tcr = regime_tcr(env, mmu_idx);
10944 if (address & tcr->mask) {
10945 if (tcr->raw_tcr & TTBCR_PD1) {
10946 /* Translation table walk disabled for TTBR1 */
10947 return false;
10949 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
10950 } else {
10951 if (tcr->raw_tcr & TTBCR_PD0) {
10952 /* Translation table walk disabled for TTBR0 */
10953 return false;
10955 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
10957 *table |= (address >> 18) & 0x3ffc;
10958 return true;
10961 /* Translate a S1 pagetable walk through S2 if needed. */
10962 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
10963 hwaddr addr, MemTxAttrs txattrs,
10964 ARMMMUFaultInfo *fi)
10966 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
10967 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
10968 target_ulong s2size;
10969 hwaddr s2pa;
10970 int s2prot;
10971 int ret;
10972 ARMCacheAttrs cacheattrs = {};
10973 ARMCacheAttrs *pcacheattrs = NULL;
10975 if (env->cp15.hcr_el2 & HCR_PTW) {
10977 * PTW means we must fault if this S1 walk touches S2 Device
10978 * memory; otherwise we don't care about the attributes and can
10979 * save the S2 translation the effort of computing them.
10981 pcacheattrs = &cacheattrs;
10984 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
10985 &txattrs, &s2prot, &s2size, fi, pcacheattrs);
10986 if (ret) {
10987 assert(fi->type != ARMFault_None);
10988 fi->s2addr = addr;
10989 fi->stage2 = true;
10990 fi->s1ptw = true;
10991 return ~0;
10993 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
10994 /* Access was to Device memory: generate Permission fault */
10995 fi->type = ARMFault_Permission;
10996 fi->s2addr = addr;
10997 fi->stage2 = true;
10998 fi->s1ptw = true;
10999 return ~0;
11001 addr = s2pa;
11003 return addr;
11006 /* All loads done in the course of a page table walk go through here. */
11007 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
11008 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
11010 ARMCPU *cpu = ARM_CPU(cs);
11011 CPUARMState *env = &cpu->env;
11012 MemTxAttrs attrs = {};
11013 MemTxResult result = MEMTX_OK;
11014 AddressSpace *as;
11015 uint32_t data;
11017 attrs.secure = is_secure;
11018 as = arm_addressspace(cs, attrs);
11019 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
11020 if (fi->s1ptw) {
11021 return 0;
11023 if (regime_translation_big_endian(env, mmu_idx)) {
11024 data = address_space_ldl_be(as, addr, attrs, &result);
11025 } else {
11026 data = address_space_ldl_le(as, addr, attrs, &result);
11028 if (result == MEMTX_OK) {
11029 return data;
11031 fi->type = ARMFault_SyncExternalOnWalk;
11032 fi->ea = arm_extabort_type(result);
11033 return 0;
11036 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
11037 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
11039 ARMCPU *cpu = ARM_CPU(cs);
11040 CPUARMState *env = &cpu->env;
11041 MemTxAttrs attrs = {};
11042 MemTxResult result = MEMTX_OK;
11043 AddressSpace *as;
11044 uint64_t data;
11046 attrs.secure = is_secure;
11047 as = arm_addressspace(cs, attrs);
11048 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
11049 if (fi->s1ptw) {
11050 return 0;
11052 if (regime_translation_big_endian(env, mmu_idx)) {
11053 data = address_space_ldq_be(as, addr, attrs, &result);
11054 } else {
11055 data = address_space_ldq_le(as, addr, attrs, &result);
11057 if (result == MEMTX_OK) {
11058 return data;
11060 fi->type = ARMFault_SyncExternalOnWalk;
11061 fi->ea = arm_extabort_type(result);
11062 return 0;
11065 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
11066 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11067 hwaddr *phys_ptr, int *prot,
11068 target_ulong *page_size,
11069 ARMMMUFaultInfo *fi)
11071 CPUState *cs = env_cpu(env);
11072 int level = 1;
11073 uint32_t table;
11074 uint32_t desc;
11075 int type;
11076 int ap;
11077 int domain = 0;
11078 int domain_prot;
11079 hwaddr phys_addr;
11080 uint32_t dacr;
11082 /* Pagetable walk. */
11083 /* Lookup l1 descriptor. */
11084 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
11085 /* Section translation fault if page walk is disabled by PD0 or PD1 */
11086 fi->type = ARMFault_Translation;
11087 goto do_fault;
11089 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
11090 mmu_idx, fi);
11091 if (fi->type != ARMFault_None) {
11092 goto do_fault;
11094 type = (desc & 3);
11095 domain = (desc >> 5) & 0x0f;
11096 if (regime_el(env, mmu_idx) == 1) {
11097 dacr = env->cp15.dacr_ns;
11098 } else {
11099 dacr = env->cp15.dacr_s;
11101 domain_prot = (dacr >> (domain * 2)) & 3;
11102 if (type == 0) {
11103 /* Section translation fault. */
11104 fi->type = ARMFault_Translation;
11105 goto do_fault;
11107 if (type != 2) {
11108 level = 2;
11110 if (domain_prot == 0 || domain_prot == 2) {
11111 fi->type = ARMFault_Domain;
11112 goto do_fault;
11114 if (type == 2) {
11115 /* 1Mb section. */
11116 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
11117 ap = (desc >> 10) & 3;
11118 *page_size = 1024 * 1024;
11119 } else {
11120 /* Lookup l2 entry. */
11121 if (type == 1) {
11122 /* Coarse pagetable. */
11123 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
11124 } else {
11125 /* Fine pagetable. */
11126 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
11128 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
11129 mmu_idx, fi);
11130 if (fi->type != ARMFault_None) {
11131 goto do_fault;
11133 switch (desc & 3) {
11134 case 0: /* Page translation fault. */
11135 fi->type = ARMFault_Translation;
11136 goto do_fault;
11137 case 1: /* 64k page. */
11138 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
11139 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
11140 *page_size = 0x10000;
11141 break;
11142 case 2: /* 4k page. */
11143 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
11144 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
11145 *page_size = 0x1000;
11146 break;
11147 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
11148 if (type == 1) {
11149 /* ARMv6/XScale extended small page format */
11150 if (arm_feature(env, ARM_FEATURE_XSCALE)
11151 || arm_feature(env, ARM_FEATURE_V6)) {
11152 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
11153 *page_size = 0x1000;
11154 } else {
11155 /* UNPREDICTABLE in ARMv5; we choose to take a
11156 * page translation fault.
11158 fi->type = ARMFault_Translation;
11159 goto do_fault;
11161 } else {
11162 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
11163 *page_size = 0x400;
11165 ap = (desc >> 4) & 3;
11166 break;
11167 default:
11168 /* Never happens, but compiler isn't smart enough to tell. */
11169 abort();
11172 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
11173 *prot |= *prot ? PAGE_EXEC : 0;
11174 if (!(*prot & (1 << access_type))) {
11175 /* Access permission fault. */
11176 fi->type = ARMFault_Permission;
11177 goto do_fault;
11179 *phys_ptr = phys_addr;
11180 return false;
11181 do_fault:
11182 fi->domain = domain;
11183 fi->level = level;
11184 return true;
11187 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
11188 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11189 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
11190 target_ulong *page_size, ARMMMUFaultInfo *fi)
11192 CPUState *cs = env_cpu(env);
11193 int level = 1;
11194 uint32_t table;
11195 uint32_t desc;
11196 uint32_t xn;
11197 uint32_t pxn = 0;
11198 int type;
11199 int ap;
11200 int domain = 0;
11201 int domain_prot;
11202 hwaddr phys_addr;
11203 uint32_t dacr;
11204 bool ns;
11206 /* Pagetable walk. */
11207 /* Lookup l1 descriptor. */
11208 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
11209 /* Section translation fault if page walk is disabled by PD0 or PD1 */
11210 fi->type = ARMFault_Translation;
11211 goto do_fault;
11213 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
11214 mmu_idx, fi);
11215 if (fi->type != ARMFault_None) {
11216 goto do_fault;
11218 type = (desc & 3);
11219 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
11220 /* Section translation fault, or attempt to use the encoding
11221 * which is Reserved on implementations without PXN.
11223 fi->type = ARMFault_Translation;
11224 goto do_fault;
11226 if ((type == 1) || !(desc & (1 << 18))) {
11227 /* Page or Section. */
11228 domain = (desc >> 5) & 0x0f;
11230 if (regime_el(env, mmu_idx) == 1) {
11231 dacr = env->cp15.dacr_ns;
11232 } else {
11233 dacr = env->cp15.dacr_s;
11235 if (type == 1) {
11236 level = 2;
11238 domain_prot = (dacr >> (domain * 2)) & 3;
11239 if (domain_prot == 0 || domain_prot == 2) {
11240 /* Section or Page domain fault */
11241 fi->type = ARMFault_Domain;
11242 goto do_fault;
11244 if (type != 1) {
11245 if (desc & (1 << 18)) {
11246 /* Supersection. */
11247 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
11248 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
11249 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
11250 *page_size = 0x1000000;
11251 } else {
11252 /* Section. */
11253 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
11254 *page_size = 0x100000;
11256 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
11257 xn = desc & (1 << 4);
11258 pxn = desc & 1;
11259 ns = extract32(desc, 19, 1);
11260 } else {
11261 if (arm_feature(env, ARM_FEATURE_PXN)) {
11262 pxn = (desc >> 2) & 1;
11264 ns = extract32(desc, 3, 1);
11265 /* Lookup l2 entry. */
11266 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
11267 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
11268 mmu_idx, fi);
11269 if (fi->type != ARMFault_None) {
11270 goto do_fault;
11272 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
11273 switch (desc & 3) {
11274 case 0: /* Page translation fault. */
11275 fi->type = ARMFault_Translation;
11276 goto do_fault;
11277 case 1: /* 64k page. */
11278 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
11279 xn = desc & (1 << 15);
11280 *page_size = 0x10000;
11281 break;
11282 case 2: case 3: /* 4k page. */
11283 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
11284 xn = desc & 1;
11285 *page_size = 0x1000;
11286 break;
11287 default:
11288 /* Never happens, but compiler isn't smart enough to tell. */
11289 abort();
11292 if (domain_prot == 3) {
11293 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11294 } else {
11295 if (pxn && !regime_is_user(env, mmu_idx)) {
11296 xn = 1;
11298 if (xn && access_type == MMU_INST_FETCH) {
11299 fi->type = ARMFault_Permission;
11300 goto do_fault;
11303 if (arm_feature(env, ARM_FEATURE_V6K) &&
11304 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
11305 /* The simplified model uses AP[0] as an access control bit. */
11306 if ((ap & 1) == 0) {
11307 /* Access flag fault. */
11308 fi->type = ARMFault_AccessFlag;
11309 goto do_fault;
11311 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
11312 } else {
11313 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
11315 if (*prot && !xn) {
11316 *prot |= PAGE_EXEC;
11318 if (!(*prot & (1 << access_type))) {
11319 /* Access permission fault. */
11320 fi->type = ARMFault_Permission;
11321 goto do_fault;
11324 if (ns) {
11325 /* The NS bit will (as required by the architecture) have no effect if
11326 * the CPU doesn't support TZ or this is a non-secure translation
11327 * regime, because the attribute will already be non-secure.
11329 attrs->secure = false;
11331 *phys_ptr = phys_addr;
11332 return false;
11333 do_fault:
11334 fi->domain = domain;
11335 fi->level = level;
11336 return true;
11340 * check_s2_mmu_setup
11341 * @cpu: ARMCPU
11342 * @is_aa64: True if the translation regime is in AArch64 state
11343 * @startlevel: Suggested starting level
11344 * @inputsize: Bitsize of IPAs
11345 * @stride: Page-table stride (See the ARM ARM)
11347 * Returns true if the suggested S2 translation parameters are OK and
11348 * false otherwise.
11350 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
11351 int inputsize, int stride)
11353 const int grainsize = stride + 3;
11354 int startsizecheck;
11356 /* Negative levels are never allowed. */
11357 if (level < 0) {
11358 return false;
11361 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
11362 if (startsizecheck < 1 || startsizecheck > stride + 4) {
11363 return false;
11366 if (is_aa64) {
11367 CPUARMState *env = &cpu->env;
11368 unsigned int pamax = arm_pamax(cpu);
11370 switch (stride) {
11371 case 13: /* 64KB Pages. */
11372 if (level == 0 || (level == 1 && pamax <= 42)) {
11373 return false;
11375 break;
11376 case 11: /* 16KB Pages. */
11377 if (level == 0 || (level == 1 && pamax <= 40)) {
11378 return false;
11380 break;
11381 case 9: /* 4KB Pages. */
11382 if (level == 0 && pamax <= 42) {
11383 return false;
11385 break;
11386 default:
11387 g_assert_not_reached();
11390 /* Inputsize checks. */
11391 if (inputsize > pamax &&
11392 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
11393 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
11394 return false;
11396 } else {
11397 /* AArch32 only supports 4KB pages. Assert on that. */
11398 assert(stride == 9);
11400 if (level == 0) {
11401 return false;
11404 return true;
11407 /* Translate from the 4-bit stage 2 representation of
11408 * memory attributes (without cache-allocation hints) to
11409 * the 8-bit representation of the stage 1 MAIR registers
11410 * (which includes allocation hints).
11412 * ref: shared/translation/attrs/S2AttrDecode()
11413 * .../S2ConvertAttrsHints()
11415 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
11417 uint8_t hiattr = extract32(s2attrs, 2, 2);
11418 uint8_t loattr = extract32(s2attrs, 0, 2);
11419 uint8_t hihint = 0, lohint = 0;
11421 if (hiattr != 0) { /* normal memory */
11422 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
11423 hiattr = loattr = 1; /* non-cacheable */
11424 } else {
11425 if (hiattr != 1) { /* Write-through or write-back */
11426 hihint = 3; /* RW allocate */
11428 if (loattr != 1) { /* Write-through or write-back */
11429 lohint = 3; /* RW allocate */
11434 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
11436 #endif /* !CONFIG_USER_ONLY */
11438 ARMVAParameters aa64_va_parameters_both(CPUARMState *env, uint64_t va,
11439 ARMMMUIdx mmu_idx)
11441 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11442 uint32_t el = regime_el(env, mmu_idx);
11443 bool tbi, tbid, epd, hpd, using16k, using64k;
11444 int select, tsz;
11447 * Bit 55 is always between the two regions, and is canonical for
11448 * determining if address tagging is enabled.
11450 select = extract64(va, 55, 1);
11452 if (el > 1) {
11453 tsz = extract32(tcr, 0, 6);
11454 using64k = extract32(tcr, 14, 1);
11455 using16k = extract32(tcr, 15, 1);
11456 if (mmu_idx == ARMMMUIdx_S2NS) {
11457 /* VTCR_EL2 */
11458 tbi = tbid = hpd = false;
11459 } else {
11460 tbi = extract32(tcr, 20, 1);
11461 hpd = extract32(tcr, 24, 1);
11462 tbid = extract32(tcr, 29, 1);
11464 epd = false;
11465 } else if (!select) {
11466 tsz = extract32(tcr, 0, 6);
11467 epd = extract32(tcr, 7, 1);
11468 using64k = extract32(tcr, 14, 1);
11469 using16k = extract32(tcr, 15, 1);
11470 tbi = extract64(tcr, 37, 1);
11471 hpd = extract64(tcr, 41, 1);
11472 tbid = extract64(tcr, 51, 1);
11473 } else {
11474 int tg = extract32(tcr, 30, 2);
11475 using16k = tg == 1;
11476 using64k = tg == 3;
11477 tsz = extract32(tcr, 16, 6);
11478 epd = extract32(tcr, 23, 1);
11479 tbi = extract64(tcr, 38, 1);
11480 hpd = extract64(tcr, 42, 1);
11481 tbid = extract64(tcr, 52, 1);
11483 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
11484 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
11486 return (ARMVAParameters) {
11487 .tsz = tsz,
11488 .select = select,
11489 .tbi = tbi,
11490 .tbid = tbid,
11491 .epd = epd,
11492 .hpd = hpd,
11493 .using16k = using16k,
11494 .using64k = using64k,
11498 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11499 ARMMMUIdx mmu_idx, bool data)
11501 ARMVAParameters ret = aa64_va_parameters_both(env, va, mmu_idx);
11503 /* Present TBI as a composite with TBID. */
11504 ret.tbi &= (data || !ret.tbid);
11505 return ret;
11508 #ifndef CONFIG_USER_ONLY
11509 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
11510 ARMMMUIdx mmu_idx)
11512 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11513 uint32_t el = regime_el(env, mmu_idx);
11514 int select, tsz;
11515 bool epd, hpd;
11517 if (mmu_idx == ARMMMUIdx_S2NS) {
11518 /* VTCR */
11519 bool sext = extract32(tcr, 4, 1);
11520 bool sign = extract32(tcr, 3, 1);
11523 * If the sign-extend bit is not the same as t0sz[3], the result
11524 * is unpredictable. Flag this as a guest error.
11526 if (sign != sext) {
11527 qemu_log_mask(LOG_GUEST_ERROR,
11528 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
11530 tsz = sextract32(tcr, 0, 4) + 8;
11531 select = 0;
11532 hpd = false;
11533 epd = false;
11534 } else if (el == 2) {
11535 /* HTCR */
11536 tsz = extract32(tcr, 0, 3);
11537 select = 0;
11538 hpd = extract64(tcr, 24, 1);
11539 epd = false;
11540 } else {
11541 int t0sz = extract32(tcr, 0, 3);
11542 int t1sz = extract32(tcr, 16, 3);
11544 if (t1sz == 0) {
11545 select = va > (0xffffffffu >> t0sz);
11546 } else {
11547 /* Note that we will detect errors later. */
11548 select = va >= ~(0xffffffffu >> t1sz);
11550 if (!select) {
11551 tsz = t0sz;
11552 epd = extract32(tcr, 7, 1);
11553 hpd = extract64(tcr, 41, 1);
11554 } else {
11555 tsz = t1sz;
11556 epd = extract32(tcr, 23, 1);
11557 hpd = extract64(tcr, 42, 1);
11559 /* For aarch32, hpd0 is not enabled without t2e as well. */
11560 hpd &= extract32(tcr, 6, 1);
11563 return (ARMVAParameters) {
11564 .tsz = tsz,
11565 .select = select,
11566 .epd = epd,
11567 .hpd = hpd,
11571 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
11572 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11573 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
11574 target_ulong *page_size_ptr,
11575 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
11577 ARMCPU *cpu = env_archcpu(env);
11578 CPUState *cs = CPU(cpu);
11579 /* Read an LPAE long-descriptor translation table. */
11580 ARMFaultType fault_type = ARMFault_Translation;
11581 uint32_t level;
11582 ARMVAParameters param;
11583 uint64_t ttbr;
11584 hwaddr descaddr, indexmask, indexmask_grainsize;
11585 uint32_t tableattrs;
11586 target_ulong page_size;
11587 uint32_t attrs;
11588 int32_t stride;
11589 int addrsize, inputsize;
11590 TCR *tcr = regime_tcr(env, mmu_idx);
11591 int ap, ns, xn, pxn;
11592 uint32_t el = regime_el(env, mmu_idx);
11593 bool ttbr1_valid;
11594 uint64_t descaddrmask;
11595 bool aarch64 = arm_el_is_aa64(env, el);
11596 bool guarded = false;
11598 /* TODO:
11599 * This code does not handle the different format TCR for VTCR_EL2.
11600 * This code also does not support shareability levels.
11601 * Attribute and permission bit handling should also be checked when adding
11602 * support for those page table walks.
11604 if (aarch64) {
11605 param = aa64_va_parameters(env, address, mmu_idx,
11606 access_type != MMU_INST_FETCH);
11607 level = 0;
11608 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
11609 * invalid.
11611 ttbr1_valid = (el < 2);
11612 addrsize = 64 - 8 * param.tbi;
11613 inputsize = 64 - param.tsz;
11614 } else {
11615 param = aa32_va_parameters(env, address, mmu_idx);
11616 level = 1;
11617 /* There is no TTBR1 for EL2 */
11618 ttbr1_valid = (el != 2);
11619 addrsize = (mmu_idx == ARMMMUIdx_S2NS ? 40 : 32);
11620 inputsize = addrsize - param.tsz;
11624 * We determined the region when collecting the parameters, but we
11625 * have not yet validated that the address is valid for the region.
11626 * Extract the top bits and verify that they all match select.
11628 * For aa32, if inputsize == addrsize, then we have selected the
11629 * region by exclusion in aa32_va_parameters and there is no more
11630 * validation to do here.
11632 if (inputsize < addrsize) {
11633 target_ulong top_bits = sextract64(address, inputsize,
11634 addrsize - inputsize);
11635 if (-top_bits != param.select || (param.select && !ttbr1_valid)) {
11636 /* The gap between the two regions is a Translation fault */
11637 fault_type = ARMFault_Translation;
11638 goto do_fault;
11642 if (param.using64k) {
11643 stride = 13;
11644 } else if (param.using16k) {
11645 stride = 11;
11646 } else {
11647 stride = 9;
11650 /* Note that QEMU ignores shareability and cacheability attributes,
11651 * so we don't need to do anything with the SH, ORGN, IRGN fields
11652 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
11653 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
11654 * implement any ASID-like capability so we can ignore it (instead
11655 * we will always flush the TLB any time the ASID is changed).
11657 ttbr = regime_ttbr(env, mmu_idx, param.select);
11659 /* Here we should have set up all the parameters for the translation:
11660 * inputsize, ttbr, epd, stride, tbi
11663 if (param.epd) {
11664 /* Translation table walk disabled => Translation fault on TLB miss
11665 * Note: This is always 0 on 64-bit EL2 and EL3.
11667 goto do_fault;
11670 if (mmu_idx != ARMMMUIdx_S2NS) {
11671 /* The starting level depends on the virtual address size (which can
11672 * be up to 48 bits) and the translation granule size. It indicates
11673 * the number of strides (stride bits at a time) needed to
11674 * consume the bits of the input address. In the pseudocode this is:
11675 * level = 4 - RoundUp((inputsize - grainsize) / stride)
11676 * where their 'inputsize' is our 'inputsize', 'grainsize' is
11677 * our 'stride + 3' and 'stride' is our 'stride'.
11678 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
11679 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
11680 * = 4 - (inputsize - 4) / stride;
11682 level = 4 - (inputsize - 4) / stride;
11683 } else {
11684 /* For stage 2 translations the starting level is specified by the
11685 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
11687 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
11688 uint32_t startlevel;
11689 bool ok;
11691 if (!aarch64 || stride == 9) {
11692 /* AArch32 or 4KB pages */
11693 startlevel = 2 - sl0;
11694 } else {
11695 /* 16KB or 64KB pages */
11696 startlevel = 3 - sl0;
11699 /* Check that the starting level is valid. */
11700 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
11701 inputsize, stride);
11702 if (!ok) {
11703 fault_type = ARMFault_Translation;
11704 goto do_fault;
11706 level = startlevel;
11709 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
11710 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
11712 /* Now we can extract the actual base address from the TTBR */
11713 descaddr = extract64(ttbr, 0, 48);
11714 descaddr &= ~indexmask;
11716 /* The address field in the descriptor goes up to bit 39 for ARMv7
11717 * but up to bit 47 for ARMv8, but we use the descaddrmask
11718 * up to bit 39 for AArch32, because we don't need other bits in that case
11719 * to construct next descriptor address (anyway they should be all zeroes).
11721 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
11722 ~indexmask_grainsize;
11724 /* Secure accesses start with the page table in secure memory and
11725 * can be downgraded to non-secure at any step. Non-secure accesses
11726 * remain non-secure. We implement this by just ORing in the NSTable/NS
11727 * bits at each step.
11729 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
11730 for (;;) {
11731 uint64_t descriptor;
11732 bool nstable;
11734 descaddr |= (address >> (stride * (4 - level))) & indexmask;
11735 descaddr &= ~7ULL;
11736 nstable = extract32(tableattrs, 4, 1);
11737 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
11738 if (fi->type != ARMFault_None) {
11739 goto do_fault;
11742 if (!(descriptor & 1) ||
11743 (!(descriptor & 2) && (level == 3))) {
11744 /* Invalid, or the Reserved level 3 encoding */
11745 goto do_fault;
11747 descaddr = descriptor & descaddrmask;
11749 if ((descriptor & 2) && (level < 3)) {
11750 /* Table entry. The top five bits are attributes which may
11751 * propagate down through lower levels of the table (and
11752 * which are all arranged so that 0 means "no effect", so
11753 * we can gather them up by ORing in the bits at each level).
11755 tableattrs |= extract64(descriptor, 59, 5);
11756 level++;
11757 indexmask = indexmask_grainsize;
11758 continue;
11760 /* Block entry at level 1 or 2, or page entry at level 3.
11761 * These are basically the same thing, although the number
11762 * of bits we pull in from the vaddr varies.
11764 page_size = (1ULL << ((stride * (4 - level)) + 3));
11765 descaddr |= (address & (page_size - 1));
11766 /* Extract attributes from the descriptor */
11767 attrs = extract64(descriptor, 2, 10)
11768 | (extract64(descriptor, 52, 12) << 10);
11770 if (mmu_idx == ARMMMUIdx_S2NS) {
11771 /* Stage 2 table descriptors do not include any attribute fields */
11772 break;
11774 /* Merge in attributes from table descriptors */
11775 attrs |= nstable << 3; /* NS */
11776 guarded = extract64(descriptor, 50, 1); /* GP */
11777 if (param.hpd) {
11778 /* HPD disables all the table attributes except NSTable. */
11779 break;
11781 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
11782 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11783 * means "force PL1 access only", which means forcing AP[1] to 0.
11785 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
11786 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
11787 break;
11789 /* Here descaddr is the final physical address, and attributes
11790 * are all in attrs.
11792 fault_type = ARMFault_AccessFlag;
11793 if ((attrs & (1 << 8)) == 0) {
11794 /* Access flag */
11795 goto do_fault;
11798 ap = extract32(attrs, 4, 2);
11799 xn = extract32(attrs, 12, 1);
11801 if (mmu_idx == ARMMMUIdx_S2NS) {
11802 ns = true;
11803 *prot = get_S2prot(env, ap, xn);
11804 } else {
11805 ns = extract32(attrs, 3, 1);
11806 pxn = extract32(attrs, 11, 1);
11807 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
11810 fault_type = ARMFault_Permission;
11811 if (!(*prot & (1 << access_type))) {
11812 goto do_fault;
11815 if (ns) {
11816 /* The NS bit will (as required by the architecture) have no effect if
11817 * the CPU doesn't support TZ or this is a non-secure translation
11818 * regime, because the attribute will already be non-secure.
11820 txattrs->secure = false;
11822 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
11823 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
11824 txattrs->target_tlb_bit0 = true;
11827 if (cacheattrs != NULL) {
11828 if (mmu_idx == ARMMMUIdx_S2NS) {
11829 cacheattrs->attrs = convert_stage2_attrs(env,
11830 extract32(attrs, 0, 4));
11831 } else {
11832 /* Index into MAIR registers for cache attributes */
11833 uint8_t attrindx = extract32(attrs, 0, 3);
11834 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11835 assert(attrindx <= 7);
11836 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
11838 cacheattrs->shareability = extract32(attrs, 6, 2);
11841 *phys_ptr = descaddr;
11842 *page_size_ptr = page_size;
11843 return false;
11845 do_fault:
11846 fi->type = fault_type;
11847 fi->level = level;
11848 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
11849 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
11850 return true;
11853 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11854 ARMMMUIdx mmu_idx,
11855 int32_t address, int *prot)
11857 if (!arm_feature(env, ARM_FEATURE_M)) {
11858 *prot = PAGE_READ | PAGE_WRITE;
11859 switch (address) {
11860 case 0xF0000000 ... 0xFFFFFFFF:
11861 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11862 /* hivecs execing is ok */
11863 *prot |= PAGE_EXEC;
11865 break;
11866 case 0x00000000 ... 0x7FFFFFFF:
11867 *prot |= PAGE_EXEC;
11868 break;
11870 } else {
11871 /* Default system address map for M profile cores.
11872 * The architecture specifies which regions are execute-never;
11873 * at the MPU level no other checks are defined.
11875 switch (address) {
11876 case 0x00000000 ... 0x1fffffff: /* ROM */
11877 case 0x20000000 ... 0x3fffffff: /* SRAM */
11878 case 0x60000000 ... 0x7fffffff: /* RAM */
11879 case 0x80000000 ... 0x9fffffff: /* RAM */
11880 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11881 break;
11882 case 0x40000000 ... 0x5fffffff: /* Peripheral */
11883 case 0xa0000000 ... 0xbfffffff: /* Device */
11884 case 0xc0000000 ... 0xdfffffff: /* Device */
11885 case 0xe0000000 ... 0xffffffff: /* System */
11886 *prot = PAGE_READ | PAGE_WRITE;
11887 break;
11888 default:
11889 g_assert_not_reached();
11894 static bool pmsav7_use_background_region(ARMCPU *cpu,
11895 ARMMMUIdx mmu_idx, bool is_user)
11897 /* Return true if we should use the default memory map as a
11898 * "background" region if there are no hits against any MPU regions.
11900 CPUARMState *env = &cpu->env;
11902 if (is_user) {
11903 return false;
11906 if (arm_feature(env, ARM_FEATURE_M)) {
11907 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11908 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
11909 } else {
11910 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11914 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11916 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11917 return arm_feature(env, ARM_FEATURE_M) &&
11918 extract32(address, 20, 12) == 0xe00;
11921 static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11923 /* True if address is in the M profile system region
11924 * 0xe0000000 - 0xffffffff
11926 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11929 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
11930 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11931 hwaddr *phys_ptr, int *prot,
11932 target_ulong *page_size,
11933 ARMMMUFaultInfo *fi)
11935 ARMCPU *cpu = env_archcpu(env);
11936 int n;
11937 bool is_user = regime_is_user(env, mmu_idx);
11939 *phys_ptr = address;
11940 *page_size = TARGET_PAGE_SIZE;
11941 *prot = 0;
11943 if (regime_translation_disabled(env, mmu_idx) ||
11944 m_is_ppb_region(env, address)) {
11945 /* MPU disabled or M profile PPB access: use default memory map.
11946 * The other case which uses the default memory map in the
11947 * v7M ARM ARM pseudocode is exception vector reads from the vector
11948 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11949 * which always does a direct read using address_space_ldl(), rather
11950 * than going via this function, so we don't need to check that here.
11952 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11953 } else { /* MPU enabled */
11954 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11955 /* region search */
11956 uint32_t base = env->pmsav7.drbar[n];
11957 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11958 uint32_t rmask;
11959 bool srdis = false;
11961 if (!(env->pmsav7.drsr[n] & 0x1)) {
11962 continue;
11965 if (!rsize) {
11966 qemu_log_mask(LOG_GUEST_ERROR,
11967 "DRSR[%d]: Rsize field cannot be 0\n", n);
11968 continue;
11970 rsize++;
11971 rmask = (1ull << rsize) - 1;
11973 if (base & rmask) {
11974 qemu_log_mask(LOG_GUEST_ERROR,
11975 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11976 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11977 n, base, rmask);
11978 continue;
11981 if (address < base || address > base + rmask) {
11983 * Address not in this region. We must check whether the
11984 * region covers addresses in the same page as our address.
11985 * In that case we must not report a size that covers the
11986 * whole page for a subsequent hit against a different MPU
11987 * region or the background region, because it would result in
11988 * incorrect TLB hits for subsequent accesses to addresses that
11989 * are in this MPU region.
11991 if (ranges_overlap(base, rmask,
11992 address & TARGET_PAGE_MASK,
11993 TARGET_PAGE_SIZE)) {
11994 *page_size = 1;
11996 continue;
11999 /* Region matched */
12001 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
12002 int i, snd;
12003 uint32_t srdis_mask;
12005 rsize -= 3; /* sub region size (power of 2) */
12006 snd = ((address - base) >> rsize) & 0x7;
12007 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
12009 srdis_mask = srdis ? 0x3 : 0x0;
12010 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
12011 /* This will check in groups of 2, 4 and then 8, whether
12012 * the subregion bits are consistent. rsize is incremented
12013 * back up to give the region size, considering consistent
12014 * adjacent subregions as one region. Stop testing if rsize
12015 * is already big enough for an entire QEMU page.
12017 int snd_rounded = snd & ~(i - 1);
12018 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
12019 snd_rounded + 8, i);
12020 if (srdis_mask ^ srdis_multi) {
12021 break;
12023 srdis_mask = (srdis_mask << i) | srdis_mask;
12024 rsize++;
12027 if (srdis) {
12028 continue;
12030 if (rsize < TARGET_PAGE_BITS) {
12031 *page_size = 1 << rsize;
12033 break;
12036 if (n == -1) { /* no hits */
12037 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
12038 /* background fault */
12039 fi->type = ARMFault_Background;
12040 return true;
12042 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
12043 } else { /* a MPU hit! */
12044 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
12045 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
12047 if (m_is_system_region(env, address)) {
12048 /* System space is always execute never */
12049 xn = 1;
12052 if (is_user) { /* User mode AP bit decoding */
12053 switch (ap) {
12054 case 0:
12055 case 1:
12056 case 5:
12057 break; /* no access */
12058 case 3:
12059 *prot |= PAGE_WRITE;
12060 /* fall through */
12061 case 2:
12062 case 6:
12063 *prot |= PAGE_READ | PAGE_EXEC;
12064 break;
12065 case 7:
12066 /* for v7M, same as 6; for R profile a reserved value */
12067 if (arm_feature(env, ARM_FEATURE_M)) {
12068 *prot |= PAGE_READ | PAGE_EXEC;
12069 break;
12071 /* fall through */
12072 default:
12073 qemu_log_mask(LOG_GUEST_ERROR,
12074 "DRACR[%d]: Bad value for AP bits: 0x%"
12075 PRIx32 "\n", n, ap);
12077 } else { /* Priv. mode AP bits decoding */
12078 switch (ap) {
12079 case 0:
12080 break; /* no access */
12081 case 1:
12082 case 2:
12083 case 3:
12084 *prot |= PAGE_WRITE;
12085 /* fall through */
12086 case 5:
12087 case 6:
12088 *prot |= PAGE_READ | PAGE_EXEC;
12089 break;
12090 case 7:
12091 /* for v7M, same as 6; for R profile a reserved value */
12092 if (arm_feature(env, ARM_FEATURE_M)) {
12093 *prot |= PAGE_READ | PAGE_EXEC;
12094 break;
12096 /* fall through */
12097 default:
12098 qemu_log_mask(LOG_GUEST_ERROR,
12099 "DRACR[%d]: Bad value for AP bits: 0x%"
12100 PRIx32 "\n", n, ap);
12104 /* execute never */
12105 if (xn) {
12106 *prot &= ~PAGE_EXEC;
12111 fi->type = ARMFault_Permission;
12112 fi->level = 1;
12113 return !(*prot & (1 << access_type));
12116 static bool v8m_is_sau_exempt(CPUARMState *env,
12117 uint32_t address, MMUAccessType access_type)
12119 /* The architecture specifies that certain address ranges are
12120 * exempt from v8M SAU/IDAU checks.
12122 return
12123 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
12124 (address >= 0xe0000000 && address <= 0xe0002fff) ||
12125 (address >= 0xe000e000 && address <= 0xe000efff) ||
12126 (address >= 0xe002e000 && address <= 0xe002efff) ||
12127 (address >= 0xe0040000 && address <= 0xe0041fff) ||
12128 (address >= 0xe00ff000 && address <= 0xe00fffff);
12131 static void v8m_security_lookup(CPUARMState *env, uint32_t address,
12132 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12133 V8M_SAttributes *sattrs)
12135 /* Look up the security attributes for this address. Compare the
12136 * pseudocode SecurityCheck() function.
12137 * We assume the caller has zero-initialized *sattrs.
12139 ARMCPU *cpu = env_archcpu(env);
12140 int r;
12141 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
12142 int idau_region = IREGION_NOTVALID;
12143 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12144 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
12146 if (cpu->idau) {
12147 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
12148 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
12150 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
12151 &idau_nsc);
12154 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
12155 /* 0xf0000000..0xffffffff is always S for insn fetches */
12156 return;
12159 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
12160 sattrs->ns = !regime_is_secure(env, mmu_idx);
12161 return;
12164 if (idau_region != IREGION_NOTVALID) {
12165 sattrs->irvalid = true;
12166 sattrs->iregion = idau_region;
12169 switch (env->sau.ctrl & 3) {
12170 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
12171 break;
12172 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
12173 sattrs->ns = true;
12174 break;
12175 default: /* SAU.ENABLE == 1 */
12176 for (r = 0; r < cpu->sau_sregion; r++) {
12177 if (env->sau.rlar[r] & 1) {
12178 uint32_t base = env->sau.rbar[r] & ~0x1f;
12179 uint32_t limit = env->sau.rlar[r] | 0x1f;
12181 if (base <= address && limit >= address) {
12182 if (base > addr_page_base || limit < addr_page_limit) {
12183 sattrs->subpage = true;
12185 if (sattrs->srvalid) {
12186 /* If we hit in more than one region then we must report
12187 * as Secure, not NS-Callable, with no valid region
12188 * number info.
12190 sattrs->ns = false;
12191 sattrs->nsc = false;
12192 sattrs->sregion = 0;
12193 sattrs->srvalid = false;
12194 break;
12195 } else {
12196 if (env->sau.rlar[r] & 2) {
12197 sattrs->nsc = true;
12198 } else {
12199 sattrs->ns = true;
12201 sattrs->srvalid = true;
12202 sattrs->sregion = r;
12204 } else {
12206 * Address not in this region. We must check whether the
12207 * region covers addresses in the same page as our address.
12208 * In that case we must not report a size that covers the
12209 * whole page for a subsequent hit against a different MPU
12210 * region or the background region, because it would result
12211 * in incorrect TLB hits for subsequent accesses to
12212 * addresses that are in this MPU region.
12214 if (limit >= base &&
12215 ranges_overlap(base, limit - base + 1,
12216 addr_page_base,
12217 TARGET_PAGE_SIZE)) {
12218 sattrs->subpage = true;
12223 break;
12227 * The IDAU will override the SAU lookup results if it specifies
12228 * higher security than the SAU does.
12230 if (!idau_ns) {
12231 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
12232 sattrs->ns = false;
12233 sattrs->nsc = idau_nsc;
12238 static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
12239 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12240 hwaddr *phys_ptr, MemTxAttrs *txattrs,
12241 int *prot, bool *is_subpage,
12242 ARMMMUFaultInfo *fi, uint32_t *mregion)
12244 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
12245 * that a full phys-to-virt translation does).
12246 * mregion is (if not NULL) set to the region number which matched,
12247 * or -1 if no region number is returned (MPU off, address did not
12248 * hit a region, address hit in multiple regions).
12249 * We set is_subpage to true if the region hit doesn't cover the
12250 * entire TARGET_PAGE the address is within.
12252 ARMCPU *cpu = env_archcpu(env);
12253 bool is_user = regime_is_user(env, mmu_idx);
12254 uint32_t secure = regime_is_secure(env, mmu_idx);
12255 int n;
12256 int matchregion = -1;
12257 bool hit = false;
12258 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12259 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
12261 *is_subpage = false;
12262 *phys_ptr = address;
12263 *prot = 0;
12264 if (mregion) {
12265 *mregion = -1;
12268 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
12269 * was an exception vector read from the vector table (which is always
12270 * done using the default system address map), because those accesses
12271 * are done in arm_v7m_load_vector(), which always does a direct
12272 * read using address_space_ldl(), rather than going via this function.
12274 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
12275 hit = true;
12276 } else if (m_is_ppb_region(env, address)) {
12277 hit = true;
12278 } else {
12279 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
12280 hit = true;
12283 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
12284 /* region search */
12285 /* Note that the base address is bits [31:5] from the register
12286 * with bits [4:0] all zeroes, but the limit address is bits
12287 * [31:5] from the register with bits [4:0] all ones.
12289 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
12290 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
12292 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
12293 /* Region disabled */
12294 continue;
12297 if (address < base || address > limit) {
12299 * Address not in this region. We must check whether the
12300 * region covers addresses in the same page as our address.
12301 * In that case we must not report a size that covers the
12302 * whole page for a subsequent hit against a different MPU
12303 * region or the background region, because it would result in
12304 * incorrect TLB hits for subsequent accesses to addresses that
12305 * are in this MPU region.
12307 if (limit >= base &&
12308 ranges_overlap(base, limit - base + 1,
12309 addr_page_base,
12310 TARGET_PAGE_SIZE)) {
12311 *is_subpage = true;
12313 continue;
12316 if (base > addr_page_base || limit < addr_page_limit) {
12317 *is_subpage = true;
12320 if (matchregion != -1) {
12321 /* Multiple regions match -- always a failure (unlike
12322 * PMSAv7 where highest-numbered-region wins)
12324 fi->type = ARMFault_Permission;
12325 fi->level = 1;
12326 return true;
12329 matchregion = n;
12330 hit = true;
12334 if (!hit) {
12335 /* background fault */
12336 fi->type = ARMFault_Background;
12337 return true;
12340 if (matchregion == -1) {
12341 /* hit using the background region */
12342 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
12343 } else {
12344 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
12345 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
12347 if (m_is_system_region(env, address)) {
12348 /* System space is always execute never */
12349 xn = 1;
12352 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
12353 if (*prot && !xn) {
12354 *prot |= PAGE_EXEC;
12356 /* We don't need to look the attribute up in the MAIR0/MAIR1
12357 * registers because that only tells us about cacheability.
12359 if (mregion) {
12360 *mregion = matchregion;
12364 fi->type = ARMFault_Permission;
12365 fi->level = 1;
12366 return !(*prot & (1 << access_type));
12370 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
12371 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12372 hwaddr *phys_ptr, MemTxAttrs *txattrs,
12373 int *prot, target_ulong *page_size,
12374 ARMMMUFaultInfo *fi)
12376 uint32_t secure = regime_is_secure(env, mmu_idx);
12377 V8M_SAttributes sattrs = {};
12378 bool ret;
12379 bool mpu_is_subpage;
12381 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
12382 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
12383 if (access_type == MMU_INST_FETCH) {
12384 /* Instruction fetches always use the MMU bank and the
12385 * transaction attribute determined by the fetch address,
12386 * regardless of CPU state. This is painful for QEMU
12387 * to handle, because it would mean we need to encode
12388 * into the mmu_idx not just the (user, negpri) information
12389 * for the current security state but also that for the
12390 * other security state, which would balloon the number
12391 * of mmu_idx values needed alarmingly.
12392 * Fortunately we can avoid this because it's not actually
12393 * possible to arbitrarily execute code from memory with
12394 * the wrong security attribute: it will always generate
12395 * an exception of some kind or another, apart from the
12396 * special case of an NS CPU executing an SG instruction
12397 * in S&NSC memory. So we always just fail the translation
12398 * here and sort things out in the exception handler
12399 * (including possibly emulating an SG instruction).
12401 if (sattrs.ns != !secure) {
12402 if (sattrs.nsc) {
12403 fi->type = ARMFault_QEMU_NSCExec;
12404 } else {
12405 fi->type = ARMFault_QEMU_SFault;
12407 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
12408 *phys_ptr = address;
12409 *prot = 0;
12410 return true;
12412 } else {
12413 /* For data accesses we always use the MMU bank indicated
12414 * by the current CPU state, but the security attributes
12415 * might downgrade a secure access to nonsecure.
12417 if (sattrs.ns) {
12418 txattrs->secure = false;
12419 } else if (!secure) {
12420 /* NS access to S memory must fault.
12421 * Architecturally we should first check whether the
12422 * MPU information for this address indicates that we
12423 * are doing an unaligned access to Device memory, which
12424 * should generate a UsageFault instead. QEMU does not
12425 * currently check for that kind of unaligned access though.
12426 * If we added it we would need to do so as a special case
12427 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
12429 fi->type = ARMFault_QEMU_SFault;
12430 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
12431 *phys_ptr = address;
12432 *prot = 0;
12433 return true;
12438 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
12439 txattrs, prot, &mpu_is_subpage, fi, NULL);
12440 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
12441 return ret;
12444 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
12445 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12446 hwaddr *phys_ptr, int *prot,
12447 ARMMMUFaultInfo *fi)
12449 int n;
12450 uint32_t mask;
12451 uint32_t base;
12452 bool is_user = regime_is_user(env, mmu_idx);
12454 if (regime_translation_disabled(env, mmu_idx)) {
12455 /* MPU disabled. */
12456 *phys_ptr = address;
12457 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12458 return false;
12461 *phys_ptr = address;
12462 for (n = 7; n >= 0; n--) {
12463 base = env->cp15.c6_region[n];
12464 if ((base & 1) == 0) {
12465 continue;
12467 mask = 1 << ((base >> 1) & 0x1f);
12468 /* Keep this shift separate from the above to avoid an
12469 (undefined) << 32. */
12470 mask = (mask << 1) - 1;
12471 if (((base ^ address) & ~mask) == 0) {
12472 break;
12475 if (n < 0) {
12476 fi->type = ARMFault_Background;
12477 return true;
12480 if (access_type == MMU_INST_FETCH) {
12481 mask = env->cp15.pmsav5_insn_ap;
12482 } else {
12483 mask = env->cp15.pmsav5_data_ap;
12485 mask = (mask >> (n * 4)) & 0xf;
12486 switch (mask) {
12487 case 0:
12488 fi->type = ARMFault_Permission;
12489 fi->level = 1;
12490 return true;
12491 case 1:
12492 if (is_user) {
12493 fi->type = ARMFault_Permission;
12494 fi->level = 1;
12495 return true;
12497 *prot = PAGE_READ | PAGE_WRITE;
12498 break;
12499 case 2:
12500 *prot = PAGE_READ;
12501 if (!is_user) {
12502 *prot |= PAGE_WRITE;
12504 break;
12505 case 3:
12506 *prot = PAGE_READ | PAGE_WRITE;
12507 break;
12508 case 5:
12509 if (is_user) {
12510 fi->type = ARMFault_Permission;
12511 fi->level = 1;
12512 return true;
12514 *prot = PAGE_READ;
12515 break;
12516 case 6:
12517 *prot = PAGE_READ;
12518 break;
12519 default:
12520 /* Bad permission. */
12521 fi->type = ARMFault_Permission;
12522 fi->level = 1;
12523 return true;
12525 *prot |= PAGE_EXEC;
12526 return false;
12529 /* Combine either inner or outer cacheability attributes for normal
12530 * memory, according to table D4-42 and pseudocode procedure
12531 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
12533 * NB: only stage 1 includes allocation hints (RW bits), leading to
12534 * some asymmetry.
12536 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
12538 if (s1 == 4 || s2 == 4) {
12539 /* non-cacheable has precedence */
12540 return 4;
12541 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
12542 /* stage 1 write-through takes precedence */
12543 return s1;
12544 } else if (extract32(s2, 2, 2) == 2) {
12545 /* stage 2 write-through takes precedence, but the allocation hint
12546 * is still taken from stage 1
12548 return (2 << 2) | extract32(s1, 0, 2);
12549 } else { /* write-back */
12550 return s1;
12554 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
12555 * and CombineS1S2Desc()
12557 * @s1: Attributes from stage 1 walk
12558 * @s2: Attributes from stage 2 walk
12560 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
12562 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
12563 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
12564 ARMCacheAttrs ret;
12566 /* Combine shareability attributes (table D4-43) */
12567 if (s1.shareability == 2 || s2.shareability == 2) {
12568 /* if either are outer-shareable, the result is outer-shareable */
12569 ret.shareability = 2;
12570 } else if (s1.shareability == 3 || s2.shareability == 3) {
12571 /* if either are inner-shareable, the result is inner-shareable */
12572 ret.shareability = 3;
12573 } else {
12574 /* both non-shareable */
12575 ret.shareability = 0;
12578 /* Combine memory type and cacheability attributes */
12579 if (s1hi == 0 || s2hi == 0) {
12580 /* Device has precedence over normal */
12581 if (s1lo == 0 || s2lo == 0) {
12582 /* nGnRnE has precedence over anything */
12583 ret.attrs = 0;
12584 } else if (s1lo == 4 || s2lo == 4) {
12585 /* non-Reordering has precedence over Reordering */
12586 ret.attrs = 4; /* nGnRE */
12587 } else if (s1lo == 8 || s2lo == 8) {
12588 /* non-Gathering has precedence over Gathering */
12589 ret.attrs = 8; /* nGRE */
12590 } else {
12591 ret.attrs = 0xc; /* GRE */
12594 /* Any location for which the resultant memory type is any
12595 * type of Device memory is always treated as Outer Shareable.
12597 ret.shareability = 2;
12598 } else { /* Normal memory */
12599 /* Outer/inner cacheability combine independently */
12600 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
12601 | combine_cacheattr_nibble(s1lo, s2lo);
12603 if (ret.attrs == 0x44) {
12604 /* Any location for which the resultant memory type is Normal
12605 * Inner Non-cacheable, Outer Non-cacheable is always treated
12606 * as Outer Shareable.
12608 ret.shareability = 2;
12612 return ret;
12616 /* get_phys_addr - get the physical address for this virtual address
12618 * Find the physical address corresponding to the given virtual address,
12619 * by doing a translation table walk on MMU based systems or using the
12620 * MPU state on MPU based systems.
12622 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
12623 * prot and page_size may not be filled in, and the populated fsr value provides
12624 * information on why the translation aborted, in the format of a
12625 * DFSR/IFSR fault register, with the following caveats:
12626 * * we honour the short vs long DFSR format differences.
12627 * * the WnR bit is never set (the caller must do this).
12628 * * for PSMAv5 based systems we don't bother to return a full FSR format
12629 * value.
12631 * @env: CPUARMState
12632 * @address: virtual address to get physical address for
12633 * @access_type: 0 for read, 1 for write, 2 for execute
12634 * @mmu_idx: MMU index indicating required translation regime
12635 * @phys_ptr: set to the physical address corresponding to the virtual address
12636 * @attrs: set to the memory transaction attributes to use
12637 * @prot: set to the permissions for the page containing phys_ptr
12638 * @page_size: set to the size of the page containing phys_ptr
12639 * @fi: set to fault info if the translation fails
12640 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
12642 static bool get_phys_addr(CPUARMState *env, target_ulong address,
12643 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12644 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
12645 target_ulong *page_size,
12646 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
12648 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
12649 /* Call ourselves recursively to do the stage 1 and then stage 2
12650 * translations.
12652 if (arm_feature(env, ARM_FEATURE_EL2)) {
12653 hwaddr ipa;
12654 int s2_prot;
12655 int ret;
12656 ARMCacheAttrs cacheattrs2 = {};
12658 ret = get_phys_addr(env, address, access_type,
12659 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
12660 prot, page_size, fi, cacheattrs);
12662 /* If S1 fails or S2 is disabled, return early. */
12663 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
12664 *phys_ptr = ipa;
12665 return ret;
12668 /* S1 is done. Now do S2 translation. */
12669 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
12670 phys_ptr, attrs, &s2_prot,
12671 page_size, fi,
12672 cacheattrs != NULL ? &cacheattrs2 : NULL);
12673 fi->s2addr = ipa;
12674 /* Combine the S1 and S2 perms. */
12675 *prot &= s2_prot;
12677 /* Combine the S1 and S2 cache attributes, if needed */
12678 if (!ret && cacheattrs != NULL) {
12679 if (env->cp15.hcr_el2 & HCR_DC) {
12681 * HCR.DC forces the first stage attributes to
12682 * Normal Non-Shareable,
12683 * Inner Write-Back Read-Allocate Write-Allocate,
12684 * Outer Write-Back Read-Allocate Write-Allocate.
12686 cacheattrs->attrs = 0xff;
12687 cacheattrs->shareability = 0;
12689 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
12692 return ret;
12693 } else {
12695 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
12697 mmu_idx = stage_1_mmu_idx(mmu_idx);
12701 /* The page table entries may downgrade secure to non-secure, but
12702 * cannot upgrade an non-secure translation regime's attributes
12703 * to secure.
12705 attrs->secure = regime_is_secure(env, mmu_idx);
12706 attrs->user = regime_is_user(env, mmu_idx);
12708 /* Fast Context Switch Extension. This doesn't exist at all in v8.
12709 * In v7 and earlier it affects all stage 1 translations.
12711 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
12712 && !arm_feature(env, ARM_FEATURE_V8)) {
12713 if (regime_el(env, mmu_idx) == 3) {
12714 address += env->cp15.fcseidr_s;
12715 } else {
12716 address += env->cp15.fcseidr_ns;
12720 if (arm_feature(env, ARM_FEATURE_PMSA)) {
12721 bool ret;
12722 *page_size = TARGET_PAGE_SIZE;
12724 if (arm_feature(env, ARM_FEATURE_V8)) {
12725 /* PMSAv8 */
12726 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
12727 phys_ptr, attrs, prot, page_size, fi);
12728 } else if (arm_feature(env, ARM_FEATURE_V7)) {
12729 /* PMSAv7 */
12730 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
12731 phys_ptr, prot, page_size, fi);
12732 } else {
12733 /* Pre-v7 MPU */
12734 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
12735 phys_ptr, prot, fi);
12737 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
12738 " mmu_idx %u -> %s (prot %c%c%c)\n",
12739 access_type == MMU_DATA_LOAD ? "reading" :
12740 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
12741 (uint32_t)address, mmu_idx,
12742 ret ? "Miss" : "Hit",
12743 *prot & PAGE_READ ? 'r' : '-',
12744 *prot & PAGE_WRITE ? 'w' : '-',
12745 *prot & PAGE_EXEC ? 'x' : '-');
12747 return ret;
12750 /* Definitely a real MMU, not an MPU */
12752 if (regime_translation_disabled(env, mmu_idx)) {
12753 /* MMU disabled. */
12754 *phys_ptr = address;
12755 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12756 *page_size = TARGET_PAGE_SIZE;
12757 return 0;
12760 if (regime_using_lpae_format(env, mmu_idx)) {
12761 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
12762 phys_ptr, attrs, prot, page_size,
12763 fi, cacheattrs);
12764 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
12765 return get_phys_addr_v6(env, address, access_type, mmu_idx,
12766 phys_ptr, attrs, prot, page_size, fi);
12767 } else {
12768 return get_phys_addr_v5(env, address, access_type, mmu_idx,
12769 phys_ptr, prot, page_size, fi);
12773 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12774 MemTxAttrs *attrs)
12776 ARMCPU *cpu = ARM_CPU(cs);
12777 CPUARMState *env = &cpu->env;
12778 hwaddr phys_addr;
12779 target_ulong page_size;
12780 int prot;
12781 bool ret;
12782 ARMMMUFaultInfo fi = {};
12783 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
12785 *attrs = (MemTxAttrs) {};
12787 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
12788 attrs, &prot, &page_size, &fi, NULL);
12790 if (ret) {
12791 return -1;
12793 return phys_addr;
12796 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
12798 uint32_t mask;
12799 unsigned el = arm_current_el(env);
12801 /* First handle registers which unprivileged can read */
12803 switch (reg) {
12804 case 0 ... 7: /* xPSR sub-fields */
12805 mask = 0;
12806 if ((reg & 1) && el) {
12807 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
12809 if (!(reg & 4)) {
12810 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
12811 if (arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
12812 mask |= XPSR_GE;
12815 /* EPSR reads as zero */
12816 return xpsr_read(env) & mask;
12817 break;
12818 case 20: /* CONTROL */
12820 uint32_t value = env->v7m.control[env->v7m.secure];
12821 if (!env->v7m.secure) {
12822 /* SFPA is RAZ/WI from NS; FPCA is stored in the M_REG_S bank */
12823 value |= env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK;
12825 return value;
12827 case 0x94: /* CONTROL_NS */
12829 * We have to handle this here because unprivileged Secure code
12830 * can read the NS CONTROL register.
12832 if (!env->v7m.secure) {
12833 return 0;
12835 return env->v7m.control[M_REG_NS] |
12836 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK);
12839 if (el == 0) {
12840 return 0; /* unprivileged reads others as zero */
12843 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
12844 switch (reg) {
12845 case 0x88: /* MSP_NS */
12846 if (!env->v7m.secure) {
12847 return 0;
12849 return env->v7m.other_ss_msp;
12850 case 0x89: /* PSP_NS */
12851 if (!env->v7m.secure) {
12852 return 0;
12854 return env->v7m.other_ss_psp;
12855 case 0x8a: /* MSPLIM_NS */
12856 if (!env->v7m.secure) {
12857 return 0;
12859 return env->v7m.msplim[M_REG_NS];
12860 case 0x8b: /* PSPLIM_NS */
12861 if (!env->v7m.secure) {
12862 return 0;
12864 return env->v7m.psplim[M_REG_NS];
12865 case 0x90: /* PRIMASK_NS */
12866 if (!env->v7m.secure) {
12867 return 0;
12869 return env->v7m.primask[M_REG_NS];
12870 case 0x91: /* BASEPRI_NS */
12871 if (!env->v7m.secure) {
12872 return 0;
12874 return env->v7m.basepri[M_REG_NS];
12875 case 0x93: /* FAULTMASK_NS */
12876 if (!env->v7m.secure) {
12877 return 0;
12879 return env->v7m.faultmask[M_REG_NS];
12880 case 0x98: /* SP_NS */
12883 * This gives the non-secure SP selected based on whether we're
12884 * currently in handler mode or not, using the NS CONTROL.SPSEL.
12886 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
12888 if (!env->v7m.secure) {
12889 return 0;
12891 if (!arm_v7m_is_handler_mode(env) && spsel) {
12892 return env->v7m.other_ss_psp;
12893 } else {
12894 return env->v7m.other_ss_msp;
12897 default:
12898 break;
12902 switch (reg) {
12903 case 8: /* MSP */
12904 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13];
12905 case 9: /* PSP */
12906 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp;
12907 case 10: /* MSPLIM */
12908 if (!arm_feature(env, ARM_FEATURE_V8)) {
12909 goto bad_reg;
12911 return env->v7m.msplim[env->v7m.secure];
12912 case 11: /* PSPLIM */
12913 if (!arm_feature(env, ARM_FEATURE_V8)) {
12914 goto bad_reg;
12916 return env->v7m.psplim[env->v7m.secure];
12917 case 16: /* PRIMASK */
12918 return env->v7m.primask[env->v7m.secure];
12919 case 17: /* BASEPRI */
12920 case 18: /* BASEPRI_MAX */
12921 return env->v7m.basepri[env->v7m.secure];
12922 case 19: /* FAULTMASK */
12923 return env->v7m.faultmask[env->v7m.secure];
12924 default:
12925 bad_reg:
12926 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
12927 " register %d\n", reg);
12928 return 0;
12932 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
12935 * We're passed bits [11..0] of the instruction; extract
12936 * SYSm and the mask bits.
12937 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
12938 * we choose to treat them as if the mask bits were valid.
12939 * NB that the pseudocode 'mask' variable is bits [11..10],
12940 * whereas ours is [11..8].
12942 uint32_t mask = extract32(maskreg, 8, 4);
12943 uint32_t reg = extract32(maskreg, 0, 8);
12944 int cur_el = arm_current_el(env);
12946 if (cur_el == 0 && reg > 7 && reg != 20) {
12948 * only xPSR sub-fields and CONTROL.SFPA may be written by
12949 * unprivileged code
12951 return;
12954 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
12955 switch (reg) {
12956 case 0x88: /* MSP_NS */
12957 if (!env->v7m.secure) {
12958 return;
12960 env->v7m.other_ss_msp = val;
12961 return;
12962 case 0x89: /* PSP_NS */
12963 if (!env->v7m.secure) {
12964 return;
12966 env->v7m.other_ss_psp = val;
12967 return;
12968 case 0x8a: /* MSPLIM_NS */
12969 if (!env->v7m.secure) {
12970 return;
12972 env->v7m.msplim[M_REG_NS] = val & ~7;
12973 return;
12974 case 0x8b: /* PSPLIM_NS */
12975 if (!env->v7m.secure) {
12976 return;
12978 env->v7m.psplim[M_REG_NS] = val & ~7;
12979 return;
12980 case 0x90: /* PRIMASK_NS */
12981 if (!env->v7m.secure) {
12982 return;
12984 env->v7m.primask[M_REG_NS] = val & 1;
12985 return;
12986 case 0x91: /* BASEPRI_NS */
12987 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) {
12988 return;
12990 env->v7m.basepri[M_REG_NS] = val & 0xff;
12991 return;
12992 case 0x93: /* FAULTMASK_NS */
12993 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) {
12994 return;
12996 env->v7m.faultmask[M_REG_NS] = val & 1;
12997 return;
12998 case 0x94: /* CONTROL_NS */
12999 if (!env->v7m.secure) {
13000 return;
13002 write_v7m_control_spsel_for_secstate(env,
13003 val & R_V7M_CONTROL_SPSEL_MASK,
13004 M_REG_NS);
13005 if (arm_feature(env, ARM_FEATURE_M_MAIN)) {
13006 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK;
13007 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK;
13010 * SFPA is RAZ/WI from NS. FPCA is RO if NSACR.CP10 == 0,
13011 * RES0 if the FPU is not present, and is stored in the S bank
13013 if (arm_feature(env, ARM_FEATURE_VFP) &&
13014 extract32(env->v7m.nsacr, 10, 1)) {
13015 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK;
13016 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_FPCA_MASK;
13018 return;
13019 case 0x98: /* SP_NS */
13022 * This gives the non-secure SP selected based on whether we're
13023 * currently in handler mode or not, using the NS CONTROL.SPSEL.
13025 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
13026 bool is_psp = !arm_v7m_is_handler_mode(env) && spsel;
13027 uint32_t limit;
13029 if (!env->v7m.secure) {
13030 return;
13033 limit = is_psp ? env->v7m.psplim[false] : env->v7m.msplim[false];
13035 if (val < limit) {
13036 CPUState *cs = env_cpu(env);
13038 cpu_restore_state(cs, GETPC(), true);
13039 raise_exception(env, EXCP_STKOF, 0, 1);
13042 if (is_psp) {
13043 env->v7m.other_ss_psp = val;
13044 } else {
13045 env->v7m.other_ss_msp = val;
13047 return;
13049 default:
13050 break;
13054 switch (reg) {
13055 case 0 ... 7: /* xPSR sub-fields */
13056 /* only APSR is actually writable */
13057 if (!(reg & 4)) {
13058 uint32_t apsrmask = 0;
13060 if (mask & 8) {
13061 apsrmask |= XPSR_NZCV | XPSR_Q;
13063 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
13064 apsrmask |= XPSR_GE;
13066 xpsr_write(env, val, apsrmask);
13068 break;
13069 case 8: /* MSP */
13070 if (v7m_using_psp(env)) {
13071 env->v7m.other_sp = val;
13072 } else {
13073 env->regs[13] = val;
13075 break;
13076 case 9: /* PSP */
13077 if (v7m_using_psp(env)) {
13078 env->regs[13] = val;
13079 } else {
13080 env->v7m.other_sp = val;
13082 break;
13083 case 10: /* MSPLIM */
13084 if (!arm_feature(env, ARM_FEATURE_V8)) {
13085 goto bad_reg;
13087 env->v7m.msplim[env->v7m.secure] = val & ~7;
13088 break;
13089 case 11: /* PSPLIM */
13090 if (!arm_feature(env, ARM_FEATURE_V8)) {
13091 goto bad_reg;
13093 env->v7m.psplim[env->v7m.secure] = val & ~7;
13094 break;
13095 case 16: /* PRIMASK */
13096 env->v7m.primask[env->v7m.secure] = val & 1;
13097 break;
13098 case 17: /* BASEPRI */
13099 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
13100 goto bad_reg;
13102 env->v7m.basepri[env->v7m.secure] = val & 0xff;
13103 break;
13104 case 18: /* BASEPRI_MAX */
13105 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
13106 goto bad_reg;
13108 val &= 0xff;
13109 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
13110 || env->v7m.basepri[env->v7m.secure] == 0)) {
13111 env->v7m.basepri[env->v7m.secure] = val;
13113 break;
13114 case 19: /* FAULTMASK */
13115 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
13116 goto bad_reg;
13118 env->v7m.faultmask[env->v7m.secure] = val & 1;
13119 break;
13120 case 20: /* CONTROL */
13122 * Writing to the SPSEL bit only has an effect if we are in
13123 * thread mode; other bits can be updated by any privileged code.
13124 * write_v7m_control_spsel() deals with updating the SPSEL bit in
13125 * env->v7m.control, so we only need update the others.
13126 * For v7M, we must just ignore explicit writes to SPSEL in handler
13127 * mode; for v8M the write is permitted but will have no effect.
13128 * All these bits are writes-ignored from non-privileged code,
13129 * except for SFPA.
13131 if (cur_el > 0 && (arm_feature(env, ARM_FEATURE_V8) ||
13132 !arm_v7m_is_handler_mode(env))) {
13133 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
13135 if (cur_el > 0 && arm_feature(env, ARM_FEATURE_M_MAIN)) {
13136 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
13137 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
13139 if (arm_feature(env, ARM_FEATURE_VFP)) {
13141 * SFPA is RAZ/WI from NS or if no FPU.
13142 * FPCA is RO if NSACR.CP10 == 0, RES0 if the FPU is not present.
13143 * Both are stored in the S bank.
13145 if (env->v7m.secure) {
13146 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
13147 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_SFPA_MASK;
13149 if (cur_el > 0 &&
13150 (env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_SECURITY) ||
13151 extract32(env->v7m.nsacr, 10, 1))) {
13152 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK;
13153 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_FPCA_MASK;
13156 break;
13157 default:
13158 bad_reg:
13159 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
13160 " register %d\n", reg);
13161 return;
13165 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
13167 /* Implement the TT instruction. op is bits [7:6] of the insn. */
13168 bool forceunpriv = op & 1;
13169 bool alt = op & 2;
13170 V8M_SAttributes sattrs = {};
13171 uint32_t tt_resp;
13172 bool r, rw, nsr, nsrw, mrvalid;
13173 int prot;
13174 ARMMMUFaultInfo fi = {};
13175 MemTxAttrs attrs = {};
13176 hwaddr phys_addr;
13177 ARMMMUIdx mmu_idx;
13178 uint32_t mregion;
13179 bool targetpriv;
13180 bool targetsec = env->v7m.secure;
13181 bool is_subpage;
13184 * Work out what the security state and privilege level we're
13185 * interested in is...
13187 if (alt) {
13188 targetsec = !targetsec;
13191 if (forceunpriv) {
13192 targetpriv = false;
13193 } else {
13194 targetpriv = arm_v7m_is_handler_mode(env) ||
13195 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK);
13198 /* ...and then figure out which MMU index this is */
13199 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv);
13202 * We know that the MPU and SAU don't care about the access type
13203 * for our purposes beyond that we don't want to claim to be
13204 * an insn fetch, so we arbitrarily call this a read.
13208 * MPU region info only available for privileged or if
13209 * inspecting the other MPU state.
13211 if (arm_current_el(env) != 0 || alt) {
13212 /* We can ignore the return value as prot is always set */
13213 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
13214 &phys_addr, &attrs, &prot, &is_subpage,
13215 &fi, &mregion);
13216 if (mregion == -1) {
13217 mrvalid = false;
13218 mregion = 0;
13219 } else {
13220 mrvalid = true;
13222 r = prot & PAGE_READ;
13223 rw = prot & PAGE_WRITE;
13224 } else {
13225 r = false;
13226 rw = false;
13227 mrvalid = false;
13228 mregion = 0;
13231 if (env->v7m.secure) {
13232 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
13233 nsr = sattrs.ns && r;
13234 nsrw = sattrs.ns && rw;
13235 } else {
13236 sattrs.ns = true;
13237 nsr = false;
13238 nsrw = false;
13241 tt_resp = (sattrs.iregion << 24) |
13242 (sattrs.irvalid << 23) |
13243 ((!sattrs.ns) << 22) |
13244 (nsrw << 21) |
13245 (nsr << 20) |
13246 (rw << 19) |
13247 (r << 18) |
13248 (sattrs.srvalid << 17) |
13249 (mrvalid << 16) |
13250 (sattrs.sregion << 8) |
13251 mregion;
13253 return tt_resp;
13256 #endif
13258 bool arm_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
13259 MMUAccessType access_type, int mmu_idx,
13260 bool probe, uintptr_t retaddr)
13262 ARMCPU *cpu = ARM_CPU(cs);
13264 #ifdef CONFIG_USER_ONLY
13265 cpu->env.exception.vaddress = address;
13266 if (access_type == MMU_INST_FETCH) {
13267 cs->exception_index = EXCP_PREFETCH_ABORT;
13268 } else {
13269 cs->exception_index = EXCP_DATA_ABORT;
13271 cpu_loop_exit_restore(cs, retaddr);
13272 #else
13273 hwaddr phys_addr;
13274 target_ulong page_size;
13275 int prot, ret;
13276 MemTxAttrs attrs = {};
13277 ARMMMUFaultInfo fi = {};
13280 * Walk the page table and (if the mapping exists) add the page
13281 * to the TLB. On success, return true. Otherwise, if probing,
13282 * return false. Otherwise populate fsr with ARM DFSR/IFSR fault
13283 * register format, and signal the fault.
13285 ret = get_phys_addr(&cpu->env, address, access_type,
13286 core_to_arm_mmu_idx(&cpu->env, mmu_idx),
13287 &phys_addr, &attrs, &prot, &page_size, &fi, NULL);
13288 if (likely(!ret)) {
13290 * Map a single [sub]page. Regions smaller than our declared
13291 * target page size are handled specially, so for those we
13292 * pass in the exact addresses.
13294 if (page_size >= TARGET_PAGE_SIZE) {
13295 phys_addr &= TARGET_PAGE_MASK;
13296 address &= TARGET_PAGE_MASK;
13298 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
13299 prot, mmu_idx, page_size);
13300 return true;
13301 } else if (probe) {
13302 return false;
13303 } else {
13304 /* now we have a real cpu fault */
13305 cpu_restore_state(cs, retaddr, true);
13306 arm_deliver_fault(cpu, address, access_type, mmu_idx, &fi);
13308 #endif
13311 /* Note that signed overflow is undefined in C. The following routines are
13312 careful to use unsigned types where modulo arithmetic is required.
13313 Failure to do so _will_ break on newer gcc. */
13315 /* Signed saturating arithmetic. */
13317 /* Perform 16-bit signed saturating addition. */
13318 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
13320 uint16_t res;
13322 res = a + b;
13323 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
13324 if (a & 0x8000)
13325 res = 0x8000;
13326 else
13327 res = 0x7fff;
13329 return res;
13332 /* Perform 8-bit signed saturating addition. */
13333 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
13335 uint8_t res;
13337 res = a + b;
13338 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
13339 if (a & 0x80)
13340 res = 0x80;
13341 else
13342 res = 0x7f;
13344 return res;
13347 /* Perform 16-bit signed saturating subtraction. */
13348 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
13350 uint16_t res;
13352 res = a - b;
13353 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
13354 if (a & 0x8000)
13355 res = 0x8000;
13356 else
13357 res = 0x7fff;
13359 return res;
13362 /* Perform 8-bit signed saturating subtraction. */
13363 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
13365 uint8_t res;
13367 res = a - b;
13368 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
13369 if (a & 0x80)
13370 res = 0x80;
13371 else
13372 res = 0x7f;
13374 return res;
13377 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
13378 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
13379 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
13380 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
13381 #define PFX q
13383 #include "op_addsub.h"
13385 /* Unsigned saturating arithmetic. */
13386 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
13388 uint16_t res;
13389 res = a + b;
13390 if (res < a)
13391 res = 0xffff;
13392 return res;
13395 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
13397 if (a > b)
13398 return a - b;
13399 else
13400 return 0;
13403 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
13405 uint8_t res;
13406 res = a + b;
13407 if (res < a)
13408 res = 0xff;
13409 return res;
13412 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
13414 if (a > b)
13415 return a - b;
13416 else
13417 return 0;
13420 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
13421 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
13422 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
13423 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
13424 #define PFX uq
13426 #include "op_addsub.h"
13428 /* Signed modulo arithmetic. */
13429 #define SARITH16(a, b, n, op) do { \
13430 int32_t sum; \
13431 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
13432 RESULT(sum, n, 16); \
13433 if (sum >= 0) \
13434 ge |= 3 << (n * 2); \
13435 } while(0)
13437 #define SARITH8(a, b, n, op) do { \
13438 int32_t sum; \
13439 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
13440 RESULT(sum, n, 8); \
13441 if (sum >= 0) \
13442 ge |= 1 << n; \
13443 } while(0)
13446 #define ADD16(a, b, n) SARITH16(a, b, n, +)
13447 #define SUB16(a, b, n) SARITH16(a, b, n, -)
13448 #define ADD8(a, b, n) SARITH8(a, b, n, +)
13449 #define SUB8(a, b, n) SARITH8(a, b, n, -)
13450 #define PFX s
13451 #define ARITH_GE
13453 #include "op_addsub.h"
13455 /* Unsigned modulo arithmetic. */
13456 #define ADD16(a, b, n) do { \
13457 uint32_t sum; \
13458 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
13459 RESULT(sum, n, 16); \
13460 if ((sum >> 16) == 1) \
13461 ge |= 3 << (n * 2); \
13462 } while(0)
13464 #define ADD8(a, b, n) do { \
13465 uint32_t sum; \
13466 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
13467 RESULT(sum, n, 8); \
13468 if ((sum >> 8) == 1) \
13469 ge |= 1 << n; \
13470 } while(0)
13472 #define SUB16(a, b, n) do { \
13473 uint32_t sum; \
13474 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
13475 RESULT(sum, n, 16); \
13476 if ((sum >> 16) == 0) \
13477 ge |= 3 << (n * 2); \
13478 } while(0)
13480 #define SUB8(a, b, n) do { \
13481 uint32_t sum; \
13482 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
13483 RESULT(sum, n, 8); \
13484 if ((sum >> 8) == 0) \
13485 ge |= 1 << n; \
13486 } while(0)
13488 #define PFX u
13489 #define ARITH_GE
13491 #include "op_addsub.h"
13493 /* Halved signed arithmetic. */
13494 #define ADD16(a, b, n) \
13495 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
13496 #define SUB16(a, b, n) \
13497 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
13498 #define ADD8(a, b, n) \
13499 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
13500 #define SUB8(a, b, n) \
13501 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
13502 #define PFX sh
13504 #include "op_addsub.h"
13506 /* Halved unsigned arithmetic. */
13507 #define ADD16(a, b, n) \
13508 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
13509 #define SUB16(a, b, n) \
13510 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
13511 #define ADD8(a, b, n) \
13512 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
13513 #define SUB8(a, b, n) \
13514 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
13515 #define PFX uh
13517 #include "op_addsub.h"
13519 static inline uint8_t do_usad(uint8_t a, uint8_t b)
13521 if (a > b)
13522 return a - b;
13523 else
13524 return b - a;
13527 /* Unsigned sum of absolute byte differences. */
13528 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
13530 uint32_t sum;
13531 sum = do_usad(a, b);
13532 sum += do_usad(a >> 8, b >> 8);
13533 sum += do_usad(a >> 16, b >>16);
13534 sum += do_usad(a >> 24, b >> 24);
13535 return sum;
13538 /* For ARMv6 SEL instruction. */
13539 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
13541 uint32_t mask;
13543 mask = 0;
13544 if (flags & 1)
13545 mask |= 0xff;
13546 if (flags & 2)
13547 mask |= 0xff00;
13548 if (flags & 4)
13549 mask |= 0xff0000;
13550 if (flags & 8)
13551 mask |= 0xff000000;
13552 return (a & mask) | (b & ~mask);
13555 /* CRC helpers.
13556 * The upper bytes of val (above the number specified by 'bytes') must have
13557 * been zeroed out by the caller.
13559 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
13561 uint8_t buf[4];
13563 stl_le_p(buf, val);
13565 /* zlib crc32 converts the accumulator and output to one's complement. */
13566 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
13569 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
13571 uint8_t buf[4];
13573 stl_le_p(buf, val);
13575 /* Linux crc32c converts the output to one's complement. */
13576 return crc32c(acc, buf, bytes) ^ 0xffffffff;
13579 /* Return the exception level to which FP-disabled exceptions should
13580 * be taken, or 0 if FP is enabled.
13582 int fp_exception_el(CPUARMState *env, int cur_el)
13584 #ifndef CONFIG_USER_ONLY
13585 int fpen;
13587 /* CPACR and the CPTR registers don't exist before v6, so FP is
13588 * always accessible
13590 if (!arm_feature(env, ARM_FEATURE_V6)) {
13591 return 0;
13594 if (arm_feature(env, ARM_FEATURE_M)) {
13595 /* CPACR can cause a NOCP UsageFault taken to current security state */
13596 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
13597 return 1;
13600 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
13601 if (!extract32(env->v7m.nsacr, 10, 1)) {
13602 /* FP insns cause a NOCP UsageFault taken to Secure */
13603 return 3;
13607 return 0;
13610 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
13611 * 0, 2 : trap EL0 and EL1/PL1 accesses
13612 * 1 : trap only EL0 accesses
13613 * 3 : trap no accesses
13615 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
13616 switch (fpen) {
13617 case 0:
13618 case 2:
13619 if (cur_el == 0 || cur_el == 1) {
13620 /* Trap to PL1, which might be EL1 or EL3 */
13621 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
13622 return 3;
13624 return 1;
13626 if (cur_el == 3 && !is_a64(env)) {
13627 /* Secure PL1 running at EL3 */
13628 return 3;
13630 break;
13631 case 1:
13632 if (cur_el == 0) {
13633 return 1;
13635 break;
13636 case 3:
13637 break;
13641 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
13642 * to control non-secure access to the FPU. It doesn't have any
13643 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
13645 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
13646 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
13647 if (!extract32(env->cp15.nsacr, 10, 1)) {
13648 /* FP insns act as UNDEF */
13649 return cur_el == 2 ? 2 : 1;
13653 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
13654 * check because zero bits in the registers mean "don't trap".
13657 /* CPTR_EL2 : present in v7VE or v8 */
13658 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
13659 && !arm_is_secure_below_el3(env)) {
13660 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
13661 return 2;
13664 /* CPTR_EL3 : present in v8 */
13665 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
13666 /* Trap all FP ops to EL3 */
13667 return 3;
13669 #endif
13670 return 0;
13673 ARMMMUIdx arm_v7m_mmu_idx_all(CPUARMState *env,
13674 bool secstate, bool priv, bool negpri)
13676 ARMMMUIdx mmu_idx = ARM_MMU_IDX_M;
13678 if (priv) {
13679 mmu_idx |= ARM_MMU_IDX_M_PRIV;
13682 if (negpri) {
13683 mmu_idx |= ARM_MMU_IDX_M_NEGPRI;
13686 if (secstate) {
13687 mmu_idx |= ARM_MMU_IDX_M_S;
13690 return mmu_idx;
13693 ARMMMUIdx arm_v7m_mmu_idx_for_secstate_and_priv(CPUARMState *env,
13694 bool secstate, bool priv)
13696 bool negpri = armv7m_nvic_neg_prio_requested(env->nvic, secstate);
13698 return arm_v7m_mmu_idx_all(env, secstate, priv, negpri);
13701 /* Return the MMU index for a v7M CPU in the specified security state */
13702 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
13704 bool priv = arm_current_el(env) != 0;
13706 return arm_v7m_mmu_idx_for_secstate_and_priv(env, secstate, priv);
13709 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
13711 int el;
13713 if (arm_feature(env, ARM_FEATURE_M)) {
13714 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
13717 el = arm_current_el(env);
13718 if (el < 2 && arm_is_secure_below_el3(env)) {
13719 return ARMMMUIdx_S1SE0 + el;
13720 } else {
13721 return ARMMMUIdx_S12NSE0 + el;
13725 int cpu_mmu_index(CPUARMState *env, bool ifetch)
13727 return arm_to_core_mmu_idx(arm_mmu_idx(env));
13730 #ifndef CONFIG_USER_ONLY
13731 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
13733 return stage_1_mmu_idx(arm_mmu_idx(env));
13735 #endif
13737 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
13738 target_ulong *cs_base, uint32_t *pflags)
13740 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
13741 int current_el = arm_current_el(env);
13742 int fp_el = fp_exception_el(env, current_el);
13743 uint32_t flags = 0;
13745 if (is_a64(env)) {
13746 ARMCPU *cpu = env_archcpu(env);
13747 uint64_t sctlr;
13749 *pc = env->pc;
13750 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
13752 /* Get control bits for tagged addresses. */
13754 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
13755 ARMVAParameters p0 = aa64_va_parameters_both(env, 0, stage1);
13756 int tbii, tbid;
13758 /* FIXME: ARMv8.1-VHE S2 translation regime. */
13759 if (regime_el(env, stage1) < 2) {
13760 ARMVAParameters p1 = aa64_va_parameters_both(env, -1, stage1);
13761 tbid = (p1.tbi << 1) | p0.tbi;
13762 tbii = tbid & ~((p1.tbid << 1) | p0.tbid);
13763 } else {
13764 tbid = p0.tbi;
13765 tbii = tbid & !p0.tbid;
13768 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
13769 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
13772 if (cpu_isar_feature(aa64_sve, cpu)) {
13773 int sve_el = sve_exception_el(env, current_el);
13774 uint32_t zcr_len;
13776 /* If SVE is disabled, but FP is enabled,
13777 * then the effective len is 0.
13779 if (sve_el != 0 && fp_el == 0) {
13780 zcr_len = 0;
13781 } else {
13782 zcr_len = sve_zcr_len_for_el(env, current_el);
13784 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
13785 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
13788 sctlr = arm_sctlr(env, current_el);
13790 if (cpu_isar_feature(aa64_pauth, cpu)) {
13792 * In order to save space in flags, we record only whether
13793 * pauth is "inactive", meaning all insns are implemented as
13794 * a nop, or "active" when some action must be performed.
13795 * The decision of which action to take is left to a helper.
13797 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
13798 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
13802 if (cpu_isar_feature(aa64_bti, cpu)) {
13803 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
13804 if (sctlr & (current_el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
13805 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
13807 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
13809 } else {
13810 *pc = env->regs[15];
13811 flags = FIELD_DP32(flags, TBFLAG_A32, THUMB, env->thumb);
13812 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN, env->vfp.vec_len);
13813 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE, env->vfp.vec_stride);
13814 flags = FIELD_DP32(flags, TBFLAG_A32, CONDEXEC, env->condexec_bits);
13815 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, arm_sctlr_b(env));
13816 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
13817 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)
13818 || arm_el_is_aa64(env, 1) || arm_feature(env, ARM_FEATURE_M)) {
13819 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
13821 /* Note that XSCALE_CPAR shares bits with VECSTRIDE */
13822 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
13823 flags = FIELD_DP32(flags, TBFLAG_A32,
13824 XSCALE_CPAR, env->cp15.c15_cpar);
13828 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
13830 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
13831 * states defined in the ARM ARM for software singlestep:
13832 * SS_ACTIVE PSTATE.SS State
13833 * 0 x Inactive (the TB flag for SS is always 0)
13834 * 1 0 Active-pending
13835 * 1 1 Active-not-pending
13837 if (arm_singlestep_active(env)) {
13838 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
13839 if (is_a64(env)) {
13840 if (env->pstate & PSTATE_SS) {
13841 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
13843 } else {
13844 if (env->uncached_cpsr & PSTATE_SS) {
13845 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
13849 if (arm_cpu_data_is_big_endian(env)) {
13850 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
13852 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
13854 if (arm_v7m_is_handler_mode(env)) {
13855 flags = FIELD_DP32(flags, TBFLAG_A32, HANDLER, 1);
13858 /* v8M always applies stack limit checks unless CCR.STKOFHFNMIGN is
13859 * suppressing them because the requested execution priority is less than 0.
13861 if (arm_feature(env, ARM_FEATURE_V8) &&
13862 arm_feature(env, ARM_FEATURE_M) &&
13863 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
13864 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
13865 flags = FIELD_DP32(flags, TBFLAG_A32, STACKCHECK, 1);
13868 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
13869 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S) != env->v7m.secure) {
13870 flags = FIELD_DP32(flags, TBFLAG_A32, FPCCR_S_WRONG, 1);
13873 if (arm_feature(env, ARM_FEATURE_M) &&
13874 (env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
13875 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
13876 (env->v7m.secure &&
13877 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
13879 * ASPEN is set, but FPCA/SFPA indicate that there is no active
13880 * FP context; we must create a new FP context before executing
13881 * any FP insn.
13883 flags = FIELD_DP32(flags, TBFLAG_A32, NEW_FP_CTXT_NEEDED, 1);
13886 if (arm_feature(env, ARM_FEATURE_M)) {
13887 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
13889 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
13890 flags = FIELD_DP32(flags, TBFLAG_A32, LSPACT, 1);
13894 *pflags = flags;
13895 *cs_base = 0;
13898 #ifdef TARGET_AARCH64
13900 * The manual says that when SVE is enabled and VQ is widened the
13901 * implementation is allowed to zero the previously inaccessible
13902 * portion of the registers. The corollary to that is that when
13903 * SVE is enabled and VQ is narrowed we are also allowed to zero
13904 * the now inaccessible portion of the registers.
13906 * The intent of this is that no predicate bit beyond VQ is ever set.
13907 * Which means that some operations on predicate registers themselves
13908 * may operate on full uint64_t or even unrolled across the maximum
13909 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
13910 * may well be cheaper than conditionals to restrict the operation
13911 * to the relevant portion of a uint16_t[16].
13913 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
13915 int i, j;
13916 uint64_t pmask;
13918 assert(vq >= 1 && vq <= ARM_MAX_VQ);
13919 assert(vq <= env_archcpu(env)->sve_max_vq);
13921 /* Zap the high bits of the zregs. */
13922 for (i = 0; i < 32; i++) {
13923 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
13926 /* Zap the high bits of the pregs and ffr. */
13927 pmask = 0;
13928 if (vq & 3) {
13929 pmask = ~(-1ULL << (16 * (vq & 3)));
13931 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13932 for (i = 0; i < 17; ++i) {
13933 env->vfp.pregs[i].p[j] &= pmask;
13935 pmask = 0;
13940 * Notice a change in SVE vector size when changing EL.
13942 void aarch64_sve_change_el(CPUARMState *env, int old_el,
13943 int new_el, bool el0_a64)
13945 ARMCPU *cpu = env_archcpu(env);
13946 int old_len, new_len;
13947 bool old_a64, new_a64;
13949 /* Nothing to do if no SVE. */
13950 if (!cpu_isar_feature(aa64_sve, cpu)) {
13951 return;
13954 /* Nothing to do if FP is disabled in either EL. */
13955 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
13956 return;
13960 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
13961 * at ELx, or not available because the EL is in AArch32 state, then
13962 * for all purposes other than a direct read, the ZCR_ELx.LEN field
13963 * has an effective value of 0".
13965 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
13966 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
13967 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
13968 * we already have the correct register contents when encountering the
13969 * vq0->vq0 transition between EL0->EL1.
13971 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
13972 old_len = (old_a64 && !sve_exception_el(env, old_el)
13973 ? sve_zcr_len_for_el(env, old_el) : 0);
13974 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
13975 new_len = (new_a64 && !sve_exception_el(env, new_el)
13976 ? sve_zcr_len_for_el(env, new_el) : 0);
13978 /* When changing vector length, clear inaccessible state. */
13979 if (new_len < old_len) {
13980 aarch64_sve_narrow_vq(env, new_len + 1);
13983 #endif