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"
14 #include "internals.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/host-utils.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/timer.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 "semihosting/semihost.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/cpu-timers.h"
28 #include "sysemu/kvm.h"
29 #include "qemu/range.h"
30 #include "qapi/qapi-commands-machine-target.h"
31 #include "qapi/error.h"
32 #include "qemu/guest-random.h"
35 #include "exec/cpu_ldst.h"
36 #include "semihosting/common-semi.h"
40 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
42 static void switch_mode(CPUARMState *env, int mode);
44 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
46 assert(ri->fieldoffset);
47 if (cpreg_field_is_64bit(ri)) {
48 return CPREG_FIELD64(env, ri);
50 return CPREG_FIELD32(env, ri);
54 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
56 assert(ri->fieldoffset);
57 if (cpreg_field_is_64bit(ri)) {
58 CPREG_FIELD64(env, ri) = value;
60 CPREG_FIELD32(env, ri) = value;
64 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
66 return (char *)env + ri->fieldoffset;
69 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
71 /* Raw read of a coprocessor register (as needed for migration, etc). */
72 if (ri->type & ARM_CP_CONST) {
73 return ri->resetvalue;
74 } else if (ri->raw_readfn) {
75 return ri->raw_readfn(env, ri);
76 } else if (ri->readfn) {
77 return ri->readfn(env, ri);
79 return raw_read(env, ri);
83 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
86 /* Raw write of a coprocessor register (as needed for migration, etc).
87 * Note that constant registers are treated as write-ignored; the
88 * caller should check for success by whether a readback gives the
91 if (ri->type & ARM_CP_CONST) {
93 } else if (ri->raw_writefn) {
94 ri->raw_writefn(env, ri, v);
95 } else if (ri->writefn) {
96 ri->writefn(env, ri, v);
98 raw_write(env, ri, v);
102 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
104 /* Return true if the regdef would cause an assertion if you called
105 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
106 * program bug for it not to have the NO_RAW flag).
107 * NB that returning false here doesn't necessarily mean that calling
108 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
109 * read/write access functions which are safe for raw use" from "has
110 * read/write access functions which have side effects but has forgotten
111 * to provide raw access functions".
112 * The tests here line up with the conditions in read/write_raw_cp_reg()
113 * and assertions in raw_read()/raw_write().
115 if ((ri->type & ARM_CP_CONST) ||
117 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
123 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
125 /* Write the coprocessor state from cpu->env to the (index,value) list. */
129 for (i = 0; i < cpu->cpreg_array_len; i++) {
130 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
131 const ARMCPRegInfo *ri;
134 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
139 if (ri->type & ARM_CP_NO_RAW) {
143 newval = read_raw_cp_reg(&cpu->env, ri);
146 * Only sync if the previous list->cpustate sync succeeded.
147 * Rather than tracking the success/failure state for every
148 * item in the list, we just recheck "does the raw write we must
149 * have made in write_list_to_cpustate() read back OK" here.
151 uint64_t oldval = cpu->cpreg_values[i];
153 if (oldval == newval) {
157 write_raw_cp_reg(&cpu->env, ri, oldval);
158 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
162 write_raw_cp_reg(&cpu->env, ri, newval);
164 cpu->cpreg_values[i] = newval;
169 bool write_list_to_cpustate(ARMCPU *cpu)
174 for (i = 0; i < cpu->cpreg_array_len; i++) {
175 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
176 uint64_t v = cpu->cpreg_values[i];
177 const ARMCPRegInfo *ri;
179 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
184 if (ri->type & ARM_CP_NO_RAW) {
187 /* Write value and confirm it reads back as written
188 * (to catch read-only registers and partially read-only
189 * registers where the incoming migration value doesn't match)
191 write_raw_cp_reg(&cpu->env, ri, v);
192 if (read_raw_cp_reg(&cpu->env, ri) != v) {
199 static void add_cpreg_to_list(gpointer key, gpointer opaque)
201 ARMCPU *cpu = opaque;
202 uint32_t regidx = (uintptr_t)key;
203 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
205 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
206 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
207 /* The value array need not be initialized at this point */
208 cpu->cpreg_array_len++;
212 static void count_cpreg(gpointer key, gpointer opaque)
214 ARMCPU *cpu = opaque;
215 const ARMCPRegInfo *ri;
217 ri = g_hash_table_lookup(cpu->cp_regs, key);
219 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
220 cpu->cpreg_array_len++;
224 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
226 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
227 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
238 void init_cpreg_list(ARMCPU *cpu)
240 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
241 * Note that we require cpreg_tuples[] to be sorted by key ID.
246 keys = g_hash_table_get_keys(cpu->cp_regs);
247 keys = g_list_sort(keys, cpreg_key_compare);
249 cpu->cpreg_array_len = 0;
251 g_list_foreach(keys, count_cpreg, cpu);
253 arraylen = cpu->cpreg_array_len;
254 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
255 cpu->cpreg_values = g_new(uint64_t, arraylen);
256 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
257 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
258 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
259 cpu->cpreg_array_len = 0;
261 g_list_foreach(keys, add_cpreg_to_list, cpu);
263 assert(cpu->cpreg_array_len == arraylen);
269 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
271 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
272 const ARMCPRegInfo *ri,
275 if (!is_a64(env) && arm_current_el(env) == 3 &&
276 arm_is_secure_below_el3(env)) {
277 return CP_ACCESS_TRAP_UNCATEGORIZED;
282 /* Some secure-only AArch32 registers trap to EL3 if used from
283 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
284 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
285 * We assume that the .access field is set to PL1_RW.
287 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
288 const ARMCPRegInfo *ri,
291 if (arm_current_el(env) == 3) {
294 if (arm_is_secure_below_el3(env)) {
295 if (env->cp15.scr_el3 & SCR_EEL2) {
296 return CP_ACCESS_TRAP_EL2;
298 return CP_ACCESS_TRAP_EL3;
300 /* This will be EL1 NS and EL2 NS, which just UNDEF */
301 return CP_ACCESS_TRAP_UNCATEGORIZED;
304 /* Check for traps to performance monitor registers, which are controlled
305 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
307 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
310 int el = arm_current_el(env);
311 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
313 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
314 return CP_ACCESS_TRAP_EL2;
316 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
317 return CP_ACCESS_TRAP_EL3;
322 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
323 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
326 if (arm_current_el(env) == 1) {
327 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
328 if (arm_hcr_el2_eff(env) & trap) {
329 return CP_ACCESS_TRAP_EL2;
335 /* Check for traps from EL1 due to HCR_EL2.TSW. */
336 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
339 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
340 return CP_ACCESS_TRAP_EL2;
345 /* Check for traps from EL1 due to HCR_EL2.TACR. */
346 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
349 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
350 return CP_ACCESS_TRAP_EL2;
355 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
356 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
359 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
360 return CP_ACCESS_TRAP_EL2;
365 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
367 ARMCPU *cpu = env_archcpu(env);
369 raw_write(env, ri, value);
370 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
373 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
375 ARMCPU *cpu = env_archcpu(env);
377 if (raw_read(env, ri) != value) {
378 /* Unlike real hardware the qemu TLB uses virtual addresses,
379 * not modified virtual addresses, so this causes a TLB flush.
382 raw_write(env, ri, value);
386 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
389 ARMCPU *cpu = env_archcpu(env);
391 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
392 && !extended_addresses_enabled(env)) {
393 /* For VMSA (when not using the LPAE long descriptor page table
394 * format) this register includes the ASID, so do a TLB flush.
395 * For PMSA it is purely a process ID and no action is needed.
399 raw_write(env, ri, value);
402 /* IS variants of TLB operations must affect all cores */
403 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
406 CPUState *cs = env_cpu(env);
408 tlb_flush_all_cpus_synced(cs);
411 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
414 CPUState *cs = env_cpu(env);
416 tlb_flush_all_cpus_synced(cs);
419 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
422 CPUState *cs = env_cpu(env);
424 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
427 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
430 CPUState *cs = env_cpu(env);
432 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
436 * Non-IS variants of TLB operations are upgraded to
437 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
438 * force broadcast of these operations.
440 static bool tlb_force_broadcast(CPUARMState *env)
442 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
445 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
448 /* Invalidate all (TLBIALL) */
449 CPUState *cs = env_cpu(env);
451 if (tlb_force_broadcast(env)) {
452 tlb_flush_all_cpus_synced(cs);
458 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
461 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
462 CPUState *cs = env_cpu(env);
464 value &= TARGET_PAGE_MASK;
465 if (tlb_force_broadcast(env)) {
466 tlb_flush_page_all_cpus_synced(cs, value);
468 tlb_flush_page(cs, value);
472 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
475 /* Invalidate by ASID (TLBIASID) */
476 CPUState *cs = env_cpu(env);
478 if (tlb_force_broadcast(env)) {
479 tlb_flush_all_cpus_synced(cs);
485 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
488 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
489 CPUState *cs = env_cpu(env);
491 value &= TARGET_PAGE_MASK;
492 if (tlb_force_broadcast(env)) {
493 tlb_flush_page_all_cpus_synced(cs, value);
495 tlb_flush_page(cs, value);
499 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
502 CPUState *cs = env_cpu(env);
504 tlb_flush_by_mmuidx(cs,
506 ARMMMUIdxBit_E10_1_PAN |
510 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
513 CPUState *cs = env_cpu(env);
515 tlb_flush_by_mmuidx_all_cpus_synced(cs,
517 ARMMMUIdxBit_E10_1_PAN |
522 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
525 CPUState *cs = env_cpu(env);
527 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
530 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
533 CPUState *cs = env_cpu(env);
535 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
538 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
541 CPUState *cs = env_cpu(env);
542 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
544 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
547 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
550 CPUState *cs = env_cpu(env);
551 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
553 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
557 static const ARMCPRegInfo cp_reginfo[] = {
558 /* Define the secure and non-secure FCSE identifier CP registers
559 * separately because there is no secure bank in V8 (no _EL3). This allows
560 * the secure register to be properly reset and migrated. There is also no
561 * v8 EL1 version of the register so the non-secure instance stands alone.
564 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
565 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
566 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
567 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
568 { .name = "FCSEIDR_S",
569 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
570 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
571 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
572 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
573 /* Define the secure and non-secure context identifier CP registers
574 * separately because there is no secure bank in V8 (no _EL3). This allows
575 * the secure register to be properly reset and migrated. In the
576 * non-secure case, the 32-bit register will have reset and migration
577 * disabled during registration as it is handled by the 64-bit instance.
579 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
580 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
581 .access = PL1_RW, .accessfn = access_tvm_trvm,
582 .secure = ARM_CP_SECSTATE_NS,
583 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
584 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
585 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
586 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
587 .access = PL1_RW, .accessfn = access_tvm_trvm,
588 .secure = ARM_CP_SECSTATE_S,
589 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
590 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
593 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
594 /* NB: Some of these registers exist in v8 but with more precise
595 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
597 /* MMU Domain access control / MPU write buffer control */
599 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
600 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
601 .writefn = dacr_write, .raw_writefn = raw_write,
602 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
603 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
604 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
605 * For v6 and v5, these mappings are overly broad.
607 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
608 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
609 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
610 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
611 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
612 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
613 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
614 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
615 /* Cache maintenance ops; some of this space may be overridden later. */
616 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
617 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
618 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
621 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
622 /* Not all pre-v6 cores implemented this WFI, so this is slightly
625 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
626 .access = PL1_W, .type = ARM_CP_WFI },
629 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
630 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
631 * is UNPREDICTABLE; we choose to NOP as most implementations do).
633 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
634 .access = PL1_W, .type = ARM_CP_WFI },
635 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
636 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
637 * OMAPCP will override this space.
639 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
640 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
642 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
643 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
645 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
646 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
647 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
649 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
650 * implementing it as RAZ means the "debug architecture version" bits
651 * will read as a reserved value, which should cause Linux to not try
652 * to use the debug hardware.
654 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
655 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
656 /* MMU TLB control. Note that the wildcarding means we cover not just
657 * the unified TLB ops but also the dside/iside/inner-shareable variants.
659 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
660 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
661 .type = ARM_CP_NO_RAW },
662 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
663 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
664 .type = ARM_CP_NO_RAW },
665 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
666 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
667 .type = ARM_CP_NO_RAW },
668 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
669 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
670 .type = ARM_CP_NO_RAW },
671 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
672 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
673 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
674 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
677 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
682 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
683 if (!arm_feature(env, ARM_FEATURE_V8)) {
684 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
685 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
686 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
688 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
689 /* VFP coprocessor: cp10 & cp11 [23:20] */
690 mask |= R_CPACR_ASEDIS_MASK |
691 R_CPACR_D32DIS_MASK |
695 if (!arm_feature(env, ARM_FEATURE_NEON)) {
696 /* ASEDIS [31] bit is RAO/WI */
697 value |= R_CPACR_ASEDIS_MASK;
700 /* VFPv3 and upwards with NEON implement 32 double precision
701 * registers (D0-D31).
703 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
704 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
705 value |= R_CPACR_D32DIS_MASK;
712 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
713 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
715 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
716 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
717 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
718 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
721 env->cp15.cpacr_el1 = value;
724 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
727 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
728 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
730 uint64_t value = env->cp15.cpacr_el1;
732 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
733 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
734 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
740 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
742 /* Call cpacr_write() so that we reset with the correct RAO bits set
743 * for our CPU features.
745 cpacr_write(env, ri, 0);
748 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
751 if (arm_feature(env, ARM_FEATURE_V8)) {
752 /* Check if CPACR accesses are to be trapped to EL2 */
753 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
754 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
755 return CP_ACCESS_TRAP_EL2;
756 /* Check if CPACR accesses are to be trapped to EL3 */
757 } else if (arm_current_el(env) < 3 &&
758 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
759 return CP_ACCESS_TRAP_EL3;
766 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
769 /* Check if CPTR accesses are set to trap to EL3 */
770 if (arm_current_el(env) == 2 &&
771 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
772 return CP_ACCESS_TRAP_EL3;
778 static const ARMCPRegInfo v6_cp_reginfo[] = {
779 /* prefetch by MVA in v6, NOP in v7 */
780 { .name = "MVA_prefetch",
781 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
782 .access = PL1_W, .type = ARM_CP_NOP },
783 /* We need to break the TB after ISB to execute self-modifying code
784 * correctly and also to take any pending interrupts immediately.
785 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
787 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
788 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
789 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
790 .access = PL0_W, .type = ARM_CP_NOP },
791 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
792 .access = PL0_W, .type = ARM_CP_NOP },
793 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
794 .access = PL1_RW, .accessfn = access_tvm_trvm,
795 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
796 offsetof(CPUARMState, cp15.ifar_ns) },
798 /* Watchpoint Fault Address Register : should actually only be present
799 * for 1136, 1176, 11MPCore.
801 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
802 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
803 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
804 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
805 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
806 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
809 typedef struct pm_event {
810 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
811 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
812 bool (*supported)(CPUARMState *);
814 * Retrieve the current count of the underlying event. The programmed
815 * counters hold a difference from the return value from this function
817 uint64_t (*get_count)(CPUARMState *);
819 * Return how many nanoseconds it will take (at a minimum) for count events
820 * to occur. A negative value indicates the counter will never overflow, or
821 * that the counter has otherwise arranged for the overflow bit to be set
822 * and the PMU interrupt to be raised on overflow.
824 int64_t (*ns_per_count)(uint64_t);
827 static bool event_always_supported(CPUARMState *env)
832 static uint64_t swinc_get_count(CPUARMState *env)
835 * SW_INCR events are written directly to the pmevcntr's by writes to
836 * PMSWINC, so there is no underlying count maintained by the PMU itself
841 static int64_t swinc_ns_per(uint64_t ignored)
847 * Return the underlying cycle count for the PMU cycle counters. If we're in
848 * usermode, simply return 0.
850 static uint64_t cycles_get_count(CPUARMState *env)
852 #ifndef CONFIG_USER_ONLY
853 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
854 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
856 return cpu_get_host_ticks();
860 #ifndef CONFIG_USER_ONLY
861 static int64_t cycles_ns_per(uint64_t cycles)
863 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
866 static bool instructions_supported(CPUARMState *env)
868 return icount_enabled() == 1; /* Precise instruction counting */
871 static uint64_t instructions_get_count(CPUARMState *env)
873 return (uint64_t)icount_get_raw();
876 static int64_t instructions_ns_per(uint64_t icount)
878 return icount_to_ns((int64_t)icount);
882 static bool pmuv3p1_events_supported(CPUARMState *env)
884 /* For events which are supported in any v8.1 PMU */
885 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
888 static bool pmuv3p4_events_supported(CPUARMState *env)
890 /* For events which are supported in any v8.1 PMU */
891 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
894 static uint64_t zero_event_get_count(CPUARMState *env)
896 /* For events which on QEMU never fire, so their count is always zero */
900 static int64_t zero_event_ns_per(uint64_t cycles)
902 /* An event which never fires can never overflow */
906 static const pm_event pm_events[] = {
907 { .number = 0x000, /* SW_INCR */
908 .supported = event_always_supported,
909 .get_count = swinc_get_count,
910 .ns_per_count = swinc_ns_per,
912 #ifndef CONFIG_USER_ONLY
913 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
914 .supported = instructions_supported,
915 .get_count = instructions_get_count,
916 .ns_per_count = instructions_ns_per,
918 { .number = 0x011, /* CPU_CYCLES, Cycle */
919 .supported = event_always_supported,
920 .get_count = cycles_get_count,
921 .ns_per_count = cycles_ns_per,
924 { .number = 0x023, /* STALL_FRONTEND */
925 .supported = pmuv3p1_events_supported,
926 .get_count = zero_event_get_count,
927 .ns_per_count = zero_event_ns_per,
929 { .number = 0x024, /* STALL_BACKEND */
930 .supported = pmuv3p1_events_supported,
931 .get_count = zero_event_get_count,
932 .ns_per_count = zero_event_ns_per,
934 { .number = 0x03c, /* STALL */
935 .supported = pmuv3p4_events_supported,
936 .get_count = zero_event_get_count,
937 .ns_per_count = zero_event_ns_per,
942 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
943 * events (i.e. the statistical profiling extension), this implementation
944 * should first be updated to something sparse instead of the current
945 * supported_event_map[] array.
947 #define MAX_EVENT_ID 0x3c
948 #define UNSUPPORTED_EVENT UINT16_MAX
949 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
952 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
953 * of ARM event numbers to indices in our pm_events array.
955 * Note: Events in the 0x40XX range are not currently supported.
957 void pmu_init(ARMCPU *cpu)
962 * Empty supported_event_map and cpu->pmceid[01] before adding supported
965 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
966 supported_event_map[i] = UNSUPPORTED_EVENT;
971 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
972 const pm_event *cnt = &pm_events[i];
973 assert(cnt->number <= MAX_EVENT_ID);
974 /* We do not currently support events in the 0x40xx range */
975 assert(cnt->number <= 0x3f);
977 if (cnt->supported(&cpu->env)) {
978 supported_event_map[cnt->number] = i;
979 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
980 if (cnt->number & 0x20) {
981 cpu->pmceid1 |= event_mask;
983 cpu->pmceid0 |= event_mask;
990 * Check at runtime whether a PMU event is supported for the current machine
992 static bool event_supported(uint16_t number)
994 if (number > MAX_EVENT_ID) {
997 return supported_event_map[number] != UNSUPPORTED_EVENT;
1000 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1003 /* Performance monitor registers user accessibility is controlled
1004 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1005 * trapping to EL2 or EL3 for other accesses.
1007 int el = arm_current_el(env);
1008 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1010 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1011 return CP_ACCESS_TRAP;
1013 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1014 return CP_ACCESS_TRAP_EL2;
1016 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1017 return CP_ACCESS_TRAP_EL3;
1020 return CP_ACCESS_OK;
1023 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1024 const ARMCPRegInfo *ri,
1027 /* ER: event counter read trap control */
1028 if (arm_feature(env, ARM_FEATURE_V8)
1029 && arm_current_el(env) == 0
1030 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1032 return CP_ACCESS_OK;
1035 return pmreg_access(env, ri, isread);
1038 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1039 const ARMCPRegInfo *ri,
1042 /* SW: software increment write trap control */
1043 if (arm_feature(env, ARM_FEATURE_V8)
1044 && arm_current_el(env) == 0
1045 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1047 return CP_ACCESS_OK;
1050 return pmreg_access(env, ri, isread);
1053 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1054 const ARMCPRegInfo *ri,
1057 /* ER: event counter read trap control */
1058 if (arm_feature(env, ARM_FEATURE_V8)
1059 && arm_current_el(env) == 0
1060 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1061 return CP_ACCESS_OK;
1064 return pmreg_access(env, ri, isread);
1067 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1068 const ARMCPRegInfo *ri,
1071 /* CR: cycle counter read trap control */
1072 if (arm_feature(env, ARM_FEATURE_V8)
1073 && arm_current_el(env) == 0
1074 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1076 return CP_ACCESS_OK;
1079 return pmreg_access(env, ri, isread);
1083 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1084 * We use these to decide whether we need to wrap a write to MDCR_EL2
1085 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1087 #define MDCR_EL2_PMU_ENABLE_BITS (MDCR_HPME | MDCR_HPMD | MDCR_HPMN)
1088 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME)
1090 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1091 * the current EL, security state, and register configuration.
1093 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1096 bool e, p, u, nsk, nsu, nsh, m;
1097 bool enabled, prohibited = false, filtered;
1098 bool secure = arm_is_secure(env);
1099 int el = arm_current_el(env);
1100 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1101 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1103 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1107 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1108 (counter < hpmn || counter == 31)) {
1109 e = env->cp15.c9_pmcr & PMCRE;
1111 e = mdcr_el2 & MDCR_HPME;
1113 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1115 /* Is event counting prohibited? */
1116 if (el == 2 && (counter < hpmn || counter == 31)) {
1117 prohibited = mdcr_el2 & MDCR_HPMD;
1120 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1123 if (prohibited && counter == 31) {
1124 prohibited = env->cp15.c9_pmcr & PMCRDP;
1127 if (counter == 31) {
1128 filter = env->cp15.pmccfiltr_el0;
1130 filter = env->cp15.c14_pmevtyper[counter];
1133 p = filter & PMXEVTYPER_P;
1134 u = filter & PMXEVTYPER_U;
1135 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1136 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1137 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1138 m = arm_el_is_aa64(env, 1) &&
1139 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1142 filtered = secure ? u : u != nsu;
1143 } else if (el == 1) {
1144 filtered = secure ? p : p != nsk;
1145 } else if (el == 2) {
1151 if (counter != 31) {
1153 * If not checking PMCCNTR, ensure the counter is setup to an event we
1156 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1157 if (!event_supported(event)) {
1162 return enabled && !prohibited && !filtered;
1165 static void pmu_update_irq(CPUARMState *env)
1167 ARMCPU *cpu = env_archcpu(env);
1168 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1169 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1172 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1175 * Return true if the clock divider is enabled and the cycle counter
1176 * is supposed to tick only once every 64 clock cycles. This is
1177 * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1178 * (64-bit) cycle counter PMCR.D has no effect.
1180 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1184 * Ensure c15_ccnt is the guest-visible count so that operations such as
1185 * enabling/disabling the counter or filtering, modifying the count itself,
1186 * etc. can be done logically. This is essentially a no-op if the counter is
1187 * not enabled at the time of the call.
1189 static void pmccntr_op_start(CPUARMState *env)
1191 uint64_t cycles = cycles_get_count(env);
1193 if (pmu_counter_enabled(env, 31)) {
1194 uint64_t eff_cycles = cycles;
1195 if (pmccntr_clockdiv_enabled(env)) {
1199 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1201 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1202 1ull << 63 : 1ull << 31;
1203 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1204 env->cp15.c9_pmovsr |= (1ULL << 31);
1205 pmu_update_irq(env);
1208 env->cp15.c15_ccnt = new_pmccntr;
1210 env->cp15.c15_ccnt_delta = cycles;
1214 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1215 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1218 static void pmccntr_op_finish(CPUARMState *env)
1220 if (pmu_counter_enabled(env, 31)) {
1221 #ifndef CONFIG_USER_ONLY
1222 /* Calculate when the counter will next overflow */
1223 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1224 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1225 remaining_cycles = (uint32_t)remaining_cycles;
1227 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1229 if (overflow_in > 0) {
1230 int64_t overflow_at;
1232 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1233 overflow_in, &overflow_at)) {
1234 ARMCPU *cpu = env_archcpu(env);
1235 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1240 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1241 if (pmccntr_clockdiv_enabled(env)) {
1244 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1248 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1251 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1253 if (event_supported(event)) {
1254 uint16_t event_idx = supported_event_map[event];
1255 count = pm_events[event_idx].get_count(env);
1258 if (pmu_counter_enabled(env, counter)) {
1259 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1261 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1262 env->cp15.c9_pmovsr |= (1 << counter);
1263 pmu_update_irq(env);
1265 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1267 env->cp15.c14_pmevcntr_delta[counter] = count;
1270 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1272 if (pmu_counter_enabled(env, counter)) {
1273 #ifndef CONFIG_USER_ONLY
1274 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1275 uint16_t event_idx = supported_event_map[event];
1276 uint64_t delta = UINT32_MAX -
1277 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1278 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1280 if (overflow_in > 0) {
1281 int64_t overflow_at;
1283 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1284 overflow_in, &overflow_at)) {
1285 ARMCPU *cpu = env_archcpu(env);
1286 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1291 env->cp15.c14_pmevcntr_delta[counter] -=
1292 env->cp15.c14_pmevcntr[counter];
1296 void pmu_op_start(CPUARMState *env)
1299 pmccntr_op_start(env);
1300 for (i = 0; i < pmu_num_counters(env); i++) {
1301 pmevcntr_op_start(env, i);
1305 void pmu_op_finish(CPUARMState *env)
1308 pmccntr_op_finish(env);
1309 for (i = 0; i < pmu_num_counters(env); i++) {
1310 pmevcntr_op_finish(env, i);
1314 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1316 pmu_op_start(&cpu->env);
1319 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1321 pmu_op_finish(&cpu->env);
1324 void arm_pmu_timer_cb(void *opaque)
1326 ARMCPU *cpu = opaque;
1329 * Update all the counter values based on the current underlying counts,
1330 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1331 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1332 * counter may expire.
1334 pmu_op_start(&cpu->env);
1335 pmu_op_finish(&cpu->env);
1338 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1343 if (value & PMCRC) {
1344 /* The counter has been reset */
1345 env->cp15.c15_ccnt = 0;
1348 if (value & PMCRP) {
1350 for (i = 0; i < pmu_num_counters(env); i++) {
1351 env->cp15.c14_pmevcntr[i] = 0;
1355 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1356 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1361 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1365 for (i = 0; i < pmu_num_counters(env); i++) {
1366 /* Increment a counter's count iff: */
1367 if ((value & (1 << i)) && /* counter's bit is set */
1368 /* counter is enabled and not filtered */
1369 pmu_counter_enabled(env, i) &&
1370 /* counter is SW_INCR */
1371 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1372 pmevcntr_op_start(env, i);
1375 * Detect if this write causes an overflow since we can't predict
1376 * PMSWINC overflows like we can for other events
1378 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1380 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1381 env->cp15.c9_pmovsr |= (1 << i);
1382 pmu_update_irq(env);
1385 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1387 pmevcntr_op_finish(env, i);
1392 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1395 pmccntr_op_start(env);
1396 ret = env->cp15.c15_ccnt;
1397 pmccntr_op_finish(env);
1401 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1404 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1405 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1406 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1409 env->cp15.c9_pmselr = value & 0x1f;
1412 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1415 pmccntr_op_start(env);
1416 env->cp15.c15_ccnt = value;
1417 pmccntr_op_finish(env);
1420 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1423 uint64_t cur_val = pmccntr_read(env, NULL);
1425 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1428 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1431 pmccntr_op_start(env);
1432 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1433 pmccntr_op_finish(env);
1436 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1439 pmccntr_op_start(env);
1440 /* M is not accessible from AArch32 */
1441 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1442 (value & PMCCFILTR);
1443 pmccntr_op_finish(env);
1446 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1448 /* M is not visible in AArch32 */
1449 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1452 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1456 value &= pmu_counter_mask(env);
1457 env->cp15.c9_pmcnten |= value;
1461 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1465 value &= pmu_counter_mask(env);
1466 env->cp15.c9_pmcnten &= ~value;
1470 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1473 value &= pmu_counter_mask(env);
1474 env->cp15.c9_pmovsr &= ~value;
1475 pmu_update_irq(env);
1478 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1481 value &= pmu_counter_mask(env);
1482 env->cp15.c9_pmovsr |= value;
1483 pmu_update_irq(env);
1486 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1487 uint64_t value, const uint8_t counter)
1489 if (counter == 31) {
1490 pmccfiltr_write(env, ri, value);
1491 } else if (counter < pmu_num_counters(env)) {
1492 pmevcntr_op_start(env, counter);
1495 * If this counter's event type is changing, store the current
1496 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1497 * pmevcntr_op_finish has the correct baseline when it converts back to
1500 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1501 PMXEVTYPER_EVTCOUNT;
1502 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1503 if (old_event != new_event) {
1505 if (event_supported(new_event)) {
1506 uint16_t event_idx = supported_event_map[new_event];
1507 count = pm_events[event_idx].get_count(env);
1509 env->cp15.c14_pmevcntr_delta[counter] = count;
1512 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1513 pmevcntr_op_finish(env, counter);
1515 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1516 * PMSELR value is equal to or greater than the number of implemented
1517 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1521 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1522 const uint8_t counter)
1524 if (counter == 31) {
1525 return env->cp15.pmccfiltr_el0;
1526 } else if (counter < pmu_num_counters(env)) {
1527 return env->cp15.c14_pmevtyper[counter];
1530 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1531 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1537 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1540 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1541 pmevtyper_write(env, ri, value, counter);
1544 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1547 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1548 env->cp15.c14_pmevtyper[counter] = value;
1551 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1552 * pmu_op_finish calls when loading saved state for a migration. Because
1553 * we're potentially updating the type of event here, the value written to
1554 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1555 * different counter type. Therefore, we need to set this value to the
1556 * current count for the counter type we're writing so that pmu_op_finish
1557 * has the correct count for its calculation.
1559 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1560 if (event_supported(event)) {
1561 uint16_t event_idx = supported_event_map[event];
1562 env->cp15.c14_pmevcntr_delta[counter] =
1563 pm_events[event_idx].get_count(env);
1567 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1569 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1570 return pmevtyper_read(env, ri, counter);
1573 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1576 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1579 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1581 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1584 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1585 uint64_t value, uint8_t counter)
1587 if (counter < pmu_num_counters(env)) {
1588 pmevcntr_op_start(env, counter);
1589 env->cp15.c14_pmevcntr[counter] = value;
1590 pmevcntr_op_finish(env, counter);
1593 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1594 * are CONSTRAINED UNPREDICTABLE.
1598 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1601 if (counter < pmu_num_counters(env)) {
1603 pmevcntr_op_start(env, counter);
1604 ret = env->cp15.c14_pmevcntr[counter];
1605 pmevcntr_op_finish(env, counter);
1608 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1609 * are CONSTRAINED UNPREDICTABLE. */
1614 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1617 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1618 pmevcntr_write(env, ri, value, counter);
1621 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1623 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1624 return pmevcntr_read(env, ri, counter);
1627 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1630 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1631 assert(counter < pmu_num_counters(env));
1632 env->cp15.c14_pmevcntr[counter] = value;
1633 pmevcntr_write(env, ri, value, counter);
1636 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1638 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1639 assert(counter < pmu_num_counters(env));
1640 return env->cp15.c14_pmevcntr[counter];
1643 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1646 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1649 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1651 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1654 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1657 if (arm_feature(env, ARM_FEATURE_V8)) {
1658 env->cp15.c9_pmuserenr = value & 0xf;
1660 env->cp15.c9_pmuserenr = value & 1;
1664 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1667 /* We have no event counters so only the C bit can be changed */
1668 value &= pmu_counter_mask(env);
1669 env->cp15.c9_pminten |= value;
1670 pmu_update_irq(env);
1673 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1676 value &= pmu_counter_mask(env);
1677 env->cp15.c9_pminten &= ~value;
1678 pmu_update_irq(env);
1681 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1684 /* Note that even though the AArch64 view of this register has bits
1685 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1686 * architectural requirements for bits which are RES0 only in some
1687 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1688 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1690 raw_write(env, ri, value & ~0x1FULL);
1693 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1695 /* Begin with base v8.0 state. */
1696 uint32_t valid_mask = 0x3fff;
1697 ARMCPU *cpu = env_archcpu(env);
1700 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1701 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1702 * Instead, choose the format based on the mode of EL3.
1704 if (arm_el_is_aa64(env, 3)) {
1705 value |= SCR_FW | SCR_AW; /* RES1 */
1706 valid_mask &= ~SCR_NET; /* RES0 */
1708 if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1709 !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1710 value |= SCR_RW; /* RAO/WI */
1712 if (cpu_isar_feature(aa64_ras, cpu)) {
1713 valid_mask |= SCR_TERR;
1715 if (cpu_isar_feature(aa64_lor, cpu)) {
1716 valid_mask |= SCR_TLOR;
1718 if (cpu_isar_feature(aa64_pauth, cpu)) {
1719 valid_mask |= SCR_API | SCR_APK;
1721 if (cpu_isar_feature(aa64_sel2, cpu)) {
1722 valid_mask |= SCR_EEL2;
1724 if (cpu_isar_feature(aa64_mte, cpu)) {
1725 valid_mask |= SCR_ATA;
1727 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1728 valid_mask |= SCR_ENSCXT;
1730 if (cpu_isar_feature(aa64_doublefault, cpu)) {
1731 valid_mask |= SCR_EASE | SCR_NMEA;
1734 valid_mask &= ~(SCR_RW | SCR_ST);
1735 if (cpu_isar_feature(aa32_ras, cpu)) {
1736 valid_mask |= SCR_TERR;
1740 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1741 valid_mask &= ~SCR_HCE;
1743 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1744 * supported if EL2 exists. The bit is UNK/SBZP when
1745 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1746 * when EL2 is unavailable.
1747 * On ARMv8, this bit is always available.
1749 if (arm_feature(env, ARM_FEATURE_V7) &&
1750 !arm_feature(env, ARM_FEATURE_V8)) {
1751 valid_mask &= ~SCR_SMD;
1755 /* Clear all-context RES0 bits. */
1756 value &= valid_mask;
1757 raw_write(env, ri, value);
1760 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1763 * scr_write will set the RES1 bits on an AArch64-only CPU.
1764 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1766 scr_write(env, ri, 0);
1769 static CPAccessResult access_aa64_tid2(CPUARMState *env,
1770 const ARMCPRegInfo *ri,
1773 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1774 return CP_ACCESS_TRAP_EL2;
1777 return CP_ACCESS_OK;
1780 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1782 ARMCPU *cpu = env_archcpu(env);
1784 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1787 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1788 ri->secure & ARM_CP_SECSTATE_S);
1790 return cpu->ccsidr[index];
1793 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1796 raw_write(env, ri, value & 0xf);
1799 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1801 CPUState *cs = env_cpu(env);
1802 bool el1 = arm_current_el(env) == 1;
1803 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1806 if (hcr_el2 & HCR_IMO) {
1807 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1811 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1816 if (hcr_el2 & HCR_FMO) {
1817 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1821 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1826 if (hcr_el2 & HCR_AMO) {
1827 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1835 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1838 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1839 return CP_ACCESS_TRAP_EL2;
1842 return CP_ACCESS_OK;
1845 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1848 if (arm_feature(env, ARM_FEATURE_V8)) {
1849 return access_aa64_tid1(env, ri, isread);
1852 return CP_ACCESS_OK;
1855 static const ARMCPRegInfo v7_cp_reginfo[] = {
1856 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1857 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1858 .access = PL1_W, .type = ARM_CP_NOP },
1859 /* Performance monitors are implementation defined in v7,
1860 * but with an ARM recommended set of registers, which we
1863 * Performance registers fall into three categories:
1864 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1865 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1866 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1867 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1868 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1870 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1871 .access = PL0_RW, .type = ARM_CP_ALIAS,
1872 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1873 .writefn = pmcntenset_write,
1874 .accessfn = pmreg_access,
1875 .raw_writefn = raw_write },
1876 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1877 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1878 .access = PL0_RW, .accessfn = pmreg_access,
1879 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1880 .writefn = pmcntenset_write, .raw_writefn = raw_write },
1881 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1883 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1884 .accessfn = pmreg_access,
1885 .writefn = pmcntenclr_write,
1886 .type = ARM_CP_ALIAS },
1887 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1888 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1889 .access = PL0_RW, .accessfn = pmreg_access,
1890 .type = ARM_CP_ALIAS,
1891 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1892 .writefn = pmcntenclr_write },
1893 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1894 .access = PL0_RW, .type = ARM_CP_IO,
1895 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1896 .accessfn = pmreg_access,
1897 .writefn = pmovsr_write,
1898 .raw_writefn = raw_write },
1899 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1900 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1901 .access = PL0_RW, .accessfn = pmreg_access,
1902 .type = ARM_CP_ALIAS | ARM_CP_IO,
1903 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1904 .writefn = pmovsr_write,
1905 .raw_writefn = raw_write },
1906 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1907 .access = PL0_W, .accessfn = pmreg_access_swinc,
1908 .type = ARM_CP_NO_RAW | ARM_CP_IO,
1909 .writefn = pmswinc_write },
1910 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1911 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1912 .access = PL0_W, .accessfn = pmreg_access_swinc,
1913 .type = ARM_CP_NO_RAW | ARM_CP_IO,
1914 .writefn = pmswinc_write },
1915 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1916 .access = PL0_RW, .type = ARM_CP_ALIAS,
1917 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1918 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1919 .raw_writefn = raw_write},
1920 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1921 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1922 .access = PL0_RW, .accessfn = pmreg_access_selr,
1923 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1924 .writefn = pmselr_write, .raw_writefn = raw_write, },
1925 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1926 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1927 .readfn = pmccntr_read, .writefn = pmccntr_write32,
1928 .accessfn = pmreg_access_ccntr },
1929 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1930 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1931 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1933 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1934 .readfn = pmccntr_read, .writefn = pmccntr_write,
1935 .raw_readfn = raw_read, .raw_writefn = raw_write, },
1936 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1937 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1938 .access = PL0_RW, .accessfn = pmreg_access,
1939 .type = ARM_CP_ALIAS | ARM_CP_IO,
1941 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1942 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1943 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1944 .access = PL0_RW, .accessfn = pmreg_access,
1946 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1948 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1949 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1950 .accessfn = pmreg_access,
1951 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1952 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1953 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1954 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1955 .accessfn = pmreg_access,
1956 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1957 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1958 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1959 .accessfn = pmreg_access_xevcntr,
1960 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1961 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
1962 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
1963 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1964 .accessfn = pmreg_access_xevcntr,
1965 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1966 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1967 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
1968 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
1970 .writefn = pmuserenr_write, .raw_writefn = raw_write },
1971 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1972 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1973 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1974 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1976 .writefn = pmuserenr_write, .raw_writefn = raw_write },
1977 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1978 .access = PL1_RW, .accessfn = access_tpm,
1979 .type = ARM_CP_ALIAS | ARM_CP_IO,
1980 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
1982 .writefn = pmintenset_write, .raw_writefn = raw_write },
1983 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
1984 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
1985 .access = PL1_RW, .accessfn = access_tpm,
1987 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1988 .writefn = pmintenset_write, .raw_writefn = raw_write,
1989 .resetvalue = 0x0 },
1990 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1991 .access = PL1_RW, .accessfn = access_tpm,
1992 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
1993 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1994 .writefn = pmintenclr_write, },
1995 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1996 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1997 .access = PL1_RW, .accessfn = access_tpm,
1998 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
1999 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2000 .writefn = pmintenclr_write },
2001 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2002 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2004 .accessfn = access_aa64_tid2,
2005 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2006 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2007 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2009 .accessfn = access_aa64_tid2,
2010 .writefn = csselr_write, .resetvalue = 0,
2011 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2012 offsetof(CPUARMState, cp15.csselr_ns) } },
2013 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2014 * just RAZ for all cores:
2016 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2017 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2018 .access = PL1_R, .type = ARM_CP_CONST,
2019 .accessfn = access_aa64_tid1,
2021 /* Auxiliary fault status registers: these also are IMPDEF, and we
2022 * choose to RAZ/WI for all cores.
2024 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2025 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2026 .access = PL1_RW, .accessfn = access_tvm_trvm,
2027 .type = ARM_CP_CONST, .resetvalue = 0 },
2028 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2029 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2030 .access = PL1_RW, .accessfn = access_tvm_trvm,
2031 .type = ARM_CP_CONST, .resetvalue = 0 },
2032 /* MAIR can just read-as-written because we don't implement caches
2033 * and so don't need to care about memory attributes.
2035 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2036 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2037 .access = PL1_RW, .accessfn = access_tvm_trvm,
2038 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2040 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2041 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2042 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2044 /* For non-long-descriptor page tables these are PRRR and NMRR;
2045 * regardless they still act as reads-as-written for QEMU.
2047 /* MAIR0/1 are defined separately from their 64-bit counterpart which
2048 * allows them to assign the correct fieldoffset based on the endianness
2049 * handled in the field definitions.
2051 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2052 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2053 .access = PL1_RW, .accessfn = access_tvm_trvm,
2054 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2055 offsetof(CPUARMState, cp15.mair0_ns) },
2056 .resetfn = arm_cp_reset_ignore },
2057 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2058 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2059 .access = PL1_RW, .accessfn = access_tvm_trvm,
2060 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2061 offsetof(CPUARMState, cp15.mair1_ns) },
2062 .resetfn = arm_cp_reset_ignore },
2063 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2064 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2065 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2066 /* 32 bit ITLB invalidates */
2067 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2068 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2069 .writefn = tlbiall_write },
2070 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2071 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2072 .writefn = tlbimva_write },
2073 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2074 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2075 .writefn = tlbiasid_write },
2076 /* 32 bit DTLB invalidates */
2077 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2078 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2079 .writefn = tlbiall_write },
2080 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2081 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2082 .writefn = tlbimva_write },
2083 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2084 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2085 .writefn = tlbiasid_write },
2086 /* 32 bit TLB invalidates */
2087 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2088 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2089 .writefn = tlbiall_write },
2090 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2091 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2092 .writefn = tlbimva_write },
2093 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2094 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2095 .writefn = tlbiasid_write },
2096 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2097 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2098 .writefn = tlbimvaa_write },
2101 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2102 /* 32 bit TLB invalidates, Inner Shareable */
2103 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2104 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2105 .writefn = tlbiall_is_write },
2106 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2107 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2108 .writefn = tlbimva_is_write },
2109 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2110 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2111 .writefn = tlbiasid_is_write },
2112 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2113 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2114 .writefn = tlbimvaa_is_write },
2117 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2118 /* PMOVSSET is not implemented in v7 before v7ve */
2119 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2120 .access = PL0_RW, .accessfn = pmreg_access,
2121 .type = ARM_CP_ALIAS | ARM_CP_IO,
2122 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2123 .writefn = pmovsset_write,
2124 .raw_writefn = raw_write },
2125 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2126 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2127 .access = PL0_RW, .accessfn = pmreg_access,
2128 .type = ARM_CP_ALIAS | ARM_CP_IO,
2129 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2130 .writefn = pmovsset_write,
2131 .raw_writefn = raw_write },
2134 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2141 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2145 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2146 * at all, so we don't need to check whether we're v8A.
2148 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2149 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2150 return CP_ACCESS_TRAP_EL2;
2152 return CP_ACCESS_OK;
2155 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2158 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2159 return CP_ACCESS_TRAP;
2161 return teecr_access(env, ri, isread);
2164 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2165 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2166 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2168 .writefn = teecr_write, .accessfn = teecr_access },
2169 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2170 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2171 .accessfn = teehbr_access, .resetvalue = 0 },
2174 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2175 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2176 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2178 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2179 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2181 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2182 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2183 .resetfn = arm_cp_reset_ignore },
2184 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2185 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2186 .access = PL0_R|PL1_W,
2187 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2189 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2190 .access = PL0_R|PL1_W,
2191 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2192 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2193 .resetfn = arm_cp_reset_ignore },
2194 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2195 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2197 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2198 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2200 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2201 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2205 #ifndef CONFIG_USER_ONLY
2207 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2210 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2211 * Writable only at the highest implemented exception level.
2213 int el = arm_current_el(env);
2219 hcr = arm_hcr_el2_eff(env);
2220 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2221 cntkctl = env->cp15.cnthctl_el2;
2223 cntkctl = env->cp15.c14_cntkctl;
2225 if (!extract32(cntkctl, 0, 2)) {
2226 return CP_ACCESS_TRAP;
2230 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2231 arm_is_secure_below_el3(env)) {
2232 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2233 return CP_ACCESS_TRAP_UNCATEGORIZED;
2241 if (!isread && el < arm_highest_el(env)) {
2242 return CP_ACCESS_TRAP_UNCATEGORIZED;
2245 return CP_ACCESS_OK;
2248 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2251 unsigned int cur_el = arm_current_el(env);
2252 bool has_el2 = arm_is_el2_enabled(env);
2253 uint64_t hcr = arm_hcr_el2_eff(env);
2257 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2258 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2259 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2260 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2263 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2264 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2265 return CP_ACCESS_TRAP;
2268 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2269 if (hcr & HCR_E2H) {
2270 if (timeridx == GTIMER_PHYS &&
2271 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2272 return CP_ACCESS_TRAP_EL2;
2275 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2276 if (has_el2 && timeridx == GTIMER_PHYS &&
2277 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2278 return CP_ACCESS_TRAP_EL2;
2284 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2285 if (has_el2 && timeridx == GTIMER_PHYS &&
2287 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2288 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2289 return CP_ACCESS_TRAP_EL2;
2293 return CP_ACCESS_OK;
2296 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2299 unsigned int cur_el = arm_current_el(env);
2300 bool has_el2 = arm_is_el2_enabled(env);
2301 uint64_t hcr = arm_hcr_el2_eff(env);
2305 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2306 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2307 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2308 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2312 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2313 * EL0 if EL0[PV]TEN is zero.
2315 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2316 return CP_ACCESS_TRAP;
2321 if (has_el2 && timeridx == GTIMER_PHYS) {
2322 if (hcr & HCR_E2H) {
2323 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2324 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2325 return CP_ACCESS_TRAP_EL2;
2328 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2329 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2330 return CP_ACCESS_TRAP_EL2;
2336 return CP_ACCESS_OK;
2339 static CPAccessResult gt_pct_access(CPUARMState *env,
2340 const ARMCPRegInfo *ri,
2343 return gt_counter_access(env, GTIMER_PHYS, isread);
2346 static CPAccessResult gt_vct_access(CPUARMState *env,
2347 const ARMCPRegInfo *ri,
2350 return gt_counter_access(env, GTIMER_VIRT, isread);
2353 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2356 return gt_timer_access(env, GTIMER_PHYS, isread);
2359 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2362 return gt_timer_access(env, GTIMER_VIRT, isread);
2365 static CPAccessResult gt_stimer_access(CPUARMState *env,
2366 const ARMCPRegInfo *ri,
2369 /* The AArch64 register view of the secure physical timer is
2370 * always accessible from EL3, and configurably accessible from
2373 switch (arm_current_el(env)) {
2375 if (!arm_is_secure(env)) {
2376 return CP_ACCESS_TRAP;
2378 if (!(env->cp15.scr_el3 & SCR_ST)) {
2379 return CP_ACCESS_TRAP_EL3;
2381 return CP_ACCESS_OK;
2384 return CP_ACCESS_TRAP;
2386 return CP_ACCESS_OK;
2388 g_assert_not_reached();
2392 static uint64_t gt_get_countervalue(CPUARMState *env)
2394 ARMCPU *cpu = env_archcpu(env);
2396 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2399 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2401 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2404 /* Timer enabled: calculate and set current ISTATUS, irq, and
2405 * reset timer to when ISTATUS next has to change
2407 uint64_t offset = timeridx == GTIMER_VIRT ?
2408 cpu->env.cp15.cntvoff_el2 : 0;
2409 uint64_t count = gt_get_countervalue(&cpu->env);
2410 /* Note that this must be unsigned 64 bit arithmetic: */
2411 int istatus = count - offset >= gt->cval;
2415 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2417 irqstate = (istatus && !(gt->ctl & 2));
2418 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2421 /* Next transition is when count rolls back over to zero */
2422 nexttick = UINT64_MAX;
2424 /* Next transition is when we hit cval */
2425 nexttick = gt->cval + offset;
2427 /* Note that the desired next expiry time might be beyond the
2428 * signed-64-bit range of a QEMUTimer -- in this case we just
2429 * set the timer for as far in the future as possible. When the
2430 * timer expires we will reset the timer for any remaining period.
2432 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2433 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2435 timer_mod(cpu->gt_timer[timeridx], nexttick);
2437 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2439 /* Timer disabled: ISTATUS and timer output always clear */
2441 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2442 timer_del(cpu->gt_timer[timeridx]);
2443 trace_arm_gt_recalc_disabled(timeridx);
2447 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2450 ARMCPU *cpu = env_archcpu(env);
2452 timer_del(cpu->gt_timer[timeridx]);
2455 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2457 return gt_get_countervalue(env);
2460 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2464 switch (arm_current_el(env)) {
2466 hcr = arm_hcr_el2_eff(env);
2467 if (hcr & HCR_E2H) {
2472 hcr = arm_hcr_el2_eff(env);
2473 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2479 return env->cp15.cntvoff_el2;
2482 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2484 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2487 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2491 trace_arm_gt_cval_write(timeridx, value);
2492 env->cp15.c14_timer[timeridx].cval = value;
2493 gt_recalc_timer(env_archcpu(env), timeridx);
2496 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2499 uint64_t offset = 0;
2503 case GTIMER_HYPVIRT:
2504 offset = gt_virt_cnt_offset(env);
2508 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2509 (gt_get_countervalue(env) - offset));
2512 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2516 uint64_t offset = 0;
2520 case GTIMER_HYPVIRT:
2521 offset = gt_virt_cnt_offset(env);
2525 trace_arm_gt_tval_write(timeridx, value);
2526 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2527 sextract64(value, 0, 32);
2528 gt_recalc_timer(env_archcpu(env), timeridx);
2531 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2535 ARMCPU *cpu = env_archcpu(env);
2536 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2538 trace_arm_gt_ctl_write(timeridx, value);
2539 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2540 if ((oldval ^ value) & 1) {
2541 /* Enable toggled */
2542 gt_recalc_timer(cpu, timeridx);
2543 } else if ((oldval ^ value) & 2) {
2544 /* IMASK toggled: don't need to recalculate,
2545 * just set the interrupt line based on ISTATUS
2547 int irqstate = (oldval & 4) && !(value & 2);
2549 trace_arm_gt_imask_toggle(timeridx, irqstate);
2550 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2554 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2556 gt_timer_reset(env, ri, GTIMER_PHYS);
2559 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2562 gt_cval_write(env, ri, GTIMER_PHYS, value);
2565 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2567 return gt_tval_read(env, ri, GTIMER_PHYS);
2570 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2573 gt_tval_write(env, ri, GTIMER_PHYS, value);
2576 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2579 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2582 static int gt_phys_redir_timeridx(CPUARMState *env)
2584 switch (arm_mmu_idx(env)) {
2585 case ARMMMUIdx_E20_0:
2586 case ARMMMUIdx_E20_2:
2587 case ARMMMUIdx_E20_2_PAN:
2588 case ARMMMUIdx_SE20_0:
2589 case ARMMMUIdx_SE20_2:
2590 case ARMMMUIdx_SE20_2_PAN:
2597 static int gt_virt_redir_timeridx(CPUARMState *env)
2599 switch (arm_mmu_idx(env)) {
2600 case ARMMMUIdx_E20_0:
2601 case ARMMMUIdx_E20_2:
2602 case ARMMMUIdx_E20_2_PAN:
2603 case ARMMMUIdx_SE20_0:
2604 case ARMMMUIdx_SE20_2:
2605 case ARMMMUIdx_SE20_2_PAN:
2606 return GTIMER_HYPVIRT;
2612 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2613 const ARMCPRegInfo *ri)
2615 int timeridx = gt_phys_redir_timeridx(env);
2616 return env->cp15.c14_timer[timeridx].cval;
2619 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2622 int timeridx = gt_phys_redir_timeridx(env);
2623 gt_cval_write(env, ri, timeridx, value);
2626 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2627 const ARMCPRegInfo *ri)
2629 int timeridx = gt_phys_redir_timeridx(env);
2630 return gt_tval_read(env, ri, timeridx);
2633 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2636 int timeridx = gt_phys_redir_timeridx(env);
2637 gt_tval_write(env, ri, timeridx, value);
2640 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2641 const ARMCPRegInfo *ri)
2643 int timeridx = gt_phys_redir_timeridx(env);
2644 return env->cp15.c14_timer[timeridx].ctl;
2647 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2650 int timeridx = gt_phys_redir_timeridx(env);
2651 gt_ctl_write(env, ri, timeridx, value);
2654 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2656 gt_timer_reset(env, ri, GTIMER_VIRT);
2659 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2662 gt_cval_write(env, ri, GTIMER_VIRT, value);
2665 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2667 return gt_tval_read(env, ri, GTIMER_VIRT);
2670 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2673 gt_tval_write(env, ri, GTIMER_VIRT, value);
2676 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2679 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2682 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2685 ARMCPU *cpu = env_archcpu(env);
2687 trace_arm_gt_cntvoff_write(value);
2688 raw_write(env, ri, value);
2689 gt_recalc_timer(cpu, GTIMER_VIRT);
2692 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2693 const ARMCPRegInfo *ri)
2695 int timeridx = gt_virt_redir_timeridx(env);
2696 return env->cp15.c14_timer[timeridx].cval;
2699 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2702 int timeridx = gt_virt_redir_timeridx(env);
2703 gt_cval_write(env, ri, timeridx, value);
2706 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2707 const ARMCPRegInfo *ri)
2709 int timeridx = gt_virt_redir_timeridx(env);
2710 return gt_tval_read(env, ri, timeridx);
2713 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2716 int timeridx = gt_virt_redir_timeridx(env);
2717 gt_tval_write(env, ri, timeridx, value);
2720 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2721 const ARMCPRegInfo *ri)
2723 int timeridx = gt_virt_redir_timeridx(env);
2724 return env->cp15.c14_timer[timeridx].ctl;
2727 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2730 int timeridx = gt_virt_redir_timeridx(env);
2731 gt_ctl_write(env, ri, timeridx, value);
2734 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2736 gt_timer_reset(env, ri, GTIMER_HYP);
2739 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2742 gt_cval_write(env, ri, GTIMER_HYP, value);
2745 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2747 return gt_tval_read(env, ri, GTIMER_HYP);
2750 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2753 gt_tval_write(env, ri, GTIMER_HYP, value);
2756 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2759 gt_ctl_write(env, ri, GTIMER_HYP, value);
2762 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2764 gt_timer_reset(env, ri, GTIMER_SEC);
2767 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2770 gt_cval_write(env, ri, GTIMER_SEC, value);
2773 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2775 return gt_tval_read(env, ri, GTIMER_SEC);
2778 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2781 gt_tval_write(env, ri, GTIMER_SEC, value);
2784 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2787 gt_ctl_write(env, ri, GTIMER_SEC, value);
2790 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2792 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2795 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2798 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2801 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2803 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2806 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2809 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2812 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2815 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2818 void arm_gt_ptimer_cb(void *opaque)
2820 ARMCPU *cpu = opaque;
2822 gt_recalc_timer(cpu, GTIMER_PHYS);
2825 void arm_gt_vtimer_cb(void *opaque)
2827 ARMCPU *cpu = opaque;
2829 gt_recalc_timer(cpu, GTIMER_VIRT);
2832 void arm_gt_htimer_cb(void *opaque)
2834 ARMCPU *cpu = opaque;
2836 gt_recalc_timer(cpu, GTIMER_HYP);
2839 void arm_gt_stimer_cb(void *opaque)
2841 ARMCPU *cpu = opaque;
2843 gt_recalc_timer(cpu, GTIMER_SEC);
2846 void arm_gt_hvtimer_cb(void *opaque)
2848 ARMCPU *cpu = opaque;
2850 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2853 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2855 ARMCPU *cpu = env_archcpu(env);
2857 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2860 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2861 /* Note that CNTFRQ is purely reads-as-written for the benefit
2862 * of software; writing it doesn't actually change the timer frequency.
2863 * Our reset value matches the fixed frequency we implement the timer at.
2865 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2866 .type = ARM_CP_ALIAS,
2867 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2868 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2870 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2871 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2872 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2873 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2874 .resetfn = arm_gt_cntfrq_reset,
2876 /* overall control: mostly access permissions */
2877 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2878 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2880 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2883 /* per-timer control */
2884 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2885 .secure = ARM_CP_SECSTATE_NS,
2886 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2887 .accessfn = gt_ptimer_access,
2888 .fieldoffset = offsetoflow32(CPUARMState,
2889 cp15.c14_timer[GTIMER_PHYS].ctl),
2890 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2891 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2893 { .name = "CNTP_CTL_S",
2894 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2895 .secure = ARM_CP_SECSTATE_S,
2896 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2897 .accessfn = gt_ptimer_access,
2898 .fieldoffset = offsetoflow32(CPUARMState,
2899 cp15.c14_timer[GTIMER_SEC].ctl),
2900 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2902 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2903 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2904 .type = ARM_CP_IO, .access = PL0_RW,
2905 .accessfn = gt_ptimer_access,
2906 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2908 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2909 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2911 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2912 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2913 .accessfn = gt_vtimer_access,
2914 .fieldoffset = offsetoflow32(CPUARMState,
2915 cp15.c14_timer[GTIMER_VIRT].ctl),
2916 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2917 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2919 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2920 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2921 .type = ARM_CP_IO, .access = PL0_RW,
2922 .accessfn = gt_vtimer_access,
2923 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2925 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2926 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2928 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2929 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2930 .secure = ARM_CP_SECSTATE_NS,
2931 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2932 .accessfn = gt_ptimer_access,
2933 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2935 { .name = "CNTP_TVAL_S",
2936 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2937 .secure = ARM_CP_SECSTATE_S,
2938 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2939 .accessfn = gt_ptimer_access,
2940 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2942 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2943 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2944 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2945 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2946 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2948 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2949 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2950 .accessfn = gt_vtimer_access,
2951 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
2953 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2954 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2955 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2956 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2957 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
2959 /* The counter itself */
2960 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
2961 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2962 .accessfn = gt_pct_access,
2963 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2965 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2966 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
2967 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2968 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
2970 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
2971 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
2972 .accessfn = gt_vct_access,
2973 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
2975 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
2976 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
2977 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2978 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
2980 /* Comparison value, indicating when the timer goes off */
2981 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
2982 .secure = ARM_CP_SECSTATE_NS,
2984 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2985 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
2986 .accessfn = gt_ptimer_access,
2987 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
2988 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
2990 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
2991 .secure = ARM_CP_SECSTATE_S,
2993 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
2994 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
2995 .accessfn = gt_ptimer_access,
2996 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
2998 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
2999 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3002 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3003 .resetvalue = 0, .accessfn = gt_ptimer_access,
3004 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3005 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3007 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3009 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3010 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3011 .accessfn = gt_vtimer_access,
3012 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3013 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3015 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3016 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3019 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3020 .resetvalue = 0, .accessfn = gt_vtimer_access,
3021 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3022 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3024 /* Secure timer -- this is actually restricted to only EL3
3025 * and configurably Secure-EL1 via the accessfn.
3027 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3028 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3029 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3030 .accessfn = gt_stimer_access,
3031 .readfn = gt_sec_tval_read,
3032 .writefn = gt_sec_tval_write,
3033 .resetfn = gt_sec_timer_reset,
3035 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3036 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3037 .type = ARM_CP_IO, .access = PL1_RW,
3038 .accessfn = gt_stimer_access,
3039 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3041 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3043 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3044 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3045 .type = ARM_CP_IO, .access = PL1_RW,
3046 .accessfn = gt_stimer_access,
3047 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3048 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3052 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3055 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3056 return CP_ACCESS_TRAP;
3058 return CP_ACCESS_OK;
3063 /* In user-mode most of the generic timer registers are inaccessible
3064 * however modern kernels (4.12+) allow access to cntvct_el0
3067 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3069 ARMCPU *cpu = env_archcpu(env);
3071 /* Currently we have no support for QEMUTimer in linux-user so we
3072 * can't call gt_get_countervalue(env), instead we directly
3073 * call the lower level functions.
3075 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3078 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3079 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3080 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3081 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3082 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3083 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3085 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3086 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3087 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3088 .readfn = gt_virt_cnt_read,
3094 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3096 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3097 raw_write(env, ri, value);
3098 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3099 raw_write(env, ri, value & 0xfffff6ff);
3101 raw_write(env, ri, value & 0xfffff1ff);
3105 #ifndef CONFIG_USER_ONLY
3106 /* get_phys_addr() isn't present for user-mode-only targets */
3108 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3112 /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3113 * Secure EL1 (which can only happen if EL3 is AArch64).
3114 * They are simply UNDEF if executed from NS EL1.
3115 * They function normally from EL2 or EL3.
3117 if (arm_current_el(env) == 1) {
3118 if (arm_is_secure_below_el3(env)) {
3119 if (env->cp15.scr_el3 & SCR_EEL2) {
3120 return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3122 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3124 return CP_ACCESS_TRAP_UNCATEGORIZED;
3127 return CP_ACCESS_OK;
3131 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3132 MMUAccessType access_type, ARMMMUIdx mmu_idx)
3135 target_ulong page_size;
3139 bool format64 = false;
3140 MemTxAttrs attrs = {};
3141 ARMMMUFaultInfo fi = {};
3142 ARMCacheAttrs cacheattrs = {};
3144 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
3145 &prot, &page_size, &fi, &cacheattrs);
3148 * ATS operations only do S1 or S1+S2 translations, so we never
3149 * have to deal with the ARMCacheAttrs format for S2 only.
3151 assert(!cacheattrs.is_s2_format);
3155 * Some kinds of translation fault must cause exceptions rather
3156 * than being reported in the PAR.
3158 int current_el = arm_current_el(env);
3160 uint32_t syn, fsr, fsc;
3161 bool take_exc = false;
3163 if (fi.s1ptw && current_el == 1
3164 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3166 * Synchronous stage 2 fault on an access made as part of the
3167 * translation table walk for AT S1E0* or AT S1E1* insn
3168 * executed from NS EL1. If this is a synchronous external abort
3169 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3170 * to EL3. Otherwise the fault is taken as an exception to EL2,
3171 * and HPFAR_EL2 holds the faulting IPA.
3173 if (fi.type == ARMFault_SyncExternalOnWalk &&
3174 (env->cp15.scr_el3 & SCR_EA)) {
3177 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3178 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3179 env->cp15.hpfar_el2 |= HPFAR_NS;
3184 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3186 * Synchronous external aborts during a translation table walk
3187 * are taken as Data Abort exceptions.
3190 if (current_el == 3) {
3196 target_el = exception_target_el(env);
3202 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3203 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3204 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3205 fsr = arm_fi_to_lfsc(&fi);
3206 fsc = extract32(fsr, 0, 6);
3208 fsr = arm_fi_to_sfsc(&fi);
3212 * Report exception with ESR indicating a fault due to a
3213 * translation table walk for a cache maintenance instruction.
3215 syn = syn_data_abort_no_iss(current_el == target_el, 0,
3216 fi.ea, 1, fi.s1ptw, 1, fsc);
3217 env->exception.vaddress = value;
3218 env->exception.fsr = fsr;
3219 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3225 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3228 * * TTBCR.EAE determines whether the result is returned using the
3229 * 32-bit or the 64-bit PAR format
3230 * * Instructions executed in Hyp mode always use the 64bit format
3232 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3233 * * The Non-secure TTBCR.EAE bit is set to 1
3234 * * The implementation includes EL2, and the value of HCR.VM is 1
3236 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3238 * ATS1Hx always uses the 64bit format.
3240 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3242 if (arm_feature(env, ARM_FEATURE_EL2)) {
3243 if (mmu_idx == ARMMMUIdx_E10_0 ||
3244 mmu_idx == ARMMMUIdx_E10_1 ||
3245 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3246 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3248 format64 |= arm_current_el(env) == 2;
3254 /* Create a 64-bit PAR */
3255 par64 = (1 << 11); /* LPAE bit always set */
3257 par64 |= phys_addr & ~0xfffULL;
3258 if (!attrs.secure) {
3259 par64 |= (1 << 9); /* NS */
3261 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3262 par64 |= cacheattrs.shareability << 7; /* SH */
3264 uint32_t fsr = arm_fi_to_lfsc(&fi);
3267 par64 |= (fsr & 0x3f) << 1; /* FS */
3269 par64 |= (1 << 9); /* S */
3272 par64 |= (1 << 8); /* PTW */
3276 /* fsr is a DFSR/IFSR value for the short descriptor
3277 * translation table format (with WnR always clear).
3278 * Convert it to a 32-bit PAR.
3281 /* We do not set any attribute bits in the PAR */
3282 if (page_size == (1 << 24)
3283 && arm_feature(env, ARM_FEATURE_V7)) {
3284 par64 = (phys_addr & 0xff000000) | (1 << 1);
3286 par64 = phys_addr & 0xfffff000;
3288 if (!attrs.secure) {
3289 par64 |= (1 << 9); /* NS */
3292 uint32_t fsr = arm_fi_to_sfsc(&fi);
3294 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3295 ((fsr & 0xf) << 1) | 1;
3300 #endif /* CONFIG_TCG */
3302 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3305 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3308 int el = arm_current_el(env);
3309 bool secure = arm_is_secure_below_el3(env);
3311 switch (ri->opc2 & 6) {
3313 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3316 mmu_idx = ARMMMUIdx_SE3;
3319 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
3322 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3323 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3324 : ARMMMUIdx_Stage1_E1_PAN);
3326 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3330 g_assert_not_reached();
3334 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3337 mmu_idx = ARMMMUIdx_SE10_0;
3340 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
3341 mmu_idx = ARMMMUIdx_Stage1_E0;
3344 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3347 g_assert_not_reached();
3351 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3352 mmu_idx = ARMMMUIdx_E10_1;
3355 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3356 mmu_idx = ARMMMUIdx_E10_0;
3359 g_assert_not_reached();
3362 par64 = do_ats_write(env, value, access_type, mmu_idx);
3364 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3366 /* Handled by hardware accelerator. */
3367 g_assert_not_reached();
3368 #endif /* CONFIG_TCG */
3371 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3375 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3378 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
3380 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3382 /* Handled by hardware accelerator. */
3383 g_assert_not_reached();
3384 #endif /* CONFIG_TCG */
3387 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3390 if (arm_current_el(env) == 3 &&
3391 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3392 return CP_ACCESS_TRAP;
3394 return CP_ACCESS_OK;
3397 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3401 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3403 int secure = arm_is_secure_below_el3(env);
3405 switch (ri->opc2 & 6) {
3408 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3409 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3410 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3411 : ARMMMUIdx_Stage1_E1_PAN);
3413 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3416 case 4: /* AT S1E2R, AT S1E2W */
3417 mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
3419 case 6: /* AT S1E3R, AT S1E3W */
3420 mmu_idx = ARMMMUIdx_SE3;
3423 g_assert_not_reached();
3426 case 2: /* AT S1E0R, AT S1E0W */
3427 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3429 case 4: /* AT S12E1R, AT S12E1W */
3430 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
3432 case 6: /* AT S12E0R, AT S12E0W */
3433 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
3436 g_assert_not_reached();
3439 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3441 /* Handled by hardware accelerator. */
3442 g_assert_not_reached();
3443 #endif /* CONFIG_TCG */
3447 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3448 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3449 .access = PL1_RW, .resetvalue = 0,
3450 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3451 offsetoflow32(CPUARMState, cp15.par_ns) },
3452 .writefn = par_write },
3453 #ifndef CONFIG_USER_ONLY
3454 /* This underdecoding is safe because the reginfo is NO_RAW. */
3455 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3456 .access = PL1_W, .accessfn = ats_access,
3457 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3461 /* Return basic MPU access permission bits. */
3462 static uint32_t simple_mpu_ap_bits(uint32_t val)
3469 for (i = 0; i < 16; i += 2) {
3470 ret |= (val >> i) & mask;
3476 /* Pad basic MPU access permission bits to extended format. */
3477 static uint32_t extended_mpu_ap_bits(uint32_t val)
3484 for (i = 0; i < 16; i += 2) {
3485 ret |= (val & mask) << i;
3491 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3494 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3497 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3499 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3502 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3505 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3508 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3510 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3513 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3515 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3521 u32p += env->pmsav7.rnr[M_REG_NS];
3525 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3528 ARMCPU *cpu = env_archcpu(env);
3529 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3535 u32p += env->pmsav7.rnr[M_REG_NS];
3536 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3540 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3543 ARMCPU *cpu = env_archcpu(env);
3544 uint32_t nrgs = cpu->pmsav7_dregion;
3546 if (value >= nrgs) {
3547 qemu_log_mask(LOG_GUEST_ERROR,
3548 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3549 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3553 raw_write(env, ri, value);
3556 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3557 /* Reset for all these registers is handled in arm_cpu_reset(),
3558 * because the PMSAv7 is also used by M-profile CPUs, which do
3559 * not register cpregs but still need the state to be reset.
3561 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3562 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3563 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3564 .readfn = pmsav7_read, .writefn = pmsav7_write,
3565 .resetfn = arm_cp_reset_ignore },
3566 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3567 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3568 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3569 .readfn = pmsav7_read, .writefn = pmsav7_write,
3570 .resetfn = arm_cp_reset_ignore },
3571 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3572 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3573 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3574 .readfn = pmsav7_read, .writefn = pmsav7_write,
3575 .resetfn = arm_cp_reset_ignore },
3576 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3578 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3579 .writefn = pmsav7_rgnr_write,
3580 .resetfn = arm_cp_reset_ignore },
3583 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3584 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3585 .access = PL1_RW, .type = ARM_CP_ALIAS,
3586 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3587 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3588 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3589 .access = PL1_RW, .type = ARM_CP_ALIAS,
3590 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3591 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3592 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3594 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3596 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3598 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3600 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3602 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3603 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3605 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3606 /* Protection region base and size registers */
3607 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3608 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3609 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3610 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3611 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3612 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3613 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3614 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3615 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3616 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3617 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3618 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3619 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3620 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3621 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3622 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3623 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3624 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3625 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3626 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3627 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3628 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3629 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3630 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3633 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3636 ARMCPU *cpu = env_archcpu(env);
3638 if (!arm_feature(env, ARM_FEATURE_V8)) {
3639 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3641 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3642 * using Long-descriptor translation table format
3644 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3645 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3647 * In an implementation that includes the Security Extensions
3648 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3649 * Short-descriptor translation table format.
3651 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3657 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3658 /* With LPAE the TTBCR could result in a change of ASID
3659 * via the TTBCR.A1 bit, so do a TLB flush.
3661 tlb_flush(CPU(cpu));
3663 raw_write(env, ri, value);
3666 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3669 ARMCPU *cpu = env_archcpu(env);
3671 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3672 tlb_flush(CPU(cpu));
3673 raw_write(env, ri, value);
3676 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3679 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3680 if (cpreg_field_is_64bit(ri) &&
3681 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3682 ARMCPU *cpu = env_archcpu(env);
3683 tlb_flush(CPU(cpu));
3685 raw_write(env, ri, value);
3688 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3692 * If we are running with E2&0 regime, then an ASID is active.
3693 * Flush if that might be changing. Note we're not checking
3694 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3695 * holds the active ASID, only checking the field that might.
3697 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3698 (arm_hcr_el2_eff(env) & HCR_E2H)) {
3699 uint16_t mask = ARMMMUIdxBit_E20_2 |
3700 ARMMMUIdxBit_E20_2_PAN |
3703 if (arm_is_secure_below_el3(env)) {
3704 mask >>= ARM_MMU_IDX_A_NS;
3707 tlb_flush_by_mmuidx(env_cpu(env), mask);
3709 raw_write(env, ri, value);
3712 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3715 ARMCPU *cpu = env_archcpu(env);
3716 CPUState *cs = CPU(cpu);
3719 * A change in VMID to the stage2 page table (Stage2) invalidates
3720 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3722 if (raw_read(env, ri) != value) {
3723 uint16_t mask = ARMMMUIdxBit_E10_1 |
3724 ARMMMUIdxBit_E10_1_PAN |
3727 if (arm_is_secure_below_el3(env)) {
3728 mask >>= ARM_MMU_IDX_A_NS;
3731 tlb_flush_by_mmuidx(cs, mask);
3732 raw_write(env, ri, value);
3736 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3737 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3738 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
3739 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3740 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3741 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3742 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3743 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3744 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3745 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3746 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3747 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3748 offsetof(CPUARMState, cp15.dfar_ns) } },
3749 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3750 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3751 .access = PL1_RW, .accessfn = access_tvm_trvm,
3752 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3756 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3757 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3758 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3759 .access = PL1_RW, .accessfn = access_tvm_trvm,
3760 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3761 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3762 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3763 .access = PL1_RW, .accessfn = access_tvm_trvm,
3764 .writefn = vmsa_ttbr_write, .resetvalue = 0,
3765 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3766 offsetof(CPUARMState, cp15.ttbr0_ns) } },
3767 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3768 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3769 .access = PL1_RW, .accessfn = access_tvm_trvm,
3770 .writefn = vmsa_ttbr_write, .resetvalue = 0,
3771 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3772 offsetof(CPUARMState, cp15.ttbr1_ns) } },
3773 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3774 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3775 .access = PL1_RW, .accessfn = access_tvm_trvm,
3776 .writefn = vmsa_tcr_el12_write,
3777 .raw_writefn = raw_write,
3779 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3780 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3781 .access = PL1_RW, .accessfn = access_tvm_trvm,
3782 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3783 .raw_writefn = raw_write,
3784 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3785 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
3788 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3789 * qemu tlbs nor adjusting cached masks.
3791 static const ARMCPRegInfo ttbcr2_reginfo = {
3792 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3793 .access = PL1_RW, .accessfn = access_tvm_trvm,
3794 .type = ARM_CP_ALIAS,
3795 .bank_fieldoffsets = {
3796 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3797 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
3801 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3804 env->cp15.c15_ticonfig = value & 0xe7;
3805 /* The OS_TYPE bit in this register changes the reported CPUID! */
3806 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3807 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3810 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3813 env->cp15.c15_threadid = value & 0xffff;
3816 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3819 /* Wait-for-interrupt (deprecated) */
3820 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3823 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3826 /* On OMAP there are registers indicating the max/min index of dcache lines
3827 * containing a dirty line; cache flush operations have to reset these.
3829 env->cp15.c15_i_max = 0x000;
3830 env->cp15.c15_i_min = 0xff0;
3833 static const ARMCPRegInfo omap_cp_reginfo[] = {
3834 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3835 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3836 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3838 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3839 .access = PL1_RW, .type = ARM_CP_NOP },
3840 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3842 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3843 .writefn = omap_ticonfig_write },
3844 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3846 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3847 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3848 .access = PL1_RW, .resetvalue = 0xff0,
3849 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3850 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3852 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3853 .writefn = omap_threadid_write },
3854 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3855 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3856 .type = ARM_CP_NO_RAW,
3857 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3858 /* TODO: Peripheral port remap register:
3859 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3860 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3863 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3864 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3865 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3866 .writefn = omap_cachemaint_write },
3867 { .name = "C9", .cp = 15, .crn = 9,
3868 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3869 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3872 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3875 env->cp15.c15_cpar = value & 0x3fff;
3878 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3879 { .name = "XSCALE_CPAR",
3880 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3881 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3882 .writefn = xscale_cpar_write, },
3883 { .name = "XSCALE_AUXCR",
3884 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3885 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3887 /* XScale specific cache-lockdown: since we have no cache we NOP these
3888 * and hope the guest does not really rely on cache behaviour.
3890 { .name = "XSCALE_LOCK_ICACHE_LINE",
3891 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3892 .access = PL1_W, .type = ARM_CP_NOP },
3893 { .name = "XSCALE_UNLOCK_ICACHE",
3894 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3895 .access = PL1_W, .type = ARM_CP_NOP },
3896 { .name = "XSCALE_DCACHE_LOCK",
3897 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3898 .access = PL1_RW, .type = ARM_CP_NOP },
3899 { .name = "XSCALE_UNLOCK_DCACHE",
3900 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3901 .access = PL1_W, .type = ARM_CP_NOP },
3904 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3905 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3906 * implementation of this implementation-defined space.
3907 * Ideally this should eventually disappear in favour of actually
3908 * implementing the correct behaviour for all cores.
3910 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3911 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3913 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3917 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3918 /* Cache status: RAZ because we have no cache so it's always clean */
3919 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3920 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3924 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3925 /* We never have a block transfer operation in progress */
3926 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
3927 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3929 /* The cache ops themselves: these all NOP for QEMU */
3930 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3931 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3932 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3933 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3934 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3935 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3936 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
3937 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3938 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
3939 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3940 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
3941 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3944 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
3945 /* The cache test-and-clean instructions always return (1 << 30)
3946 * to indicate that there are no dirty cache lines.
3948 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
3949 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3950 .resetvalue = (1 << 30) },
3951 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
3952 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3953 .resetvalue = (1 << 30) },
3956 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
3957 /* Ignore ReadBuffer accesses */
3958 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
3959 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3960 .access = PL1_RW, .resetvalue = 0,
3961 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
3964 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3966 unsigned int cur_el = arm_current_el(env);
3968 if (arm_is_el2_enabled(env) && cur_el == 1) {
3969 return env->cp15.vpidr_el2;
3971 return raw_read(env, ri);
3974 static uint64_t mpidr_read_val(CPUARMState *env)
3976 ARMCPU *cpu = env_archcpu(env);
3977 uint64_t mpidr = cpu->mp_affinity;
3979 if (arm_feature(env, ARM_FEATURE_V7MP)) {
3980 mpidr |= (1U << 31);
3981 /* Cores which are uniprocessor (non-coherent)
3982 * but still implement the MP extensions set
3983 * bit 30. (For instance, Cortex-R5).
3985 if (cpu->mp_is_up) {
3986 mpidr |= (1u << 30);
3992 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3994 unsigned int cur_el = arm_current_el(env);
3996 if (arm_is_el2_enabled(env) && cur_el == 1) {
3997 return env->cp15.vmpidr_el2;
3999 return mpidr_read_val(env);
4002 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4004 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4005 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4006 .access = PL1_RW, .accessfn = access_tvm_trvm,
4007 .type = ARM_CP_CONST, .resetvalue = 0 },
4008 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4009 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4010 .access = PL1_RW, .accessfn = access_tvm_trvm,
4011 .type = ARM_CP_CONST, .resetvalue = 0 },
4012 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4013 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4014 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4015 offsetof(CPUARMState, cp15.par_ns)} },
4016 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4017 .access = PL1_RW, .accessfn = access_tvm_trvm,
4018 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4019 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4020 offsetof(CPUARMState, cp15.ttbr0_ns) },
4021 .writefn = vmsa_ttbr_write, },
4022 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4023 .access = PL1_RW, .accessfn = access_tvm_trvm,
4024 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4025 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4026 offsetof(CPUARMState, cp15.ttbr1_ns) },
4027 .writefn = vmsa_ttbr_write, },
4030 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4032 return vfp_get_fpcr(env);
4035 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4038 vfp_set_fpcr(env, value);
4041 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4043 return vfp_get_fpsr(env);
4046 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4049 vfp_set_fpsr(env, value);
4052 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4055 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4056 return CP_ACCESS_TRAP;
4058 return CP_ACCESS_OK;
4061 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4064 env->daif = value & PSTATE_DAIF;
4067 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4069 return env->pstate & PSTATE_PAN;
4072 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4075 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4078 static const ARMCPRegInfo pan_reginfo = {
4079 .name = "PAN", .state = ARM_CP_STATE_AA64,
4080 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4081 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4082 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4085 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4087 return env->pstate & PSTATE_UAO;
4090 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4093 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4096 static const ARMCPRegInfo uao_reginfo = {
4097 .name = "UAO", .state = ARM_CP_STATE_AA64,
4098 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4099 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4100 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4103 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4105 return env->pstate & PSTATE_DIT;
4108 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4111 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4114 static const ARMCPRegInfo dit_reginfo = {
4115 .name = "DIT", .state = ARM_CP_STATE_AA64,
4116 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4117 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4118 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4121 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4123 return env->pstate & PSTATE_SSBS;
4126 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4129 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4132 static const ARMCPRegInfo ssbs_reginfo = {
4133 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4134 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4135 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4136 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4139 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4140 const ARMCPRegInfo *ri,
4143 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4144 switch (arm_current_el(env)) {
4146 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4147 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4148 return CP_ACCESS_TRAP;
4152 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4153 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4154 return CP_ACCESS_TRAP_EL2;
4158 return CP_ACCESS_OK;
4161 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4162 const ARMCPRegInfo *ri,
4165 /* Cache invalidate/clean to Point of Unification... */
4166 switch (arm_current_el(env)) {
4168 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4169 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4170 return CP_ACCESS_TRAP;
4174 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4175 if (arm_hcr_el2_eff(env) & HCR_TPU) {
4176 return CP_ACCESS_TRAP_EL2;
4180 return CP_ACCESS_OK;
4183 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4184 * Page D4-1736 (DDI0487A.b)
4187 static int vae1_tlbmask(CPUARMState *env)
4189 uint64_t hcr = arm_hcr_el2_eff(env);
4192 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4193 mask = ARMMMUIdxBit_E20_2 |
4194 ARMMMUIdxBit_E20_2_PAN |
4197 mask = ARMMMUIdxBit_E10_1 |
4198 ARMMMUIdxBit_E10_1_PAN |
4202 if (arm_is_secure_below_el3(env)) {
4203 mask >>= ARM_MMU_IDX_A_NS;
4209 /* Return 56 if TBI is enabled, 64 otherwise. */
4210 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4213 uint64_t tcr = regime_tcr(env, mmu_idx);
4214 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4215 int select = extract64(addr, 55, 1);
4217 return (tbi >> select) & 1 ? 56 : 64;
4220 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4222 uint64_t hcr = arm_hcr_el2_eff(env);
4225 /* Only the regime of the mmu_idx below is significant. */
4226 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4227 mmu_idx = ARMMMUIdx_E20_0;
4229 mmu_idx = ARMMMUIdx_E10_0;
4232 if (arm_is_secure_below_el3(env)) {
4233 mmu_idx &= ~ARM_MMU_IDX_A_NS;
4236 return tlbbits_for_regime(env, mmu_idx, addr);
4239 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4242 CPUState *cs = env_cpu(env);
4243 int mask = vae1_tlbmask(env);
4245 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4248 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4251 CPUState *cs = env_cpu(env);
4252 int mask = vae1_tlbmask(env);
4254 if (tlb_force_broadcast(env)) {
4255 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4257 tlb_flush_by_mmuidx(cs, mask);
4261 static int alle1_tlbmask(CPUARMState *env)
4264 * Note that the 'ALL' scope must invalidate both stage 1 and
4265 * stage 2 translations, whereas most other scopes only invalidate
4266 * stage 1 translations.
4268 if (arm_is_secure_below_el3(env)) {
4269 return ARMMMUIdxBit_SE10_1 |
4270 ARMMMUIdxBit_SE10_1_PAN |
4271 ARMMMUIdxBit_SE10_0;
4273 return ARMMMUIdxBit_E10_1 |
4274 ARMMMUIdxBit_E10_1_PAN |
4279 static int e2_tlbmask(CPUARMState *env)
4281 if (arm_is_secure_below_el3(env)) {
4282 return ARMMMUIdxBit_SE20_0 |
4283 ARMMMUIdxBit_SE20_2 |
4284 ARMMMUIdxBit_SE20_2_PAN |
4287 return ARMMMUIdxBit_E20_0 |
4288 ARMMMUIdxBit_E20_2 |
4289 ARMMMUIdxBit_E20_2_PAN |
4294 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4297 CPUState *cs = env_cpu(env);
4298 int mask = alle1_tlbmask(env);
4300 tlb_flush_by_mmuidx(cs, mask);
4303 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4306 CPUState *cs = env_cpu(env);
4307 int mask = e2_tlbmask(env);
4309 tlb_flush_by_mmuidx(cs, mask);
4312 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4315 ARMCPU *cpu = env_archcpu(env);
4316 CPUState *cs = CPU(cpu);
4318 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
4321 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4324 CPUState *cs = env_cpu(env);
4325 int mask = alle1_tlbmask(env);
4327 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4330 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4333 CPUState *cs = env_cpu(env);
4334 int mask = e2_tlbmask(env);
4336 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4339 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4342 CPUState *cs = env_cpu(env);
4344 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
4347 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4350 /* Invalidate by VA, EL2
4351 * Currently handles both VAE2 and VALE2, since we don't support
4352 * flush-last-level-only.
4354 CPUState *cs = env_cpu(env);
4355 int mask = e2_tlbmask(env);
4356 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4358 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4361 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4364 /* Invalidate by VA, EL3
4365 * Currently handles both VAE3 and VALE3, since we don't support
4366 * flush-last-level-only.
4368 ARMCPU *cpu = env_archcpu(env);
4369 CPUState *cs = CPU(cpu);
4370 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4372 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
4375 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4378 CPUState *cs = env_cpu(env);
4379 int mask = vae1_tlbmask(env);
4380 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4381 int bits = vae1_tlbbits(env, pageaddr);
4383 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4386 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4389 /* Invalidate by VA, EL1&0 (AArch64 version).
4390 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4391 * since we don't support flush-for-specific-ASID-only or
4392 * flush-last-level-only.
4394 CPUState *cs = env_cpu(env);
4395 int mask = vae1_tlbmask(env);
4396 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4397 int bits = vae1_tlbbits(env, pageaddr);
4399 if (tlb_force_broadcast(env)) {
4400 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4402 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4406 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4409 CPUState *cs = env_cpu(env);
4410 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4411 bool secure = arm_is_secure_below_el3(env);
4412 int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
4413 int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
4416 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4419 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4422 CPUState *cs = env_cpu(env);
4423 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4424 int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
4426 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4427 ARMMMUIdxBit_SE3, bits);
4430 #ifdef TARGET_AARCH64
4436 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4439 unsigned int page_size_granule, page_shift, num, scale, exponent;
4440 /* Extract one bit to represent the va selector in use. */
4441 uint64_t select = sextract64(value, 36, 1);
4442 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4443 TLBIRange ret = { };
4445 page_size_granule = extract64(value, 46, 2);
4447 /* The granule encoded in value must match the granule in use. */
4448 if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4449 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4454 page_shift = (page_size_granule - 1) * 2 + 12;
4455 num = extract64(value, 39, 5);
4456 scale = extract64(value, 44, 2);
4457 exponent = (5 * scale) + 1;
4459 ret.length = (num + 1) << (exponent + page_shift);
4462 ret.base = sextract64(value, 0, 37);
4464 ret.base = extract64(value, 0, 37);
4468 * With DS=1, BaseADDR is always shifted 16 so that it is able
4469 * to address all 52 va bits. The input address is perforce
4470 * aligned on a 64k boundary regardless of translation granule.
4474 ret.base <<= page_shift;
4479 static void do_rvae_write(CPUARMState *env, uint64_t value,
4480 int idxmap, bool synced)
4482 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4486 range = tlbi_aa64_get_range(env, one_idx, value);
4487 bits = tlbbits_for_regime(env, one_idx, range.base);
4490 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4496 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4497 range.length, idxmap, bits);
4501 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4502 const ARMCPRegInfo *ri,
4506 * Invalidate by VA range, EL1&0.
4507 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4508 * since we don't support flush-for-specific-ASID-only or
4509 * flush-last-level-only.
4512 do_rvae_write(env, value, vae1_tlbmask(env),
4513 tlb_force_broadcast(env));
4516 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4517 const ARMCPRegInfo *ri,
4521 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4522 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4523 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4524 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4525 * shareable specific flushes.
4528 do_rvae_write(env, value, vae1_tlbmask(env), true);
4531 static int vae2_tlbmask(CPUARMState *env)
4533 return (arm_is_secure_below_el3(env)
4534 ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4537 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4538 const ARMCPRegInfo *ri,
4542 * Invalidate by VA range, EL2.
4543 * Currently handles all of RVAE2 and RVALE2,
4544 * since we don't support flush-for-specific-ASID-only or
4545 * flush-last-level-only.
4548 do_rvae_write(env, value, vae2_tlbmask(env),
4549 tlb_force_broadcast(env));
4554 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4555 const ARMCPRegInfo *ri,
4559 * Invalidate by VA range, Inner/Outer Shareable, EL2.
4560 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4561 * since we don't support flush-for-specific-ASID-only,
4562 * flush-last-level-only or inner/outer shareable specific flushes.
4565 do_rvae_write(env, value, vae2_tlbmask(env), true);
4569 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4570 const ARMCPRegInfo *ri,
4574 * Invalidate by VA range, EL3.
4575 * Currently handles all of RVAE3 and RVALE3,
4576 * since we don't support flush-for-specific-ASID-only or
4577 * flush-last-level-only.
4580 do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4581 tlb_force_broadcast(env));
4584 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4585 const ARMCPRegInfo *ri,
4589 * Invalidate by VA range, EL3, Inner/Outer Shareable.
4590 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4591 * since we don't support flush-for-specific-ASID-only,
4592 * flush-last-level-only or inner/outer specific flushes.
4595 do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4599 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4602 int cur_el = arm_current_el(env);
4605 uint64_t hcr = arm_hcr_el2_eff(env);
4608 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4609 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4610 return CP_ACCESS_TRAP_EL2;
4613 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4614 return CP_ACCESS_TRAP;
4616 if (hcr & HCR_TDZ) {
4617 return CP_ACCESS_TRAP_EL2;
4620 } else if (hcr & HCR_TDZ) {
4621 return CP_ACCESS_TRAP_EL2;
4624 return CP_ACCESS_OK;
4627 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4629 ARMCPU *cpu = env_archcpu(env);
4630 int dzp_bit = 1 << 4;
4632 /* DZP indicates whether DC ZVA access is allowed */
4633 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4636 return cpu->dcz_blocksize | dzp_bit;
4639 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4642 if (!(env->pstate & PSTATE_SP)) {
4643 /* Access to SP_EL0 is undefined if it's being used as
4644 * the stack pointer.
4646 return CP_ACCESS_TRAP_UNCATEGORIZED;
4648 return CP_ACCESS_OK;
4651 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4653 return env->pstate & PSTATE_SP;
4656 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4658 update_spsel(env, val);
4661 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4664 ARMCPU *cpu = env_archcpu(env);
4666 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4667 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4671 /* ??? Lots of these bits are not implemented. */
4673 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4674 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4675 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4677 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4678 SCTLR_ATA0 | SCTLR_ATA);
4682 if (raw_read(env, ri) == value) {
4683 /* Skip the TLB flush if nothing actually changed; Linux likes
4684 * to do a lot of pointless SCTLR writes.
4689 raw_write(env, ri, value);
4691 /* This may enable/disable the MMU, so do a TLB flush. */
4692 tlb_flush(CPU(cpu));
4694 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4696 * Normally we would always end the TB on an SCTLR write; see the
4697 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4698 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4699 * of hflags from the translator, so do it here.
4701 arm_rebuild_hflags(env);
4705 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4709 * Some MDCR_EL3 bits affect whether PMU counters are running:
4710 * if we are trying to change any of those then we must
4711 * bracket this update with PMU start/finish calls.
4713 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4718 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4724 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4728 * Some MDCR_EL2 bits affect whether PMU counters are running:
4729 * if we are trying to change any of those then we must
4730 * bracket this update with PMU start/finish calls.
4732 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4737 env->cp15.mdcr_el2 = value;
4743 static const ARMCPRegInfo v8_cp_reginfo[] = {
4744 /* Minimal set of EL0-visible registers. This will need to be expanded
4745 * significantly for system emulation of AArch64 CPUs.
4747 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4748 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4749 .access = PL0_RW, .type = ARM_CP_NZCV },
4750 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4751 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4752 .type = ARM_CP_NO_RAW,
4753 .access = PL0_RW, .accessfn = aa64_daif_access,
4754 .fieldoffset = offsetof(CPUARMState, daif),
4755 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4756 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4757 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4758 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4759 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4760 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4761 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4762 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4763 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4764 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4765 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4766 .access = PL0_R, .type = ARM_CP_NO_RAW,
4767 .readfn = aa64_dczid_read },
4768 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4769 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4770 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4771 #ifndef CONFIG_USER_ONLY
4772 /* Avoid overhead of an access check that always passes in user-mode */
4773 .accessfn = aa64_zva_access,
4776 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4777 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4778 .access = PL1_R, .type = ARM_CP_CURRENTEL },
4779 /* Cache ops: all NOPs since we don't emulate caches */
4780 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4781 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4782 .access = PL1_W, .type = ARM_CP_NOP,
4783 .accessfn = aa64_cacheop_pou_access },
4784 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4785 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4786 .access = PL1_W, .type = ARM_CP_NOP,
4787 .accessfn = aa64_cacheop_pou_access },
4788 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4789 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4790 .access = PL0_W, .type = ARM_CP_NOP,
4791 .accessfn = aa64_cacheop_pou_access },
4792 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4793 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4794 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4795 .type = ARM_CP_NOP },
4796 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4797 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4798 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4799 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4800 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4801 .access = PL0_W, .type = ARM_CP_NOP,
4802 .accessfn = aa64_cacheop_poc_access },
4803 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4804 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4805 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4806 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4807 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4808 .access = PL0_W, .type = ARM_CP_NOP,
4809 .accessfn = aa64_cacheop_pou_access },
4810 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4811 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4812 .access = PL0_W, .type = ARM_CP_NOP,
4813 .accessfn = aa64_cacheop_poc_access },
4814 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4815 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4816 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4817 /* TLBI operations */
4818 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4819 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4820 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4821 .writefn = tlbi_aa64_vmalle1is_write },
4822 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4823 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4824 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4825 .writefn = tlbi_aa64_vae1is_write },
4826 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4827 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4828 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4829 .writefn = tlbi_aa64_vmalle1is_write },
4830 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4831 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4832 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4833 .writefn = tlbi_aa64_vae1is_write },
4834 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4835 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4836 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4837 .writefn = tlbi_aa64_vae1is_write },
4838 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4839 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4840 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4841 .writefn = tlbi_aa64_vae1is_write },
4842 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4843 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4844 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4845 .writefn = tlbi_aa64_vmalle1_write },
4846 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4847 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4848 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4849 .writefn = tlbi_aa64_vae1_write },
4850 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4851 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4852 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4853 .writefn = tlbi_aa64_vmalle1_write },
4854 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4855 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4856 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4857 .writefn = tlbi_aa64_vae1_write },
4858 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4859 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4860 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4861 .writefn = tlbi_aa64_vae1_write },
4862 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4863 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4864 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4865 .writefn = tlbi_aa64_vae1_write },
4866 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4867 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4868 .access = PL2_W, .type = ARM_CP_NOP },
4869 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4870 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4871 .access = PL2_W, .type = ARM_CP_NOP },
4872 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4873 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4874 .access = PL2_W, .type = ARM_CP_NO_RAW,
4875 .writefn = tlbi_aa64_alle1is_write },
4876 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4877 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4878 .access = PL2_W, .type = ARM_CP_NO_RAW,
4879 .writefn = tlbi_aa64_alle1is_write },
4880 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4881 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4882 .access = PL2_W, .type = ARM_CP_NOP },
4883 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4884 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4885 .access = PL2_W, .type = ARM_CP_NOP },
4886 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4887 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4888 .access = PL2_W, .type = ARM_CP_NO_RAW,
4889 .writefn = tlbi_aa64_alle1_write },
4890 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4891 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4892 .access = PL2_W, .type = ARM_CP_NO_RAW,
4893 .writefn = tlbi_aa64_alle1is_write },
4894 #ifndef CONFIG_USER_ONLY
4895 /* 64 bit address translation operations */
4896 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4897 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4898 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4899 .writefn = ats_write64 },
4900 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4901 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4902 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4903 .writefn = ats_write64 },
4904 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4905 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4906 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4907 .writefn = ats_write64 },
4908 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4909 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4910 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4911 .writefn = ats_write64 },
4912 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4913 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4914 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4915 .writefn = ats_write64 },
4916 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4917 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4918 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4919 .writefn = ats_write64 },
4920 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4921 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4922 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4923 .writefn = ats_write64 },
4924 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4925 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4926 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4927 .writefn = ats_write64 },
4928 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4929 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4930 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4931 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4932 .writefn = ats_write64 },
4933 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4934 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4935 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4936 .writefn = ats_write64 },
4937 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4938 .type = ARM_CP_ALIAS,
4939 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4940 .access = PL1_RW, .resetvalue = 0,
4941 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4942 .writefn = par_write },
4944 /* TLB invalidate last level of translation table walk */
4945 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4946 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4947 .writefn = tlbimva_is_write },
4948 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4949 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4950 .writefn = tlbimvaa_is_write },
4951 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4952 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4953 .writefn = tlbimva_write },
4954 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4955 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4956 .writefn = tlbimvaa_write },
4957 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4958 .type = ARM_CP_NO_RAW, .access = PL2_W,
4959 .writefn = tlbimva_hyp_write },
4960 { .name = "TLBIMVALHIS",
4961 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4962 .type = ARM_CP_NO_RAW, .access = PL2_W,
4963 .writefn = tlbimva_hyp_is_write },
4964 { .name = "TLBIIPAS2",
4965 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4966 .type = ARM_CP_NOP, .access = PL2_W },
4967 { .name = "TLBIIPAS2IS",
4968 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4969 .type = ARM_CP_NOP, .access = PL2_W },
4970 { .name = "TLBIIPAS2L",
4971 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4972 .type = ARM_CP_NOP, .access = PL2_W },
4973 { .name = "TLBIIPAS2LIS",
4974 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4975 .type = ARM_CP_NOP, .access = PL2_W },
4976 /* 32 bit cache operations */
4977 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4978 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
4979 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4980 .type = ARM_CP_NOP, .access = PL1_W },
4981 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4982 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
4983 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
4984 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
4985 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
4986 .type = ARM_CP_NOP, .access = PL1_W },
4987 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
4988 .type = ARM_CP_NOP, .access = PL1_W },
4989 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4990 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
4991 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4992 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4993 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
4994 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
4995 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4996 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4997 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
4998 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
4999 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5000 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5001 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5002 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5003 /* MMU Domain access control / MPU write buffer control */
5004 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5005 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5006 .writefn = dacr_write, .raw_writefn = raw_write,
5007 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5008 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5009 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5010 .type = ARM_CP_ALIAS,
5011 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5013 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5014 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5015 .type = ARM_CP_ALIAS,
5016 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5018 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5019 /* We rely on the access checks not allowing the guest to write to the
5020 * state field when SPSel indicates that it's being used as the stack
5023 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5024 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5025 .access = PL1_RW, .accessfn = sp_el0_access,
5026 .type = ARM_CP_ALIAS,
5027 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5028 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5029 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5030 .access = PL2_RW, .type = ARM_CP_ALIAS,
5031 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5032 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5033 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5034 .type = ARM_CP_NO_RAW,
5035 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5036 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5037 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5039 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5040 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5041 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5042 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5043 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5044 .writefn = dacr_write, .raw_writefn = raw_write,
5045 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5046 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5047 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5048 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5049 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5050 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5051 .type = ARM_CP_ALIAS,
5052 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5054 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5055 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5056 .type = ARM_CP_ALIAS,
5057 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5059 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5060 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5061 .type = ARM_CP_ALIAS,
5062 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5064 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5065 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5066 .type = ARM_CP_ALIAS,
5067 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5069 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5070 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5071 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5073 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5074 { .name = "SDCR", .type = ARM_CP_ALIAS,
5075 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5076 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5077 .writefn = sdcr_write,
5078 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5081 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5083 ARMCPU *cpu = env_archcpu(env);
5085 if (arm_feature(env, ARM_FEATURE_V8)) {
5086 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5088 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5091 if (arm_feature(env, ARM_FEATURE_EL3)) {
5092 valid_mask &= ~HCR_HCD;
5093 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5094 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5095 * However, if we're using the SMC PSCI conduit then QEMU is
5096 * effectively acting like EL3 firmware and so the guest at
5097 * EL2 should retain the ability to prevent EL1 from being
5098 * able to make SMC calls into the ersatz firmware, so in
5099 * that case HCR.TSC should be read/write.
5101 valid_mask &= ~HCR_TSC;
5104 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5105 if (cpu_isar_feature(aa64_vh, cpu)) {
5106 valid_mask |= HCR_E2H;
5108 if (cpu_isar_feature(aa64_ras, cpu)) {
5109 valid_mask |= HCR_TERR | HCR_TEA;
5111 if (cpu_isar_feature(aa64_lor, cpu)) {
5112 valid_mask |= HCR_TLOR;
5114 if (cpu_isar_feature(aa64_pauth, cpu)) {
5115 valid_mask |= HCR_API | HCR_APK;
5117 if (cpu_isar_feature(aa64_mte, cpu)) {
5118 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5120 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5121 valid_mask |= HCR_ENSCXT;
5123 if (cpu_isar_feature(aa64_fwb, cpu)) {
5124 valid_mask |= HCR_FWB;
5128 /* Clear RES0 bits. */
5129 value &= valid_mask;
5132 * These bits change the MMU setup:
5133 * HCR_VM enables stage 2 translation
5134 * HCR_PTW forbids certain page-table setups
5135 * HCR_DC disables stage1 and enables stage2 translation
5136 * HCR_DCT enables tagging on (disabled) stage1 translation
5137 * HCR_FWB changes the interpretation of stage2 descriptor bits
5139 if ((env->cp15.hcr_el2 ^ value) &
5140 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5141 tlb_flush(CPU(cpu));
5143 env->cp15.hcr_el2 = value;
5146 * Updates to VI and VF require us to update the status of
5147 * virtual interrupts, which are the logical OR of these bits
5148 * and the state of the input lines from the GIC. (This requires
5149 * that we have the iothread lock, which is done by marking the
5150 * reginfo structs as ARM_CP_IO.)
5151 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5152 * possible for it to be taken immediately, because VIRQ and
5153 * VFIQ are masked unless running at EL0 or EL1, and HCR
5154 * can only be written at EL2.
5156 g_assert(qemu_mutex_iothread_locked());
5157 arm_cpu_update_virq(cpu);
5158 arm_cpu_update_vfiq(cpu);
5159 arm_cpu_update_vserr(cpu);
5162 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5164 do_hcr_write(env, value, 0);
5167 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5170 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5171 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5172 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5175 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5178 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5179 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5180 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5184 * Return the effective value of HCR_EL2.
5185 * Bits that are not included here:
5186 * RW (read from SCR_EL3.RW as needed)
5188 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5190 uint64_t ret = env->cp15.hcr_el2;
5192 if (!arm_is_el2_enabled(env)) {
5194 * "This register has no effect if EL2 is not enabled in the
5195 * current Security state". This is ARMv8.4-SecEL2 speak for
5196 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5198 * Prior to that, the language was "In an implementation that
5199 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5200 * as if this field is 0 for all purposes other than a direct
5201 * read or write access of HCR_EL2". With lots of enumeration
5202 * on a per-field basis. In current QEMU, this is condition
5203 * is arm_is_secure_below_el3.
5205 * Since the v8.4 language applies to the entire register, and
5206 * appears to be backward compatible, use that.
5212 * For a cpu that supports both aarch64 and aarch32, we can set bits
5213 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5214 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5216 if (!arm_el_is_aa64(env, 2)) {
5217 uint64_t aa32_valid;
5220 * These bits are up-to-date as of ARMv8.6.
5221 * For HCR, it's easiest to list just the 2 bits that are invalid.
5222 * For HCR2, list those that are valid.
5224 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5225 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5226 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5230 if (ret & HCR_TGE) {
5231 /* These bits are up-to-date as of ARMv8.6. */
5232 if (ret & HCR_E2H) {
5233 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5234 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5235 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5236 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5237 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5238 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5240 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5242 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5243 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5244 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5252 * Corresponds to ARM pseudocode function ELIsInHost().
5254 bool el_is_in_host(CPUARMState *env, int el)
5259 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5260 * Perform the simplest bit tests first, and validate EL2 afterward.
5263 return false; /* EL1 or EL3 */
5267 * Note that hcr_write() checks isar_feature_aa64_vh(),
5268 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5270 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5271 if ((env->cp15.hcr_el2 & mask) != mask) {
5275 /* TGE and/or E2H set: double check those bits are currently legal. */
5276 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5279 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5282 uint64_t valid_mask = 0;
5284 /* No features adding bits to HCRX are implemented. */
5286 /* Clear RES0 bits. */
5287 env->cp15.hcrx_el2 = value & valid_mask;
5290 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5293 if (arm_current_el(env) < 3
5294 && arm_feature(env, ARM_FEATURE_EL3)
5295 && !(env->cp15.scr_el3 & SCR_HXEN)) {
5296 return CP_ACCESS_TRAP_EL3;
5298 return CP_ACCESS_OK;
5301 static const ARMCPRegInfo hcrx_el2_reginfo = {
5302 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5303 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5304 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5305 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5308 /* Return the effective value of HCRX_EL2. */
5309 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5312 * The bits in this register behave as 0 for all purposes other than
5313 * direct reads of the register if:
5314 * - EL2 is not enabled in the current security state,
5315 * - SCR_EL3.HXEn is 0.
5317 if (!arm_is_el2_enabled(env)
5318 || (arm_feature(env, ARM_FEATURE_EL3)
5319 && !(env->cp15.scr_el3 & SCR_HXEN))) {
5322 return env->cp15.hcrx_el2;
5325 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5329 * For A-profile AArch32 EL3, if NSACR.CP10
5330 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5332 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5333 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5334 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5335 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5337 env->cp15.cptr_el[2] = value;
5340 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5343 * For A-profile AArch32 EL3, if NSACR.CP10
5344 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5346 uint64_t value = env->cp15.cptr_el[2];
5348 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5349 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5350 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5355 static const ARMCPRegInfo el2_cp_reginfo[] = {
5356 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5358 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5359 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5360 .writefn = hcr_write },
5361 { .name = "HCR", .state = ARM_CP_STATE_AA32,
5362 .type = ARM_CP_ALIAS | ARM_CP_IO,
5363 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5364 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5365 .writefn = hcr_writelow },
5366 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5367 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5368 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5369 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5370 .type = ARM_CP_ALIAS,
5371 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5373 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5374 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5375 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5376 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5377 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5378 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5379 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5380 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5381 .type = ARM_CP_ALIAS,
5382 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5384 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5385 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5386 .type = ARM_CP_ALIAS,
5387 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5389 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5390 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5391 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5392 .access = PL2_RW, .writefn = vbar_write,
5393 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5395 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5396 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5397 .access = PL3_RW, .type = ARM_CP_ALIAS,
5398 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5399 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5400 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5401 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5402 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5403 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5404 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5405 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5406 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5408 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5409 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5410 .access = PL2_RW, .type = ARM_CP_ALIAS,
5411 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5412 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5413 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5414 .access = PL2_RW, .type = ARM_CP_CONST,
5416 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5417 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5418 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5419 .access = PL2_RW, .type = ARM_CP_CONST,
5421 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5422 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5423 .access = PL2_RW, .type = ARM_CP_CONST,
5425 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5426 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5427 .access = PL2_RW, .type = ARM_CP_CONST,
5429 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5430 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5431 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5432 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5433 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5434 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5435 .type = ARM_CP_ALIAS,
5436 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5437 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5438 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5439 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5441 /* no .writefn needed as this can't cause an ASID change */
5442 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5443 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5444 .cp = 15, .opc1 = 6, .crm = 2,
5445 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5446 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5447 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5448 .writefn = vttbr_write },
5449 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5450 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5451 .access = PL2_RW, .writefn = vttbr_write,
5452 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5453 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5454 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5455 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5456 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5457 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5458 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5459 .access = PL2_RW, .resetvalue = 0,
5460 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5461 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5462 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5463 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5464 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5465 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5466 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5467 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5468 { .name = "TLBIALLNSNH",
5469 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5470 .type = ARM_CP_NO_RAW, .access = PL2_W,
5471 .writefn = tlbiall_nsnh_write },
5472 { .name = "TLBIALLNSNHIS",
5473 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5474 .type = ARM_CP_NO_RAW, .access = PL2_W,
5475 .writefn = tlbiall_nsnh_is_write },
5476 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5477 .type = ARM_CP_NO_RAW, .access = PL2_W,
5478 .writefn = tlbiall_hyp_write },
5479 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5480 .type = ARM_CP_NO_RAW, .access = PL2_W,
5481 .writefn = tlbiall_hyp_is_write },
5482 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5483 .type = ARM_CP_NO_RAW, .access = PL2_W,
5484 .writefn = tlbimva_hyp_write },
5485 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5486 .type = ARM_CP_NO_RAW, .access = PL2_W,
5487 .writefn = tlbimva_hyp_is_write },
5488 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5489 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5490 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5491 .writefn = tlbi_aa64_alle2_write },
5492 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5493 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5494 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5495 .writefn = tlbi_aa64_vae2_write },
5496 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5497 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5498 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5499 .writefn = tlbi_aa64_vae2_write },
5500 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5501 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5502 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5503 .writefn = tlbi_aa64_alle2is_write },
5504 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5505 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5506 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5507 .writefn = tlbi_aa64_vae2is_write },
5508 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5509 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5510 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5511 .writefn = tlbi_aa64_vae2is_write },
5512 #ifndef CONFIG_USER_ONLY
5513 /* Unlike the other EL2-related AT operations, these must
5514 * UNDEF from EL3 if EL2 is not implemented, which is why we
5515 * define them here rather than with the rest of the AT ops.
5517 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5518 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5519 .access = PL2_W, .accessfn = at_s1e2_access,
5520 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5521 .writefn = ats_write64 },
5522 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5523 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5524 .access = PL2_W, .accessfn = at_s1e2_access,
5525 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5526 .writefn = ats_write64 },
5527 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5528 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5529 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5530 * to behave as if SCR.NS was 1.
5532 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5534 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5535 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5537 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5538 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5539 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5540 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5541 * reset values as IMPDEF. We choose to reset to 3 to comply with
5542 * both ARMv7 and ARMv8.
5544 .access = PL2_RW, .resetvalue = 3,
5545 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5546 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5547 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5548 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5549 .writefn = gt_cntvoff_write,
5550 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5551 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5552 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5553 .writefn = gt_cntvoff_write,
5554 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5555 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5556 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5557 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5558 .type = ARM_CP_IO, .access = PL2_RW,
5559 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5560 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5561 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5562 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5563 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5564 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5565 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5566 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5567 .resetfn = gt_hyp_timer_reset,
5568 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5569 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5571 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5573 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5575 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5577 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5578 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5579 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5580 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5581 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5582 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5584 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5585 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5586 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5588 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5591 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5592 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5593 .type = ARM_CP_ALIAS | ARM_CP_IO,
5594 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5596 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5597 .writefn = hcr_writehigh },
5600 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5603 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5604 return CP_ACCESS_OK;
5606 return CP_ACCESS_TRAP_UNCATEGORIZED;
5609 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5610 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5611 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5612 .access = PL2_RW, .accessfn = sel2_access,
5613 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5614 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5615 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5616 .access = PL2_RW, .accessfn = sel2_access,
5617 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5620 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5623 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5624 * At Secure EL1 it traps to EL3 or EL2.
5626 if (arm_current_el(env) == 3) {
5627 return CP_ACCESS_OK;
5629 if (arm_is_secure_below_el3(env)) {
5630 if (env->cp15.scr_el3 & SCR_EEL2) {
5631 return CP_ACCESS_TRAP_EL2;
5633 return CP_ACCESS_TRAP_EL3;
5635 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5637 return CP_ACCESS_OK;
5639 return CP_ACCESS_TRAP_UNCATEGORIZED;
5642 static const ARMCPRegInfo el3_cp_reginfo[] = {
5643 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5644 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5645 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5646 .resetfn = scr_reset, .writefn = scr_write },
5647 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5648 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5649 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5650 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5651 .writefn = scr_write },
5652 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5653 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5654 .access = PL3_RW, .resetvalue = 0,
5655 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5657 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5658 .access = PL3_RW, .resetvalue = 0,
5659 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5660 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5661 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5662 .writefn = vbar_write, .resetvalue = 0,
5663 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5664 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5665 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5666 .access = PL3_RW, .resetvalue = 0,
5667 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5668 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5669 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5671 /* no .writefn needed as this can't cause an ASID change */
5673 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5674 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5675 .type = ARM_CP_ALIAS,
5676 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5678 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5679 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5680 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5681 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5682 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5683 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5684 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5685 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5686 .type = ARM_CP_ALIAS,
5687 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5689 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5690 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5691 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5692 .access = PL3_RW, .writefn = vbar_write,
5693 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5695 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5696 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5697 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5698 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5699 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5700 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5701 .access = PL3_RW, .resetvalue = 0,
5702 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5703 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5704 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5705 .access = PL3_RW, .type = ARM_CP_CONST,
5707 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5708 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5709 .access = PL3_RW, .type = ARM_CP_CONST,
5711 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5712 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5713 .access = PL3_RW, .type = ARM_CP_CONST,
5715 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5716 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5717 .access = PL3_W, .type = ARM_CP_NO_RAW,
5718 .writefn = tlbi_aa64_alle3is_write },
5719 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5720 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5721 .access = PL3_W, .type = ARM_CP_NO_RAW,
5722 .writefn = tlbi_aa64_vae3is_write },
5723 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5724 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5725 .access = PL3_W, .type = ARM_CP_NO_RAW,
5726 .writefn = tlbi_aa64_vae3is_write },
5727 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5728 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5729 .access = PL3_W, .type = ARM_CP_NO_RAW,
5730 .writefn = tlbi_aa64_alle3_write },
5731 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5732 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5733 .access = PL3_W, .type = ARM_CP_NO_RAW,
5734 .writefn = tlbi_aa64_vae3_write },
5735 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5736 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5737 .access = PL3_W, .type = ARM_CP_NO_RAW,
5738 .writefn = tlbi_aa64_vae3_write },
5741 #ifndef CONFIG_USER_ONLY
5742 /* Test if system register redirection is to occur in the current state. */
5743 static bool redirect_for_e2h(CPUARMState *env)
5745 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5748 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5752 if (redirect_for_e2h(env)) {
5753 /* Switch to the saved EL2 version of the register. */
5755 readfn = ri->readfn;
5757 readfn = ri->orig_readfn;
5759 if (readfn == NULL) {
5762 return readfn(env, ri);
5765 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5770 if (redirect_for_e2h(env)) {
5771 /* Switch to the saved EL2 version of the register. */
5773 writefn = ri->writefn;
5775 writefn = ri->orig_writefn;
5777 if (writefn == NULL) {
5778 writefn = raw_write;
5780 writefn(env, ri, value);
5783 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5786 uint32_t src_key, dst_key, new_key;
5787 const char *src_name, *dst_name, *new_name;
5788 bool (*feature)(const ARMISARegisters *id);
5791 #define K(op0, op1, crn, crm, op2) \
5792 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5794 static const struct E2HAlias aliases[] = {
5795 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5796 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5797 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5798 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5799 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5800 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5801 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5802 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5803 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5804 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5805 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5806 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5807 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5808 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5809 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5810 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5811 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5812 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5813 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5814 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5815 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5816 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5817 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5818 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5819 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5820 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5821 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5822 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5823 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5824 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5825 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5826 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5829 * Note that redirection of ZCR is mentioned in the description
5830 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5831 * not in the summary table.
5833 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5834 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5835 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
5836 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
5838 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
5839 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5841 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5842 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5843 isar_feature_aa64_scxtnum },
5845 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5846 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5852 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5853 const struct E2HAlias *a = &aliases[i];
5854 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
5857 if (a->feature && !a->feature(&cpu->isar)) {
5861 src_reg = g_hash_table_lookup(cpu->cp_regs,
5862 (gpointer)(uintptr_t)a->src_key);
5863 dst_reg = g_hash_table_lookup(cpu->cp_regs,
5864 (gpointer)(uintptr_t)a->dst_key);
5865 g_assert(src_reg != NULL);
5866 g_assert(dst_reg != NULL);
5868 /* Cross-compare names to detect typos in the keys. */
5869 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5870 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5872 /* None of the core system registers use opaque; we will. */
5873 g_assert(src_reg->opaque == NULL);
5875 /* Create alias before redirection so we dup the right data. */
5876 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5878 new_reg->name = a->new_name;
5879 new_reg->type |= ARM_CP_ALIAS;
5880 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5881 new_reg->access &= PL2_RW | PL3_RW;
5883 ok = g_hash_table_insert(cpu->cp_regs,
5884 (gpointer)(uintptr_t)a->new_key, new_reg);
5887 src_reg->opaque = dst_reg;
5888 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5889 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5890 if (!src_reg->raw_readfn) {
5891 src_reg->raw_readfn = raw_read;
5893 if (!src_reg->raw_writefn) {
5894 src_reg->raw_writefn = raw_write;
5896 src_reg->readfn = el2_e2h_read;
5897 src_reg->writefn = el2_e2h_write;
5902 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5905 int cur_el = arm_current_el(env);
5908 uint64_t hcr = arm_hcr_el2_eff(env);
5911 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5912 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5913 return CP_ACCESS_TRAP_EL2;
5916 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5917 return CP_ACCESS_TRAP;
5919 if (hcr & HCR_TID2) {
5920 return CP_ACCESS_TRAP_EL2;
5923 } else if (hcr & HCR_TID2) {
5924 return CP_ACCESS_TRAP_EL2;
5928 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5929 return CP_ACCESS_TRAP_EL2;
5932 return CP_ACCESS_OK;
5936 * Check for traps to RAS registers, which are controlled
5937 * by HCR_EL2.TERR and SCR_EL3.TERR.
5939 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
5942 int el = arm_current_el(env);
5944 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
5945 return CP_ACCESS_TRAP_EL2;
5947 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
5948 return CP_ACCESS_TRAP_EL3;
5950 return CP_ACCESS_OK;
5953 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
5955 int el = arm_current_el(env);
5957 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
5958 return env->cp15.vdisr_el2;
5960 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
5961 return 0; /* RAZ/WI */
5963 return env->cp15.disr_el1;
5966 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5968 int el = arm_current_el(env);
5970 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
5971 env->cp15.vdisr_el2 = val;
5974 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
5975 return; /* RAZ/WI */
5977 env->cp15.disr_el1 = val;
5981 * Minimal RAS implementation with no Error Records.
5982 * Which means that all of the Error Record registers:
5990 * ERXPFGCDN_EL1 (RASv1p1)
5991 * ERXPFGCTL_EL1 (RASv1p1)
5992 * ERXPFGF_EL1 (RASv1p1)
5996 * may generate UNDEFINED, which is the effect we get by not
5997 * listing them at all.
5999 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6000 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6001 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6002 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6003 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6004 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6005 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6006 .access = PL1_R, .accessfn = access_terr,
6007 .type = ARM_CP_CONST, .resetvalue = 0 },
6008 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6009 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6010 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6011 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6012 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6013 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6017 * Return the exception level to which exceptions should be taken
6018 * via SVEAccessTrap. This excludes the check for whether the exception
6019 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
6020 * be found by testing 0 < fp_exception_el < sve_exception_el.
6022 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
6023 * pseudocode does *not* separate out the FP trap checks, but has them
6024 * all in one function.
6026 int sve_exception_el(CPUARMState *env, int el)
6028 #ifndef CONFIG_USER_ONLY
6029 if (el <= 1 && !el_is_in_host(env, el)) {
6030 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6042 if (el <= 2 && arm_is_el2_enabled(env)) {
6043 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6044 if (env->cp15.hcr_el2 & HCR_E2H) {
6045 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6047 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6056 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6062 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6063 if (arm_feature(env, ARM_FEATURE_EL3)
6064 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6072 * Return the exception level to which exceptions should be taken for SME.
6073 * C.f. the ARM pseudocode function CheckSMEAccess.
6075 int sme_exception_el(CPUARMState *env, int el)
6077 #ifndef CONFIG_USER_ONLY
6078 if (el <= 1 && !el_is_in_host(env, el)) {
6079 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6091 if (el <= 2 && arm_is_el2_enabled(env)) {
6092 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6093 if (env->cp15.hcr_el2 & HCR_E2H) {
6094 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6096 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6105 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6111 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
6112 if (arm_feature(env, ARM_FEATURE_EL3)
6113 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6120 /* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6121 static bool sme_fa64(CPUARMState *env, int el)
6123 if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6127 if (el <= 1 && !el_is_in_host(env, el)) {
6128 if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6132 if (el <= 2 && arm_is_el2_enabled(env)) {
6133 if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6137 if (arm_feature(env, ARM_FEATURE_EL3)) {
6138 if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6147 * Given that SVE is enabled, return the vector length for EL.
6149 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6151 ARMCPU *cpu = env_archcpu(env);
6152 uint64_t *cr = env->vfp.zcr_el;
6153 uint32_t map = cpu->sve_vq.map;
6154 uint32_t len = ARM_MAX_VQ - 1;
6157 cr = env->vfp.smcr_el;
6158 map = cpu->sme_vq.map;
6161 if (el <= 1 && !el_is_in_host(env, el)) {
6162 len = MIN(len, 0xf & (uint32_t)cr[1]);
6164 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6165 len = MIN(len, 0xf & (uint32_t)cr[2]);
6167 if (arm_feature(env, ARM_FEATURE_EL3)) {
6168 len = MIN(len, 0xf & (uint32_t)cr[3]);
6171 map &= MAKE_64BIT_MASK(0, len + 1);
6173 return 31 - clz32(map);
6176 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6178 return ctz32(cpu->sme_vq.map);
6181 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6183 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6186 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6189 int cur_el = arm_current_el(env);
6190 int old_len = sve_vqm1_for_el(env, cur_el);
6193 /* Bits other than [3:0] are RAZ/WI. */
6194 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6195 raw_write(env, ri, value & 0xf);
6198 * Because we arrived here, we know both FP and SVE are enabled;
6199 * otherwise we would have trapped access to the ZCR_ELn register.
6201 new_len = sve_vqm1_for_el(env, cur_el);
6202 if (new_len < old_len) {
6203 aarch64_sve_narrow_vq(env, new_len + 1);
6207 static const ARMCPRegInfo zcr_reginfo[] = {
6208 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6209 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6210 .access = PL1_RW, .type = ARM_CP_SVE,
6211 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6212 .writefn = zcr_write, .raw_writefn = raw_write },
6213 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6214 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6215 .access = PL2_RW, .type = ARM_CP_SVE,
6216 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6217 .writefn = zcr_write, .raw_writefn = raw_write },
6218 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6219 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6220 .access = PL3_RW, .type = ARM_CP_SVE,
6221 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6222 .writefn = zcr_write, .raw_writefn = raw_write },
6225 #ifdef TARGET_AARCH64
6226 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6229 int el = arm_current_el(env);
6232 uint64_t sctlr = arm_sctlr(env, el);
6233 if (!(sctlr & SCTLR_EnTP2)) {
6234 return CP_ACCESS_TRAP;
6237 /* TODO: FEAT_FGT */
6239 && arm_feature(env, ARM_FEATURE_EL3)
6240 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6241 return CP_ACCESS_TRAP_EL3;
6243 return CP_ACCESS_OK;
6246 static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6249 /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6250 if (arm_current_el(env) < 3
6251 && arm_feature(env, ARM_FEATURE_EL3)
6252 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6253 return CP_ACCESS_TRAP_EL3;
6255 return CP_ACCESS_OK;
6258 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6261 helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6262 helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6263 arm_rebuild_hflags(env);
6266 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6269 int cur_el = arm_current_el(env);
6270 int old_len = sve_vqm1_for_el(env, cur_el);
6273 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6274 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6275 raw_write(env, ri, value);
6278 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6279 * when SVL is widened (old values kept, or zeros). Choose to keep the
6280 * current values for simplicity. But for QEMU internals, we must still
6281 * apply the narrower SVL to the Zregs and Pregs -- see the comment
6282 * above aarch64_sve_narrow_vq.
6284 new_len = sve_vqm1_for_el(env, cur_el);
6285 if (new_len < old_len) {
6286 aarch64_sve_narrow_vq(env, new_len + 1);
6290 static const ARMCPRegInfo sme_reginfo[] = {
6291 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6292 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6293 .access = PL0_RW, .accessfn = access_tpidr2,
6294 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6295 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6296 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6297 .access = PL0_RW, .type = ARM_CP_SME,
6298 .fieldoffset = offsetof(CPUARMState, svcr),
6299 .writefn = svcr_write, .raw_writefn = raw_write },
6300 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6301 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6302 .access = PL1_RW, .type = ARM_CP_SME,
6303 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6304 .writefn = smcr_write, .raw_writefn = raw_write },
6305 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6306 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6307 .access = PL2_RW, .type = ARM_CP_SME,
6308 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6309 .writefn = smcr_write, .raw_writefn = raw_write },
6310 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6311 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6312 .access = PL3_RW, .type = ARM_CP_SME,
6313 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6314 .writefn = smcr_write, .raw_writefn = raw_write },
6315 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6316 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6317 .access = PL1_R, .accessfn = access_aa64_tid1,
6319 * IMPLEMENTOR = 0 (software)
6320 * REVISION = 0 (implementation defined)
6321 * SMPS = 0 (no streaming execution priority in QEMU)
6322 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
6324 .type = ARM_CP_CONST, .resetvalue = 0, },
6326 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6328 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6329 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6330 .access = PL1_RW, .accessfn = access_esm,
6331 .type = ARM_CP_CONST, .resetvalue = 0 },
6332 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6333 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6334 .access = PL2_RW, .accessfn = access_esm,
6335 .type = ARM_CP_CONST, .resetvalue = 0 },
6337 #endif /* TARGET_AARCH64 */
6339 static void define_pmu_regs(ARMCPU *cpu)
6342 * v7 performance monitor control register: same implementor
6343 * field as main ID register, and we implement four counters in
6344 * addition to the cycle count register.
6346 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6347 ARMCPRegInfo pmcr = {
6348 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6350 .type = ARM_CP_IO | ARM_CP_ALIAS,
6351 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6352 .accessfn = pmreg_access, .writefn = pmcr_write,
6353 .raw_writefn = raw_write,
6355 ARMCPRegInfo pmcr64 = {
6356 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6357 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6358 .access = PL0_RW, .accessfn = pmreg_access,
6360 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6361 .resetvalue = cpu->isar.reset_pmcr_el0,
6362 .writefn = pmcr_write, .raw_writefn = raw_write,
6365 define_one_arm_cp_reg(cpu, &pmcr);
6366 define_one_arm_cp_reg(cpu, &pmcr64);
6367 for (i = 0; i < pmcrn; i++) {
6368 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6369 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6370 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6371 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6372 ARMCPRegInfo pmev_regs[] = {
6373 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6374 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6375 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6376 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6377 .accessfn = pmreg_access_xevcntr },
6378 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6379 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6380 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6382 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6383 .raw_readfn = pmevcntr_rawread,
6384 .raw_writefn = pmevcntr_rawwrite },
6385 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6386 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6387 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6388 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6389 .accessfn = pmreg_access },
6390 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6391 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6392 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6394 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6395 .raw_writefn = pmevtyper_rawwrite },
6397 define_arm_cp_regs(cpu, pmev_regs);
6398 g_free(pmevcntr_name);
6399 g_free(pmevcntr_el0_name);
6400 g_free(pmevtyper_name);
6401 g_free(pmevtyper_el0_name);
6403 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6404 ARMCPRegInfo v81_pmu_regs[] = {
6405 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6406 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6407 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6408 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6409 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6410 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6411 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6412 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6414 define_arm_cp_regs(cpu, v81_pmu_regs);
6416 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6417 static const ARMCPRegInfo v84_pmmir = {
6418 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6419 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6420 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6423 define_one_arm_cp_reg(cpu, &v84_pmmir);
6427 /* We don't know until after realize whether there's a GICv3
6428 * attached, and that is what registers the gicv3 sysregs.
6429 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6432 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6434 ARMCPU *cpu = env_archcpu(env);
6435 uint64_t pfr1 = cpu->isar.id_pfr1;
6437 if (env->gicv3state) {
6443 #ifndef CONFIG_USER_ONLY
6444 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6446 ARMCPU *cpu = env_archcpu(env);
6447 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6449 if (env->gicv3state) {
6456 /* Shared logic between LORID and the rest of the LOR* registers.
6457 * Secure state exclusion has already been dealt with.
6459 static CPAccessResult access_lor_ns(CPUARMState *env,
6460 const ARMCPRegInfo *ri, bool isread)
6462 int el = arm_current_el(env);
6464 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6465 return CP_ACCESS_TRAP_EL2;
6467 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6468 return CP_ACCESS_TRAP_EL3;
6470 return CP_ACCESS_OK;
6473 static CPAccessResult access_lor_other(CPUARMState *env,
6474 const ARMCPRegInfo *ri, bool isread)
6476 if (arm_is_secure_below_el3(env)) {
6477 /* Access denied in secure mode. */
6478 return CP_ACCESS_TRAP;
6480 return access_lor_ns(env, ri, isread);
6484 * A trivial implementation of ARMv8.1-LOR leaves all of these
6485 * registers fixed at 0, which indicates that there are zero
6486 * supported Limited Ordering regions.
6488 static const ARMCPRegInfo lor_reginfo[] = {
6489 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6490 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6491 .access = PL1_RW, .accessfn = access_lor_other,
6492 .type = ARM_CP_CONST, .resetvalue = 0 },
6493 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6494 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6495 .access = PL1_RW, .accessfn = access_lor_other,
6496 .type = ARM_CP_CONST, .resetvalue = 0 },
6497 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6498 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6499 .access = PL1_RW, .accessfn = access_lor_other,
6500 .type = ARM_CP_CONST, .resetvalue = 0 },
6501 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6502 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6503 .access = PL1_RW, .accessfn = access_lor_other,
6504 .type = ARM_CP_CONST, .resetvalue = 0 },
6505 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6506 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6507 .access = PL1_R, .accessfn = access_lor_ns,
6508 .type = ARM_CP_CONST, .resetvalue = 0 },
6511 #ifdef TARGET_AARCH64
6512 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6515 int el = arm_current_el(env);
6518 arm_is_el2_enabled(env) &&
6519 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6520 return CP_ACCESS_TRAP_EL2;
6523 arm_feature(env, ARM_FEATURE_EL3) &&
6524 !(env->cp15.scr_el3 & SCR_APK)) {
6525 return CP_ACCESS_TRAP_EL3;
6527 return CP_ACCESS_OK;
6530 static const ARMCPRegInfo pauth_reginfo[] = {
6531 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6532 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6533 .access = PL1_RW, .accessfn = access_pauth,
6534 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6535 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6536 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6537 .access = PL1_RW, .accessfn = access_pauth,
6538 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6539 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6540 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6541 .access = PL1_RW, .accessfn = access_pauth,
6542 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6543 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6544 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6545 .access = PL1_RW, .accessfn = access_pauth,
6546 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6547 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6548 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6549 .access = PL1_RW, .accessfn = access_pauth,
6550 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6551 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6552 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6553 .access = PL1_RW, .accessfn = access_pauth,
6554 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6555 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6556 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6557 .access = PL1_RW, .accessfn = access_pauth,
6558 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6559 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6560 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6561 .access = PL1_RW, .accessfn = access_pauth,
6562 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6563 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6564 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6565 .access = PL1_RW, .accessfn = access_pauth,
6566 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6567 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6568 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6569 .access = PL1_RW, .accessfn = access_pauth,
6570 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6573 static const ARMCPRegInfo tlbirange_reginfo[] = {
6574 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6575 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6576 .access = PL1_W, .type = ARM_CP_NO_RAW,
6577 .writefn = tlbi_aa64_rvae1is_write },
6578 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6579 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6580 .access = PL1_W, .type = ARM_CP_NO_RAW,
6581 .writefn = tlbi_aa64_rvae1is_write },
6582 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6583 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6584 .access = PL1_W, .type = ARM_CP_NO_RAW,
6585 .writefn = tlbi_aa64_rvae1is_write },
6586 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6587 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6588 .access = PL1_W, .type = ARM_CP_NO_RAW,
6589 .writefn = tlbi_aa64_rvae1is_write },
6590 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6591 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6592 .access = PL1_W, .type = ARM_CP_NO_RAW,
6593 .writefn = tlbi_aa64_rvae1is_write },
6594 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6595 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6596 .access = PL1_W, .type = ARM_CP_NO_RAW,
6597 .writefn = tlbi_aa64_rvae1is_write },
6598 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6599 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6600 .access = PL1_W, .type = ARM_CP_NO_RAW,
6601 .writefn = tlbi_aa64_rvae1is_write },
6602 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6603 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6604 .access = PL1_W, .type = ARM_CP_NO_RAW,
6605 .writefn = tlbi_aa64_rvae1is_write },
6606 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6607 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6608 .access = PL1_W, .type = ARM_CP_NO_RAW,
6609 .writefn = tlbi_aa64_rvae1_write },
6610 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6611 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6612 .access = PL1_W, .type = ARM_CP_NO_RAW,
6613 .writefn = tlbi_aa64_rvae1_write },
6614 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6615 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6616 .access = PL1_W, .type = ARM_CP_NO_RAW,
6617 .writefn = tlbi_aa64_rvae1_write },
6618 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6619 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6620 .access = PL1_W, .type = ARM_CP_NO_RAW,
6621 .writefn = tlbi_aa64_rvae1_write },
6622 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6623 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6624 .access = PL2_W, .type = ARM_CP_NOP },
6625 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6626 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6627 .access = PL2_W, .type = ARM_CP_NOP },
6628 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6629 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
6630 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6631 .writefn = tlbi_aa64_rvae2is_write },
6632 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6633 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
6634 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6635 .writefn = tlbi_aa64_rvae2is_write },
6636 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6637 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6638 .access = PL2_W, .type = ARM_CP_NOP },
6639 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6640 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6641 .access = PL2_W, .type = ARM_CP_NOP },
6642 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6643 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
6644 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6645 .writefn = tlbi_aa64_rvae2is_write },
6646 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6647 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
6648 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6649 .writefn = tlbi_aa64_rvae2is_write },
6650 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6651 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
6652 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6653 .writefn = tlbi_aa64_rvae2_write },
6654 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6655 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
6656 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6657 .writefn = tlbi_aa64_rvae2_write },
6658 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6659 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6660 .access = PL3_W, .type = ARM_CP_NO_RAW,
6661 .writefn = tlbi_aa64_rvae3is_write },
6662 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6663 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6664 .access = PL3_W, .type = ARM_CP_NO_RAW,
6665 .writefn = tlbi_aa64_rvae3is_write },
6666 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6667 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6668 .access = PL3_W, .type = ARM_CP_NO_RAW,
6669 .writefn = tlbi_aa64_rvae3is_write },
6670 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6671 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6672 .access = PL3_W, .type = ARM_CP_NO_RAW,
6673 .writefn = tlbi_aa64_rvae3is_write },
6674 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6675 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6676 .access = PL3_W, .type = ARM_CP_NO_RAW,
6677 .writefn = tlbi_aa64_rvae3_write },
6678 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6679 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6680 .access = PL3_W, .type = ARM_CP_NO_RAW,
6681 .writefn = tlbi_aa64_rvae3_write },
6684 static const ARMCPRegInfo tlbios_reginfo[] = {
6685 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6686 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6687 .access = PL1_W, .type = ARM_CP_NO_RAW,
6688 .writefn = tlbi_aa64_vmalle1is_write },
6689 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6690 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6691 .access = PL1_W, .type = ARM_CP_NO_RAW,
6692 .writefn = tlbi_aa64_vae1is_write },
6693 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6694 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6695 .access = PL1_W, .type = ARM_CP_NO_RAW,
6696 .writefn = tlbi_aa64_vmalle1is_write },
6697 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6698 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6699 .access = PL1_W, .type = ARM_CP_NO_RAW,
6700 .writefn = tlbi_aa64_vae1is_write },
6701 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6702 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6703 .access = PL1_W, .type = ARM_CP_NO_RAW,
6704 .writefn = tlbi_aa64_vae1is_write },
6705 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6706 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6707 .access = PL1_W, .type = ARM_CP_NO_RAW,
6708 .writefn = tlbi_aa64_vae1is_write },
6709 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6710 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
6711 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6712 .writefn = tlbi_aa64_alle2is_write },
6713 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6714 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
6715 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6716 .writefn = tlbi_aa64_vae2is_write },
6717 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6718 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6719 .access = PL2_W, .type = ARM_CP_NO_RAW,
6720 .writefn = tlbi_aa64_alle1is_write },
6721 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6722 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
6723 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6724 .writefn = tlbi_aa64_vae2is_write },
6725 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6726 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6727 .access = PL2_W, .type = ARM_CP_NO_RAW,
6728 .writefn = tlbi_aa64_alle1is_write },
6729 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6730 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6731 .access = PL2_W, .type = ARM_CP_NOP },
6732 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6733 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6734 .access = PL2_W, .type = ARM_CP_NOP },
6735 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6736 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
6737 .access = PL2_W, .type = ARM_CP_NOP },
6738 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6739 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
6740 .access = PL2_W, .type = ARM_CP_NOP },
6741 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
6742 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
6743 .access = PL3_W, .type = ARM_CP_NO_RAW,
6744 .writefn = tlbi_aa64_alle3is_write },
6745 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
6746 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
6747 .access = PL3_W, .type = ARM_CP_NO_RAW,
6748 .writefn = tlbi_aa64_vae3is_write },
6749 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
6750 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
6751 .access = PL3_W, .type = ARM_CP_NO_RAW,
6752 .writefn = tlbi_aa64_vae3is_write },
6755 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6760 /* Success sets NZCV = 0000. */
6761 env->NF = env->CF = env->VF = 0, env->ZF = 1;
6763 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6765 * ??? Failed, for unknown reasons in the crypto subsystem.
6766 * The best we can do is log the reason and return the
6767 * timed-out indication to the guest. There is no reason
6768 * we know to expect this failure to be transitory, so the
6769 * guest may well hang retrying the operation.
6771 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6772 ri->name, error_get_pretty(err));
6775 env->ZF = 0; /* NZCF = 0100 */
6781 /* We do not support re-seeding, so the two registers operate the same. */
6782 static const ARMCPRegInfo rndr_reginfo[] = {
6783 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6784 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6785 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6786 .access = PL0_R, .readfn = rndr_readfn },
6787 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6788 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6789 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6790 .access = PL0_R, .readfn = rndr_readfn },
6793 #ifndef CONFIG_USER_ONLY
6794 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6797 ARMCPU *cpu = env_archcpu(env);
6798 /* CTR_EL0 System register -> DminLine, bits [19:16] */
6799 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6800 uint64_t vaddr_in = (uint64_t) value;
6801 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6803 int mem_idx = cpu_mmu_index(env, false);
6805 /* This won't be crossing page boundaries */
6806 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6812 /* RCU lock is already being held */
6813 mr = memory_region_from_host(haddr, &offset);
6816 memory_region_writeback(mr, offset, dline_size);
6821 static const ARMCPRegInfo dcpop_reg[] = {
6822 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6823 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6824 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6825 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6828 static const ARMCPRegInfo dcpodp_reg[] = {
6829 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6830 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6831 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
6832 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
6834 #endif /*CONFIG_USER_ONLY*/
6836 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6839 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6840 return CP_ACCESS_TRAP_EL2;
6843 return CP_ACCESS_OK;
6846 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6849 int el = arm_current_el(env);
6851 if (el < 2 && arm_is_el2_enabled(env)) {
6852 uint64_t hcr = arm_hcr_el2_eff(env);
6853 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
6854 return CP_ACCESS_TRAP_EL2;
6858 arm_feature(env, ARM_FEATURE_EL3) &&
6859 !(env->cp15.scr_el3 & SCR_ATA)) {
6860 return CP_ACCESS_TRAP_EL3;
6862 return CP_ACCESS_OK;
6865 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
6867 return env->pstate & PSTATE_TCO;
6870 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6872 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
6875 static const ARMCPRegInfo mte_reginfo[] = {
6876 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
6877 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
6878 .access = PL1_RW, .accessfn = access_mte,
6879 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
6880 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
6881 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
6882 .access = PL1_RW, .accessfn = access_mte,
6883 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
6884 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
6885 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
6886 .access = PL2_RW, .accessfn = access_mte,
6887 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
6888 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
6889 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
6891 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
6892 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
6893 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
6894 .access = PL1_RW, .accessfn = access_mte,
6895 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
6896 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
6897 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
6898 .access = PL1_RW, .accessfn = access_mte,
6899 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
6900 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
6901 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
6902 .access = PL1_R, .accessfn = access_aa64_tid5,
6903 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
6904 { .name = "TCO", .state = ARM_CP_STATE_AA64,
6905 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6906 .type = ARM_CP_NO_RAW,
6907 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
6908 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
6909 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
6910 .type = ARM_CP_NOP, .access = PL1_W,
6911 .accessfn = aa64_cacheop_poc_access },
6912 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
6913 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
6914 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6915 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
6916 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
6917 .type = ARM_CP_NOP, .access = PL1_W,
6918 .accessfn = aa64_cacheop_poc_access },
6919 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
6920 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
6921 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6922 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
6923 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
6924 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6925 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
6926 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
6927 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6928 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
6929 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
6930 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6931 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
6932 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
6933 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6936 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
6937 { .name = "TCO", .state = ARM_CP_STATE_AA64,
6938 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6939 .type = ARM_CP_CONST, .access = PL0_RW, },
6942 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
6943 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
6944 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
6945 .type = ARM_CP_NOP, .access = PL0_W,
6946 .accessfn = aa64_cacheop_poc_access },
6947 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
6948 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
6949 .type = ARM_CP_NOP, .access = PL0_W,
6950 .accessfn = aa64_cacheop_poc_access },
6951 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
6952 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
6953 .type = ARM_CP_NOP, .access = PL0_W,
6954 .accessfn = aa64_cacheop_poc_access },
6955 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
6956 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
6957 .type = ARM_CP_NOP, .access = PL0_W,
6958 .accessfn = aa64_cacheop_poc_access },
6959 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
6960 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
6961 .type = ARM_CP_NOP, .access = PL0_W,
6962 .accessfn = aa64_cacheop_poc_access },
6963 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
6964 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
6965 .type = ARM_CP_NOP, .access = PL0_W,
6966 .accessfn = aa64_cacheop_poc_access },
6967 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
6968 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
6969 .type = ARM_CP_NOP, .access = PL0_W,
6970 .accessfn = aa64_cacheop_poc_access },
6971 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
6972 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
6973 .type = ARM_CP_NOP, .access = PL0_W,
6974 .accessfn = aa64_cacheop_poc_access },
6975 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
6976 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
6977 .access = PL0_W, .type = ARM_CP_DC_GVA,
6978 #ifndef CONFIG_USER_ONLY
6979 /* Avoid overhead of an access check that always passes in user-mode */
6980 .accessfn = aa64_zva_access,
6983 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
6984 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
6985 .access = PL0_W, .type = ARM_CP_DC_GZVA,
6986 #ifndef CONFIG_USER_ONLY
6987 /* Avoid overhead of an access check that always passes in user-mode */
6988 .accessfn = aa64_zva_access,
6993 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
6996 uint64_t hcr = arm_hcr_el2_eff(env);
6997 int el = arm_current_el(env);
6999 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7000 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7001 if (hcr & HCR_TGE) {
7002 return CP_ACCESS_TRAP_EL2;
7004 return CP_ACCESS_TRAP;
7006 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7007 return CP_ACCESS_TRAP_EL2;
7009 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7010 return CP_ACCESS_TRAP_EL2;
7013 && arm_feature(env, ARM_FEATURE_EL3)
7014 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7015 return CP_ACCESS_TRAP_EL3;
7017 return CP_ACCESS_OK;
7020 static const ARMCPRegInfo scxtnum_reginfo[] = {
7021 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7022 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7023 .access = PL0_RW, .accessfn = access_scxtnum,
7024 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7025 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7026 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7027 .access = PL1_RW, .accessfn = access_scxtnum,
7028 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7029 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7030 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7031 .access = PL2_RW, .accessfn = access_scxtnum,
7032 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7033 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7034 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7036 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7038 #endif /* TARGET_AARCH64 */
7040 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7043 int el = arm_current_el(env);
7046 uint64_t sctlr = arm_sctlr(env, el);
7047 if (!(sctlr & SCTLR_EnRCTX)) {
7048 return CP_ACCESS_TRAP;
7050 } else if (el == 1) {
7051 uint64_t hcr = arm_hcr_el2_eff(env);
7053 return CP_ACCESS_TRAP_EL2;
7056 return CP_ACCESS_OK;
7059 static const ARMCPRegInfo predinv_reginfo[] = {
7060 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7061 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7062 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7063 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7064 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7065 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7066 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7067 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7068 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7070 * Note the AArch32 opcodes have a different OPC1.
7072 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7073 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7074 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7075 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7076 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7077 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7078 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7079 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7080 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7083 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7085 /* Read the high 32 bits of the current CCSIDR */
7086 return extract64(ccsidr_read(env, ri), 32, 32);
7089 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7090 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7091 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7093 .accessfn = access_aa64_tid2,
7094 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7097 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7100 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7101 return CP_ACCESS_TRAP_EL2;
7104 return CP_ACCESS_OK;
7107 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7110 if (arm_feature(env, ARM_FEATURE_V8)) {
7111 return access_aa64_tid3(env, ri, isread);
7114 return CP_ACCESS_OK;
7117 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7120 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7121 return CP_ACCESS_TRAP_EL2;
7124 return CP_ACCESS_OK;
7127 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7128 const ARMCPRegInfo *ri, bool isread)
7131 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7132 * in v7A, not in v8A.
7134 if (!arm_feature(env, ARM_FEATURE_V8) &&
7135 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7136 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7137 return CP_ACCESS_TRAP_EL2;
7139 return CP_ACCESS_OK;
7142 static const ARMCPRegInfo jazelle_regs[] = {
7144 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7145 .access = PL1_R, .accessfn = access_jazelle,
7146 .type = ARM_CP_CONST, .resetvalue = 0 },
7148 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7149 .accessfn = access_joscr_jmcr,
7150 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7152 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7153 .accessfn = access_joscr_jmcr,
7154 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7157 static const ARMCPRegInfo contextidr_el2 = {
7158 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7159 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7161 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7164 static const ARMCPRegInfo vhe_reginfo[] = {
7165 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7166 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7167 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7168 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7169 #ifndef CONFIG_USER_ONLY
7170 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7171 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7173 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7174 .type = ARM_CP_IO, .access = PL2_RW,
7175 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7176 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7177 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7178 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7179 .resetfn = gt_hv_timer_reset,
7180 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7181 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7183 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7185 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7186 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7187 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7188 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7189 .type = ARM_CP_IO | ARM_CP_ALIAS,
7190 .access = PL2_RW, .accessfn = e2h_access,
7191 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7192 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7193 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7194 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7195 .type = ARM_CP_IO | ARM_CP_ALIAS,
7196 .access = PL2_RW, .accessfn = e2h_access,
7197 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7198 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7199 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7200 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7201 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7202 .access = PL2_RW, .accessfn = e2h_access,
7203 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7204 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7205 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7206 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7207 .access = PL2_RW, .accessfn = e2h_access,
7208 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7209 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7210 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7211 .type = ARM_CP_IO | ARM_CP_ALIAS,
7212 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7213 .access = PL2_RW, .accessfn = e2h_access,
7214 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7215 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7216 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7217 .type = ARM_CP_IO | ARM_CP_ALIAS,
7218 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7219 .access = PL2_RW, .accessfn = e2h_access,
7220 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7224 #ifndef CONFIG_USER_ONLY
7225 static const ARMCPRegInfo ats1e1_reginfo[] = {
7226 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7227 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7228 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7229 .writefn = ats_write64 },
7230 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7231 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7232 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7233 .writefn = ats_write64 },
7236 static const ARMCPRegInfo ats1cp_reginfo[] = {
7237 { .name = "ATS1CPRP",
7238 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7239 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7240 .writefn = ats_write },
7241 { .name = "ATS1CPWP",
7242 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7243 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7244 .writefn = ats_write },
7249 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7250 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7251 * is non-zero, which is never for ARMv7, optionally in ARMv8
7252 * and mandatorily for ARMv8.2 and up.
7253 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7254 * implementation is RAZ/WI we can ignore this detail, as we
7257 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7258 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7259 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7260 .access = PL1_RW, .accessfn = access_tacr,
7261 .type = ARM_CP_CONST, .resetvalue = 0 },
7262 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7263 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7264 .access = PL2_RW, .type = ARM_CP_CONST,
7268 void register_cp_regs_for_features(ARMCPU *cpu)
7270 /* Register all the coprocessor registers based on feature bits */
7271 CPUARMState *env = &cpu->env;
7272 if (arm_feature(env, ARM_FEATURE_M)) {
7273 /* M profile has no coprocessor registers */
7277 define_arm_cp_regs(cpu, cp_reginfo);
7278 if (!arm_feature(env, ARM_FEATURE_V8)) {
7279 /* Must go early as it is full of wildcards that may be
7280 * overridden by later definitions.
7282 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7285 if (arm_feature(env, ARM_FEATURE_V6)) {
7286 /* The ID registers all have impdef reset values */
7287 ARMCPRegInfo v6_idregs[] = {
7288 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7289 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7290 .access = PL1_R, .type = ARM_CP_CONST,
7291 .accessfn = access_aa32_tid3,
7292 .resetvalue = cpu->isar.id_pfr0 },
7293 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7294 * the value of the GIC field until after we define these regs.
7296 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7297 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7298 .access = PL1_R, .type = ARM_CP_NO_RAW,
7299 .accessfn = access_aa32_tid3,
7300 .readfn = id_pfr1_read,
7301 .writefn = arm_cp_write_ignore },
7302 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7303 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7304 .access = PL1_R, .type = ARM_CP_CONST,
7305 .accessfn = access_aa32_tid3,
7306 .resetvalue = cpu->isar.id_dfr0 },
7307 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7308 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7309 .access = PL1_R, .type = ARM_CP_CONST,
7310 .accessfn = access_aa32_tid3,
7311 .resetvalue = cpu->id_afr0 },
7312 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7313 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7314 .access = PL1_R, .type = ARM_CP_CONST,
7315 .accessfn = access_aa32_tid3,
7316 .resetvalue = cpu->isar.id_mmfr0 },
7317 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7318 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7319 .access = PL1_R, .type = ARM_CP_CONST,
7320 .accessfn = access_aa32_tid3,
7321 .resetvalue = cpu->isar.id_mmfr1 },
7322 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7323 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7324 .access = PL1_R, .type = ARM_CP_CONST,
7325 .accessfn = access_aa32_tid3,
7326 .resetvalue = cpu->isar.id_mmfr2 },
7327 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7328 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7329 .access = PL1_R, .type = ARM_CP_CONST,
7330 .accessfn = access_aa32_tid3,
7331 .resetvalue = cpu->isar.id_mmfr3 },
7332 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7333 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7334 .access = PL1_R, .type = ARM_CP_CONST,
7335 .accessfn = access_aa32_tid3,
7336 .resetvalue = cpu->isar.id_isar0 },
7337 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7338 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7339 .access = PL1_R, .type = ARM_CP_CONST,
7340 .accessfn = access_aa32_tid3,
7341 .resetvalue = cpu->isar.id_isar1 },
7342 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7343 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7344 .access = PL1_R, .type = ARM_CP_CONST,
7345 .accessfn = access_aa32_tid3,
7346 .resetvalue = cpu->isar.id_isar2 },
7347 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7348 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7349 .access = PL1_R, .type = ARM_CP_CONST,
7350 .accessfn = access_aa32_tid3,
7351 .resetvalue = cpu->isar.id_isar3 },
7352 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7353 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7354 .access = PL1_R, .type = ARM_CP_CONST,
7355 .accessfn = access_aa32_tid3,
7356 .resetvalue = cpu->isar.id_isar4 },
7357 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7358 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7359 .access = PL1_R, .type = ARM_CP_CONST,
7360 .accessfn = access_aa32_tid3,
7361 .resetvalue = cpu->isar.id_isar5 },
7362 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7363 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7364 .access = PL1_R, .type = ARM_CP_CONST,
7365 .accessfn = access_aa32_tid3,
7366 .resetvalue = cpu->isar.id_mmfr4 },
7367 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7368 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7369 .access = PL1_R, .type = ARM_CP_CONST,
7370 .accessfn = access_aa32_tid3,
7371 .resetvalue = cpu->isar.id_isar6 },
7373 define_arm_cp_regs(cpu, v6_idregs);
7374 define_arm_cp_regs(cpu, v6_cp_reginfo);
7376 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7378 if (arm_feature(env, ARM_FEATURE_V6K)) {
7379 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7381 if (arm_feature(env, ARM_FEATURE_V7MP) &&
7382 !arm_feature(env, ARM_FEATURE_PMSA)) {
7383 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7385 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7386 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7388 if (arm_feature(env, ARM_FEATURE_V7)) {
7389 ARMCPRegInfo clidr = {
7390 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7391 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7392 .access = PL1_R, .type = ARM_CP_CONST,
7393 .accessfn = access_aa64_tid2,
7394 .resetvalue = cpu->clidr
7396 define_one_arm_cp_reg(cpu, &clidr);
7397 define_arm_cp_regs(cpu, v7_cp_reginfo);
7398 define_debug_regs(cpu);
7399 define_pmu_regs(cpu);
7401 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7403 if (arm_feature(env, ARM_FEATURE_V8)) {
7405 * v8 ID registers, which all have impdef reset values.
7406 * Note that within the ID register ranges the unused slots
7407 * must all RAZ, not UNDEF; future architecture versions may
7408 * define new registers here.
7409 * ID registers which are AArch64 views of the AArch32 ID registers
7410 * which already existed in v6 and v7 are handled elsewhere,
7414 ARMCPRegInfo v8_idregs[] = {
7416 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7417 * emulation because we don't know the right value for the
7418 * GIC field until after we define these regs.
7420 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7421 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7423 #ifdef CONFIG_USER_ONLY
7424 .type = ARM_CP_CONST,
7425 .resetvalue = cpu->isar.id_aa64pfr0
7427 .type = ARM_CP_NO_RAW,
7428 .accessfn = access_aa64_tid3,
7429 .readfn = id_aa64pfr0_read,
7430 .writefn = arm_cp_write_ignore
7433 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7434 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7435 .access = PL1_R, .type = ARM_CP_CONST,
7436 .accessfn = access_aa64_tid3,
7437 .resetvalue = cpu->isar.id_aa64pfr1},
7438 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7439 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7440 .access = PL1_R, .type = ARM_CP_CONST,
7441 .accessfn = access_aa64_tid3,
7443 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7444 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7445 .access = PL1_R, .type = ARM_CP_CONST,
7446 .accessfn = access_aa64_tid3,
7448 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7449 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7450 .access = PL1_R, .type = ARM_CP_CONST,
7451 .accessfn = access_aa64_tid3,
7452 .resetvalue = cpu->isar.id_aa64zfr0 },
7453 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7454 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7455 .access = PL1_R, .type = ARM_CP_CONST,
7456 .accessfn = access_aa64_tid3,
7457 .resetvalue = cpu->isar.id_aa64smfr0 },
7458 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7459 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7460 .access = PL1_R, .type = ARM_CP_CONST,
7461 .accessfn = access_aa64_tid3,
7463 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7464 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7465 .access = PL1_R, .type = ARM_CP_CONST,
7466 .accessfn = access_aa64_tid3,
7468 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7469 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7470 .access = PL1_R, .type = ARM_CP_CONST,
7471 .accessfn = access_aa64_tid3,
7472 .resetvalue = cpu->isar.id_aa64dfr0 },
7473 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7474 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7475 .access = PL1_R, .type = ARM_CP_CONST,
7476 .accessfn = access_aa64_tid3,
7477 .resetvalue = cpu->isar.id_aa64dfr1 },
7478 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7479 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7480 .access = PL1_R, .type = ARM_CP_CONST,
7481 .accessfn = access_aa64_tid3,
7483 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7484 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7485 .access = PL1_R, .type = ARM_CP_CONST,
7486 .accessfn = access_aa64_tid3,
7488 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7489 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7490 .access = PL1_R, .type = ARM_CP_CONST,
7491 .accessfn = access_aa64_tid3,
7492 .resetvalue = cpu->id_aa64afr0 },
7493 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7494 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7495 .access = PL1_R, .type = ARM_CP_CONST,
7496 .accessfn = access_aa64_tid3,
7497 .resetvalue = cpu->id_aa64afr1 },
7498 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7499 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7500 .access = PL1_R, .type = ARM_CP_CONST,
7501 .accessfn = access_aa64_tid3,
7503 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7504 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7505 .access = PL1_R, .type = ARM_CP_CONST,
7506 .accessfn = access_aa64_tid3,
7508 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7509 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7510 .access = PL1_R, .type = ARM_CP_CONST,
7511 .accessfn = access_aa64_tid3,
7512 .resetvalue = cpu->isar.id_aa64isar0 },
7513 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7514 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7515 .access = PL1_R, .type = ARM_CP_CONST,
7516 .accessfn = access_aa64_tid3,
7517 .resetvalue = cpu->isar.id_aa64isar1 },
7518 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7519 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7520 .access = PL1_R, .type = ARM_CP_CONST,
7521 .accessfn = access_aa64_tid3,
7523 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7524 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7525 .access = PL1_R, .type = ARM_CP_CONST,
7526 .accessfn = access_aa64_tid3,
7528 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7529 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7530 .access = PL1_R, .type = ARM_CP_CONST,
7531 .accessfn = access_aa64_tid3,
7533 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7534 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7535 .access = PL1_R, .type = ARM_CP_CONST,
7536 .accessfn = access_aa64_tid3,
7538 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7539 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7540 .access = PL1_R, .type = ARM_CP_CONST,
7541 .accessfn = access_aa64_tid3,
7543 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7544 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7545 .access = PL1_R, .type = ARM_CP_CONST,
7546 .accessfn = access_aa64_tid3,
7548 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7549 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7550 .access = PL1_R, .type = ARM_CP_CONST,
7551 .accessfn = access_aa64_tid3,
7552 .resetvalue = cpu->isar.id_aa64mmfr0 },
7553 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7554 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7555 .access = PL1_R, .type = ARM_CP_CONST,
7556 .accessfn = access_aa64_tid3,
7557 .resetvalue = cpu->isar.id_aa64mmfr1 },
7558 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7559 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7560 .access = PL1_R, .type = ARM_CP_CONST,
7561 .accessfn = access_aa64_tid3,
7562 .resetvalue = cpu->isar.id_aa64mmfr2 },
7563 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7564 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7565 .access = PL1_R, .type = ARM_CP_CONST,
7566 .accessfn = access_aa64_tid3,
7568 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7569 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7570 .access = PL1_R, .type = ARM_CP_CONST,
7571 .accessfn = access_aa64_tid3,
7573 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7574 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7575 .access = PL1_R, .type = ARM_CP_CONST,
7576 .accessfn = access_aa64_tid3,
7578 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7579 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7580 .access = PL1_R, .type = ARM_CP_CONST,
7581 .accessfn = access_aa64_tid3,
7583 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7584 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7585 .access = PL1_R, .type = ARM_CP_CONST,
7586 .accessfn = access_aa64_tid3,
7588 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7589 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7590 .access = PL1_R, .type = ARM_CP_CONST,
7591 .accessfn = access_aa64_tid3,
7592 .resetvalue = cpu->isar.mvfr0 },
7593 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7594 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7595 .access = PL1_R, .type = ARM_CP_CONST,
7596 .accessfn = access_aa64_tid3,
7597 .resetvalue = cpu->isar.mvfr1 },
7598 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7599 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7600 .access = PL1_R, .type = ARM_CP_CONST,
7601 .accessfn = access_aa64_tid3,
7602 .resetvalue = cpu->isar.mvfr2 },
7604 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
7605 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
7606 * as RAZ, since it is in the "reserved for future ID
7607 * registers, RAZ" part of the AArch32 encoding space.
7609 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
7610 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7611 .access = PL1_R, .type = ARM_CP_CONST,
7612 .accessfn = access_aa64_tid3,
7614 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
7615 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7616 .access = PL1_R, .type = ARM_CP_CONST,
7617 .accessfn = access_aa64_tid3,
7619 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
7620 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7621 .access = PL1_R, .type = ARM_CP_CONST,
7622 .accessfn = access_aa64_tid3,
7625 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
7626 * they're also RAZ for AArch64, and in v8 are gradually
7627 * being filled with AArch64-view-of-AArch32-ID-register
7628 * for new ID registers.
7630 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
7631 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7632 .access = PL1_R, .type = ARM_CP_CONST,
7633 .accessfn = access_aa64_tid3,
7635 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7636 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7637 .access = PL1_R, .type = ARM_CP_CONST,
7638 .accessfn = access_aa64_tid3,
7639 .resetvalue = cpu->isar.id_pfr2 },
7640 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
7641 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7642 .access = PL1_R, .type = ARM_CP_CONST,
7643 .accessfn = access_aa64_tid3,
7644 .resetvalue = cpu->isar.id_dfr1 },
7645 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
7646 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7647 .access = PL1_R, .type = ARM_CP_CONST,
7648 .accessfn = access_aa64_tid3,
7649 .resetvalue = cpu->isar.id_mmfr5 },
7650 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
7651 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7652 .access = PL1_R, .type = ARM_CP_CONST,
7653 .accessfn = access_aa64_tid3,
7655 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7656 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7657 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7658 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7659 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7660 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7661 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7662 .resetvalue = cpu->pmceid0 },
7663 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7664 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7665 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7666 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7667 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7668 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7669 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7670 .resetvalue = cpu->pmceid1 },
7672 #ifdef CONFIG_USER_ONLY
7673 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7674 { .name = "ID_AA64PFR0_EL1",
7675 .exported_bits = 0x000f000f00ff0000,
7676 .fixed_bits = 0x0000000000000011 },
7677 { .name = "ID_AA64PFR1_EL1",
7678 .exported_bits = 0x00000000000000f0 },
7679 { .name = "ID_AA64PFR*_EL1_RESERVED",
7681 { .name = "ID_AA64ZFR0_EL1" },
7682 { .name = "ID_AA64MMFR0_EL1",
7683 .fixed_bits = 0x00000000ff000000 },
7684 { .name = "ID_AA64MMFR1_EL1" },
7685 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7687 { .name = "ID_AA64DFR0_EL1",
7688 .fixed_bits = 0x0000000000000006 },
7689 { .name = "ID_AA64DFR1_EL1" },
7690 { .name = "ID_AA64DFR*_EL1_RESERVED",
7692 { .name = "ID_AA64AFR*",
7694 { .name = "ID_AA64ISAR0_EL1",
7695 .exported_bits = 0x00fffffff0fffff0 },
7696 { .name = "ID_AA64ISAR1_EL1",
7697 .exported_bits = 0x000000f0ffffffff },
7698 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7701 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7703 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7704 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7705 !arm_feature(env, ARM_FEATURE_EL2)) {
7706 ARMCPRegInfo rvbar = {
7707 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7708 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7710 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7712 define_one_arm_cp_reg(cpu, &rvbar);
7714 define_arm_cp_regs(cpu, v8_idregs);
7715 define_arm_cp_regs(cpu, v8_cp_reginfo);
7717 for (i = 4; i < 16; i++) {
7719 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
7720 * For pre-v8 cores there are RAZ patterns for these in
7721 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
7722 * v8 extends the "must RAZ" part of the ID register space
7723 * to also cover c0, 0, c{8-15}, {0-7}.
7724 * These are STATE_AA32 because in the AArch64 sysreg space
7725 * c4-c7 is where the AArch64 ID registers live (and we've
7726 * already defined those in v8_idregs[]), and c8-c15 are not
7727 * "must RAZ" for AArch64.
7729 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
7730 ARMCPRegInfo v8_aa32_raz_idregs = {
7732 .state = ARM_CP_STATE_AA32,
7733 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
7734 .access = PL1_R, .type = ARM_CP_CONST,
7735 .accessfn = access_aa64_tid3,
7737 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
7742 * Register the base EL2 cpregs.
7743 * Pre v8, these registers are implemented only as part of the
7744 * Virtualization Extensions (EL2 present). Beginning with v8,
7745 * if EL2 is missing but EL3 is enabled, mostly these become
7746 * RES0 from EL3, with some specific exceptions.
7748 if (arm_feature(env, ARM_FEATURE_EL2)
7749 || (arm_feature(env, ARM_FEATURE_EL3)
7750 && arm_feature(env, ARM_FEATURE_V8))) {
7751 uint64_t vmpidr_def = mpidr_read_val(env);
7752 ARMCPRegInfo vpidr_regs[] = {
7753 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7754 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7755 .access = PL2_RW, .accessfn = access_el3_aa32ns,
7756 .resetvalue = cpu->midr,
7757 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7758 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
7759 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7760 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7761 .access = PL2_RW, .resetvalue = cpu->midr,
7762 .type = ARM_CP_EL3_NO_EL2_C_NZ,
7763 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7764 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7765 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7766 .access = PL2_RW, .accessfn = access_el3_aa32ns,
7767 .resetvalue = vmpidr_def,
7768 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7769 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
7770 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7771 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7772 .access = PL2_RW, .resetvalue = vmpidr_def,
7773 .type = ARM_CP_EL3_NO_EL2_C_NZ,
7774 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
7777 * The only field of MDCR_EL2 that has a defined architectural reset
7778 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
7780 ARMCPRegInfo mdcr_el2 = {
7781 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
7782 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
7783 .writefn = mdcr_el2_write,
7784 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
7785 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
7787 define_one_arm_cp_reg(cpu, &mdcr_el2);
7788 define_arm_cp_regs(cpu, vpidr_regs);
7789 define_arm_cp_regs(cpu, el2_cp_reginfo);
7790 if (arm_feature(env, ARM_FEATURE_V8)) {
7791 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7793 if (cpu_isar_feature(aa64_sel2, cpu)) {
7794 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7796 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7797 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7798 ARMCPRegInfo rvbar = {
7799 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7800 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7802 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7804 define_one_arm_cp_reg(cpu, &rvbar);
7808 /* Register the base EL3 cpregs. */
7809 if (arm_feature(env, ARM_FEATURE_EL3)) {
7810 define_arm_cp_regs(cpu, el3_cp_reginfo);
7811 ARMCPRegInfo el3_regs[] = {
7812 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7813 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7815 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7817 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7818 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7820 .raw_writefn = raw_write, .writefn = sctlr_write,
7821 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7822 .resetvalue = cpu->reset_sctlr },
7825 define_arm_cp_regs(cpu, el3_regs);
7827 /* The behaviour of NSACR is sufficiently various that we don't
7828 * try to describe it in a single reginfo:
7829 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7830 * reads as constant 0xc00 from NS EL1 and NS EL2
7831 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7832 * if v7 without EL3, register doesn't exist
7833 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7835 if (arm_feature(env, ARM_FEATURE_EL3)) {
7836 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7837 static const ARMCPRegInfo nsacr = {
7838 .name = "NSACR", .type = ARM_CP_CONST,
7839 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7840 .access = PL1_RW, .accessfn = nsacr_access,
7843 define_one_arm_cp_reg(cpu, &nsacr);
7845 static const ARMCPRegInfo nsacr = {
7847 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7848 .access = PL3_RW | PL1_R,
7850 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7852 define_one_arm_cp_reg(cpu, &nsacr);
7855 if (arm_feature(env, ARM_FEATURE_V8)) {
7856 static const ARMCPRegInfo nsacr = {
7857 .name = "NSACR", .type = ARM_CP_CONST,
7858 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7862 define_one_arm_cp_reg(cpu, &nsacr);
7866 if (arm_feature(env, ARM_FEATURE_PMSA)) {
7867 if (arm_feature(env, ARM_FEATURE_V6)) {
7868 /* PMSAv6 not implemented */
7869 assert(arm_feature(env, ARM_FEATURE_V7));
7870 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7871 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7873 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7876 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7877 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
7878 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
7879 if (cpu_isar_feature(aa32_hpd, cpu)) {
7880 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7883 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7884 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7886 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7887 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7889 if (arm_feature(env, ARM_FEATURE_VAPA)) {
7890 define_arm_cp_regs(cpu, vapa_cp_reginfo);
7892 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7893 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7895 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7896 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7898 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7899 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7901 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7902 define_arm_cp_regs(cpu, omap_cp_reginfo);
7904 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7905 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7907 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7908 define_arm_cp_regs(cpu, xscale_cp_reginfo);
7910 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7911 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7913 if (arm_feature(env, ARM_FEATURE_LPAE)) {
7914 define_arm_cp_regs(cpu, lpae_cp_reginfo);
7916 if (cpu_isar_feature(aa32_jazelle, cpu)) {
7917 define_arm_cp_regs(cpu, jazelle_regs);
7919 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7920 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7921 * be read-only (ie write causes UNDEF exception).
7924 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7925 /* Pre-v8 MIDR space.
7926 * Note that the MIDR isn't a simple constant register because
7927 * of the TI925 behaviour where writes to another register can
7928 * cause the MIDR value to change.
7930 * Unimplemented registers in the c15 0 0 0 space default to
7931 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7932 * and friends override accordingly.
7935 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7936 .access = PL1_R, .resetvalue = cpu->midr,
7937 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
7938 .readfn = midr_read,
7939 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7940 .type = ARM_CP_OVERRIDE },
7941 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7943 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7944 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7946 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7947 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7949 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7950 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7952 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7953 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7955 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
7956 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7958 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
7959 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
7960 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
7961 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
7962 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7963 .readfn = midr_read },
7964 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
7965 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7966 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7967 .access = PL1_R, .resetvalue = cpu->midr },
7968 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7969 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
7970 .access = PL1_R, .resetvalue = cpu->midr },
7971 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
7972 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
7974 .accessfn = access_aa64_tid1,
7975 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
7977 ARMCPRegInfo id_cp_reginfo[] = {
7978 /* These are common to v8 and pre-v8 */
7980 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
7981 .access = PL1_R, .accessfn = ctr_el0_access,
7982 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7983 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
7984 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
7985 .access = PL0_R, .accessfn = ctr_el0_access,
7986 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7987 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
7989 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
7991 .accessfn = access_aa32_tid1,
7992 .type = ARM_CP_CONST, .resetvalue = 0 },
7994 /* TLBTR is specific to VMSA */
7995 ARMCPRegInfo id_tlbtr_reginfo = {
7997 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
7999 .accessfn = access_aa32_tid1,
8000 .type = ARM_CP_CONST, .resetvalue = 0,
8002 /* MPUIR is specific to PMSA V6+ */
8003 ARMCPRegInfo id_mpuir_reginfo = {
8005 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8006 .access = PL1_R, .type = ARM_CP_CONST,
8007 .resetvalue = cpu->pmsav7_dregion << 8
8009 static const ARMCPRegInfo crn0_wi_reginfo = {
8010 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8011 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8012 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8014 #ifdef CONFIG_USER_ONLY
8015 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8016 { .name = "MIDR_EL1",
8017 .exported_bits = 0x00000000ffffffff },
8018 { .name = "REVIDR_EL1" },
8020 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8022 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8023 arm_feature(env, ARM_FEATURE_STRONGARM)) {
8025 /* Register the blanket "writes ignored" value first to cover the
8026 * whole space. Then update the specific ID registers to allow write
8027 * access, so that they ignore writes rather than causing them to
8030 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8031 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8032 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8034 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8035 id_cp_reginfo[i].access = PL1_RW;
8037 id_mpuir_reginfo.access = PL1_RW;
8038 id_tlbtr_reginfo.access = PL1_RW;
8040 if (arm_feature(env, ARM_FEATURE_V8)) {
8041 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8043 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8045 define_arm_cp_regs(cpu, id_cp_reginfo);
8046 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8047 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8048 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8049 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8053 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8054 ARMCPRegInfo mpidr_cp_reginfo[] = {
8055 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8056 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8057 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8059 #ifdef CONFIG_USER_ONLY
8060 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8061 { .name = "MPIDR_EL1",
8062 .fixed_bits = 0x0000000080000000 },
8064 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8066 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8069 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8070 ARMCPRegInfo auxcr_reginfo[] = {
8071 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8072 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8073 .access = PL1_RW, .accessfn = access_tacr,
8074 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8075 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8076 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8077 .access = PL2_RW, .type = ARM_CP_CONST,
8079 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8080 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8081 .access = PL3_RW, .type = ARM_CP_CONST,
8084 define_arm_cp_regs(cpu, auxcr_reginfo);
8085 if (cpu_isar_feature(aa32_ac2, cpu)) {
8086 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8090 if (arm_feature(env, ARM_FEATURE_CBAR)) {
8092 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8093 * There are two flavours:
8094 * (1) older 32-bit only cores have a simple 32-bit CBAR
8095 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8096 * 32-bit register visible to AArch32 at a different encoding
8097 * to the "flavour 1" register and with the bits rearranged to
8098 * be able to squash a 64-bit address into the 32-bit view.
8099 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8100 * in future if we support AArch32-only configs of some of the
8101 * AArch64 cores we might need to add a specific feature flag
8102 * to indicate cores with "flavour 2" CBAR.
8104 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8105 /* 32 bit view is [31:18] 0...0 [43:32]. */
8106 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8107 | extract64(cpu->reset_cbar, 32, 12);
8108 ARMCPRegInfo cbar_reginfo[] = {
8110 .type = ARM_CP_CONST,
8111 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8112 .access = PL1_R, .resetvalue = cbar32 },
8113 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8114 .type = ARM_CP_CONST,
8115 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8116 .access = PL1_R, .resetvalue = cpu->reset_cbar },
8118 /* We don't implement a r/w 64 bit CBAR currently */
8119 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8120 define_arm_cp_regs(cpu, cbar_reginfo);
8122 ARMCPRegInfo cbar = {
8124 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8125 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8126 .fieldoffset = offsetof(CPUARMState,
8127 cp15.c15_config_base_address)
8129 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8130 cbar.access = PL1_R;
8131 cbar.fieldoffset = 0;
8132 cbar.type = ARM_CP_CONST;
8134 define_one_arm_cp_reg(cpu, &cbar);
8138 if (arm_feature(env, ARM_FEATURE_VBAR)) {
8139 static const ARMCPRegInfo vbar_cp_reginfo[] = {
8140 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8141 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8142 .access = PL1_RW, .writefn = vbar_write,
8143 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8144 offsetof(CPUARMState, cp15.vbar_ns) },
8147 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8150 /* Generic registers whose values depend on the implementation */
8152 ARMCPRegInfo sctlr = {
8153 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8154 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8155 .access = PL1_RW, .accessfn = access_tvm_trvm,
8156 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8157 offsetof(CPUARMState, cp15.sctlr_ns) },
8158 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8159 .raw_writefn = raw_write,
8161 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8162 /* Normally we would always end the TB on an SCTLR write, but Linux
8163 * arch/arm/mach-pxa/sleep.S expects two instructions following
8164 * an MMU enable to execute from cache. Imitate this behaviour.
8166 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8168 define_one_arm_cp_reg(cpu, &sctlr);
8171 if (cpu_isar_feature(aa64_lor, cpu)) {
8172 define_arm_cp_regs(cpu, lor_reginfo);
8174 if (cpu_isar_feature(aa64_pan, cpu)) {
8175 define_one_arm_cp_reg(cpu, &pan_reginfo);
8177 #ifndef CONFIG_USER_ONLY
8178 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8179 define_arm_cp_regs(cpu, ats1e1_reginfo);
8181 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8182 define_arm_cp_regs(cpu, ats1cp_reginfo);
8185 if (cpu_isar_feature(aa64_uao, cpu)) {
8186 define_one_arm_cp_reg(cpu, &uao_reginfo);
8189 if (cpu_isar_feature(aa64_dit, cpu)) {
8190 define_one_arm_cp_reg(cpu, &dit_reginfo);
8192 if (cpu_isar_feature(aa64_ssbs, cpu)) {
8193 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8195 if (cpu_isar_feature(any_ras, cpu)) {
8196 define_arm_cp_regs(cpu, minimal_ras_reginfo);
8199 if (cpu_isar_feature(aa64_vh, cpu) ||
8200 cpu_isar_feature(aa64_debugv8p2, cpu)) {
8201 define_one_arm_cp_reg(cpu, &contextidr_el2);
8203 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8204 define_arm_cp_regs(cpu, vhe_reginfo);
8207 if (cpu_isar_feature(aa64_sve, cpu)) {
8208 define_arm_cp_regs(cpu, zcr_reginfo);
8211 if (cpu_isar_feature(aa64_hcx, cpu)) {
8212 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8215 #ifdef TARGET_AARCH64
8216 if (cpu_isar_feature(aa64_sme, cpu)) {
8217 define_arm_cp_regs(cpu, sme_reginfo);
8219 if (cpu_isar_feature(aa64_pauth, cpu)) {
8220 define_arm_cp_regs(cpu, pauth_reginfo);
8222 if (cpu_isar_feature(aa64_rndr, cpu)) {
8223 define_arm_cp_regs(cpu, rndr_reginfo);
8225 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8226 define_arm_cp_regs(cpu, tlbirange_reginfo);
8228 if (cpu_isar_feature(aa64_tlbios, cpu)) {
8229 define_arm_cp_regs(cpu, tlbios_reginfo);
8231 #ifndef CONFIG_USER_ONLY
8232 /* Data Cache clean instructions up to PoP */
8233 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8234 define_one_arm_cp_reg(cpu, dcpop_reg);
8236 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8237 define_one_arm_cp_reg(cpu, dcpodp_reg);
8240 #endif /*CONFIG_USER_ONLY*/
8243 * If full MTE is enabled, add all of the system registers.
8244 * If only "instructions available at EL0" are enabled,
8245 * then define only a RAZ/WI version of PSTATE.TCO.
8247 if (cpu_isar_feature(aa64_mte, cpu)) {
8248 define_arm_cp_regs(cpu, mte_reginfo);
8249 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8250 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8251 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8252 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8255 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8256 define_arm_cp_regs(cpu, scxtnum_reginfo);
8260 if (cpu_isar_feature(any_predinv, cpu)) {
8261 define_arm_cp_regs(cpu, predinv_reginfo);
8264 if (cpu_isar_feature(any_ccidx, cpu)) {
8265 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8268 #ifndef CONFIG_USER_ONLY
8270 * Register redirections and aliases must be done last,
8271 * after the registers from the other extensions have been defined.
8273 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8274 define_arm_vh_e2h_redirects_aliases(cpu);
8279 /* Sort alphabetically by type name, except for "any". */
8280 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8282 ObjectClass *class_a = (ObjectClass *)a;
8283 ObjectClass *class_b = (ObjectClass *)b;
8284 const char *name_a, *name_b;
8286 name_a = object_class_get_name(class_a);
8287 name_b = object_class_get_name(class_b);
8288 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8290 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8293 return strcmp(name_a, name_b);
8297 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8299 ObjectClass *oc = data;
8300 CPUClass *cc = CPU_CLASS(oc);
8301 const char *typename;
8304 typename = object_class_get_name(oc);
8305 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8306 if (cc->deprecation_note) {
8307 qemu_printf(" %s (deprecated)\n", name);
8309 qemu_printf(" %s\n", name);
8314 void arm_cpu_list(void)
8318 list = object_class_get_list(TYPE_ARM_CPU, false);
8319 list = g_slist_sort(list, arm_cpu_list_compare);
8320 qemu_printf("Available CPUs:\n");
8321 g_slist_foreach(list, arm_cpu_list_entry, NULL);
8325 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8327 ObjectClass *oc = data;
8328 CpuDefinitionInfoList **cpu_list = user_data;
8329 CpuDefinitionInfo *info;
8330 const char *typename;
8332 typename = object_class_get_name(oc);
8333 info = g_malloc0(sizeof(*info));
8334 info->name = g_strndup(typename,
8335 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8336 info->q_typename = g_strdup(typename);
8338 QAPI_LIST_PREPEND(*cpu_list, info);
8341 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8343 CpuDefinitionInfoList *cpu_list = NULL;
8346 list = object_class_get_list(TYPE_ARM_CPU, false);
8347 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8354 * Private utility function for define_one_arm_cp_reg_with_opaque():
8355 * add a single reginfo struct to the hash table.
8357 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8358 void *opaque, CPState state,
8359 CPSecureState secstate,
8360 int crm, int opc1, int opc2,
8363 CPUARMState *env = &cpu->env;
8366 bool is64 = r->type & ARM_CP_64BIT;
8367 bool ns = secstate & ARM_CP_SECSTATE_NS;
8373 case ARM_CP_STATE_AA32:
8374 /* We assume it is a cp15 register if the .cp field is left unset. */
8375 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8378 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8380 case ARM_CP_STATE_AA64:
8382 * To allow abbreviation of ARMCPRegInfo definitions, we treat
8383 * cp == 0 as equivalent to the value for "standard guest-visible
8384 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
8385 * in their AArch64 view (the .cp value may be non-zero for the
8386 * benefit of the AArch32 view).
8388 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8389 cp = CP_REG_ARM64_SYSREG_CP;
8391 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8394 g_assert_not_reached();
8397 /* Overriding of an existing definition must be explicitly requested. */
8398 if (!(r->type & ARM_CP_OVERRIDE)) {
8399 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8401 assert(oldreg->type & ARM_CP_OVERRIDE);
8406 * Eliminate registers that are not present because the EL is missing.
8407 * Doing this here makes it easier to put all registers for a given
8408 * feature into the same ARMCPRegInfo array and define them all at once.
8411 if (arm_feature(env, ARM_FEATURE_EL3)) {
8413 * An EL2 register without EL2 but with EL3 is (usually) RES0.
8414 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8416 int min_el = ctz32(r->access) / 2;
8417 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8418 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8421 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8424 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8426 if ((r->access & max_el) == 0) {
8431 /* Combine cpreg and name into one allocation. */
8432 name_len = strlen(name) + 1;
8433 r2 = g_malloc(sizeof(*r2) + name_len);
8435 r2->name = memcpy(r2 + 1, name, name_len);
8438 * Update fields to match the instantiation, overwiting wildcards
8439 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8446 r2->secure = secstate;
8448 r2->opaque = opaque;
8452 /* This should not have been a very special register to begin. */
8453 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8454 assert(old_special == 0 || old_special == ARM_CP_NOP);
8456 * Set the special function to CONST, retaining the other flags.
8457 * This is important for e.g. ARM_CP_SVE so that we still
8458 * take the SVE trap if CPTR_EL3.EZ == 0.
8460 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8462 * Usually, these registers become RES0, but there are a few
8463 * special cases like VPIDR_EL2 which have a constant non-zero
8464 * value with writes ignored.
8466 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8470 * ARM_CP_CONST has precedence, so removing the callbacks and
8471 * offsets are not strictly necessary, but it is potentially
8472 * less confusing to debug later.
8476 r2->raw_readfn = NULL;
8477 r2->raw_writefn = NULL;
8479 r2->fieldoffset = 0;
8480 r2->bank_fieldoffsets[0] = 0;
8481 r2->bank_fieldoffsets[1] = 0;
8483 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8487 * Register is banked (using both entries in array).
8488 * Overwriting fieldoffset as the array is only used to define
8489 * banked registers but later only fieldoffset is used.
8491 r2->fieldoffset = r->bank_fieldoffsets[ns];
8493 if (state == ARM_CP_STATE_AA32) {
8496 * If the register is banked then we don't need to migrate or
8497 * reset the 32-bit instance in certain cases:
8499 * 1) If the register has both 32-bit and 64-bit instances
8500 * then we can count on the 64-bit instance taking care
8501 * of the non-secure bank.
8502 * 2) If ARMv8 is enabled then we can count on a 64-bit
8503 * version taking care of the secure bank. This requires
8504 * that separate 32 and 64-bit definitions are provided.
8506 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8507 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8508 r2->type |= ARM_CP_ALIAS;
8510 } else if ((secstate != r->secure) && !ns) {
8512 * The register is not banked so we only want to allow
8513 * migration of the non-secure instance.
8515 r2->type |= ARM_CP_ALIAS;
8518 if (HOST_BIG_ENDIAN &&
8519 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8520 r2->fieldoffset += sizeof(uint32_t);
8526 * By convention, for wildcarded registers only the first
8527 * entry is used for migration; the others are marked as
8528 * ALIAS so we don't try to transfer the register
8529 * multiple times. Special registers (ie NOP/WFI) are
8530 * never migratable and not even raw-accessible.
8532 if (r2->type & ARM_CP_SPECIAL_MASK) {
8533 r2->type |= ARM_CP_NO_RAW;
8535 if (((r->crm == CP_ANY) && crm != 0) ||
8536 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8537 ((r->opc2 == CP_ANY) && opc2 != 0)) {
8538 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8542 * Check that raw accesses are either forbidden or handled. Note that
8543 * we can't assert this earlier because the setup of fieldoffset for
8544 * banked registers has to be done first.
8546 if (!(r2->type & ARM_CP_NO_RAW)) {
8547 assert(!raw_accessors_invalid(r2));
8550 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
8554 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8555 const ARMCPRegInfo *r, void *opaque)
8557 /* Define implementations of coprocessor registers.
8558 * We store these in a hashtable because typically
8559 * there are less than 150 registers in a space which
8560 * is 16*16*16*8*8 = 262144 in size.
8561 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8562 * If a register is defined twice then the second definition is
8563 * used, so this can be used to define some generic registers and
8564 * then override them with implementation specific variations.
8565 * At least one of the original and the second definition should
8566 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8567 * against accidental use.
8569 * The state field defines whether the register is to be
8570 * visible in the AArch32 or AArch64 execution state. If the
8571 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8572 * reginfo structure for the AArch32 view, which sees the lower
8573 * 32 bits of the 64 bit register.
8575 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8576 * be wildcarded. AArch64 registers are always considered to be 64
8577 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8578 * the register, if any.
8580 int crm, opc1, opc2;
8581 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8582 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8583 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8584 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8585 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8586 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8589 /* 64 bit registers have only CRm and Opc1 fields */
8590 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8591 /* op0 only exists in the AArch64 encodings */
8592 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8593 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8594 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8596 * This API is only for Arm's system coprocessors (14 and 15) or
8597 * (M-profile or v7A-and-earlier only) for implementation defined
8598 * coprocessors in the range 0..7. Our decode assumes this, since
8599 * 8..13 can be used for other insns including VFP and Neon. See
8600 * valid_cp() in translate.c. Assert here that we haven't tried
8601 * to use an invalid coprocessor number.
8604 case ARM_CP_STATE_BOTH:
8605 /* 0 has a special meaning, but otherwise the same rules as AA32. */
8610 case ARM_CP_STATE_AA32:
8611 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8612 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8613 assert(r->cp >= 14 && r->cp <= 15);
8615 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8618 case ARM_CP_STATE_AA64:
8619 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8622 g_assert_not_reached();
8624 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8625 * encodes a minimum access level for the register. We roll this
8626 * runtime check into our general permission check code, so check
8627 * here that the reginfo's specified permissions are strict enough
8628 * to encompass the generic architectural permission check.
8630 if (r->state != ARM_CP_STATE_AA32) {
8631 CPAccessRights mask;
8634 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8635 mask = PL0U_R | PL1_RW;
8655 /* min_EL EL1, secure mode only (we don't check the latter) */
8659 /* broken reginfo with out-of-range opc1 */
8660 g_assert_not_reached();
8662 /* assert our permissions are not too lax (stricter is fine) */
8663 assert((r->access & ~mask) == 0);
8666 /* Check that the register definition has enough info to handle
8667 * reads and writes if they are permitted.
8669 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
8670 if (r->access & PL3_R) {
8671 assert((r->fieldoffset ||
8672 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8675 if (r->access & PL3_W) {
8676 assert((r->fieldoffset ||
8677 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8682 for (crm = crmmin; crm <= crmmax; crm++) {
8683 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8684 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
8685 for (state = ARM_CP_STATE_AA32;
8686 state <= ARM_CP_STATE_AA64; state++) {
8687 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8690 if (state == ARM_CP_STATE_AA32) {
8691 /* Under AArch32 CP registers can be common
8692 * (same for secure and non-secure world) or banked.
8696 switch (r->secure) {
8697 case ARM_CP_SECSTATE_S:
8698 case ARM_CP_SECSTATE_NS:
8699 add_cpreg_to_hashtable(cpu, r, opaque, state,
8700 r->secure, crm, opc1, opc2,
8703 case ARM_CP_SECSTATE_BOTH:
8704 name = g_strdup_printf("%s_S", r->name);
8705 add_cpreg_to_hashtable(cpu, r, opaque, state,
8707 crm, opc1, opc2, name);
8709 add_cpreg_to_hashtable(cpu, r, opaque, state,
8711 crm, opc1, opc2, r->name);
8714 g_assert_not_reached();
8717 /* AArch64 registers get mapped to non-secure instance
8719 add_cpreg_to_hashtable(cpu, r, opaque, state,
8721 crm, opc1, opc2, r->name);
8729 /* Define a whole list of registers */
8730 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8731 void *opaque, size_t len)
8734 for (i = 0; i < len; ++i) {
8735 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
8740 * Modify ARMCPRegInfo for access from userspace.
8742 * This is a data driven modification directed by
8743 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8744 * user-space cannot alter any values and dynamic values pertaining to
8745 * execution state are hidden from user space view anyway.
8747 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8748 const ARMCPRegUserSpaceInfo *mods,
8751 for (size_t mi = 0; mi < mods_len; ++mi) {
8752 const ARMCPRegUserSpaceInfo *m = mods + mi;
8753 GPatternSpec *pat = NULL;
8756 pat = g_pattern_spec_new(m->name);
8758 for (size_t ri = 0; ri < regs_len; ++ri) {
8759 ARMCPRegInfo *r = regs + ri;
8761 if (pat && g_pattern_match_string(pat, r->name)) {
8762 r->type = ARM_CP_CONST;
8766 } else if (strcmp(r->name, m->name) == 0) {
8767 r->type = ARM_CP_CONST;
8769 r->resetvalue &= m->exported_bits;
8770 r->resetvalue |= m->fixed_bits;
8775 g_pattern_spec_free(pat);
8780 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8782 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
8785 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8788 /* Helper coprocessor write function for write-ignore registers */
8791 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8793 /* Helper coprocessor write function for read-as-zero registers */
8797 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8799 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8802 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
8804 /* Return true if it is not valid for us to switch to
8805 * this CPU mode (ie all the UNPREDICTABLE cases in
8806 * the ARM ARM CPSRWriteByInstr pseudocode).
8809 /* Changes to or from Hyp via MSR and CPS are illegal. */
8810 if (write_type == CPSRWriteByInstr &&
8811 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8812 mode == ARM_CPU_MODE_HYP)) {
8817 case ARM_CPU_MODE_USR:
8819 case ARM_CPU_MODE_SYS:
8820 case ARM_CPU_MODE_SVC:
8821 case ARM_CPU_MODE_ABT:
8822 case ARM_CPU_MODE_UND:
8823 case ARM_CPU_MODE_IRQ:
8824 case ARM_CPU_MODE_FIQ:
8825 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8826 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8828 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8829 * and CPS are treated as illegal mode changes.
8831 if (write_type == CPSRWriteByInstr &&
8832 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
8833 (arm_hcr_el2_eff(env) & HCR_TGE)) {
8837 case ARM_CPU_MODE_HYP:
8838 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
8839 case ARM_CPU_MODE_MON:
8840 return arm_current_el(env) < 3;
8846 uint32_t cpsr_read(CPUARMState *env)
8849 ZF = (env->ZF == 0);
8850 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
8851 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8852 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8853 | ((env->condexec_bits & 0xfc) << 8)
8854 | (env->GE << 16) | (env->daif & CPSR_AIF);
8857 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8858 CPSRWriteType write_type)
8860 uint32_t changed_daif;
8861 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
8862 (mask & (CPSR_M | CPSR_E | CPSR_IL));
8864 if (mask & CPSR_NZCV) {
8865 env->ZF = (~val) & CPSR_Z;
8867 env->CF = (val >> 29) & 1;
8868 env->VF = (val << 3) & 0x80000000;
8871 env->QF = ((val & CPSR_Q) != 0);
8873 env->thumb = ((val & CPSR_T) != 0);
8874 if (mask & CPSR_IT_0_1) {
8875 env->condexec_bits &= ~3;
8876 env->condexec_bits |= (val >> 25) & 3;
8878 if (mask & CPSR_IT_2_7) {
8879 env->condexec_bits &= 3;
8880 env->condexec_bits |= (val >> 8) & 0xfc;
8882 if (mask & CPSR_GE) {
8883 env->GE = (val >> 16) & 0xf;
8886 /* In a V7 implementation that includes the security extensions but does
8887 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8888 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8889 * bits respectively.
8891 * In a V8 implementation, it is permitted for privileged software to
8892 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8894 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
8895 arm_feature(env, ARM_FEATURE_EL3) &&
8896 !arm_feature(env, ARM_FEATURE_EL2) &&
8897 !arm_is_secure(env)) {
8899 changed_daif = (env->daif ^ val) & mask;
8901 if (changed_daif & CPSR_A) {
8902 /* Check to see if we are allowed to change the masking of async
8903 * abort exceptions from a non-secure state.
8905 if (!(env->cp15.scr_el3 & SCR_AW)) {
8906 qemu_log_mask(LOG_GUEST_ERROR,
8907 "Ignoring attempt to switch CPSR_A flag from "
8908 "non-secure world with SCR.AW bit clear\n");
8913 if (changed_daif & CPSR_F) {
8914 /* Check to see if we are allowed to change the masking of FIQ
8915 * exceptions from a non-secure state.
8917 if (!(env->cp15.scr_el3 & SCR_FW)) {
8918 qemu_log_mask(LOG_GUEST_ERROR,
8919 "Ignoring attempt to switch CPSR_F flag from "
8920 "non-secure world with SCR.FW bit clear\n");
8924 /* Check whether non-maskable FIQ (NMFI) support is enabled.
8925 * If this bit is set software is not allowed to mask
8926 * FIQs, but is allowed to set CPSR_F to 0.
8928 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8930 qemu_log_mask(LOG_GUEST_ERROR,
8931 "Ignoring attempt to enable CPSR_F flag "
8932 "(non-maskable FIQ [NMFI] support enabled)\n");
8938 env->daif &= ~(CPSR_AIF & mask);
8939 env->daif |= val & CPSR_AIF & mask;
8941 if (write_type != CPSRWriteRaw &&
8942 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8943 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8944 /* Note that we can only get here in USR mode if this is a
8945 * gdb stub write; for this case we follow the architectural
8946 * behaviour for guest writes in USR mode of ignoring an attempt
8947 * to switch mode. (Those are caught by translate.c for writes
8948 * triggered by guest instructions.)
8951 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
8952 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8953 * v7, and has defined behaviour in v8:
8954 * + leave CPSR.M untouched
8955 * + allow changes to the other CPSR fields
8957 * For user changes via the GDB stub, we don't set PSTATE.IL,
8958 * as this would be unnecessarily harsh for a user error.
8961 if (write_type != CPSRWriteByGDBStub &&
8962 arm_feature(env, ARM_FEATURE_V8)) {
8966 qemu_log_mask(LOG_GUEST_ERROR,
8967 "Illegal AArch32 mode switch attempt from %s to %s\n",
8968 aarch32_mode_name(env->uncached_cpsr),
8969 aarch32_mode_name(val));
8971 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8972 write_type == CPSRWriteExceptionReturn ?
8973 "Exception return from AArch32" :
8974 "AArch32 mode switch from",
8975 aarch32_mode_name(env->uncached_cpsr),
8976 aarch32_mode_name(val), env->regs[15]);
8977 switch_mode(env, val & CPSR_M);
8980 mask &= ~CACHED_CPSR_BITS;
8981 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
8982 if (rebuild_hflags) {
8983 arm_rebuild_hflags(env);
8987 /* Sign/zero extend */
8988 uint32_t HELPER(sxtb16)(uint32_t x)
8991 res = (uint16_t)(int8_t)x;
8992 res |= (uint32_t)(int8_t)(x >> 16) << 16;
8996 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
8999 * Take a division-by-zero exception if necessary; otherwise return
9000 * to get the usual non-trapping division behaviour (result of 0)
9002 if (arm_feature(env, ARM_FEATURE_M)
9003 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9004 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9008 uint32_t HELPER(uxtb16)(uint32_t x)
9011 res = (uint16_t)(uint8_t)x;
9012 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9016 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9019 handle_possible_div0_trap(env, GETPC());
9022 if (num == INT_MIN && den == -1) {
9028 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9031 handle_possible_div0_trap(env, GETPC());
9037 uint32_t HELPER(rbit)(uint32_t x)
9042 #ifdef CONFIG_USER_ONLY
9044 static void switch_mode(CPUARMState *env, int mode)
9046 ARMCPU *cpu = env_archcpu(env);
9048 if (mode != ARM_CPU_MODE_USR) {
9049 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9053 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9054 uint32_t cur_el, bool secure)
9059 void aarch64_sync_64_to_32(CPUARMState *env)
9061 g_assert_not_reached();
9066 static void switch_mode(CPUARMState *env, int mode)
9071 old_mode = env->uncached_cpsr & CPSR_M;
9072 if (mode == old_mode)
9075 if (old_mode == ARM_CPU_MODE_FIQ) {
9076 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9077 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9078 } else if (mode == ARM_CPU_MODE_FIQ) {
9079 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9080 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9083 i = bank_number(old_mode);
9084 env->banked_r13[i] = env->regs[13];
9085 env->banked_spsr[i] = env->spsr;
9087 i = bank_number(mode);
9088 env->regs[13] = env->banked_r13[i];
9089 env->spsr = env->banked_spsr[i];
9091 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9092 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9095 /* Physical Interrupt Target EL Lookup Table
9097 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9099 * The below multi-dimensional table is used for looking up the target
9100 * exception level given numerous condition criteria. Specifically, the
9101 * target EL is based on SCR and HCR routing controls as well as the
9102 * currently executing EL and secure state.
9105 * target_el_table[2][2][2][2][2][4]
9106 * | | | | | +--- Current EL
9107 * | | | | +------ Non-secure(0)/Secure(1)
9108 * | | | +--------- HCR mask override
9109 * | | +------------ SCR exec state control
9110 * | +--------------- SCR mask override
9111 * +------------------ 32-bit(0)/64-bit(1) EL3
9113 * The table values are as such:
9117 * The ARM ARM target EL table includes entries indicating that an "exception
9118 * is not taken". The two cases where this is applicable are:
9119 * 1) An exception is taken from EL3 but the SCR does not have the exception
9121 * 2) An exception is taken from EL2 but the HCR does not have the exception
9123 * In these two cases, the below table contain a target of EL1. This value is
9124 * returned as it is expected that the consumer of the table data will check
9125 * for "target EL >= current EL" to ensure the exception is not taken.
9129 * BIT IRQ IMO Non-secure Secure
9130 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9132 static const int8_t target_el_table[2][2][2][2][2][4] = {
9133 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9134 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9135 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9136 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9137 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9138 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9139 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9140 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9141 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
9142 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9143 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9144 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
9145 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
9146 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
9147 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
9148 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
9152 * Determine the target EL for physical exceptions
9154 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9155 uint32_t cur_el, bool secure)
9157 CPUARMState *env = cs->env_ptr;
9162 /* Is the highest EL AArch64? */
9163 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9166 if (arm_feature(env, ARM_FEATURE_EL3)) {
9167 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9169 /* Either EL2 is the highest EL (and so the EL2 register width
9170 * is given by is64); or there is no EL2 or EL3, in which case
9171 * the value of 'rw' does not affect the table lookup anyway.
9176 hcr_el2 = arm_hcr_el2_eff(env);
9179 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9180 hcr = hcr_el2 & HCR_IMO;
9183 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9184 hcr = hcr_el2 & HCR_FMO;
9187 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9188 hcr = hcr_el2 & HCR_AMO;
9193 * For these purposes, TGE and AMO/IMO/FMO both force the
9194 * interrupt to EL2. Fold TGE into the bit extracted above.
9196 hcr |= (hcr_el2 & HCR_TGE) != 0;
9198 /* Perform a table-lookup for the target EL given the current state */
9199 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9201 assert(target_el > 0);
9206 void arm_log_exception(CPUState *cs)
9208 int idx = cs->exception_index;
9210 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9211 const char *exc = NULL;
9212 static const char * const excnames[] = {
9213 [EXCP_UDEF] = "Undefined Instruction",
9215 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9216 [EXCP_DATA_ABORT] = "Data Abort",
9219 [EXCP_BKPT] = "Breakpoint",
9220 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9221 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9222 [EXCP_HVC] = "Hypervisor Call",
9223 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9224 [EXCP_SMC] = "Secure Monitor Call",
9225 [EXCP_VIRQ] = "Virtual IRQ",
9226 [EXCP_VFIQ] = "Virtual FIQ",
9227 [EXCP_SEMIHOST] = "Semihosting call",
9228 [EXCP_NOCP] = "v7M NOCP UsageFault",
9229 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9230 [EXCP_STKOF] = "v8M STKOF UsageFault",
9231 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9232 [EXCP_LSERR] = "v8M LSERR UsageFault",
9233 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9234 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9235 [EXCP_VSERR] = "Virtual SERR",
9238 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9239 exc = excnames[idx];
9244 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9245 idx, exc, cs->cpu_index);
9250 * Function used to synchronize QEMU's AArch64 register set with AArch32
9251 * register set. This is necessary when switching between AArch32 and AArch64
9254 void aarch64_sync_32_to_64(CPUARMState *env)
9257 uint32_t mode = env->uncached_cpsr & CPSR_M;
9259 /* We can blanket copy R[0:7] to X[0:7] */
9260 for (i = 0; i < 8; i++) {
9261 env->xregs[i] = env->regs[i];
9265 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9266 * Otherwise, they come from the banked user regs.
9268 if (mode == ARM_CPU_MODE_FIQ) {
9269 for (i = 8; i < 13; i++) {
9270 env->xregs[i] = env->usr_regs[i - 8];
9273 for (i = 8; i < 13; i++) {
9274 env->xregs[i] = env->regs[i];
9279 * Registers x13-x23 are the various mode SP and FP registers. Registers
9280 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9281 * from the mode banked register.
9283 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9284 env->xregs[13] = env->regs[13];
9285 env->xregs[14] = env->regs[14];
9287 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9288 /* HYP is an exception in that it is copied from r14 */
9289 if (mode == ARM_CPU_MODE_HYP) {
9290 env->xregs[14] = env->regs[14];
9292 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9296 if (mode == ARM_CPU_MODE_HYP) {
9297 env->xregs[15] = env->regs[13];
9299 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9302 if (mode == ARM_CPU_MODE_IRQ) {
9303 env->xregs[16] = env->regs[14];
9304 env->xregs[17] = env->regs[13];
9306 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9307 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9310 if (mode == ARM_CPU_MODE_SVC) {
9311 env->xregs[18] = env->regs[14];
9312 env->xregs[19] = env->regs[13];
9314 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9315 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9318 if (mode == ARM_CPU_MODE_ABT) {
9319 env->xregs[20] = env->regs[14];
9320 env->xregs[21] = env->regs[13];
9322 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9323 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9326 if (mode == ARM_CPU_MODE_UND) {
9327 env->xregs[22] = env->regs[14];
9328 env->xregs[23] = env->regs[13];
9330 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9331 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9335 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9336 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9337 * FIQ bank for r8-r14.
9339 if (mode == ARM_CPU_MODE_FIQ) {
9340 for (i = 24; i < 31; i++) {
9341 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9344 for (i = 24; i < 29; i++) {
9345 env->xregs[i] = env->fiq_regs[i - 24];
9347 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9348 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9351 env->pc = env->regs[15];
9355 * Function used to synchronize QEMU's AArch32 register set with AArch64
9356 * register set. This is necessary when switching between AArch32 and AArch64
9359 void aarch64_sync_64_to_32(CPUARMState *env)
9362 uint32_t mode = env->uncached_cpsr & CPSR_M;
9364 /* We can blanket copy X[0:7] to R[0:7] */
9365 for (i = 0; i < 8; i++) {
9366 env->regs[i] = env->xregs[i];
9370 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9371 * Otherwise, we copy x8-x12 into the banked user regs.
9373 if (mode == ARM_CPU_MODE_FIQ) {
9374 for (i = 8; i < 13; i++) {
9375 env->usr_regs[i - 8] = env->xregs[i];
9378 for (i = 8; i < 13; i++) {
9379 env->regs[i] = env->xregs[i];
9384 * Registers r13 & r14 depend on the current mode.
9385 * If we are in a given mode, we copy the corresponding x registers to r13
9386 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9389 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9390 env->regs[13] = env->xregs[13];
9391 env->regs[14] = env->xregs[14];
9393 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9396 * HYP is an exception in that it does not have its own banked r14 but
9397 * shares the USR r14
9399 if (mode == ARM_CPU_MODE_HYP) {
9400 env->regs[14] = env->xregs[14];
9402 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9406 if (mode == ARM_CPU_MODE_HYP) {
9407 env->regs[13] = env->xregs[15];
9409 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9412 if (mode == ARM_CPU_MODE_IRQ) {
9413 env->regs[14] = env->xregs[16];
9414 env->regs[13] = env->xregs[17];
9416 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9417 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9420 if (mode == ARM_CPU_MODE_SVC) {
9421 env->regs[14] = env->xregs[18];
9422 env->regs[13] = env->xregs[19];
9424 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9425 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9428 if (mode == ARM_CPU_MODE_ABT) {
9429 env->regs[14] = env->xregs[20];
9430 env->regs[13] = env->xregs[21];
9432 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9433 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9436 if (mode == ARM_CPU_MODE_UND) {
9437 env->regs[14] = env->xregs[22];
9438 env->regs[13] = env->xregs[23];
9440 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9441 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9444 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9445 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9446 * FIQ bank for r8-r14.
9448 if (mode == ARM_CPU_MODE_FIQ) {
9449 for (i = 24; i < 31; i++) {
9450 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9453 for (i = 24; i < 29; i++) {
9454 env->fiq_regs[i - 24] = env->xregs[i];
9456 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9457 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9460 env->regs[15] = env->pc;
9463 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9464 uint32_t mask, uint32_t offset,
9469 /* Change the CPU state so as to actually take the exception. */
9470 switch_mode(env, new_mode);
9473 * For exceptions taken to AArch32 we must clear the SS bit in both
9474 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9476 env->pstate &= ~PSTATE_SS;
9477 env->spsr = cpsr_read(env);
9478 /* Clear IT bits. */
9479 env->condexec_bits = 0;
9480 /* Switch to the new mode, and to the correct instruction set. */
9481 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9483 /* This must be after mode switching. */
9484 new_el = arm_current_el(env);
9486 /* Set new mode endianness */
9487 env->uncached_cpsr &= ~CPSR_E;
9488 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9489 env->uncached_cpsr |= CPSR_E;
9491 /* J and IL must always be cleared for exception entry */
9492 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9495 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9496 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9497 env->uncached_cpsr |= CPSR_SSBS;
9499 env->uncached_cpsr &= ~CPSR_SSBS;
9503 if (new_mode == ARM_CPU_MODE_HYP) {
9504 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9505 env->elr_el[2] = env->regs[15];
9507 /* CPSR.PAN is normally preserved preserved unless... */
9508 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9511 if (!arm_is_secure_below_el3(env)) {
9512 /* ... the target is EL3, from non-secure state. */
9513 env->uncached_cpsr &= ~CPSR_PAN;
9516 /* ... the target is EL3, from secure state ... */
9519 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9520 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9521 env->uncached_cpsr |= CPSR_PAN;
9527 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9528 * and we should just guard the thumb mode on V4
9530 if (arm_feature(env, ARM_FEATURE_V4T)) {
9532 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9534 env->regs[14] = env->regs[15] + offset;
9536 env->regs[15] = newpc;
9537 arm_rebuild_hflags(env);
9540 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9543 * Handle exception entry to Hyp mode; this is sufficiently
9544 * different to entry to other AArch32 modes that we handle it
9547 * The vector table entry used is always the 0x14 Hyp mode entry point,
9548 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9549 * The offset applied to the preferred return address is always zero
9550 * (see DDI0487C.a section G1.12.3).
9551 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9553 uint32_t addr, mask;
9554 ARMCPU *cpu = ARM_CPU(cs);
9555 CPUARMState *env = &cpu->env;
9557 switch (cs->exception_index) {
9565 /* Fall through to prefetch abort. */
9566 case EXCP_PREFETCH_ABORT:
9567 env->cp15.ifar_s = env->exception.vaddress;
9568 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9569 (uint32_t)env->exception.vaddress);
9572 case EXCP_DATA_ABORT:
9573 env->cp15.dfar_s = env->exception.vaddress;
9574 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9575 (uint32_t)env->exception.vaddress);
9591 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9594 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9595 if (!arm_feature(env, ARM_FEATURE_V8)) {
9597 * QEMU syndrome values are v8-style. v7 has the IL bit
9598 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9599 * If this is a v7 CPU, squash the IL bit in those cases.
9601 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9602 (cs->exception_index == EXCP_DATA_ABORT &&
9603 !(env->exception.syndrome & ARM_EL_ISV)) ||
9604 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9605 env->exception.syndrome &= ~ARM_EL_IL;
9608 env->cp15.esr_el[2] = env->exception.syndrome;
9611 if (arm_current_el(env) != 2 && addr < 0x14) {
9616 if (!(env->cp15.scr_el3 & SCR_EA)) {
9619 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9622 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9626 addr += env->cp15.hvbar;
9628 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9631 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9633 ARMCPU *cpu = ARM_CPU(cs);
9634 CPUARMState *env = &cpu->env;
9641 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9642 switch (syn_get_ec(env->exception.syndrome)) {
9644 case EC_BREAKPOINT_SAME_EL:
9648 case EC_WATCHPOINT_SAME_EL:
9654 case EC_VECTORCATCH:
9663 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9666 if (env->exception.target_el == 2) {
9667 arm_cpu_do_interrupt_aarch32_hyp(cs);
9671 switch (cs->exception_index) {
9673 new_mode = ARM_CPU_MODE_UND;
9682 new_mode = ARM_CPU_MODE_SVC;
9685 /* The PC already points to the next instruction. */
9689 /* Fall through to prefetch abort. */
9690 case EXCP_PREFETCH_ABORT:
9691 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9692 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9693 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9694 env->exception.fsr, (uint32_t)env->exception.vaddress);
9695 new_mode = ARM_CPU_MODE_ABT;
9697 mask = CPSR_A | CPSR_I;
9700 case EXCP_DATA_ABORT:
9701 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9702 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9703 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9705 (uint32_t)env->exception.vaddress);
9706 new_mode = ARM_CPU_MODE_ABT;
9708 mask = CPSR_A | CPSR_I;
9712 new_mode = ARM_CPU_MODE_IRQ;
9714 /* Disable IRQ and imprecise data aborts. */
9715 mask = CPSR_A | CPSR_I;
9717 if (env->cp15.scr_el3 & SCR_IRQ) {
9718 /* IRQ routed to monitor mode */
9719 new_mode = ARM_CPU_MODE_MON;
9724 new_mode = ARM_CPU_MODE_FIQ;
9726 /* Disable FIQ, IRQ and imprecise data aborts. */
9727 mask = CPSR_A | CPSR_I | CPSR_F;
9728 if (env->cp15.scr_el3 & SCR_FIQ) {
9729 /* FIQ routed to monitor mode */
9730 new_mode = ARM_CPU_MODE_MON;
9735 new_mode = ARM_CPU_MODE_IRQ;
9737 /* Disable IRQ and imprecise data aborts. */
9738 mask = CPSR_A | CPSR_I;
9742 new_mode = ARM_CPU_MODE_FIQ;
9744 /* Disable FIQ, IRQ and imprecise data aborts. */
9745 mask = CPSR_A | CPSR_I | CPSR_F;
9751 * Note that this is reported as a data abort, but the DFAR
9752 * has an UNKNOWN value. Construct the SError syndrome from
9753 * AET and ExT fields.
9755 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
9757 if (extended_addresses_enabled(env)) {
9758 env->exception.fsr = arm_fi_to_lfsc(&fi);
9760 env->exception.fsr = arm_fi_to_sfsc(&fi);
9762 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
9763 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9764 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
9765 env->exception.fsr);
9767 new_mode = ARM_CPU_MODE_ABT;
9769 mask = CPSR_A | CPSR_I;
9774 new_mode = ARM_CPU_MODE_MON;
9776 mask = CPSR_A | CPSR_I | CPSR_F;
9780 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9781 return; /* Never happens. Keep compiler happy. */
9784 if (new_mode == ARM_CPU_MODE_MON) {
9785 addr += env->cp15.mvbar;
9786 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9787 /* High vectors. When enabled, base address cannot be remapped. */
9790 /* ARM v7 architectures provide a vector base address register to remap
9791 * the interrupt vector table.
9792 * This register is only followed in non-monitor mode, and is banked.
9793 * Note: only bits 31:5 are valid.
9795 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
9798 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9799 env->cp15.scr_el3 &= ~SCR_NS;
9802 take_aarch32_exception(env, new_mode, mask, offset, addr);
9805 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9808 * Return the register number of the AArch64 view of the AArch32
9809 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9810 * be that of the AArch32 mode the exception came from.
9812 int mode = env->uncached_cpsr & CPSR_M;
9814 switch (aarch32_reg) {
9818 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9821 case ARM_CPU_MODE_USR:
9822 case ARM_CPU_MODE_SYS:
9824 case ARM_CPU_MODE_HYP:
9826 case ARM_CPU_MODE_IRQ:
9828 case ARM_CPU_MODE_SVC:
9830 case ARM_CPU_MODE_ABT:
9832 case ARM_CPU_MODE_UND:
9834 case ARM_CPU_MODE_FIQ:
9837 g_assert_not_reached();
9841 case ARM_CPU_MODE_USR:
9842 case ARM_CPU_MODE_SYS:
9843 case ARM_CPU_MODE_HYP:
9845 case ARM_CPU_MODE_IRQ:
9847 case ARM_CPU_MODE_SVC:
9849 case ARM_CPU_MODE_ABT:
9851 case ARM_CPU_MODE_UND:
9853 case ARM_CPU_MODE_FIQ:
9856 g_assert_not_reached();
9861 g_assert_not_reached();
9865 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
9867 uint32_t ret = cpsr_read(env);
9869 /* Move DIT to the correct location for SPSR_ELx */
9870 if (ret & CPSR_DIT) {
9874 /* Merge PSTATE.SS into SPSR_ELx */
9875 ret |= env->pstate & PSTATE_SS;
9880 static bool syndrome_is_sync_extabt(uint32_t syndrome)
9882 /* Return true if this syndrome value is a synchronous external abort */
9883 switch (syn_get_ec(syndrome)) {
9885 case EC_INSNABORT_SAME_EL:
9887 case EC_DATAABORT_SAME_EL:
9888 /* Look at fault status code for all the synchronous ext abort cases */
9889 switch (syndrome & 0x3f) {
9905 /* Handle exception entry to a target EL which is using AArch64 */
9906 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
9908 ARMCPU *cpu = ARM_CPU(cs);
9909 CPUARMState *env = &cpu->env;
9910 unsigned int new_el = env->exception.target_el;
9911 target_ulong addr = env->cp15.vbar_el[new_el];
9912 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
9913 unsigned int old_mode;
9914 unsigned int cur_el = arm_current_el(env);
9918 * Note that new_el can never be 0. If cur_el is 0, then
9919 * el0_a64 is is_a64(), else el0_a64 is ignored.
9921 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
9923 if (cur_el < new_el) {
9924 /* Entry vector offset depends on whether the implemented EL
9925 * immediately lower than the target level is using AArch32 or AArch64
9932 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9935 hcr = arm_hcr_el2_eff(env);
9936 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9937 is_aa64 = (hcr & HCR_RW) != 0;
9942 is_aa64 = is_a64(env);
9945 g_assert_not_reached();
9953 } else if (pstate_read(env) & PSTATE_SP) {
9957 switch (cs->exception_index) {
9958 case EXCP_PREFETCH_ABORT:
9959 case EXCP_DATA_ABORT:
9961 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
9962 * to be taken to the SError vector entrypoint.
9964 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
9965 syndrome_is_sync_extabt(env->exception.syndrome)) {
9968 env->cp15.far_el[new_el] = env->exception.vaddress;
9969 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9970 env->cp15.far_el[new_el]);
9978 switch (syn_get_ec(env->exception.syndrome)) {
9979 case EC_ADVSIMDFPACCESSTRAP:
9981 * QEMU internal FP/SIMD syndromes from AArch32 include the
9982 * TA and coproc fields which are only exposed if the exception
9983 * is taken to AArch32 Hyp mode. Mask them out to get a valid
9984 * AArch64 format syndrome.
9986 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
9992 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
9993 * the raw register field from the insn; when taking this to
9994 * AArch64 we must convert it to the AArch64 view of the register
9995 * number. Notice that we read a 4-bit AArch32 register number and
9996 * write back a 5-bit AArch64 one.
9998 rt = extract32(env->exception.syndrome, 5, 4);
9999 rt = aarch64_regnum(env, rt);
10000 env->exception.syndrome = deposit32(env->exception.syndrome,
10003 case EC_CP15RRTTRAP:
10004 case EC_CP14RRTTRAP:
10005 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10006 rt = extract32(env->exception.syndrome, 5, 4);
10007 rt = aarch64_regnum(env, rt);
10008 env->exception.syndrome = deposit32(env->exception.syndrome,
10010 rt = extract32(env->exception.syndrome, 10, 4);
10011 rt = aarch64_regnum(env, rt);
10012 env->exception.syndrome = deposit32(env->exception.syndrome,
10016 env->cp15.esr_el[new_el] = env->exception.syndrome;
10028 /* Construct the SError syndrome from IDS and ISS fields. */
10029 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10030 env->cp15.esr_el[new_el] = env->exception.syndrome;
10033 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10037 old_mode = pstate_read(env);
10038 aarch64_save_sp(env, arm_current_el(env));
10039 env->elr_el[new_el] = env->pc;
10041 old_mode = cpsr_read_for_spsr_elx(env);
10042 env->elr_el[new_el] = env->regs[15];
10044 aarch64_sync_32_to_64(env);
10046 env->condexec_bits = 0;
10048 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10050 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10051 env->elr_el[new_el]);
10053 if (cpu_isar_feature(aa64_pan, cpu)) {
10054 /* The value of PSTATE.PAN is normally preserved, except when ... */
10055 new_mode |= old_mode & PSTATE_PAN;
10058 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
10059 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10060 != (HCR_E2H | HCR_TGE)) {
10065 /* ... the target is EL1 ... */
10066 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10067 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10068 new_mode |= PSTATE_PAN;
10073 if (cpu_isar_feature(aa64_mte, cpu)) {
10074 new_mode |= PSTATE_TCO;
10077 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10078 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10079 new_mode |= PSTATE_SSBS;
10081 new_mode &= ~PSTATE_SSBS;
10085 pstate_write(env, PSTATE_DAIF | new_mode);
10086 env->aarch64 = true;
10087 aarch64_restore_sp(env, new_el);
10088 helper_rebuild_hflags_a64(env, new_el);
10092 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10093 new_el, env->pc, pstate_read(env));
10097 * Do semihosting call and set the appropriate return value. All the
10098 * permission and validity checks have been done at translate time.
10100 * We only see semihosting exceptions in TCG only as they are not
10101 * trapped to the hypervisor in KVM.
10104 static void handle_semihosting(CPUState *cs)
10106 ARMCPU *cpu = ARM_CPU(cs);
10107 CPUARMState *env = &cpu->env;
10110 qemu_log_mask(CPU_LOG_INT,
10111 "...handling as semihosting call 0x%" PRIx64 "\n",
10113 do_common_semihosting(cs);
10116 qemu_log_mask(CPU_LOG_INT,
10117 "...handling as semihosting call 0x%x\n",
10119 do_common_semihosting(cs);
10120 env->regs[15] += env->thumb ? 2 : 4;
10125 /* Handle a CPU exception for A and R profile CPUs.
10126 * Do any appropriate logging, handle PSCI calls, and then hand off
10127 * to the AArch64-entry or AArch32-entry function depending on the
10128 * target exception level's register width.
10130 * Note: this is used for both TCG (as the do_interrupt tcg op),
10131 * and KVM to re-inject guest debug exceptions, and to
10132 * inject a Synchronous-External-Abort.
10134 void arm_cpu_do_interrupt(CPUState *cs)
10136 ARMCPU *cpu = ARM_CPU(cs);
10137 CPUARMState *env = &cpu->env;
10138 unsigned int new_el = env->exception.target_el;
10140 assert(!arm_feature(env, ARM_FEATURE_M));
10142 arm_log_exception(cs);
10143 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10145 if (qemu_loglevel_mask(CPU_LOG_INT)
10146 && !excp_is_internal(cs->exception_index)) {
10147 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10148 syn_get_ec(env->exception.syndrome),
10149 env->exception.syndrome);
10152 if (arm_is_psci_call(cpu, cs->exception_index)) {
10153 arm_handle_psci_call(cpu);
10154 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10159 * Semihosting semantics depend on the register width of the code
10160 * that caused the exception, not the target exception level, so
10161 * must be handled here.
10164 if (cs->exception_index == EXCP_SEMIHOST) {
10165 handle_semihosting(cs);
10170 /* Hooks may change global state so BQL should be held, also the
10171 * BQL needs to be held for any modification of
10172 * cs->interrupt_request.
10174 g_assert(qemu_mutex_iothread_locked());
10176 arm_call_pre_el_change_hook(cpu);
10178 assert(!excp_is_internal(cs->exception_index));
10179 if (arm_el_is_aa64(env, new_el)) {
10180 arm_cpu_do_interrupt_aarch64(cs);
10182 arm_cpu_do_interrupt_aarch32(cs);
10185 arm_call_el_change_hook(cpu);
10187 if (!kvm_enabled()) {
10188 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10191 #endif /* !CONFIG_USER_ONLY */
10193 uint64_t arm_sctlr(CPUARMState *env, int el)
10195 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10197 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10198 el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10201 return env->cp15.sctlr_el[el];
10204 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10206 if (regime_has_2_ranges(mmu_idx)) {
10207 return extract64(tcr, 37, 2);
10208 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10209 return 0; /* VTCR_EL2 */
10211 /* Replicate the single TBI bit so we always have 2 bits. */
10212 return extract32(tcr, 20, 1) * 3;
10216 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10218 if (regime_has_2_ranges(mmu_idx)) {
10219 return extract64(tcr, 51, 2);
10220 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10221 return 0; /* VTCR_EL2 */
10223 /* Replicate the single TBID bit so we always have 2 bits. */
10224 return extract32(tcr, 29, 1) * 3;
10228 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10230 if (regime_has_2_ranges(mmu_idx)) {
10231 return extract64(tcr, 57, 2);
10233 /* Replicate the single TCMA bit so we always have 2 bits. */
10234 return extract32(tcr, 30, 1) * 3;
10238 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10239 ARMMMUIdx mmu_idx, bool data)
10241 uint64_t tcr = regime_tcr(env, mmu_idx);
10242 bool epd, hpd, using16k, using64k, tsz_oob, ds;
10243 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10244 ARMCPU *cpu = env_archcpu(env);
10246 if (!regime_has_2_ranges(mmu_idx)) {
10248 tsz = extract32(tcr, 0, 6);
10249 using64k = extract32(tcr, 14, 1);
10250 using16k = extract32(tcr, 15, 1);
10251 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10255 hpd = extract32(tcr, 24, 1);
10258 sh = extract32(tcr, 12, 2);
10259 ps = extract32(tcr, 16, 3);
10260 ds = extract64(tcr, 32, 1);
10263 * Bit 55 is always between the two regions, and is canonical for
10264 * determining if address tagging is enabled.
10266 select = extract64(va, 55, 1);
10268 tsz = extract32(tcr, 0, 6);
10269 epd = extract32(tcr, 7, 1);
10270 sh = extract32(tcr, 12, 2);
10271 using64k = extract32(tcr, 14, 1);
10272 using16k = extract32(tcr, 15, 1);
10273 hpd = extract64(tcr, 41, 1);
10275 int tg = extract32(tcr, 30, 2);
10276 using16k = tg == 1;
10277 using64k = tg == 3;
10278 tsz = extract32(tcr, 16, 6);
10279 epd = extract32(tcr, 23, 1);
10280 sh = extract32(tcr, 28, 2);
10281 hpd = extract64(tcr, 42, 1);
10283 ps = extract64(tcr, 32, 3);
10284 ds = extract64(tcr, 59, 1);
10287 if (cpu_isar_feature(aa64_st, cpu)) {
10288 max_tsz = 48 - using64k;
10294 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10295 * adjust the effective value of DS, as documented.
10299 if (cpu_isar_feature(aa64_lva, cpu)) {
10305 case ARMMMUIdx_Stage2:
10306 case ARMMMUIdx_Stage2_S:
10308 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10310 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10315 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10317 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10326 if (tsz > max_tsz) {
10329 } else if (tsz < min_tsz) {
10336 /* Present TBI as a composite with TBID. */
10337 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10339 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10341 tbi = (tbi >> select) & 1;
10343 return (ARMVAParameters) {
10351 .using16k = using16k,
10352 .using64k = using64k,
10353 .tsz_oob = tsz_oob,
10358 /* Note that signed overflow is undefined in C. The following routines are
10359 careful to use unsigned types where modulo arithmetic is required.
10360 Failure to do so _will_ break on newer gcc. */
10362 /* Signed saturating arithmetic. */
10364 /* Perform 16-bit signed saturating addition. */
10365 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10370 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10379 /* Perform 8-bit signed saturating addition. */
10380 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10385 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10394 /* Perform 16-bit signed saturating subtraction. */
10395 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10400 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10409 /* Perform 8-bit signed saturating subtraction. */
10410 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10415 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10424 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10425 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10426 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10427 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10430 #include "op_addsub.h"
10432 /* Unsigned saturating arithmetic. */
10433 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10442 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10450 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10459 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10467 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10468 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10469 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10470 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10473 #include "op_addsub.h"
10475 /* Signed modulo arithmetic. */
10476 #define SARITH16(a, b, n, op) do { \
10478 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10479 RESULT(sum, n, 16); \
10481 ge |= 3 << (n * 2); \
10484 #define SARITH8(a, b, n, op) do { \
10486 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10487 RESULT(sum, n, 8); \
10493 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10494 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10495 #define ADD8(a, b, n) SARITH8(a, b, n, +)
10496 #define SUB8(a, b, n) SARITH8(a, b, n, -)
10500 #include "op_addsub.h"
10502 /* Unsigned modulo arithmetic. */
10503 #define ADD16(a, b, n) do { \
10505 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10506 RESULT(sum, n, 16); \
10507 if ((sum >> 16) == 1) \
10508 ge |= 3 << (n * 2); \
10511 #define ADD8(a, b, n) do { \
10513 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10514 RESULT(sum, n, 8); \
10515 if ((sum >> 8) == 1) \
10519 #define SUB16(a, b, n) do { \
10521 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10522 RESULT(sum, n, 16); \
10523 if ((sum >> 16) == 0) \
10524 ge |= 3 << (n * 2); \
10527 #define SUB8(a, b, n) do { \
10529 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10530 RESULT(sum, n, 8); \
10531 if ((sum >> 8) == 0) \
10538 #include "op_addsub.h"
10540 /* Halved signed arithmetic. */
10541 #define ADD16(a, b, n) \
10542 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10543 #define SUB16(a, b, n) \
10544 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10545 #define ADD8(a, b, n) \
10546 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10547 #define SUB8(a, b, n) \
10548 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10551 #include "op_addsub.h"
10553 /* Halved unsigned arithmetic. */
10554 #define ADD16(a, b, n) \
10555 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10556 #define SUB16(a, b, n) \
10557 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10558 #define ADD8(a, b, n) \
10559 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10560 #define SUB8(a, b, n) \
10561 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10564 #include "op_addsub.h"
10566 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10574 /* Unsigned sum of absolute byte differences. */
10575 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10578 sum = do_usad(a, b);
10579 sum += do_usad(a >> 8, b >> 8);
10580 sum += do_usad(a >> 16, b >> 16);
10581 sum += do_usad(a >> 24, b >> 24);
10585 /* For ARMv6 SEL instruction. */
10586 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10598 mask |= 0xff000000;
10599 return (a & mask) | (b & ~mask);
10603 * The upper bytes of val (above the number specified by 'bytes') must have
10604 * been zeroed out by the caller.
10606 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
10610 stl_le_p(buf, val);
10612 /* zlib crc32 converts the accumulator and output to one's complement. */
10613 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
10616 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
10620 stl_le_p(buf, val);
10622 /* Linux crc32c converts the output to one's complement. */
10623 return crc32c(acc, buf, bytes) ^ 0xffffffff;
10626 /* Return the exception level to which FP-disabled exceptions should
10627 * be taken, or 0 if FP is enabled.
10629 int fp_exception_el(CPUARMState *env, int cur_el)
10631 #ifndef CONFIG_USER_ONLY
10634 /* CPACR and the CPTR registers don't exist before v6, so FP is
10635 * always accessible
10637 if (!arm_feature(env, ARM_FEATURE_V6)) {
10641 if (arm_feature(env, ARM_FEATURE_M)) {
10642 /* CPACR can cause a NOCP UsageFault taken to current security state */
10643 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
10647 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
10648 if (!extract32(env->v7m.nsacr, 10, 1)) {
10649 /* FP insns cause a NOCP UsageFault taken to Secure */
10657 hcr_el2 = arm_hcr_el2_eff(env);
10659 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
10660 * 0, 2 : trap EL0 and EL1/PL1 accesses
10661 * 1 : trap only EL0 accesses
10662 * 3 : trap no accesses
10663 * This register is ignored if E2H+TGE are both set.
10665 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10666 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
10676 /* Trap from Secure PL0 or PL1 to Secure PL1. */
10677 if (!arm_el_is_aa64(env, 3)
10678 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
10689 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
10690 * to control non-secure access to the FPU. It doesn't have any
10691 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
10693 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
10694 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
10695 if (!extract32(env->cp15.nsacr, 10, 1)) {
10696 /* FP insns act as UNDEF */
10697 return cur_el == 2 ? 2 : 1;
10702 * CPTR_EL2 is present in v7VE or v8, and changes format
10703 * with HCR_EL2.E2H (regardless of TGE).
10706 if (hcr_el2 & HCR_E2H) {
10707 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
10709 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
10717 } else if (arm_is_el2_enabled(env)) {
10718 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
10724 /* CPTR_EL3 : present in v8 */
10725 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
10726 /* Trap all FP ops to EL3 */
10733 /* Return the exception level we're running at if this is our mmu_idx */
10734 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
10736 if (mmu_idx & ARM_MMU_IDX_M) {
10737 return mmu_idx & ARM_MMU_IDX_M_PRIV;
10741 case ARMMMUIdx_E10_0:
10742 case ARMMMUIdx_E20_0:
10743 case ARMMMUIdx_SE10_0:
10744 case ARMMMUIdx_SE20_0:
10746 case ARMMMUIdx_E10_1:
10747 case ARMMMUIdx_E10_1_PAN:
10748 case ARMMMUIdx_SE10_1:
10749 case ARMMMUIdx_SE10_1_PAN:
10752 case ARMMMUIdx_E20_2:
10753 case ARMMMUIdx_E20_2_PAN:
10754 case ARMMMUIdx_SE2:
10755 case ARMMMUIdx_SE20_2:
10756 case ARMMMUIdx_SE20_2_PAN:
10758 case ARMMMUIdx_SE3:
10761 g_assert_not_reached();
10766 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
10768 g_assert_not_reached();
10772 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
10777 if (arm_feature(env, ARM_FEATURE_M)) {
10778 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
10781 /* See ARM pseudo-function ELIsInHost. */
10784 hcr = arm_hcr_el2_eff(env);
10785 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
10786 idx = ARMMMUIdx_E20_0;
10788 idx = ARMMMUIdx_E10_0;
10792 if (env->pstate & PSTATE_PAN) {
10793 idx = ARMMMUIdx_E10_1_PAN;
10795 idx = ARMMMUIdx_E10_1;
10799 /* Note that TGE does not apply at EL2. */
10800 if (arm_hcr_el2_eff(env) & HCR_E2H) {
10801 if (env->pstate & PSTATE_PAN) {
10802 idx = ARMMMUIdx_E20_2_PAN;
10804 idx = ARMMMUIdx_E20_2;
10807 idx = ARMMMUIdx_E2;
10811 return ARMMMUIdx_SE3;
10813 g_assert_not_reached();
10816 if (arm_is_secure_below_el3(env)) {
10817 idx &= ~ARM_MMU_IDX_A_NS;
10823 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
10825 return arm_mmu_idx_el(env, arm_current_el(env));
10828 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
10830 CPUARMTBFlags flags)
10832 DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
10833 DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
10835 if (arm_singlestep_active(env)) {
10836 DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
10841 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
10843 CPUARMTBFlags flags)
10845 bool sctlr_b = arm_sctlr_b(env);
10848 DP_TBFLAG_A32(flags, SCTLR__B, 1);
10850 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
10851 DP_TBFLAG_ANY(flags, BE_DATA, 1);
10853 DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
10855 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
10858 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
10861 CPUARMTBFlags flags = {};
10862 uint32_t ccr = env->v7m.ccr[env->v7m.secure];
10864 /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
10865 if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
10866 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10869 if (arm_v7m_is_handler_mode(env)) {
10870 DP_TBFLAG_M32(flags, HANDLER, 1);
10874 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
10875 * is suppressing them because the requested execution priority
10878 if (arm_feature(env, ARM_FEATURE_V8) &&
10879 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
10880 (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
10881 DP_TBFLAG_M32(flags, STACKCHECK, 1);
10884 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
10887 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
10890 CPUARMTBFlags flags = {};
10891 int el = arm_current_el(env);
10893 if (arm_sctlr(env, el) & SCTLR_A) {
10894 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10897 if (arm_el_is_aa64(env, 1)) {
10898 DP_TBFLAG_A32(flags, VFPEN, 1);
10901 if (el < 2 && env->cp15.hstr_el2 &&
10902 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10903 DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
10906 if (env->uncached_cpsr & CPSR_IL) {
10907 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
10911 * The SME exception we are testing for is raised via
10912 * AArch64.CheckFPAdvSIMDEnabled(), as called from
10913 * AArch32.CheckAdvSIMDOrFPEnabled().
10916 && FIELD_EX64(env->svcr, SVCR, SM)
10917 && (!arm_is_el2_enabled(env)
10918 || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
10919 && arm_el_is_aa64(env, 1)
10920 && !sme_fa64(env, el)) {
10921 DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
10924 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
10927 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
10930 CPUARMTBFlags flags = {};
10931 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
10932 uint64_t tcr = regime_tcr(env, mmu_idx);
10936 DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
10938 /* Get control bits for tagged addresses. */
10939 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
10940 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
10942 DP_TBFLAG_A64(flags, TBII, tbii);
10943 DP_TBFLAG_A64(flags, TBID, tbid);
10945 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
10946 int sve_el = sve_exception_el(env, el);
10949 * If either FP or SVE are disabled, translator does not need len.
10950 * If SVE EL > FP EL, FP exception has precedence, and translator
10951 * does not need SVE EL. Save potential re-translations by forcing
10952 * the unneeded data to zero.
10955 if (sve_el > fp_el) {
10958 } else if (sve_el == 0) {
10959 DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
10961 DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
10963 if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
10964 int sme_el = sme_exception_el(env, el);
10965 bool sm = FIELD_EX64(env->svcr, SVCR, SM);
10967 DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
10969 /* Similarly, do not compute SVL if SME is disabled. */
10970 int svl = sve_vqm1_for_el_sm(env, el, true);
10971 DP_TBFLAG_A64(flags, SVL, svl);
10973 /* If SVE is disabled, we will not have set VL above. */
10974 DP_TBFLAG_A64(flags, VL, svl);
10978 DP_TBFLAG_A64(flags, PSTATE_SM, 1);
10979 DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
10981 DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
10984 sctlr = regime_sctlr(env, stage1);
10986 if (sctlr & SCTLR_A) {
10987 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10990 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
10991 DP_TBFLAG_ANY(flags, BE_DATA, 1);
10994 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
10996 * In order to save space in flags, we record only whether
10997 * pauth is "inactive", meaning all insns are implemented as
10998 * a nop, or "active" when some action must be performed.
10999 * The decision of which action to take is left to a helper.
11001 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11002 DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11006 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11007 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
11008 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11009 DP_TBFLAG_A64(flags, BT, 1);
11013 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11014 if (!(env->pstate & PSTATE_UAO)) {
11016 case ARMMMUIdx_E10_1:
11017 case ARMMMUIdx_E10_1_PAN:
11018 case ARMMMUIdx_SE10_1:
11019 case ARMMMUIdx_SE10_1_PAN:
11020 /* TODO: ARMv8.3-NV */
11021 DP_TBFLAG_A64(flags, UNPRIV, 1);
11023 case ARMMMUIdx_E20_2:
11024 case ARMMMUIdx_E20_2_PAN:
11025 case ARMMMUIdx_SE20_2:
11026 case ARMMMUIdx_SE20_2_PAN:
11028 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11029 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11031 if (env->cp15.hcr_el2 & HCR_TGE) {
11032 DP_TBFLAG_A64(flags, UNPRIV, 1);
11040 if (env->pstate & PSTATE_IL) {
11041 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11044 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11046 * Set MTE_ACTIVE if any access may be Checked, and leave clear
11047 * if all accesses must be Unchecked:
11048 * 1) If no TBI, then there are no tags in the address to check,
11049 * 2) If Tag Check Override, then all accesses are Unchecked,
11050 * 3) If Tag Check Fail == 0, then Checked access have no effect,
11051 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11053 if (allocation_tag_access_enabled(env, el, sctlr)) {
11054 DP_TBFLAG_A64(flags, ATA, 1);
11056 && !(env->pstate & PSTATE_TCO)
11057 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11058 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11061 /* And again for unprivileged accesses, if required. */
11062 if (EX_TBFLAG_A64(flags, UNPRIV)
11064 && !(env->pstate & PSTATE_TCO)
11065 && (sctlr & SCTLR_TCF0)
11066 && allocation_tag_access_enabled(env, 0, sctlr)) {
11067 DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11069 /* Cache TCMA as well as TBI. */
11070 DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11073 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11076 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11078 int el = arm_current_el(env);
11079 int fp_el = fp_exception_el(env, el);
11080 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11083 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11084 } else if (arm_feature(env, ARM_FEATURE_M)) {
11085 return rebuild_hflags_m32(env, fp_el, mmu_idx);
11087 return rebuild_hflags_a32(env, fp_el, mmu_idx);
11091 void arm_rebuild_hflags(CPUARMState *env)
11093 env->hflags = rebuild_hflags_internal(env);
11097 * If we have triggered a EL state change we can't rely on the
11098 * translator having passed it to us, we need to recompute.
11100 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11102 int el = arm_current_el(env);
11103 int fp_el = fp_exception_el(env, el);
11104 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11106 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11109 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11111 int fp_el = fp_exception_el(env, el);
11112 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11114 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11118 * If we have triggered a EL state change we can't rely on the
11119 * translator having passed it to us, we need to recompute.
11121 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11123 int el = arm_current_el(env);
11124 int fp_el = fp_exception_el(env, el);
11125 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11126 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11129 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11131 int fp_el = fp_exception_el(env, el);
11132 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11134 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11137 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11139 int fp_el = fp_exception_el(env, el);
11140 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11142 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11145 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11147 #ifdef CONFIG_DEBUG_TCG
11148 CPUARMTBFlags c = env->hflags;
11149 CPUARMTBFlags r = rebuild_hflags_internal(env);
11151 if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11152 fprintf(stderr, "TCG hflags mismatch "
11153 "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11154 " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11155 c.flags, c.flags2, r.flags, r.flags2);
11161 static bool mve_no_pred(CPUARMState *env)
11164 * Return true if there is definitely no predication of MVE
11165 * instructions by VPR or LTPSIZE. (Returning false even if there
11166 * isn't any predication is OK; generated code will just be
11168 * If the CPU does not implement MVE then this TB flag is always 0.
11170 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11171 * logic in gen_update_fp_context() needs to be updated to match.
11173 * We do not include the effect of the ECI bits here -- they are
11174 * tracked in other TB flags. This simplifies the logic for
11175 * "when did we emit code that changes the MVE_NO_PRED TB flag
11176 * and thus need to end the TB?".
11178 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11181 if (env->v7m.vpr) {
11184 if (env->v7m.ltpsize < 4) {
11190 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11191 target_ulong *cs_base, uint32_t *pflags)
11193 CPUARMTBFlags flags;
11195 assert_hflags_rebuild_correctly(env);
11196 flags = env->hflags;
11198 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11200 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11201 DP_TBFLAG_A64(flags, BTYPE, env->btype);
11204 *pc = env->regs[15];
11206 if (arm_feature(env, ARM_FEATURE_M)) {
11207 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11208 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11209 != env->v7m.secure) {
11210 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11213 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11214 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11215 (env->v7m.secure &&
11216 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11218 * ASPEN is set, but FPCA/SFPA indicate that there is no
11219 * active FP context; we must create a new FP context before
11220 * executing any FP insn.
11222 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11225 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11226 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11227 DP_TBFLAG_M32(flags, LSPACT, 1);
11230 if (mve_no_pred(env)) {
11231 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11235 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11236 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11238 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11239 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11241 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11242 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11244 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11245 DP_TBFLAG_A32(flags, VFPEN, 1);
11249 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11250 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11254 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11255 * states defined in the ARM ARM for software singlestep:
11256 * SS_ACTIVE PSTATE.SS State
11257 * 0 x Inactive (the TB flag for SS is always 0)
11258 * 1 0 Active-pending
11259 * 1 1 Active-not-pending
11260 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11262 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11263 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11266 *pflags = flags.flags;
11267 *cs_base = flags.flags2;
11270 #ifdef TARGET_AARCH64
11272 * The manual says that when SVE is enabled and VQ is widened the
11273 * implementation is allowed to zero the previously inaccessible
11274 * portion of the registers. The corollary to that is that when
11275 * SVE is enabled and VQ is narrowed we are also allowed to zero
11276 * the now inaccessible portion of the registers.
11278 * The intent of this is that no predicate bit beyond VQ is ever set.
11279 * Which means that some operations on predicate registers themselves
11280 * may operate on full uint64_t or even unrolled across the maximum
11281 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
11282 * may well be cheaper than conditionals to restrict the operation
11283 * to the relevant portion of a uint16_t[16].
11285 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11290 assert(vq >= 1 && vq <= ARM_MAX_VQ);
11291 assert(vq <= env_archcpu(env)->sve_max_vq);
11293 /* Zap the high bits of the zregs. */
11294 for (i = 0; i < 32; i++) {
11295 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11298 /* Zap the high bits of the pregs and ffr. */
11301 pmask = ~(-1ULL << (16 * (vq & 3)));
11303 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11304 for (i = 0; i < 17; ++i) {
11305 env->vfp.pregs[i].p[j] &= pmask;
11311 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11316 exc_el = sme_exception_el(env, el);
11318 exc_el = sve_exception_el(env, el);
11321 return 0; /* disabled */
11323 return sve_vqm1_for_el_sm(env, el, sm);
11327 * Notice a change in SVE vector size when changing EL.
11329 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11330 int new_el, bool el0_a64)
11332 ARMCPU *cpu = env_archcpu(env);
11333 int old_len, new_len;
11334 bool old_a64, new_a64, sm;
11336 /* Nothing to do if no SVE. */
11337 if (!cpu_isar_feature(aa64_sve, cpu)) {
11341 /* Nothing to do if FP is disabled in either EL. */
11342 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11346 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11347 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11350 * Both AArch64.TakeException and AArch64.ExceptionReturn
11351 * invoke ResetSVEState when taking an exception from, or
11352 * returning to, AArch32 state when PSTATE.SM is enabled.
11354 sm = FIELD_EX64(env->svcr, SVCR, SM);
11355 if (old_a64 != new_a64 && sm) {
11356 arm_reset_sve_state(env);
11361 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11362 * at ELx, or not available because the EL is in AArch32 state, then
11363 * for all purposes other than a direct read, the ZCR_ELx.LEN field
11364 * has an effective value of 0".
11366 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11367 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11368 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
11369 * we already have the correct register contents when encountering the
11370 * vq0->vq0 transition between EL0->EL1.
11372 old_len = new_len = 0;
11374 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11377 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11380 /* When changing vector length, clear inaccessible state. */
11381 if (new_len < old_len) {
11382 aarch64_sve_narrow_vq(env, new_len + 1);