target/arm: expose MPIDR_EL1 to userspace
[qemu.git] / target / arm / helper.c
blob77c73056948b44a99acd6cdb471e78cd346e8f42
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/cpus.h"
19 #include "sysemu/kvm.h"
20 #include "fpu/softfloat.h"
21 #include "qemu/range.h"
23 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
25 #ifndef CONFIG_USER_ONLY
26 /* Cacheability and shareability attributes for a memory access */
27 typedef struct ARMCacheAttrs {
28 unsigned int attrs:8; /* as in the MAIR register encoding */
29 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */
30 } ARMCacheAttrs;
32 static bool get_phys_addr(CPUARMState *env, target_ulong address,
33 MMUAccessType access_type, ARMMMUIdx mmu_idx,
34 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
35 target_ulong *page_size,
36 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
38 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
39 MMUAccessType access_type, ARMMMUIdx mmu_idx,
40 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
41 target_ulong *page_size_ptr,
42 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
44 /* Security attributes for an address, as returned by v8m_security_lookup. */
45 typedef struct V8M_SAttributes {
46 bool subpage; /* true if these attrs don't cover the whole TARGET_PAGE */
47 bool ns;
48 bool nsc;
49 uint8_t sregion;
50 bool srvalid;
51 uint8_t iregion;
52 bool irvalid;
53 } V8M_SAttributes;
55 static void v8m_security_lookup(CPUARMState *env, uint32_t address,
56 MMUAccessType access_type, ARMMMUIdx mmu_idx,
57 V8M_SAttributes *sattrs);
58 #endif
60 static void switch_mode(CPUARMState *env, int mode);
62 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
64 int nregs;
66 /* VFP data registers are always little-endian. */
67 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
68 if (reg < nregs) {
69 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
70 return 8;
72 if (arm_feature(env, ARM_FEATURE_NEON)) {
73 /* Aliases for Q regs. */
74 nregs += 16;
75 if (reg < nregs) {
76 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
77 stq_le_p(buf, q[0]);
78 stq_le_p(buf + 8, q[1]);
79 return 16;
82 switch (reg - nregs) {
83 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
84 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
85 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
87 return 0;
90 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
92 int nregs;
94 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
95 if (reg < nregs) {
96 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
97 return 8;
99 if (arm_feature(env, ARM_FEATURE_NEON)) {
100 nregs += 16;
101 if (reg < nregs) {
102 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
103 q[0] = ldq_le_p(buf);
104 q[1] = ldq_le_p(buf + 8);
105 return 16;
108 switch (reg - nregs) {
109 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
110 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
111 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
113 return 0;
116 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
118 switch (reg) {
119 case 0 ... 31:
120 /* 128 bit FP register */
122 uint64_t *q = aa64_vfp_qreg(env, reg);
123 stq_le_p(buf, q[0]);
124 stq_le_p(buf + 8, q[1]);
125 return 16;
127 case 32:
128 /* FPSR */
129 stl_p(buf, vfp_get_fpsr(env));
130 return 4;
131 case 33:
132 /* FPCR */
133 stl_p(buf, vfp_get_fpcr(env));
134 return 4;
135 default:
136 return 0;
140 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
142 switch (reg) {
143 case 0 ... 31:
144 /* 128 bit FP register */
146 uint64_t *q = aa64_vfp_qreg(env, reg);
147 q[0] = ldq_le_p(buf);
148 q[1] = ldq_le_p(buf + 8);
149 return 16;
151 case 32:
152 /* FPSR */
153 vfp_set_fpsr(env, ldl_p(buf));
154 return 4;
155 case 33:
156 /* FPCR */
157 vfp_set_fpcr(env, ldl_p(buf));
158 return 4;
159 default:
160 return 0;
164 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
166 assert(ri->fieldoffset);
167 if (cpreg_field_is_64bit(ri)) {
168 return CPREG_FIELD64(env, ri);
169 } else {
170 return CPREG_FIELD32(env, ri);
174 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
175 uint64_t value)
177 assert(ri->fieldoffset);
178 if (cpreg_field_is_64bit(ri)) {
179 CPREG_FIELD64(env, ri) = value;
180 } else {
181 CPREG_FIELD32(env, ri) = value;
185 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
187 return (char *)env + ri->fieldoffset;
190 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
192 /* Raw read of a coprocessor register (as needed for migration, etc). */
193 if (ri->type & ARM_CP_CONST) {
194 return ri->resetvalue;
195 } else if (ri->raw_readfn) {
196 return ri->raw_readfn(env, ri);
197 } else if (ri->readfn) {
198 return ri->readfn(env, ri);
199 } else {
200 return raw_read(env, ri);
204 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
205 uint64_t v)
207 /* Raw write of a coprocessor register (as needed for migration, etc).
208 * Note that constant registers are treated as write-ignored; the
209 * caller should check for success by whether a readback gives the
210 * value written.
212 if (ri->type & ARM_CP_CONST) {
213 return;
214 } else if (ri->raw_writefn) {
215 ri->raw_writefn(env, ri, v);
216 } else if (ri->writefn) {
217 ri->writefn(env, ri, v);
218 } else {
219 raw_write(env, ri, v);
223 static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg)
225 ARMCPU *cpu = arm_env_get_cpu(env);
226 const ARMCPRegInfo *ri;
227 uint32_t key;
229 key = cpu->dyn_xml.cpregs_keys[reg];
230 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
231 if (ri) {
232 if (cpreg_field_is_64bit(ri)) {
233 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
234 } else {
235 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
238 return 0;
241 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
243 return 0;
246 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
248 /* Return true if the regdef would cause an assertion if you called
249 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
250 * program bug for it not to have the NO_RAW flag).
251 * NB that returning false here doesn't necessarily mean that calling
252 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
253 * read/write access functions which are safe for raw use" from "has
254 * read/write access functions which have side effects but has forgotten
255 * to provide raw access functions".
256 * The tests here line up with the conditions in read/write_raw_cp_reg()
257 * and assertions in raw_read()/raw_write().
259 if ((ri->type & ARM_CP_CONST) ||
260 ri->fieldoffset ||
261 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
262 return false;
264 return true;
267 bool write_cpustate_to_list(ARMCPU *cpu)
269 /* Write the coprocessor state from cpu->env to the (index,value) list. */
270 int i;
271 bool ok = true;
273 for (i = 0; i < cpu->cpreg_array_len; i++) {
274 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
275 const ARMCPRegInfo *ri;
277 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
278 if (!ri) {
279 ok = false;
280 continue;
282 if (ri->type & ARM_CP_NO_RAW) {
283 continue;
285 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
287 return ok;
290 bool write_list_to_cpustate(ARMCPU *cpu)
292 int i;
293 bool ok = true;
295 for (i = 0; i < cpu->cpreg_array_len; i++) {
296 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
297 uint64_t v = cpu->cpreg_values[i];
298 const ARMCPRegInfo *ri;
300 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
301 if (!ri) {
302 ok = false;
303 continue;
305 if (ri->type & ARM_CP_NO_RAW) {
306 continue;
308 /* Write value and confirm it reads back as written
309 * (to catch read-only registers and partially read-only
310 * registers where the incoming migration value doesn't match)
312 write_raw_cp_reg(&cpu->env, ri, v);
313 if (read_raw_cp_reg(&cpu->env, ri) != v) {
314 ok = false;
317 return ok;
320 static void add_cpreg_to_list(gpointer key, gpointer opaque)
322 ARMCPU *cpu = opaque;
323 uint64_t regidx;
324 const ARMCPRegInfo *ri;
326 regidx = *(uint32_t *)key;
327 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
329 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
330 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
331 /* The value array need not be initialized at this point */
332 cpu->cpreg_array_len++;
336 static void count_cpreg(gpointer key, gpointer opaque)
338 ARMCPU *cpu = opaque;
339 uint64_t regidx;
340 const ARMCPRegInfo *ri;
342 regidx = *(uint32_t *)key;
343 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
345 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
346 cpu->cpreg_array_len++;
350 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
352 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
353 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
355 if (aidx > bidx) {
356 return 1;
358 if (aidx < bidx) {
359 return -1;
361 return 0;
364 void init_cpreg_list(ARMCPU *cpu)
366 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
367 * Note that we require cpreg_tuples[] to be sorted by key ID.
369 GList *keys;
370 int arraylen;
372 keys = g_hash_table_get_keys(cpu->cp_regs);
373 keys = g_list_sort(keys, cpreg_key_compare);
375 cpu->cpreg_array_len = 0;
377 g_list_foreach(keys, count_cpreg, cpu);
379 arraylen = cpu->cpreg_array_len;
380 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
381 cpu->cpreg_values = g_new(uint64_t, arraylen);
382 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
383 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
384 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
385 cpu->cpreg_array_len = 0;
387 g_list_foreach(keys, add_cpreg_to_list, cpu);
389 assert(cpu->cpreg_array_len == arraylen);
391 g_list_free(keys);
395 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
396 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
398 * access_el3_aa32ns: Used to check AArch32 register views.
399 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
401 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
402 const ARMCPRegInfo *ri,
403 bool isread)
405 bool secure = arm_is_secure_below_el3(env);
407 assert(!arm_el_is_aa64(env, 3));
408 if (secure) {
409 return CP_ACCESS_TRAP_UNCATEGORIZED;
411 return CP_ACCESS_OK;
414 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
415 const ARMCPRegInfo *ri,
416 bool isread)
418 if (!arm_el_is_aa64(env, 3)) {
419 return access_el3_aa32ns(env, ri, isread);
421 return CP_ACCESS_OK;
424 /* Some secure-only AArch32 registers trap to EL3 if used from
425 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
426 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
427 * We assume that the .access field is set to PL1_RW.
429 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
430 const ARMCPRegInfo *ri,
431 bool isread)
433 if (arm_current_el(env) == 3) {
434 return CP_ACCESS_OK;
436 if (arm_is_secure_below_el3(env)) {
437 return CP_ACCESS_TRAP_EL3;
439 /* This will be EL1 NS and EL2 NS, which just UNDEF */
440 return CP_ACCESS_TRAP_UNCATEGORIZED;
443 /* Check for traps to "powerdown debug" registers, which are controlled
444 * by MDCR.TDOSA
446 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
447 bool isread)
449 int el = arm_current_el(env);
450 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) ||
451 (env->cp15.mdcr_el2 & MDCR_TDE) ||
452 (arm_hcr_el2_eff(env) & HCR_TGE);
454 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) {
455 return CP_ACCESS_TRAP_EL2;
457 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
458 return CP_ACCESS_TRAP_EL3;
460 return CP_ACCESS_OK;
463 /* Check for traps to "debug ROM" registers, which are controlled
464 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
466 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
467 bool isread)
469 int el = arm_current_el(env);
470 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) ||
471 (env->cp15.mdcr_el2 & MDCR_TDE) ||
472 (arm_hcr_el2_eff(env) & HCR_TGE);
474 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) {
475 return CP_ACCESS_TRAP_EL2;
477 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
478 return CP_ACCESS_TRAP_EL3;
480 return CP_ACCESS_OK;
483 /* Check for traps to general debug registers, which are controlled
484 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
486 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
487 bool isread)
489 int el = arm_current_el(env);
490 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) ||
491 (env->cp15.mdcr_el2 & MDCR_TDE) ||
492 (arm_hcr_el2_eff(env) & HCR_TGE);
494 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) {
495 return CP_ACCESS_TRAP_EL2;
497 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
498 return CP_ACCESS_TRAP_EL3;
500 return CP_ACCESS_OK;
503 /* Check for traps to performance monitor registers, which are controlled
504 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
506 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
507 bool isread)
509 int el = arm_current_el(env);
511 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
512 && !arm_is_secure_below_el3(env)) {
513 return CP_ACCESS_TRAP_EL2;
515 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
516 return CP_ACCESS_TRAP_EL3;
518 return CP_ACCESS_OK;
521 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
523 ARMCPU *cpu = arm_env_get_cpu(env);
525 raw_write(env, ri, value);
526 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
529 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
531 ARMCPU *cpu = arm_env_get_cpu(env);
533 if (raw_read(env, ri) != value) {
534 /* Unlike real hardware the qemu TLB uses virtual addresses,
535 * not modified virtual addresses, so this causes a TLB flush.
537 tlb_flush(CPU(cpu));
538 raw_write(env, ri, value);
542 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
543 uint64_t value)
545 ARMCPU *cpu = arm_env_get_cpu(env);
547 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
548 && !extended_addresses_enabled(env)) {
549 /* For VMSA (when not using the LPAE long descriptor page table
550 * format) this register includes the ASID, so do a TLB flush.
551 * For PMSA it is purely a process ID and no action is needed.
553 tlb_flush(CPU(cpu));
555 raw_write(env, ri, value);
558 /* IS variants of TLB operations must affect all cores */
559 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
560 uint64_t value)
562 CPUState *cs = ENV_GET_CPU(env);
564 tlb_flush_all_cpus_synced(cs);
567 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
568 uint64_t value)
570 CPUState *cs = ENV_GET_CPU(env);
572 tlb_flush_all_cpus_synced(cs);
575 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
576 uint64_t value)
578 CPUState *cs = ENV_GET_CPU(env);
580 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
583 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
584 uint64_t value)
586 CPUState *cs = ENV_GET_CPU(env);
588 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
592 * Non-IS variants of TLB operations are upgraded to
593 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to
594 * force broadcast of these operations.
596 static bool tlb_force_broadcast(CPUARMState *env)
598 return (env->cp15.hcr_el2 & HCR_FB) &&
599 arm_current_el(env) == 1 && arm_is_secure_below_el3(env);
602 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
603 uint64_t value)
605 /* Invalidate all (TLBIALL) */
606 ARMCPU *cpu = arm_env_get_cpu(env);
608 if (tlb_force_broadcast(env)) {
609 tlbiall_is_write(env, NULL, value);
610 return;
613 tlb_flush(CPU(cpu));
616 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
617 uint64_t value)
619 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
620 ARMCPU *cpu = arm_env_get_cpu(env);
622 if (tlb_force_broadcast(env)) {
623 tlbimva_is_write(env, NULL, value);
624 return;
627 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
630 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
631 uint64_t value)
633 /* Invalidate by ASID (TLBIASID) */
634 ARMCPU *cpu = arm_env_get_cpu(env);
636 if (tlb_force_broadcast(env)) {
637 tlbiasid_is_write(env, NULL, value);
638 return;
641 tlb_flush(CPU(cpu));
644 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
645 uint64_t value)
647 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
648 ARMCPU *cpu = arm_env_get_cpu(env);
650 if (tlb_force_broadcast(env)) {
651 tlbimvaa_is_write(env, NULL, value);
652 return;
655 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
658 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
659 uint64_t value)
661 CPUState *cs = ENV_GET_CPU(env);
663 tlb_flush_by_mmuidx(cs,
664 ARMMMUIdxBit_S12NSE1 |
665 ARMMMUIdxBit_S12NSE0 |
666 ARMMMUIdxBit_S2NS);
669 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
670 uint64_t value)
672 CPUState *cs = ENV_GET_CPU(env);
674 tlb_flush_by_mmuidx_all_cpus_synced(cs,
675 ARMMMUIdxBit_S12NSE1 |
676 ARMMMUIdxBit_S12NSE0 |
677 ARMMMUIdxBit_S2NS);
680 static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
681 uint64_t value)
683 /* Invalidate by IPA. This has to invalidate any structures that
684 * contain only stage 2 translation information, but does not need
685 * to apply to structures that contain combined stage 1 and stage 2
686 * translation information.
687 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
689 CPUState *cs = ENV_GET_CPU(env);
690 uint64_t pageaddr;
692 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
693 return;
696 pageaddr = sextract64(value << 12, 0, 40);
698 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
701 static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
702 uint64_t value)
704 CPUState *cs = ENV_GET_CPU(env);
705 uint64_t pageaddr;
707 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
708 return;
711 pageaddr = sextract64(value << 12, 0, 40);
713 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
714 ARMMMUIdxBit_S2NS);
717 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
718 uint64_t value)
720 CPUState *cs = ENV_GET_CPU(env);
722 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
725 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
726 uint64_t value)
728 CPUState *cs = ENV_GET_CPU(env);
730 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
733 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
734 uint64_t value)
736 CPUState *cs = ENV_GET_CPU(env);
737 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
739 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
742 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
743 uint64_t value)
745 CPUState *cs = ENV_GET_CPU(env);
746 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
748 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
749 ARMMMUIdxBit_S1E2);
752 static const ARMCPRegInfo cp_reginfo[] = {
753 /* Define the secure and non-secure FCSE identifier CP registers
754 * separately because there is no secure bank in V8 (no _EL3). This allows
755 * the secure register to be properly reset and migrated. There is also no
756 * v8 EL1 version of the register so the non-secure instance stands alone.
758 { .name = "FCSEIDR",
759 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
760 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
761 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
762 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
763 { .name = "FCSEIDR_S",
764 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
765 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
766 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
767 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
768 /* Define the secure and non-secure context identifier CP registers
769 * separately because there is no secure bank in V8 (no _EL3). This allows
770 * the secure register to be properly reset and migrated. In the
771 * non-secure case, the 32-bit register will have reset and migration
772 * disabled during registration as it is handled by the 64-bit instance.
774 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
775 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
776 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
777 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
778 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
779 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
780 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
781 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
782 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
783 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
784 REGINFO_SENTINEL
787 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
788 /* NB: Some of these registers exist in v8 but with more precise
789 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
791 /* MMU Domain access control / MPU write buffer control */
792 { .name = "DACR",
793 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
794 .access = PL1_RW, .resetvalue = 0,
795 .writefn = dacr_write, .raw_writefn = raw_write,
796 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
797 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
798 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
799 * For v6 and v5, these mappings are overly broad.
801 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
802 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
803 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
804 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
805 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
806 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
807 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
808 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
809 /* Cache maintenance ops; some of this space may be overridden later. */
810 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
811 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
812 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
813 REGINFO_SENTINEL
816 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
817 /* Not all pre-v6 cores implemented this WFI, so this is slightly
818 * over-broad.
820 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
821 .access = PL1_W, .type = ARM_CP_WFI },
822 REGINFO_SENTINEL
825 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
826 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
827 * is UNPREDICTABLE; we choose to NOP as most implementations do).
829 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
830 .access = PL1_W, .type = ARM_CP_WFI },
831 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
832 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
833 * OMAPCP will override this space.
835 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
836 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
837 .resetvalue = 0 },
838 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
839 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
840 .resetvalue = 0 },
841 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
842 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
843 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
844 .resetvalue = 0 },
845 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
846 * implementing it as RAZ means the "debug architecture version" bits
847 * will read as a reserved value, which should cause Linux to not try
848 * to use the debug hardware.
850 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
851 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
852 /* MMU TLB control. Note that the wildcarding means we cover not just
853 * the unified TLB ops but also the dside/iside/inner-shareable variants.
855 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
856 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
857 .type = ARM_CP_NO_RAW },
858 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
859 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
860 .type = ARM_CP_NO_RAW },
861 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
862 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
863 .type = ARM_CP_NO_RAW },
864 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
865 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
866 .type = ARM_CP_NO_RAW },
867 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
868 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
869 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
870 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
871 REGINFO_SENTINEL
874 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
875 uint64_t value)
877 uint32_t mask = 0;
879 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
880 if (!arm_feature(env, ARM_FEATURE_V8)) {
881 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
882 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
883 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
885 if (arm_feature(env, ARM_FEATURE_VFP)) {
886 /* VFP coprocessor: cp10 & cp11 [23:20] */
887 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
889 if (!arm_feature(env, ARM_FEATURE_NEON)) {
890 /* ASEDIS [31] bit is RAO/WI */
891 value |= (1 << 31);
894 /* VFPv3 and upwards with NEON implement 32 double precision
895 * registers (D0-D31).
897 if (!arm_feature(env, ARM_FEATURE_NEON) ||
898 !arm_feature(env, ARM_FEATURE_VFP3)) {
899 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
900 value |= (1 << 30);
903 value &= mask;
905 env->cp15.cpacr_el1 = value;
908 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
910 /* Call cpacr_write() so that we reset with the correct RAO bits set
911 * for our CPU features.
913 cpacr_write(env, ri, 0);
916 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
917 bool isread)
919 if (arm_feature(env, ARM_FEATURE_V8)) {
920 /* Check if CPACR accesses are to be trapped to EL2 */
921 if (arm_current_el(env) == 1 &&
922 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
923 return CP_ACCESS_TRAP_EL2;
924 /* Check if CPACR accesses are to be trapped to EL3 */
925 } else if (arm_current_el(env) < 3 &&
926 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
927 return CP_ACCESS_TRAP_EL3;
931 return CP_ACCESS_OK;
934 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
935 bool isread)
937 /* Check if CPTR accesses are set to trap to EL3 */
938 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
939 return CP_ACCESS_TRAP_EL3;
942 return CP_ACCESS_OK;
945 static const ARMCPRegInfo v6_cp_reginfo[] = {
946 /* prefetch by MVA in v6, NOP in v7 */
947 { .name = "MVA_prefetch",
948 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
949 .access = PL1_W, .type = ARM_CP_NOP },
950 /* We need to break the TB after ISB to execute self-modifying code
951 * correctly and also to take any pending interrupts immediately.
952 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
954 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
955 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
956 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
957 .access = PL0_W, .type = ARM_CP_NOP },
958 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
959 .access = PL0_W, .type = ARM_CP_NOP },
960 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
961 .access = PL1_RW,
962 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
963 offsetof(CPUARMState, cp15.ifar_ns) },
964 .resetvalue = 0, },
965 /* Watchpoint Fault Address Register : should actually only be present
966 * for 1136, 1176, 11MPCore.
968 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
969 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
970 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
971 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
972 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
973 .resetfn = cpacr_reset, .writefn = cpacr_write },
974 REGINFO_SENTINEL
977 /* Definitions for the PMU registers */
978 #define PMCRN_MASK 0xf800
979 #define PMCRN_SHIFT 11
980 #define PMCRLC 0x40
981 #define PMCRDP 0x10
982 #define PMCRD 0x8
983 #define PMCRC 0x4
984 #define PMCRP 0x2
985 #define PMCRE 0x1
987 #define PMXEVTYPER_P 0x80000000
988 #define PMXEVTYPER_U 0x40000000
989 #define PMXEVTYPER_NSK 0x20000000
990 #define PMXEVTYPER_NSU 0x10000000
991 #define PMXEVTYPER_NSH 0x08000000
992 #define PMXEVTYPER_M 0x04000000
993 #define PMXEVTYPER_MT 0x02000000
994 #define PMXEVTYPER_EVTCOUNT 0x0000ffff
995 #define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
996 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
997 PMXEVTYPER_M | PMXEVTYPER_MT | \
998 PMXEVTYPER_EVTCOUNT)
1000 #define PMCCFILTR 0xf8000000
1001 #define PMCCFILTR_M PMXEVTYPER_M
1002 #define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M)
1004 static inline uint32_t pmu_num_counters(CPUARMState *env)
1006 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1009 /* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1010 static inline uint64_t pmu_counter_mask(CPUARMState *env)
1012 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1015 typedef struct pm_event {
1016 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1017 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1018 bool (*supported)(CPUARMState *);
1020 * Retrieve the current count of the underlying event. The programmed
1021 * counters hold a difference from the return value from this function
1023 uint64_t (*get_count)(CPUARMState *);
1025 * Return how many nanoseconds it will take (at a minimum) for count events
1026 * to occur. A negative value indicates the counter will never overflow, or
1027 * that the counter has otherwise arranged for the overflow bit to be set
1028 * and the PMU interrupt to be raised on overflow.
1030 int64_t (*ns_per_count)(uint64_t);
1031 } pm_event;
1033 static bool event_always_supported(CPUARMState *env)
1035 return true;
1038 static uint64_t swinc_get_count(CPUARMState *env)
1041 * SW_INCR events are written directly to the pmevcntr's by writes to
1042 * PMSWINC, so there is no underlying count maintained by the PMU itself
1044 return 0;
1047 static int64_t swinc_ns_per(uint64_t ignored)
1049 return -1;
1053 * Return the underlying cycle count for the PMU cycle counters. If we're in
1054 * usermode, simply return 0.
1056 static uint64_t cycles_get_count(CPUARMState *env)
1058 #ifndef CONFIG_USER_ONLY
1059 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1060 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1061 #else
1062 return cpu_get_host_ticks();
1063 #endif
1066 #ifndef CONFIG_USER_ONLY
1067 static int64_t cycles_ns_per(uint64_t cycles)
1069 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1072 static bool instructions_supported(CPUARMState *env)
1074 return use_icount == 1 /* Precise instruction counting */;
1077 static uint64_t instructions_get_count(CPUARMState *env)
1079 return (uint64_t)cpu_get_icount_raw();
1082 static int64_t instructions_ns_per(uint64_t icount)
1084 return cpu_icount_to_ns((int64_t)icount);
1086 #endif
1088 static const pm_event pm_events[] = {
1089 { .number = 0x000, /* SW_INCR */
1090 .supported = event_always_supported,
1091 .get_count = swinc_get_count,
1092 .ns_per_count = swinc_ns_per,
1094 #ifndef CONFIG_USER_ONLY
1095 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1096 .supported = instructions_supported,
1097 .get_count = instructions_get_count,
1098 .ns_per_count = instructions_ns_per,
1100 { .number = 0x011, /* CPU_CYCLES, Cycle */
1101 .supported = event_always_supported,
1102 .get_count = cycles_get_count,
1103 .ns_per_count = cycles_ns_per,
1105 #endif
1109 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1110 * events (i.e. the statistical profiling extension), this implementation
1111 * should first be updated to something sparse instead of the current
1112 * supported_event_map[] array.
1114 #define MAX_EVENT_ID 0x11
1115 #define UNSUPPORTED_EVENT UINT16_MAX
1116 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1119 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1120 * of ARM event numbers to indices in our pm_events array.
1122 * Note: Events in the 0x40XX range are not currently supported.
1124 void pmu_init(ARMCPU *cpu)
1126 unsigned int i;
1129 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1130 * events to them
1132 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1133 supported_event_map[i] = UNSUPPORTED_EVENT;
1135 cpu->pmceid0 = 0;
1136 cpu->pmceid1 = 0;
1138 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1139 const pm_event *cnt = &pm_events[i];
1140 assert(cnt->number <= MAX_EVENT_ID);
1141 /* We do not currently support events in the 0x40xx range */
1142 assert(cnt->number <= 0x3f);
1144 if (cnt->supported(&cpu->env)) {
1145 supported_event_map[cnt->number] = i;
1146 uint64_t event_mask = 1 << (cnt->number & 0x1f);
1147 if (cnt->number & 0x20) {
1148 cpu->pmceid1 |= event_mask;
1149 } else {
1150 cpu->pmceid0 |= event_mask;
1157 * Check at runtime whether a PMU event is supported for the current machine
1159 static bool event_supported(uint16_t number)
1161 if (number > MAX_EVENT_ID) {
1162 return false;
1164 return supported_event_map[number] != UNSUPPORTED_EVENT;
1167 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1168 bool isread)
1170 /* Performance monitor registers user accessibility is controlled
1171 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1172 * trapping to EL2 or EL3 for other accesses.
1174 int el = arm_current_el(env);
1176 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1177 return CP_ACCESS_TRAP;
1179 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
1180 && !arm_is_secure_below_el3(env)) {
1181 return CP_ACCESS_TRAP_EL2;
1183 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1184 return CP_ACCESS_TRAP_EL3;
1187 return CP_ACCESS_OK;
1190 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1191 const ARMCPRegInfo *ri,
1192 bool isread)
1194 /* ER: event counter read trap control */
1195 if (arm_feature(env, ARM_FEATURE_V8)
1196 && arm_current_el(env) == 0
1197 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1198 && isread) {
1199 return CP_ACCESS_OK;
1202 return pmreg_access(env, ri, isread);
1205 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1206 const ARMCPRegInfo *ri,
1207 bool isread)
1209 /* SW: software increment write trap control */
1210 if (arm_feature(env, ARM_FEATURE_V8)
1211 && arm_current_el(env) == 0
1212 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1213 && !isread) {
1214 return CP_ACCESS_OK;
1217 return pmreg_access(env, ri, isread);
1220 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1221 const ARMCPRegInfo *ri,
1222 bool isread)
1224 /* ER: event counter read trap control */
1225 if (arm_feature(env, ARM_FEATURE_V8)
1226 && arm_current_el(env) == 0
1227 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1228 return CP_ACCESS_OK;
1231 return pmreg_access(env, ri, isread);
1234 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1235 const ARMCPRegInfo *ri,
1236 bool isread)
1238 /* CR: cycle counter read trap control */
1239 if (arm_feature(env, ARM_FEATURE_V8)
1240 && arm_current_el(env) == 0
1241 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1242 && isread) {
1243 return CP_ACCESS_OK;
1246 return pmreg_access(env, ri, isread);
1249 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1250 * the current EL, security state, and register configuration.
1252 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1254 uint64_t filter;
1255 bool e, p, u, nsk, nsu, nsh, m;
1256 bool enabled, prohibited, filtered;
1257 bool secure = arm_is_secure(env);
1258 int el = arm_current_el(env);
1259 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1261 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1262 (counter < hpmn || counter == 31)) {
1263 e = env->cp15.c9_pmcr & PMCRE;
1264 } else {
1265 e = env->cp15.mdcr_el2 & MDCR_HPME;
1267 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1269 if (!secure) {
1270 if (el == 2 && (counter < hpmn || counter == 31)) {
1271 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD;
1272 } else {
1273 prohibited = false;
1275 } else {
1276 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1277 (env->cp15.mdcr_el3 & MDCR_SPME);
1280 if (prohibited && counter == 31) {
1281 prohibited = env->cp15.c9_pmcr & PMCRDP;
1284 if (counter == 31) {
1285 filter = env->cp15.pmccfiltr_el0;
1286 } else {
1287 filter = env->cp15.c14_pmevtyper[counter];
1290 p = filter & PMXEVTYPER_P;
1291 u = filter & PMXEVTYPER_U;
1292 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1293 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1294 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1295 m = arm_el_is_aa64(env, 1) &&
1296 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1298 if (el == 0) {
1299 filtered = secure ? u : u != nsu;
1300 } else if (el == 1) {
1301 filtered = secure ? p : p != nsk;
1302 } else if (el == 2) {
1303 filtered = !nsh;
1304 } else { /* EL3 */
1305 filtered = m != p;
1308 if (counter != 31) {
1310 * If not checking PMCCNTR, ensure the counter is setup to an event we
1311 * support
1313 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1314 if (!event_supported(event)) {
1315 return false;
1319 return enabled && !prohibited && !filtered;
1322 static void pmu_update_irq(CPUARMState *env)
1324 ARMCPU *cpu = arm_env_get_cpu(env);
1325 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1326 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1330 * Ensure c15_ccnt is the guest-visible count so that operations such as
1331 * enabling/disabling the counter or filtering, modifying the count itself,
1332 * etc. can be done logically. This is essentially a no-op if the counter is
1333 * not enabled at the time of the call.
1335 void pmccntr_op_start(CPUARMState *env)
1337 uint64_t cycles = cycles_get_count(env);
1339 if (pmu_counter_enabled(env, 31)) {
1340 uint64_t eff_cycles = cycles;
1341 if (env->cp15.c9_pmcr & PMCRD) {
1342 /* Increment once every 64 processor clock cycles */
1343 eff_cycles /= 64;
1346 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1348 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1349 1ull << 63 : 1ull << 31;
1350 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1351 env->cp15.c9_pmovsr |= (1 << 31);
1352 pmu_update_irq(env);
1355 env->cp15.c15_ccnt = new_pmccntr;
1357 env->cp15.c15_ccnt_delta = cycles;
1361 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1362 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1363 * pmccntr_op_start.
1365 void pmccntr_op_finish(CPUARMState *env)
1367 if (pmu_counter_enabled(env, 31)) {
1368 #ifndef CONFIG_USER_ONLY
1369 /* Calculate when the counter will next overflow */
1370 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1371 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1372 remaining_cycles = (uint32_t)remaining_cycles;
1374 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1376 if (overflow_in > 0) {
1377 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1378 overflow_in;
1379 ARMCPU *cpu = arm_env_get_cpu(env);
1380 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1382 #endif
1384 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1385 if (env->cp15.c9_pmcr & PMCRD) {
1386 /* Increment once every 64 processor clock cycles */
1387 prev_cycles /= 64;
1389 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1393 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1396 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1397 uint64_t count = 0;
1398 if (event_supported(event)) {
1399 uint16_t event_idx = supported_event_map[event];
1400 count = pm_events[event_idx].get_count(env);
1403 if (pmu_counter_enabled(env, counter)) {
1404 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1406 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1407 env->cp15.c9_pmovsr |= (1 << counter);
1408 pmu_update_irq(env);
1410 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1412 env->cp15.c14_pmevcntr_delta[counter] = count;
1415 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1417 if (pmu_counter_enabled(env, counter)) {
1418 #ifndef CONFIG_USER_ONLY
1419 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1420 uint16_t event_idx = supported_event_map[event];
1421 uint64_t delta = UINT32_MAX -
1422 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1423 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1425 if (overflow_in > 0) {
1426 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1427 overflow_in;
1428 ARMCPU *cpu = arm_env_get_cpu(env);
1429 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1431 #endif
1433 env->cp15.c14_pmevcntr_delta[counter] -=
1434 env->cp15.c14_pmevcntr[counter];
1438 void pmu_op_start(CPUARMState *env)
1440 unsigned int i;
1441 pmccntr_op_start(env);
1442 for (i = 0; i < pmu_num_counters(env); i++) {
1443 pmevcntr_op_start(env, i);
1447 void pmu_op_finish(CPUARMState *env)
1449 unsigned int i;
1450 pmccntr_op_finish(env);
1451 for (i = 0; i < pmu_num_counters(env); i++) {
1452 pmevcntr_op_finish(env, i);
1456 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1458 pmu_op_start(&cpu->env);
1461 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1463 pmu_op_finish(&cpu->env);
1466 void arm_pmu_timer_cb(void *opaque)
1468 ARMCPU *cpu = opaque;
1471 * Update all the counter values based on the current underlying counts,
1472 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1473 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1474 * counter may expire.
1476 pmu_op_start(&cpu->env);
1477 pmu_op_finish(&cpu->env);
1480 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1481 uint64_t value)
1483 pmu_op_start(env);
1485 if (value & PMCRC) {
1486 /* The counter has been reset */
1487 env->cp15.c15_ccnt = 0;
1490 if (value & PMCRP) {
1491 unsigned int i;
1492 for (i = 0; i < pmu_num_counters(env); i++) {
1493 env->cp15.c14_pmevcntr[i] = 0;
1497 /* only the DP, X, D and E bits are writable */
1498 env->cp15.c9_pmcr &= ~0x39;
1499 env->cp15.c9_pmcr |= (value & 0x39);
1501 pmu_op_finish(env);
1504 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1505 uint64_t value)
1507 unsigned int i;
1508 for (i = 0; i < pmu_num_counters(env); i++) {
1509 /* Increment a counter's count iff: */
1510 if ((value & (1 << i)) && /* counter's bit is set */
1511 /* counter is enabled and not filtered */
1512 pmu_counter_enabled(env, i) &&
1513 /* counter is SW_INCR */
1514 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1515 pmevcntr_op_start(env, i);
1518 * Detect if this write causes an overflow since we can't predict
1519 * PMSWINC overflows like we can for other events
1521 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1523 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1524 env->cp15.c9_pmovsr |= (1 << i);
1525 pmu_update_irq(env);
1528 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1530 pmevcntr_op_finish(env, i);
1535 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1537 uint64_t ret;
1538 pmccntr_op_start(env);
1539 ret = env->cp15.c15_ccnt;
1540 pmccntr_op_finish(env);
1541 return ret;
1544 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1545 uint64_t value)
1547 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1548 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1549 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1550 * accessed.
1552 env->cp15.c9_pmselr = value & 0x1f;
1555 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1556 uint64_t value)
1558 pmccntr_op_start(env);
1559 env->cp15.c15_ccnt = value;
1560 pmccntr_op_finish(env);
1563 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1564 uint64_t value)
1566 uint64_t cur_val = pmccntr_read(env, NULL);
1568 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1571 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1572 uint64_t value)
1574 pmccntr_op_start(env);
1575 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1576 pmccntr_op_finish(env);
1579 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1580 uint64_t value)
1582 pmccntr_op_start(env);
1583 /* M is not accessible from AArch32 */
1584 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1585 (value & PMCCFILTR);
1586 pmccntr_op_finish(env);
1589 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1591 /* M is not visible in AArch32 */
1592 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1595 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1596 uint64_t value)
1598 value &= pmu_counter_mask(env);
1599 env->cp15.c9_pmcnten |= value;
1602 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1603 uint64_t value)
1605 value &= pmu_counter_mask(env);
1606 env->cp15.c9_pmcnten &= ~value;
1609 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1610 uint64_t value)
1612 value &= pmu_counter_mask(env);
1613 env->cp15.c9_pmovsr &= ~value;
1614 pmu_update_irq(env);
1617 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1618 uint64_t value)
1620 value &= pmu_counter_mask(env);
1621 env->cp15.c9_pmovsr |= value;
1622 pmu_update_irq(env);
1625 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1626 uint64_t value, const uint8_t counter)
1628 if (counter == 31) {
1629 pmccfiltr_write(env, ri, value);
1630 } else if (counter < pmu_num_counters(env)) {
1631 pmevcntr_op_start(env, counter);
1634 * If this counter's event type is changing, store the current
1635 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1636 * pmevcntr_op_finish has the correct baseline when it converts back to
1637 * a delta.
1639 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1640 PMXEVTYPER_EVTCOUNT;
1641 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1642 if (old_event != new_event) {
1643 uint64_t count = 0;
1644 if (event_supported(new_event)) {
1645 uint16_t event_idx = supported_event_map[new_event];
1646 count = pm_events[event_idx].get_count(env);
1648 env->cp15.c14_pmevcntr_delta[counter] = count;
1651 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1652 pmevcntr_op_finish(env, counter);
1654 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1655 * PMSELR value is equal to or greater than the number of implemented
1656 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1660 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1661 const uint8_t counter)
1663 if (counter == 31) {
1664 return env->cp15.pmccfiltr_el0;
1665 } else if (counter < pmu_num_counters(env)) {
1666 return env->cp15.c14_pmevtyper[counter];
1667 } else {
1669 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1670 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1672 return 0;
1676 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1677 uint64_t value)
1679 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1680 pmevtyper_write(env, ri, value, counter);
1683 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1684 uint64_t value)
1686 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1687 env->cp15.c14_pmevtyper[counter] = value;
1690 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1691 * pmu_op_finish calls when loading saved state for a migration. Because
1692 * we're potentially updating the type of event here, the value written to
1693 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1694 * different counter type. Therefore, we need to set this value to the
1695 * current count for the counter type we're writing so that pmu_op_finish
1696 * has the correct count for its calculation.
1698 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1699 if (event_supported(event)) {
1700 uint16_t event_idx = supported_event_map[event];
1701 env->cp15.c14_pmevcntr_delta[counter] =
1702 pm_events[event_idx].get_count(env);
1706 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1708 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1709 return pmevtyper_read(env, ri, counter);
1712 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1713 uint64_t value)
1715 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1718 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1720 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1723 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1724 uint64_t value, uint8_t counter)
1726 if (counter < pmu_num_counters(env)) {
1727 pmevcntr_op_start(env, counter);
1728 env->cp15.c14_pmevcntr[counter] = value;
1729 pmevcntr_op_finish(env, counter);
1732 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1733 * are CONSTRAINED UNPREDICTABLE.
1737 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1738 uint8_t counter)
1740 if (counter < pmu_num_counters(env)) {
1741 uint64_t ret;
1742 pmevcntr_op_start(env, counter);
1743 ret = env->cp15.c14_pmevcntr[counter];
1744 pmevcntr_op_finish(env, counter);
1745 return ret;
1746 } else {
1747 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1748 * are CONSTRAINED UNPREDICTABLE. */
1749 return 0;
1753 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1754 uint64_t value)
1756 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1757 pmevcntr_write(env, ri, value, counter);
1760 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1762 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1763 return pmevcntr_read(env, ri, counter);
1766 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1767 uint64_t value)
1769 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1770 assert(counter < pmu_num_counters(env));
1771 env->cp15.c14_pmevcntr[counter] = value;
1772 pmevcntr_write(env, ri, value, counter);
1775 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1777 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1778 assert(counter < pmu_num_counters(env));
1779 return env->cp15.c14_pmevcntr[counter];
1782 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1783 uint64_t value)
1785 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1788 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1790 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1793 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1794 uint64_t value)
1796 if (arm_feature(env, ARM_FEATURE_V8)) {
1797 env->cp15.c9_pmuserenr = value & 0xf;
1798 } else {
1799 env->cp15.c9_pmuserenr = value & 1;
1803 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1804 uint64_t value)
1806 /* We have no event counters so only the C bit can be changed */
1807 value &= pmu_counter_mask(env);
1808 env->cp15.c9_pminten |= value;
1809 pmu_update_irq(env);
1812 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1813 uint64_t value)
1815 value &= pmu_counter_mask(env);
1816 env->cp15.c9_pminten &= ~value;
1817 pmu_update_irq(env);
1820 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1821 uint64_t value)
1823 /* Note that even though the AArch64 view of this register has bits
1824 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1825 * architectural requirements for bits which are RES0 only in some
1826 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1827 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1829 raw_write(env, ri, value & ~0x1FULL);
1832 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1834 /* Begin with base v8.0 state. */
1835 uint32_t valid_mask = 0x3fff;
1836 ARMCPU *cpu = arm_env_get_cpu(env);
1838 if (arm_el_is_aa64(env, 3)) {
1839 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
1840 valid_mask &= ~SCR_NET;
1841 } else {
1842 valid_mask &= ~(SCR_RW | SCR_ST);
1845 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1846 valid_mask &= ~SCR_HCE;
1848 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1849 * supported if EL2 exists. The bit is UNK/SBZP when
1850 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1851 * when EL2 is unavailable.
1852 * On ARMv8, this bit is always available.
1854 if (arm_feature(env, ARM_FEATURE_V7) &&
1855 !arm_feature(env, ARM_FEATURE_V8)) {
1856 valid_mask &= ~SCR_SMD;
1859 if (cpu_isar_feature(aa64_lor, cpu)) {
1860 valid_mask |= SCR_TLOR;
1862 if (cpu_isar_feature(aa64_pauth, cpu)) {
1863 valid_mask |= SCR_API | SCR_APK;
1866 /* Clear all-context RES0 bits. */
1867 value &= valid_mask;
1868 raw_write(env, ri, value);
1871 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1873 ARMCPU *cpu = arm_env_get_cpu(env);
1875 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1876 * bank
1878 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1879 ri->secure & ARM_CP_SECSTATE_S);
1881 return cpu->ccsidr[index];
1884 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1885 uint64_t value)
1887 raw_write(env, ri, value & 0xf);
1890 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1892 CPUState *cs = ENV_GET_CPU(env);
1893 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1894 uint64_t ret = 0;
1896 if (hcr_el2 & HCR_IMO) {
1897 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1898 ret |= CPSR_I;
1900 } else {
1901 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1902 ret |= CPSR_I;
1906 if (hcr_el2 & HCR_FMO) {
1907 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1908 ret |= CPSR_F;
1910 } else {
1911 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1912 ret |= CPSR_F;
1916 /* External aborts are not possible in QEMU so A bit is always clear */
1917 return ret;
1920 static const ARMCPRegInfo v7_cp_reginfo[] = {
1921 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1922 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1923 .access = PL1_W, .type = ARM_CP_NOP },
1924 /* Performance monitors are implementation defined in v7,
1925 * but with an ARM recommended set of registers, which we
1926 * follow.
1928 * Performance registers fall into three categories:
1929 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1930 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1931 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1932 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1933 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1935 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1936 .access = PL0_RW, .type = ARM_CP_ALIAS,
1937 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1938 .writefn = pmcntenset_write,
1939 .accessfn = pmreg_access,
1940 .raw_writefn = raw_write },
1941 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1942 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1943 .access = PL0_RW, .accessfn = pmreg_access,
1944 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1945 .writefn = pmcntenset_write, .raw_writefn = raw_write },
1946 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1947 .access = PL0_RW,
1948 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1949 .accessfn = pmreg_access,
1950 .writefn = pmcntenclr_write,
1951 .type = ARM_CP_ALIAS },
1952 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1953 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1954 .access = PL0_RW, .accessfn = pmreg_access,
1955 .type = ARM_CP_ALIAS,
1956 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1957 .writefn = pmcntenclr_write },
1958 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1959 .access = PL0_RW, .type = ARM_CP_IO,
1960 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1961 .accessfn = pmreg_access,
1962 .writefn = pmovsr_write,
1963 .raw_writefn = raw_write },
1964 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1965 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1966 .access = PL0_RW, .accessfn = pmreg_access,
1967 .type = ARM_CP_ALIAS | ARM_CP_IO,
1968 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1969 .writefn = pmovsr_write,
1970 .raw_writefn = raw_write },
1971 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1972 .access = PL0_W, .accessfn = pmreg_access_swinc,
1973 .type = ARM_CP_NO_RAW | ARM_CP_IO,
1974 .writefn = pmswinc_write },
1975 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1976 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1977 .access = PL0_W, .accessfn = pmreg_access_swinc,
1978 .type = ARM_CP_NO_RAW | ARM_CP_IO,
1979 .writefn = pmswinc_write },
1980 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1981 .access = PL0_RW, .type = ARM_CP_ALIAS,
1982 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1983 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1984 .raw_writefn = raw_write},
1985 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1986 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1987 .access = PL0_RW, .accessfn = pmreg_access_selr,
1988 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1989 .writefn = pmselr_write, .raw_writefn = raw_write, },
1990 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1991 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1992 .readfn = pmccntr_read, .writefn = pmccntr_write32,
1993 .accessfn = pmreg_access_ccntr },
1994 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1995 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1996 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1997 .type = ARM_CP_IO,
1998 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1999 .readfn = pmccntr_read, .writefn = pmccntr_write,
2000 .raw_readfn = raw_read, .raw_writefn = raw_write, },
2001 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2002 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2003 .access = PL0_RW, .accessfn = pmreg_access,
2004 .type = ARM_CP_ALIAS | ARM_CP_IO,
2005 .resetvalue = 0, },
2006 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2007 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2008 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2009 .access = PL0_RW, .accessfn = pmreg_access,
2010 .type = ARM_CP_IO,
2011 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2012 .resetvalue = 0, },
2013 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2014 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2015 .accessfn = pmreg_access,
2016 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2017 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2018 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2019 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2020 .accessfn = pmreg_access,
2021 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2022 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2023 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2024 .accessfn = pmreg_access_xevcntr,
2025 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2026 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2027 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2028 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2029 .accessfn = pmreg_access_xevcntr,
2030 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2031 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2032 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2033 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2034 .resetvalue = 0,
2035 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2036 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2037 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2038 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2039 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2040 .resetvalue = 0,
2041 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2042 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2043 .access = PL1_RW, .accessfn = access_tpm,
2044 .type = ARM_CP_ALIAS | ARM_CP_IO,
2045 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2046 .resetvalue = 0,
2047 .writefn = pmintenset_write, .raw_writefn = raw_write },
2048 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2049 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2050 .access = PL1_RW, .accessfn = access_tpm,
2051 .type = ARM_CP_IO,
2052 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2053 .writefn = pmintenset_write, .raw_writefn = raw_write,
2054 .resetvalue = 0x0 },
2055 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2056 .access = PL1_RW, .accessfn = access_tpm,
2057 .type = ARM_CP_ALIAS | ARM_CP_IO,
2058 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2059 .writefn = pmintenclr_write, },
2060 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2061 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2062 .access = PL1_RW, .accessfn = access_tpm,
2063 .type = ARM_CP_ALIAS | ARM_CP_IO,
2064 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2065 .writefn = pmintenclr_write },
2066 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2067 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2068 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2069 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2070 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2071 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
2072 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2073 offsetof(CPUARMState, cp15.csselr_ns) } },
2074 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2075 * just RAZ for all cores:
2077 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2078 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2079 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2080 /* Auxiliary fault status registers: these also are IMPDEF, and we
2081 * choose to RAZ/WI for all cores.
2083 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2084 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2085 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2086 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2087 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2088 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2089 /* MAIR can just read-as-written because we don't implement caches
2090 * and so don't need to care about memory attributes.
2092 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2093 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2094 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2095 .resetvalue = 0 },
2096 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2097 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2098 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2099 .resetvalue = 0 },
2100 /* For non-long-descriptor page tables these are PRRR and NMRR;
2101 * regardless they still act as reads-as-written for QEMU.
2103 /* MAIR0/1 are defined separately from their 64-bit counterpart which
2104 * allows them to assign the correct fieldoffset based on the endianness
2105 * handled in the field definitions.
2107 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2108 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
2109 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2110 offsetof(CPUARMState, cp15.mair0_ns) },
2111 .resetfn = arm_cp_reset_ignore },
2112 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2113 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
2114 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2115 offsetof(CPUARMState, cp15.mair1_ns) },
2116 .resetfn = arm_cp_reset_ignore },
2117 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2118 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2119 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2120 /* 32 bit ITLB invalidates */
2121 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2122 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2123 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2124 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2125 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2126 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2127 /* 32 bit DTLB invalidates */
2128 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2129 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2130 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2131 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2132 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2133 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2134 /* 32 bit TLB invalidates */
2135 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2136 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2137 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2138 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2139 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2140 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2141 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2142 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
2143 REGINFO_SENTINEL
2146 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2147 /* 32 bit TLB invalidates, Inner Shareable */
2148 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2149 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
2150 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2151 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
2152 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2153 .type = ARM_CP_NO_RAW, .access = PL1_W,
2154 .writefn = tlbiasid_is_write },
2155 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2156 .type = ARM_CP_NO_RAW, .access = PL1_W,
2157 .writefn = tlbimvaa_is_write },
2158 REGINFO_SENTINEL
2161 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2162 /* PMOVSSET is not implemented in v7 before v7ve */
2163 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2164 .access = PL0_RW, .accessfn = pmreg_access,
2165 .type = ARM_CP_ALIAS | ARM_CP_IO,
2166 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2167 .writefn = pmovsset_write,
2168 .raw_writefn = raw_write },
2169 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2170 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2171 .access = PL0_RW, .accessfn = pmreg_access,
2172 .type = ARM_CP_ALIAS | ARM_CP_IO,
2173 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2174 .writefn = pmovsset_write,
2175 .raw_writefn = raw_write },
2176 REGINFO_SENTINEL
2179 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2180 uint64_t value)
2182 value &= 1;
2183 env->teecr = value;
2186 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2187 bool isread)
2189 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2190 return CP_ACCESS_TRAP;
2192 return CP_ACCESS_OK;
2195 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2196 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2197 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2198 .resetvalue = 0,
2199 .writefn = teecr_write },
2200 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2201 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2202 .accessfn = teehbr_access, .resetvalue = 0 },
2203 REGINFO_SENTINEL
2206 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2207 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2208 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2209 .access = PL0_RW,
2210 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2211 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2212 .access = PL0_RW,
2213 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2214 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2215 .resetfn = arm_cp_reset_ignore },
2216 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2217 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2218 .access = PL0_R|PL1_W,
2219 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2220 .resetvalue = 0},
2221 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2222 .access = PL0_R|PL1_W,
2223 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2224 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2225 .resetfn = arm_cp_reset_ignore },
2226 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2227 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2228 .access = PL1_RW,
2229 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2230 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2231 .access = PL1_RW,
2232 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2233 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2234 .resetvalue = 0 },
2235 REGINFO_SENTINEL
2238 #ifndef CONFIG_USER_ONLY
2240 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2241 bool isread)
2243 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2244 * Writable only at the highest implemented exception level.
2246 int el = arm_current_el(env);
2248 switch (el) {
2249 case 0:
2250 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
2251 return CP_ACCESS_TRAP;
2253 break;
2254 case 1:
2255 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2256 arm_is_secure_below_el3(env)) {
2257 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2258 return CP_ACCESS_TRAP_UNCATEGORIZED;
2260 break;
2261 case 2:
2262 case 3:
2263 break;
2266 if (!isread && el < arm_highest_el(env)) {
2267 return CP_ACCESS_TRAP_UNCATEGORIZED;
2270 return CP_ACCESS_OK;
2273 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2274 bool isread)
2276 unsigned int cur_el = arm_current_el(env);
2277 bool secure = arm_is_secure(env);
2279 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
2280 if (cur_el == 0 &&
2281 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2282 return CP_ACCESS_TRAP;
2285 if (arm_feature(env, ARM_FEATURE_EL2) &&
2286 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
2287 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
2288 return CP_ACCESS_TRAP_EL2;
2290 return CP_ACCESS_OK;
2293 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2294 bool isread)
2296 unsigned int cur_el = arm_current_el(env);
2297 bool secure = arm_is_secure(env);
2299 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
2300 * EL0[PV]TEN is zero.
2302 if (cur_el == 0 &&
2303 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2304 return CP_ACCESS_TRAP;
2307 if (arm_feature(env, ARM_FEATURE_EL2) &&
2308 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
2309 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2310 return CP_ACCESS_TRAP_EL2;
2312 return CP_ACCESS_OK;
2315 static CPAccessResult gt_pct_access(CPUARMState *env,
2316 const ARMCPRegInfo *ri,
2317 bool isread)
2319 return gt_counter_access(env, GTIMER_PHYS, isread);
2322 static CPAccessResult gt_vct_access(CPUARMState *env,
2323 const ARMCPRegInfo *ri,
2324 bool isread)
2326 return gt_counter_access(env, GTIMER_VIRT, isread);
2329 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2330 bool isread)
2332 return gt_timer_access(env, GTIMER_PHYS, isread);
2335 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2336 bool isread)
2338 return gt_timer_access(env, GTIMER_VIRT, isread);
2341 static CPAccessResult gt_stimer_access(CPUARMState *env,
2342 const ARMCPRegInfo *ri,
2343 bool isread)
2345 /* The AArch64 register view of the secure physical timer is
2346 * always accessible from EL3, and configurably accessible from
2347 * Secure EL1.
2349 switch (arm_current_el(env)) {
2350 case 1:
2351 if (!arm_is_secure(env)) {
2352 return CP_ACCESS_TRAP;
2354 if (!(env->cp15.scr_el3 & SCR_ST)) {
2355 return CP_ACCESS_TRAP_EL3;
2357 return CP_ACCESS_OK;
2358 case 0:
2359 case 2:
2360 return CP_ACCESS_TRAP;
2361 case 3:
2362 return CP_ACCESS_OK;
2363 default:
2364 g_assert_not_reached();
2368 static uint64_t gt_get_countervalue(CPUARMState *env)
2370 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
2373 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2375 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2377 if (gt->ctl & 1) {
2378 /* Timer enabled: calculate and set current ISTATUS, irq, and
2379 * reset timer to when ISTATUS next has to change
2381 uint64_t offset = timeridx == GTIMER_VIRT ?
2382 cpu->env.cp15.cntvoff_el2 : 0;
2383 uint64_t count = gt_get_countervalue(&cpu->env);
2384 /* Note that this must be unsigned 64 bit arithmetic: */
2385 int istatus = count - offset >= gt->cval;
2386 uint64_t nexttick;
2387 int irqstate;
2389 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2391 irqstate = (istatus && !(gt->ctl & 2));
2392 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2394 if (istatus) {
2395 /* Next transition is when count rolls back over to zero */
2396 nexttick = UINT64_MAX;
2397 } else {
2398 /* Next transition is when we hit cval */
2399 nexttick = gt->cval + offset;
2401 /* Note that the desired next expiry time might be beyond the
2402 * signed-64-bit range of a QEMUTimer -- in this case we just
2403 * set the timer for as far in the future as possible. When the
2404 * timer expires we will reset the timer for any remaining period.
2406 if (nexttick > INT64_MAX / GTIMER_SCALE) {
2407 nexttick = INT64_MAX / GTIMER_SCALE;
2409 timer_mod(cpu->gt_timer[timeridx], nexttick);
2410 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2411 } else {
2412 /* Timer disabled: ISTATUS and timer output always clear */
2413 gt->ctl &= ~4;
2414 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2415 timer_del(cpu->gt_timer[timeridx]);
2416 trace_arm_gt_recalc_disabled(timeridx);
2420 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2421 int timeridx)
2423 ARMCPU *cpu = arm_env_get_cpu(env);
2425 timer_del(cpu->gt_timer[timeridx]);
2428 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2430 return gt_get_countervalue(env);
2433 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2435 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
2438 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2439 int timeridx,
2440 uint64_t value)
2442 trace_arm_gt_cval_write(timeridx, value);
2443 env->cp15.c14_timer[timeridx].cval = value;
2444 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
2447 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2448 int timeridx)
2450 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
2452 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2453 (gt_get_countervalue(env) - offset));
2456 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2457 int timeridx,
2458 uint64_t value)
2460 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
2462 trace_arm_gt_tval_write(timeridx, value);
2463 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2464 sextract64(value, 0, 32);
2465 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
2468 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2469 int timeridx,
2470 uint64_t value)
2472 ARMCPU *cpu = arm_env_get_cpu(env);
2473 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2475 trace_arm_gt_ctl_write(timeridx, value);
2476 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2477 if ((oldval ^ value) & 1) {
2478 /* Enable toggled */
2479 gt_recalc_timer(cpu, timeridx);
2480 } else if ((oldval ^ value) & 2) {
2481 /* IMASK toggled: don't need to recalculate,
2482 * just set the interrupt line based on ISTATUS
2484 int irqstate = (oldval & 4) && !(value & 2);
2486 trace_arm_gt_imask_toggle(timeridx, irqstate);
2487 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2491 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2493 gt_timer_reset(env, ri, GTIMER_PHYS);
2496 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2497 uint64_t value)
2499 gt_cval_write(env, ri, GTIMER_PHYS, value);
2502 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2504 return gt_tval_read(env, ri, GTIMER_PHYS);
2507 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2508 uint64_t value)
2510 gt_tval_write(env, ri, GTIMER_PHYS, value);
2513 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2514 uint64_t value)
2516 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2519 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2521 gt_timer_reset(env, ri, GTIMER_VIRT);
2524 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2525 uint64_t value)
2527 gt_cval_write(env, ri, GTIMER_VIRT, value);
2530 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2532 return gt_tval_read(env, ri, GTIMER_VIRT);
2535 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2536 uint64_t value)
2538 gt_tval_write(env, ri, GTIMER_VIRT, value);
2541 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2542 uint64_t value)
2544 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2547 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2548 uint64_t value)
2550 ARMCPU *cpu = arm_env_get_cpu(env);
2552 trace_arm_gt_cntvoff_write(value);
2553 raw_write(env, ri, value);
2554 gt_recalc_timer(cpu, GTIMER_VIRT);
2557 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2559 gt_timer_reset(env, ri, GTIMER_HYP);
2562 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2563 uint64_t value)
2565 gt_cval_write(env, ri, GTIMER_HYP, value);
2568 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2570 return gt_tval_read(env, ri, GTIMER_HYP);
2573 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2574 uint64_t value)
2576 gt_tval_write(env, ri, GTIMER_HYP, value);
2579 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2580 uint64_t value)
2582 gt_ctl_write(env, ri, GTIMER_HYP, value);
2585 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2587 gt_timer_reset(env, ri, GTIMER_SEC);
2590 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2591 uint64_t value)
2593 gt_cval_write(env, ri, GTIMER_SEC, value);
2596 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2598 return gt_tval_read(env, ri, GTIMER_SEC);
2601 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2602 uint64_t value)
2604 gt_tval_write(env, ri, GTIMER_SEC, value);
2607 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2608 uint64_t value)
2610 gt_ctl_write(env, ri, GTIMER_SEC, value);
2613 void arm_gt_ptimer_cb(void *opaque)
2615 ARMCPU *cpu = opaque;
2617 gt_recalc_timer(cpu, GTIMER_PHYS);
2620 void arm_gt_vtimer_cb(void *opaque)
2622 ARMCPU *cpu = opaque;
2624 gt_recalc_timer(cpu, GTIMER_VIRT);
2627 void arm_gt_htimer_cb(void *opaque)
2629 ARMCPU *cpu = opaque;
2631 gt_recalc_timer(cpu, GTIMER_HYP);
2634 void arm_gt_stimer_cb(void *opaque)
2636 ARMCPU *cpu = opaque;
2638 gt_recalc_timer(cpu, GTIMER_SEC);
2641 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2642 /* Note that CNTFRQ is purely reads-as-written for the benefit
2643 * of software; writing it doesn't actually change the timer frequency.
2644 * Our reset value matches the fixed frequency we implement the timer at.
2646 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2647 .type = ARM_CP_ALIAS,
2648 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2649 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2651 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2652 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2653 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2654 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2655 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
2657 /* overall control: mostly access permissions */
2658 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2659 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2660 .access = PL1_RW,
2661 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2662 .resetvalue = 0,
2664 /* per-timer control */
2665 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2666 .secure = ARM_CP_SECSTATE_NS,
2667 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
2668 .accessfn = gt_ptimer_access,
2669 .fieldoffset = offsetoflow32(CPUARMState,
2670 cp15.c14_timer[GTIMER_PHYS].ctl),
2671 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
2673 { .name = "CNTP_CTL_S",
2674 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2675 .secure = ARM_CP_SECSTATE_S,
2676 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
2677 .accessfn = gt_ptimer_access,
2678 .fieldoffset = offsetoflow32(CPUARMState,
2679 cp15.c14_timer[GTIMER_SEC].ctl),
2680 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2682 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2683 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2684 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
2685 .accessfn = gt_ptimer_access,
2686 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2687 .resetvalue = 0,
2688 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
2690 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2691 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
2692 .accessfn = gt_vtimer_access,
2693 .fieldoffset = offsetoflow32(CPUARMState,
2694 cp15.c14_timer[GTIMER_VIRT].ctl),
2695 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
2697 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2698 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2699 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
2700 .accessfn = gt_vtimer_access,
2701 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2702 .resetvalue = 0,
2703 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
2705 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2706 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2707 .secure = ARM_CP_SECSTATE_NS,
2708 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2709 .accessfn = gt_ptimer_access,
2710 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
2712 { .name = "CNTP_TVAL_S",
2713 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2714 .secure = ARM_CP_SECSTATE_S,
2715 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2716 .accessfn = gt_ptimer_access,
2717 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2719 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2720 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2721 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2722 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2723 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
2725 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2726 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2727 .accessfn = gt_vtimer_access,
2728 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2730 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2731 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2732 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
2733 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2734 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2736 /* The counter itself */
2737 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
2738 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2739 .accessfn = gt_pct_access,
2740 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2742 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2743 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
2744 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2745 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
2747 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
2748 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2749 .accessfn = gt_vct_access,
2750 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
2752 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2753 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2754 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2755 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
2757 /* Comparison value, indicating when the timer goes off */
2758 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
2759 .secure = ARM_CP_SECSTATE_NS,
2760 .access = PL1_RW | PL0_R,
2761 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2762 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2763 .accessfn = gt_ptimer_access,
2764 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2766 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
2767 .secure = ARM_CP_SECSTATE_S,
2768 .access = PL1_RW | PL0_R,
2769 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2770 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2771 .accessfn = gt_ptimer_access,
2772 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2774 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2775 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2776 .access = PL1_RW | PL0_R,
2777 .type = ARM_CP_IO,
2778 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2779 .resetvalue = 0, .accessfn = gt_ptimer_access,
2780 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2782 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2783 .access = PL1_RW | PL0_R,
2784 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2785 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2786 .accessfn = gt_vtimer_access,
2787 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2789 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2790 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2791 .access = PL1_RW | PL0_R,
2792 .type = ARM_CP_IO,
2793 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2794 .resetvalue = 0, .accessfn = gt_vtimer_access,
2795 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2797 /* Secure timer -- this is actually restricted to only EL3
2798 * and configurably Secure-EL1 via the accessfn.
2800 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2801 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2802 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2803 .accessfn = gt_stimer_access,
2804 .readfn = gt_sec_tval_read,
2805 .writefn = gt_sec_tval_write,
2806 .resetfn = gt_sec_timer_reset,
2808 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2809 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2810 .type = ARM_CP_IO, .access = PL1_RW,
2811 .accessfn = gt_stimer_access,
2812 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2813 .resetvalue = 0,
2814 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2816 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2817 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2818 .type = ARM_CP_IO, .access = PL1_RW,
2819 .accessfn = gt_stimer_access,
2820 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2821 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2823 REGINFO_SENTINEL
2826 #else
2828 /* In user-mode most of the generic timer registers are inaccessible
2829 * however modern kernels (4.12+) allow access to cntvct_el0
2832 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2834 /* Currently we have no support for QEMUTimer in linux-user so we
2835 * can't call gt_get_countervalue(env), instead we directly
2836 * call the lower level functions.
2838 return cpu_get_clock() / GTIMER_SCALE;
2841 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2842 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2843 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2844 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
2845 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2846 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
2848 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2849 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2850 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2851 .readfn = gt_virt_cnt_read,
2853 REGINFO_SENTINEL
2856 #endif
2858 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2860 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2861 raw_write(env, ri, value);
2862 } else if (arm_feature(env, ARM_FEATURE_V7)) {
2863 raw_write(env, ri, value & 0xfffff6ff);
2864 } else {
2865 raw_write(env, ri, value & 0xfffff1ff);
2869 #ifndef CONFIG_USER_ONLY
2870 /* get_phys_addr() isn't present for user-mode-only targets */
2872 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2873 bool isread)
2875 if (ri->opc2 & 4) {
2876 /* The ATS12NSO* operations must trap to EL3 if executed in
2877 * Secure EL1 (which can only happen if EL3 is AArch64).
2878 * They are simply UNDEF if executed from NS EL1.
2879 * They function normally from EL2 or EL3.
2881 if (arm_current_el(env) == 1) {
2882 if (arm_is_secure_below_el3(env)) {
2883 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2885 return CP_ACCESS_TRAP_UNCATEGORIZED;
2888 return CP_ACCESS_OK;
2891 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
2892 MMUAccessType access_type, ARMMMUIdx mmu_idx)
2894 hwaddr phys_addr;
2895 target_ulong page_size;
2896 int prot;
2897 bool ret;
2898 uint64_t par64;
2899 bool format64 = false;
2900 MemTxAttrs attrs = {};
2901 ARMMMUFaultInfo fi = {};
2902 ARMCacheAttrs cacheattrs = {};
2904 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
2905 &prot, &page_size, &fi, &cacheattrs);
2907 if (is_a64(env)) {
2908 format64 = true;
2909 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
2911 * ATS1Cxx:
2912 * * TTBCR.EAE determines whether the result is returned using the
2913 * 32-bit or the 64-bit PAR format
2914 * * Instructions executed in Hyp mode always use the 64bit format
2916 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
2917 * * The Non-secure TTBCR.EAE bit is set to 1
2918 * * The implementation includes EL2, and the value of HCR.VM is 1
2920 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
2922 * ATS1Hx always uses the 64bit format.
2924 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
2926 if (arm_feature(env, ARM_FEATURE_EL2)) {
2927 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
2928 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
2929 } else {
2930 format64 |= arm_current_el(env) == 2;
2935 if (format64) {
2936 /* Create a 64-bit PAR */
2937 par64 = (1 << 11); /* LPAE bit always set */
2938 if (!ret) {
2939 par64 |= phys_addr & ~0xfffULL;
2940 if (!attrs.secure) {
2941 par64 |= (1 << 9); /* NS */
2943 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
2944 par64 |= cacheattrs.shareability << 7; /* SH */
2945 } else {
2946 uint32_t fsr = arm_fi_to_lfsc(&fi);
2948 par64 |= 1; /* F */
2949 par64 |= (fsr & 0x3f) << 1; /* FS */
2950 if (fi.stage2) {
2951 par64 |= (1 << 9); /* S */
2953 if (fi.s1ptw) {
2954 par64 |= (1 << 8); /* PTW */
2957 } else {
2958 /* fsr is a DFSR/IFSR value for the short descriptor
2959 * translation table format (with WnR always clear).
2960 * Convert it to a 32-bit PAR.
2962 if (!ret) {
2963 /* We do not set any attribute bits in the PAR */
2964 if (page_size == (1 << 24)
2965 && arm_feature(env, ARM_FEATURE_V7)) {
2966 par64 = (phys_addr & 0xff000000) | (1 << 1);
2967 } else {
2968 par64 = phys_addr & 0xfffff000;
2970 if (!attrs.secure) {
2971 par64 |= (1 << 9); /* NS */
2973 } else {
2974 uint32_t fsr = arm_fi_to_sfsc(&fi);
2976 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
2977 ((fsr & 0xf) << 1) | 1;
2980 return par64;
2983 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2985 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
2986 uint64_t par64;
2987 ARMMMUIdx mmu_idx;
2988 int el = arm_current_el(env);
2989 bool secure = arm_is_secure_below_el3(env);
2991 switch (ri->opc2 & 6) {
2992 case 0:
2993 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2994 switch (el) {
2995 case 3:
2996 mmu_idx = ARMMMUIdx_S1E3;
2997 break;
2998 case 2:
2999 mmu_idx = ARMMMUIdx_S1NSE1;
3000 break;
3001 case 1:
3002 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
3003 break;
3004 default:
3005 g_assert_not_reached();
3007 break;
3008 case 2:
3009 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3010 switch (el) {
3011 case 3:
3012 mmu_idx = ARMMMUIdx_S1SE0;
3013 break;
3014 case 2:
3015 mmu_idx = ARMMMUIdx_S1NSE0;
3016 break;
3017 case 1:
3018 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
3019 break;
3020 default:
3021 g_assert_not_reached();
3023 break;
3024 case 4:
3025 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3026 mmu_idx = ARMMMUIdx_S12NSE1;
3027 break;
3028 case 6:
3029 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3030 mmu_idx = ARMMMUIdx_S12NSE0;
3031 break;
3032 default:
3033 g_assert_not_reached();
3036 par64 = do_ats_write(env, value, access_type, mmu_idx);
3038 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3041 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3042 uint64_t value)
3044 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3045 uint64_t par64;
3047 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S1E2);
3049 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3052 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3053 bool isread)
3055 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3056 return CP_ACCESS_TRAP;
3058 return CP_ACCESS_OK;
3061 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3062 uint64_t value)
3064 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3065 ARMMMUIdx mmu_idx;
3066 int secure = arm_is_secure_below_el3(env);
3068 switch (ri->opc2 & 6) {
3069 case 0:
3070 switch (ri->opc1) {
3071 case 0: /* AT S1E1R, AT S1E1W */
3072 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
3073 break;
3074 case 4: /* AT S1E2R, AT S1E2W */
3075 mmu_idx = ARMMMUIdx_S1E2;
3076 break;
3077 case 6: /* AT S1E3R, AT S1E3W */
3078 mmu_idx = ARMMMUIdx_S1E3;
3079 break;
3080 default:
3081 g_assert_not_reached();
3083 break;
3084 case 2: /* AT S1E0R, AT S1E0W */
3085 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
3086 break;
3087 case 4: /* AT S12E1R, AT S12E1W */
3088 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
3089 break;
3090 case 6: /* AT S12E0R, AT S12E0W */
3091 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
3092 break;
3093 default:
3094 g_assert_not_reached();
3097 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3099 #endif
3101 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3102 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3103 .access = PL1_RW, .resetvalue = 0,
3104 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3105 offsetoflow32(CPUARMState, cp15.par_ns) },
3106 .writefn = par_write },
3107 #ifndef CONFIG_USER_ONLY
3108 /* This underdecoding is safe because the reginfo is NO_RAW. */
3109 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3110 .access = PL1_W, .accessfn = ats_access,
3111 .writefn = ats_write, .type = ARM_CP_NO_RAW },
3112 #endif
3113 REGINFO_SENTINEL
3116 /* Return basic MPU access permission bits. */
3117 static uint32_t simple_mpu_ap_bits(uint32_t val)
3119 uint32_t ret;
3120 uint32_t mask;
3121 int i;
3122 ret = 0;
3123 mask = 3;
3124 for (i = 0; i < 16; i += 2) {
3125 ret |= (val >> i) & mask;
3126 mask <<= 2;
3128 return ret;
3131 /* Pad basic MPU access permission bits to extended format. */
3132 static uint32_t extended_mpu_ap_bits(uint32_t val)
3134 uint32_t ret;
3135 uint32_t mask;
3136 int i;
3137 ret = 0;
3138 mask = 3;
3139 for (i = 0; i < 16; i += 2) {
3140 ret |= (val & mask) << i;
3141 mask <<= 2;
3143 return ret;
3146 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3147 uint64_t value)
3149 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3152 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3154 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3157 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3158 uint64_t value)
3160 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3163 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3165 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3168 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3170 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3172 if (!u32p) {
3173 return 0;
3176 u32p += env->pmsav7.rnr[M_REG_NS];
3177 return *u32p;
3180 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3181 uint64_t value)
3183 ARMCPU *cpu = arm_env_get_cpu(env);
3184 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3186 if (!u32p) {
3187 return;
3190 u32p += env->pmsav7.rnr[M_REG_NS];
3191 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3192 *u32p = value;
3195 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3196 uint64_t value)
3198 ARMCPU *cpu = arm_env_get_cpu(env);
3199 uint32_t nrgs = cpu->pmsav7_dregion;
3201 if (value >= nrgs) {
3202 qemu_log_mask(LOG_GUEST_ERROR,
3203 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3204 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3205 return;
3208 raw_write(env, ri, value);
3211 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3212 /* Reset for all these registers is handled in arm_cpu_reset(),
3213 * because the PMSAv7 is also used by M-profile CPUs, which do
3214 * not register cpregs but still need the state to be reset.
3216 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3217 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3218 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3219 .readfn = pmsav7_read, .writefn = pmsav7_write,
3220 .resetfn = arm_cp_reset_ignore },
3221 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3222 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3223 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3224 .readfn = pmsav7_read, .writefn = pmsav7_write,
3225 .resetfn = arm_cp_reset_ignore },
3226 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3227 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3228 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3229 .readfn = pmsav7_read, .writefn = pmsav7_write,
3230 .resetfn = arm_cp_reset_ignore },
3231 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3232 .access = PL1_RW,
3233 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3234 .writefn = pmsav7_rgnr_write,
3235 .resetfn = arm_cp_reset_ignore },
3236 REGINFO_SENTINEL
3239 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3240 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3241 .access = PL1_RW, .type = ARM_CP_ALIAS,
3242 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3243 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3244 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3245 .access = PL1_RW, .type = ARM_CP_ALIAS,
3246 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3247 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3248 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3249 .access = PL1_RW,
3250 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3251 .resetvalue = 0, },
3252 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3253 .access = PL1_RW,
3254 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3255 .resetvalue = 0, },
3256 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3257 .access = PL1_RW,
3258 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3259 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3260 .access = PL1_RW,
3261 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3262 /* Protection region base and size registers */
3263 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3264 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3265 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3266 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3267 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3268 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3269 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3270 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3271 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3272 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3273 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3274 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3275 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3276 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3277 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3278 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3279 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3280 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3281 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3282 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3283 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3284 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3285 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3286 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3287 REGINFO_SENTINEL
3290 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3291 uint64_t value)
3293 TCR *tcr = raw_ptr(env, ri);
3294 int maskshift = extract32(value, 0, 3);
3296 if (!arm_feature(env, ARM_FEATURE_V8)) {
3297 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3298 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3299 * using Long-desciptor translation table format */
3300 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3301 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3302 /* In an implementation that includes the Security Extensions
3303 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3304 * Short-descriptor translation table format.
3306 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3307 } else {
3308 value &= TTBCR_N;
3312 /* Update the masks corresponding to the TCR bank being written
3313 * Note that we always calculate mask and base_mask, but
3314 * they are only used for short-descriptor tables (ie if EAE is 0);
3315 * for long-descriptor tables the TCR fields are used differently
3316 * and the mask and base_mask values are meaningless.
3318 tcr->raw_tcr = value;
3319 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3320 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
3323 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3324 uint64_t value)
3326 ARMCPU *cpu = arm_env_get_cpu(env);
3327 TCR *tcr = raw_ptr(env, ri);
3329 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3330 /* With LPAE the TTBCR could result in a change of ASID
3331 * via the TTBCR.A1 bit, so do a TLB flush.
3333 tlb_flush(CPU(cpu));
3335 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3336 value = deposit64(tcr->raw_tcr, 0, 32, value);
3337 vmsa_ttbcr_raw_write(env, ri, value);
3340 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3342 TCR *tcr = raw_ptr(env, ri);
3344 /* Reset both the TCR as well as the masks corresponding to the bank of
3345 * the TCR being reset.
3347 tcr->raw_tcr = 0;
3348 tcr->mask = 0;
3349 tcr->base_mask = 0xffffc000u;
3352 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3353 uint64_t value)
3355 ARMCPU *cpu = arm_env_get_cpu(env);
3356 TCR *tcr = raw_ptr(env, ri);
3358 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3359 tlb_flush(CPU(cpu));
3360 tcr->raw_tcr = value;
3363 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3364 uint64_t value)
3366 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3367 if (cpreg_field_is_64bit(ri) &&
3368 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3369 ARMCPU *cpu = arm_env_get_cpu(env);
3370 tlb_flush(CPU(cpu));
3372 raw_write(env, ri, value);
3375 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3376 uint64_t value)
3378 ARMCPU *cpu = arm_env_get_cpu(env);
3379 CPUState *cs = CPU(cpu);
3381 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
3382 if (raw_read(env, ri) != value) {
3383 tlb_flush_by_mmuidx(cs,
3384 ARMMMUIdxBit_S12NSE1 |
3385 ARMMMUIdxBit_S12NSE0 |
3386 ARMMMUIdxBit_S2NS);
3387 raw_write(env, ri, value);
3391 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3392 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3393 .access = PL1_RW, .type = ARM_CP_ALIAS,
3394 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3395 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3396 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3397 .access = PL1_RW, .resetvalue = 0,
3398 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3399 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3400 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3401 .access = PL1_RW, .resetvalue = 0,
3402 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3403 offsetof(CPUARMState, cp15.dfar_ns) } },
3404 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3405 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3406 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3407 .resetvalue = 0, },
3408 REGINFO_SENTINEL
3411 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3412 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3413 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3414 .access = PL1_RW,
3415 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3416 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3417 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3418 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3419 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3420 offsetof(CPUARMState, cp15.ttbr0_ns) } },
3421 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3422 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3423 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3424 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3425 offsetof(CPUARMState, cp15.ttbr1_ns) } },
3426 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3427 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3428 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
3429 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3430 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3431 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3432 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3433 .raw_writefn = vmsa_ttbcr_raw_write,
3434 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3435 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
3436 REGINFO_SENTINEL
3439 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3440 * qemu tlbs nor adjusting cached masks.
3442 static const ARMCPRegInfo ttbcr2_reginfo = {
3443 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3444 .access = PL1_RW, .type = ARM_CP_ALIAS,
3445 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3446 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
3449 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3450 uint64_t value)
3452 env->cp15.c15_ticonfig = value & 0xe7;
3453 /* The OS_TYPE bit in this register changes the reported CPUID! */
3454 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3455 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3458 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3459 uint64_t value)
3461 env->cp15.c15_threadid = value & 0xffff;
3464 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3465 uint64_t value)
3467 /* Wait-for-interrupt (deprecated) */
3468 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
3471 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3472 uint64_t value)
3474 /* On OMAP there are registers indicating the max/min index of dcache lines
3475 * containing a dirty line; cache flush operations have to reset these.
3477 env->cp15.c15_i_max = 0x000;
3478 env->cp15.c15_i_min = 0xff0;
3481 static const ARMCPRegInfo omap_cp_reginfo[] = {
3482 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3483 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3484 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3485 .resetvalue = 0, },
3486 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3487 .access = PL1_RW, .type = ARM_CP_NOP },
3488 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3489 .access = PL1_RW,
3490 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3491 .writefn = omap_ticonfig_write },
3492 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3493 .access = PL1_RW,
3494 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3495 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3496 .access = PL1_RW, .resetvalue = 0xff0,
3497 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3498 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3499 .access = PL1_RW,
3500 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3501 .writefn = omap_threadid_write },
3502 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3503 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3504 .type = ARM_CP_NO_RAW,
3505 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3506 /* TODO: Peripheral port remap register:
3507 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3508 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3509 * when MMU is off.
3511 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3512 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3513 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3514 .writefn = omap_cachemaint_write },
3515 { .name = "C9", .cp = 15, .crn = 9,
3516 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3517 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3518 REGINFO_SENTINEL
3521 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3522 uint64_t value)
3524 env->cp15.c15_cpar = value & 0x3fff;
3527 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3528 { .name = "XSCALE_CPAR",
3529 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3530 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3531 .writefn = xscale_cpar_write, },
3532 { .name = "XSCALE_AUXCR",
3533 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3534 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3535 .resetvalue = 0, },
3536 /* XScale specific cache-lockdown: since we have no cache we NOP these
3537 * and hope the guest does not really rely on cache behaviour.
3539 { .name = "XSCALE_LOCK_ICACHE_LINE",
3540 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3541 .access = PL1_W, .type = ARM_CP_NOP },
3542 { .name = "XSCALE_UNLOCK_ICACHE",
3543 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3544 .access = PL1_W, .type = ARM_CP_NOP },
3545 { .name = "XSCALE_DCACHE_LOCK",
3546 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3547 .access = PL1_RW, .type = ARM_CP_NOP },
3548 { .name = "XSCALE_UNLOCK_DCACHE",
3549 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3550 .access = PL1_W, .type = ARM_CP_NOP },
3551 REGINFO_SENTINEL
3554 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3555 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3556 * implementation of this implementation-defined space.
3557 * Ideally this should eventually disappear in favour of actually
3558 * implementing the correct behaviour for all cores.
3560 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3561 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3562 .access = PL1_RW,
3563 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3564 .resetvalue = 0 },
3565 REGINFO_SENTINEL
3568 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3569 /* Cache status: RAZ because we have no cache so it's always clean */
3570 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3571 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3572 .resetvalue = 0 },
3573 REGINFO_SENTINEL
3576 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3577 /* We never have a a block transfer operation in progress */
3578 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
3579 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3580 .resetvalue = 0 },
3581 /* The cache ops themselves: these all NOP for QEMU */
3582 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3583 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3584 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3585 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3586 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3587 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3588 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
3589 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3590 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
3591 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3592 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
3593 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3594 REGINFO_SENTINEL
3597 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
3598 /* The cache test-and-clean instructions always return (1 << 30)
3599 * to indicate that there are no dirty cache lines.
3601 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
3602 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3603 .resetvalue = (1 << 30) },
3604 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
3605 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3606 .resetvalue = (1 << 30) },
3607 REGINFO_SENTINEL
3610 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
3611 /* Ignore ReadBuffer accesses */
3612 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
3613 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3614 .access = PL1_RW, .resetvalue = 0,
3615 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
3616 REGINFO_SENTINEL
3619 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3621 ARMCPU *cpu = arm_env_get_cpu(env);
3622 unsigned int cur_el = arm_current_el(env);
3623 bool secure = arm_is_secure(env);
3625 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
3626 return env->cp15.vpidr_el2;
3628 return raw_read(env, ri);
3631 static uint64_t mpidr_read_val(CPUARMState *env)
3633 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
3634 uint64_t mpidr = cpu->mp_affinity;
3636 if (arm_feature(env, ARM_FEATURE_V7MP)) {
3637 mpidr |= (1U << 31);
3638 /* Cores which are uniprocessor (non-coherent)
3639 * but still implement the MP extensions set
3640 * bit 30. (For instance, Cortex-R5).
3642 if (cpu->mp_is_up) {
3643 mpidr |= (1u << 30);
3646 return mpidr;
3649 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3651 unsigned int cur_el = arm_current_el(env);
3652 bool secure = arm_is_secure(env);
3654 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
3655 return env->cp15.vmpidr_el2;
3657 return mpidr_read_val(env);
3660 static const ARMCPRegInfo lpae_cp_reginfo[] = {
3661 /* NOP AMAIR0/1 */
3662 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
3663 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
3664 .access = PL1_RW, .type = ARM_CP_CONST,
3665 .resetvalue = 0 },
3666 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
3667 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
3668 .access = PL1_RW, .type = ARM_CP_CONST,
3669 .resetvalue = 0 },
3670 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
3671 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
3672 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
3673 offsetof(CPUARMState, cp15.par_ns)} },
3674 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
3675 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3676 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3677 offsetof(CPUARMState, cp15.ttbr0_ns) },
3678 .writefn = vmsa_ttbr_write, },
3679 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
3680 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3681 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3682 offsetof(CPUARMState, cp15.ttbr1_ns) },
3683 .writefn = vmsa_ttbr_write, },
3684 REGINFO_SENTINEL
3687 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3689 return vfp_get_fpcr(env);
3692 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3693 uint64_t value)
3695 vfp_set_fpcr(env, value);
3698 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3700 return vfp_get_fpsr(env);
3703 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3704 uint64_t value)
3706 vfp_set_fpsr(env, value);
3709 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
3710 bool isread)
3712 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
3713 return CP_ACCESS_TRAP;
3715 return CP_ACCESS_OK;
3718 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
3719 uint64_t value)
3721 env->daif = value & PSTATE_DAIF;
3724 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3725 const ARMCPRegInfo *ri,
3726 bool isread)
3728 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
3729 * SCTLR_EL1.UCI is set.
3731 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
3732 return CP_ACCESS_TRAP;
3734 return CP_ACCESS_OK;
3737 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
3738 * Page D4-1736 (DDI0487A.b)
3741 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3742 uint64_t value)
3744 CPUState *cs = ENV_GET_CPU(env);
3745 bool sec = arm_is_secure_below_el3(env);
3747 if (sec) {
3748 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3749 ARMMMUIdxBit_S1SE1 |
3750 ARMMMUIdxBit_S1SE0);
3751 } else {
3752 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3753 ARMMMUIdxBit_S12NSE1 |
3754 ARMMMUIdxBit_S12NSE0);
3758 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3759 uint64_t value)
3761 CPUState *cs = ENV_GET_CPU(env);
3763 if (tlb_force_broadcast(env)) {
3764 tlbi_aa64_vmalle1is_write(env, NULL, value);
3765 return;
3768 if (arm_is_secure_below_el3(env)) {
3769 tlb_flush_by_mmuidx(cs,
3770 ARMMMUIdxBit_S1SE1 |
3771 ARMMMUIdxBit_S1SE0);
3772 } else {
3773 tlb_flush_by_mmuidx(cs,
3774 ARMMMUIdxBit_S12NSE1 |
3775 ARMMMUIdxBit_S12NSE0);
3779 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3780 uint64_t value)
3782 /* Note that the 'ALL' scope must invalidate both stage 1 and
3783 * stage 2 translations, whereas most other scopes only invalidate
3784 * stage 1 translations.
3786 ARMCPU *cpu = arm_env_get_cpu(env);
3787 CPUState *cs = CPU(cpu);
3789 if (arm_is_secure_below_el3(env)) {
3790 tlb_flush_by_mmuidx(cs,
3791 ARMMMUIdxBit_S1SE1 |
3792 ARMMMUIdxBit_S1SE0);
3793 } else {
3794 if (arm_feature(env, ARM_FEATURE_EL2)) {
3795 tlb_flush_by_mmuidx(cs,
3796 ARMMMUIdxBit_S12NSE1 |
3797 ARMMMUIdxBit_S12NSE0 |
3798 ARMMMUIdxBit_S2NS);
3799 } else {
3800 tlb_flush_by_mmuidx(cs,
3801 ARMMMUIdxBit_S12NSE1 |
3802 ARMMMUIdxBit_S12NSE0);
3807 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3808 uint64_t value)
3810 ARMCPU *cpu = arm_env_get_cpu(env);
3811 CPUState *cs = CPU(cpu);
3813 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
3816 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3817 uint64_t value)
3819 ARMCPU *cpu = arm_env_get_cpu(env);
3820 CPUState *cs = CPU(cpu);
3822 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
3825 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3826 uint64_t value)
3828 /* Note that the 'ALL' scope must invalidate both stage 1 and
3829 * stage 2 translations, whereas most other scopes only invalidate
3830 * stage 1 translations.
3832 CPUState *cs = ENV_GET_CPU(env);
3833 bool sec = arm_is_secure_below_el3(env);
3834 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
3836 if (sec) {
3837 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3838 ARMMMUIdxBit_S1SE1 |
3839 ARMMMUIdxBit_S1SE0);
3840 } else if (has_el2) {
3841 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3842 ARMMMUIdxBit_S12NSE1 |
3843 ARMMMUIdxBit_S12NSE0 |
3844 ARMMMUIdxBit_S2NS);
3845 } else {
3846 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3847 ARMMMUIdxBit_S12NSE1 |
3848 ARMMMUIdxBit_S12NSE0);
3852 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3853 uint64_t value)
3855 CPUState *cs = ENV_GET_CPU(env);
3857 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
3860 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3861 uint64_t value)
3863 CPUState *cs = ENV_GET_CPU(env);
3865 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
3868 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3869 uint64_t value)
3871 /* Invalidate by VA, EL2
3872 * Currently handles both VAE2 and VALE2, since we don't support
3873 * flush-last-level-only.
3875 ARMCPU *cpu = arm_env_get_cpu(env);
3876 CPUState *cs = CPU(cpu);
3877 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3879 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
3882 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3883 uint64_t value)
3885 /* Invalidate by VA, EL3
3886 * Currently handles both VAE3 and VALE3, since we don't support
3887 * flush-last-level-only.
3889 ARMCPU *cpu = arm_env_get_cpu(env);
3890 CPUState *cs = CPU(cpu);
3891 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3893 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
3896 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3897 uint64_t value)
3899 ARMCPU *cpu = arm_env_get_cpu(env);
3900 CPUState *cs = CPU(cpu);
3901 bool sec = arm_is_secure_below_el3(env);
3902 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3904 if (sec) {
3905 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3906 ARMMMUIdxBit_S1SE1 |
3907 ARMMMUIdxBit_S1SE0);
3908 } else {
3909 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3910 ARMMMUIdxBit_S12NSE1 |
3911 ARMMMUIdxBit_S12NSE0);
3915 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3916 uint64_t value)
3918 /* Invalidate by VA, EL1&0 (AArch64 version).
3919 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3920 * since we don't support flush-for-specific-ASID-only or
3921 * flush-last-level-only.
3923 ARMCPU *cpu = arm_env_get_cpu(env);
3924 CPUState *cs = CPU(cpu);
3925 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3927 if (tlb_force_broadcast(env)) {
3928 tlbi_aa64_vae1is_write(env, NULL, value);
3929 return;
3932 if (arm_is_secure_below_el3(env)) {
3933 tlb_flush_page_by_mmuidx(cs, pageaddr,
3934 ARMMMUIdxBit_S1SE1 |
3935 ARMMMUIdxBit_S1SE0);
3936 } else {
3937 tlb_flush_page_by_mmuidx(cs, pageaddr,
3938 ARMMMUIdxBit_S12NSE1 |
3939 ARMMMUIdxBit_S12NSE0);
3943 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3944 uint64_t value)
3946 CPUState *cs = ENV_GET_CPU(env);
3947 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3949 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3950 ARMMMUIdxBit_S1E2);
3953 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3954 uint64_t value)
3956 CPUState *cs = ENV_GET_CPU(env);
3957 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3959 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3960 ARMMMUIdxBit_S1E3);
3963 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3964 uint64_t value)
3966 /* Invalidate by IPA. This has to invalidate any structures that
3967 * contain only stage 2 translation information, but does not need
3968 * to apply to structures that contain combined stage 1 and stage 2
3969 * translation information.
3970 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
3972 ARMCPU *cpu = arm_env_get_cpu(env);
3973 CPUState *cs = CPU(cpu);
3974 uint64_t pageaddr;
3976 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3977 return;
3980 pageaddr = sextract64(value << 12, 0, 48);
3982 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
3985 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3986 uint64_t value)
3988 CPUState *cs = ENV_GET_CPU(env);
3989 uint64_t pageaddr;
3991 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
3992 return;
3995 pageaddr = sextract64(value << 12, 0, 48);
3997 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3998 ARMMMUIdxBit_S2NS);
4001 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4002 bool isread)
4004 /* We don't implement EL2, so the only control on DC ZVA is the
4005 * bit in the SCTLR which can prohibit access for EL0.
4007 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4008 return CP_ACCESS_TRAP;
4010 return CP_ACCESS_OK;
4013 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4015 ARMCPU *cpu = arm_env_get_cpu(env);
4016 int dzp_bit = 1 << 4;
4018 /* DZP indicates whether DC ZVA access is allowed */
4019 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4020 dzp_bit = 0;
4022 return cpu->dcz_blocksize | dzp_bit;
4025 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4026 bool isread)
4028 if (!(env->pstate & PSTATE_SP)) {
4029 /* Access to SP_EL0 is undefined if it's being used as
4030 * the stack pointer.
4032 return CP_ACCESS_TRAP_UNCATEGORIZED;
4034 return CP_ACCESS_OK;
4037 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4039 return env->pstate & PSTATE_SP;
4042 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4044 update_spsel(env, val);
4047 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4048 uint64_t value)
4050 ARMCPU *cpu = arm_env_get_cpu(env);
4052 if (raw_read(env, ri) == value) {
4053 /* Skip the TLB flush if nothing actually changed; Linux likes
4054 * to do a lot of pointless SCTLR writes.
4056 return;
4059 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4060 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4061 value &= ~SCTLR_M;
4064 raw_write(env, ri, value);
4065 /* ??? Lots of these bits are not implemented. */
4066 /* This may enable/disable the MMU, so do a TLB flush. */
4067 tlb_flush(CPU(cpu));
4070 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4071 bool isread)
4073 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
4074 return CP_ACCESS_TRAP_FP_EL2;
4076 if (env->cp15.cptr_el[3] & CPTR_TFP) {
4077 return CP_ACCESS_TRAP_FP_EL3;
4079 return CP_ACCESS_OK;
4082 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4083 uint64_t value)
4085 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4088 static const ARMCPRegInfo v8_cp_reginfo[] = {
4089 /* Minimal set of EL0-visible registers. This will need to be expanded
4090 * significantly for system emulation of AArch64 CPUs.
4092 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4093 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4094 .access = PL0_RW, .type = ARM_CP_NZCV },
4095 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4096 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4097 .type = ARM_CP_NO_RAW,
4098 .access = PL0_RW, .accessfn = aa64_daif_access,
4099 .fieldoffset = offsetof(CPUARMState, daif),
4100 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4101 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4102 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4103 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4104 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4105 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4106 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4107 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4108 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4109 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4110 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4111 .access = PL0_R, .type = ARM_CP_NO_RAW,
4112 .readfn = aa64_dczid_read },
4113 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4114 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4115 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4116 #ifndef CONFIG_USER_ONLY
4117 /* Avoid overhead of an access check that always passes in user-mode */
4118 .accessfn = aa64_zva_access,
4119 #endif
4121 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4122 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4123 .access = PL1_R, .type = ARM_CP_CURRENTEL },
4124 /* Cache ops: all NOPs since we don't emulate caches */
4125 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4126 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4127 .access = PL1_W, .type = ARM_CP_NOP },
4128 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4129 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4130 .access = PL1_W, .type = ARM_CP_NOP },
4131 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4132 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4133 .access = PL0_W, .type = ARM_CP_NOP,
4134 .accessfn = aa64_cacheop_access },
4135 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4136 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4137 .access = PL1_W, .type = ARM_CP_NOP },
4138 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4139 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4140 .access = PL1_W, .type = ARM_CP_NOP },
4141 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4142 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4143 .access = PL0_W, .type = ARM_CP_NOP,
4144 .accessfn = aa64_cacheop_access },
4145 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4146 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4147 .access = PL1_W, .type = ARM_CP_NOP },
4148 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4149 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4150 .access = PL0_W, .type = ARM_CP_NOP,
4151 .accessfn = aa64_cacheop_access },
4152 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4153 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4154 .access = PL0_W, .type = ARM_CP_NOP,
4155 .accessfn = aa64_cacheop_access },
4156 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4157 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4158 .access = PL1_W, .type = ARM_CP_NOP },
4159 /* TLBI operations */
4160 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4161 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4162 .access = PL1_W, .type = ARM_CP_NO_RAW,
4163 .writefn = tlbi_aa64_vmalle1is_write },
4164 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4165 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4166 .access = PL1_W, .type = ARM_CP_NO_RAW,
4167 .writefn = tlbi_aa64_vae1is_write },
4168 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4169 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4170 .access = PL1_W, .type = ARM_CP_NO_RAW,
4171 .writefn = tlbi_aa64_vmalle1is_write },
4172 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4173 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4174 .access = PL1_W, .type = ARM_CP_NO_RAW,
4175 .writefn = tlbi_aa64_vae1is_write },
4176 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4177 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4178 .access = PL1_W, .type = ARM_CP_NO_RAW,
4179 .writefn = tlbi_aa64_vae1is_write },
4180 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4181 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4182 .access = PL1_W, .type = ARM_CP_NO_RAW,
4183 .writefn = tlbi_aa64_vae1is_write },
4184 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4185 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4186 .access = PL1_W, .type = ARM_CP_NO_RAW,
4187 .writefn = tlbi_aa64_vmalle1_write },
4188 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4189 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4190 .access = PL1_W, .type = ARM_CP_NO_RAW,
4191 .writefn = tlbi_aa64_vae1_write },
4192 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4193 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4194 .access = PL1_W, .type = ARM_CP_NO_RAW,
4195 .writefn = tlbi_aa64_vmalle1_write },
4196 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4197 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4198 .access = PL1_W, .type = ARM_CP_NO_RAW,
4199 .writefn = tlbi_aa64_vae1_write },
4200 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4201 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4202 .access = PL1_W, .type = ARM_CP_NO_RAW,
4203 .writefn = tlbi_aa64_vae1_write },
4204 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4205 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4206 .access = PL1_W, .type = ARM_CP_NO_RAW,
4207 .writefn = tlbi_aa64_vae1_write },
4208 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4209 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4210 .access = PL2_W, .type = ARM_CP_NO_RAW,
4211 .writefn = tlbi_aa64_ipas2e1is_write },
4212 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4213 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4214 .access = PL2_W, .type = ARM_CP_NO_RAW,
4215 .writefn = tlbi_aa64_ipas2e1is_write },
4216 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4217 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4218 .access = PL2_W, .type = ARM_CP_NO_RAW,
4219 .writefn = tlbi_aa64_alle1is_write },
4220 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4221 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4222 .access = PL2_W, .type = ARM_CP_NO_RAW,
4223 .writefn = tlbi_aa64_alle1is_write },
4224 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4225 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4226 .access = PL2_W, .type = ARM_CP_NO_RAW,
4227 .writefn = tlbi_aa64_ipas2e1_write },
4228 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4229 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4230 .access = PL2_W, .type = ARM_CP_NO_RAW,
4231 .writefn = tlbi_aa64_ipas2e1_write },
4232 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4233 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4234 .access = PL2_W, .type = ARM_CP_NO_RAW,
4235 .writefn = tlbi_aa64_alle1_write },
4236 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4237 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4238 .access = PL2_W, .type = ARM_CP_NO_RAW,
4239 .writefn = tlbi_aa64_alle1is_write },
4240 #ifndef CONFIG_USER_ONLY
4241 /* 64 bit address translation operations */
4242 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4243 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4244 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4245 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4246 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4247 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4248 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4249 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4250 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4251 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4252 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4253 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4254 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4255 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4256 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4257 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4258 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4259 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4260 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4261 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4262 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4263 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4264 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4265 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4266 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4267 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4268 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4269 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4270 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4271 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4272 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4273 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4274 .type = ARM_CP_ALIAS,
4275 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4276 .access = PL1_RW, .resetvalue = 0,
4277 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4278 .writefn = par_write },
4279 #endif
4280 /* TLB invalidate last level of translation table walk */
4281 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4282 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
4283 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4284 .type = ARM_CP_NO_RAW, .access = PL1_W,
4285 .writefn = tlbimvaa_is_write },
4286 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4287 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
4288 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4289 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
4290 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4291 .type = ARM_CP_NO_RAW, .access = PL2_W,
4292 .writefn = tlbimva_hyp_write },
4293 { .name = "TLBIMVALHIS",
4294 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4295 .type = ARM_CP_NO_RAW, .access = PL2_W,
4296 .writefn = tlbimva_hyp_is_write },
4297 { .name = "TLBIIPAS2",
4298 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4299 .type = ARM_CP_NO_RAW, .access = PL2_W,
4300 .writefn = tlbiipas2_write },
4301 { .name = "TLBIIPAS2IS",
4302 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4303 .type = ARM_CP_NO_RAW, .access = PL2_W,
4304 .writefn = tlbiipas2_is_write },
4305 { .name = "TLBIIPAS2L",
4306 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4307 .type = ARM_CP_NO_RAW, .access = PL2_W,
4308 .writefn = tlbiipas2_write },
4309 { .name = "TLBIIPAS2LIS",
4310 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4311 .type = ARM_CP_NO_RAW, .access = PL2_W,
4312 .writefn = tlbiipas2_is_write },
4313 /* 32 bit cache operations */
4314 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4315 .type = ARM_CP_NOP, .access = PL1_W },
4316 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4317 .type = ARM_CP_NOP, .access = PL1_W },
4318 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4319 .type = ARM_CP_NOP, .access = PL1_W },
4320 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
4321 .type = ARM_CP_NOP, .access = PL1_W },
4322 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
4323 .type = ARM_CP_NOP, .access = PL1_W },
4324 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
4325 .type = ARM_CP_NOP, .access = PL1_W },
4326 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4327 .type = ARM_CP_NOP, .access = PL1_W },
4328 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4329 .type = ARM_CP_NOP, .access = PL1_W },
4330 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
4331 .type = ARM_CP_NOP, .access = PL1_W },
4332 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4333 .type = ARM_CP_NOP, .access = PL1_W },
4334 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
4335 .type = ARM_CP_NOP, .access = PL1_W },
4336 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
4337 .type = ARM_CP_NOP, .access = PL1_W },
4338 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4339 .type = ARM_CP_NOP, .access = PL1_W },
4340 /* MMU Domain access control / MPU write buffer control */
4341 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
4342 .access = PL1_RW, .resetvalue = 0,
4343 .writefn = dacr_write, .raw_writefn = raw_write,
4344 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
4345 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
4346 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
4347 .type = ARM_CP_ALIAS,
4348 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
4349 .access = PL1_RW,
4350 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
4351 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
4352 .type = ARM_CP_ALIAS,
4353 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
4354 .access = PL1_RW,
4355 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
4356 /* We rely on the access checks not allowing the guest to write to the
4357 * state field when SPSel indicates that it's being used as the stack
4358 * pointer.
4360 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
4361 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
4362 .access = PL1_RW, .accessfn = sp_el0_access,
4363 .type = ARM_CP_ALIAS,
4364 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
4365 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
4366 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
4367 .access = PL2_RW, .type = ARM_CP_ALIAS,
4368 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
4369 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
4370 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
4371 .type = ARM_CP_NO_RAW,
4372 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
4373 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
4374 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
4375 .type = ARM_CP_ALIAS,
4376 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
4377 .access = PL2_RW, .accessfn = fpexc32_access },
4378 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
4379 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
4380 .access = PL2_RW, .resetvalue = 0,
4381 .writefn = dacr_write, .raw_writefn = raw_write,
4382 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
4383 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
4384 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
4385 .access = PL2_RW, .resetvalue = 0,
4386 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
4387 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
4388 .type = ARM_CP_ALIAS,
4389 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
4390 .access = PL2_RW,
4391 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
4392 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
4393 .type = ARM_CP_ALIAS,
4394 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
4395 .access = PL2_RW,
4396 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
4397 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
4398 .type = ARM_CP_ALIAS,
4399 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
4400 .access = PL2_RW,
4401 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
4402 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
4403 .type = ARM_CP_ALIAS,
4404 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
4405 .access = PL2_RW,
4406 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
4407 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
4408 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
4409 .resetvalue = 0,
4410 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
4411 { .name = "SDCR", .type = ARM_CP_ALIAS,
4412 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
4413 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4414 .writefn = sdcr_write,
4415 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
4416 REGINFO_SENTINEL
4419 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4420 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
4421 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
4422 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
4423 .access = PL2_RW,
4424 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
4425 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
4426 .type = ARM_CP_NO_RAW,
4427 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4428 .access = PL2_RW,
4429 .type = ARM_CP_CONST, .resetvalue = 0 },
4430 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
4431 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
4432 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4433 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
4434 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
4435 .access = PL2_RW,
4436 .type = ARM_CP_CONST, .resetvalue = 0 },
4437 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
4438 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
4439 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4440 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
4441 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
4442 .access = PL2_RW, .type = ARM_CP_CONST,
4443 .resetvalue = 0 },
4444 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
4445 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
4446 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4447 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
4448 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
4449 .access = PL2_RW, .type = ARM_CP_CONST,
4450 .resetvalue = 0 },
4451 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
4452 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
4453 .access = PL2_RW, .type = ARM_CP_CONST,
4454 .resetvalue = 0 },
4455 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
4456 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
4457 .access = PL2_RW, .type = ARM_CP_CONST,
4458 .resetvalue = 0 },
4459 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
4460 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
4461 .access = PL2_RW, .type = ARM_CP_CONST,
4462 .resetvalue = 0 },
4463 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
4464 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
4465 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4466 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
4467 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
4468 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4469 .type = ARM_CP_CONST, .resetvalue = 0 },
4470 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
4471 .cp = 15, .opc1 = 6, .crm = 2,
4472 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4473 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
4474 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
4475 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
4476 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4477 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
4478 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
4479 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4480 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4481 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
4482 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4483 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
4484 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
4485 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4486 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
4487 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4488 .resetvalue = 0 },
4489 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
4490 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
4491 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4492 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
4493 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
4494 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4495 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
4496 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4497 .resetvalue = 0 },
4498 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
4499 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
4500 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4501 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4502 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4503 .resetvalue = 0 },
4504 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4505 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
4506 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4507 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4508 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4509 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4510 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4511 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4512 .access = PL2_RW, .accessfn = access_tda,
4513 .type = ARM_CP_CONST, .resetvalue = 0 },
4514 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
4515 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4516 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4517 .type = ARM_CP_CONST, .resetvalue = 0 },
4518 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4519 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4520 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4521 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
4522 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
4523 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4524 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
4525 .type = ARM_CP_CONST,
4526 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
4527 .access = PL2_RW, .resetvalue = 0 },
4528 REGINFO_SENTINEL
4531 /* Ditto, but for registers which exist in ARMv8 but not v7 */
4532 static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
4533 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
4534 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
4535 .access = PL2_RW,
4536 .type = ARM_CP_CONST, .resetvalue = 0 },
4537 REGINFO_SENTINEL
4540 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4542 ARMCPU *cpu = arm_env_get_cpu(env);
4543 uint64_t valid_mask = HCR_MASK;
4545 if (arm_feature(env, ARM_FEATURE_EL3)) {
4546 valid_mask &= ~HCR_HCD;
4547 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
4548 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
4549 * However, if we're using the SMC PSCI conduit then QEMU is
4550 * effectively acting like EL3 firmware and so the guest at
4551 * EL2 should retain the ability to prevent EL1 from being
4552 * able to make SMC calls into the ersatz firmware, so in
4553 * that case HCR.TSC should be read/write.
4555 valid_mask &= ~HCR_TSC;
4557 if (cpu_isar_feature(aa64_lor, cpu)) {
4558 valid_mask |= HCR_TLOR;
4560 if (cpu_isar_feature(aa64_pauth, cpu)) {
4561 valid_mask |= HCR_API | HCR_APK;
4564 /* Clear RES0 bits. */
4565 value &= valid_mask;
4567 /* These bits change the MMU setup:
4568 * HCR_VM enables stage 2 translation
4569 * HCR_PTW forbids certain page-table setups
4570 * HCR_DC Disables stage1 and enables stage2 translation
4572 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
4573 tlb_flush(CPU(cpu));
4575 env->cp15.hcr_el2 = value;
4578 * Updates to VI and VF require us to update the status of
4579 * virtual interrupts, which are the logical OR of these bits
4580 * and the state of the input lines from the GIC. (This requires
4581 * that we have the iothread lock, which is done by marking the
4582 * reginfo structs as ARM_CP_IO.)
4583 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
4584 * possible for it to be taken immediately, because VIRQ and
4585 * VFIQ are masked unless running at EL0 or EL1, and HCR
4586 * can only be written at EL2.
4588 g_assert(qemu_mutex_iothread_locked());
4589 arm_cpu_update_virq(cpu);
4590 arm_cpu_update_vfiq(cpu);
4593 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
4594 uint64_t value)
4596 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
4597 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
4598 hcr_write(env, NULL, value);
4601 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
4602 uint64_t value)
4604 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
4605 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
4606 hcr_write(env, NULL, value);
4610 * Return the effective value of HCR_EL2.
4611 * Bits that are not included here:
4612 * RW (read from SCR_EL3.RW as needed)
4614 uint64_t arm_hcr_el2_eff(CPUARMState *env)
4616 uint64_t ret = env->cp15.hcr_el2;
4618 if (arm_is_secure_below_el3(env)) {
4620 * "This register has no effect if EL2 is not enabled in the
4621 * current Security state". This is ARMv8.4-SecEL2 speak for
4622 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
4624 * Prior to that, the language was "In an implementation that
4625 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
4626 * as if this field is 0 for all purposes other than a direct
4627 * read or write access of HCR_EL2". With lots of enumeration
4628 * on a per-field basis. In current QEMU, this is condition
4629 * is arm_is_secure_below_el3.
4631 * Since the v8.4 language applies to the entire register, and
4632 * appears to be backward compatible, use that.
4634 ret = 0;
4635 } else if (ret & HCR_TGE) {
4636 /* These bits are up-to-date as of ARMv8.4. */
4637 if (ret & HCR_E2H) {
4638 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
4639 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
4640 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4641 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE);
4642 } else {
4643 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
4645 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
4646 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
4647 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
4648 HCR_TLOR);
4651 return ret;
4654 static const ARMCPRegInfo el2_cp_reginfo[] = {
4655 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
4656 .type = ARM_CP_IO,
4657 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4658 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
4659 .writefn = hcr_write },
4660 { .name = "HCR", .state = ARM_CP_STATE_AA32,
4661 .type = ARM_CP_ALIAS | ARM_CP_IO,
4662 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4663 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
4664 .writefn = hcr_writelow },
4665 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
4666 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
4667 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4668 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
4669 .type = ARM_CP_ALIAS,
4670 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
4671 .access = PL2_RW,
4672 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
4673 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
4674 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
4675 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
4676 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
4677 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
4678 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
4679 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
4680 .type = ARM_CP_ALIAS,
4681 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
4682 .access = PL2_RW,
4683 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
4684 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
4685 .type = ARM_CP_ALIAS,
4686 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
4687 .access = PL2_RW,
4688 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
4689 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
4690 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
4691 .access = PL2_RW, .writefn = vbar_write,
4692 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
4693 .resetvalue = 0 },
4694 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
4695 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
4696 .access = PL3_RW, .type = ARM_CP_ALIAS,
4697 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
4698 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
4699 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
4700 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
4701 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
4702 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
4703 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
4704 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
4705 .resetvalue = 0 },
4706 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
4707 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
4708 .access = PL2_RW, .type = ARM_CP_ALIAS,
4709 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
4710 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
4711 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
4712 .access = PL2_RW, .type = ARM_CP_CONST,
4713 .resetvalue = 0 },
4714 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
4715 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
4716 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
4717 .access = PL2_RW, .type = ARM_CP_CONST,
4718 .resetvalue = 0 },
4719 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
4720 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
4721 .access = PL2_RW, .type = ARM_CP_CONST,
4722 .resetvalue = 0 },
4723 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
4724 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
4725 .access = PL2_RW, .type = ARM_CP_CONST,
4726 .resetvalue = 0 },
4727 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
4728 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
4729 .access = PL2_RW,
4730 /* no .writefn needed as this can't cause an ASID change;
4731 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
4733 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
4734 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
4735 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
4736 .type = ARM_CP_ALIAS,
4737 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4738 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
4739 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
4740 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
4741 .access = PL2_RW,
4742 /* no .writefn needed as this can't cause an ASID change;
4743 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
4745 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
4746 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
4747 .cp = 15, .opc1 = 6, .crm = 2,
4748 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4749 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4750 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
4751 .writefn = vttbr_write },
4752 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
4753 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
4754 .access = PL2_RW, .writefn = vttbr_write,
4755 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
4756 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
4757 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
4758 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
4759 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
4760 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4761 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
4762 .access = PL2_RW, .resetvalue = 0,
4763 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
4764 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
4765 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
4766 .access = PL2_RW, .resetvalue = 0,
4767 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
4768 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
4769 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4770 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
4771 { .name = "TLBIALLNSNH",
4772 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4773 .type = ARM_CP_NO_RAW, .access = PL2_W,
4774 .writefn = tlbiall_nsnh_write },
4775 { .name = "TLBIALLNSNHIS",
4776 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4777 .type = ARM_CP_NO_RAW, .access = PL2_W,
4778 .writefn = tlbiall_nsnh_is_write },
4779 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
4780 .type = ARM_CP_NO_RAW, .access = PL2_W,
4781 .writefn = tlbiall_hyp_write },
4782 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
4783 .type = ARM_CP_NO_RAW, .access = PL2_W,
4784 .writefn = tlbiall_hyp_is_write },
4785 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
4786 .type = ARM_CP_NO_RAW, .access = PL2_W,
4787 .writefn = tlbimva_hyp_write },
4788 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
4789 .type = ARM_CP_NO_RAW, .access = PL2_W,
4790 .writefn = tlbimva_hyp_is_write },
4791 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
4792 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
4793 .type = ARM_CP_NO_RAW, .access = PL2_W,
4794 .writefn = tlbi_aa64_alle2_write },
4795 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
4796 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
4797 .type = ARM_CP_NO_RAW, .access = PL2_W,
4798 .writefn = tlbi_aa64_vae2_write },
4799 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
4800 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4801 .access = PL2_W, .type = ARM_CP_NO_RAW,
4802 .writefn = tlbi_aa64_vae2_write },
4803 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
4804 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
4805 .access = PL2_W, .type = ARM_CP_NO_RAW,
4806 .writefn = tlbi_aa64_alle2is_write },
4807 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
4808 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
4809 .type = ARM_CP_NO_RAW, .access = PL2_W,
4810 .writefn = tlbi_aa64_vae2is_write },
4811 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
4812 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4813 .access = PL2_W, .type = ARM_CP_NO_RAW,
4814 .writefn = tlbi_aa64_vae2is_write },
4815 #ifndef CONFIG_USER_ONLY
4816 /* Unlike the other EL2-related AT operations, these must
4817 * UNDEF from EL3 if EL2 is not implemented, which is why we
4818 * define them here rather than with the rest of the AT ops.
4820 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
4821 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
4822 .access = PL2_W, .accessfn = at_s1e2_access,
4823 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4824 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
4825 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
4826 .access = PL2_W, .accessfn = at_s1e2_access,
4827 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4828 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
4829 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
4830 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
4831 * to behave as if SCR.NS was 1.
4833 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
4834 .access = PL2_W,
4835 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
4836 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
4837 .access = PL2_W,
4838 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
4839 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
4840 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
4841 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
4842 * reset values as IMPDEF. We choose to reset to 3 to comply with
4843 * both ARMv7 and ARMv8.
4845 .access = PL2_RW, .resetvalue = 3,
4846 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
4847 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
4848 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
4849 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
4850 .writefn = gt_cntvoff_write,
4851 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
4852 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
4853 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
4854 .writefn = gt_cntvoff_write,
4855 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
4856 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
4857 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
4858 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4859 .type = ARM_CP_IO, .access = PL2_RW,
4860 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4861 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4862 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4863 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
4864 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4865 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4866 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
4867 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
4868 .resetfn = gt_hyp_timer_reset,
4869 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
4870 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4871 .type = ARM_CP_IO,
4872 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4873 .access = PL2_RW,
4874 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
4875 .resetvalue = 0,
4876 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
4877 #endif
4878 /* The only field of MDCR_EL2 that has a defined architectural reset value
4879 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
4880 * don't implement any PMU event counters, so using zero as a reset
4881 * value for MDCR_EL2 is okay
4883 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4884 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4885 .access = PL2_RW, .resetvalue = 0,
4886 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
4887 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
4888 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4889 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4890 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4891 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
4892 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4893 .access = PL2_RW,
4894 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4895 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4896 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4897 .access = PL2_RW,
4898 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
4899 REGINFO_SENTINEL
4902 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
4903 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
4904 .type = ARM_CP_ALIAS | ARM_CP_IO,
4905 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
4906 .access = PL2_RW,
4907 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
4908 .writefn = hcr_writehigh },
4909 REGINFO_SENTINEL
4912 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4913 bool isread)
4915 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
4916 * At Secure EL1 it traps to EL3.
4918 if (arm_current_el(env) == 3) {
4919 return CP_ACCESS_OK;
4921 if (arm_is_secure_below_el3(env)) {
4922 return CP_ACCESS_TRAP_EL3;
4924 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4925 if (isread) {
4926 return CP_ACCESS_OK;
4928 return CP_ACCESS_TRAP_UNCATEGORIZED;
4931 static const ARMCPRegInfo el3_cp_reginfo[] = {
4932 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
4933 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
4934 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
4935 .resetvalue = 0, .writefn = scr_write },
4936 { .name = "SCR", .type = ARM_CP_ALIAS,
4937 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
4938 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4939 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
4940 .writefn = scr_write },
4941 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
4942 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
4943 .access = PL3_RW, .resetvalue = 0,
4944 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
4945 { .name = "SDER",
4946 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
4947 .access = PL3_RW, .resetvalue = 0,
4948 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
4949 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4950 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4951 .writefn = vbar_write, .resetvalue = 0,
4952 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
4953 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
4954 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
4955 .access = PL3_RW, .resetvalue = 0,
4956 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
4957 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
4958 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
4959 .access = PL3_RW,
4960 /* no .writefn needed as this can't cause an ASID change;
4961 * we must provide a .raw_writefn and .resetfn because we handle
4962 * reset and migration for the AArch32 TTBCR(S), which might be
4963 * using mask and base_mask.
4965 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
4966 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
4967 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
4968 .type = ARM_CP_ALIAS,
4969 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
4970 .access = PL3_RW,
4971 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
4972 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
4973 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
4974 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
4975 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
4976 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
4977 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
4978 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
4979 .type = ARM_CP_ALIAS,
4980 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
4981 .access = PL3_RW,
4982 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
4983 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
4984 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
4985 .access = PL3_RW, .writefn = vbar_write,
4986 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
4987 .resetvalue = 0 },
4988 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
4989 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
4990 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
4991 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4992 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
4993 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
4994 .access = PL3_RW, .resetvalue = 0,
4995 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
4996 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
4997 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
4998 .access = PL3_RW, .type = ARM_CP_CONST,
4999 .resetvalue = 0 },
5000 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5001 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5002 .access = PL3_RW, .type = ARM_CP_CONST,
5003 .resetvalue = 0 },
5004 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5005 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5006 .access = PL3_RW, .type = ARM_CP_CONST,
5007 .resetvalue = 0 },
5008 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5009 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5010 .access = PL3_W, .type = ARM_CP_NO_RAW,
5011 .writefn = tlbi_aa64_alle3is_write },
5012 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5013 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5014 .access = PL3_W, .type = ARM_CP_NO_RAW,
5015 .writefn = tlbi_aa64_vae3is_write },
5016 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5017 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5018 .access = PL3_W, .type = ARM_CP_NO_RAW,
5019 .writefn = tlbi_aa64_vae3is_write },
5020 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5021 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5022 .access = PL3_W, .type = ARM_CP_NO_RAW,
5023 .writefn = tlbi_aa64_alle3_write },
5024 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5025 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5026 .access = PL3_W, .type = ARM_CP_NO_RAW,
5027 .writefn = tlbi_aa64_vae3_write },
5028 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5029 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5030 .access = PL3_W, .type = ARM_CP_NO_RAW,
5031 .writefn = tlbi_aa64_vae3_write },
5032 REGINFO_SENTINEL
5035 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5036 bool isread)
5038 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
5039 * but the AArch32 CTR has its own reginfo struct)
5041 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5042 return CP_ACCESS_TRAP;
5044 return CP_ACCESS_OK;
5047 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5048 uint64_t value)
5050 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5051 * read via a bit in OSLSR_EL1.
5053 int oslock;
5055 if (ri->state == ARM_CP_STATE_AA32) {
5056 oslock = (value == 0xC5ACCE55);
5057 } else {
5058 oslock = value & 1;
5061 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5064 static const ARMCPRegInfo debug_cp_reginfo[] = {
5065 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
5066 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5067 * unlike DBGDRAR it is never accessible from EL0.
5068 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5069 * accessor.
5071 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
5072 .access = PL0_R, .accessfn = access_tdra,
5073 .type = ARM_CP_CONST, .resetvalue = 0 },
5074 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
5075 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5076 .access = PL1_R, .accessfn = access_tdra,
5077 .type = ARM_CP_CONST, .resetvalue = 0 },
5078 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
5079 .access = PL0_R, .accessfn = access_tdra,
5080 .type = ARM_CP_CONST, .resetvalue = 0 },
5081 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
5082 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
5083 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
5084 .access = PL1_RW, .accessfn = access_tda,
5085 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
5086 .resetvalue = 0 },
5087 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
5088 * We don't implement the configurable EL0 access.
5090 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
5091 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
5092 .type = ARM_CP_ALIAS,
5093 .access = PL1_R, .accessfn = access_tda,
5094 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
5095 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
5096 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
5097 .access = PL1_W, .type = ARM_CP_NO_RAW,
5098 .accessfn = access_tdosa,
5099 .writefn = oslar_write },
5100 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
5101 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
5102 .access = PL1_R, .resetvalue = 10,
5103 .accessfn = access_tdosa,
5104 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5105 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
5106 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
5107 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
5108 .access = PL1_RW, .accessfn = access_tdosa,
5109 .type = ARM_CP_NOP },
5110 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
5111 * implement vector catch debug events yet.
5113 { .name = "DBGVCR",
5114 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
5115 .access = PL1_RW, .accessfn = access_tda,
5116 .type = ARM_CP_NOP },
5117 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
5118 * to save and restore a 32-bit guest's DBGVCR)
5120 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
5121 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
5122 .access = PL2_RW, .accessfn = access_tda,
5123 .type = ARM_CP_NOP },
5124 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
5125 * Channel but Linux may try to access this register. The 32-bit
5126 * alias is DBGDCCINT.
5128 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
5129 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5130 .access = PL1_RW, .accessfn = access_tda,
5131 .type = ARM_CP_NOP },
5132 REGINFO_SENTINEL
5135 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
5136 /* 64 bit access versions of the (dummy) debug registers */
5137 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
5138 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5139 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
5140 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5141 REGINFO_SENTINEL
5144 /* Return the exception level to which exceptions should be taken
5145 * via SVEAccessTrap. If an exception should be routed through
5146 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
5147 * take care of raising that exception.
5148 * C.f. the ARM pseudocode function CheckSVEEnabled.
5150 int sve_exception_el(CPUARMState *env, int el)
5152 #ifndef CONFIG_USER_ONLY
5153 if (el <= 1) {
5154 bool disabled = false;
5156 /* The CPACR.ZEN controls traps to EL1:
5157 * 0, 2 : trap EL0 and EL1 accesses
5158 * 1 : trap only EL0 accesses
5159 * 3 : trap no accesses
5161 if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
5162 disabled = true;
5163 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
5164 disabled = el == 0;
5166 if (disabled) {
5167 /* route_to_el2 */
5168 return (arm_feature(env, ARM_FEATURE_EL2)
5169 && (arm_hcr_el2_eff(env) & HCR_TGE) ? 2 : 1);
5172 /* Check CPACR.FPEN. */
5173 if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
5174 disabled = true;
5175 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
5176 disabled = el == 0;
5178 if (disabled) {
5179 return 0;
5183 /* CPTR_EL2. Since TZ and TFP are positive,
5184 * they will be zero when EL2 is not present.
5186 if (el <= 2 && !arm_is_secure_below_el3(env)) {
5187 if (env->cp15.cptr_el[2] & CPTR_TZ) {
5188 return 2;
5190 if (env->cp15.cptr_el[2] & CPTR_TFP) {
5191 return 0;
5195 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
5196 if (arm_feature(env, ARM_FEATURE_EL3)
5197 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5198 return 3;
5200 #endif
5201 return 0;
5205 * Given that SVE is enabled, return the vector length for EL.
5207 uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
5209 ARMCPU *cpu = arm_env_get_cpu(env);
5210 uint32_t zcr_len = cpu->sve_max_vq - 1;
5212 if (el <= 1) {
5213 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
5215 if (el < 2 && arm_feature(env, ARM_FEATURE_EL2)) {
5216 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
5218 if (el < 3 && arm_feature(env, ARM_FEATURE_EL3)) {
5219 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
5221 return zcr_len;
5224 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5225 uint64_t value)
5227 int cur_el = arm_current_el(env);
5228 int old_len = sve_zcr_len_for_el(env, cur_el);
5229 int new_len;
5231 /* Bits other than [3:0] are RAZ/WI. */
5232 raw_write(env, ri, value & 0xf);
5235 * Because we arrived here, we know both FP and SVE are enabled;
5236 * otherwise we would have trapped access to the ZCR_ELn register.
5238 new_len = sve_zcr_len_for_el(env, cur_el);
5239 if (new_len < old_len) {
5240 aarch64_sve_narrow_vq(env, new_len + 1);
5244 static const ARMCPRegInfo zcr_el1_reginfo = {
5245 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
5246 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
5247 .access = PL1_RW, .type = ARM_CP_SVE,
5248 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
5249 .writefn = zcr_write, .raw_writefn = raw_write
5252 static const ARMCPRegInfo zcr_el2_reginfo = {
5253 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
5254 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
5255 .access = PL2_RW, .type = ARM_CP_SVE,
5256 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
5257 .writefn = zcr_write, .raw_writefn = raw_write
5260 static const ARMCPRegInfo zcr_no_el2_reginfo = {
5261 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
5262 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
5263 .access = PL2_RW, .type = ARM_CP_SVE,
5264 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
5267 static const ARMCPRegInfo zcr_el3_reginfo = {
5268 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
5269 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
5270 .access = PL3_RW, .type = ARM_CP_SVE,
5271 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
5272 .writefn = zcr_write, .raw_writefn = raw_write
5275 void hw_watchpoint_update(ARMCPU *cpu, int n)
5277 CPUARMState *env = &cpu->env;
5278 vaddr len = 0;
5279 vaddr wvr = env->cp15.dbgwvr[n];
5280 uint64_t wcr = env->cp15.dbgwcr[n];
5281 int mask;
5282 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
5284 if (env->cpu_watchpoint[n]) {
5285 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
5286 env->cpu_watchpoint[n] = NULL;
5289 if (!extract64(wcr, 0, 1)) {
5290 /* E bit clear : watchpoint disabled */
5291 return;
5294 switch (extract64(wcr, 3, 2)) {
5295 case 0:
5296 /* LSC 00 is reserved and must behave as if the wp is disabled */
5297 return;
5298 case 1:
5299 flags |= BP_MEM_READ;
5300 break;
5301 case 2:
5302 flags |= BP_MEM_WRITE;
5303 break;
5304 case 3:
5305 flags |= BP_MEM_ACCESS;
5306 break;
5309 /* Attempts to use both MASK and BAS fields simultaneously are
5310 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
5311 * thus generating a watchpoint for every byte in the masked region.
5313 mask = extract64(wcr, 24, 4);
5314 if (mask == 1 || mask == 2) {
5315 /* Reserved values of MASK; we must act as if the mask value was
5316 * some non-reserved value, or as if the watchpoint were disabled.
5317 * We choose the latter.
5319 return;
5320 } else if (mask) {
5321 /* Watchpoint covers an aligned area up to 2GB in size */
5322 len = 1ULL << mask;
5323 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
5324 * whether the watchpoint fires when the unmasked bits match; we opt
5325 * to generate the exceptions.
5327 wvr &= ~(len - 1);
5328 } else {
5329 /* Watchpoint covers bytes defined by the byte address select bits */
5330 int bas = extract64(wcr, 5, 8);
5331 int basstart;
5333 if (bas == 0) {
5334 /* This must act as if the watchpoint is disabled */
5335 return;
5338 if (extract64(wvr, 2, 1)) {
5339 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
5340 * ignored, and BAS[3:0] define which bytes to watch.
5342 bas &= 0xf;
5344 /* The BAS bits are supposed to be programmed to indicate a contiguous
5345 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
5346 * we fire for each byte in the word/doubleword addressed by the WVR.
5347 * We choose to ignore any non-zero bits after the first range of 1s.
5349 basstart = ctz32(bas);
5350 len = cto32(bas >> basstart);
5351 wvr += basstart;
5354 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
5355 &env->cpu_watchpoint[n]);
5358 void hw_watchpoint_update_all(ARMCPU *cpu)
5360 int i;
5361 CPUARMState *env = &cpu->env;
5363 /* Completely clear out existing QEMU watchpoints and our array, to
5364 * avoid possible stale entries following migration load.
5366 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
5367 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
5369 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
5370 hw_watchpoint_update(cpu, i);
5374 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5375 uint64_t value)
5377 ARMCPU *cpu = arm_env_get_cpu(env);
5378 int i = ri->crm;
5380 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
5381 * register reads and behaves as if values written are sign extended.
5382 * Bits [1:0] are RES0.
5384 value = sextract64(value, 0, 49) & ~3ULL;
5386 raw_write(env, ri, value);
5387 hw_watchpoint_update(cpu, i);
5390 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5391 uint64_t value)
5393 ARMCPU *cpu = arm_env_get_cpu(env);
5394 int i = ri->crm;
5396 raw_write(env, ri, value);
5397 hw_watchpoint_update(cpu, i);
5400 void hw_breakpoint_update(ARMCPU *cpu, int n)
5402 CPUARMState *env = &cpu->env;
5403 uint64_t bvr = env->cp15.dbgbvr[n];
5404 uint64_t bcr = env->cp15.dbgbcr[n];
5405 vaddr addr;
5406 int bt;
5407 int flags = BP_CPU;
5409 if (env->cpu_breakpoint[n]) {
5410 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
5411 env->cpu_breakpoint[n] = NULL;
5414 if (!extract64(bcr, 0, 1)) {
5415 /* E bit clear : watchpoint disabled */
5416 return;
5419 bt = extract64(bcr, 20, 4);
5421 switch (bt) {
5422 case 4: /* unlinked address mismatch (reserved if AArch64) */
5423 case 5: /* linked address mismatch (reserved if AArch64) */
5424 qemu_log_mask(LOG_UNIMP,
5425 "arm: address mismatch breakpoint types not implemented\n");
5426 return;
5427 case 0: /* unlinked address match */
5428 case 1: /* linked address match */
5430 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
5431 * we behave as if the register was sign extended. Bits [1:0] are
5432 * RES0. The BAS field is used to allow setting breakpoints on 16
5433 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
5434 * a bp will fire if the addresses covered by the bp and the addresses
5435 * covered by the insn overlap but the insn doesn't start at the
5436 * start of the bp address range. We choose to require the insn and
5437 * the bp to have the same address. The constraints on writing to
5438 * BAS enforced in dbgbcr_write mean we have only four cases:
5439 * 0b0000 => no breakpoint
5440 * 0b0011 => breakpoint on addr
5441 * 0b1100 => breakpoint on addr + 2
5442 * 0b1111 => breakpoint on addr
5443 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
5445 int bas = extract64(bcr, 5, 4);
5446 addr = sextract64(bvr, 0, 49) & ~3ULL;
5447 if (bas == 0) {
5448 return;
5450 if (bas == 0xc) {
5451 addr += 2;
5453 break;
5455 case 2: /* unlinked context ID match */
5456 case 8: /* unlinked VMID match (reserved if no EL2) */
5457 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
5458 qemu_log_mask(LOG_UNIMP,
5459 "arm: unlinked context breakpoint types not implemented\n");
5460 return;
5461 case 9: /* linked VMID match (reserved if no EL2) */
5462 case 11: /* linked context ID and VMID match (reserved if no EL2) */
5463 case 3: /* linked context ID match */
5464 default:
5465 /* We must generate no events for Linked context matches (unless
5466 * they are linked to by some other bp/wp, which is handled in
5467 * updates for the linking bp/wp). We choose to also generate no events
5468 * for reserved values.
5470 return;
5473 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
5476 void hw_breakpoint_update_all(ARMCPU *cpu)
5478 int i;
5479 CPUARMState *env = &cpu->env;
5481 /* Completely clear out existing QEMU breakpoints and our array, to
5482 * avoid possible stale entries following migration load.
5484 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
5485 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
5487 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
5488 hw_breakpoint_update(cpu, i);
5492 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5493 uint64_t value)
5495 ARMCPU *cpu = arm_env_get_cpu(env);
5496 int i = ri->crm;
5498 raw_write(env, ri, value);
5499 hw_breakpoint_update(cpu, i);
5502 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5503 uint64_t value)
5505 ARMCPU *cpu = arm_env_get_cpu(env);
5506 int i = ri->crm;
5508 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
5509 * copy of BAS[0].
5511 value = deposit64(value, 6, 1, extract64(value, 5, 1));
5512 value = deposit64(value, 8, 1, extract64(value, 7, 1));
5514 raw_write(env, ri, value);
5515 hw_breakpoint_update(cpu, i);
5518 static void define_debug_regs(ARMCPU *cpu)
5520 /* Define v7 and v8 architectural debug registers.
5521 * These are just dummy implementations for now.
5523 int i;
5524 int wrps, brps, ctx_cmps;
5525 ARMCPRegInfo dbgdidr = {
5526 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
5527 .access = PL0_R, .accessfn = access_tda,
5528 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
5531 /* Note that all these register fields hold "number of Xs minus 1". */
5532 brps = extract32(cpu->dbgdidr, 24, 4);
5533 wrps = extract32(cpu->dbgdidr, 28, 4);
5534 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
5536 assert(ctx_cmps <= brps);
5538 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
5539 * of the debug registers such as number of breakpoints;
5540 * check that if they both exist then they agree.
5542 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
5543 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
5544 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
5545 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
5548 define_one_arm_cp_reg(cpu, &dbgdidr);
5549 define_arm_cp_regs(cpu, debug_cp_reginfo);
5551 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
5552 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
5555 for (i = 0; i < brps + 1; i++) {
5556 ARMCPRegInfo dbgregs[] = {
5557 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
5558 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
5559 .access = PL1_RW, .accessfn = access_tda,
5560 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
5561 .writefn = dbgbvr_write, .raw_writefn = raw_write
5563 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
5564 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
5565 .access = PL1_RW, .accessfn = access_tda,
5566 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
5567 .writefn = dbgbcr_write, .raw_writefn = raw_write
5569 REGINFO_SENTINEL
5571 define_arm_cp_regs(cpu, dbgregs);
5574 for (i = 0; i < wrps + 1; i++) {
5575 ARMCPRegInfo dbgregs[] = {
5576 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
5577 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
5578 .access = PL1_RW, .accessfn = access_tda,
5579 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
5580 .writefn = dbgwvr_write, .raw_writefn = raw_write
5582 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
5583 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
5584 .access = PL1_RW, .accessfn = access_tda,
5585 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
5586 .writefn = dbgwcr_write, .raw_writefn = raw_write
5588 REGINFO_SENTINEL
5590 define_arm_cp_regs(cpu, dbgregs);
5594 /* We don't know until after realize whether there's a GICv3
5595 * attached, and that is what registers the gicv3 sysregs.
5596 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
5597 * at runtime.
5599 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
5601 ARMCPU *cpu = arm_env_get_cpu(env);
5602 uint64_t pfr1 = cpu->id_pfr1;
5604 if (env->gicv3state) {
5605 pfr1 |= 1 << 28;
5607 return pfr1;
5610 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
5612 ARMCPU *cpu = arm_env_get_cpu(env);
5613 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
5615 if (env->gicv3state) {
5616 pfr0 |= 1 << 24;
5618 return pfr0;
5621 /* Shared logic between LORID and the rest of the LOR* registers.
5622 * Secure state has already been delt with.
5624 static CPAccessResult access_lor_ns(CPUARMState *env)
5626 int el = arm_current_el(env);
5628 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
5629 return CP_ACCESS_TRAP_EL2;
5631 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
5632 return CP_ACCESS_TRAP_EL3;
5634 return CP_ACCESS_OK;
5637 static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri,
5638 bool isread)
5640 if (arm_is_secure_below_el3(env)) {
5641 /* Access ok in secure mode. */
5642 return CP_ACCESS_OK;
5644 return access_lor_ns(env);
5647 static CPAccessResult access_lor_other(CPUARMState *env,
5648 const ARMCPRegInfo *ri, bool isread)
5650 if (arm_is_secure_below_el3(env)) {
5651 /* Access denied in secure mode. */
5652 return CP_ACCESS_TRAP;
5654 return access_lor_ns(env);
5657 #ifdef TARGET_AARCH64
5658 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
5659 bool isread)
5661 int el = arm_current_el(env);
5663 if (el < 2 &&
5664 arm_feature(env, ARM_FEATURE_EL2) &&
5665 !(arm_hcr_el2_eff(env) & HCR_APK)) {
5666 return CP_ACCESS_TRAP_EL2;
5668 if (el < 3 &&
5669 arm_feature(env, ARM_FEATURE_EL3) &&
5670 !(env->cp15.scr_el3 & SCR_APK)) {
5671 return CP_ACCESS_TRAP_EL3;
5673 return CP_ACCESS_OK;
5676 static const ARMCPRegInfo pauth_reginfo[] = {
5677 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5678 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
5679 .access = PL1_RW, .accessfn = access_pauth,
5680 .fieldoffset = offsetof(CPUARMState, apda_key.lo) },
5681 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5682 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
5683 .access = PL1_RW, .accessfn = access_pauth,
5684 .fieldoffset = offsetof(CPUARMState, apda_key.hi) },
5685 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5686 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
5687 .access = PL1_RW, .accessfn = access_pauth,
5688 .fieldoffset = offsetof(CPUARMState, apdb_key.lo) },
5689 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5690 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
5691 .access = PL1_RW, .accessfn = access_pauth,
5692 .fieldoffset = offsetof(CPUARMState, apdb_key.hi) },
5693 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5694 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
5695 .access = PL1_RW, .accessfn = access_pauth,
5696 .fieldoffset = offsetof(CPUARMState, apga_key.lo) },
5697 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5698 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
5699 .access = PL1_RW, .accessfn = access_pauth,
5700 .fieldoffset = offsetof(CPUARMState, apga_key.hi) },
5701 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5702 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
5703 .access = PL1_RW, .accessfn = access_pauth,
5704 .fieldoffset = offsetof(CPUARMState, apia_key.lo) },
5705 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5706 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
5707 .access = PL1_RW, .accessfn = access_pauth,
5708 .fieldoffset = offsetof(CPUARMState, apia_key.hi) },
5709 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5710 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
5711 .access = PL1_RW, .accessfn = access_pauth,
5712 .fieldoffset = offsetof(CPUARMState, apib_key.lo) },
5713 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5714 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
5715 .access = PL1_RW, .accessfn = access_pauth,
5716 .fieldoffset = offsetof(CPUARMState, apib_key.hi) },
5717 REGINFO_SENTINEL
5719 #endif
5721 void register_cp_regs_for_features(ARMCPU *cpu)
5723 /* Register all the coprocessor registers based on feature bits */
5724 CPUARMState *env = &cpu->env;
5725 if (arm_feature(env, ARM_FEATURE_M)) {
5726 /* M profile has no coprocessor registers */
5727 return;
5730 define_arm_cp_regs(cpu, cp_reginfo);
5731 if (!arm_feature(env, ARM_FEATURE_V8)) {
5732 /* Must go early as it is full of wildcards that may be
5733 * overridden by later definitions.
5735 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
5738 if (arm_feature(env, ARM_FEATURE_V6)) {
5739 /* The ID registers all have impdef reset values */
5740 ARMCPRegInfo v6_idregs[] = {
5741 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
5742 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
5743 .access = PL1_R, .type = ARM_CP_CONST,
5744 .resetvalue = cpu->id_pfr0 },
5745 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
5746 * the value of the GIC field until after we define these regs.
5748 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
5749 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
5750 .access = PL1_R, .type = ARM_CP_NO_RAW,
5751 .readfn = id_pfr1_read,
5752 .writefn = arm_cp_write_ignore },
5753 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
5754 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
5755 .access = PL1_R, .type = ARM_CP_CONST,
5756 .resetvalue = cpu->id_dfr0 },
5757 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
5758 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
5759 .access = PL1_R, .type = ARM_CP_CONST,
5760 .resetvalue = cpu->id_afr0 },
5761 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
5762 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
5763 .access = PL1_R, .type = ARM_CP_CONST,
5764 .resetvalue = cpu->id_mmfr0 },
5765 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
5766 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
5767 .access = PL1_R, .type = ARM_CP_CONST,
5768 .resetvalue = cpu->id_mmfr1 },
5769 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
5770 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
5771 .access = PL1_R, .type = ARM_CP_CONST,
5772 .resetvalue = cpu->id_mmfr2 },
5773 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
5774 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
5775 .access = PL1_R, .type = ARM_CP_CONST,
5776 .resetvalue = cpu->id_mmfr3 },
5777 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
5778 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5779 .access = PL1_R, .type = ARM_CP_CONST,
5780 .resetvalue = cpu->isar.id_isar0 },
5781 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
5782 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
5783 .access = PL1_R, .type = ARM_CP_CONST,
5784 .resetvalue = cpu->isar.id_isar1 },
5785 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
5786 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
5787 .access = PL1_R, .type = ARM_CP_CONST,
5788 .resetvalue = cpu->isar.id_isar2 },
5789 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
5790 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
5791 .access = PL1_R, .type = ARM_CP_CONST,
5792 .resetvalue = cpu->isar.id_isar3 },
5793 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
5794 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
5795 .access = PL1_R, .type = ARM_CP_CONST,
5796 .resetvalue = cpu->isar.id_isar4 },
5797 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
5798 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
5799 .access = PL1_R, .type = ARM_CP_CONST,
5800 .resetvalue = cpu->isar.id_isar5 },
5801 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
5802 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
5803 .access = PL1_R, .type = ARM_CP_CONST,
5804 .resetvalue = cpu->id_mmfr4 },
5805 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
5806 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
5807 .access = PL1_R, .type = ARM_CP_CONST,
5808 .resetvalue = cpu->isar.id_isar6 },
5809 REGINFO_SENTINEL
5811 define_arm_cp_regs(cpu, v6_idregs);
5812 define_arm_cp_regs(cpu, v6_cp_reginfo);
5813 } else {
5814 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
5816 if (arm_feature(env, ARM_FEATURE_V6K)) {
5817 define_arm_cp_regs(cpu, v6k_cp_reginfo);
5819 if (arm_feature(env, ARM_FEATURE_V7MP) &&
5820 !arm_feature(env, ARM_FEATURE_PMSA)) {
5821 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
5823 if (arm_feature(env, ARM_FEATURE_V7VE)) {
5824 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
5826 if (arm_feature(env, ARM_FEATURE_V7)) {
5827 /* v7 performance monitor control register: same implementor
5828 * field as main ID register, and we implement four counters in
5829 * addition to the cycle count register.
5831 unsigned int i, pmcrn = 4;
5832 ARMCPRegInfo pmcr = {
5833 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
5834 .access = PL0_RW,
5835 .type = ARM_CP_IO | ARM_CP_ALIAS,
5836 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
5837 .accessfn = pmreg_access, .writefn = pmcr_write,
5838 .raw_writefn = raw_write,
5840 ARMCPRegInfo pmcr64 = {
5841 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
5842 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
5843 .access = PL0_RW, .accessfn = pmreg_access,
5844 .type = ARM_CP_IO,
5845 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
5846 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT),
5847 .writefn = pmcr_write, .raw_writefn = raw_write,
5849 define_one_arm_cp_reg(cpu, &pmcr);
5850 define_one_arm_cp_reg(cpu, &pmcr64);
5851 for (i = 0; i < pmcrn; i++) {
5852 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
5853 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
5854 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
5855 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
5856 ARMCPRegInfo pmev_regs[] = {
5857 { .name = pmevcntr_name, .cp = 15, .crn = 14,
5858 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
5859 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
5860 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
5861 .accessfn = pmreg_access },
5862 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
5863 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
5864 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
5865 .type = ARM_CP_IO,
5866 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
5867 .raw_readfn = pmevcntr_rawread,
5868 .raw_writefn = pmevcntr_rawwrite },
5869 { .name = pmevtyper_name, .cp = 15, .crn = 14,
5870 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
5871 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
5872 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
5873 .accessfn = pmreg_access },
5874 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
5875 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
5876 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
5877 .type = ARM_CP_IO,
5878 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
5879 .raw_writefn = pmevtyper_rawwrite },
5880 REGINFO_SENTINEL
5882 define_arm_cp_regs(cpu, pmev_regs);
5883 g_free(pmevcntr_name);
5884 g_free(pmevcntr_el0_name);
5885 g_free(pmevtyper_name);
5886 g_free(pmevtyper_el0_name);
5888 ARMCPRegInfo clidr = {
5889 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
5890 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
5891 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
5893 define_one_arm_cp_reg(cpu, &clidr);
5894 define_arm_cp_regs(cpu, v7_cp_reginfo);
5895 define_debug_regs(cpu);
5896 } else {
5897 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
5899 if (FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) >= 4 &&
5900 FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) != 0xf) {
5901 ARMCPRegInfo v81_pmu_regs[] = {
5902 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
5903 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
5904 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5905 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
5906 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
5907 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
5908 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
5909 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
5910 REGINFO_SENTINEL
5912 define_arm_cp_regs(cpu, v81_pmu_regs);
5914 if (arm_feature(env, ARM_FEATURE_V8)) {
5915 /* AArch64 ID registers, which all have impdef reset values.
5916 * Note that within the ID register ranges the unused slots
5917 * must all RAZ, not UNDEF; future architecture versions may
5918 * define new registers here.
5920 ARMCPRegInfo v8_idregs[] = {
5921 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
5922 * know the right value for the GIC field until after we
5923 * define these regs.
5925 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
5926 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
5927 .access = PL1_R, .type = ARM_CP_NO_RAW,
5928 .readfn = id_aa64pfr0_read,
5929 .writefn = arm_cp_write_ignore },
5930 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
5931 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
5932 .access = PL1_R, .type = ARM_CP_CONST,
5933 .resetvalue = cpu->isar.id_aa64pfr1},
5934 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5935 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
5936 .access = PL1_R, .type = ARM_CP_CONST,
5937 .resetvalue = 0 },
5938 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5939 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
5940 .access = PL1_R, .type = ARM_CP_CONST,
5941 .resetvalue = 0 },
5942 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
5943 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
5944 .access = PL1_R, .type = ARM_CP_CONST,
5945 /* At present, only SVEver == 0 is defined anyway. */
5946 .resetvalue = 0 },
5947 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5948 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
5949 .access = PL1_R, .type = ARM_CP_CONST,
5950 .resetvalue = 0 },
5951 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5952 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
5953 .access = PL1_R, .type = ARM_CP_CONST,
5954 .resetvalue = 0 },
5955 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5956 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
5957 .access = PL1_R, .type = ARM_CP_CONST,
5958 .resetvalue = 0 },
5959 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
5960 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
5961 .access = PL1_R, .type = ARM_CP_CONST,
5962 .resetvalue = cpu->id_aa64dfr0 },
5963 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
5964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
5965 .access = PL1_R, .type = ARM_CP_CONST,
5966 .resetvalue = cpu->id_aa64dfr1 },
5967 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5968 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
5969 .access = PL1_R, .type = ARM_CP_CONST,
5970 .resetvalue = 0 },
5971 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5972 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
5973 .access = PL1_R, .type = ARM_CP_CONST,
5974 .resetvalue = 0 },
5975 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
5976 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
5977 .access = PL1_R, .type = ARM_CP_CONST,
5978 .resetvalue = cpu->id_aa64afr0 },
5979 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
5980 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
5981 .access = PL1_R, .type = ARM_CP_CONST,
5982 .resetvalue = cpu->id_aa64afr1 },
5983 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
5985 .access = PL1_R, .type = ARM_CP_CONST,
5986 .resetvalue = 0 },
5987 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
5988 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
5989 .access = PL1_R, .type = ARM_CP_CONST,
5990 .resetvalue = 0 },
5991 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
5992 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
5993 .access = PL1_R, .type = ARM_CP_CONST,
5994 .resetvalue = cpu->isar.id_aa64isar0 },
5995 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
5996 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
5997 .access = PL1_R, .type = ARM_CP_CONST,
5998 .resetvalue = cpu->isar.id_aa64isar1 },
5999 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6000 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
6001 .access = PL1_R, .type = ARM_CP_CONST,
6002 .resetvalue = 0 },
6003 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
6005 .access = PL1_R, .type = ARM_CP_CONST,
6006 .resetvalue = 0 },
6007 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6008 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
6009 .access = PL1_R, .type = ARM_CP_CONST,
6010 .resetvalue = 0 },
6011 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6012 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
6013 .access = PL1_R, .type = ARM_CP_CONST,
6014 .resetvalue = 0 },
6015 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6016 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
6017 .access = PL1_R, .type = ARM_CP_CONST,
6018 .resetvalue = 0 },
6019 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6020 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
6021 .access = PL1_R, .type = ARM_CP_CONST,
6022 .resetvalue = 0 },
6023 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
6024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
6025 .access = PL1_R, .type = ARM_CP_CONST,
6026 .resetvalue = cpu->isar.id_aa64mmfr0 },
6027 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
6028 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
6029 .access = PL1_R, .type = ARM_CP_CONST,
6030 .resetvalue = cpu->isar.id_aa64mmfr1 },
6031 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6032 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
6033 .access = PL1_R, .type = ARM_CP_CONST,
6034 .resetvalue = 0 },
6035 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6036 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
6037 .access = PL1_R, .type = ARM_CP_CONST,
6038 .resetvalue = 0 },
6039 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6040 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
6041 .access = PL1_R, .type = ARM_CP_CONST,
6042 .resetvalue = 0 },
6043 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6044 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
6045 .access = PL1_R, .type = ARM_CP_CONST,
6046 .resetvalue = 0 },
6047 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6048 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
6049 .access = PL1_R, .type = ARM_CP_CONST,
6050 .resetvalue = 0 },
6051 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6052 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
6053 .access = PL1_R, .type = ARM_CP_CONST,
6054 .resetvalue = 0 },
6055 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
6056 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
6057 .access = PL1_R, .type = ARM_CP_CONST,
6058 .resetvalue = cpu->isar.mvfr0 },
6059 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
6060 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
6061 .access = PL1_R, .type = ARM_CP_CONST,
6062 .resetvalue = cpu->isar.mvfr1 },
6063 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
6064 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
6065 .access = PL1_R, .type = ARM_CP_CONST,
6066 .resetvalue = cpu->isar.mvfr2 },
6067 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6068 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
6069 .access = PL1_R, .type = ARM_CP_CONST,
6070 .resetvalue = 0 },
6071 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6072 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
6073 .access = PL1_R, .type = ARM_CP_CONST,
6074 .resetvalue = 0 },
6075 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6076 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
6077 .access = PL1_R, .type = ARM_CP_CONST,
6078 .resetvalue = 0 },
6079 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6080 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
6081 .access = PL1_R, .type = ARM_CP_CONST,
6082 .resetvalue = 0 },
6083 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6084 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
6085 .access = PL1_R, .type = ARM_CP_CONST,
6086 .resetvalue = 0 },
6087 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
6088 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
6089 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6090 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
6091 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
6092 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
6093 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6094 .resetvalue = cpu->pmceid0 },
6095 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
6096 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
6097 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6098 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
6099 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
6100 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
6101 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6102 .resetvalue = cpu->pmceid1 },
6103 REGINFO_SENTINEL
6105 #ifdef CONFIG_USER_ONLY
6106 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6107 { .name = "ID_AA64PFR0_EL1",
6108 .exported_bits = 0x000f000f00ff0000,
6109 .fixed_bits = 0x0000000000000011 },
6110 { .name = "ID_AA64PFR1_EL1",
6111 .exported_bits = 0x00000000000000f0 },
6112 { .name = "ID_AA64ZFR0_EL1" },
6113 { .name = "ID_AA64MMFR0_EL1",
6114 .fixed_bits = 0x00000000ff000000 },
6115 { .name = "ID_AA64MMFR1_EL1" },
6116 { .name = "ID_AA64DFR0_EL1",
6117 .fixed_bits = 0x0000000000000006 },
6118 { .name = "ID_AA64DFR1_EL1" },
6119 { .name = "ID_AA64AFR0_EL1" },
6120 { .name = "ID_AA64AFR1_EL1" },
6121 { .name = "ID_AA64ISAR0_EL1",
6122 .exported_bits = 0x00fffffff0fffff0 },
6123 { .name = "ID_AA64ISAR1_EL1",
6124 .exported_bits = 0x000000f0ffffffff },
6125 REGUSERINFO_SENTINEL
6127 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
6128 #endif
6129 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
6130 if (!arm_feature(env, ARM_FEATURE_EL3) &&
6131 !arm_feature(env, ARM_FEATURE_EL2)) {
6132 ARMCPRegInfo rvbar = {
6133 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
6134 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6135 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
6137 define_one_arm_cp_reg(cpu, &rvbar);
6139 define_arm_cp_regs(cpu, v8_idregs);
6140 define_arm_cp_regs(cpu, v8_cp_reginfo);
6142 if (arm_feature(env, ARM_FEATURE_EL2)) {
6143 uint64_t vmpidr_def = mpidr_read_val(env);
6144 ARMCPRegInfo vpidr_regs[] = {
6145 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
6146 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6147 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6148 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
6149 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
6150 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
6151 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6152 .access = PL2_RW, .resetvalue = cpu->midr,
6153 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
6154 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
6155 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6156 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6157 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
6158 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
6159 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
6160 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6161 .access = PL2_RW,
6162 .resetvalue = vmpidr_def,
6163 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
6164 REGINFO_SENTINEL
6166 define_arm_cp_regs(cpu, vpidr_regs);
6167 define_arm_cp_regs(cpu, el2_cp_reginfo);
6168 if (arm_feature(env, ARM_FEATURE_V8)) {
6169 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
6171 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
6172 if (!arm_feature(env, ARM_FEATURE_EL3)) {
6173 ARMCPRegInfo rvbar = {
6174 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
6175 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
6176 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
6178 define_one_arm_cp_reg(cpu, &rvbar);
6180 } else {
6181 /* If EL2 is missing but higher ELs are enabled, we need to
6182 * register the no_el2 reginfos.
6184 if (arm_feature(env, ARM_FEATURE_EL3)) {
6185 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
6186 * of MIDR_EL1 and MPIDR_EL1.
6188 ARMCPRegInfo vpidr_regs[] = {
6189 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6190 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6191 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
6192 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
6193 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
6194 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6195 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6196 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
6197 .type = ARM_CP_NO_RAW,
6198 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
6199 REGINFO_SENTINEL
6201 define_arm_cp_regs(cpu, vpidr_regs);
6202 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
6203 if (arm_feature(env, ARM_FEATURE_V8)) {
6204 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
6208 if (arm_feature(env, ARM_FEATURE_EL3)) {
6209 define_arm_cp_regs(cpu, el3_cp_reginfo);
6210 ARMCPRegInfo el3_regs[] = {
6211 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
6212 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
6213 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
6214 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
6215 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
6216 .access = PL3_RW,
6217 .raw_writefn = raw_write, .writefn = sctlr_write,
6218 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
6219 .resetvalue = cpu->reset_sctlr },
6220 REGINFO_SENTINEL
6223 define_arm_cp_regs(cpu, el3_regs);
6225 /* The behaviour of NSACR is sufficiently various that we don't
6226 * try to describe it in a single reginfo:
6227 * if EL3 is 64 bit, then trap to EL3 from S EL1,
6228 * reads as constant 0xc00 from NS EL1 and NS EL2
6229 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
6230 * if v7 without EL3, register doesn't exist
6231 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
6233 if (arm_feature(env, ARM_FEATURE_EL3)) {
6234 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6235 ARMCPRegInfo nsacr = {
6236 .name = "NSACR", .type = ARM_CP_CONST,
6237 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6238 .access = PL1_RW, .accessfn = nsacr_access,
6239 .resetvalue = 0xc00
6241 define_one_arm_cp_reg(cpu, &nsacr);
6242 } else {
6243 ARMCPRegInfo nsacr = {
6244 .name = "NSACR",
6245 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6246 .access = PL3_RW | PL1_R,
6247 .resetvalue = 0,
6248 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
6250 define_one_arm_cp_reg(cpu, &nsacr);
6252 } else {
6253 if (arm_feature(env, ARM_FEATURE_V8)) {
6254 ARMCPRegInfo nsacr = {
6255 .name = "NSACR", .type = ARM_CP_CONST,
6256 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6257 .access = PL1_R,
6258 .resetvalue = 0xc00
6260 define_one_arm_cp_reg(cpu, &nsacr);
6264 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6265 if (arm_feature(env, ARM_FEATURE_V6)) {
6266 /* PMSAv6 not implemented */
6267 assert(arm_feature(env, ARM_FEATURE_V7));
6268 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
6269 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
6270 } else {
6271 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
6273 } else {
6274 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
6275 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
6276 /* TTCBR2 is introduced with ARMv8.2-A32HPD. */
6277 if (FIELD_EX32(cpu->id_mmfr4, ID_MMFR4, HPDS) != 0) {
6278 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
6281 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
6282 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
6284 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
6285 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
6287 if (arm_feature(env, ARM_FEATURE_VAPA)) {
6288 define_arm_cp_regs(cpu, vapa_cp_reginfo);
6290 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
6291 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
6293 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
6294 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
6296 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
6297 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
6299 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
6300 define_arm_cp_regs(cpu, omap_cp_reginfo);
6302 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
6303 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
6305 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
6306 define_arm_cp_regs(cpu, xscale_cp_reginfo);
6308 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
6309 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
6311 if (arm_feature(env, ARM_FEATURE_LPAE)) {
6312 define_arm_cp_regs(cpu, lpae_cp_reginfo);
6314 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
6315 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
6316 * be read-only (ie write causes UNDEF exception).
6319 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
6320 /* Pre-v8 MIDR space.
6321 * Note that the MIDR isn't a simple constant register because
6322 * of the TI925 behaviour where writes to another register can
6323 * cause the MIDR value to change.
6325 * Unimplemented registers in the c15 0 0 0 space default to
6326 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
6327 * and friends override accordingly.
6329 { .name = "MIDR",
6330 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
6331 .access = PL1_R, .resetvalue = cpu->midr,
6332 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
6333 .readfn = midr_read,
6334 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
6335 .type = ARM_CP_OVERRIDE },
6336 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
6337 { .name = "DUMMY",
6338 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
6339 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6340 { .name = "DUMMY",
6341 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
6342 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6343 { .name = "DUMMY",
6344 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
6345 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6346 { .name = "DUMMY",
6347 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
6348 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6349 { .name = "DUMMY",
6350 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
6351 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6352 REGINFO_SENTINEL
6354 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
6355 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
6356 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
6357 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
6358 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
6359 .readfn = midr_read },
6360 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
6361 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
6362 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
6363 .access = PL1_R, .resetvalue = cpu->midr },
6364 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
6365 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
6366 .access = PL1_R, .resetvalue = cpu->midr },
6367 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
6368 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
6369 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
6370 REGINFO_SENTINEL
6372 ARMCPRegInfo id_cp_reginfo[] = {
6373 /* These are common to v8 and pre-v8 */
6374 { .name = "CTR",
6375 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
6376 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
6377 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
6378 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
6379 .access = PL0_R, .accessfn = ctr_el0_access,
6380 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
6381 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
6382 { .name = "TCMTR",
6383 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
6384 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6385 REGINFO_SENTINEL
6387 /* TLBTR is specific to VMSA */
6388 ARMCPRegInfo id_tlbtr_reginfo = {
6389 .name = "TLBTR",
6390 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
6391 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
6393 /* MPUIR is specific to PMSA V6+ */
6394 ARMCPRegInfo id_mpuir_reginfo = {
6395 .name = "MPUIR",
6396 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
6397 .access = PL1_R, .type = ARM_CP_CONST,
6398 .resetvalue = cpu->pmsav7_dregion << 8
6400 ARMCPRegInfo crn0_wi_reginfo = {
6401 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
6402 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
6403 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
6405 #ifdef CONFIG_USER_ONLY
6406 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6407 { .name = "MIDR_EL1",
6408 .exported_bits = 0x00000000ffffffff },
6409 { .name = "REVIDR_EL1" },
6410 REGUSERINFO_SENTINEL
6412 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
6413 #endif
6414 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
6415 arm_feature(env, ARM_FEATURE_STRONGARM)) {
6416 ARMCPRegInfo *r;
6417 /* Register the blanket "writes ignored" value first to cover the
6418 * whole space. Then update the specific ID registers to allow write
6419 * access, so that they ignore writes rather than causing them to
6420 * UNDEF.
6422 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
6423 for (r = id_pre_v8_midr_cp_reginfo;
6424 r->type != ARM_CP_SENTINEL; r++) {
6425 r->access = PL1_RW;
6427 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
6428 r->access = PL1_RW;
6430 id_mpuir_reginfo.access = PL1_RW;
6431 id_tlbtr_reginfo.access = PL1_RW;
6433 if (arm_feature(env, ARM_FEATURE_V8)) {
6434 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
6435 } else {
6436 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
6438 define_arm_cp_regs(cpu, id_cp_reginfo);
6439 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
6440 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
6441 } else if (arm_feature(env, ARM_FEATURE_V7)) {
6442 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
6446 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
6447 ARMCPRegInfo mpidr_cp_reginfo[] = {
6448 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
6449 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
6450 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
6451 REGINFO_SENTINEL
6453 #ifdef CONFIG_USER_ONLY
6454 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
6455 { .name = "MPIDR_EL1",
6456 .fixed_bits = 0x0000000080000000 },
6457 REGUSERINFO_SENTINEL
6459 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
6460 #endif
6461 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
6464 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
6465 ARMCPRegInfo auxcr_reginfo[] = {
6466 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
6467 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
6468 .access = PL1_RW, .type = ARM_CP_CONST,
6469 .resetvalue = cpu->reset_auxcr },
6470 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
6471 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
6472 .access = PL2_RW, .type = ARM_CP_CONST,
6473 .resetvalue = 0 },
6474 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
6475 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
6476 .access = PL3_RW, .type = ARM_CP_CONST,
6477 .resetvalue = 0 },
6478 REGINFO_SENTINEL
6480 define_arm_cp_regs(cpu, auxcr_reginfo);
6481 if (arm_feature(env, ARM_FEATURE_V8)) {
6482 /* HACTLR2 maps to ACTLR_EL2[63:32] and is not in ARMv7 */
6483 ARMCPRegInfo hactlr2_reginfo = {
6484 .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
6485 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
6486 .access = PL2_RW, .type = ARM_CP_CONST,
6487 .resetvalue = 0
6489 define_one_arm_cp_reg(cpu, &hactlr2_reginfo);
6493 if (arm_feature(env, ARM_FEATURE_CBAR)) {
6494 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6495 /* 32 bit view is [31:18] 0...0 [43:32]. */
6496 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
6497 | extract64(cpu->reset_cbar, 32, 12);
6498 ARMCPRegInfo cbar_reginfo[] = {
6499 { .name = "CBAR",
6500 .type = ARM_CP_CONST,
6501 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
6502 .access = PL1_R, .resetvalue = cpu->reset_cbar },
6503 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
6504 .type = ARM_CP_CONST,
6505 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
6506 .access = PL1_R, .resetvalue = cbar32 },
6507 REGINFO_SENTINEL
6509 /* We don't implement a r/w 64 bit CBAR currently */
6510 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
6511 define_arm_cp_regs(cpu, cbar_reginfo);
6512 } else {
6513 ARMCPRegInfo cbar = {
6514 .name = "CBAR",
6515 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
6516 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
6517 .fieldoffset = offsetof(CPUARMState,
6518 cp15.c15_config_base_address)
6520 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
6521 cbar.access = PL1_R;
6522 cbar.fieldoffset = 0;
6523 cbar.type = ARM_CP_CONST;
6525 define_one_arm_cp_reg(cpu, &cbar);
6529 if (arm_feature(env, ARM_FEATURE_VBAR)) {
6530 ARMCPRegInfo vbar_cp_reginfo[] = {
6531 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
6532 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
6533 .access = PL1_RW, .writefn = vbar_write,
6534 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
6535 offsetof(CPUARMState, cp15.vbar_ns) },
6536 .resetvalue = 0 },
6537 REGINFO_SENTINEL
6539 define_arm_cp_regs(cpu, vbar_cp_reginfo);
6542 /* Generic registers whose values depend on the implementation */
6544 ARMCPRegInfo sctlr = {
6545 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
6546 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
6547 .access = PL1_RW,
6548 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
6549 offsetof(CPUARMState, cp15.sctlr_ns) },
6550 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
6551 .raw_writefn = raw_write,
6553 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
6554 /* Normally we would always end the TB on an SCTLR write, but Linux
6555 * arch/arm/mach-pxa/sleep.S expects two instructions following
6556 * an MMU enable to execute from cache. Imitate this behaviour.
6558 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
6560 define_one_arm_cp_reg(cpu, &sctlr);
6563 if (cpu_isar_feature(aa64_lor, cpu)) {
6565 * A trivial implementation of ARMv8.1-LOR leaves all of these
6566 * registers fixed at 0, which indicates that there are zero
6567 * supported Limited Ordering regions.
6569 static const ARMCPRegInfo lor_reginfo[] = {
6570 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6571 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6572 .access = PL1_RW, .accessfn = access_lor_other,
6573 .type = ARM_CP_CONST, .resetvalue = 0 },
6574 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6575 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6576 .access = PL1_RW, .accessfn = access_lor_other,
6577 .type = ARM_CP_CONST, .resetvalue = 0 },
6578 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6579 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6580 .access = PL1_RW, .accessfn = access_lor_other,
6581 .type = ARM_CP_CONST, .resetvalue = 0 },
6582 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6583 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6584 .access = PL1_RW, .accessfn = access_lor_other,
6585 .type = ARM_CP_CONST, .resetvalue = 0 },
6586 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6587 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6588 .access = PL1_R, .accessfn = access_lorid,
6589 .type = ARM_CP_CONST, .resetvalue = 0 },
6590 REGINFO_SENTINEL
6592 define_arm_cp_regs(cpu, lor_reginfo);
6595 if (cpu_isar_feature(aa64_sve, cpu)) {
6596 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
6597 if (arm_feature(env, ARM_FEATURE_EL2)) {
6598 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
6599 } else {
6600 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
6602 if (arm_feature(env, ARM_FEATURE_EL3)) {
6603 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
6607 #ifdef TARGET_AARCH64
6608 if (cpu_isar_feature(aa64_pauth, cpu)) {
6609 define_arm_cp_regs(cpu, pauth_reginfo);
6611 #endif
6614 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
6616 CPUState *cs = CPU(cpu);
6617 CPUARMState *env = &cpu->env;
6619 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6620 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
6621 aarch64_fpu_gdb_set_reg,
6622 34, "aarch64-fpu.xml", 0);
6623 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
6624 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
6625 51, "arm-neon.xml", 0);
6626 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
6627 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
6628 35, "arm-vfp3.xml", 0);
6629 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
6630 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
6631 19, "arm-vfp.xml", 0);
6633 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
6634 arm_gen_dynamic_xml(cs),
6635 "system-registers.xml", 0);
6638 /* Sort alphabetically by type name, except for "any". */
6639 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
6641 ObjectClass *class_a = (ObjectClass *)a;
6642 ObjectClass *class_b = (ObjectClass *)b;
6643 const char *name_a, *name_b;
6645 name_a = object_class_get_name(class_a);
6646 name_b = object_class_get_name(class_b);
6647 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
6648 return 1;
6649 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
6650 return -1;
6651 } else {
6652 return strcmp(name_a, name_b);
6656 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
6658 ObjectClass *oc = data;
6659 CPUListState *s = user_data;
6660 const char *typename;
6661 char *name;
6663 typename = object_class_get_name(oc);
6664 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
6665 (*s->cpu_fprintf)(s->file, " %s\n",
6666 name);
6667 g_free(name);
6670 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
6672 CPUListState s = {
6673 .file = f,
6674 .cpu_fprintf = cpu_fprintf,
6676 GSList *list;
6678 list = object_class_get_list(TYPE_ARM_CPU, false);
6679 list = g_slist_sort(list, arm_cpu_list_compare);
6680 (*cpu_fprintf)(f, "Available CPUs:\n");
6681 g_slist_foreach(list, arm_cpu_list_entry, &s);
6682 g_slist_free(list);
6685 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
6687 ObjectClass *oc = data;
6688 CpuDefinitionInfoList **cpu_list = user_data;
6689 CpuDefinitionInfoList *entry;
6690 CpuDefinitionInfo *info;
6691 const char *typename;
6693 typename = object_class_get_name(oc);
6694 info = g_malloc0(sizeof(*info));
6695 info->name = g_strndup(typename,
6696 strlen(typename) - strlen("-" TYPE_ARM_CPU));
6697 info->q_typename = g_strdup(typename);
6699 entry = g_malloc0(sizeof(*entry));
6700 entry->value = info;
6701 entry->next = *cpu_list;
6702 *cpu_list = entry;
6705 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
6707 CpuDefinitionInfoList *cpu_list = NULL;
6708 GSList *list;
6710 list = object_class_get_list(TYPE_ARM_CPU, false);
6711 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
6712 g_slist_free(list);
6714 return cpu_list;
6717 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
6718 void *opaque, int state, int secstate,
6719 int crm, int opc1, int opc2,
6720 const char *name)
6722 /* Private utility function for define_one_arm_cp_reg_with_opaque():
6723 * add a single reginfo struct to the hash table.
6725 uint32_t *key = g_new(uint32_t, 1);
6726 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
6727 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
6728 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
6730 r2->name = g_strdup(name);
6731 /* Reset the secure state to the specific incoming state. This is
6732 * necessary as the register may have been defined with both states.
6734 r2->secure = secstate;
6736 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
6737 /* Register is banked (using both entries in array).
6738 * Overwriting fieldoffset as the array is only used to define
6739 * banked registers but later only fieldoffset is used.
6741 r2->fieldoffset = r->bank_fieldoffsets[ns];
6744 if (state == ARM_CP_STATE_AA32) {
6745 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
6746 /* If the register is banked then we don't need to migrate or
6747 * reset the 32-bit instance in certain cases:
6749 * 1) If the register has both 32-bit and 64-bit instances then we
6750 * can count on the 64-bit instance taking care of the
6751 * non-secure bank.
6752 * 2) If ARMv8 is enabled then we can count on a 64-bit version
6753 * taking care of the secure bank. This requires that separate
6754 * 32 and 64-bit definitions are provided.
6756 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
6757 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
6758 r2->type |= ARM_CP_ALIAS;
6760 } else if ((secstate != r->secure) && !ns) {
6761 /* The register is not banked so we only want to allow migration of
6762 * the non-secure instance.
6764 r2->type |= ARM_CP_ALIAS;
6767 if (r->state == ARM_CP_STATE_BOTH) {
6768 /* We assume it is a cp15 register if the .cp field is left unset.
6770 if (r2->cp == 0) {
6771 r2->cp = 15;
6774 #ifdef HOST_WORDS_BIGENDIAN
6775 if (r2->fieldoffset) {
6776 r2->fieldoffset += sizeof(uint32_t);
6778 #endif
6781 if (state == ARM_CP_STATE_AA64) {
6782 /* To allow abbreviation of ARMCPRegInfo
6783 * definitions, we treat cp == 0 as equivalent to
6784 * the value for "standard guest-visible sysreg".
6785 * STATE_BOTH definitions are also always "standard
6786 * sysreg" in their AArch64 view (the .cp value may
6787 * be non-zero for the benefit of the AArch32 view).
6789 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
6790 r2->cp = CP_REG_ARM64_SYSREG_CP;
6792 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
6793 r2->opc0, opc1, opc2);
6794 } else {
6795 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
6797 if (opaque) {
6798 r2->opaque = opaque;
6800 /* reginfo passed to helpers is correct for the actual access,
6801 * and is never ARM_CP_STATE_BOTH:
6803 r2->state = state;
6804 /* Make sure reginfo passed to helpers for wildcarded regs
6805 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
6807 r2->crm = crm;
6808 r2->opc1 = opc1;
6809 r2->opc2 = opc2;
6810 /* By convention, for wildcarded registers only the first
6811 * entry is used for migration; the others are marked as
6812 * ALIAS so we don't try to transfer the register
6813 * multiple times. Special registers (ie NOP/WFI) are
6814 * never migratable and not even raw-accessible.
6816 if ((r->type & ARM_CP_SPECIAL)) {
6817 r2->type |= ARM_CP_NO_RAW;
6819 if (((r->crm == CP_ANY) && crm != 0) ||
6820 ((r->opc1 == CP_ANY) && opc1 != 0) ||
6821 ((r->opc2 == CP_ANY) && opc2 != 0)) {
6822 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6825 /* Check that raw accesses are either forbidden or handled. Note that
6826 * we can't assert this earlier because the setup of fieldoffset for
6827 * banked registers has to be done first.
6829 if (!(r2->type & ARM_CP_NO_RAW)) {
6830 assert(!raw_accessors_invalid(r2));
6833 /* Overriding of an existing definition must be explicitly
6834 * requested.
6836 if (!(r->type & ARM_CP_OVERRIDE)) {
6837 ARMCPRegInfo *oldreg;
6838 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
6839 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
6840 fprintf(stderr, "Register redefined: cp=%d %d bit "
6841 "crn=%d crm=%d opc1=%d opc2=%d, "
6842 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
6843 r2->crn, r2->crm, r2->opc1, r2->opc2,
6844 oldreg->name, r2->name);
6845 g_assert_not_reached();
6848 g_hash_table_insert(cpu->cp_regs, key, r2);
6852 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
6853 const ARMCPRegInfo *r, void *opaque)
6855 /* Define implementations of coprocessor registers.
6856 * We store these in a hashtable because typically
6857 * there are less than 150 registers in a space which
6858 * is 16*16*16*8*8 = 262144 in size.
6859 * Wildcarding is supported for the crm, opc1 and opc2 fields.
6860 * If a register is defined twice then the second definition is
6861 * used, so this can be used to define some generic registers and
6862 * then override them with implementation specific variations.
6863 * At least one of the original and the second definition should
6864 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
6865 * against accidental use.
6867 * The state field defines whether the register is to be
6868 * visible in the AArch32 or AArch64 execution state. If the
6869 * state is set to ARM_CP_STATE_BOTH then we synthesise a
6870 * reginfo structure for the AArch32 view, which sees the lower
6871 * 32 bits of the 64 bit register.
6873 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
6874 * be wildcarded. AArch64 registers are always considered to be 64
6875 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
6876 * the register, if any.
6878 int crm, opc1, opc2, state;
6879 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
6880 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
6881 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
6882 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
6883 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
6884 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
6885 /* 64 bit registers have only CRm and Opc1 fields */
6886 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
6887 /* op0 only exists in the AArch64 encodings */
6888 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
6889 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
6890 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
6891 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
6892 * encodes a minimum access level for the register. We roll this
6893 * runtime check into our general permission check code, so check
6894 * here that the reginfo's specified permissions are strict enough
6895 * to encompass the generic architectural permission check.
6897 if (r->state != ARM_CP_STATE_AA32) {
6898 int mask = 0;
6899 switch (r->opc1) {
6900 case 0:
6901 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
6902 mask = PL0U_R | PL1_RW;
6903 break;
6904 case 1: case 2:
6905 /* min_EL EL1 */
6906 mask = PL1_RW;
6907 break;
6908 case 3:
6909 /* min_EL EL0 */
6910 mask = PL0_RW;
6911 break;
6912 case 4:
6913 /* min_EL EL2 */
6914 mask = PL2_RW;
6915 break;
6916 case 5:
6917 /* unallocated encoding, so not possible */
6918 assert(false);
6919 break;
6920 case 6:
6921 /* min_EL EL3 */
6922 mask = PL3_RW;
6923 break;
6924 case 7:
6925 /* min_EL EL1, secure mode only (we don't check the latter) */
6926 mask = PL1_RW;
6927 break;
6928 default:
6929 /* broken reginfo with out-of-range opc1 */
6930 assert(false);
6931 break;
6933 /* assert our permissions are not too lax (stricter is fine) */
6934 assert((r->access & ~mask) == 0);
6937 /* Check that the register definition has enough info to handle
6938 * reads and writes if they are permitted.
6940 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
6941 if (r->access & PL3_R) {
6942 assert((r->fieldoffset ||
6943 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
6944 r->readfn);
6946 if (r->access & PL3_W) {
6947 assert((r->fieldoffset ||
6948 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
6949 r->writefn);
6952 /* Bad type field probably means missing sentinel at end of reg list */
6953 assert(cptype_valid(r->type));
6954 for (crm = crmmin; crm <= crmmax; crm++) {
6955 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
6956 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
6957 for (state = ARM_CP_STATE_AA32;
6958 state <= ARM_CP_STATE_AA64; state++) {
6959 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
6960 continue;
6962 if (state == ARM_CP_STATE_AA32) {
6963 /* Under AArch32 CP registers can be common
6964 * (same for secure and non-secure world) or banked.
6966 char *name;
6968 switch (r->secure) {
6969 case ARM_CP_SECSTATE_S:
6970 case ARM_CP_SECSTATE_NS:
6971 add_cpreg_to_hashtable(cpu, r, opaque, state,
6972 r->secure, crm, opc1, opc2,
6973 r->name);
6974 break;
6975 default:
6976 name = g_strdup_printf("%s_S", r->name);
6977 add_cpreg_to_hashtable(cpu, r, opaque, state,
6978 ARM_CP_SECSTATE_S,
6979 crm, opc1, opc2, name);
6980 g_free(name);
6981 add_cpreg_to_hashtable(cpu, r, opaque, state,
6982 ARM_CP_SECSTATE_NS,
6983 crm, opc1, opc2, r->name);
6984 break;
6986 } else {
6987 /* AArch64 registers get mapped to non-secure instance
6988 * of AArch32 */
6989 add_cpreg_to_hashtable(cpu, r, opaque, state,
6990 ARM_CP_SECSTATE_NS,
6991 crm, opc1, opc2, r->name);
6999 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
7000 const ARMCPRegInfo *regs, void *opaque)
7002 /* Define a whole list of registers */
7003 const ARMCPRegInfo *r;
7004 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
7005 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
7010 * Modify ARMCPRegInfo for access from userspace.
7012 * This is a data driven modification directed by
7013 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
7014 * user-space cannot alter any values and dynamic values pertaining to
7015 * execution state are hidden from user space view anyway.
7017 void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
7019 const ARMCPRegUserSpaceInfo *m;
7020 ARMCPRegInfo *r;
7022 for (m = mods; m->name; m++) {
7023 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
7024 if (strcmp(r->name, m->name) == 0) {
7025 r->type = ARM_CP_CONST;
7026 r->access = PL0U_R;
7027 r->resetvalue &= m->exported_bits;
7028 r->resetvalue |= m->fixed_bits;
7029 break;
7035 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
7037 return g_hash_table_lookup(cpregs, &encoded_cp);
7040 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
7041 uint64_t value)
7043 /* Helper coprocessor write function for write-ignore registers */
7046 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
7048 /* Helper coprocessor write function for read-as-zero registers */
7049 return 0;
7052 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
7054 /* Helper coprocessor reset function for do-nothing-on-reset registers */
7057 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
7059 /* Return true if it is not valid for us to switch to
7060 * this CPU mode (ie all the UNPREDICTABLE cases in
7061 * the ARM ARM CPSRWriteByInstr pseudocode).
7064 /* Changes to or from Hyp via MSR and CPS are illegal. */
7065 if (write_type == CPSRWriteByInstr &&
7066 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
7067 mode == ARM_CPU_MODE_HYP)) {
7068 return 1;
7071 switch (mode) {
7072 case ARM_CPU_MODE_USR:
7073 return 0;
7074 case ARM_CPU_MODE_SYS:
7075 case ARM_CPU_MODE_SVC:
7076 case ARM_CPU_MODE_ABT:
7077 case ARM_CPU_MODE_UND:
7078 case ARM_CPU_MODE_IRQ:
7079 case ARM_CPU_MODE_FIQ:
7080 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
7081 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
7083 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
7084 * and CPS are treated as illegal mode changes.
7086 if (write_type == CPSRWriteByInstr &&
7087 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7088 (arm_hcr_el2_eff(env) & HCR_TGE)) {
7089 return 1;
7091 return 0;
7092 case ARM_CPU_MODE_HYP:
7093 return !arm_feature(env, ARM_FEATURE_EL2)
7094 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env);
7095 case ARM_CPU_MODE_MON:
7096 return arm_current_el(env) < 3;
7097 default:
7098 return 1;
7102 uint32_t cpsr_read(CPUARMState *env)
7104 int ZF;
7105 ZF = (env->ZF == 0);
7106 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
7107 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
7108 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
7109 | ((env->condexec_bits & 0xfc) << 8)
7110 | (env->GE << 16) | (env->daif & CPSR_AIF);
7113 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
7114 CPSRWriteType write_type)
7116 uint32_t changed_daif;
7118 if (mask & CPSR_NZCV) {
7119 env->ZF = (~val) & CPSR_Z;
7120 env->NF = val;
7121 env->CF = (val >> 29) & 1;
7122 env->VF = (val << 3) & 0x80000000;
7124 if (mask & CPSR_Q)
7125 env->QF = ((val & CPSR_Q) != 0);
7126 if (mask & CPSR_T)
7127 env->thumb = ((val & CPSR_T) != 0);
7128 if (mask & CPSR_IT_0_1) {
7129 env->condexec_bits &= ~3;
7130 env->condexec_bits |= (val >> 25) & 3;
7132 if (mask & CPSR_IT_2_7) {
7133 env->condexec_bits &= 3;
7134 env->condexec_bits |= (val >> 8) & 0xfc;
7136 if (mask & CPSR_GE) {
7137 env->GE = (val >> 16) & 0xf;
7140 /* In a V7 implementation that includes the security extensions but does
7141 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
7142 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
7143 * bits respectively.
7145 * In a V8 implementation, it is permitted for privileged software to
7146 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
7148 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
7149 arm_feature(env, ARM_FEATURE_EL3) &&
7150 !arm_feature(env, ARM_FEATURE_EL2) &&
7151 !arm_is_secure(env)) {
7153 changed_daif = (env->daif ^ val) & mask;
7155 if (changed_daif & CPSR_A) {
7156 /* Check to see if we are allowed to change the masking of async
7157 * abort exceptions from a non-secure state.
7159 if (!(env->cp15.scr_el3 & SCR_AW)) {
7160 qemu_log_mask(LOG_GUEST_ERROR,
7161 "Ignoring attempt to switch CPSR_A flag from "
7162 "non-secure world with SCR.AW bit clear\n");
7163 mask &= ~CPSR_A;
7167 if (changed_daif & CPSR_F) {
7168 /* Check to see if we are allowed to change the masking of FIQ
7169 * exceptions from a non-secure state.
7171 if (!(env->cp15.scr_el3 & SCR_FW)) {
7172 qemu_log_mask(LOG_GUEST_ERROR,
7173 "Ignoring attempt to switch CPSR_F flag from "
7174 "non-secure world with SCR.FW bit clear\n");
7175 mask &= ~CPSR_F;
7178 /* Check whether non-maskable FIQ (NMFI) support is enabled.
7179 * If this bit is set software is not allowed to mask
7180 * FIQs, but is allowed to set CPSR_F to 0.
7182 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
7183 (val & CPSR_F)) {
7184 qemu_log_mask(LOG_GUEST_ERROR,
7185 "Ignoring attempt to enable CPSR_F flag "
7186 "(non-maskable FIQ [NMFI] support enabled)\n");
7187 mask &= ~CPSR_F;
7192 env->daif &= ~(CPSR_AIF & mask);
7193 env->daif |= val & CPSR_AIF & mask;
7195 if (write_type != CPSRWriteRaw &&
7196 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
7197 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
7198 /* Note that we can only get here in USR mode if this is a
7199 * gdb stub write; for this case we follow the architectural
7200 * behaviour for guest writes in USR mode of ignoring an attempt
7201 * to switch mode. (Those are caught by translate.c for writes
7202 * triggered by guest instructions.)
7204 mask &= ~CPSR_M;
7205 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
7206 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
7207 * v7, and has defined behaviour in v8:
7208 * + leave CPSR.M untouched
7209 * + allow changes to the other CPSR fields
7210 * + set PSTATE.IL
7211 * For user changes via the GDB stub, we don't set PSTATE.IL,
7212 * as this would be unnecessarily harsh for a user error.
7214 mask &= ~CPSR_M;
7215 if (write_type != CPSRWriteByGDBStub &&
7216 arm_feature(env, ARM_FEATURE_V8)) {
7217 mask |= CPSR_IL;
7218 val |= CPSR_IL;
7220 qemu_log_mask(LOG_GUEST_ERROR,
7221 "Illegal AArch32 mode switch attempt from %s to %s\n",
7222 aarch32_mode_name(env->uncached_cpsr),
7223 aarch32_mode_name(val));
7224 } else {
7225 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
7226 write_type == CPSRWriteExceptionReturn ?
7227 "Exception return from AArch32" :
7228 "AArch32 mode switch from",
7229 aarch32_mode_name(env->uncached_cpsr),
7230 aarch32_mode_name(val), env->regs[15]);
7231 switch_mode(env, val & CPSR_M);
7234 mask &= ~CACHED_CPSR_BITS;
7235 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
7238 /* Sign/zero extend */
7239 uint32_t HELPER(sxtb16)(uint32_t x)
7241 uint32_t res;
7242 res = (uint16_t)(int8_t)x;
7243 res |= (uint32_t)(int8_t)(x >> 16) << 16;
7244 return res;
7247 uint32_t HELPER(uxtb16)(uint32_t x)
7249 uint32_t res;
7250 res = (uint16_t)(uint8_t)x;
7251 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
7252 return res;
7255 int32_t HELPER(sdiv)(int32_t num, int32_t den)
7257 if (den == 0)
7258 return 0;
7259 if (num == INT_MIN && den == -1)
7260 return INT_MIN;
7261 return num / den;
7264 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
7266 if (den == 0)
7267 return 0;
7268 return num / den;
7271 uint32_t HELPER(rbit)(uint32_t x)
7273 return revbit32(x);
7276 #ifdef CONFIG_USER_ONLY
7278 /* These should probably raise undefined insn exceptions. */
7279 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
7281 ARMCPU *cpu = arm_env_get_cpu(env);
7283 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
7286 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
7288 ARMCPU *cpu = arm_env_get_cpu(env);
7290 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
7291 return 0;
7294 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
7296 /* translate.c should never generate calls here in user-only mode */
7297 g_assert_not_reached();
7300 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
7302 /* translate.c should never generate calls here in user-only mode */
7303 g_assert_not_reached();
7306 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
7308 /* The TT instructions can be used by unprivileged code, but in
7309 * user-only emulation we don't have the MPU.
7310 * Luckily since we know we are NonSecure unprivileged (and that in
7311 * turn means that the A flag wasn't specified), all the bits in the
7312 * register must be zero:
7313 * IREGION: 0 because IRVALID is 0
7314 * IRVALID: 0 because NS
7315 * S: 0 because NS
7316 * NSRW: 0 because NS
7317 * NSR: 0 because NS
7318 * RW: 0 because unpriv and A flag not set
7319 * R: 0 because unpriv and A flag not set
7320 * SRVALID: 0 because NS
7321 * MRVALID: 0 because unpriv and A flag not set
7322 * SREGION: 0 becaus SRVALID is 0
7323 * MREGION: 0 because MRVALID is 0
7325 return 0;
7328 static void switch_mode(CPUARMState *env, int mode)
7330 ARMCPU *cpu = arm_env_get_cpu(env);
7332 if (mode != ARM_CPU_MODE_USR) {
7333 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
7337 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
7338 uint32_t cur_el, bool secure)
7340 return 1;
7343 void aarch64_sync_64_to_32(CPUARMState *env)
7345 g_assert_not_reached();
7348 #else
7350 static void switch_mode(CPUARMState *env, int mode)
7352 int old_mode;
7353 int i;
7355 old_mode = env->uncached_cpsr & CPSR_M;
7356 if (mode == old_mode)
7357 return;
7359 if (old_mode == ARM_CPU_MODE_FIQ) {
7360 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
7361 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
7362 } else if (mode == ARM_CPU_MODE_FIQ) {
7363 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
7364 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
7367 i = bank_number(old_mode);
7368 env->banked_r13[i] = env->regs[13];
7369 env->banked_spsr[i] = env->spsr;
7371 i = bank_number(mode);
7372 env->regs[13] = env->banked_r13[i];
7373 env->spsr = env->banked_spsr[i];
7375 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
7376 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
7379 /* Physical Interrupt Target EL Lookup Table
7381 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
7383 * The below multi-dimensional table is used for looking up the target
7384 * exception level given numerous condition criteria. Specifically, the
7385 * target EL is based on SCR and HCR routing controls as well as the
7386 * currently executing EL and secure state.
7388 * Dimensions:
7389 * target_el_table[2][2][2][2][2][4]
7390 * | | | | | +--- Current EL
7391 * | | | | +------ Non-secure(0)/Secure(1)
7392 * | | | +--------- HCR mask override
7393 * | | +------------ SCR exec state control
7394 * | +--------------- SCR mask override
7395 * +------------------ 32-bit(0)/64-bit(1) EL3
7397 * The table values are as such:
7398 * 0-3 = EL0-EL3
7399 * -1 = Cannot occur
7401 * The ARM ARM target EL table includes entries indicating that an "exception
7402 * is not taken". The two cases where this is applicable are:
7403 * 1) An exception is taken from EL3 but the SCR does not have the exception
7404 * routed to EL3.
7405 * 2) An exception is taken from EL2 but the HCR does not have the exception
7406 * routed to EL2.
7407 * In these two cases, the below table contain a target of EL1. This value is
7408 * returned as it is expected that the consumer of the table data will check
7409 * for "target EL >= current EL" to ensure the exception is not taken.
7411 * SCR HCR
7412 * 64 EA AMO From
7413 * BIT IRQ IMO Non-secure Secure
7414 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
7416 static const int8_t target_el_table[2][2][2][2][2][4] = {
7417 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
7418 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
7419 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
7420 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
7421 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
7422 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
7423 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
7424 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
7425 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
7426 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
7427 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
7428 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
7429 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
7430 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
7431 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
7432 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
7436 * Determine the target EL for physical exceptions
7438 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
7439 uint32_t cur_el, bool secure)
7441 CPUARMState *env = cs->env_ptr;
7442 bool rw;
7443 bool scr;
7444 bool hcr;
7445 int target_el;
7446 /* Is the highest EL AArch64? */
7447 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
7448 uint64_t hcr_el2;
7450 if (arm_feature(env, ARM_FEATURE_EL3)) {
7451 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
7452 } else {
7453 /* Either EL2 is the highest EL (and so the EL2 register width
7454 * is given by is64); or there is no EL2 or EL3, in which case
7455 * the value of 'rw' does not affect the table lookup anyway.
7457 rw = is64;
7460 hcr_el2 = arm_hcr_el2_eff(env);
7461 switch (excp_idx) {
7462 case EXCP_IRQ:
7463 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
7464 hcr = hcr_el2 & HCR_IMO;
7465 break;
7466 case EXCP_FIQ:
7467 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
7468 hcr = hcr_el2 & HCR_FMO;
7469 break;
7470 default:
7471 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
7472 hcr = hcr_el2 & HCR_AMO;
7473 break;
7476 /* Perform a table-lookup for the target EL given the current state */
7477 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
7479 assert(target_el > 0);
7481 return target_el;
7484 static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value,
7485 ARMMMUIdx mmu_idx, bool ignfault)
7487 CPUState *cs = CPU(cpu);
7488 CPUARMState *env = &cpu->env;
7489 MemTxAttrs attrs = {};
7490 MemTxResult txres;
7491 target_ulong page_size;
7492 hwaddr physaddr;
7493 int prot;
7494 ARMMMUFaultInfo fi = {};
7495 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
7496 int exc;
7497 bool exc_secure;
7499 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr,
7500 &attrs, &prot, &page_size, &fi, NULL)) {
7501 /* MPU/SAU lookup failed */
7502 if (fi.type == ARMFault_QEMU_SFault) {
7503 qemu_log_mask(CPU_LOG_INT,
7504 "...SecureFault with SFSR.AUVIOL during stacking\n");
7505 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
7506 env->v7m.sfar = addr;
7507 exc = ARMV7M_EXCP_SECURE;
7508 exc_secure = false;
7509 } else {
7510 qemu_log_mask(CPU_LOG_INT, "...MemManageFault with CFSR.MSTKERR\n");
7511 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK;
7512 exc = ARMV7M_EXCP_MEM;
7513 exc_secure = secure;
7515 goto pend_fault;
7517 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value,
7518 attrs, &txres);
7519 if (txres != MEMTX_OK) {
7520 /* BusFault trying to write the data */
7521 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n");
7522 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK;
7523 exc = ARMV7M_EXCP_BUS;
7524 exc_secure = false;
7525 goto pend_fault;
7527 return true;
7529 pend_fault:
7530 /* By pending the exception at this point we are making
7531 * the IMPDEF choice "overridden exceptions pended" (see the
7532 * MergeExcInfo() pseudocode). The other choice would be to not
7533 * pend them now and then make a choice about which to throw away
7534 * later if we have two derived exceptions.
7535 * The only case when we must not pend the exception but instead
7536 * throw it away is if we are doing the push of the callee registers
7537 * and we've already generated a derived exception. Even in this
7538 * case we will still update the fault status registers.
7540 if (!ignfault) {
7541 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure);
7543 return false;
7546 static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr,
7547 ARMMMUIdx mmu_idx)
7549 CPUState *cs = CPU(cpu);
7550 CPUARMState *env = &cpu->env;
7551 MemTxAttrs attrs = {};
7552 MemTxResult txres;
7553 target_ulong page_size;
7554 hwaddr physaddr;
7555 int prot;
7556 ARMMMUFaultInfo fi = {};
7557 bool secure = mmu_idx & ARM_MMU_IDX_M_S;
7558 int exc;
7559 bool exc_secure;
7560 uint32_t value;
7562 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr,
7563 &attrs, &prot, &page_size, &fi, NULL)) {
7564 /* MPU/SAU lookup failed */
7565 if (fi.type == ARMFault_QEMU_SFault) {
7566 qemu_log_mask(CPU_LOG_INT,
7567 "...SecureFault with SFSR.AUVIOL during unstack\n");
7568 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK;
7569 env->v7m.sfar = addr;
7570 exc = ARMV7M_EXCP_SECURE;
7571 exc_secure = false;
7572 } else {
7573 qemu_log_mask(CPU_LOG_INT,
7574 "...MemManageFault with CFSR.MUNSTKERR\n");
7575 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK;
7576 exc = ARMV7M_EXCP_MEM;
7577 exc_secure = secure;
7579 goto pend_fault;
7582 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr,
7583 attrs, &txres);
7584 if (txres != MEMTX_OK) {
7585 /* BusFault trying to read the data */
7586 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n");
7587 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK;
7588 exc = ARMV7M_EXCP_BUS;
7589 exc_secure = false;
7590 goto pend_fault;
7593 *dest = value;
7594 return true;
7596 pend_fault:
7597 /* By pending the exception at this point we are making
7598 * the IMPDEF choice "overridden exceptions pended" (see the
7599 * MergeExcInfo() pseudocode). The other choice would be to not
7600 * pend them now and then make a choice about which to throw away
7601 * later if we have two derived exceptions.
7603 armv7m_nvic_set_pending(env->nvic, exc, exc_secure);
7604 return false;
7607 /* Write to v7M CONTROL.SPSEL bit for the specified security bank.
7608 * This may change the current stack pointer between Main and Process
7609 * stack pointers if it is done for the CONTROL register for the current
7610 * security state.
7612 static void write_v7m_control_spsel_for_secstate(CPUARMState *env,
7613 bool new_spsel,
7614 bool secstate)
7616 bool old_is_psp = v7m_using_psp(env);
7618 env->v7m.control[secstate] =
7619 deposit32(env->v7m.control[secstate],
7620 R_V7M_CONTROL_SPSEL_SHIFT,
7621 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel);
7623 if (secstate == env->v7m.secure) {
7624 bool new_is_psp = v7m_using_psp(env);
7625 uint32_t tmp;
7627 if (old_is_psp != new_is_psp) {
7628 tmp = env->v7m.other_sp;
7629 env->v7m.other_sp = env->regs[13];
7630 env->regs[13] = tmp;
7635 /* Write to v7M CONTROL.SPSEL bit. This may change the current
7636 * stack pointer between Main and Process stack pointers.
7638 static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel)
7640 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure);
7643 void write_v7m_exception(CPUARMState *env, uint32_t new_exc)
7645 /* Write a new value to v7m.exception, thus transitioning into or out
7646 * of Handler mode; this may result in a change of active stack pointer.
7648 bool new_is_psp, old_is_psp = v7m_using_psp(env);
7649 uint32_t tmp;
7651 env->v7m.exception = new_exc;
7653 new_is_psp = v7m_using_psp(env);
7655 if (old_is_psp != new_is_psp) {
7656 tmp = env->v7m.other_sp;
7657 env->v7m.other_sp = env->regs[13];
7658 env->regs[13] = tmp;
7662 /* Switch M profile security state between NS and S */
7663 static void switch_v7m_security_state(CPUARMState *env, bool new_secstate)
7665 uint32_t new_ss_msp, new_ss_psp;
7667 if (env->v7m.secure == new_secstate) {
7668 return;
7671 /* All the banked state is accessed by looking at env->v7m.secure
7672 * except for the stack pointer; rearrange the SP appropriately.
7674 new_ss_msp = env->v7m.other_ss_msp;
7675 new_ss_psp = env->v7m.other_ss_psp;
7677 if (v7m_using_psp(env)) {
7678 env->v7m.other_ss_psp = env->regs[13];
7679 env->v7m.other_ss_msp = env->v7m.other_sp;
7680 } else {
7681 env->v7m.other_ss_msp = env->regs[13];
7682 env->v7m.other_ss_psp = env->v7m.other_sp;
7685 env->v7m.secure = new_secstate;
7687 if (v7m_using_psp(env)) {
7688 env->regs[13] = new_ss_psp;
7689 env->v7m.other_sp = new_ss_msp;
7690 } else {
7691 env->regs[13] = new_ss_msp;
7692 env->v7m.other_sp = new_ss_psp;
7696 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest)
7698 /* Handle v7M BXNS:
7699 * - if the return value is a magic value, do exception return (like BX)
7700 * - otherwise bit 0 of the return value is the target security state
7702 uint32_t min_magic;
7704 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
7705 /* Covers FNC_RETURN and EXC_RETURN magic */
7706 min_magic = FNC_RETURN_MIN_MAGIC;
7707 } else {
7708 /* EXC_RETURN magic only */
7709 min_magic = EXC_RETURN_MIN_MAGIC;
7712 if (dest >= min_magic) {
7713 /* This is an exception return magic value; put it where
7714 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT.
7715 * Note that if we ever add gen_ss_advance() singlestep support to
7716 * M profile this should count as an "instruction execution complete"
7717 * event (compare gen_bx_excret_final_code()).
7719 env->regs[15] = dest & ~1;
7720 env->thumb = dest & 1;
7721 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT);
7722 /* notreached */
7725 /* translate.c should have made BXNS UNDEF unless we're secure */
7726 assert(env->v7m.secure);
7728 switch_v7m_security_state(env, dest & 1);
7729 env->thumb = 1;
7730 env->regs[15] = dest & ~1;
7733 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest)
7735 /* Handle v7M BLXNS:
7736 * - bit 0 of the destination address is the target security state
7739 /* At this point regs[15] is the address just after the BLXNS */
7740 uint32_t nextinst = env->regs[15] | 1;
7741 uint32_t sp = env->regs[13] - 8;
7742 uint32_t saved_psr;
7744 /* translate.c will have made BLXNS UNDEF unless we're secure */
7745 assert(env->v7m.secure);
7747 if (dest & 1) {
7748 /* target is Secure, so this is just a normal BLX,
7749 * except that the low bit doesn't indicate Thumb/not.
7751 env->regs[14] = nextinst;
7752 env->thumb = 1;
7753 env->regs[15] = dest & ~1;
7754 return;
7757 /* Target is non-secure: first push a stack frame */
7758 if (!QEMU_IS_ALIGNED(sp, 8)) {
7759 qemu_log_mask(LOG_GUEST_ERROR,
7760 "BLXNS with misaligned SP is UNPREDICTABLE\n");
7763 if (sp < v7m_sp_limit(env)) {
7764 raise_exception(env, EXCP_STKOF, 0, 1);
7767 saved_psr = env->v7m.exception;
7768 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) {
7769 saved_psr |= XPSR_SFPA;
7772 /* Note that these stores can throw exceptions on MPU faults */
7773 cpu_stl_data(env, sp, nextinst);
7774 cpu_stl_data(env, sp + 4, saved_psr);
7776 env->regs[13] = sp;
7777 env->regs[14] = 0xfeffffff;
7778 if (arm_v7m_is_handler_mode(env)) {
7779 /* Write a dummy value to IPSR, to avoid leaking the current secure
7780 * exception number to non-secure code. This is guaranteed not
7781 * to cause write_v7m_exception() to actually change stacks.
7783 write_v7m_exception(env, 1);
7785 switch_v7m_security_state(env, 0);
7786 env->thumb = 1;
7787 env->regs[15] = dest;
7790 static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode,
7791 bool spsel)
7793 /* Return a pointer to the location where we currently store the
7794 * stack pointer for the requested security state and thread mode.
7795 * This pointer will become invalid if the CPU state is updated
7796 * such that the stack pointers are switched around (eg changing
7797 * the SPSEL control bit).
7798 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode().
7799 * Unlike that pseudocode, we require the caller to pass us in the
7800 * SPSEL control bit value; this is because we also use this
7801 * function in handling of pushing of the callee-saves registers
7802 * part of the v8M stack frame (pseudocode PushCalleeStack()),
7803 * and in the tailchain codepath the SPSEL bit comes from the exception
7804 * return magic LR value from the previous exception. The pseudocode
7805 * opencodes the stack-selection in PushCalleeStack(), but we prefer
7806 * to make this utility function generic enough to do the job.
7808 bool want_psp = threadmode && spsel;
7810 if (secure == env->v7m.secure) {
7811 if (want_psp == v7m_using_psp(env)) {
7812 return &env->regs[13];
7813 } else {
7814 return &env->v7m.other_sp;
7816 } else {
7817 if (want_psp) {
7818 return &env->v7m.other_ss_psp;
7819 } else {
7820 return &env->v7m.other_ss_msp;
7825 static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure,
7826 uint32_t *pvec)
7828 CPUState *cs = CPU(cpu);
7829 CPUARMState *env = &cpu->env;
7830 MemTxResult result;
7831 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4;
7832 uint32_t vector_entry;
7833 MemTxAttrs attrs = {};
7834 ARMMMUIdx mmu_idx;
7835 bool exc_secure;
7837 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true);
7839 /* We don't do a get_phys_addr() here because the rules for vector
7840 * loads are special: they always use the default memory map, and
7841 * the default memory map permits reads from all addresses.
7842 * Since there's no easy way to pass through to pmsav8_mpu_lookup()
7843 * that we want this special case which would always say "yes",
7844 * we just do the SAU lookup here followed by a direct physical load.
7846 attrs.secure = targets_secure;
7847 attrs.user = false;
7849 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
7850 V8M_SAttributes sattrs = {};
7852 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
7853 if (sattrs.ns) {
7854 attrs.secure = false;
7855 } else if (!targets_secure) {
7856 /* NS access to S memory */
7857 goto load_fail;
7861 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr,
7862 attrs, &result);
7863 if (result != MEMTX_OK) {
7864 goto load_fail;
7866 *pvec = vector_entry;
7867 return true;
7869 load_fail:
7870 /* All vector table fetch fails are reported as HardFault, with
7871 * HFSR.VECTTBL and .FORCED set. (FORCED is set because
7872 * technically the underlying exception is a MemManage or BusFault
7873 * that is escalated to HardFault.) This is a terminal exception,
7874 * so we will either take the HardFault immediately or else enter
7875 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()).
7877 exc_secure = targets_secure ||
7878 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
7879 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK;
7880 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure);
7881 return false;
7884 static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain,
7885 bool ignore_faults)
7887 /* For v8M, push the callee-saves register part of the stack frame.
7888 * Compare the v8M pseudocode PushCalleeStack().
7889 * In the tailchaining case this may not be the current stack.
7891 CPUARMState *env = &cpu->env;
7892 uint32_t *frame_sp_p;
7893 uint32_t frameptr;
7894 ARMMMUIdx mmu_idx;
7895 bool stacked_ok;
7896 uint32_t limit;
7897 bool want_psp;
7899 if (dotailchain) {
7900 bool mode = lr & R_V7M_EXCRET_MODE_MASK;
7901 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) ||
7902 !mode;
7904 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv);
7905 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode,
7906 lr & R_V7M_EXCRET_SPSEL_MASK);
7907 want_psp = mode && (lr & R_V7M_EXCRET_SPSEL_MASK);
7908 if (want_psp) {
7909 limit = env->v7m.psplim[M_REG_S];
7910 } else {
7911 limit = env->v7m.msplim[M_REG_S];
7913 } else {
7914 mmu_idx = arm_mmu_idx(env);
7915 frame_sp_p = &env->regs[13];
7916 limit = v7m_sp_limit(env);
7919 frameptr = *frame_sp_p - 0x28;
7920 if (frameptr < limit) {
7922 * Stack limit failure: set SP to the limit value, and generate
7923 * STKOF UsageFault. Stack pushes below the limit must not be
7924 * performed. It is IMPDEF whether pushes above the limit are
7925 * performed; we choose not to.
7927 qemu_log_mask(CPU_LOG_INT,
7928 "...STKOF during callee-saves register stacking\n");
7929 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK;
7930 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
7931 env->v7m.secure);
7932 *frame_sp_p = limit;
7933 return true;
7936 /* Write as much of the stack frame as we can. A write failure may
7937 * cause us to pend a derived exception.
7939 stacked_ok =
7940 v7m_stack_write(cpu, frameptr, 0xfefa125b, mmu_idx, ignore_faults) &&
7941 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx,
7942 ignore_faults) &&
7943 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx,
7944 ignore_faults) &&
7945 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx,
7946 ignore_faults) &&
7947 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx,
7948 ignore_faults) &&
7949 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx,
7950 ignore_faults) &&
7951 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx,
7952 ignore_faults) &&
7953 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx,
7954 ignore_faults) &&
7955 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx,
7956 ignore_faults);
7958 /* Update SP regardless of whether any of the stack accesses failed. */
7959 *frame_sp_p = frameptr;
7961 return !stacked_ok;
7964 static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain,
7965 bool ignore_stackfaults)
7967 /* Do the "take the exception" parts of exception entry,
7968 * but not the pushing of state to the stack. This is
7969 * similar to the pseudocode ExceptionTaken() function.
7971 CPUARMState *env = &cpu->env;
7972 uint32_t addr;
7973 bool targets_secure;
7974 int exc;
7975 bool push_failed = false;
7977 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure);
7978 qemu_log_mask(CPU_LOG_INT, "...taking pending %s exception %d\n",
7979 targets_secure ? "secure" : "nonsecure", exc);
7981 if (arm_feature(env, ARM_FEATURE_V8)) {
7982 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
7983 (lr & R_V7M_EXCRET_S_MASK)) {
7984 /* The background code (the owner of the registers in the
7985 * exception frame) is Secure. This means it may either already
7986 * have or now needs to push callee-saves registers.
7988 if (targets_secure) {
7989 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) {
7990 /* We took an exception from Secure to NonSecure
7991 * (which means the callee-saved registers got stacked)
7992 * and are now tailchaining to a Secure exception.
7993 * Clear DCRS so eventual return from this Secure
7994 * exception unstacks the callee-saved registers.
7996 lr &= ~R_V7M_EXCRET_DCRS_MASK;
7998 } else {
7999 /* We're going to a non-secure exception; push the
8000 * callee-saves registers to the stack now, if they're
8001 * not already saved.
8003 if (lr & R_V7M_EXCRET_DCRS_MASK &&
8004 !(dotailchain && !(lr & R_V7M_EXCRET_ES_MASK))) {
8005 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain,
8006 ignore_stackfaults);
8008 lr |= R_V7M_EXCRET_DCRS_MASK;
8012 lr &= ~R_V7M_EXCRET_ES_MASK;
8013 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8014 lr |= R_V7M_EXCRET_ES_MASK;
8016 lr &= ~R_V7M_EXCRET_SPSEL_MASK;
8017 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) {
8018 lr |= R_V7M_EXCRET_SPSEL_MASK;
8021 /* Clear registers if necessary to prevent non-secure exception
8022 * code being able to see register values from secure code.
8023 * Where register values become architecturally UNKNOWN we leave
8024 * them with their previous values.
8026 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8027 if (!targets_secure) {
8028 /* Always clear the caller-saved registers (they have been
8029 * pushed to the stack earlier in v7m_push_stack()).
8030 * Clear callee-saved registers if the background code is
8031 * Secure (in which case these regs were saved in
8032 * v7m_push_callee_stack()).
8034 int i;
8036 for (i = 0; i < 13; i++) {
8037 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */
8038 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) {
8039 env->regs[i] = 0;
8042 /* Clear EAPSR */
8043 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT);
8048 if (push_failed && !ignore_stackfaults) {
8049 /* Derived exception on callee-saves register stacking:
8050 * we might now want to take a different exception which
8051 * targets a different security state, so try again from the top.
8053 qemu_log_mask(CPU_LOG_INT,
8054 "...derived exception on callee-saves register stacking");
8055 v7m_exception_taken(cpu, lr, true, true);
8056 return;
8059 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) {
8060 /* Vector load failed: derived exception */
8061 qemu_log_mask(CPU_LOG_INT, "...derived exception on vector table load");
8062 v7m_exception_taken(cpu, lr, true, true);
8063 return;
8066 /* Now we've done everything that might cause a derived exception
8067 * we can go ahead and activate whichever exception we're going to
8068 * take (which might now be the derived exception).
8070 armv7m_nvic_acknowledge_irq(env->nvic);
8072 /* Switch to target security state -- must do this before writing SPSEL */
8073 switch_v7m_security_state(env, targets_secure);
8074 write_v7m_control_spsel(env, 0);
8075 arm_clear_exclusive(env);
8076 /* Clear IT bits */
8077 env->condexec_bits = 0;
8078 env->regs[14] = lr;
8079 env->regs[15] = addr & 0xfffffffe;
8080 env->thumb = addr & 1;
8083 static bool v7m_push_stack(ARMCPU *cpu)
8085 /* Do the "set up stack frame" part of exception entry,
8086 * similar to pseudocode PushStack().
8087 * Return true if we generate a derived exception (and so
8088 * should ignore further stack faults trying to process
8089 * that derived exception.)
8091 bool stacked_ok;
8092 CPUARMState *env = &cpu->env;
8093 uint32_t xpsr = xpsr_read(env);
8094 uint32_t frameptr = env->regs[13];
8095 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
8097 /* Align stack pointer if the guest wants that */
8098 if ((frameptr & 4) &&
8099 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) {
8100 frameptr -= 4;
8101 xpsr |= XPSR_SPREALIGN;
8104 frameptr -= 0x20;
8106 if (arm_feature(env, ARM_FEATURE_V8)) {
8107 uint32_t limit = v7m_sp_limit(env);
8109 if (frameptr < limit) {
8111 * Stack limit failure: set SP to the limit value, and generate
8112 * STKOF UsageFault. Stack pushes below the limit must not be
8113 * performed. It is IMPDEF whether pushes above the limit are
8114 * performed; we choose not to.
8116 qemu_log_mask(CPU_LOG_INT,
8117 "...STKOF during stacking\n");
8118 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK;
8119 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
8120 env->v7m.secure);
8121 env->regs[13] = limit;
8122 return true;
8126 /* Write as much of the stack frame as we can. If we fail a stack
8127 * write this will result in a derived exception being pended
8128 * (which may be taken in preference to the one we started with
8129 * if it has higher priority).
8131 stacked_ok =
8132 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, false) &&
8133 v7m_stack_write(cpu, frameptr + 4, env->regs[1], mmu_idx, false) &&
8134 v7m_stack_write(cpu, frameptr + 8, env->regs[2], mmu_idx, false) &&
8135 v7m_stack_write(cpu, frameptr + 12, env->regs[3], mmu_idx, false) &&
8136 v7m_stack_write(cpu, frameptr + 16, env->regs[12], mmu_idx, false) &&
8137 v7m_stack_write(cpu, frameptr + 20, env->regs[14], mmu_idx, false) &&
8138 v7m_stack_write(cpu, frameptr + 24, env->regs[15], mmu_idx, false) &&
8139 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, false);
8141 /* Update SP regardless of whether any of the stack accesses failed. */
8142 env->regs[13] = frameptr;
8144 return !stacked_ok;
8147 static void do_v7m_exception_exit(ARMCPU *cpu)
8149 CPUARMState *env = &cpu->env;
8150 uint32_t excret;
8151 uint32_t xpsr;
8152 bool ufault = false;
8153 bool sfault = false;
8154 bool return_to_sp_process;
8155 bool return_to_handler;
8156 bool rettobase = false;
8157 bool exc_secure = false;
8158 bool return_to_secure;
8160 /* If we're not in Handler mode then jumps to magic exception-exit
8161 * addresses don't have magic behaviour. However for the v8M
8162 * security extensions the magic secure-function-return has to
8163 * work in thread mode too, so to avoid doing an extra check in
8164 * the generated code we allow exception-exit magic to also cause the
8165 * internal exception and bring us here in thread mode. Correct code
8166 * will never try to do this (the following insn fetch will always
8167 * fault) so we the overhead of having taken an unnecessary exception
8168 * doesn't matter.
8170 if (!arm_v7m_is_handler_mode(env)) {
8171 return;
8174 /* In the spec pseudocode ExceptionReturn() is called directly
8175 * from BXWritePC() and gets the full target PC value including
8176 * bit zero. In QEMU's implementation we treat it as a normal
8177 * jump-to-register (which is then caught later on), and so split
8178 * the target value up between env->regs[15] and env->thumb in
8179 * gen_bx(). Reconstitute it.
8181 excret = env->regs[15];
8182 if (env->thumb) {
8183 excret |= 1;
8186 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32
8187 " previous exception %d\n",
8188 excret, env->v7m.exception);
8190 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) {
8191 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception "
8192 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n",
8193 excret);
8196 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8197 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before
8198 * we pick which FAULTMASK to clear.
8200 if (!env->v7m.secure &&
8201 ((excret & R_V7M_EXCRET_ES_MASK) ||
8202 !(excret & R_V7M_EXCRET_DCRS_MASK))) {
8203 sfault = 1;
8204 /* For all other purposes, treat ES as 0 (R_HXSR) */
8205 excret &= ~R_V7M_EXCRET_ES_MASK;
8207 exc_secure = excret & R_V7M_EXCRET_ES_MASK;
8210 if (env->v7m.exception != ARMV7M_EXCP_NMI) {
8211 /* Auto-clear FAULTMASK on return from other than NMI.
8212 * If the security extension is implemented then this only
8213 * happens if the raw execution priority is >= 0; the
8214 * value of the ES bit in the exception return value indicates
8215 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.)
8217 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8218 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) {
8219 env->v7m.faultmask[exc_secure] = 0;
8221 } else {
8222 env->v7m.faultmask[M_REG_NS] = 0;
8226 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception,
8227 exc_secure)) {
8228 case -1:
8229 /* attempt to exit an exception that isn't active */
8230 ufault = true;
8231 break;
8232 case 0:
8233 /* still an irq active now */
8234 break;
8235 case 1:
8236 /* we returned to base exception level, no nesting.
8237 * (In the pseudocode this is written using "NestedActivation != 1"
8238 * where we have 'rettobase == false'.)
8240 rettobase = true;
8241 break;
8242 default:
8243 g_assert_not_reached();
8246 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK);
8247 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK;
8248 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
8249 (excret & R_V7M_EXCRET_S_MASK);
8251 if (arm_feature(env, ARM_FEATURE_V8)) {
8252 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) {
8253 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP);
8254 * we choose to take the UsageFault.
8256 if ((excret & R_V7M_EXCRET_S_MASK) ||
8257 (excret & R_V7M_EXCRET_ES_MASK) ||
8258 !(excret & R_V7M_EXCRET_DCRS_MASK)) {
8259 ufault = true;
8262 if (excret & R_V7M_EXCRET_RES0_MASK) {
8263 ufault = true;
8265 } else {
8266 /* For v7M we only recognize certain combinations of the low bits */
8267 switch (excret & 0xf) {
8268 case 1: /* Return to Handler */
8269 break;
8270 case 13: /* Return to Thread using Process stack */
8271 case 9: /* Return to Thread using Main stack */
8272 /* We only need to check NONBASETHRDENA for v7M, because in
8273 * v8M this bit does not exist (it is RES1).
8275 if (!rettobase &&
8276 !(env->v7m.ccr[env->v7m.secure] &
8277 R_V7M_CCR_NONBASETHRDENA_MASK)) {
8278 ufault = true;
8280 break;
8281 default:
8282 ufault = true;
8287 * Set CONTROL.SPSEL from excret.SPSEL. Since we're still in
8288 * Handler mode (and will be until we write the new XPSR.Interrupt
8289 * field) this does not switch around the current stack pointer.
8290 * We must do this before we do any kind of tailchaining, including
8291 * for the derived exceptions on integrity check failures, or we will
8292 * give the guest an incorrect EXCRET.SPSEL value on exception entry.
8294 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure);
8296 if (sfault) {
8297 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK;
8298 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
8299 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
8300 "stackframe: failed EXC_RETURN.ES validity check\n");
8301 v7m_exception_taken(cpu, excret, true, false);
8302 return;
8305 if (ufault) {
8306 /* Bad exception return: instead of popping the exception
8307 * stack, directly take a usage fault on the current stack.
8309 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
8310 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
8311 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
8312 "stackframe: failed exception return integrity check\n");
8313 v7m_exception_taken(cpu, excret, true, false);
8314 return;
8318 * Tailchaining: if there is currently a pending exception that
8319 * is high enough priority to preempt execution at the level we're
8320 * about to return to, then just directly take that exception now,
8321 * avoiding an unstack-and-then-stack. Note that now we have
8322 * deactivated the previous exception by calling armv7m_nvic_complete_irq()
8323 * our current execution priority is already the execution priority we are
8324 * returning to -- none of the state we would unstack or set based on
8325 * the EXCRET value affects it.
8327 if (armv7m_nvic_can_take_pending_exception(env->nvic)) {
8328 qemu_log_mask(CPU_LOG_INT, "...tailchaining to pending exception\n");
8329 v7m_exception_taken(cpu, excret, true, false);
8330 return;
8333 switch_v7m_security_state(env, return_to_secure);
8336 /* The stack pointer we should be reading the exception frame from
8337 * depends on bits in the magic exception return type value (and
8338 * for v8M isn't necessarily the stack pointer we will eventually
8339 * end up resuming execution with). Get a pointer to the location
8340 * in the CPU state struct where the SP we need is currently being
8341 * stored; we will use and modify it in place.
8342 * We use this limited C variable scope so we don't accidentally
8343 * use 'frame_sp_p' after we do something that makes it invalid.
8345 uint32_t *frame_sp_p = get_v7m_sp_ptr(env,
8346 return_to_secure,
8347 !return_to_handler,
8348 return_to_sp_process);
8349 uint32_t frameptr = *frame_sp_p;
8350 bool pop_ok = true;
8351 ARMMMUIdx mmu_idx;
8352 bool return_to_priv = return_to_handler ||
8353 !(env->v7m.control[return_to_secure] & R_V7M_CONTROL_NPRIV_MASK);
8355 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure,
8356 return_to_priv);
8358 if (!QEMU_IS_ALIGNED(frameptr, 8) &&
8359 arm_feature(env, ARM_FEATURE_V8)) {
8360 qemu_log_mask(LOG_GUEST_ERROR,
8361 "M profile exception return with non-8-aligned SP "
8362 "for destination state is UNPREDICTABLE\n");
8365 /* Do we need to pop callee-saved registers? */
8366 if (return_to_secure &&
8367 ((excret & R_V7M_EXCRET_ES_MASK) == 0 ||
8368 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) {
8369 uint32_t expected_sig = 0xfefa125b;
8370 uint32_t actual_sig;
8372 pop_ok = v7m_stack_read(cpu, &actual_sig, frameptr, mmu_idx);
8374 if (pop_ok && expected_sig != actual_sig) {
8375 /* Take a SecureFault on the current stack */
8376 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK;
8377 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
8378 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing "
8379 "stackframe: failed exception return integrity "
8380 "signature check\n");
8381 v7m_exception_taken(cpu, excret, true, false);
8382 return;
8385 pop_ok = pop_ok &&
8386 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) &&
8387 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) &&
8388 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) &&
8389 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) &&
8390 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) &&
8391 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) &&
8392 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) &&
8393 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx);
8395 frameptr += 0x28;
8398 /* Pop registers */
8399 pop_ok = pop_ok &&
8400 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) &&
8401 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) &&
8402 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) &&
8403 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) &&
8404 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) &&
8405 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) &&
8406 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) &&
8407 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx);
8409 if (!pop_ok) {
8410 /* v7m_stack_read() pended a fault, so take it (as a tail
8411 * chained exception on the same stack frame)
8413 qemu_log_mask(CPU_LOG_INT, "...derived exception on unstacking\n");
8414 v7m_exception_taken(cpu, excret, true, false);
8415 return;
8418 /* Returning from an exception with a PC with bit 0 set is defined
8419 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified
8420 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore
8421 * the lsbit, and there are several RTOSes out there which incorrectly
8422 * assume the r15 in the stack frame should be a Thumb-style "lsbit
8423 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but
8424 * complain about the badly behaved guest.
8426 if (env->regs[15] & 1) {
8427 env->regs[15] &= ~1U;
8428 if (!arm_feature(env, ARM_FEATURE_V8)) {
8429 qemu_log_mask(LOG_GUEST_ERROR,
8430 "M profile return from interrupt with misaligned "
8431 "PC is UNPREDICTABLE on v7M\n");
8435 if (arm_feature(env, ARM_FEATURE_V8)) {
8436 /* For v8M we have to check whether the xPSR exception field
8437 * matches the EXCRET value for return to handler/thread
8438 * before we commit to changing the SP and xPSR.
8440 bool will_be_handler = (xpsr & XPSR_EXCP) != 0;
8441 if (return_to_handler != will_be_handler) {
8442 /* Take an INVPC UsageFault on the current stack.
8443 * By this point we will have switched to the security state
8444 * for the background state, so this UsageFault will target
8445 * that state.
8447 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
8448 env->v7m.secure);
8449 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
8450 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing "
8451 "stackframe: failed exception return integrity "
8452 "check\n");
8453 v7m_exception_taken(cpu, excret, true, false);
8454 return;
8458 /* Commit to consuming the stack frame */
8459 frameptr += 0x20;
8460 /* Undo stack alignment (the SPREALIGN bit indicates that the original
8461 * pre-exception SP was not 8-aligned and we added a padding word to
8462 * align it, so we undo this by ORing in the bit that increases it
8463 * from the current 8-aligned value to the 8-unaligned value. (Adding 4
8464 * would work too but a logical OR is how the pseudocode specifies it.)
8466 if (xpsr & XPSR_SPREALIGN) {
8467 frameptr |= 4;
8469 *frame_sp_p = frameptr;
8471 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */
8472 xpsr_write(env, xpsr, ~XPSR_SPREALIGN);
8474 /* The restored xPSR exception field will be zero if we're
8475 * resuming in Thread mode. If that doesn't match what the
8476 * exception return excret specified then this is a UsageFault.
8477 * v7M requires we make this check here; v8M did it earlier.
8479 if (return_to_handler != arm_v7m_is_handler_mode(env)) {
8480 /* Take an INVPC UsageFault by pushing the stack again;
8481 * we know we're v7M so this is never a Secure UsageFault.
8483 bool ignore_stackfaults;
8485 assert(!arm_feature(env, ARM_FEATURE_V8));
8486 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false);
8487 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
8488 ignore_stackfaults = v7m_push_stack(cpu);
8489 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: "
8490 "failed exception return integrity check\n");
8491 v7m_exception_taken(cpu, excret, false, ignore_stackfaults);
8492 return;
8495 /* Otherwise, we have a successful exception exit. */
8496 arm_clear_exclusive(env);
8497 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n");
8500 static bool do_v7m_function_return(ARMCPU *cpu)
8502 /* v8M security extensions magic function return.
8503 * We may either:
8504 * (1) throw an exception (longjump)
8505 * (2) return true if we successfully handled the function return
8506 * (3) return false if we failed a consistency check and have
8507 * pended a UsageFault that needs to be taken now
8509 * At this point the magic return value is split between env->regs[15]
8510 * and env->thumb. We don't bother to reconstitute it because we don't
8511 * need it (all values are handled the same way).
8513 CPUARMState *env = &cpu->env;
8514 uint32_t newpc, newpsr, newpsr_exc;
8516 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n");
8519 bool threadmode, spsel;
8520 TCGMemOpIdx oi;
8521 ARMMMUIdx mmu_idx;
8522 uint32_t *frame_sp_p;
8523 uint32_t frameptr;
8525 /* Pull the return address and IPSR from the Secure stack */
8526 threadmode = !arm_v7m_is_handler_mode(env);
8527 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK;
8529 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel);
8530 frameptr = *frame_sp_p;
8532 /* These loads may throw an exception (for MPU faults). We want to
8533 * do them as secure, so work out what MMU index that is.
8535 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
8536 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx));
8537 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0);
8538 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0);
8540 /* Consistency checks on new IPSR */
8541 newpsr_exc = newpsr & XPSR_EXCP;
8542 if (!((env->v7m.exception == 0 && newpsr_exc == 0) ||
8543 (env->v7m.exception == 1 && newpsr_exc != 0))) {
8544 /* Pend the fault and tell our caller to take it */
8545 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK;
8546 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE,
8547 env->v7m.secure);
8548 qemu_log_mask(CPU_LOG_INT,
8549 "...taking INVPC UsageFault: "
8550 "IPSR consistency check failed\n");
8551 return false;
8554 *frame_sp_p = frameptr + 8;
8557 /* This invalidates frame_sp_p */
8558 switch_v7m_security_state(env, true);
8559 env->v7m.exception = newpsr_exc;
8560 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK;
8561 if (newpsr & XPSR_SFPA) {
8562 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK;
8564 xpsr_write(env, 0, XPSR_IT);
8565 env->thumb = newpc & 1;
8566 env->regs[15] = newpc & ~1;
8568 qemu_log_mask(CPU_LOG_INT, "...function return successful\n");
8569 return true;
8572 static void arm_log_exception(int idx)
8574 if (qemu_loglevel_mask(CPU_LOG_INT)) {
8575 const char *exc = NULL;
8576 static const char * const excnames[] = {
8577 [EXCP_UDEF] = "Undefined Instruction",
8578 [EXCP_SWI] = "SVC",
8579 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
8580 [EXCP_DATA_ABORT] = "Data Abort",
8581 [EXCP_IRQ] = "IRQ",
8582 [EXCP_FIQ] = "FIQ",
8583 [EXCP_BKPT] = "Breakpoint",
8584 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
8585 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
8586 [EXCP_HVC] = "Hypervisor Call",
8587 [EXCP_HYP_TRAP] = "Hypervisor Trap",
8588 [EXCP_SMC] = "Secure Monitor Call",
8589 [EXCP_VIRQ] = "Virtual IRQ",
8590 [EXCP_VFIQ] = "Virtual FIQ",
8591 [EXCP_SEMIHOST] = "Semihosting call",
8592 [EXCP_NOCP] = "v7M NOCP UsageFault",
8593 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
8594 [EXCP_STKOF] = "v8M STKOF UsageFault",
8597 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
8598 exc = excnames[idx];
8600 if (!exc) {
8601 exc = "unknown";
8603 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
8607 static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx,
8608 uint32_t addr, uint16_t *insn)
8610 /* Load a 16-bit portion of a v7M instruction, returning true on success,
8611 * or false on failure (in which case we will have pended the appropriate
8612 * exception).
8613 * We need to do the instruction fetch's MPU and SAU checks
8614 * like this because there is no MMU index that would allow
8615 * doing the load with a single function call. Instead we must
8616 * first check that the security attributes permit the load
8617 * and that they don't mismatch on the two halves of the instruction,
8618 * and then we do the load as a secure load (ie using the security
8619 * attributes of the address, not the CPU, as architecturally required).
8621 CPUState *cs = CPU(cpu);
8622 CPUARMState *env = &cpu->env;
8623 V8M_SAttributes sattrs = {};
8624 MemTxAttrs attrs = {};
8625 ARMMMUFaultInfo fi = {};
8626 MemTxResult txres;
8627 target_ulong page_size;
8628 hwaddr physaddr;
8629 int prot;
8631 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs);
8632 if (!sattrs.nsc || sattrs.ns) {
8633 /* This must be the second half of the insn, and it straddles a
8634 * region boundary with the second half not being S&NSC.
8636 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
8637 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
8638 qemu_log_mask(CPU_LOG_INT,
8639 "...really SecureFault with SFSR.INVEP\n");
8640 return false;
8642 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx,
8643 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) {
8644 /* the MPU lookup failed */
8645 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
8646 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure);
8647 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n");
8648 return false;
8650 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr,
8651 attrs, &txres);
8652 if (txres != MEMTX_OK) {
8653 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
8654 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
8655 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n");
8656 return false;
8658 return true;
8661 static bool v7m_handle_execute_nsc(ARMCPU *cpu)
8663 /* Check whether this attempt to execute code in a Secure & NS-Callable
8664 * memory region is for an SG instruction; if so, then emulate the
8665 * effect of the SG instruction and return true. Otherwise pend
8666 * the correct kind of exception and return false.
8668 CPUARMState *env = &cpu->env;
8669 ARMMMUIdx mmu_idx;
8670 uint16_t insn;
8672 /* We should never get here unless get_phys_addr_pmsav8() caused
8673 * an exception for NS executing in S&NSC memory.
8675 assert(!env->v7m.secure);
8676 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
8678 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */
8679 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true);
8681 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) {
8682 return false;
8685 if (!env->thumb) {
8686 goto gen_invep;
8689 if (insn != 0xe97f) {
8690 /* Not an SG instruction first half (we choose the IMPDEF
8691 * early-SG-check option).
8693 goto gen_invep;
8696 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) {
8697 return false;
8700 if (insn != 0xe97f) {
8701 /* Not an SG instruction second half (yes, both halves of the SG
8702 * insn have the same hex value)
8704 goto gen_invep;
8707 /* OK, we have confirmed that we really have an SG instruction.
8708 * We know we're NS in S memory so don't need to repeat those checks.
8710 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32
8711 ", executing it\n", env->regs[15]);
8712 env->regs[14] &= ~1;
8713 switch_v7m_security_state(env, true);
8714 xpsr_write(env, 0, XPSR_IT);
8715 env->regs[15] += 4;
8716 return true;
8718 gen_invep:
8719 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
8720 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
8721 qemu_log_mask(CPU_LOG_INT,
8722 "...really SecureFault with SFSR.INVEP\n");
8723 return false;
8726 void arm_v7m_cpu_do_interrupt(CPUState *cs)
8728 ARMCPU *cpu = ARM_CPU(cs);
8729 CPUARMState *env = &cpu->env;
8730 uint32_t lr;
8731 bool ignore_stackfaults;
8733 arm_log_exception(cs->exception_index);
8735 /* For exceptions we just mark as pending on the NVIC, and let that
8736 handle it. */
8737 switch (cs->exception_index) {
8738 case EXCP_UDEF:
8739 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
8740 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK;
8741 break;
8742 case EXCP_NOCP:
8743 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
8744 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK;
8745 break;
8746 case EXCP_INVSTATE:
8747 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
8748 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK;
8749 break;
8750 case EXCP_STKOF:
8751 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure);
8752 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK;
8753 break;
8754 case EXCP_SWI:
8755 /* The PC already points to the next instruction. */
8756 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure);
8757 break;
8758 case EXCP_PREFETCH_ABORT:
8759 case EXCP_DATA_ABORT:
8760 /* Note that for M profile we don't have a guest facing FSR, but
8761 * the env->exception.fsr will be populated by the code that
8762 * raises the fault, in the A profile short-descriptor format.
8764 switch (env->exception.fsr & 0xf) {
8765 case M_FAKE_FSR_NSC_EXEC:
8766 /* Exception generated when we try to execute code at an address
8767 * which is marked as Secure & Non-Secure Callable and the CPU
8768 * is in the Non-Secure state. The only instruction which can
8769 * be executed like this is SG (and that only if both halves of
8770 * the SG instruction have the same security attributes.)
8771 * Everything else must generate an INVEP SecureFault, so we
8772 * emulate the SG instruction here.
8774 if (v7m_handle_execute_nsc(cpu)) {
8775 return;
8777 break;
8778 case M_FAKE_FSR_SFAULT:
8779 /* Various flavours of SecureFault for attempts to execute or
8780 * access data in the wrong security state.
8782 switch (cs->exception_index) {
8783 case EXCP_PREFETCH_ABORT:
8784 if (env->v7m.secure) {
8785 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK;
8786 qemu_log_mask(CPU_LOG_INT,
8787 "...really SecureFault with SFSR.INVTRAN\n");
8788 } else {
8789 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK;
8790 qemu_log_mask(CPU_LOG_INT,
8791 "...really SecureFault with SFSR.INVEP\n");
8793 break;
8794 case EXCP_DATA_ABORT:
8795 /* This must be an NS access to S memory */
8796 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK;
8797 qemu_log_mask(CPU_LOG_INT,
8798 "...really SecureFault with SFSR.AUVIOL\n");
8799 break;
8801 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false);
8802 break;
8803 case 0x8: /* External Abort */
8804 switch (cs->exception_index) {
8805 case EXCP_PREFETCH_ABORT:
8806 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK;
8807 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n");
8808 break;
8809 case EXCP_DATA_ABORT:
8810 env->v7m.cfsr[M_REG_NS] |=
8811 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK);
8812 env->v7m.bfar = env->exception.vaddress;
8813 qemu_log_mask(CPU_LOG_INT,
8814 "...with CFSR.PRECISERR and BFAR 0x%x\n",
8815 env->v7m.bfar);
8816 break;
8818 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false);
8819 break;
8820 default:
8821 /* All other FSR values are either MPU faults or "can't happen
8822 * for M profile" cases.
8824 switch (cs->exception_index) {
8825 case EXCP_PREFETCH_ABORT:
8826 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK;
8827 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n");
8828 break;
8829 case EXCP_DATA_ABORT:
8830 env->v7m.cfsr[env->v7m.secure] |=
8831 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK);
8832 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress;
8833 qemu_log_mask(CPU_LOG_INT,
8834 "...with CFSR.DACCVIOL and MMFAR 0x%x\n",
8835 env->v7m.mmfar[env->v7m.secure]);
8836 break;
8838 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM,
8839 env->v7m.secure);
8840 break;
8842 break;
8843 case EXCP_BKPT:
8844 if (semihosting_enabled()) {
8845 int nr;
8846 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
8847 if (nr == 0xab) {
8848 env->regs[15] += 2;
8849 qemu_log_mask(CPU_LOG_INT,
8850 "...handling as semihosting call 0x%x\n",
8851 env->regs[0]);
8852 env->regs[0] = do_arm_semihosting(env);
8853 return;
8856 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false);
8857 break;
8858 case EXCP_IRQ:
8859 break;
8860 case EXCP_EXCEPTION_EXIT:
8861 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) {
8862 /* Must be v8M security extension function return */
8863 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC);
8864 assert(arm_feature(env, ARM_FEATURE_M_SECURITY));
8865 if (do_v7m_function_return(cpu)) {
8866 return;
8868 } else {
8869 do_v7m_exception_exit(cpu);
8870 return;
8872 break;
8873 default:
8874 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8875 return; /* Never happens. Keep compiler happy. */
8878 if (arm_feature(env, ARM_FEATURE_V8)) {
8879 lr = R_V7M_EXCRET_RES1_MASK |
8880 R_V7M_EXCRET_DCRS_MASK |
8881 R_V7M_EXCRET_FTYPE_MASK;
8882 /* The S bit indicates whether we should return to Secure
8883 * or NonSecure (ie our current state).
8884 * The ES bit indicates whether we're taking this exception
8885 * to Secure or NonSecure (ie our target state). We set it
8886 * later, in v7m_exception_taken().
8887 * The SPSEL bit is also set in v7m_exception_taken() for v8M.
8888 * This corresponds to the ARM ARM pseudocode for v8M setting
8889 * some LR bits in PushStack() and some in ExceptionTaken();
8890 * the distinction matters for the tailchain cases where we
8891 * can take an exception without pushing the stack.
8893 if (env->v7m.secure) {
8894 lr |= R_V7M_EXCRET_S_MASK;
8896 } else {
8897 lr = R_V7M_EXCRET_RES1_MASK |
8898 R_V7M_EXCRET_S_MASK |
8899 R_V7M_EXCRET_DCRS_MASK |
8900 R_V7M_EXCRET_FTYPE_MASK |
8901 R_V7M_EXCRET_ES_MASK;
8902 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) {
8903 lr |= R_V7M_EXCRET_SPSEL_MASK;
8906 if (!arm_v7m_is_handler_mode(env)) {
8907 lr |= R_V7M_EXCRET_MODE_MASK;
8910 ignore_stackfaults = v7m_push_stack(cpu);
8911 v7m_exception_taken(cpu, lr, false, ignore_stackfaults);
8914 /* Function used to synchronize QEMU's AArch64 register set with AArch32
8915 * register set. This is necessary when switching between AArch32 and AArch64
8916 * execution state.
8918 void aarch64_sync_32_to_64(CPUARMState *env)
8920 int i;
8921 uint32_t mode = env->uncached_cpsr & CPSR_M;
8923 /* We can blanket copy R[0:7] to X[0:7] */
8924 for (i = 0; i < 8; i++) {
8925 env->xregs[i] = env->regs[i];
8928 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
8929 * Otherwise, they come from the banked user regs.
8931 if (mode == ARM_CPU_MODE_FIQ) {
8932 for (i = 8; i < 13; i++) {
8933 env->xregs[i] = env->usr_regs[i - 8];
8935 } else {
8936 for (i = 8; i < 13; i++) {
8937 env->xregs[i] = env->regs[i];
8941 /* Registers x13-x23 are the various mode SP and FP registers. Registers
8942 * r13 and r14 are only copied if we are in that mode, otherwise we copy
8943 * from the mode banked register.
8945 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
8946 env->xregs[13] = env->regs[13];
8947 env->xregs[14] = env->regs[14];
8948 } else {
8949 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
8950 /* HYP is an exception in that it is copied from r14 */
8951 if (mode == ARM_CPU_MODE_HYP) {
8952 env->xregs[14] = env->regs[14];
8953 } else {
8954 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
8958 if (mode == ARM_CPU_MODE_HYP) {
8959 env->xregs[15] = env->regs[13];
8960 } else {
8961 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
8964 if (mode == ARM_CPU_MODE_IRQ) {
8965 env->xregs[16] = env->regs[14];
8966 env->xregs[17] = env->regs[13];
8967 } else {
8968 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
8969 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
8972 if (mode == ARM_CPU_MODE_SVC) {
8973 env->xregs[18] = env->regs[14];
8974 env->xregs[19] = env->regs[13];
8975 } else {
8976 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
8977 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
8980 if (mode == ARM_CPU_MODE_ABT) {
8981 env->xregs[20] = env->regs[14];
8982 env->xregs[21] = env->regs[13];
8983 } else {
8984 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
8985 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
8988 if (mode == ARM_CPU_MODE_UND) {
8989 env->xregs[22] = env->regs[14];
8990 env->xregs[23] = env->regs[13];
8991 } else {
8992 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
8993 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
8996 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
8997 * mode, then we can copy from r8-r14. Otherwise, we copy from the
8998 * FIQ bank for r8-r14.
9000 if (mode == ARM_CPU_MODE_FIQ) {
9001 for (i = 24; i < 31; i++) {
9002 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9004 } else {
9005 for (i = 24; i < 29; i++) {
9006 env->xregs[i] = env->fiq_regs[i - 24];
9008 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9009 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9012 env->pc = env->regs[15];
9015 /* Function used to synchronize QEMU's AArch32 register set with AArch64
9016 * register set. This is necessary when switching between AArch32 and AArch64
9017 * execution state.
9019 void aarch64_sync_64_to_32(CPUARMState *env)
9021 int i;
9022 uint32_t mode = env->uncached_cpsr & CPSR_M;
9024 /* We can blanket copy X[0:7] to R[0:7] */
9025 for (i = 0; i < 8; i++) {
9026 env->regs[i] = env->xregs[i];
9029 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9030 * Otherwise, we copy x8-x12 into the banked user regs.
9032 if (mode == ARM_CPU_MODE_FIQ) {
9033 for (i = 8; i < 13; i++) {
9034 env->usr_regs[i - 8] = env->xregs[i];
9036 } else {
9037 for (i = 8; i < 13; i++) {
9038 env->regs[i] = env->xregs[i];
9042 /* Registers r13 & r14 depend on the current mode.
9043 * If we are in a given mode, we copy the corresponding x registers to r13
9044 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9045 * for the mode.
9047 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9048 env->regs[13] = env->xregs[13];
9049 env->regs[14] = env->xregs[14];
9050 } else {
9051 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9053 /* HYP is an exception in that it does not have its own banked r14 but
9054 * shares the USR r14
9056 if (mode == ARM_CPU_MODE_HYP) {
9057 env->regs[14] = env->xregs[14];
9058 } else {
9059 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9063 if (mode == ARM_CPU_MODE_HYP) {
9064 env->regs[13] = env->xregs[15];
9065 } else {
9066 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9069 if (mode == ARM_CPU_MODE_IRQ) {
9070 env->regs[14] = env->xregs[16];
9071 env->regs[13] = env->xregs[17];
9072 } else {
9073 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9074 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9077 if (mode == ARM_CPU_MODE_SVC) {
9078 env->regs[14] = env->xregs[18];
9079 env->regs[13] = env->xregs[19];
9080 } else {
9081 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9082 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9085 if (mode == ARM_CPU_MODE_ABT) {
9086 env->regs[14] = env->xregs[20];
9087 env->regs[13] = env->xregs[21];
9088 } else {
9089 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9090 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9093 if (mode == ARM_CPU_MODE_UND) {
9094 env->regs[14] = env->xregs[22];
9095 env->regs[13] = env->xregs[23];
9096 } else {
9097 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9098 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9101 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9102 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9103 * FIQ bank for r8-r14.
9105 if (mode == ARM_CPU_MODE_FIQ) {
9106 for (i = 24; i < 31; i++) {
9107 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9109 } else {
9110 for (i = 24; i < 29; i++) {
9111 env->fiq_regs[i - 24] = env->xregs[i];
9113 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9114 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9117 env->regs[15] = env->pc;
9120 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9121 uint32_t mask, uint32_t offset,
9122 uint32_t newpc)
9124 /* Change the CPU state so as to actually take the exception. */
9125 switch_mode(env, new_mode);
9127 * For exceptions taken to AArch32 we must clear the SS bit in both
9128 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9130 env->uncached_cpsr &= ~PSTATE_SS;
9131 env->spsr = cpsr_read(env);
9132 /* Clear IT bits. */
9133 env->condexec_bits = 0;
9134 /* Switch to the new mode, and to the correct instruction set. */
9135 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9136 /* Set new mode endianness */
9137 env->uncached_cpsr &= ~CPSR_E;
9138 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
9139 env->uncached_cpsr |= CPSR_E;
9141 /* J and IL must always be cleared for exception entry */
9142 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9143 env->daif |= mask;
9145 if (new_mode == ARM_CPU_MODE_HYP) {
9146 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9147 env->elr_el[2] = env->regs[15];
9148 } else {
9150 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9151 * and we should just guard the thumb mode on V4
9153 if (arm_feature(env, ARM_FEATURE_V4T)) {
9154 env->thumb =
9155 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9157 env->regs[14] = env->regs[15] + offset;
9159 env->regs[15] = newpc;
9162 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9165 * Handle exception entry to Hyp mode; this is sufficiently
9166 * different to entry to other AArch32 modes that we handle it
9167 * separately here.
9169 * The vector table entry used is always the 0x14 Hyp mode entry point,
9170 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
9171 * The offset applied to the preferred return address is always zero
9172 * (see DDI0487C.a section G1.12.3).
9173 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9175 uint32_t addr, mask;
9176 ARMCPU *cpu = ARM_CPU(cs);
9177 CPUARMState *env = &cpu->env;
9179 switch (cs->exception_index) {
9180 case EXCP_UDEF:
9181 addr = 0x04;
9182 break;
9183 case EXCP_SWI:
9184 addr = 0x14;
9185 break;
9186 case EXCP_BKPT:
9187 /* Fall through to prefetch abort. */
9188 case EXCP_PREFETCH_ABORT:
9189 env->cp15.ifar_s = env->exception.vaddress;
9190 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9191 (uint32_t)env->exception.vaddress);
9192 addr = 0x0c;
9193 break;
9194 case EXCP_DATA_ABORT:
9195 env->cp15.dfar_s = env->exception.vaddress;
9196 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9197 (uint32_t)env->exception.vaddress);
9198 addr = 0x10;
9199 break;
9200 case EXCP_IRQ:
9201 addr = 0x18;
9202 break;
9203 case EXCP_FIQ:
9204 addr = 0x1c;
9205 break;
9206 case EXCP_HVC:
9207 addr = 0x08;
9208 break;
9209 case EXCP_HYP_TRAP:
9210 addr = 0x14;
9211 default:
9212 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9215 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9216 if (!arm_feature(env, ARM_FEATURE_V8)) {
9218 * QEMU syndrome values are v8-style. v7 has the IL bit
9219 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9220 * If this is a v7 CPU, squash the IL bit in those cases.
9222 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9223 (cs->exception_index == EXCP_DATA_ABORT &&
9224 !(env->exception.syndrome & ARM_EL_ISV)) ||
9225 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9226 env->exception.syndrome &= ~ARM_EL_IL;
9229 env->cp15.esr_el[2] = env->exception.syndrome;
9232 if (arm_current_el(env) != 2 && addr < 0x14) {
9233 addr = 0x14;
9236 mask = 0;
9237 if (!(env->cp15.scr_el3 & SCR_EA)) {
9238 mask |= CPSR_A;
9240 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9241 mask |= CPSR_I;
9243 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9244 mask |= CPSR_F;
9247 addr += env->cp15.hvbar;
9249 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9252 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9254 ARMCPU *cpu = ARM_CPU(cs);
9255 CPUARMState *env = &cpu->env;
9256 uint32_t addr;
9257 uint32_t mask;
9258 int new_mode;
9259 uint32_t offset;
9260 uint32_t moe;
9262 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9263 switch (syn_get_ec(env->exception.syndrome)) {
9264 case EC_BREAKPOINT:
9265 case EC_BREAKPOINT_SAME_EL:
9266 moe = 1;
9267 break;
9268 case EC_WATCHPOINT:
9269 case EC_WATCHPOINT_SAME_EL:
9270 moe = 10;
9271 break;
9272 case EC_AA32_BKPT:
9273 moe = 3;
9274 break;
9275 case EC_VECTORCATCH:
9276 moe = 5;
9277 break;
9278 default:
9279 moe = 0;
9280 break;
9283 if (moe) {
9284 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9287 if (env->exception.target_el == 2) {
9288 arm_cpu_do_interrupt_aarch32_hyp(cs);
9289 return;
9292 switch (cs->exception_index) {
9293 case EXCP_UDEF:
9294 new_mode = ARM_CPU_MODE_UND;
9295 addr = 0x04;
9296 mask = CPSR_I;
9297 if (env->thumb)
9298 offset = 2;
9299 else
9300 offset = 4;
9301 break;
9302 case EXCP_SWI:
9303 new_mode = ARM_CPU_MODE_SVC;
9304 addr = 0x08;
9305 mask = CPSR_I;
9306 /* The PC already points to the next instruction. */
9307 offset = 0;
9308 break;
9309 case EXCP_BKPT:
9310 /* Fall through to prefetch abort. */
9311 case EXCP_PREFETCH_ABORT:
9312 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9313 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9314 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9315 env->exception.fsr, (uint32_t)env->exception.vaddress);
9316 new_mode = ARM_CPU_MODE_ABT;
9317 addr = 0x0c;
9318 mask = CPSR_A | CPSR_I;
9319 offset = 4;
9320 break;
9321 case EXCP_DATA_ABORT:
9322 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9323 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9324 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9325 env->exception.fsr,
9326 (uint32_t)env->exception.vaddress);
9327 new_mode = ARM_CPU_MODE_ABT;
9328 addr = 0x10;
9329 mask = CPSR_A | CPSR_I;
9330 offset = 8;
9331 break;
9332 case EXCP_IRQ:
9333 new_mode = ARM_CPU_MODE_IRQ;
9334 addr = 0x18;
9335 /* Disable IRQ and imprecise data aborts. */
9336 mask = CPSR_A | CPSR_I;
9337 offset = 4;
9338 if (env->cp15.scr_el3 & SCR_IRQ) {
9339 /* IRQ routed to monitor mode */
9340 new_mode = ARM_CPU_MODE_MON;
9341 mask |= CPSR_F;
9343 break;
9344 case EXCP_FIQ:
9345 new_mode = ARM_CPU_MODE_FIQ;
9346 addr = 0x1c;
9347 /* Disable FIQ, IRQ and imprecise data aborts. */
9348 mask = CPSR_A | CPSR_I | CPSR_F;
9349 if (env->cp15.scr_el3 & SCR_FIQ) {
9350 /* FIQ routed to monitor mode */
9351 new_mode = ARM_CPU_MODE_MON;
9353 offset = 4;
9354 break;
9355 case EXCP_VIRQ:
9356 new_mode = ARM_CPU_MODE_IRQ;
9357 addr = 0x18;
9358 /* Disable IRQ and imprecise data aborts. */
9359 mask = CPSR_A | CPSR_I;
9360 offset = 4;
9361 break;
9362 case EXCP_VFIQ:
9363 new_mode = ARM_CPU_MODE_FIQ;
9364 addr = 0x1c;
9365 /* Disable FIQ, IRQ and imprecise data aborts. */
9366 mask = CPSR_A | CPSR_I | CPSR_F;
9367 offset = 4;
9368 break;
9369 case EXCP_SMC:
9370 new_mode = ARM_CPU_MODE_MON;
9371 addr = 0x08;
9372 mask = CPSR_A | CPSR_I | CPSR_F;
9373 offset = 0;
9374 break;
9375 default:
9376 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9377 return; /* Never happens. Keep compiler happy. */
9380 if (new_mode == ARM_CPU_MODE_MON) {
9381 addr += env->cp15.mvbar;
9382 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9383 /* High vectors. When enabled, base address cannot be remapped. */
9384 addr += 0xffff0000;
9385 } else {
9386 /* ARM v7 architectures provide a vector base address register to remap
9387 * the interrupt vector table.
9388 * This register is only followed in non-monitor mode, and is banked.
9389 * Note: only bits 31:5 are valid.
9391 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
9394 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9395 env->cp15.scr_el3 &= ~SCR_NS;
9398 take_aarch32_exception(env, new_mode, mask, offset, addr);
9401 /* Handle exception entry to a target EL which is using AArch64 */
9402 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
9404 ARMCPU *cpu = ARM_CPU(cs);
9405 CPUARMState *env = &cpu->env;
9406 unsigned int new_el = env->exception.target_el;
9407 target_ulong addr = env->cp15.vbar_el[new_el];
9408 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
9409 unsigned int cur_el = arm_current_el(env);
9412 * Note that new_el can never be 0. If cur_el is 0, then
9413 * el0_a64 is is_a64(), else el0_a64 is ignored.
9415 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
9417 if (cur_el < new_el) {
9418 /* Entry vector offset depends on whether the implemented EL
9419 * immediately lower than the target level is using AArch32 or AArch64
9421 bool is_aa64;
9423 switch (new_el) {
9424 case 3:
9425 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9426 break;
9427 case 2:
9428 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
9429 break;
9430 case 1:
9431 is_aa64 = is_a64(env);
9432 break;
9433 default:
9434 g_assert_not_reached();
9437 if (is_aa64) {
9438 addr += 0x400;
9439 } else {
9440 addr += 0x600;
9442 } else if (pstate_read(env) & PSTATE_SP) {
9443 addr += 0x200;
9446 switch (cs->exception_index) {
9447 case EXCP_PREFETCH_ABORT:
9448 case EXCP_DATA_ABORT:
9449 env->cp15.far_el[new_el] = env->exception.vaddress;
9450 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9451 env->cp15.far_el[new_el]);
9452 /* fall through */
9453 case EXCP_BKPT:
9454 case EXCP_UDEF:
9455 case EXCP_SWI:
9456 case EXCP_HVC:
9457 case EXCP_HYP_TRAP:
9458 case EXCP_SMC:
9459 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) {
9461 * QEMU internal FP/SIMD syndromes from AArch32 include the
9462 * TA and coproc fields which are only exposed if the exception
9463 * is taken to AArch32 Hyp mode. Mask them out to get a valid
9464 * AArch64 format syndrome.
9466 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
9468 env->cp15.esr_el[new_el] = env->exception.syndrome;
9469 break;
9470 case EXCP_IRQ:
9471 case EXCP_VIRQ:
9472 addr += 0x80;
9473 break;
9474 case EXCP_FIQ:
9475 case EXCP_VFIQ:
9476 addr += 0x100;
9477 break;
9478 case EXCP_SEMIHOST:
9479 qemu_log_mask(CPU_LOG_INT,
9480 "...handling as semihosting call 0x%" PRIx64 "\n",
9481 env->xregs[0]);
9482 env->xregs[0] = do_arm_semihosting(env);
9483 return;
9484 default:
9485 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9488 if (is_a64(env)) {
9489 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
9490 aarch64_save_sp(env, arm_current_el(env));
9491 env->elr_el[new_el] = env->pc;
9492 } else {
9493 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
9494 env->elr_el[new_el] = env->regs[15];
9496 aarch64_sync_32_to_64(env);
9498 env->condexec_bits = 0;
9500 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
9501 env->elr_el[new_el]);
9503 pstate_write(env, PSTATE_DAIF | new_mode);
9504 env->aarch64 = 1;
9505 aarch64_restore_sp(env, new_el);
9507 env->pc = addr;
9509 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
9510 new_el, env->pc, pstate_read(env));
9513 static inline bool check_for_semihosting(CPUState *cs)
9515 /* Check whether this exception is a semihosting call; if so
9516 * then handle it and return true; otherwise return false.
9518 ARMCPU *cpu = ARM_CPU(cs);
9519 CPUARMState *env = &cpu->env;
9521 if (is_a64(env)) {
9522 if (cs->exception_index == EXCP_SEMIHOST) {
9523 /* This is always the 64-bit semihosting exception.
9524 * The "is this usermode" and "is semihosting enabled"
9525 * checks have been done at translate time.
9527 qemu_log_mask(CPU_LOG_INT,
9528 "...handling as semihosting call 0x%" PRIx64 "\n",
9529 env->xregs[0]);
9530 env->xregs[0] = do_arm_semihosting(env);
9531 return true;
9533 return false;
9534 } else {
9535 uint32_t imm;
9537 /* Only intercept calls from privileged modes, to provide some
9538 * semblance of security.
9540 if (cs->exception_index != EXCP_SEMIHOST &&
9541 (!semihosting_enabled() ||
9542 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
9543 return false;
9546 switch (cs->exception_index) {
9547 case EXCP_SEMIHOST:
9548 /* This is always a semihosting call; the "is this usermode"
9549 * and "is semihosting enabled" checks have been done at
9550 * translate time.
9552 break;
9553 case EXCP_SWI:
9554 /* Check for semihosting interrupt. */
9555 if (env->thumb) {
9556 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
9557 & 0xff;
9558 if (imm == 0xab) {
9559 break;
9561 } else {
9562 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
9563 & 0xffffff;
9564 if (imm == 0x123456) {
9565 break;
9568 return false;
9569 case EXCP_BKPT:
9570 /* See if this is a semihosting syscall. */
9571 if (env->thumb) {
9572 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
9573 & 0xff;
9574 if (imm == 0xab) {
9575 env->regs[15] += 2;
9576 break;
9579 return false;
9580 default:
9581 return false;
9584 qemu_log_mask(CPU_LOG_INT,
9585 "...handling as semihosting call 0x%x\n",
9586 env->regs[0]);
9587 env->regs[0] = do_arm_semihosting(env);
9588 return true;
9592 /* Handle a CPU exception for A and R profile CPUs.
9593 * Do any appropriate logging, handle PSCI calls, and then hand off
9594 * to the AArch64-entry or AArch32-entry function depending on the
9595 * target exception level's register width.
9597 void arm_cpu_do_interrupt(CPUState *cs)
9599 ARMCPU *cpu = ARM_CPU(cs);
9600 CPUARMState *env = &cpu->env;
9601 unsigned int new_el = env->exception.target_el;
9603 assert(!arm_feature(env, ARM_FEATURE_M));
9605 arm_log_exception(cs->exception_index);
9606 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
9607 new_el);
9608 if (qemu_loglevel_mask(CPU_LOG_INT)
9609 && !excp_is_internal(cs->exception_index)) {
9610 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
9611 syn_get_ec(env->exception.syndrome),
9612 env->exception.syndrome);
9615 if (arm_is_psci_call(cpu, cs->exception_index)) {
9616 arm_handle_psci_call(cpu);
9617 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
9618 return;
9621 /* Semihosting semantics depend on the register width of the
9622 * code that caused the exception, not the target exception level,
9623 * so must be handled here.
9625 if (check_for_semihosting(cs)) {
9626 return;
9629 /* Hooks may change global state so BQL should be held, also the
9630 * BQL needs to be held for any modification of
9631 * cs->interrupt_request.
9633 g_assert(qemu_mutex_iothread_locked());
9635 arm_call_pre_el_change_hook(cpu);
9637 assert(!excp_is_internal(cs->exception_index));
9638 if (arm_el_is_aa64(env, new_el)) {
9639 arm_cpu_do_interrupt_aarch64(cs);
9640 } else {
9641 arm_cpu_do_interrupt_aarch32(cs);
9644 arm_call_el_change_hook(cpu);
9646 if (!kvm_enabled()) {
9647 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
9650 #endif /* !CONFIG_USER_ONLY */
9652 /* Return the exception level which controls this address translation regime */
9653 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
9655 switch (mmu_idx) {
9656 case ARMMMUIdx_S2NS:
9657 case ARMMMUIdx_S1E2:
9658 return 2;
9659 case ARMMMUIdx_S1E3:
9660 return 3;
9661 case ARMMMUIdx_S1SE0:
9662 return arm_el_is_aa64(env, 3) ? 1 : 3;
9663 case ARMMMUIdx_S1SE1:
9664 case ARMMMUIdx_S1NSE0:
9665 case ARMMMUIdx_S1NSE1:
9666 case ARMMMUIdx_MPrivNegPri:
9667 case ARMMMUIdx_MUserNegPri:
9668 case ARMMMUIdx_MPriv:
9669 case ARMMMUIdx_MUser:
9670 case ARMMMUIdx_MSPrivNegPri:
9671 case ARMMMUIdx_MSUserNegPri:
9672 case ARMMMUIdx_MSPriv:
9673 case ARMMMUIdx_MSUser:
9674 return 1;
9675 default:
9676 g_assert_not_reached();
9680 #ifndef CONFIG_USER_ONLY
9682 /* Return the SCTLR value which controls this address translation regime */
9683 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
9685 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
9688 /* Return true if the specified stage of address translation is disabled */
9689 static inline bool regime_translation_disabled(CPUARMState *env,
9690 ARMMMUIdx mmu_idx)
9692 if (arm_feature(env, ARM_FEATURE_M)) {
9693 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
9694 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
9695 case R_V7M_MPU_CTRL_ENABLE_MASK:
9696 /* Enabled, but not for HardFault and NMI */
9697 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
9698 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
9699 /* Enabled for all cases */
9700 return false;
9701 case 0:
9702 default:
9703 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
9704 * we warned about that in armv7m_nvic.c when the guest set it.
9706 return true;
9710 if (mmu_idx == ARMMMUIdx_S2NS) {
9711 /* HCR.DC means HCR.VM behaves as 1 */
9712 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
9715 if (env->cp15.hcr_el2 & HCR_TGE) {
9716 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
9717 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
9718 return true;
9722 if ((env->cp15.hcr_el2 & HCR_DC) &&
9723 (mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1)) {
9724 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
9725 return true;
9728 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
9731 static inline bool regime_translation_big_endian(CPUARMState *env,
9732 ARMMMUIdx mmu_idx)
9734 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
9737 /* Return the TTBR associated with this translation regime */
9738 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
9739 int ttbrn)
9741 if (mmu_idx == ARMMMUIdx_S2NS) {
9742 return env->cp15.vttbr_el2;
9744 if (ttbrn == 0) {
9745 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
9746 } else {
9747 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
9751 #endif /* !CONFIG_USER_ONLY */
9753 /* Return the TCR controlling this translation regime */
9754 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
9756 if (mmu_idx == ARMMMUIdx_S2NS) {
9757 return &env->cp15.vtcr_el2;
9759 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
9762 /* Convert a possible stage1+2 MMU index into the appropriate
9763 * stage 1 MMU index
9765 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
9767 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
9768 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
9770 return mmu_idx;
9773 /* Return true if the translation regime is using LPAE format page tables */
9774 static inline bool regime_using_lpae_format(CPUARMState *env,
9775 ARMMMUIdx mmu_idx)
9777 int el = regime_el(env, mmu_idx);
9778 if (el == 2 || arm_el_is_aa64(env, el)) {
9779 return true;
9781 if (arm_feature(env, ARM_FEATURE_LPAE)
9782 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
9783 return true;
9785 return false;
9788 /* Returns true if the stage 1 translation regime is using LPAE format page
9789 * tables. Used when raising alignment exceptions, whose FSR changes depending
9790 * on whether the long or short descriptor format is in use. */
9791 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
9793 mmu_idx = stage_1_mmu_idx(mmu_idx);
9795 return regime_using_lpae_format(env, mmu_idx);
9798 #ifndef CONFIG_USER_ONLY
9799 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
9801 switch (mmu_idx) {
9802 case ARMMMUIdx_S1SE0:
9803 case ARMMMUIdx_S1NSE0:
9804 case ARMMMUIdx_MUser:
9805 case ARMMMUIdx_MSUser:
9806 case ARMMMUIdx_MUserNegPri:
9807 case ARMMMUIdx_MSUserNegPri:
9808 return true;
9809 default:
9810 return false;
9811 case ARMMMUIdx_S12NSE0:
9812 case ARMMMUIdx_S12NSE1:
9813 g_assert_not_reached();
9817 /* Translate section/page access permissions to page
9818 * R/W protection flags
9820 * @env: CPUARMState
9821 * @mmu_idx: MMU index indicating required translation regime
9822 * @ap: The 3-bit access permissions (AP[2:0])
9823 * @domain_prot: The 2-bit domain access permissions
9825 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
9826 int ap, int domain_prot)
9828 bool is_user = regime_is_user(env, mmu_idx);
9830 if (domain_prot == 3) {
9831 return PAGE_READ | PAGE_WRITE;
9834 switch (ap) {
9835 case 0:
9836 if (arm_feature(env, ARM_FEATURE_V7)) {
9837 return 0;
9839 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
9840 case SCTLR_S:
9841 return is_user ? 0 : PAGE_READ;
9842 case SCTLR_R:
9843 return PAGE_READ;
9844 default:
9845 return 0;
9847 case 1:
9848 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9849 case 2:
9850 if (is_user) {
9851 return PAGE_READ;
9852 } else {
9853 return PAGE_READ | PAGE_WRITE;
9855 case 3:
9856 return PAGE_READ | PAGE_WRITE;
9857 case 4: /* Reserved. */
9858 return 0;
9859 case 5:
9860 return is_user ? 0 : PAGE_READ;
9861 case 6:
9862 return PAGE_READ;
9863 case 7:
9864 if (!arm_feature(env, ARM_FEATURE_V6K)) {
9865 return 0;
9867 return PAGE_READ;
9868 default:
9869 g_assert_not_reached();
9873 /* Translate section/page access permissions to page
9874 * R/W protection flags.
9876 * @ap: The 2-bit simple AP (AP[2:1])
9877 * @is_user: TRUE if accessing from PL0
9879 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
9881 switch (ap) {
9882 case 0:
9883 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9884 case 1:
9885 return PAGE_READ | PAGE_WRITE;
9886 case 2:
9887 return is_user ? 0 : PAGE_READ;
9888 case 3:
9889 return PAGE_READ;
9890 default:
9891 g_assert_not_reached();
9895 static inline int
9896 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
9898 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
9901 /* Translate S2 section/page access permissions to protection flags
9903 * @env: CPUARMState
9904 * @s2ap: The 2-bit stage2 access permissions (S2AP)
9905 * @xn: XN (execute-never) bit
9907 static int get_S2prot(CPUARMState *env, int s2ap, int xn)
9909 int prot = 0;
9911 if (s2ap & 1) {
9912 prot |= PAGE_READ;
9914 if (s2ap & 2) {
9915 prot |= PAGE_WRITE;
9917 if (!xn) {
9918 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
9919 prot |= PAGE_EXEC;
9922 return prot;
9925 /* Translate section/page access permissions to protection flags
9927 * @env: CPUARMState
9928 * @mmu_idx: MMU index indicating required translation regime
9929 * @is_aa64: TRUE if AArch64
9930 * @ap: The 2-bit simple AP (AP[2:1])
9931 * @ns: NS (non-secure) bit
9932 * @xn: XN (execute-never) bit
9933 * @pxn: PXN (privileged execute-never) bit
9935 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
9936 int ap, int ns, int xn, int pxn)
9938 bool is_user = regime_is_user(env, mmu_idx);
9939 int prot_rw, user_rw;
9940 bool have_wxn;
9941 int wxn = 0;
9943 assert(mmu_idx != ARMMMUIdx_S2NS);
9945 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
9946 if (is_user) {
9947 prot_rw = user_rw;
9948 } else {
9949 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
9952 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
9953 return prot_rw;
9956 /* TODO have_wxn should be replaced with
9957 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
9958 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
9959 * compatible processors have EL2, which is required for [U]WXN.
9961 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
9963 if (have_wxn) {
9964 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
9967 if (is_aa64) {
9968 switch (regime_el(env, mmu_idx)) {
9969 case 1:
9970 if (!is_user) {
9971 xn = pxn || (user_rw & PAGE_WRITE);
9973 break;
9974 case 2:
9975 case 3:
9976 break;
9978 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9979 switch (regime_el(env, mmu_idx)) {
9980 case 1:
9981 case 3:
9982 if (is_user) {
9983 xn = xn || !(user_rw & PAGE_READ);
9984 } else {
9985 int uwxn = 0;
9986 if (have_wxn) {
9987 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
9989 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
9990 (uwxn && (user_rw & PAGE_WRITE));
9992 break;
9993 case 2:
9994 break;
9996 } else {
9997 xn = wxn = 0;
10000 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10001 return prot_rw;
10003 return prot_rw | PAGE_EXEC;
10006 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10007 uint32_t *table, uint32_t address)
10009 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
10010 TCR *tcr = regime_tcr(env, mmu_idx);
10012 if (address & tcr->mask) {
10013 if (tcr->raw_tcr & TTBCR_PD1) {
10014 /* Translation table walk disabled for TTBR1 */
10015 return false;
10017 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
10018 } else {
10019 if (tcr->raw_tcr & TTBCR_PD0) {
10020 /* Translation table walk disabled for TTBR0 */
10021 return false;
10023 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
10025 *table |= (address >> 18) & 0x3ffc;
10026 return true;
10029 /* Translate a S1 pagetable walk through S2 if needed. */
10030 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
10031 hwaddr addr, MemTxAttrs txattrs,
10032 ARMMMUFaultInfo *fi)
10034 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
10035 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
10036 target_ulong s2size;
10037 hwaddr s2pa;
10038 int s2prot;
10039 int ret;
10040 ARMCacheAttrs cacheattrs = {};
10041 ARMCacheAttrs *pcacheattrs = NULL;
10043 if (env->cp15.hcr_el2 & HCR_PTW) {
10045 * PTW means we must fault if this S1 walk touches S2 Device
10046 * memory; otherwise we don't care about the attributes and can
10047 * save the S2 translation the effort of computing them.
10049 pcacheattrs = &cacheattrs;
10052 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
10053 &txattrs, &s2prot, &s2size, fi, pcacheattrs);
10054 if (ret) {
10055 assert(fi->type != ARMFault_None);
10056 fi->s2addr = addr;
10057 fi->stage2 = true;
10058 fi->s1ptw = true;
10059 return ~0;
10061 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
10062 /* Access was to Device memory: generate Permission fault */
10063 fi->type = ARMFault_Permission;
10064 fi->s2addr = addr;
10065 fi->stage2 = true;
10066 fi->s1ptw = true;
10067 return ~0;
10069 addr = s2pa;
10071 return addr;
10074 /* All loads done in the course of a page table walk go through here. */
10075 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
10076 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
10078 ARMCPU *cpu = ARM_CPU(cs);
10079 CPUARMState *env = &cpu->env;
10080 MemTxAttrs attrs = {};
10081 MemTxResult result = MEMTX_OK;
10082 AddressSpace *as;
10083 uint32_t data;
10085 attrs.secure = is_secure;
10086 as = arm_addressspace(cs, attrs);
10087 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
10088 if (fi->s1ptw) {
10089 return 0;
10091 if (regime_translation_big_endian(env, mmu_idx)) {
10092 data = address_space_ldl_be(as, addr, attrs, &result);
10093 } else {
10094 data = address_space_ldl_le(as, addr, attrs, &result);
10096 if (result == MEMTX_OK) {
10097 return data;
10099 fi->type = ARMFault_SyncExternalOnWalk;
10100 fi->ea = arm_extabort_type(result);
10101 return 0;
10104 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
10105 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
10107 ARMCPU *cpu = ARM_CPU(cs);
10108 CPUARMState *env = &cpu->env;
10109 MemTxAttrs attrs = {};
10110 MemTxResult result = MEMTX_OK;
10111 AddressSpace *as;
10112 uint64_t data;
10114 attrs.secure = is_secure;
10115 as = arm_addressspace(cs, attrs);
10116 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
10117 if (fi->s1ptw) {
10118 return 0;
10120 if (regime_translation_big_endian(env, mmu_idx)) {
10121 data = address_space_ldq_be(as, addr, attrs, &result);
10122 } else {
10123 data = address_space_ldq_le(as, addr, attrs, &result);
10125 if (result == MEMTX_OK) {
10126 return data;
10128 fi->type = ARMFault_SyncExternalOnWalk;
10129 fi->ea = arm_extabort_type(result);
10130 return 0;
10133 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
10134 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10135 hwaddr *phys_ptr, int *prot,
10136 target_ulong *page_size,
10137 ARMMMUFaultInfo *fi)
10139 CPUState *cs = CPU(arm_env_get_cpu(env));
10140 int level = 1;
10141 uint32_t table;
10142 uint32_t desc;
10143 int type;
10144 int ap;
10145 int domain = 0;
10146 int domain_prot;
10147 hwaddr phys_addr;
10148 uint32_t dacr;
10150 /* Pagetable walk. */
10151 /* Lookup l1 descriptor. */
10152 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
10153 /* Section translation fault if page walk is disabled by PD0 or PD1 */
10154 fi->type = ARMFault_Translation;
10155 goto do_fault;
10157 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10158 mmu_idx, fi);
10159 if (fi->type != ARMFault_None) {
10160 goto do_fault;
10162 type = (desc & 3);
10163 domain = (desc >> 5) & 0x0f;
10164 if (regime_el(env, mmu_idx) == 1) {
10165 dacr = env->cp15.dacr_ns;
10166 } else {
10167 dacr = env->cp15.dacr_s;
10169 domain_prot = (dacr >> (domain * 2)) & 3;
10170 if (type == 0) {
10171 /* Section translation fault. */
10172 fi->type = ARMFault_Translation;
10173 goto do_fault;
10175 if (type != 2) {
10176 level = 2;
10178 if (domain_prot == 0 || domain_prot == 2) {
10179 fi->type = ARMFault_Domain;
10180 goto do_fault;
10182 if (type == 2) {
10183 /* 1Mb section. */
10184 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10185 ap = (desc >> 10) & 3;
10186 *page_size = 1024 * 1024;
10187 } else {
10188 /* Lookup l2 entry. */
10189 if (type == 1) {
10190 /* Coarse pagetable. */
10191 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10192 } else {
10193 /* Fine pagetable. */
10194 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10196 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10197 mmu_idx, fi);
10198 if (fi->type != ARMFault_None) {
10199 goto do_fault;
10201 switch (desc & 3) {
10202 case 0: /* Page translation fault. */
10203 fi->type = ARMFault_Translation;
10204 goto do_fault;
10205 case 1: /* 64k page. */
10206 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10207 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
10208 *page_size = 0x10000;
10209 break;
10210 case 2: /* 4k page. */
10211 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10212 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
10213 *page_size = 0x1000;
10214 break;
10215 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
10216 if (type == 1) {
10217 /* ARMv6/XScale extended small page format */
10218 if (arm_feature(env, ARM_FEATURE_XSCALE)
10219 || arm_feature(env, ARM_FEATURE_V6)) {
10220 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10221 *page_size = 0x1000;
10222 } else {
10223 /* UNPREDICTABLE in ARMv5; we choose to take a
10224 * page translation fault.
10226 fi->type = ARMFault_Translation;
10227 goto do_fault;
10229 } else {
10230 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
10231 *page_size = 0x400;
10233 ap = (desc >> 4) & 3;
10234 break;
10235 default:
10236 /* Never happens, but compiler isn't smart enough to tell. */
10237 abort();
10240 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10241 *prot |= *prot ? PAGE_EXEC : 0;
10242 if (!(*prot & (1 << access_type))) {
10243 /* Access permission fault. */
10244 fi->type = ARMFault_Permission;
10245 goto do_fault;
10247 *phys_ptr = phys_addr;
10248 return false;
10249 do_fault:
10250 fi->domain = domain;
10251 fi->level = level;
10252 return true;
10255 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
10256 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10257 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
10258 target_ulong *page_size, ARMMMUFaultInfo *fi)
10260 CPUState *cs = CPU(arm_env_get_cpu(env));
10261 int level = 1;
10262 uint32_t table;
10263 uint32_t desc;
10264 uint32_t xn;
10265 uint32_t pxn = 0;
10266 int type;
10267 int ap;
10268 int domain = 0;
10269 int domain_prot;
10270 hwaddr phys_addr;
10271 uint32_t dacr;
10272 bool ns;
10274 /* Pagetable walk. */
10275 /* Lookup l1 descriptor. */
10276 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
10277 /* Section translation fault if page walk is disabled by PD0 or PD1 */
10278 fi->type = ARMFault_Translation;
10279 goto do_fault;
10281 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10282 mmu_idx, fi);
10283 if (fi->type != ARMFault_None) {
10284 goto do_fault;
10286 type = (desc & 3);
10287 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
10288 /* Section translation fault, or attempt to use the encoding
10289 * which is Reserved on implementations without PXN.
10291 fi->type = ARMFault_Translation;
10292 goto do_fault;
10294 if ((type == 1) || !(desc & (1 << 18))) {
10295 /* Page or Section. */
10296 domain = (desc >> 5) & 0x0f;
10298 if (regime_el(env, mmu_idx) == 1) {
10299 dacr = env->cp15.dacr_ns;
10300 } else {
10301 dacr = env->cp15.dacr_s;
10303 if (type == 1) {
10304 level = 2;
10306 domain_prot = (dacr >> (domain * 2)) & 3;
10307 if (domain_prot == 0 || domain_prot == 2) {
10308 /* Section or Page domain fault */
10309 fi->type = ARMFault_Domain;
10310 goto do_fault;
10312 if (type != 1) {
10313 if (desc & (1 << 18)) {
10314 /* Supersection. */
10315 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
10316 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10317 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
10318 *page_size = 0x1000000;
10319 } else {
10320 /* Section. */
10321 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10322 *page_size = 0x100000;
10324 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10325 xn = desc & (1 << 4);
10326 pxn = desc & 1;
10327 ns = extract32(desc, 19, 1);
10328 } else {
10329 if (arm_feature(env, ARM_FEATURE_PXN)) {
10330 pxn = (desc >> 2) & 1;
10332 ns = extract32(desc, 3, 1);
10333 /* Lookup l2 entry. */
10334 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10335 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10336 mmu_idx, fi);
10337 if (fi->type != ARMFault_None) {
10338 goto do_fault;
10340 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
10341 switch (desc & 3) {
10342 case 0: /* Page translation fault. */
10343 fi->type = ARMFault_Translation;
10344 goto do_fault;
10345 case 1: /* 64k page. */
10346 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10347 xn = desc & (1 << 15);
10348 *page_size = 0x10000;
10349 break;
10350 case 2: case 3: /* 4k page. */
10351 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10352 xn = desc & 1;
10353 *page_size = 0x1000;
10354 break;
10355 default:
10356 /* Never happens, but compiler isn't smart enough to tell. */
10357 abort();
10360 if (domain_prot == 3) {
10361 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10362 } else {
10363 if (pxn && !regime_is_user(env, mmu_idx)) {
10364 xn = 1;
10366 if (xn && access_type == MMU_INST_FETCH) {
10367 fi->type = ARMFault_Permission;
10368 goto do_fault;
10371 if (arm_feature(env, ARM_FEATURE_V6K) &&
10372 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
10373 /* The simplified model uses AP[0] as an access control bit. */
10374 if ((ap & 1) == 0) {
10375 /* Access flag fault. */
10376 fi->type = ARMFault_AccessFlag;
10377 goto do_fault;
10379 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
10380 } else {
10381 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10383 if (*prot && !xn) {
10384 *prot |= PAGE_EXEC;
10386 if (!(*prot & (1 << access_type))) {
10387 /* Access permission fault. */
10388 fi->type = ARMFault_Permission;
10389 goto do_fault;
10392 if (ns) {
10393 /* The NS bit will (as required by the architecture) have no effect if
10394 * the CPU doesn't support TZ or this is a non-secure translation
10395 * regime, because the attribute will already be non-secure.
10397 attrs->secure = false;
10399 *phys_ptr = phys_addr;
10400 return false;
10401 do_fault:
10402 fi->domain = domain;
10403 fi->level = level;
10404 return true;
10408 * check_s2_mmu_setup
10409 * @cpu: ARMCPU
10410 * @is_aa64: True if the translation regime is in AArch64 state
10411 * @startlevel: Suggested starting level
10412 * @inputsize: Bitsize of IPAs
10413 * @stride: Page-table stride (See the ARM ARM)
10415 * Returns true if the suggested S2 translation parameters are OK and
10416 * false otherwise.
10418 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
10419 int inputsize, int stride)
10421 const int grainsize = stride + 3;
10422 int startsizecheck;
10424 /* Negative levels are never allowed. */
10425 if (level < 0) {
10426 return false;
10429 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
10430 if (startsizecheck < 1 || startsizecheck > stride + 4) {
10431 return false;
10434 if (is_aa64) {
10435 CPUARMState *env = &cpu->env;
10436 unsigned int pamax = arm_pamax(cpu);
10438 switch (stride) {
10439 case 13: /* 64KB Pages. */
10440 if (level == 0 || (level == 1 && pamax <= 42)) {
10441 return false;
10443 break;
10444 case 11: /* 16KB Pages. */
10445 if (level == 0 || (level == 1 && pamax <= 40)) {
10446 return false;
10448 break;
10449 case 9: /* 4KB Pages. */
10450 if (level == 0 && pamax <= 42) {
10451 return false;
10453 break;
10454 default:
10455 g_assert_not_reached();
10458 /* Inputsize checks. */
10459 if (inputsize > pamax &&
10460 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
10461 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
10462 return false;
10464 } else {
10465 /* AArch32 only supports 4KB pages. Assert on that. */
10466 assert(stride == 9);
10468 if (level == 0) {
10469 return false;
10472 return true;
10475 /* Translate from the 4-bit stage 2 representation of
10476 * memory attributes (without cache-allocation hints) to
10477 * the 8-bit representation of the stage 1 MAIR registers
10478 * (which includes allocation hints).
10480 * ref: shared/translation/attrs/S2AttrDecode()
10481 * .../S2ConvertAttrsHints()
10483 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
10485 uint8_t hiattr = extract32(s2attrs, 2, 2);
10486 uint8_t loattr = extract32(s2attrs, 0, 2);
10487 uint8_t hihint = 0, lohint = 0;
10489 if (hiattr != 0) { /* normal memory */
10490 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
10491 hiattr = loattr = 1; /* non-cacheable */
10492 } else {
10493 if (hiattr != 1) { /* Write-through or write-back */
10494 hihint = 3; /* RW allocate */
10496 if (loattr != 1) { /* Write-through or write-back */
10497 lohint = 3; /* RW allocate */
10502 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
10504 #endif /* !CONFIG_USER_ONLY */
10506 ARMVAParameters aa64_va_parameters_both(CPUARMState *env, uint64_t va,
10507 ARMMMUIdx mmu_idx)
10509 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10510 uint32_t el = regime_el(env, mmu_idx);
10511 bool tbi, tbid, epd, hpd, using16k, using64k;
10512 int select, tsz;
10515 * Bit 55 is always between the two regions, and is canonical for
10516 * determining if address tagging is enabled.
10518 select = extract64(va, 55, 1);
10520 if (el > 1) {
10521 tsz = extract32(tcr, 0, 6);
10522 using64k = extract32(tcr, 14, 1);
10523 using16k = extract32(tcr, 15, 1);
10524 if (mmu_idx == ARMMMUIdx_S2NS) {
10525 /* VTCR_EL2 */
10526 tbi = tbid = hpd = false;
10527 } else {
10528 tbi = extract32(tcr, 20, 1);
10529 hpd = extract32(tcr, 24, 1);
10530 tbid = extract32(tcr, 29, 1);
10532 epd = false;
10533 } else if (!select) {
10534 tsz = extract32(tcr, 0, 6);
10535 epd = extract32(tcr, 7, 1);
10536 using64k = extract32(tcr, 14, 1);
10537 using16k = extract32(tcr, 15, 1);
10538 tbi = extract64(tcr, 37, 1);
10539 hpd = extract64(tcr, 41, 1);
10540 tbid = extract64(tcr, 51, 1);
10541 } else {
10542 int tg = extract32(tcr, 30, 2);
10543 using16k = tg == 1;
10544 using64k = tg == 3;
10545 tsz = extract32(tcr, 16, 6);
10546 epd = extract32(tcr, 23, 1);
10547 tbi = extract64(tcr, 38, 1);
10548 hpd = extract64(tcr, 42, 1);
10549 tbid = extract64(tcr, 52, 1);
10551 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
10552 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
10554 return (ARMVAParameters) {
10555 .tsz = tsz,
10556 .select = select,
10557 .tbi = tbi,
10558 .tbid = tbid,
10559 .epd = epd,
10560 .hpd = hpd,
10561 .using16k = using16k,
10562 .using64k = using64k,
10566 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10567 ARMMMUIdx mmu_idx, bool data)
10569 ARMVAParameters ret = aa64_va_parameters_both(env, va, mmu_idx);
10571 /* Present TBI as a composite with TBID. */
10572 ret.tbi &= (data || !ret.tbid);
10573 return ret;
10576 #ifndef CONFIG_USER_ONLY
10577 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
10578 ARMMMUIdx mmu_idx)
10580 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10581 uint32_t el = regime_el(env, mmu_idx);
10582 int select, tsz;
10583 bool epd, hpd;
10585 if (mmu_idx == ARMMMUIdx_S2NS) {
10586 /* VTCR */
10587 bool sext = extract32(tcr, 4, 1);
10588 bool sign = extract32(tcr, 3, 1);
10591 * If the sign-extend bit is not the same as t0sz[3], the result
10592 * is unpredictable. Flag this as a guest error.
10594 if (sign != sext) {
10595 qemu_log_mask(LOG_GUEST_ERROR,
10596 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
10598 tsz = sextract32(tcr, 0, 4) + 8;
10599 select = 0;
10600 hpd = false;
10601 epd = false;
10602 } else if (el == 2) {
10603 /* HTCR */
10604 tsz = extract32(tcr, 0, 3);
10605 select = 0;
10606 hpd = extract64(tcr, 24, 1);
10607 epd = false;
10608 } else {
10609 int t0sz = extract32(tcr, 0, 3);
10610 int t1sz = extract32(tcr, 16, 3);
10612 if (t1sz == 0) {
10613 select = va > (0xffffffffu >> t0sz);
10614 } else {
10615 /* Note that we will detect errors later. */
10616 select = va >= ~(0xffffffffu >> t1sz);
10618 if (!select) {
10619 tsz = t0sz;
10620 epd = extract32(tcr, 7, 1);
10621 hpd = extract64(tcr, 41, 1);
10622 } else {
10623 tsz = t1sz;
10624 epd = extract32(tcr, 23, 1);
10625 hpd = extract64(tcr, 42, 1);
10627 /* For aarch32, hpd0 is not enabled without t2e as well. */
10628 hpd &= extract32(tcr, 6, 1);
10631 return (ARMVAParameters) {
10632 .tsz = tsz,
10633 .select = select,
10634 .epd = epd,
10635 .hpd = hpd,
10639 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
10640 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10641 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
10642 target_ulong *page_size_ptr,
10643 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
10645 ARMCPU *cpu = arm_env_get_cpu(env);
10646 CPUState *cs = CPU(cpu);
10647 /* Read an LPAE long-descriptor translation table. */
10648 ARMFaultType fault_type = ARMFault_Translation;
10649 uint32_t level;
10650 ARMVAParameters param;
10651 uint64_t ttbr;
10652 hwaddr descaddr, indexmask, indexmask_grainsize;
10653 uint32_t tableattrs;
10654 target_ulong page_size;
10655 uint32_t attrs;
10656 int32_t stride;
10657 int addrsize, inputsize;
10658 TCR *tcr = regime_tcr(env, mmu_idx);
10659 int ap, ns, xn, pxn;
10660 uint32_t el = regime_el(env, mmu_idx);
10661 bool ttbr1_valid;
10662 uint64_t descaddrmask;
10663 bool aarch64 = arm_el_is_aa64(env, el);
10664 bool guarded = false;
10666 /* TODO:
10667 * This code does not handle the different format TCR for VTCR_EL2.
10668 * This code also does not support shareability levels.
10669 * Attribute and permission bit handling should also be checked when adding
10670 * support for those page table walks.
10672 if (aarch64) {
10673 param = aa64_va_parameters(env, address, mmu_idx,
10674 access_type != MMU_INST_FETCH);
10675 level = 0;
10676 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
10677 * invalid.
10679 ttbr1_valid = (el < 2);
10680 addrsize = 64 - 8 * param.tbi;
10681 inputsize = 64 - param.tsz;
10682 } else {
10683 param = aa32_va_parameters(env, address, mmu_idx);
10684 level = 1;
10685 /* There is no TTBR1 for EL2 */
10686 ttbr1_valid = (el != 2);
10687 addrsize = (mmu_idx == ARMMMUIdx_S2NS ? 40 : 32);
10688 inputsize = addrsize - param.tsz;
10692 * We determined the region when collecting the parameters, but we
10693 * have not yet validated that the address is valid for the region.
10694 * Extract the top bits and verify that they all match select.
10696 * For aa32, if inputsize == addrsize, then we have selected the
10697 * region by exclusion in aa32_va_parameters and there is no more
10698 * validation to do here.
10700 if (inputsize < addrsize) {
10701 target_ulong top_bits = sextract64(address, inputsize,
10702 addrsize - inputsize);
10703 if (-top_bits != param.select || (param.select && !ttbr1_valid)) {
10704 /* The gap between the two regions is a Translation fault */
10705 fault_type = ARMFault_Translation;
10706 goto do_fault;
10710 if (param.using64k) {
10711 stride = 13;
10712 } else if (param.using16k) {
10713 stride = 11;
10714 } else {
10715 stride = 9;
10718 /* Note that QEMU ignores shareability and cacheability attributes,
10719 * so we don't need to do anything with the SH, ORGN, IRGN fields
10720 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
10721 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
10722 * implement any ASID-like capability so we can ignore it (instead
10723 * we will always flush the TLB any time the ASID is changed).
10725 ttbr = regime_ttbr(env, mmu_idx, param.select);
10727 /* Here we should have set up all the parameters for the translation:
10728 * inputsize, ttbr, epd, stride, tbi
10731 if (param.epd) {
10732 /* Translation table walk disabled => Translation fault on TLB miss
10733 * Note: This is always 0 on 64-bit EL2 and EL3.
10735 goto do_fault;
10738 if (mmu_idx != ARMMMUIdx_S2NS) {
10739 /* The starting level depends on the virtual address size (which can
10740 * be up to 48 bits) and the translation granule size. It indicates
10741 * the number of strides (stride bits at a time) needed to
10742 * consume the bits of the input address. In the pseudocode this is:
10743 * level = 4 - RoundUp((inputsize - grainsize) / stride)
10744 * where their 'inputsize' is our 'inputsize', 'grainsize' is
10745 * our 'stride + 3' and 'stride' is our 'stride'.
10746 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
10747 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
10748 * = 4 - (inputsize - 4) / stride;
10750 level = 4 - (inputsize - 4) / stride;
10751 } else {
10752 /* For stage 2 translations the starting level is specified by the
10753 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
10755 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
10756 uint32_t startlevel;
10757 bool ok;
10759 if (!aarch64 || stride == 9) {
10760 /* AArch32 or 4KB pages */
10761 startlevel = 2 - sl0;
10762 } else {
10763 /* 16KB or 64KB pages */
10764 startlevel = 3 - sl0;
10767 /* Check that the starting level is valid. */
10768 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
10769 inputsize, stride);
10770 if (!ok) {
10771 fault_type = ARMFault_Translation;
10772 goto do_fault;
10774 level = startlevel;
10777 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
10778 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
10780 /* Now we can extract the actual base address from the TTBR */
10781 descaddr = extract64(ttbr, 0, 48);
10782 descaddr &= ~indexmask;
10784 /* The address field in the descriptor goes up to bit 39 for ARMv7
10785 * but up to bit 47 for ARMv8, but we use the descaddrmask
10786 * up to bit 39 for AArch32, because we don't need other bits in that case
10787 * to construct next descriptor address (anyway they should be all zeroes).
10789 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
10790 ~indexmask_grainsize;
10792 /* Secure accesses start with the page table in secure memory and
10793 * can be downgraded to non-secure at any step. Non-secure accesses
10794 * remain non-secure. We implement this by just ORing in the NSTable/NS
10795 * bits at each step.
10797 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
10798 for (;;) {
10799 uint64_t descriptor;
10800 bool nstable;
10802 descaddr |= (address >> (stride * (4 - level))) & indexmask;
10803 descaddr &= ~7ULL;
10804 nstable = extract32(tableattrs, 4, 1);
10805 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
10806 if (fi->type != ARMFault_None) {
10807 goto do_fault;
10810 if (!(descriptor & 1) ||
10811 (!(descriptor & 2) && (level == 3))) {
10812 /* Invalid, or the Reserved level 3 encoding */
10813 goto do_fault;
10815 descaddr = descriptor & descaddrmask;
10817 if ((descriptor & 2) && (level < 3)) {
10818 /* Table entry. The top five bits are attributes which may
10819 * propagate down through lower levels of the table (and
10820 * which are all arranged so that 0 means "no effect", so
10821 * we can gather them up by ORing in the bits at each level).
10823 tableattrs |= extract64(descriptor, 59, 5);
10824 level++;
10825 indexmask = indexmask_grainsize;
10826 continue;
10828 /* Block entry at level 1 or 2, or page entry at level 3.
10829 * These are basically the same thing, although the number
10830 * of bits we pull in from the vaddr varies.
10832 page_size = (1ULL << ((stride * (4 - level)) + 3));
10833 descaddr |= (address & (page_size - 1));
10834 /* Extract attributes from the descriptor */
10835 attrs = extract64(descriptor, 2, 10)
10836 | (extract64(descriptor, 52, 12) << 10);
10838 if (mmu_idx == ARMMMUIdx_S2NS) {
10839 /* Stage 2 table descriptors do not include any attribute fields */
10840 break;
10842 /* Merge in attributes from table descriptors */
10843 attrs |= nstable << 3; /* NS */
10844 guarded = extract64(descriptor, 50, 1); /* GP */
10845 if (param.hpd) {
10846 /* HPD disables all the table attributes except NSTable. */
10847 break;
10849 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
10850 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
10851 * means "force PL1 access only", which means forcing AP[1] to 0.
10853 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
10854 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
10855 break;
10857 /* Here descaddr is the final physical address, and attributes
10858 * are all in attrs.
10860 fault_type = ARMFault_AccessFlag;
10861 if ((attrs & (1 << 8)) == 0) {
10862 /* Access flag */
10863 goto do_fault;
10866 ap = extract32(attrs, 4, 2);
10867 xn = extract32(attrs, 12, 1);
10869 if (mmu_idx == ARMMMUIdx_S2NS) {
10870 ns = true;
10871 *prot = get_S2prot(env, ap, xn);
10872 } else {
10873 ns = extract32(attrs, 3, 1);
10874 pxn = extract32(attrs, 11, 1);
10875 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
10878 fault_type = ARMFault_Permission;
10879 if (!(*prot & (1 << access_type))) {
10880 goto do_fault;
10883 if (ns) {
10884 /* The NS bit will (as required by the architecture) have no effect if
10885 * the CPU doesn't support TZ or this is a non-secure translation
10886 * regime, because the attribute will already be non-secure.
10888 txattrs->secure = false;
10890 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
10891 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
10892 txattrs->target_tlb_bit0 = true;
10895 if (cacheattrs != NULL) {
10896 if (mmu_idx == ARMMMUIdx_S2NS) {
10897 cacheattrs->attrs = convert_stage2_attrs(env,
10898 extract32(attrs, 0, 4));
10899 } else {
10900 /* Index into MAIR registers for cache attributes */
10901 uint8_t attrindx = extract32(attrs, 0, 3);
10902 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
10903 assert(attrindx <= 7);
10904 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
10906 cacheattrs->shareability = extract32(attrs, 6, 2);
10909 *phys_ptr = descaddr;
10910 *page_size_ptr = page_size;
10911 return false;
10913 do_fault:
10914 fi->type = fault_type;
10915 fi->level = level;
10916 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
10917 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
10918 return true;
10921 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
10922 ARMMMUIdx mmu_idx,
10923 int32_t address, int *prot)
10925 if (!arm_feature(env, ARM_FEATURE_M)) {
10926 *prot = PAGE_READ | PAGE_WRITE;
10927 switch (address) {
10928 case 0xF0000000 ... 0xFFFFFFFF:
10929 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
10930 /* hivecs execing is ok */
10931 *prot |= PAGE_EXEC;
10933 break;
10934 case 0x00000000 ... 0x7FFFFFFF:
10935 *prot |= PAGE_EXEC;
10936 break;
10938 } else {
10939 /* Default system address map for M profile cores.
10940 * The architecture specifies which regions are execute-never;
10941 * at the MPU level no other checks are defined.
10943 switch (address) {
10944 case 0x00000000 ... 0x1fffffff: /* ROM */
10945 case 0x20000000 ... 0x3fffffff: /* SRAM */
10946 case 0x60000000 ... 0x7fffffff: /* RAM */
10947 case 0x80000000 ... 0x9fffffff: /* RAM */
10948 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10949 break;
10950 case 0x40000000 ... 0x5fffffff: /* Peripheral */
10951 case 0xa0000000 ... 0xbfffffff: /* Device */
10952 case 0xc0000000 ... 0xdfffffff: /* Device */
10953 case 0xe0000000 ... 0xffffffff: /* System */
10954 *prot = PAGE_READ | PAGE_WRITE;
10955 break;
10956 default:
10957 g_assert_not_reached();
10962 static bool pmsav7_use_background_region(ARMCPU *cpu,
10963 ARMMMUIdx mmu_idx, bool is_user)
10965 /* Return true if we should use the default memory map as a
10966 * "background" region if there are no hits against any MPU regions.
10968 CPUARMState *env = &cpu->env;
10970 if (is_user) {
10971 return false;
10974 if (arm_feature(env, ARM_FEATURE_M)) {
10975 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
10976 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
10977 } else {
10978 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
10982 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
10984 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
10985 return arm_feature(env, ARM_FEATURE_M) &&
10986 extract32(address, 20, 12) == 0xe00;
10989 static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
10991 /* True if address is in the M profile system region
10992 * 0xe0000000 - 0xffffffff
10994 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
10997 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
10998 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10999 hwaddr *phys_ptr, int *prot,
11000 target_ulong *page_size,
11001 ARMMMUFaultInfo *fi)
11003 ARMCPU *cpu = arm_env_get_cpu(env);
11004 int n;
11005 bool is_user = regime_is_user(env, mmu_idx);
11007 *phys_ptr = address;
11008 *page_size = TARGET_PAGE_SIZE;
11009 *prot = 0;
11011 if (regime_translation_disabled(env, mmu_idx) ||
11012 m_is_ppb_region(env, address)) {
11013 /* MPU disabled or M profile PPB access: use default memory map.
11014 * The other case which uses the default memory map in the
11015 * v7M ARM ARM pseudocode is exception vector reads from the vector
11016 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11017 * which always does a direct read using address_space_ldl(), rather
11018 * than going via this function, so we don't need to check that here.
11020 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11021 } else { /* MPU enabled */
11022 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11023 /* region search */
11024 uint32_t base = env->pmsav7.drbar[n];
11025 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11026 uint32_t rmask;
11027 bool srdis = false;
11029 if (!(env->pmsav7.drsr[n] & 0x1)) {
11030 continue;
11033 if (!rsize) {
11034 qemu_log_mask(LOG_GUEST_ERROR,
11035 "DRSR[%d]: Rsize field cannot be 0\n", n);
11036 continue;
11038 rsize++;
11039 rmask = (1ull << rsize) - 1;
11041 if (base & rmask) {
11042 qemu_log_mask(LOG_GUEST_ERROR,
11043 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11044 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11045 n, base, rmask);
11046 continue;
11049 if (address < base || address > base + rmask) {
11051 * Address not in this region. We must check whether the
11052 * region covers addresses in the same page as our address.
11053 * In that case we must not report a size that covers the
11054 * whole page for a subsequent hit against a different MPU
11055 * region or the background region, because it would result in
11056 * incorrect TLB hits for subsequent accesses to addresses that
11057 * are in this MPU region.
11059 if (ranges_overlap(base, rmask,
11060 address & TARGET_PAGE_MASK,
11061 TARGET_PAGE_SIZE)) {
11062 *page_size = 1;
11064 continue;
11067 /* Region matched */
11069 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
11070 int i, snd;
11071 uint32_t srdis_mask;
11073 rsize -= 3; /* sub region size (power of 2) */
11074 snd = ((address - base) >> rsize) & 0x7;
11075 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
11077 srdis_mask = srdis ? 0x3 : 0x0;
11078 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
11079 /* This will check in groups of 2, 4 and then 8, whether
11080 * the subregion bits are consistent. rsize is incremented
11081 * back up to give the region size, considering consistent
11082 * adjacent subregions as one region. Stop testing if rsize
11083 * is already big enough for an entire QEMU page.
11085 int snd_rounded = snd & ~(i - 1);
11086 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
11087 snd_rounded + 8, i);
11088 if (srdis_mask ^ srdis_multi) {
11089 break;
11091 srdis_mask = (srdis_mask << i) | srdis_mask;
11092 rsize++;
11095 if (srdis) {
11096 continue;
11098 if (rsize < TARGET_PAGE_BITS) {
11099 *page_size = 1 << rsize;
11101 break;
11104 if (n == -1) { /* no hits */
11105 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11106 /* background fault */
11107 fi->type = ARMFault_Background;
11108 return true;
11110 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11111 } else { /* a MPU hit! */
11112 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
11113 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
11115 if (m_is_system_region(env, address)) {
11116 /* System space is always execute never */
11117 xn = 1;
11120 if (is_user) { /* User mode AP bit decoding */
11121 switch (ap) {
11122 case 0:
11123 case 1:
11124 case 5:
11125 break; /* no access */
11126 case 3:
11127 *prot |= PAGE_WRITE;
11128 /* fall through */
11129 case 2:
11130 case 6:
11131 *prot |= PAGE_READ | PAGE_EXEC;
11132 break;
11133 case 7:
11134 /* for v7M, same as 6; for R profile a reserved value */
11135 if (arm_feature(env, ARM_FEATURE_M)) {
11136 *prot |= PAGE_READ | PAGE_EXEC;
11137 break;
11139 /* fall through */
11140 default:
11141 qemu_log_mask(LOG_GUEST_ERROR,
11142 "DRACR[%d]: Bad value for AP bits: 0x%"
11143 PRIx32 "\n", n, ap);
11145 } else { /* Priv. mode AP bits decoding */
11146 switch (ap) {
11147 case 0:
11148 break; /* no access */
11149 case 1:
11150 case 2:
11151 case 3:
11152 *prot |= PAGE_WRITE;
11153 /* fall through */
11154 case 5:
11155 case 6:
11156 *prot |= PAGE_READ | PAGE_EXEC;
11157 break;
11158 case 7:
11159 /* for v7M, same as 6; for R profile a reserved value */
11160 if (arm_feature(env, ARM_FEATURE_M)) {
11161 *prot |= PAGE_READ | PAGE_EXEC;
11162 break;
11164 /* fall through */
11165 default:
11166 qemu_log_mask(LOG_GUEST_ERROR,
11167 "DRACR[%d]: Bad value for AP bits: 0x%"
11168 PRIx32 "\n", n, ap);
11172 /* execute never */
11173 if (xn) {
11174 *prot &= ~PAGE_EXEC;
11179 fi->type = ARMFault_Permission;
11180 fi->level = 1;
11181 return !(*prot & (1 << access_type));
11184 static bool v8m_is_sau_exempt(CPUARMState *env,
11185 uint32_t address, MMUAccessType access_type)
11187 /* The architecture specifies that certain address ranges are
11188 * exempt from v8M SAU/IDAU checks.
11190 return
11191 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
11192 (address >= 0xe0000000 && address <= 0xe0002fff) ||
11193 (address >= 0xe000e000 && address <= 0xe000efff) ||
11194 (address >= 0xe002e000 && address <= 0xe002efff) ||
11195 (address >= 0xe0040000 && address <= 0xe0041fff) ||
11196 (address >= 0xe00ff000 && address <= 0xe00fffff);
11199 static void v8m_security_lookup(CPUARMState *env, uint32_t address,
11200 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11201 V8M_SAttributes *sattrs)
11203 /* Look up the security attributes for this address. Compare the
11204 * pseudocode SecurityCheck() function.
11205 * We assume the caller has zero-initialized *sattrs.
11207 ARMCPU *cpu = arm_env_get_cpu(env);
11208 int r;
11209 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
11210 int idau_region = IREGION_NOTVALID;
11211 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11212 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
11214 if (cpu->idau) {
11215 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
11216 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
11218 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
11219 &idau_nsc);
11222 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
11223 /* 0xf0000000..0xffffffff is always S for insn fetches */
11224 return;
11227 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
11228 sattrs->ns = !regime_is_secure(env, mmu_idx);
11229 return;
11232 if (idau_region != IREGION_NOTVALID) {
11233 sattrs->irvalid = true;
11234 sattrs->iregion = idau_region;
11237 switch (env->sau.ctrl & 3) {
11238 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
11239 break;
11240 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
11241 sattrs->ns = true;
11242 break;
11243 default: /* SAU.ENABLE == 1 */
11244 for (r = 0; r < cpu->sau_sregion; r++) {
11245 if (env->sau.rlar[r] & 1) {
11246 uint32_t base = env->sau.rbar[r] & ~0x1f;
11247 uint32_t limit = env->sau.rlar[r] | 0x1f;
11249 if (base <= address && limit >= address) {
11250 if (base > addr_page_base || limit < addr_page_limit) {
11251 sattrs->subpage = true;
11253 if (sattrs->srvalid) {
11254 /* If we hit in more than one region then we must report
11255 * as Secure, not NS-Callable, with no valid region
11256 * number info.
11258 sattrs->ns = false;
11259 sattrs->nsc = false;
11260 sattrs->sregion = 0;
11261 sattrs->srvalid = false;
11262 break;
11263 } else {
11264 if (env->sau.rlar[r] & 2) {
11265 sattrs->nsc = true;
11266 } else {
11267 sattrs->ns = true;
11269 sattrs->srvalid = true;
11270 sattrs->sregion = r;
11272 } else {
11274 * Address not in this region. We must check whether the
11275 * region covers addresses in the same page as our address.
11276 * In that case we must not report a size that covers the
11277 * whole page for a subsequent hit against a different MPU
11278 * region or the background region, because it would result
11279 * in incorrect TLB hits for subsequent accesses to
11280 * addresses that are in this MPU region.
11282 if (limit >= base &&
11283 ranges_overlap(base, limit - base + 1,
11284 addr_page_base,
11285 TARGET_PAGE_SIZE)) {
11286 sattrs->subpage = true;
11291 break;
11295 * The IDAU will override the SAU lookup results if it specifies
11296 * higher security than the SAU does.
11298 if (!idau_ns) {
11299 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
11300 sattrs->ns = false;
11301 sattrs->nsc = idau_nsc;
11306 static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
11307 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11308 hwaddr *phys_ptr, MemTxAttrs *txattrs,
11309 int *prot, bool *is_subpage,
11310 ARMMMUFaultInfo *fi, uint32_t *mregion)
11312 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
11313 * that a full phys-to-virt translation does).
11314 * mregion is (if not NULL) set to the region number which matched,
11315 * or -1 if no region number is returned (MPU off, address did not
11316 * hit a region, address hit in multiple regions).
11317 * We set is_subpage to true if the region hit doesn't cover the
11318 * entire TARGET_PAGE the address is within.
11320 ARMCPU *cpu = arm_env_get_cpu(env);
11321 bool is_user = regime_is_user(env, mmu_idx);
11322 uint32_t secure = regime_is_secure(env, mmu_idx);
11323 int n;
11324 int matchregion = -1;
11325 bool hit = false;
11326 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11327 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
11329 *is_subpage = false;
11330 *phys_ptr = address;
11331 *prot = 0;
11332 if (mregion) {
11333 *mregion = -1;
11336 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
11337 * was an exception vector read from the vector table (which is always
11338 * done using the default system address map), because those accesses
11339 * are done in arm_v7m_load_vector(), which always does a direct
11340 * read using address_space_ldl(), rather than going via this function.
11342 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
11343 hit = true;
11344 } else if (m_is_ppb_region(env, address)) {
11345 hit = true;
11346 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11347 hit = true;
11348 } else {
11349 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11350 /* region search */
11351 /* Note that the base address is bits [31:5] from the register
11352 * with bits [4:0] all zeroes, but the limit address is bits
11353 * [31:5] from the register with bits [4:0] all ones.
11355 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
11356 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
11358 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
11359 /* Region disabled */
11360 continue;
11363 if (address < base || address > limit) {
11365 * Address not in this region. We must check whether the
11366 * region covers addresses in the same page as our address.
11367 * In that case we must not report a size that covers the
11368 * whole page for a subsequent hit against a different MPU
11369 * region or the background region, because it would result in
11370 * incorrect TLB hits for subsequent accesses to addresses that
11371 * are in this MPU region.
11373 if (limit >= base &&
11374 ranges_overlap(base, limit - base + 1,
11375 addr_page_base,
11376 TARGET_PAGE_SIZE)) {
11377 *is_subpage = true;
11379 continue;
11382 if (base > addr_page_base || limit < addr_page_limit) {
11383 *is_subpage = true;
11386 if (hit) {
11387 /* Multiple regions match -- always a failure (unlike
11388 * PMSAv7 where highest-numbered-region wins)
11390 fi->type = ARMFault_Permission;
11391 fi->level = 1;
11392 return true;
11395 matchregion = n;
11396 hit = true;
11400 if (!hit) {
11401 /* background fault */
11402 fi->type = ARMFault_Background;
11403 return true;
11406 if (matchregion == -1) {
11407 /* hit using the background region */
11408 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11409 } else {
11410 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
11411 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
11413 if (m_is_system_region(env, address)) {
11414 /* System space is always execute never */
11415 xn = 1;
11418 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
11419 if (*prot && !xn) {
11420 *prot |= PAGE_EXEC;
11422 /* We don't need to look the attribute up in the MAIR0/MAIR1
11423 * registers because that only tells us about cacheability.
11425 if (mregion) {
11426 *mregion = matchregion;
11430 fi->type = ARMFault_Permission;
11431 fi->level = 1;
11432 return !(*prot & (1 << access_type));
11436 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
11437 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11438 hwaddr *phys_ptr, MemTxAttrs *txattrs,
11439 int *prot, target_ulong *page_size,
11440 ARMMMUFaultInfo *fi)
11442 uint32_t secure = regime_is_secure(env, mmu_idx);
11443 V8M_SAttributes sattrs = {};
11444 bool ret;
11445 bool mpu_is_subpage;
11447 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
11448 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
11449 if (access_type == MMU_INST_FETCH) {
11450 /* Instruction fetches always use the MMU bank and the
11451 * transaction attribute determined by the fetch address,
11452 * regardless of CPU state. This is painful for QEMU
11453 * to handle, because it would mean we need to encode
11454 * into the mmu_idx not just the (user, negpri) information
11455 * for the current security state but also that for the
11456 * other security state, which would balloon the number
11457 * of mmu_idx values needed alarmingly.
11458 * Fortunately we can avoid this because it's not actually
11459 * possible to arbitrarily execute code from memory with
11460 * the wrong security attribute: it will always generate
11461 * an exception of some kind or another, apart from the
11462 * special case of an NS CPU executing an SG instruction
11463 * in S&NSC memory. So we always just fail the translation
11464 * here and sort things out in the exception handler
11465 * (including possibly emulating an SG instruction).
11467 if (sattrs.ns != !secure) {
11468 if (sattrs.nsc) {
11469 fi->type = ARMFault_QEMU_NSCExec;
11470 } else {
11471 fi->type = ARMFault_QEMU_SFault;
11473 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
11474 *phys_ptr = address;
11475 *prot = 0;
11476 return true;
11478 } else {
11479 /* For data accesses we always use the MMU bank indicated
11480 * by the current CPU state, but the security attributes
11481 * might downgrade a secure access to nonsecure.
11483 if (sattrs.ns) {
11484 txattrs->secure = false;
11485 } else if (!secure) {
11486 /* NS access to S memory must fault.
11487 * Architecturally we should first check whether the
11488 * MPU information for this address indicates that we
11489 * are doing an unaligned access to Device memory, which
11490 * should generate a UsageFault instead. QEMU does not
11491 * currently check for that kind of unaligned access though.
11492 * If we added it we would need to do so as a special case
11493 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
11495 fi->type = ARMFault_QEMU_SFault;
11496 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
11497 *phys_ptr = address;
11498 *prot = 0;
11499 return true;
11504 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
11505 txattrs, prot, &mpu_is_subpage, fi, NULL);
11506 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
11507 return ret;
11510 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
11511 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11512 hwaddr *phys_ptr, int *prot,
11513 ARMMMUFaultInfo *fi)
11515 int n;
11516 uint32_t mask;
11517 uint32_t base;
11518 bool is_user = regime_is_user(env, mmu_idx);
11520 if (regime_translation_disabled(env, mmu_idx)) {
11521 /* MPU disabled. */
11522 *phys_ptr = address;
11523 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11524 return false;
11527 *phys_ptr = address;
11528 for (n = 7; n >= 0; n--) {
11529 base = env->cp15.c6_region[n];
11530 if ((base & 1) == 0) {
11531 continue;
11533 mask = 1 << ((base >> 1) & 0x1f);
11534 /* Keep this shift separate from the above to avoid an
11535 (undefined) << 32. */
11536 mask = (mask << 1) - 1;
11537 if (((base ^ address) & ~mask) == 0) {
11538 break;
11541 if (n < 0) {
11542 fi->type = ARMFault_Background;
11543 return true;
11546 if (access_type == MMU_INST_FETCH) {
11547 mask = env->cp15.pmsav5_insn_ap;
11548 } else {
11549 mask = env->cp15.pmsav5_data_ap;
11551 mask = (mask >> (n * 4)) & 0xf;
11552 switch (mask) {
11553 case 0:
11554 fi->type = ARMFault_Permission;
11555 fi->level = 1;
11556 return true;
11557 case 1:
11558 if (is_user) {
11559 fi->type = ARMFault_Permission;
11560 fi->level = 1;
11561 return true;
11563 *prot = PAGE_READ | PAGE_WRITE;
11564 break;
11565 case 2:
11566 *prot = PAGE_READ;
11567 if (!is_user) {
11568 *prot |= PAGE_WRITE;
11570 break;
11571 case 3:
11572 *prot = PAGE_READ | PAGE_WRITE;
11573 break;
11574 case 5:
11575 if (is_user) {
11576 fi->type = ARMFault_Permission;
11577 fi->level = 1;
11578 return true;
11580 *prot = PAGE_READ;
11581 break;
11582 case 6:
11583 *prot = PAGE_READ;
11584 break;
11585 default:
11586 /* Bad permission. */
11587 fi->type = ARMFault_Permission;
11588 fi->level = 1;
11589 return true;
11591 *prot |= PAGE_EXEC;
11592 return false;
11595 /* Combine either inner or outer cacheability attributes for normal
11596 * memory, according to table D4-42 and pseudocode procedure
11597 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
11599 * NB: only stage 1 includes allocation hints (RW bits), leading to
11600 * some asymmetry.
11602 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
11604 if (s1 == 4 || s2 == 4) {
11605 /* non-cacheable has precedence */
11606 return 4;
11607 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
11608 /* stage 1 write-through takes precedence */
11609 return s1;
11610 } else if (extract32(s2, 2, 2) == 2) {
11611 /* stage 2 write-through takes precedence, but the allocation hint
11612 * is still taken from stage 1
11614 return (2 << 2) | extract32(s1, 0, 2);
11615 } else { /* write-back */
11616 return s1;
11620 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
11621 * and CombineS1S2Desc()
11623 * @s1: Attributes from stage 1 walk
11624 * @s2: Attributes from stage 2 walk
11626 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
11628 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
11629 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
11630 ARMCacheAttrs ret;
11632 /* Combine shareability attributes (table D4-43) */
11633 if (s1.shareability == 2 || s2.shareability == 2) {
11634 /* if either are outer-shareable, the result is outer-shareable */
11635 ret.shareability = 2;
11636 } else if (s1.shareability == 3 || s2.shareability == 3) {
11637 /* if either are inner-shareable, the result is inner-shareable */
11638 ret.shareability = 3;
11639 } else {
11640 /* both non-shareable */
11641 ret.shareability = 0;
11644 /* Combine memory type and cacheability attributes */
11645 if (s1hi == 0 || s2hi == 0) {
11646 /* Device has precedence over normal */
11647 if (s1lo == 0 || s2lo == 0) {
11648 /* nGnRnE has precedence over anything */
11649 ret.attrs = 0;
11650 } else if (s1lo == 4 || s2lo == 4) {
11651 /* non-Reordering has precedence over Reordering */
11652 ret.attrs = 4; /* nGnRE */
11653 } else if (s1lo == 8 || s2lo == 8) {
11654 /* non-Gathering has precedence over Gathering */
11655 ret.attrs = 8; /* nGRE */
11656 } else {
11657 ret.attrs = 0xc; /* GRE */
11660 /* Any location for which the resultant memory type is any
11661 * type of Device memory is always treated as Outer Shareable.
11663 ret.shareability = 2;
11664 } else { /* Normal memory */
11665 /* Outer/inner cacheability combine independently */
11666 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
11667 | combine_cacheattr_nibble(s1lo, s2lo);
11669 if (ret.attrs == 0x44) {
11670 /* Any location for which the resultant memory type is Normal
11671 * Inner Non-cacheable, Outer Non-cacheable is always treated
11672 * as Outer Shareable.
11674 ret.shareability = 2;
11678 return ret;
11682 /* get_phys_addr - get the physical address for this virtual address
11684 * Find the physical address corresponding to the given virtual address,
11685 * by doing a translation table walk on MMU based systems or using the
11686 * MPU state on MPU based systems.
11688 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11689 * prot and page_size may not be filled in, and the populated fsr value provides
11690 * information on why the translation aborted, in the format of a
11691 * DFSR/IFSR fault register, with the following caveats:
11692 * * we honour the short vs long DFSR format differences.
11693 * * the WnR bit is never set (the caller must do this).
11694 * * for PSMAv5 based systems we don't bother to return a full FSR format
11695 * value.
11697 * @env: CPUARMState
11698 * @address: virtual address to get physical address for
11699 * @access_type: 0 for read, 1 for write, 2 for execute
11700 * @mmu_idx: MMU index indicating required translation regime
11701 * @phys_ptr: set to the physical address corresponding to the virtual address
11702 * @attrs: set to the memory transaction attributes to use
11703 * @prot: set to the permissions for the page containing phys_ptr
11704 * @page_size: set to the size of the page containing phys_ptr
11705 * @fi: set to fault info if the translation fails
11706 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
11708 static bool get_phys_addr(CPUARMState *env, target_ulong address,
11709 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11710 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
11711 target_ulong *page_size,
11712 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
11714 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
11715 /* Call ourselves recursively to do the stage 1 and then stage 2
11716 * translations.
11718 if (arm_feature(env, ARM_FEATURE_EL2)) {
11719 hwaddr ipa;
11720 int s2_prot;
11721 int ret;
11722 ARMCacheAttrs cacheattrs2 = {};
11724 ret = get_phys_addr(env, address, access_type,
11725 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
11726 prot, page_size, fi, cacheattrs);
11728 /* If S1 fails or S2 is disabled, return early. */
11729 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
11730 *phys_ptr = ipa;
11731 return ret;
11734 /* S1 is done. Now do S2 translation. */
11735 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
11736 phys_ptr, attrs, &s2_prot,
11737 page_size, fi,
11738 cacheattrs != NULL ? &cacheattrs2 : NULL);
11739 fi->s2addr = ipa;
11740 /* Combine the S1 and S2 perms. */
11741 *prot &= s2_prot;
11743 /* Combine the S1 and S2 cache attributes, if needed */
11744 if (!ret && cacheattrs != NULL) {
11745 if (env->cp15.hcr_el2 & HCR_DC) {
11747 * HCR.DC forces the first stage attributes to
11748 * Normal Non-Shareable,
11749 * Inner Write-Back Read-Allocate Write-Allocate,
11750 * Outer Write-Back Read-Allocate Write-Allocate.
11752 cacheattrs->attrs = 0xff;
11753 cacheattrs->shareability = 0;
11755 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
11758 return ret;
11759 } else {
11761 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
11763 mmu_idx = stage_1_mmu_idx(mmu_idx);
11767 /* The page table entries may downgrade secure to non-secure, but
11768 * cannot upgrade an non-secure translation regime's attributes
11769 * to secure.
11771 attrs->secure = regime_is_secure(env, mmu_idx);
11772 attrs->user = regime_is_user(env, mmu_idx);
11774 /* Fast Context Switch Extension. This doesn't exist at all in v8.
11775 * In v7 and earlier it affects all stage 1 translations.
11777 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
11778 && !arm_feature(env, ARM_FEATURE_V8)) {
11779 if (regime_el(env, mmu_idx) == 3) {
11780 address += env->cp15.fcseidr_s;
11781 } else {
11782 address += env->cp15.fcseidr_ns;
11786 if (arm_feature(env, ARM_FEATURE_PMSA)) {
11787 bool ret;
11788 *page_size = TARGET_PAGE_SIZE;
11790 if (arm_feature(env, ARM_FEATURE_V8)) {
11791 /* PMSAv8 */
11792 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
11793 phys_ptr, attrs, prot, page_size, fi);
11794 } else if (arm_feature(env, ARM_FEATURE_V7)) {
11795 /* PMSAv7 */
11796 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
11797 phys_ptr, prot, page_size, fi);
11798 } else {
11799 /* Pre-v7 MPU */
11800 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
11801 phys_ptr, prot, fi);
11803 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
11804 " mmu_idx %u -> %s (prot %c%c%c)\n",
11805 access_type == MMU_DATA_LOAD ? "reading" :
11806 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
11807 (uint32_t)address, mmu_idx,
11808 ret ? "Miss" : "Hit",
11809 *prot & PAGE_READ ? 'r' : '-',
11810 *prot & PAGE_WRITE ? 'w' : '-',
11811 *prot & PAGE_EXEC ? 'x' : '-');
11813 return ret;
11816 /* Definitely a real MMU, not an MPU */
11818 if (regime_translation_disabled(env, mmu_idx)) {
11819 /* MMU disabled. */
11820 *phys_ptr = address;
11821 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11822 *page_size = TARGET_PAGE_SIZE;
11823 return 0;
11826 if (regime_using_lpae_format(env, mmu_idx)) {
11827 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
11828 phys_ptr, attrs, prot, page_size,
11829 fi, cacheattrs);
11830 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
11831 return get_phys_addr_v6(env, address, access_type, mmu_idx,
11832 phys_ptr, attrs, prot, page_size, fi);
11833 } else {
11834 return get_phys_addr_v5(env, address, access_type, mmu_idx,
11835 phys_ptr, prot, page_size, fi);
11839 /* Walk the page table and (if the mapping exists) add the page
11840 * to the TLB. Return false on success, or true on failure. Populate
11841 * fsr with ARM DFSR/IFSR fault register format value on failure.
11843 bool arm_tlb_fill(CPUState *cs, vaddr address,
11844 MMUAccessType access_type, int mmu_idx,
11845 ARMMMUFaultInfo *fi)
11847 ARMCPU *cpu = ARM_CPU(cs);
11848 CPUARMState *env = &cpu->env;
11849 hwaddr phys_addr;
11850 target_ulong page_size;
11851 int prot;
11852 int ret;
11853 MemTxAttrs attrs = {};
11855 ret = get_phys_addr(env, address, access_type,
11856 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr,
11857 &attrs, &prot, &page_size, fi, NULL);
11858 if (!ret) {
11860 * Map a single [sub]page. Regions smaller than our declared
11861 * target page size are handled specially, so for those we
11862 * pass in the exact addresses.
11864 if (page_size >= TARGET_PAGE_SIZE) {
11865 phys_addr &= TARGET_PAGE_MASK;
11866 address &= TARGET_PAGE_MASK;
11868 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
11869 prot, mmu_idx, page_size);
11870 return 0;
11873 return ret;
11876 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
11877 MemTxAttrs *attrs)
11879 ARMCPU *cpu = ARM_CPU(cs);
11880 CPUARMState *env = &cpu->env;
11881 hwaddr phys_addr;
11882 target_ulong page_size;
11883 int prot;
11884 bool ret;
11885 ARMMMUFaultInfo fi = {};
11886 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
11888 *attrs = (MemTxAttrs) {};
11890 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
11891 attrs, &prot, &page_size, &fi, NULL);
11893 if (ret) {
11894 return -1;
11896 return phys_addr;
11899 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
11901 uint32_t mask;
11902 unsigned el = arm_current_el(env);
11904 /* First handle registers which unprivileged can read */
11906 switch (reg) {
11907 case 0 ... 7: /* xPSR sub-fields */
11908 mask = 0;
11909 if ((reg & 1) && el) {
11910 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */
11912 if (!(reg & 4)) {
11913 mask |= XPSR_NZCV | XPSR_Q; /* APSR */
11915 /* EPSR reads as zero */
11916 return xpsr_read(env) & mask;
11917 break;
11918 case 20: /* CONTROL */
11919 return env->v7m.control[env->v7m.secure];
11920 case 0x94: /* CONTROL_NS */
11921 /* We have to handle this here because unprivileged Secure code
11922 * can read the NS CONTROL register.
11924 if (!env->v7m.secure) {
11925 return 0;
11927 return env->v7m.control[M_REG_NS];
11930 if (el == 0) {
11931 return 0; /* unprivileged reads others as zero */
11934 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
11935 switch (reg) {
11936 case 0x88: /* MSP_NS */
11937 if (!env->v7m.secure) {
11938 return 0;
11940 return env->v7m.other_ss_msp;
11941 case 0x89: /* PSP_NS */
11942 if (!env->v7m.secure) {
11943 return 0;
11945 return env->v7m.other_ss_psp;
11946 case 0x8a: /* MSPLIM_NS */
11947 if (!env->v7m.secure) {
11948 return 0;
11950 return env->v7m.msplim[M_REG_NS];
11951 case 0x8b: /* PSPLIM_NS */
11952 if (!env->v7m.secure) {
11953 return 0;
11955 return env->v7m.psplim[M_REG_NS];
11956 case 0x90: /* PRIMASK_NS */
11957 if (!env->v7m.secure) {
11958 return 0;
11960 return env->v7m.primask[M_REG_NS];
11961 case 0x91: /* BASEPRI_NS */
11962 if (!env->v7m.secure) {
11963 return 0;
11965 return env->v7m.basepri[M_REG_NS];
11966 case 0x93: /* FAULTMASK_NS */
11967 if (!env->v7m.secure) {
11968 return 0;
11970 return env->v7m.faultmask[M_REG_NS];
11971 case 0x98: /* SP_NS */
11973 /* This gives the non-secure SP selected based on whether we're
11974 * currently in handler mode or not, using the NS CONTROL.SPSEL.
11976 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
11978 if (!env->v7m.secure) {
11979 return 0;
11981 if (!arm_v7m_is_handler_mode(env) && spsel) {
11982 return env->v7m.other_ss_psp;
11983 } else {
11984 return env->v7m.other_ss_msp;
11987 default:
11988 break;
11992 switch (reg) {
11993 case 8: /* MSP */
11994 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13];
11995 case 9: /* PSP */
11996 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp;
11997 case 10: /* MSPLIM */
11998 if (!arm_feature(env, ARM_FEATURE_V8)) {
11999 goto bad_reg;
12001 return env->v7m.msplim[env->v7m.secure];
12002 case 11: /* PSPLIM */
12003 if (!arm_feature(env, ARM_FEATURE_V8)) {
12004 goto bad_reg;
12006 return env->v7m.psplim[env->v7m.secure];
12007 case 16: /* PRIMASK */
12008 return env->v7m.primask[env->v7m.secure];
12009 case 17: /* BASEPRI */
12010 case 18: /* BASEPRI_MAX */
12011 return env->v7m.basepri[env->v7m.secure];
12012 case 19: /* FAULTMASK */
12013 return env->v7m.faultmask[env->v7m.secure];
12014 default:
12015 bad_reg:
12016 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special"
12017 " register %d\n", reg);
12018 return 0;
12022 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val)
12024 /* We're passed bits [11..0] of the instruction; extract
12025 * SYSm and the mask bits.
12026 * Invalid combinations of SYSm and mask are UNPREDICTABLE;
12027 * we choose to treat them as if the mask bits were valid.
12028 * NB that the pseudocode 'mask' variable is bits [11..10],
12029 * whereas ours is [11..8].
12031 uint32_t mask = extract32(maskreg, 8, 4);
12032 uint32_t reg = extract32(maskreg, 0, 8);
12034 if (arm_current_el(env) == 0 && reg > 7) {
12035 /* only xPSR sub-fields may be written by unprivileged */
12036 return;
12039 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
12040 switch (reg) {
12041 case 0x88: /* MSP_NS */
12042 if (!env->v7m.secure) {
12043 return;
12045 env->v7m.other_ss_msp = val;
12046 return;
12047 case 0x89: /* PSP_NS */
12048 if (!env->v7m.secure) {
12049 return;
12051 env->v7m.other_ss_psp = val;
12052 return;
12053 case 0x8a: /* MSPLIM_NS */
12054 if (!env->v7m.secure) {
12055 return;
12057 env->v7m.msplim[M_REG_NS] = val & ~7;
12058 return;
12059 case 0x8b: /* PSPLIM_NS */
12060 if (!env->v7m.secure) {
12061 return;
12063 env->v7m.psplim[M_REG_NS] = val & ~7;
12064 return;
12065 case 0x90: /* PRIMASK_NS */
12066 if (!env->v7m.secure) {
12067 return;
12069 env->v7m.primask[M_REG_NS] = val & 1;
12070 return;
12071 case 0x91: /* BASEPRI_NS */
12072 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) {
12073 return;
12075 env->v7m.basepri[M_REG_NS] = val & 0xff;
12076 return;
12077 case 0x93: /* FAULTMASK_NS */
12078 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) {
12079 return;
12081 env->v7m.faultmask[M_REG_NS] = val & 1;
12082 return;
12083 case 0x94: /* CONTROL_NS */
12084 if (!env->v7m.secure) {
12085 return;
12087 write_v7m_control_spsel_for_secstate(env,
12088 val & R_V7M_CONTROL_SPSEL_MASK,
12089 M_REG_NS);
12090 if (arm_feature(env, ARM_FEATURE_M_MAIN)) {
12091 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK;
12092 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK;
12094 return;
12095 case 0x98: /* SP_NS */
12097 /* This gives the non-secure SP selected based on whether we're
12098 * currently in handler mode or not, using the NS CONTROL.SPSEL.
12100 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK;
12101 bool is_psp = !arm_v7m_is_handler_mode(env) && spsel;
12102 uint32_t limit;
12104 if (!env->v7m.secure) {
12105 return;
12108 limit = is_psp ? env->v7m.psplim[false] : env->v7m.msplim[false];
12110 if (val < limit) {
12111 CPUState *cs = CPU(arm_env_get_cpu(env));
12113 cpu_restore_state(cs, GETPC(), true);
12114 raise_exception(env, EXCP_STKOF, 0, 1);
12117 if (is_psp) {
12118 env->v7m.other_ss_psp = val;
12119 } else {
12120 env->v7m.other_ss_msp = val;
12122 return;
12124 default:
12125 break;
12129 switch (reg) {
12130 case 0 ... 7: /* xPSR sub-fields */
12131 /* only APSR is actually writable */
12132 if (!(reg & 4)) {
12133 uint32_t apsrmask = 0;
12135 if (mask & 8) {
12136 apsrmask |= XPSR_NZCV | XPSR_Q;
12138 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) {
12139 apsrmask |= XPSR_GE;
12141 xpsr_write(env, val, apsrmask);
12143 break;
12144 case 8: /* MSP */
12145 if (v7m_using_psp(env)) {
12146 env->v7m.other_sp = val;
12147 } else {
12148 env->regs[13] = val;
12150 break;
12151 case 9: /* PSP */
12152 if (v7m_using_psp(env)) {
12153 env->regs[13] = val;
12154 } else {
12155 env->v7m.other_sp = val;
12157 break;
12158 case 10: /* MSPLIM */
12159 if (!arm_feature(env, ARM_FEATURE_V8)) {
12160 goto bad_reg;
12162 env->v7m.msplim[env->v7m.secure] = val & ~7;
12163 break;
12164 case 11: /* PSPLIM */
12165 if (!arm_feature(env, ARM_FEATURE_V8)) {
12166 goto bad_reg;
12168 env->v7m.psplim[env->v7m.secure] = val & ~7;
12169 break;
12170 case 16: /* PRIMASK */
12171 env->v7m.primask[env->v7m.secure] = val & 1;
12172 break;
12173 case 17: /* BASEPRI */
12174 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
12175 goto bad_reg;
12177 env->v7m.basepri[env->v7m.secure] = val & 0xff;
12178 break;
12179 case 18: /* BASEPRI_MAX */
12180 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
12181 goto bad_reg;
12183 val &= 0xff;
12184 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure]
12185 || env->v7m.basepri[env->v7m.secure] == 0)) {
12186 env->v7m.basepri[env->v7m.secure] = val;
12188 break;
12189 case 19: /* FAULTMASK */
12190 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) {
12191 goto bad_reg;
12193 env->v7m.faultmask[env->v7m.secure] = val & 1;
12194 break;
12195 case 20: /* CONTROL */
12196 /* Writing to the SPSEL bit only has an effect if we are in
12197 * thread mode; other bits can be updated by any privileged code.
12198 * write_v7m_control_spsel() deals with updating the SPSEL bit in
12199 * env->v7m.control, so we only need update the others.
12200 * For v7M, we must just ignore explicit writes to SPSEL in handler
12201 * mode; for v8M the write is permitted but will have no effect.
12203 if (arm_feature(env, ARM_FEATURE_V8) ||
12204 !arm_v7m_is_handler_mode(env)) {
12205 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0);
12207 if (arm_feature(env, ARM_FEATURE_M_MAIN)) {
12208 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK;
12209 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK;
12211 break;
12212 default:
12213 bad_reg:
12214 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special"
12215 " register %d\n", reg);
12216 return;
12220 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op)
12222 /* Implement the TT instruction. op is bits [7:6] of the insn. */
12223 bool forceunpriv = op & 1;
12224 bool alt = op & 2;
12225 V8M_SAttributes sattrs = {};
12226 uint32_t tt_resp;
12227 bool r, rw, nsr, nsrw, mrvalid;
12228 int prot;
12229 ARMMMUFaultInfo fi = {};
12230 MemTxAttrs attrs = {};
12231 hwaddr phys_addr;
12232 ARMMMUIdx mmu_idx;
12233 uint32_t mregion;
12234 bool targetpriv;
12235 bool targetsec = env->v7m.secure;
12236 bool is_subpage;
12238 /* Work out what the security state and privilege level we're
12239 * interested in is...
12241 if (alt) {
12242 targetsec = !targetsec;
12245 if (forceunpriv) {
12246 targetpriv = false;
12247 } else {
12248 targetpriv = arm_v7m_is_handler_mode(env) ||
12249 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK);
12252 /* ...and then figure out which MMU index this is */
12253 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv);
12255 /* We know that the MPU and SAU don't care about the access type
12256 * for our purposes beyond that we don't want to claim to be
12257 * an insn fetch, so we arbitrarily call this a read.
12260 /* MPU region info only available for privileged or if
12261 * inspecting the other MPU state.
12263 if (arm_current_el(env) != 0 || alt) {
12264 /* We can ignore the return value as prot is always set */
12265 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx,
12266 &phys_addr, &attrs, &prot, &is_subpage,
12267 &fi, &mregion);
12268 if (mregion == -1) {
12269 mrvalid = false;
12270 mregion = 0;
12271 } else {
12272 mrvalid = true;
12274 r = prot & PAGE_READ;
12275 rw = prot & PAGE_WRITE;
12276 } else {
12277 r = false;
12278 rw = false;
12279 mrvalid = false;
12280 mregion = 0;
12283 if (env->v7m.secure) {
12284 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs);
12285 nsr = sattrs.ns && r;
12286 nsrw = sattrs.ns && rw;
12287 } else {
12288 sattrs.ns = true;
12289 nsr = false;
12290 nsrw = false;
12293 tt_resp = (sattrs.iregion << 24) |
12294 (sattrs.irvalid << 23) |
12295 ((!sattrs.ns) << 22) |
12296 (nsrw << 21) |
12297 (nsr << 20) |
12298 (rw << 19) |
12299 (r << 18) |
12300 (sattrs.srvalid << 17) |
12301 (mrvalid << 16) |
12302 (sattrs.sregion << 8) |
12303 mregion;
12305 return tt_resp;
12308 #endif
12310 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
12312 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
12313 * Note that we do not implement the (architecturally mandated)
12314 * alignment fault for attempts to use this on Device memory
12315 * (which matches the usual QEMU behaviour of not implementing either
12316 * alignment faults or any memory attribute handling).
12319 ARMCPU *cpu = arm_env_get_cpu(env);
12320 uint64_t blocklen = 4 << cpu->dcz_blocksize;
12321 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
12323 #ifndef CONFIG_USER_ONLY
12325 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
12326 * the block size so we might have to do more than one TLB lookup.
12327 * We know that in fact for any v8 CPU the page size is at least 4K
12328 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
12329 * 1K as an artefact of legacy v5 subpage support being present in the
12330 * same QEMU executable.
12332 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
12333 void *hostaddr[maxidx];
12334 int try, i;
12335 unsigned mmu_idx = cpu_mmu_index(env, false);
12336 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
12338 for (try = 0; try < 2; try++) {
12340 for (i = 0; i < maxidx; i++) {
12341 hostaddr[i] = tlb_vaddr_to_host(env,
12342 vaddr + TARGET_PAGE_SIZE * i,
12343 1, mmu_idx);
12344 if (!hostaddr[i]) {
12345 break;
12348 if (i == maxidx) {
12349 /* If it's all in the TLB it's fair game for just writing to;
12350 * we know we don't need to update dirty status, etc.
12352 for (i = 0; i < maxidx - 1; i++) {
12353 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
12355 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
12356 return;
12358 /* OK, try a store and see if we can populate the tlb. This
12359 * might cause an exception if the memory isn't writable,
12360 * in which case we will longjmp out of here. We must for
12361 * this purpose use the actual register value passed to us
12362 * so that we get the fault address right.
12364 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC());
12365 /* Now we can populate the other TLB entries, if any */
12366 for (i = 0; i < maxidx; i++) {
12367 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
12368 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
12369 helper_ret_stb_mmu(env, va, 0, oi, GETPC());
12374 /* Slow path (probably attempt to do this to an I/O device or
12375 * similar, or clearing of a block of code we have translations
12376 * cached for). Just do a series of byte writes as the architecture
12377 * demands. It's not worth trying to use a cpu_physical_memory_map(),
12378 * memset(), unmap() sequence here because:
12379 * + we'd need to account for the blocksize being larger than a page
12380 * + the direct-RAM access case is almost always going to be dealt
12381 * with in the fastpath code above, so there's no speed benefit
12382 * + we would have to deal with the map returning NULL because the
12383 * bounce buffer was in use
12385 for (i = 0; i < blocklen; i++) {
12386 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC());
12389 #else
12390 memset(g2h(vaddr), 0, blocklen);
12391 #endif
12394 /* Note that signed overflow is undefined in C. The following routines are
12395 careful to use unsigned types where modulo arithmetic is required.
12396 Failure to do so _will_ break on newer gcc. */
12398 /* Signed saturating arithmetic. */
12400 /* Perform 16-bit signed saturating addition. */
12401 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12403 uint16_t res;
12405 res = a + b;
12406 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12407 if (a & 0x8000)
12408 res = 0x8000;
12409 else
12410 res = 0x7fff;
12412 return res;
12415 /* Perform 8-bit signed saturating addition. */
12416 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12418 uint8_t res;
12420 res = a + b;
12421 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12422 if (a & 0x80)
12423 res = 0x80;
12424 else
12425 res = 0x7f;
12427 return res;
12430 /* Perform 16-bit signed saturating subtraction. */
12431 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12433 uint16_t res;
12435 res = a - b;
12436 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12437 if (a & 0x8000)
12438 res = 0x8000;
12439 else
12440 res = 0x7fff;
12442 return res;
12445 /* Perform 8-bit signed saturating subtraction. */
12446 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12448 uint8_t res;
12450 res = a - b;
12451 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12452 if (a & 0x80)
12453 res = 0x80;
12454 else
12455 res = 0x7f;
12457 return res;
12460 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12461 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12462 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12463 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12464 #define PFX q
12466 #include "op_addsub.h"
12468 /* Unsigned saturating arithmetic. */
12469 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
12471 uint16_t res;
12472 res = a + b;
12473 if (res < a)
12474 res = 0xffff;
12475 return res;
12478 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
12480 if (a > b)
12481 return a - b;
12482 else
12483 return 0;
12486 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12488 uint8_t res;
12489 res = a + b;
12490 if (res < a)
12491 res = 0xff;
12492 return res;
12495 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12497 if (a > b)
12498 return a - b;
12499 else
12500 return 0;
12503 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12504 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12505 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12506 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12507 #define PFX uq
12509 #include "op_addsub.h"
12511 /* Signed modulo arithmetic. */
12512 #define SARITH16(a, b, n, op) do { \
12513 int32_t sum; \
12514 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
12515 RESULT(sum, n, 16); \
12516 if (sum >= 0) \
12517 ge |= 3 << (n * 2); \
12518 } while(0)
12520 #define SARITH8(a, b, n, op) do { \
12521 int32_t sum; \
12522 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
12523 RESULT(sum, n, 8); \
12524 if (sum >= 0) \
12525 ge |= 1 << n; \
12526 } while(0)
12529 #define ADD16(a, b, n) SARITH16(a, b, n, +)
12530 #define SUB16(a, b, n) SARITH16(a, b, n, -)
12531 #define ADD8(a, b, n) SARITH8(a, b, n, +)
12532 #define SUB8(a, b, n) SARITH8(a, b, n, -)
12533 #define PFX s
12534 #define ARITH_GE
12536 #include "op_addsub.h"
12538 /* Unsigned modulo arithmetic. */
12539 #define ADD16(a, b, n) do { \
12540 uint32_t sum; \
12541 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12542 RESULT(sum, n, 16); \
12543 if ((sum >> 16) == 1) \
12544 ge |= 3 << (n * 2); \
12545 } while(0)
12547 #define ADD8(a, b, n) do { \
12548 uint32_t sum; \
12549 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12550 RESULT(sum, n, 8); \
12551 if ((sum >> 8) == 1) \
12552 ge |= 1 << n; \
12553 } while(0)
12555 #define SUB16(a, b, n) do { \
12556 uint32_t sum; \
12557 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12558 RESULT(sum, n, 16); \
12559 if ((sum >> 16) == 0) \
12560 ge |= 3 << (n * 2); \
12561 } while(0)
12563 #define SUB8(a, b, n) do { \
12564 uint32_t sum; \
12565 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12566 RESULT(sum, n, 8); \
12567 if ((sum >> 8) == 0) \
12568 ge |= 1 << n; \
12569 } while(0)
12571 #define PFX u
12572 #define ARITH_GE
12574 #include "op_addsub.h"
12576 /* Halved signed arithmetic. */
12577 #define ADD16(a, b, n) \
12578 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12579 #define SUB16(a, b, n) \
12580 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12581 #define ADD8(a, b, n) \
12582 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12583 #define SUB8(a, b, n) \
12584 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12585 #define PFX sh
12587 #include "op_addsub.h"
12589 /* Halved unsigned arithmetic. */
12590 #define ADD16(a, b, n) \
12591 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12592 #define SUB16(a, b, n) \
12593 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12594 #define ADD8(a, b, n) \
12595 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12596 #define SUB8(a, b, n) \
12597 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12598 #define PFX uh
12600 #include "op_addsub.h"
12602 static inline uint8_t do_usad(uint8_t a, uint8_t b)
12604 if (a > b)
12605 return a - b;
12606 else
12607 return b - a;
12610 /* Unsigned sum of absolute byte differences. */
12611 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12613 uint32_t sum;
12614 sum = do_usad(a, b);
12615 sum += do_usad(a >> 8, b >> 8);
12616 sum += do_usad(a >> 16, b >>16);
12617 sum += do_usad(a >> 24, b >> 24);
12618 return sum;
12621 /* For ARMv6 SEL instruction. */
12622 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12624 uint32_t mask;
12626 mask = 0;
12627 if (flags & 1)
12628 mask |= 0xff;
12629 if (flags & 2)
12630 mask |= 0xff00;
12631 if (flags & 4)
12632 mask |= 0xff0000;
12633 if (flags & 8)
12634 mask |= 0xff000000;
12635 return (a & mask) | (b & ~mask);
12638 /* VFP support. We follow the convention used for VFP instructions:
12639 Single precision routines have a "s" suffix, double precision a
12640 "d" suffix. */
12642 /* Convert host exception flags to vfp form. */
12643 static inline int vfp_exceptbits_from_host(int host_bits)
12645 int target_bits = 0;
12647 if (host_bits & float_flag_invalid)
12648 target_bits |= 1;
12649 if (host_bits & float_flag_divbyzero)
12650 target_bits |= 2;
12651 if (host_bits & float_flag_overflow)
12652 target_bits |= 4;
12653 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
12654 target_bits |= 8;
12655 if (host_bits & float_flag_inexact)
12656 target_bits |= 0x10;
12657 if (host_bits & float_flag_input_denormal)
12658 target_bits |= 0x80;
12659 return target_bits;
12662 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
12664 int i;
12665 uint32_t fpscr;
12667 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
12668 | (env->vfp.vec_len << 16)
12669 | (env->vfp.vec_stride << 20);
12671 i = get_float_exception_flags(&env->vfp.fp_status);
12672 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
12673 /* FZ16 does not generate an input denormal exception. */
12674 i |= (get_float_exception_flags(&env->vfp.fp_status_f16)
12675 & ~float_flag_input_denormal);
12677 fpscr |= vfp_exceptbits_from_host(i);
12678 return fpscr;
12681 uint32_t vfp_get_fpscr(CPUARMState *env)
12683 return HELPER(vfp_get_fpscr)(env);
12686 /* Convert vfp exception flags to target form. */
12687 static inline int vfp_exceptbits_to_host(int target_bits)
12689 int host_bits = 0;
12691 if (target_bits & 1)
12692 host_bits |= float_flag_invalid;
12693 if (target_bits & 2)
12694 host_bits |= float_flag_divbyzero;
12695 if (target_bits & 4)
12696 host_bits |= float_flag_overflow;
12697 if (target_bits & 8)
12698 host_bits |= float_flag_underflow;
12699 if (target_bits & 0x10)
12700 host_bits |= float_flag_inexact;
12701 if (target_bits & 0x80)
12702 host_bits |= float_flag_input_denormal;
12703 return host_bits;
12706 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
12708 int i;
12709 uint32_t changed;
12711 /* When ARMv8.2-FP16 is not supported, FZ16 is RES0. */
12712 if (!cpu_isar_feature(aa64_fp16, arm_env_get_cpu(env))) {
12713 val &= ~FPCR_FZ16;
12717 * We don't implement trapped exception handling, so the
12718 * trap enable bits are all RAZ/WI (not RES0!)
12720 val &= ~(FPCR_IDE | FPCR_IXE | FPCR_UFE | FPCR_OFE | FPCR_DZE | FPCR_IOE);
12722 changed = env->vfp.xregs[ARM_VFP_FPSCR];
12723 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
12724 env->vfp.vec_len = (val >> 16) & 7;
12725 env->vfp.vec_stride = (val >> 20) & 3;
12727 changed ^= val;
12728 if (changed & (3 << 22)) {
12729 i = (val >> 22) & 3;
12730 switch (i) {
12731 case FPROUNDING_TIEEVEN:
12732 i = float_round_nearest_even;
12733 break;
12734 case FPROUNDING_POSINF:
12735 i = float_round_up;
12736 break;
12737 case FPROUNDING_NEGINF:
12738 i = float_round_down;
12739 break;
12740 case FPROUNDING_ZERO:
12741 i = float_round_to_zero;
12742 break;
12744 set_float_rounding_mode(i, &env->vfp.fp_status);
12745 set_float_rounding_mode(i, &env->vfp.fp_status_f16);
12747 if (changed & FPCR_FZ16) {
12748 bool ftz_enabled = val & FPCR_FZ16;
12749 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
12750 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
12752 if (changed & FPCR_FZ) {
12753 bool ftz_enabled = val & FPCR_FZ;
12754 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status);
12755 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status);
12757 if (changed & FPCR_DN) {
12758 bool dnan_enabled = val & FPCR_DN;
12759 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status);
12760 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status_f16);
12763 /* The exception flags are ORed together when we read fpscr so we
12764 * only need to preserve the current state in one of our
12765 * float_status values.
12767 i = vfp_exceptbits_to_host(val);
12768 set_float_exception_flags(i, &env->vfp.fp_status);
12769 set_float_exception_flags(0, &env->vfp.fp_status_f16);
12770 set_float_exception_flags(0, &env->vfp.standard_fp_status);
12773 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
12775 HELPER(vfp_set_fpscr)(env, val);
12778 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
12780 #define VFP_BINOP(name) \
12781 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
12783 float_status *fpst = fpstp; \
12784 return float32_ ## name(a, b, fpst); \
12786 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
12788 float_status *fpst = fpstp; \
12789 return float64_ ## name(a, b, fpst); \
12791 VFP_BINOP(add)
12792 VFP_BINOP(sub)
12793 VFP_BINOP(mul)
12794 VFP_BINOP(div)
12795 VFP_BINOP(min)
12796 VFP_BINOP(max)
12797 VFP_BINOP(minnum)
12798 VFP_BINOP(maxnum)
12799 #undef VFP_BINOP
12801 float32 VFP_HELPER(neg, s)(float32 a)
12803 return float32_chs(a);
12806 float64 VFP_HELPER(neg, d)(float64 a)
12808 return float64_chs(a);
12811 float32 VFP_HELPER(abs, s)(float32 a)
12813 return float32_abs(a);
12816 float64 VFP_HELPER(abs, d)(float64 a)
12818 return float64_abs(a);
12821 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
12823 return float32_sqrt(a, &env->vfp.fp_status);
12826 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
12828 return float64_sqrt(a, &env->vfp.fp_status);
12831 /* XXX: check quiet/signaling case */
12832 #define DO_VFP_cmp(p, type) \
12833 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
12835 uint32_t flags; \
12836 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
12837 case 0: flags = 0x6; break; \
12838 case -1: flags = 0x8; break; \
12839 case 1: flags = 0x2; break; \
12840 default: case 2: flags = 0x3; break; \
12842 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
12843 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
12845 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
12847 uint32_t flags; \
12848 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
12849 case 0: flags = 0x6; break; \
12850 case -1: flags = 0x8; break; \
12851 case 1: flags = 0x2; break; \
12852 default: case 2: flags = 0x3; break; \
12854 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
12855 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
12857 DO_VFP_cmp(s, float32)
12858 DO_VFP_cmp(d, float64)
12859 #undef DO_VFP_cmp
12861 /* Integer to float and float to integer conversions */
12863 #define CONV_ITOF(name, ftype, fsz, sign) \
12864 ftype HELPER(name)(uint32_t x, void *fpstp) \
12866 float_status *fpst = fpstp; \
12867 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
12870 #define CONV_FTOI(name, ftype, fsz, sign, round) \
12871 sign##int32_t HELPER(name)(ftype x, void *fpstp) \
12873 float_status *fpst = fpstp; \
12874 if (float##fsz##_is_any_nan(x)) { \
12875 float_raise(float_flag_invalid, fpst); \
12876 return 0; \
12878 return float##fsz##_to_##sign##int32##round(x, fpst); \
12881 #define FLOAT_CONVS(name, p, ftype, fsz, sign) \
12882 CONV_ITOF(vfp_##name##to##p, ftype, fsz, sign) \
12883 CONV_FTOI(vfp_to##name##p, ftype, fsz, sign, ) \
12884 CONV_FTOI(vfp_to##name##z##p, ftype, fsz, sign, _round_to_zero)
12886 FLOAT_CONVS(si, h, uint32_t, 16, )
12887 FLOAT_CONVS(si, s, float32, 32, )
12888 FLOAT_CONVS(si, d, float64, 64, )
12889 FLOAT_CONVS(ui, h, uint32_t, 16, u)
12890 FLOAT_CONVS(ui, s, float32, 32, u)
12891 FLOAT_CONVS(ui, d, float64, 64, u)
12893 #undef CONV_ITOF
12894 #undef CONV_FTOI
12895 #undef FLOAT_CONVS
12897 /* floating point conversion */
12898 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
12900 return float32_to_float64(x, &env->vfp.fp_status);
12903 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
12905 return float64_to_float32(x, &env->vfp.fp_status);
12908 /* VFP3 fixed point conversion. */
12909 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
12910 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
12911 void *fpstp) \
12912 { return itype##_to_##float##fsz##_scalbn(x, -shift, fpstp); }
12914 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, ROUND, suff) \
12915 uint##isz##_t HELPER(vfp_to##name##p##suff)(float##fsz x, uint32_t shift, \
12916 void *fpst) \
12918 if (unlikely(float##fsz##_is_any_nan(x))) { \
12919 float_raise(float_flag_invalid, fpst); \
12920 return 0; \
12922 return float##fsz##_to_##itype##_scalbn(x, ROUND, shift, fpst); \
12925 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \
12926 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
12927 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, \
12928 float_round_to_zero, _round_to_zero) \
12929 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, \
12930 get_float_rounding_mode(fpst), )
12932 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
12933 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
12934 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, \
12935 get_float_rounding_mode(fpst), )
12937 VFP_CONV_FIX(sh, d, 64, 64, int16)
12938 VFP_CONV_FIX(sl, d, 64, 64, int32)
12939 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
12940 VFP_CONV_FIX(uh, d, 64, 64, uint16)
12941 VFP_CONV_FIX(ul, d, 64, 64, uint32)
12942 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
12943 VFP_CONV_FIX(sh, s, 32, 32, int16)
12944 VFP_CONV_FIX(sl, s, 32, 32, int32)
12945 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
12946 VFP_CONV_FIX(uh, s, 32, 32, uint16)
12947 VFP_CONV_FIX(ul, s, 32, 32, uint32)
12948 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
12950 #undef VFP_CONV_FIX
12951 #undef VFP_CONV_FIX_FLOAT
12952 #undef VFP_CONV_FLOAT_FIX_ROUND
12953 #undef VFP_CONV_FIX_A64
12955 uint32_t HELPER(vfp_sltoh)(uint32_t x, uint32_t shift, void *fpst)
12957 return int32_to_float16_scalbn(x, -shift, fpst);
12960 uint32_t HELPER(vfp_ultoh)(uint32_t x, uint32_t shift, void *fpst)
12962 return uint32_to_float16_scalbn(x, -shift, fpst);
12965 uint32_t HELPER(vfp_sqtoh)(uint64_t x, uint32_t shift, void *fpst)
12967 return int64_to_float16_scalbn(x, -shift, fpst);
12970 uint32_t HELPER(vfp_uqtoh)(uint64_t x, uint32_t shift, void *fpst)
12972 return uint64_to_float16_scalbn(x, -shift, fpst);
12975 uint32_t HELPER(vfp_toshh)(uint32_t x, uint32_t shift, void *fpst)
12977 if (unlikely(float16_is_any_nan(x))) {
12978 float_raise(float_flag_invalid, fpst);
12979 return 0;
12981 return float16_to_int16_scalbn(x, get_float_rounding_mode(fpst),
12982 shift, fpst);
12985 uint32_t HELPER(vfp_touhh)(uint32_t x, uint32_t shift, void *fpst)
12987 if (unlikely(float16_is_any_nan(x))) {
12988 float_raise(float_flag_invalid, fpst);
12989 return 0;
12991 return float16_to_uint16_scalbn(x, get_float_rounding_mode(fpst),
12992 shift, fpst);
12995 uint32_t HELPER(vfp_toslh)(uint32_t x, uint32_t shift, void *fpst)
12997 if (unlikely(float16_is_any_nan(x))) {
12998 float_raise(float_flag_invalid, fpst);
12999 return 0;
13001 return float16_to_int32_scalbn(x, get_float_rounding_mode(fpst),
13002 shift, fpst);
13005 uint32_t HELPER(vfp_toulh)(uint32_t x, uint32_t shift, void *fpst)
13007 if (unlikely(float16_is_any_nan(x))) {
13008 float_raise(float_flag_invalid, fpst);
13009 return 0;
13011 return float16_to_uint32_scalbn(x, get_float_rounding_mode(fpst),
13012 shift, fpst);
13015 uint64_t HELPER(vfp_tosqh)(uint32_t x, uint32_t shift, void *fpst)
13017 if (unlikely(float16_is_any_nan(x))) {
13018 float_raise(float_flag_invalid, fpst);
13019 return 0;
13021 return float16_to_int64_scalbn(x, get_float_rounding_mode(fpst),
13022 shift, fpst);
13025 uint64_t HELPER(vfp_touqh)(uint32_t x, uint32_t shift, void *fpst)
13027 if (unlikely(float16_is_any_nan(x))) {
13028 float_raise(float_flag_invalid, fpst);
13029 return 0;
13031 return float16_to_uint64_scalbn(x, get_float_rounding_mode(fpst),
13032 shift, fpst);
13035 /* Set the current fp rounding mode and return the old one.
13036 * The argument is a softfloat float_round_ value.
13038 uint32_t HELPER(set_rmode)(uint32_t rmode, void *fpstp)
13040 float_status *fp_status = fpstp;
13042 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
13043 set_float_rounding_mode(rmode, fp_status);
13045 return prev_rmode;
13048 /* Set the current fp rounding mode in the standard fp status and return
13049 * the old one. This is for NEON instructions that need to change the
13050 * rounding mode but wish to use the standard FPSCR values for everything
13051 * else. Always set the rounding mode back to the correct value after
13052 * modifying it.
13053 * The argument is a softfloat float_round_ value.
13055 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
13057 float_status *fp_status = &env->vfp.standard_fp_status;
13059 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
13060 set_float_rounding_mode(rmode, fp_status);
13062 return prev_rmode;
13065 /* Half precision conversions. */
13066 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, void *fpstp, uint32_t ahp_mode)
13068 /* Squash FZ16 to 0 for the duration of conversion. In this case,
13069 * it would affect flushing input denormals.
13071 float_status *fpst = fpstp;
13072 flag save = get_flush_inputs_to_zero(fpst);
13073 set_flush_inputs_to_zero(false, fpst);
13074 float32 r = float16_to_float32(a, !ahp_mode, fpst);
13075 set_flush_inputs_to_zero(save, fpst);
13076 return r;
13079 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, void *fpstp, uint32_t ahp_mode)
13081 /* Squash FZ16 to 0 for the duration of conversion. In this case,
13082 * it would affect flushing output denormals.
13084 float_status *fpst = fpstp;
13085 flag save = get_flush_to_zero(fpst);
13086 set_flush_to_zero(false, fpst);
13087 float16 r = float32_to_float16(a, !ahp_mode, fpst);
13088 set_flush_to_zero(save, fpst);
13089 return r;
13092 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, void *fpstp, uint32_t ahp_mode)
13094 /* Squash FZ16 to 0 for the duration of conversion. In this case,
13095 * it would affect flushing input denormals.
13097 float_status *fpst = fpstp;
13098 flag save = get_flush_inputs_to_zero(fpst);
13099 set_flush_inputs_to_zero(false, fpst);
13100 float64 r = float16_to_float64(a, !ahp_mode, fpst);
13101 set_flush_inputs_to_zero(save, fpst);
13102 return r;
13105 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, void *fpstp, uint32_t ahp_mode)
13107 /* Squash FZ16 to 0 for the duration of conversion. In this case,
13108 * it would affect flushing output denormals.
13110 float_status *fpst = fpstp;
13111 flag save = get_flush_to_zero(fpst);
13112 set_flush_to_zero(false, fpst);
13113 float16 r = float64_to_float16(a, !ahp_mode, fpst);
13114 set_flush_to_zero(save, fpst);
13115 return r;
13118 #define float32_two make_float32(0x40000000)
13119 #define float32_three make_float32(0x40400000)
13120 #define float32_one_point_five make_float32(0x3fc00000)
13122 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
13124 float_status *s = &env->vfp.standard_fp_status;
13125 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
13126 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
13127 if (!(float32_is_zero(a) || float32_is_zero(b))) {
13128 float_raise(float_flag_input_denormal, s);
13130 return float32_two;
13132 return float32_sub(float32_two, float32_mul(a, b, s), s);
13135 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
13137 float_status *s = &env->vfp.standard_fp_status;
13138 float32 product;
13139 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
13140 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
13141 if (!(float32_is_zero(a) || float32_is_zero(b))) {
13142 float_raise(float_flag_input_denormal, s);
13144 return float32_one_point_five;
13146 product = float32_mul(a, b, s);
13147 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
13150 /* NEON helpers. */
13152 /* Constants 256 and 512 are used in some helpers; we avoid relying on
13153 * int->float conversions at run-time. */
13154 #define float64_256 make_float64(0x4070000000000000LL)
13155 #define float64_512 make_float64(0x4080000000000000LL)
13156 #define float16_maxnorm make_float16(0x7bff)
13157 #define float32_maxnorm make_float32(0x7f7fffff)
13158 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
13160 /* Reciprocal functions
13162 * The algorithm that must be used to calculate the estimate
13163 * is specified by the ARM ARM, see FPRecipEstimate()/RecipEstimate
13166 /* See RecipEstimate()
13168 * input is a 9 bit fixed point number
13169 * input range 256 .. 511 for a number from 0.5 <= x < 1.0.
13170 * result range 256 .. 511 for a number from 1.0 to 511/256.
13173 static int recip_estimate(int input)
13175 int a, b, r;
13176 assert(256 <= input && input < 512);
13177 a = (input * 2) + 1;
13178 b = (1 << 19) / a;
13179 r = (b + 1) >> 1;
13180 assert(256 <= r && r < 512);
13181 return r;
13185 * Common wrapper to call recip_estimate
13187 * The parameters are exponent and 64 bit fraction (without implicit
13188 * bit) where the binary point is nominally at bit 52. Returns a
13189 * float64 which can then be rounded to the appropriate size by the
13190 * callee.
13193 static uint64_t call_recip_estimate(int *exp, int exp_off, uint64_t frac)
13195 uint32_t scaled, estimate;
13196 uint64_t result_frac;
13197 int result_exp;
13199 /* Handle sub-normals */
13200 if (*exp == 0) {
13201 if (extract64(frac, 51, 1) == 0) {
13202 *exp = -1;
13203 frac <<= 2;
13204 } else {
13205 frac <<= 1;
13209 /* scaled = UInt('1':fraction<51:44>) */
13210 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
13211 estimate = recip_estimate(scaled);
13213 result_exp = exp_off - *exp;
13214 result_frac = deposit64(0, 44, 8, estimate);
13215 if (result_exp == 0) {
13216 result_frac = deposit64(result_frac >> 1, 51, 1, 1);
13217 } else if (result_exp == -1) {
13218 result_frac = deposit64(result_frac >> 2, 50, 2, 1);
13219 result_exp = 0;
13222 *exp = result_exp;
13224 return result_frac;
13227 static bool round_to_inf(float_status *fpst, bool sign_bit)
13229 switch (fpst->float_rounding_mode) {
13230 case float_round_nearest_even: /* Round to Nearest */
13231 return true;
13232 case float_round_up: /* Round to +Inf */
13233 return !sign_bit;
13234 case float_round_down: /* Round to -Inf */
13235 return sign_bit;
13236 case float_round_to_zero: /* Round to Zero */
13237 return false;
13240 g_assert_not_reached();
13243 uint32_t HELPER(recpe_f16)(uint32_t input, void *fpstp)
13245 float_status *fpst = fpstp;
13246 float16 f16 = float16_squash_input_denormal(input, fpst);
13247 uint32_t f16_val = float16_val(f16);
13248 uint32_t f16_sign = float16_is_neg(f16);
13249 int f16_exp = extract32(f16_val, 10, 5);
13250 uint32_t f16_frac = extract32(f16_val, 0, 10);
13251 uint64_t f64_frac;
13253 if (float16_is_any_nan(f16)) {
13254 float16 nan = f16;
13255 if (float16_is_signaling_nan(f16, fpst)) {
13256 float_raise(float_flag_invalid, fpst);
13257 nan = float16_silence_nan(f16, fpst);
13259 if (fpst->default_nan_mode) {
13260 nan = float16_default_nan(fpst);
13262 return nan;
13263 } else if (float16_is_infinity(f16)) {
13264 return float16_set_sign(float16_zero, float16_is_neg(f16));
13265 } else if (float16_is_zero(f16)) {
13266 float_raise(float_flag_divbyzero, fpst);
13267 return float16_set_sign(float16_infinity, float16_is_neg(f16));
13268 } else if (float16_abs(f16) < (1 << 8)) {
13269 /* Abs(value) < 2.0^-16 */
13270 float_raise(float_flag_overflow | float_flag_inexact, fpst);
13271 if (round_to_inf(fpst, f16_sign)) {
13272 return float16_set_sign(float16_infinity, f16_sign);
13273 } else {
13274 return float16_set_sign(float16_maxnorm, f16_sign);
13276 } else if (f16_exp >= 29 && fpst->flush_to_zero) {
13277 float_raise(float_flag_underflow, fpst);
13278 return float16_set_sign(float16_zero, float16_is_neg(f16));
13281 f64_frac = call_recip_estimate(&f16_exp, 29,
13282 ((uint64_t) f16_frac) << (52 - 10));
13284 /* result = sign : result_exp<4:0> : fraction<51:42> */
13285 f16_val = deposit32(0, 15, 1, f16_sign);
13286 f16_val = deposit32(f16_val, 10, 5, f16_exp);
13287 f16_val = deposit32(f16_val, 0, 10, extract64(f64_frac, 52 - 10, 10));
13288 return make_float16(f16_val);
13291 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
13293 float_status *fpst = fpstp;
13294 float32 f32 = float32_squash_input_denormal(input, fpst);
13295 uint32_t f32_val = float32_val(f32);
13296 bool f32_sign = float32_is_neg(f32);
13297 int f32_exp = extract32(f32_val, 23, 8);
13298 uint32_t f32_frac = extract32(f32_val, 0, 23);
13299 uint64_t f64_frac;
13301 if (float32_is_any_nan(f32)) {
13302 float32 nan = f32;
13303 if (float32_is_signaling_nan(f32, fpst)) {
13304 float_raise(float_flag_invalid, fpst);
13305 nan = float32_silence_nan(f32, fpst);
13307 if (fpst->default_nan_mode) {
13308 nan = float32_default_nan(fpst);
13310 return nan;
13311 } else if (float32_is_infinity(f32)) {
13312 return float32_set_sign(float32_zero, float32_is_neg(f32));
13313 } else if (float32_is_zero(f32)) {
13314 float_raise(float_flag_divbyzero, fpst);
13315 return float32_set_sign(float32_infinity, float32_is_neg(f32));
13316 } else if (float32_abs(f32) < (1ULL << 21)) {
13317 /* Abs(value) < 2.0^-128 */
13318 float_raise(float_flag_overflow | float_flag_inexact, fpst);
13319 if (round_to_inf(fpst, f32_sign)) {
13320 return float32_set_sign(float32_infinity, f32_sign);
13321 } else {
13322 return float32_set_sign(float32_maxnorm, f32_sign);
13324 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
13325 float_raise(float_flag_underflow, fpst);
13326 return float32_set_sign(float32_zero, float32_is_neg(f32));
13329 f64_frac = call_recip_estimate(&f32_exp, 253,
13330 ((uint64_t) f32_frac) << (52 - 23));
13332 /* result = sign : result_exp<7:0> : fraction<51:29> */
13333 f32_val = deposit32(0, 31, 1, f32_sign);
13334 f32_val = deposit32(f32_val, 23, 8, f32_exp);
13335 f32_val = deposit32(f32_val, 0, 23, extract64(f64_frac, 52 - 23, 23));
13336 return make_float32(f32_val);
13339 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
13341 float_status *fpst = fpstp;
13342 float64 f64 = float64_squash_input_denormal(input, fpst);
13343 uint64_t f64_val = float64_val(f64);
13344 bool f64_sign = float64_is_neg(f64);
13345 int f64_exp = extract64(f64_val, 52, 11);
13346 uint64_t f64_frac = extract64(f64_val, 0, 52);
13348 /* Deal with any special cases */
13349 if (float64_is_any_nan(f64)) {
13350 float64 nan = f64;
13351 if (float64_is_signaling_nan(f64, fpst)) {
13352 float_raise(float_flag_invalid, fpst);
13353 nan = float64_silence_nan(f64, fpst);
13355 if (fpst->default_nan_mode) {
13356 nan = float64_default_nan(fpst);
13358 return nan;
13359 } else if (float64_is_infinity(f64)) {
13360 return float64_set_sign(float64_zero, float64_is_neg(f64));
13361 } else if (float64_is_zero(f64)) {
13362 float_raise(float_flag_divbyzero, fpst);
13363 return float64_set_sign(float64_infinity, float64_is_neg(f64));
13364 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
13365 /* Abs(value) < 2.0^-1024 */
13366 float_raise(float_flag_overflow | float_flag_inexact, fpst);
13367 if (round_to_inf(fpst, f64_sign)) {
13368 return float64_set_sign(float64_infinity, f64_sign);
13369 } else {
13370 return float64_set_sign(float64_maxnorm, f64_sign);
13372 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
13373 float_raise(float_flag_underflow, fpst);
13374 return float64_set_sign(float64_zero, float64_is_neg(f64));
13377 f64_frac = call_recip_estimate(&f64_exp, 2045, f64_frac);
13379 /* result = sign : result_exp<10:0> : fraction<51:0>; */
13380 f64_val = deposit64(0, 63, 1, f64_sign);
13381 f64_val = deposit64(f64_val, 52, 11, f64_exp);
13382 f64_val = deposit64(f64_val, 0, 52, f64_frac);
13383 return make_float64(f64_val);
13386 /* The algorithm that must be used to calculate the estimate
13387 * is specified by the ARM ARM.
13390 static int do_recip_sqrt_estimate(int a)
13392 int b, estimate;
13394 assert(128 <= a && a < 512);
13395 if (a < 256) {
13396 a = a * 2 + 1;
13397 } else {
13398 a = (a >> 1) << 1;
13399 a = (a + 1) * 2;
13401 b = 512;
13402 while (a * (b + 1) * (b + 1) < (1 << 28)) {
13403 b += 1;
13405 estimate = (b + 1) / 2;
13406 assert(256 <= estimate && estimate < 512);
13408 return estimate;
13412 static uint64_t recip_sqrt_estimate(int *exp , int exp_off, uint64_t frac)
13414 int estimate;
13415 uint32_t scaled;
13417 if (*exp == 0) {
13418 while (extract64(frac, 51, 1) == 0) {
13419 frac = frac << 1;
13420 *exp -= 1;
13422 frac = extract64(frac, 0, 51) << 1;
13425 if (*exp & 1) {
13426 /* scaled = UInt('01':fraction<51:45>) */
13427 scaled = deposit32(1 << 7, 0, 7, extract64(frac, 45, 7));
13428 } else {
13429 /* scaled = UInt('1':fraction<51:44>) */
13430 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
13432 estimate = do_recip_sqrt_estimate(scaled);
13434 *exp = (exp_off - *exp) / 2;
13435 return extract64(estimate, 0, 8) << 44;
13438 uint32_t HELPER(rsqrte_f16)(uint32_t input, void *fpstp)
13440 float_status *s = fpstp;
13441 float16 f16 = float16_squash_input_denormal(input, s);
13442 uint16_t val = float16_val(f16);
13443 bool f16_sign = float16_is_neg(f16);
13444 int f16_exp = extract32(val, 10, 5);
13445 uint16_t f16_frac = extract32(val, 0, 10);
13446 uint64_t f64_frac;
13448 if (float16_is_any_nan(f16)) {
13449 float16 nan = f16;
13450 if (float16_is_signaling_nan(f16, s)) {
13451 float_raise(float_flag_invalid, s);
13452 nan = float16_silence_nan(f16, s);
13454 if (s->default_nan_mode) {
13455 nan = float16_default_nan(s);
13457 return nan;
13458 } else if (float16_is_zero(f16)) {
13459 float_raise(float_flag_divbyzero, s);
13460 return float16_set_sign(float16_infinity, f16_sign);
13461 } else if (f16_sign) {
13462 float_raise(float_flag_invalid, s);
13463 return float16_default_nan(s);
13464 } else if (float16_is_infinity(f16)) {
13465 return float16_zero;
13468 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
13469 * preserving the parity of the exponent. */
13471 f64_frac = ((uint64_t) f16_frac) << (52 - 10);
13473 f64_frac = recip_sqrt_estimate(&f16_exp, 44, f64_frac);
13475 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(2) */
13476 val = deposit32(0, 15, 1, f16_sign);
13477 val = deposit32(val, 10, 5, f16_exp);
13478 val = deposit32(val, 2, 8, extract64(f64_frac, 52 - 8, 8));
13479 return make_float16(val);
13482 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
13484 float_status *s = fpstp;
13485 float32 f32 = float32_squash_input_denormal(input, s);
13486 uint32_t val = float32_val(f32);
13487 uint32_t f32_sign = float32_is_neg(f32);
13488 int f32_exp = extract32(val, 23, 8);
13489 uint32_t f32_frac = extract32(val, 0, 23);
13490 uint64_t f64_frac;
13492 if (float32_is_any_nan(f32)) {
13493 float32 nan = f32;
13494 if (float32_is_signaling_nan(f32, s)) {
13495 float_raise(float_flag_invalid, s);
13496 nan = float32_silence_nan(f32, s);
13498 if (s->default_nan_mode) {
13499 nan = float32_default_nan(s);
13501 return nan;
13502 } else if (float32_is_zero(f32)) {
13503 float_raise(float_flag_divbyzero, s);
13504 return float32_set_sign(float32_infinity, float32_is_neg(f32));
13505 } else if (float32_is_neg(f32)) {
13506 float_raise(float_flag_invalid, s);
13507 return float32_default_nan(s);
13508 } else if (float32_is_infinity(f32)) {
13509 return float32_zero;
13512 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
13513 * preserving the parity of the exponent. */
13515 f64_frac = ((uint64_t) f32_frac) << 29;
13517 f64_frac = recip_sqrt_estimate(&f32_exp, 380, f64_frac);
13519 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(15) */
13520 val = deposit32(0, 31, 1, f32_sign);
13521 val = deposit32(val, 23, 8, f32_exp);
13522 val = deposit32(val, 15, 8, extract64(f64_frac, 52 - 8, 8));
13523 return make_float32(val);
13526 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
13528 float_status *s = fpstp;
13529 float64 f64 = float64_squash_input_denormal(input, s);
13530 uint64_t val = float64_val(f64);
13531 bool f64_sign = float64_is_neg(f64);
13532 int f64_exp = extract64(val, 52, 11);
13533 uint64_t f64_frac = extract64(val, 0, 52);
13535 if (float64_is_any_nan(f64)) {
13536 float64 nan = f64;
13537 if (float64_is_signaling_nan(f64, s)) {
13538 float_raise(float_flag_invalid, s);
13539 nan = float64_silence_nan(f64, s);
13541 if (s->default_nan_mode) {
13542 nan = float64_default_nan(s);
13544 return nan;
13545 } else if (float64_is_zero(f64)) {
13546 float_raise(float_flag_divbyzero, s);
13547 return float64_set_sign(float64_infinity, float64_is_neg(f64));
13548 } else if (float64_is_neg(f64)) {
13549 float_raise(float_flag_invalid, s);
13550 return float64_default_nan(s);
13551 } else if (float64_is_infinity(f64)) {
13552 return float64_zero;
13555 f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac);
13557 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */
13558 val = deposit64(0, 61, 1, f64_sign);
13559 val = deposit64(val, 52, 11, f64_exp);
13560 val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
13561 return make_float64(val);
13564 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
13566 /* float_status *s = fpstp; */
13567 int input, estimate;
13569 if ((a & 0x80000000) == 0) {
13570 return 0xffffffff;
13573 input = extract32(a, 23, 9);
13574 estimate = recip_estimate(input);
13576 return deposit32(0, (32 - 9), 9, estimate);
13579 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
13581 int estimate;
13583 if ((a & 0xc0000000) == 0) {
13584 return 0xffffffff;
13587 estimate = do_recip_sqrt_estimate(extract32(a, 23, 9));
13589 return deposit32(0, 23, 9, estimate);
13592 /* VFPv4 fused multiply-accumulate */
13593 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
13595 float_status *fpst = fpstp;
13596 return float32_muladd(a, b, c, 0, fpst);
13599 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
13601 float_status *fpst = fpstp;
13602 return float64_muladd(a, b, c, 0, fpst);
13605 /* ARMv8 round to integral */
13606 float32 HELPER(rints_exact)(float32 x, void *fp_status)
13608 return float32_round_to_int(x, fp_status);
13611 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
13613 return float64_round_to_int(x, fp_status);
13616 float32 HELPER(rints)(float32 x, void *fp_status)
13618 int old_flags = get_float_exception_flags(fp_status), new_flags;
13619 float32 ret;
13621 ret = float32_round_to_int(x, fp_status);
13623 /* Suppress any inexact exceptions the conversion produced */
13624 if (!(old_flags & float_flag_inexact)) {
13625 new_flags = get_float_exception_flags(fp_status);
13626 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
13629 return ret;
13632 float64 HELPER(rintd)(float64 x, void *fp_status)
13634 int old_flags = get_float_exception_flags(fp_status), new_flags;
13635 float64 ret;
13637 ret = float64_round_to_int(x, fp_status);
13639 new_flags = get_float_exception_flags(fp_status);
13641 /* Suppress any inexact exceptions the conversion produced */
13642 if (!(old_flags & float_flag_inexact)) {
13643 new_flags = get_float_exception_flags(fp_status);
13644 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
13647 return ret;
13650 /* Convert ARM rounding mode to softfloat */
13651 int arm_rmode_to_sf(int rmode)
13653 switch (rmode) {
13654 case FPROUNDING_TIEAWAY:
13655 rmode = float_round_ties_away;
13656 break;
13657 case FPROUNDING_ODD:
13658 /* FIXME: add support for TIEAWAY and ODD */
13659 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
13660 rmode);
13661 /* fall through for now */
13662 case FPROUNDING_TIEEVEN:
13663 default:
13664 rmode = float_round_nearest_even;
13665 break;
13666 case FPROUNDING_POSINF:
13667 rmode = float_round_up;
13668 break;
13669 case FPROUNDING_NEGINF:
13670 rmode = float_round_down;
13671 break;
13672 case FPROUNDING_ZERO:
13673 rmode = float_round_to_zero;
13674 break;
13676 return rmode;
13679 /* CRC helpers.
13680 * The upper bytes of val (above the number specified by 'bytes') must have
13681 * been zeroed out by the caller.
13683 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
13685 uint8_t buf[4];
13687 stl_le_p(buf, val);
13689 /* zlib crc32 converts the accumulator and output to one's complement. */
13690 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
13693 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
13695 uint8_t buf[4];
13697 stl_le_p(buf, val);
13699 /* Linux crc32c converts the output to one's complement. */
13700 return crc32c(acc, buf, bytes) ^ 0xffffffff;
13703 /* Return the exception level to which FP-disabled exceptions should
13704 * be taken, or 0 if FP is enabled.
13706 int fp_exception_el(CPUARMState *env, int cur_el)
13708 #ifndef CONFIG_USER_ONLY
13709 int fpen;
13711 /* CPACR and the CPTR registers don't exist before v6, so FP is
13712 * always accessible
13714 if (!arm_feature(env, ARM_FEATURE_V6)) {
13715 return 0;
13718 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
13719 * 0, 2 : trap EL0 and EL1/PL1 accesses
13720 * 1 : trap only EL0 accesses
13721 * 3 : trap no accesses
13723 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
13724 switch (fpen) {
13725 case 0:
13726 case 2:
13727 if (cur_el == 0 || cur_el == 1) {
13728 /* Trap to PL1, which might be EL1 or EL3 */
13729 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
13730 return 3;
13732 return 1;
13734 if (cur_el == 3 && !is_a64(env)) {
13735 /* Secure PL1 running at EL3 */
13736 return 3;
13738 break;
13739 case 1:
13740 if (cur_el == 0) {
13741 return 1;
13743 break;
13744 case 3:
13745 break;
13748 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
13749 * check because zero bits in the registers mean "don't trap".
13752 /* CPTR_EL2 : present in v7VE or v8 */
13753 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
13754 && !arm_is_secure_below_el3(env)) {
13755 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
13756 return 2;
13759 /* CPTR_EL3 : present in v8 */
13760 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
13761 /* Trap all FP ops to EL3 */
13762 return 3;
13764 #endif
13765 return 0;
13768 ARMMMUIdx arm_v7m_mmu_idx_for_secstate_and_priv(CPUARMState *env,
13769 bool secstate, bool priv)
13771 ARMMMUIdx mmu_idx = ARM_MMU_IDX_M;
13773 if (priv) {
13774 mmu_idx |= ARM_MMU_IDX_M_PRIV;
13777 if (armv7m_nvic_neg_prio_requested(env->nvic, secstate)) {
13778 mmu_idx |= ARM_MMU_IDX_M_NEGPRI;
13781 if (secstate) {
13782 mmu_idx |= ARM_MMU_IDX_M_S;
13785 return mmu_idx;
13788 /* Return the MMU index for a v7M CPU in the specified security state */
13789 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
13791 bool priv = arm_current_el(env) != 0;
13793 return arm_v7m_mmu_idx_for_secstate_and_priv(env, secstate, priv);
13796 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
13798 int el;
13800 if (arm_feature(env, ARM_FEATURE_M)) {
13801 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
13804 el = arm_current_el(env);
13805 if (el < 2 && arm_is_secure_below_el3(env)) {
13806 return ARMMMUIdx_S1SE0 + el;
13807 } else {
13808 return ARMMMUIdx_S12NSE0 + el;
13812 int cpu_mmu_index(CPUARMState *env, bool ifetch)
13814 return arm_to_core_mmu_idx(arm_mmu_idx(env));
13817 #ifndef CONFIG_USER_ONLY
13818 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
13820 return stage_1_mmu_idx(arm_mmu_idx(env));
13822 #endif
13824 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
13825 target_ulong *cs_base, uint32_t *pflags)
13827 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
13828 int current_el = arm_current_el(env);
13829 int fp_el = fp_exception_el(env, current_el);
13830 uint32_t flags = 0;
13832 if (is_a64(env)) {
13833 ARMCPU *cpu = arm_env_get_cpu(env);
13834 uint64_t sctlr;
13836 *pc = env->pc;
13837 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
13839 /* Get control bits for tagged addresses. */
13841 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
13842 ARMVAParameters p0 = aa64_va_parameters_both(env, 0, stage1);
13843 int tbii, tbid;
13845 /* FIXME: ARMv8.1-VHE S2 translation regime. */
13846 if (regime_el(env, stage1) < 2) {
13847 ARMVAParameters p1 = aa64_va_parameters_both(env, -1, stage1);
13848 tbid = (p1.tbi << 1) | p0.tbi;
13849 tbii = tbid & ~((p1.tbid << 1) | p0.tbid);
13850 } else {
13851 tbid = p0.tbi;
13852 tbii = tbid & !p0.tbid;
13855 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
13856 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
13859 if (cpu_isar_feature(aa64_sve, cpu)) {
13860 int sve_el = sve_exception_el(env, current_el);
13861 uint32_t zcr_len;
13863 /* If SVE is disabled, but FP is enabled,
13864 * then the effective len is 0.
13866 if (sve_el != 0 && fp_el == 0) {
13867 zcr_len = 0;
13868 } else {
13869 zcr_len = sve_zcr_len_for_el(env, current_el);
13871 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
13872 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
13875 if (current_el == 0) {
13876 /* FIXME: ARMv8.1-VHE S2 translation regime. */
13877 sctlr = env->cp15.sctlr_el[1];
13878 } else {
13879 sctlr = env->cp15.sctlr_el[current_el];
13881 if (cpu_isar_feature(aa64_pauth, cpu)) {
13883 * In order to save space in flags, we record only whether
13884 * pauth is "inactive", meaning all insns are implemented as
13885 * a nop, or "active" when some action must be performed.
13886 * The decision of which action to take is left to a helper.
13888 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
13889 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
13893 if (cpu_isar_feature(aa64_bti, cpu)) {
13894 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
13895 if (sctlr & (current_el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
13896 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
13898 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
13900 } else {
13901 *pc = env->regs[15];
13902 flags = FIELD_DP32(flags, TBFLAG_A32, THUMB, env->thumb);
13903 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN, env->vfp.vec_len);
13904 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE, env->vfp.vec_stride);
13905 flags = FIELD_DP32(flags, TBFLAG_A32, CONDEXEC, env->condexec_bits);
13906 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, arm_sctlr_b(env));
13907 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
13908 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)
13909 || arm_el_is_aa64(env, 1)) {
13910 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
13912 flags = FIELD_DP32(flags, TBFLAG_A32, XSCALE_CPAR, env->cp15.c15_cpar);
13915 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
13917 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
13918 * states defined in the ARM ARM for software singlestep:
13919 * SS_ACTIVE PSTATE.SS State
13920 * 0 x Inactive (the TB flag for SS is always 0)
13921 * 1 0 Active-pending
13922 * 1 1 Active-not-pending
13924 if (arm_singlestep_active(env)) {
13925 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
13926 if (is_a64(env)) {
13927 if (env->pstate & PSTATE_SS) {
13928 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
13930 } else {
13931 if (env->uncached_cpsr & PSTATE_SS) {
13932 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
13936 if (arm_cpu_data_is_big_endian(env)) {
13937 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
13939 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
13941 if (arm_v7m_is_handler_mode(env)) {
13942 flags = FIELD_DP32(flags, TBFLAG_A32, HANDLER, 1);
13945 /* v8M always applies stack limit checks unless CCR.STKOFHFNMIGN is
13946 * suppressing them because the requested execution priority is less than 0.
13948 if (arm_feature(env, ARM_FEATURE_V8) &&
13949 arm_feature(env, ARM_FEATURE_M) &&
13950 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
13951 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
13952 flags = FIELD_DP32(flags, TBFLAG_A32, STACKCHECK, 1);
13955 *pflags = flags;
13956 *cs_base = 0;
13959 #ifdef TARGET_AARCH64
13961 * The manual says that when SVE is enabled and VQ is widened the
13962 * implementation is allowed to zero the previously inaccessible
13963 * portion of the registers. The corollary to that is that when
13964 * SVE is enabled and VQ is narrowed we are also allowed to zero
13965 * the now inaccessible portion of the registers.
13967 * The intent of this is that no predicate bit beyond VQ is ever set.
13968 * Which means that some operations on predicate registers themselves
13969 * may operate on full uint64_t or even unrolled across the maximum
13970 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
13971 * may well be cheaper than conditionals to restrict the operation
13972 * to the relevant portion of a uint16_t[16].
13974 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
13976 int i, j;
13977 uint64_t pmask;
13979 assert(vq >= 1 && vq <= ARM_MAX_VQ);
13980 assert(vq <= arm_env_get_cpu(env)->sve_max_vq);
13982 /* Zap the high bits of the zregs. */
13983 for (i = 0; i < 32; i++) {
13984 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
13987 /* Zap the high bits of the pregs and ffr. */
13988 pmask = 0;
13989 if (vq & 3) {
13990 pmask = ~(-1ULL << (16 * (vq & 3)));
13992 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13993 for (i = 0; i < 17; ++i) {
13994 env->vfp.pregs[i].p[j] &= pmask;
13996 pmask = 0;
14001 * Notice a change in SVE vector size when changing EL.
14003 void aarch64_sve_change_el(CPUARMState *env, int old_el,
14004 int new_el, bool el0_a64)
14006 ARMCPU *cpu = arm_env_get_cpu(env);
14007 int old_len, new_len;
14008 bool old_a64, new_a64;
14010 /* Nothing to do if no SVE. */
14011 if (!cpu_isar_feature(aa64_sve, cpu)) {
14012 return;
14015 /* Nothing to do if FP is disabled in either EL. */
14016 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
14017 return;
14021 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
14022 * at ELx, or not available because the EL is in AArch32 state, then
14023 * for all purposes other than a direct read, the ZCR_ELx.LEN field
14024 * has an effective value of 0".
14026 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
14027 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
14028 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
14029 * we already have the correct register contents when encountering the
14030 * vq0->vq0 transition between EL0->EL1.
14032 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
14033 old_len = (old_a64 && !sve_exception_el(env, old_el)
14034 ? sve_zcr_len_for_el(env, old_el) : 0);
14035 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
14036 new_len = (new_a64 && !sve_exception_el(env, new_el)
14037 ? sve_zcr_len_for_el(env, new_el) : 0);
14039 /* When changing vector length, clear inaccessible state. */
14040 if (new_len < old_len) {
14041 aarch64_sve_narrow_vq(env, new_len + 1);
14044 #endif