target/arm/helper: zcr: Add build bug next to value range assumption
[qemu/ar7.git] / target / arm / helper.c
blob2fd504ea7a1134d1f2b29ba93d7900f514b6e835
1 /*
2 * ARM generic helpers.
4 * This code is licensed under the GNU GPL v2 or later.
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8 #include "qemu/osdep.h"
9 #include "qemu/units.h"
10 #include "target/arm/idau.h"
11 #include "trace.h"
12 #include "cpu.h"
13 #include "internals.h"
14 #include "exec/gdbstub.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/host-utils.h"
17 #include "sysemu/sysemu.h"
18 #include "qemu/bitops.h"
19 #include "qemu/crc32c.h"
20 #include "qemu/qemu-print.h"
21 #include "exec/exec-all.h"
22 #include <zlib.h> /* For crc32 */
23 #include "hw/semihosting/semihost.h"
24 #include "sysemu/cpus.h"
25 #include "sysemu/kvm.h"
26 #include "qemu/range.h"
27 #include "qapi/qapi-commands-machine-target.h"
28 #include "qapi/error.h"
29 #include "qemu/guest-random.h"
30 #ifdef CONFIG_TCG
31 #include "arm_ldst.h"
32 #include "exec/cpu_ldst.h"
33 #endif
35 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
37 #ifndef CONFIG_USER_ONLY
39 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
40 MMUAccessType access_type, ARMMMUIdx mmu_idx,
41 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
42 target_ulong *page_size_ptr,
43 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
44 #endif
46 static void switch_mode(CPUARMState *env, int mode);
48 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
50 int nregs;
52 /* VFP data registers are always little-endian. */
53 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
54 if (reg < nregs) {
55 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
56 return 8;
58 if (arm_feature(env, ARM_FEATURE_NEON)) {
59 /* Aliases for Q regs. */
60 nregs += 16;
61 if (reg < nregs) {
62 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
63 stq_le_p(buf, q[0]);
64 stq_le_p(buf + 8, q[1]);
65 return 16;
68 switch (reg - nregs) {
69 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
70 case 1: stl_p(buf, vfp_get_fpscr(env)); return 4;
71 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
73 return 0;
76 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
78 int nregs;
80 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
81 if (reg < nregs) {
82 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
83 return 8;
85 if (arm_feature(env, ARM_FEATURE_NEON)) {
86 nregs += 16;
87 if (reg < nregs) {
88 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
89 q[0] = ldq_le_p(buf);
90 q[1] = ldq_le_p(buf + 8);
91 return 16;
94 switch (reg - nregs) {
95 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
96 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4;
97 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
99 return 0;
102 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
104 switch (reg) {
105 case 0 ... 31:
106 /* 128 bit FP register */
108 uint64_t *q = aa64_vfp_qreg(env, reg);
109 stq_le_p(buf, q[0]);
110 stq_le_p(buf + 8, q[1]);
111 return 16;
113 case 32:
114 /* FPSR */
115 stl_p(buf, vfp_get_fpsr(env));
116 return 4;
117 case 33:
118 /* FPCR */
119 stl_p(buf, vfp_get_fpcr(env));
120 return 4;
121 default:
122 return 0;
126 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
128 switch (reg) {
129 case 0 ... 31:
130 /* 128 bit FP register */
132 uint64_t *q = aa64_vfp_qreg(env, reg);
133 q[0] = ldq_le_p(buf);
134 q[1] = ldq_le_p(buf + 8);
135 return 16;
137 case 32:
138 /* FPSR */
139 vfp_set_fpsr(env, ldl_p(buf));
140 return 4;
141 case 33:
142 /* FPCR */
143 vfp_set_fpcr(env, ldl_p(buf));
144 return 4;
145 default:
146 return 0;
150 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
152 assert(ri->fieldoffset);
153 if (cpreg_field_is_64bit(ri)) {
154 return CPREG_FIELD64(env, ri);
155 } else {
156 return CPREG_FIELD32(env, ri);
160 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
161 uint64_t value)
163 assert(ri->fieldoffset);
164 if (cpreg_field_is_64bit(ri)) {
165 CPREG_FIELD64(env, ri) = value;
166 } else {
167 CPREG_FIELD32(env, ri) = value;
171 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
173 return (char *)env + ri->fieldoffset;
176 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
178 /* Raw read of a coprocessor register (as needed for migration, etc). */
179 if (ri->type & ARM_CP_CONST) {
180 return ri->resetvalue;
181 } else if (ri->raw_readfn) {
182 return ri->raw_readfn(env, ri);
183 } else if (ri->readfn) {
184 return ri->readfn(env, ri);
185 } else {
186 return raw_read(env, ri);
190 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
191 uint64_t v)
193 /* Raw write of a coprocessor register (as needed for migration, etc).
194 * Note that constant registers are treated as write-ignored; the
195 * caller should check for success by whether a readback gives the
196 * value written.
198 if (ri->type & ARM_CP_CONST) {
199 return;
200 } else if (ri->raw_writefn) {
201 ri->raw_writefn(env, ri, v);
202 } else if (ri->writefn) {
203 ri->writefn(env, ri, v);
204 } else {
205 raw_write(env, ri, v);
209 static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg)
211 ARMCPU *cpu = env_archcpu(env);
212 const ARMCPRegInfo *ri;
213 uint32_t key;
215 key = cpu->dyn_xml.cpregs_keys[reg];
216 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
217 if (ri) {
218 if (cpreg_field_is_64bit(ri)) {
219 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
220 } else {
221 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
224 return 0;
227 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
229 return 0;
232 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
234 /* Return true if the regdef would cause an assertion if you called
235 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
236 * program bug for it not to have the NO_RAW flag).
237 * NB that returning false here doesn't necessarily mean that calling
238 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
239 * read/write access functions which are safe for raw use" from "has
240 * read/write access functions which have side effects but has forgotten
241 * to provide raw access functions".
242 * The tests here line up with the conditions in read/write_raw_cp_reg()
243 * and assertions in raw_read()/raw_write().
245 if ((ri->type & ARM_CP_CONST) ||
246 ri->fieldoffset ||
247 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
248 return false;
250 return true;
253 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
255 /* Write the coprocessor state from cpu->env to the (index,value) list. */
256 int i;
257 bool ok = true;
259 for (i = 0; i < cpu->cpreg_array_len; i++) {
260 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
261 const ARMCPRegInfo *ri;
262 uint64_t newval;
264 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
265 if (!ri) {
266 ok = false;
267 continue;
269 if (ri->type & ARM_CP_NO_RAW) {
270 continue;
273 newval = read_raw_cp_reg(&cpu->env, ri);
274 if (kvm_sync) {
276 * Only sync if the previous list->cpustate sync succeeded.
277 * Rather than tracking the success/failure state for every
278 * item in the list, we just recheck "does the raw write we must
279 * have made in write_list_to_cpustate() read back OK" here.
281 uint64_t oldval = cpu->cpreg_values[i];
283 if (oldval == newval) {
284 continue;
287 write_raw_cp_reg(&cpu->env, ri, oldval);
288 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
289 continue;
292 write_raw_cp_reg(&cpu->env, ri, newval);
294 cpu->cpreg_values[i] = newval;
296 return ok;
299 bool write_list_to_cpustate(ARMCPU *cpu)
301 int i;
302 bool ok = true;
304 for (i = 0; i < cpu->cpreg_array_len; i++) {
305 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
306 uint64_t v = cpu->cpreg_values[i];
307 const ARMCPRegInfo *ri;
309 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
310 if (!ri) {
311 ok = false;
312 continue;
314 if (ri->type & ARM_CP_NO_RAW) {
315 continue;
317 /* Write value and confirm it reads back as written
318 * (to catch read-only registers and partially read-only
319 * registers where the incoming migration value doesn't match)
321 write_raw_cp_reg(&cpu->env, ri, v);
322 if (read_raw_cp_reg(&cpu->env, ri) != v) {
323 ok = false;
326 return ok;
329 static void add_cpreg_to_list(gpointer key, gpointer opaque)
331 ARMCPU *cpu = opaque;
332 uint64_t regidx;
333 const ARMCPRegInfo *ri;
335 regidx = *(uint32_t *)key;
336 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
338 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
339 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
340 /* The value array need not be initialized at this point */
341 cpu->cpreg_array_len++;
345 static void count_cpreg(gpointer key, gpointer opaque)
347 ARMCPU *cpu = opaque;
348 uint64_t regidx;
349 const ARMCPRegInfo *ri;
351 regidx = *(uint32_t *)key;
352 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
354 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
355 cpu->cpreg_array_len++;
359 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
361 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
362 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
364 if (aidx > bidx) {
365 return 1;
367 if (aidx < bidx) {
368 return -1;
370 return 0;
373 void init_cpreg_list(ARMCPU *cpu)
375 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
376 * Note that we require cpreg_tuples[] to be sorted by key ID.
378 GList *keys;
379 int arraylen;
381 keys = g_hash_table_get_keys(cpu->cp_regs);
382 keys = g_list_sort(keys, cpreg_key_compare);
384 cpu->cpreg_array_len = 0;
386 g_list_foreach(keys, count_cpreg, cpu);
388 arraylen = cpu->cpreg_array_len;
389 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
390 cpu->cpreg_values = g_new(uint64_t, arraylen);
391 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
392 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
393 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
394 cpu->cpreg_array_len = 0;
396 g_list_foreach(keys, add_cpreg_to_list, cpu);
398 assert(cpu->cpreg_array_len == arraylen);
400 g_list_free(keys);
404 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
405 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
407 * access_el3_aa32ns: Used to check AArch32 register views.
408 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
410 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
411 const ARMCPRegInfo *ri,
412 bool isread)
414 bool secure = arm_is_secure_below_el3(env);
416 assert(!arm_el_is_aa64(env, 3));
417 if (secure) {
418 return CP_ACCESS_TRAP_UNCATEGORIZED;
420 return CP_ACCESS_OK;
423 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
424 const ARMCPRegInfo *ri,
425 bool isread)
427 if (!arm_el_is_aa64(env, 3)) {
428 return access_el3_aa32ns(env, ri, isread);
430 return CP_ACCESS_OK;
433 /* Some secure-only AArch32 registers trap to EL3 if used from
434 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
435 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
436 * We assume that the .access field is set to PL1_RW.
438 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
439 const ARMCPRegInfo *ri,
440 bool isread)
442 if (arm_current_el(env) == 3) {
443 return CP_ACCESS_OK;
445 if (arm_is_secure_below_el3(env)) {
446 return CP_ACCESS_TRAP_EL3;
448 /* This will be EL1 NS and EL2 NS, which just UNDEF */
449 return CP_ACCESS_TRAP_UNCATEGORIZED;
452 /* Check for traps to "powerdown debug" registers, which are controlled
453 * by MDCR.TDOSA
455 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
456 bool isread)
458 int el = arm_current_el(env);
459 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) ||
460 (env->cp15.mdcr_el2 & MDCR_TDE) ||
461 (arm_hcr_el2_eff(env) & HCR_TGE);
463 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) {
464 return CP_ACCESS_TRAP_EL2;
466 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
467 return CP_ACCESS_TRAP_EL3;
469 return CP_ACCESS_OK;
472 /* Check for traps to "debug ROM" registers, which are controlled
473 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
475 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
476 bool isread)
478 int el = arm_current_el(env);
479 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) ||
480 (env->cp15.mdcr_el2 & MDCR_TDE) ||
481 (arm_hcr_el2_eff(env) & HCR_TGE);
483 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) {
484 return CP_ACCESS_TRAP_EL2;
486 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
487 return CP_ACCESS_TRAP_EL3;
489 return CP_ACCESS_OK;
492 /* Check for traps to general debug registers, which are controlled
493 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
495 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
496 bool isread)
498 int el = arm_current_el(env);
499 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) ||
500 (env->cp15.mdcr_el2 & MDCR_TDE) ||
501 (arm_hcr_el2_eff(env) & HCR_TGE);
503 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) {
504 return CP_ACCESS_TRAP_EL2;
506 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
507 return CP_ACCESS_TRAP_EL3;
509 return CP_ACCESS_OK;
512 /* Check for traps to performance monitor registers, which are controlled
513 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
515 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
516 bool isread)
518 int el = arm_current_el(env);
520 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
521 && !arm_is_secure_below_el3(env)) {
522 return CP_ACCESS_TRAP_EL2;
524 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
525 return CP_ACCESS_TRAP_EL3;
527 return CP_ACCESS_OK;
530 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
532 ARMCPU *cpu = env_archcpu(env);
534 raw_write(env, ri, value);
535 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
538 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
540 ARMCPU *cpu = env_archcpu(env);
542 if (raw_read(env, ri) != value) {
543 /* Unlike real hardware the qemu TLB uses virtual addresses,
544 * not modified virtual addresses, so this causes a TLB flush.
546 tlb_flush(CPU(cpu));
547 raw_write(env, ri, value);
551 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
552 uint64_t value)
554 ARMCPU *cpu = env_archcpu(env);
556 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
557 && !extended_addresses_enabled(env)) {
558 /* For VMSA (when not using the LPAE long descriptor page table
559 * format) this register includes the ASID, so do a TLB flush.
560 * For PMSA it is purely a process ID and no action is needed.
562 tlb_flush(CPU(cpu));
564 raw_write(env, ri, value);
567 /* IS variants of TLB operations must affect all cores */
568 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
569 uint64_t value)
571 CPUState *cs = env_cpu(env);
573 tlb_flush_all_cpus_synced(cs);
576 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
577 uint64_t value)
579 CPUState *cs = env_cpu(env);
581 tlb_flush_all_cpus_synced(cs);
584 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
585 uint64_t value)
587 CPUState *cs = env_cpu(env);
589 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
592 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
593 uint64_t value)
595 CPUState *cs = env_cpu(env);
597 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
601 * Non-IS variants of TLB operations are upgraded to
602 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to
603 * force broadcast of these operations.
605 static bool tlb_force_broadcast(CPUARMState *env)
607 return (env->cp15.hcr_el2 & HCR_FB) &&
608 arm_current_el(env) == 1 && arm_is_secure_below_el3(env);
611 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
612 uint64_t value)
614 /* Invalidate all (TLBIALL) */
615 ARMCPU *cpu = env_archcpu(env);
617 if (tlb_force_broadcast(env)) {
618 tlbiall_is_write(env, NULL, value);
619 return;
622 tlb_flush(CPU(cpu));
625 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
626 uint64_t value)
628 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
629 ARMCPU *cpu = env_archcpu(env);
631 if (tlb_force_broadcast(env)) {
632 tlbimva_is_write(env, NULL, value);
633 return;
636 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
639 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
640 uint64_t value)
642 /* Invalidate by ASID (TLBIASID) */
643 ARMCPU *cpu = env_archcpu(env);
645 if (tlb_force_broadcast(env)) {
646 tlbiasid_is_write(env, NULL, value);
647 return;
650 tlb_flush(CPU(cpu));
653 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
654 uint64_t value)
656 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
657 ARMCPU *cpu = env_archcpu(env);
659 if (tlb_force_broadcast(env)) {
660 tlbimvaa_is_write(env, NULL, value);
661 return;
664 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
667 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
668 uint64_t value)
670 CPUState *cs = env_cpu(env);
672 tlb_flush_by_mmuidx(cs,
673 ARMMMUIdxBit_S12NSE1 |
674 ARMMMUIdxBit_S12NSE0 |
675 ARMMMUIdxBit_S2NS);
678 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
679 uint64_t value)
681 CPUState *cs = env_cpu(env);
683 tlb_flush_by_mmuidx_all_cpus_synced(cs,
684 ARMMMUIdxBit_S12NSE1 |
685 ARMMMUIdxBit_S12NSE0 |
686 ARMMMUIdxBit_S2NS);
689 static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
690 uint64_t value)
692 /* Invalidate by IPA. This has to invalidate any structures that
693 * contain only stage 2 translation information, but does not need
694 * to apply to structures that contain combined stage 1 and stage 2
695 * translation information.
696 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
698 CPUState *cs = env_cpu(env);
699 uint64_t pageaddr;
701 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
702 return;
705 pageaddr = sextract64(value << 12, 0, 40);
707 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
710 static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
711 uint64_t value)
713 CPUState *cs = env_cpu(env);
714 uint64_t pageaddr;
716 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
717 return;
720 pageaddr = sextract64(value << 12, 0, 40);
722 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
723 ARMMMUIdxBit_S2NS);
726 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
727 uint64_t value)
729 CPUState *cs = env_cpu(env);
731 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
734 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
735 uint64_t value)
737 CPUState *cs = env_cpu(env);
739 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
742 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
743 uint64_t value)
745 CPUState *cs = env_cpu(env);
746 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
748 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
751 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
752 uint64_t value)
754 CPUState *cs = env_cpu(env);
755 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
757 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
758 ARMMMUIdxBit_S1E2);
761 static const ARMCPRegInfo cp_reginfo[] = {
762 /* Define the secure and non-secure FCSE identifier CP registers
763 * separately because there is no secure bank in V8 (no _EL3). This allows
764 * the secure register to be properly reset and migrated. There is also no
765 * v8 EL1 version of the register so the non-secure instance stands alone.
767 { .name = "FCSEIDR",
768 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
769 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
770 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
771 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
772 { .name = "FCSEIDR_S",
773 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
774 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
775 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
776 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
777 /* Define the secure and non-secure context identifier CP registers
778 * separately because there is no secure bank in V8 (no _EL3). This allows
779 * the secure register to be properly reset and migrated. In the
780 * non-secure case, the 32-bit register will have reset and migration
781 * disabled during registration as it is handled by the 64-bit instance.
783 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
784 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
785 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
786 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
787 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
788 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
789 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
790 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
791 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
792 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
793 REGINFO_SENTINEL
796 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
797 /* NB: Some of these registers exist in v8 but with more precise
798 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
800 /* MMU Domain access control / MPU write buffer control */
801 { .name = "DACR",
802 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
803 .access = PL1_RW, .resetvalue = 0,
804 .writefn = dacr_write, .raw_writefn = raw_write,
805 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
806 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
807 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
808 * For v6 and v5, these mappings are overly broad.
810 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
811 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
812 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
813 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
814 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
815 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
816 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
817 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
818 /* Cache maintenance ops; some of this space may be overridden later. */
819 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
820 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
821 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
822 REGINFO_SENTINEL
825 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
826 /* Not all pre-v6 cores implemented this WFI, so this is slightly
827 * over-broad.
829 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
830 .access = PL1_W, .type = ARM_CP_WFI },
831 REGINFO_SENTINEL
834 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
835 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
836 * is UNPREDICTABLE; we choose to NOP as most implementations do).
838 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
839 .access = PL1_W, .type = ARM_CP_WFI },
840 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
841 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
842 * OMAPCP will override this space.
844 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
845 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
846 .resetvalue = 0 },
847 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
848 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
849 .resetvalue = 0 },
850 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
851 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
852 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
853 .resetvalue = 0 },
854 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
855 * implementing it as RAZ means the "debug architecture version" bits
856 * will read as a reserved value, which should cause Linux to not try
857 * to use the debug hardware.
859 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
860 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
861 /* MMU TLB control. Note that the wildcarding means we cover not just
862 * the unified TLB ops but also the dside/iside/inner-shareable variants.
864 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
865 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
866 .type = ARM_CP_NO_RAW },
867 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
868 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
869 .type = ARM_CP_NO_RAW },
870 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
871 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
872 .type = ARM_CP_NO_RAW },
873 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
874 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
875 .type = ARM_CP_NO_RAW },
876 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
877 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
878 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
879 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
880 REGINFO_SENTINEL
883 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
884 uint64_t value)
886 uint32_t mask = 0;
888 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
889 if (!arm_feature(env, ARM_FEATURE_V8)) {
890 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
891 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
892 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
894 if (arm_feature(env, ARM_FEATURE_VFP)) {
895 /* VFP coprocessor: cp10 & cp11 [23:20] */
896 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
898 if (!arm_feature(env, ARM_FEATURE_NEON)) {
899 /* ASEDIS [31] bit is RAO/WI */
900 value |= (1 << 31);
903 /* VFPv3 and upwards with NEON implement 32 double precision
904 * registers (D0-D31).
906 if (!arm_feature(env, ARM_FEATURE_NEON) ||
907 !arm_feature(env, ARM_FEATURE_VFP3)) {
908 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
909 value |= (1 << 30);
912 value &= mask;
916 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
917 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
919 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
920 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
921 value &= ~(0xf << 20);
922 value |= env->cp15.cpacr_el1 & (0xf << 20);
925 env->cp15.cpacr_el1 = value;
928 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
931 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
932 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
934 uint64_t value = env->cp15.cpacr_el1;
936 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
937 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
938 value &= ~(0xf << 20);
940 return value;
944 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
946 /* Call cpacr_write() so that we reset with the correct RAO bits set
947 * for our CPU features.
949 cpacr_write(env, ri, 0);
952 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
953 bool isread)
955 if (arm_feature(env, ARM_FEATURE_V8)) {
956 /* Check if CPACR accesses are to be trapped to EL2 */
957 if (arm_current_el(env) == 1 &&
958 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
959 return CP_ACCESS_TRAP_EL2;
960 /* Check if CPACR accesses are to be trapped to EL3 */
961 } else if (arm_current_el(env) < 3 &&
962 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
963 return CP_ACCESS_TRAP_EL3;
967 return CP_ACCESS_OK;
970 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
971 bool isread)
973 /* Check if CPTR accesses are set to trap to EL3 */
974 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
975 return CP_ACCESS_TRAP_EL3;
978 return CP_ACCESS_OK;
981 static const ARMCPRegInfo v6_cp_reginfo[] = {
982 /* prefetch by MVA in v6, NOP in v7 */
983 { .name = "MVA_prefetch",
984 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
985 .access = PL1_W, .type = ARM_CP_NOP },
986 /* We need to break the TB after ISB to execute self-modifying code
987 * correctly and also to take any pending interrupts immediately.
988 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
990 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
991 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
992 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
993 .access = PL0_W, .type = ARM_CP_NOP },
994 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
995 .access = PL0_W, .type = ARM_CP_NOP },
996 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
997 .access = PL1_RW,
998 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
999 offsetof(CPUARMState, cp15.ifar_ns) },
1000 .resetvalue = 0, },
1001 /* Watchpoint Fault Address Register : should actually only be present
1002 * for 1136, 1176, 11MPCore.
1004 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1005 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
1006 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
1007 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
1008 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
1009 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
1010 REGINFO_SENTINEL
1013 /* Definitions for the PMU registers */
1014 #define PMCRN_MASK 0xf800
1015 #define PMCRN_SHIFT 11
1016 #define PMCRLC 0x40
1017 #define PMCRDP 0x10
1018 #define PMCRD 0x8
1019 #define PMCRC 0x4
1020 #define PMCRP 0x2
1021 #define PMCRE 0x1
1023 #define PMXEVTYPER_P 0x80000000
1024 #define PMXEVTYPER_U 0x40000000
1025 #define PMXEVTYPER_NSK 0x20000000
1026 #define PMXEVTYPER_NSU 0x10000000
1027 #define PMXEVTYPER_NSH 0x08000000
1028 #define PMXEVTYPER_M 0x04000000
1029 #define PMXEVTYPER_MT 0x02000000
1030 #define PMXEVTYPER_EVTCOUNT 0x0000ffff
1031 #define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
1032 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
1033 PMXEVTYPER_M | PMXEVTYPER_MT | \
1034 PMXEVTYPER_EVTCOUNT)
1036 #define PMCCFILTR 0xf8000000
1037 #define PMCCFILTR_M PMXEVTYPER_M
1038 #define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M)
1040 static inline uint32_t pmu_num_counters(CPUARMState *env)
1042 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1045 /* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1046 static inline uint64_t pmu_counter_mask(CPUARMState *env)
1048 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1051 typedef struct pm_event {
1052 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1053 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1054 bool (*supported)(CPUARMState *);
1056 * Retrieve the current count of the underlying event. The programmed
1057 * counters hold a difference from the return value from this function
1059 uint64_t (*get_count)(CPUARMState *);
1061 * Return how many nanoseconds it will take (at a minimum) for count events
1062 * to occur. A negative value indicates the counter will never overflow, or
1063 * that the counter has otherwise arranged for the overflow bit to be set
1064 * and the PMU interrupt to be raised on overflow.
1066 int64_t (*ns_per_count)(uint64_t);
1067 } pm_event;
1069 static bool event_always_supported(CPUARMState *env)
1071 return true;
1074 static uint64_t swinc_get_count(CPUARMState *env)
1077 * SW_INCR events are written directly to the pmevcntr's by writes to
1078 * PMSWINC, so there is no underlying count maintained by the PMU itself
1080 return 0;
1083 static int64_t swinc_ns_per(uint64_t ignored)
1085 return -1;
1089 * Return the underlying cycle count for the PMU cycle counters. If we're in
1090 * usermode, simply return 0.
1092 static uint64_t cycles_get_count(CPUARMState *env)
1094 #ifndef CONFIG_USER_ONLY
1095 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1096 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1097 #else
1098 return cpu_get_host_ticks();
1099 #endif
1102 #ifndef CONFIG_USER_ONLY
1103 static int64_t cycles_ns_per(uint64_t cycles)
1105 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1108 static bool instructions_supported(CPUARMState *env)
1110 return use_icount == 1 /* Precise instruction counting */;
1113 static uint64_t instructions_get_count(CPUARMState *env)
1115 return (uint64_t)cpu_get_icount_raw();
1118 static int64_t instructions_ns_per(uint64_t icount)
1120 return cpu_icount_to_ns((int64_t)icount);
1122 #endif
1124 static const pm_event pm_events[] = {
1125 { .number = 0x000, /* SW_INCR */
1126 .supported = event_always_supported,
1127 .get_count = swinc_get_count,
1128 .ns_per_count = swinc_ns_per,
1130 #ifndef CONFIG_USER_ONLY
1131 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1132 .supported = instructions_supported,
1133 .get_count = instructions_get_count,
1134 .ns_per_count = instructions_ns_per,
1136 { .number = 0x011, /* CPU_CYCLES, Cycle */
1137 .supported = event_always_supported,
1138 .get_count = cycles_get_count,
1139 .ns_per_count = cycles_ns_per,
1141 #endif
1145 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1146 * events (i.e. the statistical profiling extension), this implementation
1147 * should first be updated to something sparse instead of the current
1148 * supported_event_map[] array.
1150 #define MAX_EVENT_ID 0x11
1151 #define UNSUPPORTED_EVENT UINT16_MAX
1152 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1155 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1156 * of ARM event numbers to indices in our pm_events array.
1158 * Note: Events in the 0x40XX range are not currently supported.
1160 void pmu_init(ARMCPU *cpu)
1162 unsigned int i;
1165 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1166 * events to them
1168 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1169 supported_event_map[i] = UNSUPPORTED_EVENT;
1171 cpu->pmceid0 = 0;
1172 cpu->pmceid1 = 0;
1174 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1175 const pm_event *cnt = &pm_events[i];
1176 assert(cnt->number <= MAX_EVENT_ID);
1177 /* We do not currently support events in the 0x40xx range */
1178 assert(cnt->number <= 0x3f);
1180 if (cnt->supported(&cpu->env)) {
1181 supported_event_map[cnt->number] = i;
1182 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1183 if (cnt->number & 0x20) {
1184 cpu->pmceid1 |= event_mask;
1185 } else {
1186 cpu->pmceid0 |= event_mask;
1193 * Check at runtime whether a PMU event is supported for the current machine
1195 static bool event_supported(uint16_t number)
1197 if (number > MAX_EVENT_ID) {
1198 return false;
1200 return supported_event_map[number] != UNSUPPORTED_EVENT;
1203 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1204 bool isread)
1206 /* Performance monitor registers user accessibility is controlled
1207 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1208 * trapping to EL2 or EL3 for other accesses.
1210 int el = arm_current_el(env);
1212 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1213 return CP_ACCESS_TRAP;
1215 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
1216 && !arm_is_secure_below_el3(env)) {
1217 return CP_ACCESS_TRAP_EL2;
1219 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1220 return CP_ACCESS_TRAP_EL3;
1223 return CP_ACCESS_OK;
1226 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1227 const ARMCPRegInfo *ri,
1228 bool isread)
1230 /* ER: event counter read trap control */
1231 if (arm_feature(env, ARM_FEATURE_V8)
1232 && arm_current_el(env) == 0
1233 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1234 && isread) {
1235 return CP_ACCESS_OK;
1238 return pmreg_access(env, ri, isread);
1241 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1242 const ARMCPRegInfo *ri,
1243 bool isread)
1245 /* SW: software increment write trap control */
1246 if (arm_feature(env, ARM_FEATURE_V8)
1247 && arm_current_el(env) == 0
1248 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1249 && !isread) {
1250 return CP_ACCESS_OK;
1253 return pmreg_access(env, ri, isread);
1256 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1257 const ARMCPRegInfo *ri,
1258 bool isread)
1260 /* ER: event counter read trap control */
1261 if (arm_feature(env, ARM_FEATURE_V8)
1262 && arm_current_el(env) == 0
1263 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1264 return CP_ACCESS_OK;
1267 return pmreg_access(env, ri, isread);
1270 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1271 const ARMCPRegInfo *ri,
1272 bool isread)
1274 /* CR: cycle counter read trap control */
1275 if (arm_feature(env, ARM_FEATURE_V8)
1276 && arm_current_el(env) == 0
1277 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1278 && isread) {
1279 return CP_ACCESS_OK;
1282 return pmreg_access(env, ri, isread);
1285 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1286 * the current EL, security state, and register configuration.
1288 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1290 uint64_t filter;
1291 bool e, p, u, nsk, nsu, nsh, m;
1292 bool enabled, prohibited, filtered;
1293 bool secure = arm_is_secure(env);
1294 int el = arm_current_el(env);
1295 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1297 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1298 return false;
1301 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1302 (counter < hpmn || counter == 31)) {
1303 e = env->cp15.c9_pmcr & PMCRE;
1304 } else {
1305 e = env->cp15.mdcr_el2 & MDCR_HPME;
1307 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1309 if (!secure) {
1310 if (el == 2 && (counter < hpmn || counter == 31)) {
1311 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD;
1312 } else {
1313 prohibited = false;
1315 } else {
1316 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1317 (env->cp15.mdcr_el3 & MDCR_SPME);
1320 if (prohibited && counter == 31) {
1321 prohibited = env->cp15.c9_pmcr & PMCRDP;
1324 if (counter == 31) {
1325 filter = env->cp15.pmccfiltr_el0;
1326 } else {
1327 filter = env->cp15.c14_pmevtyper[counter];
1330 p = filter & PMXEVTYPER_P;
1331 u = filter & PMXEVTYPER_U;
1332 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1333 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1334 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1335 m = arm_el_is_aa64(env, 1) &&
1336 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1338 if (el == 0) {
1339 filtered = secure ? u : u != nsu;
1340 } else if (el == 1) {
1341 filtered = secure ? p : p != nsk;
1342 } else if (el == 2) {
1343 filtered = !nsh;
1344 } else { /* EL3 */
1345 filtered = m != p;
1348 if (counter != 31) {
1350 * If not checking PMCCNTR, ensure the counter is setup to an event we
1351 * support
1353 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1354 if (!event_supported(event)) {
1355 return false;
1359 return enabled && !prohibited && !filtered;
1362 static void pmu_update_irq(CPUARMState *env)
1364 ARMCPU *cpu = env_archcpu(env);
1365 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1366 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1370 * Ensure c15_ccnt is the guest-visible count so that operations such as
1371 * enabling/disabling the counter or filtering, modifying the count itself,
1372 * etc. can be done logically. This is essentially a no-op if the counter is
1373 * not enabled at the time of the call.
1375 static void pmccntr_op_start(CPUARMState *env)
1377 uint64_t cycles = cycles_get_count(env);
1379 if (pmu_counter_enabled(env, 31)) {
1380 uint64_t eff_cycles = cycles;
1381 if (env->cp15.c9_pmcr & PMCRD) {
1382 /* Increment once every 64 processor clock cycles */
1383 eff_cycles /= 64;
1386 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1388 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1389 1ull << 63 : 1ull << 31;
1390 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1391 env->cp15.c9_pmovsr |= (1 << 31);
1392 pmu_update_irq(env);
1395 env->cp15.c15_ccnt = new_pmccntr;
1397 env->cp15.c15_ccnt_delta = cycles;
1401 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1402 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1403 * pmccntr_op_start.
1405 static void pmccntr_op_finish(CPUARMState *env)
1407 if (pmu_counter_enabled(env, 31)) {
1408 #ifndef CONFIG_USER_ONLY
1409 /* Calculate when the counter will next overflow */
1410 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1411 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1412 remaining_cycles = (uint32_t)remaining_cycles;
1414 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1416 if (overflow_in > 0) {
1417 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1418 overflow_in;
1419 ARMCPU *cpu = env_archcpu(env);
1420 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1422 #endif
1424 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1425 if (env->cp15.c9_pmcr & PMCRD) {
1426 /* Increment once every 64 processor clock cycles */
1427 prev_cycles /= 64;
1429 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1433 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1436 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1437 uint64_t count = 0;
1438 if (event_supported(event)) {
1439 uint16_t event_idx = supported_event_map[event];
1440 count = pm_events[event_idx].get_count(env);
1443 if (pmu_counter_enabled(env, counter)) {
1444 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1446 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1447 env->cp15.c9_pmovsr |= (1 << counter);
1448 pmu_update_irq(env);
1450 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1452 env->cp15.c14_pmevcntr_delta[counter] = count;
1455 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1457 if (pmu_counter_enabled(env, counter)) {
1458 #ifndef CONFIG_USER_ONLY
1459 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1460 uint16_t event_idx = supported_event_map[event];
1461 uint64_t delta = UINT32_MAX -
1462 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1463 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1465 if (overflow_in > 0) {
1466 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1467 overflow_in;
1468 ARMCPU *cpu = env_archcpu(env);
1469 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1471 #endif
1473 env->cp15.c14_pmevcntr_delta[counter] -=
1474 env->cp15.c14_pmevcntr[counter];
1478 void pmu_op_start(CPUARMState *env)
1480 unsigned int i;
1481 pmccntr_op_start(env);
1482 for (i = 0; i < pmu_num_counters(env); i++) {
1483 pmevcntr_op_start(env, i);
1487 void pmu_op_finish(CPUARMState *env)
1489 unsigned int i;
1490 pmccntr_op_finish(env);
1491 for (i = 0; i < pmu_num_counters(env); i++) {
1492 pmevcntr_op_finish(env, i);
1496 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1498 pmu_op_start(&cpu->env);
1501 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1503 pmu_op_finish(&cpu->env);
1506 void arm_pmu_timer_cb(void *opaque)
1508 ARMCPU *cpu = opaque;
1511 * Update all the counter values based on the current underlying counts,
1512 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1513 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1514 * counter may expire.
1516 pmu_op_start(&cpu->env);
1517 pmu_op_finish(&cpu->env);
1520 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1521 uint64_t value)
1523 pmu_op_start(env);
1525 if (value & PMCRC) {
1526 /* The counter has been reset */
1527 env->cp15.c15_ccnt = 0;
1530 if (value & PMCRP) {
1531 unsigned int i;
1532 for (i = 0; i < pmu_num_counters(env); i++) {
1533 env->cp15.c14_pmevcntr[i] = 0;
1537 /* only the DP, X, D and E bits are writable */
1538 env->cp15.c9_pmcr &= ~0x39;
1539 env->cp15.c9_pmcr |= (value & 0x39);
1541 pmu_op_finish(env);
1544 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1545 uint64_t value)
1547 unsigned int i;
1548 for (i = 0; i < pmu_num_counters(env); i++) {
1549 /* Increment a counter's count iff: */
1550 if ((value & (1 << i)) && /* counter's bit is set */
1551 /* counter is enabled and not filtered */
1552 pmu_counter_enabled(env, i) &&
1553 /* counter is SW_INCR */
1554 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1555 pmevcntr_op_start(env, i);
1558 * Detect if this write causes an overflow since we can't predict
1559 * PMSWINC overflows like we can for other events
1561 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1563 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1564 env->cp15.c9_pmovsr |= (1 << i);
1565 pmu_update_irq(env);
1568 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1570 pmevcntr_op_finish(env, i);
1575 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1577 uint64_t ret;
1578 pmccntr_op_start(env);
1579 ret = env->cp15.c15_ccnt;
1580 pmccntr_op_finish(env);
1581 return ret;
1584 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1585 uint64_t value)
1587 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1588 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1589 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1590 * accessed.
1592 env->cp15.c9_pmselr = value & 0x1f;
1595 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1596 uint64_t value)
1598 pmccntr_op_start(env);
1599 env->cp15.c15_ccnt = value;
1600 pmccntr_op_finish(env);
1603 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1604 uint64_t value)
1606 uint64_t cur_val = pmccntr_read(env, NULL);
1608 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1611 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1612 uint64_t value)
1614 pmccntr_op_start(env);
1615 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1616 pmccntr_op_finish(env);
1619 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1620 uint64_t value)
1622 pmccntr_op_start(env);
1623 /* M is not accessible from AArch32 */
1624 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1625 (value & PMCCFILTR);
1626 pmccntr_op_finish(env);
1629 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1631 /* M is not visible in AArch32 */
1632 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1635 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636 uint64_t value)
1638 value &= pmu_counter_mask(env);
1639 env->cp15.c9_pmcnten |= value;
1642 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1643 uint64_t value)
1645 value &= pmu_counter_mask(env);
1646 env->cp15.c9_pmcnten &= ~value;
1649 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1650 uint64_t value)
1652 value &= pmu_counter_mask(env);
1653 env->cp15.c9_pmovsr &= ~value;
1654 pmu_update_irq(env);
1657 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1658 uint64_t value)
1660 value &= pmu_counter_mask(env);
1661 env->cp15.c9_pmovsr |= value;
1662 pmu_update_irq(env);
1665 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1666 uint64_t value, const uint8_t counter)
1668 if (counter == 31) {
1669 pmccfiltr_write(env, ri, value);
1670 } else if (counter < pmu_num_counters(env)) {
1671 pmevcntr_op_start(env, counter);
1674 * If this counter's event type is changing, store the current
1675 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1676 * pmevcntr_op_finish has the correct baseline when it converts back to
1677 * a delta.
1679 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1680 PMXEVTYPER_EVTCOUNT;
1681 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1682 if (old_event != new_event) {
1683 uint64_t count = 0;
1684 if (event_supported(new_event)) {
1685 uint16_t event_idx = supported_event_map[new_event];
1686 count = pm_events[event_idx].get_count(env);
1688 env->cp15.c14_pmevcntr_delta[counter] = count;
1691 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1692 pmevcntr_op_finish(env, counter);
1694 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1695 * PMSELR value is equal to or greater than the number of implemented
1696 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1700 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1701 const uint8_t counter)
1703 if (counter == 31) {
1704 return env->cp15.pmccfiltr_el0;
1705 } else if (counter < pmu_num_counters(env)) {
1706 return env->cp15.c14_pmevtyper[counter];
1707 } else {
1709 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1710 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1712 return 0;
1716 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1717 uint64_t value)
1719 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1720 pmevtyper_write(env, ri, value, counter);
1723 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1724 uint64_t value)
1726 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1727 env->cp15.c14_pmevtyper[counter] = value;
1730 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1731 * pmu_op_finish calls when loading saved state for a migration. Because
1732 * we're potentially updating the type of event here, the value written to
1733 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1734 * different counter type. Therefore, we need to set this value to the
1735 * current count for the counter type we're writing so that pmu_op_finish
1736 * has the correct count for its calculation.
1738 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1739 if (event_supported(event)) {
1740 uint16_t event_idx = supported_event_map[event];
1741 env->cp15.c14_pmevcntr_delta[counter] =
1742 pm_events[event_idx].get_count(env);
1746 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1748 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1749 return pmevtyper_read(env, ri, counter);
1752 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1753 uint64_t value)
1755 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1758 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1760 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1763 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1764 uint64_t value, uint8_t counter)
1766 if (counter < pmu_num_counters(env)) {
1767 pmevcntr_op_start(env, counter);
1768 env->cp15.c14_pmevcntr[counter] = value;
1769 pmevcntr_op_finish(env, counter);
1772 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1773 * are CONSTRAINED UNPREDICTABLE.
1777 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1778 uint8_t counter)
1780 if (counter < pmu_num_counters(env)) {
1781 uint64_t ret;
1782 pmevcntr_op_start(env, counter);
1783 ret = env->cp15.c14_pmevcntr[counter];
1784 pmevcntr_op_finish(env, counter);
1785 return ret;
1786 } else {
1787 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1788 * are CONSTRAINED UNPREDICTABLE. */
1789 return 0;
1793 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1794 uint64_t value)
1796 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1797 pmevcntr_write(env, ri, value, counter);
1800 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1802 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1803 return pmevcntr_read(env, ri, counter);
1806 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1807 uint64_t value)
1809 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1810 assert(counter < pmu_num_counters(env));
1811 env->cp15.c14_pmevcntr[counter] = value;
1812 pmevcntr_write(env, ri, value, counter);
1815 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1817 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1818 assert(counter < pmu_num_counters(env));
1819 return env->cp15.c14_pmevcntr[counter];
1822 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1823 uint64_t value)
1825 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1828 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1830 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1833 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1834 uint64_t value)
1836 if (arm_feature(env, ARM_FEATURE_V8)) {
1837 env->cp15.c9_pmuserenr = value & 0xf;
1838 } else {
1839 env->cp15.c9_pmuserenr = value & 1;
1843 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1844 uint64_t value)
1846 /* We have no event counters so only the C bit can be changed */
1847 value &= pmu_counter_mask(env);
1848 env->cp15.c9_pminten |= value;
1849 pmu_update_irq(env);
1852 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1853 uint64_t value)
1855 value &= pmu_counter_mask(env);
1856 env->cp15.c9_pminten &= ~value;
1857 pmu_update_irq(env);
1860 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1861 uint64_t value)
1863 /* Note that even though the AArch64 view of this register has bits
1864 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1865 * architectural requirements for bits which are RES0 only in some
1866 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1867 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1869 raw_write(env, ri, value & ~0x1FULL);
1872 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1874 /* Begin with base v8.0 state. */
1875 uint32_t valid_mask = 0x3fff;
1876 ARMCPU *cpu = env_archcpu(env);
1878 if (arm_el_is_aa64(env, 3)) {
1879 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
1880 valid_mask &= ~SCR_NET;
1881 } else {
1882 valid_mask &= ~(SCR_RW | SCR_ST);
1885 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1886 valid_mask &= ~SCR_HCE;
1888 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1889 * supported if EL2 exists. The bit is UNK/SBZP when
1890 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1891 * when EL2 is unavailable.
1892 * On ARMv8, this bit is always available.
1894 if (arm_feature(env, ARM_FEATURE_V7) &&
1895 !arm_feature(env, ARM_FEATURE_V8)) {
1896 valid_mask &= ~SCR_SMD;
1899 if (cpu_isar_feature(aa64_lor, cpu)) {
1900 valid_mask |= SCR_TLOR;
1902 if (cpu_isar_feature(aa64_pauth, cpu)) {
1903 valid_mask |= SCR_API | SCR_APK;
1906 /* Clear all-context RES0 bits. */
1907 value &= valid_mask;
1908 raw_write(env, ri, value);
1911 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1913 ARMCPU *cpu = env_archcpu(env);
1915 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1916 * bank
1918 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1919 ri->secure & ARM_CP_SECSTATE_S);
1921 return cpu->ccsidr[index];
1924 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1925 uint64_t value)
1927 raw_write(env, ri, value & 0xf);
1930 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1932 CPUState *cs = env_cpu(env);
1933 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1934 uint64_t ret = 0;
1936 if (hcr_el2 & HCR_IMO) {
1937 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1938 ret |= CPSR_I;
1940 } else {
1941 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1942 ret |= CPSR_I;
1946 if (hcr_el2 & HCR_FMO) {
1947 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1948 ret |= CPSR_F;
1950 } else {
1951 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1952 ret |= CPSR_F;
1956 /* External aborts are not possible in QEMU so A bit is always clear */
1957 return ret;
1960 static const ARMCPRegInfo v7_cp_reginfo[] = {
1961 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1962 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1963 .access = PL1_W, .type = ARM_CP_NOP },
1964 /* Performance monitors are implementation defined in v7,
1965 * but with an ARM recommended set of registers, which we
1966 * follow.
1968 * Performance registers fall into three categories:
1969 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1970 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1971 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1972 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1973 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1975 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1976 .access = PL0_RW, .type = ARM_CP_ALIAS,
1977 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1978 .writefn = pmcntenset_write,
1979 .accessfn = pmreg_access,
1980 .raw_writefn = raw_write },
1981 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1982 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1983 .access = PL0_RW, .accessfn = pmreg_access,
1984 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1985 .writefn = pmcntenset_write, .raw_writefn = raw_write },
1986 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1987 .access = PL0_RW,
1988 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1989 .accessfn = pmreg_access,
1990 .writefn = pmcntenclr_write,
1991 .type = ARM_CP_ALIAS },
1992 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1993 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1994 .access = PL0_RW, .accessfn = pmreg_access,
1995 .type = ARM_CP_ALIAS,
1996 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1997 .writefn = pmcntenclr_write },
1998 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1999 .access = PL0_RW, .type = ARM_CP_IO,
2000 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2001 .accessfn = pmreg_access,
2002 .writefn = pmovsr_write,
2003 .raw_writefn = raw_write },
2004 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2005 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2006 .access = PL0_RW, .accessfn = pmreg_access,
2007 .type = ARM_CP_ALIAS | ARM_CP_IO,
2008 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2009 .writefn = pmovsr_write,
2010 .raw_writefn = raw_write },
2011 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2012 .access = PL0_W, .accessfn = pmreg_access_swinc,
2013 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2014 .writefn = pmswinc_write },
2015 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2016 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2017 .access = PL0_W, .accessfn = pmreg_access_swinc,
2018 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2019 .writefn = pmswinc_write },
2020 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2021 .access = PL0_RW, .type = ARM_CP_ALIAS,
2022 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2023 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2024 .raw_writefn = raw_write},
2025 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2026 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2027 .access = PL0_RW, .accessfn = pmreg_access_selr,
2028 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2029 .writefn = pmselr_write, .raw_writefn = raw_write, },
2030 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2031 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2032 .readfn = pmccntr_read, .writefn = pmccntr_write32,
2033 .accessfn = pmreg_access_ccntr },
2034 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2035 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2036 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2037 .type = ARM_CP_IO,
2038 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2039 .readfn = pmccntr_read, .writefn = pmccntr_write,
2040 .raw_readfn = raw_read, .raw_writefn = raw_write, },
2041 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2042 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2043 .access = PL0_RW, .accessfn = pmreg_access,
2044 .type = ARM_CP_ALIAS | ARM_CP_IO,
2045 .resetvalue = 0, },
2046 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2047 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2048 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2049 .access = PL0_RW, .accessfn = pmreg_access,
2050 .type = ARM_CP_IO,
2051 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2052 .resetvalue = 0, },
2053 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2054 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2055 .accessfn = pmreg_access,
2056 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2057 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2058 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2059 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2060 .accessfn = pmreg_access,
2061 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2062 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2063 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2064 .accessfn = pmreg_access_xevcntr,
2065 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2066 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2067 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2068 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2069 .accessfn = pmreg_access_xevcntr,
2070 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2071 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2072 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2073 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2074 .resetvalue = 0,
2075 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2076 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2077 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2078 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2079 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2080 .resetvalue = 0,
2081 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2082 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2083 .access = PL1_RW, .accessfn = access_tpm,
2084 .type = ARM_CP_ALIAS | ARM_CP_IO,
2085 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2086 .resetvalue = 0,
2087 .writefn = pmintenset_write, .raw_writefn = raw_write },
2088 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2089 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2090 .access = PL1_RW, .accessfn = access_tpm,
2091 .type = ARM_CP_IO,
2092 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2093 .writefn = pmintenset_write, .raw_writefn = raw_write,
2094 .resetvalue = 0x0 },
2095 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2096 .access = PL1_RW, .accessfn = access_tpm,
2097 .type = ARM_CP_ALIAS | ARM_CP_IO,
2098 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2099 .writefn = pmintenclr_write, },
2100 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2101 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2102 .access = PL1_RW, .accessfn = access_tpm,
2103 .type = ARM_CP_ALIAS | ARM_CP_IO,
2104 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2105 .writefn = pmintenclr_write },
2106 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2107 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2108 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2109 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2110 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2111 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
2112 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2113 offsetof(CPUARMState, cp15.csselr_ns) } },
2114 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2115 * just RAZ for all cores:
2117 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2118 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2119 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2120 /* Auxiliary fault status registers: these also are IMPDEF, and we
2121 * choose to RAZ/WI for all cores.
2123 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2124 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2125 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2126 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2127 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2128 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2129 /* MAIR can just read-as-written because we don't implement caches
2130 * and so don't need to care about memory attributes.
2132 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2133 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2134 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2135 .resetvalue = 0 },
2136 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2137 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2138 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2139 .resetvalue = 0 },
2140 /* For non-long-descriptor page tables these are PRRR and NMRR;
2141 * regardless they still act as reads-as-written for QEMU.
2143 /* MAIR0/1 are defined separately from their 64-bit counterpart which
2144 * allows them to assign the correct fieldoffset based on the endianness
2145 * handled in the field definitions.
2147 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2148 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
2149 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2150 offsetof(CPUARMState, cp15.mair0_ns) },
2151 .resetfn = arm_cp_reset_ignore },
2152 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2153 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
2154 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2155 offsetof(CPUARMState, cp15.mair1_ns) },
2156 .resetfn = arm_cp_reset_ignore },
2157 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2158 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2159 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2160 /* 32 bit ITLB invalidates */
2161 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2162 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2163 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2164 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2165 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2166 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2167 /* 32 bit DTLB invalidates */
2168 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2169 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2170 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2171 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2172 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2173 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2174 /* 32 bit TLB invalidates */
2175 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2176 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2177 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2178 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2179 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2180 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2181 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2182 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
2183 REGINFO_SENTINEL
2186 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2187 /* 32 bit TLB invalidates, Inner Shareable */
2188 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2189 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
2190 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2191 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
2192 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2193 .type = ARM_CP_NO_RAW, .access = PL1_W,
2194 .writefn = tlbiasid_is_write },
2195 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2196 .type = ARM_CP_NO_RAW, .access = PL1_W,
2197 .writefn = tlbimvaa_is_write },
2198 REGINFO_SENTINEL
2201 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2202 /* PMOVSSET is not implemented in v7 before v7ve */
2203 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2204 .access = PL0_RW, .accessfn = pmreg_access,
2205 .type = ARM_CP_ALIAS | ARM_CP_IO,
2206 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2207 .writefn = pmovsset_write,
2208 .raw_writefn = raw_write },
2209 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2210 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2211 .access = PL0_RW, .accessfn = pmreg_access,
2212 .type = ARM_CP_ALIAS | ARM_CP_IO,
2213 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2214 .writefn = pmovsset_write,
2215 .raw_writefn = raw_write },
2216 REGINFO_SENTINEL
2219 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2220 uint64_t value)
2222 value &= 1;
2223 env->teecr = value;
2226 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2227 bool isread)
2229 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2230 return CP_ACCESS_TRAP;
2232 return CP_ACCESS_OK;
2235 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2236 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2237 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2238 .resetvalue = 0,
2239 .writefn = teecr_write },
2240 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2241 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2242 .accessfn = teehbr_access, .resetvalue = 0 },
2243 REGINFO_SENTINEL
2246 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2247 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2248 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2249 .access = PL0_RW,
2250 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2251 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2252 .access = PL0_RW,
2253 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2254 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2255 .resetfn = arm_cp_reset_ignore },
2256 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2257 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2258 .access = PL0_R|PL1_W,
2259 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2260 .resetvalue = 0},
2261 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2262 .access = PL0_R|PL1_W,
2263 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2264 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2265 .resetfn = arm_cp_reset_ignore },
2266 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2267 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2268 .access = PL1_RW,
2269 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2270 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2271 .access = PL1_RW,
2272 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2273 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2274 .resetvalue = 0 },
2275 REGINFO_SENTINEL
2278 #ifndef CONFIG_USER_ONLY
2280 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2281 bool isread)
2283 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2284 * Writable only at the highest implemented exception level.
2286 int el = arm_current_el(env);
2288 switch (el) {
2289 case 0:
2290 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
2291 return CP_ACCESS_TRAP;
2293 break;
2294 case 1:
2295 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2296 arm_is_secure_below_el3(env)) {
2297 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2298 return CP_ACCESS_TRAP_UNCATEGORIZED;
2300 break;
2301 case 2:
2302 case 3:
2303 break;
2306 if (!isread && el < arm_highest_el(env)) {
2307 return CP_ACCESS_TRAP_UNCATEGORIZED;
2310 return CP_ACCESS_OK;
2313 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2314 bool isread)
2316 unsigned int cur_el = arm_current_el(env);
2317 bool secure = arm_is_secure(env);
2319 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
2320 if (cur_el == 0 &&
2321 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2322 return CP_ACCESS_TRAP;
2325 if (arm_feature(env, ARM_FEATURE_EL2) &&
2326 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
2327 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
2328 return CP_ACCESS_TRAP_EL2;
2330 return CP_ACCESS_OK;
2333 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2334 bool isread)
2336 unsigned int cur_el = arm_current_el(env);
2337 bool secure = arm_is_secure(env);
2339 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
2340 * EL0[PV]TEN is zero.
2342 if (cur_el == 0 &&
2343 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2344 return CP_ACCESS_TRAP;
2347 if (arm_feature(env, ARM_FEATURE_EL2) &&
2348 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
2349 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2350 return CP_ACCESS_TRAP_EL2;
2352 return CP_ACCESS_OK;
2355 static CPAccessResult gt_pct_access(CPUARMState *env,
2356 const ARMCPRegInfo *ri,
2357 bool isread)
2359 return gt_counter_access(env, GTIMER_PHYS, isread);
2362 static CPAccessResult gt_vct_access(CPUARMState *env,
2363 const ARMCPRegInfo *ri,
2364 bool isread)
2366 return gt_counter_access(env, GTIMER_VIRT, isread);
2369 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2370 bool isread)
2372 return gt_timer_access(env, GTIMER_PHYS, isread);
2375 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2376 bool isread)
2378 return gt_timer_access(env, GTIMER_VIRT, isread);
2381 static CPAccessResult gt_stimer_access(CPUARMState *env,
2382 const ARMCPRegInfo *ri,
2383 bool isread)
2385 /* The AArch64 register view of the secure physical timer is
2386 * always accessible from EL3, and configurably accessible from
2387 * Secure EL1.
2389 switch (arm_current_el(env)) {
2390 case 1:
2391 if (!arm_is_secure(env)) {
2392 return CP_ACCESS_TRAP;
2394 if (!(env->cp15.scr_el3 & SCR_ST)) {
2395 return CP_ACCESS_TRAP_EL3;
2397 return CP_ACCESS_OK;
2398 case 0:
2399 case 2:
2400 return CP_ACCESS_TRAP;
2401 case 3:
2402 return CP_ACCESS_OK;
2403 default:
2404 g_assert_not_reached();
2408 static uint64_t gt_get_countervalue(CPUARMState *env)
2410 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
2413 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2415 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2417 if (gt->ctl & 1) {
2418 /* Timer enabled: calculate and set current ISTATUS, irq, and
2419 * reset timer to when ISTATUS next has to change
2421 uint64_t offset = timeridx == GTIMER_VIRT ?
2422 cpu->env.cp15.cntvoff_el2 : 0;
2423 uint64_t count = gt_get_countervalue(&cpu->env);
2424 /* Note that this must be unsigned 64 bit arithmetic: */
2425 int istatus = count - offset >= gt->cval;
2426 uint64_t nexttick;
2427 int irqstate;
2429 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2431 irqstate = (istatus && !(gt->ctl & 2));
2432 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2434 if (istatus) {
2435 /* Next transition is when count rolls back over to zero */
2436 nexttick = UINT64_MAX;
2437 } else {
2438 /* Next transition is when we hit cval */
2439 nexttick = gt->cval + offset;
2441 /* Note that the desired next expiry time might be beyond the
2442 * signed-64-bit range of a QEMUTimer -- in this case we just
2443 * set the timer for as far in the future as possible. When the
2444 * timer expires we will reset the timer for any remaining period.
2446 if (nexttick > INT64_MAX / GTIMER_SCALE) {
2447 nexttick = INT64_MAX / GTIMER_SCALE;
2449 timer_mod(cpu->gt_timer[timeridx], nexttick);
2450 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2451 } else {
2452 /* Timer disabled: ISTATUS and timer output always clear */
2453 gt->ctl &= ~4;
2454 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2455 timer_del(cpu->gt_timer[timeridx]);
2456 trace_arm_gt_recalc_disabled(timeridx);
2460 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2461 int timeridx)
2463 ARMCPU *cpu = env_archcpu(env);
2465 timer_del(cpu->gt_timer[timeridx]);
2468 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2470 return gt_get_countervalue(env);
2473 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2475 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
2478 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2479 int timeridx,
2480 uint64_t value)
2482 trace_arm_gt_cval_write(timeridx, value);
2483 env->cp15.c14_timer[timeridx].cval = value;
2484 gt_recalc_timer(env_archcpu(env), timeridx);
2487 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2488 int timeridx)
2490 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
2492 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2493 (gt_get_countervalue(env) - offset));
2496 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2497 int timeridx,
2498 uint64_t value)
2500 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
2502 trace_arm_gt_tval_write(timeridx, value);
2503 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2504 sextract64(value, 0, 32);
2505 gt_recalc_timer(env_archcpu(env), timeridx);
2508 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2509 int timeridx,
2510 uint64_t value)
2512 ARMCPU *cpu = env_archcpu(env);
2513 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2515 trace_arm_gt_ctl_write(timeridx, value);
2516 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2517 if ((oldval ^ value) & 1) {
2518 /* Enable toggled */
2519 gt_recalc_timer(cpu, timeridx);
2520 } else if ((oldval ^ value) & 2) {
2521 /* IMASK toggled: don't need to recalculate,
2522 * just set the interrupt line based on ISTATUS
2524 int irqstate = (oldval & 4) && !(value & 2);
2526 trace_arm_gt_imask_toggle(timeridx, irqstate);
2527 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2531 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2533 gt_timer_reset(env, ri, GTIMER_PHYS);
2536 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2537 uint64_t value)
2539 gt_cval_write(env, ri, GTIMER_PHYS, value);
2542 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2544 return gt_tval_read(env, ri, GTIMER_PHYS);
2547 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2548 uint64_t value)
2550 gt_tval_write(env, ri, GTIMER_PHYS, value);
2553 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2554 uint64_t value)
2556 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2559 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2561 gt_timer_reset(env, ri, GTIMER_VIRT);
2564 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2565 uint64_t value)
2567 gt_cval_write(env, ri, GTIMER_VIRT, value);
2570 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2572 return gt_tval_read(env, ri, GTIMER_VIRT);
2575 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2576 uint64_t value)
2578 gt_tval_write(env, ri, GTIMER_VIRT, value);
2581 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2582 uint64_t value)
2584 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2587 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2588 uint64_t value)
2590 ARMCPU *cpu = env_archcpu(env);
2592 trace_arm_gt_cntvoff_write(value);
2593 raw_write(env, ri, value);
2594 gt_recalc_timer(cpu, GTIMER_VIRT);
2597 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2599 gt_timer_reset(env, ri, GTIMER_HYP);
2602 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2603 uint64_t value)
2605 gt_cval_write(env, ri, GTIMER_HYP, value);
2608 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2610 return gt_tval_read(env, ri, GTIMER_HYP);
2613 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2614 uint64_t value)
2616 gt_tval_write(env, ri, GTIMER_HYP, value);
2619 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2620 uint64_t value)
2622 gt_ctl_write(env, ri, GTIMER_HYP, value);
2625 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2627 gt_timer_reset(env, ri, GTIMER_SEC);
2630 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2631 uint64_t value)
2633 gt_cval_write(env, ri, GTIMER_SEC, value);
2636 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2638 return gt_tval_read(env, ri, GTIMER_SEC);
2641 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2642 uint64_t value)
2644 gt_tval_write(env, ri, GTIMER_SEC, value);
2647 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2648 uint64_t value)
2650 gt_ctl_write(env, ri, GTIMER_SEC, value);
2653 void arm_gt_ptimer_cb(void *opaque)
2655 ARMCPU *cpu = opaque;
2657 gt_recalc_timer(cpu, GTIMER_PHYS);
2660 void arm_gt_vtimer_cb(void *opaque)
2662 ARMCPU *cpu = opaque;
2664 gt_recalc_timer(cpu, GTIMER_VIRT);
2667 void arm_gt_htimer_cb(void *opaque)
2669 ARMCPU *cpu = opaque;
2671 gt_recalc_timer(cpu, GTIMER_HYP);
2674 void arm_gt_stimer_cb(void *opaque)
2676 ARMCPU *cpu = opaque;
2678 gt_recalc_timer(cpu, GTIMER_SEC);
2681 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2682 /* Note that CNTFRQ is purely reads-as-written for the benefit
2683 * of software; writing it doesn't actually change the timer frequency.
2684 * Our reset value matches the fixed frequency we implement the timer at.
2686 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2687 .type = ARM_CP_ALIAS,
2688 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2689 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2691 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2692 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2693 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2694 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2695 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
2697 /* overall control: mostly access permissions */
2698 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2699 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2700 .access = PL1_RW,
2701 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2702 .resetvalue = 0,
2704 /* per-timer control */
2705 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2706 .secure = ARM_CP_SECSTATE_NS,
2707 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2708 .accessfn = gt_ptimer_access,
2709 .fieldoffset = offsetoflow32(CPUARMState,
2710 cp15.c14_timer[GTIMER_PHYS].ctl),
2711 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
2713 { .name = "CNTP_CTL_S",
2714 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2715 .secure = ARM_CP_SECSTATE_S,
2716 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2717 .accessfn = gt_ptimer_access,
2718 .fieldoffset = offsetoflow32(CPUARMState,
2719 cp15.c14_timer[GTIMER_SEC].ctl),
2720 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2722 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2723 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2724 .type = ARM_CP_IO, .access = PL0_RW,
2725 .accessfn = gt_ptimer_access,
2726 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2727 .resetvalue = 0,
2728 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
2730 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2731 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2732 .accessfn = gt_vtimer_access,
2733 .fieldoffset = offsetoflow32(CPUARMState,
2734 cp15.c14_timer[GTIMER_VIRT].ctl),
2735 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
2737 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2738 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2739 .type = ARM_CP_IO, .access = PL0_RW,
2740 .accessfn = gt_vtimer_access,
2741 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2742 .resetvalue = 0,
2743 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
2745 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2746 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2747 .secure = ARM_CP_SECSTATE_NS,
2748 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2749 .accessfn = gt_ptimer_access,
2750 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
2752 { .name = "CNTP_TVAL_S",
2753 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2754 .secure = ARM_CP_SECSTATE_S,
2755 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2756 .accessfn = gt_ptimer_access,
2757 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2759 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2760 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2761 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2762 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2763 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
2765 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2766 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2767 .accessfn = gt_vtimer_access,
2768 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2770 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2771 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2772 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2773 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2774 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
2776 /* The counter itself */
2777 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
2778 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2779 .accessfn = gt_pct_access,
2780 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2782 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2783 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
2784 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2785 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
2787 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
2788 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2789 .accessfn = gt_vct_access,
2790 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
2792 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2793 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2794 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2795 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
2797 /* Comparison value, indicating when the timer goes off */
2798 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
2799 .secure = ARM_CP_SECSTATE_NS,
2800 .access = PL0_RW,
2801 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2802 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2803 .accessfn = gt_ptimer_access,
2804 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2806 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
2807 .secure = ARM_CP_SECSTATE_S,
2808 .access = PL0_RW,
2809 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2810 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2811 .accessfn = gt_ptimer_access,
2812 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2814 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2815 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
2816 .access = PL0_RW,
2817 .type = ARM_CP_IO,
2818 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2819 .resetvalue = 0, .accessfn = gt_ptimer_access,
2820 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
2822 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
2823 .access = PL0_RW,
2824 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2825 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2826 .accessfn = gt_vtimer_access,
2827 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2829 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2830 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
2831 .access = PL0_RW,
2832 .type = ARM_CP_IO,
2833 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
2834 .resetvalue = 0, .accessfn = gt_vtimer_access,
2835 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
2837 /* Secure timer -- this is actually restricted to only EL3
2838 * and configurably Secure-EL1 via the accessfn.
2840 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
2841 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
2842 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
2843 .accessfn = gt_stimer_access,
2844 .readfn = gt_sec_tval_read,
2845 .writefn = gt_sec_tval_write,
2846 .resetfn = gt_sec_timer_reset,
2848 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
2849 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
2850 .type = ARM_CP_IO, .access = PL1_RW,
2851 .accessfn = gt_stimer_access,
2852 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
2853 .resetvalue = 0,
2854 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2856 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
2857 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
2858 .type = ARM_CP_IO, .access = PL1_RW,
2859 .accessfn = gt_stimer_access,
2860 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2861 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2863 REGINFO_SENTINEL
2866 #else
2868 /* In user-mode most of the generic timer registers are inaccessible
2869 * however modern kernels (4.12+) allow access to cntvct_el0
2872 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2874 /* Currently we have no support for QEMUTimer in linux-user so we
2875 * can't call gt_get_countervalue(env), instead we directly
2876 * call the lower level functions.
2878 return cpu_get_clock() / GTIMER_SCALE;
2881 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2882 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2883 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2884 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
2885 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2886 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
2888 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2889 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2890 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2891 .readfn = gt_virt_cnt_read,
2893 REGINFO_SENTINEL
2896 #endif
2898 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2900 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2901 raw_write(env, ri, value);
2902 } else if (arm_feature(env, ARM_FEATURE_V7)) {
2903 raw_write(env, ri, value & 0xfffff6ff);
2904 } else {
2905 raw_write(env, ri, value & 0xfffff1ff);
2909 #ifndef CONFIG_USER_ONLY
2910 /* get_phys_addr() isn't present for user-mode-only targets */
2912 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
2913 bool isread)
2915 if (ri->opc2 & 4) {
2916 /* The ATS12NSO* operations must trap to EL3 if executed in
2917 * Secure EL1 (which can only happen if EL3 is AArch64).
2918 * They are simply UNDEF if executed from NS EL1.
2919 * They function normally from EL2 or EL3.
2921 if (arm_current_el(env) == 1) {
2922 if (arm_is_secure_below_el3(env)) {
2923 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
2925 return CP_ACCESS_TRAP_UNCATEGORIZED;
2928 return CP_ACCESS_OK;
2931 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
2932 MMUAccessType access_type, ARMMMUIdx mmu_idx)
2934 hwaddr phys_addr;
2935 target_ulong page_size;
2936 int prot;
2937 bool ret;
2938 uint64_t par64;
2939 bool format64 = false;
2940 MemTxAttrs attrs = {};
2941 ARMMMUFaultInfo fi = {};
2942 ARMCacheAttrs cacheattrs = {};
2944 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
2945 &prot, &page_size, &fi, &cacheattrs);
2947 if (is_a64(env)) {
2948 format64 = true;
2949 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
2951 * ATS1Cxx:
2952 * * TTBCR.EAE determines whether the result is returned using the
2953 * 32-bit or the 64-bit PAR format
2954 * * Instructions executed in Hyp mode always use the 64bit format
2956 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
2957 * * The Non-secure TTBCR.EAE bit is set to 1
2958 * * The implementation includes EL2, and the value of HCR.VM is 1
2960 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
2962 * ATS1Hx always uses the 64bit format.
2964 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
2966 if (arm_feature(env, ARM_FEATURE_EL2)) {
2967 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
2968 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
2969 } else {
2970 format64 |= arm_current_el(env) == 2;
2975 if (format64) {
2976 /* Create a 64-bit PAR */
2977 par64 = (1 << 11); /* LPAE bit always set */
2978 if (!ret) {
2979 par64 |= phys_addr & ~0xfffULL;
2980 if (!attrs.secure) {
2981 par64 |= (1 << 9); /* NS */
2983 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
2984 par64 |= cacheattrs.shareability << 7; /* SH */
2985 } else {
2986 uint32_t fsr = arm_fi_to_lfsc(&fi);
2988 par64 |= 1; /* F */
2989 par64 |= (fsr & 0x3f) << 1; /* FS */
2990 if (fi.stage2) {
2991 par64 |= (1 << 9); /* S */
2993 if (fi.s1ptw) {
2994 par64 |= (1 << 8); /* PTW */
2997 } else {
2998 /* fsr is a DFSR/IFSR value for the short descriptor
2999 * translation table format (with WnR always clear).
3000 * Convert it to a 32-bit PAR.
3002 if (!ret) {
3003 /* We do not set any attribute bits in the PAR */
3004 if (page_size == (1 << 24)
3005 && arm_feature(env, ARM_FEATURE_V7)) {
3006 par64 = (phys_addr & 0xff000000) | (1 << 1);
3007 } else {
3008 par64 = phys_addr & 0xfffff000;
3010 if (!attrs.secure) {
3011 par64 |= (1 << 9); /* NS */
3013 } else {
3014 uint32_t fsr = arm_fi_to_sfsc(&fi);
3016 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3017 ((fsr & 0xf) << 1) | 1;
3020 return par64;
3023 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3025 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3026 uint64_t par64;
3027 ARMMMUIdx mmu_idx;
3028 int el = arm_current_el(env);
3029 bool secure = arm_is_secure_below_el3(env);
3031 switch (ri->opc2 & 6) {
3032 case 0:
3033 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
3034 switch (el) {
3035 case 3:
3036 mmu_idx = ARMMMUIdx_S1E3;
3037 break;
3038 case 2:
3039 mmu_idx = ARMMMUIdx_S1NSE1;
3040 break;
3041 case 1:
3042 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
3043 break;
3044 default:
3045 g_assert_not_reached();
3047 break;
3048 case 2:
3049 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3050 switch (el) {
3051 case 3:
3052 mmu_idx = ARMMMUIdx_S1SE0;
3053 break;
3054 case 2:
3055 mmu_idx = ARMMMUIdx_S1NSE0;
3056 break;
3057 case 1:
3058 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
3059 break;
3060 default:
3061 g_assert_not_reached();
3063 break;
3064 case 4:
3065 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3066 mmu_idx = ARMMMUIdx_S12NSE1;
3067 break;
3068 case 6:
3069 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3070 mmu_idx = ARMMMUIdx_S12NSE0;
3071 break;
3072 default:
3073 g_assert_not_reached();
3076 par64 = do_ats_write(env, value, access_type, mmu_idx);
3078 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3081 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3082 uint64_t value)
3084 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3085 uint64_t par64;
3087 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S1E2);
3089 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3092 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3093 bool isread)
3095 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3096 return CP_ACCESS_TRAP;
3098 return CP_ACCESS_OK;
3101 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3102 uint64_t value)
3104 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3105 ARMMMUIdx mmu_idx;
3106 int secure = arm_is_secure_below_el3(env);
3108 switch (ri->opc2 & 6) {
3109 case 0:
3110 switch (ri->opc1) {
3111 case 0: /* AT S1E1R, AT S1E1W */
3112 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
3113 break;
3114 case 4: /* AT S1E2R, AT S1E2W */
3115 mmu_idx = ARMMMUIdx_S1E2;
3116 break;
3117 case 6: /* AT S1E3R, AT S1E3W */
3118 mmu_idx = ARMMMUIdx_S1E3;
3119 break;
3120 default:
3121 g_assert_not_reached();
3123 break;
3124 case 2: /* AT S1E0R, AT S1E0W */
3125 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
3126 break;
3127 case 4: /* AT S12E1R, AT S12E1W */
3128 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
3129 break;
3130 case 6: /* AT S12E0R, AT S12E0W */
3131 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
3132 break;
3133 default:
3134 g_assert_not_reached();
3137 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3139 #endif
3141 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3142 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3143 .access = PL1_RW, .resetvalue = 0,
3144 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3145 offsetoflow32(CPUARMState, cp15.par_ns) },
3146 .writefn = par_write },
3147 #ifndef CONFIG_USER_ONLY
3148 /* This underdecoding is safe because the reginfo is NO_RAW. */
3149 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3150 .access = PL1_W, .accessfn = ats_access,
3151 .writefn = ats_write, .type = ARM_CP_NO_RAW },
3152 #endif
3153 REGINFO_SENTINEL
3156 /* Return basic MPU access permission bits. */
3157 static uint32_t simple_mpu_ap_bits(uint32_t val)
3159 uint32_t ret;
3160 uint32_t mask;
3161 int i;
3162 ret = 0;
3163 mask = 3;
3164 for (i = 0; i < 16; i += 2) {
3165 ret |= (val >> i) & mask;
3166 mask <<= 2;
3168 return ret;
3171 /* Pad basic MPU access permission bits to extended format. */
3172 static uint32_t extended_mpu_ap_bits(uint32_t val)
3174 uint32_t ret;
3175 uint32_t mask;
3176 int i;
3177 ret = 0;
3178 mask = 3;
3179 for (i = 0; i < 16; i += 2) {
3180 ret |= (val & mask) << i;
3181 mask <<= 2;
3183 return ret;
3186 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3187 uint64_t value)
3189 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3192 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3194 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3197 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3198 uint64_t value)
3200 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3203 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3205 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3208 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3210 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3212 if (!u32p) {
3213 return 0;
3216 u32p += env->pmsav7.rnr[M_REG_NS];
3217 return *u32p;
3220 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3221 uint64_t value)
3223 ARMCPU *cpu = env_archcpu(env);
3224 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3226 if (!u32p) {
3227 return;
3230 u32p += env->pmsav7.rnr[M_REG_NS];
3231 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3232 *u32p = value;
3235 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3236 uint64_t value)
3238 ARMCPU *cpu = env_archcpu(env);
3239 uint32_t nrgs = cpu->pmsav7_dregion;
3241 if (value >= nrgs) {
3242 qemu_log_mask(LOG_GUEST_ERROR,
3243 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3244 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3245 return;
3248 raw_write(env, ri, value);
3251 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3252 /* Reset for all these registers is handled in arm_cpu_reset(),
3253 * because the PMSAv7 is also used by M-profile CPUs, which do
3254 * not register cpregs but still need the state to be reset.
3256 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3257 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3258 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3259 .readfn = pmsav7_read, .writefn = pmsav7_write,
3260 .resetfn = arm_cp_reset_ignore },
3261 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3262 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3263 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3264 .readfn = pmsav7_read, .writefn = pmsav7_write,
3265 .resetfn = arm_cp_reset_ignore },
3266 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3267 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3268 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3269 .readfn = pmsav7_read, .writefn = pmsav7_write,
3270 .resetfn = arm_cp_reset_ignore },
3271 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3272 .access = PL1_RW,
3273 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3274 .writefn = pmsav7_rgnr_write,
3275 .resetfn = arm_cp_reset_ignore },
3276 REGINFO_SENTINEL
3279 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3280 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3281 .access = PL1_RW, .type = ARM_CP_ALIAS,
3282 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3283 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3284 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3285 .access = PL1_RW, .type = ARM_CP_ALIAS,
3286 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3287 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3288 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3289 .access = PL1_RW,
3290 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3291 .resetvalue = 0, },
3292 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3293 .access = PL1_RW,
3294 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3295 .resetvalue = 0, },
3296 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3297 .access = PL1_RW,
3298 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3299 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3300 .access = PL1_RW,
3301 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3302 /* Protection region base and size registers */
3303 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3304 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3305 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3306 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3307 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3308 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3309 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3310 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3311 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3312 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3313 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3314 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3315 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3316 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3317 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3318 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3319 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3320 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3321 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3322 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3323 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3324 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3325 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3326 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3327 REGINFO_SENTINEL
3330 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3331 uint64_t value)
3333 TCR *tcr = raw_ptr(env, ri);
3334 int maskshift = extract32(value, 0, 3);
3336 if (!arm_feature(env, ARM_FEATURE_V8)) {
3337 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3338 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3339 * using Long-desciptor translation table format */
3340 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3341 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3342 /* In an implementation that includes the Security Extensions
3343 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3344 * Short-descriptor translation table format.
3346 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3347 } else {
3348 value &= TTBCR_N;
3352 /* Update the masks corresponding to the TCR bank being written
3353 * Note that we always calculate mask and base_mask, but
3354 * they are only used for short-descriptor tables (ie if EAE is 0);
3355 * for long-descriptor tables the TCR fields are used differently
3356 * and the mask and base_mask values are meaningless.
3358 tcr->raw_tcr = value;
3359 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3360 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
3363 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3364 uint64_t value)
3366 ARMCPU *cpu = env_archcpu(env);
3367 TCR *tcr = raw_ptr(env, ri);
3369 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3370 /* With LPAE the TTBCR could result in a change of ASID
3371 * via the TTBCR.A1 bit, so do a TLB flush.
3373 tlb_flush(CPU(cpu));
3375 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3376 value = deposit64(tcr->raw_tcr, 0, 32, value);
3377 vmsa_ttbcr_raw_write(env, ri, value);
3380 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3382 TCR *tcr = raw_ptr(env, ri);
3384 /* Reset both the TCR as well as the masks corresponding to the bank of
3385 * the TCR being reset.
3387 tcr->raw_tcr = 0;
3388 tcr->mask = 0;
3389 tcr->base_mask = 0xffffc000u;
3392 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3393 uint64_t value)
3395 ARMCPU *cpu = env_archcpu(env);
3396 TCR *tcr = raw_ptr(env, ri);
3398 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3399 tlb_flush(CPU(cpu));
3400 tcr->raw_tcr = value;
3403 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3404 uint64_t value)
3406 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3407 if (cpreg_field_is_64bit(ri) &&
3408 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3409 ARMCPU *cpu = env_archcpu(env);
3410 tlb_flush(CPU(cpu));
3412 raw_write(env, ri, value);
3415 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3416 uint64_t value)
3418 ARMCPU *cpu = env_archcpu(env);
3419 CPUState *cs = CPU(cpu);
3421 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
3422 if (raw_read(env, ri) != value) {
3423 tlb_flush_by_mmuidx(cs,
3424 ARMMMUIdxBit_S12NSE1 |
3425 ARMMMUIdxBit_S12NSE0 |
3426 ARMMMUIdxBit_S2NS);
3427 raw_write(env, ri, value);
3431 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3432 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3433 .access = PL1_RW, .type = ARM_CP_ALIAS,
3434 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3435 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3436 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3437 .access = PL1_RW, .resetvalue = 0,
3438 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3439 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3440 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3441 .access = PL1_RW, .resetvalue = 0,
3442 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3443 offsetof(CPUARMState, cp15.dfar_ns) } },
3444 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3445 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3446 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3447 .resetvalue = 0, },
3448 REGINFO_SENTINEL
3451 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3452 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3453 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3454 .access = PL1_RW,
3455 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3456 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3457 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3458 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3459 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3460 offsetof(CPUARMState, cp15.ttbr0_ns) } },
3461 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3462 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3463 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3464 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3465 offsetof(CPUARMState, cp15.ttbr1_ns) } },
3466 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3467 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3468 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
3469 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3470 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3471 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3472 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3473 .raw_writefn = vmsa_ttbcr_raw_write,
3474 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3475 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
3476 REGINFO_SENTINEL
3479 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3480 * qemu tlbs nor adjusting cached masks.
3482 static const ARMCPRegInfo ttbcr2_reginfo = {
3483 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3484 .access = PL1_RW, .type = ARM_CP_ALIAS,
3485 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3486 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
3489 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3490 uint64_t value)
3492 env->cp15.c15_ticonfig = value & 0xe7;
3493 /* The OS_TYPE bit in this register changes the reported CPUID! */
3494 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3495 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3498 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3499 uint64_t value)
3501 env->cp15.c15_threadid = value & 0xffff;
3504 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3505 uint64_t value)
3507 /* Wait-for-interrupt (deprecated) */
3508 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3511 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3512 uint64_t value)
3514 /* On OMAP there are registers indicating the max/min index of dcache lines
3515 * containing a dirty line; cache flush operations have to reset these.
3517 env->cp15.c15_i_max = 0x000;
3518 env->cp15.c15_i_min = 0xff0;
3521 static const ARMCPRegInfo omap_cp_reginfo[] = {
3522 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3523 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3524 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3525 .resetvalue = 0, },
3526 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3527 .access = PL1_RW, .type = ARM_CP_NOP },
3528 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3529 .access = PL1_RW,
3530 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3531 .writefn = omap_ticonfig_write },
3532 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3533 .access = PL1_RW,
3534 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3535 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3536 .access = PL1_RW, .resetvalue = 0xff0,
3537 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3538 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3539 .access = PL1_RW,
3540 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3541 .writefn = omap_threadid_write },
3542 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3543 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3544 .type = ARM_CP_NO_RAW,
3545 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3546 /* TODO: Peripheral port remap register:
3547 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3548 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3549 * when MMU is off.
3551 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3552 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3553 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3554 .writefn = omap_cachemaint_write },
3555 { .name = "C9", .cp = 15, .crn = 9,
3556 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3557 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3558 REGINFO_SENTINEL
3561 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3562 uint64_t value)
3564 env->cp15.c15_cpar = value & 0x3fff;
3567 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3568 { .name = "XSCALE_CPAR",
3569 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3570 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3571 .writefn = xscale_cpar_write, },
3572 { .name = "XSCALE_AUXCR",
3573 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3574 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3575 .resetvalue = 0, },
3576 /* XScale specific cache-lockdown: since we have no cache we NOP these
3577 * and hope the guest does not really rely on cache behaviour.
3579 { .name = "XSCALE_LOCK_ICACHE_LINE",
3580 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3581 .access = PL1_W, .type = ARM_CP_NOP },
3582 { .name = "XSCALE_UNLOCK_ICACHE",
3583 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3584 .access = PL1_W, .type = ARM_CP_NOP },
3585 { .name = "XSCALE_DCACHE_LOCK",
3586 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3587 .access = PL1_RW, .type = ARM_CP_NOP },
3588 { .name = "XSCALE_UNLOCK_DCACHE",
3589 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3590 .access = PL1_W, .type = ARM_CP_NOP },
3591 REGINFO_SENTINEL
3594 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3595 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3596 * implementation of this implementation-defined space.
3597 * Ideally this should eventually disappear in favour of actually
3598 * implementing the correct behaviour for all cores.
3600 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3601 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3602 .access = PL1_RW,
3603 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3604 .resetvalue = 0 },
3605 REGINFO_SENTINEL
3608 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3609 /* Cache status: RAZ because we have no cache so it's always clean */
3610 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3611 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3612 .resetvalue = 0 },
3613 REGINFO_SENTINEL
3616 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3617 /* We never have a a block transfer operation in progress */
3618 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
3619 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3620 .resetvalue = 0 },
3621 /* The cache ops themselves: these all NOP for QEMU */
3622 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3623 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3624 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3625 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3626 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3627 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3628 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
3629 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3630 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
3631 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3632 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
3633 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3634 REGINFO_SENTINEL
3637 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
3638 /* The cache test-and-clean instructions always return (1 << 30)
3639 * to indicate that there are no dirty cache lines.
3641 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
3642 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3643 .resetvalue = (1 << 30) },
3644 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
3645 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3646 .resetvalue = (1 << 30) },
3647 REGINFO_SENTINEL
3650 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
3651 /* Ignore ReadBuffer accesses */
3652 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
3653 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3654 .access = PL1_RW, .resetvalue = 0,
3655 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
3656 REGINFO_SENTINEL
3659 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3661 ARMCPU *cpu = env_archcpu(env);
3662 unsigned int cur_el = arm_current_el(env);
3663 bool secure = arm_is_secure(env);
3665 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
3666 return env->cp15.vpidr_el2;
3668 return raw_read(env, ri);
3671 static uint64_t mpidr_read_val(CPUARMState *env)
3673 ARMCPU *cpu = env_archcpu(env);
3674 uint64_t mpidr = cpu->mp_affinity;
3676 if (arm_feature(env, ARM_FEATURE_V7MP)) {
3677 mpidr |= (1U << 31);
3678 /* Cores which are uniprocessor (non-coherent)
3679 * but still implement the MP extensions set
3680 * bit 30. (For instance, Cortex-R5).
3682 if (cpu->mp_is_up) {
3683 mpidr |= (1u << 30);
3686 return mpidr;
3689 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3691 unsigned int cur_el = arm_current_el(env);
3692 bool secure = arm_is_secure(env);
3694 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
3695 return env->cp15.vmpidr_el2;
3697 return mpidr_read_val(env);
3700 static const ARMCPRegInfo lpae_cp_reginfo[] = {
3701 /* NOP AMAIR0/1 */
3702 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
3703 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
3704 .access = PL1_RW, .type = ARM_CP_CONST,
3705 .resetvalue = 0 },
3706 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
3707 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
3708 .access = PL1_RW, .type = ARM_CP_CONST,
3709 .resetvalue = 0 },
3710 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
3711 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
3712 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
3713 offsetof(CPUARMState, cp15.par_ns)} },
3714 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
3715 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3716 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3717 offsetof(CPUARMState, cp15.ttbr0_ns) },
3718 .writefn = vmsa_ttbr_write, },
3719 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
3720 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3721 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3722 offsetof(CPUARMState, cp15.ttbr1_ns) },
3723 .writefn = vmsa_ttbr_write, },
3724 REGINFO_SENTINEL
3727 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3729 return vfp_get_fpcr(env);
3732 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3733 uint64_t value)
3735 vfp_set_fpcr(env, value);
3738 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3740 return vfp_get_fpsr(env);
3743 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3744 uint64_t value)
3746 vfp_set_fpsr(env, value);
3749 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
3750 bool isread)
3752 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
3753 return CP_ACCESS_TRAP;
3755 return CP_ACCESS_OK;
3758 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
3759 uint64_t value)
3761 env->daif = value & PSTATE_DAIF;
3764 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
3765 const ARMCPRegInfo *ri,
3766 bool isread)
3768 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
3769 * SCTLR_EL1.UCI is set.
3771 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
3772 return CP_ACCESS_TRAP;
3774 return CP_ACCESS_OK;
3777 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
3778 * Page D4-1736 (DDI0487A.b)
3781 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3782 uint64_t value)
3784 CPUState *cs = env_cpu(env);
3785 bool sec = arm_is_secure_below_el3(env);
3787 if (sec) {
3788 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3789 ARMMMUIdxBit_S1SE1 |
3790 ARMMMUIdxBit_S1SE0);
3791 } else {
3792 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3793 ARMMMUIdxBit_S12NSE1 |
3794 ARMMMUIdxBit_S12NSE0);
3798 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3799 uint64_t value)
3801 CPUState *cs = env_cpu(env);
3803 if (tlb_force_broadcast(env)) {
3804 tlbi_aa64_vmalle1is_write(env, NULL, value);
3805 return;
3808 if (arm_is_secure_below_el3(env)) {
3809 tlb_flush_by_mmuidx(cs,
3810 ARMMMUIdxBit_S1SE1 |
3811 ARMMMUIdxBit_S1SE0);
3812 } else {
3813 tlb_flush_by_mmuidx(cs,
3814 ARMMMUIdxBit_S12NSE1 |
3815 ARMMMUIdxBit_S12NSE0);
3819 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3820 uint64_t value)
3822 /* Note that the 'ALL' scope must invalidate both stage 1 and
3823 * stage 2 translations, whereas most other scopes only invalidate
3824 * stage 1 translations.
3826 ARMCPU *cpu = env_archcpu(env);
3827 CPUState *cs = CPU(cpu);
3829 if (arm_is_secure_below_el3(env)) {
3830 tlb_flush_by_mmuidx(cs,
3831 ARMMMUIdxBit_S1SE1 |
3832 ARMMMUIdxBit_S1SE0);
3833 } else {
3834 if (arm_feature(env, ARM_FEATURE_EL2)) {
3835 tlb_flush_by_mmuidx(cs,
3836 ARMMMUIdxBit_S12NSE1 |
3837 ARMMMUIdxBit_S12NSE0 |
3838 ARMMMUIdxBit_S2NS);
3839 } else {
3840 tlb_flush_by_mmuidx(cs,
3841 ARMMMUIdxBit_S12NSE1 |
3842 ARMMMUIdxBit_S12NSE0);
3847 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3848 uint64_t value)
3850 ARMCPU *cpu = env_archcpu(env);
3851 CPUState *cs = CPU(cpu);
3853 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2);
3856 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3857 uint64_t value)
3859 ARMCPU *cpu = env_archcpu(env);
3860 CPUState *cs = CPU(cpu);
3862 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3);
3865 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3866 uint64_t value)
3868 /* Note that the 'ALL' scope must invalidate both stage 1 and
3869 * stage 2 translations, whereas most other scopes only invalidate
3870 * stage 1 translations.
3872 CPUState *cs = env_cpu(env);
3873 bool sec = arm_is_secure_below_el3(env);
3874 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
3876 if (sec) {
3877 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3878 ARMMMUIdxBit_S1SE1 |
3879 ARMMMUIdxBit_S1SE0);
3880 } else if (has_el2) {
3881 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3882 ARMMMUIdxBit_S12NSE1 |
3883 ARMMMUIdxBit_S12NSE0 |
3884 ARMMMUIdxBit_S2NS);
3885 } else {
3886 tlb_flush_by_mmuidx_all_cpus_synced(cs,
3887 ARMMMUIdxBit_S12NSE1 |
3888 ARMMMUIdxBit_S12NSE0);
3892 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3893 uint64_t value)
3895 CPUState *cs = env_cpu(env);
3897 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2);
3900 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3901 uint64_t value)
3903 CPUState *cs = env_cpu(env);
3905 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3);
3908 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3909 uint64_t value)
3911 /* Invalidate by VA, EL2
3912 * Currently handles both VAE2 and VALE2, since we don't support
3913 * flush-last-level-only.
3915 ARMCPU *cpu = env_archcpu(env);
3916 CPUState *cs = CPU(cpu);
3917 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3919 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2);
3922 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
3923 uint64_t value)
3925 /* Invalidate by VA, EL3
3926 * Currently handles both VAE3 and VALE3, since we don't support
3927 * flush-last-level-only.
3929 ARMCPU *cpu = env_archcpu(env);
3930 CPUState *cs = CPU(cpu);
3931 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3933 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3);
3936 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3937 uint64_t value)
3939 ARMCPU *cpu = env_archcpu(env);
3940 CPUState *cs = CPU(cpu);
3941 bool sec = arm_is_secure_below_el3(env);
3942 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3944 if (sec) {
3945 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3946 ARMMMUIdxBit_S1SE1 |
3947 ARMMMUIdxBit_S1SE0);
3948 } else {
3949 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3950 ARMMMUIdxBit_S12NSE1 |
3951 ARMMMUIdxBit_S12NSE0);
3955 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
3956 uint64_t value)
3958 /* Invalidate by VA, EL1&0 (AArch64 version).
3959 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
3960 * since we don't support flush-for-specific-ASID-only or
3961 * flush-last-level-only.
3963 ARMCPU *cpu = env_archcpu(env);
3964 CPUState *cs = CPU(cpu);
3965 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3967 if (tlb_force_broadcast(env)) {
3968 tlbi_aa64_vae1is_write(env, NULL, value);
3969 return;
3972 if (arm_is_secure_below_el3(env)) {
3973 tlb_flush_page_by_mmuidx(cs, pageaddr,
3974 ARMMMUIdxBit_S1SE1 |
3975 ARMMMUIdxBit_S1SE0);
3976 } else {
3977 tlb_flush_page_by_mmuidx(cs, pageaddr,
3978 ARMMMUIdxBit_S12NSE1 |
3979 ARMMMUIdxBit_S12NSE0);
3983 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3984 uint64_t value)
3986 CPUState *cs = env_cpu(env);
3987 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3989 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
3990 ARMMMUIdxBit_S1E2);
3993 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
3994 uint64_t value)
3996 CPUState *cs = env_cpu(env);
3997 uint64_t pageaddr = sextract64(value << 12, 0, 56);
3999 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
4000 ARMMMUIdxBit_S1E3);
4003 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4004 uint64_t value)
4006 /* Invalidate by IPA. This has to invalidate any structures that
4007 * contain only stage 2 translation information, but does not need
4008 * to apply to structures that contain combined stage 1 and stage 2
4009 * translation information.
4010 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
4012 ARMCPU *cpu = env_archcpu(env);
4013 CPUState *cs = CPU(cpu);
4014 uint64_t pageaddr;
4016 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4017 return;
4020 pageaddr = sextract64(value << 12, 0, 48);
4022 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS);
4025 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4026 uint64_t value)
4028 CPUState *cs = env_cpu(env);
4029 uint64_t pageaddr;
4031 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4032 return;
4035 pageaddr = sextract64(value << 12, 0, 48);
4037 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
4038 ARMMMUIdxBit_S2NS);
4041 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4042 bool isread)
4044 /* We don't implement EL2, so the only control on DC ZVA is the
4045 * bit in the SCTLR which can prohibit access for EL0.
4047 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4048 return CP_ACCESS_TRAP;
4050 return CP_ACCESS_OK;
4053 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4055 ARMCPU *cpu = env_archcpu(env);
4056 int dzp_bit = 1 << 4;
4058 /* DZP indicates whether DC ZVA access is allowed */
4059 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4060 dzp_bit = 0;
4062 return cpu->dcz_blocksize | dzp_bit;
4065 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4066 bool isread)
4068 if (!(env->pstate & PSTATE_SP)) {
4069 /* Access to SP_EL0 is undefined if it's being used as
4070 * the stack pointer.
4072 return CP_ACCESS_TRAP_UNCATEGORIZED;
4074 return CP_ACCESS_OK;
4077 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4079 return env->pstate & PSTATE_SP;
4082 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4084 update_spsel(env, val);
4087 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4088 uint64_t value)
4090 ARMCPU *cpu = env_archcpu(env);
4092 if (raw_read(env, ri) == value) {
4093 /* Skip the TLB flush if nothing actually changed; Linux likes
4094 * to do a lot of pointless SCTLR writes.
4096 return;
4099 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4100 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4101 value &= ~SCTLR_M;
4104 raw_write(env, ri, value);
4105 /* ??? Lots of these bits are not implemented. */
4106 /* This may enable/disable the MMU, so do a TLB flush. */
4107 tlb_flush(CPU(cpu));
4110 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4111 bool isread)
4113 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
4114 return CP_ACCESS_TRAP_FP_EL2;
4116 if (env->cp15.cptr_el[3] & CPTR_TFP) {
4117 return CP_ACCESS_TRAP_FP_EL3;
4119 return CP_ACCESS_OK;
4122 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4123 uint64_t value)
4125 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4128 static const ARMCPRegInfo v8_cp_reginfo[] = {
4129 /* Minimal set of EL0-visible registers. This will need to be expanded
4130 * significantly for system emulation of AArch64 CPUs.
4132 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4133 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4134 .access = PL0_RW, .type = ARM_CP_NZCV },
4135 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4136 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4137 .type = ARM_CP_NO_RAW,
4138 .access = PL0_RW, .accessfn = aa64_daif_access,
4139 .fieldoffset = offsetof(CPUARMState, daif),
4140 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4141 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4142 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4143 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4144 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4145 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4146 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4147 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4148 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4149 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4150 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4151 .access = PL0_R, .type = ARM_CP_NO_RAW,
4152 .readfn = aa64_dczid_read },
4153 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4154 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4155 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4156 #ifndef CONFIG_USER_ONLY
4157 /* Avoid overhead of an access check that always passes in user-mode */
4158 .accessfn = aa64_zva_access,
4159 #endif
4161 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4162 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4163 .access = PL1_R, .type = ARM_CP_CURRENTEL },
4164 /* Cache ops: all NOPs since we don't emulate caches */
4165 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4166 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4167 .access = PL1_W, .type = ARM_CP_NOP },
4168 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4169 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4170 .access = PL1_W, .type = ARM_CP_NOP },
4171 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4172 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4173 .access = PL0_W, .type = ARM_CP_NOP,
4174 .accessfn = aa64_cacheop_access },
4175 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4176 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4177 .access = PL1_W, .type = ARM_CP_NOP },
4178 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4179 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4180 .access = PL1_W, .type = ARM_CP_NOP },
4181 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4182 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4183 .access = PL0_W, .type = ARM_CP_NOP,
4184 .accessfn = aa64_cacheop_access },
4185 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4186 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4187 .access = PL1_W, .type = ARM_CP_NOP },
4188 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4189 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4190 .access = PL0_W, .type = ARM_CP_NOP,
4191 .accessfn = aa64_cacheop_access },
4192 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4193 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4194 .access = PL0_W, .type = ARM_CP_NOP,
4195 .accessfn = aa64_cacheop_access },
4196 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4197 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4198 .access = PL1_W, .type = ARM_CP_NOP },
4199 /* TLBI operations */
4200 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4201 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4202 .access = PL1_W, .type = ARM_CP_NO_RAW,
4203 .writefn = tlbi_aa64_vmalle1is_write },
4204 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4205 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4206 .access = PL1_W, .type = ARM_CP_NO_RAW,
4207 .writefn = tlbi_aa64_vae1is_write },
4208 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4209 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4210 .access = PL1_W, .type = ARM_CP_NO_RAW,
4211 .writefn = tlbi_aa64_vmalle1is_write },
4212 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4213 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4214 .access = PL1_W, .type = ARM_CP_NO_RAW,
4215 .writefn = tlbi_aa64_vae1is_write },
4216 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4217 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4218 .access = PL1_W, .type = ARM_CP_NO_RAW,
4219 .writefn = tlbi_aa64_vae1is_write },
4220 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4221 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4222 .access = PL1_W, .type = ARM_CP_NO_RAW,
4223 .writefn = tlbi_aa64_vae1is_write },
4224 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4225 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4226 .access = PL1_W, .type = ARM_CP_NO_RAW,
4227 .writefn = tlbi_aa64_vmalle1_write },
4228 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4229 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4230 .access = PL1_W, .type = ARM_CP_NO_RAW,
4231 .writefn = tlbi_aa64_vae1_write },
4232 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4233 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4234 .access = PL1_W, .type = ARM_CP_NO_RAW,
4235 .writefn = tlbi_aa64_vmalle1_write },
4236 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4237 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4238 .access = PL1_W, .type = ARM_CP_NO_RAW,
4239 .writefn = tlbi_aa64_vae1_write },
4240 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4241 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4242 .access = PL1_W, .type = ARM_CP_NO_RAW,
4243 .writefn = tlbi_aa64_vae1_write },
4244 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4245 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4246 .access = PL1_W, .type = ARM_CP_NO_RAW,
4247 .writefn = tlbi_aa64_vae1_write },
4248 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4249 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4250 .access = PL2_W, .type = ARM_CP_NO_RAW,
4251 .writefn = tlbi_aa64_ipas2e1is_write },
4252 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4253 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4254 .access = PL2_W, .type = ARM_CP_NO_RAW,
4255 .writefn = tlbi_aa64_ipas2e1is_write },
4256 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4257 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4258 .access = PL2_W, .type = ARM_CP_NO_RAW,
4259 .writefn = tlbi_aa64_alle1is_write },
4260 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4261 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4262 .access = PL2_W, .type = ARM_CP_NO_RAW,
4263 .writefn = tlbi_aa64_alle1is_write },
4264 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4265 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4266 .access = PL2_W, .type = ARM_CP_NO_RAW,
4267 .writefn = tlbi_aa64_ipas2e1_write },
4268 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4269 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4270 .access = PL2_W, .type = ARM_CP_NO_RAW,
4271 .writefn = tlbi_aa64_ipas2e1_write },
4272 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4273 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4274 .access = PL2_W, .type = ARM_CP_NO_RAW,
4275 .writefn = tlbi_aa64_alle1_write },
4276 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4277 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4278 .access = PL2_W, .type = ARM_CP_NO_RAW,
4279 .writefn = tlbi_aa64_alle1is_write },
4280 #ifndef CONFIG_USER_ONLY
4281 /* 64 bit address translation operations */
4282 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4283 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4284 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4285 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4286 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4287 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4288 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4289 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4290 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4291 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4292 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4293 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4294 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4295 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4296 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4297 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4298 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4299 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4300 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4301 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4302 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4303 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4304 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4305 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4306 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4307 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4308 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4309 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4310 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4311 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4312 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4313 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4314 .type = ARM_CP_ALIAS,
4315 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4316 .access = PL1_RW, .resetvalue = 0,
4317 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4318 .writefn = par_write },
4319 #endif
4320 /* TLB invalidate last level of translation table walk */
4321 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4322 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
4323 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4324 .type = ARM_CP_NO_RAW, .access = PL1_W,
4325 .writefn = tlbimvaa_is_write },
4326 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4327 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
4328 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4329 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
4330 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4331 .type = ARM_CP_NO_RAW, .access = PL2_W,
4332 .writefn = tlbimva_hyp_write },
4333 { .name = "TLBIMVALHIS",
4334 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4335 .type = ARM_CP_NO_RAW, .access = PL2_W,
4336 .writefn = tlbimva_hyp_is_write },
4337 { .name = "TLBIIPAS2",
4338 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4339 .type = ARM_CP_NO_RAW, .access = PL2_W,
4340 .writefn = tlbiipas2_write },
4341 { .name = "TLBIIPAS2IS",
4342 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4343 .type = ARM_CP_NO_RAW, .access = PL2_W,
4344 .writefn = tlbiipas2_is_write },
4345 { .name = "TLBIIPAS2L",
4346 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4347 .type = ARM_CP_NO_RAW, .access = PL2_W,
4348 .writefn = tlbiipas2_write },
4349 { .name = "TLBIIPAS2LIS",
4350 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4351 .type = ARM_CP_NO_RAW, .access = PL2_W,
4352 .writefn = tlbiipas2_is_write },
4353 /* 32 bit cache operations */
4354 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4355 .type = ARM_CP_NOP, .access = PL1_W },
4356 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4357 .type = ARM_CP_NOP, .access = PL1_W },
4358 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4359 .type = ARM_CP_NOP, .access = PL1_W },
4360 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
4361 .type = ARM_CP_NOP, .access = PL1_W },
4362 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
4363 .type = ARM_CP_NOP, .access = PL1_W },
4364 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
4365 .type = ARM_CP_NOP, .access = PL1_W },
4366 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4367 .type = ARM_CP_NOP, .access = PL1_W },
4368 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4369 .type = ARM_CP_NOP, .access = PL1_W },
4370 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
4371 .type = ARM_CP_NOP, .access = PL1_W },
4372 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4373 .type = ARM_CP_NOP, .access = PL1_W },
4374 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
4375 .type = ARM_CP_NOP, .access = PL1_W },
4376 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
4377 .type = ARM_CP_NOP, .access = PL1_W },
4378 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4379 .type = ARM_CP_NOP, .access = PL1_W },
4380 /* MMU Domain access control / MPU write buffer control */
4381 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
4382 .access = PL1_RW, .resetvalue = 0,
4383 .writefn = dacr_write, .raw_writefn = raw_write,
4384 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
4385 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
4386 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
4387 .type = ARM_CP_ALIAS,
4388 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
4389 .access = PL1_RW,
4390 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
4391 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
4392 .type = ARM_CP_ALIAS,
4393 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
4394 .access = PL1_RW,
4395 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
4396 /* We rely on the access checks not allowing the guest to write to the
4397 * state field when SPSel indicates that it's being used as the stack
4398 * pointer.
4400 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
4401 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
4402 .access = PL1_RW, .accessfn = sp_el0_access,
4403 .type = ARM_CP_ALIAS,
4404 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
4405 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
4406 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
4407 .access = PL2_RW, .type = ARM_CP_ALIAS,
4408 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
4409 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
4410 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
4411 .type = ARM_CP_NO_RAW,
4412 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
4413 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
4414 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
4415 .type = ARM_CP_ALIAS,
4416 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
4417 .access = PL2_RW, .accessfn = fpexc32_access },
4418 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
4419 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
4420 .access = PL2_RW, .resetvalue = 0,
4421 .writefn = dacr_write, .raw_writefn = raw_write,
4422 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
4423 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
4424 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
4425 .access = PL2_RW, .resetvalue = 0,
4426 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
4427 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
4428 .type = ARM_CP_ALIAS,
4429 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
4430 .access = PL2_RW,
4431 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
4432 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
4433 .type = ARM_CP_ALIAS,
4434 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
4435 .access = PL2_RW,
4436 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
4437 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
4438 .type = ARM_CP_ALIAS,
4439 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
4440 .access = PL2_RW,
4441 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
4442 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
4443 .type = ARM_CP_ALIAS,
4444 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
4445 .access = PL2_RW,
4446 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
4447 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
4448 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
4449 .resetvalue = 0,
4450 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
4451 { .name = "SDCR", .type = ARM_CP_ALIAS,
4452 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
4453 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4454 .writefn = sdcr_write,
4455 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
4456 REGINFO_SENTINEL
4459 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4460 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
4461 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
4462 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
4463 .access = PL2_RW,
4464 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
4465 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
4466 .type = ARM_CP_NO_RAW,
4467 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4468 .access = PL2_RW,
4469 .type = ARM_CP_CONST, .resetvalue = 0 },
4470 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
4471 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
4472 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4473 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
4474 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
4475 .access = PL2_RW,
4476 .type = ARM_CP_CONST, .resetvalue = 0 },
4477 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
4478 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
4479 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4480 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
4481 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
4482 .access = PL2_RW, .type = ARM_CP_CONST,
4483 .resetvalue = 0 },
4484 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
4485 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
4486 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4487 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
4488 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
4489 .access = PL2_RW, .type = ARM_CP_CONST,
4490 .resetvalue = 0 },
4491 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
4492 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
4493 .access = PL2_RW, .type = ARM_CP_CONST,
4494 .resetvalue = 0 },
4495 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
4496 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
4497 .access = PL2_RW, .type = ARM_CP_CONST,
4498 .resetvalue = 0 },
4499 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
4500 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
4501 .access = PL2_RW, .type = ARM_CP_CONST,
4502 .resetvalue = 0 },
4503 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
4504 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
4505 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4506 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
4507 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
4508 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4509 .type = ARM_CP_CONST, .resetvalue = 0 },
4510 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
4511 .cp = 15, .opc1 = 6, .crm = 2,
4512 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4513 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
4514 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
4515 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
4516 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4517 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
4518 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
4519 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4520 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4521 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
4522 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4523 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
4524 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
4525 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4526 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
4527 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4528 .resetvalue = 0 },
4529 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
4530 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
4531 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4532 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
4533 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
4534 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4535 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
4536 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4537 .resetvalue = 0 },
4538 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
4539 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
4540 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4541 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4542 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4543 .resetvalue = 0 },
4544 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4545 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
4546 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4547 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4548 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4549 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4550 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4551 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4552 .access = PL2_RW, .accessfn = access_tda,
4553 .type = ARM_CP_CONST, .resetvalue = 0 },
4554 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
4555 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4556 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4557 .type = ARM_CP_CONST, .resetvalue = 0 },
4558 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4559 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4560 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4561 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
4562 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
4563 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4564 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
4565 .type = ARM_CP_CONST,
4566 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
4567 .access = PL2_RW, .resetvalue = 0 },
4568 REGINFO_SENTINEL
4571 /* Ditto, but for registers which exist in ARMv8 but not v7 */
4572 static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
4573 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
4574 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
4575 .access = PL2_RW,
4576 .type = ARM_CP_CONST, .resetvalue = 0 },
4577 REGINFO_SENTINEL
4580 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4582 ARMCPU *cpu = env_archcpu(env);
4583 uint64_t valid_mask = HCR_MASK;
4585 if (arm_feature(env, ARM_FEATURE_EL3)) {
4586 valid_mask &= ~HCR_HCD;
4587 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
4588 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
4589 * However, if we're using the SMC PSCI conduit then QEMU is
4590 * effectively acting like EL3 firmware and so the guest at
4591 * EL2 should retain the ability to prevent EL1 from being
4592 * able to make SMC calls into the ersatz firmware, so in
4593 * that case HCR.TSC should be read/write.
4595 valid_mask &= ~HCR_TSC;
4597 if (cpu_isar_feature(aa64_lor, cpu)) {
4598 valid_mask |= HCR_TLOR;
4600 if (cpu_isar_feature(aa64_pauth, cpu)) {
4601 valid_mask |= HCR_API | HCR_APK;
4604 /* Clear RES0 bits. */
4605 value &= valid_mask;
4607 /* These bits change the MMU setup:
4608 * HCR_VM enables stage 2 translation
4609 * HCR_PTW forbids certain page-table setups
4610 * HCR_DC Disables stage1 and enables stage2 translation
4612 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
4613 tlb_flush(CPU(cpu));
4615 env->cp15.hcr_el2 = value;
4618 * Updates to VI and VF require us to update the status of
4619 * virtual interrupts, which are the logical OR of these bits
4620 * and the state of the input lines from the GIC. (This requires
4621 * that we have the iothread lock, which is done by marking the
4622 * reginfo structs as ARM_CP_IO.)
4623 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
4624 * possible for it to be taken immediately, because VIRQ and
4625 * VFIQ are masked unless running at EL0 or EL1, and HCR
4626 * can only be written at EL2.
4628 g_assert(qemu_mutex_iothread_locked());
4629 arm_cpu_update_virq(cpu);
4630 arm_cpu_update_vfiq(cpu);
4633 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
4634 uint64_t value)
4636 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
4637 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
4638 hcr_write(env, NULL, value);
4641 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
4642 uint64_t value)
4644 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
4645 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
4646 hcr_write(env, NULL, value);
4650 * Return the effective value of HCR_EL2.
4651 * Bits that are not included here:
4652 * RW (read from SCR_EL3.RW as needed)
4654 uint64_t arm_hcr_el2_eff(CPUARMState *env)
4656 uint64_t ret = env->cp15.hcr_el2;
4658 if (arm_is_secure_below_el3(env)) {
4660 * "This register has no effect if EL2 is not enabled in the
4661 * current Security state". This is ARMv8.4-SecEL2 speak for
4662 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
4664 * Prior to that, the language was "In an implementation that
4665 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
4666 * as if this field is 0 for all purposes other than a direct
4667 * read or write access of HCR_EL2". With lots of enumeration
4668 * on a per-field basis. In current QEMU, this is condition
4669 * is arm_is_secure_below_el3.
4671 * Since the v8.4 language applies to the entire register, and
4672 * appears to be backward compatible, use that.
4674 ret = 0;
4675 } else if (ret & HCR_TGE) {
4676 /* These bits are up-to-date as of ARMv8.4. */
4677 if (ret & HCR_E2H) {
4678 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
4679 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
4680 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4681 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE);
4682 } else {
4683 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
4685 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
4686 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
4687 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
4688 HCR_TLOR);
4691 return ret;
4694 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4695 uint64_t value)
4698 * For A-profile AArch32 EL3, if NSACR.CP10
4699 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
4701 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
4702 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
4703 value &= ~(0x3 << 10);
4704 value |= env->cp15.cptr_el[2] & (0x3 << 10);
4706 env->cp15.cptr_el[2] = value;
4709 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
4712 * For A-profile AArch32 EL3, if NSACR.CP10
4713 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
4715 uint64_t value = env->cp15.cptr_el[2];
4717 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
4718 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
4719 value |= 0x3 << 10;
4721 return value;
4724 static const ARMCPRegInfo el2_cp_reginfo[] = {
4725 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
4726 .type = ARM_CP_IO,
4727 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4728 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
4729 .writefn = hcr_write },
4730 { .name = "HCR", .state = ARM_CP_STATE_AA32,
4731 .type = ARM_CP_ALIAS | ARM_CP_IO,
4732 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4733 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
4734 .writefn = hcr_writelow },
4735 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
4736 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
4737 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4738 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
4739 .type = ARM_CP_ALIAS,
4740 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
4741 .access = PL2_RW,
4742 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
4743 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
4744 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
4745 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
4746 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
4747 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
4748 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
4749 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
4750 .type = ARM_CP_ALIAS,
4751 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
4752 .access = PL2_RW,
4753 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
4754 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
4755 .type = ARM_CP_ALIAS,
4756 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
4757 .access = PL2_RW,
4758 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
4759 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
4760 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
4761 .access = PL2_RW, .writefn = vbar_write,
4762 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
4763 .resetvalue = 0 },
4764 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
4765 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
4766 .access = PL3_RW, .type = ARM_CP_ALIAS,
4767 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
4768 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
4769 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
4770 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
4771 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
4772 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
4773 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
4774 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
4775 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
4776 .resetvalue = 0 },
4777 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
4778 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
4779 .access = PL2_RW, .type = ARM_CP_ALIAS,
4780 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
4781 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
4782 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
4783 .access = PL2_RW, .type = ARM_CP_CONST,
4784 .resetvalue = 0 },
4785 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
4786 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
4787 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
4788 .access = PL2_RW, .type = ARM_CP_CONST,
4789 .resetvalue = 0 },
4790 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
4791 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
4792 .access = PL2_RW, .type = ARM_CP_CONST,
4793 .resetvalue = 0 },
4794 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
4795 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
4796 .access = PL2_RW, .type = ARM_CP_CONST,
4797 .resetvalue = 0 },
4798 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
4799 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
4800 .access = PL2_RW,
4801 /* no .writefn needed as this can't cause an ASID change;
4802 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
4804 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
4805 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
4806 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
4807 .type = ARM_CP_ALIAS,
4808 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4809 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
4810 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
4811 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
4812 .access = PL2_RW,
4813 /* no .writefn needed as this can't cause an ASID change;
4814 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
4816 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
4817 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
4818 .cp = 15, .opc1 = 6, .crm = 2,
4819 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4820 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4821 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
4822 .writefn = vttbr_write },
4823 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
4824 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
4825 .access = PL2_RW, .writefn = vttbr_write,
4826 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
4827 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
4828 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
4829 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
4830 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
4831 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4832 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
4833 .access = PL2_RW, .resetvalue = 0,
4834 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
4835 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
4836 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
4837 .access = PL2_RW, .resetvalue = 0,
4838 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
4839 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
4840 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4841 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
4842 { .name = "TLBIALLNSNH",
4843 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4844 .type = ARM_CP_NO_RAW, .access = PL2_W,
4845 .writefn = tlbiall_nsnh_write },
4846 { .name = "TLBIALLNSNHIS",
4847 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4848 .type = ARM_CP_NO_RAW, .access = PL2_W,
4849 .writefn = tlbiall_nsnh_is_write },
4850 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
4851 .type = ARM_CP_NO_RAW, .access = PL2_W,
4852 .writefn = tlbiall_hyp_write },
4853 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
4854 .type = ARM_CP_NO_RAW, .access = PL2_W,
4855 .writefn = tlbiall_hyp_is_write },
4856 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
4857 .type = ARM_CP_NO_RAW, .access = PL2_W,
4858 .writefn = tlbimva_hyp_write },
4859 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
4860 .type = ARM_CP_NO_RAW, .access = PL2_W,
4861 .writefn = tlbimva_hyp_is_write },
4862 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
4863 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
4864 .type = ARM_CP_NO_RAW, .access = PL2_W,
4865 .writefn = tlbi_aa64_alle2_write },
4866 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
4867 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
4868 .type = ARM_CP_NO_RAW, .access = PL2_W,
4869 .writefn = tlbi_aa64_vae2_write },
4870 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
4871 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4872 .access = PL2_W, .type = ARM_CP_NO_RAW,
4873 .writefn = tlbi_aa64_vae2_write },
4874 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
4875 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
4876 .access = PL2_W, .type = ARM_CP_NO_RAW,
4877 .writefn = tlbi_aa64_alle2is_write },
4878 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
4879 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
4880 .type = ARM_CP_NO_RAW, .access = PL2_W,
4881 .writefn = tlbi_aa64_vae2is_write },
4882 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
4883 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4884 .access = PL2_W, .type = ARM_CP_NO_RAW,
4885 .writefn = tlbi_aa64_vae2is_write },
4886 #ifndef CONFIG_USER_ONLY
4887 /* Unlike the other EL2-related AT operations, these must
4888 * UNDEF from EL3 if EL2 is not implemented, which is why we
4889 * define them here rather than with the rest of the AT ops.
4891 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
4892 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
4893 .access = PL2_W, .accessfn = at_s1e2_access,
4894 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4895 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
4896 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
4897 .access = PL2_W, .accessfn = at_s1e2_access,
4898 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
4899 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
4900 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
4901 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
4902 * to behave as if SCR.NS was 1.
4904 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
4905 .access = PL2_W,
4906 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
4907 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
4908 .access = PL2_W,
4909 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
4910 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
4911 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
4912 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
4913 * reset values as IMPDEF. We choose to reset to 3 to comply with
4914 * both ARMv7 and ARMv8.
4916 .access = PL2_RW, .resetvalue = 3,
4917 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
4918 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
4919 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
4920 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
4921 .writefn = gt_cntvoff_write,
4922 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
4923 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
4924 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
4925 .writefn = gt_cntvoff_write,
4926 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
4927 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
4928 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
4929 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4930 .type = ARM_CP_IO, .access = PL2_RW,
4931 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4932 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4933 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
4934 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
4935 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
4936 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4937 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
4938 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
4939 .resetfn = gt_hyp_timer_reset,
4940 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
4941 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4942 .type = ARM_CP_IO,
4943 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4944 .access = PL2_RW,
4945 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
4946 .resetvalue = 0,
4947 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
4948 #endif
4949 /* The only field of MDCR_EL2 that has a defined architectural reset value
4950 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
4951 * don't implement any PMU event counters, so using zero as a reset
4952 * value for MDCR_EL2 is okay
4954 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4955 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4956 .access = PL2_RW, .resetvalue = 0,
4957 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
4958 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
4959 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4960 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4961 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4962 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
4963 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4964 .access = PL2_RW,
4965 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
4966 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4967 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4968 .access = PL2_RW,
4969 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
4970 REGINFO_SENTINEL
4973 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
4974 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
4975 .type = ARM_CP_ALIAS | ARM_CP_IO,
4976 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
4977 .access = PL2_RW,
4978 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
4979 .writefn = hcr_writehigh },
4980 REGINFO_SENTINEL
4983 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
4984 bool isread)
4986 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
4987 * At Secure EL1 it traps to EL3.
4989 if (arm_current_el(env) == 3) {
4990 return CP_ACCESS_OK;
4992 if (arm_is_secure_below_el3(env)) {
4993 return CP_ACCESS_TRAP_EL3;
4995 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
4996 if (isread) {
4997 return CP_ACCESS_OK;
4999 return CP_ACCESS_TRAP_UNCATEGORIZED;
5002 static const ARMCPRegInfo el3_cp_reginfo[] = {
5003 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5004 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5005 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5006 .resetvalue = 0, .writefn = scr_write },
5007 { .name = "SCR", .type = ARM_CP_ALIAS,
5008 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5009 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5010 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5011 .writefn = scr_write },
5012 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5013 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5014 .access = PL3_RW, .resetvalue = 0,
5015 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5016 { .name = "SDER",
5017 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5018 .access = PL3_RW, .resetvalue = 0,
5019 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5020 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5021 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5022 .writefn = vbar_write, .resetvalue = 0,
5023 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5024 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5025 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5026 .access = PL3_RW, .resetvalue = 0,
5027 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5028 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5029 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5030 .access = PL3_RW,
5031 /* no .writefn needed as this can't cause an ASID change;
5032 * we must provide a .raw_writefn and .resetfn because we handle
5033 * reset and migration for the AArch32 TTBCR(S), which might be
5034 * using mask and base_mask.
5036 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
5037 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5038 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5039 .type = ARM_CP_ALIAS,
5040 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5041 .access = PL3_RW,
5042 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5043 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5044 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5045 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5046 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5047 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5048 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5049 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5050 .type = ARM_CP_ALIAS,
5051 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5052 .access = PL3_RW,
5053 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5054 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5055 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5056 .access = PL3_RW, .writefn = vbar_write,
5057 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5058 .resetvalue = 0 },
5059 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5060 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5061 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5062 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5063 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5064 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5065 .access = PL3_RW, .resetvalue = 0,
5066 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5067 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5068 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5069 .access = PL3_RW, .type = ARM_CP_CONST,
5070 .resetvalue = 0 },
5071 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5072 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5073 .access = PL3_RW, .type = ARM_CP_CONST,
5074 .resetvalue = 0 },
5075 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5076 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5077 .access = PL3_RW, .type = ARM_CP_CONST,
5078 .resetvalue = 0 },
5079 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5080 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5081 .access = PL3_W, .type = ARM_CP_NO_RAW,
5082 .writefn = tlbi_aa64_alle3is_write },
5083 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5084 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5085 .access = PL3_W, .type = ARM_CP_NO_RAW,
5086 .writefn = tlbi_aa64_vae3is_write },
5087 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5088 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5089 .access = PL3_W, .type = ARM_CP_NO_RAW,
5090 .writefn = tlbi_aa64_vae3is_write },
5091 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5092 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5093 .access = PL3_W, .type = ARM_CP_NO_RAW,
5094 .writefn = tlbi_aa64_alle3_write },
5095 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5096 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5097 .access = PL3_W, .type = ARM_CP_NO_RAW,
5098 .writefn = tlbi_aa64_vae3_write },
5099 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5100 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5101 .access = PL3_W, .type = ARM_CP_NO_RAW,
5102 .writefn = tlbi_aa64_vae3_write },
5103 REGINFO_SENTINEL
5106 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5107 bool isread)
5109 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
5110 * but the AArch32 CTR has its own reginfo struct)
5112 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5113 return CP_ACCESS_TRAP;
5115 return CP_ACCESS_OK;
5118 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5119 uint64_t value)
5121 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5122 * read via a bit in OSLSR_EL1.
5124 int oslock;
5126 if (ri->state == ARM_CP_STATE_AA32) {
5127 oslock = (value == 0xC5ACCE55);
5128 } else {
5129 oslock = value & 1;
5132 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5135 static const ARMCPRegInfo debug_cp_reginfo[] = {
5136 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
5137 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5138 * unlike DBGDRAR it is never accessible from EL0.
5139 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5140 * accessor.
5142 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
5143 .access = PL0_R, .accessfn = access_tdra,
5144 .type = ARM_CP_CONST, .resetvalue = 0 },
5145 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
5146 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5147 .access = PL1_R, .accessfn = access_tdra,
5148 .type = ARM_CP_CONST, .resetvalue = 0 },
5149 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
5150 .access = PL0_R, .accessfn = access_tdra,
5151 .type = ARM_CP_CONST, .resetvalue = 0 },
5152 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
5153 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
5154 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
5155 .access = PL1_RW, .accessfn = access_tda,
5156 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
5157 .resetvalue = 0 },
5158 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
5159 * We don't implement the configurable EL0 access.
5161 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
5162 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
5163 .type = ARM_CP_ALIAS,
5164 .access = PL1_R, .accessfn = access_tda,
5165 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
5166 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
5167 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
5168 .access = PL1_W, .type = ARM_CP_NO_RAW,
5169 .accessfn = access_tdosa,
5170 .writefn = oslar_write },
5171 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
5172 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
5173 .access = PL1_R, .resetvalue = 10,
5174 .accessfn = access_tdosa,
5175 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5176 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
5177 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
5178 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
5179 .access = PL1_RW, .accessfn = access_tdosa,
5180 .type = ARM_CP_NOP },
5181 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
5182 * implement vector catch debug events yet.
5184 { .name = "DBGVCR",
5185 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
5186 .access = PL1_RW, .accessfn = access_tda,
5187 .type = ARM_CP_NOP },
5188 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
5189 * to save and restore a 32-bit guest's DBGVCR)
5191 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
5192 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
5193 .access = PL2_RW, .accessfn = access_tda,
5194 .type = ARM_CP_NOP },
5195 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
5196 * Channel but Linux may try to access this register. The 32-bit
5197 * alias is DBGDCCINT.
5199 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
5200 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5201 .access = PL1_RW, .accessfn = access_tda,
5202 .type = ARM_CP_NOP },
5203 REGINFO_SENTINEL
5206 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
5207 /* 64 bit access versions of the (dummy) debug registers */
5208 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
5209 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5210 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
5211 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5212 REGINFO_SENTINEL
5215 /* Return the exception level to which exceptions should be taken
5216 * via SVEAccessTrap. If an exception should be routed through
5217 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
5218 * take care of raising that exception.
5219 * C.f. the ARM pseudocode function CheckSVEEnabled.
5221 int sve_exception_el(CPUARMState *env, int el)
5223 #ifndef CONFIG_USER_ONLY
5224 if (el <= 1) {
5225 bool disabled = false;
5227 /* The CPACR.ZEN controls traps to EL1:
5228 * 0, 2 : trap EL0 and EL1 accesses
5229 * 1 : trap only EL0 accesses
5230 * 3 : trap no accesses
5232 if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
5233 disabled = true;
5234 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
5235 disabled = el == 0;
5237 if (disabled) {
5238 /* route_to_el2 */
5239 return (arm_feature(env, ARM_FEATURE_EL2)
5240 && (arm_hcr_el2_eff(env) & HCR_TGE) ? 2 : 1);
5243 /* Check CPACR.FPEN. */
5244 if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
5245 disabled = true;
5246 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
5247 disabled = el == 0;
5249 if (disabled) {
5250 return 0;
5254 /* CPTR_EL2. Since TZ and TFP are positive,
5255 * they will be zero when EL2 is not present.
5257 if (el <= 2 && !arm_is_secure_below_el3(env)) {
5258 if (env->cp15.cptr_el[2] & CPTR_TZ) {
5259 return 2;
5261 if (env->cp15.cptr_el[2] & CPTR_TFP) {
5262 return 0;
5266 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
5267 if (arm_feature(env, ARM_FEATURE_EL3)
5268 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5269 return 3;
5271 #endif
5272 return 0;
5276 * Given that SVE is enabled, return the vector length for EL.
5278 uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
5280 ARMCPU *cpu = env_archcpu(env);
5281 uint32_t zcr_len = cpu->sve_max_vq - 1;
5283 if (el <= 1) {
5284 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
5286 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
5287 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
5289 if (arm_feature(env, ARM_FEATURE_EL3)) {
5290 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
5292 return zcr_len;
5295 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5296 uint64_t value)
5298 int cur_el = arm_current_el(env);
5299 int old_len = sve_zcr_len_for_el(env, cur_el);
5300 int new_len;
5302 /* Bits other than [3:0] are RAZ/WI. */
5303 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5304 raw_write(env, ri, value & 0xf);
5307 * Because we arrived here, we know both FP and SVE are enabled;
5308 * otherwise we would have trapped access to the ZCR_ELn register.
5310 new_len = sve_zcr_len_for_el(env, cur_el);
5311 if (new_len < old_len) {
5312 aarch64_sve_narrow_vq(env, new_len + 1);
5316 static const ARMCPRegInfo zcr_el1_reginfo = {
5317 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
5318 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
5319 .access = PL1_RW, .type = ARM_CP_SVE,
5320 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
5321 .writefn = zcr_write, .raw_writefn = raw_write
5324 static const ARMCPRegInfo zcr_el2_reginfo = {
5325 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
5326 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
5327 .access = PL2_RW, .type = ARM_CP_SVE,
5328 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
5329 .writefn = zcr_write, .raw_writefn = raw_write
5332 static const ARMCPRegInfo zcr_no_el2_reginfo = {
5333 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
5334 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
5335 .access = PL2_RW, .type = ARM_CP_SVE,
5336 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
5339 static const ARMCPRegInfo zcr_el3_reginfo = {
5340 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
5341 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
5342 .access = PL3_RW, .type = ARM_CP_SVE,
5343 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
5344 .writefn = zcr_write, .raw_writefn = raw_write
5347 void hw_watchpoint_update(ARMCPU *cpu, int n)
5349 CPUARMState *env = &cpu->env;
5350 vaddr len = 0;
5351 vaddr wvr = env->cp15.dbgwvr[n];
5352 uint64_t wcr = env->cp15.dbgwcr[n];
5353 int mask;
5354 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
5356 if (env->cpu_watchpoint[n]) {
5357 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
5358 env->cpu_watchpoint[n] = NULL;
5361 if (!extract64(wcr, 0, 1)) {
5362 /* E bit clear : watchpoint disabled */
5363 return;
5366 switch (extract64(wcr, 3, 2)) {
5367 case 0:
5368 /* LSC 00 is reserved and must behave as if the wp is disabled */
5369 return;
5370 case 1:
5371 flags |= BP_MEM_READ;
5372 break;
5373 case 2:
5374 flags |= BP_MEM_WRITE;
5375 break;
5376 case 3:
5377 flags |= BP_MEM_ACCESS;
5378 break;
5381 /* Attempts to use both MASK and BAS fields simultaneously are
5382 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
5383 * thus generating a watchpoint for every byte in the masked region.
5385 mask = extract64(wcr, 24, 4);
5386 if (mask == 1 || mask == 2) {
5387 /* Reserved values of MASK; we must act as if the mask value was
5388 * some non-reserved value, or as if the watchpoint were disabled.
5389 * We choose the latter.
5391 return;
5392 } else if (mask) {
5393 /* Watchpoint covers an aligned area up to 2GB in size */
5394 len = 1ULL << mask;
5395 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
5396 * whether the watchpoint fires when the unmasked bits match; we opt
5397 * to generate the exceptions.
5399 wvr &= ~(len - 1);
5400 } else {
5401 /* Watchpoint covers bytes defined by the byte address select bits */
5402 int bas = extract64(wcr, 5, 8);
5403 int basstart;
5405 if (bas == 0) {
5406 /* This must act as if the watchpoint is disabled */
5407 return;
5410 if (extract64(wvr, 2, 1)) {
5411 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
5412 * ignored, and BAS[3:0] define which bytes to watch.
5414 bas &= 0xf;
5416 /* The BAS bits are supposed to be programmed to indicate a contiguous
5417 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
5418 * we fire for each byte in the word/doubleword addressed by the WVR.
5419 * We choose to ignore any non-zero bits after the first range of 1s.
5421 basstart = ctz32(bas);
5422 len = cto32(bas >> basstart);
5423 wvr += basstart;
5426 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
5427 &env->cpu_watchpoint[n]);
5430 void hw_watchpoint_update_all(ARMCPU *cpu)
5432 int i;
5433 CPUARMState *env = &cpu->env;
5435 /* Completely clear out existing QEMU watchpoints and our array, to
5436 * avoid possible stale entries following migration load.
5438 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
5439 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
5441 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
5442 hw_watchpoint_update(cpu, i);
5446 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5447 uint64_t value)
5449 ARMCPU *cpu = env_archcpu(env);
5450 int i = ri->crm;
5452 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
5453 * register reads and behaves as if values written are sign extended.
5454 * Bits [1:0] are RES0.
5456 value = sextract64(value, 0, 49) & ~3ULL;
5458 raw_write(env, ri, value);
5459 hw_watchpoint_update(cpu, i);
5462 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5463 uint64_t value)
5465 ARMCPU *cpu = env_archcpu(env);
5466 int i = ri->crm;
5468 raw_write(env, ri, value);
5469 hw_watchpoint_update(cpu, i);
5472 void hw_breakpoint_update(ARMCPU *cpu, int n)
5474 CPUARMState *env = &cpu->env;
5475 uint64_t bvr = env->cp15.dbgbvr[n];
5476 uint64_t bcr = env->cp15.dbgbcr[n];
5477 vaddr addr;
5478 int bt;
5479 int flags = BP_CPU;
5481 if (env->cpu_breakpoint[n]) {
5482 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
5483 env->cpu_breakpoint[n] = NULL;
5486 if (!extract64(bcr, 0, 1)) {
5487 /* E bit clear : watchpoint disabled */
5488 return;
5491 bt = extract64(bcr, 20, 4);
5493 switch (bt) {
5494 case 4: /* unlinked address mismatch (reserved if AArch64) */
5495 case 5: /* linked address mismatch (reserved if AArch64) */
5496 qemu_log_mask(LOG_UNIMP,
5497 "arm: address mismatch breakpoint types not implemented\n");
5498 return;
5499 case 0: /* unlinked address match */
5500 case 1: /* linked address match */
5502 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
5503 * we behave as if the register was sign extended. Bits [1:0] are
5504 * RES0. The BAS field is used to allow setting breakpoints on 16
5505 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
5506 * a bp will fire if the addresses covered by the bp and the addresses
5507 * covered by the insn overlap but the insn doesn't start at the
5508 * start of the bp address range. We choose to require the insn and
5509 * the bp to have the same address. The constraints on writing to
5510 * BAS enforced in dbgbcr_write mean we have only four cases:
5511 * 0b0000 => no breakpoint
5512 * 0b0011 => breakpoint on addr
5513 * 0b1100 => breakpoint on addr + 2
5514 * 0b1111 => breakpoint on addr
5515 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
5517 int bas = extract64(bcr, 5, 4);
5518 addr = sextract64(bvr, 0, 49) & ~3ULL;
5519 if (bas == 0) {
5520 return;
5522 if (bas == 0xc) {
5523 addr += 2;
5525 break;
5527 case 2: /* unlinked context ID match */
5528 case 8: /* unlinked VMID match (reserved if no EL2) */
5529 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
5530 qemu_log_mask(LOG_UNIMP,
5531 "arm: unlinked context breakpoint types not implemented\n");
5532 return;
5533 case 9: /* linked VMID match (reserved if no EL2) */
5534 case 11: /* linked context ID and VMID match (reserved if no EL2) */
5535 case 3: /* linked context ID match */
5536 default:
5537 /* We must generate no events for Linked context matches (unless
5538 * they are linked to by some other bp/wp, which is handled in
5539 * updates for the linking bp/wp). We choose to also generate no events
5540 * for reserved values.
5542 return;
5545 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
5548 void hw_breakpoint_update_all(ARMCPU *cpu)
5550 int i;
5551 CPUARMState *env = &cpu->env;
5553 /* Completely clear out existing QEMU breakpoints and our array, to
5554 * avoid possible stale entries following migration load.
5556 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
5557 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
5559 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
5560 hw_breakpoint_update(cpu, i);
5564 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5565 uint64_t value)
5567 ARMCPU *cpu = env_archcpu(env);
5568 int i = ri->crm;
5570 raw_write(env, ri, value);
5571 hw_breakpoint_update(cpu, i);
5574 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5575 uint64_t value)
5577 ARMCPU *cpu = env_archcpu(env);
5578 int i = ri->crm;
5580 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
5581 * copy of BAS[0].
5583 value = deposit64(value, 6, 1, extract64(value, 5, 1));
5584 value = deposit64(value, 8, 1, extract64(value, 7, 1));
5586 raw_write(env, ri, value);
5587 hw_breakpoint_update(cpu, i);
5590 static void define_debug_regs(ARMCPU *cpu)
5592 /* Define v7 and v8 architectural debug registers.
5593 * These are just dummy implementations for now.
5595 int i;
5596 int wrps, brps, ctx_cmps;
5597 ARMCPRegInfo dbgdidr = {
5598 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
5599 .access = PL0_R, .accessfn = access_tda,
5600 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
5603 /* Note that all these register fields hold "number of Xs minus 1". */
5604 brps = extract32(cpu->dbgdidr, 24, 4);
5605 wrps = extract32(cpu->dbgdidr, 28, 4);
5606 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
5608 assert(ctx_cmps <= brps);
5610 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
5611 * of the debug registers such as number of breakpoints;
5612 * check that if they both exist then they agree.
5614 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
5615 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
5616 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
5617 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
5620 define_one_arm_cp_reg(cpu, &dbgdidr);
5621 define_arm_cp_regs(cpu, debug_cp_reginfo);
5623 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
5624 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
5627 for (i = 0; i < brps + 1; i++) {
5628 ARMCPRegInfo dbgregs[] = {
5629 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
5630 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
5631 .access = PL1_RW, .accessfn = access_tda,
5632 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
5633 .writefn = dbgbvr_write, .raw_writefn = raw_write
5635 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
5636 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
5637 .access = PL1_RW, .accessfn = access_tda,
5638 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
5639 .writefn = dbgbcr_write, .raw_writefn = raw_write
5641 REGINFO_SENTINEL
5643 define_arm_cp_regs(cpu, dbgregs);
5646 for (i = 0; i < wrps + 1; i++) {
5647 ARMCPRegInfo dbgregs[] = {
5648 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
5649 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
5650 .access = PL1_RW, .accessfn = access_tda,
5651 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
5652 .writefn = dbgwvr_write, .raw_writefn = raw_write
5654 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
5655 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
5656 .access = PL1_RW, .accessfn = access_tda,
5657 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
5658 .writefn = dbgwcr_write, .raw_writefn = raw_write
5660 REGINFO_SENTINEL
5662 define_arm_cp_regs(cpu, dbgregs);
5666 /* We don't know until after realize whether there's a GICv3
5667 * attached, and that is what registers the gicv3 sysregs.
5668 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
5669 * at runtime.
5671 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
5673 ARMCPU *cpu = env_archcpu(env);
5674 uint64_t pfr1 = cpu->id_pfr1;
5676 if (env->gicv3state) {
5677 pfr1 |= 1 << 28;
5679 return pfr1;
5682 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
5684 ARMCPU *cpu = env_archcpu(env);
5685 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
5687 if (env->gicv3state) {
5688 pfr0 |= 1 << 24;
5690 return pfr0;
5693 /* Shared logic between LORID and the rest of the LOR* registers.
5694 * Secure state has already been delt with.
5696 static CPAccessResult access_lor_ns(CPUARMState *env)
5698 int el = arm_current_el(env);
5700 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
5701 return CP_ACCESS_TRAP_EL2;
5703 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
5704 return CP_ACCESS_TRAP_EL3;
5706 return CP_ACCESS_OK;
5709 static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri,
5710 bool isread)
5712 if (arm_is_secure_below_el3(env)) {
5713 /* Access ok in secure mode. */
5714 return CP_ACCESS_OK;
5716 return access_lor_ns(env);
5719 static CPAccessResult access_lor_other(CPUARMState *env,
5720 const ARMCPRegInfo *ri, bool isread)
5722 if (arm_is_secure_below_el3(env)) {
5723 /* Access denied in secure mode. */
5724 return CP_ACCESS_TRAP;
5726 return access_lor_ns(env);
5729 #ifdef TARGET_AARCH64
5730 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
5731 bool isread)
5733 int el = arm_current_el(env);
5735 if (el < 2 &&
5736 arm_feature(env, ARM_FEATURE_EL2) &&
5737 !(arm_hcr_el2_eff(env) & HCR_APK)) {
5738 return CP_ACCESS_TRAP_EL2;
5740 if (el < 3 &&
5741 arm_feature(env, ARM_FEATURE_EL3) &&
5742 !(env->cp15.scr_el3 & SCR_APK)) {
5743 return CP_ACCESS_TRAP_EL3;
5745 return CP_ACCESS_OK;
5748 static const ARMCPRegInfo pauth_reginfo[] = {
5749 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5750 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
5751 .access = PL1_RW, .accessfn = access_pauth,
5752 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
5753 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5754 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
5755 .access = PL1_RW, .accessfn = access_pauth,
5756 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
5757 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5758 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
5759 .access = PL1_RW, .accessfn = access_pauth,
5760 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
5761 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5762 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
5763 .access = PL1_RW, .accessfn = access_pauth,
5764 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
5765 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5766 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
5767 .access = PL1_RW, .accessfn = access_pauth,
5768 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
5769 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5770 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
5771 .access = PL1_RW, .accessfn = access_pauth,
5772 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
5773 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5774 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
5775 .access = PL1_RW, .accessfn = access_pauth,
5776 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
5777 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5778 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
5779 .access = PL1_RW, .accessfn = access_pauth,
5780 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
5781 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
5782 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
5783 .access = PL1_RW, .accessfn = access_pauth,
5784 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
5785 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
5786 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
5787 .access = PL1_RW, .accessfn = access_pauth,
5788 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
5789 REGINFO_SENTINEL
5792 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
5794 Error *err = NULL;
5795 uint64_t ret;
5797 /* Success sets NZCV = 0000. */
5798 env->NF = env->CF = env->VF = 0, env->ZF = 1;
5800 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
5802 * ??? Failed, for unknown reasons in the crypto subsystem.
5803 * The best we can do is log the reason and return the
5804 * timed-out indication to the guest. There is no reason
5805 * we know to expect this failure to be transitory, so the
5806 * guest may well hang retrying the operation.
5808 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
5809 ri->name, error_get_pretty(err));
5810 error_free(err);
5812 env->ZF = 0; /* NZCF = 0100 */
5813 return 0;
5815 return ret;
5818 /* We do not support re-seeding, so the two registers operate the same. */
5819 static const ARMCPRegInfo rndr_reginfo[] = {
5820 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
5821 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
5822 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
5823 .access = PL0_R, .readfn = rndr_readfn },
5824 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
5825 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
5826 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
5827 .access = PL0_R, .readfn = rndr_readfn },
5828 REGINFO_SENTINEL
5830 #endif
5832 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
5833 bool isread)
5835 int el = arm_current_el(env);
5837 if (el == 0) {
5838 uint64_t sctlr = arm_sctlr(env, el);
5839 if (!(sctlr & SCTLR_EnRCTX)) {
5840 return CP_ACCESS_TRAP;
5842 } else if (el == 1) {
5843 uint64_t hcr = arm_hcr_el2_eff(env);
5844 if (hcr & HCR_NV) {
5845 return CP_ACCESS_TRAP_EL2;
5848 return CP_ACCESS_OK;
5851 static const ARMCPRegInfo predinv_reginfo[] = {
5852 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
5853 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
5854 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5855 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
5856 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
5857 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5858 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
5859 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
5860 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5862 * Note the AArch32 opcodes have a different OPC1.
5864 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
5865 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
5866 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5867 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
5868 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
5869 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5870 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
5871 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
5872 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
5873 REGINFO_SENTINEL
5876 void register_cp_regs_for_features(ARMCPU *cpu)
5878 /* Register all the coprocessor registers based on feature bits */
5879 CPUARMState *env = &cpu->env;
5880 if (arm_feature(env, ARM_FEATURE_M)) {
5881 /* M profile has no coprocessor registers */
5882 return;
5885 define_arm_cp_regs(cpu, cp_reginfo);
5886 if (!arm_feature(env, ARM_FEATURE_V8)) {
5887 /* Must go early as it is full of wildcards that may be
5888 * overridden by later definitions.
5890 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
5893 if (arm_feature(env, ARM_FEATURE_V6)) {
5894 /* The ID registers all have impdef reset values */
5895 ARMCPRegInfo v6_idregs[] = {
5896 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
5897 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
5898 .access = PL1_R, .type = ARM_CP_CONST,
5899 .resetvalue = cpu->id_pfr0 },
5900 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
5901 * the value of the GIC field until after we define these regs.
5903 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
5904 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
5905 .access = PL1_R, .type = ARM_CP_NO_RAW,
5906 .readfn = id_pfr1_read,
5907 .writefn = arm_cp_write_ignore },
5908 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
5909 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
5910 .access = PL1_R, .type = ARM_CP_CONST,
5911 .resetvalue = cpu->id_dfr0 },
5912 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
5913 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
5914 .access = PL1_R, .type = ARM_CP_CONST,
5915 .resetvalue = cpu->id_afr0 },
5916 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
5917 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
5918 .access = PL1_R, .type = ARM_CP_CONST,
5919 .resetvalue = cpu->id_mmfr0 },
5920 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
5921 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
5922 .access = PL1_R, .type = ARM_CP_CONST,
5923 .resetvalue = cpu->id_mmfr1 },
5924 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
5925 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
5926 .access = PL1_R, .type = ARM_CP_CONST,
5927 .resetvalue = cpu->id_mmfr2 },
5928 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
5929 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
5930 .access = PL1_R, .type = ARM_CP_CONST,
5931 .resetvalue = cpu->id_mmfr3 },
5932 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
5933 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5934 .access = PL1_R, .type = ARM_CP_CONST,
5935 .resetvalue = cpu->isar.id_isar0 },
5936 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
5937 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
5938 .access = PL1_R, .type = ARM_CP_CONST,
5939 .resetvalue = cpu->isar.id_isar1 },
5940 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
5941 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
5942 .access = PL1_R, .type = ARM_CP_CONST,
5943 .resetvalue = cpu->isar.id_isar2 },
5944 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
5945 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
5946 .access = PL1_R, .type = ARM_CP_CONST,
5947 .resetvalue = cpu->isar.id_isar3 },
5948 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
5949 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
5950 .access = PL1_R, .type = ARM_CP_CONST,
5951 .resetvalue = cpu->isar.id_isar4 },
5952 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
5953 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
5954 .access = PL1_R, .type = ARM_CP_CONST,
5955 .resetvalue = cpu->isar.id_isar5 },
5956 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
5957 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
5958 .access = PL1_R, .type = ARM_CP_CONST,
5959 .resetvalue = cpu->id_mmfr4 },
5960 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
5961 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
5962 .access = PL1_R, .type = ARM_CP_CONST,
5963 .resetvalue = cpu->isar.id_isar6 },
5964 REGINFO_SENTINEL
5966 define_arm_cp_regs(cpu, v6_idregs);
5967 define_arm_cp_regs(cpu, v6_cp_reginfo);
5968 } else {
5969 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
5971 if (arm_feature(env, ARM_FEATURE_V6K)) {
5972 define_arm_cp_regs(cpu, v6k_cp_reginfo);
5974 if (arm_feature(env, ARM_FEATURE_V7MP) &&
5975 !arm_feature(env, ARM_FEATURE_PMSA)) {
5976 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
5978 if (arm_feature(env, ARM_FEATURE_V7VE)) {
5979 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
5981 if (arm_feature(env, ARM_FEATURE_V7)) {
5982 /* v7 performance monitor control register: same implementor
5983 * field as main ID register, and we implement four counters in
5984 * addition to the cycle count register.
5986 unsigned int i, pmcrn = 4;
5987 ARMCPRegInfo pmcr = {
5988 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
5989 .access = PL0_RW,
5990 .type = ARM_CP_IO | ARM_CP_ALIAS,
5991 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
5992 .accessfn = pmreg_access, .writefn = pmcr_write,
5993 .raw_writefn = raw_write,
5995 ARMCPRegInfo pmcr64 = {
5996 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
5997 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
5998 .access = PL0_RW, .accessfn = pmreg_access,
5999 .type = ARM_CP_IO,
6000 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6001 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT),
6002 .writefn = pmcr_write, .raw_writefn = raw_write,
6004 define_one_arm_cp_reg(cpu, &pmcr);
6005 define_one_arm_cp_reg(cpu, &pmcr64);
6006 for (i = 0; i < pmcrn; i++) {
6007 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6008 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6009 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6010 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6011 ARMCPRegInfo pmev_regs[] = {
6012 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6013 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6014 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6015 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6016 .accessfn = pmreg_access },
6017 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6018 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6019 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6020 .type = ARM_CP_IO,
6021 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6022 .raw_readfn = pmevcntr_rawread,
6023 .raw_writefn = pmevcntr_rawwrite },
6024 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6025 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6026 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6027 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6028 .accessfn = pmreg_access },
6029 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6030 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6031 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6032 .type = ARM_CP_IO,
6033 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6034 .raw_writefn = pmevtyper_rawwrite },
6035 REGINFO_SENTINEL
6037 define_arm_cp_regs(cpu, pmev_regs);
6038 g_free(pmevcntr_name);
6039 g_free(pmevcntr_el0_name);
6040 g_free(pmevtyper_name);
6041 g_free(pmevtyper_el0_name);
6043 ARMCPRegInfo clidr = {
6044 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
6045 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
6046 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
6048 define_one_arm_cp_reg(cpu, &clidr);
6049 define_arm_cp_regs(cpu, v7_cp_reginfo);
6050 define_debug_regs(cpu);
6051 } else {
6052 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
6054 if (FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) >= 4 &&
6055 FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) != 0xf) {
6056 ARMCPRegInfo v81_pmu_regs[] = {
6057 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6058 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6059 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6060 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6061 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6062 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6063 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6064 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6065 REGINFO_SENTINEL
6067 define_arm_cp_regs(cpu, v81_pmu_regs);
6069 if (arm_feature(env, ARM_FEATURE_V8)) {
6070 /* AArch64 ID registers, which all have impdef reset values.
6071 * Note that within the ID register ranges the unused slots
6072 * must all RAZ, not UNDEF; future architecture versions may
6073 * define new registers here.
6075 ARMCPRegInfo v8_idregs[] = {
6076 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
6077 * know the right value for the GIC field until after we
6078 * define these regs.
6080 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
6081 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
6082 .access = PL1_R, .type = ARM_CP_NO_RAW,
6083 .readfn = id_aa64pfr0_read,
6084 .writefn = arm_cp_write_ignore },
6085 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
6086 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
6087 .access = PL1_R, .type = ARM_CP_CONST,
6088 .resetvalue = cpu->isar.id_aa64pfr1},
6089 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6090 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
6091 .access = PL1_R, .type = ARM_CP_CONST,
6092 .resetvalue = 0 },
6093 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6094 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
6095 .access = PL1_R, .type = ARM_CP_CONST,
6096 .resetvalue = 0 },
6097 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
6098 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
6099 .access = PL1_R, .type = ARM_CP_CONST,
6100 /* At present, only SVEver == 0 is defined anyway. */
6101 .resetvalue = 0 },
6102 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6103 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
6104 .access = PL1_R, .type = ARM_CP_CONST,
6105 .resetvalue = 0 },
6106 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6107 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
6108 .access = PL1_R, .type = ARM_CP_CONST,
6109 .resetvalue = 0 },
6110 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6111 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
6112 .access = PL1_R, .type = ARM_CP_CONST,
6113 .resetvalue = 0 },
6114 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
6115 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
6116 .access = PL1_R, .type = ARM_CP_CONST,
6117 .resetvalue = cpu->id_aa64dfr0 },
6118 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
6119 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
6120 .access = PL1_R, .type = ARM_CP_CONST,
6121 .resetvalue = cpu->id_aa64dfr1 },
6122 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6123 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
6124 .access = PL1_R, .type = ARM_CP_CONST,
6125 .resetvalue = 0 },
6126 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6127 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
6128 .access = PL1_R, .type = ARM_CP_CONST,
6129 .resetvalue = 0 },
6130 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
6131 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
6132 .access = PL1_R, .type = ARM_CP_CONST,
6133 .resetvalue = cpu->id_aa64afr0 },
6134 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
6135 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
6136 .access = PL1_R, .type = ARM_CP_CONST,
6137 .resetvalue = cpu->id_aa64afr1 },
6138 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6139 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
6140 .access = PL1_R, .type = ARM_CP_CONST,
6141 .resetvalue = 0 },
6142 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6143 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
6144 .access = PL1_R, .type = ARM_CP_CONST,
6145 .resetvalue = 0 },
6146 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
6147 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
6148 .access = PL1_R, .type = ARM_CP_CONST,
6149 .resetvalue = cpu->isar.id_aa64isar0 },
6150 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
6151 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
6152 .access = PL1_R, .type = ARM_CP_CONST,
6153 .resetvalue = cpu->isar.id_aa64isar1 },
6154 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6155 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
6156 .access = PL1_R, .type = ARM_CP_CONST,
6157 .resetvalue = 0 },
6158 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6159 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
6160 .access = PL1_R, .type = ARM_CP_CONST,
6161 .resetvalue = 0 },
6162 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6163 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
6164 .access = PL1_R, .type = ARM_CP_CONST,
6165 .resetvalue = 0 },
6166 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6167 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
6168 .access = PL1_R, .type = ARM_CP_CONST,
6169 .resetvalue = 0 },
6170 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6171 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
6172 .access = PL1_R, .type = ARM_CP_CONST,
6173 .resetvalue = 0 },
6174 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6175 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
6176 .access = PL1_R, .type = ARM_CP_CONST,
6177 .resetvalue = 0 },
6178 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
6179 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
6180 .access = PL1_R, .type = ARM_CP_CONST,
6181 .resetvalue = cpu->isar.id_aa64mmfr0 },
6182 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
6183 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
6184 .access = PL1_R, .type = ARM_CP_CONST,
6185 .resetvalue = cpu->isar.id_aa64mmfr1 },
6186 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6187 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
6188 .access = PL1_R, .type = ARM_CP_CONST,
6189 .resetvalue = 0 },
6190 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6191 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
6192 .access = PL1_R, .type = ARM_CP_CONST,
6193 .resetvalue = 0 },
6194 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6195 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
6196 .access = PL1_R, .type = ARM_CP_CONST,
6197 .resetvalue = 0 },
6198 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6199 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
6200 .access = PL1_R, .type = ARM_CP_CONST,
6201 .resetvalue = 0 },
6202 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6203 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
6204 .access = PL1_R, .type = ARM_CP_CONST,
6205 .resetvalue = 0 },
6206 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6207 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
6208 .access = PL1_R, .type = ARM_CP_CONST,
6209 .resetvalue = 0 },
6210 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
6211 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
6212 .access = PL1_R, .type = ARM_CP_CONST,
6213 .resetvalue = cpu->isar.mvfr0 },
6214 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
6215 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
6216 .access = PL1_R, .type = ARM_CP_CONST,
6217 .resetvalue = cpu->isar.mvfr1 },
6218 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
6219 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
6220 .access = PL1_R, .type = ARM_CP_CONST,
6221 .resetvalue = cpu->isar.mvfr2 },
6222 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6223 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
6224 .access = PL1_R, .type = ARM_CP_CONST,
6225 .resetvalue = 0 },
6226 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6227 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
6228 .access = PL1_R, .type = ARM_CP_CONST,
6229 .resetvalue = 0 },
6230 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6231 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
6232 .access = PL1_R, .type = ARM_CP_CONST,
6233 .resetvalue = 0 },
6234 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6235 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
6236 .access = PL1_R, .type = ARM_CP_CONST,
6237 .resetvalue = 0 },
6238 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6239 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
6240 .access = PL1_R, .type = ARM_CP_CONST,
6241 .resetvalue = 0 },
6242 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
6243 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
6244 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6245 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
6246 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
6247 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
6248 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6249 .resetvalue = cpu->pmceid0 },
6250 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
6251 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
6252 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6253 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
6254 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
6255 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
6256 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6257 .resetvalue = cpu->pmceid1 },
6258 REGINFO_SENTINEL
6260 #ifdef CONFIG_USER_ONLY
6261 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6262 { .name = "ID_AA64PFR0_EL1",
6263 .exported_bits = 0x000f000f00ff0000,
6264 .fixed_bits = 0x0000000000000011 },
6265 { .name = "ID_AA64PFR1_EL1",
6266 .exported_bits = 0x00000000000000f0 },
6267 { .name = "ID_AA64PFR*_EL1_RESERVED",
6268 .is_glob = true },
6269 { .name = "ID_AA64ZFR0_EL1" },
6270 { .name = "ID_AA64MMFR0_EL1",
6271 .fixed_bits = 0x00000000ff000000 },
6272 { .name = "ID_AA64MMFR1_EL1" },
6273 { .name = "ID_AA64MMFR*_EL1_RESERVED",
6274 .is_glob = true },
6275 { .name = "ID_AA64DFR0_EL1",
6276 .fixed_bits = 0x0000000000000006 },
6277 { .name = "ID_AA64DFR1_EL1" },
6278 { .name = "ID_AA64DFR*_EL1_RESERVED",
6279 .is_glob = true },
6280 { .name = "ID_AA64AFR*",
6281 .is_glob = true },
6282 { .name = "ID_AA64ISAR0_EL1",
6283 .exported_bits = 0x00fffffff0fffff0 },
6284 { .name = "ID_AA64ISAR1_EL1",
6285 .exported_bits = 0x000000f0ffffffff },
6286 { .name = "ID_AA64ISAR*_EL1_RESERVED",
6287 .is_glob = true },
6288 REGUSERINFO_SENTINEL
6290 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
6291 #endif
6292 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
6293 if (!arm_feature(env, ARM_FEATURE_EL3) &&
6294 !arm_feature(env, ARM_FEATURE_EL2)) {
6295 ARMCPRegInfo rvbar = {
6296 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
6297 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6298 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
6300 define_one_arm_cp_reg(cpu, &rvbar);
6302 define_arm_cp_regs(cpu, v8_idregs);
6303 define_arm_cp_regs(cpu, v8_cp_reginfo);
6305 if (arm_feature(env, ARM_FEATURE_EL2)) {
6306 uint64_t vmpidr_def = mpidr_read_val(env);
6307 ARMCPRegInfo vpidr_regs[] = {
6308 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
6309 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6310 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6311 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
6312 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
6313 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
6314 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6315 .access = PL2_RW, .resetvalue = cpu->midr,
6316 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
6317 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
6318 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6319 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6320 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
6321 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
6322 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
6323 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6324 .access = PL2_RW,
6325 .resetvalue = vmpidr_def,
6326 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
6327 REGINFO_SENTINEL
6329 define_arm_cp_regs(cpu, vpidr_regs);
6330 define_arm_cp_regs(cpu, el2_cp_reginfo);
6331 if (arm_feature(env, ARM_FEATURE_V8)) {
6332 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
6334 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
6335 if (!arm_feature(env, ARM_FEATURE_EL3)) {
6336 ARMCPRegInfo rvbar = {
6337 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
6338 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
6339 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
6341 define_one_arm_cp_reg(cpu, &rvbar);
6343 } else {
6344 /* If EL2 is missing but higher ELs are enabled, we need to
6345 * register the no_el2 reginfos.
6347 if (arm_feature(env, ARM_FEATURE_EL3)) {
6348 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
6349 * of MIDR_EL1 and MPIDR_EL1.
6351 ARMCPRegInfo vpidr_regs[] = {
6352 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6353 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
6354 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
6355 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
6356 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
6357 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6358 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
6359 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
6360 .type = ARM_CP_NO_RAW,
6361 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
6362 REGINFO_SENTINEL
6364 define_arm_cp_regs(cpu, vpidr_regs);
6365 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
6366 if (arm_feature(env, ARM_FEATURE_V8)) {
6367 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
6371 if (arm_feature(env, ARM_FEATURE_EL3)) {
6372 define_arm_cp_regs(cpu, el3_cp_reginfo);
6373 ARMCPRegInfo el3_regs[] = {
6374 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
6375 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
6376 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
6377 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
6378 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
6379 .access = PL3_RW,
6380 .raw_writefn = raw_write, .writefn = sctlr_write,
6381 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
6382 .resetvalue = cpu->reset_sctlr },
6383 REGINFO_SENTINEL
6386 define_arm_cp_regs(cpu, el3_regs);
6388 /* The behaviour of NSACR is sufficiently various that we don't
6389 * try to describe it in a single reginfo:
6390 * if EL3 is 64 bit, then trap to EL3 from S EL1,
6391 * reads as constant 0xc00 from NS EL1 and NS EL2
6392 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
6393 * if v7 without EL3, register doesn't exist
6394 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
6396 if (arm_feature(env, ARM_FEATURE_EL3)) {
6397 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6398 ARMCPRegInfo nsacr = {
6399 .name = "NSACR", .type = ARM_CP_CONST,
6400 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6401 .access = PL1_RW, .accessfn = nsacr_access,
6402 .resetvalue = 0xc00
6404 define_one_arm_cp_reg(cpu, &nsacr);
6405 } else {
6406 ARMCPRegInfo nsacr = {
6407 .name = "NSACR",
6408 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6409 .access = PL3_RW | PL1_R,
6410 .resetvalue = 0,
6411 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
6413 define_one_arm_cp_reg(cpu, &nsacr);
6415 } else {
6416 if (arm_feature(env, ARM_FEATURE_V8)) {
6417 ARMCPRegInfo nsacr = {
6418 .name = "NSACR", .type = ARM_CP_CONST,
6419 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
6420 .access = PL1_R,
6421 .resetvalue = 0xc00
6423 define_one_arm_cp_reg(cpu, &nsacr);
6427 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6428 if (arm_feature(env, ARM_FEATURE_V6)) {
6429 /* PMSAv6 not implemented */
6430 assert(arm_feature(env, ARM_FEATURE_V7));
6431 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
6432 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
6433 } else {
6434 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
6436 } else {
6437 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
6438 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
6439 /* TTCBR2 is introduced with ARMv8.2-A32HPD. */
6440 if (FIELD_EX32(cpu->id_mmfr4, ID_MMFR4, HPDS) != 0) {
6441 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
6444 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
6445 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
6447 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
6448 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
6450 if (arm_feature(env, ARM_FEATURE_VAPA)) {
6451 define_arm_cp_regs(cpu, vapa_cp_reginfo);
6453 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
6454 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
6456 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
6457 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
6459 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
6460 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
6462 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
6463 define_arm_cp_regs(cpu, omap_cp_reginfo);
6465 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
6466 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
6468 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
6469 define_arm_cp_regs(cpu, xscale_cp_reginfo);
6471 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
6472 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
6474 if (arm_feature(env, ARM_FEATURE_LPAE)) {
6475 define_arm_cp_regs(cpu, lpae_cp_reginfo);
6477 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
6478 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
6479 * be read-only (ie write causes UNDEF exception).
6482 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
6483 /* Pre-v8 MIDR space.
6484 * Note that the MIDR isn't a simple constant register because
6485 * of the TI925 behaviour where writes to another register can
6486 * cause the MIDR value to change.
6488 * Unimplemented registers in the c15 0 0 0 space default to
6489 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
6490 * and friends override accordingly.
6492 { .name = "MIDR",
6493 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
6494 .access = PL1_R, .resetvalue = cpu->midr,
6495 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
6496 .readfn = midr_read,
6497 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
6498 .type = ARM_CP_OVERRIDE },
6499 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
6500 { .name = "DUMMY",
6501 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
6502 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6503 { .name = "DUMMY",
6504 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
6505 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6506 { .name = "DUMMY",
6507 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
6508 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6509 { .name = "DUMMY",
6510 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
6511 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6512 { .name = "DUMMY",
6513 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
6514 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6515 REGINFO_SENTINEL
6517 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
6518 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
6519 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
6520 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
6521 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
6522 .readfn = midr_read },
6523 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
6524 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
6525 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
6526 .access = PL1_R, .resetvalue = cpu->midr },
6527 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
6528 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
6529 .access = PL1_R, .resetvalue = cpu->midr },
6530 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
6531 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
6532 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
6533 REGINFO_SENTINEL
6535 ARMCPRegInfo id_cp_reginfo[] = {
6536 /* These are common to v8 and pre-v8 */
6537 { .name = "CTR",
6538 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
6539 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
6540 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
6541 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
6542 .access = PL0_R, .accessfn = ctr_el0_access,
6543 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
6544 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
6545 { .name = "TCMTR",
6546 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
6547 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
6548 REGINFO_SENTINEL
6550 /* TLBTR is specific to VMSA */
6551 ARMCPRegInfo id_tlbtr_reginfo = {
6552 .name = "TLBTR",
6553 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
6554 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
6556 /* MPUIR is specific to PMSA V6+ */
6557 ARMCPRegInfo id_mpuir_reginfo = {
6558 .name = "MPUIR",
6559 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
6560 .access = PL1_R, .type = ARM_CP_CONST,
6561 .resetvalue = cpu->pmsav7_dregion << 8
6563 ARMCPRegInfo crn0_wi_reginfo = {
6564 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
6565 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
6566 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
6568 #ifdef CONFIG_USER_ONLY
6569 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6570 { .name = "MIDR_EL1",
6571 .exported_bits = 0x00000000ffffffff },
6572 { .name = "REVIDR_EL1" },
6573 REGUSERINFO_SENTINEL
6575 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
6576 #endif
6577 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
6578 arm_feature(env, ARM_FEATURE_STRONGARM)) {
6579 ARMCPRegInfo *r;
6580 /* Register the blanket "writes ignored" value first to cover the
6581 * whole space. Then update the specific ID registers to allow write
6582 * access, so that they ignore writes rather than causing them to
6583 * UNDEF.
6585 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
6586 for (r = id_pre_v8_midr_cp_reginfo;
6587 r->type != ARM_CP_SENTINEL; r++) {
6588 r->access = PL1_RW;
6590 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
6591 r->access = PL1_RW;
6593 id_mpuir_reginfo.access = PL1_RW;
6594 id_tlbtr_reginfo.access = PL1_RW;
6596 if (arm_feature(env, ARM_FEATURE_V8)) {
6597 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
6598 } else {
6599 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
6601 define_arm_cp_regs(cpu, id_cp_reginfo);
6602 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
6603 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
6604 } else if (arm_feature(env, ARM_FEATURE_V7)) {
6605 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
6609 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
6610 ARMCPRegInfo mpidr_cp_reginfo[] = {
6611 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
6612 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
6613 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
6614 REGINFO_SENTINEL
6616 #ifdef CONFIG_USER_ONLY
6617 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
6618 { .name = "MPIDR_EL1",
6619 .fixed_bits = 0x0000000080000000 },
6620 REGUSERINFO_SENTINEL
6622 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
6623 #endif
6624 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
6627 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
6628 ARMCPRegInfo auxcr_reginfo[] = {
6629 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
6630 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
6631 .access = PL1_RW, .type = ARM_CP_CONST,
6632 .resetvalue = cpu->reset_auxcr },
6633 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
6634 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
6635 .access = PL2_RW, .type = ARM_CP_CONST,
6636 .resetvalue = 0 },
6637 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
6638 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
6639 .access = PL3_RW, .type = ARM_CP_CONST,
6640 .resetvalue = 0 },
6641 REGINFO_SENTINEL
6643 define_arm_cp_regs(cpu, auxcr_reginfo);
6644 if (arm_feature(env, ARM_FEATURE_V8)) {
6645 /* HACTLR2 maps to ACTLR_EL2[63:32] and is not in ARMv7 */
6646 ARMCPRegInfo hactlr2_reginfo = {
6647 .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
6648 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
6649 .access = PL2_RW, .type = ARM_CP_CONST,
6650 .resetvalue = 0
6652 define_one_arm_cp_reg(cpu, &hactlr2_reginfo);
6656 if (arm_feature(env, ARM_FEATURE_CBAR)) {
6657 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6658 /* 32 bit view is [31:18] 0...0 [43:32]. */
6659 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
6660 | extract64(cpu->reset_cbar, 32, 12);
6661 ARMCPRegInfo cbar_reginfo[] = {
6662 { .name = "CBAR",
6663 .type = ARM_CP_CONST,
6664 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
6665 .access = PL1_R, .resetvalue = cpu->reset_cbar },
6666 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
6667 .type = ARM_CP_CONST,
6668 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
6669 .access = PL1_R, .resetvalue = cbar32 },
6670 REGINFO_SENTINEL
6672 /* We don't implement a r/w 64 bit CBAR currently */
6673 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
6674 define_arm_cp_regs(cpu, cbar_reginfo);
6675 } else {
6676 ARMCPRegInfo cbar = {
6677 .name = "CBAR",
6678 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
6679 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
6680 .fieldoffset = offsetof(CPUARMState,
6681 cp15.c15_config_base_address)
6683 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
6684 cbar.access = PL1_R;
6685 cbar.fieldoffset = 0;
6686 cbar.type = ARM_CP_CONST;
6688 define_one_arm_cp_reg(cpu, &cbar);
6692 if (arm_feature(env, ARM_FEATURE_VBAR)) {
6693 ARMCPRegInfo vbar_cp_reginfo[] = {
6694 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
6695 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
6696 .access = PL1_RW, .writefn = vbar_write,
6697 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
6698 offsetof(CPUARMState, cp15.vbar_ns) },
6699 .resetvalue = 0 },
6700 REGINFO_SENTINEL
6702 define_arm_cp_regs(cpu, vbar_cp_reginfo);
6705 /* Generic registers whose values depend on the implementation */
6707 ARMCPRegInfo sctlr = {
6708 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
6709 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
6710 .access = PL1_RW,
6711 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
6712 offsetof(CPUARMState, cp15.sctlr_ns) },
6713 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
6714 .raw_writefn = raw_write,
6716 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
6717 /* Normally we would always end the TB on an SCTLR write, but Linux
6718 * arch/arm/mach-pxa/sleep.S expects two instructions following
6719 * an MMU enable to execute from cache. Imitate this behaviour.
6721 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
6723 define_one_arm_cp_reg(cpu, &sctlr);
6726 if (cpu_isar_feature(aa64_lor, cpu)) {
6728 * A trivial implementation of ARMv8.1-LOR leaves all of these
6729 * registers fixed at 0, which indicates that there are zero
6730 * supported Limited Ordering regions.
6732 static const ARMCPRegInfo lor_reginfo[] = {
6733 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6734 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6735 .access = PL1_RW, .accessfn = access_lor_other,
6736 .type = ARM_CP_CONST, .resetvalue = 0 },
6737 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6738 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6739 .access = PL1_RW, .accessfn = access_lor_other,
6740 .type = ARM_CP_CONST, .resetvalue = 0 },
6741 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6742 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6743 .access = PL1_RW, .accessfn = access_lor_other,
6744 .type = ARM_CP_CONST, .resetvalue = 0 },
6745 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6746 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6747 .access = PL1_RW, .accessfn = access_lor_other,
6748 .type = ARM_CP_CONST, .resetvalue = 0 },
6749 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6750 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6751 .access = PL1_R, .accessfn = access_lorid,
6752 .type = ARM_CP_CONST, .resetvalue = 0 },
6753 REGINFO_SENTINEL
6755 define_arm_cp_regs(cpu, lor_reginfo);
6758 if (cpu_isar_feature(aa64_sve, cpu)) {
6759 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
6760 if (arm_feature(env, ARM_FEATURE_EL2)) {
6761 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
6762 } else {
6763 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
6765 if (arm_feature(env, ARM_FEATURE_EL3)) {
6766 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
6770 #ifdef TARGET_AARCH64
6771 if (cpu_isar_feature(aa64_pauth, cpu)) {
6772 define_arm_cp_regs(cpu, pauth_reginfo);
6774 if (cpu_isar_feature(aa64_rndr, cpu)) {
6775 define_arm_cp_regs(cpu, rndr_reginfo);
6777 #endif
6780 * While all v8.0 cpus support aarch64, QEMU does have configurations
6781 * that do not set ID_AA64ISAR1, e.g. user-only qemu-arm -cpu max,
6782 * which will set ID_ISAR6.
6784 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)
6785 ? cpu_isar_feature(aa64_predinv, cpu)
6786 : cpu_isar_feature(aa32_predinv, cpu)) {
6787 define_arm_cp_regs(cpu, predinv_reginfo);
6791 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
6793 CPUState *cs = CPU(cpu);
6794 CPUARMState *env = &cpu->env;
6796 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6797 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
6798 aarch64_fpu_gdb_set_reg,
6799 34, "aarch64-fpu.xml", 0);
6800 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
6801 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
6802 51, "arm-neon.xml", 0);
6803 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
6804 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
6805 35, "arm-vfp3.xml", 0);
6806 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
6807 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
6808 19, "arm-vfp.xml", 0);
6810 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
6811 arm_gen_dynamic_xml(cs),
6812 "system-registers.xml", 0);
6815 /* Sort alphabetically by type name, except for "any". */
6816 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
6818 ObjectClass *class_a = (ObjectClass *)a;
6819 ObjectClass *class_b = (ObjectClass *)b;
6820 const char *name_a, *name_b;
6822 name_a = object_class_get_name(class_a);
6823 name_b = object_class_get_name(class_b);
6824 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
6825 return 1;
6826 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
6827 return -1;
6828 } else {
6829 return strcmp(name_a, name_b);
6833 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
6835 ObjectClass *oc = data;
6836 const char *typename;
6837 char *name;
6839 typename = object_class_get_name(oc);
6840 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
6841 qemu_printf(" %s\n", name);
6842 g_free(name);
6845 void arm_cpu_list(void)
6847 GSList *list;
6849 list = object_class_get_list(TYPE_ARM_CPU, false);
6850 list = g_slist_sort(list, arm_cpu_list_compare);
6851 qemu_printf("Available CPUs:\n");
6852 g_slist_foreach(list, arm_cpu_list_entry, NULL);
6853 g_slist_free(list);
6856 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
6858 ObjectClass *oc = data;
6859 CpuDefinitionInfoList **cpu_list = user_data;
6860 CpuDefinitionInfoList *entry;
6861 CpuDefinitionInfo *info;
6862 const char *typename;
6864 typename = object_class_get_name(oc);
6865 info = g_malloc0(sizeof(*info));
6866 info->name = g_strndup(typename,
6867 strlen(typename) - strlen("-" TYPE_ARM_CPU));
6868 info->q_typename = g_strdup(typename);
6870 entry = g_malloc0(sizeof(*entry));
6871 entry->value = info;
6872 entry->next = *cpu_list;
6873 *cpu_list = entry;
6876 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
6878 CpuDefinitionInfoList *cpu_list = NULL;
6879 GSList *list;
6881 list = object_class_get_list(TYPE_ARM_CPU, false);
6882 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
6883 g_slist_free(list);
6885 return cpu_list;
6888 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
6889 void *opaque, int state, int secstate,
6890 int crm, int opc1, int opc2,
6891 const char *name)
6893 /* Private utility function for define_one_arm_cp_reg_with_opaque():
6894 * add a single reginfo struct to the hash table.
6896 uint32_t *key = g_new(uint32_t, 1);
6897 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
6898 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
6899 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
6901 r2->name = g_strdup(name);
6902 /* Reset the secure state to the specific incoming state. This is
6903 * necessary as the register may have been defined with both states.
6905 r2->secure = secstate;
6907 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
6908 /* Register is banked (using both entries in array).
6909 * Overwriting fieldoffset as the array is only used to define
6910 * banked registers but later only fieldoffset is used.
6912 r2->fieldoffset = r->bank_fieldoffsets[ns];
6915 if (state == ARM_CP_STATE_AA32) {
6916 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
6917 /* If the register is banked then we don't need to migrate or
6918 * reset the 32-bit instance in certain cases:
6920 * 1) If the register has both 32-bit and 64-bit instances then we
6921 * can count on the 64-bit instance taking care of the
6922 * non-secure bank.
6923 * 2) If ARMv8 is enabled then we can count on a 64-bit version
6924 * taking care of the secure bank. This requires that separate
6925 * 32 and 64-bit definitions are provided.
6927 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
6928 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
6929 r2->type |= ARM_CP_ALIAS;
6931 } else if ((secstate != r->secure) && !ns) {
6932 /* The register is not banked so we only want to allow migration of
6933 * the non-secure instance.
6935 r2->type |= ARM_CP_ALIAS;
6938 if (r->state == ARM_CP_STATE_BOTH) {
6939 /* We assume it is a cp15 register if the .cp field is left unset.
6941 if (r2->cp == 0) {
6942 r2->cp = 15;
6945 #ifdef HOST_WORDS_BIGENDIAN
6946 if (r2->fieldoffset) {
6947 r2->fieldoffset += sizeof(uint32_t);
6949 #endif
6952 if (state == ARM_CP_STATE_AA64) {
6953 /* To allow abbreviation of ARMCPRegInfo
6954 * definitions, we treat cp == 0 as equivalent to
6955 * the value for "standard guest-visible sysreg".
6956 * STATE_BOTH definitions are also always "standard
6957 * sysreg" in their AArch64 view (the .cp value may
6958 * be non-zero for the benefit of the AArch32 view).
6960 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
6961 r2->cp = CP_REG_ARM64_SYSREG_CP;
6963 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
6964 r2->opc0, opc1, opc2);
6965 } else {
6966 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
6968 if (opaque) {
6969 r2->opaque = opaque;
6971 /* reginfo passed to helpers is correct for the actual access,
6972 * and is never ARM_CP_STATE_BOTH:
6974 r2->state = state;
6975 /* Make sure reginfo passed to helpers for wildcarded regs
6976 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
6978 r2->crm = crm;
6979 r2->opc1 = opc1;
6980 r2->opc2 = opc2;
6981 /* By convention, for wildcarded registers only the first
6982 * entry is used for migration; the others are marked as
6983 * ALIAS so we don't try to transfer the register
6984 * multiple times. Special registers (ie NOP/WFI) are
6985 * never migratable and not even raw-accessible.
6987 if ((r->type & ARM_CP_SPECIAL)) {
6988 r2->type |= ARM_CP_NO_RAW;
6990 if (((r->crm == CP_ANY) && crm != 0) ||
6991 ((r->opc1 == CP_ANY) && opc1 != 0) ||
6992 ((r->opc2 == CP_ANY) && opc2 != 0)) {
6993 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6996 /* Check that raw accesses are either forbidden or handled. Note that
6997 * we can't assert this earlier because the setup of fieldoffset for
6998 * banked registers has to be done first.
7000 if (!(r2->type & ARM_CP_NO_RAW)) {
7001 assert(!raw_accessors_invalid(r2));
7004 /* Overriding of an existing definition must be explicitly
7005 * requested.
7007 if (!(r->type & ARM_CP_OVERRIDE)) {
7008 ARMCPRegInfo *oldreg;
7009 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
7010 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
7011 fprintf(stderr, "Register redefined: cp=%d %d bit "
7012 "crn=%d crm=%d opc1=%d opc2=%d, "
7013 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
7014 r2->crn, r2->crm, r2->opc1, r2->opc2,
7015 oldreg->name, r2->name);
7016 g_assert_not_reached();
7019 g_hash_table_insert(cpu->cp_regs, key, r2);
7023 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
7024 const ARMCPRegInfo *r, void *opaque)
7026 /* Define implementations of coprocessor registers.
7027 * We store these in a hashtable because typically
7028 * there are less than 150 registers in a space which
7029 * is 16*16*16*8*8 = 262144 in size.
7030 * Wildcarding is supported for the crm, opc1 and opc2 fields.
7031 * If a register is defined twice then the second definition is
7032 * used, so this can be used to define some generic registers and
7033 * then override them with implementation specific variations.
7034 * At least one of the original and the second definition should
7035 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
7036 * against accidental use.
7038 * The state field defines whether the register is to be
7039 * visible in the AArch32 or AArch64 execution state. If the
7040 * state is set to ARM_CP_STATE_BOTH then we synthesise a
7041 * reginfo structure for the AArch32 view, which sees the lower
7042 * 32 bits of the 64 bit register.
7044 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
7045 * be wildcarded. AArch64 registers are always considered to be 64
7046 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
7047 * the register, if any.
7049 int crm, opc1, opc2, state;
7050 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
7051 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
7052 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
7053 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
7054 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
7055 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
7056 /* 64 bit registers have only CRm and Opc1 fields */
7057 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
7058 /* op0 only exists in the AArch64 encodings */
7059 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
7060 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
7061 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
7062 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
7063 * encodes a minimum access level for the register. We roll this
7064 * runtime check into our general permission check code, so check
7065 * here that the reginfo's specified permissions are strict enough
7066 * to encompass the generic architectural permission check.
7068 if (r->state != ARM_CP_STATE_AA32) {
7069 int mask = 0;
7070 switch (r->opc1) {
7071 case 0:
7072 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
7073 mask = PL0U_R | PL1_RW;
7074 break;
7075 case 1: case 2:
7076 /* min_EL EL1 */
7077 mask = PL1_RW;
7078 break;
7079 case 3:
7080 /* min_EL EL0 */
7081 mask = PL0_RW;
7082 break;
7083 case 4:
7084 /* min_EL EL2 */
7085 mask = PL2_RW;
7086 break;
7087 case 5:
7088 /* unallocated encoding, so not possible */
7089 assert(false);
7090 break;
7091 case 6:
7092 /* min_EL EL3 */
7093 mask = PL3_RW;
7094 break;
7095 case 7:
7096 /* min_EL EL1, secure mode only (we don't check the latter) */
7097 mask = PL1_RW;
7098 break;
7099 default:
7100 /* broken reginfo with out-of-range opc1 */
7101 assert(false);
7102 break;
7104 /* assert our permissions are not too lax (stricter is fine) */
7105 assert((r->access & ~mask) == 0);
7108 /* Check that the register definition has enough info to handle
7109 * reads and writes if they are permitted.
7111 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
7112 if (r->access & PL3_R) {
7113 assert((r->fieldoffset ||
7114 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
7115 r->readfn);
7117 if (r->access & PL3_W) {
7118 assert((r->fieldoffset ||
7119 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
7120 r->writefn);
7123 /* Bad type field probably means missing sentinel at end of reg list */
7124 assert(cptype_valid(r->type));
7125 for (crm = crmmin; crm <= crmmax; crm++) {
7126 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
7127 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
7128 for (state = ARM_CP_STATE_AA32;
7129 state <= ARM_CP_STATE_AA64; state++) {
7130 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
7131 continue;
7133 if (state == ARM_CP_STATE_AA32) {
7134 /* Under AArch32 CP registers can be common
7135 * (same for secure and non-secure world) or banked.
7137 char *name;
7139 switch (r->secure) {
7140 case ARM_CP_SECSTATE_S:
7141 case ARM_CP_SECSTATE_NS:
7142 add_cpreg_to_hashtable(cpu, r, opaque, state,
7143 r->secure, crm, opc1, opc2,
7144 r->name);
7145 break;
7146 default:
7147 name = g_strdup_printf("%s_S", r->name);
7148 add_cpreg_to_hashtable(cpu, r, opaque, state,
7149 ARM_CP_SECSTATE_S,
7150 crm, opc1, opc2, name);
7151 g_free(name);
7152 add_cpreg_to_hashtable(cpu, r, opaque, state,
7153 ARM_CP_SECSTATE_NS,
7154 crm, opc1, opc2, r->name);
7155 break;
7157 } else {
7158 /* AArch64 registers get mapped to non-secure instance
7159 * of AArch32 */
7160 add_cpreg_to_hashtable(cpu, r, opaque, state,
7161 ARM_CP_SECSTATE_NS,
7162 crm, opc1, opc2, r->name);
7170 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
7171 const ARMCPRegInfo *regs, void *opaque)
7173 /* Define a whole list of registers */
7174 const ARMCPRegInfo *r;
7175 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
7176 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
7181 * Modify ARMCPRegInfo for access from userspace.
7183 * This is a data driven modification directed by
7184 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
7185 * user-space cannot alter any values and dynamic values pertaining to
7186 * execution state are hidden from user space view anyway.
7188 void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
7190 const ARMCPRegUserSpaceInfo *m;
7191 ARMCPRegInfo *r;
7193 for (m = mods; m->name; m++) {
7194 GPatternSpec *pat = NULL;
7195 if (m->is_glob) {
7196 pat = g_pattern_spec_new(m->name);
7198 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
7199 if (pat && g_pattern_match_string(pat, r->name)) {
7200 r->type = ARM_CP_CONST;
7201 r->access = PL0U_R;
7202 r->resetvalue = 0;
7203 /* continue */
7204 } else if (strcmp(r->name, m->name) == 0) {
7205 r->type = ARM_CP_CONST;
7206 r->access = PL0U_R;
7207 r->resetvalue &= m->exported_bits;
7208 r->resetvalue |= m->fixed_bits;
7209 break;
7212 if (pat) {
7213 g_pattern_spec_free(pat);
7218 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
7220 return g_hash_table_lookup(cpregs, &encoded_cp);
7223 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
7224 uint64_t value)
7226 /* Helper coprocessor write function for write-ignore registers */
7229 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
7231 /* Helper coprocessor write function for read-as-zero registers */
7232 return 0;
7235 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
7237 /* Helper coprocessor reset function for do-nothing-on-reset registers */
7240 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
7242 /* Return true if it is not valid for us to switch to
7243 * this CPU mode (ie all the UNPREDICTABLE cases in
7244 * the ARM ARM CPSRWriteByInstr pseudocode).
7247 /* Changes to or from Hyp via MSR and CPS are illegal. */
7248 if (write_type == CPSRWriteByInstr &&
7249 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
7250 mode == ARM_CPU_MODE_HYP)) {
7251 return 1;
7254 switch (mode) {
7255 case ARM_CPU_MODE_USR:
7256 return 0;
7257 case ARM_CPU_MODE_SYS:
7258 case ARM_CPU_MODE_SVC:
7259 case ARM_CPU_MODE_ABT:
7260 case ARM_CPU_MODE_UND:
7261 case ARM_CPU_MODE_IRQ:
7262 case ARM_CPU_MODE_FIQ:
7263 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
7264 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
7266 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
7267 * and CPS are treated as illegal mode changes.
7269 if (write_type == CPSRWriteByInstr &&
7270 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7271 (arm_hcr_el2_eff(env) & HCR_TGE)) {
7272 return 1;
7274 return 0;
7275 case ARM_CPU_MODE_HYP:
7276 return !arm_feature(env, ARM_FEATURE_EL2)
7277 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env);
7278 case ARM_CPU_MODE_MON:
7279 return arm_current_el(env) < 3;
7280 default:
7281 return 1;
7285 uint32_t cpsr_read(CPUARMState *env)
7287 int ZF;
7288 ZF = (env->ZF == 0);
7289 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
7290 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
7291 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
7292 | ((env->condexec_bits & 0xfc) << 8)
7293 | (env->GE << 16) | (env->daif & CPSR_AIF);
7296 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
7297 CPSRWriteType write_type)
7299 uint32_t changed_daif;
7301 if (mask & CPSR_NZCV) {
7302 env->ZF = (~val) & CPSR_Z;
7303 env->NF = val;
7304 env->CF = (val >> 29) & 1;
7305 env->VF = (val << 3) & 0x80000000;
7307 if (mask & CPSR_Q)
7308 env->QF = ((val & CPSR_Q) != 0);
7309 if (mask & CPSR_T)
7310 env->thumb = ((val & CPSR_T) != 0);
7311 if (mask & CPSR_IT_0_1) {
7312 env->condexec_bits &= ~3;
7313 env->condexec_bits |= (val >> 25) & 3;
7315 if (mask & CPSR_IT_2_7) {
7316 env->condexec_bits &= 3;
7317 env->condexec_bits |= (val >> 8) & 0xfc;
7319 if (mask & CPSR_GE) {
7320 env->GE = (val >> 16) & 0xf;
7323 /* In a V7 implementation that includes the security extensions but does
7324 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
7325 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
7326 * bits respectively.
7328 * In a V8 implementation, it is permitted for privileged software to
7329 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
7331 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
7332 arm_feature(env, ARM_FEATURE_EL3) &&
7333 !arm_feature(env, ARM_FEATURE_EL2) &&
7334 !arm_is_secure(env)) {
7336 changed_daif = (env->daif ^ val) & mask;
7338 if (changed_daif & CPSR_A) {
7339 /* Check to see if we are allowed to change the masking of async
7340 * abort exceptions from a non-secure state.
7342 if (!(env->cp15.scr_el3 & SCR_AW)) {
7343 qemu_log_mask(LOG_GUEST_ERROR,
7344 "Ignoring attempt to switch CPSR_A flag from "
7345 "non-secure world with SCR.AW bit clear\n");
7346 mask &= ~CPSR_A;
7350 if (changed_daif & CPSR_F) {
7351 /* Check to see if we are allowed to change the masking of FIQ
7352 * exceptions from a non-secure state.
7354 if (!(env->cp15.scr_el3 & SCR_FW)) {
7355 qemu_log_mask(LOG_GUEST_ERROR,
7356 "Ignoring attempt to switch CPSR_F flag from "
7357 "non-secure world with SCR.FW bit clear\n");
7358 mask &= ~CPSR_F;
7361 /* Check whether non-maskable FIQ (NMFI) support is enabled.
7362 * If this bit is set software is not allowed to mask
7363 * FIQs, but is allowed to set CPSR_F to 0.
7365 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
7366 (val & CPSR_F)) {
7367 qemu_log_mask(LOG_GUEST_ERROR,
7368 "Ignoring attempt to enable CPSR_F flag "
7369 "(non-maskable FIQ [NMFI] support enabled)\n");
7370 mask &= ~CPSR_F;
7375 env->daif &= ~(CPSR_AIF & mask);
7376 env->daif |= val & CPSR_AIF & mask;
7378 if (write_type != CPSRWriteRaw &&
7379 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
7380 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
7381 /* Note that we can only get here in USR mode if this is a
7382 * gdb stub write; for this case we follow the architectural
7383 * behaviour for guest writes in USR mode of ignoring an attempt
7384 * to switch mode. (Those are caught by translate.c for writes
7385 * triggered by guest instructions.)
7387 mask &= ~CPSR_M;
7388 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
7389 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
7390 * v7, and has defined behaviour in v8:
7391 * + leave CPSR.M untouched
7392 * + allow changes to the other CPSR fields
7393 * + set PSTATE.IL
7394 * For user changes via the GDB stub, we don't set PSTATE.IL,
7395 * as this would be unnecessarily harsh for a user error.
7397 mask &= ~CPSR_M;
7398 if (write_type != CPSRWriteByGDBStub &&
7399 arm_feature(env, ARM_FEATURE_V8)) {
7400 mask |= CPSR_IL;
7401 val |= CPSR_IL;
7403 qemu_log_mask(LOG_GUEST_ERROR,
7404 "Illegal AArch32 mode switch attempt from %s to %s\n",
7405 aarch32_mode_name(env->uncached_cpsr),
7406 aarch32_mode_name(val));
7407 } else {
7408 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
7409 write_type == CPSRWriteExceptionReturn ?
7410 "Exception return from AArch32" :
7411 "AArch32 mode switch from",
7412 aarch32_mode_name(env->uncached_cpsr),
7413 aarch32_mode_name(val), env->regs[15]);
7414 switch_mode(env, val & CPSR_M);
7417 mask &= ~CACHED_CPSR_BITS;
7418 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
7421 /* Sign/zero extend */
7422 uint32_t HELPER(sxtb16)(uint32_t x)
7424 uint32_t res;
7425 res = (uint16_t)(int8_t)x;
7426 res |= (uint32_t)(int8_t)(x >> 16) << 16;
7427 return res;
7430 uint32_t HELPER(uxtb16)(uint32_t x)
7432 uint32_t res;
7433 res = (uint16_t)(uint8_t)x;
7434 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
7435 return res;
7438 int32_t HELPER(sdiv)(int32_t num, int32_t den)
7440 if (den == 0)
7441 return 0;
7442 if (num == INT_MIN && den == -1)
7443 return INT_MIN;
7444 return num / den;
7447 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
7449 if (den == 0)
7450 return 0;
7451 return num / den;
7454 uint32_t HELPER(rbit)(uint32_t x)
7456 return revbit32(x);
7459 #ifdef CONFIG_USER_ONLY
7461 static void switch_mode(CPUARMState *env, int mode)
7463 ARMCPU *cpu = env_archcpu(env);
7465 if (mode != ARM_CPU_MODE_USR) {
7466 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
7470 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
7471 uint32_t cur_el, bool secure)
7473 return 1;
7476 void aarch64_sync_64_to_32(CPUARMState *env)
7478 g_assert_not_reached();
7481 #else
7483 static void switch_mode(CPUARMState *env, int mode)
7485 int old_mode;
7486 int i;
7488 old_mode = env->uncached_cpsr & CPSR_M;
7489 if (mode == old_mode)
7490 return;
7492 if (old_mode == ARM_CPU_MODE_FIQ) {
7493 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
7494 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
7495 } else if (mode == ARM_CPU_MODE_FIQ) {
7496 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
7497 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
7500 i = bank_number(old_mode);
7501 env->banked_r13[i] = env->regs[13];
7502 env->banked_spsr[i] = env->spsr;
7504 i = bank_number(mode);
7505 env->regs[13] = env->banked_r13[i];
7506 env->spsr = env->banked_spsr[i];
7508 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
7509 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
7512 /* Physical Interrupt Target EL Lookup Table
7514 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
7516 * The below multi-dimensional table is used for looking up the target
7517 * exception level given numerous condition criteria. Specifically, the
7518 * target EL is based on SCR and HCR routing controls as well as the
7519 * currently executing EL and secure state.
7521 * Dimensions:
7522 * target_el_table[2][2][2][2][2][4]
7523 * | | | | | +--- Current EL
7524 * | | | | +------ Non-secure(0)/Secure(1)
7525 * | | | +--------- HCR mask override
7526 * | | +------------ SCR exec state control
7527 * | +--------------- SCR mask override
7528 * +------------------ 32-bit(0)/64-bit(1) EL3
7530 * The table values are as such:
7531 * 0-3 = EL0-EL3
7532 * -1 = Cannot occur
7534 * The ARM ARM target EL table includes entries indicating that an "exception
7535 * is not taken". The two cases where this is applicable are:
7536 * 1) An exception is taken from EL3 but the SCR does not have the exception
7537 * routed to EL3.
7538 * 2) An exception is taken from EL2 but the HCR does not have the exception
7539 * routed to EL2.
7540 * In these two cases, the below table contain a target of EL1. This value is
7541 * returned as it is expected that the consumer of the table data will check
7542 * for "target EL >= current EL" to ensure the exception is not taken.
7544 * SCR HCR
7545 * 64 EA AMO From
7546 * BIT IRQ IMO Non-secure Secure
7547 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
7549 static const int8_t target_el_table[2][2][2][2][2][4] = {
7550 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
7551 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
7552 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
7553 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
7554 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
7555 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
7556 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
7557 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
7558 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
7559 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
7560 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
7561 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
7562 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
7563 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
7564 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
7565 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
7569 * Determine the target EL for physical exceptions
7571 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
7572 uint32_t cur_el, bool secure)
7574 CPUARMState *env = cs->env_ptr;
7575 bool rw;
7576 bool scr;
7577 bool hcr;
7578 int target_el;
7579 /* Is the highest EL AArch64? */
7580 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
7581 uint64_t hcr_el2;
7583 if (arm_feature(env, ARM_FEATURE_EL3)) {
7584 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
7585 } else {
7586 /* Either EL2 is the highest EL (and so the EL2 register width
7587 * is given by is64); or there is no EL2 or EL3, in which case
7588 * the value of 'rw' does not affect the table lookup anyway.
7590 rw = is64;
7593 hcr_el2 = arm_hcr_el2_eff(env);
7594 switch (excp_idx) {
7595 case EXCP_IRQ:
7596 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
7597 hcr = hcr_el2 & HCR_IMO;
7598 break;
7599 case EXCP_FIQ:
7600 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
7601 hcr = hcr_el2 & HCR_FMO;
7602 break;
7603 default:
7604 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
7605 hcr = hcr_el2 & HCR_AMO;
7606 break;
7609 /* Perform a table-lookup for the target EL given the current state */
7610 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
7612 assert(target_el > 0);
7614 return target_el;
7617 void arm_log_exception(int idx)
7619 if (qemu_loglevel_mask(CPU_LOG_INT)) {
7620 const char *exc = NULL;
7621 static const char * const excnames[] = {
7622 [EXCP_UDEF] = "Undefined Instruction",
7623 [EXCP_SWI] = "SVC",
7624 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
7625 [EXCP_DATA_ABORT] = "Data Abort",
7626 [EXCP_IRQ] = "IRQ",
7627 [EXCP_FIQ] = "FIQ",
7628 [EXCP_BKPT] = "Breakpoint",
7629 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
7630 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
7631 [EXCP_HVC] = "Hypervisor Call",
7632 [EXCP_HYP_TRAP] = "Hypervisor Trap",
7633 [EXCP_SMC] = "Secure Monitor Call",
7634 [EXCP_VIRQ] = "Virtual IRQ",
7635 [EXCP_VFIQ] = "Virtual FIQ",
7636 [EXCP_SEMIHOST] = "Semihosting call",
7637 [EXCP_NOCP] = "v7M NOCP UsageFault",
7638 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
7639 [EXCP_STKOF] = "v8M STKOF UsageFault",
7640 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
7641 [EXCP_LSERR] = "v8M LSERR UsageFault",
7642 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
7645 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
7646 exc = excnames[idx];
7648 if (!exc) {
7649 exc = "unknown";
7651 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
7656 * Function used to synchronize QEMU's AArch64 register set with AArch32
7657 * register set. This is necessary when switching between AArch32 and AArch64
7658 * execution state.
7660 void aarch64_sync_32_to_64(CPUARMState *env)
7662 int i;
7663 uint32_t mode = env->uncached_cpsr & CPSR_M;
7665 /* We can blanket copy R[0:7] to X[0:7] */
7666 for (i = 0; i < 8; i++) {
7667 env->xregs[i] = env->regs[i];
7671 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
7672 * Otherwise, they come from the banked user regs.
7674 if (mode == ARM_CPU_MODE_FIQ) {
7675 for (i = 8; i < 13; i++) {
7676 env->xregs[i] = env->usr_regs[i - 8];
7678 } else {
7679 for (i = 8; i < 13; i++) {
7680 env->xregs[i] = env->regs[i];
7685 * Registers x13-x23 are the various mode SP and FP registers. Registers
7686 * r13 and r14 are only copied if we are in that mode, otherwise we copy
7687 * from the mode banked register.
7689 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7690 env->xregs[13] = env->regs[13];
7691 env->xregs[14] = env->regs[14];
7692 } else {
7693 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
7694 /* HYP is an exception in that it is copied from r14 */
7695 if (mode == ARM_CPU_MODE_HYP) {
7696 env->xregs[14] = env->regs[14];
7697 } else {
7698 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
7702 if (mode == ARM_CPU_MODE_HYP) {
7703 env->xregs[15] = env->regs[13];
7704 } else {
7705 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
7708 if (mode == ARM_CPU_MODE_IRQ) {
7709 env->xregs[16] = env->regs[14];
7710 env->xregs[17] = env->regs[13];
7711 } else {
7712 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
7713 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
7716 if (mode == ARM_CPU_MODE_SVC) {
7717 env->xregs[18] = env->regs[14];
7718 env->xregs[19] = env->regs[13];
7719 } else {
7720 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
7721 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
7724 if (mode == ARM_CPU_MODE_ABT) {
7725 env->xregs[20] = env->regs[14];
7726 env->xregs[21] = env->regs[13];
7727 } else {
7728 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
7729 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
7732 if (mode == ARM_CPU_MODE_UND) {
7733 env->xregs[22] = env->regs[14];
7734 env->xregs[23] = env->regs[13];
7735 } else {
7736 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
7737 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
7741 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7742 * mode, then we can copy from r8-r14. Otherwise, we copy from the
7743 * FIQ bank for r8-r14.
7745 if (mode == ARM_CPU_MODE_FIQ) {
7746 for (i = 24; i < 31; i++) {
7747 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
7749 } else {
7750 for (i = 24; i < 29; i++) {
7751 env->xregs[i] = env->fiq_regs[i - 24];
7753 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
7754 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
7757 env->pc = env->regs[15];
7761 * Function used to synchronize QEMU's AArch32 register set with AArch64
7762 * register set. This is necessary when switching between AArch32 and AArch64
7763 * execution state.
7765 void aarch64_sync_64_to_32(CPUARMState *env)
7767 int i;
7768 uint32_t mode = env->uncached_cpsr & CPSR_M;
7770 /* We can blanket copy X[0:7] to R[0:7] */
7771 for (i = 0; i < 8; i++) {
7772 env->regs[i] = env->xregs[i];
7776 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
7777 * Otherwise, we copy x8-x12 into the banked user regs.
7779 if (mode == ARM_CPU_MODE_FIQ) {
7780 for (i = 8; i < 13; i++) {
7781 env->usr_regs[i - 8] = env->xregs[i];
7783 } else {
7784 for (i = 8; i < 13; i++) {
7785 env->regs[i] = env->xregs[i];
7790 * Registers r13 & r14 depend on the current mode.
7791 * If we are in a given mode, we copy the corresponding x registers to r13
7792 * and r14. Otherwise, we copy the x register to the banked r13 and r14
7793 * for the mode.
7795 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
7796 env->regs[13] = env->xregs[13];
7797 env->regs[14] = env->xregs[14];
7798 } else {
7799 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
7802 * HYP is an exception in that it does not have its own banked r14 but
7803 * shares the USR r14
7805 if (mode == ARM_CPU_MODE_HYP) {
7806 env->regs[14] = env->xregs[14];
7807 } else {
7808 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
7812 if (mode == ARM_CPU_MODE_HYP) {
7813 env->regs[13] = env->xregs[15];
7814 } else {
7815 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
7818 if (mode == ARM_CPU_MODE_IRQ) {
7819 env->regs[14] = env->xregs[16];
7820 env->regs[13] = env->xregs[17];
7821 } else {
7822 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
7823 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
7826 if (mode == ARM_CPU_MODE_SVC) {
7827 env->regs[14] = env->xregs[18];
7828 env->regs[13] = env->xregs[19];
7829 } else {
7830 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
7831 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
7834 if (mode == ARM_CPU_MODE_ABT) {
7835 env->regs[14] = env->xregs[20];
7836 env->regs[13] = env->xregs[21];
7837 } else {
7838 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
7839 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
7842 if (mode == ARM_CPU_MODE_UND) {
7843 env->regs[14] = env->xregs[22];
7844 env->regs[13] = env->xregs[23];
7845 } else {
7846 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
7847 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
7850 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
7851 * mode, then we can copy to r8-r14. Otherwise, we copy to the
7852 * FIQ bank for r8-r14.
7854 if (mode == ARM_CPU_MODE_FIQ) {
7855 for (i = 24; i < 31; i++) {
7856 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
7858 } else {
7859 for (i = 24; i < 29; i++) {
7860 env->fiq_regs[i - 24] = env->xregs[i];
7862 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
7863 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
7866 env->regs[15] = env->pc;
7869 static void take_aarch32_exception(CPUARMState *env, int new_mode,
7870 uint32_t mask, uint32_t offset,
7871 uint32_t newpc)
7873 /* Change the CPU state so as to actually take the exception. */
7874 switch_mode(env, new_mode);
7876 * For exceptions taken to AArch32 we must clear the SS bit in both
7877 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
7879 env->uncached_cpsr &= ~PSTATE_SS;
7880 env->spsr = cpsr_read(env);
7881 /* Clear IT bits. */
7882 env->condexec_bits = 0;
7883 /* Switch to the new mode, and to the correct instruction set. */
7884 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
7885 /* Set new mode endianness */
7886 env->uncached_cpsr &= ~CPSR_E;
7887 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
7888 env->uncached_cpsr |= CPSR_E;
7890 /* J and IL must always be cleared for exception entry */
7891 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
7892 env->daif |= mask;
7894 if (new_mode == ARM_CPU_MODE_HYP) {
7895 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
7896 env->elr_el[2] = env->regs[15];
7897 } else {
7899 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
7900 * and we should just guard the thumb mode on V4
7902 if (arm_feature(env, ARM_FEATURE_V4T)) {
7903 env->thumb =
7904 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
7906 env->regs[14] = env->regs[15] + offset;
7908 env->regs[15] = newpc;
7911 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
7914 * Handle exception entry to Hyp mode; this is sufficiently
7915 * different to entry to other AArch32 modes that we handle it
7916 * separately here.
7918 * The vector table entry used is always the 0x14 Hyp mode entry point,
7919 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
7920 * The offset applied to the preferred return address is always zero
7921 * (see DDI0487C.a section G1.12.3).
7922 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
7924 uint32_t addr, mask;
7925 ARMCPU *cpu = ARM_CPU(cs);
7926 CPUARMState *env = &cpu->env;
7928 switch (cs->exception_index) {
7929 case EXCP_UDEF:
7930 addr = 0x04;
7931 break;
7932 case EXCP_SWI:
7933 addr = 0x14;
7934 break;
7935 case EXCP_BKPT:
7936 /* Fall through to prefetch abort. */
7937 case EXCP_PREFETCH_ABORT:
7938 env->cp15.ifar_s = env->exception.vaddress;
7939 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
7940 (uint32_t)env->exception.vaddress);
7941 addr = 0x0c;
7942 break;
7943 case EXCP_DATA_ABORT:
7944 env->cp15.dfar_s = env->exception.vaddress;
7945 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
7946 (uint32_t)env->exception.vaddress);
7947 addr = 0x10;
7948 break;
7949 case EXCP_IRQ:
7950 addr = 0x18;
7951 break;
7952 case EXCP_FIQ:
7953 addr = 0x1c;
7954 break;
7955 case EXCP_HVC:
7956 addr = 0x08;
7957 break;
7958 case EXCP_HYP_TRAP:
7959 addr = 0x14;
7960 break;
7961 default:
7962 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
7965 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
7966 if (!arm_feature(env, ARM_FEATURE_V8)) {
7968 * QEMU syndrome values are v8-style. v7 has the IL bit
7969 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
7970 * If this is a v7 CPU, squash the IL bit in those cases.
7972 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
7973 (cs->exception_index == EXCP_DATA_ABORT &&
7974 !(env->exception.syndrome & ARM_EL_ISV)) ||
7975 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
7976 env->exception.syndrome &= ~ARM_EL_IL;
7979 env->cp15.esr_el[2] = env->exception.syndrome;
7982 if (arm_current_el(env) != 2 && addr < 0x14) {
7983 addr = 0x14;
7986 mask = 0;
7987 if (!(env->cp15.scr_el3 & SCR_EA)) {
7988 mask |= CPSR_A;
7990 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
7991 mask |= CPSR_I;
7993 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
7994 mask |= CPSR_F;
7997 addr += env->cp15.hvbar;
7999 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
8002 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
8004 ARMCPU *cpu = ARM_CPU(cs);
8005 CPUARMState *env = &cpu->env;
8006 uint32_t addr;
8007 uint32_t mask;
8008 int new_mode;
8009 uint32_t offset;
8010 uint32_t moe;
8012 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
8013 switch (syn_get_ec(env->exception.syndrome)) {
8014 case EC_BREAKPOINT:
8015 case EC_BREAKPOINT_SAME_EL:
8016 moe = 1;
8017 break;
8018 case EC_WATCHPOINT:
8019 case EC_WATCHPOINT_SAME_EL:
8020 moe = 10;
8021 break;
8022 case EC_AA32_BKPT:
8023 moe = 3;
8024 break;
8025 case EC_VECTORCATCH:
8026 moe = 5;
8027 break;
8028 default:
8029 moe = 0;
8030 break;
8033 if (moe) {
8034 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
8037 if (env->exception.target_el == 2) {
8038 arm_cpu_do_interrupt_aarch32_hyp(cs);
8039 return;
8042 switch (cs->exception_index) {
8043 case EXCP_UDEF:
8044 new_mode = ARM_CPU_MODE_UND;
8045 addr = 0x04;
8046 mask = CPSR_I;
8047 if (env->thumb)
8048 offset = 2;
8049 else
8050 offset = 4;
8051 break;
8052 case EXCP_SWI:
8053 new_mode = ARM_CPU_MODE_SVC;
8054 addr = 0x08;
8055 mask = CPSR_I;
8056 /* The PC already points to the next instruction. */
8057 offset = 0;
8058 break;
8059 case EXCP_BKPT:
8060 /* Fall through to prefetch abort. */
8061 case EXCP_PREFETCH_ABORT:
8062 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
8063 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
8064 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
8065 env->exception.fsr, (uint32_t)env->exception.vaddress);
8066 new_mode = ARM_CPU_MODE_ABT;
8067 addr = 0x0c;
8068 mask = CPSR_A | CPSR_I;
8069 offset = 4;
8070 break;
8071 case EXCP_DATA_ABORT:
8072 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
8073 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
8074 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
8075 env->exception.fsr,
8076 (uint32_t)env->exception.vaddress);
8077 new_mode = ARM_CPU_MODE_ABT;
8078 addr = 0x10;
8079 mask = CPSR_A | CPSR_I;
8080 offset = 8;
8081 break;
8082 case EXCP_IRQ:
8083 new_mode = ARM_CPU_MODE_IRQ;
8084 addr = 0x18;
8085 /* Disable IRQ and imprecise data aborts. */
8086 mask = CPSR_A | CPSR_I;
8087 offset = 4;
8088 if (env->cp15.scr_el3 & SCR_IRQ) {
8089 /* IRQ routed to monitor mode */
8090 new_mode = ARM_CPU_MODE_MON;
8091 mask |= CPSR_F;
8093 break;
8094 case EXCP_FIQ:
8095 new_mode = ARM_CPU_MODE_FIQ;
8096 addr = 0x1c;
8097 /* Disable FIQ, IRQ and imprecise data aborts. */
8098 mask = CPSR_A | CPSR_I | CPSR_F;
8099 if (env->cp15.scr_el3 & SCR_FIQ) {
8100 /* FIQ routed to monitor mode */
8101 new_mode = ARM_CPU_MODE_MON;
8103 offset = 4;
8104 break;
8105 case EXCP_VIRQ:
8106 new_mode = ARM_CPU_MODE_IRQ;
8107 addr = 0x18;
8108 /* Disable IRQ and imprecise data aborts. */
8109 mask = CPSR_A | CPSR_I;
8110 offset = 4;
8111 break;
8112 case EXCP_VFIQ:
8113 new_mode = ARM_CPU_MODE_FIQ;
8114 addr = 0x1c;
8115 /* Disable FIQ, IRQ and imprecise data aborts. */
8116 mask = CPSR_A | CPSR_I | CPSR_F;
8117 offset = 4;
8118 break;
8119 case EXCP_SMC:
8120 new_mode = ARM_CPU_MODE_MON;
8121 addr = 0x08;
8122 mask = CPSR_A | CPSR_I | CPSR_F;
8123 offset = 0;
8124 break;
8125 default:
8126 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8127 return; /* Never happens. Keep compiler happy. */
8130 if (new_mode == ARM_CPU_MODE_MON) {
8131 addr += env->cp15.mvbar;
8132 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
8133 /* High vectors. When enabled, base address cannot be remapped. */
8134 addr += 0xffff0000;
8135 } else {
8136 /* ARM v7 architectures provide a vector base address register to remap
8137 * the interrupt vector table.
8138 * This register is only followed in non-monitor mode, and is banked.
8139 * Note: only bits 31:5 are valid.
8141 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
8144 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
8145 env->cp15.scr_el3 &= ~SCR_NS;
8148 take_aarch32_exception(env, new_mode, mask, offset, addr);
8151 /* Handle exception entry to a target EL which is using AArch64 */
8152 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
8154 ARMCPU *cpu = ARM_CPU(cs);
8155 CPUARMState *env = &cpu->env;
8156 unsigned int new_el = env->exception.target_el;
8157 target_ulong addr = env->cp15.vbar_el[new_el];
8158 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
8159 unsigned int cur_el = arm_current_el(env);
8162 * Note that new_el can never be 0. If cur_el is 0, then
8163 * el0_a64 is is_a64(), else el0_a64 is ignored.
8165 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
8167 if (cur_el < new_el) {
8168 /* Entry vector offset depends on whether the implemented EL
8169 * immediately lower than the target level is using AArch32 or AArch64
8171 bool is_aa64;
8173 switch (new_el) {
8174 case 3:
8175 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
8176 break;
8177 case 2:
8178 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
8179 break;
8180 case 1:
8181 is_aa64 = is_a64(env);
8182 break;
8183 default:
8184 g_assert_not_reached();
8187 if (is_aa64) {
8188 addr += 0x400;
8189 } else {
8190 addr += 0x600;
8192 } else if (pstate_read(env) & PSTATE_SP) {
8193 addr += 0x200;
8196 switch (cs->exception_index) {
8197 case EXCP_PREFETCH_ABORT:
8198 case EXCP_DATA_ABORT:
8199 env->cp15.far_el[new_el] = env->exception.vaddress;
8200 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
8201 env->cp15.far_el[new_el]);
8202 /* fall through */
8203 case EXCP_BKPT:
8204 case EXCP_UDEF:
8205 case EXCP_SWI:
8206 case EXCP_HVC:
8207 case EXCP_HYP_TRAP:
8208 case EXCP_SMC:
8209 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) {
8211 * QEMU internal FP/SIMD syndromes from AArch32 include the
8212 * TA and coproc fields which are only exposed if the exception
8213 * is taken to AArch32 Hyp mode. Mask them out to get a valid
8214 * AArch64 format syndrome.
8216 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
8218 env->cp15.esr_el[new_el] = env->exception.syndrome;
8219 break;
8220 case EXCP_IRQ:
8221 case EXCP_VIRQ:
8222 addr += 0x80;
8223 break;
8224 case EXCP_FIQ:
8225 case EXCP_VFIQ:
8226 addr += 0x100;
8227 break;
8228 case EXCP_SEMIHOST:
8229 qemu_log_mask(CPU_LOG_INT,
8230 "...handling as semihosting call 0x%" PRIx64 "\n",
8231 env->xregs[0]);
8232 env->xregs[0] = do_arm_semihosting(env);
8233 return;
8234 default:
8235 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8238 if (is_a64(env)) {
8239 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
8240 aarch64_save_sp(env, arm_current_el(env));
8241 env->elr_el[new_el] = env->pc;
8242 } else {
8243 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
8244 env->elr_el[new_el] = env->regs[15];
8246 aarch64_sync_32_to_64(env);
8248 env->condexec_bits = 0;
8250 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
8251 env->elr_el[new_el]);
8253 pstate_write(env, PSTATE_DAIF | new_mode);
8254 env->aarch64 = 1;
8255 aarch64_restore_sp(env, new_el);
8257 env->pc = addr;
8259 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
8260 new_el, env->pc, pstate_read(env));
8263 static inline bool check_for_semihosting(CPUState *cs)
8265 #ifdef CONFIG_TCG
8266 /* Check whether this exception is a semihosting call; if so
8267 * then handle it and return true; otherwise return false.
8269 ARMCPU *cpu = ARM_CPU(cs);
8270 CPUARMState *env = &cpu->env;
8272 if (is_a64(env)) {
8273 if (cs->exception_index == EXCP_SEMIHOST) {
8274 /* This is always the 64-bit semihosting exception.
8275 * The "is this usermode" and "is semihosting enabled"
8276 * checks have been done at translate time.
8278 qemu_log_mask(CPU_LOG_INT,
8279 "...handling as semihosting call 0x%" PRIx64 "\n",
8280 env->xregs[0]);
8281 env->xregs[0] = do_arm_semihosting(env);
8282 return true;
8284 return false;
8285 } else {
8286 uint32_t imm;
8288 /* Only intercept calls from privileged modes, to provide some
8289 * semblance of security.
8291 if (cs->exception_index != EXCP_SEMIHOST &&
8292 (!semihosting_enabled() ||
8293 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) {
8294 return false;
8297 switch (cs->exception_index) {
8298 case EXCP_SEMIHOST:
8299 /* This is always a semihosting call; the "is this usermode"
8300 * and "is semihosting enabled" checks have been done at
8301 * translate time.
8303 break;
8304 case EXCP_SWI:
8305 /* Check for semihosting interrupt. */
8306 if (env->thumb) {
8307 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
8308 & 0xff;
8309 if (imm == 0xab) {
8310 break;
8312 } else {
8313 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
8314 & 0xffffff;
8315 if (imm == 0x123456) {
8316 break;
8319 return false;
8320 case EXCP_BKPT:
8321 /* See if this is a semihosting syscall. */
8322 if (env->thumb) {
8323 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
8324 & 0xff;
8325 if (imm == 0xab) {
8326 env->regs[15] += 2;
8327 break;
8330 return false;
8331 default:
8332 return false;
8335 qemu_log_mask(CPU_LOG_INT,
8336 "...handling as semihosting call 0x%x\n",
8337 env->regs[0]);
8338 env->regs[0] = do_arm_semihosting(env);
8339 return true;
8341 #else
8342 return false;
8343 #endif
8346 /* Handle a CPU exception for A and R profile CPUs.
8347 * Do any appropriate logging, handle PSCI calls, and then hand off
8348 * to the AArch64-entry or AArch32-entry function depending on the
8349 * target exception level's register width.
8351 void arm_cpu_do_interrupt(CPUState *cs)
8353 ARMCPU *cpu = ARM_CPU(cs);
8354 CPUARMState *env = &cpu->env;
8355 unsigned int new_el = env->exception.target_el;
8357 assert(!arm_feature(env, ARM_FEATURE_M));
8359 arm_log_exception(cs->exception_index);
8360 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
8361 new_el);
8362 if (qemu_loglevel_mask(CPU_LOG_INT)
8363 && !excp_is_internal(cs->exception_index)) {
8364 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
8365 syn_get_ec(env->exception.syndrome),
8366 env->exception.syndrome);
8369 if (arm_is_psci_call(cpu, cs->exception_index)) {
8370 arm_handle_psci_call(cpu);
8371 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
8372 return;
8375 /* Semihosting semantics depend on the register width of the
8376 * code that caused the exception, not the target exception level,
8377 * so must be handled here.
8379 if (check_for_semihosting(cs)) {
8380 return;
8383 /* Hooks may change global state so BQL should be held, also the
8384 * BQL needs to be held for any modification of
8385 * cs->interrupt_request.
8387 g_assert(qemu_mutex_iothread_locked());
8389 arm_call_pre_el_change_hook(cpu);
8391 assert(!excp_is_internal(cs->exception_index));
8392 if (arm_el_is_aa64(env, new_el)) {
8393 arm_cpu_do_interrupt_aarch64(cs);
8394 } else {
8395 arm_cpu_do_interrupt_aarch32(cs);
8398 arm_call_el_change_hook(cpu);
8400 if (!kvm_enabled()) {
8401 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
8404 #endif /* !CONFIG_USER_ONLY */
8406 /* Return the exception level which controls this address translation regime */
8407 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
8409 switch (mmu_idx) {
8410 case ARMMMUIdx_S2NS:
8411 case ARMMMUIdx_S1E2:
8412 return 2;
8413 case ARMMMUIdx_S1E3:
8414 return 3;
8415 case ARMMMUIdx_S1SE0:
8416 return arm_el_is_aa64(env, 3) ? 1 : 3;
8417 case ARMMMUIdx_S1SE1:
8418 case ARMMMUIdx_S1NSE0:
8419 case ARMMMUIdx_S1NSE1:
8420 case ARMMMUIdx_MPrivNegPri:
8421 case ARMMMUIdx_MUserNegPri:
8422 case ARMMMUIdx_MPriv:
8423 case ARMMMUIdx_MUser:
8424 case ARMMMUIdx_MSPrivNegPri:
8425 case ARMMMUIdx_MSUserNegPri:
8426 case ARMMMUIdx_MSPriv:
8427 case ARMMMUIdx_MSUser:
8428 return 1;
8429 default:
8430 g_assert_not_reached();
8434 #ifndef CONFIG_USER_ONLY
8436 /* Return the SCTLR value which controls this address translation regime */
8437 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
8439 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
8442 /* Return true if the specified stage of address translation is disabled */
8443 static inline bool regime_translation_disabled(CPUARMState *env,
8444 ARMMMUIdx mmu_idx)
8446 if (arm_feature(env, ARM_FEATURE_M)) {
8447 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
8448 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
8449 case R_V7M_MPU_CTRL_ENABLE_MASK:
8450 /* Enabled, but not for HardFault and NMI */
8451 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
8452 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
8453 /* Enabled for all cases */
8454 return false;
8455 case 0:
8456 default:
8457 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
8458 * we warned about that in armv7m_nvic.c when the guest set it.
8460 return true;
8464 if (mmu_idx == ARMMMUIdx_S2NS) {
8465 /* HCR.DC means HCR.VM behaves as 1 */
8466 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
8469 if (env->cp15.hcr_el2 & HCR_TGE) {
8470 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
8471 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
8472 return true;
8476 if ((env->cp15.hcr_el2 & HCR_DC) &&
8477 (mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1)) {
8478 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
8479 return true;
8482 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
8485 static inline bool regime_translation_big_endian(CPUARMState *env,
8486 ARMMMUIdx mmu_idx)
8488 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
8491 /* Return the TTBR associated with this translation regime */
8492 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
8493 int ttbrn)
8495 if (mmu_idx == ARMMMUIdx_S2NS) {
8496 return env->cp15.vttbr_el2;
8498 if (ttbrn == 0) {
8499 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
8500 } else {
8501 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
8505 #endif /* !CONFIG_USER_ONLY */
8507 /* Return the TCR controlling this translation regime */
8508 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
8510 if (mmu_idx == ARMMMUIdx_S2NS) {
8511 return &env->cp15.vtcr_el2;
8513 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
8516 /* Convert a possible stage1+2 MMU index into the appropriate
8517 * stage 1 MMU index
8519 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
8521 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
8522 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0);
8524 return mmu_idx;
8527 /* Return true if the translation regime is using LPAE format page tables */
8528 static inline bool regime_using_lpae_format(CPUARMState *env,
8529 ARMMMUIdx mmu_idx)
8531 int el = regime_el(env, mmu_idx);
8532 if (el == 2 || arm_el_is_aa64(env, el)) {
8533 return true;
8535 if (arm_feature(env, ARM_FEATURE_LPAE)
8536 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
8537 return true;
8539 return false;
8542 /* Returns true if the stage 1 translation regime is using LPAE format page
8543 * tables. Used when raising alignment exceptions, whose FSR changes depending
8544 * on whether the long or short descriptor format is in use. */
8545 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
8547 mmu_idx = stage_1_mmu_idx(mmu_idx);
8549 return regime_using_lpae_format(env, mmu_idx);
8552 #ifndef CONFIG_USER_ONLY
8553 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
8555 switch (mmu_idx) {
8556 case ARMMMUIdx_S1SE0:
8557 case ARMMMUIdx_S1NSE0:
8558 case ARMMMUIdx_MUser:
8559 case ARMMMUIdx_MSUser:
8560 case ARMMMUIdx_MUserNegPri:
8561 case ARMMMUIdx_MSUserNegPri:
8562 return true;
8563 default:
8564 return false;
8565 case ARMMMUIdx_S12NSE0:
8566 case ARMMMUIdx_S12NSE1:
8567 g_assert_not_reached();
8571 /* Translate section/page access permissions to page
8572 * R/W protection flags
8574 * @env: CPUARMState
8575 * @mmu_idx: MMU index indicating required translation regime
8576 * @ap: The 3-bit access permissions (AP[2:0])
8577 * @domain_prot: The 2-bit domain access permissions
8579 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
8580 int ap, int domain_prot)
8582 bool is_user = regime_is_user(env, mmu_idx);
8584 if (domain_prot == 3) {
8585 return PAGE_READ | PAGE_WRITE;
8588 switch (ap) {
8589 case 0:
8590 if (arm_feature(env, ARM_FEATURE_V7)) {
8591 return 0;
8593 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
8594 case SCTLR_S:
8595 return is_user ? 0 : PAGE_READ;
8596 case SCTLR_R:
8597 return PAGE_READ;
8598 default:
8599 return 0;
8601 case 1:
8602 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8603 case 2:
8604 if (is_user) {
8605 return PAGE_READ;
8606 } else {
8607 return PAGE_READ | PAGE_WRITE;
8609 case 3:
8610 return PAGE_READ | PAGE_WRITE;
8611 case 4: /* Reserved. */
8612 return 0;
8613 case 5:
8614 return is_user ? 0 : PAGE_READ;
8615 case 6:
8616 return PAGE_READ;
8617 case 7:
8618 if (!arm_feature(env, ARM_FEATURE_V6K)) {
8619 return 0;
8621 return PAGE_READ;
8622 default:
8623 g_assert_not_reached();
8627 /* Translate section/page access permissions to page
8628 * R/W protection flags.
8630 * @ap: The 2-bit simple AP (AP[2:1])
8631 * @is_user: TRUE if accessing from PL0
8633 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
8635 switch (ap) {
8636 case 0:
8637 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
8638 case 1:
8639 return PAGE_READ | PAGE_WRITE;
8640 case 2:
8641 return is_user ? 0 : PAGE_READ;
8642 case 3:
8643 return PAGE_READ;
8644 default:
8645 g_assert_not_reached();
8649 static inline int
8650 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
8652 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
8655 /* Translate S2 section/page access permissions to protection flags
8657 * @env: CPUARMState
8658 * @s2ap: The 2-bit stage2 access permissions (S2AP)
8659 * @xn: XN (execute-never) bit
8661 static int get_S2prot(CPUARMState *env, int s2ap, int xn)
8663 int prot = 0;
8665 if (s2ap & 1) {
8666 prot |= PAGE_READ;
8668 if (s2ap & 2) {
8669 prot |= PAGE_WRITE;
8671 if (!xn) {
8672 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
8673 prot |= PAGE_EXEC;
8676 return prot;
8679 /* Translate section/page access permissions to protection flags
8681 * @env: CPUARMState
8682 * @mmu_idx: MMU index indicating required translation regime
8683 * @is_aa64: TRUE if AArch64
8684 * @ap: The 2-bit simple AP (AP[2:1])
8685 * @ns: NS (non-secure) bit
8686 * @xn: XN (execute-never) bit
8687 * @pxn: PXN (privileged execute-never) bit
8689 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
8690 int ap, int ns, int xn, int pxn)
8692 bool is_user = regime_is_user(env, mmu_idx);
8693 int prot_rw, user_rw;
8694 bool have_wxn;
8695 int wxn = 0;
8697 assert(mmu_idx != ARMMMUIdx_S2NS);
8699 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
8700 if (is_user) {
8701 prot_rw = user_rw;
8702 } else {
8703 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
8706 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
8707 return prot_rw;
8710 /* TODO have_wxn should be replaced with
8711 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
8712 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
8713 * compatible processors have EL2, which is required for [U]WXN.
8715 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
8717 if (have_wxn) {
8718 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
8721 if (is_aa64) {
8722 switch (regime_el(env, mmu_idx)) {
8723 case 1:
8724 if (!is_user) {
8725 xn = pxn || (user_rw & PAGE_WRITE);
8727 break;
8728 case 2:
8729 case 3:
8730 break;
8732 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8733 switch (regime_el(env, mmu_idx)) {
8734 case 1:
8735 case 3:
8736 if (is_user) {
8737 xn = xn || !(user_rw & PAGE_READ);
8738 } else {
8739 int uwxn = 0;
8740 if (have_wxn) {
8741 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
8743 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
8744 (uwxn && (user_rw & PAGE_WRITE));
8746 break;
8747 case 2:
8748 break;
8750 } else {
8751 xn = wxn = 0;
8754 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
8755 return prot_rw;
8757 return prot_rw | PAGE_EXEC;
8760 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
8761 uint32_t *table, uint32_t address)
8763 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
8764 TCR *tcr = regime_tcr(env, mmu_idx);
8766 if (address & tcr->mask) {
8767 if (tcr->raw_tcr & TTBCR_PD1) {
8768 /* Translation table walk disabled for TTBR1 */
8769 return false;
8771 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
8772 } else {
8773 if (tcr->raw_tcr & TTBCR_PD0) {
8774 /* Translation table walk disabled for TTBR0 */
8775 return false;
8777 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
8779 *table |= (address >> 18) & 0x3ffc;
8780 return true;
8783 /* Translate a S1 pagetable walk through S2 if needed. */
8784 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
8785 hwaddr addr, MemTxAttrs txattrs,
8786 ARMMMUFaultInfo *fi)
8788 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
8789 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
8790 target_ulong s2size;
8791 hwaddr s2pa;
8792 int s2prot;
8793 int ret;
8794 ARMCacheAttrs cacheattrs = {};
8795 ARMCacheAttrs *pcacheattrs = NULL;
8797 if (env->cp15.hcr_el2 & HCR_PTW) {
8799 * PTW means we must fault if this S1 walk touches S2 Device
8800 * memory; otherwise we don't care about the attributes and can
8801 * save the S2 translation the effort of computing them.
8803 pcacheattrs = &cacheattrs;
8806 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
8807 &txattrs, &s2prot, &s2size, fi, pcacheattrs);
8808 if (ret) {
8809 assert(fi->type != ARMFault_None);
8810 fi->s2addr = addr;
8811 fi->stage2 = true;
8812 fi->s1ptw = true;
8813 return ~0;
8815 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
8816 /* Access was to Device memory: generate Permission fault */
8817 fi->type = ARMFault_Permission;
8818 fi->s2addr = addr;
8819 fi->stage2 = true;
8820 fi->s1ptw = true;
8821 return ~0;
8823 addr = s2pa;
8825 return addr;
8828 /* All loads done in the course of a page table walk go through here. */
8829 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8830 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
8832 ARMCPU *cpu = ARM_CPU(cs);
8833 CPUARMState *env = &cpu->env;
8834 MemTxAttrs attrs = {};
8835 MemTxResult result = MEMTX_OK;
8836 AddressSpace *as;
8837 uint32_t data;
8839 attrs.secure = is_secure;
8840 as = arm_addressspace(cs, attrs);
8841 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
8842 if (fi->s1ptw) {
8843 return 0;
8845 if (regime_translation_big_endian(env, mmu_idx)) {
8846 data = address_space_ldl_be(as, addr, attrs, &result);
8847 } else {
8848 data = address_space_ldl_le(as, addr, attrs, &result);
8850 if (result == MEMTX_OK) {
8851 return data;
8853 fi->type = ARMFault_SyncExternalOnWalk;
8854 fi->ea = arm_extabort_type(result);
8855 return 0;
8858 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
8859 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
8861 ARMCPU *cpu = ARM_CPU(cs);
8862 CPUARMState *env = &cpu->env;
8863 MemTxAttrs attrs = {};
8864 MemTxResult result = MEMTX_OK;
8865 AddressSpace *as;
8866 uint64_t data;
8868 attrs.secure = is_secure;
8869 as = arm_addressspace(cs, attrs);
8870 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
8871 if (fi->s1ptw) {
8872 return 0;
8874 if (regime_translation_big_endian(env, mmu_idx)) {
8875 data = address_space_ldq_be(as, addr, attrs, &result);
8876 } else {
8877 data = address_space_ldq_le(as, addr, attrs, &result);
8879 if (result == MEMTX_OK) {
8880 return data;
8882 fi->type = ARMFault_SyncExternalOnWalk;
8883 fi->ea = arm_extabort_type(result);
8884 return 0;
8887 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
8888 MMUAccessType access_type, ARMMMUIdx mmu_idx,
8889 hwaddr *phys_ptr, int *prot,
8890 target_ulong *page_size,
8891 ARMMMUFaultInfo *fi)
8893 CPUState *cs = env_cpu(env);
8894 int level = 1;
8895 uint32_t table;
8896 uint32_t desc;
8897 int type;
8898 int ap;
8899 int domain = 0;
8900 int domain_prot;
8901 hwaddr phys_addr;
8902 uint32_t dacr;
8904 /* Pagetable walk. */
8905 /* Lookup l1 descriptor. */
8906 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
8907 /* Section translation fault if page walk is disabled by PD0 or PD1 */
8908 fi->type = ARMFault_Translation;
8909 goto do_fault;
8911 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8912 mmu_idx, fi);
8913 if (fi->type != ARMFault_None) {
8914 goto do_fault;
8916 type = (desc & 3);
8917 domain = (desc >> 5) & 0x0f;
8918 if (regime_el(env, mmu_idx) == 1) {
8919 dacr = env->cp15.dacr_ns;
8920 } else {
8921 dacr = env->cp15.dacr_s;
8923 domain_prot = (dacr >> (domain * 2)) & 3;
8924 if (type == 0) {
8925 /* Section translation fault. */
8926 fi->type = ARMFault_Translation;
8927 goto do_fault;
8929 if (type != 2) {
8930 level = 2;
8932 if (domain_prot == 0 || domain_prot == 2) {
8933 fi->type = ARMFault_Domain;
8934 goto do_fault;
8936 if (type == 2) {
8937 /* 1Mb section. */
8938 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
8939 ap = (desc >> 10) & 3;
8940 *page_size = 1024 * 1024;
8941 } else {
8942 /* Lookup l2 entry. */
8943 if (type == 1) {
8944 /* Coarse pagetable. */
8945 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
8946 } else {
8947 /* Fine pagetable. */
8948 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
8950 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
8951 mmu_idx, fi);
8952 if (fi->type != ARMFault_None) {
8953 goto do_fault;
8955 switch (desc & 3) {
8956 case 0: /* Page translation fault. */
8957 fi->type = ARMFault_Translation;
8958 goto do_fault;
8959 case 1: /* 64k page. */
8960 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
8961 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
8962 *page_size = 0x10000;
8963 break;
8964 case 2: /* 4k page. */
8965 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8966 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
8967 *page_size = 0x1000;
8968 break;
8969 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
8970 if (type == 1) {
8971 /* ARMv6/XScale extended small page format */
8972 if (arm_feature(env, ARM_FEATURE_XSCALE)
8973 || arm_feature(env, ARM_FEATURE_V6)) {
8974 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
8975 *page_size = 0x1000;
8976 } else {
8977 /* UNPREDICTABLE in ARMv5; we choose to take a
8978 * page translation fault.
8980 fi->type = ARMFault_Translation;
8981 goto do_fault;
8983 } else {
8984 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
8985 *page_size = 0x400;
8987 ap = (desc >> 4) & 3;
8988 break;
8989 default:
8990 /* Never happens, but compiler isn't smart enough to tell. */
8991 abort();
8994 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
8995 *prot |= *prot ? PAGE_EXEC : 0;
8996 if (!(*prot & (1 << access_type))) {
8997 /* Access permission fault. */
8998 fi->type = ARMFault_Permission;
8999 goto do_fault;
9001 *phys_ptr = phys_addr;
9002 return false;
9003 do_fault:
9004 fi->domain = domain;
9005 fi->level = level;
9006 return true;
9009 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
9010 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9011 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
9012 target_ulong *page_size, ARMMMUFaultInfo *fi)
9014 CPUState *cs = env_cpu(env);
9015 int level = 1;
9016 uint32_t table;
9017 uint32_t desc;
9018 uint32_t xn;
9019 uint32_t pxn = 0;
9020 int type;
9021 int ap;
9022 int domain = 0;
9023 int domain_prot;
9024 hwaddr phys_addr;
9025 uint32_t dacr;
9026 bool ns;
9028 /* Pagetable walk. */
9029 /* Lookup l1 descriptor. */
9030 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
9031 /* Section translation fault if page walk is disabled by PD0 or PD1 */
9032 fi->type = ARMFault_Translation;
9033 goto do_fault;
9035 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
9036 mmu_idx, fi);
9037 if (fi->type != ARMFault_None) {
9038 goto do_fault;
9040 type = (desc & 3);
9041 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
9042 /* Section translation fault, or attempt to use the encoding
9043 * which is Reserved on implementations without PXN.
9045 fi->type = ARMFault_Translation;
9046 goto do_fault;
9048 if ((type == 1) || !(desc & (1 << 18))) {
9049 /* Page or Section. */
9050 domain = (desc >> 5) & 0x0f;
9052 if (regime_el(env, mmu_idx) == 1) {
9053 dacr = env->cp15.dacr_ns;
9054 } else {
9055 dacr = env->cp15.dacr_s;
9057 if (type == 1) {
9058 level = 2;
9060 domain_prot = (dacr >> (domain * 2)) & 3;
9061 if (domain_prot == 0 || domain_prot == 2) {
9062 /* Section or Page domain fault */
9063 fi->type = ARMFault_Domain;
9064 goto do_fault;
9066 if (type != 1) {
9067 if (desc & (1 << 18)) {
9068 /* Supersection. */
9069 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
9070 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
9071 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
9072 *page_size = 0x1000000;
9073 } else {
9074 /* Section. */
9075 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
9076 *page_size = 0x100000;
9078 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
9079 xn = desc & (1 << 4);
9080 pxn = desc & 1;
9081 ns = extract32(desc, 19, 1);
9082 } else {
9083 if (arm_feature(env, ARM_FEATURE_PXN)) {
9084 pxn = (desc >> 2) & 1;
9086 ns = extract32(desc, 3, 1);
9087 /* Lookup l2 entry. */
9088 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
9089 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
9090 mmu_idx, fi);
9091 if (fi->type != ARMFault_None) {
9092 goto do_fault;
9094 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
9095 switch (desc & 3) {
9096 case 0: /* Page translation fault. */
9097 fi->type = ARMFault_Translation;
9098 goto do_fault;
9099 case 1: /* 64k page. */
9100 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
9101 xn = desc & (1 << 15);
9102 *page_size = 0x10000;
9103 break;
9104 case 2: case 3: /* 4k page. */
9105 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
9106 xn = desc & 1;
9107 *page_size = 0x1000;
9108 break;
9109 default:
9110 /* Never happens, but compiler isn't smart enough to tell. */
9111 abort();
9114 if (domain_prot == 3) {
9115 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9116 } else {
9117 if (pxn && !regime_is_user(env, mmu_idx)) {
9118 xn = 1;
9120 if (xn && access_type == MMU_INST_FETCH) {
9121 fi->type = ARMFault_Permission;
9122 goto do_fault;
9125 if (arm_feature(env, ARM_FEATURE_V6K) &&
9126 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
9127 /* The simplified model uses AP[0] as an access control bit. */
9128 if ((ap & 1) == 0) {
9129 /* Access flag fault. */
9130 fi->type = ARMFault_AccessFlag;
9131 goto do_fault;
9133 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
9134 } else {
9135 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
9137 if (*prot && !xn) {
9138 *prot |= PAGE_EXEC;
9140 if (!(*prot & (1 << access_type))) {
9141 /* Access permission fault. */
9142 fi->type = ARMFault_Permission;
9143 goto do_fault;
9146 if (ns) {
9147 /* The NS bit will (as required by the architecture) have no effect if
9148 * the CPU doesn't support TZ or this is a non-secure translation
9149 * regime, because the attribute will already be non-secure.
9151 attrs->secure = false;
9153 *phys_ptr = phys_addr;
9154 return false;
9155 do_fault:
9156 fi->domain = domain;
9157 fi->level = level;
9158 return true;
9162 * check_s2_mmu_setup
9163 * @cpu: ARMCPU
9164 * @is_aa64: True if the translation regime is in AArch64 state
9165 * @startlevel: Suggested starting level
9166 * @inputsize: Bitsize of IPAs
9167 * @stride: Page-table stride (See the ARM ARM)
9169 * Returns true if the suggested S2 translation parameters are OK and
9170 * false otherwise.
9172 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
9173 int inputsize, int stride)
9175 const int grainsize = stride + 3;
9176 int startsizecheck;
9178 /* Negative levels are never allowed. */
9179 if (level < 0) {
9180 return false;
9183 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
9184 if (startsizecheck < 1 || startsizecheck > stride + 4) {
9185 return false;
9188 if (is_aa64) {
9189 CPUARMState *env = &cpu->env;
9190 unsigned int pamax = arm_pamax(cpu);
9192 switch (stride) {
9193 case 13: /* 64KB Pages. */
9194 if (level == 0 || (level == 1 && pamax <= 42)) {
9195 return false;
9197 break;
9198 case 11: /* 16KB Pages. */
9199 if (level == 0 || (level == 1 && pamax <= 40)) {
9200 return false;
9202 break;
9203 case 9: /* 4KB Pages. */
9204 if (level == 0 && pamax <= 42) {
9205 return false;
9207 break;
9208 default:
9209 g_assert_not_reached();
9212 /* Inputsize checks. */
9213 if (inputsize > pamax &&
9214 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
9215 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
9216 return false;
9218 } else {
9219 /* AArch32 only supports 4KB pages. Assert on that. */
9220 assert(stride == 9);
9222 if (level == 0) {
9223 return false;
9226 return true;
9229 /* Translate from the 4-bit stage 2 representation of
9230 * memory attributes (without cache-allocation hints) to
9231 * the 8-bit representation of the stage 1 MAIR registers
9232 * (which includes allocation hints).
9234 * ref: shared/translation/attrs/S2AttrDecode()
9235 * .../S2ConvertAttrsHints()
9237 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
9239 uint8_t hiattr = extract32(s2attrs, 2, 2);
9240 uint8_t loattr = extract32(s2attrs, 0, 2);
9241 uint8_t hihint = 0, lohint = 0;
9243 if (hiattr != 0) { /* normal memory */
9244 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
9245 hiattr = loattr = 1; /* non-cacheable */
9246 } else {
9247 if (hiattr != 1) { /* Write-through or write-back */
9248 hihint = 3; /* RW allocate */
9250 if (loattr != 1) { /* Write-through or write-back */
9251 lohint = 3; /* RW allocate */
9256 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
9258 #endif /* !CONFIG_USER_ONLY */
9260 ARMVAParameters aa64_va_parameters_both(CPUARMState *env, uint64_t va,
9261 ARMMMUIdx mmu_idx)
9263 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
9264 uint32_t el = regime_el(env, mmu_idx);
9265 bool tbi, tbid, epd, hpd, using16k, using64k;
9266 int select, tsz;
9269 * Bit 55 is always between the two regions, and is canonical for
9270 * determining if address tagging is enabled.
9272 select = extract64(va, 55, 1);
9274 if (el > 1) {
9275 tsz = extract32(tcr, 0, 6);
9276 using64k = extract32(tcr, 14, 1);
9277 using16k = extract32(tcr, 15, 1);
9278 if (mmu_idx == ARMMMUIdx_S2NS) {
9279 /* VTCR_EL2 */
9280 tbi = tbid = hpd = false;
9281 } else {
9282 tbi = extract32(tcr, 20, 1);
9283 hpd = extract32(tcr, 24, 1);
9284 tbid = extract32(tcr, 29, 1);
9286 epd = false;
9287 } else if (!select) {
9288 tsz = extract32(tcr, 0, 6);
9289 epd = extract32(tcr, 7, 1);
9290 using64k = extract32(tcr, 14, 1);
9291 using16k = extract32(tcr, 15, 1);
9292 tbi = extract64(tcr, 37, 1);
9293 hpd = extract64(tcr, 41, 1);
9294 tbid = extract64(tcr, 51, 1);
9295 } else {
9296 int tg = extract32(tcr, 30, 2);
9297 using16k = tg == 1;
9298 using64k = tg == 3;
9299 tsz = extract32(tcr, 16, 6);
9300 epd = extract32(tcr, 23, 1);
9301 tbi = extract64(tcr, 38, 1);
9302 hpd = extract64(tcr, 42, 1);
9303 tbid = extract64(tcr, 52, 1);
9305 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
9306 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
9308 return (ARMVAParameters) {
9309 .tsz = tsz,
9310 .select = select,
9311 .tbi = tbi,
9312 .tbid = tbid,
9313 .epd = epd,
9314 .hpd = hpd,
9315 .using16k = using16k,
9316 .using64k = using64k,
9320 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
9321 ARMMMUIdx mmu_idx, bool data)
9323 ARMVAParameters ret = aa64_va_parameters_both(env, va, mmu_idx);
9325 /* Present TBI as a composite with TBID. */
9326 ret.tbi &= (data || !ret.tbid);
9327 return ret;
9330 #ifndef CONFIG_USER_ONLY
9331 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
9332 ARMMMUIdx mmu_idx)
9334 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
9335 uint32_t el = regime_el(env, mmu_idx);
9336 int select, tsz;
9337 bool epd, hpd;
9339 if (mmu_idx == ARMMMUIdx_S2NS) {
9340 /* VTCR */
9341 bool sext = extract32(tcr, 4, 1);
9342 bool sign = extract32(tcr, 3, 1);
9345 * If the sign-extend bit is not the same as t0sz[3], the result
9346 * is unpredictable. Flag this as a guest error.
9348 if (sign != sext) {
9349 qemu_log_mask(LOG_GUEST_ERROR,
9350 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
9352 tsz = sextract32(tcr, 0, 4) + 8;
9353 select = 0;
9354 hpd = false;
9355 epd = false;
9356 } else if (el == 2) {
9357 /* HTCR */
9358 tsz = extract32(tcr, 0, 3);
9359 select = 0;
9360 hpd = extract64(tcr, 24, 1);
9361 epd = false;
9362 } else {
9363 int t0sz = extract32(tcr, 0, 3);
9364 int t1sz = extract32(tcr, 16, 3);
9366 if (t1sz == 0) {
9367 select = va > (0xffffffffu >> t0sz);
9368 } else {
9369 /* Note that we will detect errors later. */
9370 select = va >= ~(0xffffffffu >> t1sz);
9372 if (!select) {
9373 tsz = t0sz;
9374 epd = extract32(tcr, 7, 1);
9375 hpd = extract64(tcr, 41, 1);
9376 } else {
9377 tsz = t1sz;
9378 epd = extract32(tcr, 23, 1);
9379 hpd = extract64(tcr, 42, 1);
9381 /* For aarch32, hpd0 is not enabled without t2e as well. */
9382 hpd &= extract32(tcr, 6, 1);
9385 return (ARMVAParameters) {
9386 .tsz = tsz,
9387 .select = select,
9388 .epd = epd,
9389 .hpd = hpd,
9393 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
9394 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9395 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
9396 target_ulong *page_size_ptr,
9397 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9399 ARMCPU *cpu = env_archcpu(env);
9400 CPUState *cs = CPU(cpu);
9401 /* Read an LPAE long-descriptor translation table. */
9402 ARMFaultType fault_type = ARMFault_Translation;
9403 uint32_t level;
9404 ARMVAParameters param;
9405 uint64_t ttbr;
9406 hwaddr descaddr, indexmask, indexmask_grainsize;
9407 uint32_t tableattrs;
9408 target_ulong page_size;
9409 uint32_t attrs;
9410 int32_t stride;
9411 int addrsize, inputsize;
9412 TCR *tcr = regime_tcr(env, mmu_idx);
9413 int ap, ns, xn, pxn;
9414 uint32_t el = regime_el(env, mmu_idx);
9415 bool ttbr1_valid;
9416 uint64_t descaddrmask;
9417 bool aarch64 = arm_el_is_aa64(env, el);
9418 bool guarded = false;
9420 /* TODO:
9421 * This code does not handle the different format TCR for VTCR_EL2.
9422 * This code also does not support shareability levels.
9423 * Attribute and permission bit handling should also be checked when adding
9424 * support for those page table walks.
9426 if (aarch64) {
9427 param = aa64_va_parameters(env, address, mmu_idx,
9428 access_type != MMU_INST_FETCH);
9429 level = 0;
9430 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
9431 * invalid.
9433 ttbr1_valid = (el < 2);
9434 addrsize = 64 - 8 * param.tbi;
9435 inputsize = 64 - param.tsz;
9436 } else {
9437 param = aa32_va_parameters(env, address, mmu_idx);
9438 level = 1;
9439 /* There is no TTBR1 for EL2 */
9440 ttbr1_valid = (el != 2);
9441 addrsize = (mmu_idx == ARMMMUIdx_S2NS ? 40 : 32);
9442 inputsize = addrsize - param.tsz;
9446 * We determined the region when collecting the parameters, but we
9447 * have not yet validated that the address is valid for the region.
9448 * Extract the top bits and verify that they all match select.
9450 * For aa32, if inputsize == addrsize, then we have selected the
9451 * region by exclusion in aa32_va_parameters and there is no more
9452 * validation to do here.
9454 if (inputsize < addrsize) {
9455 target_ulong top_bits = sextract64(address, inputsize,
9456 addrsize - inputsize);
9457 if (-top_bits != param.select || (param.select && !ttbr1_valid)) {
9458 /* The gap between the two regions is a Translation fault */
9459 fault_type = ARMFault_Translation;
9460 goto do_fault;
9464 if (param.using64k) {
9465 stride = 13;
9466 } else if (param.using16k) {
9467 stride = 11;
9468 } else {
9469 stride = 9;
9472 /* Note that QEMU ignores shareability and cacheability attributes,
9473 * so we don't need to do anything with the SH, ORGN, IRGN fields
9474 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
9475 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
9476 * implement any ASID-like capability so we can ignore it (instead
9477 * we will always flush the TLB any time the ASID is changed).
9479 ttbr = regime_ttbr(env, mmu_idx, param.select);
9481 /* Here we should have set up all the parameters for the translation:
9482 * inputsize, ttbr, epd, stride, tbi
9485 if (param.epd) {
9486 /* Translation table walk disabled => Translation fault on TLB miss
9487 * Note: This is always 0 on 64-bit EL2 and EL3.
9489 goto do_fault;
9492 if (mmu_idx != ARMMMUIdx_S2NS) {
9493 /* The starting level depends on the virtual address size (which can
9494 * be up to 48 bits) and the translation granule size. It indicates
9495 * the number of strides (stride bits at a time) needed to
9496 * consume the bits of the input address. In the pseudocode this is:
9497 * level = 4 - RoundUp((inputsize - grainsize) / stride)
9498 * where their 'inputsize' is our 'inputsize', 'grainsize' is
9499 * our 'stride + 3' and 'stride' is our 'stride'.
9500 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
9501 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
9502 * = 4 - (inputsize - 4) / stride;
9504 level = 4 - (inputsize - 4) / stride;
9505 } else {
9506 /* For stage 2 translations the starting level is specified by the
9507 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
9509 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
9510 uint32_t startlevel;
9511 bool ok;
9513 if (!aarch64 || stride == 9) {
9514 /* AArch32 or 4KB pages */
9515 startlevel = 2 - sl0;
9516 } else {
9517 /* 16KB or 64KB pages */
9518 startlevel = 3 - sl0;
9521 /* Check that the starting level is valid. */
9522 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
9523 inputsize, stride);
9524 if (!ok) {
9525 fault_type = ARMFault_Translation;
9526 goto do_fault;
9528 level = startlevel;
9531 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
9532 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
9534 /* Now we can extract the actual base address from the TTBR */
9535 descaddr = extract64(ttbr, 0, 48);
9536 descaddr &= ~indexmask;
9538 /* The address field in the descriptor goes up to bit 39 for ARMv7
9539 * but up to bit 47 for ARMv8, but we use the descaddrmask
9540 * up to bit 39 for AArch32, because we don't need other bits in that case
9541 * to construct next descriptor address (anyway they should be all zeroes).
9543 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
9544 ~indexmask_grainsize;
9546 /* Secure accesses start with the page table in secure memory and
9547 * can be downgraded to non-secure at any step. Non-secure accesses
9548 * remain non-secure. We implement this by just ORing in the NSTable/NS
9549 * bits at each step.
9551 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
9552 for (;;) {
9553 uint64_t descriptor;
9554 bool nstable;
9556 descaddr |= (address >> (stride * (4 - level))) & indexmask;
9557 descaddr &= ~7ULL;
9558 nstable = extract32(tableattrs, 4, 1);
9559 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
9560 if (fi->type != ARMFault_None) {
9561 goto do_fault;
9564 if (!(descriptor & 1) ||
9565 (!(descriptor & 2) && (level == 3))) {
9566 /* Invalid, or the Reserved level 3 encoding */
9567 goto do_fault;
9569 descaddr = descriptor & descaddrmask;
9571 if ((descriptor & 2) && (level < 3)) {
9572 /* Table entry. The top five bits are attributes which may
9573 * propagate down through lower levels of the table (and
9574 * which are all arranged so that 0 means "no effect", so
9575 * we can gather them up by ORing in the bits at each level).
9577 tableattrs |= extract64(descriptor, 59, 5);
9578 level++;
9579 indexmask = indexmask_grainsize;
9580 continue;
9582 /* Block entry at level 1 or 2, or page entry at level 3.
9583 * These are basically the same thing, although the number
9584 * of bits we pull in from the vaddr varies.
9586 page_size = (1ULL << ((stride * (4 - level)) + 3));
9587 descaddr |= (address & (page_size - 1));
9588 /* Extract attributes from the descriptor */
9589 attrs = extract64(descriptor, 2, 10)
9590 | (extract64(descriptor, 52, 12) << 10);
9592 if (mmu_idx == ARMMMUIdx_S2NS) {
9593 /* Stage 2 table descriptors do not include any attribute fields */
9594 break;
9596 /* Merge in attributes from table descriptors */
9597 attrs |= nstable << 3; /* NS */
9598 guarded = extract64(descriptor, 50, 1); /* GP */
9599 if (param.hpd) {
9600 /* HPD disables all the table attributes except NSTable. */
9601 break;
9603 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
9604 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
9605 * means "force PL1 access only", which means forcing AP[1] to 0.
9607 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
9608 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
9609 break;
9611 /* Here descaddr is the final physical address, and attributes
9612 * are all in attrs.
9614 fault_type = ARMFault_AccessFlag;
9615 if ((attrs & (1 << 8)) == 0) {
9616 /* Access flag */
9617 goto do_fault;
9620 ap = extract32(attrs, 4, 2);
9621 xn = extract32(attrs, 12, 1);
9623 if (mmu_idx == ARMMMUIdx_S2NS) {
9624 ns = true;
9625 *prot = get_S2prot(env, ap, xn);
9626 } else {
9627 ns = extract32(attrs, 3, 1);
9628 pxn = extract32(attrs, 11, 1);
9629 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
9632 fault_type = ARMFault_Permission;
9633 if (!(*prot & (1 << access_type))) {
9634 goto do_fault;
9637 if (ns) {
9638 /* The NS bit will (as required by the architecture) have no effect if
9639 * the CPU doesn't support TZ or this is a non-secure translation
9640 * regime, because the attribute will already be non-secure.
9642 txattrs->secure = false;
9644 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
9645 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
9646 txattrs->target_tlb_bit0 = true;
9649 if (cacheattrs != NULL) {
9650 if (mmu_idx == ARMMMUIdx_S2NS) {
9651 cacheattrs->attrs = convert_stage2_attrs(env,
9652 extract32(attrs, 0, 4));
9653 } else {
9654 /* Index into MAIR registers for cache attributes */
9655 uint8_t attrindx = extract32(attrs, 0, 3);
9656 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
9657 assert(attrindx <= 7);
9658 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
9660 cacheattrs->shareability = extract32(attrs, 6, 2);
9663 *phys_ptr = descaddr;
9664 *page_size_ptr = page_size;
9665 return false;
9667 do_fault:
9668 fi->type = fault_type;
9669 fi->level = level;
9670 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
9671 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
9672 return true;
9675 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
9676 ARMMMUIdx mmu_idx,
9677 int32_t address, int *prot)
9679 if (!arm_feature(env, ARM_FEATURE_M)) {
9680 *prot = PAGE_READ | PAGE_WRITE;
9681 switch (address) {
9682 case 0xF0000000 ... 0xFFFFFFFF:
9683 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
9684 /* hivecs execing is ok */
9685 *prot |= PAGE_EXEC;
9687 break;
9688 case 0x00000000 ... 0x7FFFFFFF:
9689 *prot |= PAGE_EXEC;
9690 break;
9692 } else {
9693 /* Default system address map for M profile cores.
9694 * The architecture specifies which regions are execute-never;
9695 * at the MPU level no other checks are defined.
9697 switch (address) {
9698 case 0x00000000 ... 0x1fffffff: /* ROM */
9699 case 0x20000000 ... 0x3fffffff: /* SRAM */
9700 case 0x60000000 ... 0x7fffffff: /* RAM */
9701 case 0x80000000 ... 0x9fffffff: /* RAM */
9702 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9703 break;
9704 case 0x40000000 ... 0x5fffffff: /* Peripheral */
9705 case 0xa0000000 ... 0xbfffffff: /* Device */
9706 case 0xc0000000 ... 0xdfffffff: /* Device */
9707 case 0xe0000000 ... 0xffffffff: /* System */
9708 *prot = PAGE_READ | PAGE_WRITE;
9709 break;
9710 default:
9711 g_assert_not_reached();
9716 static bool pmsav7_use_background_region(ARMCPU *cpu,
9717 ARMMMUIdx mmu_idx, bool is_user)
9719 /* Return true if we should use the default memory map as a
9720 * "background" region if there are no hits against any MPU regions.
9722 CPUARMState *env = &cpu->env;
9724 if (is_user) {
9725 return false;
9728 if (arm_feature(env, ARM_FEATURE_M)) {
9729 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
9730 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
9731 } else {
9732 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
9736 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
9738 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
9739 return arm_feature(env, ARM_FEATURE_M) &&
9740 extract32(address, 20, 12) == 0xe00;
9743 static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
9745 /* True if address is in the M profile system region
9746 * 0xe0000000 - 0xffffffff
9748 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
9751 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
9752 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9753 hwaddr *phys_ptr, int *prot,
9754 target_ulong *page_size,
9755 ARMMMUFaultInfo *fi)
9757 ARMCPU *cpu = env_archcpu(env);
9758 int n;
9759 bool is_user = regime_is_user(env, mmu_idx);
9761 *phys_ptr = address;
9762 *page_size = TARGET_PAGE_SIZE;
9763 *prot = 0;
9765 if (regime_translation_disabled(env, mmu_idx) ||
9766 m_is_ppb_region(env, address)) {
9767 /* MPU disabled or M profile PPB access: use default memory map.
9768 * The other case which uses the default memory map in the
9769 * v7M ARM ARM pseudocode is exception vector reads from the vector
9770 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
9771 * which always does a direct read using address_space_ldl(), rather
9772 * than going via this function, so we don't need to check that here.
9774 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9775 } else { /* MPU enabled */
9776 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
9777 /* region search */
9778 uint32_t base = env->pmsav7.drbar[n];
9779 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
9780 uint32_t rmask;
9781 bool srdis = false;
9783 if (!(env->pmsav7.drsr[n] & 0x1)) {
9784 continue;
9787 if (!rsize) {
9788 qemu_log_mask(LOG_GUEST_ERROR,
9789 "DRSR[%d]: Rsize field cannot be 0\n", n);
9790 continue;
9792 rsize++;
9793 rmask = (1ull << rsize) - 1;
9795 if (base & rmask) {
9796 qemu_log_mask(LOG_GUEST_ERROR,
9797 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
9798 "to DRSR region size, mask = 0x%" PRIx32 "\n",
9799 n, base, rmask);
9800 continue;
9803 if (address < base || address > base + rmask) {
9805 * Address not in this region. We must check whether the
9806 * region covers addresses in the same page as our address.
9807 * In that case we must not report a size that covers the
9808 * whole page for a subsequent hit against a different MPU
9809 * region or the background region, because it would result in
9810 * incorrect TLB hits for subsequent accesses to addresses that
9811 * are in this MPU region.
9813 if (ranges_overlap(base, rmask,
9814 address & TARGET_PAGE_MASK,
9815 TARGET_PAGE_SIZE)) {
9816 *page_size = 1;
9818 continue;
9821 /* Region matched */
9823 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
9824 int i, snd;
9825 uint32_t srdis_mask;
9827 rsize -= 3; /* sub region size (power of 2) */
9828 snd = ((address - base) >> rsize) & 0x7;
9829 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
9831 srdis_mask = srdis ? 0x3 : 0x0;
9832 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
9833 /* This will check in groups of 2, 4 and then 8, whether
9834 * the subregion bits are consistent. rsize is incremented
9835 * back up to give the region size, considering consistent
9836 * adjacent subregions as one region. Stop testing if rsize
9837 * is already big enough for an entire QEMU page.
9839 int snd_rounded = snd & ~(i - 1);
9840 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
9841 snd_rounded + 8, i);
9842 if (srdis_mask ^ srdis_multi) {
9843 break;
9845 srdis_mask = (srdis_mask << i) | srdis_mask;
9846 rsize++;
9849 if (srdis) {
9850 continue;
9852 if (rsize < TARGET_PAGE_BITS) {
9853 *page_size = 1 << rsize;
9855 break;
9858 if (n == -1) { /* no hits */
9859 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
9860 /* background fault */
9861 fi->type = ARMFault_Background;
9862 return true;
9864 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
9865 } else { /* a MPU hit! */
9866 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
9867 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
9869 if (m_is_system_region(env, address)) {
9870 /* System space is always execute never */
9871 xn = 1;
9874 if (is_user) { /* User mode AP bit decoding */
9875 switch (ap) {
9876 case 0:
9877 case 1:
9878 case 5:
9879 break; /* no access */
9880 case 3:
9881 *prot |= PAGE_WRITE;
9882 /* fall through */
9883 case 2:
9884 case 6:
9885 *prot |= PAGE_READ | PAGE_EXEC;
9886 break;
9887 case 7:
9888 /* for v7M, same as 6; for R profile a reserved value */
9889 if (arm_feature(env, ARM_FEATURE_M)) {
9890 *prot |= PAGE_READ | PAGE_EXEC;
9891 break;
9893 /* fall through */
9894 default:
9895 qemu_log_mask(LOG_GUEST_ERROR,
9896 "DRACR[%d]: Bad value for AP bits: 0x%"
9897 PRIx32 "\n", n, ap);
9899 } else { /* Priv. mode AP bits decoding */
9900 switch (ap) {
9901 case 0:
9902 break; /* no access */
9903 case 1:
9904 case 2:
9905 case 3:
9906 *prot |= PAGE_WRITE;
9907 /* fall through */
9908 case 5:
9909 case 6:
9910 *prot |= PAGE_READ | PAGE_EXEC;
9911 break;
9912 case 7:
9913 /* for v7M, same as 6; for R profile a reserved value */
9914 if (arm_feature(env, ARM_FEATURE_M)) {
9915 *prot |= PAGE_READ | PAGE_EXEC;
9916 break;
9918 /* fall through */
9919 default:
9920 qemu_log_mask(LOG_GUEST_ERROR,
9921 "DRACR[%d]: Bad value for AP bits: 0x%"
9922 PRIx32 "\n", n, ap);
9926 /* execute never */
9927 if (xn) {
9928 *prot &= ~PAGE_EXEC;
9933 fi->type = ARMFault_Permission;
9934 fi->level = 1;
9935 return !(*prot & (1 << access_type));
9938 static bool v8m_is_sau_exempt(CPUARMState *env,
9939 uint32_t address, MMUAccessType access_type)
9941 /* The architecture specifies that certain address ranges are
9942 * exempt from v8M SAU/IDAU checks.
9944 return
9945 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
9946 (address >= 0xe0000000 && address <= 0xe0002fff) ||
9947 (address >= 0xe000e000 && address <= 0xe000efff) ||
9948 (address >= 0xe002e000 && address <= 0xe002efff) ||
9949 (address >= 0xe0040000 && address <= 0xe0041fff) ||
9950 (address >= 0xe00ff000 && address <= 0xe00fffff);
9953 void v8m_security_lookup(CPUARMState *env, uint32_t address,
9954 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9955 V8M_SAttributes *sattrs)
9957 /* Look up the security attributes for this address. Compare the
9958 * pseudocode SecurityCheck() function.
9959 * We assume the caller has zero-initialized *sattrs.
9961 ARMCPU *cpu = env_archcpu(env);
9962 int r;
9963 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
9964 int idau_region = IREGION_NOTVALID;
9965 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
9966 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
9968 if (cpu->idau) {
9969 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
9970 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
9972 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
9973 &idau_nsc);
9976 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
9977 /* 0xf0000000..0xffffffff is always S for insn fetches */
9978 return;
9981 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
9982 sattrs->ns = !regime_is_secure(env, mmu_idx);
9983 return;
9986 if (idau_region != IREGION_NOTVALID) {
9987 sattrs->irvalid = true;
9988 sattrs->iregion = idau_region;
9991 switch (env->sau.ctrl & 3) {
9992 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
9993 break;
9994 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
9995 sattrs->ns = true;
9996 break;
9997 default: /* SAU.ENABLE == 1 */
9998 for (r = 0; r < cpu->sau_sregion; r++) {
9999 if (env->sau.rlar[r] & 1) {
10000 uint32_t base = env->sau.rbar[r] & ~0x1f;
10001 uint32_t limit = env->sau.rlar[r] | 0x1f;
10003 if (base <= address && limit >= address) {
10004 if (base > addr_page_base || limit < addr_page_limit) {
10005 sattrs->subpage = true;
10007 if (sattrs->srvalid) {
10008 /* If we hit in more than one region then we must report
10009 * as Secure, not NS-Callable, with no valid region
10010 * number info.
10012 sattrs->ns = false;
10013 sattrs->nsc = false;
10014 sattrs->sregion = 0;
10015 sattrs->srvalid = false;
10016 break;
10017 } else {
10018 if (env->sau.rlar[r] & 2) {
10019 sattrs->nsc = true;
10020 } else {
10021 sattrs->ns = true;
10023 sattrs->srvalid = true;
10024 sattrs->sregion = r;
10026 } else {
10028 * Address not in this region. We must check whether the
10029 * region covers addresses in the same page as our address.
10030 * In that case we must not report a size that covers the
10031 * whole page for a subsequent hit against a different MPU
10032 * region or the background region, because it would result
10033 * in incorrect TLB hits for subsequent accesses to
10034 * addresses that are in this MPU region.
10036 if (limit >= base &&
10037 ranges_overlap(base, limit - base + 1,
10038 addr_page_base,
10039 TARGET_PAGE_SIZE)) {
10040 sattrs->subpage = true;
10045 break;
10049 * The IDAU will override the SAU lookup results if it specifies
10050 * higher security than the SAU does.
10052 if (!idau_ns) {
10053 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
10054 sattrs->ns = false;
10055 sattrs->nsc = idau_nsc;
10060 bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
10061 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10062 hwaddr *phys_ptr, MemTxAttrs *txattrs,
10063 int *prot, bool *is_subpage,
10064 ARMMMUFaultInfo *fi, uint32_t *mregion)
10066 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
10067 * that a full phys-to-virt translation does).
10068 * mregion is (if not NULL) set to the region number which matched,
10069 * or -1 if no region number is returned (MPU off, address did not
10070 * hit a region, address hit in multiple regions).
10071 * We set is_subpage to true if the region hit doesn't cover the
10072 * entire TARGET_PAGE the address is within.
10074 ARMCPU *cpu = env_archcpu(env);
10075 bool is_user = regime_is_user(env, mmu_idx);
10076 uint32_t secure = regime_is_secure(env, mmu_idx);
10077 int n;
10078 int matchregion = -1;
10079 bool hit = false;
10080 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
10081 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
10083 *is_subpage = false;
10084 *phys_ptr = address;
10085 *prot = 0;
10086 if (mregion) {
10087 *mregion = -1;
10090 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
10091 * was an exception vector read from the vector table (which is always
10092 * done using the default system address map), because those accesses
10093 * are done in arm_v7m_load_vector(), which always does a direct
10094 * read using address_space_ldl(), rather than going via this function.
10096 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
10097 hit = true;
10098 } else if (m_is_ppb_region(env, address)) {
10099 hit = true;
10100 } else {
10101 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
10102 hit = true;
10105 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
10106 /* region search */
10107 /* Note that the base address is bits [31:5] from the register
10108 * with bits [4:0] all zeroes, but the limit address is bits
10109 * [31:5] from the register with bits [4:0] all ones.
10111 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
10112 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
10114 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
10115 /* Region disabled */
10116 continue;
10119 if (address < base || address > limit) {
10121 * Address not in this region. We must check whether the
10122 * region covers addresses in the same page as our address.
10123 * In that case we must not report a size that covers the
10124 * whole page for a subsequent hit against a different MPU
10125 * region or the background region, because it would result in
10126 * incorrect TLB hits for subsequent accesses to addresses that
10127 * are in this MPU region.
10129 if (limit >= base &&
10130 ranges_overlap(base, limit - base + 1,
10131 addr_page_base,
10132 TARGET_PAGE_SIZE)) {
10133 *is_subpage = true;
10135 continue;
10138 if (base > addr_page_base || limit < addr_page_limit) {
10139 *is_subpage = true;
10142 if (matchregion != -1) {
10143 /* Multiple regions match -- always a failure (unlike
10144 * PMSAv7 where highest-numbered-region wins)
10146 fi->type = ARMFault_Permission;
10147 fi->level = 1;
10148 return true;
10151 matchregion = n;
10152 hit = true;
10156 if (!hit) {
10157 /* background fault */
10158 fi->type = ARMFault_Background;
10159 return true;
10162 if (matchregion == -1) {
10163 /* hit using the background region */
10164 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10165 } else {
10166 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
10167 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
10169 if (m_is_system_region(env, address)) {
10170 /* System space is always execute never */
10171 xn = 1;
10174 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
10175 if (*prot && !xn) {
10176 *prot |= PAGE_EXEC;
10178 /* We don't need to look the attribute up in the MAIR0/MAIR1
10179 * registers because that only tells us about cacheability.
10181 if (mregion) {
10182 *mregion = matchregion;
10186 fi->type = ARMFault_Permission;
10187 fi->level = 1;
10188 return !(*prot & (1 << access_type));
10192 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
10193 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10194 hwaddr *phys_ptr, MemTxAttrs *txattrs,
10195 int *prot, target_ulong *page_size,
10196 ARMMMUFaultInfo *fi)
10198 uint32_t secure = regime_is_secure(env, mmu_idx);
10199 V8M_SAttributes sattrs = {};
10200 bool ret;
10201 bool mpu_is_subpage;
10203 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
10204 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
10205 if (access_type == MMU_INST_FETCH) {
10206 /* Instruction fetches always use the MMU bank and the
10207 * transaction attribute determined by the fetch address,
10208 * regardless of CPU state. This is painful for QEMU
10209 * to handle, because it would mean we need to encode
10210 * into the mmu_idx not just the (user, negpri) information
10211 * for the current security state but also that for the
10212 * other security state, which would balloon the number
10213 * of mmu_idx values needed alarmingly.
10214 * Fortunately we can avoid this because it's not actually
10215 * possible to arbitrarily execute code from memory with
10216 * the wrong security attribute: it will always generate
10217 * an exception of some kind or another, apart from the
10218 * special case of an NS CPU executing an SG instruction
10219 * in S&NSC memory. So we always just fail the translation
10220 * here and sort things out in the exception handler
10221 * (including possibly emulating an SG instruction).
10223 if (sattrs.ns != !secure) {
10224 if (sattrs.nsc) {
10225 fi->type = ARMFault_QEMU_NSCExec;
10226 } else {
10227 fi->type = ARMFault_QEMU_SFault;
10229 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
10230 *phys_ptr = address;
10231 *prot = 0;
10232 return true;
10234 } else {
10235 /* For data accesses we always use the MMU bank indicated
10236 * by the current CPU state, but the security attributes
10237 * might downgrade a secure access to nonsecure.
10239 if (sattrs.ns) {
10240 txattrs->secure = false;
10241 } else if (!secure) {
10242 /* NS access to S memory must fault.
10243 * Architecturally we should first check whether the
10244 * MPU information for this address indicates that we
10245 * are doing an unaligned access to Device memory, which
10246 * should generate a UsageFault instead. QEMU does not
10247 * currently check for that kind of unaligned access though.
10248 * If we added it we would need to do so as a special case
10249 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
10251 fi->type = ARMFault_QEMU_SFault;
10252 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
10253 *phys_ptr = address;
10254 *prot = 0;
10255 return true;
10260 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
10261 txattrs, prot, &mpu_is_subpage, fi, NULL);
10262 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
10263 return ret;
10266 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
10267 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10268 hwaddr *phys_ptr, int *prot,
10269 ARMMMUFaultInfo *fi)
10271 int n;
10272 uint32_t mask;
10273 uint32_t base;
10274 bool is_user = regime_is_user(env, mmu_idx);
10276 if (regime_translation_disabled(env, mmu_idx)) {
10277 /* MPU disabled. */
10278 *phys_ptr = address;
10279 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10280 return false;
10283 *phys_ptr = address;
10284 for (n = 7; n >= 0; n--) {
10285 base = env->cp15.c6_region[n];
10286 if ((base & 1) == 0) {
10287 continue;
10289 mask = 1 << ((base >> 1) & 0x1f);
10290 /* Keep this shift separate from the above to avoid an
10291 (undefined) << 32. */
10292 mask = (mask << 1) - 1;
10293 if (((base ^ address) & ~mask) == 0) {
10294 break;
10297 if (n < 0) {
10298 fi->type = ARMFault_Background;
10299 return true;
10302 if (access_type == MMU_INST_FETCH) {
10303 mask = env->cp15.pmsav5_insn_ap;
10304 } else {
10305 mask = env->cp15.pmsav5_data_ap;
10307 mask = (mask >> (n * 4)) & 0xf;
10308 switch (mask) {
10309 case 0:
10310 fi->type = ARMFault_Permission;
10311 fi->level = 1;
10312 return true;
10313 case 1:
10314 if (is_user) {
10315 fi->type = ARMFault_Permission;
10316 fi->level = 1;
10317 return true;
10319 *prot = PAGE_READ | PAGE_WRITE;
10320 break;
10321 case 2:
10322 *prot = PAGE_READ;
10323 if (!is_user) {
10324 *prot |= PAGE_WRITE;
10326 break;
10327 case 3:
10328 *prot = PAGE_READ | PAGE_WRITE;
10329 break;
10330 case 5:
10331 if (is_user) {
10332 fi->type = ARMFault_Permission;
10333 fi->level = 1;
10334 return true;
10336 *prot = PAGE_READ;
10337 break;
10338 case 6:
10339 *prot = PAGE_READ;
10340 break;
10341 default:
10342 /* Bad permission. */
10343 fi->type = ARMFault_Permission;
10344 fi->level = 1;
10345 return true;
10347 *prot |= PAGE_EXEC;
10348 return false;
10351 /* Combine either inner or outer cacheability attributes for normal
10352 * memory, according to table D4-42 and pseudocode procedure
10353 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
10355 * NB: only stage 1 includes allocation hints (RW bits), leading to
10356 * some asymmetry.
10358 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
10360 if (s1 == 4 || s2 == 4) {
10361 /* non-cacheable has precedence */
10362 return 4;
10363 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
10364 /* stage 1 write-through takes precedence */
10365 return s1;
10366 } else if (extract32(s2, 2, 2) == 2) {
10367 /* stage 2 write-through takes precedence, but the allocation hint
10368 * is still taken from stage 1
10370 return (2 << 2) | extract32(s1, 0, 2);
10371 } else { /* write-back */
10372 return s1;
10376 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
10377 * and CombineS1S2Desc()
10379 * @s1: Attributes from stage 1 walk
10380 * @s2: Attributes from stage 2 walk
10382 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
10384 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
10385 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
10386 ARMCacheAttrs ret;
10388 /* Combine shareability attributes (table D4-43) */
10389 if (s1.shareability == 2 || s2.shareability == 2) {
10390 /* if either are outer-shareable, the result is outer-shareable */
10391 ret.shareability = 2;
10392 } else if (s1.shareability == 3 || s2.shareability == 3) {
10393 /* if either are inner-shareable, the result is inner-shareable */
10394 ret.shareability = 3;
10395 } else {
10396 /* both non-shareable */
10397 ret.shareability = 0;
10400 /* Combine memory type and cacheability attributes */
10401 if (s1hi == 0 || s2hi == 0) {
10402 /* Device has precedence over normal */
10403 if (s1lo == 0 || s2lo == 0) {
10404 /* nGnRnE has precedence over anything */
10405 ret.attrs = 0;
10406 } else if (s1lo == 4 || s2lo == 4) {
10407 /* non-Reordering has precedence over Reordering */
10408 ret.attrs = 4; /* nGnRE */
10409 } else if (s1lo == 8 || s2lo == 8) {
10410 /* non-Gathering has precedence over Gathering */
10411 ret.attrs = 8; /* nGRE */
10412 } else {
10413 ret.attrs = 0xc; /* GRE */
10416 /* Any location for which the resultant memory type is any
10417 * type of Device memory is always treated as Outer Shareable.
10419 ret.shareability = 2;
10420 } else { /* Normal memory */
10421 /* Outer/inner cacheability combine independently */
10422 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
10423 | combine_cacheattr_nibble(s1lo, s2lo);
10425 if (ret.attrs == 0x44) {
10426 /* Any location for which the resultant memory type is Normal
10427 * Inner Non-cacheable, Outer Non-cacheable is always treated
10428 * as Outer Shareable.
10430 ret.shareability = 2;
10434 return ret;
10438 /* get_phys_addr - get the physical address for this virtual address
10440 * Find the physical address corresponding to the given virtual address,
10441 * by doing a translation table walk on MMU based systems or using the
10442 * MPU state on MPU based systems.
10444 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10445 * prot and page_size may not be filled in, and the populated fsr value provides
10446 * information on why the translation aborted, in the format of a
10447 * DFSR/IFSR fault register, with the following caveats:
10448 * * we honour the short vs long DFSR format differences.
10449 * * the WnR bit is never set (the caller must do this).
10450 * * for PSMAv5 based systems we don't bother to return a full FSR format
10451 * value.
10453 * @env: CPUARMState
10454 * @address: virtual address to get physical address for
10455 * @access_type: 0 for read, 1 for write, 2 for execute
10456 * @mmu_idx: MMU index indicating required translation regime
10457 * @phys_ptr: set to the physical address corresponding to the virtual address
10458 * @attrs: set to the memory transaction attributes to use
10459 * @prot: set to the permissions for the page containing phys_ptr
10460 * @page_size: set to the size of the page containing phys_ptr
10461 * @fi: set to fault info if the translation fails
10462 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
10464 bool get_phys_addr(CPUARMState *env, target_ulong address,
10465 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10466 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
10467 target_ulong *page_size,
10468 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
10470 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
10471 /* Call ourselves recursively to do the stage 1 and then stage 2
10472 * translations.
10474 if (arm_feature(env, ARM_FEATURE_EL2)) {
10475 hwaddr ipa;
10476 int s2_prot;
10477 int ret;
10478 ARMCacheAttrs cacheattrs2 = {};
10480 ret = get_phys_addr(env, address, access_type,
10481 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
10482 prot, page_size, fi, cacheattrs);
10484 /* If S1 fails or S2 is disabled, return early. */
10485 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
10486 *phys_ptr = ipa;
10487 return ret;
10490 /* S1 is done. Now do S2 translation. */
10491 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
10492 phys_ptr, attrs, &s2_prot,
10493 page_size, fi,
10494 cacheattrs != NULL ? &cacheattrs2 : NULL);
10495 fi->s2addr = ipa;
10496 /* Combine the S1 and S2 perms. */
10497 *prot &= s2_prot;
10499 /* Combine the S1 and S2 cache attributes, if needed */
10500 if (!ret && cacheattrs != NULL) {
10501 if (env->cp15.hcr_el2 & HCR_DC) {
10503 * HCR.DC forces the first stage attributes to
10504 * Normal Non-Shareable,
10505 * Inner Write-Back Read-Allocate Write-Allocate,
10506 * Outer Write-Back Read-Allocate Write-Allocate.
10508 cacheattrs->attrs = 0xff;
10509 cacheattrs->shareability = 0;
10511 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
10514 return ret;
10515 } else {
10517 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
10519 mmu_idx = stage_1_mmu_idx(mmu_idx);
10523 /* The page table entries may downgrade secure to non-secure, but
10524 * cannot upgrade an non-secure translation regime's attributes
10525 * to secure.
10527 attrs->secure = regime_is_secure(env, mmu_idx);
10528 attrs->user = regime_is_user(env, mmu_idx);
10530 /* Fast Context Switch Extension. This doesn't exist at all in v8.
10531 * In v7 and earlier it affects all stage 1 translations.
10533 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
10534 && !arm_feature(env, ARM_FEATURE_V8)) {
10535 if (regime_el(env, mmu_idx) == 3) {
10536 address += env->cp15.fcseidr_s;
10537 } else {
10538 address += env->cp15.fcseidr_ns;
10542 if (arm_feature(env, ARM_FEATURE_PMSA)) {
10543 bool ret;
10544 *page_size = TARGET_PAGE_SIZE;
10546 if (arm_feature(env, ARM_FEATURE_V8)) {
10547 /* PMSAv8 */
10548 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
10549 phys_ptr, attrs, prot, page_size, fi);
10550 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10551 /* PMSAv7 */
10552 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
10553 phys_ptr, prot, page_size, fi);
10554 } else {
10555 /* Pre-v7 MPU */
10556 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
10557 phys_ptr, prot, fi);
10559 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
10560 " mmu_idx %u -> %s (prot %c%c%c)\n",
10561 access_type == MMU_DATA_LOAD ? "reading" :
10562 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
10563 (uint32_t)address, mmu_idx,
10564 ret ? "Miss" : "Hit",
10565 *prot & PAGE_READ ? 'r' : '-',
10566 *prot & PAGE_WRITE ? 'w' : '-',
10567 *prot & PAGE_EXEC ? 'x' : '-');
10569 return ret;
10572 /* Definitely a real MMU, not an MPU */
10574 if (regime_translation_disabled(env, mmu_idx)) {
10575 /* MMU disabled. */
10576 *phys_ptr = address;
10577 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10578 *page_size = TARGET_PAGE_SIZE;
10579 return 0;
10582 if (regime_using_lpae_format(env, mmu_idx)) {
10583 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
10584 phys_ptr, attrs, prot, page_size,
10585 fi, cacheattrs);
10586 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
10587 return get_phys_addr_v6(env, address, access_type, mmu_idx,
10588 phys_ptr, attrs, prot, page_size, fi);
10589 } else {
10590 return get_phys_addr_v5(env, address, access_type, mmu_idx,
10591 phys_ptr, prot, page_size, fi);
10595 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
10596 MemTxAttrs *attrs)
10598 ARMCPU *cpu = ARM_CPU(cs);
10599 CPUARMState *env = &cpu->env;
10600 hwaddr phys_addr;
10601 target_ulong page_size;
10602 int prot;
10603 bool ret;
10604 ARMMMUFaultInfo fi = {};
10605 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
10607 *attrs = (MemTxAttrs) {};
10609 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
10610 attrs, &prot, &page_size, &fi, NULL);
10612 if (ret) {
10613 return -1;
10615 return phys_addr;
10618 #endif
10620 /* Note that signed overflow is undefined in C. The following routines are
10621 careful to use unsigned types where modulo arithmetic is required.
10622 Failure to do so _will_ break on newer gcc. */
10624 /* Signed saturating arithmetic. */
10626 /* Perform 16-bit signed saturating addition. */
10627 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10629 uint16_t res;
10631 res = a + b;
10632 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10633 if (a & 0x8000)
10634 res = 0x8000;
10635 else
10636 res = 0x7fff;
10638 return res;
10641 /* Perform 8-bit signed saturating addition. */
10642 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10644 uint8_t res;
10646 res = a + b;
10647 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10648 if (a & 0x80)
10649 res = 0x80;
10650 else
10651 res = 0x7f;
10653 return res;
10656 /* Perform 16-bit signed saturating subtraction. */
10657 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10659 uint16_t res;
10661 res = a - b;
10662 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10663 if (a & 0x8000)
10664 res = 0x8000;
10665 else
10666 res = 0x7fff;
10668 return res;
10671 /* Perform 8-bit signed saturating subtraction. */
10672 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10674 uint8_t res;
10676 res = a - b;
10677 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10678 if (a & 0x80)
10679 res = 0x80;
10680 else
10681 res = 0x7f;
10683 return res;
10686 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10687 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10688 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10689 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10690 #define PFX q
10692 #include "op_addsub.h"
10694 /* Unsigned saturating arithmetic. */
10695 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10697 uint16_t res;
10698 res = a + b;
10699 if (res < a)
10700 res = 0xffff;
10701 return res;
10704 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10706 if (a > b)
10707 return a - b;
10708 else
10709 return 0;
10712 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10714 uint8_t res;
10715 res = a + b;
10716 if (res < a)
10717 res = 0xff;
10718 return res;
10721 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10723 if (a > b)
10724 return a - b;
10725 else
10726 return 0;
10729 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10730 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10731 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10732 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10733 #define PFX uq
10735 #include "op_addsub.h"
10737 /* Signed modulo arithmetic. */
10738 #define SARITH16(a, b, n, op) do { \
10739 int32_t sum; \
10740 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10741 RESULT(sum, n, 16); \
10742 if (sum >= 0) \
10743 ge |= 3 << (n * 2); \
10744 } while(0)
10746 #define SARITH8(a, b, n, op) do { \
10747 int32_t sum; \
10748 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10749 RESULT(sum, n, 8); \
10750 if (sum >= 0) \
10751 ge |= 1 << n; \
10752 } while(0)
10755 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10756 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10757 #define ADD8(a, b, n) SARITH8(a, b, n, +)
10758 #define SUB8(a, b, n) SARITH8(a, b, n, -)
10759 #define PFX s
10760 #define ARITH_GE
10762 #include "op_addsub.h"
10764 /* Unsigned modulo arithmetic. */
10765 #define ADD16(a, b, n) do { \
10766 uint32_t sum; \
10767 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10768 RESULT(sum, n, 16); \
10769 if ((sum >> 16) == 1) \
10770 ge |= 3 << (n * 2); \
10771 } while(0)
10773 #define ADD8(a, b, n) do { \
10774 uint32_t sum; \
10775 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10776 RESULT(sum, n, 8); \
10777 if ((sum >> 8) == 1) \
10778 ge |= 1 << n; \
10779 } while(0)
10781 #define SUB16(a, b, n) do { \
10782 uint32_t sum; \
10783 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10784 RESULT(sum, n, 16); \
10785 if ((sum >> 16) == 0) \
10786 ge |= 3 << (n * 2); \
10787 } while(0)
10789 #define SUB8(a, b, n) do { \
10790 uint32_t sum; \
10791 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10792 RESULT(sum, n, 8); \
10793 if ((sum >> 8) == 0) \
10794 ge |= 1 << n; \
10795 } while(0)
10797 #define PFX u
10798 #define ARITH_GE
10800 #include "op_addsub.h"
10802 /* Halved signed arithmetic. */
10803 #define ADD16(a, b, n) \
10804 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10805 #define SUB16(a, b, n) \
10806 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10807 #define ADD8(a, b, n) \
10808 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10809 #define SUB8(a, b, n) \
10810 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10811 #define PFX sh
10813 #include "op_addsub.h"
10815 /* Halved unsigned arithmetic. */
10816 #define ADD16(a, b, n) \
10817 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10818 #define SUB16(a, b, n) \
10819 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10820 #define ADD8(a, b, n) \
10821 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10822 #define SUB8(a, b, n) \
10823 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10824 #define PFX uh
10826 #include "op_addsub.h"
10828 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10830 if (a > b)
10831 return a - b;
10832 else
10833 return b - a;
10836 /* Unsigned sum of absolute byte differences. */
10837 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10839 uint32_t sum;
10840 sum = do_usad(a, b);
10841 sum += do_usad(a >> 8, b >> 8);
10842 sum += do_usad(a >> 16, b >>16);
10843 sum += do_usad(a >> 24, b >> 24);
10844 return sum;
10847 /* For ARMv6 SEL instruction. */
10848 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10850 uint32_t mask;
10852 mask = 0;
10853 if (flags & 1)
10854 mask |= 0xff;
10855 if (flags & 2)
10856 mask |= 0xff00;
10857 if (flags & 4)
10858 mask |= 0xff0000;
10859 if (flags & 8)
10860 mask |= 0xff000000;
10861 return (a & mask) | (b & ~mask);
10864 /* CRC helpers.
10865 * The upper bytes of val (above the number specified by 'bytes') must have
10866 * been zeroed out by the caller.
10868 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
10870 uint8_t buf[4];
10872 stl_le_p(buf, val);
10874 /* zlib crc32 converts the accumulator and output to one's complement. */
10875 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
10878 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
10880 uint8_t buf[4];
10882 stl_le_p(buf, val);
10884 /* Linux crc32c converts the output to one's complement. */
10885 return crc32c(acc, buf, bytes) ^ 0xffffffff;
10888 /* Return the exception level to which FP-disabled exceptions should
10889 * be taken, or 0 if FP is enabled.
10891 int fp_exception_el(CPUARMState *env, int cur_el)
10893 #ifndef CONFIG_USER_ONLY
10894 int fpen;
10896 /* CPACR and the CPTR registers don't exist before v6, so FP is
10897 * always accessible
10899 if (!arm_feature(env, ARM_FEATURE_V6)) {
10900 return 0;
10903 if (arm_feature(env, ARM_FEATURE_M)) {
10904 /* CPACR can cause a NOCP UsageFault taken to current security state */
10905 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
10906 return 1;
10909 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
10910 if (!extract32(env->v7m.nsacr, 10, 1)) {
10911 /* FP insns cause a NOCP UsageFault taken to Secure */
10912 return 3;
10916 return 0;
10919 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
10920 * 0, 2 : trap EL0 and EL1/PL1 accesses
10921 * 1 : trap only EL0 accesses
10922 * 3 : trap no accesses
10924 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
10925 switch (fpen) {
10926 case 0:
10927 case 2:
10928 if (cur_el == 0 || cur_el == 1) {
10929 /* Trap to PL1, which might be EL1 or EL3 */
10930 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
10931 return 3;
10933 return 1;
10935 if (cur_el == 3 && !is_a64(env)) {
10936 /* Secure PL1 running at EL3 */
10937 return 3;
10939 break;
10940 case 1:
10941 if (cur_el == 0) {
10942 return 1;
10944 break;
10945 case 3:
10946 break;
10950 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
10951 * to control non-secure access to the FPU. It doesn't have any
10952 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
10954 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
10955 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
10956 if (!extract32(env->cp15.nsacr, 10, 1)) {
10957 /* FP insns act as UNDEF */
10958 return cur_el == 2 ? 2 : 1;
10962 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
10963 * check because zero bits in the registers mean "don't trap".
10966 /* CPTR_EL2 : present in v7VE or v8 */
10967 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
10968 && !arm_is_secure_below_el3(env)) {
10969 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
10970 return 2;
10973 /* CPTR_EL3 : present in v8 */
10974 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
10975 /* Trap all FP ops to EL3 */
10976 return 3;
10978 #endif
10979 return 0;
10982 #ifndef CONFIG_TCG
10983 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
10985 g_assert_not_reached();
10987 #endif
10989 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
10991 int el;
10993 if (arm_feature(env, ARM_FEATURE_M)) {
10994 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
10997 el = arm_current_el(env);
10998 if (el < 2 && arm_is_secure_below_el3(env)) {
10999 return ARMMMUIdx_S1SE0 + el;
11000 } else {
11001 return ARMMMUIdx_S12NSE0 + el;
11005 int cpu_mmu_index(CPUARMState *env, bool ifetch)
11007 return arm_to_core_mmu_idx(arm_mmu_idx(env));
11010 #ifndef CONFIG_USER_ONLY
11011 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
11013 return stage_1_mmu_idx(arm_mmu_idx(env));
11015 #endif
11017 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11018 target_ulong *cs_base, uint32_t *pflags)
11020 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
11021 int current_el = arm_current_el(env);
11022 int fp_el = fp_exception_el(env, current_el);
11023 uint32_t flags = 0;
11025 if (is_a64(env)) {
11026 ARMCPU *cpu = env_archcpu(env);
11027 uint64_t sctlr;
11029 *pc = env->pc;
11030 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
11032 /* Get control bits for tagged addresses. */
11034 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11035 ARMVAParameters p0 = aa64_va_parameters_both(env, 0, stage1);
11036 int tbii, tbid;
11038 /* FIXME: ARMv8.1-VHE S2 translation regime. */
11039 if (regime_el(env, stage1) < 2) {
11040 ARMVAParameters p1 = aa64_va_parameters_both(env, -1, stage1);
11041 tbid = (p1.tbi << 1) | p0.tbi;
11042 tbii = tbid & ~((p1.tbid << 1) | p0.tbid);
11043 } else {
11044 tbid = p0.tbi;
11045 tbii = tbid & !p0.tbid;
11048 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
11049 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
11052 if (cpu_isar_feature(aa64_sve, cpu)) {
11053 int sve_el = sve_exception_el(env, current_el);
11054 uint32_t zcr_len;
11056 /* If SVE is disabled, but FP is enabled,
11057 * then the effective len is 0.
11059 if (sve_el != 0 && fp_el == 0) {
11060 zcr_len = 0;
11061 } else {
11062 zcr_len = sve_zcr_len_for_el(env, current_el);
11064 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
11065 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
11068 sctlr = arm_sctlr(env, current_el);
11070 if (cpu_isar_feature(aa64_pauth, cpu)) {
11072 * In order to save space in flags, we record only whether
11073 * pauth is "inactive", meaning all insns are implemented as
11074 * a nop, or "active" when some action must be performed.
11075 * The decision of which action to take is left to a helper.
11077 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11078 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
11082 if (cpu_isar_feature(aa64_bti, cpu)) {
11083 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
11084 if (sctlr & (current_el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11085 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
11087 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
11089 } else {
11090 *pc = env->regs[15];
11091 flags = FIELD_DP32(flags, TBFLAG_A32, THUMB, env->thumb);
11092 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN, env->vfp.vec_len);
11093 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE, env->vfp.vec_stride);
11094 flags = FIELD_DP32(flags, TBFLAG_A32, CONDEXEC, env->condexec_bits);
11095 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, arm_sctlr_b(env));
11096 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
11097 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)
11098 || arm_el_is_aa64(env, 1) || arm_feature(env, ARM_FEATURE_M)) {
11099 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
11101 /* Note that XSCALE_CPAR shares bits with VECSTRIDE */
11102 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11103 flags = FIELD_DP32(flags, TBFLAG_A32,
11104 XSCALE_CPAR, env->cp15.c15_cpar);
11108 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
11110 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11111 * states defined in the ARM ARM for software singlestep:
11112 * SS_ACTIVE PSTATE.SS State
11113 * 0 x Inactive (the TB flag for SS is always 0)
11114 * 1 0 Active-pending
11115 * 1 1 Active-not-pending
11117 if (arm_singlestep_active(env)) {
11118 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
11119 if (is_a64(env)) {
11120 if (env->pstate & PSTATE_SS) {
11121 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
11123 } else {
11124 if (env->uncached_cpsr & PSTATE_SS) {
11125 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
11129 if (arm_cpu_data_is_big_endian(env)) {
11130 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
11132 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
11134 if (arm_v7m_is_handler_mode(env)) {
11135 flags = FIELD_DP32(flags, TBFLAG_A32, HANDLER, 1);
11138 /* v8M always applies stack limit checks unless CCR.STKOFHFNMIGN is
11139 * suppressing them because the requested execution priority is less than 0.
11141 if (arm_feature(env, ARM_FEATURE_V8) &&
11142 arm_feature(env, ARM_FEATURE_M) &&
11143 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11144 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11145 flags = FIELD_DP32(flags, TBFLAG_A32, STACKCHECK, 1);
11148 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11149 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S) != env->v7m.secure) {
11150 flags = FIELD_DP32(flags, TBFLAG_A32, FPCCR_S_WRONG, 1);
11153 if (arm_feature(env, ARM_FEATURE_M) &&
11154 (env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11155 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11156 (env->v7m.secure &&
11157 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11159 * ASPEN is set, but FPCA/SFPA indicate that there is no active
11160 * FP context; we must create a new FP context before executing
11161 * any FP insn.
11163 flags = FIELD_DP32(flags, TBFLAG_A32, NEW_FP_CTXT_NEEDED, 1);
11166 if (arm_feature(env, ARM_FEATURE_M)) {
11167 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11169 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11170 flags = FIELD_DP32(flags, TBFLAG_A32, LSPACT, 1);
11174 if (!arm_feature(env, ARM_FEATURE_M)) {
11175 int target_el = arm_debug_target_el(env);
11177 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL, target_el);
11180 *pflags = flags;
11181 *cs_base = 0;
11184 #ifdef TARGET_AARCH64
11186 * The manual says that when SVE is enabled and VQ is widened the
11187 * implementation is allowed to zero the previously inaccessible
11188 * portion of the registers. The corollary to that is that when
11189 * SVE is enabled and VQ is narrowed we are also allowed to zero
11190 * the now inaccessible portion of the registers.
11192 * The intent of this is that no predicate bit beyond VQ is ever set.
11193 * Which means that some operations on predicate registers themselves
11194 * may operate on full uint64_t or even unrolled across the maximum
11195 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
11196 * may well be cheaper than conditionals to restrict the operation
11197 * to the relevant portion of a uint16_t[16].
11199 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11201 int i, j;
11202 uint64_t pmask;
11204 assert(vq >= 1 && vq <= ARM_MAX_VQ);
11205 assert(vq <= env_archcpu(env)->sve_max_vq);
11207 /* Zap the high bits of the zregs. */
11208 for (i = 0; i < 32; i++) {
11209 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11212 /* Zap the high bits of the pregs and ffr. */
11213 pmask = 0;
11214 if (vq & 3) {
11215 pmask = ~(-1ULL << (16 * (vq & 3)));
11217 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11218 for (i = 0; i < 17; ++i) {
11219 env->vfp.pregs[i].p[j] &= pmask;
11221 pmask = 0;
11226 * Notice a change in SVE vector size when changing EL.
11228 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11229 int new_el, bool el0_a64)
11231 ARMCPU *cpu = env_archcpu(env);
11232 int old_len, new_len;
11233 bool old_a64, new_a64;
11235 /* Nothing to do if no SVE. */
11236 if (!cpu_isar_feature(aa64_sve, cpu)) {
11237 return;
11240 /* Nothing to do if FP is disabled in either EL. */
11241 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11242 return;
11246 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11247 * at ELx, or not available because the EL is in AArch32 state, then
11248 * for all purposes other than a direct read, the ZCR_ELx.LEN field
11249 * has an effective value of 0".
11251 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11252 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11253 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
11254 * we already have the correct register contents when encountering the
11255 * vq0->vq0 transition between EL0->EL1.
11257 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11258 old_len = (old_a64 && !sve_exception_el(env, old_el)
11259 ? sve_zcr_len_for_el(env, old_el) : 0);
11260 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11261 new_len = (new_a64 && !sve_exception_el(env, new_el)
11262 ? sve_zcr_len_for_el(env, new_el) : 0);
11264 /* When changing vector length, clear inaccessible state. */
11265 if (new_len < old_len) {
11266 aarch64_sve_narrow_vq(env, new_len + 1);
11269 #endif