4 * This code is licensed under the GNU GPL v2 or later.
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include "qemu/osdep.h"
10 #include "qemu/units.h"
11 #include "target/arm/idau.h"
14 #include "internals.h"
15 #include "exec/gdbstub.h"
16 #include "exec/helper-proto.h"
17 #include "qemu/host-utils.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/bitops.h"
20 #include "qemu/crc32c.h"
21 #include "qemu/qemu-print.h"
22 #include "exec/exec-all.h"
23 #include <zlib.h> /* For crc32 */
25 #include "hw/semihosting/semihost.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/kvm.h"
28 #include "qemu/range.h"
29 #include "qapi/qapi-commands-machine-target.h"
30 #include "qapi/error.h"
31 #include "qemu/guest-random.h"
34 #include "exec/cpu_ldst.h"
37 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
39 #ifndef CONFIG_USER_ONLY
41 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
42 MMUAccessType access_type, ARMMMUIdx mmu_idx,
43 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
44 target_ulong *page_size_ptr,
45 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs);
48 static void switch_mode(CPUARMState *env, int mode);
50 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
54 /* VFP data registers are always little-endian. */
55 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
57 stq_le_p(buf, *aa32_vfp_dreg(env, reg));
60 if (arm_feature(env, ARM_FEATURE_NEON)) {
61 /* Aliases for Q regs. */
64 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
66 stq_le_p(buf + 8, q[1]);
70 switch (reg - nregs) {
71 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
72 case 1: stl_p(buf, vfp_get_fpscr(env)); return 4;
73 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
78 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
82 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
84 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
87 if (arm_feature(env, ARM_FEATURE_NEON)) {
90 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
92 q[1] = ldq_le_p(buf + 8);
96 switch (reg - nregs) {
97 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
98 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4;
99 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
104 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
108 /* 128 bit FP register */
110 uint64_t *q = aa64_vfp_qreg(env, reg);
112 stq_le_p(buf + 8, q[1]);
117 stl_p(buf, vfp_get_fpsr(env));
121 stl_p(buf, vfp_get_fpcr(env));
128 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
132 /* 128 bit FP register */
134 uint64_t *q = aa64_vfp_qreg(env, reg);
135 q[0] = ldq_le_p(buf);
136 q[1] = ldq_le_p(buf + 8);
141 vfp_set_fpsr(env, ldl_p(buf));
145 vfp_set_fpcr(env, ldl_p(buf));
152 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
154 assert(ri->fieldoffset);
155 if (cpreg_field_is_64bit(ri)) {
156 return CPREG_FIELD64(env, ri);
158 return CPREG_FIELD32(env, ri);
162 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
165 assert(ri->fieldoffset);
166 if (cpreg_field_is_64bit(ri)) {
167 CPREG_FIELD64(env, ri) = value;
169 CPREG_FIELD32(env, ri) = value;
173 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
175 return (char *)env + ri->fieldoffset;
178 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
180 /* Raw read of a coprocessor register (as needed for migration, etc). */
181 if (ri->type & ARM_CP_CONST) {
182 return ri->resetvalue;
183 } else if (ri->raw_readfn) {
184 return ri->raw_readfn(env, ri);
185 } else if (ri->readfn) {
186 return ri->readfn(env, ri);
188 return raw_read(env, ri);
192 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
195 /* Raw write of a coprocessor register (as needed for migration, etc).
196 * Note that constant registers are treated as write-ignored; the
197 * caller should check for success by whether a readback gives the
200 if (ri->type & ARM_CP_CONST) {
202 } else if (ri->raw_writefn) {
203 ri->raw_writefn(env, ri, v);
204 } else if (ri->writefn) {
205 ri->writefn(env, ri, v);
207 raw_write(env, ri, v);
211 static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg)
213 ARMCPU *cpu = env_archcpu(env);
214 const ARMCPRegInfo *ri;
217 key = cpu->dyn_xml.cpregs_keys[reg];
218 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
220 if (cpreg_field_is_64bit(ri)) {
221 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
223 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
229 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
234 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
236 /* Return true if the regdef would cause an assertion if you called
237 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
238 * program bug for it not to have the NO_RAW flag).
239 * NB that returning false here doesn't necessarily mean that calling
240 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
241 * read/write access functions which are safe for raw use" from "has
242 * read/write access functions which have side effects but has forgotten
243 * to provide raw access functions".
244 * The tests here line up with the conditions in read/write_raw_cp_reg()
245 * and assertions in raw_read()/raw_write().
247 if ((ri->type & ARM_CP_CONST) ||
249 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
255 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
257 /* Write the coprocessor state from cpu->env to the (index,value) list. */
261 for (i = 0; i < cpu->cpreg_array_len; i++) {
262 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
263 const ARMCPRegInfo *ri;
266 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
271 if (ri->type & ARM_CP_NO_RAW) {
275 newval = read_raw_cp_reg(&cpu->env, ri);
278 * Only sync if the previous list->cpustate sync succeeded.
279 * Rather than tracking the success/failure state for every
280 * item in the list, we just recheck "does the raw write we must
281 * have made in write_list_to_cpustate() read back OK" here.
283 uint64_t oldval = cpu->cpreg_values[i];
285 if (oldval == newval) {
289 write_raw_cp_reg(&cpu->env, ri, oldval);
290 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
294 write_raw_cp_reg(&cpu->env, ri, newval);
296 cpu->cpreg_values[i] = newval;
301 bool write_list_to_cpustate(ARMCPU *cpu)
306 for (i = 0; i < cpu->cpreg_array_len; i++) {
307 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
308 uint64_t v = cpu->cpreg_values[i];
309 const ARMCPRegInfo *ri;
311 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
316 if (ri->type & ARM_CP_NO_RAW) {
319 /* Write value and confirm it reads back as written
320 * (to catch read-only registers and partially read-only
321 * registers where the incoming migration value doesn't match)
323 write_raw_cp_reg(&cpu->env, ri, v);
324 if (read_raw_cp_reg(&cpu->env, ri) != v) {
331 static void add_cpreg_to_list(gpointer key, gpointer opaque)
333 ARMCPU *cpu = opaque;
335 const ARMCPRegInfo *ri;
337 regidx = *(uint32_t *)key;
338 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
340 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
341 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
342 /* The value array need not be initialized at this point */
343 cpu->cpreg_array_len++;
347 static void count_cpreg(gpointer key, gpointer opaque)
349 ARMCPU *cpu = opaque;
351 const ARMCPRegInfo *ri;
353 regidx = *(uint32_t *)key;
354 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
356 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
357 cpu->cpreg_array_len++;
361 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
363 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
364 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
375 void init_cpreg_list(ARMCPU *cpu)
377 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
378 * Note that we require cpreg_tuples[] to be sorted by key ID.
383 keys = g_hash_table_get_keys(cpu->cp_regs);
384 keys = g_list_sort(keys, cpreg_key_compare);
386 cpu->cpreg_array_len = 0;
388 g_list_foreach(keys, count_cpreg, cpu);
390 arraylen = cpu->cpreg_array_len;
391 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
392 cpu->cpreg_values = g_new(uint64_t, arraylen);
393 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
394 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
395 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
396 cpu->cpreg_array_len = 0;
398 g_list_foreach(keys, add_cpreg_to_list, cpu);
400 assert(cpu->cpreg_array_len == arraylen);
406 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
407 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
409 * access_el3_aa32ns: Used to check AArch32 register views.
410 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
412 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
413 const ARMCPRegInfo *ri,
416 bool secure = arm_is_secure_below_el3(env);
418 assert(!arm_el_is_aa64(env, 3));
420 return CP_ACCESS_TRAP_UNCATEGORIZED;
425 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
426 const ARMCPRegInfo *ri,
429 if (!arm_el_is_aa64(env, 3)) {
430 return access_el3_aa32ns(env, ri, isread);
435 /* Some secure-only AArch32 registers trap to EL3 if used from
436 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
437 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
438 * We assume that the .access field is set to PL1_RW.
440 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
441 const ARMCPRegInfo *ri,
444 if (arm_current_el(env) == 3) {
447 if (arm_is_secure_below_el3(env)) {
448 return CP_ACCESS_TRAP_EL3;
450 /* This will be EL1 NS and EL2 NS, which just UNDEF */
451 return CP_ACCESS_TRAP_UNCATEGORIZED;
454 /* Check for traps to "powerdown debug" registers, which are controlled
457 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
460 int el = arm_current_el(env);
461 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) ||
462 (env->cp15.mdcr_el2 & MDCR_TDE) ||
463 (arm_hcr_el2_eff(env) & HCR_TGE);
465 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) {
466 return CP_ACCESS_TRAP_EL2;
468 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
469 return CP_ACCESS_TRAP_EL3;
474 /* Check for traps to "debug ROM" registers, which are controlled
475 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
477 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
480 int el = arm_current_el(env);
481 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) ||
482 (env->cp15.mdcr_el2 & MDCR_TDE) ||
483 (arm_hcr_el2_eff(env) & HCR_TGE);
485 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) {
486 return CP_ACCESS_TRAP_EL2;
488 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
489 return CP_ACCESS_TRAP_EL3;
494 /* Check for traps to general debug registers, which are controlled
495 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
497 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
500 int el = arm_current_el(env);
501 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) ||
502 (env->cp15.mdcr_el2 & MDCR_TDE) ||
503 (arm_hcr_el2_eff(env) & HCR_TGE);
505 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) {
506 return CP_ACCESS_TRAP_EL2;
508 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
509 return CP_ACCESS_TRAP_EL3;
514 /* Check for traps to performance monitor registers, which are controlled
515 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
517 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
520 int el = arm_current_el(env);
522 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
523 && !arm_is_secure_below_el3(env)) {
524 return CP_ACCESS_TRAP_EL2;
526 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
527 return CP_ACCESS_TRAP_EL3;
532 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
534 ARMCPU *cpu = env_archcpu(env);
536 raw_write(env, ri, value);
537 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
540 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
542 ARMCPU *cpu = env_archcpu(env);
544 if (raw_read(env, ri) != value) {
545 /* Unlike real hardware the qemu TLB uses virtual addresses,
546 * not modified virtual addresses, so this causes a TLB flush.
549 raw_write(env, ri, value);
553 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
556 ARMCPU *cpu = env_archcpu(env);
558 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
559 && !extended_addresses_enabled(env)) {
560 /* For VMSA (when not using the LPAE long descriptor page table
561 * format) this register includes the ASID, so do a TLB flush.
562 * For PMSA it is purely a process ID and no action is needed.
566 raw_write(env, ri, value);
569 /* IS variants of TLB operations must affect all cores */
570 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
573 CPUState *cs = env_cpu(env);
575 tlb_flush_all_cpus_synced(cs);
578 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
581 CPUState *cs = env_cpu(env);
583 tlb_flush_all_cpus_synced(cs);
586 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
589 CPUState *cs = env_cpu(env);
591 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
594 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
597 CPUState *cs = env_cpu(env);
599 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
603 * Non-IS variants of TLB operations are upgraded to
604 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to
605 * force broadcast of these operations.
607 static bool tlb_force_broadcast(CPUARMState *env)
609 return (env->cp15.hcr_el2 & HCR_FB) &&
610 arm_current_el(env) == 1 && arm_is_secure_below_el3(env);
613 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
616 /* Invalidate all (TLBIALL) */
617 CPUState *cs = env_cpu(env);
619 if (tlb_force_broadcast(env)) {
620 tlb_flush_all_cpus_synced(cs);
626 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
629 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
630 CPUState *cs = env_cpu(env);
632 value &= TARGET_PAGE_MASK;
633 if (tlb_force_broadcast(env)) {
634 tlb_flush_page_all_cpus_synced(cs, value);
636 tlb_flush_page(cs, value);
640 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
643 /* Invalidate by ASID (TLBIASID) */
644 CPUState *cs = env_cpu(env);
646 if (tlb_force_broadcast(env)) {
647 tlb_flush_all_cpus_synced(cs);
653 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
656 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
657 CPUState *cs = env_cpu(env);
659 value &= TARGET_PAGE_MASK;
660 if (tlb_force_broadcast(env)) {
661 tlb_flush_page_all_cpus_synced(cs, value);
663 tlb_flush_page(cs, value);
667 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
670 CPUState *cs = env_cpu(env);
672 tlb_flush_by_mmuidx(cs,
675 ARMMMUIdxBit_Stage2);
678 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
681 CPUState *cs = env_cpu(env);
683 tlb_flush_by_mmuidx_all_cpus_synced(cs,
686 ARMMMUIdxBit_Stage2);
689 static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri,
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);
701 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
705 pageaddr = sextract64(value << 12, 0, 40);
707 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
710 static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
713 CPUState *cs = env_cpu(env);
716 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
720 pageaddr = sextract64(value << 12, 0, 40);
722 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
723 ARMMMUIdxBit_Stage2);
726 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
729 CPUState *cs = env_cpu(env);
731 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
734 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
737 CPUState *cs = env_cpu(env);
739 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
742 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
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_E2);
751 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
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,
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.
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, },
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 */
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 },
825 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
826 /* Not all pre-v6 cores implemented this WFI, so this is slightly
829 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
830 .access = PL1_W, .type = ARM_CP_WFI },
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),
847 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
848 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
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,
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 },
883 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
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 */
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. */
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);
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,
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;
970 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
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;
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,
998 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
999 offsetof(CPUARMState, cp15.ifar_ns) },
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 },
1013 /* Definitions for the PMU registers */
1014 #define PMCRN_MASK 0xf800
1015 #define PMCRN_SHIFT 11
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);
1069 static bool event_always_supported(CPUARMState *env)
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
1083 static int64_t swinc_ns_per(uint64_t ignored)
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);
1098 return cpu_get_host_ticks();
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);
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,
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)
1165 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1168 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1169 supported_event_map[i] = UNSUPPORTED_EVENT;
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;
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) {
1200 return supported_event_map[number] != UNSUPPORTED_EVENT;
1203 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
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,
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
1235 return CP_ACCESS_OK;
1238 return pmreg_access(env, ri, isread);
1241 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1242 const ARMCPRegInfo *ri,
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
1250 return CP_ACCESS_OK;
1253 return pmreg_access(env, ri, isread);
1256 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1257 const ARMCPRegInfo *ri,
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,
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
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)
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)) {
1301 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1302 (counter < hpmn || counter == 31)) {
1303 e = env->cp15.c9_pmcr & PMCRE;
1305 e = env->cp15.mdcr_el2 & MDCR_HPME;
1307 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1310 if (el == 2 && (counter < hpmn || counter == 31)) {
1311 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD;
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;
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);
1339 filtered = secure ? u : u != nsu;
1340 } else if (el == 1) {
1341 filtered = secure ? p : p != nsk;
1342 } else if (el == 2) {
1348 if (counter != 31) {
1350 * If not checking PMCCNTR, ensure the counter is setup to an event we
1353 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1354 if (!event_supported(event)) {
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 */
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
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) +
1419 ARMCPU *cpu = env_archcpu(env);
1420 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
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 */
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;
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) +
1468 ARMCPU *cpu = env_archcpu(env);
1469 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1473 env->cp15.c14_pmevcntr_delta[counter] -=
1474 env->cp15.c14_pmevcntr[counter];
1478 void pmu_op_start(CPUARMState *env)
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)
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,
1525 if (value & PMCRC) {
1526 /* The counter has been reset */
1527 env->cp15.c15_ccnt = 0;
1530 if (value & PMCRP) {
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);
1544 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
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)
1578 pmccntr_op_start(env);
1579 ret = env->cp15.c15_ccnt;
1580 pmccntr_op_finish(env);
1584 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
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
1592 env->cp15.c9_pmselr = value & 0x1f;
1595 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
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,
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,
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,
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,
1638 value &= pmu_counter_mask(env);
1639 env->cp15.c9_pmcnten |= value;
1642 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1645 value &= pmu_counter_mask(env);
1646 env->cp15.c9_pmcnten &= ~value;
1649 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
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,
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
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) {
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];
1709 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1710 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1716 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
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,
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,
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,
1780 if (counter < pmu_num_counters(env)) {
1782 pmevcntr_op_start(env, counter);
1783 ret = env->cp15.c14_pmevcntr[counter];
1784 pmevcntr_op_finish(env, counter);
1787 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1788 * are CONSTRAINED UNPREDICTABLE. */
1793 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
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,
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,
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,
1836 if (arm_feature(env, ARM_FEATURE_V8)) {
1837 env->cp15.c9_pmuserenr = value & 0xf;
1839 env->cp15.c9_pmuserenr = value & 1;
1843 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
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,
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,
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;
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 CPAccessResult access_aa64_tid2(CPUARMState *env,
1912 const ARMCPRegInfo *ri,
1915 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1916 return CP_ACCESS_TRAP_EL2;
1919 return CP_ACCESS_OK;
1922 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1924 ARMCPU *cpu = env_archcpu(env);
1926 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1929 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1930 ri->secure & ARM_CP_SECSTATE_S);
1932 return cpu->ccsidr[index];
1935 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1938 raw_write(env, ri, value & 0xf);
1941 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1943 CPUState *cs = env_cpu(env);
1944 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1946 bool allow_virt = (arm_current_el(env) == 1 &&
1947 (!arm_is_secure_below_el3(env) ||
1948 (env->cp15.scr_el3 & SCR_EEL2)));
1950 if (allow_virt && (hcr_el2 & HCR_IMO)) {
1951 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1955 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1960 if (allow_virt && (hcr_el2 & HCR_FMO)) {
1961 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1965 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1970 /* External aborts are not possible in QEMU so A bit is always clear */
1974 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1977 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1978 return CP_ACCESS_TRAP_EL2;
1981 return CP_ACCESS_OK;
1984 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1987 if (arm_feature(env, ARM_FEATURE_V8)) {
1988 return access_aa64_tid1(env, ri, isread);
1991 return CP_ACCESS_OK;
1994 static const ARMCPRegInfo v7_cp_reginfo[] = {
1995 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1996 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1997 .access = PL1_W, .type = ARM_CP_NOP },
1998 /* Performance monitors are implementation defined in v7,
1999 * but with an ARM recommended set of registers, which we
2002 * Performance registers fall into three categories:
2003 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2004 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2005 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2006 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2007 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2009 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2010 .access = PL0_RW, .type = ARM_CP_ALIAS,
2011 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2012 .writefn = pmcntenset_write,
2013 .accessfn = pmreg_access,
2014 .raw_writefn = raw_write },
2015 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
2016 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2017 .access = PL0_RW, .accessfn = pmreg_access,
2018 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2019 .writefn = pmcntenset_write, .raw_writefn = raw_write },
2020 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2022 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2023 .accessfn = pmreg_access,
2024 .writefn = pmcntenclr_write,
2025 .type = ARM_CP_ALIAS },
2026 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2027 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2028 .access = PL0_RW, .accessfn = pmreg_access,
2029 .type = ARM_CP_ALIAS,
2030 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2031 .writefn = pmcntenclr_write },
2032 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2033 .access = PL0_RW, .type = ARM_CP_IO,
2034 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2035 .accessfn = pmreg_access,
2036 .writefn = pmovsr_write,
2037 .raw_writefn = raw_write },
2038 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2039 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2040 .access = PL0_RW, .accessfn = pmreg_access,
2041 .type = ARM_CP_ALIAS | ARM_CP_IO,
2042 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2043 .writefn = pmovsr_write,
2044 .raw_writefn = raw_write },
2045 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2046 .access = PL0_W, .accessfn = pmreg_access_swinc,
2047 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2048 .writefn = pmswinc_write },
2049 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2050 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2051 .access = PL0_W, .accessfn = pmreg_access_swinc,
2052 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2053 .writefn = pmswinc_write },
2054 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2055 .access = PL0_RW, .type = ARM_CP_ALIAS,
2056 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2057 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2058 .raw_writefn = raw_write},
2059 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2060 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2061 .access = PL0_RW, .accessfn = pmreg_access_selr,
2062 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2063 .writefn = pmselr_write, .raw_writefn = raw_write, },
2064 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2065 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2066 .readfn = pmccntr_read, .writefn = pmccntr_write32,
2067 .accessfn = pmreg_access_ccntr },
2068 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2069 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2070 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2072 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2073 .readfn = pmccntr_read, .writefn = pmccntr_write,
2074 .raw_readfn = raw_read, .raw_writefn = raw_write, },
2075 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2076 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2077 .access = PL0_RW, .accessfn = pmreg_access,
2078 .type = ARM_CP_ALIAS | ARM_CP_IO,
2080 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2081 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2082 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2083 .access = PL0_RW, .accessfn = pmreg_access,
2085 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2087 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2088 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2089 .accessfn = pmreg_access,
2090 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2091 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2092 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2093 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2094 .accessfn = pmreg_access,
2095 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2096 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2097 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2098 .accessfn = pmreg_access_xevcntr,
2099 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2100 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2101 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2102 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2103 .accessfn = pmreg_access_xevcntr,
2104 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2105 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2106 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2107 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2109 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2110 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2111 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2112 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2113 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2115 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2116 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2117 .access = PL1_RW, .accessfn = access_tpm,
2118 .type = ARM_CP_ALIAS | ARM_CP_IO,
2119 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2121 .writefn = pmintenset_write, .raw_writefn = raw_write },
2122 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2123 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2124 .access = PL1_RW, .accessfn = access_tpm,
2126 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2127 .writefn = pmintenset_write, .raw_writefn = raw_write,
2128 .resetvalue = 0x0 },
2129 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2130 .access = PL1_RW, .accessfn = access_tpm,
2131 .type = ARM_CP_ALIAS | ARM_CP_IO,
2132 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2133 .writefn = pmintenclr_write, },
2134 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2135 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2136 .access = PL1_RW, .accessfn = access_tpm,
2137 .type = ARM_CP_ALIAS | ARM_CP_IO,
2138 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2139 .writefn = pmintenclr_write },
2140 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2141 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2143 .accessfn = access_aa64_tid2,
2144 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2145 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2146 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2148 .accessfn = access_aa64_tid2,
2149 .writefn = csselr_write, .resetvalue = 0,
2150 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2151 offsetof(CPUARMState, cp15.csselr_ns) } },
2152 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2153 * just RAZ for all cores:
2155 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2156 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2157 .access = PL1_R, .type = ARM_CP_CONST,
2158 .accessfn = access_aa64_tid1,
2160 /* Auxiliary fault status registers: these also are IMPDEF, and we
2161 * choose to RAZ/WI for all cores.
2163 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2164 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2165 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2166 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2167 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2168 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2169 /* MAIR can just read-as-written because we don't implement caches
2170 * and so don't need to care about memory attributes.
2172 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2173 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2174 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2176 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2177 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2178 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2180 /* For non-long-descriptor page tables these are PRRR and NMRR;
2181 * regardless they still act as reads-as-written for QEMU.
2183 /* MAIR0/1 are defined separately from their 64-bit counterpart which
2184 * allows them to assign the correct fieldoffset based on the endianness
2185 * handled in the field definitions.
2187 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2188 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
2189 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2190 offsetof(CPUARMState, cp15.mair0_ns) },
2191 .resetfn = arm_cp_reset_ignore },
2192 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2193 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
2194 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2195 offsetof(CPUARMState, cp15.mair1_ns) },
2196 .resetfn = arm_cp_reset_ignore },
2197 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2198 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2199 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2200 /* 32 bit ITLB invalidates */
2201 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2202 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2203 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2204 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2205 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2206 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2207 /* 32 bit DTLB invalidates */
2208 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2209 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2210 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2211 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2212 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2213 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2214 /* 32 bit TLB invalidates */
2215 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2216 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
2217 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2218 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2219 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2220 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
2221 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2222 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
2226 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2227 /* 32 bit TLB invalidates, Inner Shareable */
2228 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2229 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
2230 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2231 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
2232 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2233 .type = ARM_CP_NO_RAW, .access = PL1_W,
2234 .writefn = tlbiasid_is_write },
2235 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2236 .type = ARM_CP_NO_RAW, .access = PL1_W,
2237 .writefn = tlbimvaa_is_write },
2241 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2242 /* PMOVSSET is not implemented in v7 before v7ve */
2243 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2244 .access = PL0_RW, .accessfn = pmreg_access,
2245 .type = ARM_CP_ALIAS | ARM_CP_IO,
2246 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2247 .writefn = pmovsset_write,
2248 .raw_writefn = raw_write },
2249 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2250 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2251 .access = PL0_RW, .accessfn = pmreg_access,
2252 .type = ARM_CP_ALIAS | ARM_CP_IO,
2253 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2254 .writefn = pmovsset_write,
2255 .raw_writefn = raw_write },
2259 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2266 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2269 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2270 return CP_ACCESS_TRAP;
2272 return CP_ACCESS_OK;
2275 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2276 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2277 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2279 .writefn = teecr_write },
2280 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2281 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2282 .accessfn = teehbr_access, .resetvalue = 0 },
2286 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2287 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2288 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2290 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2291 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2293 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2294 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2295 .resetfn = arm_cp_reset_ignore },
2296 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2297 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2298 .access = PL0_R|PL1_W,
2299 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2301 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2302 .access = PL0_R|PL1_W,
2303 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2304 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2305 .resetfn = arm_cp_reset_ignore },
2306 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2307 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2309 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2310 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2312 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2313 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2318 #ifndef CONFIG_USER_ONLY
2320 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2323 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2324 * Writable only at the highest implemented exception level.
2326 int el = arm_current_el(env);
2332 hcr = arm_hcr_el2_eff(env);
2333 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2334 cntkctl = env->cp15.cnthctl_el2;
2336 cntkctl = env->cp15.c14_cntkctl;
2338 if (!extract32(cntkctl, 0, 2)) {
2339 return CP_ACCESS_TRAP;
2343 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2344 arm_is_secure_below_el3(env)) {
2345 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2346 return CP_ACCESS_TRAP_UNCATEGORIZED;
2354 if (!isread && el < arm_highest_el(env)) {
2355 return CP_ACCESS_TRAP_UNCATEGORIZED;
2358 return CP_ACCESS_OK;
2361 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2364 unsigned int cur_el = arm_current_el(env);
2365 bool secure = arm_is_secure(env);
2366 uint64_t hcr = arm_hcr_el2_eff(env);
2370 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2371 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2372 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2373 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2376 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2377 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2378 return CP_ACCESS_TRAP;
2381 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2382 if (hcr & HCR_E2H) {
2383 if (timeridx == GTIMER_PHYS &&
2384 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2385 return CP_ACCESS_TRAP_EL2;
2388 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2389 if (arm_feature(env, ARM_FEATURE_EL2) &&
2390 timeridx == GTIMER_PHYS && !secure &&
2391 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2392 return CP_ACCESS_TRAP_EL2;
2398 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2399 if (arm_feature(env, ARM_FEATURE_EL2) &&
2400 timeridx == GTIMER_PHYS && !secure &&
2402 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2403 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2404 return CP_ACCESS_TRAP_EL2;
2408 return CP_ACCESS_OK;
2411 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2414 unsigned int cur_el = arm_current_el(env);
2415 bool secure = arm_is_secure(env);
2416 uint64_t hcr = arm_hcr_el2_eff(env);
2420 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2421 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2422 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2423 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2427 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2428 * EL0 if EL0[PV]TEN is zero.
2430 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2431 return CP_ACCESS_TRAP;
2436 if (arm_feature(env, ARM_FEATURE_EL2) &&
2437 timeridx == GTIMER_PHYS && !secure) {
2438 if (hcr & HCR_E2H) {
2439 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2440 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2441 return CP_ACCESS_TRAP_EL2;
2444 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2445 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2446 return CP_ACCESS_TRAP_EL2;
2452 return CP_ACCESS_OK;
2455 static CPAccessResult gt_pct_access(CPUARMState *env,
2456 const ARMCPRegInfo *ri,
2459 return gt_counter_access(env, GTIMER_PHYS, isread);
2462 static CPAccessResult gt_vct_access(CPUARMState *env,
2463 const ARMCPRegInfo *ri,
2466 return gt_counter_access(env, GTIMER_VIRT, isread);
2469 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2472 return gt_timer_access(env, GTIMER_PHYS, isread);
2475 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2478 return gt_timer_access(env, GTIMER_VIRT, isread);
2481 static CPAccessResult gt_stimer_access(CPUARMState *env,
2482 const ARMCPRegInfo *ri,
2485 /* The AArch64 register view of the secure physical timer is
2486 * always accessible from EL3, and configurably accessible from
2489 switch (arm_current_el(env)) {
2491 if (!arm_is_secure(env)) {
2492 return CP_ACCESS_TRAP;
2494 if (!(env->cp15.scr_el3 & SCR_ST)) {
2495 return CP_ACCESS_TRAP_EL3;
2497 return CP_ACCESS_OK;
2500 return CP_ACCESS_TRAP;
2502 return CP_ACCESS_OK;
2504 g_assert_not_reached();
2508 static uint64_t gt_get_countervalue(CPUARMState *env)
2510 ARMCPU *cpu = env_archcpu(env);
2512 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2515 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2517 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2520 /* Timer enabled: calculate and set current ISTATUS, irq, and
2521 * reset timer to when ISTATUS next has to change
2523 uint64_t offset = timeridx == GTIMER_VIRT ?
2524 cpu->env.cp15.cntvoff_el2 : 0;
2525 uint64_t count = gt_get_countervalue(&cpu->env);
2526 /* Note that this must be unsigned 64 bit arithmetic: */
2527 int istatus = count - offset >= gt->cval;
2531 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2533 irqstate = (istatus && !(gt->ctl & 2));
2534 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2537 /* Next transition is when count rolls back over to zero */
2538 nexttick = UINT64_MAX;
2540 /* Next transition is when we hit cval */
2541 nexttick = gt->cval + offset;
2543 /* Note that the desired next expiry time might be beyond the
2544 * signed-64-bit range of a QEMUTimer -- in this case we just
2545 * set the timer for as far in the future as possible. When the
2546 * timer expires we will reset the timer for any remaining period.
2548 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2549 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2551 timer_mod(cpu->gt_timer[timeridx], nexttick);
2553 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2555 /* Timer disabled: ISTATUS and timer output always clear */
2557 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2558 timer_del(cpu->gt_timer[timeridx]);
2559 trace_arm_gt_recalc_disabled(timeridx);
2563 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2566 ARMCPU *cpu = env_archcpu(env);
2568 timer_del(cpu->gt_timer[timeridx]);
2571 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2573 return gt_get_countervalue(env);
2576 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2580 switch (arm_current_el(env)) {
2582 hcr = arm_hcr_el2_eff(env);
2583 if (hcr & HCR_E2H) {
2588 hcr = arm_hcr_el2_eff(env);
2589 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2595 return env->cp15.cntvoff_el2;
2598 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2600 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2603 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2607 trace_arm_gt_cval_write(timeridx, value);
2608 env->cp15.c14_timer[timeridx].cval = value;
2609 gt_recalc_timer(env_archcpu(env), timeridx);
2612 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2615 uint64_t offset = 0;
2619 case GTIMER_HYPVIRT:
2620 offset = gt_virt_cnt_offset(env);
2624 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2625 (gt_get_countervalue(env) - offset));
2628 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2632 uint64_t offset = 0;
2636 case GTIMER_HYPVIRT:
2637 offset = gt_virt_cnt_offset(env);
2641 trace_arm_gt_tval_write(timeridx, value);
2642 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2643 sextract64(value, 0, 32);
2644 gt_recalc_timer(env_archcpu(env), timeridx);
2647 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2651 ARMCPU *cpu = env_archcpu(env);
2652 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2654 trace_arm_gt_ctl_write(timeridx, value);
2655 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2656 if ((oldval ^ value) & 1) {
2657 /* Enable toggled */
2658 gt_recalc_timer(cpu, timeridx);
2659 } else if ((oldval ^ value) & 2) {
2660 /* IMASK toggled: don't need to recalculate,
2661 * just set the interrupt line based on ISTATUS
2663 int irqstate = (oldval & 4) && !(value & 2);
2665 trace_arm_gt_imask_toggle(timeridx, irqstate);
2666 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2670 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2672 gt_timer_reset(env, ri, GTIMER_PHYS);
2675 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2678 gt_cval_write(env, ri, GTIMER_PHYS, value);
2681 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2683 return gt_tval_read(env, ri, GTIMER_PHYS);
2686 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2689 gt_tval_write(env, ri, GTIMER_PHYS, value);
2692 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2695 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2698 static int gt_phys_redir_timeridx(CPUARMState *env)
2700 switch (arm_mmu_idx(env)) {
2701 case ARMMMUIdx_E20_0:
2702 case ARMMMUIdx_E20_2:
2709 static int gt_virt_redir_timeridx(CPUARMState *env)
2711 switch (arm_mmu_idx(env)) {
2712 case ARMMMUIdx_E20_0:
2713 case ARMMMUIdx_E20_2:
2714 return GTIMER_HYPVIRT;
2720 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2721 const ARMCPRegInfo *ri)
2723 int timeridx = gt_phys_redir_timeridx(env);
2724 return env->cp15.c14_timer[timeridx].cval;
2727 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2730 int timeridx = gt_phys_redir_timeridx(env);
2731 gt_cval_write(env, ri, timeridx, value);
2734 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2735 const ARMCPRegInfo *ri)
2737 int timeridx = gt_phys_redir_timeridx(env);
2738 return gt_tval_read(env, ri, timeridx);
2741 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2744 int timeridx = gt_phys_redir_timeridx(env);
2745 gt_tval_write(env, ri, timeridx, value);
2748 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2749 const ARMCPRegInfo *ri)
2751 int timeridx = gt_phys_redir_timeridx(env);
2752 return env->cp15.c14_timer[timeridx].ctl;
2755 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2758 int timeridx = gt_phys_redir_timeridx(env);
2759 gt_ctl_write(env, ri, timeridx, value);
2762 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2764 gt_timer_reset(env, ri, GTIMER_VIRT);
2767 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2770 gt_cval_write(env, ri, GTIMER_VIRT, value);
2773 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2775 return gt_tval_read(env, ri, GTIMER_VIRT);
2778 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2781 gt_tval_write(env, ri, GTIMER_VIRT, value);
2784 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2787 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2790 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2793 ARMCPU *cpu = env_archcpu(env);
2795 trace_arm_gt_cntvoff_write(value);
2796 raw_write(env, ri, value);
2797 gt_recalc_timer(cpu, GTIMER_VIRT);
2800 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2801 const ARMCPRegInfo *ri)
2803 int timeridx = gt_virt_redir_timeridx(env);
2804 return env->cp15.c14_timer[timeridx].cval;
2807 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2810 int timeridx = gt_virt_redir_timeridx(env);
2811 gt_cval_write(env, ri, timeridx, value);
2814 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2815 const ARMCPRegInfo *ri)
2817 int timeridx = gt_virt_redir_timeridx(env);
2818 return gt_tval_read(env, ri, timeridx);
2821 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2824 int timeridx = gt_virt_redir_timeridx(env);
2825 gt_tval_write(env, ri, timeridx, value);
2828 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2829 const ARMCPRegInfo *ri)
2831 int timeridx = gt_virt_redir_timeridx(env);
2832 return env->cp15.c14_timer[timeridx].ctl;
2835 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2838 int timeridx = gt_virt_redir_timeridx(env);
2839 gt_ctl_write(env, ri, timeridx, value);
2842 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2844 gt_timer_reset(env, ri, GTIMER_HYP);
2847 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2850 gt_cval_write(env, ri, GTIMER_HYP, value);
2853 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2855 return gt_tval_read(env, ri, GTIMER_HYP);
2858 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2861 gt_tval_write(env, ri, GTIMER_HYP, value);
2864 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2867 gt_ctl_write(env, ri, GTIMER_HYP, value);
2870 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2872 gt_timer_reset(env, ri, GTIMER_SEC);
2875 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2878 gt_cval_write(env, ri, GTIMER_SEC, value);
2881 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2883 return gt_tval_read(env, ri, GTIMER_SEC);
2886 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2889 gt_tval_write(env, ri, GTIMER_SEC, value);
2892 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2895 gt_ctl_write(env, ri, GTIMER_SEC, value);
2898 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2900 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2903 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2906 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2909 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2911 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2914 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2917 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2920 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2923 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2926 void arm_gt_ptimer_cb(void *opaque)
2928 ARMCPU *cpu = opaque;
2930 gt_recalc_timer(cpu, GTIMER_PHYS);
2933 void arm_gt_vtimer_cb(void *opaque)
2935 ARMCPU *cpu = opaque;
2937 gt_recalc_timer(cpu, GTIMER_VIRT);
2940 void arm_gt_htimer_cb(void *opaque)
2942 ARMCPU *cpu = opaque;
2944 gt_recalc_timer(cpu, GTIMER_HYP);
2947 void arm_gt_stimer_cb(void *opaque)
2949 ARMCPU *cpu = opaque;
2951 gt_recalc_timer(cpu, GTIMER_SEC);
2954 void arm_gt_hvtimer_cb(void *opaque)
2956 ARMCPU *cpu = opaque;
2958 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2961 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2963 ARMCPU *cpu = env_archcpu(env);
2965 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2968 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2969 /* Note that CNTFRQ is purely reads-as-written for the benefit
2970 * of software; writing it doesn't actually change the timer frequency.
2971 * Our reset value matches the fixed frequency we implement the timer at.
2973 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2974 .type = ARM_CP_ALIAS,
2975 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2976 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2978 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2979 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2980 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2981 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2982 .resetfn = arm_gt_cntfrq_reset,
2984 /* overall control: mostly access permissions */
2985 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2986 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2988 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2991 /* per-timer control */
2992 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2993 .secure = ARM_CP_SECSTATE_NS,
2994 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2995 .accessfn = gt_ptimer_access,
2996 .fieldoffset = offsetoflow32(CPUARMState,
2997 cp15.c14_timer[GTIMER_PHYS].ctl),
2998 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2999 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3001 { .name = "CNTP_CTL_S",
3002 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3003 .secure = ARM_CP_SECSTATE_S,
3004 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3005 .accessfn = gt_ptimer_access,
3006 .fieldoffset = offsetoflow32(CPUARMState,
3007 cp15.c14_timer[GTIMER_SEC].ctl),
3008 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3010 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3011 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3012 .type = ARM_CP_IO, .access = PL0_RW,
3013 .accessfn = gt_ptimer_access,
3014 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3016 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3017 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3019 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3020 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3021 .accessfn = gt_vtimer_access,
3022 .fieldoffset = offsetoflow32(CPUARMState,
3023 cp15.c14_timer[GTIMER_VIRT].ctl),
3024 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3025 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3027 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3028 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3029 .type = ARM_CP_IO, .access = PL0_RW,
3030 .accessfn = gt_vtimer_access,
3031 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3033 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3034 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3036 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3037 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3038 .secure = ARM_CP_SECSTATE_NS,
3039 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3040 .accessfn = gt_ptimer_access,
3041 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3043 { .name = "CNTP_TVAL_S",
3044 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3045 .secure = ARM_CP_SECSTATE_S,
3046 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3047 .accessfn = gt_ptimer_access,
3048 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3050 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3051 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3052 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3053 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3054 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3056 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3057 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3058 .accessfn = gt_vtimer_access,
3059 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3061 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3062 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3063 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3064 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3065 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3067 /* The counter itself */
3068 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3069 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3070 .accessfn = gt_pct_access,
3071 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3073 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3074 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3075 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3076 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3078 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3079 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3080 .accessfn = gt_vct_access,
3081 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3083 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3084 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3085 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3086 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3088 /* Comparison value, indicating when the timer goes off */
3089 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3090 .secure = ARM_CP_SECSTATE_NS,
3092 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3093 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3094 .accessfn = gt_ptimer_access,
3095 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3096 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3098 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3099 .secure = ARM_CP_SECSTATE_S,
3101 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3102 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3103 .accessfn = gt_ptimer_access,
3104 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3106 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3107 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3110 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3111 .resetvalue = 0, .accessfn = gt_ptimer_access,
3112 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3113 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3115 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3117 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3118 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3119 .accessfn = gt_vtimer_access,
3120 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3121 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3123 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3124 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3127 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3128 .resetvalue = 0, .accessfn = gt_vtimer_access,
3129 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3130 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3132 /* Secure timer -- this is actually restricted to only EL3
3133 * and configurably Secure-EL1 via the accessfn.
3135 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3136 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3137 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3138 .accessfn = gt_stimer_access,
3139 .readfn = gt_sec_tval_read,
3140 .writefn = gt_sec_tval_write,
3141 .resetfn = gt_sec_timer_reset,
3143 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3144 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3145 .type = ARM_CP_IO, .access = PL1_RW,
3146 .accessfn = gt_stimer_access,
3147 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3149 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3151 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3152 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3153 .type = ARM_CP_IO, .access = PL1_RW,
3154 .accessfn = gt_stimer_access,
3155 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3156 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3161 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3164 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3165 return CP_ACCESS_TRAP;
3167 return CP_ACCESS_OK;
3172 /* In user-mode most of the generic timer registers are inaccessible
3173 * however modern kernels (4.12+) allow access to cntvct_el0
3176 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3178 ARMCPU *cpu = env_archcpu(env);
3180 /* Currently we have no support for QEMUTimer in linux-user so we
3181 * can't call gt_get_countervalue(env), instead we directly
3182 * call the lower level functions.
3184 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3187 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3188 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3189 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3190 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3191 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3192 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3194 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3195 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3196 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3197 .readfn = gt_virt_cnt_read,
3204 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3206 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3207 raw_write(env, ri, value);
3208 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3209 raw_write(env, ri, value & 0xfffff6ff);
3211 raw_write(env, ri, value & 0xfffff1ff);
3215 #ifndef CONFIG_USER_ONLY
3216 /* get_phys_addr() isn't present for user-mode-only targets */
3218 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3222 /* The ATS12NSO* operations must trap to EL3 if executed in
3223 * Secure EL1 (which can only happen if EL3 is AArch64).
3224 * They are simply UNDEF if executed from NS EL1.
3225 * They function normally from EL2 or EL3.
3227 if (arm_current_el(env) == 1) {
3228 if (arm_is_secure_below_el3(env)) {
3229 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3231 return CP_ACCESS_TRAP_UNCATEGORIZED;
3234 return CP_ACCESS_OK;
3237 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3238 MMUAccessType access_type, ARMMMUIdx mmu_idx)
3241 target_ulong page_size;
3245 bool format64 = false;
3246 MemTxAttrs attrs = {};
3247 ARMMMUFaultInfo fi = {};
3248 ARMCacheAttrs cacheattrs = {};
3250 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
3251 &prot, &page_size, &fi, &cacheattrs);
3255 * Some kinds of translation fault must cause exceptions rather
3256 * than being reported in the PAR.
3258 int current_el = arm_current_el(env);
3260 uint32_t syn, fsr, fsc;
3261 bool take_exc = false;
3263 if (fi.s1ptw && current_el == 1 && !arm_is_secure(env)
3264 && (mmu_idx == ARMMMUIdx_Stage1_E1 ||
3265 mmu_idx == ARMMMUIdx_Stage1_E0)) {
3267 * Synchronous stage 2 fault on an access made as part of the
3268 * translation table walk for AT S1E0* or AT S1E1* insn
3269 * executed from NS EL1. If this is a synchronous external abort
3270 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3271 * to EL3. Otherwise the fault is taken as an exception to EL2,
3272 * and HPFAR_EL2 holds the faulting IPA.
3274 if (fi.type == ARMFault_SyncExternalOnWalk &&
3275 (env->cp15.scr_el3 & SCR_EA)) {
3278 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3282 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3284 * Synchronous external aborts during a translation table walk
3285 * are taken as Data Abort exceptions.
3288 if (current_el == 3) {
3294 target_el = exception_target_el(env);
3300 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3301 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3302 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3303 fsr = arm_fi_to_lfsc(&fi);
3304 fsc = extract32(fsr, 0, 6);
3306 fsr = arm_fi_to_sfsc(&fi);
3310 * Report exception with ESR indicating a fault due to a
3311 * translation table walk for a cache maintenance instruction.
3313 syn = syn_data_abort_no_iss(current_el == target_el,
3314 fi.ea, 1, fi.s1ptw, 1, fsc);
3315 env->exception.vaddress = value;
3316 env->exception.fsr = fsr;
3317 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3323 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3326 * * TTBCR.EAE determines whether the result is returned using the
3327 * 32-bit or the 64-bit PAR format
3328 * * Instructions executed in Hyp mode always use the 64bit format
3330 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3331 * * The Non-secure TTBCR.EAE bit is set to 1
3332 * * The implementation includes EL2, and the value of HCR.VM is 1
3334 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3336 * ATS1Hx always uses the 64bit format.
3338 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3340 if (arm_feature(env, ARM_FEATURE_EL2)) {
3341 if (mmu_idx == ARMMMUIdx_E10_0 || mmu_idx == ARMMMUIdx_E10_1) {
3342 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3344 format64 |= arm_current_el(env) == 2;
3350 /* Create a 64-bit PAR */
3351 par64 = (1 << 11); /* LPAE bit always set */
3353 par64 |= phys_addr & ~0xfffULL;
3354 if (!attrs.secure) {
3355 par64 |= (1 << 9); /* NS */
3357 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3358 par64 |= cacheattrs.shareability << 7; /* SH */
3360 uint32_t fsr = arm_fi_to_lfsc(&fi);
3363 par64 |= (fsr & 0x3f) << 1; /* FS */
3365 par64 |= (1 << 9); /* S */
3368 par64 |= (1 << 8); /* PTW */
3372 /* fsr is a DFSR/IFSR value for the short descriptor
3373 * translation table format (with WnR always clear).
3374 * Convert it to a 32-bit PAR.
3377 /* We do not set any attribute bits in the PAR */
3378 if (page_size == (1 << 24)
3379 && arm_feature(env, ARM_FEATURE_V7)) {
3380 par64 = (phys_addr & 0xff000000) | (1 << 1);
3382 par64 = phys_addr & 0xfffff000;
3384 if (!attrs.secure) {
3385 par64 |= (1 << 9); /* NS */
3388 uint32_t fsr = arm_fi_to_sfsc(&fi);
3390 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3391 ((fsr & 0xf) << 1) | 1;
3397 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3399 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3402 int el = arm_current_el(env);
3403 bool secure = arm_is_secure_below_el3(env);
3405 switch (ri->opc2 & 6) {
3407 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
3410 mmu_idx = ARMMMUIdx_SE3;
3413 mmu_idx = ARMMMUIdx_Stage1_E1;
3416 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3419 g_assert_not_reached();
3423 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3426 mmu_idx = ARMMMUIdx_SE10_0;
3429 mmu_idx = ARMMMUIdx_Stage1_E0;
3432 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
3435 g_assert_not_reached();
3439 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3440 mmu_idx = ARMMMUIdx_E10_1;
3443 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3444 mmu_idx = ARMMMUIdx_E10_0;
3447 g_assert_not_reached();
3450 par64 = do_ats_write(env, value, access_type, mmu_idx);
3452 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3455 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3458 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3461 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
3463 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3466 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3469 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3470 return CP_ACCESS_TRAP;
3472 return CP_ACCESS_OK;
3475 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3478 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3480 int secure = arm_is_secure_below_el3(env);
3482 switch (ri->opc2 & 6) {
3485 case 0: /* AT S1E1R, AT S1E1W */
3486 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3488 case 4: /* AT S1E2R, AT S1E2W */
3489 mmu_idx = ARMMMUIdx_E2;
3491 case 6: /* AT S1E3R, AT S1E3W */
3492 mmu_idx = ARMMMUIdx_SE3;
3495 g_assert_not_reached();
3498 case 2: /* AT S1E0R, AT S1E0W */
3499 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
3501 case 4: /* AT S12E1R, AT S12E1W */
3502 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
3504 case 6: /* AT S12E0R, AT S12E0W */
3505 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
3508 g_assert_not_reached();
3511 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3515 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3516 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3517 .access = PL1_RW, .resetvalue = 0,
3518 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3519 offsetoflow32(CPUARMState, cp15.par_ns) },
3520 .writefn = par_write },
3521 #ifndef CONFIG_USER_ONLY
3522 /* This underdecoding is safe because the reginfo is NO_RAW. */
3523 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3524 .access = PL1_W, .accessfn = ats_access,
3525 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3530 /* Return basic MPU access permission bits. */
3531 static uint32_t simple_mpu_ap_bits(uint32_t val)
3538 for (i = 0; i < 16; i += 2) {
3539 ret |= (val >> i) & mask;
3545 /* Pad basic MPU access permission bits to extended format. */
3546 static uint32_t extended_mpu_ap_bits(uint32_t val)
3553 for (i = 0; i < 16; i += 2) {
3554 ret |= (val & mask) << i;
3560 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3563 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3566 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3568 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3571 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3574 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3577 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3579 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3582 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3584 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3590 u32p += env->pmsav7.rnr[M_REG_NS];
3594 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3597 ARMCPU *cpu = env_archcpu(env);
3598 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3604 u32p += env->pmsav7.rnr[M_REG_NS];
3605 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3609 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3612 ARMCPU *cpu = env_archcpu(env);
3613 uint32_t nrgs = cpu->pmsav7_dregion;
3615 if (value >= nrgs) {
3616 qemu_log_mask(LOG_GUEST_ERROR,
3617 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3618 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3622 raw_write(env, ri, value);
3625 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3626 /* Reset for all these registers is handled in arm_cpu_reset(),
3627 * because the PMSAv7 is also used by M-profile CPUs, which do
3628 * not register cpregs but still need the state to be reset.
3630 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3631 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3632 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3633 .readfn = pmsav7_read, .writefn = pmsav7_write,
3634 .resetfn = arm_cp_reset_ignore },
3635 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3636 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3637 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3638 .readfn = pmsav7_read, .writefn = pmsav7_write,
3639 .resetfn = arm_cp_reset_ignore },
3640 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3641 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3642 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3643 .readfn = pmsav7_read, .writefn = pmsav7_write,
3644 .resetfn = arm_cp_reset_ignore },
3645 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3647 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3648 .writefn = pmsav7_rgnr_write,
3649 .resetfn = arm_cp_reset_ignore },
3653 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3654 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3655 .access = PL1_RW, .type = ARM_CP_ALIAS,
3656 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3657 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3658 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3659 .access = PL1_RW, .type = ARM_CP_ALIAS,
3660 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3661 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3662 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3664 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3666 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3668 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3670 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3672 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3673 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3675 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3676 /* Protection region base and size registers */
3677 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3678 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3679 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3680 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3681 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3682 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3683 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3684 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3685 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3686 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3687 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3688 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3689 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3690 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3691 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3692 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3693 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3694 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3695 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3696 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3697 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3698 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3699 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3700 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3704 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3707 TCR *tcr = raw_ptr(env, ri);
3708 int maskshift = extract32(value, 0, 3);
3710 if (!arm_feature(env, ARM_FEATURE_V8)) {
3711 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3712 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3713 * using Long-desciptor translation table format */
3714 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3715 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3716 /* In an implementation that includes the Security Extensions
3717 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3718 * Short-descriptor translation table format.
3720 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3726 /* Update the masks corresponding to the TCR bank being written
3727 * Note that we always calculate mask and base_mask, but
3728 * they are only used for short-descriptor tables (ie if EAE is 0);
3729 * for long-descriptor tables the TCR fields are used differently
3730 * and the mask and base_mask values are meaningless.
3732 tcr->raw_tcr = value;
3733 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3734 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
3737 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3740 ARMCPU *cpu = env_archcpu(env);
3741 TCR *tcr = raw_ptr(env, ri);
3743 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3744 /* With LPAE the TTBCR could result in a change of ASID
3745 * via the TTBCR.A1 bit, so do a TLB flush.
3747 tlb_flush(CPU(cpu));
3749 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3750 value = deposit64(tcr->raw_tcr, 0, 32, value);
3751 vmsa_ttbcr_raw_write(env, ri, value);
3754 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3756 TCR *tcr = raw_ptr(env, ri);
3758 /* Reset both the TCR as well as the masks corresponding to the bank of
3759 * the TCR being reset.
3763 tcr->base_mask = 0xffffc000u;
3766 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3769 ARMCPU *cpu = env_archcpu(env);
3770 TCR *tcr = raw_ptr(env, ri);
3772 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3773 tlb_flush(CPU(cpu));
3774 tcr->raw_tcr = value;
3777 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3780 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3781 if (cpreg_field_is_64bit(ri) &&
3782 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3783 ARMCPU *cpu = env_archcpu(env);
3784 tlb_flush(CPU(cpu));
3786 raw_write(env, ri, value);
3789 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3793 * If we are running with E2&0 regime, then an ASID is active.
3794 * Flush if that might be changing. Note we're not checking
3795 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3796 * holds the active ASID, only checking the field that might.
3798 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3799 (arm_hcr_el2_eff(env) & HCR_E2H)) {
3800 tlb_flush_by_mmuidx(env_cpu(env),
3801 ARMMMUIdxBit_E20_2 | ARMMMUIdxBit_E20_0);
3803 raw_write(env, ri, value);
3806 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3809 ARMCPU *cpu = env_archcpu(env);
3810 CPUState *cs = CPU(cpu);
3813 * A change in VMID to the stage2 page table (Stage2) invalidates
3814 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3816 if (raw_read(env, ri) != value) {
3817 tlb_flush_by_mmuidx(cs,
3818 ARMMMUIdxBit_E10_1 |
3819 ARMMMUIdxBit_E10_0 |
3820 ARMMMUIdxBit_Stage2);
3821 raw_write(env, ri, value);
3825 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3826 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3827 .access = PL1_RW, .type = ARM_CP_ALIAS,
3828 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3829 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3830 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3831 .access = PL1_RW, .resetvalue = 0,
3832 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3833 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3834 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3835 .access = PL1_RW, .resetvalue = 0,
3836 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3837 offsetof(CPUARMState, cp15.dfar_ns) } },
3838 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3839 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3840 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3845 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3846 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3847 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3849 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3850 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3851 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3852 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3853 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3854 offsetof(CPUARMState, cp15.ttbr0_ns) } },
3855 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3856 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3857 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3858 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3859 offsetof(CPUARMState, cp15.ttbr1_ns) } },
3860 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3861 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3862 .access = PL1_RW, .writefn = vmsa_tcr_el12_write,
3863 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3864 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3865 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3866 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3867 .raw_writefn = vmsa_ttbcr_raw_write,
3868 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3869 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
3873 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3874 * qemu tlbs nor adjusting cached masks.
3876 static const ARMCPRegInfo ttbcr2_reginfo = {
3877 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3878 .access = PL1_RW, .type = ARM_CP_ALIAS,
3879 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3880 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
3883 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3886 env->cp15.c15_ticonfig = value & 0xe7;
3887 /* The OS_TYPE bit in this register changes the reported CPUID! */
3888 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3889 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3892 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3895 env->cp15.c15_threadid = value & 0xffff;
3898 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3901 /* Wait-for-interrupt (deprecated) */
3902 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3905 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3908 /* On OMAP there are registers indicating the max/min index of dcache lines
3909 * containing a dirty line; cache flush operations have to reset these.
3911 env->cp15.c15_i_max = 0x000;
3912 env->cp15.c15_i_min = 0xff0;
3915 static const ARMCPRegInfo omap_cp_reginfo[] = {
3916 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3917 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3918 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3920 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3921 .access = PL1_RW, .type = ARM_CP_NOP },
3922 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3924 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3925 .writefn = omap_ticonfig_write },
3926 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3928 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3929 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3930 .access = PL1_RW, .resetvalue = 0xff0,
3931 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3932 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3934 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3935 .writefn = omap_threadid_write },
3936 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3937 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3938 .type = ARM_CP_NO_RAW,
3939 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3940 /* TODO: Peripheral port remap register:
3941 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3942 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3945 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3946 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3947 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3948 .writefn = omap_cachemaint_write },
3949 { .name = "C9", .cp = 15, .crn = 9,
3950 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3951 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3955 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3958 env->cp15.c15_cpar = value & 0x3fff;
3961 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3962 { .name = "XSCALE_CPAR",
3963 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3964 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3965 .writefn = xscale_cpar_write, },
3966 { .name = "XSCALE_AUXCR",
3967 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3968 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3970 /* XScale specific cache-lockdown: since we have no cache we NOP these
3971 * and hope the guest does not really rely on cache behaviour.
3973 { .name = "XSCALE_LOCK_ICACHE_LINE",
3974 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3975 .access = PL1_W, .type = ARM_CP_NOP },
3976 { .name = "XSCALE_UNLOCK_ICACHE",
3977 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3978 .access = PL1_W, .type = ARM_CP_NOP },
3979 { .name = "XSCALE_DCACHE_LOCK",
3980 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3981 .access = PL1_RW, .type = ARM_CP_NOP },
3982 { .name = "XSCALE_UNLOCK_DCACHE",
3983 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3984 .access = PL1_W, .type = ARM_CP_NOP },
3988 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3989 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3990 * implementation of this implementation-defined space.
3991 * Ideally this should eventually disappear in favour of actually
3992 * implementing the correct behaviour for all cores.
3994 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3995 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3997 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4002 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4003 /* Cache status: RAZ because we have no cache so it's always clean */
4004 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4005 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4010 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4011 /* We never have a a block transfer operation in progress */
4012 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4013 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4015 /* The cache ops themselves: these all NOP for QEMU */
4016 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4017 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4018 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4019 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4020 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4021 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4022 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4023 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4024 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4025 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4026 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4027 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4031 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4032 /* The cache test-and-clean instructions always return (1 << 30)
4033 * to indicate that there are no dirty cache lines.
4035 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4036 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4037 .resetvalue = (1 << 30) },
4038 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4039 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4040 .resetvalue = (1 << 30) },
4044 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4045 /* Ignore ReadBuffer accesses */
4046 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4047 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4048 .access = PL1_RW, .resetvalue = 0,
4049 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4053 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4055 ARMCPU *cpu = env_archcpu(env);
4056 unsigned int cur_el = arm_current_el(env);
4057 bool secure = arm_is_secure(env);
4059 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4060 return env->cp15.vpidr_el2;
4062 return raw_read(env, ri);
4065 static uint64_t mpidr_read_val(CPUARMState *env)
4067 ARMCPU *cpu = env_archcpu(env);
4068 uint64_t mpidr = cpu->mp_affinity;
4070 if (arm_feature(env, ARM_FEATURE_V7MP)) {
4071 mpidr |= (1U << 31);
4072 /* Cores which are uniprocessor (non-coherent)
4073 * but still implement the MP extensions set
4074 * bit 30. (For instance, Cortex-R5).
4076 if (cpu->mp_is_up) {
4077 mpidr |= (1u << 30);
4083 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4085 unsigned int cur_el = arm_current_el(env);
4086 bool secure = arm_is_secure(env);
4088 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4089 return env->cp15.vmpidr_el2;
4091 return mpidr_read_val(env);
4094 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4096 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4097 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4098 .access = PL1_RW, .type = ARM_CP_CONST,
4100 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4101 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4102 .access = PL1_RW, .type = ARM_CP_CONST,
4104 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4105 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4106 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4107 offsetof(CPUARMState, cp15.par_ns)} },
4108 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4109 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4110 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4111 offsetof(CPUARMState, cp15.ttbr0_ns) },
4112 .writefn = vmsa_ttbr_write, },
4113 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4114 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4115 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4116 offsetof(CPUARMState, cp15.ttbr1_ns) },
4117 .writefn = vmsa_ttbr_write, },
4121 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4123 return vfp_get_fpcr(env);
4126 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4129 vfp_set_fpcr(env, value);
4132 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4134 return vfp_get_fpsr(env);
4137 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4140 vfp_set_fpsr(env, value);
4143 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4146 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4147 return CP_ACCESS_TRAP;
4149 return CP_ACCESS_OK;
4152 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4155 env->daif = value & PSTATE_DAIF;
4158 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
4159 const ARMCPRegInfo *ri,
4162 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
4163 * SCTLR_EL1.UCI is set.
4165 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UCI)) {
4166 return CP_ACCESS_TRAP;
4168 return CP_ACCESS_OK;
4171 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4172 * Page D4-1736 (DDI0487A.b)
4175 static int vae1_tlbmask(CPUARMState *env)
4177 /* Since we exclude secure first, we may read HCR_EL2 directly. */
4178 if (arm_is_secure_below_el3(env)) {
4179 return ARMMMUIdxBit_SE10_1 | ARMMMUIdxBit_SE10_0;
4180 } else if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE))
4181 == (HCR_E2H | HCR_TGE)) {
4182 return ARMMMUIdxBit_E20_2 | ARMMMUIdxBit_E20_0;
4184 return ARMMMUIdxBit_E10_1 | ARMMMUIdxBit_E10_0;
4188 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4191 CPUState *cs = env_cpu(env);
4192 int mask = vae1_tlbmask(env);
4194 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4197 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4200 CPUState *cs = env_cpu(env);
4201 int mask = vae1_tlbmask(env);
4203 if (tlb_force_broadcast(env)) {
4204 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4206 tlb_flush_by_mmuidx(cs, mask);
4210 static int alle1_tlbmask(CPUARMState *env)
4213 * Note that the 'ALL' scope must invalidate both stage 1 and
4214 * stage 2 translations, whereas most other scopes only invalidate
4215 * stage 1 translations.
4217 if (arm_is_secure_below_el3(env)) {
4218 return ARMMMUIdxBit_SE10_1 | ARMMMUIdxBit_SE10_0;
4219 } else if (arm_feature(env, ARM_FEATURE_EL2)) {
4220 return ARMMMUIdxBit_E10_1 | ARMMMUIdxBit_E10_0 | ARMMMUIdxBit_Stage2;
4222 return ARMMMUIdxBit_E10_1 | ARMMMUIdxBit_E10_0;
4226 static int e2_tlbmask(CPUARMState *env)
4228 /* TODO: ARMv8.4-SecEL2 */
4229 return ARMMMUIdxBit_E20_0 | ARMMMUIdxBit_E20_2 | ARMMMUIdxBit_E2;
4232 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4235 CPUState *cs = env_cpu(env);
4236 int mask = alle1_tlbmask(env);
4238 tlb_flush_by_mmuidx(cs, mask);
4241 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4244 CPUState *cs = env_cpu(env);
4245 int mask = e2_tlbmask(env);
4247 tlb_flush_by_mmuidx(cs, mask);
4250 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4253 ARMCPU *cpu = env_archcpu(env);
4254 CPUState *cs = CPU(cpu);
4256 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
4259 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4262 CPUState *cs = env_cpu(env);
4263 int mask = alle1_tlbmask(env);
4265 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4268 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4271 CPUState *cs = env_cpu(env);
4272 int mask = e2_tlbmask(env);
4274 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4277 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4280 CPUState *cs = env_cpu(env);
4282 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
4285 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4288 /* Invalidate by VA, EL2
4289 * Currently handles both VAE2 and VALE2, since we don't support
4290 * flush-last-level-only.
4292 CPUState *cs = env_cpu(env);
4293 int mask = e2_tlbmask(env);
4294 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4296 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4299 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4302 /* Invalidate by VA, EL3
4303 * Currently handles both VAE3 and VALE3, since we don't support
4304 * flush-last-level-only.
4306 ARMCPU *cpu = env_archcpu(env);
4307 CPUState *cs = CPU(cpu);
4308 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4310 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
4313 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4316 CPUState *cs = env_cpu(env);
4317 int mask = vae1_tlbmask(env);
4318 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4320 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4323 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4326 /* Invalidate by VA, EL1&0 (AArch64 version).
4327 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4328 * since we don't support flush-for-specific-ASID-only or
4329 * flush-last-level-only.
4331 CPUState *cs = env_cpu(env);
4332 int mask = vae1_tlbmask(env);
4333 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4335 if (tlb_force_broadcast(env)) {
4336 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4338 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4342 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4345 CPUState *cs = env_cpu(env);
4346 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4348 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
4352 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4355 CPUState *cs = env_cpu(env);
4356 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4358 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
4362 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4365 /* Invalidate by IPA. This has to invalidate any structures that
4366 * contain only stage 2 translation information, but does not need
4367 * to apply to structures that contain combined stage 1 and stage 2
4368 * translation information.
4369 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
4371 ARMCPU *cpu = env_archcpu(env);
4372 CPUState *cs = CPU(cpu);
4375 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4379 pageaddr = sextract64(value << 12, 0, 48);
4381 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
4384 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4387 CPUState *cs = env_cpu(env);
4390 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
4394 pageaddr = sextract64(value << 12, 0, 48);
4396 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
4397 ARMMMUIdxBit_Stage2);
4400 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4403 int cur_el = arm_current_el(env);
4406 uint64_t hcr = arm_hcr_el2_eff(env);
4409 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4410 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4411 return CP_ACCESS_TRAP_EL2;
4414 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4415 return CP_ACCESS_TRAP;
4417 if (hcr & HCR_TDZ) {
4418 return CP_ACCESS_TRAP_EL2;
4421 } else if (hcr & HCR_TDZ) {
4422 return CP_ACCESS_TRAP_EL2;
4425 return CP_ACCESS_OK;
4428 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4430 ARMCPU *cpu = env_archcpu(env);
4431 int dzp_bit = 1 << 4;
4433 /* DZP indicates whether DC ZVA access is allowed */
4434 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4437 return cpu->dcz_blocksize | dzp_bit;
4440 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4443 if (!(env->pstate & PSTATE_SP)) {
4444 /* Access to SP_EL0 is undefined if it's being used as
4445 * the stack pointer.
4447 return CP_ACCESS_TRAP_UNCATEGORIZED;
4449 return CP_ACCESS_OK;
4452 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4454 return env->pstate & PSTATE_SP;
4457 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4459 update_spsel(env, val);
4462 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4465 ARMCPU *cpu = env_archcpu(env);
4467 if (raw_read(env, ri) == value) {
4468 /* Skip the TLB flush if nothing actually changed; Linux likes
4469 * to do a lot of pointless SCTLR writes.
4474 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4475 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4479 raw_write(env, ri, value);
4480 /* ??? Lots of these bits are not implemented. */
4481 /* This may enable/disable the MMU, so do a TLB flush. */
4482 tlb_flush(CPU(cpu));
4484 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4486 * Normally we would always end the TB on an SCTLR write; see the
4487 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4488 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4489 * of hflags from the translator, so do it here.
4491 arm_rebuild_hflags(env);
4495 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4498 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
4499 return CP_ACCESS_TRAP_FP_EL2;
4501 if (env->cp15.cptr_el[3] & CPTR_TFP) {
4502 return CP_ACCESS_TRAP_FP_EL3;
4504 return CP_ACCESS_OK;
4507 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4510 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4513 static const ARMCPRegInfo v8_cp_reginfo[] = {
4514 /* Minimal set of EL0-visible registers. This will need to be expanded
4515 * significantly for system emulation of AArch64 CPUs.
4517 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4518 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4519 .access = PL0_RW, .type = ARM_CP_NZCV },
4520 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4521 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4522 .type = ARM_CP_NO_RAW,
4523 .access = PL0_RW, .accessfn = aa64_daif_access,
4524 .fieldoffset = offsetof(CPUARMState, daif),
4525 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4526 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4527 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4528 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4529 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4530 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4531 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4532 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4533 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4534 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4535 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4536 .access = PL0_R, .type = ARM_CP_NO_RAW,
4537 .readfn = aa64_dczid_read },
4538 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4539 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4540 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4541 #ifndef CONFIG_USER_ONLY
4542 /* Avoid overhead of an access check that always passes in user-mode */
4543 .accessfn = aa64_zva_access,
4546 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4547 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4548 .access = PL1_R, .type = ARM_CP_CURRENTEL },
4549 /* Cache ops: all NOPs since we don't emulate caches */
4550 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4551 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4552 .access = PL1_W, .type = ARM_CP_NOP },
4553 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4554 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4555 .access = PL1_W, .type = ARM_CP_NOP },
4556 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4557 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4558 .access = PL0_W, .type = ARM_CP_NOP,
4559 .accessfn = aa64_cacheop_access },
4560 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4561 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4562 .access = PL1_W, .type = ARM_CP_NOP },
4563 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4564 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4565 .access = PL1_W, .type = ARM_CP_NOP },
4566 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4567 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4568 .access = PL0_W, .type = ARM_CP_NOP,
4569 .accessfn = aa64_cacheop_access },
4570 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4571 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4572 .access = PL1_W, .type = ARM_CP_NOP },
4573 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4574 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4575 .access = PL0_W, .type = ARM_CP_NOP,
4576 .accessfn = aa64_cacheop_access },
4577 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4578 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4579 .access = PL0_W, .type = ARM_CP_NOP,
4580 .accessfn = aa64_cacheop_access },
4581 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4582 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4583 .access = PL1_W, .type = ARM_CP_NOP },
4584 /* TLBI operations */
4585 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4586 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4587 .access = PL1_W, .type = ARM_CP_NO_RAW,
4588 .writefn = tlbi_aa64_vmalle1is_write },
4589 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4590 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4591 .access = PL1_W, .type = ARM_CP_NO_RAW,
4592 .writefn = tlbi_aa64_vae1is_write },
4593 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4594 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4595 .access = PL1_W, .type = ARM_CP_NO_RAW,
4596 .writefn = tlbi_aa64_vmalle1is_write },
4597 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4598 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4599 .access = PL1_W, .type = ARM_CP_NO_RAW,
4600 .writefn = tlbi_aa64_vae1is_write },
4601 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4602 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4603 .access = PL1_W, .type = ARM_CP_NO_RAW,
4604 .writefn = tlbi_aa64_vae1is_write },
4605 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4606 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4607 .access = PL1_W, .type = ARM_CP_NO_RAW,
4608 .writefn = tlbi_aa64_vae1is_write },
4609 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4610 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4611 .access = PL1_W, .type = ARM_CP_NO_RAW,
4612 .writefn = tlbi_aa64_vmalle1_write },
4613 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4614 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4615 .access = PL1_W, .type = ARM_CP_NO_RAW,
4616 .writefn = tlbi_aa64_vae1_write },
4617 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4618 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4619 .access = PL1_W, .type = ARM_CP_NO_RAW,
4620 .writefn = tlbi_aa64_vmalle1_write },
4621 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4622 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4623 .access = PL1_W, .type = ARM_CP_NO_RAW,
4624 .writefn = tlbi_aa64_vae1_write },
4625 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4626 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4627 .access = PL1_W, .type = ARM_CP_NO_RAW,
4628 .writefn = tlbi_aa64_vae1_write },
4629 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4630 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4631 .access = PL1_W, .type = ARM_CP_NO_RAW,
4632 .writefn = tlbi_aa64_vae1_write },
4633 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4634 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4635 .access = PL2_W, .type = ARM_CP_NO_RAW,
4636 .writefn = tlbi_aa64_ipas2e1is_write },
4637 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4638 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4639 .access = PL2_W, .type = ARM_CP_NO_RAW,
4640 .writefn = tlbi_aa64_ipas2e1is_write },
4641 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4642 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4643 .access = PL2_W, .type = ARM_CP_NO_RAW,
4644 .writefn = tlbi_aa64_alle1is_write },
4645 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4646 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4647 .access = PL2_W, .type = ARM_CP_NO_RAW,
4648 .writefn = tlbi_aa64_alle1is_write },
4649 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4650 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4651 .access = PL2_W, .type = ARM_CP_NO_RAW,
4652 .writefn = tlbi_aa64_ipas2e1_write },
4653 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4654 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4655 .access = PL2_W, .type = ARM_CP_NO_RAW,
4656 .writefn = tlbi_aa64_ipas2e1_write },
4657 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4658 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4659 .access = PL2_W, .type = ARM_CP_NO_RAW,
4660 .writefn = tlbi_aa64_alle1_write },
4661 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4662 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4663 .access = PL2_W, .type = ARM_CP_NO_RAW,
4664 .writefn = tlbi_aa64_alle1is_write },
4665 #ifndef CONFIG_USER_ONLY
4666 /* 64 bit address translation operations */
4667 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4668 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4669 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4670 .writefn = ats_write64 },
4671 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4672 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4673 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4674 .writefn = ats_write64 },
4675 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4676 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4677 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4678 .writefn = ats_write64 },
4679 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4680 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4681 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4682 .writefn = ats_write64 },
4683 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4684 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4685 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4686 .writefn = ats_write64 },
4687 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4688 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4689 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4690 .writefn = ats_write64 },
4691 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4692 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4693 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4694 .writefn = ats_write64 },
4695 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4696 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4697 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4698 .writefn = ats_write64 },
4699 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4700 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4701 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4702 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4703 .writefn = ats_write64 },
4704 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4705 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4706 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4707 .writefn = ats_write64 },
4708 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4709 .type = ARM_CP_ALIAS,
4710 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4711 .access = PL1_RW, .resetvalue = 0,
4712 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4713 .writefn = par_write },
4715 /* TLB invalidate last level of translation table walk */
4716 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4717 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
4718 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4719 .type = ARM_CP_NO_RAW, .access = PL1_W,
4720 .writefn = tlbimvaa_is_write },
4721 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4722 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
4723 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4724 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
4725 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4726 .type = ARM_CP_NO_RAW, .access = PL2_W,
4727 .writefn = tlbimva_hyp_write },
4728 { .name = "TLBIMVALHIS",
4729 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4730 .type = ARM_CP_NO_RAW, .access = PL2_W,
4731 .writefn = tlbimva_hyp_is_write },
4732 { .name = "TLBIIPAS2",
4733 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4734 .type = ARM_CP_NO_RAW, .access = PL2_W,
4735 .writefn = tlbiipas2_write },
4736 { .name = "TLBIIPAS2IS",
4737 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4738 .type = ARM_CP_NO_RAW, .access = PL2_W,
4739 .writefn = tlbiipas2_is_write },
4740 { .name = "TLBIIPAS2L",
4741 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4742 .type = ARM_CP_NO_RAW, .access = PL2_W,
4743 .writefn = tlbiipas2_write },
4744 { .name = "TLBIIPAS2LIS",
4745 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4746 .type = ARM_CP_NO_RAW, .access = PL2_W,
4747 .writefn = tlbiipas2_is_write },
4748 /* 32 bit cache operations */
4749 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4750 .type = ARM_CP_NOP, .access = PL1_W },
4751 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4752 .type = ARM_CP_NOP, .access = PL1_W },
4753 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4754 .type = ARM_CP_NOP, .access = PL1_W },
4755 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
4756 .type = ARM_CP_NOP, .access = PL1_W },
4757 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
4758 .type = ARM_CP_NOP, .access = PL1_W },
4759 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
4760 .type = ARM_CP_NOP, .access = PL1_W },
4761 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4762 .type = ARM_CP_NOP, .access = PL1_W },
4763 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4764 .type = ARM_CP_NOP, .access = PL1_W },
4765 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
4766 .type = ARM_CP_NOP, .access = PL1_W },
4767 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4768 .type = ARM_CP_NOP, .access = PL1_W },
4769 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
4770 .type = ARM_CP_NOP, .access = PL1_W },
4771 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
4772 .type = ARM_CP_NOP, .access = PL1_W },
4773 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4774 .type = ARM_CP_NOP, .access = PL1_W },
4775 /* MMU Domain access control / MPU write buffer control */
4776 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
4777 .access = PL1_RW, .resetvalue = 0,
4778 .writefn = dacr_write, .raw_writefn = raw_write,
4779 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
4780 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
4781 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
4782 .type = ARM_CP_ALIAS,
4783 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
4785 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
4786 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
4787 .type = ARM_CP_ALIAS,
4788 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
4790 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
4791 /* We rely on the access checks not allowing the guest to write to the
4792 * state field when SPSel indicates that it's being used as the stack
4795 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
4796 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
4797 .access = PL1_RW, .accessfn = sp_el0_access,
4798 .type = ARM_CP_ALIAS,
4799 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
4800 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
4801 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
4802 .access = PL2_RW, .type = ARM_CP_ALIAS,
4803 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
4804 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
4805 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
4806 .type = ARM_CP_NO_RAW,
4807 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
4808 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
4809 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
4810 .type = ARM_CP_ALIAS,
4811 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
4812 .access = PL2_RW, .accessfn = fpexc32_access },
4813 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
4814 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
4815 .access = PL2_RW, .resetvalue = 0,
4816 .writefn = dacr_write, .raw_writefn = raw_write,
4817 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
4818 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
4819 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
4820 .access = PL2_RW, .resetvalue = 0,
4821 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
4822 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
4823 .type = ARM_CP_ALIAS,
4824 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
4826 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
4827 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
4828 .type = ARM_CP_ALIAS,
4829 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
4831 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
4832 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
4833 .type = ARM_CP_ALIAS,
4834 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
4836 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
4837 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
4838 .type = ARM_CP_ALIAS,
4839 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
4841 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
4842 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
4843 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
4845 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
4846 { .name = "SDCR", .type = ARM_CP_ALIAS,
4847 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
4848 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
4849 .writefn = sdcr_write,
4850 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
4854 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4855 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
4856 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
4857 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
4859 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
4860 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
4861 .type = ARM_CP_NO_RAW,
4862 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
4864 .type = ARM_CP_CONST, .resetvalue = 0 },
4865 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
4866 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
4867 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4868 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
4869 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
4871 .type = ARM_CP_CONST, .resetvalue = 0 },
4872 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
4873 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
4874 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4875 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
4876 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
4877 .access = PL2_RW, .type = ARM_CP_CONST,
4879 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
4880 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
4881 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4882 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
4883 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
4884 .access = PL2_RW, .type = ARM_CP_CONST,
4886 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
4887 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
4888 .access = PL2_RW, .type = ARM_CP_CONST,
4890 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
4891 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
4892 .access = PL2_RW, .type = ARM_CP_CONST,
4894 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
4895 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
4896 .access = PL2_RW, .type = ARM_CP_CONST,
4898 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
4899 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
4900 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4901 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
4902 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
4903 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4904 .type = ARM_CP_CONST, .resetvalue = 0 },
4905 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
4906 .cp = 15, .opc1 = 6, .crm = 2,
4907 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4908 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
4909 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
4910 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
4911 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4912 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
4913 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
4914 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4915 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4916 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
4917 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4918 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
4919 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
4920 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4921 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
4922 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4924 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
4925 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
4926 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4927 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
4928 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
4929 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4930 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
4931 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4933 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
4934 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
4935 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4936 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
4937 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
4939 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
4940 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
4941 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4942 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
4943 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
4944 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4945 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
4946 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
4947 .access = PL2_RW, .accessfn = access_tda,
4948 .type = ARM_CP_CONST, .resetvalue = 0 },
4949 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
4950 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
4951 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4952 .type = ARM_CP_CONST, .resetvalue = 0 },
4953 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
4954 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
4955 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4956 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
4957 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
4958 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
4959 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
4960 .type = ARM_CP_CONST,
4961 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
4962 .access = PL2_RW, .resetvalue = 0 },
4966 /* Ditto, but for registers which exist in ARMv8 but not v7 */
4967 static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
4968 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
4969 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
4971 .type = ARM_CP_CONST, .resetvalue = 0 },
4975 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4977 ARMCPU *cpu = env_archcpu(env);
4978 /* Begin with bits defined in base ARMv8.0. */
4979 uint64_t valid_mask = MAKE_64BIT_MASK(0, 34);
4981 if (arm_feature(env, ARM_FEATURE_EL3)) {
4982 valid_mask &= ~HCR_HCD;
4983 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
4984 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
4985 * However, if we're using the SMC PSCI conduit then QEMU is
4986 * effectively acting like EL3 firmware and so the guest at
4987 * EL2 should retain the ability to prevent EL1 from being
4988 * able to make SMC calls into the ersatz firmware, so in
4989 * that case HCR.TSC should be read/write.
4991 valid_mask &= ~HCR_TSC;
4993 if (cpu_isar_feature(aa64_vh, cpu)) {
4994 valid_mask |= HCR_E2H;
4996 if (cpu_isar_feature(aa64_lor, cpu)) {
4997 valid_mask |= HCR_TLOR;
4999 if (cpu_isar_feature(aa64_pauth, cpu)) {
5000 valid_mask |= HCR_API | HCR_APK;
5003 /* Clear RES0 bits. */
5004 value &= valid_mask;
5006 /* These bits change the MMU setup:
5007 * HCR_VM enables stage 2 translation
5008 * HCR_PTW forbids certain page-table setups
5009 * HCR_DC Disables stage1 and enables stage2 translation
5011 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
5012 tlb_flush(CPU(cpu));
5014 env->cp15.hcr_el2 = value;
5017 * Updates to VI and VF require us to update the status of
5018 * virtual interrupts, which are the logical OR of these bits
5019 * and the state of the input lines from the GIC. (This requires
5020 * that we have the iothread lock, which is done by marking the
5021 * reginfo structs as ARM_CP_IO.)
5022 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5023 * possible for it to be taken immediately, because VIRQ and
5024 * VFIQ are masked unless running at EL0 or EL1, and HCR
5025 * can only be written at EL2.
5027 g_assert(qemu_mutex_iothread_locked());
5028 arm_cpu_update_virq(cpu);
5029 arm_cpu_update_vfiq(cpu);
5032 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5035 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5036 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5037 hcr_write(env, NULL, value);
5040 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5043 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5044 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5045 hcr_write(env, NULL, value);
5049 * Return the effective value of HCR_EL2.
5050 * Bits that are not included here:
5051 * RW (read from SCR_EL3.RW as needed)
5053 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5055 uint64_t ret = env->cp15.hcr_el2;
5057 if (arm_is_secure_below_el3(env)) {
5059 * "This register has no effect if EL2 is not enabled in the
5060 * current Security state". This is ARMv8.4-SecEL2 speak for
5061 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5063 * Prior to that, the language was "In an implementation that
5064 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5065 * as if this field is 0 for all purposes other than a direct
5066 * read or write access of HCR_EL2". With lots of enumeration
5067 * on a per-field basis. In current QEMU, this is condition
5068 * is arm_is_secure_below_el3.
5070 * Since the v8.4 language applies to the entire register, and
5071 * appears to be backward compatible, use that.
5074 } else if (ret & HCR_TGE) {
5075 /* These bits are up-to-date as of ARMv8.4. */
5076 if (ret & HCR_E2H) {
5077 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5078 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5079 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5080 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE);
5082 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5084 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5085 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5086 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5093 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5097 * For A-profile AArch32 EL3, if NSACR.CP10
5098 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5100 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5101 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5102 value &= ~(0x3 << 10);
5103 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5105 env->cp15.cptr_el[2] = value;
5108 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5111 * For A-profile AArch32 EL3, if NSACR.CP10
5112 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5114 uint64_t value = env->cp15.cptr_el[2];
5116 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5117 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5123 static const ARMCPRegInfo el2_cp_reginfo[] = {
5124 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5126 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5127 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5128 .writefn = hcr_write },
5129 { .name = "HCR", .state = ARM_CP_STATE_AA32,
5130 .type = ARM_CP_ALIAS | ARM_CP_IO,
5131 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5132 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5133 .writefn = hcr_writelow },
5134 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5135 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5136 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5137 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5138 .type = ARM_CP_ALIAS,
5139 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5141 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5142 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5143 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5144 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5145 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5146 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5147 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5148 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5149 .type = ARM_CP_ALIAS,
5150 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5152 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5153 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5154 .type = ARM_CP_ALIAS,
5155 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5157 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5158 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5159 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5160 .access = PL2_RW, .writefn = vbar_write,
5161 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5163 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5164 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5165 .access = PL3_RW, .type = ARM_CP_ALIAS,
5166 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5167 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5168 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5169 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5170 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5171 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5172 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5173 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5174 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5176 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5177 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5178 .access = PL2_RW, .type = ARM_CP_ALIAS,
5179 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5180 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5181 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5182 .access = PL2_RW, .type = ARM_CP_CONST,
5184 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5185 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5186 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5187 .access = PL2_RW, .type = ARM_CP_CONST,
5189 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5190 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5191 .access = PL2_RW, .type = ARM_CP_CONST,
5193 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5194 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5195 .access = PL2_RW, .type = ARM_CP_CONST,
5197 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5198 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5199 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5200 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
5201 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5202 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5203 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5204 .type = ARM_CP_ALIAS,
5205 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5206 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5207 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5208 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5210 /* no .writefn needed as this can't cause an ASID change;
5211 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5213 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5214 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5215 .cp = 15, .opc1 = 6, .crm = 2,
5216 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5217 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5218 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5219 .writefn = vttbr_write },
5220 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5221 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5222 .access = PL2_RW, .writefn = vttbr_write,
5223 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5224 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5225 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5226 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5227 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5228 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5229 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5230 .access = PL2_RW, .resetvalue = 0,
5231 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5232 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5233 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5234 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5235 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5236 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5237 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5238 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5239 { .name = "TLBIALLNSNH",
5240 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5241 .type = ARM_CP_NO_RAW, .access = PL2_W,
5242 .writefn = tlbiall_nsnh_write },
5243 { .name = "TLBIALLNSNHIS",
5244 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5245 .type = ARM_CP_NO_RAW, .access = PL2_W,
5246 .writefn = tlbiall_nsnh_is_write },
5247 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5248 .type = ARM_CP_NO_RAW, .access = PL2_W,
5249 .writefn = tlbiall_hyp_write },
5250 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5251 .type = ARM_CP_NO_RAW, .access = PL2_W,
5252 .writefn = tlbiall_hyp_is_write },
5253 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5254 .type = ARM_CP_NO_RAW, .access = PL2_W,
5255 .writefn = tlbimva_hyp_write },
5256 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5257 .type = ARM_CP_NO_RAW, .access = PL2_W,
5258 .writefn = tlbimva_hyp_is_write },
5259 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5260 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5261 .type = ARM_CP_NO_RAW, .access = PL2_W,
5262 .writefn = tlbi_aa64_alle2_write },
5263 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5264 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5265 .type = ARM_CP_NO_RAW, .access = PL2_W,
5266 .writefn = tlbi_aa64_vae2_write },
5267 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5268 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5269 .access = PL2_W, .type = ARM_CP_NO_RAW,
5270 .writefn = tlbi_aa64_vae2_write },
5271 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5272 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5273 .access = PL2_W, .type = ARM_CP_NO_RAW,
5274 .writefn = tlbi_aa64_alle2is_write },
5275 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5276 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5277 .type = ARM_CP_NO_RAW, .access = PL2_W,
5278 .writefn = tlbi_aa64_vae2is_write },
5279 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5280 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5281 .access = PL2_W, .type = ARM_CP_NO_RAW,
5282 .writefn = tlbi_aa64_vae2is_write },
5283 #ifndef CONFIG_USER_ONLY
5284 /* Unlike the other EL2-related AT operations, these must
5285 * UNDEF from EL3 if EL2 is not implemented, which is why we
5286 * define them here rather than with the rest of the AT ops.
5288 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5289 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5290 .access = PL2_W, .accessfn = at_s1e2_access,
5291 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
5292 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5293 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5294 .access = PL2_W, .accessfn = at_s1e2_access,
5295 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
5296 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5297 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5298 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5299 * to behave as if SCR.NS was 1.
5301 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5303 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5304 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5306 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5307 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5308 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5309 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5310 * reset values as IMPDEF. We choose to reset to 3 to comply with
5311 * both ARMv7 and ARMv8.
5313 .access = PL2_RW, .resetvalue = 3,
5314 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5315 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5316 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5317 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5318 .writefn = gt_cntvoff_write,
5319 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5320 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5321 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5322 .writefn = gt_cntvoff_write,
5323 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5324 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5325 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5326 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5327 .type = ARM_CP_IO, .access = PL2_RW,
5328 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5329 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5330 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5331 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5332 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5333 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5334 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5335 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5336 .resetfn = gt_hyp_timer_reset,
5337 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5338 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5340 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5342 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5344 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5346 /* The only field of MDCR_EL2 that has a defined architectural reset value
5347 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
5348 * don't implement any PMU event counters, so using zero as a reset
5349 * value for MDCR_EL2 is okay
5351 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5352 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5353 .access = PL2_RW, .resetvalue = 0,
5354 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
5355 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5356 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5357 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5358 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5359 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5360 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5362 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5363 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5364 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5366 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5370 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5371 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5372 .type = ARM_CP_ALIAS | ARM_CP_IO,
5373 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5375 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5376 .writefn = hcr_writehigh },
5380 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5383 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5384 * At Secure EL1 it traps to EL3.
5386 if (arm_current_el(env) == 3) {
5387 return CP_ACCESS_OK;
5389 if (arm_is_secure_below_el3(env)) {
5390 return CP_ACCESS_TRAP_EL3;
5392 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5394 return CP_ACCESS_OK;
5396 return CP_ACCESS_TRAP_UNCATEGORIZED;
5399 static const ARMCPRegInfo el3_cp_reginfo[] = {
5400 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5401 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5402 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5403 .resetvalue = 0, .writefn = scr_write },
5404 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5405 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5406 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5407 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5408 .writefn = scr_write },
5409 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5410 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5411 .access = PL3_RW, .resetvalue = 0,
5412 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5414 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5415 .access = PL3_RW, .resetvalue = 0,
5416 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5417 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5418 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5419 .writefn = vbar_write, .resetvalue = 0,
5420 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5421 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5422 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5423 .access = PL3_RW, .resetvalue = 0,
5424 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5425 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5426 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5428 /* no .writefn needed as this can't cause an ASID change;
5429 * we must provide a .raw_writefn and .resetfn because we handle
5430 * reset and migration for the AArch32 TTBCR(S), which might be
5431 * using mask and base_mask.
5433 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
5434 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5435 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5436 .type = ARM_CP_ALIAS,
5437 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5439 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5440 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5441 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5442 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5443 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5444 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5445 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5446 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5447 .type = ARM_CP_ALIAS,
5448 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5450 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5451 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5452 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5453 .access = PL3_RW, .writefn = vbar_write,
5454 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5456 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5457 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5458 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5459 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5460 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5461 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5462 .access = PL3_RW, .resetvalue = 0,
5463 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5464 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5465 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5466 .access = PL3_RW, .type = ARM_CP_CONST,
5468 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5469 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5470 .access = PL3_RW, .type = ARM_CP_CONST,
5472 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5473 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5474 .access = PL3_RW, .type = ARM_CP_CONST,
5476 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5477 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5478 .access = PL3_W, .type = ARM_CP_NO_RAW,
5479 .writefn = tlbi_aa64_alle3is_write },
5480 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5481 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5482 .access = PL3_W, .type = ARM_CP_NO_RAW,
5483 .writefn = tlbi_aa64_vae3is_write },
5484 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5485 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5486 .access = PL3_W, .type = ARM_CP_NO_RAW,
5487 .writefn = tlbi_aa64_vae3is_write },
5488 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5489 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5490 .access = PL3_W, .type = ARM_CP_NO_RAW,
5491 .writefn = tlbi_aa64_alle3_write },
5492 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5493 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5494 .access = PL3_W, .type = ARM_CP_NO_RAW,
5495 .writefn = tlbi_aa64_vae3_write },
5496 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5497 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5498 .access = PL3_W, .type = ARM_CP_NO_RAW,
5499 .writefn = tlbi_aa64_vae3_write },
5503 #ifndef CONFIG_USER_ONLY
5504 /* Test if system register redirection is to occur in the current state. */
5505 static bool redirect_for_e2h(CPUARMState *env)
5507 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5510 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5514 if (redirect_for_e2h(env)) {
5515 /* Switch to the saved EL2 version of the register. */
5517 readfn = ri->readfn;
5519 readfn = ri->orig_readfn;
5521 if (readfn == NULL) {
5524 return readfn(env, ri);
5527 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5532 if (redirect_for_e2h(env)) {
5533 /* Switch to the saved EL2 version of the register. */
5535 writefn = ri->writefn;
5537 writefn = ri->orig_writefn;
5539 if (writefn == NULL) {
5540 writefn = raw_write;
5542 writefn(env, ri, value);
5545 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5548 uint32_t src_key, dst_key, new_key;
5549 const char *src_name, *dst_name, *new_name;
5550 bool (*feature)(const ARMISARegisters *id);
5553 #define K(op0, op1, crn, crm, op2) \
5554 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5556 static const struct E2HAlias aliases[] = {
5557 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5558 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5559 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5560 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5561 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5562 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5563 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5564 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5565 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5566 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5567 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5568 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5569 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5570 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5571 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5572 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5573 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5574 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5575 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5576 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5577 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5578 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5579 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5580 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5581 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5582 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5583 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5584 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5585 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5586 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5587 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5588 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5591 * Note that redirection of ZCR is mentioned in the description
5592 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5593 * not in the summary table.
5595 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5596 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5598 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5599 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5605 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5606 const struct E2HAlias *a = &aliases[i];
5607 ARMCPRegInfo *src_reg, *dst_reg;
5609 if (a->feature && !a->feature(&cpu->isar)) {
5613 src_reg = g_hash_table_lookup(cpu->cp_regs, &a->src_key);
5614 dst_reg = g_hash_table_lookup(cpu->cp_regs, &a->dst_key);
5615 g_assert(src_reg != NULL);
5616 g_assert(dst_reg != NULL);
5618 /* Cross-compare names to detect typos in the keys. */
5619 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5620 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5622 /* None of the core system registers use opaque; we will. */
5623 g_assert(src_reg->opaque == NULL);
5625 /* Create alias before redirection so we dup the right data. */
5627 ARMCPRegInfo *new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5628 uint32_t *new_key = g_memdup(&a->new_key, sizeof(uint32_t));
5631 new_reg->name = a->new_name;
5632 new_reg->type |= ARM_CP_ALIAS;
5633 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5634 new_reg->access &= PL2_RW | PL3_RW;
5636 ok = g_hash_table_insert(cpu->cp_regs, new_key, new_reg);
5640 src_reg->opaque = dst_reg;
5641 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5642 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5643 if (!src_reg->raw_readfn) {
5644 src_reg->raw_readfn = raw_read;
5646 if (!src_reg->raw_writefn) {
5647 src_reg->raw_writefn = raw_write;
5649 src_reg->readfn = el2_e2h_read;
5650 src_reg->writefn = el2_e2h_write;
5655 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5658 int cur_el = arm_current_el(env);
5661 uint64_t hcr = arm_hcr_el2_eff(env);
5664 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5665 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5666 return CP_ACCESS_TRAP_EL2;
5669 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5670 return CP_ACCESS_TRAP;
5672 if (hcr & HCR_TID2) {
5673 return CP_ACCESS_TRAP_EL2;
5676 } else if (hcr & HCR_TID2) {
5677 return CP_ACCESS_TRAP_EL2;
5681 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5682 return CP_ACCESS_TRAP_EL2;
5685 return CP_ACCESS_OK;
5688 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5691 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5692 * read via a bit in OSLSR_EL1.
5696 if (ri->state == ARM_CP_STATE_AA32) {
5697 oslock = (value == 0xC5ACCE55);
5702 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5705 static const ARMCPRegInfo debug_cp_reginfo[] = {
5706 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
5707 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5708 * unlike DBGDRAR it is never accessible from EL0.
5709 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5712 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
5713 .access = PL0_R, .accessfn = access_tdra,
5714 .type = ARM_CP_CONST, .resetvalue = 0 },
5715 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
5716 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5717 .access = PL1_R, .accessfn = access_tdra,
5718 .type = ARM_CP_CONST, .resetvalue = 0 },
5719 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
5720 .access = PL0_R, .accessfn = access_tdra,
5721 .type = ARM_CP_CONST, .resetvalue = 0 },
5722 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
5723 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
5724 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
5725 .access = PL1_RW, .accessfn = access_tda,
5726 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
5728 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
5729 * We don't implement the configurable EL0 access.
5731 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
5732 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
5733 .type = ARM_CP_ALIAS,
5734 .access = PL1_R, .accessfn = access_tda,
5735 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
5736 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
5737 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
5738 .access = PL1_W, .type = ARM_CP_NO_RAW,
5739 .accessfn = access_tdosa,
5740 .writefn = oslar_write },
5741 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
5742 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
5743 .access = PL1_R, .resetvalue = 10,
5744 .accessfn = access_tdosa,
5745 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5746 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
5747 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
5748 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
5749 .access = PL1_RW, .accessfn = access_tdosa,
5750 .type = ARM_CP_NOP },
5751 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
5752 * implement vector catch debug events yet.
5755 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
5756 .access = PL1_RW, .accessfn = access_tda,
5757 .type = ARM_CP_NOP },
5758 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
5759 * to save and restore a 32-bit guest's DBGVCR)
5761 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
5762 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
5763 .access = PL2_RW, .accessfn = access_tda,
5764 .type = ARM_CP_NOP },
5765 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
5766 * Channel but Linux may try to access this register. The 32-bit
5767 * alias is DBGDCCINT.
5769 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
5770 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5771 .access = PL1_RW, .accessfn = access_tda,
5772 .type = ARM_CP_NOP },
5776 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
5777 /* 64 bit access versions of the (dummy) debug registers */
5778 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
5779 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5780 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
5781 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5785 /* Return the exception level to which exceptions should be taken
5786 * via SVEAccessTrap. If an exception should be routed through
5787 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
5788 * take care of raising that exception.
5789 * C.f. the ARM pseudocode function CheckSVEEnabled.
5791 int sve_exception_el(CPUARMState *env, int el)
5793 #ifndef CONFIG_USER_ONLY
5795 bool disabled = false;
5797 /* The CPACR.ZEN controls traps to EL1:
5798 * 0, 2 : trap EL0 and EL1 accesses
5799 * 1 : trap only EL0 accesses
5800 * 3 : trap no accesses
5802 if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
5804 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
5809 return (arm_feature(env, ARM_FEATURE_EL2)
5810 && (arm_hcr_el2_eff(env) & HCR_TGE) ? 2 : 1);
5813 /* Check CPACR.FPEN. */
5814 if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
5816 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
5824 /* CPTR_EL2. Since TZ and TFP are positive,
5825 * they will be zero when EL2 is not present.
5827 if (el <= 2 && !arm_is_secure_below_el3(env)) {
5828 if (env->cp15.cptr_el[2] & CPTR_TZ) {
5831 if (env->cp15.cptr_el[2] & CPTR_TFP) {
5836 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
5837 if (arm_feature(env, ARM_FEATURE_EL3)
5838 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5845 static uint32_t sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
5849 end_len = start_len &= 0xf;
5850 if (!test_bit(start_len, cpu->sve_vq_map)) {
5851 end_len = find_last_bit(cpu->sve_vq_map, start_len);
5852 assert(end_len < start_len);
5858 * Given that SVE is enabled, return the vector length for EL.
5860 uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
5862 ARMCPU *cpu = env_archcpu(env);
5863 uint32_t zcr_len = cpu->sve_max_vq - 1;
5866 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
5868 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
5869 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
5871 if (arm_feature(env, ARM_FEATURE_EL3)) {
5872 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
5875 return sve_zcr_get_valid_len(cpu, zcr_len);
5878 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5881 int cur_el = arm_current_el(env);
5882 int old_len = sve_zcr_len_for_el(env, cur_el);
5885 /* Bits other than [3:0] are RAZ/WI. */
5886 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5887 raw_write(env, ri, value & 0xf);
5890 * Because we arrived here, we know both FP and SVE are enabled;
5891 * otherwise we would have trapped access to the ZCR_ELn register.
5893 new_len = sve_zcr_len_for_el(env, cur_el);
5894 if (new_len < old_len) {
5895 aarch64_sve_narrow_vq(env, new_len + 1);
5899 static const ARMCPRegInfo zcr_el1_reginfo = {
5900 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
5901 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
5902 .access = PL1_RW, .type = ARM_CP_SVE,
5903 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
5904 .writefn = zcr_write, .raw_writefn = raw_write
5907 static const ARMCPRegInfo zcr_el2_reginfo = {
5908 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
5909 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
5910 .access = PL2_RW, .type = ARM_CP_SVE,
5911 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
5912 .writefn = zcr_write, .raw_writefn = raw_write
5915 static const ARMCPRegInfo zcr_no_el2_reginfo = {
5916 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
5917 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
5918 .access = PL2_RW, .type = ARM_CP_SVE,
5919 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
5922 static const ARMCPRegInfo zcr_el3_reginfo = {
5923 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
5924 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
5925 .access = PL3_RW, .type = ARM_CP_SVE,
5926 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
5927 .writefn = zcr_write, .raw_writefn = raw_write
5930 void hw_watchpoint_update(ARMCPU *cpu, int n)
5932 CPUARMState *env = &cpu->env;
5934 vaddr wvr = env->cp15.dbgwvr[n];
5935 uint64_t wcr = env->cp15.dbgwcr[n];
5937 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
5939 if (env->cpu_watchpoint[n]) {
5940 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
5941 env->cpu_watchpoint[n] = NULL;
5944 if (!extract64(wcr, 0, 1)) {
5945 /* E bit clear : watchpoint disabled */
5949 switch (extract64(wcr, 3, 2)) {
5951 /* LSC 00 is reserved and must behave as if the wp is disabled */
5954 flags |= BP_MEM_READ;
5957 flags |= BP_MEM_WRITE;
5960 flags |= BP_MEM_ACCESS;
5964 /* Attempts to use both MASK and BAS fields simultaneously are
5965 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
5966 * thus generating a watchpoint for every byte in the masked region.
5968 mask = extract64(wcr, 24, 4);
5969 if (mask == 1 || mask == 2) {
5970 /* Reserved values of MASK; we must act as if the mask value was
5971 * some non-reserved value, or as if the watchpoint were disabled.
5972 * We choose the latter.
5976 /* Watchpoint covers an aligned area up to 2GB in size */
5978 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
5979 * whether the watchpoint fires when the unmasked bits match; we opt
5980 * to generate the exceptions.
5984 /* Watchpoint covers bytes defined by the byte address select bits */
5985 int bas = extract64(wcr, 5, 8);
5989 /* This must act as if the watchpoint is disabled */
5993 if (extract64(wvr, 2, 1)) {
5994 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
5995 * ignored, and BAS[3:0] define which bytes to watch.
5999 /* The BAS bits are supposed to be programmed to indicate a contiguous
6000 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6001 * we fire for each byte in the word/doubleword addressed by the WVR.
6002 * We choose to ignore any non-zero bits after the first range of 1s.
6004 basstart = ctz32(bas);
6005 len = cto32(bas >> basstart);
6009 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6010 &env->cpu_watchpoint[n]);
6013 void hw_watchpoint_update_all(ARMCPU *cpu)
6016 CPUARMState *env = &cpu->env;
6018 /* Completely clear out existing QEMU watchpoints and our array, to
6019 * avoid possible stale entries following migration load.
6021 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6022 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6024 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6025 hw_watchpoint_update(cpu, i);
6029 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6032 ARMCPU *cpu = env_archcpu(env);
6035 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
6036 * register reads and behaves as if values written are sign extended.
6037 * Bits [1:0] are RES0.
6039 value = sextract64(value, 0, 49) & ~3ULL;
6041 raw_write(env, ri, value);
6042 hw_watchpoint_update(cpu, i);
6045 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6048 ARMCPU *cpu = env_archcpu(env);
6051 raw_write(env, ri, value);
6052 hw_watchpoint_update(cpu, i);
6055 void hw_breakpoint_update(ARMCPU *cpu, int n)
6057 CPUARMState *env = &cpu->env;
6058 uint64_t bvr = env->cp15.dbgbvr[n];
6059 uint64_t bcr = env->cp15.dbgbcr[n];
6064 if (env->cpu_breakpoint[n]) {
6065 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6066 env->cpu_breakpoint[n] = NULL;
6069 if (!extract64(bcr, 0, 1)) {
6070 /* E bit clear : watchpoint disabled */
6074 bt = extract64(bcr, 20, 4);
6077 case 4: /* unlinked address mismatch (reserved if AArch64) */
6078 case 5: /* linked address mismatch (reserved if AArch64) */
6079 qemu_log_mask(LOG_UNIMP,
6080 "arm: address mismatch breakpoint types not implemented\n");
6082 case 0: /* unlinked address match */
6083 case 1: /* linked address match */
6085 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
6086 * we behave as if the register was sign extended. Bits [1:0] are
6087 * RES0. The BAS field is used to allow setting breakpoints on 16
6088 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6089 * a bp will fire if the addresses covered by the bp and the addresses
6090 * covered by the insn overlap but the insn doesn't start at the
6091 * start of the bp address range. We choose to require the insn and
6092 * the bp to have the same address. The constraints on writing to
6093 * BAS enforced in dbgbcr_write mean we have only four cases:
6094 * 0b0000 => no breakpoint
6095 * 0b0011 => breakpoint on addr
6096 * 0b1100 => breakpoint on addr + 2
6097 * 0b1111 => breakpoint on addr
6098 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6100 int bas = extract64(bcr, 5, 4);
6101 addr = sextract64(bvr, 0, 49) & ~3ULL;
6110 case 2: /* unlinked context ID match */
6111 case 8: /* unlinked VMID match (reserved if no EL2) */
6112 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6113 qemu_log_mask(LOG_UNIMP,
6114 "arm: unlinked context breakpoint types not implemented\n");
6116 case 9: /* linked VMID match (reserved if no EL2) */
6117 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6118 case 3: /* linked context ID match */
6120 /* We must generate no events for Linked context matches (unless
6121 * they are linked to by some other bp/wp, which is handled in
6122 * updates for the linking bp/wp). We choose to also generate no events
6123 * for reserved values.
6128 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6131 void hw_breakpoint_update_all(ARMCPU *cpu)
6134 CPUARMState *env = &cpu->env;
6136 /* Completely clear out existing QEMU breakpoints and our array, to
6137 * avoid possible stale entries following migration load.
6139 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6140 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6142 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6143 hw_breakpoint_update(cpu, i);
6147 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6150 ARMCPU *cpu = env_archcpu(env);
6153 raw_write(env, ri, value);
6154 hw_breakpoint_update(cpu, i);
6157 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6160 ARMCPU *cpu = env_archcpu(env);
6163 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6166 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6167 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6169 raw_write(env, ri, value);
6170 hw_breakpoint_update(cpu, i);
6173 static void define_debug_regs(ARMCPU *cpu)
6175 /* Define v7 and v8 architectural debug registers.
6176 * These are just dummy implementations for now.
6179 int wrps, brps, ctx_cmps;
6180 ARMCPRegInfo dbgdidr = {
6181 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
6182 .access = PL0_R, .accessfn = access_tda,
6183 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
6186 /* Note that all these register fields hold "number of Xs minus 1". */
6187 brps = extract32(cpu->dbgdidr, 24, 4);
6188 wrps = extract32(cpu->dbgdidr, 28, 4);
6189 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
6191 assert(ctx_cmps <= brps);
6193 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
6194 * of the debug registers such as number of breakpoints;
6195 * check that if they both exist then they agree.
6197 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
6198 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
6199 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
6200 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
6203 define_one_arm_cp_reg(cpu, &dbgdidr);
6204 define_arm_cp_regs(cpu, debug_cp_reginfo);
6206 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6207 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6210 for (i = 0; i < brps + 1; i++) {
6211 ARMCPRegInfo dbgregs[] = {
6212 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6213 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
6214 .access = PL1_RW, .accessfn = access_tda,
6215 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6216 .writefn = dbgbvr_write, .raw_writefn = raw_write
6218 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6219 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
6220 .access = PL1_RW, .accessfn = access_tda,
6221 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6222 .writefn = dbgbcr_write, .raw_writefn = raw_write
6226 define_arm_cp_regs(cpu, dbgregs);
6229 for (i = 0; i < wrps + 1; i++) {
6230 ARMCPRegInfo dbgregs[] = {
6231 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6232 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
6233 .access = PL1_RW, .accessfn = access_tda,
6234 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6235 .writefn = dbgwvr_write, .raw_writefn = raw_write
6237 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6238 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
6239 .access = PL1_RW, .accessfn = access_tda,
6240 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6241 .writefn = dbgwcr_write, .raw_writefn = raw_write
6245 define_arm_cp_regs(cpu, dbgregs);
6249 /* We don't know until after realize whether there's a GICv3
6250 * attached, and that is what registers the gicv3 sysregs.
6251 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6254 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6256 ARMCPU *cpu = env_archcpu(env);
6257 uint64_t pfr1 = cpu->id_pfr1;
6259 if (env->gicv3state) {
6265 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6267 ARMCPU *cpu = env_archcpu(env);
6268 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6270 if (env->gicv3state) {
6276 /* Shared logic between LORID and the rest of the LOR* registers.
6277 * Secure state has already been delt with.
6279 static CPAccessResult access_lor_ns(CPUARMState *env)
6281 int el = arm_current_el(env);
6283 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6284 return CP_ACCESS_TRAP_EL2;
6286 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6287 return CP_ACCESS_TRAP_EL3;
6289 return CP_ACCESS_OK;
6292 static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri,
6295 if (arm_is_secure_below_el3(env)) {
6296 /* Access ok in secure mode. */
6297 return CP_ACCESS_OK;
6299 return access_lor_ns(env);
6302 static CPAccessResult access_lor_other(CPUARMState *env,
6303 const ARMCPRegInfo *ri, bool isread)
6305 if (arm_is_secure_below_el3(env)) {
6306 /* Access denied in secure mode. */
6307 return CP_ACCESS_TRAP;
6309 return access_lor_ns(env);
6312 #ifdef TARGET_AARCH64
6313 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6316 int el = arm_current_el(env);
6319 arm_feature(env, ARM_FEATURE_EL2) &&
6320 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6321 return CP_ACCESS_TRAP_EL2;
6324 arm_feature(env, ARM_FEATURE_EL3) &&
6325 !(env->cp15.scr_el3 & SCR_APK)) {
6326 return CP_ACCESS_TRAP_EL3;
6328 return CP_ACCESS_OK;
6331 static const ARMCPRegInfo pauth_reginfo[] = {
6332 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6333 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6334 .access = PL1_RW, .accessfn = access_pauth,
6335 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6336 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6337 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6338 .access = PL1_RW, .accessfn = access_pauth,
6339 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6340 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6341 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6342 .access = PL1_RW, .accessfn = access_pauth,
6343 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6344 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6345 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6346 .access = PL1_RW, .accessfn = access_pauth,
6347 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6348 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6349 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6350 .access = PL1_RW, .accessfn = access_pauth,
6351 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6352 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6353 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6354 .access = PL1_RW, .accessfn = access_pauth,
6355 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6356 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6357 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6358 .access = PL1_RW, .accessfn = access_pauth,
6359 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6360 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6361 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6362 .access = PL1_RW, .accessfn = access_pauth,
6363 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6364 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6365 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6366 .access = PL1_RW, .accessfn = access_pauth,
6367 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6368 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6369 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6370 .access = PL1_RW, .accessfn = access_pauth,
6371 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6375 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6380 /* Success sets NZCV = 0000. */
6381 env->NF = env->CF = env->VF = 0, env->ZF = 1;
6383 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6385 * ??? Failed, for unknown reasons in the crypto subsystem.
6386 * The best we can do is log the reason and return the
6387 * timed-out indication to the guest. There is no reason
6388 * we know to expect this failure to be transitory, so the
6389 * guest may well hang retrying the operation.
6391 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6392 ri->name, error_get_pretty(err));
6395 env->ZF = 0; /* NZCF = 0100 */
6401 /* We do not support re-seeding, so the two registers operate the same. */
6402 static const ARMCPRegInfo rndr_reginfo[] = {
6403 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6404 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6405 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6406 .access = PL0_R, .readfn = rndr_readfn },
6407 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6408 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6409 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6410 .access = PL0_R, .readfn = rndr_readfn },
6414 #ifndef CONFIG_USER_ONLY
6415 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6418 ARMCPU *cpu = env_archcpu(env);
6419 /* CTR_EL0 System register -> DminLine, bits [19:16] */
6420 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6421 uint64_t vaddr_in = (uint64_t) value;
6422 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6424 int mem_idx = cpu_mmu_index(env, false);
6426 /* This won't be crossing page boundaries */
6427 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6433 /* RCU lock is already being held */
6434 mr = memory_region_from_host(haddr, &offset);
6437 memory_region_do_writeback(mr, offset, dline_size);
6442 static const ARMCPRegInfo dcpop_reg[] = {
6443 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6444 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6445 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6446 .accessfn = aa64_cacheop_access, .writefn = dccvap_writefn },
6450 static const ARMCPRegInfo dcpodp_reg[] = {
6451 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6452 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6453 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6454 .accessfn = aa64_cacheop_access, .writefn = dccvap_writefn },
6457 #endif /*CONFIG_USER_ONLY*/
6461 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
6464 int el = arm_current_el(env);
6467 uint64_t sctlr = arm_sctlr(env, el);
6468 if (!(sctlr & SCTLR_EnRCTX)) {
6469 return CP_ACCESS_TRAP;
6471 } else if (el == 1) {
6472 uint64_t hcr = arm_hcr_el2_eff(env);
6474 return CP_ACCESS_TRAP_EL2;
6477 return CP_ACCESS_OK;
6480 static const ARMCPRegInfo predinv_reginfo[] = {
6481 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
6482 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
6483 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6484 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
6485 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
6486 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6487 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
6488 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
6489 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6491 * Note the AArch32 opcodes have a different OPC1.
6493 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
6494 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
6495 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6496 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
6497 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
6498 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6499 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
6500 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
6501 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
6505 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
6508 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
6509 return CP_ACCESS_TRAP_EL2;
6512 return CP_ACCESS_OK;
6515 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
6518 if (arm_feature(env, ARM_FEATURE_V8)) {
6519 return access_aa64_tid3(env, ri, isread);
6522 return CP_ACCESS_OK;
6525 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
6528 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
6529 return CP_ACCESS_TRAP_EL2;
6532 return CP_ACCESS_OK;
6535 static const ARMCPRegInfo jazelle_regs[] = {
6537 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
6538 .access = PL1_R, .accessfn = access_jazelle,
6539 .type = ARM_CP_CONST, .resetvalue = 0 },
6541 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
6542 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6544 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
6545 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6549 static const ARMCPRegInfo vhe_reginfo[] = {
6550 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
6551 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
6553 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
6554 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
6555 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
6556 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
6557 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
6558 #ifndef CONFIG_USER_ONLY
6559 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6560 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
6562 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
6563 .type = ARM_CP_IO, .access = PL2_RW,
6564 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
6565 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6566 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
6567 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6568 .resetfn = gt_hv_timer_reset,
6569 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
6570 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6572 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
6574 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
6575 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
6576 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
6577 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
6578 .type = ARM_CP_IO | ARM_CP_ALIAS,
6579 .access = PL2_RW, .accessfn = e2h_access,
6580 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
6581 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
6582 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
6583 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
6584 .type = ARM_CP_IO | ARM_CP_ALIAS,
6585 .access = PL2_RW, .accessfn = e2h_access,
6586 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
6587 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
6588 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
6589 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
6590 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
6591 .access = PL2_RW, .accessfn = e2h_access,
6592 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
6593 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
6594 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
6595 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
6596 .access = PL2_RW, .accessfn = e2h_access,
6597 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
6598 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
6599 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
6600 .type = ARM_CP_IO | ARM_CP_ALIAS,
6601 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
6602 .access = PL2_RW, .accessfn = e2h_access,
6603 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
6604 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
6605 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
6606 .type = ARM_CP_IO | ARM_CP_ALIAS,
6607 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
6608 .access = PL2_RW, .accessfn = e2h_access,
6609 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
6614 void register_cp_regs_for_features(ARMCPU *cpu)
6616 /* Register all the coprocessor registers based on feature bits */
6617 CPUARMState *env = &cpu->env;
6618 if (arm_feature(env, ARM_FEATURE_M)) {
6619 /* M profile has no coprocessor registers */
6623 define_arm_cp_regs(cpu, cp_reginfo);
6624 if (!arm_feature(env, ARM_FEATURE_V8)) {
6625 /* Must go early as it is full of wildcards that may be
6626 * overridden by later definitions.
6628 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
6631 if (arm_feature(env, ARM_FEATURE_V6)) {
6632 /* The ID registers all have impdef reset values */
6633 ARMCPRegInfo v6_idregs[] = {
6634 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
6635 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
6636 .access = PL1_R, .type = ARM_CP_CONST,
6637 .accessfn = access_aa32_tid3,
6638 .resetvalue = cpu->id_pfr0 },
6639 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
6640 * the value of the GIC field until after we define these regs.
6642 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
6643 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
6644 .access = PL1_R, .type = ARM_CP_NO_RAW,
6645 .accessfn = access_aa32_tid3,
6646 .readfn = id_pfr1_read,
6647 .writefn = arm_cp_write_ignore },
6648 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
6649 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
6650 .access = PL1_R, .type = ARM_CP_CONST,
6651 .accessfn = access_aa32_tid3,
6652 .resetvalue = cpu->id_dfr0 },
6653 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
6654 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
6655 .access = PL1_R, .type = ARM_CP_CONST,
6656 .accessfn = access_aa32_tid3,
6657 .resetvalue = cpu->id_afr0 },
6658 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
6659 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
6660 .access = PL1_R, .type = ARM_CP_CONST,
6661 .accessfn = access_aa32_tid3,
6662 .resetvalue = cpu->id_mmfr0 },
6663 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
6664 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
6665 .access = PL1_R, .type = ARM_CP_CONST,
6666 .accessfn = access_aa32_tid3,
6667 .resetvalue = cpu->id_mmfr1 },
6668 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
6669 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
6670 .access = PL1_R, .type = ARM_CP_CONST,
6671 .accessfn = access_aa32_tid3,
6672 .resetvalue = cpu->id_mmfr2 },
6673 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
6674 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
6675 .access = PL1_R, .type = ARM_CP_CONST,
6676 .accessfn = access_aa32_tid3,
6677 .resetvalue = cpu->id_mmfr3 },
6678 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
6679 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6680 .access = PL1_R, .type = ARM_CP_CONST,
6681 .accessfn = access_aa32_tid3,
6682 .resetvalue = cpu->isar.id_isar0 },
6683 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
6684 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
6685 .access = PL1_R, .type = ARM_CP_CONST,
6686 .accessfn = access_aa32_tid3,
6687 .resetvalue = cpu->isar.id_isar1 },
6688 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
6689 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
6690 .access = PL1_R, .type = ARM_CP_CONST,
6691 .accessfn = access_aa32_tid3,
6692 .resetvalue = cpu->isar.id_isar2 },
6693 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
6694 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
6695 .access = PL1_R, .type = ARM_CP_CONST,
6696 .accessfn = access_aa32_tid3,
6697 .resetvalue = cpu->isar.id_isar3 },
6698 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
6699 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
6700 .access = PL1_R, .type = ARM_CP_CONST,
6701 .accessfn = access_aa32_tid3,
6702 .resetvalue = cpu->isar.id_isar4 },
6703 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
6704 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
6705 .access = PL1_R, .type = ARM_CP_CONST,
6706 .accessfn = access_aa32_tid3,
6707 .resetvalue = cpu->isar.id_isar5 },
6708 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
6709 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
6710 .access = PL1_R, .type = ARM_CP_CONST,
6711 .accessfn = access_aa32_tid3,
6712 .resetvalue = cpu->id_mmfr4 },
6713 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
6714 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
6715 .access = PL1_R, .type = ARM_CP_CONST,
6716 .accessfn = access_aa32_tid3,
6717 .resetvalue = cpu->isar.id_isar6 },
6720 define_arm_cp_regs(cpu, v6_idregs);
6721 define_arm_cp_regs(cpu, v6_cp_reginfo);
6723 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
6725 if (arm_feature(env, ARM_FEATURE_V6K)) {
6726 define_arm_cp_regs(cpu, v6k_cp_reginfo);
6728 if (arm_feature(env, ARM_FEATURE_V7MP) &&
6729 !arm_feature(env, ARM_FEATURE_PMSA)) {
6730 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
6732 if (arm_feature(env, ARM_FEATURE_V7VE)) {
6733 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
6735 if (arm_feature(env, ARM_FEATURE_V7)) {
6736 /* v7 performance monitor control register: same implementor
6737 * field as main ID register, and we implement four counters in
6738 * addition to the cycle count register.
6740 unsigned int i, pmcrn = 4;
6741 ARMCPRegInfo pmcr = {
6742 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6744 .type = ARM_CP_IO | ARM_CP_ALIAS,
6745 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6746 .accessfn = pmreg_access, .writefn = pmcr_write,
6747 .raw_writefn = raw_write,
6749 ARMCPRegInfo pmcr64 = {
6750 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6751 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6752 .access = PL0_RW, .accessfn = pmreg_access,
6754 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6755 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT),
6756 .writefn = pmcr_write, .raw_writefn = raw_write,
6758 define_one_arm_cp_reg(cpu, &pmcr);
6759 define_one_arm_cp_reg(cpu, &pmcr64);
6760 for (i = 0; i < pmcrn; i++) {
6761 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6762 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6763 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6764 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6765 ARMCPRegInfo pmev_regs[] = {
6766 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6767 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6768 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6769 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6770 .accessfn = pmreg_access },
6771 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6772 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6773 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6775 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6776 .raw_readfn = pmevcntr_rawread,
6777 .raw_writefn = pmevcntr_rawwrite },
6778 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6779 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6780 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6781 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6782 .accessfn = pmreg_access },
6783 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6784 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6785 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6787 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6788 .raw_writefn = pmevtyper_rawwrite },
6791 define_arm_cp_regs(cpu, pmev_regs);
6792 g_free(pmevcntr_name);
6793 g_free(pmevcntr_el0_name);
6794 g_free(pmevtyper_name);
6795 g_free(pmevtyper_el0_name);
6797 ARMCPRegInfo clidr = {
6798 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
6799 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
6800 .access = PL1_R, .type = ARM_CP_CONST,
6801 .accessfn = access_aa64_tid2,
6802 .resetvalue = cpu->clidr
6804 define_one_arm_cp_reg(cpu, &clidr);
6805 define_arm_cp_regs(cpu, v7_cp_reginfo);
6806 define_debug_regs(cpu);
6808 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
6810 if (FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) >= 4 &&
6811 FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) != 0xf) {
6812 ARMCPRegInfo v81_pmu_regs[] = {
6813 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6814 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6815 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6816 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6817 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6818 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6819 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6820 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6823 define_arm_cp_regs(cpu, v81_pmu_regs);
6825 if (arm_feature(env, ARM_FEATURE_V8)) {
6826 /* AArch64 ID registers, which all have impdef reset values.
6827 * Note that within the ID register ranges the unused slots
6828 * must all RAZ, not UNDEF; future architecture versions may
6829 * define new registers here.
6831 ARMCPRegInfo v8_idregs[] = {
6832 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't
6833 * know the right value for the GIC field until after we
6834 * define these regs.
6836 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
6837 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
6838 .access = PL1_R, .type = ARM_CP_NO_RAW,
6839 .accessfn = access_aa64_tid3,
6840 .readfn = id_aa64pfr0_read,
6841 .writefn = arm_cp_write_ignore },
6842 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
6843 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
6844 .access = PL1_R, .type = ARM_CP_CONST,
6845 .accessfn = access_aa64_tid3,
6846 .resetvalue = cpu->isar.id_aa64pfr1},
6847 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6848 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
6849 .access = PL1_R, .type = ARM_CP_CONST,
6850 .accessfn = access_aa64_tid3,
6852 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6853 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
6854 .access = PL1_R, .type = ARM_CP_CONST,
6855 .accessfn = access_aa64_tid3,
6857 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
6858 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
6859 .access = PL1_R, .type = ARM_CP_CONST,
6860 .accessfn = access_aa64_tid3,
6861 /* At present, only SVEver == 0 is defined anyway. */
6863 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6864 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
6865 .access = PL1_R, .type = ARM_CP_CONST,
6866 .accessfn = access_aa64_tid3,
6868 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6869 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
6870 .access = PL1_R, .type = ARM_CP_CONST,
6871 .accessfn = access_aa64_tid3,
6873 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6874 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
6875 .access = PL1_R, .type = ARM_CP_CONST,
6876 .accessfn = access_aa64_tid3,
6878 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
6879 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
6880 .access = PL1_R, .type = ARM_CP_CONST,
6881 .accessfn = access_aa64_tid3,
6882 .resetvalue = cpu->id_aa64dfr0 },
6883 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
6884 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
6885 .access = PL1_R, .type = ARM_CP_CONST,
6886 .accessfn = access_aa64_tid3,
6887 .resetvalue = cpu->id_aa64dfr1 },
6888 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6889 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
6890 .access = PL1_R, .type = ARM_CP_CONST,
6891 .accessfn = access_aa64_tid3,
6893 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6894 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
6895 .access = PL1_R, .type = ARM_CP_CONST,
6896 .accessfn = access_aa64_tid3,
6898 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
6899 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
6900 .access = PL1_R, .type = ARM_CP_CONST,
6901 .accessfn = access_aa64_tid3,
6902 .resetvalue = cpu->id_aa64afr0 },
6903 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
6904 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
6905 .access = PL1_R, .type = ARM_CP_CONST,
6906 .accessfn = access_aa64_tid3,
6907 .resetvalue = cpu->id_aa64afr1 },
6908 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6909 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
6910 .access = PL1_R, .type = ARM_CP_CONST,
6911 .accessfn = access_aa64_tid3,
6913 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6914 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
6915 .access = PL1_R, .type = ARM_CP_CONST,
6916 .accessfn = access_aa64_tid3,
6918 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
6919 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
6920 .access = PL1_R, .type = ARM_CP_CONST,
6921 .accessfn = access_aa64_tid3,
6922 .resetvalue = cpu->isar.id_aa64isar0 },
6923 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
6924 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
6925 .access = PL1_R, .type = ARM_CP_CONST,
6926 .accessfn = access_aa64_tid3,
6927 .resetvalue = cpu->isar.id_aa64isar1 },
6928 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6929 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
6930 .access = PL1_R, .type = ARM_CP_CONST,
6931 .accessfn = access_aa64_tid3,
6933 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6934 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
6935 .access = PL1_R, .type = ARM_CP_CONST,
6936 .accessfn = access_aa64_tid3,
6938 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6939 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
6940 .access = PL1_R, .type = ARM_CP_CONST,
6941 .accessfn = access_aa64_tid3,
6943 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6944 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
6945 .access = PL1_R, .type = ARM_CP_CONST,
6946 .accessfn = access_aa64_tid3,
6948 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6949 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
6950 .access = PL1_R, .type = ARM_CP_CONST,
6951 .accessfn = access_aa64_tid3,
6953 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6954 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
6955 .access = PL1_R, .type = ARM_CP_CONST,
6956 .accessfn = access_aa64_tid3,
6958 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
6959 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
6960 .access = PL1_R, .type = ARM_CP_CONST,
6961 .accessfn = access_aa64_tid3,
6962 .resetvalue = cpu->isar.id_aa64mmfr0 },
6963 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
6964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
6965 .access = PL1_R, .type = ARM_CP_CONST,
6966 .accessfn = access_aa64_tid3,
6967 .resetvalue = cpu->isar.id_aa64mmfr1 },
6968 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6969 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
6970 .access = PL1_R, .type = ARM_CP_CONST,
6971 .accessfn = access_aa64_tid3,
6973 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6974 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
6975 .access = PL1_R, .type = ARM_CP_CONST,
6976 .accessfn = access_aa64_tid3,
6978 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6979 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
6980 .access = PL1_R, .type = ARM_CP_CONST,
6981 .accessfn = access_aa64_tid3,
6983 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
6985 .access = PL1_R, .type = ARM_CP_CONST,
6986 .accessfn = access_aa64_tid3,
6988 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6989 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
6990 .access = PL1_R, .type = ARM_CP_CONST,
6991 .accessfn = access_aa64_tid3,
6993 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
6994 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
6995 .access = PL1_R, .type = ARM_CP_CONST,
6996 .accessfn = access_aa64_tid3,
6998 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
6999 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7000 .access = PL1_R, .type = ARM_CP_CONST,
7001 .accessfn = access_aa64_tid3,
7002 .resetvalue = cpu->isar.mvfr0 },
7003 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7005 .access = PL1_R, .type = ARM_CP_CONST,
7006 .accessfn = access_aa64_tid3,
7007 .resetvalue = cpu->isar.mvfr1 },
7008 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7009 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7010 .access = PL1_R, .type = ARM_CP_CONST,
7011 .accessfn = access_aa64_tid3,
7012 .resetvalue = cpu->isar.mvfr2 },
7013 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7014 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7015 .access = PL1_R, .type = ARM_CP_CONST,
7016 .accessfn = access_aa64_tid3,
7018 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7019 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7020 .access = PL1_R, .type = ARM_CP_CONST,
7021 .accessfn = access_aa64_tid3,
7023 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7025 .access = PL1_R, .type = ARM_CP_CONST,
7026 .accessfn = access_aa64_tid3,
7028 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7029 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7030 .access = PL1_R, .type = ARM_CP_CONST,
7031 .accessfn = access_aa64_tid3,
7033 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7034 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7035 .access = PL1_R, .type = ARM_CP_CONST,
7036 .accessfn = access_aa64_tid3,
7038 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7039 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7040 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7041 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7042 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7043 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7044 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7045 .resetvalue = cpu->pmceid0 },
7046 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7047 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7048 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7049 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7050 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7051 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7052 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7053 .resetvalue = cpu->pmceid1 },
7056 #ifdef CONFIG_USER_ONLY
7057 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7058 { .name = "ID_AA64PFR0_EL1",
7059 .exported_bits = 0x000f000f00ff0000,
7060 .fixed_bits = 0x0000000000000011 },
7061 { .name = "ID_AA64PFR1_EL1",
7062 .exported_bits = 0x00000000000000f0 },
7063 { .name = "ID_AA64PFR*_EL1_RESERVED",
7065 { .name = "ID_AA64ZFR0_EL1" },
7066 { .name = "ID_AA64MMFR0_EL1",
7067 .fixed_bits = 0x00000000ff000000 },
7068 { .name = "ID_AA64MMFR1_EL1" },
7069 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7071 { .name = "ID_AA64DFR0_EL1",
7072 .fixed_bits = 0x0000000000000006 },
7073 { .name = "ID_AA64DFR1_EL1" },
7074 { .name = "ID_AA64DFR*_EL1_RESERVED",
7076 { .name = "ID_AA64AFR*",
7078 { .name = "ID_AA64ISAR0_EL1",
7079 .exported_bits = 0x00fffffff0fffff0 },
7080 { .name = "ID_AA64ISAR1_EL1",
7081 .exported_bits = 0x000000f0ffffffff },
7082 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7084 REGUSERINFO_SENTINEL
7086 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7088 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7089 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7090 !arm_feature(env, ARM_FEATURE_EL2)) {
7091 ARMCPRegInfo rvbar = {
7092 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7093 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7094 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
7096 define_one_arm_cp_reg(cpu, &rvbar);
7098 define_arm_cp_regs(cpu, v8_idregs);
7099 define_arm_cp_regs(cpu, v8_cp_reginfo);
7101 if (arm_feature(env, ARM_FEATURE_EL2)) {
7102 uint64_t vmpidr_def = mpidr_read_val(env);
7103 ARMCPRegInfo vpidr_regs[] = {
7104 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7105 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7106 .access = PL2_RW, .accessfn = access_el3_aa32ns,
7107 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7108 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
7109 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7110 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7111 .access = PL2_RW, .resetvalue = cpu->midr,
7112 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7113 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7114 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7115 .access = PL2_RW, .accessfn = access_el3_aa32ns,
7116 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7117 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
7118 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7119 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7121 .resetvalue = vmpidr_def,
7122 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
7125 define_arm_cp_regs(cpu, vpidr_regs);
7126 define_arm_cp_regs(cpu, el2_cp_reginfo);
7127 if (arm_feature(env, ARM_FEATURE_V8)) {
7128 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7130 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7131 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7132 ARMCPRegInfo rvbar = {
7133 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7134 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7135 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
7137 define_one_arm_cp_reg(cpu, &rvbar);
7140 /* If EL2 is missing but higher ELs are enabled, we need to
7141 * register the no_el2 reginfos.
7143 if (arm_feature(env, ARM_FEATURE_EL3)) {
7144 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
7145 * of MIDR_EL1 and MPIDR_EL1.
7147 ARMCPRegInfo vpidr_regs[] = {
7148 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7149 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7150 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
7151 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
7152 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7153 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7154 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7155 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
7156 .type = ARM_CP_NO_RAW,
7157 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
7160 define_arm_cp_regs(cpu, vpidr_regs);
7161 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
7162 if (arm_feature(env, ARM_FEATURE_V8)) {
7163 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
7167 if (arm_feature(env, ARM_FEATURE_EL3)) {
7168 define_arm_cp_regs(cpu, el3_cp_reginfo);
7169 ARMCPRegInfo el3_regs[] = {
7170 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7171 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7172 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
7173 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7174 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7176 .raw_writefn = raw_write, .writefn = sctlr_write,
7177 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7178 .resetvalue = cpu->reset_sctlr },
7182 define_arm_cp_regs(cpu, el3_regs);
7184 /* The behaviour of NSACR is sufficiently various that we don't
7185 * try to describe it in a single reginfo:
7186 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7187 * reads as constant 0xc00 from NS EL1 and NS EL2
7188 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7189 * if v7 without EL3, register doesn't exist
7190 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7192 if (arm_feature(env, ARM_FEATURE_EL3)) {
7193 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7194 ARMCPRegInfo nsacr = {
7195 .name = "NSACR", .type = ARM_CP_CONST,
7196 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7197 .access = PL1_RW, .accessfn = nsacr_access,
7200 define_one_arm_cp_reg(cpu, &nsacr);
7202 ARMCPRegInfo nsacr = {
7204 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7205 .access = PL3_RW | PL1_R,
7207 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7209 define_one_arm_cp_reg(cpu, &nsacr);
7212 if (arm_feature(env, ARM_FEATURE_V8)) {
7213 ARMCPRegInfo nsacr = {
7214 .name = "NSACR", .type = ARM_CP_CONST,
7215 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7219 define_one_arm_cp_reg(cpu, &nsacr);
7223 if (arm_feature(env, ARM_FEATURE_PMSA)) {
7224 if (arm_feature(env, ARM_FEATURE_V6)) {
7225 /* PMSAv6 not implemented */
7226 assert(arm_feature(env, ARM_FEATURE_V7));
7227 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7228 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7230 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7233 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7234 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
7235 /* TTCBR2 is introduced with ARMv8.2-A32HPD. */
7236 if (FIELD_EX32(cpu->id_mmfr4, ID_MMFR4, HPDS) != 0) {
7237 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7240 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7241 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7243 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7244 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7246 if (arm_feature(env, ARM_FEATURE_VAPA)) {
7247 define_arm_cp_regs(cpu, vapa_cp_reginfo);
7249 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7250 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7252 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7253 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7255 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7256 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7258 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7259 define_arm_cp_regs(cpu, omap_cp_reginfo);
7261 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7262 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7264 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7265 define_arm_cp_regs(cpu, xscale_cp_reginfo);
7267 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7268 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7270 if (arm_feature(env, ARM_FEATURE_LPAE)) {
7271 define_arm_cp_regs(cpu, lpae_cp_reginfo);
7273 if (cpu_isar_feature(jazelle, cpu)) {
7274 define_arm_cp_regs(cpu, jazelle_regs);
7276 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7277 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7278 * be read-only (ie write causes UNDEF exception).
7281 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7282 /* Pre-v8 MIDR space.
7283 * Note that the MIDR isn't a simple constant register because
7284 * of the TI925 behaviour where writes to another register can
7285 * cause the MIDR value to change.
7287 * Unimplemented registers in the c15 0 0 0 space default to
7288 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7289 * and friends override accordingly.
7292 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7293 .access = PL1_R, .resetvalue = cpu->midr,
7294 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
7295 .readfn = midr_read,
7296 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7297 .type = ARM_CP_OVERRIDE },
7298 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7300 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7301 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7303 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7304 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7306 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7307 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7309 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7310 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7312 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
7313 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7316 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
7317 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
7318 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
7319 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
7320 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7321 .readfn = midr_read },
7322 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
7323 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7324 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7325 .access = PL1_R, .resetvalue = cpu->midr },
7326 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7327 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
7328 .access = PL1_R, .resetvalue = cpu->midr },
7329 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
7330 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
7332 .accessfn = access_aa64_tid1,
7333 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
7336 ARMCPRegInfo id_cp_reginfo[] = {
7337 /* These are common to v8 and pre-v8 */
7339 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
7340 .access = PL1_R, .accessfn = ctr_el0_access,
7341 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7342 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
7343 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
7344 .access = PL0_R, .accessfn = ctr_el0_access,
7345 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7346 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
7348 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
7350 .accessfn = access_aa32_tid1,
7351 .type = ARM_CP_CONST, .resetvalue = 0 },
7354 /* TLBTR is specific to VMSA */
7355 ARMCPRegInfo id_tlbtr_reginfo = {
7357 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
7359 .accessfn = access_aa32_tid1,
7360 .type = ARM_CP_CONST, .resetvalue = 0,
7362 /* MPUIR is specific to PMSA V6+ */
7363 ARMCPRegInfo id_mpuir_reginfo = {
7365 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7366 .access = PL1_R, .type = ARM_CP_CONST,
7367 .resetvalue = cpu->pmsav7_dregion << 8
7369 ARMCPRegInfo crn0_wi_reginfo = {
7370 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
7371 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
7372 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
7374 #ifdef CONFIG_USER_ONLY
7375 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
7376 { .name = "MIDR_EL1",
7377 .exported_bits = 0x00000000ffffffff },
7378 { .name = "REVIDR_EL1" },
7379 REGUSERINFO_SENTINEL
7381 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
7383 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
7384 arm_feature(env, ARM_FEATURE_STRONGARM)) {
7386 /* Register the blanket "writes ignored" value first to cover the
7387 * whole space. Then update the specific ID registers to allow write
7388 * access, so that they ignore writes rather than causing them to
7391 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
7392 for (r = id_pre_v8_midr_cp_reginfo;
7393 r->type != ARM_CP_SENTINEL; r++) {
7396 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
7399 id_mpuir_reginfo.access = PL1_RW;
7400 id_tlbtr_reginfo.access = PL1_RW;
7402 if (arm_feature(env, ARM_FEATURE_V8)) {
7403 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
7405 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
7407 define_arm_cp_regs(cpu, id_cp_reginfo);
7408 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
7409 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
7410 } else if (arm_feature(env, ARM_FEATURE_V7)) {
7411 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
7415 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
7416 ARMCPRegInfo mpidr_cp_reginfo[] = {
7417 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
7418 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7419 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
7422 #ifdef CONFIG_USER_ONLY
7423 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
7424 { .name = "MPIDR_EL1",
7425 .fixed_bits = 0x0000000080000000 },
7426 REGUSERINFO_SENTINEL
7428 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
7430 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
7433 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
7434 ARMCPRegInfo auxcr_reginfo[] = {
7435 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
7436 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
7437 .access = PL1_RW, .type = ARM_CP_CONST,
7438 .resetvalue = cpu->reset_auxcr },
7439 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
7440 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
7441 .access = PL2_RW, .type = ARM_CP_CONST,
7443 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
7444 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
7445 .access = PL3_RW, .type = ARM_CP_CONST,
7449 define_arm_cp_regs(cpu, auxcr_reginfo);
7450 if (arm_feature(env, ARM_FEATURE_V8)) {
7451 /* HACTLR2 maps to ACTLR_EL2[63:32] and is not in ARMv7 */
7452 ARMCPRegInfo hactlr2_reginfo = {
7453 .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7454 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7455 .access = PL2_RW, .type = ARM_CP_CONST,
7458 define_one_arm_cp_reg(cpu, &hactlr2_reginfo);
7462 if (arm_feature(env, ARM_FEATURE_CBAR)) {
7464 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
7465 * There are two flavours:
7466 * (1) older 32-bit only cores have a simple 32-bit CBAR
7467 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
7468 * 32-bit register visible to AArch32 at a different encoding
7469 * to the "flavour 1" register and with the bits rearranged to
7470 * be able to squash a 64-bit address into the 32-bit view.
7471 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
7472 * in future if we support AArch32-only configs of some of the
7473 * AArch64 cores we might need to add a specific feature flag
7474 * to indicate cores with "flavour 2" CBAR.
7476 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7477 /* 32 bit view is [31:18] 0...0 [43:32]. */
7478 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
7479 | extract64(cpu->reset_cbar, 32, 12);
7480 ARMCPRegInfo cbar_reginfo[] = {
7482 .type = ARM_CP_CONST,
7483 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
7484 .access = PL1_R, .resetvalue = cbar32 },
7485 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
7486 .type = ARM_CP_CONST,
7487 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
7488 .access = PL1_R, .resetvalue = cpu->reset_cbar },
7491 /* We don't implement a r/w 64 bit CBAR currently */
7492 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
7493 define_arm_cp_regs(cpu, cbar_reginfo);
7495 ARMCPRegInfo cbar = {
7497 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
7498 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
7499 .fieldoffset = offsetof(CPUARMState,
7500 cp15.c15_config_base_address)
7502 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
7503 cbar.access = PL1_R;
7504 cbar.fieldoffset = 0;
7505 cbar.type = ARM_CP_CONST;
7507 define_one_arm_cp_reg(cpu, &cbar);
7511 if (arm_feature(env, ARM_FEATURE_VBAR)) {
7512 ARMCPRegInfo vbar_cp_reginfo[] = {
7513 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
7514 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
7515 .access = PL1_RW, .writefn = vbar_write,
7516 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
7517 offsetof(CPUARMState, cp15.vbar_ns) },
7521 define_arm_cp_regs(cpu, vbar_cp_reginfo);
7524 /* Generic registers whose values depend on the implementation */
7526 ARMCPRegInfo sctlr = {
7527 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
7528 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
7530 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
7531 offsetof(CPUARMState, cp15.sctlr_ns) },
7532 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
7533 .raw_writefn = raw_write,
7535 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7536 /* Normally we would always end the TB on an SCTLR write, but Linux
7537 * arch/arm/mach-pxa/sleep.S expects two instructions following
7538 * an MMU enable to execute from cache. Imitate this behaviour.
7540 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
7542 define_one_arm_cp_reg(cpu, &sctlr);
7545 if (cpu_isar_feature(aa64_lor, cpu)) {
7547 * A trivial implementation of ARMv8.1-LOR leaves all of these
7548 * registers fixed at 0, which indicates that there are zero
7549 * supported Limited Ordering regions.
7551 static const ARMCPRegInfo lor_reginfo[] = {
7552 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7553 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7554 .access = PL1_RW, .accessfn = access_lor_other,
7555 .type = ARM_CP_CONST, .resetvalue = 0 },
7556 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7557 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7558 .access = PL1_RW, .accessfn = access_lor_other,
7559 .type = ARM_CP_CONST, .resetvalue = 0 },
7560 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7561 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7562 .access = PL1_RW, .accessfn = access_lor_other,
7563 .type = ARM_CP_CONST, .resetvalue = 0 },
7564 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7565 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7566 .access = PL1_RW, .accessfn = access_lor_other,
7567 .type = ARM_CP_CONST, .resetvalue = 0 },
7568 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7569 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7570 .access = PL1_R, .accessfn = access_lorid,
7571 .type = ARM_CP_CONST, .resetvalue = 0 },
7574 define_arm_cp_regs(cpu, lor_reginfo);
7577 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
7578 define_arm_cp_regs(cpu, vhe_reginfo);
7581 if (cpu_isar_feature(aa64_sve, cpu)) {
7582 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
7583 if (arm_feature(env, ARM_FEATURE_EL2)) {
7584 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
7586 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
7588 if (arm_feature(env, ARM_FEATURE_EL3)) {
7589 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
7593 #ifdef TARGET_AARCH64
7594 if (cpu_isar_feature(aa64_pauth, cpu)) {
7595 define_arm_cp_regs(cpu, pauth_reginfo);
7597 if (cpu_isar_feature(aa64_rndr, cpu)) {
7598 define_arm_cp_regs(cpu, rndr_reginfo);
7600 #ifndef CONFIG_USER_ONLY
7601 /* Data Cache clean instructions up to PoP */
7602 if (cpu_isar_feature(aa64_dcpop, cpu)) {
7603 define_one_arm_cp_reg(cpu, dcpop_reg);
7605 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
7606 define_one_arm_cp_reg(cpu, dcpodp_reg);
7609 #endif /*CONFIG_USER_ONLY*/
7613 * While all v8.0 cpus support aarch64, QEMU does have configurations
7614 * that do not set ID_AA64ISAR1, e.g. user-only qemu-arm -cpu max,
7615 * which will set ID_ISAR6.
7617 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)
7618 ? cpu_isar_feature(aa64_predinv, cpu)
7619 : cpu_isar_feature(aa32_predinv, cpu)) {
7620 define_arm_cp_regs(cpu, predinv_reginfo);
7623 #ifndef CONFIG_USER_ONLY
7625 * Register redirections and aliases must be done last,
7626 * after the registers from the other extensions have been defined.
7628 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
7629 define_arm_vh_e2h_redirects_aliases(cpu);
7634 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
7636 CPUState *cs = CPU(cpu);
7637 CPUARMState *env = &cpu->env;
7639 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7640 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
7641 aarch64_fpu_gdb_set_reg,
7642 34, "aarch64-fpu.xml", 0);
7643 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
7644 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
7645 51, "arm-neon.xml", 0);
7646 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
7647 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
7648 35, "arm-vfp3.xml", 0);
7649 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
7650 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
7651 19, "arm-vfp.xml", 0);
7653 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
7654 arm_gen_dynamic_xml(cs),
7655 "system-registers.xml", 0);
7658 /* Sort alphabetically by type name, except for "any". */
7659 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
7661 ObjectClass *class_a = (ObjectClass *)a;
7662 ObjectClass *class_b = (ObjectClass *)b;
7663 const char *name_a, *name_b;
7665 name_a = object_class_get_name(class_a);
7666 name_b = object_class_get_name(class_b);
7667 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
7669 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
7672 return strcmp(name_a, name_b);
7676 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
7678 ObjectClass *oc = data;
7679 const char *typename;
7682 typename = object_class_get_name(oc);
7683 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
7684 qemu_printf(" %s\n", name);
7688 void arm_cpu_list(void)
7692 list = object_class_get_list(TYPE_ARM_CPU, false);
7693 list = g_slist_sort(list, arm_cpu_list_compare);
7694 qemu_printf("Available CPUs:\n");
7695 g_slist_foreach(list, arm_cpu_list_entry, NULL);
7699 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
7701 ObjectClass *oc = data;
7702 CpuDefinitionInfoList **cpu_list = user_data;
7703 CpuDefinitionInfoList *entry;
7704 CpuDefinitionInfo *info;
7705 const char *typename;
7707 typename = object_class_get_name(oc);
7708 info = g_malloc0(sizeof(*info));
7709 info->name = g_strndup(typename,
7710 strlen(typename) - strlen("-" TYPE_ARM_CPU));
7711 info->q_typename = g_strdup(typename);
7713 entry = g_malloc0(sizeof(*entry));
7714 entry->value = info;
7715 entry->next = *cpu_list;
7719 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
7721 CpuDefinitionInfoList *cpu_list = NULL;
7724 list = object_class_get_list(TYPE_ARM_CPU, false);
7725 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
7731 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
7732 void *opaque, int state, int secstate,
7733 int crm, int opc1, int opc2,
7736 /* Private utility function for define_one_arm_cp_reg_with_opaque():
7737 * add a single reginfo struct to the hash table.
7739 uint32_t *key = g_new(uint32_t, 1);
7740 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
7741 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
7742 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
7744 r2->name = g_strdup(name);
7745 /* Reset the secure state to the specific incoming state. This is
7746 * necessary as the register may have been defined with both states.
7748 r2->secure = secstate;
7750 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
7751 /* Register is banked (using both entries in array).
7752 * Overwriting fieldoffset as the array is only used to define
7753 * banked registers but later only fieldoffset is used.
7755 r2->fieldoffset = r->bank_fieldoffsets[ns];
7758 if (state == ARM_CP_STATE_AA32) {
7759 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
7760 /* If the register is banked then we don't need to migrate or
7761 * reset the 32-bit instance in certain cases:
7763 * 1) If the register has both 32-bit and 64-bit instances then we
7764 * can count on the 64-bit instance taking care of the
7766 * 2) If ARMv8 is enabled then we can count on a 64-bit version
7767 * taking care of the secure bank. This requires that separate
7768 * 32 and 64-bit definitions are provided.
7770 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
7771 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7772 r2->type |= ARM_CP_ALIAS;
7774 } else if ((secstate != r->secure) && !ns) {
7775 /* The register is not banked so we only want to allow migration of
7776 * the non-secure instance.
7778 r2->type |= ARM_CP_ALIAS;
7781 if (r->state == ARM_CP_STATE_BOTH) {
7782 /* We assume it is a cp15 register if the .cp field is left unset.
7788 #ifdef HOST_WORDS_BIGENDIAN
7789 if (r2->fieldoffset) {
7790 r2->fieldoffset += sizeof(uint32_t);
7795 if (state == ARM_CP_STATE_AA64) {
7796 /* To allow abbreviation of ARMCPRegInfo
7797 * definitions, we treat cp == 0 as equivalent to
7798 * the value for "standard guest-visible sysreg".
7799 * STATE_BOTH definitions are also always "standard
7800 * sysreg" in their AArch64 view (the .cp value may
7801 * be non-zero for the benefit of the AArch32 view).
7803 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
7804 r2->cp = CP_REG_ARM64_SYSREG_CP;
7806 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
7807 r2->opc0, opc1, opc2);
7809 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
7812 r2->opaque = opaque;
7814 /* reginfo passed to helpers is correct for the actual access,
7815 * and is never ARM_CP_STATE_BOTH:
7818 /* Make sure reginfo passed to helpers for wildcarded regs
7819 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
7824 /* By convention, for wildcarded registers only the first
7825 * entry is used for migration; the others are marked as
7826 * ALIAS so we don't try to transfer the register
7827 * multiple times. Special registers (ie NOP/WFI) are
7828 * never migratable and not even raw-accessible.
7830 if ((r->type & ARM_CP_SPECIAL)) {
7831 r2->type |= ARM_CP_NO_RAW;
7833 if (((r->crm == CP_ANY) && crm != 0) ||
7834 ((r->opc1 == CP_ANY) && opc1 != 0) ||
7835 ((r->opc2 == CP_ANY) && opc2 != 0)) {
7836 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
7839 /* Check that raw accesses are either forbidden or handled. Note that
7840 * we can't assert this earlier because the setup of fieldoffset for
7841 * banked registers has to be done first.
7843 if (!(r2->type & ARM_CP_NO_RAW)) {
7844 assert(!raw_accessors_invalid(r2));
7847 /* Overriding of an existing definition must be explicitly
7850 if (!(r->type & ARM_CP_OVERRIDE)) {
7851 ARMCPRegInfo *oldreg;
7852 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
7853 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
7854 fprintf(stderr, "Register redefined: cp=%d %d bit "
7855 "crn=%d crm=%d opc1=%d opc2=%d, "
7856 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
7857 r2->crn, r2->crm, r2->opc1, r2->opc2,
7858 oldreg->name, r2->name);
7859 g_assert_not_reached();
7862 g_hash_table_insert(cpu->cp_regs, key, r2);
7866 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
7867 const ARMCPRegInfo *r, void *opaque)
7869 /* Define implementations of coprocessor registers.
7870 * We store these in a hashtable because typically
7871 * there are less than 150 registers in a space which
7872 * is 16*16*16*8*8 = 262144 in size.
7873 * Wildcarding is supported for the crm, opc1 and opc2 fields.
7874 * If a register is defined twice then the second definition is
7875 * used, so this can be used to define some generic registers and
7876 * then override them with implementation specific variations.
7877 * At least one of the original and the second definition should
7878 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
7879 * against accidental use.
7881 * The state field defines whether the register is to be
7882 * visible in the AArch32 or AArch64 execution state. If the
7883 * state is set to ARM_CP_STATE_BOTH then we synthesise a
7884 * reginfo structure for the AArch32 view, which sees the lower
7885 * 32 bits of the 64 bit register.
7887 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
7888 * be wildcarded. AArch64 registers are always considered to be 64
7889 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
7890 * the register, if any.
7892 int crm, opc1, opc2, state;
7893 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
7894 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
7895 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
7896 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
7897 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
7898 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
7899 /* 64 bit registers have only CRm and Opc1 fields */
7900 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
7901 /* op0 only exists in the AArch64 encodings */
7902 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
7903 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
7904 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
7905 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
7906 * encodes a minimum access level for the register. We roll this
7907 * runtime check into our general permission check code, so check
7908 * here that the reginfo's specified permissions are strict enough
7909 * to encompass the generic architectural permission check.
7911 if (r->state != ARM_CP_STATE_AA32) {
7915 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
7916 mask = PL0U_R | PL1_RW;
7936 /* min_EL EL1, secure mode only (we don't check the latter) */
7940 /* broken reginfo with out-of-range opc1 */
7944 /* assert our permissions are not too lax (stricter is fine) */
7945 assert((r->access & ~mask) == 0);
7948 /* Check that the register definition has enough info to handle
7949 * reads and writes if they are permitted.
7951 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
7952 if (r->access & PL3_R) {
7953 assert((r->fieldoffset ||
7954 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
7957 if (r->access & PL3_W) {
7958 assert((r->fieldoffset ||
7959 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
7963 /* Bad type field probably means missing sentinel at end of reg list */
7964 assert(cptype_valid(r->type));
7965 for (crm = crmmin; crm <= crmmax; crm++) {
7966 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
7967 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
7968 for (state = ARM_CP_STATE_AA32;
7969 state <= ARM_CP_STATE_AA64; state++) {
7970 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
7973 if (state == ARM_CP_STATE_AA32) {
7974 /* Under AArch32 CP registers can be common
7975 * (same for secure and non-secure world) or banked.
7979 switch (r->secure) {
7980 case ARM_CP_SECSTATE_S:
7981 case ARM_CP_SECSTATE_NS:
7982 add_cpreg_to_hashtable(cpu, r, opaque, state,
7983 r->secure, crm, opc1, opc2,
7987 name = g_strdup_printf("%s_S", r->name);
7988 add_cpreg_to_hashtable(cpu, r, opaque, state,
7990 crm, opc1, opc2, name);
7992 add_cpreg_to_hashtable(cpu, r, opaque, state,
7994 crm, opc1, opc2, r->name);
7998 /* AArch64 registers get mapped to non-secure instance
8000 add_cpreg_to_hashtable(cpu, r, opaque, state,
8002 crm, opc1, opc2, r->name);
8010 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
8011 const ARMCPRegInfo *regs, void *opaque)
8013 /* Define a whole list of registers */
8014 const ARMCPRegInfo *r;
8015 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8016 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
8021 * Modify ARMCPRegInfo for access from userspace.
8023 * This is a data driven modification directed by
8024 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8025 * user-space cannot alter any values and dynamic values pertaining to
8026 * execution state are hidden from user space view anyway.
8028 void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
8030 const ARMCPRegUserSpaceInfo *m;
8033 for (m = mods; m->name; m++) {
8034 GPatternSpec *pat = NULL;
8036 pat = g_pattern_spec_new(m->name);
8038 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8039 if (pat && g_pattern_match_string(pat, r->name)) {
8040 r->type = ARM_CP_CONST;
8044 } else if (strcmp(r->name, m->name) == 0) {
8045 r->type = ARM_CP_CONST;
8047 r->resetvalue &= m->exported_bits;
8048 r->resetvalue |= m->fixed_bits;
8053 g_pattern_spec_free(pat);
8058 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8060 return g_hash_table_lookup(cpregs, &encoded_cp);
8063 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8066 /* Helper coprocessor write function for write-ignore registers */
8069 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8071 /* Helper coprocessor write function for read-as-zero registers */
8075 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8077 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8080 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
8082 /* Return true if it is not valid for us to switch to
8083 * this CPU mode (ie all the UNPREDICTABLE cases in
8084 * the ARM ARM CPSRWriteByInstr pseudocode).
8087 /* Changes to or from Hyp via MSR and CPS are illegal. */
8088 if (write_type == CPSRWriteByInstr &&
8089 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8090 mode == ARM_CPU_MODE_HYP)) {
8095 case ARM_CPU_MODE_USR:
8097 case ARM_CPU_MODE_SYS:
8098 case ARM_CPU_MODE_SVC:
8099 case ARM_CPU_MODE_ABT:
8100 case ARM_CPU_MODE_UND:
8101 case ARM_CPU_MODE_IRQ:
8102 case ARM_CPU_MODE_FIQ:
8103 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8104 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8106 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8107 * and CPS are treated as illegal mode changes.
8109 if (write_type == CPSRWriteByInstr &&
8110 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
8111 (arm_hcr_el2_eff(env) & HCR_TGE)) {
8115 case ARM_CPU_MODE_HYP:
8116 return !arm_feature(env, ARM_FEATURE_EL2)
8117 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env);
8118 case ARM_CPU_MODE_MON:
8119 return arm_current_el(env) < 3;
8125 uint32_t cpsr_read(CPUARMState *env)
8128 ZF = (env->ZF == 0);
8129 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
8130 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8131 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8132 | ((env->condexec_bits & 0xfc) << 8)
8133 | (env->GE << 16) | (env->daif & CPSR_AIF);
8136 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8137 CPSRWriteType write_type)
8139 uint32_t changed_daif;
8141 if (mask & CPSR_NZCV) {
8142 env->ZF = (~val) & CPSR_Z;
8144 env->CF = (val >> 29) & 1;
8145 env->VF = (val << 3) & 0x80000000;
8148 env->QF = ((val & CPSR_Q) != 0);
8150 env->thumb = ((val & CPSR_T) != 0);
8151 if (mask & CPSR_IT_0_1) {
8152 env->condexec_bits &= ~3;
8153 env->condexec_bits |= (val >> 25) & 3;
8155 if (mask & CPSR_IT_2_7) {
8156 env->condexec_bits &= 3;
8157 env->condexec_bits |= (val >> 8) & 0xfc;
8159 if (mask & CPSR_GE) {
8160 env->GE = (val >> 16) & 0xf;
8163 /* In a V7 implementation that includes the security extensions but does
8164 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8165 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8166 * bits respectively.
8168 * In a V8 implementation, it is permitted for privileged software to
8169 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8171 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
8172 arm_feature(env, ARM_FEATURE_EL3) &&
8173 !arm_feature(env, ARM_FEATURE_EL2) &&
8174 !arm_is_secure(env)) {
8176 changed_daif = (env->daif ^ val) & mask;
8178 if (changed_daif & CPSR_A) {
8179 /* Check to see if we are allowed to change the masking of async
8180 * abort exceptions from a non-secure state.
8182 if (!(env->cp15.scr_el3 & SCR_AW)) {
8183 qemu_log_mask(LOG_GUEST_ERROR,
8184 "Ignoring attempt to switch CPSR_A flag from "
8185 "non-secure world with SCR.AW bit clear\n");
8190 if (changed_daif & CPSR_F) {
8191 /* Check to see if we are allowed to change the masking of FIQ
8192 * exceptions from a non-secure state.
8194 if (!(env->cp15.scr_el3 & SCR_FW)) {
8195 qemu_log_mask(LOG_GUEST_ERROR,
8196 "Ignoring attempt to switch CPSR_F flag from "
8197 "non-secure world with SCR.FW bit clear\n");
8201 /* Check whether non-maskable FIQ (NMFI) support is enabled.
8202 * If this bit is set software is not allowed to mask
8203 * FIQs, but is allowed to set CPSR_F to 0.
8205 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8207 qemu_log_mask(LOG_GUEST_ERROR,
8208 "Ignoring attempt to enable CPSR_F flag "
8209 "(non-maskable FIQ [NMFI] support enabled)\n");
8215 env->daif &= ~(CPSR_AIF & mask);
8216 env->daif |= val & CPSR_AIF & mask;
8218 if (write_type != CPSRWriteRaw &&
8219 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8220 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8221 /* Note that we can only get here in USR mode if this is a
8222 * gdb stub write; for this case we follow the architectural
8223 * behaviour for guest writes in USR mode of ignoring an attempt
8224 * to switch mode. (Those are caught by translate.c for writes
8225 * triggered by guest instructions.)
8228 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
8229 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8230 * v7, and has defined behaviour in v8:
8231 * + leave CPSR.M untouched
8232 * + allow changes to the other CPSR fields
8234 * For user changes via the GDB stub, we don't set PSTATE.IL,
8235 * as this would be unnecessarily harsh for a user error.
8238 if (write_type != CPSRWriteByGDBStub &&
8239 arm_feature(env, ARM_FEATURE_V8)) {
8243 qemu_log_mask(LOG_GUEST_ERROR,
8244 "Illegal AArch32 mode switch attempt from %s to %s\n",
8245 aarch32_mode_name(env->uncached_cpsr),
8246 aarch32_mode_name(val));
8248 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8249 write_type == CPSRWriteExceptionReturn ?
8250 "Exception return from AArch32" :
8251 "AArch32 mode switch from",
8252 aarch32_mode_name(env->uncached_cpsr),
8253 aarch32_mode_name(val), env->regs[15]);
8254 switch_mode(env, val & CPSR_M);
8257 mask &= ~CACHED_CPSR_BITS;
8258 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
8261 /* Sign/zero extend */
8262 uint32_t HELPER(sxtb16)(uint32_t x)
8265 res = (uint16_t)(int8_t)x;
8266 res |= (uint32_t)(int8_t)(x >> 16) << 16;
8270 uint32_t HELPER(uxtb16)(uint32_t x)
8273 res = (uint16_t)(uint8_t)x;
8274 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
8278 int32_t HELPER(sdiv)(int32_t num, int32_t den)
8282 if (num == INT_MIN && den == -1)
8287 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
8294 uint32_t HELPER(rbit)(uint32_t x)
8299 #ifdef CONFIG_USER_ONLY
8301 static void switch_mode(CPUARMState *env, int mode)
8303 ARMCPU *cpu = env_archcpu(env);
8305 if (mode != ARM_CPU_MODE_USR) {
8306 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
8310 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8311 uint32_t cur_el, bool secure)
8316 void aarch64_sync_64_to_32(CPUARMState *env)
8318 g_assert_not_reached();
8323 static void switch_mode(CPUARMState *env, int mode)
8328 old_mode = env->uncached_cpsr & CPSR_M;
8329 if (mode == old_mode)
8332 if (old_mode == ARM_CPU_MODE_FIQ) {
8333 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8334 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
8335 } else if (mode == ARM_CPU_MODE_FIQ) {
8336 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8337 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
8340 i = bank_number(old_mode);
8341 env->banked_r13[i] = env->regs[13];
8342 env->banked_spsr[i] = env->spsr;
8344 i = bank_number(mode);
8345 env->regs[13] = env->banked_r13[i];
8346 env->spsr = env->banked_spsr[i];
8348 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
8349 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
8352 /* Physical Interrupt Target EL Lookup Table
8354 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
8356 * The below multi-dimensional table is used for looking up the target
8357 * exception level given numerous condition criteria. Specifically, the
8358 * target EL is based on SCR and HCR routing controls as well as the
8359 * currently executing EL and secure state.
8362 * target_el_table[2][2][2][2][2][4]
8363 * | | | | | +--- Current EL
8364 * | | | | +------ Non-secure(0)/Secure(1)
8365 * | | | +--------- HCR mask override
8366 * | | +------------ SCR exec state control
8367 * | +--------------- SCR mask override
8368 * +------------------ 32-bit(0)/64-bit(1) EL3
8370 * The table values are as such:
8374 * The ARM ARM target EL table includes entries indicating that an "exception
8375 * is not taken". The two cases where this is applicable are:
8376 * 1) An exception is taken from EL3 but the SCR does not have the exception
8378 * 2) An exception is taken from EL2 but the HCR does not have the exception
8380 * In these two cases, the below table contain a target of EL1. This value is
8381 * returned as it is expected that the consumer of the table data will check
8382 * for "target EL >= current EL" to ensure the exception is not taken.
8386 * BIT IRQ IMO Non-secure Secure
8387 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
8389 static const int8_t target_el_table[2][2][2][2][2][4] = {
8390 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8391 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
8392 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8393 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
8394 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8395 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
8396 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8397 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
8398 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
8399 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
8400 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
8401 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
8402 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8403 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
8404 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8405 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
8409 * Determine the target EL for physical exceptions
8411 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8412 uint32_t cur_el, bool secure)
8414 CPUARMState *env = cs->env_ptr;
8419 /* Is the highest EL AArch64? */
8420 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
8423 if (arm_feature(env, ARM_FEATURE_EL3)) {
8424 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
8426 /* Either EL2 is the highest EL (and so the EL2 register width
8427 * is given by is64); or there is no EL2 or EL3, in which case
8428 * the value of 'rw' does not affect the table lookup anyway.
8433 hcr_el2 = arm_hcr_el2_eff(env);
8436 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
8437 hcr = hcr_el2 & HCR_IMO;
8440 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
8441 hcr = hcr_el2 & HCR_FMO;
8444 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
8445 hcr = hcr_el2 & HCR_AMO;
8450 * For these purposes, TGE and AMO/IMO/FMO both force the
8451 * interrupt to EL2. Fold TGE into the bit extracted above.
8453 hcr |= (hcr_el2 & HCR_TGE) != 0;
8455 /* Perform a table-lookup for the target EL given the current state */
8456 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
8458 assert(target_el > 0);
8463 void arm_log_exception(int idx)
8465 if (qemu_loglevel_mask(CPU_LOG_INT)) {
8466 const char *exc = NULL;
8467 static const char * const excnames[] = {
8468 [EXCP_UDEF] = "Undefined Instruction",
8470 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
8471 [EXCP_DATA_ABORT] = "Data Abort",
8474 [EXCP_BKPT] = "Breakpoint",
8475 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
8476 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
8477 [EXCP_HVC] = "Hypervisor Call",
8478 [EXCP_HYP_TRAP] = "Hypervisor Trap",
8479 [EXCP_SMC] = "Secure Monitor Call",
8480 [EXCP_VIRQ] = "Virtual IRQ",
8481 [EXCP_VFIQ] = "Virtual FIQ",
8482 [EXCP_SEMIHOST] = "Semihosting call",
8483 [EXCP_NOCP] = "v7M NOCP UsageFault",
8484 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
8485 [EXCP_STKOF] = "v8M STKOF UsageFault",
8486 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
8487 [EXCP_LSERR] = "v8M LSERR UsageFault",
8488 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
8491 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
8492 exc = excnames[idx];
8497 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
8502 * Function used to synchronize QEMU's AArch64 register set with AArch32
8503 * register set. This is necessary when switching between AArch32 and AArch64
8506 void aarch64_sync_32_to_64(CPUARMState *env)
8509 uint32_t mode = env->uncached_cpsr & CPSR_M;
8511 /* We can blanket copy R[0:7] to X[0:7] */
8512 for (i = 0; i < 8; i++) {
8513 env->xregs[i] = env->regs[i];
8517 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
8518 * Otherwise, they come from the banked user regs.
8520 if (mode == ARM_CPU_MODE_FIQ) {
8521 for (i = 8; i < 13; i++) {
8522 env->xregs[i] = env->usr_regs[i - 8];
8525 for (i = 8; i < 13; i++) {
8526 env->xregs[i] = env->regs[i];
8531 * Registers x13-x23 are the various mode SP and FP registers. Registers
8532 * r13 and r14 are only copied if we are in that mode, otherwise we copy
8533 * from the mode banked register.
8535 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
8536 env->xregs[13] = env->regs[13];
8537 env->xregs[14] = env->regs[14];
8539 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
8540 /* HYP is an exception in that it is copied from r14 */
8541 if (mode == ARM_CPU_MODE_HYP) {
8542 env->xregs[14] = env->regs[14];
8544 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
8548 if (mode == ARM_CPU_MODE_HYP) {
8549 env->xregs[15] = env->regs[13];
8551 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
8554 if (mode == ARM_CPU_MODE_IRQ) {
8555 env->xregs[16] = env->regs[14];
8556 env->xregs[17] = env->regs[13];
8558 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
8559 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
8562 if (mode == ARM_CPU_MODE_SVC) {
8563 env->xregs[18] = env->regs[14];
8564 env->xregs[19] = env->regs[13];
8566 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
8567 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
8570 if (mode == ARM_CPU_MODE_ABT) {
8571 env->xregs[20] = env->regs[14];
8572 env->xregs[21] = env->regs[13];
8574 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
8575 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
8578 if (mode == ARM_CPU_MODE_UND) {
8579 env->xregs[22] = env->regs[14];
8580 env->xregs[23] = env->regs[13];
8582 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
8583 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
8587 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
8588 * mode, then we can copy from r8-r14. Otherwise, we copy from the
8589 * FIQ bank for r8-r14.
8591 if (mode == ARM_CPU_MODE_FIQ) {
8592 for (i = 24; i < 31; i++) {
8593 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
8596 for (i = 24; i < 29; i++) {
8597 env->xregs[i] = env->fiq_regs[i - 24];
8599 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
8600 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
8603 env->pc = env->regs[15];
8607 * Function used to synchronize QEMU's AArch32 register set with AArch64
8608 * register set. This is necessary when switching between AArch32 and AArch64
8611 void aarch64_sync_64_to_32(CPUARMState *env)
8614 uint32_t mode = env->uncached_cpsr & CPSR_M;
8616 /* We can blanket copy X[0:7] to R[0:7] */
8617 for (i = 0; i < 8; i++) {
8618 env->regs[i] = env->xregs[i];
8622 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
8623 * Otherwise, we copy x8-x12 into the banked user regs.
8625 if (mode == ARM_CPU_MODE_FIQ) {
8626 for (i = 8; i < 13; i++) {
8627 env->usr_regs[i - 8] = env->xregs[i];
8630 for (i = 8; i < 13; i++) {
8631 env->regs[i] = env->xregs[i];
8636 * Registers r13 & r14 depend on the current mode.
8637 * If we are in a given mode, we copy the corresponding x registers to r13
8638 * and r14. Otherwise, we copy the x register to the banked r13 and r14
8641 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
8642 env->regs[13] = env->xregs[13];
8643 env->regs[14] = env->xregs[14];
8645 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
8648 * HYP is an exception in that it does not have its own banked r14 but
8649 * shares the USR r14
8651 if (mode == ARM_CPU_MODE_HYP) {
8652 env->regs[14] = env->xregs[14];
8654 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
8658 if (mode == ARM_CPU_MODE_HYP) {
8659 env->regs[13] = env->xregs[15];
8661 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
8664 if (mode == ARM_CPU_MODE_IRQ) {
8665 env->regs[14] = env->xregs[16];
8666 env->regs[13] = env->xregs[17];
8668 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
8669 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
8672 if (mode == ARM_CPU_MODE_SVC) {
8673 env->regs[14] = env->xregs[18];
8674 env->regs[13] = env->xregs[19];
8676 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
8677 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
8680 if (mode == ARM_CPU_MODE_ABT) {
8681 env->regs[14] = env->xregs[20];
8682 env->regs[13] = env->xregs[21];
8684 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
8685 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
8688 if (mode == ARM_CPU_MODE_UND) {
8689 env->regs[14] = env->xregs[22];
8690 env->regs[13] = env->xregs[23];
8692 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
8693 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
8696 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
8697 * mode, then we can copy to r8-r14. Otherwise, we copy to the
8698 * FIQ bank for r8-r14.
8700 if (mode == ARM_CPU_MODE_FIQ) {
8701 for (i = 24; i < 31; i++) {
8702 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
8705 for (i = 24; i < 29; i++) {
8706 env->fiq_regs[i - 24] = env->xregs[i];
8708 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
8709 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
8712 env->regs[15] = env->pc;
8715 static void take_aarch32_exception(CPUARMState *env, int new_mode,
8716 uint32_t mask, uint32_t offset,
8719 /* Change the CPU state so as to actually take the exception. */
8720 switch_mode(env, new_mode);
8722 * For exceptions taken to AArch32 we must clear the SS bit in both
8723 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
8725 env->uncached_cpsr &= ~PSTATE_SS;
8726 env->spsr = cpsr_read(env);
8727 /* Clear IT bits. */
8728 env->condexec_bits = 0;
8729 /* Switch to the new mode, and to the correct instruction set. */
8730 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
8731 /* Set new mode endianness */
8732 env->uncached_cpsr &= ~CPSR_E;
8733 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
8734 env->uncached_cpsr |= CPSR_E;
8736 /* J and IL must always be cleared for exception entry */
8737 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
8740 if (new_mode == ARM_CPU_MODE_HYP) {
8741 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
8742 env->elr_el[2] = env->regs[15];
8745 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
8746 * and we should just guard the thumb mode on V4
8748 if (arm_feature(env, ARM_FEATURE_V4T)) {
8750 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
8752 env->regs[14] = env->regs[15] + offset;
8754 env->regs[15] = newpc;
8755 arm_rebuild_hflags(env);
8758 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
8761 * Handle exception entry to Hyp mode; this is sufficiently
8762 * different to entry to other AArch32 modes that we handle it
8765 * The vector table entry used is always the 0x14 Hyp mode entry point,
8766 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
8767 * The offset applied to the preferred return address is always zero
8768 * (see DDI0487C.a section G1.12.3).
8769 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
8771 uint32_t addr, mask;
8772 ARMCPU *cpu = ARM_CPU(cs);
8773 CPUARMState *env = &cpu->env;
8775 switch (cs->exception_index) {
8783 /* Fall through to prefetch abort. */
8784 case EXCP_PREFETCH_ABORT:
8785 env->cp15.ifar_s = env->exception.vaddress;
8786 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
8787 (uint32_t)env->exception.vaddress);
8790 case EXCP_DATA_ABORT:
8791 env->cp15.dfar_s = env->exception.vaddress;
8792 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
8793 (uint32_t)env->exception.vaddress);
8809 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8812 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
8813 if (!arm_feature(env, ARM_FEATURE_V8)) {
8815 * QEMU syndrome values are v8-style. v7 has the IL bit
8816 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
8817 * If this is a v7 CPU, squash the IL bit in those cases.
8819 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
8820 (cs->exception_index == EXCP_DATA_ABORT &&
8821 !(env->exception.syndrome & ARM_EL_ISV)) ||
8822 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
8823 env->exception.syndrome &= ~ARM_EL_IL;
8826 env->cp15.esr_el[2] = env->exception.syndrome;
8829 if (arm_current_el(env) != 2 && addr < 0x14) {
8834 if (!(env->cp15.scr_el3 & SCR_EA)) {
8837 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
8840 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
8844 addr += env->cp15.hvbar;
8846 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
8849 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
8851 ARMCPU *cpu = ARM_CPU(cs);
8852 CPUARMState *env = &cpu->env;
8859 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
8860 switch (syn_get_ec(env->exception.syndrome)) {
8862 case EC_BREAKPOINT_SAME_EL:
8866 case EC_WATCHPOINT_SAME_EL:
8872 case EC_VECTORCATCH:
8881 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
8884 if (env->exception.target_el == 2) {
8885 arm_cpu_do_interrupt_aarch32_hyp(cs);
8889 switch (cs->exception_index) {
8891 new_mode = ARM_CPU_MODE_UND;
8900 new_mode = ARM_CPU_MODE_SVC;
8903 /* The PC already points to the next instruction. */
8907 /* Fall through to prefetch abort. */
8908 case EXCP_PREFETCH_ABORT:
8909 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
8910 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
8911 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
8912 env->exception.fsr, (uint32_t)env->exception.vaddress);
8913 new_mode = ARM_CPU_MODE_ABT;
8915 mask = CPSR_A | CPSR_I;
8918 case EXCP_DATA_ABORT:
8919 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
8920 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
8921 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
8923 (uint32_t)env->exception.vaddress);
8924 new_mode = ARM_CPU_MODE_ABT;
8926 mask = CPSR_A | CPSR_I;
8930 new_mode = ARM_CPU_MODE_IRQ;
8932 /* Disable IRQ and imprecise data aborts. */
8933 mask = CPSR_A | CPSR_I;
8935 if (env->cp15.scr_el3 & SCR_IRQ) {
8936 /* IRQ routed to monitor mode */
8937 new_mode = ARM_CPU_MODE_MON;
8942 new_mode = ARM_CPU_MODE_FIQ;
8944 /* Disable FIQ, IRQ and imprecise data aborts. */
8945 mask = CPSR_A | CPSR_I | CPSR_F;
8946 if (env->cp15.scr_el3 & SCR_FIQ) {
8947 /* FIQ routed to monitor mode */
8948 new_mode = ARM_CPU_MODE_MON;
8953 new_mode = ARM_CPU_MODE_IRQ;
8955 /* Disable IRQ and imprecise data aborts. */
8956 mask = CPSR_A | CPSR_I;
8960 new_mode = ARM_CPU_MODE_FIQ;
8962 /* Disable FIQ, IRQ and imprecise data aborts. */
8963 mask = CPSR_A | CPSR_I | CPSR_F;
8967 new_mode = ARM_CPU_MODE_MON;
8969 mask = CPSR_A | CPSR_I | CPSR_F;
8973 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
8974 return; /* Never happens. Keep compiler happy. */
8977 if (new_mode == ARM_CPU_MODE_MON) {
8978 addr += env->cp15.mvbar;
8979 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
8980 /* High vectors. When enabled, base address cannot be remapped. */
8983 /* ARM v7 architectures provide a vector base address register to remap
8984 * the interrupt vector table.
8985 * This register is only followed in non-monitor mode, and is banked.
8986 * Note: only bits 31:5 are valid.
8988 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
8991 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
8992 env->cp15.scr_el3 &= ~SCR_NS;
8995 take_aarch32_exception(env, new_mode, mask, offset, addr);
8998 /* Handle exception entry to a target EL which is using AArch64 */
8999 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
9001 ARMCPU *cpu = ARM_CPU(cs);
9002 CPUARMState *env = &cpu->env;
9003 unsigned int new_el = env->exception.target_el;
9004 target_ulong addr = env->cp15.vbar_el[new_el];
9005 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
9006 unsigned int cur_el = arm_current_el(env);
9009 * Note that new_el can never be 0. If cur_el is 0, then
9010 * el0_a64 is is_a64(), else el0_a64 is ignored.
9012 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
9014 if (cur_el < new_el) {
9015 /* Entry vector offset depends on whether the implemented EL
9016 * immediately lower than the target level is using AArch32 or AArch64
9022 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9025 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
9028 is_aa64 = is_a64(env);
9031 g_assert_not_reached();
9039 } else if (pstate_read(env) & PSTATE_SP) {
9043 switch (cs->exception_index) {
9044 case EXCP_PREFETCH_ABORT:
9045 case EXCP_DATA_ABORT:
9046 env->cp15.far_el[new_el] = env->exception.vaddress;
9047 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9048 env->cp15.far_el[new_el]);
9056 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) {
9058 * QEMU internal FP/SIMD syndromes from AArch32 include the
9059 * TA and coproc fields which are only exposed if the exception
9060 * is taken to AArch32 Hyp mode. Mask them out to get a valid
9061 * AArch64 format syndrome.
9063 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
9065 env->cp15.esr_el[new_el] = env->exception.syndrome;
9076 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9080 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
9081 aarch64_save_sp(env, arm_current_el(env));
9082 env->elr_el[new_el] = env->pc;
9084 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
9085 env->elr_el[new_el] = env->regs[15];
9087 aarch64_sync_32_to_64(env);
9089 env->condexec_bits = 0;
9091 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
9092 env->elr_el[new_el]);
9094 pstate_write(env, PSTATE_DAIF | new_mode);
9096 aarch64_restore_sp(env, new_el);
9097 helper_rebuild_hflags_a64(env, new_el);
9101 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
9102 new_el, env->pc, pstate_read(env));
9106 * Do semihosting call and set the appropriate return value. All the
9107 * permission and validity checks have been done at translate time.
9109 * We only see semihosting exceptions in TCG only as they are not
9110 * trapped to the hypervisor in KVM.
9113 static void handle_semihosting(CPUState *cs)
9115 ARMCPU *cpu = ARM_CPU(cs);
9116 CPUARMState *env = &cpu->env;
9119 qemu_log_mask(CPU_LOG_INT,
9120 "...handling as semihosting call 0x%" PRIx64 "\n",
9122 env->xregs[0] = do_arm_semihosting(env);
9125 qemu_log_mask(CPU_LOG_INT,
9126 "...handling as semihosting call 0x%x\n",
9128 env->regs[0] = do_arm_semihosting(env);
9129 env->regs[15] += env->thumb ? 2 : 4;
9134 /* Handle a CPU exception for A and R profile CPUs.
9135 * Do any appropriate logging, handle PSCI calls, and then hand off
9136 * to the AArch64-entry or AArch32-entry function depending on the
9137 * target exception level's register width.
9139 void arm_cpu_do_interrupt(CPUState *cs)
9141 ARMCPU *cpu = ARM_CPU(cs);
9142 CPUARMState *env = &cpu->env;
9143 unsigned int new_el = env->exception.target_el;
9145 assert(!arm_feature(env, ARM_FEATURE_M));
9147 arm_log_exception(cs->exception_index);
9148 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
9150 if (qemu_loglevel_mask(CPU_LOG_INT)
9151 && !excp_is_internal(cs->exception_index)) {
9152 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
9153 syn_get_ec(env->exception.syndrome),
9154 env->exception.syndrome);
9157 if (arm_is_psci_call(cpu, cs->exception_index)) {
9158 arm_handle_psci_call(cpu);
9159 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
9164 * Semihosting semantics depend on the register width of the code
9165 * that caused the exception, not the target exception level, so
9166 * must be handled here.
9169 if (cs->exception_index == EXCP_SEMIHOST) {
9170 handle_semihosting(cs);
9175 /* Hooks may change global state so BQL should be held, also the
9176 * BQL needs to be held for any modification of
9177 * cs->interrupt_request.
9179 g_assert(qemu_mutex_iothread_locked());
9181 arm_call_pre_el_change_hook(cpu);
9183 assert(!excp_is_internal(cs->exception_index));
9184 if (arm_el_is_aa64(env, new_el)) {
9185 arm_cpu_do_interrupt_aarch64(cs);
9187 arm_cpu_do_interrupt_aarch32(cs);
9190 arm_call_el_change_hook(cpu);
9192 if (!kvm_enabled()) {
9193 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
9196 #endif /* !CONFIG_USER_ONLY */
9198 /* Return the exception level which controls this address translation regime */
9199 static uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
9202 case ARMMMUIdx_E20_0:
9203 case ARMMMUIdx_E20_2:
9204 case ARMMMUIdx_Stage2:
9209 case ARMMMUIdx_SE10_0:
9210 return arm_el_is_aa64(env, 3) ? 1 : 3;
9211 case ARMMMUIdx_SE10_1:
9212 case ARMMMUIdx_Stage1_E0:
9213 case ARMMMUIdx_Stage1_E1:
9214 case ARMMMUIdx_E10_0:
9215 case ARMMMUIdx_E10_1:
9216 case ARMMMUIdx_MPrivNegPri:
9217 case ARMMMUIdx_MUserNegPri:
9218 case ARMMMUIdx_MPriv:
9219 case ARMMMUIdx_MUser:
9220 case ARMMMUIdx_MSPrivNegPri:
9221 case ARMMMUIdx_MSUserNegPri:
9222 case ARMMMUIdx_MSPriv:
9223 case ARMMMUIdx_MSUser:
9226 g_assert_not_reached();
9230 uint64_t arm_sctlr(CPUARMState *env, int el)
9232 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
9234 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
9235 el = (mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1);
9237 return env->cp15.sctlr_el[el];
9240 /* Return the SCTLR value which controls this address translation regime */
9241 static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
9243 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
9246 #ifndef CONFIG_USER_ONLY
9248 /* Return true if the specified stage of address translation is disabled */
9249 static inline bool regime_translation_disabled(CPUARMState *env,
9252 if (arm_feature(env, ARM_FEATURE_M)) {
9253 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
9254 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
9255 case R_V7M_MPU_CTRL_ENABLE_MASK:
9256 /* Enabled, but not for HardFault and NMI */
9257 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
9258 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
9259 /* Enabled for all cases */
9263 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
9264 * we warned about that in armv7m_nvic.c when the guest set it.
9270 if (mmu_idx == ARMMMUIdx_Stage2) {
9271 /* HCR.DC means HCR.VM behaves as 1 */
9272 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
9275 if (env->cp15.hcr_el2 & HCR_TGE) {
9276 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
9277 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
9282 if ((env->cp15.hcr_el2 & HCR_DC) &&
9283 (mmu_idx == ARMMMUIdx_Stage1_E0 || mmu_idx == ARMMMUIdx_Stage1_E1)) {
9284 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
9288 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
9291 static inline bool regime_translation_big_endian(CPUARMState *env,
9294 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
9297 /* Return the TTBR associated with this translation regime */
9298 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
9301 if (mmu_idx == ARMMMUIdx_Stage2) {
9302 return env->cp15.vttbr_el2;
9305 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
9307 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
9311 #endif /* !CONFIG_USER_ONLY */
9313 /* Return the TCR controlling this translation regime */
9314 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
9316 if (mmu_idx == ARMMMUIdx_Stage2) {
9317 return &env->cp15.vtcr_el2;
9319 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
9322 /* Convert a possible stage1+2 MMU index into the appropriate
9325 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
9328 case ARMMMUIdx_E10_0:
9329 return ARMMMUIdx_Stage1_E0;
9330 case ARMMMUIdx_E10_1:
9331 return ARMMMUIdx_Stage1_E1;
9337 /* Return true if the translation regime is using LPAE format page tables */
9338 static inline bool regime_using_lpae_format(CPUARMState *env,
9341 int el = regime_el(env, mmu_idx);
9342 if (el == 2 || arm_el_is_aa64(env, el)) {
9345 if (arm_feature(env, ARM_FEATURE_LPAE)
9346 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
9352 /* Returns true if the stage 1 translation regime is using LPAE format page
9353 * tables. Used when raising alignment exceptions, whose FSR changes depending
9354 * on whether the long or short descriptor format is in use. */
9355 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
9357 mmu_idx = stage_1_mmu_idx(mmu_idx);
9359 return regime_using_lpae_format(env, mmu_idx);
9362 #ifndef CONFIG_USER_ONLY
9363 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
9366 case ARMMMUIdx_SE10_0:
9367 case ARMMMUIdx_E20_0:
9368 case ARMMMUIdx_Stage1_E0:
9369 case ARMMMUIdx_MUser:
9370 case ARMMMUIdx_MSUser:
9371 case ARMMMUIdx_MUserNegPri:
9372 case ARMMMUIdx_MSUserNegPri:
9376 case ARMMMUIdx_E10_0:
9377 case ARMMMUIdx_E10_1:
9378 g_assert_not_reached();
9382 /* Translate section/page access permissions to page
9383 * R/W protection flags
9386 * @mmu_idx: MMU index indicating required translation regime
9387 * @ap: The 3-bit access permissions (AP[2:0])
9388 * @domain_prot: The 2-bit domain access permissions
9390 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
9391 int ap, int domain_prot)
9393 bool is_user = regime_is_user(env, mmu_idx);
9395 if (domain_prot == 3) {
9396 return PAGE_READ | PAGE_WRITE;
9401 if (arm_feature(env, ARM_FEATURE_V7)) {
9404 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
9406 return is_user ? 0 : PAGE_READ;
9413 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9418 return PAGE_READ | PAGE_WRITE;
9421 return PAGE_READ | PAGE_WRITE;
9422 case 4: /* Reserved. */
9425 return is_user ? 0 : PAGE_READ;
9429 if (!arm_feature(env, ARM_FEATURE_V6K)) {
9434 g_assert_not_reached();
9438 /* Translate section/page access permissions to page
9439 * R/W protection flags.
9441 * @ap: The 2-bit simple AP (AP[2:1])
9442 * @is_user: TRUE if accessing from PL0
9444 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
9448 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9450 return PAGE_READ | PAGE_WRITE;
9452 return is_user ? 0 : PAGE_READ;
9456 g_assert_not_reached();
9461 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
9463 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
9466 /* Translate S2 section/page access permissions to protection flags
9469 * @s2ap: The 2-bit stage2 access permissions (S2AP)
9470 * @xn: XN (execute-never) bit
9472 static int get_S2prot(CPUARMState *env, int s2ap, int xn)
9483 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
9490 /* Translate section/page access permissions to protection flags
9493 * @mmu_idx: MMU index indicating required translation regime
9494 * @is_aa64: TRUE if AArch64
9495 * @ap: The 2-bit simple AP (AP[2:1])
9496 * @ns: NS (non-secure) bit
9497 * @xn: XN (execute-never) bit
9498 * @pxn: PXN (privileged execute-never) bit
9500 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
9501 int ap, int ns, int xn, int pxn)
9503 bool is_user = regime_is_user(env, mmu_idx);
9504 int prot_rw, user_rw;
9508 assert(mmu_idx != ARMMMUIdx_Stage2);
9510 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
9514 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
9517 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
9521 /* TODO have_wxn should be replaced with
9522 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
9523 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
9524 * compatible processors have EL2, which is required for [U]WXN.
9526 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
9529 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
9533 if (regime_has_2_ranges(mmu_idx) && !is_user) {
9534 xn = pxn || (user_rw & PAGE_WRITE);
9536 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9537 switch (regime_el(env, mmu_idx)) {
9541 xn = xn || !(user_rw & PAGE_READ);
9545 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
9547 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
9548 (uwxn && (user_rw & PAGE_WRITE));
9558 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
9561 return prot_rw | PAGE_EXEC;
9564 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
9565 uint32_t *table, uint32_t address)
9567 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
9568 TCR *tcr = regime_tcr(env, mmu_idx);
9570 if (address & tcr->mask) {
9571 if (tcr->raw_tcr & TTBCR_PD1) {
9572 /* Translation table walk disabled for TTBR1 */
9575 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
9577 if (tcr->raw_tcr & TTBCR_PD0) {
9578 /* Translation table walk disabled for TTBR0 */
9581 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
9583 *table |= (address >> 18) & 0x3ffc;
9587 /* Translate a S1 pagetable walk through S2 if needed. */
9588 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
9589 hwaddr addr, MemTxAttrs txattrs,
9590 ARMMMUFaultInfo *fi)
9592 if ((mmu_idx == ARMMMUIdx_Stage1_E0 || mmu_idx == ARMMMUIdx_Stage1_E1) &&
9593 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9594 target_ulong s2size;
9598 ARMCacheAttrs cacheattrs = {};
9599 ARMCacheAttrs *pcacheattrs = NULL;
9601 if (env->cp15.hcr_el2 & HCR_PTW) {
9603 * PTW means we must fault if this S1 walk touches S2 Device
9604 * memory; otherwise we don't care about the attributes and can
9605 * save the S2 translation the effort of computing them.
9607 pcacheattrs = &cacheattrs;
9610 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_Stage2, &s2pa,
9611 &txattrs, &s2prot, &s2size, fi, pcacheattrs);
9613 assert(fi->type != ARMFault_None);
9619 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
9620 /* Access was to Device memory: generate Permission fault */
9621 fi->type = ARMFault_Permission;
9632 /* All loads done in the course of a page table walk go through here. */
9633 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
9634 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
9636 ARMCPU *cpu = ARM_CPU(cs);
9637 CPUARMState *env = &cpu->env;
9638 MemTxAttrs attrs = {};
9639 MemTxResult result = MEMTX_OK;
9643 attrs.secure = is_secure;
9644 as = arm_addressspace(cs, attrs);
9645 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
9649 if (regime_translation_big_endian(env, mmu_idx)) {
9650 data = address_space_ldl_be(as, addr, attrs, &result);
9652 data = address_space_ldl_le(as, addr, attrs, &result);
9654 if (result == MEMTX_OK) {
9657 fi->type = ARMFault_SyncExternalOnWalk;
9658 fi->ea = arm_extabort_type(result);
9662 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
9663 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
9665 ARMCPU *cpu = ARM_CPU(cs);
9666 CPUARMState *env = &cpu->env;
9667 MemTxAttrs attrs = {};
9668 MemTxResult result = MEMTX_OK;
9672 attrs.secure = is_secure;
9673 as = arm_addressspace(cs, attrs);
9674 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
9678 if (regime_translation_big_endian(env, mmu_idx)) {
9679 data = address_space_ldq_be(as, addr, attrs, &result);
9681 data = address_space_ldq_le(as, addr, attrs, &result);
9683 if (result == MEMTX_OK) {
9686 fi->type = ARMFault_SyncExternalOnWalk;
9687 fi->ea = arm_extabort_type(result);
9691 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
9692 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9693 hwaddr *phys_ptr, int *prot,
9694 target_ulong *page_size,
9695 ARMMMUFaultInfo *fi)
9697 CPUState *cs = env_cpu(env);
9708 /* Pagetable walk. */
9709 /* Lookup l1 descriptor. */
9710 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
9711 /* Section translation fault if page walk is disabled by PD0 or PD1 */
9712 fi->type = ARMFault_Translation;
9715 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
9717 if (fi->type != ARMFault_None) {
9721 domain = (desc >> 5) & 0x0f;
9722 if (regime_el(env, mmu_idx) == 1) {
9723 dacr = env->cp15.dacr_ns;
9725 dacr = env->cp15.dacr_s;
9727 domain_prot = (dacr >> (domain * 2)) & 3;
9729 /* Section translation fault. */
9730 fi->type = ARMFault_Translation;
9736 if (domain_prot == 0 || domain_prot == 2) {
9737 fi->type = ARMFault_Domain;
9742 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
9743 ap = (desc >> 10) & 3;
9744 *page_size = 1024 * 1024;
9746 /* Lookup l2 entry. */
9748 /* Coarse pagetable. */
9749 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
9751 /* Fine pagetable. */
9752 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
9754 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
9756 if (fi->type != ARMFault_None) {
9760 case 0: /* Page translation fault. */
9761 fi->type = ARMFault_Translation;
9763 case 1: /* 64k page. */
9764 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
9765 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
9766 *page_size = 0x10000;
9768 case 2: /* 4k page. */
9769 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
9770 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
9771 *page_size = 0x1000;
9773 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
9775 /* ARMv6/XScale extended small page format */
9776 if (arm_feature(env, ARM_FEATURE_XSCALE)
9777 || arm_feature(env, ARM_FEATURE_V6)) {
9778 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
9779 *page_size = 0x1000;
9781 /* UNPREDICTABLE in ARMv5; we choose to take a
9782 * page translation fault.
9784 fi->type = ARMFault_Translation;
9788 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
9791 ap = (desc >> 4) & 3;
9794 /* Never happens, but compiler isn't smart enough to tell. */
9798 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
9799 *prot |= *prot ? PAGE_EXEC : 0;
9800 if (!(*prot & (1 << access_type))) {
9801 /* Access permission fault. */
9802 fi->type = ARMFault_Permission;
9805 *phys_ptr = phys_addr;
9808 fi->domain = domain;
9813 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
9814 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9815 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
9816 target_ulong *page_size, ARMMMUFaultInfo *fi)
9818 CPUState *cs = env_cpu(env);
9832 /* Pagetable walk. */
9833 /* Lookup l1 descriptor. */
9834 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
9835 /* Section translation fault if page walk is disabled by PD0 or PD1 */
9836 fi->type = ARMFault_Translation;
9839 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
9841 if (fi->type != ARMFault_None) {
9845 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
9846 /* Section translation fault, or attempt to use the encoding
9847 * which is Reserved on implementations without PXN.
9849 fi->type = ARMFault_Translation;
9852 if ((type == 1) || !(desc & (1 << 18))) {
9853 /* Page or Section. */
9854 domain = (desc >> 5) & 0x0f;
9856 if (regime_el(env, mmu_idx) == 1) {
9857 dacr = env->cp15.dacr_ns;
9859 dacr = env->cp15.dacr_s;
9864 domain_prot = (dacr >> (domain * 2)) & 3;
9865 if (domain_prot == 0 || domain_prot == 2) {
9866 /* Section or Page domain fault */
9867 fi->type = ARMFault_Domain;
9871 if (desc & (1 << 18)) {
9873 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
9874 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
9875 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
9876 *page_size = 0x1000000;
9879 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
9880 *page_size = 0x100000;
9882 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
9883 xn = desc & (1 << 4);
9885 ns = extract32(desc, 19, 1);
9887 if (arm_feature(env, ARM_FEATURE_PXN)) {
9888 pxn = (desc >> 2) & 1;
9890 ns = extract32(desc, 3, 1);
9891 /* Lookup l2 entry. */
9892 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
9893 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
9895 if (fi->type != ARMFault_None) {
9898 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
9900 case 0: /* Page translation fault. */
9901 fi->type = ARMFault_Translation;
9903 case 1: /* 64k page. */
9904 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
9905 xn = desc & (1 << 15);
9906 *page_size = 0x10000;
9908 case 2: case 3: /* 4k page. */
9909 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
9911 *page_size = 0x1000;
9914 /* Never happens, but compiler isn't smart enough to tell. */
9918 if (domain_prot == 3) {
9919 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
9921 if (pxn && !regime_is_user(env, mmu_idx)) {
9924 if (xn && access_type == MMU_INST_FETCH) {
9925 fi->type = ARMFault_Permission;
9929 if (arm_feature(env, ARM_FEATURE_V6K) &&
9930 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
9931 /* The simplified model uses AP[0] as an access control bit. */
9932 if ((ap & 1) == 0) {
9933 /* Access flag fault. */
9934 fi->type = ARMFault_AccessFlag;
9937 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
9939 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
9944 if (!(*prot & (1 << access_type))) {
9945 /* Access permission fault. */
9946 fi->type = ARMFault_Permission;
9951 /* The NS bit will (as required by the architecture) have no effect if
9952 * the CPU doesn't support TZ or this is a non-secure translation
9953 * regime, because the attribute will already be non-secure.
9955 attrs->secure = false;
9957 *phys_ptr = phys_addr;
9960 fi->domain = domain;
9966 * check_s2_mmu_setup
9968 * @is_aa64: True if the translation regime is in AArch64 state
9969 * @startlevel: Suggested starting level
9970 * @inputsize: Bitsize of IPAs
9971 * @stride: Page-table stride (See the ARM ARM)
9973 * Returns true if the suggested S2 translation parameters are OK and
9976 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
9977 int inputsize, int stride)
9979 const int grainsize = stride + 3;
9982 /* Negative levels are never allowed. */
9987 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
9988 if (startsizecheck < 1 || startsizecheck > stride + 4) {
9993 CPUARMState *env = &cpu->env;
9994 unsigned int pamax = arm_pamax(cpu);
9997 case 13: /* 64KB Pages. */
9998 if (level == 0 || (level == 1 && pamax <= 42)) {
10002 case 11: /* 16KB Pages. */
10003 if (level == 0 || (level == 1 && pamax <= 40)) {
10007 case 9: /* 4KB Pages. */
10008 if (level == 0 && pamax <= 42) {
10013 g_assert_not_reached();
10016 /* Inputsize checks. */
10017 if (inputsize > pamax &&
10018 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
10019 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
10023 /* AArch32 only supports 4KB pages. Assert on that. */
10024 assert(stride == 9);
10033 /* Translate from the 4-bit stage 2 representation of
10034 * memory attributes (without cache-allocation hints) to
10035 * the 8-bit representation of the stage 1 MAIR registers
10036 * (which includes allocation hints).
10038 * ref: shared/translation/attrs/S2AttrDecode()
10039 * .../S2ConvertAttrsHints()
10041 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
10043 uint8_t hiattr = extract32(s2attrs, 2, 2);
10044 uint8_t loattr = extract32(s2attrs, 0, 2);
10045 uint8_t hihint = 0, lohint = 0;
10047 if (hiattr != 0) { /* normal memory */
10048 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
10049 hiattr = loattr = 1; /* non-cacheable */
10051 if (hiattr != 1) { /* Write-through or write-back */
10052 hihint = 3; /* RW allocate */
10054 if (loattr != 1) { /* Write-through or write-back */
10055 lohint = 3; /* RW allocate */
10060 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
10062 #endif /* !CONFIG_USER_ONLY */
10064 ARMVAParameters aa64_va_parameters_both(CPUARMState *env, uint64_t va,
10067 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10068 bool tbi, tbid, epd, hpd, using16k, using64k;
10072 * Bit 55 is always between the two regions, and is canonical for
10073 * determining if address tagging is enabled.
10075 select = extract64(va, 55, 1);
10077 if (!regime_has_2_ranges(mmu_idx)) {
10078 tsz = extract32(tcr, 0, 6);
10079 using64k = extract32(tcr, 14, 1);
10080 using16k = extract32(tcr, 15, 1);
10081 if (mmu_idx == ARMMMUIdx_Stage2) {
10083 tbi = tbid = hpd = false;
10085 tbi = extract32(tcr, 20, 1);
10086 hpd = extract32(tcr, 24, 1);
10087 tbid = extract32(tcr, 29, 1);
10090 } else if (!select) {
10091 tsz = extract32(tcr, 0, 6);
10092 epd = extract32(tcr, 7, 1);
10093 using64k = extract32(tcr, 14, 1);
10094 using16k = extract32(tcr, 15, 1);
10095 tbi = extract64(tcr, 37, 1);
10096 hpd = extract64(tcr, 41, 1);
10097 tbid = extract64(tcr, 51, 1);
10099 int tg = extract32(tcr, 30, 2);
10100 using16k = tg == 1;
10101 using64k = tg == 3;
10102 tsz = extract32(tcr, 16, 6);
10103 epd = extract32(tcr, 23, 1);
10104 tbi = extract64(tcr, 38, 1);
10105 hpd = extract64(tcr, 42, 1);
10106 tbid = extract64(tcr, 52, 1);
10108 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
10109 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
10111 return (ARMVAParameters) {
10118 .using16k = using16k,
10119 .using64k = using64k,
10123 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10124 ARMMMUIdx mmu_idx, bool data)
10126 ARMVAParameters ret = aa64_va_parameters_both(env, va, mmu_idx);
10128 /* Present TBI as a composite with TBID. */
10129 ret.tbi &= (data || !ret.tbid);
10133 #ifndef CONFIG_USER_ONLY
10134 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
10137 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10138 uint32_t el = regime_el(env, mmu_idx);
10142 if (mmu_idx == ARMMMUIdx_Stage2) {
10144 bool sext = extract32(tcr, 4, 1);
10145 bool sign = extract32(tcr, 3, 1);
10148 * If the sign-extend bit is not the same as t0sz[3], the result
10149 * is unpredictable. Flag this as a guest error.
10151 if (sign != sext) {
10152 qemu_log_mask(LOG_GUEST_ERROR,
10153 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
10155 tsz = sextract32(tcr, 0, 4) + 8;
10159 } else if (el == 2) {
10161 tsz = extract32(tcr, 0, 3);
10163 hpd = extract64(tcr, 24, 1);
10166 int t0sz = extract32(tcr, 0, 3);
10167 int t1sz = extract32(tcr, 16, 3);
10170 select = va > (0xffffffffu >> t0sz);
10172 /* Note that we will detect errors later. */
10173 select = va >= ~(0xffffffffu >> t1sz);
10177 epd = extract32(tcr, 7, 1);
10178 hpd = extract64(tcr, 41, 1);
10181 epd = extract32(tcr, 23, 1);
10182 hpd = extract64(tcr, 42, 1);
10184 /* For aarch32, hpd0 is not enabled without t2e as well. */
10185 hpd &= extract32(tcr, 6, 1);
10188 return (ARMVAParameters) {
10196 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
10197 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10198 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
10199 target_ulong *page_size_ptr,
10200 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
10202 ARMCPU *cpu = env_archcpu(env);
10203 CPUState *cs = CPU(cpu);
10204 /* Read an LPAE long-descriptor translation table. */
10205 ARMFaultType fault_type = ARMFault_Translation;
10207 ARMVAParameters param;
10209 hwaddr descaddr, indexmask, indexmask_grainsize;
10210 uint32_t tableattrs;
10211 target_ulong page_size;
10214 int addrsize, inputsize;
10215 TCR *tcr = regime_tcr(env, mmu_idx);
10216 int ap, ns, xn, pxn;
10217 uint32_t el = regime_el(env, mmu_idx);
10219 uint64_t descaddrmask;
10220 bool aarch64 = arm_el_is_aa64(env, el);
10221 bool guarded = false;
10224 * This code does not handle the different format TCR for VTCR_EL2.
10225 * This code also does not support shareability levels.
10226 * Attribute and permission bit handling should also be checked when adding
10227 * support for those page table walks.
10230 param = aa64_va_parameters(env, address, mmu_idx,
10231 access_type != MMU_INST_FETCH);
10233 ttbr1_valid = regime_has_2_ranges(mmu_idx);
10234 addrsize = 64 - 8 * param.tbi;
10235 inputsize = 64 - param.tsz;
10237 param = aa32_va_parameters(env, address, mmu_idx);
10239 /* There is no TTBR1 for EL2 */
10240 ttbr1_valid = (el != 2);
10241 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
10242 inputsize = addrsize - param.tsz;
10246 * We determined the region when collecting the parameters, but we
10247 * have not yet validated that the address is valid for the region.
10248 * Extract the top bits and verify that they all match select.
10250 * For aa32, if inputsize == addrsize, then we have selected the
10251 * region by exclusion in aa32_va_parameters and there is no more
10252 * validation to do here.
10254 if (inputsize < addrsize) {
10255 target_ulong top_bits = sextract64(address, inputsize,
10256 addrsize - inputsize);
10257 if (-top_bits != param.select || (param.select && !ttbr1_valid)) {
10258 /* The gap between the two regions is a Translation fault */
10259 fault_type = ARMFault_Translation;
10264 if (param.using64k) {
10266 } else if (param.using16k) {
10272 /* Note that QEMU ignores shareability and cacheability attributes,
10273 * so we don't need to do anything with the SH, ORGN, IRGN fields
10274 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
10275 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
10276 * implement any ASID-like capability so we can ignore it (instead
10277 * we will always flush the TLB any time the ASID is changed).
10279 ttbr = regime_ttbr(env, mmu_idx, param.select);
10281 /* Here we should have set up all the parameters for the translation:
10282 * inputsize, ttbr, epd, stride, tbi
10286 /* Translation table walk disabled => Translation fault on TLB miss
10287 * Note: This is always 0 on 64-bit EL2 and EL3.
10292 if (mmu_idx != ARMMMUIdx_Stage2) {
10293 /* The starting level depends on the virtual address size (which can
10294 * be up to 48 bits) and the translation granule size. It indicates
10295 * the number of strides (stride bits at a time) needed to
10296 * consume the bits of the input address. In the pseudocode this is:
10297 * level = 4 - RoundUp((inputsize - grainsize) / stride)
10298 * where their 'inputsize' is our 'inputsize', 'grainsize' is
10299 * our 'stride + 3' and 'stride' is our 'stride'.
10300 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
10301 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
10302 * = 4 - (inputsize - 4) / stride;
10304 level = 4 - (inputsize - 4) / stride;
10306 /* For stage 2 translations the starting level is specified by the
10307 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
10309 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
10310 uint32_t startlevel;
10313 if (!aarch64 || stride == 9) {
10314 /* AArch32 or 4KB pages */
10315 startlevel = 2 - sl0;
10317 /* 16KB or 64KB pages */
10318 startlevel = 3 - sl0;
10321 /* Check that the starting level is valid. */
10322 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
10323 inputsize, stride);
10325 fault_type = ARMFault_Translation;
10328 level = startlevel;
10331 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
10332 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
10334 /* Now we can extract the actual base address from the TTBR */
10335 descaddr = extract64(ttbr, 0, 48);
10336 descaddr &= ~indexmask;
10338 /* The address field in the descriptor goes up to bit 39 for ARMv7
10339 * but up to bit 47 for ARMv8, but we use the descaddrmask
10340 * up to bit 39 for AArch32, because we don't need other bits in that case
10341 * to construct next descriptor address (anyway they should be all zeroes).
10343 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
10344 ~indexmask_grainsize;
10346 /* Secure accesses start with the page table in secure memory and
10347 * can be downgraded to non-secure at any step. Non-secure accesses
10348 * remain non-secure. We implement this by just ORing in the NSTable/NS
10349 * bits at each step.
10351 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
10353 uint64_t descriptor;
10356 descaddr |= (address >> (stride * (4 - level))) & indexmask;
10358 nstable = extract32(tableattrs, 4, 1);
10359 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
10360 if (fi->type != ARMFault_None) {
10364 if (!(descriptor & 1) ||
10365 (!(descriptor & 2) && (level == 3))) {
10366 /* Invalid, or the Reserved level 3 encoding */
10369 descaddr = descriptor & descaddrmask;
10371 if ((descriptor & 2) && (level < 3)) {
10372 /* Table entry. The top five bits are attributes which may
10373 * propagate down through lower levels of the table (and
10374 * which are all arranged so that 0 means "no effect", so
10375 * we can gather them up by ORing in the bits at each level).
10377 tableattrs |= extract64(descriptor, 59, 5);
10379 indexmask = indexmask_grainsize;
10382 /* Block entry at level 1 or 2, or page entry at level 3.
10383 * These are basically the same thing, although the number
10384 * of bits we pull in from the vaddr varies.
10386 page_size = (1ULL << ((stride * (4 - level)) + 3));
10387 descaddr |= (address & (page_size - 1));
10388 /* Extract attributes from the descriptor */
10389 attrs = extract64(descriptor, 2, 10)
10390 | (extract64(descriptor, 52, 12) << 10);
10392 if (mmu_idx == ARMMMUIdx_Stage2) {
10393 /* Stage 2 table descriptors do not include any attribute fields */
10396 /* Merge in attributes from table descriptors */
10397 attrs |= nstable << 3; /* NS */
10398 guarded = extract64(descriptor, 50, 1); /* GP */
10400 /* HPD disables all the table attributes except NSTable. */
10403 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
10404 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
10405 * means "force PL1 access only", which means forcing AP[1] to 0.
10407 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
10408 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
10411 /* Here descaddr is the final physical address, and attributes
10412 * are all in attrs.
10414 fault_type = ARMFault_AccessFlag;
10415 if ((attrs & (1 << 8)) == 0) {
10420 ap = extract32(attrs, 4, 2);
10421 xn = extract32(attrs, 12, 1);
10423 if (mmu_idx == ARMMMUIdx_Stage2) {
10425 *prot = get_S2prot(env, ap, xn);
10427 ns = extract32(attrs, 3, 1);
10428 pxn = extract32(attrs, 11, 1);
10429 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
10432 fault_type = ARMFault_Permission;
10433 if (!(*prot & (1 << access_type))) {
10438 /* The NS bit will (as required by the architecture) have no effect if
10439 * the CPU doesn't support TZ or this is a non-secure translation
10440 * regime, because the attribute will already be non-secure.
10442 txattrs->secure = false;
10444 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
10445 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
10446 txattrs->target_tlb_bit0 = true;
10449 if (cacheattrs != NULL) {
10450 if (mmu_idx == ARMMMUIdx_Stage2) {
10451 cacheattrs->attrs = convert_stage2_attrs(env,
10452 extract32(attrs, 0, 4));
10454 /* Index into MAIR registers for cache attributes */
10455 uint8_t attrindx = extract32(attrs, 0, 3);
10456 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
10457 assert(attrindx <= 7);
10458 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
10460 cacheattrs->shareability = extract32(attrs, 6, 2);
10463 *phys_ptr = descaddr;
10464 *page_size_ptr = page_size;
10468 fi->type = fault_type;
10470 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
10471 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2);
10475 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
10477 int32_t address, int *prot)
10479 if (!arm_feature(env, ARM_FEATURE_M)) {
10480 *prot = PAGE_READ | PAGE_WRITE;
10482 case 0xF0000000 ... 0xFFFFFFFF:
10483 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
10484 /* hivecs execing is ok */
10485 *prot |= PAGE_EXEC;
10488 case 0x00000000 ... 0x7FFFFFFF:
10489 *prot |= PAGE_EXEC;
10493 /* Default system address map for M profile cores.
10494 * The architecture specifies which regions are execute-never;
10495 * at the MPU level no other checks are defined.
10498 case 0x00000000 ... 0x1fffffff: /* ROM */
10499 case 0x20000000 ... 0x3fffffff: /* SRAM */
10500 case 0x60000000 ... 0x7fffffff: /* RAM */
10501 case 0x80000000 ... 0x9fffffff: /* RAM */
10502 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10504 case 0x40000000 ... 0x5fffffff: /* Peripheral */
10505 case 0xa0000000 ... 0xbfffffff: /* Device */
10506 case 0xc0000000 ... 0xdfffffff: /* Device */
10507 case 0xe0000000 ... 0xffffffff: /* System */
10508 *prot = PAGE_READ | PAGE_WRITE;
10511 g_assert_not_reached();
10516 static bool pmsav7_use_background_region(ARMCPU *cpu,
10517 ARMMMUIdx mmu_idx, bool is_user)
10519 /* Return true if we should use the default memory map as a
10520 * "background" region if there are no hits against any MPU regions.
10522 CPUARMState *env = &cpu->env;
10528 if (arm_feature(env, ARM_FEATURE_M)) {
10529 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
10530 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
10532 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
10536 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
10538 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
10539 return arm_feature(env, ARM_FEATURE_M) &&
10540 extract32(address, 20, 12) == 0xe00;
10543 static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
10545 /* True if address is in the M profile system region
10546 * 0xe0000000 - 0xffffffff
10548 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
10551 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
10552 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10553 hwaddr *phys_ptr, int *prot,
10554 target_ulong *page_size,
10555 ARMMMUFaultInfo *fi)
10557 ARMCPU *cpu = env_archcpu(env);
10559 bool is_user = regime_is_user(env, mmu_idx);
10561 *phys_ptr = address;
10562 *page_size = TARGET_PAGE_SIZE;
10565 if (regime_translation_disabled(env, mmu_idx) ||
10566 m_is_ppb_region(env, address)) {
10567 /* MPU disabled or M profile PPB access: use default memory map.
10568 * The other case which uses the default memory map in the
10569 * v7M ARM ARM pseudocode is exception vector reads from the vector
10570 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
10571 * which always does a direct read using address_space_ldl(), rather
10572 * than going via this function, so we don't need to check that here.
10574 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10575 } else { /* MPU enabled */
10576 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
10577 /* region search */
10578 uint32_t base = env->pmsav7.drbar[n];
10579 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
10581 bool srdis = false;
10583 if (!(env->pmsav7.drsr[n] & 0x1)) {
10588 qemu_log_mask(LOG_GUEST_ERROR,
10589 "DRSR[%d]: Rsize field cannot be 0\n", n);
10593 rmask = (1ull << rsize) - 1;
10595 if (base & rmask) {
10596 qemu_log_mask(LOG_GUEST_ERROR,
10597 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
10598 "to DRSR region size, mask = 0x%" PRIx32 "\n",
10603 if (address < base || address > base + rmask) {
10605 * Address not in this region. We must check whether the
10606 * region covers addresses in the same page as our address.
10607 * In that case we must not report a size that covers the
10608 * whole page for a subsequent hit against a different MPU
10609 * region or the background region, because it would result in
10610 * incorrect TLB hits for subsequent accesses to addresses that
10611 * are in this MPU region.
10613 if (ranges_overlap(base, rmask,
10614 address & TARGET_PAGE_MASK,
10615 TARGET_PAGE_SIZE)) {
10621 /* Region matched */
10623 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
10625 uint32_t srdis_mask;
10627 rsize -= 3; /* sub region size (power of 2) */
10628 snd = ((address - base) >> rsize) & 0x7;
10629 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
10631 srdis_mask = srdis ? 0x3 : 0x0;
10632 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
10633 /* This will check in groups of 2, 4 and then 8, whether
10634 * the subregion bits are consistent. rsize is incremented
10635 * back up to give the region size, considering consistent
10636 * adjacent subregions as one region. Stop testing if rsize
10637 * is already big enough for an entire QEMU page.
10639 int snd_rounded = snd & ~(i - 1);
10640 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
10641 snd_rounded + 8, i);
10642 if (srdis_mask ^ srdis_multi) {
10645 srdis_mask = (srdis_mask << i) | srdis_mask;
10652 if (rsize < TARGET_PAGE_BITS) {
10653 *page_size = 1 << rsize;
10658 if (n == -1) { /* no hits */
10659 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
10660 /* background fault */
10661 fi->type = ARMFault_Background;
10664 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10665 } else { /* a MPU hit! */
10666 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
10667 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
10669 if (m_is_system_region(env, address)) {
10670 /* System space is always execute never */
10674 if (is_user) { /* User mode AP bit decoding */
10679 break; /* no access */
10681 *prot |= PAGE_WRITE;
10685 *prot |= PAGE_READ | PAGE_EXEC;
10688 /* for v7M, same as 6; for R profile a reserved value */
10689 if (arm_feature(env, ARM_FEATURE_M)) {
10690 *prot |= PAGE_READ | PAGE_EXEC;
10695 qemu_log_mask(LOG_GUEST_ERROR,
10696 "DRACR[%d]: Bad value for AP bits: 0x%"
10697 PRIx32 "\n", n, ap);
10699 } else { /* Priv. mode AP bits decoding */
10702 break; /* no access */
10706 *prot |= PAGE_WRITE;
10710 *prot |= PAGE_READ | PAGE_EXEC;
10713 /* for v7M, same as 6; for R profile a reserved value */
10714 if (arm_feature(env, ARM_FEATURE_M)) {
10715 *prot |= PAGE_READ | PAGE_EXEC;
10720 qemu_log_mask(LOG_GUEST_ERROR,
10721 "DRACR[%d]: Bad value for AP bits: 0x%"
10722 PRIx32 "\n", n, ap);
10726 /* execute never */
10728 *prot &= ~PAGE_EXEC;
10733 fi->type = ARMFault_Permission;
10735 return !(*prot & (1 << access_type));
10738 static bool v8m_is_sau_exempt(CPUARMState *env,
10739 uint32_t address, MMUAccessType access_type)
10741 /* The architecture specifies that certain address ranges are
10742 * exempt from v8M SAU/IDAU checks.
10745 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
10746 (address >= 0xe0000000 && address <= 0xe0002fff) ||
10747 (address >= 0xe000e000 && address <= 0xe000efff) ||
10748 (address >= 0xe002e000 && address <= 0xe002efff) ||
10749 (address >= 0xe0040000 && address <= 0xe0041fff) ||
10750 (address >= 0xe00ff000 && address <= 0xe00fffff);
10753 void v8m_security_lookup(CPUARMState *env, uint32_t address,
10754 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10755 V8M_SAttributes *sattrs)
10757 /* Look up the security attributes for this address. Compare the
10758 * pseudocode SecurityCheck() function.
10759 * We assume the caller has zero-initialized *sattrs.
10761 ARMCPU *cpu = env_archcpu(env);
10763 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
10764 int idau_region = IREGION_NOTVALID;
10765 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
10766 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
10769 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
10770 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
10772 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
10776 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
10777 /* 0xf0000000..0xffffffff is always S for insn fetches */
10781 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
10782 sattrs->ns = !regime_is_secure(env, mmu_idx);
10786 if (idau_region != IREGION_NOTVALID) {
10787 sattrs->irvalid = true;
10788 sattrs->iregion = idau_region;
10791 switch (env->sau.ctrl & 3) {
10792 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
10794 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
10797 default: /* SAU.ENABLE == 1 */
10798 for (r = 0; r < cpu->sau_sregion; r++) {
10799 if (env->sau.rlar[r] & 1) {
10800 uint32_t base = env->sau.rbar[r] & ~0x1f;
10801 uint32_t limit = env->sau.rlar[r] | 0x1f;
10803 if (base <= address && limit >= address) {
10804 if (base > addr_page_base || limit < addr_page_limit) {
10805 sattrs->subpage = true;
10807 if (sattrs->srvalid) {
10808 /* If we hit in more than one region then we must report
10809 * as Secure, not NS-Callable, with no valid region
10812 sattrs->ns = false;
10813 sattrs->nsc = false;
10814 sattrs->sregion = 0;
10815 sattrs->srvalid = false;
10818 if (env->sau.rlar[r] & 2) {
10819 sattrs->nsc = true;
10823 sattrs->srvalid = true;
10824 sattrs->sregion = r;
10828 * Address not in this region. We must check whether the
10829 * region covers addresses in the same page as our address.
10830 * In that case we must not report a size that covers the
10831 * whole page for a subsequent hit against a different MPU
10832 * region or the background region, because it would result
10833 * in incorrect TLB hits for subsequent accesses to
10834 * addresses that are in this MPU region.
10836 if (limit >= base &&
10837 ranges_overlap(base, limit - base + 1,
10839 TARGET_PAGE_SIZE)) {
10840 sattrs->subpage = true;
10849 * The IDAU will override the SAU lookup results if it specifies
10850 * higher security than the SAU does.
10853 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
10854 sattrs->ns = false;
10855 sattrs->nsc = idau_nsc;
10860 bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
10861 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10862 hwaddr *phys_ptr, MemTxAttrs *txattrs,
10863 int *prot, bool *is_subpage,
10864 ARMMMUFaultInfo *fi, uint32_t *mregion)
10866 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
10867 * that a full phys-to-virt translation does).
10868 * mregion is (if not NULL) set to the region number which matched,
10869 * or -1 if no region number is returned (MPU off, address did not
10870 * hit a region, address hit in multiple regions).
10871 * We set is_subpage to true if the region hit doesn't cover the
10872 * entire TARGET_PAGE the address is within.
10874 ARMCPU *cpu = env_archcpu(env);
10875 bool is_user = regime_is_user(env, mmu_idx);
10876 uint32_t secure = regime_is_secure(env, mmu_idx);
10878 int matchregion = -1;
10880 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
10881 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
10883 *is_subpage = false;
10884 *phys_ptr = address;
10890 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
10891 * was an exception vector read from the vector table (which is always
10892 * done using the default system address map), because those accesses
10893 * are done in arm_v7m_load_vector(), which always does a direct
10894 * read using address_space_ldl(), rather than going via this function.
10896 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
10898 } else if (m_is_ppb_region(env, address)) {
10901 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
10905 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
10906 /* region search */
10907 /* Note that the base address is bits [31:5] from the register
10908 * with bits [4:0] all zeroes, but the limit address is bits
10909 * [31:5] from the register with bits [4:0] all ones.
10911 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
10912 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
10914 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
10915 /* Region disabled */
10919 if (address < base || address > limit) {
10921 * Address not in this region. We must check whether the
10922 * region covers addresses in the same page as our address.
10923 * In that case we must not report a size that covers the
10924 * whole page for a subsequent hit against a different MPU
10925 * region or the background region, because it would result in
10926 * incorrect TLB hits for subsequent accesses to addresses that
10927 * are in this MPU region.
10929 if (limit >= base &&
10930 ranges_overlap(base, limit - base + 1,
10932 TARGET_PAGE_SIZE)) {
10933 *is_subpage = true;
10938 if (base > addr_page_base || limit < addr_page_limit) {
10939 *is_subpage = true;
10942 if (matchregion != -1) {
10943 /* Multiple regions match -- always a failure (unlike
10944 * PMSAv7 where highest-numbered-region wins)
10946 fi->type = ARMFault_Permission;
10957 /* background fault */
10958 fi->type = ARMFault_Background;
10962 if (matchregion == -1) {
10963 /* hit using the background region */
10964 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
10966 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
10967 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
10969 if (m_is_system_region(env, address)) {
10970 /* System space is always execute never */
10974 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
10975 if (*prot && !xn) {
10976 *prot |= PAGE_EXEC;
10978 /* We don't need to look the attribute up in the MAIR0/MAIR1
10979 * registers because that only tells us about cacheability.
10982 *mregion = matchregion;
10986 fi->type = ARMFault_Permission;
10988 return !(*prot & (1 << access_type));
10992 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
10993 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10994 hwaddr *phys_ptr, MemTxAttrs *txattrs,
10995 int *prot, target_ulong *page_size,
10996 ARMMMUFaultInfo *fi)
10998 uint32_t secure = regime_is_secure(env, mmu_idx);
10999 V8M_SAttributes sattrs = {};
11001 bool mpu_is_subpage;
11003 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
11004 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
11005 if (access_type == MMU_INST_FETCH) {
11006 /* Instruction fetches always use the MMU bank and the
11007 * transaction attribute determined by the fetch address,
11008 * regardless of CPU state. This is painful for QEMU
11009 * to handle, because it would mean we need to encode
11010 * into the mmu_idx not just the (user, negpri) information
11011 * for the current security state but also that for the
11012 * other security state, which would balloon the number
11013 * of mmu_idx values needed alarmingly.
11014 * Fortunately we can avoid this because it's not actually
11015 * possible to arbitrarily execute code from memory with
11016 * the wrong security attribute: it will always generate
11017 * an exception of some kind or another, apart from the
11018 * special case of an NS CPU executing an SG instruction
11019 * in S&NSC memory. So we always just fail the translation
11020 * here and sort things out in the exception handler
11021 * (including possibly emulating an SG instruction).
11023 if (sattrs.ns != !secure) {
11025 fi->type = ARMFault_QEMU_NSCExec;
11027 fi->type = ARMFault_QEMU_SFault;
11029 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
11030 *phys_ptr = address;
11035 /* For data accesses we always use the MMU bank indicated
11036 * by the current CPU state, but the security attributes
11037 * might downgrade a secure access to nonsecure.
11040 txattrs->secure = false;
11041 } else if (!secure) {
11042 /* NS access to S memory must fault.
11043 * Architecturally we should first check whether the
11044 * MPU information for this address indicates that we
11045 * are doing an unaligned access to Device memory, which
11046 * should generate a UsageFault instead. QEMU does not
11047 * currently check for that kind of unaligned access though.
11048 * If we added it we would need to do so as a special case
11049 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
11051 fi->type = ARMFault_QEMU_SFault;
11052 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
11053 *phys_ptr = address;
11060 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
11061 txattrs, prot, &mpu_is_subpage, fi, NULL);
11062 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
11066 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
11067 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11068 hwaddr *phys_ptr, int *prot,
11069 ARMMMUFaultInfo *fi)
11074 bool is_user = regime_is_user(env, mmu_idx);
11076 if (regime_translation_disabled(env, mmu_idx)) {
11077 /* MPU disabled. */
11078 *phys_ptr = address;
11079 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11083 *phys_ptr = address;
11084 for (n = 7; n >= 0; n--) {
11085 base = env->cp15.c6_region[n];
11086 if ((base & 1) == 0) {
11089 mask = 1 << ((base >> 1) & 0x1f);
11090 /* Keep this shift separate from the above to avoid an
11091 (undefined) << 32. */
11092 mask = (mask << 1) - 1;
11093 if (((base ^ address) & ~mask) == 0) {
11098 fi->type = ARMFault_Background;
11102 if (access_type == MMU_INST_FETCH) {
11103 mask = env->cp15.pmsav5_insn_ap;
11105 mask = env->cp15.pmsav5_data_ap;
11107 mask = (mask >> (n * 4)) & 0xf;
11110 fi->type = ARMFault_Permission;
11115 fi->type = ARMFault_Permission;
11119 *prot = PAGE_READ | PAGE_WRITE;
11124 *prot |= PAGE_WRITE;
11128 *prot = PAGE_READ | PAGE_WRITE;
11132 fi->type = ARMFault_Permission;
11142 /* Bad permission. */
11143 fi->type = ARMFault_Permission;
11147 *prot |= PAGE_EXEC;
11151 /* Combine either inner or outer cacheability attributes for normal
11152 * memory, according to table D4-42 and pseudocode procedure
11153 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
11155 * NB: only stage 1 includes allocation hints (RW bits), leading to
11158 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
11160 if (s1 == 4 || s2 == 4) {
11161 /* non-cacheable has precedence */
11163 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
11164 /* stage 1 write-through takes precedence */
11166 } else if (extract32(s2, 2, 2) == 2) {
11167 /* stage 2 write-through takes precedence, but the allocation hint
11168 * is still taken from stage 1
11170 return (2 << 2) | extract32(s1, 0, 2);
11171 } else { /* write-back */
11176 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
11177 * and CombineS1S2Desc()
11179 * @s1: Attributes from stage 1 walk
11180 * @s2: Attributes from stage 2 walk
11182 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
11184 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4);
11185 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4);
11188 /* Combine shareability attributes (table D4-43) */
11189 if (s1.shareability == 2 || s2.shareability == 2) {
11190 /* if either are outer-shareable, the result is outer-shareable */
11191 ret.shareability = 2;
11192 } else if (s1.shareability == 3 || s2.shareability == 3) {
11193 /* if either are inner-shareable, the result is inner-shareable */
11194 ret.shareability = 3;
11196 /* both non-shareable */
11197 ret.shareability = 0;
11200 /* Combine memory type and cacheability attributes */
11201 if (s1hi == 0 || s2hi == 0) {
11202 /* Device has precedence over normal */
11203 if (s1lo == 0 || s2lo == 0) {
11204 /* nGnRnE has precedence over anything */
11206 } else if (s1lo == 4 || s2lo == 4) {
11207 /* non-Reordering has precedence over Reordering */
11208 ret.attrs = 4; /* nGnRE */
11209 } else if (s1lo == 8 || s2lo == 8) {
11210 /* non-Gathering has precedence over Gathering */
11211 ret.attrs = 8; /* nGRE */
11213 ret.attrs = 0xc; /* GRE */
11216 /* Any location for which the resultant memory type is any
11217 * type of Device memory is always treated as Outer Shareable.
11219 ret.shareability = 2;
11220 } else { /* Normal memory */
11221 /* Outer/inner cacheability combine independently */
11222 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
11223 | combine_cacheattr_nibble(s1lo, s2lo);
11225 if (ret.attrs == 0x44) {
11226 /* Any location for which the resultant memory type is Normal
11227 * Inner Non-cacheable, Outer Non-cacheable is always treated
11228 * as Outer Shareable.
11230 ret.shareability = 2;
11238 /* get_phys_addr - get the physical address for this virtual address
11240 * Find the physical address corresponding to the given virtual address,
11241 * by doing a translation table walk on MMU based systems or using the
11242 * MPU state on MPU based systems.
11244 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11245 * prot and page_size may not be filled in, and the populated fsr value provides
11246 * information on why the translation aborted, in the format of a
11247 * DFSR/IFSR fault register, with the following caveats:
11248 * * we honour the short vs long DFSR format differences.
11249 * * the WnR bit is never set (the caller must do this).
11250 * * for PSMAv5 based systems we don't bother to return a full FSR format
11253 * @env: CPUARMState
11254 * @address: virtual address to get physical address for
11255 * @access_type: 0 for read, 1 for write, 2 for execute
11256 * @mmu_idx: MMU index indicating required translation regime
11257 * @phys_ptr: set to the physical address corresponding to the virtual address
11258 * @attrs: set to the memory transaction attributes to use
11259 * @prot: set to the permissions for the page containing phys_ptr
11260 * @page_size: set to the size of the page containing phys_ptr
11261 * @fi: set to fault info if the translation fails
11262 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
11264 bool get_phys_addr(CPUARMState *env, target_ulong address,
11265 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11266 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
11267 target_ulong *page_size,
11268 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
11270 if (mmu_idx == ARMMMUIdx_E10_0 || mmu_idx == ARMMMUIdx_E10_1) {
11271 /* Call ourselves recursively to do the stage 1 and then stage 2
11274 if (arm_feature(env, ARM_FEATURE_EL2)) {
11278 ARMCacheAttrs cacheattrs2 = {};
11280 ret = get_phys_addr(env, address, access_type,
11281 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
11282 prot, page_size, fi, cacheattrs);
11284 /* If S1 fails or S2 is disabled, return early. */
11285 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
11290 /* S1 is done. Now do S2 translation. */
11291 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_Stage2,
11292 phys_ptr, attrs, &s2_prot,
11294 cacheattrs != NULL ? &cacheattrs2 : NULL);
11296 /* Combine the S1 and S2 perms. */
11299 /* Combine the S1 and S2 cache attributes, if needed */
11300 if (!ret && cacheattrs != NULL) {
11301 if (env->cp15.hcr_el2 & HCR_DC) {
11303 * HCR.DC forces the first stage attributes to
11304 * Normal Non-Shareable,
11305 * Inner Write-Back Read-Allocate Write-Allocate,
11306 * Outer Write-Back Read-Allocate Write-Allocate.
11308 cacheattrs->attrs = 0xff;
11309 cacheattrs->shareability = 0;
11311 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
11317 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
11319 mmu_idx = stage_1_mmu_idx(mmu_idx);
11323 /* The page table entries may downgrade secure to non-secure, but
11324 * cannot upgrade an non-secure translation regime's attributes
11327 attrs->secure = regime_is_secure(env, mmu_idx);
11328 attrs->user = regime_is_user(env, mmu_idx);
11330 /* Fast Context Switch Extension. This doesn't exist at all in v8.
11331 * In v7 and earlier it affects all stage 1 translations.
11333 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
11334 && !arm_feature(env, ARM_FEATURE_V8)) {
11335 if (regime_el(env, mmu_idx) == 3) {
11336 address += env->cp15.fcseidr_s;
11338 address += env->cp15.fcseidr_ns;
11342 if (arm_feature(env, ARM_FEATURE_PMSA)) {
11344 *page_size = TARGET_PAGE_SIZE;
11346 if (arm_feature(env, ARM_FEATURE_V8)) {
11348 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
11349 phys_ptr, attrs, prot, page_size, fi);
11350 } else if (arm_feature(env, ARM_FEATURE_V7)) {
11352 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
11353 phys_ptr, prot, page_size, fi);
11356 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
11357 phys_ptr, prot, fi);
11359 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
11360 " mmu_idx %u -> %s (prot %c%c%c)\n",
11361 access_type == MMU_DATA_LOAD ? "reading" :
11362 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
11363 (uint32_t)address, mmu_idx,
11364 ret ? "Miss" : "Hit",
11365 *prot & PAGE_READ ? 'r' : '-',
11366 *prot & PAGE_WRITE ? 'w' : '-',
11367 *prot & PAGE_EXEC ? 'x' : '-');
11372 /* Definitely a real MMU, not an MPU */
11374 if (regime_translation_disabled(env, mmu_idx)) {
11375 /* MMU disabled. */
11376 *phys_ptr = address;
11377 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11378 *page_size = TARGET_PAGE_SIZE;
11382 if (regime_using_lpae_format(env, mmu_idx)) {
11383 return get_phys_addr_lpae(env, address, access_type, mmu_idx,
11384 phys_ptr, attrs, prot, page_size,
11386 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
11387 return get_phys_addr_v6(env, address, access_type, mmu_idx,
11388 phys_ptr, attrs, prot, page_size, fi);
11390 return get_phys_addr_v5(env, address, access_type, mmu_idx,
11391 phys_ptr, prot, page_size, fi);
11395 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
11398 ARMCPU *cpu = ARM_CPU(cs);
11399 CPUARMState *env = &cpu->env;
11401 target_ulong page_size;
11404 ARMMMUFaultInfo fi = {};
11405 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
11407 *attrs = (MemTxAttrs) {};
11409 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
11410 attrs, &prot, &page_size, &fi, NULL);
11420 /* Note that signed overflow is undefined in C. The following routines are
11421 careful to use unsigned types where modulo arithmetic is required.
11422 Failure to do so _will_ break on newer gcc. */
11424 /* Signed saturating arithmetic. */
11426 /* Perform 16-bit signed saturating addition. */
11427 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11432 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11441 /* Perform 8-bit signed saturating addition. */
11442 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11447 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11456 /* Perform 16-bit signed saturating subtraction. */
11457 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11462 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11471 /* Perform 8-bit signed saturating subtraction. */
11472 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11477 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11486 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11487 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11488 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11489 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11492 #include "op_addsub.h"
11494 /* Unsigned saturating arithmetic. */
11495 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11504 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11512 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11521 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11529 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11530 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11531 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11532 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11535 #include "op_addsub.h"
11537 /* Signed modulo arithmetic. */
11538 #define SARITH16(a, b, n, op) do { \
11540 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
11541 RESULT(sum, n, 16); \
11543 ge |= 3 << (n * 2); \
11546 #define SARITH8(a, b, n, op) do { \
11548 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
11549 RESULT(sum, n, 8); \
11555 #define ADD16(a, b, n) SARITH16(a, b, n, +)
11556 #define SUB16(a, b, n) SARITH16(a, b, n, -)
11557 #define ADD8(a, b, n) SARITH8(a, b, n, +)
11558 #define SUB8(a, b, n) SARITH8(a, b, n, -)
11562 #include "op_addsub.h"
11564 /* Unsigned modulo arithmetic. */
11565 #define ADD16(a, b, n) do { \
11567 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11568 RESULT(sum, n, 16); \
11569 if ((sum >> 16) == 1) \
11570 ge |= 3 << (n * 2); \
11573 #define ADD8(a, b, n) do { \
11575 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11576 RESULT(sum, n, 8); \
11577 if ((sum >> 8) == 1) \
11581 #define SUB16(a, b, n) do { \
11583 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11584 RESULT(sum, n, 16); \
11585 if ((sum >> 16) == 0) \
11586 ge |= 3 << (n * 2); \
11589 #define SUB8(a, b, n) do { \
11591 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11592 RESULT(sum, n, 8); \
11593 if ((sum >> 8) == 0) \
11600 #include "op_addsub.h"
11602 /* Halved signed arithmetic. */
11603 #define ADD16(a, b, n) \
11604 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11605 #define SUB16(a, b, n) \
11606 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11607 #define ADD8(a, b, n) \
11608 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11609 #define SUB8(a, b, n) \
11610 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11613 #include "op_addsub.h"
11615 /* Halved unsigned arithmetic. */
11616 #define ADD16(a, b, n) \
11617 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11618 #define SUB16(a, b, n) \
11619 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11620 #define ADD8(a, b, n) \
11621 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11622 #define SUB8(a, b, n) \
11623 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11626 #include "op_addsub.h"
11628 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11636 /* Unsigned sum of absolute byte differences. */
11637 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11640 sum = do_usad(a, b);
11641 sum += do_usad(a >> 8, b >> 8);
11642 sum += do_usad(a >> 16, b >>16);
11643 sum += do_usad(a >> 24, b >> 24);
11647 /* For ARMv6 SEL instruction. */
11648 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11660 mask |= 0xff000000;
11661 return (a & mask) | (b & ~mask);
11665 * The upper bytes of val (above the number specified by 'bytes') must have
11666 * been zeroed out by the caller.
11668 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11672 stl_le_p(buf, val);
11674 /* zlib crc32 converts the accumulator and output to one's complement. */
11675 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11678 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11682 stl_le_p(buf, val);
11684 /* Linux crc32c converts the output to one's complement. */
11685 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11688 /* Return the exception level to which FP-disabled exceptions should
11689 * be taken, or 0 if FP is enabled.
11691 int fp_exception_el(CPUARMState *env, int cur_el)
11693 #ifndef CONFIG_USER_ONLY
11696 /* CPACR and the CPTR registers don't exist before v6, so FP is
11697 * always accessible
11699 if (!arm_feature(env, ARM_FEATURE_V6)) {
11703 if (arm_feature(env, ARM_FEATURE_M)) {
11704 /* CPACR can cause a NOCP UsageFault taken to current security state */
11705 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11709 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11710 if (!extract32(env->v7m.nsacr, 10, 1)) {
11711 /* FP insns cause a NOCP UsageFault taken to Secure */
11719 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11720 * 0, 2 : trap EL0 and EL1/PL1 accesses
11721 * 1 : trap only EL0 accesses
11722 * 3 : trap no accesses
11724 fpen = extract32(env->cp15.cpacr_el1, 20, 2);
11728 if (cur_el == 0 || cur_el == 1) {
11729 /* Trap to PL1, which might be EL1 or EL3 */
11730 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
11735 if (cur_el == 3 && !is_a64(env)) {
11736 /* Secure PL1 running at EL3 */
11750 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11751 * to control non-secure access to the FPU. It doesn't have any
11752 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11754 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11755 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11756 if (!extract32(env->cp15.nsacr, 10, 1)) {
11757 /* FP insns act as UNDEF */
11758 return cur_el == 2 ? 2 : 1;
11762 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
11763 * check because zero bits in the registers mean "don't trap".
11766 /* CPTR_EL2 : present in v7VE or v8 */
11767 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
11768 && !arm_is_secure_below_el3(env)) {
11769 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
11773 /* CPTR_EL3 : present in v8 */
11774 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
11775 /* Trap all FP ops to EL3 */
11782 /* Return the exception level we're running at if this is our mmu_idx */
11783 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11785 if (mmu_idx & ARM_MMU_IDX_M) {
11786 return mmu_idx & ARM_MMU_IDX_M_PRIV;
11790 case ARMMMUIdx_E10_0:
11791 case ARMMMUIdx_E20_0:
11792 case ARMMMUIdx_SE10_0:
11794 case ARMMMUIdx_E10_1:
11795 case ARMMMUIdx_SE10_1:
11798 case ARMMMUIdx_E20_2:
11800 case ARMMMUIdx_SE3:
11803 g_assert_not_reached();
11808 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11810 g_assert_not_reached();
11814 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11816 if (arm_feature(env, ARM_FEATURE_M)) {
11817 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11820 /* See ARM pseudo-function ELIsInHost. */
11823 if (arm_is_secure_below_el3(env)) {
11824 return ARMMMUIdx_SE10_0;
11826 if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)
11827 && arm_el_is_aa64(env, 2)) {
11828 return ARMMMUIdx_E20_0;
11830 return ARMMMUIdx_E10_0;
11832 if (arm_is_secure_below_el3(env)) {
11833 return ARMMMUIdx_SE10_1;
11835 return ARMMMUIdx_E10_1;
11837 /* TODO: ARMv8.4-SecEL2 */
11838 /* Note that TGE does not apply at EL2. */
11839 if ((env->cp15.hcr_el2 & HCR_E2H) && arm_el_is_aa64(env, 2)) {
11840 return ARMMMUIdx_E20_2;
11842 return ARMMMUIdx_E2;
11844 return ARMMMUIdx_SE3;
11846 g_assert_not_reached();
11850 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11852 return arm_mmu_idx_el(env, arm_current_el(env));
11855 int cpu_mmu_index(CPUARMState *env, bool ifetch)
11857 return arm_to_core_mmu_idx(arm_mmu_idx(env));
11860 #ifndef CONFIG_USER_ONLY
11861 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
11863 return stage_1_mmu_idx(arm_mmu_idx(env));
11867 static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el,
11868 ARMMMUIdx mmu_idx, uint32_t flags)
11870 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
11871 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX,
11872 arm_to_core_mmu_idx(mmu_idx));
11874 if (arm_singlestep_active(env)) {
11875 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
11880 static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11881 ARMMMUIdx mmu_idx, uint32_t flags)
11883 bool sctlr_b = arm_sctlr_b(env);
11886 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1);
11888 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
11889 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
11891 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
11893 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11896 static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el,
11899 uint32_t flags = 0;
11901 if (arm_v7m_is_handler_mode(env)) {
11902 flags = FIELD_DP32(flags, TBFLAG_M32, HANDLER, 1);
11906 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11907 * is suppressing them because the requested execution priority
11910 if (arm_feature(env, ARM_FEATURE_V8) &&
11911 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11912 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11913 flags = FIELD_DP32(flags, TBFLAG_M32, STACKCHECK, 1);
11916 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11919 static uint32_t rebuild_hflags_aprofile(CPUARMState *env)
11923 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL,
11924 arm_debug_target_el(env));
11928 static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el,
11931 uint32_t flags = rebuild_hflags_aprofile(env);
11933 if (arm_el_is_aa64(env, 1)) {
11934 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
11937 if (arm_current_el(env) < 2 && env->cp15.hstr_el2 &&
11938 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11939 flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1);
11942 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11945 static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11948 uint32_t flags = rebuild_hflags_aprofile(env);
11949 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11950 ARMVAParameters p0 = aa64_va_parameters_both(env, 0, stage1);
11954 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
11956 /* Get control bits for tagged addresses. */
11957 if (regime_has_2_ranges(mmu_idx)) {
11958 ARMVAParameters p1 = aa64_va_parameters_both(env, -1, stage1);
11959 tbid = (p1.tbi << 1) | p0.tbi;
11960 tbii = tbid & ~((p1.tbid << 1) | p0.tbid);
11963 tbii = tbid & !p0.tbid;
11966 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
11967 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
11969 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11970 int sve_el = sve_exception_el(env, el);
11974 * If SVE is disabled, but FP is enabled,
11975 * then the effective len is 0.
11977 if (sve_el != 0 && fp_el == 0) {
11980 zcr_len = sve_zcr_len_for_el(env, el);
11982 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
11983 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
11986 sctlr = regime_sctlr(env, stage1);
11988 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11989 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
11992 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11994 * In order to save space in flags, we record only whether
11995 * pauth is "inactive", meaning all insns are implemented as
11996 * a nop, or "active" when some action must be performed.
11997 * The decision of which action to take is left to a helper.
11999 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
12000 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
12004 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12005 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
12006 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
12007 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
12011 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12014 static uint32_t rebuild_hflags_internal(CPUARMState *env)
12016 int el = arm_current_el(env);
12017 int fp_el = fp_exception_el(env, el);
12018 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12021 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12022 } else if (arm_feature(env, ARM_FEATURE_M)) {
12023 return rebuild_hflags_m32(env, fp_el, mmu_idx);
12025 return rebuild_hflags_a32(env, fp_el, mmu_idx);
12029 void arm_rebuild_hflags(CPUARMState *env)
12031 env->hflags = rebuild_hflags_internal(env);
12034 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
12036 int fp_el = fp_exception_el(env, el);
12037 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12039 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
12043 * If we have triggered a EL state change we can't rely on the
12044 * translator having passed it too us, we need to recompute.
12046 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
12048 int el = arm_current_el(env);
12049 int fp_el = fp_exception_el(env, el);
12050 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12051 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12054 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
12056 int fp_el = fp_exception_el(env, el);
12057 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12059 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12062 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
12064 int fp_el = fp_exception_el(env, el);
12065 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12067 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12070 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
12072 #ifdef CONFIG_DEBUG_TCG
12073 uint32_t env_flags_current = env->hflags;
12074 uint32_t env_flags_rebuilt = rebuild_hflags_internal(env);
12076 if (unlikely(env_flags_current != env_flags_rebuilt)) {
12077 fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n",
12078 env_flags_current, env_flags_rebuilt);
12084 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12085 target_ulong *cs_base, uint32_t *pflags)
12087 uint32_t flags = env->hflags;
12088 uint32_t pstate_for_ss;
12091 assert_hflags_rebuild_correctly(env);
12093 if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) {
12095 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12096 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
12098 pstate_for_ss = env->pstate;
12100 *pc = env->regs[15];
12102 if (arm_feature(env, ARM_FEATURE_M)) {
12103 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12104 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12105 != env->v7m.secure) {
12106 flags = FIELD_DP32(flags, TBFLAG_M32, FPCCR_S_WRONG, 1);
12109 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12110 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12111 (env->v7m.secure &&
12112 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12114 * ASPEN is set, but FPCA/SFPA indicate that there is no
12115 * active FP context; we must create a new FP context before
12116 * executing any FP insn.
12118 flags = FIELD_DP32(flags, TBFLAG_M32, NEW_FP_CTXT_NEEDED, 1);
12121 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12122 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12123 flags = FIELD_DP32(flags, TBFLAG_M32, LSPACT, 1);
12127 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12128 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12130 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12131 flags = FIELD_DP32(flags, TBFLAG_A32,
12132 XSCALE_CPAR, env->cp15.c15_cpar);
12134 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN,
12136 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE,
12137 env->vfp.vec_stride);
12139 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12140 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12144 flags = FIELD_DP32(flags, TBFLAG_AM32, THUMB, env->thumb);
12145 flags = FIELD_DP32(flags, TBFLAG_AM32, CONDEXEC, env->condexec_bits);
12146 pstate_for_ss = env->uncached_cpsr;
12150 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12151 * states defined in the ARM ARM for software singlestep:
12152 * SS_ACTIVE PSTATE.SS State
12153 * 0 x Inactive (the TB flag for SS is always 0)
12154 * 1 0 Active-pending
12155 * 1 1 Active-not-pending
12156 * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB.
12158 if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) &&
12159 (pstate_for_ss & PSTATE_SS)) {
12160 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
12166 #ifdef TARGET_AARCH64
12168 * The manual says that when SVE is enabled and VQ is widened the
12169 * implementation is allowed to zero the previously inaccessible
12170 * portion of the registers. The corollary to that is that when
12171 * SVE is enabled and VQ is narrowed we are also allowed to zero
12172 * the now inaccessible portion of the registers.
12174 * The intent of this is that no predicate bit beyond VQ is ever set.
12175 * Which means that some operations on predicate registers themselves
12176 * may operate on full uint64_t or even unrolled across the maximum
12177 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12178 * may well be cheaper than conditionals to restrict the operation
12179 * to the relevant portion of a uint16_t[16].
12181 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12186 assert(vq >= 1 && vq <= ARM_MAX_VQ);
12187 assert(vq <= env_archcpu(env)->sve_max_vq);
12189 /* Zap the high bits of the zregs. */
12190 for (i = 0; i < 32; i++) {
12191 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12194 /* Zap the high bits of the pregs and ffr. */
12197 pmask = ~(-1ULL << (16 * (vq & 3)));
12199 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12200 for (i = 0; i < 17; ++i) {
12201 env->vfp.pregs[i].p[j] &= pmask;
12208 * Notice a change in SVE vector size when changing EL.
12210 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12211 int new_el, bool el0_a64)
12213 ARMCPU *cpu = env_archcpu(env);
12214 int old_len, new_len;
12215 bool old_a64, new_a64;
12217 /* Nothing to do if no SVE. */
12218 if (!cpu_isar_feature(aa64_sve, cpu)) {
12222 /* Nothing to do if FP is disabled in either EL. */
12223 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12228 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12229 * at ELx, or not available because the EL is in AArch32 state, then
12230 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12231 * has an effective value of 0".
12233 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12234 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12235 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12236 * we already have the correct register contents when encountering the
12237 * vq0->vq0 transition between EL0->EL1.
12239 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12240 old_len = (old_a64 && !sve_exception_el(env, old_el)
12241 ? sve_zcr_len_for_el(env, old_el) : 0);
12242 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12243 new_len = (new_a64 && !sve_exception_el(env, new_el)
12244 ? sve_zcr_len_for_el(env, new_el) : 0);
12246 /* When changing vector length, clear inaccessible state. */
12247 if (new_len < old_len) {
12248 aarch64_sve_narrow_vq(env, new_len + 1);