virtio-gpu: fix crashes upon warm reboot with vga mode
[qemu/ar7.git] / target / arm / helper.c
blob66afb08ee0ffe4f8211f45227d4cb49401e1a1cd
1 #include "qemu/osdep.h"
2 #include "target/arm/idau.h"
3 #include "trace.h"
4 #include "cpu.h"
5 #include "internals.h"
6 #include "exec/gdbstub.h"
7 #include "exec/helper-proto.h"
8 #include "qemu/host-utils.h"
9 #include "sysemu/arch_init.h"
10 #include "sysemu/sysemu.h"
11 #include "qemu/bitops.h"
12 #include "qemu/crc32c.h"
13 #include "exec/exec-all.h"
14 #include "exec/cpu_ldst.h"
15 #include "arm_ldst.h"
16 #include <zlib.h> /* For crc32 */
17 #include "exec/semihost.h"
18 #include "sysemu/kvm.h"
19 #include "fpu/softfloat.h"
20 #include "qemu/range.h"
22 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
24 #ifndef CONFIG_USER_ONLY
25 /* Cacheability and shareability attributes for a memory access */
26 typedef struct ARMCacheAttrs {
27 unsigned int attrs:8; /* as in the MAIR register encoding */
28 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */
29 } ARMCacheAttrs;
31 static bool get_phys_addr(CPUARMState *env, target_ulong address,
32 MMUAccessType access_type, ARMMMUIdx mmu_idx,
33 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
34 target_ulong *page_size,
35 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
37 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
38 MMUAccessType access_type, ARMMMUIdx mmu_idx,
39 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
40 target_ulong *page_size_ptr,
41 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
43 /* Security attributes for an address, as returned by v8m_security_lookup. */
44 typedef struct V8M_SAttributes {
45 bool subpage; /* true if these attrs don't cover the whole TARGET_PAGE */
46 bool ns;
47 bool nsc;
48 uint8_t sregion;
49 bool srvalid;
50 uint8_t iregion;
51 bool irvalid;
52 } V8M_SAttributes;
54 static void v8m_security_lookup(CPUARMState *env, uint32_t address,
55 MMUAccessType access_type, ARMMMUIdx mmu_idx,
56 V8M_SAttributes *sattrs);
57 #endif
59 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
61 int nregs;
63 /* VFP data registers are always little-endian. */
64 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
65 if (reg < nregs) {
66 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
67 return 8;
69 if (arm_feature(env, ARM_FEATURE_NEON)) {
70 /* Aliases for Q regs. */
71 nregs += 16;
72 if (reg < nregs) {
73 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
74 stq_le_p(buf, q[0]);
75 stq_le_p(buf + 8, q[1]);
76 return 16;
79 switch (reg - nregs) {
80 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
81 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
82 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
84 return 0;
87 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
89 int nregs;
91 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
92 if (reg < nregs) {
93 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
94 return 8;
96 if (arm_feature(env, ARM_FEATURE_NEON)) {
97 nregs += 16;
98 if (reg < nregs) {
99 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
100 q[0] = ldq_le_p(buf);
101 q[1] = ldq_le_p(buf + 8);
102 return 16;
105 switch (reg - nregs) {
106 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
107 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
108 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
110 return 0;
113 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
115 switch (reg) {
116 case 0 ... 31:
117 /* 128 bit FP register */
119 uint64_t *q = aa64_vfp_qreg(env, reg);
120 stq_le_p(buf, q[0]);
121 stq_le_p(buf + 8, q[1]);
122 return 16;
124 case 32:
125 /* FPSR */
126 stl_p(buf, vfp_get_fpsr(env));
127 return 4;
128 case 33:
129 /* FPCR */
130 stl_p(buf, vfp_get_fpcr(env));
131 return 4;
132 default:
133 return 0;
137 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
139 switch (reg) {
140 case 0 ... 31:
141 /* 128 bit FP register */
143 uint64_t *q = aa64_vfp_qreg(env, reg);
144 q[0] = ldq_le_p(buf);
145 q[1] = ldq_le_p(buf + 8);
146 return 16;
148 case 32:
149 /* FPSR */
150 vfp_set_fpsr(env, ldl_p(buf));
151 return 4;
152 case 33:
153 /* FPCR */
154 vfp_set_fpcr(env, ldl_p(buf));
155 return 4;
156 default:
157 return 0;
161 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
163 assert(ri->fieldoffset);
164 if (cpreg_field_is_64bit(ri)) {
165 return CPREG_FIELD64(env, ri);
166 } else {
167 return CPREG_FIELD32(env, ri);
171 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
172 uint64_t value)
174 assert(ri->fieldoffset);
175 if (cpreg_field_is_64bit(ri)) {
176 CPREG_FIELD64(env, ri) = value;
177 } else {
178 CPREG_FIELD32(env, ri) = value;
182 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
184 return (char *)env + ri->fieldoffset;
187 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
189 /* Raw read of a coprocessor register (as needed for migration, etc). */
190 if (ri->type & ARM_CP_CONST) {
191 return ri->resetvalue;
192 } else if (ri->raw_readfn) {
193 return ri->raw_readfn(env, ri);
194 } else if (ri->readfn) {
195 return ri->readfn(env, ri);
196 } else {
197 return raw_read(env, ri);
201 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
202 uint64_t v)
204 /* Raw write of a coprocessor register (as needed for migration, etc).
205 * Note that constant registers are treated as write-ignored; the
206 * caller should check for success by whether a readback gives the
207 * value written.
209 if (ri->type & ARM_CP_CONST) {
210 return;
211 } else if (ri->raw_writefn) {
212 ri->raw_writefn(env, ri, v);
213 } else if (ri->writefn) {
214 ri->writefn(env, ri, v);
215 } else {
216 raw_write(env, ri, v);
220 static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg)
222 ARMCPU *cpu = arm_env_get_cpu(env);
223 const ARMCPRegInfo *ri;
224 uint32_t key;
226 key = cpu->dyn_xml.cpregs_keys[reg];
227 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
228 if (ri) {
229 if (cpreg_field_is_64bit(ri)) {
230 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
231 } else {
232 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
235 return 0;
238 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
240 return 0;
243 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
245 /* Return true if the regdef would cause an assertion if you called
246 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
247 * program bug for it not to have the NO_RAW flag).
248 * NB that returning false here doesn't necessarily mean that calling
249 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
250 * read/write access functions which are safe for raw use" from "has
251 * read/write access functions which have side effects but has forgotten
252 * to provide raw access functions".
253 * The tests here line up with the conditions in read/write_raw_cp_reg()
254 * and assertions in raw_read()/raw_write().
256 if ((ri->type & ARM_CP_CONST) ||
257 ri->fieldoffset ||
258 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
259 return false;
261 return true;
264 bool write_cpustate_to_list(ARMCPU *cpu)
266 /* Write the coprocessor state from cpu->env to the (index,value) list. */
267 int i;
268 bool ok = true;
270 for (i = 0; i < cpu->cpreg_array_len; i++) {
271 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
272 const ARMCPRegInfo *ri;
274 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
275 if (!ri) {
276 ok = false;
277 continue;
279 if (ri->type & ARM_CP_NO_RAW) {
280 continue;
282 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
284 return ok;
287 bool write_list_to_cpustate(ARMCPU *cpu)
289 int i;
290 bool ok = true;
292 for (i = 0; i < cpu->cpreg_array_len; i++) {
293 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
294 uint64_t v = cpu->cpreg_values[i];
295 const ARMCPRegInfo *ri;
297 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
298 if (!ri) {
299 ok = false;
300 continue;
302 if (ri->type & ARM_CP_NO_RAW) {
303 continue;
305 /* Write value and confirm it reads back as written
306 * (to catch read-only registers and partially read-only
307 * registers where the incoming migration value doesn't match)
309 write_raw_cp_reg(&cpu->env, ri, v);
310 if (read_raw_cp_reg(&cpu->env, ri) != v) {
311 ok = false;
314 return ok;
317 static void add_cpreg_to_list(gpointer key, gpointer opaque)
319 ARMCPU *cpu = opaque;
320 uint64_t regidx;
321 const ARMCPRegInfo *ri;
323 regidx = *(uint32_t *)key;
324 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
326 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
327 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
328 /* The value array need not be initialized at this point */
329 cpu->cpreg_array_len++;
333 static void count_cpreg(gpointer key, gpointer opaque)
335 ARMCPU *cpu = opaque;
336 uint64_t regidx;
337 const ARMCPRegInfo *ri;
339 regidx = *(uint32_t *)key;
340 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
342 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
343 cpu->cpreg_array_len++;
347 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
349 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
350 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
352 if (aidx > bidx) {
353 return 1;
355 if (aidx < bidx) {
356 return -1;
358 return 0;
361 void init_cpreg_list(ARMCPU *cpu)
363 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
364 * Note that we require cpreg_tuples[] to be sorted by key ID.
366 GList *keys;
367 int arraylen;
369 keys = g_hash_table_get_keys(cpu->cp_regs);
370 keys = g_list_sort(keys, cpreg_key_compare);
372 cpu->cpreg_array_len = 0;
374 g_list_foreach(keys, count_cpreg, cpu);
376 arraylen = cpu->cpreg_array_len;
377 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
378 cpu->cpreg_values = g_new(uint64_t, arraylen);
379 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
380 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
381 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
382 cpu->cpreg_array_len = 0;
384 g_list_foreach(keys, add_cpreg_to_list, cpu);
386 assert(cpu->cpreg_array_len == arraylen);
388 g_list_free(keys);
392 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
393 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
395 * access_el3_aa32ns: Used to check AArch32 register views.
396 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
398 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
399 const ARMCPRegInfo *ri,
400 bool isread)
402 bool secure = arm_is_secure_below_el3(env);
404 assert(!arm_el_is_aa64(env, 3));
405 if (secure) {
406 return CP_ACCESS_TRAP_UNCATEGORIZED;
408 return CP_ACCESS_OK;
411 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
412 const ARMCPRegInfo *ri,
413 bool isread)
415 if (!arm_el_is_aa64(env, 3)) {
416 return access_el3_aa32ns(env, ri, isread);
418 return CP_ACCESS_OK;
421 /* Some secure-only AArch32 registers trap to EL3 if used from
422 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
423 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
424 * We assume that the .access field is set to PL1_RW.
426 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
427 const ARMCPRegInfo *ri,
428 bool isread)
430 if (arm_current_el(env) == 3) {
431 return CP_ACCESS_OK;
433 if (arm_is_secure_below_el3(env)) {
434 return CP_ACCESS_TRAP_EL3;
436 /* This will be EL1 NS and EL2 NS, which just UNDEF */
437 return CP_ACCESS_TRAP_UNCATEGORIZED;
440 /* Check for traps to "powerdown debug" registers, which are controlled
441 * by MDCR.TDOSA
443 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
444 bool isread)
446 int el = arm_current_el(env);
448 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
449 && !arm_is_secure_below_el3(env)) {
450 return CP_ACCESS_TRAP_EL2;
452 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
453 return CP_ACCESS_TRAP_EL3;
455 return CP_ACCESS_OK;
458 /* Check for traps to "debug ROM" registers, which are controlled
459 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
461 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
462 bool isread)
464 int el = arm_current_el(env);
466 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
467 && !arm_is_secure_below_el3(env)) {
468 return CP_ACCESS_TRAP_EL2;
470 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
471 return CP_ACCESS_TRAP_EL3;
473 return CP_ACCESS_OK;
476 /* Check for traps to general debug registers, which are controlled
477 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
479 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
480 bool isread)
482 int el = arm_current_el(env);
484 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
485 && !arm_is_secure_below_el3(env)) {
486 return CP_ACCESS_TRAP_EL2;
488 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
489 return CP_ACCESS_TRAP_EL3;
491 return CP_ACCESS_OK;
494 /* Check for traps to performance monitor registers, which are controlled
495 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
497 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
498 bool isread)
500 int el = arm_current_el(env);
502 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
503 && !arm_is_secure_below_el3(env)) {
504 return CP_ACCESS_TRAP_EL2;
506 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
507 return CP_ACCESS_TRAP_EL3;
509 return CP_ACCESS_OK;
512 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
514 ARMCPU *cpu = arm_env_get_cpu(env);
516 raw_write(env, ri, value);
517 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
520 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
522 ARMCPU *cpu = arm_env_get_cpu(env);
524 if (raw_read(env, ri) != value) {
525 /* Unlike real hardware the qemu TLB uses virtual addresses,
526 * not modified virtual addresses, so this causes a TLB flush.
528 tlb_flush(CPU(cpu));
529 raw_write(env, ri, value);
533 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
534 uint64_t value)
536 ARMCPU *cpu = arm_env_get_cpu(env);
538 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
539 && !extended_addresses_enabled(env)) {
540 /* For VMSA (when not using the LPAE long descriptor page table
541 * format) this register includes the ASID, so do a TLB flush.
542 * For PMSA it is purely a process ID and no action is needed.
544 tlb_flush(CPU(cpu));
546 raw_write(env, ri, value);
549 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
550 uint64_t value)
552 /* Invalidate all (TLBIALL) */
553 ARMCPU *cpu = arm_env_get_cpu(env);
555 tlb_flush(CPU(cpu));
558 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
559 uint64_t value)
561 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
562 ARMCPU *cpu = arm_env_get_cpu(env);
564 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
567 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
568 uint64_t value)
570 /* Invalidate by ASID (TLBIASID) */
571 ARMCPU *cpu = arm_env_get_cpu(env);
573 tlb_flush(CPU(cpu));
576 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
577 uint64_t value)
579 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
580 ARMCPU *cpu = arm_env_get_cpu(env);
582 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
585 /* IS variants of TLB operations must affect all cores */
586 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
587 uint64_t value)
589 CPUState *cs = ENV_GET_CPU(env);
591 tlb_flush_all_cpus_synced(cs);
594 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
595 uint64_t value)
597 CPUState *cs = ENV_GET_CPU(env);
599 tlb_flush_all_cpus_synced(cs);
602 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
603 uint64_t value)
605 CPUState *cs = ENV_GET_CPU(env);
607 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
610 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
611 uint64_t value)
613 CPUState *cs = ENV_GET_CPU(env);
615 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
618 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
619 uint64_t value)
621 CPUState *cs = ENV_GET_CPU(env);
623 tlb_flush_by_mmuidx(cs,
624 ARMMMUIdxBit_S12NSE1 |
625 ARMMMUIdxBit_S12NSE0 |
626 ARMMMUIdxBit_S2NS);
629 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
630 uint64_t value)
632 CPUState *cs = ENV_GET_CPU(env);
634 tlb_flush_by_mmuidx_all_cpus_synced(cs,
635 ARMMMUIdxBit_S12NSE1 |
636 ARMMMUIdxBit_S12NSE0 |
637 ARMMMUIdxBit_S2NS);
640 static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
641 uint64_t value)
643 /* Invalidate by IPA. This has to invalidate any structures that
644 * contain only stage 2 translation information, but does not need
645 * to apply to structures that contain combined stage 1 and stage 2
646 * translation information.
647 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
649 CPUState *cs = ENV_GET_CPU(env);
650 uint64_t pageaddr;
652 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
653 return;
656 pageaddr = sextract64(value << 12, 0, 40);
658 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
661 static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
662 uint64_t value)
664 CPUState *cs = ENV_GET_CPU(env);
665 uint64_t pageaddr;
667 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
668 return;
671 pageaddr = sextract64(value << 12, 0, 40);
673 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
674 ARMMMUIdxBit_S2NS);
677 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
678 uint64_t value)
680 CPUState *cs = ENV_GET_CPU(env);
682 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
685 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
686 uint64_t value)
688 CPUState *cs = ENV_GET_CPU(env);
690 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
693 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
694 uint64_t value)
696 CPUState *cs = ENV_GET_CPU(env);
697 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
699 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
702 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
703 uint64_t value)
705 CPUState *cs = ENV_GET_CPU(env);
706 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
708 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
709 ARMMMUIdxBit_S1E2);
712 static const ARMCPRegInfo cp_reginfo[] = {
713 /* Define the secure and non-secure FCSE identifier CP registers
714 * separately because there is no secure bank in V8 (no _EL3). This allows
715 * the secure register to be properly reset and migrated. There is also no
716 * v8 EL1 version of the register so the non-secure instance stands alone.
718 { .name = "FCSEIDR",
719 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
720 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
721 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
722 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
723 { .name = "FCSEIDR_S",
724 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
725 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
726 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
727 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
728 /* Define the secure and non-secure context identifier CP registers
729 * separately because there is no secure bank in V8 (no _EL3). This allows
730 * the secure register to be properly reset and migrated. In the
731 * non-secure case, the 32-bit register will have reset and migration
732 * disabled during registration as it is handled by the 64-bit instance.
734 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
735 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
736 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
737 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
738 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
739 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
740 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
741 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
742 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
743 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
744 REGINFO_SENTINEL
747 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
748 /* NB: Some of these registers exist in v8 but with more precise
749 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
751 /* MMU Domain access control / MPU write buffer control */
752 { .name = "DACR",
753 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
754 .access = PL1_RW, .resetvalue = 0,
755 .writefn = dacr_write, .raw_writefn = raw_write,
756 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
757 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
758 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
759 * For v6 and v5, these mappings are overly broad.
761 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
762 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
763 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
764 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
765 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
766 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
767 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
768 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
769 /* Cache maintenance ops; some of this space may be overridden later. */
770 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
771 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
772 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
773 REGINFO_SENTINEL
776 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
777 /* Not all pre-v6 cores implemented this WFI, so this is slightly
778 * over-broad.
780 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
781 .access = PL1_W, .type = ARM_CP_WFI },
782 REGINFO_SENTINEL
785 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
786 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
787 * is UNPREDICTABLE; we choose to NOP as most implementations do).
789 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
790 .access = PL1_W, .type = ARM_CP_WFI },
791 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
792 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
793 * OMAPCP will override this space.
795 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
796 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
797 .resetvalue = 0 },
798 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
799 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
800 .resetvalue = 0 },
801 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
802 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
803 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
804 .resetvalue = 0 },
805 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
806 * implementing it as RAZ means the "debug architecture version" bits
807 * will read as a reserved value, which should cause Linux to not try
808 * to use the debug hardware.
810 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
811 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
812 /* MMU TLB control. Note that the wildcarding means we cover not just
813 * the unified TLB ops but also the dside/iside/inner-shareable variants.
815 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
816 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
817 .type = ARM_CP_NO_RAW },
818 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
819 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
820 .type = ARM_CP_NO_RAW },
821 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
822 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
823 .type = ARM_CP_NO_RAW },
824 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
825 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
826 .type = ARM_CP_NO_RAW },
827 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
828 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
829 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
830 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
831 REGINFO_SENTINEL
834 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
835 uint64_t value)
837 uint32_t mask = 0;
839 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
840 if (!arm_feature(env, ARM_FEATURE_V8)) {
841 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
842 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
843 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
845 if (arm_feature(env, ARM_FEATURE_VFP)) {
846 /* VFP coprocessor: cp10 & cp11 [23:20] */
847 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
849 if (!arm_feature(env, ARM_FEATURE_NEON)) {
850 /* ASEDIS [31] bit is RAO/WI */
851 value |= (1 << 31);
854 /* VFPv3 and upwards with NEON implement 32 double precision
855 * registers (D0-D31).
857 if (!arm_feature(env, ARM_FEATURE_NEON) ||
858 !arm_feature(env, ARM_FEATURE_VFP3)) {
859 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
860 value |= (1 << 30);
863 value &= mask;
865 env->cp15.cpacr_el1 = value;
868 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
870 /* Call cpacr_write() so that we reset with the correct RAO bits set
871 * for our CPU features.
873 cpacr_write(env, ri, 0);
876 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
877 bool isread)
879 if (arm_feature(env, ARM_FEATURE_V8)) {
880 /* Check if CPACR accesses are to be trapped to EL2 */
881 if (arm_current_el(env) == 1 &&
882 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
883 return CP_ACCESS_TRAP_EL2;
884 /* Check if CPACR accesses are to be trapped to EL3 */
885 } else if (arm_current_el(env) < 3 &&
886 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
887 return CP_ACCESS_TRAP_EL3;
891 return CP_ACCESS_OK;
894 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
895 bool isread)
897 /* Check if CPTR accesses are set to trap to EL3 */
898 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
899 return CP_ACCESS_TRAP_EL3;
902 return CP_ACCESS_OK;
905 static const ARMCPRegInfo v6_cp_reginfo[] = {
906 /* prefetch by MVA in v6, NOP in v7 */
907 { .name = "MVA_prefetch",
908 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
909 .access = PL1_W, .type = ARM_CP_NOP },
910 /* We need to break the TB after ISB to execute self-modifying code
911 * correctly and also to take any pending interrupts immediately.
912 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
914 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
915 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
916 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
917 .access = PL0_W, .type = ARM_CP_NOP },
918 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
919 .access = PL0_W, .type = ARM_CP_NOP },
920 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
921 .access = PL1_RW,
922 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
923 offsetof(CPUARMState, cp15.ifar_ns) },
924 .resetvalue = 0, },
925 /* Watchpoint Fault Address Register : should actually only be present
926 * for 1136, 1176, 11MPCore.
928 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
929 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
930 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
931 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
932 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
933 .resetfn = cpacr_reset, .writefn = cpacr_write },
934 REGINFO_SENTINEL
937 /* Definitions for the PMU registers */
938 #define PMCRN_MASK 0xf800
939 #define PMCRN_SHIFT 11
940 #define PMCRD 0x8
941 #define PMCRC 0x4
942 #define PMCRE 0x1
944 static inline uint32_t pmu_num_counters(CPUARMState *env)
946 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
949 /* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
950 static inline uint64_t pmu_counter_mask(CPUARMState *env)
952 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
955 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
956 bool isread)
958 /* Performance monitor registers user accessibility is controlled
959 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
960 * trapping to EL2 or EL3 for other accesses.
962 int el = arm_current_el(env);
964 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
965 return CP_ACCESS_TRAP;
967 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
968 && !arm_is_secure_below_el3(env)) {
969 return CP_ACCESS_TRAP_EL2;
971 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
972 return CP_ACCESS_TRAP_EL3;
975 return CP_ACCESS_OK;
978 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
979 const ARMCPRegInfo *ri,
980 bool isread)
982 /* ER: event counter read trap control */
983 if (arm_feature(env, ARM_FEATURE_V8)
984 && arm_current_el(env) == 0
985 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
986 && isread) {
987 return CP_ACCESS_OK;
990 return pmreg_access(env, ri, isread);
993 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
994 const ARMCPRegInfo *ri,
995 bool isread)
997 /* SW: software increment write trap control */
998 if (arm_feature(env, ARM_FEATURE_V8)
999 && arm_current_el(env) == 0
1000 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1001 && !isread) {
1002 return CP_ACCESS_OK;
1005 return pmreg_access(env, ri, isread);
1008 #ifndef CONFIG_USER_ONLY
1010 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1011 const ARMCPRegInfo *ri,
1012 bool isread)
1014 /* ER: event counter read trap control */
1015 if (arm_feature(env, ARM_FEATURE_V8)
1016 && arm_current_el(env) == 0
1017 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1018 return CP_ACCESS_OK;
1021 return pmreg_access(env, ri, isread);
1024 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1025 const ARMCPRegInfo *ri,
1026 bool isread)
1028 /* CR: cycle counter read trap control */
1029 if (arm_feature(env, ARM_FEATURE_V8)
1030 && arm_current_el(env) == 0
1031 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1032 && isread) {
1033 return CP_ACCESS_OK;
1036 return pmreg_access(env, ri, isread);
1039 static inline bool arm_ccnt_enabled(CPUARMState *env)
1041 /* This does not support checking PMCCFILTR_EL0 register */
1043 if (!(env->cp15.c9_pmcr & PMCRE) || !(env->cp15.c9_pmcnten & (1 << 31))) {
1044 return false;
1047 return true;
1050 void pmccntr_sync(CPUARMState *env)
1052 uint64_t temp_ticks;
1054 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1055 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1057 if (env->cp15.c9_pmcr & PMCRD) {
1058 /* Increment once every 64 processor clock cycles */
1059 temp_ticks /= 64;
1062 if (arm_ccnt_enabled(env)) {
1063 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
1067 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1068 uint64_t value)
1070 pmccntr_sync(env);
1072 if (value & PMCRC) {
1073 /* The counter has been reset */
1074 env->cp15.c15_ccnt = 0;
1077 /* only the DP, X, D and E bits are writable */
1078 env->cp15.c9_pmcr &= ~0x39;
1079 env->cp15.c9_pmcr |= (value & 0x39);
1081 pmccntr_sync(env);
1084 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1086 uint64_t total_ticks;
1088 if (!arm_ccnt_enabled(env)) {
1089 /* Counter is disabled, do not change value */
1090 return env->cp15.c15_ccnt;
1093 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1094 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1096 if (env->cp15.c9_pmcr & PMCRD) {
1097 /* Increment once every 64 processor clock cycles */
1098 total_ticks /= 64;
1100 return total_ticks - env->cp15.c15_ccnt;
1103 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1104 uint64_t value)
1106 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1107 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1108 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1109 * accessed.
1111 env->cp15.c9_pmselr = value & 0x1f;
1114 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1115 uint64_t value)
1117 uint64_t total_ticks;
1119 if (!arm_ccnt_enabled(env)) {
1120 /* Counter is disabled, set the absolute value */
1121 env->cp15.c15_ccnt = value;
1122 return;
1125 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1126 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1128 if (env->cp15.c9_pmcr & PMCRD) {
1129 /* Increment once every 64 processor clock cycles */
1130 total_ticks /= 64;
1132 env->cp15.c15_ccnt = total_ticks - value;
1135 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1136 uint64_t value)
1138 uint64_t cur_val = pmccntr_read(env, NULL);
1140 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1143 #else /* CONFIG_USER_ONLY */
1145 void pmccntr_sync(CPUARMState *env)
1149 #endif
1151 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1152 uint64_t value)
1154 pmccntr_sync(env);
1155 env->cp15.pmccfiltr_el0 = value & 0xfc000000;
1156 pmccntr_sync(env);
1159 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1160 uint64_t value)
1162 value &= pmu_counter_mask(env);
1163 env->cp15.c9_pmcnten |= value;
1166 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1167 uint64_t value)
1169 value &= pmu_counter_mask(env);
1170 env->cp15.c9_pmcnten &= ~value;
1173 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1174 uint64_t value)
1176 env->cp15.c9_pmovsr &= ~value;
1179 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1180 uint64_t value)
1182 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1183 * PMSELR value is equal to or greater than the number of implemented
1184 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1186 if (env->cp15.c9_pmselr == 0x1f) {
1187 pmccfiltr_write(env, ri, value);
1191 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1193 /* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1194 * are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write().
1196 if (env->cp15.c9_pmselr == 0x1f) {
1197 return env->cp15.pmccfiltr_el0;
1198 } else {
1199 return 0;
1203 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1204 uint64_t value)
1206 if (arm_feature(env, ARM_FEATURE_V8)) {
1207 env->cp15.c9_pmuserenr = value & 0xf;
1208 } else {
1209 env->cp15.c9_pmuserenr = value & 1;
1213 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1214 uint64_t value)
1216 /* We have no event counters so only the C bit can be changed */
1217 value &= pmu_counter_mask(env);
1218 env->cp15.c9_pminten |= value;
1221 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1222 uint64_t value)
1224 value &= pmu_counter_mask(env);
1225 env->cp15.c9_pminten &= ~value;
1228 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1229 uint64_t value)
1231 /* Note that even though the AArch64 view of this register has bits
1232 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1233 * architectural requirements for bits which are RES0 only in some
1234 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1235 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1237 raw_write(env, ri, value & ~0x1FULL);
1240 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1242 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
1243 * For bits that vary between AArch32/64, code needs to check the
1244 * current execution mode before directly using the feature bit.
1246 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
1248 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1249 valid_mask &= ~SCR_HCE;
1251 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1252 * supported if EL2 exists. The bit is UNK/SBZP when
1253 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1254 * when EL2 is unavailable.
1255 * On ARMv8, this bit is always available.
1257 if (arm_feature(env, ARM_FEATURE_V7) &&
1258 !arm_feature(env, ARM_FEATURE_V8)) {
1259 valid_mask &= ~SCR_SMD;
1263 /* Clear all-context RES0 bits. */
1264 value &= valid_mask;
1265 raw_write(env, ri, value);
1268 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1270 ARMCPU *cpu = arm_env_get_cpu(env);
1272 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1273 * bank
1275 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1276 ri->secure & ARM_CP_SECSTATE_S);
1278 return cpu->ccsidr[index];
1281 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1282 uint64_t value)
1284 raw_write(env, ri, value & 0xf);
1287 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1289 CPUState *cs = ENV_GET_CPU(env);
1290 uint64_t ret = 0;
1292 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1293 ret |= CPSR_I;
1295 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1296 ret |= CPSR_F;
1298 /* External aborts are not possible in QEMU so A bit is always clear */
1299 return ret;
1302 static const ARMCPRegInfo v7_cp_reginfo[] = {
1303 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1304 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1305 .access = PL1_W, .type = ARM_CP_NOP },
1306 /* Performance monitors are implementation defined in v7,
1307 * but with an ARM recommended set of registers, which we
1308 * follow (although we don't actually implement any counters)
1310 * Performance registers fall into three categories:
1311 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1312 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1313 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1314 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1315 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1317 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1318 .access = PL0_RW, .type = ARM_CP_ALIAS,
1319 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1320 .writefn = pmcntenset_write,
1321 .accessfn = pmreg_access,
1322 .raw_writefn = raw_write },
1323 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1324 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1325 .access = PL0_RW, .accessfn = pmreg_access,
1326 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1327 .writefn = pmcntenset_write, .raw_writefn = raw_write },
1328 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1329 .access = PL0_RW,
1330 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1331 .accessfn = pmreg_access,
1332 .writefn = pmcntenclr_write,
1333 .type = ARM_CP_ALIAS },
1334 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1335 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1336 .access = PL0_RW, .accessfn = pmreg_access,
1337 .type = ARM_CP_ALIAS,
1338 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1339 .writefn = pmcntenclr_write },
1340 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1341 .access = PL0_RW,
1342 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1343 .accessfn = pmreg_access,
1344 .writefn = pmovsr_write,
1345 .raw_writefn = raw_write },
1346 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1347 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1348 .access = PL0_RW, .accessfn = pmreg_access,
1349 .type = ARM_CP_ALIAS,
1350 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1351 .writefn = pmovsr_write,
1352 .raw_writefn = raw_write },
1353 /* Unimplemented so WI. */
1354 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1355 .access = PL0_W, .accessfn = pmreg_access_swinc, .type = ARM_CP_NOP },
1356 #ifndef CONFIG_USER_ONLY
1357 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1358 .access = PL0_RW, .type = ARM_CP_ALIAS,
1359 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1360 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1361 .raw_writefn = raw_write},
1362 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1363 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1364 .access = PL0_RW, .accessfn = pmreg_access_selr,
1365 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1366 .writefn = pmselr_write, .raw_writefn = raw_write, },
1367 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1368 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1369 .readfn = pmccntr_read, .writefn = pmccntr_write32,
1370 .accessfn = pmreg_access_ccntr },
1371 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1372 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1373 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1374 .type = ARM_CP_IO,
1375 .readfn = pmccntr_read, .writefn = pmccntr_write, },
1376 #endif
1377 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1378 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1379 .writefn = pmccfiltr_write,
1380 .access = PL0_RW, .accessfn = pmreg_access,
1381 .type = ARM_CP_IO,
1382 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1383 .resetvalue = 0, },
1384 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1385 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1386 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1387 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1388 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1389 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access,
1390 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1391 /* Unimplemented, RAZ/WI. */
1392 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1393 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1394 .accessfn = pmreg_access_xevcntr },
1395 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1396 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
1397 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
1398 .resetvalue = 0,
1399 .writefn = pmuserenr_write, .raw_writefn = raw_write },
1400 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1401 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1402 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1403 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1404 .resetvalue = 0,
1405 .writefn = pmuserenr_write, .raw_writefn = raw_write },
1406 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1407 .access = PL1_RW, .accessfn = access_tpm,
1408 .type = ARM_CP_ALIAS | ARM_CP_IO,
1409 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
1410 .resetvalue = 0,
1411 .writefn = pmintenset_write, .raw_writefn = raw_write },
1412 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
1413 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
1414 .access = PL1_RW, .accessfn = access_tpm,
1415 .type = ARM_CP_IO,
1416 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1417 .writefn = pmintenset_write, .raw_writefn = raw_write,
1418 .resetvalue = 0x0 },
1419 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1420 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1421 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1422 .writefn = pmintenclr_write, },
1423 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1424 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1425 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1426 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1427 .writefn = pmintenclr_write },
1428 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1429 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
1430 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
1431 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1432 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
1433 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1434 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1435 offsetof(CPUARMState, cp15.csselr_ns) } },
1436 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1437 * just RAZ for all cores:
1439 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1440 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
1441 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1442 /* Auxiliary fault status registers: these also are IMPDEF, and we
1443 * choose to RAZ/WI for all cores.
1445 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1446 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1447 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1448 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1449 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1450 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1451 /* MAIR can just read-as-written because we don't implement caches
1452 * and so don't need to care about memory attributes.
1454 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1455 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
1456 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
1457 .resetvalue = 0 },
1458 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1459 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1460 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1461 .resetvalue = 0 },
1462 /* For non-long-descriptor page tables these are PRRR and NMRR;
1463 * regardless they still act as reads-as-written for QEMU.
1465 /* MAIR0/1 are defined separately from their 64-bit counterpart which
1466 * allows them to assign the correct fieldoffset based on the endianness
1467 * handled in the field definitions.
1469 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
1470 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
1471 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1472 offsetof(CPUARMState, cp15.mair0_ns) },
1473 .resetfn = arm_cp_reset_ignore },
1474 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
1475 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
1476 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1477 offsetof(CPUARMState, cp15.mair1_ns) },
1478 .resetfn = arm_cp_reset_ignore },
1479 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1480 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
1481 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
1482 /* 32 bit ITLB invalidates */
1483 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
1484 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1485 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
1486 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1487 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
1488 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1489 /* 32 bit DTLB invalidates */
1490 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
1491 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1492 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
1493 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1494 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
1495 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1496 /* 32 bit TLB invalidates */
1497 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
1498 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1499 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
1500 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1501 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
1502 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1503 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
1504 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
1505 REGINFO_SENTINEL
1508 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1509 /* 32 bit TLB invalidates, Inner Shareable */
1510 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
1511 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
1512 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
1513 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
1514 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
1515 .type = ARM_CP_NO_RAW, .access = PL1_W,
1516 .writefn = tlbiasid_is_write },
1517 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
1518 .type = ARM_CP_NO_RAW, .access = PL1_W,
1519 .writefn = tlbimvaa_is_write },
1520 REGINFO_SENTINEL
1523 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1524 uint64_t value)
1526 value &= 1;
1527 env->teecr = value;
1530 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1531 bool isread)
1533 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
1534 return CP_ACCESS_TRAP;
1536 return CP_ACCESS_OK;
1539 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1540 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1541 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1542 .resetvalue = 0,
1543 .writefn = teecr_write },
1544 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1545 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
1546 .accessfn = teehbr_access, .resetvalue = 0 },
1547 REGINFO_SENTINEL
1550 static const ARMCPRegInfo v6k_cp_reginfo[] = {
1551 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1552 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1553 .access = PL0_RW,
1554 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
1555 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1556 .access = PL0_RW,
1557 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1558 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
1559 .resetfn = arm_cp_reset_ignore },
1560 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1561 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1562 .access = PL0_R|PL1_W,
1563 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1564 .resetvalue = 0},
1565 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1566 .access = PL0_R|PL1_W,
1567 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1568 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
1569 .resetfn = arm_cp_reset_ignore },
1570 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
1571 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
1572 .access = PL1_RW,
1573 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1574 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1575 .access = PL1_RW,
1576 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1577 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1578 .resetvalue = 0 },
1579 REGINFO_SENTINEL
1582 #ifndef CONFIG_USER_ONLY
1584 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1585 bool isread)
1587 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1588 * Writable only at the highest implemented exception level.
1590 int el = arm_current_el(env);
1592 switch (el) {
1593 case 0:
1594 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1595 return CP_ACCESS_TRAP;
1597 break;
1598 case 1:
1599 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1600 arm_is_secure_below_el3(env)) {
1601 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1602 return CP_ACCESS_TRAP_UNCATEGORIZED;
1604 break;
1605 case 2:
1606 case 3:
1607 break;
1610 if (!isread && el < arm_highest_el(env)) {
1611 return CP_ACCESS_TRAP_UNCATEGORIZED;
1614 return CP_ACCESS_OK;
1617 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1618 bool isread)
1620 unsigned int cur_el = arm_current_el(env);
1621 bool secure = arm_is_secure(env);
1623 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
1624 if (cur_el == 0 &&
1625 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1626 return CP_ACCESS_TRAP;
1629 if (arm_feature(env, ARM_FEATURE_EL2) &&
1630 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1631 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1632 return CP_ACCESS_TRAP_EL2;
1634 return CP_ACCESS_OK;
1637 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1638 bool isread)
1640 unsigned int cur_el = arm_current_el(env);
1641 bool secure = arm_is_secure(env);
1643 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1644 * EL0[PV]TEN is zero.
1646 if (cur_el == 0 &&
1647 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1648 return CP_ACCESS_TRAP;
1651 if (arm_feature(env, ARM_FEATURE_EL2) &&
1652 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1653 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1654 return CP_ACCESS_TRAP_EL2;
1656 return CP_ACCESS_OK;
1659 static CPAccessResult gt_pct_access(CPUARMState *env,
1660 const ARMCPRegInfo *ri,
1661 bool isread)
1663 return gt_counter_access(env, GTIMER_PHYS, isread);
1666 static CPAccessResult gt_vct_access(CPUARMState *env,
1667 const ARMCPRegInfo *ri,
1668 bool isread)
1670 return gt_counter_access(env, GTIMER_VIRT, isread);
1673 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1674 bool isread)
1676 return gt_timer_access(env, GTIMER_PHYS, isread);
1679 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1680 bool isread)
1682 return gt_timer_access(env, GTIMER_VIRT, isread);
1685 static CPAccessResult gt_stimer_access(CPUARMState *env,
1686 const ARMCPRegInfo *ri,
1687 bool isread)
1689 /* The AArch64 register view of the secure physical timer is
1690 * always accessible from EL3, and configurably accessible from
1691 * Secure EL1.
1693 switch (arm_current_el(env)) {
1694 case 1:
1695 if (!arm_is_secure(env)) {
1696 return CP_ACCESS_TRAP;
1698 if (!(env->cp15.scr_el3 & SCR_ST)) {
1699 return CP_ACCESS_TRAP_EL3;
1701 return CP_ACCESS_OK;
1702 case 0:
1703 case 2:
1704 return CP_ACCESS_TRAP;
1705 case 3:
1706 return CP_ACCESS_OK;
1707 default:
1708 g_assert_not_reached();
1712 static uint64_t gt_get_countervalue(CPUARMState *env)
1714 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
1717 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1719 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1721 if (gt->ctl & 1) {
1722 /* Timer enabled: calculate and set current ISTATUS, irq, and
1723 * reset timer to when ISTATUS next has to change
1725 uint64_t offset = timeridx == GTIMER_VIRT ?
1726 cpu->env.cp15.cntvoff_el2 : 0;
1727 uint64_t count = gt_get_countervalue(&cpu->env);
1728 /* Note that this must be unsigned 64 bit arithmetic: */
1729 int istatus = count - offset >= gt->cval;
1730 uint64_t nexttick;
1731 int irqstate;
1733 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1735 irqstate = (istatus && !(gt->ctl & 2));
1736 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1738 if (istatus) {
1739 /* Next transition is when count rolls back over to zero */
1740 nexttick = UINT64_MAX;
1741 } else {
1742 /* Next transition is when we hit cval */
1743 nexttick = gt->cval + offset;
1745 /* Note that the desired next expiry time might be beyond the
1746 * signed-64-bit range of a QEMUTimer -- in this case we just
1747 * set the timer for as far in the future as possible. When the
1748 * timer expires we will reset the timer for any remaining period.
1750 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1751 nexttick = INT64_MAX / GTIMER_SCALE;
1753 timer_mod(cpu->gt_timer[timeridx], nexttick);
1754 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
1755 } else {
1756 /* Timer disabled: ISTATUS and timer output always clear */
1757 gt->ctl &= ~4;
1758 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
1759 timer_del(cpu->gt_timer[timeridx]);
1760 trace_arm_gt_recalc_disabled(timeridx);
1764 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1765 int timeridx)
1767 ARMCPU *cpu = arm_env_get_cpu(env);
1769 timer_del(cpu->gt_timer[timeridx]);
1772 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1774 return gt_get_countervalue(env);
1777 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1779 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1782 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1783 int timeridx,
1784 uint64_t value)
1786 trace_arm_gt_cval_write(timeridx, value);
1787 env->cp15.c14_timer[timeridx].cval = value;
1788 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1791 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1792 int timeridx)
1794 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1796 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
1797 (gt_get_countervalue(env) - offset));
1800 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1801 int timeridx,
1802 uint64_t value)
1804 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1806 trace_arm_gt_tval_write(timeridx, value);
1807 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
1808 sextract64(value, 0, 32);
1809 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1812 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1813 int timeridx,
1814 uint64_t value)
1816 ARMCPU *cpu = arm_env_get_cpu(env);
1817 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1819 trace_arm_gt_ctl_write(timeridx, value);
1820 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
1821 if ((oldval ^ value) & 1) {
1822 /* Enable toggled */
1823 gt_recalc_timer(cpu, timeridx);
1824 } else if ((oldval ^ value) & 2) {
1825 /* IMASK toggled: don't need to recalculate,
1826 * just set the interrupt line based on ISTATUS
1828 int irqstate = (oldval & 4) && !(value & 2);
1830 trace_arm_gt_imask_toggle(timeridx, irqstate);
1831 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
1835 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1837 gt_timer_reset(env, ri, GTIMER_PHYS);
1840 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1841 uint64_t value)
1843 gt_cval_write(env, ri, GTIMER_PHYS, value);
1846 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1848 return gt_tval_read(env, ri, GTIMER_PHYS);
1851 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1852 uint64_t value)
1854 gt_tval_write(env, ri, GTIMER_PHYS, value);
1857 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1858 uint64_t value)
1860 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1863 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1865 gt_timer_reset(env, ri, GTIMER_VIRT);
1868 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1869 uint64_t value)
1871 gt_cval_write(env, ri, GTIMER_VIRT, value);
1874 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1876 return gt_tval_read(env, ri, GTIMER_VIRT);
1879 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1880 uint64_t value)
1882 gt_tval_write(env, ri, GTIMER_VIRT, value);
1885 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1886 uint64_t value)
1888 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1891 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1892 uint64_t value)
1894 ARMCPU *cpu = arm_env_get_cpu(env);
1896 trace_arm_gt_cntvoff_write(value);
1897 raw_write(env, ri, value);
1898 gt_recalc_timer(cpu, GTIMER_VIRT);
1901 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1903 gt_timer_reset(env, ri, GTIMER_HYP);
1906 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1907 uint64_t value)
1909 gt_cval_write(env, ri, GTIMER_HYP, value);
1912 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1914 return gt_tval_read(env, ri, GTIMER_HYP);
1917 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1918 uint64_t value)
1920 gt_tval_write(env, ri, GTIMER_HYP, value);
1923 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1924 uint64_t value)
1926 gt_ctl_write(env, ri, GTIMER_HYP, value);
1929 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1931 gt_timer_reset(env, ri, GTIMER_SEC);
1934 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1935 uint64_t value)
1937 gt_cval_write(env, ri, GTIMER_SEC, value);
1940 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1942 return gt_tval_read(env, ri, GTIMER_SEC);
1945 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1946 uint64_t value)
1948 gt_tval_write(env, ri, GTIMER_SEC, value);
1951 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1952 uint64_t value)
1954 gt_ctl_write(env, ri, GTIMER_SEC, value);
1957 void arm_gt_ptimer_cb(void *opaque)
1959 ARMCPU *cpu = opaque;
1961 gt_recalc_timer(cpu, GTIMER_PHYS);
1964 void arm_gt_vtimer_cb(void *opaque)
1966 ARMCPU *cpu = opaque;
1968 gt_recalc_timer(cpu, GTIMER_VIRT);
1971 void arm_gt_htimer_cb(void *opaque)
1973 ARMCPU *cpu = opaque;
1975 gt_recalc_timer(cpu, GTIMER_HYP);
1978 void arm_gt_stimer_cb(void *opaque)
1980 ARMCPU *cpu = opaque;
1982 gt_recalc_timer(cpu, GTIMER_SEC);
1985 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1986 /* Note that CNTFRQ is purely reads-as-written for the benefit
1987 * of software; writing it doesn't actually change the timer frequency.
1988 * Our reset value matches the fixed frequency we implement the timer at.
1990 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
1991 .type = ARM_CP_ALIAS,
1992 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1993 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
1995 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1996 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1997 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1998 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1999 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
2001 /* overall control: mostly access permissions */
2002 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2003 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2004 .access = PL1_RW,
2005 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2006 .resetvalue = 0,
2008 /* per-timer control */
2009 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2010 .secure = ARM_CP_SECSTATE_NS,
2011 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
2012 .accessfn = gt_ptimer_access,
2013 .fieldoffset = offsetoflow32(CPUARMState,
2014 cp15.c14_timer[GTIMER_PHYS].ctl),
2015 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
2017 { .name = "CNTP_CTL_S",
2018 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2019 .secure = ARM_CP_SECSTATE_S,
2020 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
2021 .accessfn = gt_ptimer_access,
2022 .fieldoffset = offsetoflow32(CPUARMState,
2023 cp15.c14_timer[GTIMER_SEC].ctl),
2024 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2026 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2027 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2028 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
2029 .accessfn = gt_ptimer_access,
2030 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2031 .resetvalue = 0,
2032 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
2034 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2035 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
2036 .accessfn = gt_vtimer_access,
2037 .fieldoffset = offsetoflow32(CPUARMState,
2038 cp15.c14_timer[GTIMER_VIRT].ctl),
2039 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
2041 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2042 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2043 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
2044 .accessfn = gt_vtimer_access,
2045 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2046 .resetvalue = 0,
2047 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
2049 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2050 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2051 .secure = ARM_CP_SECSTATE_NS,
2052 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2053 .accessfn = gt_ptimer_access,
2054 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
2056 { .name = "CNTP_TVAL_S",
2057 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2058 .secure = ARM_CP_SECSTATE_S,
2059 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2060 .accessfn = gt_ptimer_access,
2061 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2063 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2064 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2065 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2066 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2067 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
2069 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2070 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2071 .accessfn = gt_vtimer_access,
2072 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2074 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2075 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2076 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2077 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2078 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2080 /* The counter itself */
2081 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
2082 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2083 .accessfn = gt_pct_access,
2084 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2086 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2087 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
2088 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2089 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
2091 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
2092 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2093 .accessfn = gt_vct_access,
2094 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
2096 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2097 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2098 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2099 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
2101 /* Comparison value, indicating when the timer goes off */
2102 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
2103 .secure = ARM_CP_SECSTATE_NS,
2104 .access = PL1_RW | PL0_R,
2105 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2106 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2107 .accessfn = gt_ptimer_access,
2108 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2110 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
2111 .secure = ARM_CP_SECSTATE_S,
2112 .access = PL1_RW | PL0_R,
2113 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2114 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2115 .accessfn = gt_ptimer_access,
2116 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2118 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2119 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2120 .access = PL1_RW | PL0_R,
2121 .type = ARM_CP_IO,
2122 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2123 .resetvalue = 0, .accessfn = gt_ptimer_access,
2124 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2126 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2127 .access = PL1_RW | PL0_R,
2128 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2129 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2130 .accessfn = gt_vtimer_access,
2131 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2133 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2134 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2135 .access = PL1_RW | PL0_R,
2136 .type = ARM_CP_IO,
2137 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2138 .resetvalue = 0, .accessfn = gt_vtimer_access,
2139 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2141 /* Secure timer -- this is actually restricted to only EL3
2142 * and configurably Secure-EL1 via the accessfn.
2144 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2145 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2146 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2147 .accessfn = gt_stimer_access,
2148 .readfn = gt_sec_tval_read,
2149 .writefn = gt_sec_tval_write,
2150 .resetfn = gt_sec_timer_reset,
2152 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2153 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2154 .type = ARM_CP_IO, .access = PL1_RW,
2155 .accessfn = gt_stimer_access,
2156 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2157 .resetvalue = 0,
2158 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2160 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2161 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2162 .type = ARM_CP_IO, .access = PL1_RW,
2163 .accessfn = gt_stimer_access,
2164 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2165 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2167 REGINFO_SENTINEL
2170 #else
2172 /* In user-mode most of the generic timer registers are inaccessible
2173 * however modern kernels (4.12+) allow access to cntvct_el0
2176 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2178 /* Currently we have no support for QEMUTimer in linux-user so we
2179 * can't call gt_get_countervalue(env), instead we directly
2180 * call the lower level functions.
2182 return cpu_get_clock() / GTIMER_SCALE;
2185 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2186 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2187 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2188 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
2189 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2190 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
2192 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2193 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2194 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2195 .readfn = gt_virt_cnt_read,
2197 REGINFO_SENTINEL
2200 #endif
2202 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2204 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2205 raw_write(env, ri, value);
2206 } else if (arm_feature(env, ARM_FEATURE_V7)) {
2207 raw_write(env, ri, value & 0xfffff6ff);
2208 } else {
2209 raw_write(env, ri, value & 0xfffff1ff);
2213 #ifndef CONFIG_USER_ONLY
2214 /* get_phys_addr() isn't present for user-mode-only targets */
2216 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2217 bool isread)
2219 if (ri->opc2 & 4) {
2220 /* The ATS12NSO* operations must trap to EL3 if executed in
2221 * Secure EL1 (which can only happen if EL3 is AArch64).
2222 * They are simply UNDEF if executed from NS EL1.
2223 * They function normally from EL2 or EL3.
2225 if (arm_current_el(env) == 1) {
2226 if (arm_is_secure_below_el3(env)) {
2227 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2229 return CP_ACCESS_TRAP_UNCATEGORIZED;
2232 return CP_ACCESS_OK;
2235 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
2236 MMUAccessType access_type, ARMMMUIdx mmu_idx)
2238 hwaddr phys_addr;
2239 target_ulong page_size;
2240 int prot;
2241 bool ret;
2242 uint64_t par64;
2243 bool format64 = false;
2244 MemTxAttrs attrs = {};
2245 ARMMMUFaultInfo fi = {};
2246 ARMCacheAttrs cacheattrs = {};
2248 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
2249 &prot, &page_size, &fi, &cacheattrs);
2251 if (is_a64(env)) {
2252 format64 = true;
2253 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
2255 * ATS1Cxx:
2256 * * TTBCR.EAE determines whether the result is returned using the
2257 * 32-bit or the 64-bit PAR format
2258 * * Instructions executed in Hyp mode always use the 64bit format
2260 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
2261 * * The Non-secure TTBCR.EAE bit is set to 1
2262 * * The implementation includes EL2, and the value of HCR.VM is 1
2264 * ATS1Hx always uses the 64bit format (not supported yet).
2266 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
2268 if (arm_feature(env, ARM_FEATURE_EL2)) {
2269 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
2270 format64 |= env->cp15.hcr_el2 & HCR_VM;
2271 } else {
2272 format64 |= arm_current_el(env) == 2;
2277 if (format64) {
2278 /* Create a 64-bit PAR */
2279 par64 = (1 << 11); /* LPAE bit always set */
2280 if (!ret) {
2281 par64 |= phys_addr & ~0xfffULL;
2282 if (!attrs.secure) {
2283 par64 |= (1 << 9); /* NS */
2285 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
2286 par64 |= cacheattrs.shareability << 7; /* SH */
2287 } else {
2288 uint32_t fsr = arm_fi_to_lfsc(&fi);
2290 par64 |= 1; /* F */
2291 par64 |= (fsr & 0x3f) << 1; /* FS */
2292 /* Note that S2WLK and FSTAGE are always zero, because we don't
2293 * implement virtualization and therefore there can't be a stage 2
2294 * fault.
2297 } else {
2298 /* fsr is a DFSR/IFSR value for the short descriptor
2299 * translation table format (with WnR always clear).
2300 * Convert it to a 32-bit PAR.
2302 if (!ret) {
2303 /* We do not set any attribute bits in the PAR */
2304 if (page_size == (1 << 24)
2305 && arm_feature(env, ARM_FEATURE_V7)) {
2306 par64 = (phys_addr & 0xff000000) | (1 << 1);
2307 } else {
2308 par64 = phys_addr & 0xfffff000;
2310 if (!attrs.secure) {
2311 par64 |= (1 << 9); /* NS */
2313 } else {
2314 uint32_t fsr = arm_fi_to_sfsc(&fi);
2316 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
2317 ((fsr & 0xf) << 1) | 1;
2320 return par64;
2323 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2325 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2326 uint64_t par64;
2327 ARMMMUIdx mmu_idx;
2328 int el = arm_current_el(env);
2329 bool secure = arm_is_secure_below_el3(env);
2331 switch (ri->opc2 & 6) {
2332 case 0:
2333 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2334 switch (el) {
2335 case 3:
2336 mmu_idx = ARMMMUIdx_S1E3;
2337 break;
2338 case 2:
2339 mmu_idx = ARMMMUIdx_S1NSE1;
2340 break;
2341 case 1:
2342 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2343 break;
2344 default:
2345 g_assert_not_reached();
2347 break;
2348 case 2:
2349 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2350 switch (el) {
2351 case 3:
2352 mmu_idx = ARMMMUIdx_S1SE0;
2353 break;
2354 case 2:
2355 mmu_idx = ARMMMUIdx_S1NSE0;
2356 break;
2357 case 1:
2358 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2359 break;
2360 default:
2361 g_assert_not_reached();
2363 break;
2364 case 4:
2365 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2366 mmu_idx = ARMMMUIdx_S12NSE1;
2367 break;
2368 case 6:
2369 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2370 mmu_idx = ARMMMUIdx_S12NSE0;
2371 break;
2372 default:
2373 g_assert_not_reached();
2376 par64 = do_ats_write(env, value, access_type, mmu_idx);
2378 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2381 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2382 uint64_t value)
2384 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2385 uint64_t par64;
2387 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2389 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2392 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2393 bool isread)
2395 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2396 return CP_ACCESS_TRAP;
2398 return CP_ACCESS_OK;
2401 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2402 uint64_t value)
2404 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2405 ARMMMUIdx mmu_idx;
2406 int secure = arm_is_secure_below_el3(env);
2408 switch (ri->opc2 & 6) {
2409 case 0:
2410 switch (ri->opc1) {
2411 case 0: /* AT S1E1R, AT S1E1W */
2412 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2413 break;
2414 case 4: /* AT S1E2R, AT S1E2W */
2415 mmu_idx = ARMMMUIdx_S1E2;
2416 break;
2417 case 6: /* AT S1E3R, AT S1E3W */
2418 mmu_idx = ARMMMUIdx_S1E3;
2419 break;
2420 default:
2421 g_assert_not_reached();
2423 break;
2424 case 2: /* AT S1E0R, AT S1E0W */
2425 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2426 break;
2427 case 4: /* AT S12E1R, AT S12E1W */
2428 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
2429 break;
2430 case 6: /* AT S12E0R, AT S12E0W */
2431 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
2432 break;
2433 default:
2434 g_assert_not_reached();
2437 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
2439 #endif
2441 static const ARMCPRegInfo vapa_cp_reginfo[] = {
2442 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2443 .access = PL1_RW, .resetvalue = 0,
2444 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2445 offsetoflow32(CPUARMState, cp15.par_ns) },
2446 .writefn = par_write },
2447 #ifndef CONFIG_USER_ONLY
2448 /* This underdecoding is safe because the reginfo is NO_RAW. */
2449 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
2450 .access = PL1_W, .accessfn = ats_access,
2451 .writefn = ats_write, .type = ARM_CP_NO_RAW },
2452 #endif
2453 REGINFO_SENTINEL
2456 /* Return basic MPU access permission bits. */
2457 static uint32_t simple_mpu_ap_bits(uint32_t val)
2459 uint32_t ret;
2460 uint32_t mask;
2461 int i;
2462 ret = 0;
2463 mask = 3;
2464 for (i = 0; i < 16; i += 2) {
2465 ret |= (val >> i) & mask;
2466 mask <<= 2;
2468 return ret;
2471 /* Pad basic MPU access permission bits to extended format. */
2472 static uint32_t extended_mpu_ap_bits(uint32_t val)
2474 uint32_t ret;
2475 uint32_t mask;
2476 int i;
2477 ret = 0;
2478 mask = 3;
2479 for (i = 0; i < 16; i += 2) {
2480 ret |= (val & mask) << i;
2481 mask <<= 2;
2483 return ret;
2486 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2487 uint64_t value)
2489 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
2492 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2494 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
2497 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2498 uint64_t value)
2500 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
2503 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2505 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
2508 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2510 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2512 if (!u32p) {
2513 return 0;
2516 u32p += env->pmsav7.rnr[M_REG_NS];
2517 return *u32p;
2520 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2521 uint64_t value)
2523 ARMCPU *cpu = arm_env_get_cpu(env);
2524 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2526 if (!u32p) {
2527 return;
2530 u32p += env->pmsav7.rnr[M_REG_NS];
2531 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
2532 *u32p = value;
2535 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2536 uint64_t value)
2538 ARMCPU *cpu = arm_env_get_cpu(env);
2539 uint32_t nrgs = cpu->pmsav7_dregion;
2541 if (value >= nrgs) {
2542 qemu_log_mask(LOG_GUEST_ERROR,
2543 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2544 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2545 return;
2548 raw_write(env, ri, value);
2551 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
2552 /* Reset for all these registers is handled in arm_cpu_reset(),
2553 * because the PMSAv7 is also used by M-profile CPUs, which do
2554 * not register cpregs but still need the state to be reset.
2556 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2557 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2558 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
2559 .readfn = pmsav7_read, .writefn = pmsav7_write,
2560 .resetfn = arm_cp_reset_ignore },
2561 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2562 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2563 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
2564 .readfn = pmsav7_read, .writefn = pmsav7_write,
2565 .resetfn = arm_cp_reset_ignore },
2566 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2567 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2568 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
2569 .readfn = pmsav7_read, .writefn = pmsav7_write,
2570 .resetfn = arm_cp_reset_ignore },
2571 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2572 .access = PL1_RW,
2573 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
2574 .writefn = pmsav7_rgnr_write,
2575 .resetfn = arm_cp_reset_ignore },
2576 REGINFO_SENTINEL
2579 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2580 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2581 .access = PL1_RW, .type = ARM_CP_ALIAS,
2582 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2583 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2584 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2585 .access = PL1_RW, .type = ARM_CP_ALIAS,
2586 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2587 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2588 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2589 .access = PL1_RW,
2590 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2591 .resetvalue = 0, },
2592 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2593 .access = PL1_RW,
2594 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2595 .resetvalue = 0, },
2596 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2597 .access = PL1_RW,
2598 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2599 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2600 .access = PL1_RW,
2601 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
2602 /* Protection region base and size registers */
2603 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2604 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2605 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2606 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2607 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2608 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2609 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2610 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2611 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2612 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2613 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2614 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2615 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2616 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2617 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2618 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2619 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2620 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2621 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2622 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2623 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2624 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2625 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2626 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
2627 REGINFO_SENTINEL
2630 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2631 uint64_t value)
2633 TCR *tcr = raw_ptr(env, ri);
2634 int maskshift = extract32(value, 0, 3);
2636 if (!arm_feature(env, ARM_FEATURE_V8)) {
2637 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2638 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2639 * using Long-desciptor translation table format */
2640 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2641 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2642 /* In an implementation that includes the Security Extensions
2643 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2644 * Short-descriptor translation table format.
2646 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2647 } else {
2648 value &= TTBCR_N;
2652 /* Update the masks corresponding to the TCR bank being written
2653 * Note that we always calculate mask and base_mask, but
2654 * they are only used for short-descriptor tables (ie if EAE is 0);
2655 * for long-descriptor tables the TCR fields are used differently
2656 * and the mask and base_mask values are meaningless.
2658 tcr->raw_tcr = value;
2659 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2660 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
2663 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2664 uint64_t value)
2666 ARMCPU *cpu = arm_env_get_cpu(env);
2668 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2669 /* With LPAE the TTBCR could result in a change of ASID
2670 * via the TTBCR.A1 bit, so do a TLB flush.
2672 tlb_flush(CPU(cpu));
2674 vmsa_ttbcr_raw_write(env, ri, value);
2677 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2679 TCR *tcr = raw_ptr(env, ri);
2681 /* Reset both the TCR as well as the masks corresponding to the bank of
2682 * the TCR being reset.
2684 tcr->raw_tcr = 0;
2685 tcr->mask = 0;
2686 tcr->base_mask = 0xffffc000u;
2689 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2690 uint64_t value)
2692 ARMCPU *cpu = arm_env_get_cpu(env);
2693 TCR *tcr = raw_ptr(env, ri);
2695 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
2696 tlb_flush(CPU(cpu));
2697 tcr->raw_tcr = value;
2700 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2701 uint64_t value)
2703 /* 64 bit accesses to the TTBRs can change the ASID and so we
2704 * must flush the TLB.
2706 if (cpreg_field_is_64bit(ri)) {
2707 ARMCPU *cpu = arm_env_get_cpu(env);
2709 tlb_flush(CPU(cpu));
2711 raw_write(env, ri, value);
2714 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2715 uint64_t value)
2717 ARMCPU *cpu = arm_env_get_cpu(env);
2718 CPUState *cs = CPU(cpu);
2720 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2721 if (raw_read(env, ri) != value) {
2722 tlb_flush_by_mmuidx(cs,
2723 ARMMMUIdxBit_S12NSE1 |
2724 ARMMMUIdxBit_S12NSE0 |
2725 ARMMMUIdxBit_S2NS);
2726 raw_write(env, ri, value);
2730 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
2731 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2732 .access = PL1_RW, .type = ARM_CP_ALIAS,
2733 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
2734 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
2735 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2736 .access = PL1_RW, .resetvalue = 0,
2737 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2738 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
2739 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2740 .access = PL1_RW, .resetvalue = 0,
2741 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2742 offsetof(CPUARMState, cp15.dfar_ns) } },
2743 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2744 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2745 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2746 .resetvalue = 0, },
2747 REGINFO_SENTINEL
2750 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
2751 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2752 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2753 .access = PL1_RW,
2754 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
2755 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
2756 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2757 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2758 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2759 offsetof(CPUARMState, cp15.ttbr0_ns) } },
2760 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
2761 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2762 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2763 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2764 offsetof(CPUARMState, cp15.ttbr1_ns) } },
2765 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2766 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2767 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2768 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
2769 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
2770 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2771 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
2772 .raw_writefn = vmsa_ttbcr_raw_write,
2773 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2774 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
2775 REGINFO_SENTINEL
2778 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2779 uint64_t value)
2781 env->cp15.c15_ticonfig = value & 0xe7;
2782 /* The OS_TYPE bit in this register changes the reported CPUID! */
2783 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2784 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
2787 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2788 uint64_t value)
2790 env->cp15.c15_threadid = value & 0xffff;
2793 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2794 uint64_t value)
2796 /* Wait-for-interrupt (deprecated) */
2797 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
2800 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2801 uint64_t value)
2803 /* On OMAP there are registers indicating the max/min index of dcache lines
2804 * containing a dirty line; cache flush operations have to reset these.
2806 env->cp15.c15_i_max = 0x000;
2807 env->cp15.c15_i_min = 0xff0;
2810 static const ARMCPRegInfo omap_cp_reginfo[] = {
2811 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2812 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
2813 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
2814 .resetvalue = 0, },
2815 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2816 .access = PL1_RW, .type = ARM_CP_NOP },
2817 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2818 .access = PL1_RW,
2819 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2820 .writefn = omap_ticonfig_write },
2821 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2822 .access = PL1_RW,
2823 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2824 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2825 .access = PL1_RW, .resetvalue = 0xff0,
2826 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2827 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2828 .access = PL1_RW,
2829 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2830 .writefn = omap_threadid_write },
2831 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2832 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2833 .type = ARM_CP_NO_RAW,
2834 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2835 /* TODO: Peripheral port remap register:
2836 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2837 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2838 * when MMU is off.
2840 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
2841 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
2842 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
2843 .writefn = omap_cachemaint_write },
2844 { .name = "C9", .cp = 15, .crn = 9,
2845 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2846 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
2847 REGINFO_SENTINEL
2850 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2851 uint64_t value)
2853 env->cp15.c15_cpar = value & 0x3fff;
2856 static const ARMCPRegInfo xscale_cp_reginfo[] = {
2857 { .name = "XSCALE_CPAR",
2858 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2859 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2860 .writefn = xscale_cpar_write, },
2861 { .name = "XSCALE_AUXCR",
2862 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2863 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2864 .resetvalue = 0, },
2865 /* XScale specific cache-lockdown: since we have no cache we NOP these
2866 * and hope the guest does not really rely on cache behaviour.
2868 { .name = "XSCALE_LOCK_ICACHE_LINE",
2869 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2870 .access = PL1_W, .type = ARM_CP_NOP },
2871 { .name = "XSCALE_UNLOCK_ICACHE",
2872 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2873 .access = PL1_W, .type = ARM_CP_NOP },
2874 { .name = "XSCALE_DCACHE_LOCK",
2875 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2876 .access = PL1_RW, .type = ARM_CP_NOP },
2877 { .name = "XSCALE_UNLOCK_DCACHE",
2878 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2879 .access = PL1_W, .type = ARM_CP_NOP },
2880 REGINFO_SENTINEL
2883 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2884 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2885 * implementation of this implementation-defined space.
2886 * Ideally this should eventually disappear in favour of actually
2887 * implementing the correct behaviour for all cores.
2889 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2890 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2891 .access = PL1_RW,
2892 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
2893 .resetvalue = 0 },
2894 REGINFO_SENTINEL
2897 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2898 /* Cache status: RAZ because we have no cache so it's always clean */
2899 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
2900 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2901 .resetvalue = 0 },
2902 REGINFO_SENTINEL
2905 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2906 /* We never have a a block transfer operation in progress */
2907 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
2908 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2909 .resetvalue = 0 },
2910 /* The cache ops themselves: these all NOP for QEMU */
2911 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2912 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2913 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2914 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2915 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2916 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2917 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2918 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2919 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2920 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2921 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2922 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2923 REGINFO_SENTINEL
2926 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2927 /* The cache test-and-clean instructions always return (1 << 30)
2928 * to indicate that there are no dirty cache lines.
2930 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
2931 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2932 .resetvalue = (1 << 30) },
2933 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
2934 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2935 .resetvalue = (1 << 30) },
2936 REGINFO_SENTINEL
2939 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2940 /* Ignore ReadBuffer accesses */
2941 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2942 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2943 .access = PL1_RW, .resetvalue = 0,
2944 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
2945 REGINFO_SENTINEL
2948 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2950 ARMCPU *cpu = arm_env_get_cpu(env);
2951 unsigned int cur_el = arm_current_el(env);
2952 bool secure = arm_is_secure(env);
2954 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2955 return env->cp15.vpidr_el2;
2957 return raw_read(env, ri);
2960 static uint64_t mpidr_read_val(CPUARMState *env)
2962 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2963 uint64_t mpidr = cpu->mp_affinity;
2965 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2966 mpidr |= (1U << 31);
2967 /* Cores which are uniprocessor (non-coherent)
2968 * but still implement the MP extensions set
2969 * bit 30. (For instance, Cortex-R5).
2971 if (cpu->mp_is_up) {
2972 mpidr |= (1u << 30);
2975 return mpidr;
2978 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2980 unsigned int cur_el = arm_current_el(env);
2981 bool secure = arm_is_secure(env);
2983 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2984 return env->cp15.vmpidr_el2;
2986 return mpidr_read_val(env);
2989 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
2990 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2991 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
2992 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
2993 REGINFO_SENTINEL
2996 static const ARMCPRegInfo lpae_cp_reginfo[] = {
2997 /* NOP AMAIR0/1 */
2998 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2999 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
3000 .access = PL1_RW, .type = ARM_CP_CONST,
3001 .resetvalue = 0 },
3002 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
3003 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
3004 .access = PL1_RW, .type = ARM_CP_CONST,
3005 .resetvalue = 0 },
3006 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
3007 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
3008 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
3009 offsetof(CPUARMState, cp15.par_ns)} },
3010 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
3011 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3012 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3013 offsetof(CPUARMState, cp15.ttbr0_ns) },
3014 .writefn = vmsa_ttbr_write, },
3015 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
3016 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3017 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3018 offsetof(CPUARMState, cp15.ttbr1_ns) },
3019 .writefn = vmsa_ttbr_write, },
3020 REGINFO_SENTINEL
3023 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3025 return vfp_get_fpcr(env);
3028 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3029 uint64_t value)
3031 vfp_set_fpcr(env, value);
3034 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3036 return vfp_get_fpsr(env);
3039 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3040 uint64_t value)
3042 vfp_set_fpsr(env, value);
3045 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
3046 bool isread)
3048 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
3049 return CP_ACCESS_TRAP;
3051 return CP_ACCESS_OK;
3054 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
3055 uint64_t value)
3057 env->daif = value & PSTATE_DAIF;
3060 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3061 const ARMCPRegInfo *ri,
3062 bool isread)
3064 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
3065 * SCTLR_EL1.UCI is set.
3067 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
3068 return CP_ACCESS_TRAP;
3070 return CP_ACCESS_OK;
3073 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
3074 * Page D4-1736 (DDI0487A.b)
3077 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3078 uint64_t value)
3080 CPUState *cs = ENV_GET_CPU(env);
3082 if (arm_is_secure_below_el3(env)) {
3083 tlb_flush_by_mmuidx(cs,
3084 ARMMMUIdxBit_S1SE1 |
3085 ARMMMUIdxBit_S1SE0);
3086 } else {
3087 tlb_flush_by_mmuidx(cs,
3088 ARMMMUIdxBit_S12NSE1 |
3089 ARMMMUIdxBit_S12NSE0);
3093 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3094 uint64_t value)
3096 CPUState *cs = ENV_GET_CPU(env);
3097 bool sec = arm_is_secure_below_el3(env);
3099 if (sec) {
3100 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3101 ARMMMUIdxBit_S1SE1 |
3102 ARMMMUIdxBit_S1SE0);
3103 } else {
3104 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3105 ARMMMUIdxBit_S12NSE1 |
3106 ARMMMUIdxBit_S12NSE0);
3110 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3111 uint64_t value)
3113 /* Note that the 'ALL' scope must invalidate both stage 1 and
3114 * stage 2 translations, whereas most other scopes only invalidate
3115 * stage 1 translations.
3117 ARMCPU *cpu = arm_env_get_cpu(env);
3118 CPUState *cs = CPU(cpu);
3120 if (arm_is_secure_below_el3(env)) {
3121 tlb_flush_by_mmuidx(cs,
3122 ARMMMUIdxBit_S1SE1 |
3123 ARMMMUIdxBit_S1SE0);
3124 } else {
3125 if (arm_feature(env, ARM_FEATURE_EL2)) {
3126 tlb_flush_by_mmuidx(cs,
3127 ARMMMUIdxBit_S12NSE1 |
3128 ARMMMUIdxBit_S12NSE0 |
3129 ARMMMUIdxBit_S2NS);
3130 } else {
3131 tlb_flush_by_mmuidx(cs,
3132 ARMMMUIdxBit_S12NSE1 |
3133 ARMMMUIdxBit_S12NSE0);
3138 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3139 uint64_t value)
3141 ARMCPU *cpu = arm_env_get_cpu(env);
3142 CPUState *cs = CPU(cpu);
3144 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
3147 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3148 uint64_t value)
3150 ARMCPU *cpu = arm_env_get_cpu(env);
3151 CPUState *cs = CPU(cpu);
3153 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
3156 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3157 uint64_t value)
3159 /* Note that the 'ALL' scope must invalidate both stage 1 and
3160 * stage 2 translations, whereas most other scopes only invalidate
3161 * stage 1 translations.
3163 CPUState *cs = ENV_GET_CPU(env);
3164 bool sec = arm_is_secure_below_el3(env);
3165 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
3167 if (sec) {
3168 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3169 ARMMMUIdxBit_S1SE1 |
3170 ARMMMUIdxBit_S1SE0);
3171 } else if (has_el2) {
3172 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3173 ARMMMUIdxBit_S12NSE1 |
3174 ARMMMUIdxBit_S12NSE0 |
3175 ARMMMUIdxBit_S2NS);
3176 } else {
3177 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3178 ARMMMUIdxBit_S12NSE1 |
3179 ARMMMUIdxBit_S12NSE0);
3183 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3184 uint64_t value)
3186 CPUState *cs = ENV_GET_CPU(env);
3188 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
3191 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3192 uint64_t value)
3194 CPUState *cs = ENV_GET_CPU(env);
3196 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
3199 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3200 uint64_t value)
3202 /* Invalidate by VA, EL1&0 (AArch64 version).
3203 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3204 * since we don't support flush-for-specific-ASID-only or
3205 * flush-last-level-only.
3207 ARMCPU *cpu = arm_env_get_cpu(env);
3208 CPUState *cs = CPU(cpu);
3209 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3211 if (arm_is_secure_below_el3(env)) {
3212 tlb_flush_page_by_mmuidx(cs, pageaddr,
3213 ARMMMUIdxBit_S1SE1 |
3214 ARMMMUIdxBit_S1SE0);
3215 } else {
3216 tlb_flush_page_by_mmuidx(cs, pageaddr,
3217 ARMMMUIdxBit_S12NSE1 |
3218 ARMMMUIdxBit_S12NSE0);
3222 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3223 uint64_t value)
3225 /* Invalidate by VA, EL2
3226 * Currently handles both VAE2 and VALE2, since we don't support
3227 * flush-last-level-only.
3229 ARMCPU *cpu = arm_env_get_cpu(env);
3230 CPUState *cs = CPU(cpu);
3231 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3233 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
3236 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3237 uint64_t value)
3239 /* Invalidate by VA, EL3
3240 * Currently handles both VAE3 and VALE3, since we don't support
3241 * flush-last-level-only.
3243 ARMCPU *cpu = arm_env_get_cpu(env);
3244 CPUState *cs = CPU(cpu);
3245 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3247 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
3250 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3251 uint64_t value)
3253 ARMCPU *cpu = arm_env_get_cpu(env);
3254 CPUState *cs = CPU(cpu);
3255 bool sec = arm_is_secure_below_el3(env);
3256 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3258 if (sec) {
3259 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3260 ARMMMUIdxBit_S1SE1 |
3261 ARMMMUIdxBit_S1SE0);
3262 } else {
3263 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3264 ARMMMUIdxBit_S12NSE1 |
3265 ARMMMUIdxBit_S12NSE0);
3269 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3270 uint64_t value)
3272 CPUState *cs = ENV_GET_CPU(env);
3273 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3275 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3276 ARMMMUIdxBit_S1E2);
3279 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3280 uint64_t value)
3282 CPUState *cs = ENV_GET_CPU(env);
3283 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3285 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3286 ARMMMUIdxBit_S1E3);
3289 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3290 uint64_t value)
3292 /* Invalidate by IPA. This has to invalidate any structures that
3293 * contain only stage 2 translation information, but does not need
3294 * to apply to structures that contain combined stage 1 and stage 2
3295 * translation information.
3296 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
3298 ARMCPU *cpu = arm_env_get_cpu(env);
3299 CPUState *cs = CPU(cpu);
3300 uint64_t pageaddr;
3302 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3303 return;
3306 pageaddr = sextract64(value << 12, 0, 48);
3308 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
3311 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3312 uint64_t value)
3314 CPUState *cs = ENV_GET_CPU(env);
3315 uint64_t pageaddr;
3317 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3318 return;
3321 pageaddr = sextract64(value << 12, 0, 48);
3323 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3324 ARMMMUIdxBit_S2NS);
3327 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
3328 bool isread)
3330 /* We don't implement EL2, so the only control on DC ZVA is the
3331 * bit in the SCTLR which can prohibit access for EL0.
3333 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
3334 return CP_ACCESS_TRAP;
3336 return CP_ACCESS_OK;
3339 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3341 ARMCPU *cpu = arm_env_get_cpu(env);
3342 int dzp_bit = 1 << 4;
3344 /* DZP indicates whether DC ZVA access is allowed */
3345 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
3346 dzp_bit = 0;
3348 return cpu->dcz_blocksize | dzp_bit;
3351 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3352 bool isread)
3354 if (!(env->pstate & PSTATE_SP)) {
3355 /* Access to SP_EL0 is undefined if it's being used as
3356 * the stack pointer.
3358 return CP_ACCESS_TRAP_UNCATEGORIZED;
3360 return CP_ACCESS_OK;
3363 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3365 return env->pstate & PSTATE_SP;
3368 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3370 update_spsel(env, val);
3373 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3374 uint64_t value)
3376 ARMCPU *cpu = arm_env_get_cpu(env);
3378 if (raw_read(env, ri) == value) {
3379 /* Skip the TLB flush if nothing actually changed; Linux likes
3380 * to do a lot of pointless SCTLR writes.
3382 return;
3385 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
3386 /* M bit is RAZ/WI for PMSA with no MPU implemented */
3387 value &= ~SCTLR_M;
3390 raw_write(env, ri, value);
3391 /* ??? Lots of these bits are not implemented. */
3392 /* This may enable/disable the MMU, so do a TLB flush. */
3393 tlb_flush(CPU(cpu));
3396 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3397 bool isread)
3399 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
3400 return CP_ACCESS_TRAP_FP_EL2;
3402 if (env->cp15.cptr_el[3] & CPTR_TFP) {
3403 return CP_ACCESS_TRAP_FP_EL3;
3405 return CP_ACCESS_OK;
3408 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3409 uint64_t value)
3411 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3414 static const ARMCPRegInfo v8_cp_reginfo[] = {
3415 /* Minimal set of EL0-visible registers. This will need to be expanded
3416 * significantly for system emulation of AArch64 CPUs.
3418 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3419 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3420 .access = PL0_RW, .type = ARM_CP_NZCV },
3421 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3422 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
3423 .type = ARM_CP_NO_RAW,
3424 .access = PL0_RW, .accessfn = aa64_daif_access,
3425 .fieldoffset = offsetof(CPUARMState, daif),
3426 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
3427 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3428 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
3429 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
3430 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
3431 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3432 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
3433 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
3434 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
3435 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3436 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
3437 .access = PL0_R, .type = ARM_CP_NO_RAW,
3438 .readfn = aa64_dczid_read },
3439 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3440 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3441 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3442 #ifndef CONFIG_USER_ONLY
3443 /* Avoid overhead of an access check that always passes in user-mode */
3444 .accessfn = aa64_zva_access,
3445 #endif
3447 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3448 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3449 .access = PL1_R, .type = ARM_CP_CURRENTEL },
3450 /* Cache ops: all NOPs since we don't emulate caches */
3451 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3452 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3453 .access = PL1_W, .type = ARM_CP_NOP },
3454 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3455 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3456 .access = PL1_W, .type = ARM_CP_NOP },
3457 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3458 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3459 .access = PL0_W, .type = ARM_CP_NOP,
3460 .accessfn = aa64_cacheop_access },
3461 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3462 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3463 .access = PL1_W, .type = ARM_CP_NOP },
3464 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3465 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3466 .access = PL1_W, .type = ARM_CP_NOP },
3467 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3468 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3469 .access = PL0_W, .type = ARM_CP_NOP,
3470 .accessfn = aa64_cacheop_access },
3471 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3472 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3473 .access = PL1_W, .type = ARM_CP_NOP },
3474 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3475 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3476 .access = PL0_W, .type = ARM_CP_NOP,
3477 .accessfn = aa64_cacheop_access },
3478 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3479 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3480 .access = PL0_W, .type = ARM_CP_NOP,
3481 .accessfn = aa64_cacheop_access },
3482 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3483 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3484 .access = PL1_W, .type = ARM_CP_NOP },
3485 /* TLBI operations */
3486 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
3487 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
3488 .access = PL1_W, .type = ARM_CP_NO_RAW,
3489 .writefn = tlbi_aa64_vmalle1is_write },
3490 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
3491 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
3492 .access = PL1_W, .type = ARM_CP_NO_RAW,
3493 .writefn = tlbi_aa64_vae1is_write },
3494 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
3495 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
3496 .access = PL1_W, .type = ARM_CP_NO_RAW,
3497 .writefn = tlbi_aa64_vmalle1is_write },
3498 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
3499 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
3500 .access = PL1_W, .type = ARM_CP_NO_RAW,
3501 .writefn = tlbi_aa64_vae1is_write },
3502 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
3503 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3504 .access = PL1_W, .type = ARM_CP_NO_RAW,
3505 .writefn = tlbi_aa64_vae1is_write },
3506 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
3507 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3508 .access = PL1_W, .type = ARM_CP_NO_RAW,
3509 .writefn = tlbi_aa64_vae1is_write },
3510 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
3511 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
3512 .access = PL1_W, .type = ARM_CP_NO_RAW,
3513 .writefn = tlbi_aa64_vmalle1_write },
3514 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
3515 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
3516 .access = PL1_W, .type = ARM_CP_NO_RAW,
3517 .writefn = tlbi_aa64_vae1_write },
3518 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
3519 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
3520 .access = PL1_W, .type = ARM_CP_NO_RAW,
3521 .writefn = tlbi_aa64_vmalle1_write },
3522 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
3523 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
3524 .access = PL1_W, .type = ARM_CP_NO_RAW,
3525 .writefn = tlbi_aa64_vae1_write },
3526 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
3527 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3528 .access = PL1_W, .type = ARM_CP_NO_RAW,
3529 .writefn = tlbi_aa64_vae1_write },
3530 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
3531 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3532 .access = PL1_W, .type = ARM_CP_NO_RAW,
3533 .writefn = tlbi_aa64_vae1_write },
3534 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3535 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3536 .access = PL2_W, .type = ARM_CP_NO_RAW,
3537 .writefn = tlbi_aa64_ipas2e1is_write },
3538 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3539 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3540 .access = PL2_W, .type = ARM_CP_NO_RAW,
3541 .writefn = tlbi_aa64_ipas2e1is_write },
3542 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3543 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3544 .access = PL2_W, .type = ARM_CP_NO_RAW,
3545 .writefn = tlbi_aa64_alle1is_write },
3546 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3547 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3548 .access = PL2_W, .type = ARM_CP_NO_RAW,
3549 .writefn = tlbi_aa64_alle1is_write },
3550 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3551 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3552 .access = PL2_W, .type = ARM_CP_NO_RAW,
3553 .writefn = tlbi_aa64_ipas2e1_write },
3554 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3555 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3556 .access = PL2_W, .type = ARM_CP_NO_RAW,
3557 .writefn = tlbi_aa64_ipas2e1_write },
3558 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3559 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3560 .access = PL2_W, .type = ARM_CP_NO_RAW,
3561 .writefn = tlbi_aa64_alle1_write },
3562 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3563 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3564 .access = PL2_W, .type = ARM_CP_NO_RAW,
3565 .writefn = tlbi_aa64_alle1is_write },
3566 #ifndef CONFIG_USER_ONLY
3567 /* 64 bit address translation operations */
3568 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3569 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
3570 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3571 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3572 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
3573 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3574 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3575 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
3576 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3577 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3578 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
3579 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3580 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
3581 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
3582 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3583 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
3584 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
3585 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3586 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
3587 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
3588 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3589 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
3590 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
3591 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3592 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3593 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3594 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3595 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3596 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3597 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3598 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3599 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3600 .type = ARM_CP_ALIAS,
3601 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3602 .access = PL1_RW, .resetvalue = 0,
3603 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3604 .writefn = par_write },
3605 #endif
3606 /* TLB invalidate last level of translation table walk */
3607 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3608 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
3609 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3610 .type = ARM_CP_NO_RAW, .access = PL1_W,
3611 .writefn = tlbimvaa_is_write },
3612 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3613 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
3614 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3615 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
3616 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3617 .type = ARM_CP_NO_RAW, .access = PL2_W,
3618 .writefn = tlbimva_hyp_write },
3619 { .name = "TLBIMVALHIS",
3620 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3621 .type = ARM_CP_NO_RAW, .access = PL2_W,
3622 .writefn = tlbimva_hyp_is_write },
3623 { .name = "TLBIIPAS2",
3624 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3625 .type = ARM_CP_NO_RAW, .access = PL2_W,
3626 .writefn = tlbiipas2_write },
3627 { .name = "TLBIIPAS2IS",
3628 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3629 .type = ARM_CP_NO_RAW, .access = PL2_W,
3630 .writefn = tlbiipas2_is_write },
3631 { .name = "TLBIIPAS2L",
3632 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3633 .type = ARM_CP_NO_RAW, .access = PL2_W,
3634 .writefn = tlbiipas2_write },
3635 { .name = "TLBIIPAS2LIS",
3636 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3637 .type = ARM_CP_NO_RAW, .access = PL2_W,
3638 .writefn = tlbiipas2_is_write },
3639 /* 32 bit cache operations */
3640 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3641 .type = ARM_CP_NOP, .access = PL1_W },
3642 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3643 .type = ARM_CP_NOP, .access = PL1_W },
3644 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3645 .type = ARM_CP_NOP, .access = PL1_W },
3646 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3647 .type = ARM_CP_NOP, .access = PL1_W },
3648 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3649 .type = ARM_CP_NOP, .access = PL1_W },
3650 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3651 .type = ARM_CP_NOP, .access = PL1_W },
3652 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3653 .type = ARM_CP_NOP, .access = PL1_W },
3654 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3655 .type = ARM_CP_NOP, .access = PL1_W },
3656 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3657 .type = ARM_CP_NOP, .access = PL1_W },
3658 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3659 .type = ARM_CP_NOP, .access = PL1_W },
3660 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3661 .type = ARM_CP_NOP, .access = PL1_W },
3662 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3663 .type = ARM_CP_NOP, .access = PL1_W },
3664 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3665 .type = ARM_CP_NOP, .access = PL1_W },
3666 /* MMU Domain access control / MPU write buffer control */
3667 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3668 .access = PL1_RW, .resetvalue = 0,
3669 .writefn = dacr_write, .raw_writefn = raw_write,
3670 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3671 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
3672 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
3673 .type = ARM_CP_ALIAS,
3674 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
3675 .access = PL1_RW,
3676 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
3677 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
3678 .type = ARM_CP_ALIAS,
3679 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
3680 .access = PL1_RW,
3681 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
3682 /* We rely on the access checks not allowing the guest to write to the
3683 * state field when SPSel indicates that it's being used as the stack
3684 * pointer.
3686 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3687 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3688 .access = PL1_RW, .accessfn = sp_el0_access,
3689 .type = ARM_CP_ALIAS,
3690 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
3691 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3692 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
3693 .access = PL2_RW, .type = ARM_CP_ALIAS,
3694 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
3695 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3696 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
3697 .type = ARM_CP_NO_RAW,
3698 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
3699 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3700 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3701 .type = ARM_CP_ALIAS,
3702 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3703 .access = PL2_RW, .accessfn = fpexc32_access },
3704 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3705 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3706 .access = PL2_RW, .resetvalue = 0,
3707 .writefn = dacr_write, .raw_writefn = raw_write,
3708 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3709 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3710 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3711 .access = PL2_RW, .resetvalue = 0,
3712 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3713 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3714 .type = ARM_CP_ALIAS,
3715 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3716 .access = PL2_RW,
3717 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3718 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3719 .type = ARM_CP_ALIAS,
3720 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3721 .access = PL2_RW,
3722 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3723 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3724 .type = ARM_CP_ALIAS,
3725 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3726 .access = PL2_RW,
3727 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3728 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3729 .type = ARM_CP_ALIAS,
3730 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3731 .access = PL2_RW,
3732 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
3733 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3734 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3735 .resetvalue = 0,
3736 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3737 { .name = "SDCR", .type = ARM_CP_ALIAS,
3738 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3739 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3740 .writefn = sdcr_write,
3741 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
3742 REGINFO_SENTINEL
3745 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
3746 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
3747 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3748 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3749 .access = PL2_RW,
3750 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3751 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3752 .type = ARM_CP_NO_RAW,
3753 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3754 .access = PL2_RW,
3755 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3756 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3757 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3758 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3759 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3760 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3761 .access = PL2_RW, .type = ARM_CP_CONST,
3762 .resetvalue = 0 },
3763 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3764 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3765 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3766 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3767 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3768 .access = PL2_RW, .type = ARM_CP_CONST,
3769 .resetvalue = 0 },
3770 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3771 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3772 .access = PL2_RW, .type = ARM_CP_CONST,
3773 .resetvalue = 0 },
3774 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3775 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3776 .access = PL2_RW, .type = ARM_CP_CONST,
3777 .resetvalue = 0 },
3778 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3779 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3780 .access = PL2_RW, .type = ARM_CP_CONST,
3781 .resetvalue = 0 },
3782 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3783 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3784 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3785 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3786 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3787 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3788 .type = ARM_CP_CONST, .resetvalue = 0 },
3789 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3790 .cp = 15, .opc1 = 6, .crm = 2,
3791 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3792 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3793 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3794 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3795 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3796 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3797 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3798 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3799 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3800 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3801 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3802 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3803 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3804 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3805 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3806 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3807 .resetvalue = 0 },
3808 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3809 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3810 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3811 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3812 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3813 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3814 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3815 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3816 .resetvalue = 0 },
3817 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3818 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3819 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3820 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3821 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3822 .resetvalue = 0 },
3823 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3824 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3825 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3826 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3827 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3828 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3829 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3830 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3831 .access = PL2_RW, .accessfn = access_tda,
3832 .type = ARM_CP_CONST, .resetvalue = 0 },
3833 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3834 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3835 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3836 .type = ARM_CP_CONST, .resetvalue = 0 },
3837 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
3838 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
3839 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3840 REGINFO_SENTINEL
3843 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3845 ARMCPU *cpu = arm_env_get_cpu(env);
3846 uint64_t valid_mask = HCR_MASK;
3848 if (arm_feature(env, ARM_FEATURE_EL3)) {
3849 valid_mask &= ~HCR_HCD;
3850 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
3851 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
3852 * However, if we're using the SMC PSCI conduit then QEMU is
3853 * effectively acting like EL3 firmware and so the guest at
3854 * EL2 should retain the ability to prevent EL1 from being
3855 * able to make SMC calls into the ersatz firmware, so in
3856 * that case HCR.TSC should be read/write.
3858 valid_mask &= ~HCR_TSC;
3861 /* Clear RES0 bits. */
3862 value &= valid_mask;
3864 /* These bits change the MMU setup:
3865 * HCR_VM enables stage 2 translation
3866 * HCR_PTW forbids certain page-table setups
3867 * HCR_DC Disables stage1 and enables stage2 translation
3869 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3870 tlb_flush(CPU(cpu));
3872 raw_write(env, ri, value);
3875 static const ARMCPRegInfo el2_cp_reginfo[] = {
3876 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3877 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3878 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3879 .writefn = hcr_write },
3880 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
3881 .type = ARM_CP_ALIAS,
3882 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3883 .access = PL2_RW,
3884 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
3885 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
3886 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3887 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
3888 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3889 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3890 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3891 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
3892 .type = ARM_CP_ALIAS,
3893 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
3894 .access = PL2_RW,
3895 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
3896 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3897 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3898 .access = PL2_RW, .writefn = vbar_write,
3899 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3900 .resetvalue = 0 },
3901 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3902 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
3903 .access = PL3_RW, .type = ARM_CP_ALIAS,
3904 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
3905 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3906 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3907 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3908 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
3909 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3910 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3911 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3912 .resetvalue = 0 },
3913 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3914 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3915 .access = PL2_RW, .type = ARM_CP_ALIAS,
3916 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
3917 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3918 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3919 .access = PL2_RW, .type = ARM_CP_CONST,
3920 .resetvalue = 0 },
3921 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3922 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3923 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3924 .access = PL2_RW, .type = ARM_CP_CONST,
3925 .resetvalue = 0 },
3926 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3927 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3928 .access = PL2_RW, .type = ARM_CP_CONST,
3929 .resetvalue = 0 },
3930 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3931 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3932 .access = PL2_RW, .type = ARM_CP_CONST,
3933 .resetvalue = 0 },
3934 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3935 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3936 .access = PL2_RW,
3937 /* no .writefn needed as this can't cause an ASID change;
3938 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3940 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
3941 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3942 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3943 .type = ARM_CP_ALIAS,
3944 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3945 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3946 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3947 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3948 .access = PL2_RW,
3949 /* no .writefn needed as this can't cause an ASID change;
3950 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3952 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3953 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3954 .cp = 15, .opc1 = 6, .crm = 2,
3955 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3956 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3957 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3958 .writefn = vttbr_write },
3959 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3960 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3961 .access = PL2_RW, .writefn = vttbr_write,
3962 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
3963 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3964 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3965 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3966 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
3967 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3968 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3969 .access = PL2_RW, .resetvalue = 0,
3970 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
3971 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3972 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3973 .access = PL2_RW, .resetvalue = 0,
3974 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3975 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3976 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3977 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3978 { .name = "TLBIALLNSNH",
3979 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3980 .type = ARM_CP_NO_RAW, .access = PL2_W,
3981 .writefn = tlbiall_nsnh_write },
3982 { .name = "TLBIALLNSNHIS",
3983 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3984 .type = ARM_CP_NO_RAW, .access = PL2_W,
3985 .writefn = tlbiall_nsnh_is_write },
3986 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3987 .type = ARM_CP_NO_RAW, .access = PL2_W,
3988 .writefn = tlbiall_hyp_write },
3989 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3990 .type = ARM_CP_NO_RAW, .access = PL2_W,
3991 .writefn = tlbiall_hyp_is_write },
3992 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3993 .type = ARM_CP_NO_RAW, .access = PL2_W,
3994 .writefn = tlbimva_hyp_write },
3995 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3996 .type = ARM_CP_NO_RAW, .access = PL2_W,
3997 .writefn = tlbimva_hyp_is_write },
3998 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3999 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
4000 .type = ARM_CP_NO_RAW, .access = PL2_W,
4001 .writefn = tlbi_aa64_alle2_write },
4002 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
4003 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
4004 .type = ARM_CP_NO_RAW, .access = PL2_W,
4005 .writefn = tlbi_aa64_vae2_write },
4006 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
4007 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4008 .access = PL2_W, .type = ARM_CP_NO_RAW,
4009 .writefn = tlbi_aa64_vae2_write },
4010 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
4011 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
4012 .access = PL2_W, .type = ARM_CP_NO_RAW,
4013 .writefn = tlbi_aa64_alle2is_write },
4014 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
4015 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
4016 .type = ARM_CP_NO_RAW, .access = PL2_W,
4017 .writefn = tlbi_aa64_vae2is_write },
4018 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
4019 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4020 .access = PL2_W, .type = ARM_CP_NO_RAW,
4021 .writefn = tlbi_aa64_vae2is_write },
4022 #ifndef CONFIG_USER_ONLY
4023 /* Unlike the other EL2-related AT operations, these must
4024 * UNDEF from EL3 if EL2 is not implemented, which is why we
4025 * define them here rather than with the rest of the AT ops.
4027 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
4028 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
4029 .access = PL2_W, .accessfn = at_s1e2_access,
4030 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4031 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
4032 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
4033 .access = PL2_W, .accessfn = at_s1e2_access,
4034 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4035 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
4036 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
4037 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
4038 * to behave as if SCR.NS was 1.
4040 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
4041 .access = PL2_W,
4042 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
4043 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
4044 .access = PL2_W,
4045 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
4046 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
4047 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
4048 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
4049 * reset values as IMPDEF. We choose to reset to 3 to comply with
4050 * both ARMv7 and ARMv8.
4052 .access = PL2_RW, .resetvalue = 3,
4053 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
4054 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
4055 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
4056 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
4057 .writefn = gt_cntvoff_write,
4058 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
4059 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
4060 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
4061 .writefn = gt_cntvoff_write,
4062 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
4063 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
4064 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
4065 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4066 .type = ARM_CP_IO, .access = PL2_RW,
4067 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4068 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4069 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4070 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
4071 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4072 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4073 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
4074 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
4075 .resetfn = gt_hyp_timer_reset,
4076 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
4077 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4078 .type = ARM_CP_IO,
4079 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4080 .access = PL2_RW,
4081 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
4082 .resetvalue = 0,
4083 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
4084 #endif
4085 /* The only field of MDCR_EL2 that has a defined architectural reset value
4086 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
4087 * don't impelment any PMU event counters, so using zero as a reset
4088 * value for MDCR_EL2 is okay
4090 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4091 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4092 .access = PL2_RW, .resetvalue = 0,
4093 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
4094 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
4095 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4096 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4097 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4098 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
4099 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4100 .access = PL2_RW,
4101 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4102 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4103 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4104 .access = PL2_RW,
4105 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
4106 REGINFO_SENTINEL
4109 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4110 bool isread)
4112 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
4113 * At Secure EL1 it traps to EL3.
4115 if (arm_current_el(env) == 3) {
4116 return CP_ACCESS_OK;
4118 if (arm_is_secure_below_el3(env)) {
4119 return CP_ACCESS_TRAP_EL3;
4121 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4122 if (isread) {
4123 return CP_ACCESS_OK;
4125 return CP_ACCESS_TRAP_UNCATEGORIZED;
4128 static const ARMCPRegInfo el3_cp_reginfo[] = {
4129 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
4130 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
4131 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
4132 .resetvalue = 0, .writefn = scr_write },
4133 { .name = "SCR", .type = ARM_CP_ALIAS,
4134 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
4135 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4136 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
4137 .writefn = scr_write },
4138 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
4139 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
4140 .access = PL3_RW, .resetvalue = 0,
4141 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
4142 { .name = "SDER",
4143 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
4144 .access = PL3_RW, .resetvalue = 0,
4145 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
4146 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4147 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4148 .writefn = vbar_write, .resetvalue = 0,
4149 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
4150 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
4151 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
4152 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
4153 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
4154 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
4155 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
4156 .access = PL3_RW,
4157 /* no .writefn needed as this can't cause an ASID change;
4158 * we must provide a .raw_writefn and .resetfn because we handle
4159 * reset and migration for the AArch32 TTBCR(S), which might be
4160 * using mask and base_mask.
4162 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
4163 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
4164 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
4165 .type = ARM_CP_ALIAS,
4166 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
4167 .access = PL3_RW,
4168 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
4169 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
4170 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
4171 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
4172 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
4173 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
4174 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
4175 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
4176 .type = ARM_CP_ALIAS,
4177 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
4178 .access = PL3_RW,
4179 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
4180 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
4181 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
4182 .access = PL3_RW, .writefn = vbar_write,
4183 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
4184 .resetvalue = 0 },
4185 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
4186 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
4187 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
4188 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4189 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
4190 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
4191 .access = PL3_RW, .resetvalue = 0,
4192 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
4193 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
4194 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
4195 .access = PL3_RW, .type = ARM_CP_CONST,
4196 .resetvalue = 0 },
4197 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
4198 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
4199 .access = PL3_RW, .type = ARM_CP_CONST,
4200 .resetvalue = 0 },
4201 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
4202 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
4203 .access = PL3_RW, .type = ARM_CP_CONST,
4204 .resetvalue = 0 },
4205 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
4206 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
4207 .access = PL3_W, .type = ARM_CP_NO_RAW,
4208 .writefn = tlbi_aa64_alle3is_write },
4209 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
4210 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
4211 .access = PL3_W, .type = ARM_CP_NO_RAW,
4212 .writefn = tlbi_aa64_vae3is_write },
4213 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
4214 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
4215 .access = PL3_W, .type = ARM_CP_NO_RAW,
4216 .writefn = tlbi_aa64_vae3is_write },
4217 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
4218 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
4219 .access = PL3_W, .type = ARM_CP_NO_RAW,
4220 .writefn = tlbi_aa64_alle3_write },
4221 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
4222 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
4223 .access = PL3_W, .type = ARM_CP_NO_RAW,
4224 .writefn = tlbi_aa64_vae3_write },
4225 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
4226 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
4227 .access = PL3_W, .type = ARM_CP_NO_RAW,
4228 .writefn = tlbi_aa64_vae3_write },
4229 REGINFO_SENTINEL
4232 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4233 bool isread)
4235 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
4236 * but the AArch32 CTR has its own reginfo struct)
4238 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
4239 return CP_ACCESS_TRAP;
4241 return CP_ACCESS_OK;
4244 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4245 uint64_t value)
4247 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
4248 * read via a bit in OSLSR_EL1.
4250 int oslock;
4252 if (ri->state == ARM_CP_STATE_AA32) {
4253 oslock = (value == 0xC5ACCE55);
4254 } else {
4255 oslock = value & 1;
4258 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
4261 static const ARMCPRegInfo debug_cp_reginfo[] = {
4262 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
4263 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
4264 * unlike DBGDRAR it is never accessible from EL0.
4265 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
4266 * accessor.
4268 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
4269 .access = PL0_R, .accessfn = access_tdra,
4270 .type = ARM_CP_CONST, .resetvalue = 0 },
4271 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
4272 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4273 .access = PL1_R, .accessfn = access_tdra,
4274 .type = ARM_CP_CONST, .resetvalue = 0 },
4275 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4276 .access = PL0_R, .accessfn = access_tdra,
4277 .type = ARM_CP_CONST, .resetvalue = 0 },
4278 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
4279 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
4280 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4281 .access = PL1_RW, .accessfn = access_tda,
4282 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
4283 .resetvalue = 0 },
4284 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
4285 * We don't implement the configurable EL0 access.
4287 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
4288 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4289 .type = ARM_CP_ALIAS,
4290 .access = PL1_R, .accessfn = access_tda,
4291 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
4292 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
4293 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
4294 .access = PL1_W, .type = ARM_CP_NO_RAW,
4295 .accessfn = access_tdosa,
4296 .writefn = oslar_write },
4297 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
4298 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
4299 .access = PL1_R, .resetvalue = 10,
4300 .accessfn = access_tdosa,
4301 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
4302 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
4303 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
4304 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
4305 .access = PL1_RW, .accessfn = access_tdosa,
4306 .type = ARM_CP_NOP },
4307 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
4308 * implement vector catch debug events yet.
4310 { .name = "DBGVCR",
4311 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4312 .access = PL1_RW, .accessfn = access_tda,
4313 .type = ARM_CP_NOP },
4314 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
4315 * to save and restore a 32-bit guest's DBGVCR)
4317 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
4318 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
4319 .access = PL2_RW, .accessfn = access_tda,
4320 .type = ARM_CP_NOP },
4321 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
4322 * Channel but Linux may try to access this register. The 32-bit
4323 * alias is DBGDCCINT.
4325 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
4326 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4327 .access = PL1_RW, .accessfn = access_tda,
4328 .type = ARM_CP_NOP },
4329 REGINFO_SENTINEL
4332 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
4333 /* 64 bit access versions of the (dummy) debug registers */
4334 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
4335 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4336 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
4337 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
4338 REGINFO_SENTINEL
4341 /* Return the exception level to which SVE-disabled exceptions should
4342 * be taken, or 0 if SVE is enabled.
4344 static int sve_exception_el(CPUARMState *env)
4346 #ifndef CONFIG_USER_ONLY
4347 unsigned current_el = arm_current_el(env);
4349 /* The CPACR.ZEN controls traps to EL1:
4350 * 0, 2 : trap EL0 and EL1 accesses
4351 * 1 : trap only EL0 accesses
4352 * 3 : trap no accesses
4354 switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
4355 default:
4356 if (current_el <= 1) {
4357 /* Trap to PL1, which might be EL1 or EL3 */
4358 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4359 return 3;
4361 return 1;
4363 break;
4364 case 1:
4365 if (current_el == 0) {
4366 return 1;
4368 break;
4369 case 3:
4370 break;
4373 /* Similarly for CPACR.FPEN, after having checked ZEN. */
4374 switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
4375 default:
4376 if (current_el <= 1) {
4377 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
4378 return 3;
4380 return 1;
4382 break;
4383 case 1:
4384 if (current_el == 0) {
4385 return 1;
4387 break;
4388 case 3:
4389 break;
4392 /* CPTR_EL2. Check both TZ and TFP. */
4393 if (current_el <= 2
4394 && (env->cp15.cptr_el[2] & (CPTR_TFP | CPTR_TZ))
4395 && !arm_is_secure_below_el3(env)) {
4396 return 2;
4399 /* CPTR_EL3. Check both EZ and TFP. */
4400 if (!(env->cp15.cptr_el[3] & CPTR_EZ)
4401 || (env->cp15.cptr_el[3] & CPTR_TFP)) {
4402 return 3;
4404 #endif
4405 return 0;
4408 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4409 uint64_t value)
4411 /* Bits other than [3:0] are RAZ/WI. */
4412 raw_write(env, ri, value & 0xf);
4415 static const ARMCPRegInfo zcr_el1_reginfo = {
4416 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
4417 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
4418 .access = PL1_RW, .type = ARM_CP_SVE,
4419 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
4420 .writefn = zcr_write, .raw_writefn = raw_write
4423 static const ARMCPRegInfo zcr_el2_reginfo = {
4424 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4425 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
4426 .access = PL2_RW, .type = ARM_CP_SVE,
4427 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
4428 .writefn = zcr_write, .raw_writefn = raw_write
4431 static const ARMCPRegInfo zcr_no_el2_reginfo = {
4432 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
4433 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
4434 .access = PL2_RW, .type = ARM_CP_SVE,
4435 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
4438 static const ARMCPRegInfo zcr_el3_reginfo = {
4439 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
4440 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
4441 .access = PL3_RW, .type = ARM_CP_SVE,
4442 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
4443 .writefn = zcr_write, .raw_writefn = raw_write
4446 void hw_watchpoint_update(ARMCPU *cpu, int n)
4448 CPUARMState *env = &cpu->env;
4449 vaddr len = 0;
4450 vaddr wvr = env->cp15.dbgwvr[n];
4451 uint64_t wcr = env->cp15.dbgwcr[n];
4452 int mask;
4453 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
4455 if (env->cpu_watchpoint[n]) {
4456 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
4457 env->cpu_watchpoint[n] = NULL;
4460 if (!extract64(wcr, 0, 1)) {
4461 /* E bit clear : watchpoint disabled */
4462 return;
4465 switch (extract64(wcr, 3, 2)) {
4466 case 0:
4467 /* LSC 00 is reserved and must behave as if the wp is disabled */
4468 return;
4469 case 1:
4470 flags |= BP_MEM_READ;
4471 break;
4472 case 2:
4473 flags |= BP_MEM_WRITE;
4474 break;
4475 case 3:
4476 flags |= BP_MEM_ACCESS;
4477 break;
4480 /* Attempts to use both MASK and BAS fields simultaneously are
4481 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
4482 * thus generating a watchpoint for every byte in the masked region.
4484 mask = extract64(wcr, 24, 4);
4485 if (mask == 1 || mask == 2) {
4486 /* Reserved values of MASK; we must act as if the mask value was
4487 * some non-reserved value, or as if the watchpoint were disabled.
4488 * We choose the latter.
4490 return;
4491 } else if (mask) {
4492 /* Watchpoint covers an aligned area up to 2GB in size */
4493 len = 1ULL << mask;
4494 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
4495 * whether the watchpoint fires when the unmasked bits match; we opt
4496 * to generate the exceptions.
4498 wvr &= ~(len - 1);
4499 } else {
4500 /* Watchpoint covers bytes defined by the byte address select bits */
4501 int bas = extract64(wcr, 5, 8);
4502 int basstart;
4504 if (bas == 0) {
4505 /* This must act as if the watchpoint is disabled */
4506 return;
4509 if (extract64(wvr, 2, 1)) {
4510 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
4511 * ignored, and BAS[3:0] define which bytes to watch.
4513 bas &= 0xf;
4515 /* The BAS bits are supposed to be programmed to indicate a contiguous
4516 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
4517 * we fire for each byte in the word/doubleword addressed by the WVR.
4518 * We choose to ignore any non-zero bits after the first range of 1s.
4520 basstart = ctz32(bas);
4521 len = cto32(bas >> basstart);
4522 wvr += basstart;
4525 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4526 &env->cpu_watchpoint[n]);
4529 void hw_watchpoint_update_all(ARMCPU *cpu)
4531 int i;
4532 CPUARMState *env = &cpu->env;
4534 /* Completely clear out existing QEMU watchpoints and our array, to
4535 * avoid possible stale entries following migration load.
4537 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4538 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4540 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4541 hw_watchpoint_update(cpu, i);
4545 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4546 uint64_t value)
4548 ARMCPU *cpu = arm_env_get_cpu(env);
4549 int i = ri->crm;
4551 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4552 * register reads and behaves as if values written are sign extended.
4553 * Bits [1:0] are RES0.
4555 value = sextract64(value, 0, 49) & ~3ULL;
4557 raw_write(env, ri, value);
4558 hw_watchpoint_update(cpu, i);
4561 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4562 uint64_t value)
4564 ARMCPU *cpu = arm_env_get_cpu(env);
4565 int i = ri->crm;
4567 raw_write(env, ri, value);
4568 hw_watchpoint_update(cpu, i);
4571 void hw_breakpoint_update(ARMCPU *cpu, int n)
4573 CPUARMState *env = &cpu->env;
4574 uint64_t bvr = env->cp15.dbgbvr[n];
4575 uint64_t bcr = env->cp15.dbgbcr[n];
4576 vaddr addr;
4577 int bt;
4578 int flags = BP_CPU;
4580 if (env->cpu_breakpoint[n]) {
4581 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4582 env->cpu_breakpoint[n] = NULL;
4585 if (!extract64(bcr, 0, 1)) {
4586 /* E bit clear : watchpoint disabled */
4587 return;
4590 bt = extract64(bcr, 20, 4);
4592 switch (bt) {
4593 case 4: /* unlinked address mismatch (reserved if AArch64) */
4594 case 5: /* linked address mismatch (reserved if AArch64) */
4595 qemu_log_mask(LOG_UNIMP,
4596 "arm: address mismatch breakpoint types not implemented\n");
4597 return;
4598 case 0: /* unlinked address match */
4599 case 1: /* linked address match */
4601 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4602 * we behave as if the register was sign extended. Bits [1:0] are
4603 * RES0. The BAS field is used to allow setting breakpoints on 16
4604 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4605 * a bp will fire if the addresses covered by the bp and the addresses
4606 * covered by the insn overlap but the insn doesn't start at the
4607 * start of the bp address range. We choose to require the insn and
4608 * the bp to have the same address. The constraints on writing to
4609 * BAS enforced in dbgbcr_write mean we have only four cases:
4610 * 0b0000 => no breakpoint
4611 * 0b0011 => breakpoint on addr
4612 * 0b1100 => breakpoint on addr + 2
4613 * 0b1111 => breakpoint on addr
4614 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4616 int bas = extract64(bcr, 5, 4);
4617 addr = sextract64(bvr, 0, 49) & ~3ULL;
4618 if (bas == 0) {
4619 return;
4621 if (bas == 0xc) {
4622 addr += 2;
4624 break;
4626 case 2: /* unlinked context ID match */
4627 case 8: /* unlinked VMID match (reserved if no EL2) */
4628 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4629 qemu_log_mask(LOG_UNIMP,
4630 "arm: unlinked context breakpoint types not implemented\n");
4631 return;
4632 case 9: /* linked VMID match (reserved if no EL2) */
4633 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4634 case 3: /* linked context ID match */
4635 default:
4636 /* We must generate no events for Linked context matches (unless
4637 * they are linked to by some other bp/wp, which is handled in
4638 * updates for the linking bp/wp). We choose to also generate no events
4639 * for reserved values.
4641 return;
4644 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4647 void hw_breakpoint_update_all(ARMCPU *cpu)
4649 int i;
4650 CPUARMState *env = &cpu->env;
4652 /* Completely clear out existing QEMU breakpoints and our array, to
4653 * avoid possible stale entries following migration load.
4655 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4656 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4658 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4659 hw_breakpoint_update(cpu, i);
4663 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4664 uint64_t value)
4666 ARMCPU *cpu = arm_env_get_cpu(env);
4667 int i = ri->crm;
4669 raw_write(env, ri, value);
4670 hw_breakpoint_update(cpu, i);
4673 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4674 uint64_t value)
4676 ARMCPU *cpu = arm_env_get_cpu(env);
4677 int i = ri->crm;
4679 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4680 * copy of BAS[0].
4682 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4683 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4685 raw_write(env, ri, value);
4686 hw_breakpoint_update(cpu, i);
4689 static void define_debug_regs(ARMCPU *cpu)
4691 /* Define v7 and v8 architectural debug registers.
4692 * These are just dummy implementations for now.
4694 int i;
4695 int wrps, brps, ctx_cmps;
4696 ARMCPRegInfo dbgdidr = {
4697 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
4698 .access = PL0_R, .accessfn = access_tda,
4699 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
4702 /* Note that all these register fields hold "number of Xs minus 1". */
4703 brps = extract32(cpu->dbgdidr, 24, 4);
4704 wrps = extract32(cpu->dbgdidr, 28, 4);
4705 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4707 assert(ctx_cmps <= brps);
4709 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4710 * of the debug registers such as number of breakpoints;
4711 * check that if they both exist then they agree.
4713 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4714 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4715 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
4716 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
4719 define_one_arm_cp_reg(cpu, &dbgdidr);
4720 define_arm_cp_regs(cpu, debug_cp_reginfo);
4722 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4723 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4726 for (i = 0; i < brps + 1; i++) {
4727 ARMCPRegInfo dbgregs[] = {
4728 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4729 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
4730 .access = PL1_RW, .accessfn = access_tda,
4731 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4732 .writefn = dbgbvr_write, .raw_writefn = raw_write
4734 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4735 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
4736 .access = PL1_RW, .accessfn = access_tda,
4737 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4738 .writefn = dbgbcr_write, .raw_writefn = raw_write
4740 REGINFO_SENTINEL
4742 define_arm_cp_regs(cpu, dbgregs);
4745 for (i = 0; i < wrps + 1; i++) {
4746 ARMCPRegInfo dbgregs[] = {
4747 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4748 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
4749 .access = PL1_RW, .accessfn = access_tda,
4750 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4751 .writefn = dbgwvr_write, .raw_writefn = raw_write
4753 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4754 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
4755 .access = PL1_RW, .accessfn = access_tda,
4756 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4757 .writefn = dbgwcr_write, .raw_writefn = raw_write
4759 REGINFO_SENTINEL
4761 define_arm_cp_regs(cpu, dbgregs);
4765 /* We don't know until after realize whether there's a GICv3
4766 * attached, and that is what registers the gicv3 sysregs.
4767 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
4768 * at runtime.
4770 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
4772 ARMCPU *cpu = arm_env_get_cpu(env);
4773 uint64_t pfr1 = cpu->id_pfr1;
4775 if (env->gicv3state) {
4776 pfr1 |= 1 << 28;
4778 return pfr1;
4781 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
4783 ARMCPU *cpu = arm_env_get_cpu(env);
4784 uint64_t pfr0 = cpu->id_aa64pfr0;
4786 if (env->gicv3state) {
4787 pfr0 |= 1 << 24;
4789 return pfr0;
4792 void register_cp_regs_for_features(ARMCPU *cpu)
4794 /* Register all the coprocessor registers based on feature bits */
4795 CPUARMState *env = &cpu->env;
4796 if (arm_feature(env, ARM_FEATURE_M)) {
4797 /* M profile has no coprocessor registers */
4798 return;
4801 define_arm_cp_regs(cpu, cp_reginfo);
4802 if (!arm_feature(env, ARM_FEATURE_V8)) {
4803 /* Must go early as it is full of wildcards that may be
4804 * overridden by later definitions.
4806 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4809 if (arm_feature(env, ARM_FEATURE_V6)) {
4810 /* The ID registers all have impdef reset values */
4811 ARMCPRegInfo v6_idregs[] = {
4812 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4813 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4814 .access = PL1_R, .type = ARM_CP_CONST,
4815 .resetvalue = cpu->id_pfr0 },
4816 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
4817 * the value of the GIC field until after we define these regs.
4819 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4820 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
4821 .access = PL1_R, .type = ARM_CP_NO_RAW,
4822 .readfn = id_pfr1_read,
4823 .writefn = arm_cp_write_ignore },
4824 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4825 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4826 .access = PL1_R, .type = ARM_CP_CONST,
4827 .resetvalue = cpu->id_dfr0 },
4828 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4829 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4830 .access = PL1_R, .type = ARM_CP_CONST,
4831 .resetvalue = cpu->id_afr0 },
4832 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4833 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4834 .access = PL1_R, .type = ARM_CP_CONST,
4835 .resetvalue = cpu->id_mmfr0 },
4836 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4837 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4838 .access = PL1_R, .type = ARM_CP_CONST,
4839 .resetvalue = cpu->id_mmfr1 },
4840 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4841 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4842 .access = PL1_R, .type = ARM_CP_CONST,
4843 .resetvalue = cpu->id_mmfr2 },
4844 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4845 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4846 .access = PL1_R, .type = ARM_CP_CONST,
4847 .resetvalue = cpu->id_mmfr3 },
4848 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4849 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4850 .access = PL1_R, .type = ARM_CP_CONST,
4851 .resetvalue = cpu->id_isar0 },
4852 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4853 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4854 .access = PL1_R, .type = ARM_CP_CONST,
4855 .resetvalue = cpu->id_isar1 },
4856 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4857 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4858 .access = PL1_R, .type = ARM_CP_CONST,
4859 .resetvalue = cpu->id_isar2 },
4860 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4861 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4862 .access = PL1_R, .type = ARM_CP_CONST,
4863 .resetvalue = cpu->id_isar3 },
4864 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4865 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4866 .access = PL1_R, .type = ARM_CP_CONST,
4867 .resetvalue = cpu->id_isar4 },
4868 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4869 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4870 .access = PL1_R, .type = ARM_CP_CONST,
4871 .resetvalue = cpu->id_isar5 },
4872 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4873 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4874 .access = PL1_R, .type = ARM_CP_CONST,
4875 .resetvalue = cpu->id_mmfr4 },
4876 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
4877 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4878 .access = PL1_R, .type = ARM_CP_CONST,
4879 .resetvalue = cpu->id_isar6 },
4880 REGINFO_SENTINEL
4882 define_arm_cp_regs(cpu, v6_idregs);
4883 define_arm_cp_regs(cpu, v6_cp_reginfo);
4884 } else {
4885 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4887 if (arm_feature(env, ARM_FEATURE_V6K)) {
4888 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4890 if (arm_feature(env, ARM_FEATURE_V7MP) &&
4891 !arm_feature(env, ARM_FEATURE_PMSA)) {
4892 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4894 if (arm_feature(env, ARM_FEATURE_V7)) {
4895 /* v7 performance monitor control register: same implementor
4896 * field as main ID register, and we implement only the cycle
4897 * count register.
4899 #ifndef CONFIG_USER_ONLY
4900 ARMCPRegInfo pmcr = {
4901 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
4902 .access = PL0_RW,
4903 .type = ARM_CP_IO | ARM_CP_ALIAS,
4904 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
4905 .accessfn = pmreg_access, .writefn = pmcr_write,
4906 .raw_writefn = raw_write,
4908 ARMCPRegInfo pmcr64 = {
4909 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4910 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4911 .access = PL0_RW, .accessfn = pmreg_access,
4912 .type = ARM_CP_IO,
4913 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4914 .resetvalue = cpu->midr & 0xff000000,
4915 .writefn = pmcr_write, .raw_writefn = raw_write,
4917 define_one_arm_cp_reg(cpu, &pmcr);
4918 define_one_arm_cp_reg(cpu, &pmcr64);
4919 #endif
4920 ARMCPRegInfo clidr = {
4921 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4922 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
4923 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4925 define_one_arm_cp_reg(cpu, &clidr);
4926 define_arm_cp_regs(cpu, v7_cp_reginfo);
4927 define_debug_regs(cpu);
4928 } else {
4929 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
4931 if (arm_feature(env, ARM_FEATURE_V8)) {
4932 /* AArch64 ID registers, which all have impdef reset values.
4933 * Note that within the ID register ranges the unused slots
4934 * must all RAZ, not UNDEF; future architecture versions may
4935 * define new registers here.
4937 ARMCPRegInfo v8_idregs[] = {
4938 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
4939 * know the right value for the GIC field until after we
4940 * define these regs.
4942 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4943 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4944 .access = PL1_R, .type = ARM_CP_NO_RAW,
4945 .readfn = id_aa64pfr0_read,
4946 .writefn = arm_cp_write_ignore },
4947 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4948 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4949 .access = PL1_R, .type = ARM_CP_CONST,
4950 .resetvalue = cpu->id_aa64pfr1},
4951 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4952 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4953 .access = PL1_R, .type = ARM_CP_CONST,
4954 .resetvalue = 0 },
4955 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4956 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4957 .access = PL1_R, .type = ARM_CP_CONST,
4958 .resetvalue = 0 },
4959 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4960 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4961 .access = PL1_R, .type = ARM_CP_CONST,
4962 .resetvalue = 0 },
4963 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4965 .access = PL1_R, .type = ARM_CP_CONST,
4966 .resetvalue = 0 },
4967 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4968 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4969 .access = PL1_R, .type = ARM_CP_CONST,
4970 .resetvalue = 0 },
4971 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4972 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4973 .access = PL1_R, .type = ARM_CP_CONST,
4974 .resetvalue = 0 },
4975 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4976 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4977 .access = PL1_R, .type = ARM_CP_CONST,
4978 .resetvalue = cpu->id_aa64dfr0 },
4979 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4980 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4981 .access = PL1_R, .type = ARM_CP_CONST,
4982 .resetvalue = cpu->id_aa64dfr1 },
4983 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4985 .access = PL1_R, .type = ARM_CP_CONST,
4986 .resetvalue = 0 },
4987 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4988 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4989 .access = PL1_R, .type = ARM_CP_CONST,
4990 .resetvalue = 0 },
4991 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4992 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4993 .access = PL1_R, .type = ARM_CP_CONST,
4994 .resetvalue = cpu->id_aa64afr0 },
4995 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4996 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4997 .access = PL1_R, .type = ARM_CP_CONST,
4998 .resetvalue = cpu->id_aa64afr1 },
4999 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5000 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
5001 .access = PL1_R, .type = ARM_CP_CONST,
5002 .resetvalue = 0 },
5003 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
5005 .access = PL1_R, .type = ARM_CP_CONST,
5006 .resetvalue = 0 },
5007 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
5008 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
5009 .access = PL1_R, .type = ARM_CP_CONST,
5010 .resetvalue = cpu->id_aa64isar0 },
5011 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
5012 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
5013 .access = PL1_R, .type = ARM_CP_CONST,
5014 .resetvalue = cpu->id_aa64isar1 },
5015 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5016 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
5017 .access = PL1_R, .type = ARM_CP_CONST,
5018 .resetvalue = 0 },
5019 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5020 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
5021 .access = PL1_R, .type = ARM_CP_CONST,
5022 .resetvalue = 0 },
5023 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
5025 .access = PL1_R, .type = ARM_CP_CONST,
5026 .resetvalue = 0 },
5027 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5028 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
5029 .access = PL1_R, .type = ARM_CP_CONST,
5030 .resetvalue = 0 },
5031 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5032 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
5033 .access = PL1_R, .type = ARM_CP_CONST,
5034 .resetvalue = 0 },
5035 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5036 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
5037 .access = PL1_R, .type = ARM_CP_CONST,
5038 .resetvalue = 0 },
5039 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
5040 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
5041 .access = PL1_R, .type = ARM_CP_CONST,
5042 .resetvalue = cpu->id_aa64mmfr0 },
5043 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
5044 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
5045 .access = PL1_R, .type = ARM_CP_CONST,
5046 .resetvalue = cpu->id_aa64mmfr1 },
5047 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5048 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
5049 .access = PL1_R, .type = ARM_CP_CONST,
5050 .resetvalue = 0 },
5051 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5052 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
5053 .access = PL1_R, .type = ARM_CP_CONST,
5054 .resetvalue = 0 },
5055 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5056 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
5057 .access = PL1_R, .type = ARM_CP_CONST,
5058 .resetvalue = 0 },
5059 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5060 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
5061 .access = PL1_R, .type = ARM_CP_CONST,
5062 .resetvalue = 0 },
5063 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5064 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
5065 .access = PL1_R, .type = ARM_CP_CONST,
5066 .resetvalue = 0 },
5067 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5068 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
5069 .access = PL1_R, .type = ARM_CP_CONST,
5070 .resetvalue = 0 },
5071 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
5072 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
5073 .access = PL1_R, .type = ARM_CP_CONST,
5074 .resetvalue = cpu->mvfr0 },
5075 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
5076 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
5077 .access = PL1_R, .type = ARM_CP_CONST,
5078 .resetvalue = cpu->mvfr1 },
5079 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
5080 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
5081 .access = PL1_R, .type = ARM_CP_CONST,
5082 .resetvalue = cpu->mvfr2 },
5083 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5084 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
5085 .access = PL1_R, .type = ARM_CP_CONST,
5086 .resetvalue = 0 },
5087 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5088 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
5089 .access = PL1_R, .type = ARM_CP_CONST,
5090 .resetvalue = 0 },
5091 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5092 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
5093 .access = PL1_R, .type = ARM_CP_CONST,
5094 .resetvalue = 0 },
5095 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5096 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
5097 .access = PL1_R, .type = ARM_CP_CONST,
5098 .resetvalue = 0 },
5099 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5100 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
5101 .access = PL1_R, .type = ARM_CP_CONST,
5102 .resetvalue = 0 },
5103 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
5104 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
5105 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5106 .resetvalue = cpu->pmceid0 },
5107 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
5108 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
5109 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5110 .resetvalue = cpu->pmceid0 },
5111 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
5112 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
5113 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5114 .resetvalue = cpu->pmceid1 },
5115 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
5116 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
5117 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5118 .resetvalue = cpu->pmceid1 },
5119 REGINFO_SENTINEL
5121 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
5122 if (!arm_feature(env, ARM_FEATURE_EL3) &&
5123 !arm_feature(env, ARM_FEATURE_EL2)) {
5124 ARMCPRegInfo rvbar = {
5125 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
5126 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5127 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
5129 define_one_arm_cp_reg(cpu, &rvbar);
5131 define_arm_cp_regs(cpu, v8_idregs);
5132 define_arm_cp_regs(cpu, v8_cp_reginfo);
5134 if (arm_feature(env, ARM_FEATURE_EL2)) {
5135 uint64_t vmpidr_def = mpidr_read_val(env);
5136 ARMCPRegInfo vpidr_regs[] = {
5137 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
5138 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5139 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5140 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
5141 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
5142 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
5143 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5144 .access = PL2_RW, .resetvalue = cpu->midr,
5145 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
5146 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
5147 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5148 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5149 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
5150 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
5151 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
5152 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5153 .access = PL2_RW,
5154 .resetvalue = vmpidr_def,
5155 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
5156 REGINFO_SENTINEL
5158 define_arm_cp_regs(cpu, vpidr_regs);
5159 define_arm_cp_regs(cpu, el2_cp_reginfo);
5160 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
5161 if (!arm_feature(env, ARM_FEATURE_EL3)) {
5162 ARMCPRegInfo rvbar = {
5163 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
5164 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
5165 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
5167 define_one_arm_cp_reg(cpu, &rvbar);
5169 } else {
5170 /* If EL2 is missing but higher ELs are enabled, we need to
5171 * register the no_el2 reginfos.
5173 if (arm_feature(env, ARM_FEATURE_EL3)) {
5174 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
5175 * of MIDR_EL1 and MPIDR_EL1.
5177 ARMCPRegInfo vpidr_regs[] = {
5178 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5179 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
5180 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5181 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
5182 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
5183 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5184 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
5185 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
5186 .type = ARM_CP_NO_RAW,
5187 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
5188 REGINFO_SENTINEL
5190 define_arm_cp_regs(cpu, vpidr_regs);
5191 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
5194 if (arm_feature(env, ARM_FEATURE_EL3)) {
5195 define_arm_cp_regs(cpu, el3_cp_reginfo);
5196 ARMCPRegInfo el3_regs[] = {
5197 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
5198 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
5199 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
5200 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
5201 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
5202 .access = PL3_RW,
5203 .raw_writefn = raw_write, .writefn = sctlr_write,
5204 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
5205 .resetvalue = cpu->reset_sctlr },
5206 REGINFO_SENTINEL
5209 define_arm_cp_regs(cpu, el3_regs);
5211 /* The behaviour of NSACR is sufficiently various that we don't
5212 * try to describe it in a single reginfo:
5213 * if EL3 is 64 bit, then trap to EL3 from S EL1,
5214 * reads as constant 0xc00 from NS EL1 and NS EL2
5215 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
5216 * if v7 without EL3, register doesn't exist
5217 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
5219 if (arm_feature(env, ARM_FEATURE_EL3)) {
5220 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5221 ARMCPRegInfo nsacr = {
5222 .name = "NSACR", .type = ARM_CP_CONST,
5223 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5224 .access = PL1_RW, .accessfn = nsacr_access,
5225 .resetvalue = 0xc00
5227 define_one_arm_cp_reg(cpu, &nsacr);
5228 } else {
5229 ARMCPRegInfo nsacr = {
5230 .name = "NSACR",
5231 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5232 .access = PL3_RW | PL1_R,
5233 .resetvalue = 0,
5234 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
5236 define_one_arm_cp_reg(cpu, &nsacr);
5238 } else {
5239 if (arm_feature(env, ARM_FEATURE_V8)) {
5240 ARMCPRegInfo nsacr = {
5241 .name = "NSACR", .type = ARM_CP_CONST,
5242 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
5243 .access = PL1_R,
5244 .resetvalue = 0xc00
5246 define_one_arm_cp_reg(cpu, &nsacr);
5250 if (arm_feature(env, ARM_FEATURE_PMSA)) {
5251 if (arm_feature(env, ARM_FEATURE_V6)) {
5252 /* PMSAv6 not implemented */
5253 assert(arm_feature(env, ARM_FEATURE_V7));
5254 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5255 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
5256 } else {
5257 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
5259 } else {
5260 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
5261 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
5263 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
5264 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
5266 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
5267 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
5269 if (arm_feature(env, ARM_FEATURE_VAPA)) {
5270 define_arm_cp_regs(cpu, vapa_cp_reginfo);
5272 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
5273 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
5275 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
5276 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
5278 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
5279 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
5281 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
5282 define_arm_cp_regs(cpu, omap_cp_reginfo);
5284 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
5285 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
5287 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5288 define_arm_cp_regs(cpu, xscale_cp_reginfo);
5290 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
5291 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
5293 if (arm_feature(env, ARM_FEATURE_LPAE)) {
5294 define_arm_cp_regs(cpu, lpae_cp_reginfo);
5296 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
5297 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
5298 * be read-only (ie write causes UNDEF exception).
5301 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
5302 /* Pre-v8 MIDR space.
5303 * Note that the MIDR isn't a simple constant register because
5304 * of the TI925 behaviour where writes to another register can
5305 * cause the MIDR value to change.
5307 * Unimplemented registers in the c15 0 0 0 space default to
5308 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
5309 * and friends override accordingly.
5311 { .name = "MIDR",
5312 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
5313 .access = PL1_R, .resetvalue = cpu->midr,
5314 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
5315 .readfn = midr_read,
5316 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5317 .type = ARM_CP_OVERRIDE },
5318 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
5319 { .name = "DUMMY",
5320 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
5321 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5322 { .name = "DUMMY",
5323 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
5324 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5325 { .name = "DUMMY",
5326 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
5327 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5328 { .name = "DUMMY",
5329 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
5330 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5331 { .name = "DUMMY",
5332 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
5333 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5334 REGINFO_SENTINEL
5336 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
5337 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
5338 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
5339 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
5340 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
5341 .readfn = midr_read },
5342 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
5343 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5344 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5345 .access = PL1_R, .resetvalue = cpu->midr },
5346 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
5347 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
5348 .access = PL1_R, .resetvalue = cpu->midr },
5349 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
5350 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
5351 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
5352 REGINFO_SENTINEL
5354 ARMCPRegInfo id_cp_reginfo[] = {
5355 /* These are common to v8 and pre-v8 */
5356 { .name = "CTR",
5357 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
5358 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5359 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
5360 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
5361 .access = PL0_R, .accessfn = ctr_el0_access,
5362 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
5363 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
5364 { .name = "TCMTR",
5365 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
5366 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
5367 REGINFO_SENTINEL
5369 /* TLBTR is specific to VMSA */
5370 ARMCPRegInfo id_tlbtr_reginfo = {
5371 .name = "TLBTR",
5372 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
5373 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
5375 /* MPUIR is specific to PMSA V6+ */
5376 ARMCPRegInfo id_mpuir_reginfo = {
5377 .name = "MPUIR",
5378 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
5379 .access = PL1_R, .type = ARM_CP_CONST,
5380 .resetvalue = cpu->pmsav7_dregion << 8
5382 ARMCPRegInfo crn0_wi_reginfo = {
5383 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
5384 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
5385 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
5387 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
5388 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5389 ARMCPRegInfo *r;
5390 /* Register the blanket "writes ignored" value first to cover the
5391 * whole space. Then update the specific ID registers to allow write
5392 * access, so that they ignore writes rather than causing them to
5393 * UNDEF.
5395 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5396 for (r = id_pre_v8_midr_cp_reginfo;
5397 r->type != ARM_CP_SENTINEL; r++) {
5398 r->access = PL1_RW;
5400 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
5401 r->access = PL1_RW;
5403 id_mpuir_reginfo.access = PL1_RW;
5404 id_tlbtr_reginfo.access = PL1_RW;
5406 if (arm_feature(env, ARM_FEATURE_V8)) {
5407 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
5408 } else {
5409 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
5411 define_arm_cp_regs(cpu, id_cp_reginfo);
5412 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
5413 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
5414 } else if (arm_feature(env, ARM_FEATURE_V7)) {
5415 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
5419 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
5420 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
5423 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
5424 ARMCPRegInfo auxcr_reginfo[] = {
5425 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
5426 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
5427 .access = PL1_RW, .type = ARM_CP_CONST,
5428 .resetvalue = cpu->reset_auxcr },
5429 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
5430 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
5431 .access = PL2_RW, .type = ARM_CP_CONST,
5432 .resetvalue = 0 },
5433 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
5434 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
5435 .access = PL3_RW, .type = ARM_CP_CONST,
5436 .resetvalue = 0 },
5437 REGINFO_SENTINEL
5439 define_arm_cp_regs(cpu, auxcr_reginfo);
5442 if (arm_feature(env, ARM_FEATURE_CBAR)) {
5443 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5444 /* 32 bit view is [31:18] 0...0 [43:32]. */
5445 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
5446 | extract64(cpu->reset_cbar, 32, 12);
5447 ARMCPRegInfo cbar_reginfo[] = {
5448 { .name = "CBAR",
5449 .type = ARM_CP_CONST,
5450 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5451 .access = PL1_R, .resetvalue = cpu->reset_cbar },
5452 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
5453 .type = ARM_CP_CONST,
5454 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
5455 .access = PL1_R, .resetvalue = cbar32 },
5456 REGINFO_SENTINEL
5458 /* We don't implement a r/w 64 bit CBAR currently */
5459 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
5460 define_arm_cp_regs(cpu, cbar_reginfo);
5461 } else {
5462 ARMCPRegInfo cbar = {
5463 .name = "CBAR",
5464 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
5465 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
5466 .fieldoffset = offsetof(CPUARMState,
5467 cp15.c15_config_base_address)
5469 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
5470 cbar.access = PL1_R;
5471 cbar.fieldoffset = 0;
5472 cbar.type = ARM_CP_CONST;
5474 define_one_arm_cp_reg(cpu, &cbar);
5478 if (arm_feature(env, ARM_FEATURE_VBAR)) {
5479 ARMCPRegInfo vbar_cp_reginfo[] = {
5480 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
5481 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
5482 .access = PL1_RW, .writefn = vbar_write,
5483 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
5484 offsetof(CPUARMState, cp15.vbar_ns) },
5485 .resetvalue = 0 },
5486 REGINFO_SENTINEL
5488 define_arm_cp_regs(cpu, vbar_cp_reginfo);
5491 /* Generic registers whose values depend on the implementation */
5493 ARMCPRegInfo sctlr = {
5494 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
5495 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5496 .access = PL1_RW,
5497 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
5498 offsetof(CPUARMState, cp15.sctlr_ns) },
5499 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
5500 .raw_writefn = raw_write,
5502 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5503 /* Normally we would always end the TB on an SCTLR write, but Linux
5504 * arch/arm/mach-pxa/sleep.S expects two instructions following
5505 * an MMU enable to execute from cache. Imitate this behaviour.
5507 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
5509 define_one_arm_cp_reg(cpu, &sctlr);
5512 if (arm_feature(env, ARM_FEATURE_SVE)) {
5513 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
5514 if (arm_feature(env, ARM_FEATURE_EL2)) {
5515 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
5516 } else {
5517 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
5519 if (arm_feature(env, ARM_FEATURE_EL3)) {
5520 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
5525 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
5527 CPUState *cs = CPU(cpu);
5528 CPUARMState *env = &cpu->env;
5530 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5531 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
5532 aarch64_fpu_gdb_set_reg,
5533 34, "aarch64-fpu.xml", 0);
5534 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
5535 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5536 51, "arm-neon.xml", 0);
5537 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
5538 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5539 35, "arm-vfp3.xml", 0);
5540 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
5541 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
5542 19, "arm-vfp.xml", 0);
5544 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
5545 arm_gen_dynamic_xml(cs),
5546 "system-registers.xml", 0);
5549 /* Sort alphabetically by type name, except for "any". */
5550 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5552 ObjectClass *class_a = (ObjectClass *)a;
5553 ObjectClass *class_b = (ObjectClass *)b;
5554 const char *name_a, *name_b;
5556 name_a = object_class_get_name(class_a);
5557 name_b = object_class_get_name(class_b);
5558 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
5559 return 1;
5560 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
5561 return -1;
5562 } else {
5563 return strcmp(name_a, name_b);
5567 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
5569 ObjectClass *oc = data;
5570 CPUListState *s = user_data;
5571 const char *typename;
5572 char *name;
5574 typename = object_class_get_name(oc);
5575 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
5576 (*s->cpu_fprintf)(s->file, " %s\n",
5577 name);
5578 g_free(name);
5581 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5583 CPUListState s = {
5584 .file = f,
5585 .cpu_fprintf = cpu_fprintf,
5587 GSList *list;
5589 list = object_class_get_list(TYPE_ARM_CPU, false);
5590 list = g_slist_sort(list, arm_cpu_list_compare);
5591 (*cpu_fprintf)(f, "Available CPUs:\n");
5592 g_slist_foreach(list, arm_cpu_list_entry, &s);
5593 g_slist_free(list);
5596 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5598 ObjectClass *oc = data;
5599 CpuDefinitionInfoList **cpu_list = user_data;
5600 CpuDefinitionInfoList *entry;
5601 CpuDefinitionInfo *info;
5602 const char *typename;
5604 typename = object_class_get_name(oc);
5605 info = g_malloc0(sizeof(*info));
5606 info->name = g_strndup(typename,
5607 strlen(typename) - strlen("-" TYPE_ARM_CPU));
5608 info->q_typename = g_strdup(typename);
5610 entry = g_malloc0(sizeof(*entry));
5611 entry->value = info;
5612 entry->next = *cpu_list;
5613 *cpu_list = entry;
5616 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5618 CpuDefinitionInfoList *cpu_list = NULL;
5619 GSList *list;
5621 list = object_class_get_list(TYPE_ARM_CPU, false);
5622 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5623 g_slist_free(list);
5625 return cpu_list;
5628 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
5629 void *opaque, int state, int secstate,
5630 int crm, int opc1, int opc2,
5631 const char *name)
5633 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5634 * add a single reginfo struct to the hash table.
5636 uint32_t *key = g_new(uint32_t, 1);
5637 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5638 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
5639 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5641 r2->name = g_strdup(name);
5642 /* Reset the secure state to the specific incoming state. This is
5643 * necessary as the register may have been defined with both states.
5645 r2->secure = secstate;
5647 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5648 /* Register is banked (using both entries in array).
5649 * Overwriting fieldoffset as the array is only used to define
5650 * banked registers but later only fieldoffset is used.
5652 r2->fieldoffset = r->bank_fieldoffsets[ns];
5655 if (state == ARM_CP_STATE_AA32) {
5656 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5657 /* If the register is banked then we don't need to migrate or
5658 * reset the 32-bit instance in certain cases:
5660 * 1) If the register has both 32-bit and 64-bit instances then we
5661 * can count on the 64-bit instance taking care of the
5662 * non-secure bank.
5663 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5664 * taking care of the secure bank. This requires that separate
5665 * 32 and 64-bit definitions are provided.
5667 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5668 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
5669 r2->type |= ARM_CP_ALIAS;
5671 } else if ((secstate != r->secure) && !ns) {
5672 /* The register is not banked so we only want to allow migration of
5673 * the non-secure instance.
5675 r2->type |= ARM_CP_ALIAS;
5678 if (r->state == ARM_CP_STATE_BOTH) {
5679 /* We assume it is a cp15 register if the .cp field is left unset.
5681 if (r2->cp == 0) {
5682 r2->cp = 15;
5685 #ifdef HOST_WORDS_BIGENDIAN
5686 if (r2->fieldoffset) {
5687 r2->fieldoffset += sizeof(uint32_t);
5689 #endif
5692 if (state == ARM_CP_STATE_AA64) {
5693 /* To allow abbreviation of ARMCPRegInfo
5694 * definitions, we treat cp == 0 as equivalent to
5695 * the value for "standard guest-visible sysreg".
5696 * STATE_BOTH definitions are also always "standard
5697 * sysreg" in their AArch64 view (the .cp value may
5698 * be non-zero for the benefit of the AArch32 view).
5700 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
5701 r2->cp = CP_REG_ARM64_SYSREG_CP;
5703 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5704 r2->opc0, opc1, opc2);
5705 } else {
5706 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
5708 if (opaque) {
5709 r2->opaque = opaque;
5711 /* reginfo passed to helpers is correct for the actual access,
5712 * and is never ARM_CP_STATE_BOTH:
5714 r2->state = state;
5715 /* Make sure reginfo passed to helpers for wildcarded regs
5716 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5718 r2->crm = crm;
5719 r2->opc1 = opc1;
5720 r2->opc2 = opc2;
5721 /* By convention, for wildcarded registers only the first
5722 * entry is used for migration; the others are marked as
5723 * ALIAS so we don't try to transfer the register
5724 * multiple times. Special registers (ie NOP/WFI) are
5725 * never migratable and not even raw-accessible.
5727 if ((r->type & ARM_CP_SPECIAL)) {
5728 r2->type |= ARM_CP_NO_RAW;
5730 if (((r->crm == CP_ANY) && crm != 0) ||
5731 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5732 ((r->opc2 == CP_ANY) && opc2 != 0)) {
5733 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
5736 /* Check that raw accesses are either forbidden or handled. Note that
5737 * we can't assert this earlier because the setup of fieldoffset for
5738 * banked registers has to be done first.
5740 if (!(r2->type & ARM_CP_NO_RAW)) {
5741 assert(!raw_accessors_invalid(r2));
5744 /* Overriding of an existing definition must be explicitly
5745 * requested.
5747 if (!(r->type & ARM_CP_OVERRIDE)) {
5748 ARMCPRegInfo *oldreg;
5749 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5750 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5751 fprintf(stderr, "Register redefined: cp=%d %d bit "
5752 "crn=%d crm=%d opc1=%d opc2=%d, "
5753 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5754 r2->crn, r2->crm, r2->opc1, r2->opc2,
5755 oldreg->name, r2->name);
5756 g_assert_not_reached();
5759 g_hash_table_insert(cpu->cp_regs, key, r2);
5763 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5764 const ARMCPRegInfo *r, void *opaque)
5766 /* Define implementations of coprocessor registers.
5767 * We store these in a hashtable because typically
5768 * there are less than 150 registers in a space which
5769 * is 16*16*16*8*8 = 262144 in size.
5770 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5771 * If a register is defined twice then the second definition is
5772 * used, so this can be used to define some generic registers and
5773 * then override them with implementation specific variations.
5774 * At least one of the original and the second definition should
5775 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5776 * against accidental use.
5778 * The state field defines whether the register is to be
5779 * visible in the AArch32 or AArch64 execution state. If the
5780 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5781 * reginfo structure for the AArch32 view, which sees the lower
5782 * 32 bits of the 64 bit register.
5784 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5785 * be wildcarded. AArch64 registers are always considered to be 64
5786 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5787 * the register, if any.
5789 int crm, opc1, opc2, state;
5790 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5791 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5792 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5793 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5794 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5795 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5796 /* 64 bit registers have only CRm and Opc1 fields */
5797 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
5798 /* op0 only exists in the AArch64 encodings */
5799 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5800 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5801 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5802 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5803 * encodes a minimum access level for the register. We roll this
5804 * runtime check into our general permission check code, so check
5805 * here that the reginfo's specified permissions are strict enough
5806 * to encompass the generic architectural permission check.
5808 if (r->state != ARM_CP_STATE_AA32) {
5809 int mask = 0;
5810 switch (r->opc1) {
5811 case 0: case 1: case 2:
5812 /* min_EL EL1 */
5813 mask = PL1_RW;
5814 break;
5815 case 3:
5816 /* min_EL EL0 */
5817 mask = PL0_RW;
5818 break;
5819 case 4:
5820 /* min_EL EL2 */
5821 mask = PL2_RW;
5822 break;
5823 case 5:
5824 /* unallocated encoding, so not possible */
5825 assert(false);
5826 break;
5827 case 6:
5828 /* min_EL EL3 */
5829 mask = PL3_RW;
5830 break;
5831 case 7:
5832 /* min_EL EL1, secure mode only (we don't check the latter) */
5833 mask = PL1_RW;
5834 break;
5835 default:
5836 /* broken reginfo with out-of-range opc1 */
5837 assert(false);
5838 break;
5840 /* assert our permissions are not too lax (stricter is fine) */
5841 assert((r->access & ~mask) == 0);
5844 /* Check that the register definition has enough info to handle
5845 * reads and writes if they are permitted.
5847 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5848 if (r->access & PL3_R) {
5849 assert((r->fieldoffset ||
5850 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5851 r->readfn);
5853 if (r->access & PL3_W) {
5854 assert((r->fieldoffset ||
5855 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5856 r->writefn);
5859 /* Bad type field probably means missing sentinel at end of reg list */
5860 assert(cptype_valid(r->type));
5861 for (crm = crmmin; crm <= crmmax; crm++) {
5862 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5863 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
5864 for (state = ARM_CP_STATE_AA32;
5865 state <= ARM_CP_STATE_AA64; state++) {
5866 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5867 continue;
5869 if (state == ARM_CP_STATE_AA32) {
5870 /* Under AArch32 CP registers can be common
5871 * (same for secure and non-secure world) or banked.
5873 char *name;
5875 switch (r->secure) {
5876 case ARM_CP_SECSTATE_S:
5877 case ARM_CP_SECSTATE_NS:
5878 add_cpreg_to_hashtable(cpu, r, opaque, state,
5879 r->secure, crm, opc1, opc2,
5880 r->name);
5881 break;
5882 default:
5883 name = g_strdup_printf("%s_S", r->name);
5884 add_cpreg_to_hashtable(cpu, r, opaque, state,
5885 ARM_CP_SECSTATE_S,
5886 crm, opc1, opc2, name);
5887 g_free(name);
5888 add_cpreg_to_hashtable(cpu, r, opaque, state,
5889 ARM_CP_SECSTATE_NS,
5890 crm, opc1, opc2, r->name);
5891 break;
5893 } else {
5894 /* AArch64 registers get mapped to non-secure instance
5895 * of AArch32 */
5896 add_cpreg_to_hashtable(cpu, r, opaque, state,
5897 ARM_CP_SECSTATE_NS,
5898 crm, opc1, opc2, r->name);
5906 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5907 const ARMCPRegInfo *regs, void *opaque)
5909 /* Define a whole list of registers */
5910 const ARMCPRegInfo *r;
5911 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5912 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5916 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
5918 return g_hash_table_lookup(cpregs, &encoded_cp);
5921 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5922 uint64_t value)
5924 /* Helper coprocessor write function for write-ignore registers */
5927 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
5929 /* Helper coprocessor write function for read-as-zero registers */
5930 return 0;
5933 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5935 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5938 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
5940 /* Return true if it is not valid for us to switch to
5941 * this CPU mode (ie all the UNPREDICTABLE cases in
5942 * the ARM ARM CPSRWriteByInstr pseudocode).
5945 /* Changes to or from Hyp via MSR and CPS are illegal. */
5946 if (write_type == CPSRWriteByInstr &&
5947 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5948 mode == ARM_CPU_MODE_HYP)) {
5949 return 1;
5952 switch (mode) {
5953 case ARM_CPU_MODE_USR:
5954 return 0;
5955 case ARM_CPU_MODE_SYS:
5956 case ARM_CPU_MODE_SVC:
5957 case ARM_CPU_MODE_ABT:
5958 case ARM_CPU_MODE_UND:
5959 case ARM_CPU_MODE_IRQ:
5960 case ARM_CPU_MODE_FIQ:
5961 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5962 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5964 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5965 * and CPS are treated as illegal mode changes.
5967 if (write_type == CPSRWriteByInstr &&
5968 (env->cp15.hcr_el2 & HCR_TGE) &&
5969 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5970 !arm_is_secure_below_el3(env)) {
5971 return 1;
5973 return 0;
5974 case ARM_CPU_MODE_HYP:
5975 return !arm_feature(env, ARM_FEATURE_EL2)
5976 || arm_current_el(env) < 2 || arm_is_secure(env);
5977 case ARM_CPU_MODE_MON:
5978 return arm_current_el(env) < 3;
5979 default:
5980 return 1;
5984 uint32_t cpsr_read(CPUARMState *env)
5986 int ZF;
5987 ZF = (env->ZF == 0);
5988 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
5989 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5990 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5991 | ((env->condexec_bits & 0xfc) << 8)
5992 | (env->GE << 16) | (env->daif & CPSR_AIF);
5995 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5996 CPSRWriteType write_type)
5998 uint32_t changed_daif;
6000 if (mask & CPSR_NZCV) {
6001 env->ZF = (~val) & CPSR_Z;
6002 env->NF = val;
6003 env->CF = (val >> 29) & 1;
6004 env->VF = (val << 3) & 0x80000000;
6006 if (mask & CPSR_Q)
6007 env->QF = ((val & CPSR_Q) != 0);
6008 if (mask & CPSR_T)
6009 env->thumb = ((val & CPSR_T) != 0);
6010 if (mask & CPSR_IT_0_1) {
6011 env->condexec_bits &= ~3;
6012 env->condexec_bits |= (val >> 25) & 3;
6014 if (mask & CPSR_IT_2_7) {
6015 env->condexec_bits &= 3;
6016 env->condexec_bits |= (val >> 8) & 0xfc;
6018 if (mask & CPSR_GE) {
6019 env->GE = (val >> 16) & 0xf;
6022 /* In a V7 implementation that includes the security extensions but does
6023 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
6024 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
6025 * bits respectively.
6027 * In a V8 implementation, it is permitted for privileged software to
6028 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
6030 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6031 arm_feature(env, ARM_FEATURE_EL3) &&
6032 !arm_feature(env, ARM_FEATURE_EL2) &&
6033 !arm_is_secure(env)) {
6035 changed_daif = (env->daif ^ val) & mask;
6037 if (changed_daif & CPSR_A) {
6038 /* Check to see if we are allowed to change the masking of async
6039 * abort exceptions from a non-secure state.
6041 if (!(env->cp15.scr_el3 & SCR_AW)) {
6042 qemu_log_mask(LOG_GUEST_ERROR,
6043 "Ignoring attempt to switch CPSR_A flag from "
6044 "non-secure world with SCR.AW bit clear\n");
6045 mask &= ~CPSR_A;
6049 if (changed_daif & CPSR_F) {
6050 /* Check to see if we are allowed to change the masking of FIQ
6051 * exceptions from a non-secure state.
6053 if (!(env->cp15.scr_el3 & SCR_FW)) {
6054 qemu_log_mask(LOG_GUEST_ERROR,
6055 "Ignoring attempt to switch CPSR_F flag from "
6056 "non-secure world with SCR.FW bit clear\n");
6057 mask &= ~CPSR_F;
6060 /* Check whether non-maskable FIQ (NMFI) support is enabled.
6061 * If this bit is set software is not allowed to mask
6062 * FIQs, but is allowed to set CPSR_F to 0.
6064 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
6065 (val & CPSR_F)) {
6066 qemu_log_mask(LOG_GUEST_ERROR,
6067 "Ignoring attempt to enable CPSR_F flag "
6068 "(non-maskable FIQ [NMFI] support enabled)\n");
6069 mask &= ~CPSR_F;
6074 env->daif &= ~(CPSR_AIF & mask);
6075 env->daif |= val & CPSR_AIF & mask;
6077 if (write_type != CPSRWriteRaw &&
6078 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
6079 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
6080 /* Note that we can only get here in USR mode if this is a
6081 * gdb stub write; for this case we follow the architectural
6082 * behaviour for guest writes in USR mode of ignoring an attempt
6083 * to switch mode. (Those are caught by translate.c for writes
6084 * triggered by guest instructions.)
6086 mask &= ~CPSR_M;
6087 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
6088 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
6089 * v7, and has defined behaviour in v8:
6090 * + leave CPSR.M untouched
6091 * + allow changes to the other CPSR fields
6092 * + set PSTATE.IL
6093 * For user changes via the GDB stub, we don't set PSTATE.IL,
6094 * as this would be unnecessarily harsh for a user error.
6096 mask &= ~CPSR_M;
6097 if (write_type != CPSRWriteByGDBStub &&
6098 arm_feature(env, ARM_FEATURE_V8)) {
6099 mask |= CPSR_IL;
6100 val |= CPSR_IL;
6102 } else {
6103 switch_mode(env, val & CPSR_M);
6106 mask &= ~CACHED_CPSR_BITS;
6107 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
6110 /* Sign/zero extend */
6111 uint32_t HELPER(sxtb16)(uint32_t x)
6113 uint32_t res;
6114 res = (uint16_t)(int8_t)x;
6115 res |= (uint32_t)(int8_t)(x >> 16) << 16;
6116 return res;
6119 uint32_t HELPER(uxtb16)(uint32_t x)
6121 uint32_t res;
6122 res = (uint16_t)(uint8_t)x;
6123 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
6124 return res;
6127 int32_t HELPER(sdiv)(int32_t num, int32_t den)
6129 if (den == 0)
6130 return 0;
6131 if (num == INT_MIN && den == -1)
6132 return INT_MIN;
6133 return num / den;
6136 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
6138 if (den == 0)
6139 return 0;
6140 return num / den;
6143 uint32_t HELPER(rbit)(uint32_t x)
6145 return revbit32(x);
6148 #if defined(CONFIG_USER_ONLY)
6150 /* These should probably raise undefined insn exceptions. */
6151 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
6153 ARMCPU *cpu = arm_env_get_cpu(env);
6155 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
6158 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
6160 ARMCPU *cpu = arm_env_get_cpu(env);
6162 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
6163 return 0;
6166 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6168 /* translate.c should never generate calls here in user-only mode */
6169 g_assert_not_reached();
6172 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6174 /* translate.c should never generate calls here in user-only mode */
6175 g_assert_not_reached();
6178 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
6180 /* The TT instructions can be used by unprivileged code, but in
6181 * user-only emulation we don't have the MPU.
6182 * Luckily since we know we are NonSecure unprivileged (and that in
6183 * turn means that the A flag wasn't specified), all the bits in the
6184 * register must be zero:
6185 * IREGION: 0 because IRVALID is 0
6186 * IRVALID: 0 because NS
6187 * S: 0 because NS
6188 * NSRW: 0 because NS
6189 * NSR: 0 because NS
6190 * RW: 0 because unpriv and A flag not set
6191 * R: 0 because unpriv and A flag not set
6192 * SRVALID: 0 because NS
6193 * MRVALID: 0 because unpriv and A flag not set
6194 * SREGION: 0 becaus SRVALID is 0
6195 * MREGION: 0 because MRVALID is 0
6197 return 0;
6200 void switch_mode(CPUARMState *env, int mode)
6202 ARMCPU *cpu = arm_env_get_cpu(env);
6204 if (mode != ARM_CPU_MODE_USR) {
6205 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
6209 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6210 uint32_t cur_el, bool secure)
6212 return 1;
6215 void aarch64_sync_64_to_32(CPUARMState *env)
6217 g_assert_not_reached();
6220 #else
6222 void switch_mode(CPUARMState *env, int mode)
6224 int old_mode;
6225 int i;
6227 old_mode = env->uncached_cpsr & CPSR_M;
6228 if (mode == old_mode)
6229 return;
6231 if (old_mode == ARM_CPU_MODE_FIQ) {
6232 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
6233 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
6234 } else if (mode == ARM_CPU_MODE_FIQ) {
6235 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
6236 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
6239 i = bank_number(old_mode);
6240 env->banked_r13[i] = env->regs[13];
6241 env->banked_r14[i] = env->regs[14];
6242 env->banked_spsr[i] = env->spsr;
6244 i = bank_number(mode);
6245 env->regs[13] = env->banked_r13[i];
6246 env->regs[14] = env->banked_r14[i];
6247 env->spsr = env->banked_spsr[i];
6250 /* Physical Interrupt Target EL Lookup Table
6252 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
6254 * The below multi-dimensional table is used for looking up the target
6255 * exception level given numerous condition criteria. Specifically, the
6256 * target EL is based on SCR and HCR routing controls as well as the
6257 * currently executing EL and secure state.
6259 * Dimensions:
6260 * target_el_table[2][2][2][2][2][4]
6261 * | | | | | +--- Current EL
6262 * | | | | +------ Non-secure(0)/Secure(1)
6263 * | | | +--------- HCR mask override
6264 * | | +------------ SCR exec state control
6265 * | +--------------- SCR mask override
6266 * +------------------ 32-bit(0)/64-bit(1) EL3
6268 * The table values are as such:
6269 * 0-3 = EL0-EL3
6270 * -1 = Cannot occur
6272 * The ARM ARM target EL table includes entries indicating that an "exception
6273 * is not taken". The two cases where this is applicable are:
6274 * 1) An exception is taken from EL3 but the SCR does not have the exception
6275 * routed to EL3.
6276 * 2) An exception is taken from EL2 but the HCR does not have the exception
6277 * routed to EL2.
6278 * In these two cases, the below table contain a target of EL1. This value is
6279 * returned as it is expected that the consumer of the table data will check
6280 * for "target EL >= current EL" to ensure the exception is not taken.
6282 * SCR HCR
6283 * 64 EA AMO From
6284 * BIT IRQ IMO Non-secure Secure
6285 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
6287 static const int8_t target_el_table[2][2][2][2][2][4] = {
6288 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6289 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
6290 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
6291 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
6292 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6293 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
6294 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
6295 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
6296 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6297 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
6298 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
6299 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
6300 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6301 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6302 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
6303 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
6307 * Determine the target EL for physical exceptions
6309 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
6310 uint32_t cur_el, bool secure)
6312 CPUARMState *env = cs->env_ptr;
6313 int rw;
6314 int scr;
6315 int hcr;
6316 int target_el;
6317 /* Is the highest EL AArch64? */
6318 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
6320 if (arm_feature(env, ARM_FEATURE_EL3)) {
6321 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
6322 } else {
6323 /* Either EL2 is the highest EL (and so the EL2 register width
6324 * is given by is64); or there is no EL2 or EL3, in which case
6325 * the value of 'rw' does not affect the table lookup anyway.
6327 rw = is64;
6330 switch (excp_idx) {
6331 case EXCP_IRQ:
6332 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
6333 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
6334 break;
6335 case EXCP_FIQ:
6336 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
6337 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
6338 break;
6339 default:
6340 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
6341 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
6342 break;
6345 /* If HCR.TGE is set then HCR is treated as being 1 */
6346 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
6348 /* Perform a table-lookup for the target EL given the current state */
6349 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
6351 assert(target_el > 0);
6353 return target_el;
6356 static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value,
6357 ARMMMUIdx mmu_idx, bool ignfault)
6359 CPUState *cs = CPU(cpu);
6360 CPUARMState *env = &cpu->env;
6361 MemTxAttrs attrs = {};
6362 MemTxResult txres;
6363 target_ulong page_size;
6364 hwaddr physaddr;
6365 int prot;
6366 ARMMMUFaultInfo fi;
6367 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6368 int exc;
6369 bool exc_secure;
6371 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr,
6372 &attrs, &prot, &page_size, &fi, NULL)) {
6373 /* MPU/SAU lookup failed */
6374 if (fi.type == ARMFault_QEMU_SFault) {
6375 qemu_log_mask(CPU_LOG_INT,
6376 "...SecureFault with SFSR.AUVIOL during stacking\n");
6377 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6378 env->v7m.sfar = addr;
6379 exc = ARMV7M_EXCP_SECURE;
6380 exc_secure = false;
6381 } else {
6382 qemu_log_mask(CPU_LOG_INT, "...MemManageFault with CFSR.MSTKERR\n");
6383 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK;
6384 exc = ARMV7M_EXCP_MEM;
6385 exc_secure = secure;
6387 goto pend_fault;
6389 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value,
6390 attrs, &txres);
6391 if (txres != MEMTX_OK) {
6392 /* BusFault trying to write the data */
6393 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n");
6394 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK;
6395 exc = ARMV7M_EXCP_BUS;
6396 exc_secure = false;
6397 goto pend_fault;
6399 return true;
6401 pend_fault:
6402 /* By pending the exception at this point we are making
6403 * the IMPDEF choice "overridden exceptions pended" (see the
6404 * MergeExcInfo() pseudocode). The other choice would be to not
6405 * pend them now and then make a choice about which to throw away
6406 * later if we have two derived exceptions.
6407 * The only case when we must not pend the exception but instead
6408 * throw it away is if we are doing the push of the callee registers
6409 * and we've already generated a derived exception. Even in this
6410 * case we will still update the fault status registers.
6412 if (!ignfault) {
6413 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure);
6415 return false;
6418 static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr,
6419 ARMMMUIdx mmu_idx)
6421 CPUState *cs = CPU(cpu);
6422 CPUARMState *env = &cpu->env;
6423 MemTxAttrs attrs = {};
6424 MemTxResult txres;
6425 target_ulong page_size;
6426 hwaddr physaddr;
6427 int prot;
6428 ARMMMUFaultInfo fi;
6429 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
6430 int exc;
6431 bool exc_secure;
6432 uint32_t value;
6434 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr,
6435 &attrs, &prot, &page_size, &fi, NULL)) {
6436 /* MPU/SAU lookup failed */
6437 if (fi.type == ARMFault_QEMU_SFault) {
6438 qemu_log_mask(CPU_LOG_INT,
6439 "...SecureFault with SFSR.AUVIOL during unstack\n");
6440 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
6441 env->v7m.sfar = addr;
6442 exc = ARMV7M_EXCP_SECURE;
6443 exc_secure = false;
6444 } else {
6445 qemu_log_mask(CPU_LOG_INT,
6446 "...MemManageFault with CFSR.MUNSTKERR\n");
6447 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK;
6448 exc = ARMV7M_EXCP_MEM;
6449 exc_secure = secure;
6451 goto pend_fault;
6454 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr,
6455 attrs, &txres);
6456 if (txres != MEMTX_OK) {
6457 /* BusFault trying to read the data */
6458 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n");
6459 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK;
6460 exc = ARMV7M_EXCP_BUS;
6461 exc_secure = false;
6462 goto pend_fault;
6465 *dest = value;
6466 return true;
6468 pend_fault:
6469 /* By pending the exception at this point we are making
6470 * the IMPDEF choice "overridden exceptions pended" (see the
6471 * MergeExcInfo() pseudocode). The other choice would be to not
6472 * pend them now and then make a choice about which to throw away
6473 * later if we have two derived exceptions.
6475 armv7m_nvic_set_pending(env->nvic, exc, exc_secure);
6476 return false;
6479 /* Return true if we're using the process stack pointer (not the MSP) */
6480 static bool v7m_using_psp(CPUARMState *env)
6482 /* Handler mode always uses the main stack; for thread mode
6483 * the CONTROL.SPSEL bit determines the answer.
6484 * Note that in v7M it is not possible to be in Handler mode with
6485 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both.
6487 return !arm_v7m_is_handler_mode(env) &&
6488 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK;
6491 /* Write to v7M CONTROL.SPSEL bit for the specified security bank.
6492 * This may change the current stack pointer between Main and Process
6493 * stack pointers if it is done for the CONTROL register for the current
6494 * security state.
6496 static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
6497 bool new_spsel,
6498 bool secstate)
6500 bool old_is_psp = v7m_using_psp(env);
6502 env->v7m.control[secstate] =
6503 deposit32(env->v7m.control[secstate],
6504 R_V7M_CONTROL_SPSEL_SHIFT,
6505 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
6507 if (secstate == env->v7m.secure) {
6508 bool new_is_psp = v7m_using_psp(env);
6509 uint32_t tmp;
6511 if (old_is_psp != new_is_psp) {
6512 tmp = env->v7m.other_sp;
6513 env->v7m.other_sp = env->regs[13];
6514 env->regs[13] = tmp;
6519 /* Write to v7M CONTROL.SPSEL bit. This may change the current
6520 * stack pointer between Main and Process stack pointers.
6522 static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
6524 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
6527 void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
6529 /* Write a new value to v7m.exception, thus transitioning into or out
6530 * of Handler mode; this may result in a change of active stack pointer.
6532 bool new_is_psp, old_is_psp = v7m_using_psp(env);
6533 uint32_t tmp;
6535 env->v7m.exception = new_exc;
6537 new_is_psp = v7m_using_psp(env);
6539 if (old_is_psp != new_is_psp) {
6540 tmp = env->v7m.other_sp;
6541 env->v7m.other_sp = env->regs[13];
6542 env->regs[13] = tmp;
6546 /* Switch M profile security state between NS and S */
6547 static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
6549 uint32_t new_ss_msp, new_ss_psp;
6551 if (env->v7m.secure == new_secstate) {
6552 return;
6555 /* All the banked state is accessed by looking at env->v7m.secure
6556 * except for the stack pointer; rearrange the SP appropriately.
6558 new_ss_msp = env->v7m.other_ss_msp;
6559 new_ss_psp = env->v7m.other_ss_psp;
6561 if (v7m_using_psp(env)) {
6562 env->v7m.other_ss_psp = env->regs[13];
6563 env->v7m.other_ss_msp = env->v7m.other_sp;
6564 } else {
6565 env->v7m.other_ss_msp = env->regs[13];
6566 env->v7m.other_ss_psp = env->v7m.other_sp;
6569 env->v7m.secure = new_secstate;
6571 if (v7m_using_psp(env)) {
6572 env->regs[13] = new_ss_psp;
6573 env->v7m.other_sp = new_ss_msp;
6574 } else {
6575 env->regs[13] = new_ss_msp;
6576 env->v7m.other_sp = new_ss_psp;
6580 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
6582 /* Handle v7M BXNS:
6583 * - if the return value is a magic value, do exception return (like BX)
6584 * - otherwise bit 0 of the return value is the target security state
6586 uint32_t min_magic;
6588 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6589 /* Covers FNC_RETURN and EXC_RETURN magic */
6590 min_magic = FNC_RETURN_MIN_MAGIC;
6591 } else {
6592 /* EXC_RETURN magic only */
6593 min_magic = EXC_RETURN_MIN_MAGIC;
6596 if (dest >= min_magic) {
6597 /* This is an exception return magic value; put it where
6598 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
6599 * Note that if we ever add gen_ss_advance() singlestep support to
6600 * M profile this should count as an "instruction execution complete"
6601 * event (compare gen_bx_excret_final_code()).
6603 env->regs[15] = dest & ~1;
6604 env->thumb = dest & 1;
6605 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
6606 /* notreached */
6609 /* translate.c should have made BXNS UNDEF unless we're secure */
6610 assert(env->v7m.secure);
6612 switch_v7m_security_state(env, dest & 1);
6613 env->thumb = 1;
6614 env->regs[15] = dest & ~1;
6617 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
6619 /* Handle v7M BLXNS:
6620 * - bit 0 of the destination address is the target security state
6623 /* At this point regs[15] is the address just after the BLXNS */
6624 uint32_t nextinst = env->regs[15] | 1;
6625 uint32_t sp = env->regs[13] - 8;
6626 uint32_t saved_psr;
6628 /* translate.c will have made BLXNS UNDEF unless we're secure */
6629 assert(env->v7m.secure);
6631 if (dest & 1) {
6632 /* target is Secure, so this is just a normal BLX,
6633 * except that the low bit doesn't indicate Thumb/not.
6635 env->regs[14] = nextinst;
6636 env->thumb = 1;
6637 env->regs[15] = dest & ~1;
6638 return;
6641 /* Target is non-secure: first push a stack frame */
6642 if (!QEMU_IS_ALIGNED(sp, 8)) {
6643 qemu_log_mask(LOG_GUEST_ERROR,
6644 "BLXNS with misaligned SP is UNPREDICTABLE\n");
6647 saved_psr = env->v7m.exception;
6648 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
6649 saved_psr |= XPSR_SFPA;
6652 /* Note that these stores can throw exceptions on MPU faults */
6653 cpu_stl_data(env, sp, nextinst);
6654 cpu_stl_data(env, sp + 4, saved_psr);
6656 env->regs[13] = sp;
6657 env->regs[14] = 0xfeffffff;
6658 if (arm_v7m_is_handler_mode(env)) {
6659 /* Write a dummy value to IPSR, to avoid leaking the current secure
6660 * exception number to non-secure code. This is guaranteed not
6661 * to cause write_v7m_exception() to actually change stacks.
6663 write_v7m_exception(env, 1);
6665 switch_v7m_security_state(env, 0);
6666 env->thumb = 1;
6667 env->regs[15] = dest;
6670 static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
6671 bool spsel)
6673 /* Return a pointer to the location where we currently store the
6674 * stack pointer for the requested security state and thread mode.
6675 * This pointer will become invalid if the CPU state is updated
6676 * such that the stack pointers are switched around (eg changing
6677 * the SPSEL control bit).
6678 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
6679 * Unlike that pseudocode, we require the caller to pass us in the
6680 * SPSEL control bit value; this is because we also use this
6681 * function in handling of pushing of the callee-saves registers
6682 * part of the v8M stack frame (pseudocode PushCalleeStack()),
6683 * and in the tailchain codepath the SPSEL bit comes from the exception
6684 * return magic LR value from the previous exception. The pseudocode
6685 * opencodes the stack-selection in PushCalleeStack(), but we prefer
6686 * to make this utility function generic enough to do the job.
6688 bool want_psp = threadmode && spsel;
6690 if (secure == env->v7m.secure) {
6691 if (want_psp == v7m_using_psp(env)) {
6692 return &env->regs[13];
6693 } else {
6694 return &env->v7m.other_sp;
6696 } else {
6697 if (want_psp) {
6698 return &env->v7m.other_ss_psp;
6699 } else {
6700 return &env->v7m.other_ss_msp;
6705 static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure,
6706 uint32_t *pvec)
6708 CPUState *cs = CPU(cpu);
6709 CPUARMState *env = &cpu->env;
6710 MemTxResult result;
6711 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4;
6712 uint32_t vector_entry;
6713 MemTxAttrs attrs = {};
6714 ARMMMUIdx mmu_idx;
6715 bool exc_secure;
6717 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true);
6719 /* We don't do a get_phys_addr() here because the rules for vector
6720 * loads are special: they always use the default memory map, and
6721 * the default memory map permits reads from all addresses.
6722 * Since there's no easy way to pass through to pmsav8_mpu_lookup()
6723 * that we want this special case which would always say "yes",
6724 * we just do the SAU lookup here followed by a direct physical load.
6726 attrs.secure = targets_secure;
6727 attrs.user = false;
6729 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6730 V8M_SAttributes sattrs = {};
6732 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
6733 if (sattrs.ns) {
6734 attrs.secure = false;
6735 } else if (!targets_secure) {
6736 /* NS access to S memory */
6737 goto load_fail;
6741 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr,
6742 attrs, &result);
6743 if (result != MEMTX_OK) {
6744 goto load_fail;
6746 *pvec = vector_entry;
6747 return true;
6749 load_fail:
6750 /* All vector table fetch fails are reported as HardFault, with
6751 * HFSR.VECTTBL and .FORCED set. (FORCED is set because
6752 * technically the underlying exception is a MemManage or BusFault
6753 * that is escalated to HardFault.) This is a terminal exception,
6754 * so we will either take the HardFault immediately or else enter
6755 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()).
6757 exc_secure = targets_secure ||
6758 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
6759 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK;
6760 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure);
6761 return false;
6764 static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain,
6765 bool ignore_faults)
6767 /* For v8M, push the callee-saves register part of the stack frame.
6768 * Compare the v8M pseudocode PushCalleeStack().
6769 * In the tailchaining case this may not be the current stack.
6771 CPUARMState *env = &cpu->env;
6772 uint32_t *frame_sp_p;
6773 uint32_t frameptr;
6774 ARMMMUIdx mmu_idx;
6775 bool stacked_ok;
6777 if (dotailchain) {
6778 bool mode = lr & R_V7M_EXCRET_MODE_MASK;
6779 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) ||
6780 !mode;
6782 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv);
6783 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode,
6784 lr & R_V7M_EXCRET_SPSEL_MASK);
6785 } else {
6786 mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
6787 frame_sp_p = &env->regs[13];
6790 frameptr = *frame_sp_p - 0x28;
6792 /* Write as much of the stack frame as we can. A write failure may
6793 * cause us to pend a derived exception.
6795 stacked_ok =
6796 v7m_stack_write(cpu, frameptr, 0xfefa125b, mmu_idx, ignore_faults) &&
6797 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx,
6798 ignore_faults) &&
6799 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx,
6800 ignore_faults) &&
6801 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx,
6802 ignore_faults) &&
6803 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx,
6804 ignore_faults) &&
6805 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx,
6806 ignore_faults) &&
6807 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx,
6808 ignore_faults) &&
6809 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx,
6810 ignore_faults) &&
6811 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx,
6812 ignore_faults);
6814 /* Update SP regardless of whether any of the stack accesses failed.
6815 * When we implement v8M stack limit checking then this attempt to
6816 * update SP might also fail and result in a derived exception.
6818 *frame_sp_p = frameptr;
6820 return !stacked_ok;
6823 static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain,
6824 bool ignore_stackfaults)
6826 /* Do the "take the exception" parts of exception entry,
6827 * but not the pushing of state to the stack. This is
6828 * similar to the pseudocode ExceptionTaken() function.
6830 CPUARMState *env = &cpu->env;
6831 uint32_t addr;
6832 bool targets_secure;
6833 int exc;
6834 bool push_failed = false;
6836 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure);
6838 if (arm_feature(env, ARM_FEATURE_V8)) {
6839 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
6840 (lr & R_V7M_EXCRET_S_MASK)) {
6841 /* The background code (the owner of the registers in the
6842 * exception frame) is Secure. This means it may either already
6843 * have or now needs to push callee-saves registers.
6845 if (targets_secure) {
6846 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
6847 /* We took an exception from Secure to NonSecure
6848 * (which means the callee-saved registers got stacked)
6849 * and are now tailchaining to a Secure exception.
6850 * Clear DCRS so eventual return from this Secure
6851 * exception unstacks the callee-saved registers.
6853 lr &= ~R_V7M_EXCRET_DCRS_MASK;
6855 } else {
6856 /* We're going to a non-secure exception; push the
6857 * callee-saves registers to the stack now, if they're
6858 * not already saved.
6860 if (lr & R_V7M_EXCRET_DCRS_MASK &&
6861 !(dotailchain && (lr & R_V7M_EXCRET_ES_MASK))) {
6862 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain,
6863 ignore_stackfaults);
6865 lr |= R_V7M_EXCRET_DCRS_MASK;
6869 lr &= ~R_V7M_EXCRET_ES_MASK;
6870 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6871 lr |= R_V7M_EXCRET_ES_MASK;
6873 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
6874 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
6875 lr |= R_V7M_EXCRET_SPSEL_MASK;
6878 /* Clear registers if necessary to prevent non-secure exception
6879 * code being able to see register values from secure code.
6880 * Where register values become architecturally UNKNOWN we leave
6881 * them with their previous values.
6883 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
6884 if (!targets_secure) {
6885 /* Always clear the caller-saved registers (they have been
6886 * pushed to the stack earlier in v7m_push_stack()).
6887 * Clear callee-saved registers if the background code is
6888 * Secure (in which case these regs were saved in
6889 * v7m_push_callee_stack()).
6891 int i;
6893 for (i = 0; i < 13; i++) {
6894 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
6895 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
6896 env->regs[i] = 0;
6899 /* Clear EAPSR */
6900 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
6905 if (push_failed && !ignore_stackfaults) {
6906 /* Derived exception on callee-saves register stacking:
6907 * we might now want to take a different exception which
6908 * targets a different security state, so try again from the top.
6910 v7m_exception_taken(cpu, lr, true, true);
6911 return;
6914 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) {
6915 /* Vector load failed: derived exception */
6916 v7m_exception_taken(cpu, lr, true, true);
6917 return;
6920 /* Now we've done everything that might cause a derived exception
6921 * we can go ahead and activate whichever exception we're going to
6922 * take (which might now be the derived exception).
6924 armv7m_nvic_acknowledge_irq(env->nvic);
6926 /* Switch to target security state -- must do this before writing SPSEL */
6927 switch_v7m_security_state(env, targets_secure);
6928 write_v7m_control_spsel(env, 0);
6929 arm_clear_exclusive(env);
6930 /* Clear IT bits */
6931 env->condexec_bits = 0;
6932 env->regs[14] = lr;
6933 env->regs[15] = addr & 0xfffffffe;
6934 env->thumb = addr & 1;
6937 static bool v7m_push_stack(ARMCPU *cpu)
6939 /* Do the "set up stack frame" part of exception entry,
6940 * similar to pseudocode PushStack().
6941 * Return true if we generate a derived exception (and so
6942 * should ignore further stack faults trying to process
6943 * that derived exception.)
6945 bool stacked_ok;
6946 CPUARMState *env = &cpu->env;
6947 uint32_t xpsr = xpsr_read(env);
6948 uint32_t frameptr = env->regs[13];
6949 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
6951 /* Align stack pointer if the guest wants that */
6952 if ((frameptr & 4) &&
6953 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
6954 frameptr -= 4;
6955 xpsr |= XPSR_SPREALIGN;
6958 frameptr -= 0x20;
6960 /* Write as much of the stack frame as we can. If we fail a stack
6961 * write this will result in a derived exception being pended
6962 * (which may be taken in preference to the one we started with
6963 * if it has higher priority).
6965 stacked_ok =
6966 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, false) &&
6967 v7m_stack_write(cpu, frameptr + 4, env->regs[1], mmu_idx, false) &&
6968 v7m_stack_write(cpu, frameptr + 8, env->regs[2], mmu_idx, false) &&
6969 v7m_stack_write(cpu, frameptr + 12, env->regs[3], mmu_idx, false) &&
6970 v7m_stack_write(cpu, frameptr + 16, env->regs[12], mmu_idx, false) &&
6971 v7m_stack_write(cpu, frameptr + 20, env->regs[14], mmu_idx, false) &&
6972 v7m_stack_write(cpu, frameptr + 24, env->regs[15], mmu_idx, false) &&
6973 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, false);
6975 /* Update SP regardless of whether any of the stack accesses failed.
6976 * When we implement v8M stack limit checking then this attempt to
6977 * update SP might also fail and result in a derived exception.
6979 env->regs[13] = frameptr;
6981 return !stacked_ok;
6984 static void do_v7m_exception_exit(ARMCPU *cpu)
6986 CPUARMState *env = &cpu->env;
6987 uint32_t excret;
6988 uint32_t xpsr;
6989 bool ufault = false;
6990 bool sfault = false;
6991 bool return_to_sp_process;
6992 bool return_to_handler;
6993 bool rettobase = false;
6994 bool exc_secure = false;
6995 bool return_to_secure;
6997 /* If we're not in Handler mode then jumps to magic exception-exit
6998 * addresses don't have magic behaviour. However for the v8M
6999 * security extensions the magic secure-function-return has to
7000 * work in thread mode too, so to avoid doing an extra check in
7001 * the generated code we allow exception-exit magic to also cause the
7002 * internal exception and bring us here in thread mode. Correct code
7003 * will never try to do this (the following insn fetch will always
7004 * fault) so we the overhead of having taken an unnecessary exception
7005 * doesn't matter.
7007 if (!arm_v7m_is_handler_mode(env)) {
7008 return;
7011 /* In the spec pseudocode ExceptionReturn() is called directly
7012 * from BXWritePC() and gets the full target PC value including
7013 * bit zero. In QEMU's implementation we treat it as a normal
7014 * jump-to-register (which is then caught later on), and so split
7015 * the target value up between env->regs[15] and env->thumb in
7016 * gen_bx(). Reconstitute it.
7018 excret = env->regs[15];
7019 if (env->thumb) {
7020 excret |= 1;
7023 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
7024 " previous exception %d\n",
7025 excret, env->v7m.exception);
7027 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
7028 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
7029 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
7030 excret);
7033 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
7034 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before
7035 * we pick which FAULTMASK to clear.
7037 if (!env->v7m.secure &&
7038 ((excret & R_V7M_EXCRET_ES_MASK) ||
7039 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
7040 sfault = 1;
7041 /* For all other purposes, treat ES as 0 (R_HXSR) */
7042 excret &= ~R_V7M_EXCRET_ES_MASK;
7046 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
7047 /* Auto-clear FAULTMASK on return from other than NMI.
7048 * If the security extension is implemented then this only
7049 * happens if the raw execution priority is >= 0; the
7050 * value of the ES bit in the exception return value indicates
7051 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
7053 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
7054 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
7055 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
7056 env->v7m.faultmask[exc_secure] = 0;
7058 } else {
7059 env->v7m.faultmask[M_REG_NS] = 0;
7063 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
7064 exc_secure)) {
7065 case -1:
7066 /* attempt to exit an exception that isn't active */
7067 ufault = true;
7068 break;
7069 case 0:
7070 /* still an irq active now */
7071 break;
7072 case 1:
7073 /* we returned to base exception level, no nesting.
7074 * (In the pseudocode this is written using "NestedActivation != 1"
7075 * where we have 'rettobase == false'.)
7077 rettobase = true;
7078 break;
7079 default:
7080 g_assert_not_reached();
7083 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
7084 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
7085 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
7086 (excret & R_V7M_EXCRET_S_MASK);
7088 if (arm_feature(env, ARM_FEATURE_V8)) {
7089 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
7090 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
7091 * we choose to take the UsageFault.
7093 if ((excret & R_V7M_EXCRET_S_MASK) ||
7094 (excret & R_V7M_EXCRET_ES_MASK) ||
7095 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
7096 ufault = true;
7099 if (excret & R_V7M_EXCRET_RES0_MASK) {
7100 ufault = true;
7102 } else {
7103 /* For v7M we only recognize certain combinations of the low bits */
7104 switch (excret & 0xf) {
7105 case 1: /* Return to Handler */
7106 break;
7107 case 13: /* Return to Thread using Process stack */
7108 case 9: /* Return to Thread using Main stack */
7109 /* We only need to check NONBASETHRDENA for v7M, because in
7110 * v8M this bit does not exist (it is RES1).
7112 if (!rettobase &&
7113 !(env->v7m.ccr[env->v7m.secure] &
7114 R_V7M_CCR_NONBASETHRDENA_MASK)) {
7115 ufault = true;
7117 break;
7118 default:
7119 ufault = true;
7123 if (sfault) {
7124 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
7125 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7126 v7m_exception_taken(cpu, excret, true, false);
7127 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
7128 "stackframe: failed EXC_RETURN.ES validity check\n");
7129 return;
7132 if (ufault) {
7133 /* Bad exception return: instead of popping the exception
7134 * stack, directly take a usage fault on the current stack.
7136 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7137 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7138 v7m_exception_taken(cpu, excret, true, false);
7139 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
7140 "stackframe: failed exception return integrity check\n");
7141 return;
7144 /* Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
7145 * Handler mode (and will be until we write the new XPSR.Interrupt
7146 * field) this does not switch around the current stack pointer.
7148 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
7150 switch_v7m_security_state(env, return_to_secure);
7153 /* The stack pointer we should be reading the exception frame from
7154 * depends on bits in the magic exception return type value (and
7155 * for v8M isn't necessarily the stack pointer we will eventually
7156 * end up resuming execution with). Get a pointer to the location
7157 * in the CPU state struct where the SP we need is currently being
7158 * stored; we will use and modify it in place.
7159 * We use this limited C variable scope so we don't accidentally
7160 * use 'frame_sp_p' after we do something that makes it invalid.
7162 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
7163 return_to_secure,
7164 !return_to_handler,
7165 return_to_sp_process);
7166 uint32_t frameptr = *frame_sp_p;
7167 bool pop_ok = true;
7168 ARMMMUIdx mmu_idx;
7169 bool return_to_priv = return_to_handler ||
7170 !(env->v7m.control[return_to_secure] & R_V7M_CONTROL_NPRIV_MASK);
7172 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure,
7173 return_to_priv);
7175 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
7176 arm_feature(env, ARM_FEATURE_V8)) {
7177 qemu_log_mask(LOG_GUEST_ERROR,
7178 "M profile exception return with non-8-aligned SP "
7179 "for destination state is UNPREDICTABLE\n");
7182 /* Do we need to pop callee-saved registers? */
7183 if (return_to_secure &&
7184 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
7185 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
7186 uint32_t expected_sig = 0xfefa125b;
7187 uint32_t actual_sig;
7189 pop_ok = v7m_stack_read(cpu, &actual_sig, frameptr, mmu_idx);
7191 if (pop_ok && expected_sig != actual_sig) {
7192 /* Take a SecureFault on the current stack */
7193 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
7194 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7195 v7m_exception_taken(cpu, excret, true, false);
7196 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
7197 "stackframe: failed exception return integrity "
7198 "signature check\n");
7199 return;
7202 pop_ok = pop_ok &&
7203 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7204 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
7205 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) &&
7206 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) &&
7207 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) &&
7208 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) &&
7209 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) &&
7210 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) &&
7211 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx);
7213 frameptr += 0x28;
7216 /* Pop registers */
7217 pop_ok = pop_ok &&
7218 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) &&
7219 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) &&
7220 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) &&
7221 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) &&
7222 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) &&
7223 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) &&
7224 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) &&
7225 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx);
7227 if (!pop_ok) {
7228 /* v7m_stack_read() pended a fault, so take it (as a tail
7229 * chained exception on the same stack frame)
7231 v7m_exception_taken(cpu, excret, true, false);
7232 return;
7235 /* Returning from an exception with a PC with bit 0 set is defined
7236 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
7237 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
7238 * the lsbit, and there are several RTOSes out there which incorrectly
7239 * assume the r15 in the stack frame should be a Thumb-style "lsbit
7240 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
7241 * complain about the badly behaved guest.
7243 if (env->regs[15] & 1) {
7244 env->regs[15] &= ~1U;
7245 if (!arm_feature(env, ARM_FEATURE_V8)) {
7246 qemu_log_mask(LOG_GUEST_ERROR,
7247 "M profile return from interrupt with misaligned "
7248 "PC is UNPREDICTABLE on v7M\n");
7252 if (arm_feature(env, ARM_FEATURE_V8)) {
7253 /* For v8M we have to check whether the xPSR exception field
7254 * matches the EXCRET value for return to handler/thread
7255 * before we commit to changing the SP and xPSR.
7257 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
7258 if (return_to_handler != will_be_handler) {
7259 /* Take an INVPC UsageFault on the current stack.
7260 * By this point we will have switched to the security state
7261 * for the background state, so this UsageFault will target
7262 * that state.
7264 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7265 env->v7m.secure);
7266 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7267 v7m_exception_taken(cpu, excret, true, false);
7268 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
7269 "stackframe: failed exception return integrity "
7270 "check\n");
7271 return;
7275 /* Commit to consuming the stack frame */
7276 frameptr += 0x20;
7277 /* Undo stack alignment (the SPREALIGN bit indicates that the original
7278 * pre-exception SP was not 8-aligned and we added a padding word to
7279 * align it, so we undo this by ORing in the bit that increases it
7280 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
7281 * would work too but a logical OR is how the pseudocode specifies it.)
7283 if (xpsr & XPSR_SPREALIGN) {
7284 frameptr |= 4;
7286 *frame_sp_p = frameptr;
7288 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
7289 xpsr_write(env, xpsr, ~XPSR_SPREALIGN);
7291 /* The restored xPSR exception field will be zero if we're
7292 * resuming in Thread mode. If that doesn't match what the
7293 * exception return excret specified then this is a UsageFault.
7294 * v7M requires we make this check here; v8M did it earlier.
7296 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
7297 /* Take an INVPC UsageFault by pushing the stack again;
7298 * we know we're v7M so this is never a Secure UsageFault.
7300 bool ignore_stackfaults;
7302 assert(!arm_feature(env, ARM_FEATURE_V8));
7303 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
7304 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7305 ignore_stackfaults = v7m_push_stack(cpu);
7306 v7m_exception_taken(cpu, excret, false, ignore_stackfaults);
7307 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
7308 "failed exception return integrity check\n");
7309 return;
7312 /* Otherwise, we have a successful exception exit. */
7313 arm_clear_exclusive(env);
7314 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
7317 static bool do_v7m_function_return(ARMCPU *cpu)
7319 /* v8M security extensions magic function return.
7320 * We may either:
7321 * (1) throw an exception (longjump)
7322 * (2) return true if we successfully handled the function return
7323 * (3) return false if we failed a consistency check and have
7324 * pended a UsageFault that needs to be taken now
7326 * At this point the magic return value is split between env->regs[15]
7327 * and env->thumb. We don't bother to reconstitute it because we don't
7328 * need it (all values are handled the same way).
7330 CPUARMState *env = &cpu->env;
7331 uint32_t newpc, newpsr, newpsr_exc;
7333 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
7336 bool threadmode, spsel;
7337 TCGMemOpIdx oi;
7338 ARMMMUIdx mmu_idx;
7339 uint32_t *frame_sp_p;
7340 uint32_t frameptr;
7342 /* Pull the return address and IPSR from the Secure stack */
7343 threadmode = !arm_v7m_is_handler_mode(env);
7344 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
7346 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
7347 frameptr = *frame_sp_p;
7349 /* These loads may throw an exception (for MPU faults). We want to
7350 * do them as secure, so work out what MMU index that is.
7352 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7353 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx));
7354 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0);
7355 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0);
7357 /* Consistency checks on new IPSR */
7358 newpsr_exc = newpsr & XPSR_EXCP;
7359 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
7360 (env->v7m.exception == 1 && newpsr_exc != 0))) {
7361 /* Pend the fault and tell our caller to take it */
7362 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
7363 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7364 env->v7m.secure);
7365 qemu_log_mask(CPU_LOG_INT,
7366 "...taking INVPC UsageFault: "
7367 "IPSR consistency check failed\n");
7368 return false;
7371 *frame_sp_p = frameptr + 8;
7374 /* This invalidates frame_sp_p */
7375 switch_v7m_security_state(env, true);
7376 env->v7m.exception = newpsr_exc;
7377 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
7378 if (newpsr & XPSR_SFPA) {
7379 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
7381 xpsr_write(env, 0, XPSR_IT);
7382 env->thumb = newpc & 1;
7383 env->regs[15] = newpc & ~1;
7385 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
7386 return true;
7389 static void arm_log_exception(int idx)
7391 if (qemu_loglevel_mask(CPU_LOG_INT)) {
7392 const char *exc = NULL;
7393 static const char * const excnames[] = {
7394 [EXCP_UDEF] = "Undefined Instruction",
7395 [EXCP_SWI] = "SVC",
7396 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
7397 [EXCP_DATA_ABORT] = "Data Abort",
7398 [EXCP_IRQ] = "IRQ",
7399 [EXCP_FIQ] = "FIQ",
7400 [EXCP_BKPT] = "Breakpoint",
7401 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
7402 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
7403 [EXCP_HVC] = "Hypervisor Call",
7404 [EXCP_HYP_TRAP] = "Hypervisor Trap",
7405 [EXCP_SMC] = "Secure Monitor Call",
7406 [EXCP_VIRQ] = "Virtual IRQ",
7407 [EXCP_VFIQ] = "Virtual FIQ",
7408 [EXCP_SEMIHOST] = "Semihosting call",
7409 [EXCP_NOCP] = "v7M NOCP UsageFault",
7410 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
7413 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
7414 exc = excnames[idx];
7416 if (!exc) {
7417 exc = "unknown";
7419 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
7423 static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
7424 uint32_t addr, uint16_t *insn)
7426 /* Load a 16-bit portion of a v7M instruction, returning true on success,
7427 * or false on failure (in which case we will have pended the appropriate
7428 * exception).
7429 * We need to do the instruction fetch's MPU and SAU checks
7430 * like this because there is no MMU index that would allow
7431 * doing the load with a single function call. Instead we must
7432 * first check that the security attributes permit the load
7433 * and that they don't mismatch on the two halves of the instruction,
7434 * and then we do the load as a secure load (ie using the security
7435 * attributes of the address, not the CPU, as architecturally required).
7437 CPUState *cs = CPU(cpu);
7438 CPUARMState *env = &cpu->env;
7439 V8M_SAttributes sattrs = {};
7440 MemTxAttrs attrs = {};
7441 ARMMMUFaultInfo fi = {};
7442 MemTxResult txres;
7443 target_ulong page_size;
7444 hwaddr physaddr;
7445 int prot;
7447 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
7448 if (!sattrs.nsc || sattrs.ns) {
7449 /* This must be the second half of the insn, and it straddles a
7450 * region boundary with the second half not being S&NSC.
7452 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7453 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7454 qemu_log_mask(CPU_LOG_INT,
7455 "...really SecureFault with SFSR.INVEP\n");
7456 return false;
7458 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
7459 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) {
7460 /* the MPU lookup failed */
7461 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
7462 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
7463 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
7464 return false;
7466 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
7467 attrs, &txres);
7468 if (txres != MEMTX_OK) {
7469 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7470 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7471 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
7472 return false;
7474 return true;
7477 static bool v7m_handle_execute_nsc(ARMCPU *cpu)
7479 /* Check whether this attempt to execute code in a Secure & NS-Callable
7480 * memory region is for an SG instruction; if so, then emulate the
7481 * effect of the SG instruction and return true. Otherwise pend
7482 * the correct kind of exception and return false.
7484 CPUARMState *env = &cpu->env;
7485 ARMMMUIdx mmu_idx;
7486 uint16_t insn;
7488 /* We should never get here unless get_phys_addr_pmsav8() caused
7489 * an exception for NS executing in S&NSC memory.
7491 assert(!env->v7m.secure);
7492 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7494 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
7495 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
7497 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
7498 return false;
7501 if (!env->thumb) {
7502 goto gen_invep;
7505 if (insn != 0xe97f) {
7506 /* Not an SG instruction first half (we choose the IMPDEF
7507 * early-SG-check option).
7509 goto gen_invep;
7512 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
7513 return false;
7516 if (insn != 0xe97f) {
7517 /* Not an SG instruction second half (yes, both halves of the SG
7518 * insn have the same hex value)
7520 goto gen_invep;
7523 /* OK, we have confirmed that we really have an SG instruction.
7524 * We know we're NS in S memory so don't need to repeat those checks.
7526 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
7527 ", executing it\n", env->regs[15]);
7528 env->regs[14] &= ~1;
7529 switch_v7m_security_state(env, true);
7530 xpsr_write(env, 0, XPSR_IT);
7531 env->regs[15] += 4;
7532 return true;
7534 gen_invep:
7535 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7536 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7537 qemu_log_mask(CPU_LOG_INT,
7538 "...really SecureFault with SFSR.INVEP\n");
7539 return false;
7542 void arm_v7m_cpu_do_interrupt(CPUState *cs)
7544 ARMCPU *cpu = ARM_CPU(cs);
7545 CPUARMState *env = &cpu->env;
7546 uint32_t lr;
7547 bool ignore_stackfaults;
7549 arm_log_exception(cs->exception_index);
7551 /* For exceptions we just mark as pending on the NVIC, and let that
7552 handle it. */
7553 switch (cs->exception_index) {
7554 case EXCP_UDEF:
7555 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7556 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
7557 break;
7558 case EXCP_NOCP:
7559 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7560 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
7561 break;
7562 case EXCP_INVSTATE:
7563 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
7564 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
7565 break;
7566 case EXCP_SWI:
7567 /* The PC already points to the next instruction. */
7568 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
7569 break;
7570 case EXCP_PREFETCH_ABORT:
7571 case EXCP_DATA_ABORT:
7572 /* Note that for M profile we don't have a guest facing FSR, but
7573 * the env->exception.fsr will be populated by the code that
7574 * raises the fault, in the A profile short-descriptor format.
7576 switch (env->exception.fsr & 0xf) {
7577 case M_FAKE_FSR_NSC_EXEC:
7578 /* Exception generated when we try to execute code at an address
7579 * which is marked as Secure & Non-Secure Callable and the CPU
7580 * is in the Non-Secure state. The only instruction which can
7581 * be executed like this is SG (and that only if both halves of
7582 * the SG instruction have the same security attributes.)
7583 * Everything else must generate an INVEP SecureFault, so we
7584 * emulate the SG instruction here.
7586 if (v7m_handle_execute_nsc(cpu)) {
7587 return;
7589 break;
7590 case M_FAKE_FSR_SFAULT:
7591 /* Various flavours of SecureFault for attempts to execute or
7592 * access data in the wrong security state.
7594 switch (cs->exception_index) {
7595 case EXCP_PREFETCH_ABORT:
7596 if (env->v7m.secure) {
7597 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
7598 qemu_log_mask(CPU_LOG_INT,
7599 "...really SecureFault with SFSR.INVTRAN\n");
7600 } else {
7601 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
7602 qemu_log_mask(CPU_LOG_INT,
7603 "...really SecureFault with SFSR.INVEP\n");
7605 break;
7606 case EXCP_DATA_ABORT:
7607 /* This must be an NS access to S memory */
7608 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
7609 qemu_log_mask(CPU_LOG_INT,
7610 "...really SecureFault with SFSR.AUVIOL\n");
7611 break;
7613 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
7614 break;
7615 case 0x8: /* External Abort */
7616 switch (cs->exception_index) {
7617 case EXCP_PREFETCH_ABORT:
7618 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
7619 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
7620 break;
7621 case EXCP_DATA_ABORT:
7622 env->v7m.cfsr[M_REG_NS] |=
7623 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
7624 env->v7m.bfar = env->exception.vaddress;
7625 qemu_log_mask(CPU_LOG_INT,
7626 "...with CFSR.PRECISERR and BFAR 0x%x\n",
7627 env->v7m.bfar);
7628 break;
7630 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
7631 break;
7632 default:
7633 /* All other FSR values are either MPU faults or "can't happen
7634 * for M profile" cases.
7636 switch (cs->exception_index) {
7637 case EXCP_PREFETCH_ABORT:
7638 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
7639 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
7640 break;
7641 case EXCP_DATA_ABORT:
7642 env->v7m.cfsr[env->v7m.secure] |=
7643 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
7644 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
7645 qemu_log_mask(CPU_LOG_INT,
7646 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
7647 env->v7m.mmfar[env->v7m.secure]);
7648 break;
7650 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
7651 env->v7m.secure);
7652 break;
7654 break;
7655 case EXCP_BKPT:
7656 if (semihosting_enabled()) {
7657 int nr;
7658 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
7659 if (nr == 0xab) {
7660 env->regs[15] += 2;
7661 qemu_log_mask(CPU_LOG_INT,
7662 "...handling as semihosting call 0x%x\n",
7663 env->regs[0]);
7664 env->regs[0] = do_arm_semihosting(env);
7665 return;
7668 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
7669 break;
7670 case EXCP_IRQ:
7671 break;
7672 case EXCP_EXCEPTION_EXIT:
7673 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
7674 /* Must be v8M security extension function return */
7675 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
7676 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
7677 if (do_v7m_function_return(cpu)) {
7678 return;
7680 } else {
7681 do_v7m_exception_exit(cpu);
7682 return;
7684 break;
7685 default:
7686 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7687 return; /* Never happens. Keep compiler happy. */
7690 if (arm_feature(env, ARM_FEATURE_V8)) {
7691 lr = R_V7M_EXCRET_RES1_MASK |
7692 R_V7M_EXCRET_DCRS_MASK |
7693 R_V7M_EXCRET_FTYPE_MASK;
7694 /* The S bit indicates whether we should return to Secure
7695 * or NonSecure (ie our current state).
7696 * The ES bit indicates whether we're taking this exception
7697 * to Secure or NonSecure (ie our target state). We set it
7698 * later, in v7m_exception_taken().
7699 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
7700 * This corresponds to the ARM ARM pseudocode for v8M setting
7701 * some LR bits in PushStack() and some in ExceptionTaken();
7702 * the distinction matters for the tailchain cases where we
7703 * can take an exception without pushing the stack.
7705 if (env->v7m.secure) {
7706 lr |= R_V7M_EXCRET_S_MASK;
7708 } else {
7709 lr = R_V7M_EXCRET_RES1_MASK |
7710 R_V7M_EXCRET_S_MASK |
7711 R_V7M_EXCRET_DCRS_MASK |
7712 R_V7M_EXCRET_FTYPE_MASK |
7713 R_V7M_EXCRET_ES_MASK;
7714 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
7715 lr |= R_V7M_EXCRET_SPSEL_MASK;
7718 if (!arm_v7m_is_handler_mode(env)) {
7719 lr |= R_V7M_EXCRET_MODE_MASK;
7722 ignore_stackfaults = v7m_push_stack(cpu);
7723 v7m_exception_taken(cpu, lr, false, ignore_stackfaults);
7724 qemu_log_mask(CPU_LOG_INT, "... as %d\n", env->v7m.exception);
7727 /* Function used to synchronize QEMU's AArch64 register set with AArch32
7728 * register set. This is necessary when switching between AArch32 and AArch64
7729 * execution state.
7731 void aarch64_sync_32_to_64(CPUARMState *env)
7733 int i;
7734 uint32_t mode = env->uncached_cpsr & CPSR_M;
7736 /* We can blanket copy R[0:7] to X[0:7] */
7737 for (i = 0; i < 8; i++) {
7738 env->xregs[i] = env->regs[i];
7741 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
7742 * Otherwise, they come from the banked user regs.
7744 if (mode == ARM_CPU_MODE_FIQ) {
7745 for (i = 8; i < 13; i++) {
7746 env->xregs[i] = env->usr_regs[i - 8];
7748 } else {
7749 for (i = 8; i < 13; i++) {
7750 env->xregs[i] = env->regs[i];
7754 /* Registers x13-x23 are the various mode SP and FP registers. Registers
7755 * r13 and r14 are only copied if we are in that mode, otherwise we copy
7756 * from the mode banked register.
7758 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7759 env->xregs[13] = env->regs[13];
7760 env->xregs[14] = env->regs[14];
7761 } else {
7762 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
7763 /* HYP is an exception in that it is copied from r14 */
7764 if (mode == ARM_CPU_MODE_HYP) {
7765 env->xregs[14] = env->regs[14];
7766 } else {
7767 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
7771 if (mode == ARM_CPU_MODE_HYP) {
7772 env->xregs[15] = env->regs[13];
7773 } else {
7774 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
7777 if (mode == ARM_CPU_MODE_IRQ) {
7778 env->xregs[16] = env->regs[14];
7779 env->xregs[17] = env->regs[13];
7780 } else {
7781 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
7782 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
7785 if (mode == ARM_CPU_MODE_SVC) {
7786 env->xregs[18] = env->regs[14];
7787 env->xregs[19] = env->regs[13];
7788 } else {
7789 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
7790 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
7793 if (mode == ARM_CPU_MODE_ABT) {
7794 env->xregs[20] = env->regs[14];
7795 env->xregs[21] = env->regs[13];
7796 } else {
7797 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
7798 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
7801 if (mode == ARM_CPU_MODE_UND) {
7802 env->xregs[22] = env->regs[14];
7803 env->xregs[23] = env->regs[13];
7804 } else {
7805 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
7806 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
7809 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7810 * mode, then we can copy from r8-r14. Otherwise, we copy from the
7811 * FIQ bank for r8-r14.
7813 if (mode == ARM_CPU_MODE_FIQ) {
7814 for (i = 24; i < 31; i++) {
7815 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
7817 } else {
7818 for (i = 24; i < 29; i++) {
7819 env->xregs[i] = env->fiq_regs[i - 24];
7821 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
7822 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
7825 env->pc = env->regs[15];
7828 /* Function used to synchronize QEMU's AArch32 register set with AArch64
7829 * register set. This is necessary when switching between AArch32 and AArch64
7830 * execution state.
7832 void aarch64_sync_64_to_32(CPUARMState *env)
7834 int i;
7835 uint32_t mode = env->uncached_cpsr & CPSR_M;
7837 /* We can blanket copy X[0:7] to R[0:7] */
7838 for (i = 0; i < 8; i++) {
7839 env->regs[i] = env->xregs[i];
7842 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
7843 * Otherwise, we copy x8-x12 into the banked user regs.
7845 if (mode == ARM_CPU_MODE_FIQ) {
7846 for (i = 8; i < 13; i++) {
7847 env->usr_regs[i - 8] = env->xregs[i];
7849 } else {
7850 for (i = 8; i < 13; i++) {
7851 env->regs[i] = env->xregs[i];
7855 /* Registers r13 & r14 depend on the current mode.
7856 * If we are in a given mode, we copy the corresponding x registers to r13
7857 * and r14. Otherwise, we copy the x register to the banked r13 and r14
7858 * for the mode.
7860 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7861 env->regs[13] = env->xregs[13];
7862 env->regs[14] = env->xregs[14];
7863 } else {
7864 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
7866 /* HYP is an exception in that it does not have its own banked r14 but
7867 * shares the USR r14
7869 if (mode == ARM_CPU_MODE_HYP) {
7870 env->regs[14] = env->xregs[14];
7871 } else {
7872 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
7876 if (mode == ARM_CPU_MODE_HYP) {
7877 env->regs[13] = env->xregs[15];
7878 } else {
7879 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
7882 if (mode == ARM_CPU_MODE_IRQ) {
7883 env->regs[14] = env->xregs[16];
7884 env->regs[13] = env->xregs[17];
7885 } else {
7886 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
7887 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
7890 if (mode == ARM_CPU_MODE_SVC) {
7891 env->regs[14] = env->xregs[18];
7892 env->regs[13] = env->xregs[19];
7893 } else {
7894 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
7895 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
7898 if (mode == ARM_CPU_MODE_ABT) {
7899 env->regs[14] = env->xregs[20];
7900 env->regs[13] = env->xregs[21];
7901 } else {
7902 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
7903 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
7906 if (mode == ARM_CPU_MODE_UND) {
7907 env->regs[14] = env->xregs[22];
7908 env->regs[13] = env->xregs[23];
7909 } else {
7910 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
7911 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
7914 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7915 * mode, then we can copy to r8-r14. Otherwise, we copy to the
7916 * FIQ bank for r8-r14.
7918 if (mode == ARM_CPU_MODE_FIQ) {
7919 for (i = 24; i < 31; i++) {
7920 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
7922 } else {
7923 for (i = 24; i < 29; i++) {
7924 env->fiq_regs[i - 24] = env->xregs[i];
7926 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
7927 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
7930 env->regs[15] = env->pc;
7933 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
7935 ARMCPU *cpu = ARM_CPU(cs);
7936 CPUARMState *env = &cpu->env;
7937 uint32_t addr;
7938 uint32_t mask;
7939 int new_mode;
7940 uint32_t offset;
7941 uint32_t moe;
7943 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
7944 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
7945 case EC_BREAKPOINT:
7946 case EC_BREAKPOINT_SAME_EL:
7947 moe = 1;
7948 break;
7949 case EC_WATCHPOINT:
7950 case EC_WATCHPOINT_SAME_EL:
7951 moe = 10;
7952 break;
7953 case EC_AA32_BKPT:
7954 moe = 3;
7955 break;
7956 case EC_VECTORCATCH:
7957 moe = 5;
7958 break;
7959 default:
7960 moe = 0;
7961 break;
7964 if (moe) {
7965 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
7968 /* TODO: Vectored interrupt controller. */
7969 switch (cs->exception_index) {
7970 case EXCP_UDEF:
7971 new_mode = ARM_CPU_MODE_UND;
7972 addr = 0x04;
7973 mask = CPSR_I;
7974 if (env->thumb)
7975 offset = 2;
7976 else
7977 offset = 4;
7978 break;
7979 case EXCP_SWI:
7980 new_mode = ARM_CPU_MODE_SVC;
7981 addr = 0x08;
7982 mask = CPSR_I;
7983 /* The PC already points to the next instruction. */
7984 offset = 0;
7985 break;
7986 case EXCP_BKPT:
7987 /* Fall through to prefetch abort. */
7988 case EXCP_PREFETCH_ABORT:
7989 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
7990 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
7991 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
7992 env->exception.fsr, (uint32_t)env->exception.vaddress);
7993 new_mode = ARM_CPU_MODE_ABT;
7994 addr = 0x0c;
7995 mask = CPSR_A | CPSR_I;
7996 offset = 4;
7997 break;
7998 case EXCP_DATA_ABORT:
7999 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
8000 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
8001 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
8002 env->exception.fsr,
8003 (uint32_t)env->exception.vaddress);
8004 new_mode = ARM_CPU_MODE_ABT;
8005 addr = 0x10;
8006 mask = CPSR_A | CPSR_I;
8007 offset = 8;
8008 break;
8009 case EXCP_IRQ:
8010 new_mode = ARM_CPU_MODE_IRQ;
8011 addr = 0x18;
8012 /* Disable IRQ and imprecise data aborts. */
8013 mask = CPSR_A | CPSR_I;
8014 offset = 4;
8015 if (env->cp15.scr_el3 & SCR_IRQ) {
8016 /* IRQ routed to monitor mode */
8017 new_mode = ARM_CPU_MODE_MON;
8018 mask |= CPSR_F;
8020 break;
8021 case EXCP_FIQ:
8022 new_mode = ARM_CPU_MODE_FIQ;
8023 addr = 0x1c;
8024 /* Disable FIQ, IRQ and imprecise data aborts. */
8025 mask = CPSR_A | CPSR_I | CPSR_F;
8026 if (env->cp15.scr_el3 & SCR_FIQ) {
8027 /* FIQ routed to monitor mode */
8028 new_mode = ARM_CPU_MODE_MON;
8030 offset = 4;
8031 break;
8032 case EXCP_VIRQ:
8033 new_mode = ARM_CPU_MODE_IRQ;
8034 addr = 0x18;
8035 /* Disable IRQ and imprecise data aborts. */
8036 mask = CPSR_A | CPSR_I;
8037 offset = 4;
8038 break;
8039 case EXCP_VFIQ:
8040 new_mode = ARM_CPU_MODE_FIQ;
8041 addr = 0x1c;
8042 /* Disable FIQ, IRQ and imprecise data aborts. */
8043 mask = CPSR_A | CPSR_I | CPSR_F;
8044 offset = 4;
8045 break;
8046 case EXCP_SMC:
8047 new_mode = ARM_CPU_MODE_MON;
8048 addr = 0x08;
8049 mask = CPSR_A | CPSR_I | CPSR_F;
8050 offset = 0;
8051 break;
8052 default:
8053 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8054 return; /* Never happens. Keep compiler happy. */
8057 if (new_mode == ARM_CPU_MODE_MON) {
8058 addr += env->cp15.mvbar;
8059 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
8060 /* High vectors. When enabled, base address cannot be remapped. */
8061 addr += 0xffff0000;
8062 } else {
8063 /* ARM v7 architectures provide a vector base address register to remap
8064 * the interrupt vector table.
8065 * This register is only followed in non-monitor mode, and is banked.
8066 * Note: only bits 31:5 are valid.
8068 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
8071 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
8072 env->cp15.scr_el3 &= ~SCR_NS;
8075 switch_mode (env, new_mode);
8076 /* For exceptions taken to AArch32 we must clear the SS bit in both
8077 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
8079 env->uncached_cpsr &= ~PSTATE_SS;
8080 env->spsr = cpsr_read(env);
8081 /* Clear IT bits. */
8082 env->condexec_bits = 0;
8083 /* Switch to the new mode, and to the correct instruction set. */
8084 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
8085 /* Set new mode endianness */
8086 env->uncached_cpsr &= ~CPSR_E;
8087 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
8088 env->uncached_cpsr |= CPSR_E;
8090 env->daif |= mask;
8091 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
8092 * and we should just guard the thumb mode on V4 */
8093 if (arm_feature(env, ARM_FEATURE_V4T)) {
8094 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
8096 env->regs[14] = env->regs[15] + offset;
8097 env->regs[15] = addr;
8100 /* Handle exception entry to a target EL which is using AArch64 */
8101 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
8103 ARMCPU *cpu = ARM_CPU(cs);
8104 CPUARMState *env = &cpu->env;
8105 unsigned int new_el = env->exception.target_el;
8106 target_ulong addr = env->cp15.vbar_el[new_el];
8107 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
8109 if (arm_current_el(env) < new_el) {
8110 /* Entry vector offset depends on whether the implemented EL
8111 * immediately lower than the target level is using AArch32 or AArch64
8113 bool is_aa64;
8115 switch (new_el) {
8116 case 3:
8117 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
8118 break;
8119 case 2:
8120 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
8121 break;
8122 case 1:
8123 is_aa64 = is_a64(env);
8124 break;
8125 default:
8126 g_assert_not_reached();
8129 if (is_aa64) {
8130 addr += 0x400;
8131 } else {
8132 addr += 0x600;
8134 } else if (pstate_read(env) & PSTATE_SP) {
8135 addr += 0x200;
8138 switch (cs->exception_index) {
8139 case EXCP_PREFETCH_ABORT:
8140 case EXCP_DATA_ABORT:
8141 env->cp15.far_el[new_el] = env->exception.vaddress;
8142 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
8143 env->cp15.far_el[new_el]);
8144 /* fall through */
8145 case EXCP_BKPT:
8146 case EXCP_UDEF:
8147 case EXCP_SWI:
8148 case EXCP_HVC:
8149 case EXCP_HYP_TRAP:
8150 case EXCP_SMC:
8151 env->cp15.esr_el[new_el] = env->exception.syndrome;
8152 break;
8153 case EXCP_IRQ:
8154 case EXCP_VIRQ:
8155 addr += 0x80;
8156 break;
8157 case EXCP_FIQ:
8158 case EXCP_VFIQ:
8159 addr += 0x100;
8160 break;
8161 case EXCP_SEMIHOST:
8162 qemu_log_mask(CPU_LOG_INT,
8163 "...handling as semihosting call 0x%" PRIx64 "\n",
8164 env->xregs[0]);
8165 env->xregs[0] = do_arm_semihosting(env);
8166 return;
8167 default:
8168 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8171 if (is_a64(env)) {
8172 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
8173 aarch64_save_sp(env, arm_current_el(env));
8174 env->elr_el[new_el] = env->pc;
8175 } else {
8176 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
8177 env->elr_el[new_el] = env->regs[15];
8179 aarch64_sync_32_to_64(env);
8181 env->condexec_bits = 0;
8183 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
8184 env->elr_el[new_el]);
8186 pstate_write(env, PSTATE_DAIF | new_mode);
8187 env->aarch64 = 1;
8188 aarch64_restore_sp(env, new_el);
8190 env->pc = addr;
8192 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
8193 new_el, env->pc, pstate_read(env));
8196 static inline bool check_for_semihosting(CPUState *cs)
8198 /* Check whether this exception is a semihosting call; if so
8199 * then handle it and return true; otherwise return false.
8201 ARMCPU *cpu = ARM_CPU(cs);
8202 CPUARMState *env = &cpu->env;
8204 if (is_a64(env)) {
8205 if (cs->exception_index == EXCP_SEMIHOST) {
8206 /* This is always the 64-bit semihosting exception.
8207 * The "is this usermode" and "is semihosting enabled"
8208 * checks have been done at translate time.
8210 qemu_log_mask(CPU_LOG_INT,
8211 "...handling as semihosting call 0x%" PRIx64 "\n",
8212 env->xregs[0]);
8213 env->xregs[0] = do_arm_semihosting(env);
8214 return true;
8216 return false;
8217 } else {
8218 uint32_t imm;
8220 /* Only intercept calls from privileged modes, to provide some
8221 * semblance of security.
8223 if (cs->exception_index != EXCP_SEMIHOST &&
8224 (!semihosting_enabled() ||
8225 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
8226 return false;
8229 switch (cs->exception_index) {
8230 case EXCP_SEMIHOST:
8231 /* This is always a semihosting call; the "is this usermode"
8232 * and "is semihosting enabled" checks have been done at
8233 * translate time.
8235 break;
8236 case EXCP_SWI:
8237 /* Check for semihosting interrupt. */
8238 if (env->thumb) {
8239 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
8240 & 0xff;
8241 if (imm == 0xab) {
8242 break;
8244 } else {
8245 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
8246 & 0xffffff;
8247 if (imm == 0x123456) {
8248 break;
8251 return false;
8252 case EXCP_BKPT:
8253 /* See if this is a semihosting syscall. */
8254 if (env->thumb) {
8255 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
8256 & 0xff;
8257 if (imm == 0xab) {
8258 env->regs[15] += 2;
8259 break;
8262 return false;
8263 default:
8264 return false;
8267 qemu_log_mask(CPU_LOG_INT,
8268 "...handling as semihosting call 0x%x\n",
8269 env->regs[0]);
8270 env->regs[0] = do_arm_semihosting(env);
8271 return true;
8275 /* Handle a CPU exception for A and R profile CPUs.
8276 * Do any appropriate logging, handle PSCI calls, and then hand off
8277 * to the AArch64-entry or AArch32-entry function depending on the
8278 * target exception level's register width.
8280 void arm_cpu_do_interrupt(CPUState *cs)
8282 ARMCPU *cpu = ARM_CPU(cs);
8283 CPUARMState *env = &cpu->env;
8284 unsigned int new_el = env->exception.target_el;
8286 assert(!arm_feature(env, ARM_FEATURE_M));
8288 arm_log_exception(cs->exception_index);
8289 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
8290 new_el);
8291 if (qemu_loglevel_mask(CPU_LOG_INT)
8292 && !excp_is_internal(cs->exception_index)) {
8293 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
8294 env->exception.syndrome >> ARM_EL_EC_SHIFT,
8295 env->exception.syndrome);
8298 if (arm_is_psci_call(cpu, cs->exception_index)) {
8299 arm_handle_psci_call(cpu);
8300 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
8301 return;
8304 /* Semihosting semantics depend on the register width of the
8305 * code that caused the exception, not the target exception level,
8306 * so must be handled here.
8308 if (check_for_semihosting(cs)) {
8309 return;
8312 /* Hooks may change global state so BQL should be held, also the
8313 * BQL needs to be held for any modification of
8314 * cs->interrupt_request.
8316 g_assert(qemu_mutex_iothread_locked());
8318 arm_call_pre_el_change_hook(cpu);
8320 assert(!excp_is_internal(cs->exception_index));
8321 if (arm_el_is_aa64(env, new_el)) {
8322 arm_cpu_do_interrupt_aarch64(cs);
8323 } else {
8324 arm_cpu_do_interrupt_aarch32(cs);
8327 arm_call_el_change_hook(cpu);
8329 if (!kvm_enabled()) {
8330 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
8334 /* Return the exception level which controls this address translation regime */
8335 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
8337 switch (mmu_idx) {
8338 case ARMMMUIdx_S2NS:
8339 case ARMMMUIdx_S1E2:
8340 return 2;
8341 case ARMMMUIdx_S1E3:
8342 return 3;
8343 case ARMMMUIdx_S1SE0:
8344 return arm_el_is_aa64(env, 3) ? 1 : 3;
8345 case ARMMMUIdx_S1SE1:
8346 case ARMMMUIdx_S1NSE0:
8347 case ARMMMUIdx_S1NSE1:
8348 case ARMMMUIdx_MPrivNegPri:
8349 case ARMMMUIdx_MUserNegPri:
8350 case ARMMMUIdx_MPriv:
8351 case ARMMMUIdx_MUser:
8352 case ARMMMUIdx_MSPrivNegPri:
8353 case ARMMMUIdx_MSUserNegPri:
8354 case ARMMMUIdx_MSPriv:
8355 case ARMMMUIdx_MSUser:
8356 return 1;
8357 default:
8358 g_assert_not_reached();
8362 /* Return the SCTLR value which controls this address translation regime */
8363 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
8365 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
8368 /* Return true if the specified stage of address translation is disabled */
8369 static inline bool regime_translation_disabled(CPUARMState *env,
8370 ARMMMUIdx mmu_idx)
8372 if (arm_feature(env, ARM_FEATURE_M)) {
8373 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
8374 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
8375 case R_V7M_MPU_CTRL_ENABLE_MASK:
8376 /* Enabled, but not for HardFault and NMI */
8377 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
8378 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
8379 /* Enabled for all cases */
8380 return false;
8381 case 0:
8382 default:
8383 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
8384 * we warned about that in armv7m_nvic.c when the guest set it.
8386 return true;
8390 if (mmu_idx == ARMMMUIdx_S2NS) {
8391 return (env->cp15.hcr_el2 & HCR_VM) == 0;
8393 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
8396 static inline bool regime_translation_big_endian(CPUARMState *env,
8397 ARMMMUIdx mmu_idx)
8399 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
8402 /* Return the TCR controlling this translation regime */
8403 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
8405 if (mmu_idx == ARMMMUIdx_S2NS) {
8406 return &env->cp15.vtcr_el2;
8408 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
8411 /* Convert a possible stage1+2 MMU index into the appropriate
8412 * stage 1 MMU index
8414 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
8416 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
8417 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
8419 return mmu_idx;
8422 /* Returns TBI0 value for current regime el */
8423 uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx)
8425 TCR *tcr;
8426 uint32_t el;
8428 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8429 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8431 mmu_idx = stage_1_mmu_idx(mmu_idx);
8433 tcr = regime_tcr(env, mmu_idx);
8434 el = regime_el(env, mmu_idx);
8436 if (el > 1) {
8437 return extract64(tcr->raw_tcr, 20, 1);
8438 } else {
8439 return extract64(tcr->raw_tcr, 37, 1);
8443 /* Returns TBI1 value for current regime el */
8444 uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx)
8446 TCR *tcr;
8447 uint32_t el;
8449 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert
8450 * a stage 1+2 mmu index into the appropriate stage 1 mmu index.
8452 mmu_idx = stage_1_mmu_idx(mmu_idx);
8454 tcr = regime_tcr(env, mmu_idx);
8455 el = regime_el(env, mmu_idx);
8457 if (el > 1) {
8458 return 0;
8459 } else {
8460 return extract64(tcr->raw_tcr, 38, 1);
8464 /* Return the TTBR associated with this translation regime */
8465 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
8466 int ttbrn)
8468 if (mmu_idx == ARMMMUIdx_S2NS) {
8469 return env->cp15.vttbr_el2;
8471 if (ttbrn == 0) {
8472 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
8473 } else {
8474 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
8478 /* Return true if the translation regime is using LPAE format page tables */
8479 static inline bool regime_using_lpae_format(CPUARMState *env,
8480 ARMMMUIdx mmu_idx)
8482 int el = regime_el(env, mmu_idx);
8483 if (el == 2 || arm_el_is_aa64(env, el)) {
8484 return true;
8486 if (arm_feature(env, ARM_FEATURE_LPAE)
8487 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
8488 return true;
8490 return false;
8493 /* Returns true if the stage 1 translation regime is using LPAE format page
8494 * tables. Used when raising alignment exceptions, whose FSR changes depending
8495 * on whether the long or short descriptor format is in use. */
8496 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
8498 mmu_idx = stage_1_mmu_idx(mmu_idx);
8500 return regime_using_lpae_format(env, mmu_idx);
8503 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
8505 switch (mmu_idx) {
8506 case ARMMMUIdx_S1SE0:
8507 case ARMMMUIdx_S1NSE0:
8508 case ARMMMUIdx_MUser:
8509 case ARMMMUIdx_MSUser:
8510 case ARMMMUIdx_MUserNegPri:
8511 case ARMMMUIdx_MSUserNegPri:
8512 return true;
8513 default:
8514 return false;
8515 case ARMMMUIdx_S12NSE0:
8516 case ARMMMUIdx_S12NSE1:
8517 g_assert_not_reached();
8521 /* Translate section/page access permissions to page
8522 * R/W protection flags
8524 * @env: CPUARMState
8525 * @mmu_idx: MMU index indicating required translation regime
8526 * @ap: The 3-bit access permissions (AP[2:0])
8527 * @domain_prot: The 2-bit domain access permissions
8529 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8530 int ap, int domain_prot)
8532 bool is_user = regime_is_user(env, mmu_idx);
8534 if (domain_prot == 3) {
8535 return PAGE_READ | PAGE_WRITE;
8538 switch (ap) {
8539 case 0:
8540 if (arm_feature(env, ARM_FEATURE_V7)) {
8541 return 0;
8543 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8544 case SCTLR_S:
8545 return is_user ? 0 : PAGE_READ;
8546 case SCTLR_R:
8547 return PAGE_READ;
8548 default:
8549 return 0;
8551 case 1:
8552 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8553 case 2:
8554 if (is_user) {
8555 return PAGE_READ;
8556 } else {
8557 return PAGE_READ | PAGE_WRITE;
8559 case 3:
8560 return PAGE_READ | PAGE_WRITE;
8561 case 4: /* Reserved. */
8562 return 0;
8563 case 5:
8564 return is_user ? 0 : PAGE_READ;
8565 case 6:
8566 return PAGE_READ;
8567 case 7:
8568 if (!arm_feature(env, ARM_FEATURE_V6K)) {
8569 return 0;
8571 return PAGE_READ;
8572 default:
8573 g_assert_not_reached();
8577 /* Translate section/page access permissions to page
8578 * R/W protection flags.
8580 * @ap: The 2-bit simple AP (AP[2:1])
8581 * @is_user: TRUE if accessing from PL0
8583 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
8585 switch (ap) {
8586 case 0:
8587 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8588 case 1:
8589 return PAGE_READ | PAGE_WRITE;
8590 case 2:
8591 return is_user ? 0 : PAGE_READ;
8592 case 3:
8593 return PAGE_READ;
8594 default:
8595 g_assert_not_reached();
8599 static inline int
8600 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8602 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8605 /* Translate S2 section/page access permissions to protection flags
8607 * @env: CPUARMState
8608 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8609 * @xn: XN (execute-never) bit
8611 static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8613 int prot = 0;
8615 if (s2ap & 1) {
8616 prot |= PAGE_READ;
8618 if (s2ap & 2) {
8619 prot |= PAGE_WRITE;
8621 if (!xn) {
8622 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8623 prot |= PAGE_EXEC;
8626 return prot;
8629 /* Translate section/page access permissions to protection flags
8631 * @env: CPUARMState
8632 * @mmu_idx: MMU index indicating required translation regime
8633 * @is_aa64: TRUE if AArch64
8634 * @ap: The 2-bit simple AP (AP[2:1])
8635 * @ns: NS (non-secure) bit
8636 * @xn: XN (execute-never) bit
8637 * @pxn: PXN (privileged execute-never) bit
8639 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8640 int ap, int ns, int xn, int pxn)
8642 bool is_user = regime_is_user(env, mmu_idx);
8643 int prot_rw, user_rw;
8644 bool have_wxn;
8645 int wxn = 0;
8647 assert(mmu_idx != ARMMMUIdx_S2NS);
8649 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
8650 if (is_user) {
8651 prot_rw = user_rw;
8652 } else {
8653 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
8656 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
8657 return prot_rw;
8660 /* TODO have_wxn should be replaced with
8661 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
8662 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
8663 * compatible processors have EL2, which is required for [U]WXN.
8665 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
8667 if (have_wxn) {
8668 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
8671 if (is_aa64) {
8672 switch (regime_el(env, mmu_idx)) {
8673 case 1:
8674 if (!is_user) {
8675 xn = pxn || (user_rw & PAGE_WRITE);
8677 break;
8678 case 2:
8679 case 3:
8680 break;
8682 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8683 switch (regime_el(env, mmu_idx)) {
8684 case 1:
8685 case 3:
8686 if (is_user) {
8687 xn = xn || !(user_rw & PAGE_READ);
8688 } else {
8689 int uwxn = 0;
8690 if (have_wxn) {
8691 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
8693 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
8694 (uwxn && (user_rw & PAGE_WRITE));
8696 break;
8697 case 2:
8698 break;
8700 } else {
8701 xn = wxn = 0;
8704 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
8705 return prot_rw;
8707 return prot_rw | PAGE_EXEC;
8710 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
8711 uint32_t *table, uint32_t address)
8713 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
8714 TCR *tcr = regime_tcr(env, mmu_idx);
8716 if (address & tcr->mask) {
8717 if (tcr->raw_tcr & TTBCR_PD1) {
8718 /* Translation table walk disabled for TTBR1 */
8719 return false;
8721 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
8722 } else {
8723 if (tcr->raw_tcr & TTBCR_PD0) {
8724 /* Translation table walk disabled for TTBR0 */
8725 return false;
8727 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
8729 *table |= (address >> 18) & 0x3ffc;
8730 return true;
8733 /* Translate a S1 pagetable walk through S2 if needed. */
8734 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
8735 hwaddr addr, MemTxAttrs txattrs,
8736 ARMMMUFaultInfo *fi)
8738 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
8739 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
8740 target_ulong s2size;
8741 hwaddr s2pa;
8742 int s2prot;
8743 int ret;
8745 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
8746 &txattrs, &s2prot, &s2size, fi, NULL);
8747 if (ret) {
8748 assert(fi->type != ARMFault_None);
8749 fi->s2addr = addr;
8750 fi->stage2 = true;
8751 fi->s1ptw = true;
8752 return ~0;
8754 addr = s2pa;
8756 return addr;
8759 /* All loads done in the course of a page table walk go through here. */
8760 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8761 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
8763 ARMCPU *cpu = ARM_CPU(cs);
8764 CPUARMState *env = &cpu->env;
8765 MemTxAttrs attrs = {};
8766 MemTxResult result = MEMTX_OK;
8767 AddressSpace *as;
8768 uint32_t data;
8770 attrs.secure = is_secure;
8771 as = arm_addressspace(cs, attrs);
8772 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
8773 if (fi->s1ptw) {
8774 return 0;
8776 if (regime_translation_big_endian(env, mmu_idx)) {
8777 data = address_space_ldl_be(as, addr, attrs, &result);
8778 } else {
8779 data = address_space_ldl_le(as, addr, attrs, &result);
8781 if (result == MEMTX_OK) {
8782 return data;
8784 fi->type = ARMFault_SyncExternalOnWalk;
8785 fi->ea = arm_extabort_type(result);
8786 return 0;
8789 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8790 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
8792 ARMCPU *cpu = ARM_CPU(cs);
8793 CPUARMState *env = &cpu->env;
8794 MemTxAttrs attrs = {};
8795 MemTxResult result = MEMTX_OK;
8796 AddressSpace *as;
8797 uint64_t data;
8799 attrs.secure = is_secure;
8800 as = arm_addressspace(cs, attrs);
8801 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
8802 if (fi->s1ptw) {
8803 return 0;
8805 if (regime_translation_big_endian(env, mmu_idx)) {
8806 data = address_space_ldq_be(as, addr, attrs, &result);
8807 } else {
8808 data = address_space_ldq_le(as, addr, attrs, &result);
8810 if (result == MEMTX_OK) {
8811 return data;
8813 fi->type = ARMFault_SyncExternalOnWalk;
8814 fi->ea = arm_extabort_type(result);
8815 return 0;
8818 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
8819 MMUAccessType access_type, ARMMMUIdx mmu_idx,
8820 hwaddr *phys_ptr, int *prot,
8821 target_ulong *page_size,
8822 ARMMMUFaultInfo *fi)
8824 CPUState *cs = CPU(arm_env_get_cpu(env));
8825 int level = 1;
8826 uint32_t table;
8827 uint32_t desc;
8828 int type;
8829 int ap;
8830 int domain = 0;
8831 int domain_prot;
8832 hwaddr phys_addr;
8833 uint32_t dacr;
8835 /* Pagetable walk. */
8836 /* Lookup l1 descriptor. */
8837 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
8838 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8839 fi->type = ARMFault_Translation;
8840 goto do_fault;
8842 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8843 mmu_idx, fi);
8844 if (fi->type != ARMFault_None) {
8845 goto do_fault;
8847 type = (desc & 3);
8848 domain = (desc >> 5) & 0x0f;
8849 if (regime_el(env, mmu_idx) == 1) {
8850 dacr = env->cp15.dacr_ns;
8851 } else {
8852 dacr = env->cp15.dacr_s;
8854 domain_prot = (dacr >> (domain * 2)) & 3;
8855 if (type == 0) {
8856 /* Section translation fault. */
8857 fi->type = ARMFault_Translation;
8858 goto do_fault;
8860 if (type != 2) {
8861 level = 2;
8863 if (domain_prot == 0 || domain_prot == 2) {
8864 fi->type = ARMFault_Domain;
8865 goto do_fault;
8867 if (type == 2) {
8868 /* 1Mb section. */
8869 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8870 ap = (desc >> 10) & 3;
8871 *page_size = 1024 * 1024;
8872 } else {
8873 /* Lookup l2 entry. */
8874 if (type == 1) {
8875 /* Coarse pagetable. */
8876 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8877 } else {
8878 /* Fine pagetable. */
8879 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
8881 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8882 mmu_idx, fi);
8883 if (fi->type != ARMFault_None) {
8884 goto do_fault;
8886 switch (desc & 3) {
8887 case 0: /* Page translation fault. */
8888 fi->type = ARMFault_Translation;
8889 goto do_fault;
8890 case 1: /* 64k page. */
8891 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8892 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
8893 *page_size = 0x10000;
8894 break;
8895 case 2: /* 4k page. */
8896 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8897 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
8898 *page_size = 0x1000;
8899 break;
8900 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
8901 if (type == 1) {
8902 /* ARMv6/XScale extended small page format */
8903 if (arm_feature(env, ARM_FEATURE_XSCALE)
8904 || arm_feature(env, ARM_FEATURE_V6)) {
8905 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8906 *page_size = 0x1000;
8907 } else {
8908 /* UNPREDICTABLE in ARMv5; we choose to take a
8909 * page translation fault.
8911 fi->type = ARMFault_Translation;
8912 goto do_fault;
8914 } else {
8915 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
8916 *page_size = 0x400;
8918 ap = (desc >> 4) & 3;
8919 break;
8920 default:
8921 /* Never happens, but compiler isn't smart enough to tell. */
8922 abort();
8925 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8926 *prot |= *prot ? PAGE_EXEC : 0;
8927 if (!(*prot & (1 << access_type))) {
8928 /* Access permission fault. */
8929 fi->type = ARMFault_Permission;
8930 goto do_fault;
8932 *phys_ptr = phys_addr;
8933 return false;
8934 do_fault:
8935 fi->domain = domain;
8936 fi->level = level;
8937 return true;
8940 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
8941 MMUAccessType access_type, ARMMMUIdx mmu_idx,
8942 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
8943 target_ulong *page_size, ARMMMUFaultInfo *fi)
8945 CPUState *cs = CPU(arm_env_get_cpu(env));
8946 int level = 1;
8947 uint32_t table;
8948 uint32_t desc;
8949 uint32_t xn;
8950 uint32_t pxn = 0;
8951 int type;
8952 int ap;
8953 int domain = 0;
8954 int domain_prot;
8955 hwaddr phys_addr;
8956 uint32_t dacr;
8957 bool ns;
8959 /* Pagetable walk. */
8960 /* Lookup l1 descriptor. */
8961 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
8962 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8963 fi->type = ARMFault_Translation;
8964 goto do_fault;
8966 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8967 mmu_idx, fi);
8968 if (fi->type != ARMFault_None) {
8969 goto do_fault;
8971 type = (desc & 3);
8972 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
8973 /* Section translation fault, or attempt to use the encoding
8974 * which is Reserved on implementations without PXN.
8976 fi->type = ARMFault_Translation;
8977 goto do_fault;
8979 if ((type == 1) || !(desc & (1 << 18))) {
8980 /* Page or Section. */
8981 domain = (desc >> 5) & 0x0f;
8983 if (regime_el(env, mmu_idx) == 1) {
8984 dacr = env->cp15.dacr_ns;
8985 } else {
8986 dacr = env->cp15.dacr_s;
8988 if (type == 1) {
8989 level = 2;
8991 domain_prot = (dacr >> (domain * 2)) & 3;
8992 if (domain_prot == 0 || domain_prot == 2) {
8993 /* Section or Page domain fault */
8994 fi->type = ARMFault_Domain;
8995 goto do_fault;
8997 if (type != 1) {
8998 if (desc & (1 << 18)) {
8999 /* Supersection. */
9000 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
9001 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
9002 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
9003 *page_size = 0x1000000;
9004 } else {
9005 /* Section. */
9006 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
9007 *page_size = 0x100000;
9009 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
9010 xn = desc & (1 << 4);
9011 pxn = desc & 1;
9012 ns = extract32(desc, 19, 1);
9013 } else {
9014 if (arm_feature(env, ARM_FEATURE_PXN)) {
9015 pxn = (desc >> 2) & 1;
9017 ns = extract32(desc, 3, 1);
9018 /* Lookup l2 entry. */
9019 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
9020 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
9021 mmu_idx, fi);
9022 if (fi->type != ARMFault_None) {
9023 goto do_fault;
9025 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
9026 switch (desc & 3) {
9027 case 0: /* Page translation fault. */
9028 fi->type = ARMFault_Translation;
9029 goto do_fault;
9030 case 1: /* 64k page. */
9031 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
9032 xn = desc & (1 << 15);
9033 *page_size = 0x10000;
9034 break;
9035 case 2: case 3: /* 4k page. */
9036 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
9037 xn = desc & 1;
9038 *page_size = 0x1000;
9039 break;
9040 default:
9041 /* Never happens, but compiler isn't smart enough to tell. */
9042 abort();
9045 if (domain_prot == 3) {
9046 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9047 } else {
9048 if (pxn && !regime_is_user(env, mmu_idx)) {
9049 xn = 1;
9051 if (xn && access_type == MMU_INST_FETCH) {
9052 fi->type = ARMFault_Permission;
9053 goto do_fault;
9056 if (arm_feature(env, ARM_FEATURE_V6K) &&
9057 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
9058 /* The simplified model uses AP[0] as an access control bit. */
9059 if ((ap & 1) == 0) {
9060 /* Access flag fault. */
9061 fi->type = ARMFault_AccessFlag;
9062 goto do_fault;
9064 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
9065 } else {
9066 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
9068 if (*prot && !xn) {
9069 *prot |= PAGE_EXEC;
9071 if (!(*prot & (1 << access_type))) {
9072 /* Access permission fault. */
9073 fi->type = ARMFault_Permission;
9074 goto do_fault;
9077 if (ns) {
9078 /* The NS bit will (as required by the architecture) have no effect if
9079 * the CPU doesn't support TZ or this is a non-secure translation
9080 * regime, because the attribute will already be non-secure.
9082 attrs->secure = false;
9084 *phys_ptr = phys_addr;
9085 return false;
9086 do_fault:
9087 fi->domain = domain;
9088 fi->level = level;
9089 return true;
9093 * check_s2_mmu_setup
9094 * @cpu: ARMCPU
9095 * @is_aa64: True if the translation regime is in AArch64 state
9096 * @startlevel: Suggested starting level
9097 * @inputsize: Bitsize of IPAs
9098 * @stride: Page-table stride (See the ARM ARM)
9100 * Returns true if the suggested S2 translation parameters are OK and
9101 * false otherwise.
9103 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
9104 int inputsize, int stride)
9106 const int grainsize = stride + 3;
9107 int startsizecheck;
9109 /* Negative levels are never allowed. */
9110 if (level < 0) {
9111 return false;
9114 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
9115 if (startsizecheck < 1 || startsizecheck > stride + 4) {
9116 return false;
9119 if (is_aa64) {
9120 CPUARMState *env = &cpu->env;
9121 unsigned int pamax = arm_pamax(cpu);
9123 switch (stride) {
9124 case 13: /* 64KB Pages. */
9125 if (level == 0 || (level == 1 && pamax <= 42)) {
9126 return false;
9128 break;
9129 case 11: /* 16KB Pages. */
9130 if (level == 0 || (level == 1 && pamax <= 40)) {
9131 return false;
9133 break;
9134 case 9: /* 4KB Pages. */
9135 if (level == 0 && pamax <= 42) {
9136 return false;
9138 break;
9139 default:
9140 g_assert_not_reached();
9143 /* Inputsize checks. */
9144 if (inputsize > pamax &&
9145 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
9146 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
9147 return false;
9149 } else {
9150 /* AArch32 only supports 4KB pages. Assert on that. */
9151 assert(stride == 9);
9153 if (level == 0) {
9154 return false;
9157 return true;
9160 /* Translate from the 4-bit stage 2 representation of
9161 * memory attributes (without cache-allocation hints) to
9162 * the 8-bit representation of the stage 1 MAIR registers
9163 * (which includes allocation hints).
9165 * ref: shared/translation/attrs/S2AttrDecode()
9166 * .../S2ConvertAttrsHints()
9168 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
9170 uint8_t hiattr = extract32(s2attrs, 2, 2);
9171 uint8_t loattr = extract32(s2attrs, 0, 2);
9172 uint8_t hihint = 0, lohint = 0;
9174 if (hiattr != 0) { /* normal memory */
9175 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
9176 hiattr = loattr = 1; /* non-cacheable */
9177 } else {
9178 if (hiattr != 1) { /* Write-through or write-back */
9179 hihint = 3; /* RW allocate */
9181 if (loattr != 1) { /* Write-through or write-back */
9182 lohint = 3; /* RW allocate */
9187 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
9190 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
9191 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9192 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
9193 target_ulong *page_size_ptr,
9194 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9196 ARMCPU *cpu = arm_env_get_cpu(env);
9197 CPUState *cs = CPU(cpu);
9198 /* Read an LPAE long-descriptor translation table. */
9199 ARMFaultType fault_type = ARMFault_Translation;
9200 uint32_t level;
9201 uint32_t epd = 0;
9202 int32_t t0sz, t1sz;
9203 uint32_t tg;
9204 uint64_t ttbr;
9205 int ttbr_select;
9206 hwaddr descaddr, indexmask, indexmask_grainsize;
9207 uint32_t tableattrs;
9208 target_ulong page_size;
9209 uint32_t attrs;
9210 int32_t stride = 9;
9211 int32_t addrsize;
9212 int inputsize;
9213 int32_t tbi = 0;
9214 TCR *tcr = regime_tcr(env, mmu_idx);
9215 int ap, ns, xn, pxn;
9216 uint32_t el = regime_el(env, mmu_idx);
9217 bool ttbr1_valid = true;
9218 uint64_t descaddrmask;
9219 bool aarch64 = arm_el_is_aa64(env, el);
9221 /* TODO:
9222 * This code does not handle the different format TCR for VTCR_EL2.
9223 * This code also does not support shareability levels.
9224 * Attribute and permission bit handling should also be checked when adding
9225 * support for those page table walks.
9227 if (aarch64) {
9228 level = 0;
9229 addrsize = 64;
9230 if (el > 1) {
9231 if (mmu_idx != ARMMMUIdx_S2NS) {
9232 tbi = extract64(tcr->raw_tcr, 20, 1);
9234 } else {
9235 if (extract64(address, 55, 1)) {
9236 tbi = extract64(tcr->raw_tcr, 38, 1);
9237 } else {
9238 tbi = extract64(tcr->raw_tcr, 37, 1);
9241 tbi *= 8;
9243 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
9244 * invalid.
9246 if (el > 1) {
9247 ttbr1_valid = false;
9249 } else {
9250 level = 1;
9251 addrsize = 32;
9252 /* There is no TTBR1 for EL2 */
9253 if (el == 2) {
9254 ttbr1_valid = false;
9258 /* Determine whether this address is in the region controlled by
9259 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
9260 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
9261 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
9263 if (aarch64) {
9264 /* AArch64 translation. */
9265 t0sz = extract32(tcr->raw_tcr, 0, 6);
9266 t0sz = MIN(t0sz, 39);
9267 t0sz = MAX(t0sz, 16);
9268 } else if (mmu_idx != ARMMMUIdx_S2NS) {
9269 /* AArch32 stage 1 translation. */
9270 t0sz = extract32(tcr->raw_tcr, 0, 3);
9271 } else {
9272 /* AArch32 stage 2 translation. */
9273 bool sext = extract32(tcr->raw_tcr, 4, 1);
9274 bool sign = extract32(tcr->raw_tcr, 3, 1);
9275 /* Address size is 40-bit for a stage 2 translation,
9276 * and t0sz can be negative (from -8 to 7),
9277 * so we need to adjust it to use the TTBR selecting logic below.
9279 addrsize = 40;
9280 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8;
9282 /* If the sign-extend bit is not the same as t0sz[3], the result
9283 * is unpredictable. Flag this as a guest error. */
9284 if (sign != sext) {
9285 qemu_log_mask(LOG_GUEST_ERROR,
9286 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
9289 t1sz = extract32(tcr->raw_tcr, 16, 6);
9290 if (aarch64) {
9291 t1sz = MIN(t1sz, 39);
9292 t1sz = MAX(t1sz, 16);
9294 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) {
9295 /* there is a ttbr0 region and we are in it (high bits all zero) */
9296 ttbr_select = 0;
9297 } else if (ttbr1_valid && t1sz &&
9298 !extract64(~address, addrsize - t1sz, t1sz - tbi)) {
9299 /* there is a ttbr1 region and we are in it (high bits all one) */
9300 ttbr_select = 1;
9301 } else if (!t0sz) {
9302 /* ttbr0 region is "everything not in the ttbr1 region" */
9303 ttbr_select = 0;
9304 } else if (!t1sz && ttbr1_valid) {
9305 /* ttbr1 region is "everything not in the ttbr0 region" */
9306 ttbr_select = 1;
9307 } else {
9308 /* in the gap between the two regions, this is a Translation fault */
9309 fault_type = ARMFault_Translation;
9310 goto do_fault;
9313 /* Note that QEMU ignores shareability and cacheability attributes,
9314 * so we don't need to do anything with the SH, ORGN, IRGN fields
9315 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
9316 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
9317 * implement any ASID-like capability so we can ignore it (instead
9318 * we will always flush the TLB any time the ASID is changed).
9320 if (ttbr_select == 0) {
9321 ttbr = regime_ttbr(env, mmu_idx, 0);
9322 if (el < 2) {
9323 epd = extract32(tcr->raw_tcr, 7, 1);
9325 inputsize = addrsize - t0sz;
9327 tg = extract32(tcr->raw_tcr, 14, 2);
9328 if (tg == 1) { /* 64KB pages */
9329 stride = 13;
9331 if (tg == 2) { /* 16KB pages */
9332 stride = 11;
9334 } else {
9335 /* We should only be here if TTBR1 is valid */
9336 assert(ttbr1_valid);
9338 ttbr = regime_ttbr(env, mmu_idx, 1);
9339 epd = extract32(tcr->raw_tcr, 23, 1);
9340 inputsize = addrsize - t1sz;
9342 tg = extract32(tcr->raw_tcr, 30, 2);
9343 if (tg == 3) { /* 64KB pages */
9344 stride = 13;
9346 if (tg == 1) { /* 16KB pages */
9347 stride = 11;
9351 /* Here we should have set up all the parameters for the translation:
9352 * inputsize, ttbr, epd, stride, tbi
9355 if (epd) {
9356 /* Translation table walk disabled => Translation fault on TLB miss
9357 * Note: This is always 0 on 64-bit EL2 and EL3.
9359 goto do_fault;
9362 if (mmu_idx != ARMMMUIdx_S2NS) {
9363 /* The starting level depends on the virtual address size (which can
9364 * be up to 48 bits) and the translation granule size. It indicates
9365 * the number of strides (stride bits at a time) needed to
9366 * consume the bits of the input address. In the pseudocode this is:
9367 * level = 4 - RoundUp((inputsize - grainsize) / stride)
9368 * where their 'inputsize' is our 'inputsize', 'grainsize' is
9369 * our 'stride + 3' and 'stride' is our 'stride'.
9370 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
9371 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
9372 * = 4 - (inputsize - 4) / stride;
9374 level = 4 - (inputsize - 4) / stride;
9375 } else {
9376 /* For stage 2 translations the starting level is specified by the
9377 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
9379 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
9380 uint32_t startlevel;
9381 bool ok;
9383 if (!aarch64 || stride == 9) {
9384 /* AArch32 or 4KB pages */
9385 startlevel = 2 - sl0;
9386 } else {
9387 /* 16KB or 64KB pages */
9388 startlevel = 3 - sl0;
9391 /* Check that the starting level is valid. */
9392 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
9393 inputsize, stride);
9394 if (!ok) {
9395 fault_type = ARMFault_Translation;
9396 goto do_fault;
9398 level = startlevel;
9401 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
9402 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
9404 /* Now we can extract the actual base address from the TTBR */
9405 descaddr = extract64(ttbr, 0, 48);
9406 descaddr &= ~indexmask;
9408 /* The address field in the descriptor goes up to bit 39 for ARMv7
9409 * but up to bit 47 for ARMv8, but we use the descaddrmask
9410 * up to bit 39 for AArch32, because we don't need other bits in that case
9411 * to construct next descriptor address (anyway they should be all zeroes).
9413 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
9414 ~indexmask_grainsize;
9416 /* Secure accesses start with the page table in secure memory and
9417 * can be downgraded to non-secure at any step. Non-secure accesses
9418 * remain non-secure. We implement this by just ORing in the NSTable/NS
9419 * bits at each step.
9421 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
9422 for (;;) {
9423 uint64_t descriptor;
9424 bool nstable;
9426 descaddr |= (address >> (stride * (4 - level))) & indexmask;
9427 descaddr &= ~7ULL;
9428 nstable = extract32(tableattrs, 4, 1);
9429 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
9430 if (fi->type != ARMFault_None) {
9431 goto do_fault;
9434 if (!(descriptor & 1) ||
9435 (!(descriptor & 2) && (level == 3))) {
9436 /* Invalid, or the Reserved level 3 encoding */
9437 goto do_fault;
9439 descaddr = descriptor & descaddrmask;
9441 if ((descriptor & 2) && (level < 3)) {
9442 /* Table entry. The top five bits are attributes which may
9443 * propagate down through lower levels of the table (and
9444 * which are all arranged so that 0 means "no effect", so
9445 * we can gather them up by ORing in the bits at each level).
9447 tableattrs |= extract64(descriptor, 59, 5);
9448 level++;
9449 indexmask = indexmask_grainsize;
9450 continue;
9452 /* Block entry at level 1 or 2, or page entry at level 3.
9453 * These are basically the same thing, although the number
9454 * of bits we pull in from the vaddr varies.
9456 page_size = (1ULL << ((stride * (4 - level)) + 3));
9457 descaddr |= (address & (page_size - 1));
9458 /* Extract attributes from the descriptor */
9459 attrs = extract64(descriptor, 2, 10)
9460 | (extract64(descriptor, 52, 12) << 10);
9462 if (mmu_idx == ARMMMUIdx_S2NS) {
9463 /* Stage 2 table descriptors do not include any attribute fields */
9464 break;
9466 /* Merge in attributes from table descriptors */
9467 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
9468 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
9469 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
9470 * means "force PL1 access only", which means forcing AP[1] to 0.
9472 if (extract32(tableattrs, 2, 1)) {
9473 attrs &= ~(1 << 4);
9475 attrs |= nstable << 3; /* NS */
9476 break;
9478 /* Here descaddr is the final physical address, and attributes
9479 * are all in attrs.
9481 fault_type = ARMFault_AccessFlag;
9482 if ((attrs & (1 << 8)) == 0) {
9483 /* Access flag */
9484 goto do_fault;
9487 ap = extract32(attrs, 4, 2);
9488 xn = extract32(attrs, 12, 1);
9490 if (mmu_idx == ARMMMUIdx_S2NS) {
9491 ns = true;
9492 *prot = get_S2prot(env, ap, xn);
9493 } else {
9494 ns = extract32(attrs, 3, 1);
9495 pxn = extract32(attrs, 11, 1);
9496 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
9499 fault_type = ARMFault_Permission;
9500 if (!(*prot & (1 << access_type))) {
9501 goto do_fault;
9504 if (ns) {
9505 /* The NS bit will (as required by the architecture) have no effect if
9506 * the CPU doesn't support TZ or this is a non-secure translation
9507 * regime, because the attribute will already be non-secure.
9509 txattrs->secure = false;
9512 if (cacheattrs != NULL) {
9513 if (mmu_idx == ARMMMUIdx_S2NS) {
9514 cacheattrs->attrs = convert_stage2_attrs(env,
9515 extract32(attrs, 0, 4));
9516 } else {
9517 /* Index into MAIR registers for cache attributes */
9518 uint8_t attrindx = extract32(attrs, 0, 3);
9519 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
9520 assert(attrindx <= 7);
9521 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
9523 cacheattrs->shareability = extract32(attrs, 6, 2);
9526 *phys_ptr = descaddr;
9527 *page_size_ptr = page_size;
9528 return false;
9530 do_fault:
9531 fi->type = fault_type;
9532 fi->level = level;
9533 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
9534 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
9535 return true;
9538 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
9539 ARMMMUIdx mmu_idx,
9540 int32_t address, int *prot)
9542 if (!arm_feature(env, ARM_FEATURE_M)) {
9543 *prot = PAGE_READ | PAGE_WRITE;
9544 switch (address) {
9545 case 0xF0000000 ... 0xFFFFFFFF:
9546 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9547 /* hivecs execing is ok */
9548 *prot |= PAGE_EXEC;
9550 break;
9551 case 0x00000000 ... 0x7FFFFFFF:
9552 *prot |= PAGE_EXEC;
9553 break;
9555 } else {
9556 /* Default system address map for M profile cores.
9557 * The architecture specifies which regions are execute-never;
9558 * at the MPU level no other checks are defined.
9560 switch (address) {
9561 case 0x00000000 ... 0x1fffffff: /* ROM */
9562 case 0x20000000 ... 0x3fffffff: /* SRAM */
9563 case 0x60000000 ... 0x7fffffff: /* RAM */
9564 case 0x80000000 ... 0x9fffffff: /* RAM */
9565 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9566 break;
9567 case 0x40000000 ... 0x5fffffff: /* Peripheral */
9568 case 0xa0000000 ... 0xbfffffff: /* Device */
9569 case 0xc0000000 ... 0xdfffffff: /* Device */
9570 case 0xe0000000 ... 0xffffffff: /* System */
9571 *prot = PAGE_READ | PAGE_WRITE;
9572 break;
9573 default:
9574 g_assert_not_reached();
9579 static bool pmsav7_use_background_region(ARMCPU *cpu,
9580 ARMMMUIdx mmu_idx, bool is_user)
9582 /* Return true if we should use the default memory map as a
9583 * "background" region if there are no hits against any MPU regions.
9585 CPUARMState *env = &cpu->env;
9587 if (is_user) {
9588 return false;
9591 if (arm_feature(env, ARM_FEATURE_M)) {
9592 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
9593 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
9594 } else {
9595 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
9599 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
9601 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
9602 return arm_feature(env, ARM_FEATURE_M) &&
9603 extract32(address, 20, 12) == 0xe00;
9606 static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
9608 /* True if address is in the M profile system region
9609 * 0xe0000000 - 0xffffffff
9611 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
9614 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
9615 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9616 hwaddr *phys_ptr, int *prot,
9617 target_ulong *page_size,
9618 ARMMMUFaultInfo *fi)
9620 ARMCPU *cpu = arm_env_get_cpu(env);
9621 int n;
9622 bool is_user = regime_is_user(env, mmu_idx);
9624 *phys_ptr = address;
9625 *page_size = TARGET_PAGE_SIZE;
9626 *prot = 0;
9628 if (regime_translation_disabled(env, mmu_idx) ||
9629 m_is_ppb_region(env, address)) {
9630 /* MPU disabled or M profile PPB access: use default memory map.
9631 * The other case which uses the default memory map in the
9632 * v7M ARM ARM pseudocode is exception vector reads from the vector
9633 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
9634 * which always does a direct read using address_space_ldl(), rather
9635 * than going via this function, so we don't need to check that here.
9637 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9638 } else { /* MPU enabled */
9639 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9640 /* region search */
9641 uint32_t base = env->pmsav7.drbar[n];
9642 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
9643 uint32_t rmask;
9644 bool srdis = false;
9646 if (!(env->pmsav7.drsr[n] & 0x1)) {
9647 continue;
9650 if (!rsize) {
9651 qemu_log_mask(LOG_GUEST_ERROR,
9652 "DRSR[%d]: Rsize field cannot be 0\n", n);
9653 continue;
9655 rsize++;
9656 rmask = (1ull << rsize) - 1;
9658 if (base & rmask) {
9659 qemu_log_mask(LOG_GUEST_ERROR,
9660 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
9661 "to DRSR region size, mask = 0x%" PRIx32 "\n",
9662 n, base, rmask);
9663 continue;
9666 if (address < base || address > base + rmask) {
9668 * Address not in this region. We must check whether the
9669 * region covers addresses in the same page as our address.
9670 * In that case we must not report a size that covers the
9671 * whole page for a subsequent hit against a different MPU
9672 * region or the background region, because it would result in
9673 * incorrect TLB hits for subsequent accesses to addresses that
9674 * are in this MPU region.
9676 if (ranges_overlap(base, rmask,
9677 address & TARGET_PAGE_MASK,
9678 TARGET_PAGE_SIZE)) {
9679 *page_size = 1;
9681 continue;
9684 /* Region matched */
9686 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
9687 int i, snd;
9688 uint32_t srdis_mask;
9690 rsize -= 3; /* sub region size (power of 2) */
9691 snd = ((address - base) >> rsize) & 0x7;
9692 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
9694 srdis_mask = srdis ? 0x3 : 0x0;
9695 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
9696 /* This will check in groups of 2, 4 and then 8, whether
9697 * the subregion bits are consistent. rsize is incremented
9698 * back up to give the region size, considering consistent
9699 * adjacent subregions as one region. Stop testing if rsize
9700 * is already big enough for an entire QEMU page.
9702 int snd_rounded = snd & ~(i - 1);
9703 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
9704 snd_rounded + 8, i);
9705 if (srdis_mask ^ srdis_multi) {
9706 break;
9708 srdis_mask = (srdis_mask << i) | srdis_mask;
9709 rsize++;
9712 if (srdis) {
9713 continue;
9715 if (rsize < TARGET_PAGE_BITS) {
9716 *page_size = 1 << rsize;
9718 break;
9721 if (n == -1) { /* no hits */
9722 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9723 /* background fault */
9724 fi->type = ARMFault_Background;
9725 return true;
9727 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9728 } else { /* a MPU hit! */
9729 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
9730 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
9732 if (m_is_system_region(env, address)) {
9733 /* System space is always execute never */
9734 xn = 1;
9737 if (is_user) { /* User mode AP bit decoding */
9738 switch (ap) {
9739 case 0:
9740 case 1:
9741 case 5:
9742 break; /* no access */
9743 case 3:
9744 *prot |= PAGE_WRITE;
9745 /* fall through */
9746 case 2:
9747 case 6:
9748 *prot |= PAGE_READ | PAGE_EXEC;
9749 break;
9750 case 7:
9751 /* for v7M, same as 6; for R profile a reserved value */
9752 if (arm_feature(env, ARM_FEATURE_M)) {
9753 *prot |= PAGE_READ | PAGE_EXEC;
9754 break;
9756 /* fall through */
9757 default:
9758 qemu_log_mask(LOG_GUEST_ERROR,
9759 "DRACR[%d]: Bad value for AP bits: 0x%"
9760 PRIx32 "\n", n, ap);
9762 } else { /* Priv. mode AP bits decoding */
9763 switch (ap) {
9764 case 0:
9765 break; /* no access */
9766 case 1:
9767 case 2:
9768 case 3:
9769 *prot |= PAGE_WRITE;
9770 /* fall through */
9771 case 5:
9772 case 6:
9773 *prot |= PAGE_READ | PAGE_EXEC;
9774 break;
9775 case 7:
9776 /* for v7M, same as 6; for R profile a reserved value */
9777 if (arm_feature(env, ARM_FEATURE_M)) {
9778 *prot |= PAGE_READ | PAGE_EXEC;
9779 break;
9781 /* fall through */
9782 default:
9783 qemu_log_mask(LOG_GUEST_ERROR,
9784 "DRACR[%d]: Bad value for AP bits: 0x%"
9785 PRIx32 "\n", n, ap);
9789 /* execute never */
9790 if (xn) {
9791 *prot &= ~PAGE_EXEC;
9796 fi->type = ARMFault_Permission;
9797 fi->level = 1;
9799 * Core QEMU code can't handle execution from small pages yet, so
9800 * don't try it. This way we'll get an MPU exception, rather than
9801 * eventually causing QEMU to exit in get_page_addr_code().
9803 if (*page_size < TARGET_PAGE_SIZE && (*prot & PAGE_EXEC)) {
9804 qemu_log_mask(LOG_UNIMP,
9805 "MPU: No support for execution from regions "
9806 "smaller than 1K\n");
9807 *prot &= ~PAGE_EXEC;
9809 return !(*prot & (1 << access_type));
9812 static bool v8m_is_sau_exempt(CPUARMState *env,
9813 uint32_t address, MMUAccessType access_type)
9815 /* The architecture specifies that certain address ranges are
9816 * exempt from v8M SAU/IDAU checks.
9818 return
9819 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
9820 (address >= 0xe0000000 && address <= 0xe0002fff) ||
9821 (address >= 0xe000e000 && address <= 0xe000efff) ||
9822 (address >= 0xe002e000 && address <= 0xe002efff) ||
9823 (address >= 0xe0040000 && address <= 0xe0041fff) ||
9824 (address >= 0xe00ff000 && address <= 0xe00fffff);
9827 static void v8m_security_lookup(CPUARMState *env, uint32_t address,
9828 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9829 V8M_SAttributes *sattrs)
9831 /* Look up the security attributes for this address. Compare the
9832 * pseudocode SecurityCheck() function.
9833 * We assume the caller has zero-initialized *sattrs.
9835 ARMCPU *cpu = arm_env_get_cpu(env);
9836 int r;
9837 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
9838 int idau_region = IREGION_NOTVALID;
9839 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
9840 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
9842 if (cpu->idau) {
9843 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
9844 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
9846 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
9847 &idau_nsc);
9850 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
9851 /* 0xf0000000..0xffffffff is always S for insn fetches */
9852 return;
9855 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
9856 sattrs->ns = !regime_is_secure(env, mmu_idx);
9857 return;
9860 if (idau_region != IREGION_NOTVALID) {
9861 sattrs->irvalid = true;
9862 sattrs->iregion = idau_region;
9865 switch (env->sau.ctrl & 3) {
9866 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
9867 break;
9868 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
9869 sattrs->ns = true;
9870 break;
9871 default: /* SAU.ENABLE == 1 */
9872 for (r = 0; r < cpu->sau_sregion; r++) {
9873 if (env->sau.rlar[r] & 1) {
9874 uint32_t base = env->sau.rbar[r] & ~0x1f;
9875 uint32_t limit = env->sau.rlar[r] | 0x1f;
9877 if (base <= address && limit >= address) {
9878 if (base > addr_page_base || limit < addr_page_limit) {
9879 sattrs->subpage = true;
9881 if (sattrs->srvalid) {
9882 /* If we hit in more than one region then we must report
9883 * as Secure, not NS-Callable, with no valid region
9884 * number info.
9886 sattrs->ns = false;
9887 sattrs->nsc = false;
9888 sattrs->sregion = 0;
9889 sattrs->srvalid = false;
9890 break;
9891 } else {
9892 if (env->sau.rlar[r] & 2) {
9893 sattrs->nsc = true;
9894 } else {
9895 sattrs->ns = true;
9897 sattrs->srvalid = true;
9898 sattrs->sregion = r;
9900 } else {
9902 * Address not in this region. We must check whether the
9903 * region covers addresses in the same page as our address.
9904 * In that case we must not report a size that covers the
9905 * whole page for a subsequent hit against a different MPU
9906 * region or the background region, because it would result
9907 * in incorrect TLB hits for subsequent accesses to
9908 * addresses that are in this MPU region.
9910 if (limit >= base &&
9911 ranges_overlap(base, limit - base + 1,
9912 addr_page_base,
9913 TARGET_PAGE_SIZE)) {
9914 sattrs->subpage = true;
9920 /* The IDAU will override the SAU lookup results if it specifies
9921 * higher security than the SAU does.
9923 if (!idau_ns) {
9924 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
9925 sattrs->ns = false;
9926 sattrs->nsc = idau_nsc;
9929 break;
9933 static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
9934 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9935 hwaddr *phys_ptr, MemTxAttrs *txattrs,
9936 int *prot, bool *is_subpage,
9937 ARMMMUFaultInfo *fi, uint32_t *mregion)
9939 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
9940 * that a full phys-to-virt translation does).
9941 * mregion is (if not NULL) set to the region number which matched,
9942 * or -1 if no region number is returned (MPU off, address did not
9943 * hit a region, address hit in multiple regions).
9944 * We set is_subpage to true if the region hit doesn't cover the
9945 * entire TARGET_PAGE the address is within.
9947 ARMCPU *cpu = arm_env_get_cpu(env);
9948 bool is_user = regime_is_user(env, mmu_idx);
9949 uint32_t secure = regime_is_secure(env, mmu_idx);
9950 int n;
9951 int matchregion = -1;
9952 bool hit = false;
9953 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
9954 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
9956 *is_subpage = false;
9957 *phys_ptr = address;
9958 *prot = 0;
9959 if (mregion) {
9960 *mregion = -1;
9963 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
9964 * was an exception vector read from the vector table (which is always
9965 * done using the default system address map), because those accesses
9966 * are done in arm_v7m_load_vector(), which always does a direct
9967 * read using address_space_ldl(), rather than going via this function.
9969 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
9970 hit = true;
9971 } else if (m_is_ppb_region(env, address)) {
9972 hit = true;
9973 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9974 hit = true;
9975 } else {
9976 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9977 /* region search */
9978 /* Note that the base address is bits [31:5] from the register
9979 * with bits [4:0] all zeroes, but the limit address is bits
9980 * [31:5] from the register with bits [4:0] all ones.
9982 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
9983 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
9985 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
9986 /* Region disabled */
9987 continue;
9990 if (address < base || address > limit) {
9992 * Address not in this region. We must check whether the
9993 * region covers addresses in the same page as our address.
9994 * In that case we must not report a size that covers the
9995 * whole page for a subsequent hit against a different MPU
9996 * region or the background region, because it would result in
9997 * incorrect TLB hits for subsequent accesses to addresses that
9998 * are in this MPU region.
10000 if (limit >= base &&
10001 ranges_overlap(base, limit - base + 1,
10002 addr_page_base,
10003 TARGET_PAGE_SIZE)) {
10004 *is_subpage = true;
10006 continue;
10009 if (base > addr_page_base || limit < addr_page_limit) {
10010 *is_subpage = true;
10013 if (hit) {
10014 /* Multiple regions match -- always a failure (unlike
10015 * PMSAv7 where highest-numbered-region wins)
10017 fi->type = ARMFault_Permission;
10018 fi->level = 1;
10019 return true;
10022 matchregion = n;
10023 hit = true;
10027 if (!hit) {
10028 /* background fault */
10029 fi->type = ARMFault_Background;
10030 return true;
10033 if (matchregion == -1) {
10034 /* hit using the background region */
10035 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10036 } else {
10037 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
10038 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
10040 if (m_is_system_region(env, address)) {
10041 /* System space is always execute never */
10042 xn = 1;
10045 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
10046 if (*prot && !xn) {
10047 *prot |= PAGE_EXEC;
10049 /* We don't need to look the attribute up in the MAIR0/MAIR1
10050 * registers because that only tells us about cacheability.
10052 if (mregion) {
10053 *mregion = matchregion;
10057 fi->type = ARMFault_Permission;
10058 fi->level = 1;
10060 * Core QEMU code can't handle execution from small pages yet, so
10061 * don't try it. This means any attempted execution will generate
10062 * an MPU exception, rather than eventually causing QEMU to exit in
10063 * get_page_addr_code().
10065 if (*is_subpage && (*prot & PAGE_EXEC)) {
10066 qemu_log_mask(LOG_UNIMP,
10067 "MPU: No support for execution from regions "
10068 "smaller than 1K\n");
10069 *prot &= ~PAGE_EXEC;
10071 return !(*prot & (1 << access_type));
10075 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
10076 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10077 hwaddr *phys_ptr, MemTxAttrs *txattrs,
10078 int *prot, target_ulong *page_size,
10079 ARMMMUFaultInfo *fi)
10081 uint32_t secure = regime_is_secure(env, mmu_idx);
10082 V8M_SAttributes sattrs = {};
10083 bool ret;
10084 bool mpu_is_subpage;
10086 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10087 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
10088 if (access_type == MMU_INST_FETCH) {
10089 /* Instruction fetches always use the MMU bank and the
10090 * transaction attribute determined by the fetch address,
10091 * regardless of CPU state. This is painful for QEMU
10092 * to handle, because it would mean we need to encode
10093 * into the mmu_idx not just the (user, negpri) information
10094 * for the current security state but also that for the
10095 * other security state, which would balloon the number
10096 * of mmu_idx values needed alarmingly.
10097 * Fortunately we can avoid this because it's not actually
10098 * possible to arbitrarily execute code from memory with
10099 * the wrong security attribute: it will always generate
10100 * an exception of some kind or another, apart from the
10101 * special case of an NS CPU executing an SG instruction
10102 * in S&NSC memory. So we always just fail the translation
10103 * here and sort things out in the exception handler
10104 * (including possibly emulating an SG instruction).
10106 if (sattrs.ns != !secure) {
10107 if (sattrs.nsc) {
10108 fi->type = ARMFault_QEMU_NSCExec;
10109 } else {
10110 fi->type = ARMFault_QEMU_SFault;
10112 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
10113 *phys_ptr = address;
10114 *prot = 0;
10115 return true;
10117 } else {
10118 /* For data accesses we always use the MMU bank indicated
10119 * by the current CPU state, but the security attributes
10120 * might downgrade a secure access to nonsecure.
10122 if (sattrs.ns) {
10123 txattrs->secure = false;
10124 } else if (!secure) {
10125 /* NS access to S memory must fault.
10126 * Architecturally we should first check whether the
10127 * MPU information for this address indicates that we
10128 * are doing an unaligned access to Device memory, which
10129 * should generate a UsageFault instead. QEMU does not
10130 * currently check for that kind of unaligned access though.
10131 * If we added it we would need to do so as a special case
10132 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
10134 fi->type = ARMFault_QEMU_SFault;
10135 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
10136 *phys_ptr = address;
10137 *prot = 0;
10138 return true;
10143 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
10144 txattrs, prot, &mpu_is_subpage, fi, NULL);
10146 * TODO: this is a temporary hack to ignore the fact that the SAU region
10147 * is smaller than a page if this is an executable region. We never
10148 * supported small MPU regions, but we did (accidentally) allow small
10149 * SAU regions, and if we now made small SAU regions not be executable
10150 * then this would break previously working guest code. We can't
10151 * remove this until/unless we implement support for execution from
10152 * small regions.
10154 if (*prot & PAGE_EXEC) {
10155 sattrs.subpage = false;
10157 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
10158 return ret;
10161 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
10162 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10163 hwaddr *phys_ptr, int *prot,
10164 ARMMMUFaultInfo *fi)
10166 int n;
10167 uint32_t mask;
10168 uint32_t base;
10169 bool is_user = regime_is_user(env, mmu_idx);
10171 if (regime_translation_disabled(env, mmu_idx)) {
10172 /* MPU disabled. */
10173 *phys_ptr = address;
10174 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10175 return false;
10178 *phys_ptr = address;
10179 for (n = 7; n >= 0; n--) {
10180 base = env->cp15.c6_region[n];
10181 if ((base & 1) == 0) {
10182 continue;
10184 mask = 1 << ((base >> 1) & 0x1f);
10185 /* Keep this shift separate from the above to avoid an
10186 (undefined) << 32. */
10187 mask = (mask << 1) - 1;
10188 if (((base ^ address) & ~mask) == 0) {
10189 break;
10192 if (n < 0) {
10193 fi->type = ARMFault_Background;
10194 return true;
10197 if (access_type == MMU_INST_FETCH) {
10198 mask = env->cp15.pmsav5_insn_ap;
10199 } else {
10200 mask = env->cp15.pmsav5_data_ap;
10202 mask = (mask >> (n * 4)) & 0xf;
10203 switch (mask) {
10204 case 0:
10205 fi->type = ARMFault_Permission;
10206 fi->level = 1;
10207 return true;
10208 case 1:
10209 if (is_user) {
10210 fi->type = ARMFault_Permission;
10211 fi->level = 1;
10212 return true;
10214 *prot = PAGE_READ | PAGE_WRITE;
10215 break;
10216 case 2:
10217 *prot = PAGE_READ;
10218 if (!is_user) {
10219 *prot |= PAGE_WRITE;
10221 break;
10222 case 3:
10223 *prot = PAGE_READ | PAGE_WRITE;
10224 break;
10225 case 5:
10226 if (is_user) {
10227 fi->type = ARMFault_Permission;
10228 fi->level = 1;
10229 return true;
10231 *prot = PAGE_READ;
10232 break;
10233 case 6:
10234 *prot = PAGE_READ;
10235 break;
10236 default:
10237 /* Bad permission. */
10238 fi->type = ARMFault_Permission;
10239 fi->level = 1;
10240 return true;
10242 *prot |= PAGE_EXEC;
10243 return false;
10246 /* Combine either inner or outer cacheability attributes for normal
10247 * memory, according to table D4-42 and pseudocode procedure
10248 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
10250 * NB: only stage 1 includes allocation hints (RW bits), leading to
10251 * some asymmetry.
10253 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
10255 if (s1 == 4 || s2 == 4) {
10256 /* non-cacheable has precedence */
10257 return 4;
10258 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
10259 /* stage 1 write-through takes precedence */
10260 return s1;
10261 } else if (extract32(s2, 2, 2) == 2) {
10262 /* stage 2 write-through takes precedence, but the allocation hint
10263 * is still taken from stage 1
10265 return (2 << 2) | extract32(s1, 0, 2);
10266 } else { /* write-back */
10267 return s1;
10271 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
10272 * and CombineS1S2Desc()
10274 * @s1: Attributes from stage 1 walk
10275 * @s2: Attributes from stage 2 walk
10277 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
10279 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
10280 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
10281 ARMCacheAttrs ret;
10283 /* Combine shareability attributes (table D4-43) */
10284 if (s1.shareability == 2 || s2.shareability == 2) {
10285 /* if either are outer-shareable, the result is outer-shareable */
10286 ret.shareability = 2;
10287 } else if (s1.shareability == 3 || s2.shareability == 3) {
10288 /* if either are inner-shareable, the result is inner-shareable */
10289 ret.shareability = 3;
10290 } else {
10291 /* both non-shareable */
10292 ret.shareability = 0;
10295 /* Combine memory type and cacheability attributes */
10296 if (s1hi == 0 || s2hi == 0) {
10297 /* Device has precedence over normal */
10298 if (s1lo == 0 || s2lo == 0) {
10299 /* nGnRnE has precedence over anything */
10300 ret.attrs = 0;
10301 } else if (s1lo == 4 || s2lo == 4) {
10302 /* non-Reordering has precedence over Reordering */
10303 ret.attrs = 4; /* nGnRE */
10304 } else if (s1lo == 8 || s2lo == 8) {
10305 /* non-Gathering has precedence over Gathering */
10306 ret.attrs = 8; /* nGRE */
10307 } else {
10308 ret.attrs = 0xc; /* GRE */
10311 /* Any location for which the resultant memory type is any
10312 * type of Device memory is always treated as Outer Shareable.
10314 ret.shareability = 2;
10315 } else { /* Normal memory */
10316 /* Outer/inner cacheability combine independently */
10317 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
10318 | combine_cacheattr_nibble(s1lo, s2lo);
10320 if (ret.attrs == 0x44) {
10321 /* Any location for which the resultant memory type is Normal
10322 * Inner Non-cacheable, Outer Non-cacheable is always treated
10323 * as Outer Shareable.
10325 ret.shareability = 2;
10329 return ret;
10333 /* get_phys_addr - get the physical address for this virtual address
10335 * Find the physical address corresponding to the given virtual address,
10336 * by doing a translation table walk on MMU based systems or using the
10337 * MPU state on MPU based systems.
10339 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10340 * prot and page_size may not be filled in, and the populated fsr value provides
10341 * information on why the translation aborted, in the format of a
10342 * DFSR/IFSR fault register, with the following caveats:
10343 * * we honour the short vs long DFSR format differences.
10344 * * the WnR bit is never set (the caller must do this).
10345 * * for PSMAv5 based systems we don't bother to return a full FSR format
10346 * value.
10348 * @env: CPUARMState
10349 * @address: virtual address to get physical address for
10350 * @access_type: 0 for read, 1 for write, 2 for execute
10351 * @mmu_idx: MMU index indicating required translation regime
10352 * @phys_ptr: set to the physical address corresponding to the virtual address
10353 * @attrs: set to the memory transaction attributes to use
10354 * @prot: set to the permissions for the page containing phys_ptr
10355 * @page_size: set to the size of the page containing phys_ptr
10356 * @fi: set to fault info if the translation fails
10357 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
10359 static bool get_phys_addr(CPUARMState *env, target_ulong address,
10360 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10361 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
10362 target_ulong *page_size,
10363 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
10365 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
10366 /* Call ourselves recursively to do the stage 1 and then stage 2
10367 * translations.
10369 if (arm_feature(env, ARM_FEATURE_EL2)) {
10370 hwaddr ipa;
10371 int s2_prot;
10372 int ret;
10373 ARMCacheAttrs cacheattrs2 = {};
10375 ret = get_phys_addr(env, address, access_type,
10376 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
10377 prot, page_size, fi, cacheattrs);
10379 /* If S1 fails or S2 is disabled, return early. */
10380 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
10381 *phys_ptr = ipa;
10382 return ret;
10385 /* S1 is done. Now do S2 translation. */
10386 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
10387 phys_ptr, attrs, &s2_prot,
10388 page_size, fi,
10389 cacheattrs != NULL ? &cacheattrs2 : NULL);
10390 fi->s2addr = ipa;
10391 /* Combine the S1 and S2 perms. */
10392 *prot &= s2_prot;
10394 /* Combine the S1 and S2 cache attributes, if needed */
10395 if (!ret && cacheattrs != NULL) {
10396 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
10399 return ret;
10400 } else {
10402 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
10404 mmu_idx = stage_1_mmu_idx(mmu_idx);
10408 /* The page table entries may downgrade secure to non-secure, but
10409 * cannot upgrade an non-secure translation regime's attributes
10410 * to secure.
10412 attrs->secure = regime_is_secure(env, mmu_idx);
10413 attrs->user = regime_is_user(env, mmu_idx);
10415 /* Fast Context Switch Extension. This doesn't exist at all in v8.
10416 * In v7 and earlier it affects all stage 1 translations.
10418 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
10419 && !arm_feature(env, ARM_FEATURE_V8)) {
10420 if (regime_el(env, mmu_idx) == 3) {
10421 address += env->cp15.fcseidr_s;
10422 } else {
10423 address += env->cp15.fcseidr_ns;
10427 if (arm_feature(env, ARM_FEATURE_PMSA)) {
10428 bool ret;
10429 *page_size = TARGET_PAGE_SIZE;
10431 if (arm_feature(env, ARM_FEATURE_V8)) {
10432 /* PMSAv8 */
10433 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
10434 phys_ptr, attrs, prot, page_size, fi);
10435 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10436 /* PMSAv7 */
10437 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
10438 phys_ptr, prot, page_size, fi);
10439 } else {
10440 /* Pre-v7 MPU */
10441 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
10442 phys_ptr, prot, fi);
10444 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
10445 " mmu_idx %u -> %s (prot %c%c%c)\n",
10446 access_type == MMU_DATA_LOAD ? "reading" :
10447 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
10448 (uint32_t)address, mmu_idx,
10449 ret ? "Miss" : "Hit",
10450 *prot & PAGE_READ ? 'r' : '-',
10451 *prot & PAGE_WRITE ? 'w' : '-',
10452 *prot & PAGE_EXEC ? 'x' : '-');
10454 return ret;
10457 /* Definitely a real MMU, not an MPU */
10459 if (regime_translation_disabled(env, mmu_idx)) {
10460 /* MMU disabled. */
10461 *phys_ptr = address;
10462 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10463 *page_size = TARGET_PAGE_SIZE;
10464 return 0;
10467 if (regime_using_lpae_format(env, mmu_idx)) {
10468 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
10469 phys_ptr, attrs, prot, page_size,
10470 fi, cacheattrs);
10471 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
10472 return get_phys_addr_v6(env, address, access_type, mmu_idx,
10473 phys_ptr, attrs, prot, page_size, fi);
10474 } else {
10475 return get_phys_addr_v5(env, address, access_type, mmu_idx,
10476 phys_ptr, prot, page_size, fi);
10480 /* Walk the page table and (if the mapping exists) add the page
10481 * to the TLB. Return false on success, or true on failure. Populate
10482 * fsr with ARM DFSR/IFSR fault register format value on failure.
10484 bool arm_tlb_fill(CPUState *cs, vaddr address,
10485 MMUAccessType access_type, int mmu_idx,
10486 ARMMMUFaultInfo *fi)
10488 ARMCPU *cpu = ARM_CPU(cs);
10489 CPUARMState *env = &cpu->env;
10490 hwaddr phys_addr;
10491 target_ulong page_size;
10492 int prot;
10493 int ret;
10494 MemTxAttrs attrs = {};
10496 ret = get_phys_addr(env, address, access_type,
10497 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr,
10498 &attrs, &prot, &page_size, fi, NULL);
10499 if (!ret) {
10501 * Map a single [sub]page. Regions smaller than our declared
10502 * target page size are handled specially, so for those we
10503 * pass in the exact addresses.
10505 if (page_size >= TARGET_PAGE_SIZE) {
10506 phys_addr &= TARGET_PAGE_MASK;
10507 address &= TARGET_PAGE_MASK;
10509 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
10510 prot, mmu_idx, page_size);
10511 return 0;
10514 return ret;
10517 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
10518 MemTxAttrs *attrs)
10520 ARMCPU *cpu = ARM_CPU(cs);
10521 CPUARMState *env = &cpu->env;
10522 hwaddr phys_addr;
10523 target_ulong page_size;
10524 int prot;
10525 bool ret;
10526 ARMMMUFaultInfo fi = {};
10527 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
10529 *attrs = (MemTxAttrs) {};
10531 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
10532 attrs, &prot, &page_size, &fi, NULL);
10534 if (ret) {
10535 return -1;
10537 return phys_addr;
10540 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
10542 uint32_t mask;
10543 unsigned el = arm_current_el(env);
10545 /* First handle registers which unprivileged can read */
10547 switch (reg) {
10548 case 0 ... 7: /* xPSR sub-fields */
10549 mask = 0;
10550 if ((reg & 1) && el) {
10551 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
10553 if (!(reg & 4)) {
10554 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
10556 /* EPSR reads as zero */
10557 return xpsr_read(env) & mask;
10558 break;
10559 case 20: /* CONTROL */
10560 return env->v7m.control[env->v7m.secure];
10561 case 0x94: /* CONTROL_NS */
10562 /* We have to handle this here because unprivileged Secure code
10563 * can read the NS CONTROL register.
10565 if (!env->v7m.secure) {
10566 return 0;
10568 return env->v7m.control[M_REG_NS];
10571 if (el == 0) {
10572 return 0; /* unprivileged reads others as zero */
10575 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10576 switch (reg) {
10577 case 0x88: /* MSP_NS */
10578 if (!env->v7m.secure) {
10579 return 0;
10581 return env->v7m.other_ss_msp;
10582 case 0x89: /* PSP_NS */
10583 if (!env->v7m.secure) {
10584 return 0;
10586 return env->v7m.other_ss_psp;
10587 case 0x8a: /* MSPLIM_NS */
10588 if (!env->v7m.secure) {
10589 return 0;
10591 return env->v7m.msplim[M_REG_NS];
10592 case 0x8b: /* PSPLIM_NS */
10593 if (!env->v7m.secure) {
10594 return 0;
10596 return env->v7m.psplim[M_REG_NS];
10597 case 0x90: /* PRIMASK_NS */
10598 if (!env->v7m.secure) {
10599 return 0;
10601 return env->v7m.primask[M_REG_NS];
10602 case 0x91: /* BASEPRI_NS */
10603 if (!env->v7m.secure) {
10604 return 0;
10606 return env->v7m.basepri[M_REG_NS];
10607 case 0x93: /* FAULTMASK_NS */
10608 if (!env->v7m.secure) {
10609 return 0;
10611 return env->v7m.faultmask[M_REG_NS];
10612 case 0x98: /* SP_NS */
10614 /* This gives the non-secure SP selected based on whether we're
10615 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10617 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10619 if (!env->v7m.secure) {
10620 return 0;
10622 if (!arm_v7m_is_handler_mode(env) && spsel) {
10623 return env->v7m.other_ss_psp;
10624 } else {
10625 return env->v7m.other_ss_msp;
10628 default:
10629 break;
10633 switch (reg) {
10634 case 8: /* MSP */
10635 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13];
10636 case 9: /* PSP */
10637 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp;
10638 case 10: /* MSPLIM */
10639 if (!arm_feature(env, ARM_FEATURE_V8)) {
10640 goto bad_reg;
10642 return env->v7m.msplim[env->v7m.secure];
10643 case 11: /* PSPLIM */
10644 if (!arm_feature(env, ARM_FEATURE_V8)) {
10645 goto bad_reg;
10647 return env->v7m.psplim[env->v7m.secure];
10648 case 16: /* PRIMASK */
10649 return env->v7m.primask[env->v7m.secure];
10650 case 17: /* BASEPRI */
10651 case 18: /* BASEPRI_MAX */
10652 return env->v7m.basepri[env->v7m.secure];
10653 case 19: /* FAULTMASK */
10654 return env->v7m.faultmask[env->v7m.secure];
10655 default:
10656 bad_reg:
10657 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
10658 " register %d\n", reg);
10659 return 0;
10663 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
10665 /* We're passed bits [11..0] of the instruction; extract
10666 * SYSm and the mask bits.
10667 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
10668 * we choose to treat them as if the mask bits were valid.
10669 * NB that the pseudocode 'mask' variable is bits [11..10],
10670 * whereas ours is [11..8].
10672 uint32_t mask = extract32(maskreg, 8, 4);
10673 uint32_t reg = extract32(maskreg, 0, 8);
10675 if (arm_current_el(env) == 0 && reg > 7) {
10676 /* only xPSR sub-fields may be written by unprivileged */
10677 return;
10680 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10681 switch (reg) {
10682 case 0x88: /* MSP_NS */
10683 if (!env->v7m.secure) {
10684 return;
10686 env->v7m.other_ss_msp = val;
10687 return;
10688 case 0x89: /* PSP_NS */
10689 if (!env->v7m.secure) {
10690 return;
10692 env->v7m.other_ss_psp = val;
10693 return;
10694 case 0x8a: /* MSPLIM_NS */
10695 if (!env->v7m.secure) {
10696 return;
10698 env->v7m.msplim[M_REG_NS] = val & ~7;
10699 return;
10700 case 0x8b: /* PSPLIM_NS */
10701 if (!env->v7m.secure) {
10702 return;
10704 env->v7m.psplim[M_REG_NS] = val & ~7;
10705 return;
10706 case 0x90: /* PRIMASK_NS */
10707 if (!env->v7m.secure) {
10708 return;
10710 env->v7m.primask[M_REG_NS] = val & 1;
10711 return;
10712 case 0x91: /* BASEPRI_NS */
10713 if (!env->v7m.secure) {
10714 return;
10716 env->v7m.basepri[M_REG_NS] = val & 0xff;
10717 return;
10718 case 0x93: /* FAULTMASK_NS */
10719 if (!env->v7m.secure) {
10720 return;
10722 env->v7m.faultmask[M_REG_NS] = val & 1;
10723 return;
10724 case 0x94: /* CONTROL_NS */
10725 if (!env->v7m.secure) {
10726 return;
10728 write_v7m_control_spsel_for_secstate(env,
10729 val & R_V7M_CONTROL_SPSEL_MASK,
10730 M_REG_NS);
10731 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK;
10732 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK;
10733 return;
10734 case 0x98: /* SP_NS */
10736 /* This gives the non-secure SP selected based on whether we're
10737 * currently in handler mode or not, using the NS CONTROL.SPSEL.
10739 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
10741 if (!env->v7m.secure) {
10742 return;
10744 if (!arm_v7m_is_handler_mode(env) && spsel) {
10745 env->v7m.other_ss_psp = val;
10746 } else {
10747 env->v7m.other_ss_msp = val;
10749 return;
10751 default:
10752 break;
10756 switch (reg) {
10757 case 0 ... 7: /* xPSR sub-fields */
10758 /* only APSR is actually writable */
10759 if (!(reg & 4)) {
10760 uint32_t apsrmask = 0;
10762 if (mask & 8) {
10763 apsrmask |= XPSR_NZCV | XPSR_Q;
10765 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
10766 apsrmask |= XPSR_GE;
10768 xpsr_write(env, val, apsrmask);
10770 break;
10771 case 8: /* MSP */
10772 if (v7m_using_psp(env)) {
10773 env->v7m.other_sp = val;
10774 } else {
10775 env->regs[13] = val;
10777 break;
10778 case 9: /* PSP */
10779 if (v7m_using_psp(env)) {
10780 env->regs[13] = val;
10781 } else {
10782 env->v7m.other_sp = val;
10784 break;
10785 case 10: /* MSPLIM */
10786 if (!arm_feature(env, ARM_FEATURE_V8)) {
10787 goto bad_reg;
10789 env->v7m.msplim[env->v7m.secure] = val & ~7;
10790 break;
10791 case 11: /* PSPLIM */
10792 if (!arm_feature(env, ARM_FEATURE_V8)) {
10793 goto bad_reg;
10795 env->v7m.psplim[env->v7m.secure] = val & ~7;
10796 break;
10797 case 16: /* PRIMASK */
10798 env->v7m.primask[env->v7m.secure] = val & 1;
10799 break;
10800 case 17: /* BASEPRI */
10801 env->v7m.basepri[env->v7m.secure] = val & 0xff;
10802 break;
10803 case 18: /* BASEPRI_MAX */
10804 val &= 0xff;
10805 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
10806 || env->v7m.basepri[env->v7m.secure] == 0)) {
10807 env->v7m.basepri[env->v7m.secure] = val;
10809 break;
10810 case 19: /* FAULTMASK */
10811 env->v7m.faultmask[env->v7m.secure] = val & 1;
10812 break;
10813 case 20: /* CONTROL */
10814 /* Writing to the SPSEL bit only has an effect if we are in
10815 * thread mode; other bits can be updated by any privileged code.
10816 * write_v7m_control_spsel() deals with updating the SPSEL bit in
10817 * env->v7m.control, so we only need update the others.
10818 * For v7M, we must just ignore explicit writes to SPSEL in handler
10819 * mode; for v8M the write is permitted but will have no effect.
10821 if (arm_feature(env, ARM_FEATURE_V8) ||
10822 !arm_v7m_is_handler_mode(env)) {
10823 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
10825 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
10826 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
10827 break;
10828 default:
10829 bad_reg:
10830 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
10831 " register %d\n", reg);
10832 return;
10836 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
10838 /* Implement the TT instruction. op is bits [7:6] of the insn. */
10839 bool forceunpriv = op & 1;
10840 bool alt = op & 2;
10841 V8M_SAttributes sattrs = {};
10842 uint32_t tt_resp;
10843 bool r, rw, nsr, nsrw, mrvalid;
10844 int prot;
10845 ARMMMUFaultInfo fi = {};
10846 MemTxAttrs attrs = {};
10847 hwaddr phys_addr;
10848 ARMMMUIdx mmu_idx;
10849 uint32_t mregion;
10850 bool targetpriv;
10851 bool targetsec = env->v7m.secure;
10852 bool is_subpage;
10854 /* Work out what the security state and privilege level we're
10855 * interested in is...
10857 if (alt) {
10858 targetsec = !targetsec;
10861 if (forceunpriv) {
10862 targetpriv = false;
10863 } else {
10864 targetpriv = arm_v7m_is_handler_mode(env) ||
10865 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK);
10868 /* ...and then figure out which MMU index this is */
10869 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv);
10871 /* We know that the MPU and SAU don't care about the access type
10872 * for our purposes beyond that we don't want to claim to be
10873 * an insn fetch, so we arbitrarily call this a read.
10876 /* MPU region info only available for privileged or if
10877 * inspecting the other MPU state.
10879 if (arm_current_el(env) != 0 || alt) {
10880 /* We can ignore the return value as prot is always set */
10881 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
10882 &phys_addr, &attrs, &prot, &is_subpage,
10883 &fi, &mregion);
10884 if (mregion == -1) {
10885 mrvalid = false;
10886 mregion = 0;
10887 } else {
10888 mrvalid = true;
10890 r = prot & PAGE_READ;
10891 rw = prot & PAGE_WRITE;
10892 } else {
10893 r = false;
10894 rw = false;
10895 mrvalid = false;
10896 mregion = 0;
10899 if (env->v7m.secure) {
10900 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
10901 nsr = sattrs.ns && r;
10902 nsrw = sattrs.ns && rw;
10903 } else {
10904 sattrs.ns = true;
10905 nsr = false;
10906 nsrw = false;
10909 tt_resp = (sattrs.iregion << 24) |
10910 (sattrs.irvalid << 23) |
10911 ((!sattrs.ns) << 22) |
10912 (nsrw << 21) |
10913 (nsr << 20) |
10914 (rw << 19) |
10915 (r << 18) |
10916 (sattrs.srvalid << 17) |
10917 (mrvalid << 16) |
10918 (sattrs.sregion << 8) |
10919 mregion;
10921 return tt_resp;
10924 #endif
10926 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
10928 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
10929 * Note that we do not implement the (architecturally mandated)
10930 * alignment fault for attempts to use this on Device memory
10931 * (which matches the usual QEMU behaviour of not implementing either
10932 * alignment faults or any memory attribute handling).
10935 ARMCPU *cpu = arm_env_get_cpu(env);
10936 uint64_t blocklen = 4 << cpu->dcz_blocksize;
10937 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
10939 #ifndef CONFIG_USER_ONLY
10941 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
10942 * the block size so we might have to do more than one TLB lookup.
10943 * We know that in fact for any v8 CPU the page size is at least 4K
10944 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
10945 * 1K as an artefact of legacy v5 subpage support being present in the
10946 * same QEMU executable.
10948 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
10949 void *hostaddr[maxidx];
10950 int try, i;
10951 unsigned mmu_idx = cpu_mmu_index(env, false);
10952 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
10954 for (try = 0; try < 2; try++) {
10956 for (i = 0; i < maxidx; i++) {
10957 hostaddr[i] = tlb_vaddr_to_host(env,
10958 vaddr + TARGET_PAGE_SIZE * i,
10959 1, mmu_idx);
10960 if (!hostaddr[i]) {
10961 break;
10964 if (i == maxidx) {
10965 /* If it's all in the TLB it's fair game for just writing to;
10966 * we know we don't need to update dirty status, etc.
10968 for (i = 0; i < maxidx - 1; i++) {
10969 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
10971 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
10972 return;
10974 /* OK, try a store and see if we can populate the tlb. This
10975 * might cause an exception if the memory isn't writable,
10976 * in which case we will longjmp out of here. We must for
10977 * this purpose use the actual register value passed to us
10978 * so that we get the fault address right.
10980 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
10981 /* Now we can populate the other TLB entries, if any */
10982 for (i = 0; i < maxidx; i++) {
10983 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
10984 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
10985 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
10990 /* Slow path (probably attempt to do this to an I/O device or
10991 * similar, or clearing of a block of code we have translations
10992 * cached for). Just do a series of byte writes as the architecture
10993 * demands. It's not worth trying to use a cpu_physical_memory_map(),
10994 * memset(), unmap() sequence here because:
10995 * + we'd need to account for the blocksize being larger than a page
10996 * + the direct-RAM access case is almost always going to be dealt
10997 * with in the fastpath code above, so there's no speed benefit
10998 * + we would have to deal with the map returning NULL because the
10999 * bounce buffer was in use
11001 for (i = 0; i < blocklen; i++) {
11002 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
11005 #else
11006 memset(g2h(vaddr), 0, blocklen);
11007 #endif
11010 /* Note that signed overflow is undefined in C. The following routines are
11011 careful to use unsigned types where modulo arithmetic is required.
11012 Failure to do so _will_ break on newer gcc. */
11014 /* Signed saturating arithmetic. */
11016 /* Perform 16-bit signed saturating addition. */
11017 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11019 uint16_t res;
11021 res = a + b;
11022 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11023 if (a & 0x8000)
11024 res = 0x8000;
11025 else
11026 res = 0x7fff;
11028 return res;
11031 /* Perform 8-bit signed saturating addition. */
11032 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11034 uint8_t res;
11036 res = a + b;
11037 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11038 if (a & 0x80)
11039 res = 0x80;
11040 else
11041 res = 0x7f;
11043 return res;
11046 /* Perform 16-bit signed saturating subtraction. */
11047 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11049 uint16_t res;
11051 res = a - b;
11052 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11053 if (a & 0x8000)
11054 res = 0x8000;
11055 else
11056 res = 0x7fff;
11058 return res;
11061 /* Perform 8-bit signed saturating subtraction. */
11062 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11064 uint8_t res;
11066 res = a - b;
11067 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11068 if (a & 0x80)
11069 res = 0x80;
11070 else
11071 res = 0x7f;
11073 return res;
11076 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11077 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11078 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11079 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11080 #define PFX q
11082 #include "op_addsub.h"
11084 /* Unsigned saturating arithmetic. */
11085 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11087 uint16_t res;
11088 res = a + b;
11089 if (res < a)
11090 res = 0xffff;
11091 return res;
11094 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11096 if (a > b)
11097 return a - b;
11098 else
11099 return 0;
11102 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11104 uint8_t res;
11105 res = a + b;
11106 if (res < a)
11107 res = 0xff;
11108 return res;
11111 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11113 if (a > b)
11114 return a - b;
11115 else
11116 return 0;
11119 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11120 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11121 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11122 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11123 #define PFX uq
11125 #include "op_addsub.h"
11127 /* Signed modulo arithmetic. */
11128 #define SARITH16(a, b, n, op) do { \
11129 int32_t sum; \
11130 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
11131 RESULT(sum, n, 16); \
11132 if (sum >= 0) \
11133 ge |= 3 << (n * 2); \
11134 } while(0)
11136 #define SARITH8(a, b, n, op) do { \
11137 int32_t sum; \
11138 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
11139 RESULT(sum, n, 8); \
11140 if (sum >= 0) \
11141 ge |= 1 << n; \
11142 } while(0)
11145 #define ADD16(a, b, n) SARITH16(a, b, n, +)
11146 #define SUB16(a, b, n) SARITH16(a, b, n, -)
11147 #define ADD8(a, b, n) SARITH8(a, b, n, +)
11148 #define SUB8(a, b, n) SARITH8(a, b, n, -)
11149 #define PFX s
11150 #define ARITH_GE
11152 #include "op_addsub.h"
11154 /* Unsigned modulo arithmetic. */
11155 #define ADD16(a, b, n) do { \
11156 uint32_t sum; \
11157 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11158 RESULT(sum, n, 16); \
11159 if ((sum >> 16) == 1) \
11160 ge |= 3 << (n * 2); \
11161 } while(0)
11163 #define ADD8(a, b, n) do { \
11164 uint32_t sum; \
11165 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11166 RESULT(sum, n, 8); \
11167 if ((sum >> 8) == 1) \
11168 ge |= 1 << n; \
11169 } while(0)
11171 #define SUB16(a, b, n) do { \
11172 uint32_t sum; \
11173 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11174 RESULT(sum, n, 16); \
11175 if ((sum >> 16) == 0) \
11176 ge |= 3 << (n * 2); \
11177 } while(0)
11179 #define SUB8(a, b, n) do { \
11180 uint32_t sum; \
11181 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11182 RESULT(sum, n, 8); \
11183 if ((sum >> 8) == 0) \
11184 ge |= 1 << n; \
11185 } while(0)
11187 #define PFX u
11188 #define ARITH_GE
11190 #include "op_addsub.h"
11192 /* Halved signed arithmetic. */
11193 #define ADD16(a, b, n) \
11194 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11195 #define SUB16(a, b, n) \
11196 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11197 #define ADD8(a, b, n) \
11198 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11199 #define SUB8(a, b, n) \
11200 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11201 #define PFX sh
11203 #include "op_addsub.h"
11205 /* Halved unsigned arithmetic. */
11206 #define ADD16(a, b, n) \
11207 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11208 #define SUB16(a, b, n) \
11209 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11210 #define ADD8(a, b, n) \
11211 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11212 #define SUB8(a, b, n) \
11213 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11214 #define PFX uh
11216 #include "op_addsub.h"
11218 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11220 if (a > b)
11221 return a - b;
11222 else
11223 return b - a;
11226 /* Unsigned sum of absolute byte differences. */
11227 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11229 uint32_t sum;
11230 sum = do_usad(a, b);
11231 sum += do_usad(a >> 8, b >> 8);
11232 sum += do_usad(a >> 16, b >>16);
11233 sum += do_usad(a >> 24, b >> 24);
11234 return sum;
11237 /* For ARMv6 SEL instruction. */
11238 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11240 uint32_t mask;
11242 mask = 0;
11243 if (flags & 1)
11244 mask |= 0xff;
11245 if (flags & 2)
11246 mask |= 0xff00;
11247 if (flags & 4)
11248 mask |= 0xff0000;
11249 if (flags & 8)
11250 mask |= 0xff000000;
11251 return (a & mask) | (b & ~mask);
11254 /* VFP support. We follow the convention used for VFP instructions:
11255 Single precision routines have a "s" suffix, double precision a
11256 "d" suffix. */
11258 /* Convert host exception flags to vfp form. */
11259 static inline int vfp_exceptbits_from_host(int host_bits)
11261 int target_bits = 0;
11263 if (host_bits & float_flag_invalid)
11264 target_bits |= 1;
11265 if (host_bits & float_flag_divbyzero)
11266 target_bits |= 2;
11267 if (host_bits & float_flag_overflow)
11268 target_bits |= 4;
11269 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
11270 target_bits |= 8;
11271 if (host_bits & float_flag_inexact)
11272 target_bits |= 0x10;
11273 if (host_bits & float_flag_input_denormal)
11274 target_bits |= 0x80;
11275 return target_bits;
11278 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
11280 int i;
11281 uint32_t fpscr;
11283 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
11284 | (env->vfp.vec_len << 16)
11285 | (env->vfp.vec_stride << 20);
11286 i = get_float_exception_flags(&env->vfp.fp_status);
11287 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
11288 i |= get_float_exception_flags(&env->vfp.fp_status_f16);
11289 fpscr |= vfp_exceptbits_from_host(i);
11290 return fpscr;
11293 uint32_t vfp_get_fpscr(CPUARMState *env)
11295 return HELPER(vfp_get_fpscr)(env);
11298 /* Convert vfp exception flags to target form. */
11299 static inline int vfp_exceptbits_to_host(int target_bits)
11301 int host_bits = 0;
11303 if (target_bits & 1)
11304 host_bits |= float_flag_invalid;
11305 if (target_bits & 2)
11306 host_bits |= float_flag_divbyzero;
11307 if (target_bits & 4)
11308 host_bits |= float_flag_overflow;
11309 if (target_bits & 8)
11310 host_bits |= float_flag_underflow;
11311 if (target_bits & 0x10)
11312 host_bits |= float_flag_inexact;
11313 if (target_bits & 0x80)
11314 host_bits |= float_flag_input_denormal;
11315 return host_bits;
11318 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
11320 int i;
11321 uint32_t changed;
11323 changed = env->vfp.xregs[ARM_VFP_FPSCR];
11324 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
11325 env->vfp.vec_len = (val >> 16) & 7;
11326 env->vfp.vec_stride = (val >> 20) & 3;
11328 changed ^= val;
11329 if (changed & (3 << 22)) {
11330 i = (val >> 22) & 3;
11331 switch (i) {
11332 case FPROUNDING_TIEEVEN:
11333 i = float_round_nearest_even;
11334 break;
11335 case FPROUNDING_POSINF:
11336 i = float_round_up;
11337 break;
11338 case FPROUNDING_NEGINF:
11339 i = float_round_down;
11340 break;
11341 case FPROUNDING_ZERO:
11342 i = float_round_to_zero;
11343 break;
11345 set_float_rounding_mode(i, &env->vfp.fp_status);
11346 set_float_rounding_mode(i, &env->vfp.fp_status_f16);
11348 if (changed & FPCR_FZ16) {
11349 bool ftz_enabled = val & FPCR_FZ16;
11350 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
11351 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
11353 if (changed & FPCR_FZ) {
11354 bool ftz_enabled = val & FPCR_FZ;
11355 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status);
11356 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status);
11358 if (changed & FPCR_DN) {
11359 bool dnan_enabled = val & FPCR_DN;
11360 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status);
11361 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status_f16);
11364 /* The exception flags are ORed together when we read fpscr so we
11365 * only need to preserve the current state in one of our
11366 * float_status values.
11368 i = vfp_exceptbits_to_host(val);
11369 set_float_exception_flags(i, &env->vfp.fp_status);
11370 set_float_exception_flags(0, &env->vfp.fp_status_f16);
11371 set_float_exception_flags(0, &env->vfp.standard_fp_status);
11374 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
11376 HELPER(vfp_set_fpscr)(env, val);
11379 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
11381 #define VFP_BINOP(name) \
11382 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
11384 float_status *fpst = fpstp; \
11385 return float32_ ## name(a, b, fpst); \
11387 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
11389 float_status *fpst = fpstp; \
11390 return float64_ ## name(a, b, fpst); \
11392 VFP_BINOP(add)
11393 VFP_BINOP(sub)
11394 VFP_BINOP(mul)
11395 VFP_BINOP(div)
11396 VFP_BINOP(min)
11397 VFP_BINOP(max)
11398 VFP_BINOP(minnum)
11399 VFP_BINOP(maxnum)
11400 #undef VFP_BINOP
11402 float32 VFP_HELPER(neg, s)(float32 a)
11404 return float32_chs(a);
11407 float64 VFP_HELPER(neg, d)(float64 a)
11409 return float64_chs(a);
11412 float32 VFP_HELPER(abs, s)(float32 a)
11414 return float32_abs(a);
11417 float64 VFP_HELPER(abs, d)(float64 a)
11419 return float64_abs(a);
11422 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
11424 return float32_sqrt(a, &env->vfp.fp_status);
11427 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
11429 return float64_sqrt(a, &env->vfp.fp_status);
11432 /* XXX: check quiet/signaling case */
11433 #define DO_VFP_cmp(p, type) \
11434 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
11436 uint32_t flags; \
11437 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
11438 case 0: flags = 0x6; break; \
11439 case -1: flags = 0x8; break; \
11440 case 1: flags = 0x2; break; \
11441 default: case 2: flags = 0x3; break; \
11443 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11444 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11446 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
11448 uint32_t flags; \
11449 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
11450 case 0: flags = 0x6; break; \
11451 case -1: flags = 0x8; break; \
11452 case 1: flags = 0x2; break; \
11453 default: case 2: flags = 0x3; break; \
11455 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
11456 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
11458 DO_VFP_cmp(s, float32)
11459 DO_VFP_cmp(d, float64)
11460 #undef DO_VFP_cmp
11462 /* Integer to float and float to integer conversions */
11464 #define CONV_ITOF(name, ftype, fsz, sign) \
11465 ftype HELPER(name)(uint32_t x, void *fpstp) \
11467 float_status *fpst = fpstp; \
11468 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
11471 #define CONV_FTOI(name, ftype, fsz, sign, round) \
11472 sign##int32_t HELPER(name)(ftype x, void *fpstp) \
11474 float_status *fpst = fpstp; \
11475 if (float##fsz##_is_any_nan(x)) { \
11476 float_raise(float_flag_invalid, fpst); \
11477 return 0; \
11479 return float##fsz##_to_##sign##int32##round(x, fpst); \
11482 #define FLOAT_CONVS(name, p, ftype, fsz, sign) \
11483 CONV_ITOF(vfp_##name##to##p, ftype, fsz, sign) \
11484 CONV_FTOI(vfp_to##name##p, ftype, fsz, sign, ) \
11485 CONV_FTOI(vfp_to##name##z##p, ftype, fsz, sign, _round_to_zero)
11487 FLOAT_CONVS(si, h, uint32_t, 16, )
11488 FLOAT_CONVS(si, s, float32, 32, )
11489 FLOAT_CONVS(si, d, float64, 64, )
11490 FLOAT_CONVS(ui, h, uint32_t, 16, u)
11491 FLOAT_CONVS(ui, s, float32, 32, u)
11492 FLOAT_CONVS(ui, d, float64, 64, u)
11494 #undef CONV_ITOF
11495 #undef CONV_FTOI
11496 #undef FLOAT_CONVS
11498 /* floating point conversion */
11499 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
11501 return float32_to_float64(x, &env->vfp.fp_status);
11504 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
11506 return float64_to_float32(x, &env->vfp.fp_status);
11509 /* VFP3 fixed point conversion. */
11510 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
11511 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
11512 void *fpstp) \
11514 float_status *fpst = fpstp; \
11515 float##fsz tmp; \
11516 tmp = itype##_to_##float##fsz(x, fpst); \
11517 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
11520 /* Notice that we want only input-denormal exception flags from the
11521 * scalbn operation: the other possible flags (overflow+inexact if
11522 * we overflow to infinity, output-denormal) aren't correct for the
11523 * complete scale-and-convert operation.
11525 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
11526 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
11527 uint32_t shift, \
11528 void *fpstp) \
11530 float_status *fpst = fpstp; \
11531 int old_exc_flags = get_float_exception_flags(fpst); \
11532 float##fsz tmp; \
11533 if (float##fsz##_is_any_nan(x)) { \
11534 float_raise(float_flag_invalid, fpst); \
11535 return 0; \
11537 tmp = float##fsz##_scalbn(x, shift, fpst); \
11538 old_exc_flags |= get_float_exception_flags(fpst) \
11539 & float_flag_input_denormal; \
11540 set_float_exception_flags(old_exc_flags, fpst); \
11541 return float##fsz##_to_##itype##round(tmp, fpst); \
11544 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \
11545 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
11546 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
11547 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
11549 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
11550 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
11551 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
11553 VFP_CONV_FIX(sh, d, 64, 64, int16)
11554 VFP_CONV_FIX(sl, d, 64, 64, int32)
11555 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
11556 VFP_CONV_FIX(uh, d, 64, 64, uint16)
11557 VFP_CONV_FIX(ul, d, 64, 64, uint32)
11558 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
11559 VFP_CONV_FIX(sh, s, 32, 32, int16)
11560 VFP_CONV_FIX(sl, s, 32, 32, int32)
11561 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
11562 VFP_CONV_FIX(uh, s, 32, 32, uint16)
11563 VFP_CONV_FIX(ul, s, 32, 32, uint32)
11564 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
11566 #undef VFP_CONV_FIX
11567 #undef VFP_CONV_FIX_FLOAT
11568 #undef VFP_CONV_FLOAT_FIX_ROUND
11569 #undef VFP_CONV_FIX_A64
11571 /* Conversion to/from f16 can overflow to infinity before/after scaling.
11572 * Therefore we convert to f64, scale, and then convert f64 to f16; or
11573 * vice versa for conversion to integer.
11575 * For 16- and 32-bit integers, the conversion to f64 never rounds.
11576 * For 64-bit integers, any integer that would cause rounding will also
11577 * overflow to f16 infinity, so there is no double rounding problem.
11580 static float16 do_postscale_fp16(float64 f, int shift, float_status *fpst)
11582 return float64_to_float16(float64_scalbn(f, -shift, fpst), true, fpst);
11585 uint32_t HELPER(vfp_sltoh)(uint32_t x, uint32_t shift, void *fpst)
11587 return do_postscale_fp16(int32_to_float64(x, fpst), shift, fpst);
11590 uint32_t HELPER(vfp_ultoh)(uint32_t x, uint32_t shift, void *fpst)
11592 return do_postscale_fp16(uint32_to_float64(x, fpst), shift, fpst);
11595 uint32_t HELPER(vfp_sqtoh)(uint64_t x, uint32_t shift, void *fpst)
11597 return do_postscale_fp16(int64_to_float64(x, fpst), shift, fpst);
11600 uint32_t HELPER(vfp_uqtoh)(uint64_t x, uint32_t shift, void *fpst)
11602 return do_postscale_fp16(uint64_to_float64(x, fpst), shift, fpst);
11605 static float64 do_prescale_fp16(float16 f, int shift, float_status *fpst)
11607 if (unlikely(float16_is_any_nan(f))) {
11608 float_raise(float_flag_invalid, fpst);
11609 return 0;
11610 } else {
11611 int old_exc_flags = get_float_exception_flags(fpst);
11612 float64 ret;
11614 ret = float16_to_float64(f, true, fpst);
11615 ret = float64_scalbn(ret, shift, fpst);
11616 old_exc_flags |= get_float_exception_flags(fpst)
11617 & float_flag_input_denormal;
11618 set_float_exception_flags(old_exc_flags, fpst);
11620 return ret;
11624 uint32_t HELPER(vfp_toshh)(uint32_t x, uint32_t shift, void *fpst)
11626 return float64_to_int16(do_prescale_fp16(x, shift, fpst), fpst);
11629 uint32_t HELPER(vfp_touhh)(uint32_t x, uint32_t shift, void *fpst)
11631 return float64_to_uint16(do_prescale_fp16(x, shift, fpst), fpst);
11634 uint32_t HELPER(vfp_toslh)(uint32_t x, uint32_t shift, void *fpst)
11636 return float64_to_int32(do_prescale_fp16(x, shift, fpst), fpst);
11639 uint32_t HELPER(vfp_toulh)(uint32_t x, uint32_t shift, void *fpst)
11641 return float64_to_uint32(do_prescale_fp16(x, shift, fpst), fpst);
11644 uint64_t HELPER(vfp_tosqh)(uint32_t x, uint32_t shift, void *fpst)
11646 return float64_to_int64(do_prescale_fp16(x, shift, fpst), fpst);
11649 uint64_t HELPER(vfp_touqh)(uint32_t x, uint32_t shift, void *fpst)
11651 return float64_to_uint64(do_prescale_fp16(x, shift, fpst), fpst);
11654 /* Set the current fp rounding mode and return the old one.
11655 * The argument is a softfloat float_round_ value.
11657 uint32_t HELPER(set_rmode)(uint32_t rmode, void *fpstp)
11659 float_status *fp_status = fpstp;
11661 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11662 set_float_rounding_mode(rmode, fp_status);
11664 return prev_rmode;
11667 /* Set the current fp rounding mode in the standard fp status and return
11668 * the old one. This is for NEON instructions that need to change the
11669 * rounding mode but wish to use the standard FPSCR values for everything
11670 * else. Always set the rounding mode back to the correct value after
11671 * modifying it.
11672 * The argument is a softfloat float_round_ value.
11674 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
11676 float_status *fp_status = &env->vfp.standard_fp_status;
11678 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
11679 set_float_rounding_mode(rmode, fp_status);
11681 return prev_rmode;
11684 /* Half precision conversions. */
11685 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, void *fpstp, uint32_t ahp_mode)
11687 /* Squash FZ16 to 0 for the duration of conversion. In this case,
11688 * it would affect flushing input denormals.
11690 float_status *fpst = fpstp;
11691 flag save = get_flush_inputs_to_zero(fpst);
11692 set_flush_inputs_to_zero(false, fpst);
11693 float32 r = float16_to_float32(a, !ahp_mode, fpst);
11694 set_flush_inputs_to_zero(save, fpst);
11695 return r;
11698 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, void *fpstp, uint32_t ahp_mode)
11700 /* Squash FZ16 to 0 for the duration of conversion. In this case,
11701 * it would affect flushing output denormals.
11703 float_status *fpst = fpstp;
11704 flag save = get_flush_to_zero(fpst);
11705 set_flush_to_zero(false, fpst);
11706 float16 r = float32_to_float16(a, !ahp_mode, fpst);
11707 set_flush_to_zero(save, fpst);
11708 return r;
11711 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, void *fpstp, uint32_t ahp_mode)
11713 /* Squash FZ16 to 0 for the duration of conversion. In this case,
11714 * it would affect flushing input denormals.
11716 float_status *fpst = fpstp;
11717 flag save = get_flush_inputs_to_zero(fpst);
11718 set_flush_inputs_to_zero(false, fpst);
11719 float64 r = float16_to_float64(a, !ahp_mode, fpst);
11720 set_flush_inputs_to_zero(save, fpst);
11721 return r;
11724 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, void *fpstp, uint32_t ahp_mode)
11726 /* Squash FZ16 to 0 for the duration of conversion. In this case,
11727 * it would affect flushing output denormals.
11729 float_status *fpst = fpstp;
11730 flag save = get_flush_to_zero(fpst);
11731 set_flush_to_zero(false, fpst);
11732 float16 r = float64_to_float16(a, !ahp_mode, fpst);
11733 set_flush_to_zero(save, fpst);
11734 return r;
11737 #define float32_two make_float32(0x40000000)
11738 #define float32_three make_float32(0x40400000)
11739 #define float32_one_point_five make_float32(0x3fc00000)
11741 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
11743 float_status *s = &env->vfp.standard_fp_status;
11744 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11745 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
11746 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11747 float_raise(float_flag_input_denormal, s);
11749 return float32_two;
11751 return float32_sub(float32_two, float32_mul(a, b, s), s);
11754 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
11756 float_status *s = &env->vfp.standard_fp_status;
11757 float32 product;
11758 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
11759 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
11760 if (!(float32_is_zero(a) || float32_is_zero(b))) {
11761 float_raise(float_flag_input_denormal, s);
11763 return float32_one_point_five;
11765 product = float32_mul(a, b, s);
11766 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
11769 /* NEON helpers. */
11771 /* Constants 256 and 512 are used in some helpers; we avoid relying on
11772 * int->float conversions at run-time. */
11773 #define float64_256 make_float64(0x4070000000000000LL)
11774 #define float64_512 make_float64(0x4080000000000000LL)
11775 #define float16_maxnorm make_float16(0x7bff)
11776 #define float32_maxnorm make_float32(0x7f7fffff)
11777 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
11779 /* Reciprocal functions
11781 * The algorithm that must be used to calculate the estimate
11782 * is specified by the ARM ARM, see FPRecipEstimate()/RecipEstimate
11785 /* See RecipEstimate()
11787 * input is a 9 bit fixed point number
11788 * input range 256 .. 511 for a number from 0.5 <= x < 1.0.
11789 * result range 256 .. 511 for a number from 1.0 to 511/256.
11792 static int recip_estimate(int input)
11794 int a, b, r;
11795 assert(256 <= input && input < 512);
11796 a = (input * 2) + 1;
11797 b = (1 << 19) / a;
11798 r = (b + 1) >> 1;
11799 assert(256 <= r && r < 512);
11800 return r;
11804 * Common wrapper to call recip_estimate
11806 * The parameters are exponent and 64 bit fraction (without implicit
11807 * bit) where the binary point is nominally at bit 52. Returns a
11808 * float64 which can then be rounded to the appropriate size by the
11809 * callee.
11812 static uint64_t call_recip_estimate(int *exp, int exp_off, uint64_t frac)
11814 uint32_t scaled, estimate;
11815 uint64_t result_frac;
11816 int result_exp;
11818 /* Handle sub-normals */
11819 if (*exp == 0) {
11820 if (extract64(frac, 51, 1) == 0) {
11821 *exp = -1;
11822 frac <<= 2;
11823 } else {
11824 frac <<= 1;
11828 /* scaled = UInt('1':fraction<51:44>) */
11829 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
11830 estimate = recip_estimate(scaled);
11832 result_exp = exp_off - *exp;
11833 result_frac = deposit64(0, 44, 8, estimate);
11834 if (result_exp == 0) {
11835 result_frac = deposit64(result_frac >> 1, 51, 1, 1);
11836 } else if (result_exp == -1) {
11837 result_frac = deposit64(result_frac >> 2, 50, 2, 1);
11838 result_exp = 0;
11841 *exp = result_exp;
11843 return result_frac;
11846 static bool round_to_inf(float_status *fpst, bool sign_bit)
11848 switch (fpst->float_rounding_mode) {
11849 case float_round_nearest_even: /* Round to Nearest */
11850 return true;
11851 case float_round_up: /* Round to +Inf */
11852 return !sign_bit;
11853 case float_round_down: /* Round to -Inf */
11854 return sign_bit;
11855 case float_round_to_zero: /* Round to Zero */
11856 return false;
11859 g_assert_not_reached();
11862 uint32_t HELPER(recpe_f16)(uint32_t input, void *fpstp)
11864 float_status *fpst = fpstp;
11865 float16 f16 = float16_squash_input_denormal(input, fpst);
11866 uint32_t f16_val = float16_val(f16);
11867 uint32_t f16_sign = float16_is_neg(f16);
11868 int f16_exp = extract32(f16_val, 10, 5);
11869 uint32_t f16_frac = extract32(f16_val, 0, 10);
11870 uint64_t f64_frac;
11872 if (float16_is_any_nan(f16)) {
11873 float16 nan = f16;
11874 if (float16_is_signaling_nan(f16, fpst)) {
11875 float_raise(float_flag_invalid, fpst);
11876 nan = float16_silence_nan(f16, fpst);
11878 if (fpst->default_nan_mode) {
11879 nan = float16_default_nan(fpst);
11881 return nan;
11882 } else if (float16_is_infinity(f16)) {
11883 return float16_set_sign(float16_zero, float16_is_neg(f16));
11884 } else if (float16_is_zero(f16)) {
11885 float_raise(float_flag_divbyzero, fpst);
11886 return float16_set_sign(float16_infinity, float16_is_neg(f16));
11887 } else if (float16_abs(f16) < (1 << 8)) {
11888 /* Abs(value) < 2.0^-16 */
11889 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11890 if (round_to_inf(fpst, f16_sign)) {
11891 return float16_set_sign(float16_infinity, f16_sign);
11892 } else {
11893 return float16_set_sign(float16_maxnorm, f16_sign);
11895 } else if (f16_exp >= 29 && fpst->flush_to_zero) {
11896 float_raise(float_flag_underflow, fpst);
11897 return float16_set_sign(float16_zero, float16_is_neg(f16));
11900 f64_frac = call_recip_estimate(&f16_exp, 29,
11901 ((uint64_t) f16_frac) << (52 - 10));
11903 /* result = sign : result_exp<4:0> : fraction<51:42> */
11904 f16_val = deposit32(0, 15, 1, f16_sign);
11905 f16_val = deposit32(f16_val, 10, 5, f16_exp);
11906 f16_val = deposit32(f16_val, 0, 10, extract64(f64_frac, 52 - 10, 10));
11907 return make_float16(f16_val);
11910 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
11912 float_status *fpst = fpstp;
11913 float32 f32 = float32_squash_input_denormal(input, fpst);
11914 uint32_t f32_val = float32_val(f32);
11915 bool f32_sign = float32_is_neg(f32);
11916 int f32_exp = extract32(f32_val, 23, 8);
11917 uint32_t f32_frac = extract32(f32_val, 0, 23);
11918 uint64_t f64_frac;
11920 if (float32_is_any_nan(f32)) {
11921 float32 nan = f32;
11922 if (float32_is_signaling_nan(f32, fpst)) {
11923 float_raise(float_flag_invalid, fpst);
11924 nan = float32_silence_nan(f32, fpst);
11926 if (fpst->default_nan_mode) {
11927 nan = float32_default_nan(fpst);
11929 return nan;
11930 } else if (float32_is_infinity(f32)) {
11931 return float32_set_sign(float32_zero, float32_is_neg(f32));
11932 } else if (float32_is_zero(f32)) {
11933 float_raise(float_flag_divbyzero, fpst);
11934 return float32_set_sign(float32_infinity, float32_is_neg(f32));
11935 } else if (float32_abs(f32) < (1ULL << 21)) {
11936 /* Abs(value) < 2.0^-128 */
11937 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11938 if (round_to_inf(fpst, f32_sign)) {
11939 return float32_set_sign(float32_infinity, f32_sign);
11940 } else {
11941 return float32_set_sign(float32_maxnorm, f32_sign);
11943 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
11944 float_raise(float_flag_underflow, fpst);
11945 return float32_set_sign(float32_zero, float32_is_neg(f32));
11948 f64_frac = call_recip_estimate(&f32_exp, 253,
11949 ((uint64_t) f32_frac) << (52 - 23));
11951 /* result = sign : result_exp<7:0> : fraction<51:29> */
11952 f32_val = deposit32(0, 31, 1, f32_sign);
11953 f32_val = deposit32(f32_val, 23, 8, f32_exp);
11954 f32_val = deposit32(f32_val, 0, 23, extract64(f64_frac, 52 - 23, 23));
11955 return make_float32(f32_val);
11958 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
11960 float_status *fpst = fpstp;
11961 float64 f64 = float64_squash_input_denormal(input, fpst);
11962 uint64_t f64_val = float64_val(f64);
11963 bool f64_sign = float64_is_neg(f64);
11964 int f64_exp = extract64(f64_val, 52, 11);
11965 uint64_t f64_frac = extract64(f64_val, 0, 52);
11967 /* Deal with any special cases */
11968 if (float64_is_any_nan(f64)) {
11969 float64 nan = f64;
11970 if (float64_is_signaling_nan(f64, fpst)) {
11971 float_raise(float_flag_invalid, fpst);
11972 nan = float64_silence_nan(f64, fpst);
11974 if (fpst->default_nan_mode) {
11975 nan = float64_default_nan(fpst);
11977 return nan;
11978 } else if (float64_is_infinity(f64)) {
11979 return float64_set_sign(float64_zero, float64_is_neg(f64));
11980 } else if (float64_is_zero(f64)) {
11981 float_raise(float_flag_divbyzero, fpst);
11982 return float64_set_sign(float64_infinity, float64_is_neg(f64));
11983 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
11984 /* Abs(value) < 2.0^-1024 */
11985 float_raise(float_flag_overflow | float_flag_inexact, fpst);
11986 if (round_to_inf(fpst, f64_sign)) {
11987 return float64_set_sign(float64_infinity, f64_sign);
11988 } else {
11989 return float64_set_sign(float64_maxnorm, f64_sign);
11991 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
11992 float_raise(float_flag_underflow, fpst);
11993 return float64_set_sign(float64_zero, float64_is_neg(f64));
11996 f64_frac = call_recip_estimate(&f64_exp, 2045, f64_frac);
11998 /* result = sign : result_exp<10:0> : fraction<51:0>; */
11999 f64_val = deposit64(0, 63, 1, f64_sign);
12000 f64_val = deposit64(f64_val, 52, 11, f64_exp);
12001 f64_val = deposit64(f64_val, 0, 52, f64_frac);
12002 return make_float64(f64_val);
12005 /* The algorithm that must be used to calculate the estimate
12006 * is specified by the ARM ARM.
12009 static int do_recip_sqrt_estimate(int a)
12011 int b, estimate;
12013 assert(128 <= a && a < 512);
12014 if (a < 256) {
12015 a = a * 2 + 1;
12016 } else {
12017 a = (a >> 1) << 1;
12018 a = (a + 1) * 2;
12020 b = 512;
12021 while (a * (b + 1) * (b + 1) < (1 << 28)) {
12022 b += 1;
12024 estimate = (b + 1) / 2;
12025 assert(256 <= estimate && estimate < 512);
12027 return estimate;
12031 static uint64_t recip_sqrt_estimate(int *exp , int exp_off, uint64_t frac)
12033 int estimate;
12034 uint32_t scaled;
12036 if (*exp == 0) {
12037 while (extract64(frac, 51, 1) == 0) {
12038 frac = frac << 1;
12039 *exp -= 1;
12041 frac = extract64(frac, 0, 51) << 1;
12044 if (*exp & 1) {
12045 /* scaled = UInt('01':fraction<51:45>) */
12046 scaled = deposit32(1 << 7, 0, 7, extract64(frac, 45, 7));
12047 } else {
12048 /* scaled = UInt('1':fraction<51:44>) */
12049 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
12051 estimate = do_recip_sqrt_estimate(scaled);
12053 *exp = (exp_off - *exp) / 2;
12054 return extract64(estimate, 0, 8) << 44;
12057 uint32_t HELPER(rsqrte_f16)(uint32_t input, void *fpstp)
12059 float_status *s = fpstp;
12060 float16 f16 = float16_squash_input_denormal(input, s);
12061 uint16_t val = float16_val(f16);
12062 bool f16_sign = float16_is_neg(f16);
12063 int f16_exp = extract32(val, 10, 5);
12064 uint16_t f16_frac = extract32(val, 0, 10);
12065 uint64_t f64_frac;
12067 if (float16_is_any_nan(f16)) {
12068 float16 nan = f16;
12069 if (float16_is_signaling_nan(f16, s)) {
12070 float_raise(float_flag_invalid, s);
12071 nan = float16_silence_nan(f16, s);
12073 if (s->default_nan_mode) {
12074 nan = float16_default_nan(s);
12076 return nan;
12077 } else if (float16_is_zero(f16)) {
12078 float_raise(float_flag_divbyzero, s);
12079 return float16_set_sign(float16_infinity, f16_sign);
12080 } else if (f16_sign) {
12081 float_raise(float_flag_invalid, s);
12082 return float16_default_nan(s);
12083 } else if (float16_is_infinity(f16)) {
12084 return float16_zero;
12087 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
12088 * preserving the parity of the exponent. */
12090 f64_frac = ((uint64_t) f16_frac) << (52 - 10);
12092 f64_frac = recip_sqrt_estimate(&f16_exp, 44, f64_frac);
12094 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(2) */
12095 val = deposit32(0, 15, 1, f16_sign);
12096 val = deposit32(val, 10, 5, f16_exp);
12097 val = deposit32(val, 2, 8, extract64(f64_frac, 52 - 8, 8));
12098 return make_float16(val);
12101 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
12103 float_status *s = fpstp;
12104 float32 f32 = float32_squash_input_denormal(input, s);
12105 uint32_t val = float32_val(f32);
12106 uint32_t f32_sign = float32_is_neg(f32);
12107 int f32_exp = extract32(val, 23, 8);
12108 uint32_t f32_frac = extract32(val, 0, 23);
12109 uint64_t f64_frac;
12111 if (float32_is_any_nan(f32)) {
12112 float32 nan = f32;
12113 if (float32_is_signaling_nan(f32, s)) {
12114 float_raise(float_flag_invalid, s);
12115 nan = float32_silence_nan(f32, s);
12117 if (s->default_nan_mode) {
12118 nan = float32_default_nan(s);
12120 return nan;
12121 } else if (float32_is_zero(f32)) {
12122 float_raise(float_flag_divbyzero, s);
12123 return float32_set_sign(float32_infinity, float32_is_neg(f32));
12124 } else if (float32_is_neg(f32)) {
12125 float_raise(float_flag_invalid, s);
12126 return float32_default_nan(s);
12127 } else if (float32_is_infinity(f32)) {
12128 return float32_zero;
12131 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
12132 * preserving the parity of the exponent. */
12134 f64_frac = ((uint64_t) f32_frac) << 29;
12136 f64_frac = recip_sqrt_estimate(&f32_exp, 380, f64_frac);
12138 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(15) */
12139 val = deposit32(0, 31, 1, f32_sign);
12140 val = deposit32(val, 23, 8, f32_exp);
12141 val = deposit32(val, 15, 8, extract64(f64_frac, 52 - 8, 8));
12142 return make_float32(val);
12145 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
12147 float_status *s = fpstp;
12148 float64 f64 = float64_squash_input_denormal(input, s);
12149 uint64_t val = float64_val(f64);
12150 bool f64_sign = float64_is_neg(f64);
12151 int f64_exp = extract64(val, 52, 11);
12152 uint64_t f64_frac = extract64(val, 0, 52);
12154 if (float64_is_any_nan(f64)) {
12155 float64 nan = f64;
12156 if (float64_is_signaling_nan(f64, s)) {
12157 float_raise(float_flag_invalid, s);
12158 nan = float64_silence_nan(f64, s);
12160 if (s->default_nan_mode) {
12161 nan = float64_default_nan(s);
12163 return nan;
12164 } else if (float64_is_zero(f64)) {
12165 float_raise(float_flag_divbyzero, s);
12166 return float64_set_sign(float64_infinity, float64_is_neg(f64));
12167 } else if (float64_is_neg(f64)) {
12168 float_raise(float_flag_invalid, s);
12169 return float64_default_nan(s);
12170 } else if (float64_is_infinity(f64)) {
12171 return float64_zero;
12174 f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac);
12176 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */
12177 val = deposit64(0, 61, 1, f64_sign);
12178 val = deposit64(val, 52, 11, f64_exp);
12179 val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
12180 return make_float64(val);
12183 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
12185 /* float_status *s = fpstp; */
12186 int input, estimate;
12188 if ((a & 0x80000000) == 0) {
12189 return 0xffffffff;
12192 input = extract32(a, 23, 9);
12193 estimate = recip_estimate(input);
12195 return deposit32(0, (32 - 9), 9, estimate);
12198 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
12200 int estimate;
12202 if ((a & 0xc0000000) == 0) {
12203 return 0xffffffff;
12206 estimate = do_recip_sqrt_estimate(extract32(a, 23, 9));
12208 return deposit32(0, 23, 9, estimate);
12211 /* VFPv4 fused multiply-accumulate */
12212 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
12214 float_status *fpst = fpstp;
12215 return float32_muladd(a, b, c, 0, fpst);
12218 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
12220 float_status *fpst = fpstp;
12221 return float64_muladd(a, b, c, 0, fpst);
12224 /* ARMv8 round to integral */
12225 float32 HELPER(rints_exact)(float32 x, void *fp_status)
12227 return float32_round_to_int(x, fp_status);
12230 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
12232 return float64_round_to_int(x, fp_status);
12235 float32 HELPER(rints)(float32 x, void *fp_status)
12237 int old_flags = get_float_exception_flags(fp_status), new_flags;
12238 float32 ret;
12240 ret = float32_round_to_int(x, fp_status);
12242 /* Suppress any inexact exceptions the conversion produced */
12243 if (!(old_flags & float_flag_inexact)) {
12244 new_flags = get_float_exception_flags(fp_status);
12245 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
12248 return ret;
12251 float64 HELPER(rintd)(float64 x, void *fp_status)
12253 int old_flags = get_float_exception_flags(fp_status), new_flags;
12254 float64 ret;
12256 ret = float64_round_to_int(x, fp_status);
12258 new_flags = get_float_exception_flags(fp_status);
12260 /* Suppress any inexact exceptions the conversion produced */
12261 if (!(old_flags & float_flag_inexact)) {
12262 new_flags = get_float_exception_flags(fp_status);
12263 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
12266 return ret;
12269 /* Convert ARM rounding mode to softfloat */
12270 int arm_rmode_to_sf(int rmode)
12272 switch (rmode) {
12273 case FPROUNDING_TIEAWAY:
12274 rmode = float_round_ties_away;
12275 break;
12276 case FPROUNDING_ODD:
12277 /* FIXME: add support for TIEAWAY and ODD */
12278 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
12279 rmode);
12280 case FPROUNDING_TIEEVEN:
12281 default:
12282 rmode = float_round_nearest_even;
12283 break;
12284 case FPROUNDING_POSINF:
12285 rmode = float_round_up;
12286 break;
12287 case FPROUNDING_NEGINF:
12288 rmode = float_round_down;
12289 break;
12290 case FPROUNDING_ZERO:
12291 rmode = float_round_to_zero;
12292 break;
12294 return rmode;
12297 /* CRC helpers.
12298 * The upper bytes of val (above the number specified by 'bytes') must have
12299 * been zeroed out by the caller.
12301 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12303 uint8_t buf[4];
12305 stl_le_p(buf, val);
12307 /* zlib crc32 converts the accumulator and output to one's complement. */
12308 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12311 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12313 uint8_t buf[4];
12315 stl_le_p(buf, val);
12317 /* Linux crc32c converts the output to one's complement. */
12318 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12321 /* Return the exception level to which FP-disabled exceptions should
12322 * be taken, or 0 if FP is enabled.
12324 static inline int fp_exception_el(CPUARMState *env)
12326 #ifndef CONFIG_USER_ONLY
12327 int fpen;
12328 int cur_el = arm_current_el(env);
12330 /* CPACR and the CPTR registers don't exist before v6, so FP is
12331 * always accessible
12333 if (!arm_feature(env, ARM_FEATURE_V6)) {
12334 return 0;
12337 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12338 * 0, 2 : trap EL0 and EL1/PL1 accesses
12339 * 1 : trap only EL0 accesses
12340 * 3 : trap no accesses
12342 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12343 switch (fpen) {
12344 case 0:
12345 case 2:
12346 if (cur_el == 0 || cur_el == 1) {
12347 /* Trap to PL1, which might be EL1 or EL3 */
12348 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12349 return 3;
12351 return 1;
12353 if (cur_el == 3 && !is_a64(env)) {
12354 /* Secure PL1 running at EL3 */
12355 return 3;
12357 break;
12358 case 1:
12359 if (cur_el == 0) {
12360 return 1;
12362 break;
12363 case 3:
12364 break;
12367 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12368 * check because zero bits in the registers mean "don't trap".
12371 /* CPTR_EL2 : present in v7VE or v8 */
12372 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12373 && !arm_is_secure_below_el3(env)) {
12374 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12375 return 2;
12378 /* CPTR_EL3 : present in v8 */
12379 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12380 /* Trap all FP ops to EL3 */
12381 return 3;
12383 #endif
12384 return 0;
12387 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12388 target_ulong *cs_base, uint32_t *pflags)
12390 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false));
12391 int fp_el = fp_exception_el(env);
12392 uint32_t flags;
12394 if (is_a64(env)) {
12395 int sve_el = sve_exception_el(env);
12396 uint32_t zcr_len;
12398 *pc = env->pc;
12399 flags = ARM_TBFLAG_AARCH64_STATE_MASK;
12400 /* Get control bits for tagged addresses */
12401 flags |= (arm_regime_tbi0(env, mmu_idx) << ARM_TBFLAG_TBI0_SHIFT);
12402 flags |= (arm_regime_tbi1(env, mmu_idx) << ARM_TBFLAG_TBI1_SHIFT);
12403 flags |= sve_el << ARM_TBFLAG_SVEEXC_EL_SHIFT;
12405 /* If SVE is disabled, but FP is enabled,
12406 then the effective len is 0. */
12407 if (sve_el != 0 && fp_el == 0) {
12408 zcr_len = 0;
12409 } else {
12410 int current_el = arm_current_el(env);
12412 zcr_len = env->vfp.zcr_el[current_el <= 1 ? 1 : current_el];
12413 zcr_len &= 0xf;
12414 if (current_el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
12415 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
12417 if (current_el < 3 && arm_feature(env, ARM_FEATURE_EL3)) {
12418 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
12421 flags |= zcr_len << ARM_TBFLAG_ZCR_LEN_SHIFT;
12422 } else {
12423 *pc = env->regs[15];
12424 flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT)
12425 | (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT)
12426 | (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT)
12427 | (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT)
12428 | (arm_sctlr_b(env) << ARM_TBFLAG_SCTLR_B_SHIFT);
12429 if (!(access_secure_reg(env))) {
12430 flags |= ARM_TBFLAG_NS_MASK;
12432 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)
12433 || arm_el_is_aa64(env, 1)) {
12434 flags |= ARM_TBFLAG_VFPEN_MASK;
12436 flags |= (extract32(env->cp15.c15_cpar, 0, 2)
12437 << ARM_TBFLAG_XSCALE_CPAR_SHIFT);
12440 flags |= (arm_to_core_mmu_idx(mmu_idx) << ARM_TBFLAG_MMUIDX_SHIFT);
12442 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12443 * states defined in the ARM ARM for software singlestep:
12444 * SS_ACTIVE PSTATE.SS State
12445 * 0 x Inactive (the TB flag for SS is always 0)
12446 * 1 0 Active-pending
12447 * 1 1 Active-not-pending
12449 if (arm_singlestep_active(env)) {
12450 flags |= ARM_TBFLAG_SS_ACTIVE_MASK;
12451 if (is_a64(env)) {
12452 if (env->pstate & PSTATE_SS) {
12453 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
12455 } else {
12456 if (env->uncached_cpsr & PSTATE_SS) {
12457 flags |= ARM_TBFLAG_PSTATE_SS_MASK;
12461 if (arm_cpu_data_is_big_endian(env)) {
12462 flags |= ARM_TBFLAG_BE_DATA_MASK;
12464 flags |= fp_el << ARM_TBFLAG_FPEXC_EL_SHIFT;
12466 if (arm_v7m_is_handler_mode(env)) {
12467 flags |= ARM_TBFLAG_HANDLER_MASK;
12470 *pflags = flags;
12471 *cs_base = 0;