target/arm: Postpone interpretation of stage 2 descriptor attribute bits
[qemu/rayw.git] / target / arm / helper.c
blob93c58ad29abcb0bbb1e89e57dcb03887de742487
1 /*
2 * ARM generic helpers.
4 * This code is licensed under the GNU GPL v2 or later.
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
9 #include "qemu/osdep.h"
10 #include "qemu/units.h"
11 #include "qemu/log.h"
12 #include "target/arm/idau.h"
13 #include "trace.h"
14 #include "cpu.h"
15 #include "internals.h"
16 #include "exec/helper-proto.h"
17 #include "qemu/host-utils.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/timer.h"
20 #include "qemu/bitops.h"
21 #include "qemu/crc32c.h"
22 #include "qemu/qemu-print.h"
23 #include "exec/exec-all.h"
24 #include <zlib.h> /* For crc32 */
25 #include "hw/irq.h"
26 #include "semihosting/semihost.h"
27 #include "sysemu/cpus.h"
28 #include "sysemu/cpu-timers.h"
29 #include "sysemu/kvm.h"
30 #include "qemu/range.h"
31 #include "qapi/qapi-commands-machine-target.h"
32 #include "qapi/error.h"
33 #include "qemu/guest-random.h"
34 #ifdef CONFIG_TCG
35 #include "arm_ldst.h"
36 #include "exec/cpu_ldst.h"
37 #include "semihosting/common-semi.h"
38 #endif
39 #include "cpregs.h"
41 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
42 #define PMCR_NUM_COUNTERS 4 /* QEMU IMPDEF choice */
44 #ifndef CONFIG_USER_ONLY
46 static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
47 MMUAccessType access_type, ARMMMUIdx mmu_idx,
48 bool s1_is_el0,
49 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
50 target_ulong *page_size_ptr,
51 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
52 __attribute__((nonnull));
53 #endif
55 static void switch_mode(CPUARMState *env, int mode);
56 static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx);
58 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
60 assert(ri->fieldoffset);
61 if (cpreg_field_is_64bit(ri)) {
62 return CPREG_FIELD64(env, ri);
63 } else {
64 return CPREG_FIELD32(env, ri);
68 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
69 uint64_t value)
71 assert(ri->fieldoffset);
72 if (cpreg_field_is_64bit(ri)) {
73 CPREG_FIELD64(env, ri) = value;
74 } else {
75 CPREG_FIELD32(env, ri) = value;
79 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
81 return (char *)env + ri->fieldoffset;
84 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
86 /* Raw read of a coprocessor register (as needed for migration, etc). */
87 if (ri->type & ARM_CP_CONST) {
88 return ri->resetvalue;
89 } else if (ri->raw_readfn) {
90 return ri->raw_readfn(env, ri);
91 } else if (ri->readfn) {
92 return ri->readfn(env, ri);
93 } else {
94 return raw_read(env, ri);
98 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
99 uint64_t v)
101 /* Raw write of a coprocessor register (as needed for migration, etc).
102 * Note that constant registers are treated as write-ignored; the
103 * caller should check for success by whether a readback gives the
104 * value written.
106 if (ri->type & ARM_CP_CONST) {
107 return;
108 } else if (ri->raw_writefn) {
109 ri->raw_writefn(env, ri, v);
110 } else if (ri->writefn) {
111 ri->writefn(env, ri, v);
112 } else {
113 raw_write(env, ri, v);
117 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
119 /* Return true if the regdef would cause an assertion if you called
120 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
121 * program bug for it not to have the NO_RAW flag).
122 * NB that returning false here doesn't necessarily mean that calling
123 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
124 * read/write access functions which are safe for raw use" from "has
125 * read/write access functions which have side effects but has forgotten
126 * to provide raw access functions".
127 * The tests here line up with the conditions in read/write_raw_cp_reg()
128 * and assertions in raw_read()/raw_write().
130 if ((ri->type & ARM_CP_CONST) ||
131 ri->fieldoffset ||
132 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
133 return false;
135 return true;
138 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
140 /* Write the coprocessor state from cpu->env to the (index,value) list. */
141 int i;
142 bool ok = true;
144 for (i = 0; i < cpu->cpreg_array_len; i++) {
145 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
146 const ARMCPRegInfo *ri;
147 uint64_t newval;
149 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
150 if (!ri) {
151 ok = false;
152 continue;
154 if (ri->type & ARM_CP_NO_RAW) {
155 continue;
158 newval = read_raw_cp_reg(&cpu->env, ri);
159 if (kvm_sync) {
161 * Only sync if the previous list->cpustate sync succeeded.
162 * Rather than tracking the success/failure state for every
163 * item in the list, we just recheck "does the raw write we must
164 * have made in write_list_to_cpustate() read back OK" here.
166 uint64_t oldval = cpu->cpreg_values[i];
168 if (oldval == newval) {
169 continue;
172 write_raw_cp_reg(&cpu->env, ri, oldval);
173 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
174 continue;
177 write_raw_cp_reg(&cpu->env, ri, newval);
179 cpu->cpreg_values[i] = newval;
181 return ok;
184 bool write_list_to_cpustate(ARMCPU *cpu)
186 int i;
187 bool ok = true;
189 for (i = 0; i < cpu->cpreg_array_len; i++) {
190 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
191 uint64_t v = cpu->cpreg_values[i];
192 const ARMCPRegInfo *ri;
194 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
195 if (!ri) {
196 ok = false;
197 continue;
199 if (ri->type & ARM_CP_NO_RAW) {
200 continue;
202 /* Write value and confirm it reads back as written
203 * (to catch read-only registers and partially read-only
204 * registers where the incoming migration value doesn't match)
206 write_raw_cp_reg(&cpu->env, ri, v);
207 if (read_raw_cp_reg(&cpu->env, ri) != v) {
208 ok = false;
211 return ok;
214 static void add_cpreg_to_list(gpointer key, gpointer opaque)
216 ARMCPU *cpu = opaque;
217 uint32_t regidx = (uintptr_t)key;
218 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
220 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
221 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
222 /* The value array need not be initialized at this point */
223 cpu->cpreg_array_len++;
227 static void count_cpreg(gpointer key, gpointer opaque)
229 ARMCPU *cpu = opaque;
230 const ARMCPRegInfo *ri;
232 ri = g_hash_table_lookup(cpu->cp_regs, key);
234 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
235 cpu->cpreg_array_len++;
239 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
241 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
242 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
244 if (aidx > bidx) {
245 return 1;
247 if (aidx < bidx) {
248 return -1;
250 return 0;
253 void init_cpreg_list(ARMCPU *cpu)
255 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
256 * Note that we require cpreg_tuples[] to be sorted by key ID.
258 GList *keys;
259 int arraylen;
261 keys = g_hash_table_get_keys(cpu->cp_regs);
262 keys = g_list_sort(keys, cpreg_key_compare);
264 cpu->cpreg_array_len = 0;
266 g_list_foreach(keys, count_cpreg, cpu);
268 arraylen = cpu->cpreg_array_len;
269 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
270 cpu->cpreg_values = g_new(uint64_t, arraylen);
271 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
272 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
273 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
274 cpu->cpreg_array_len = 0;
276 g_list_foreach(keys, add_cpreg_to_list, cpu);
278 assert(cpu->cpreg_array_len == arraylen);
280 g_list_free(keys);
284 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
286 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
287 const ARMCPRegInfo *ri,
288 bool isread)
290 if (!is_a64(env) && arm_current_el(env) == 3 &&
291 arm_is_secure_below_el3(env)) {
292 return CP_ACCESS_TRAP_UNCATEGORIZED;
294 return CP_ACCESS_OK;
297 /* Some secure-only AArch32 registers trap to EL3 if used from
298 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
299 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
300 * We assume that the .access field is set to PL1_RW.
302 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
303 const ARMCPRegInfo *ri,
304 bool isread)
306 if (arm_current_el(env) == 3) {
307 return CP_ACCESS_OK;
309 if (arm_is_secure_below_el3(env)) {
310 if (env->cp15.scr_el3 & SCR_EEL2) {
311 return CP_ACCESS_TRAP_EL2;
313 return CP_ACCESS_TRAP_EL3;
315 /* This will be EL1 NS and EL2 NS, which just UNDEF */
316 return CP_ACCESS_TRAP_UNCATEGORIZED;
319 static uint64_t arm_mdcr_el2_eff(CPUARMState *env)
321 return arm_is_el2_enabled(env) ? env->cp15.mdcr_el2 : 0;
324 /* Check for traps to "powerdown debug" registers, which are controlled
325 * by MDCR.TDOSA
327 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
328 bool isread)
330 int el = arm_current_el(env);
331 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
332 bool mdcr_el2_tdosa = (mdcr_el2 & MDCR_TDOSA) || (mdcr_el2 & MDCR_TDE) ||
333 (arm_hcr_el2_eff(env) & HCR_TGE);
335 if (el < 2 && mdcr_el2_tdosa) {
336 return CP_ACCESS_TRAP_EL2;
338 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
339 return CP_ACCESS_TRAP_EL3;
341 return CP_ACCESS_OK;
344 /* Check for traps to "debug ROM" registers, which are controlled
345 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
347 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
348 bool isread)
350 int el = arm_current_el(env);
351 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
352 bool mdcr_el2_tdra = (mdcr_el2 & MDCR_TDRA) || (mdcr_el2 & MDCR_TDE) ||
353 (arm_hcr_el2_eff(env) & HCR_TGE);
355 if (el < 2 && mdcr_el2_tdra) {
356 return CP_ACCESS_TRAP_EL2;
358 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
359 return CP_ACCESS_TRAP_EL3;
361 return CP_ACCESS_OK;
364 /* Check for traps to general debug registers, which are controlled
365 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
367 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
368 bool isread)
370 int el = arm_current_el(env);
371 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
372 bool mdcr_el2_tda = (mdcr_el2 & MDCR_TDA) || (mdcr_el2 & MDCR_TDE) ||
373 (arm_hcr_el2_eff(env) & HCR_TGE);
375 if (el < 2 && mdcr_el2_tda) {
376 return CP_ACCESS_TRAP_EL2;
378 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
379 return CP_ACCESS_TRAP_EL3;
381 return CP_ACCESS_OK;
384 /* Check for traps to performance monitor registers, which are controlled
385 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
387 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
388 bool isread)
390 int el = arm_current_el(env);
391 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
393 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
394 return CP_ACCESS_TRAP_EL2;
396 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
397 return CP_ACCESS_TRAP_EL3;
399 return CP_ACCESS_OK;
402 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
403 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
404 bool isread)
406 if (arm_current_el(env) == 1) {
407 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
408 if (arm_hcr_el2_eff(env) & trap) {
409 return CP_ACCESS_TRAP_EL2;
412 return CP_ACCESS_OK;
415 /* Check for traps from EL1 due to HCR_EL2.TSW. */
416 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
417 bool isread)
419 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
420 return CP_ACCESS_TRAP_EL2;
422 return CP_ACCESS_OK;
425 /* Check for traps from EL1 due to HCR_EL2.TACR. */
426 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
427 bool isread)
429 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
430 return CP_ACCESS_TRAP_EL2;
432 return CP_ACCESS_OK;
435 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
436 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
437 bool isread)
439 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
440 return CP_ACCESS_TRAP_EL2;
442 return CP_ACCESS_OK;
445 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
447 ARMCPU *cpu = env_archcpu(env);
449 raw_write(env, ri, value);
450 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
453 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
455 ARMCPU *cpu = env_archcpu(env);
457 if (raw_read(env, ri) != value) {
458 /* Unlike real hardware the qemu TLB uses virtual addresses,
459 * not modified virtual addresses, so this causes a TLB flush.
461 tlb_flush(CPU(cpu));
462 raw_write(env, ri, value);
466 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
467 uint64_t value)
469 ARMCPU *cpu = env_archcpu(env);
471 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
472 && !extended_addresses_enabled(env)) {
473 /* For VMSA (when not using the LPAE long descriptor page table
474 * format) this register includes the ASID, so do a TLB flush.
475 * For PMSA it is purely a process ID and no action is needed.
477 tlb_flush(CPU(cpu));
479 raw_write(env, ri, value);
482 /* IS variants of TLB operations must affect all cores */
483 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
484 uint64_t value)
486 CPUState *cs = env_cpu(env);
488 tlb_flush_all_cpus_synced(cs);
491 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
492 uint64_t value)
494 CPUState *cs = env_cpu(env);
496 tlb_flush_all_cpus_synced(cs);
499 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
500 uint64_t value)
502 CPUState *cs = env_cpu(env);
504 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
507 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
508 uint64_t value)
510 CPUState *cs = env_cpu(env);
512 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
516 * Non-IS variants of TLB operations are upgraded to
517 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
518 * force broadcast of these operations.
520 static bool tlb_force_broadcast(CPUARMState *env)
522 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
525 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
526 uint64_t value)
528 /* Invalidate all (TLBIALL) */
529 CPUState *cs = env_cpu(env);
531 if (tlb_force_broadcast(env)) {
532 tlb_flush_all_cpus_synced(cs);
533 } else {
534 tlb_flush(cs);
538 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
539 uint64_t value)
541 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
542 CPUState *cs = env_cpu(env);
544 value &= TARGET_PAGE_MASK;
545 if (tlb_force_broadcast(env)) {
546 tlb_flush_page_all_cpus_synced(cs, value);
547 } else {
548 tlb_flush_page(cs, value);
552 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
553 uint64_t value)
555 /* Invalidate by ASID (TLBIASID) */
556 CPUState *cs = env_cpu(env);
558 if (tlb_force_broadcast(env)) {
559 tlb_flush_all_cpus_synced(cs);
560 } else {
561 tlb_flush(cs);
565 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 uint64_t value)
568 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
569 CPUState *cs = env_cpu(env);
571 value &= TARGET_PAGE_MASK;
572 if (tlb_force_broadcast(env)) {
573 tlb_flush_page_all_cpus_synced(cs, value);
574 } else {
575 tlb_flush_page(cs, value);
579 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
580 uint64_t value)
582 CPUState *cs = env_cpu(env);
584 tlb_flush_by_mmuidx(cs,
585 ARMMMUIdxBit_E10_1 |
586 ARMMMUIdxBit_E10_1_PAN |
587 ARMMMUIdxBit_E10_0);
590 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
591 uint64_t value)
593 CPUState *cs = env_cpu(env);
595 tlb_flush_by_mmuidx_all_cpus_synced(cs,
596 ARMMMUIdxBit_E10_1 |
597 ARMMMUIdxBit_E10_1_PAN |
598 ARMMMUIdxBit_E10_0);
602 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
603 uint64_t value)
605 CPUState *cs = env_cpu(env);
607 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
610 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
611 uint64_t value)
613 CPUState *cs = env_cpu(env);
615 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
618 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
619 uint64_t value)
621 CPUState *cs = env_cpu(env);
622 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
624 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
627 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
628 uint64_t value)
630 CPUState *cs = env_cpu(env);
631 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
633 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
634 ARMMMUIdxBit_E2);
637 static const ARMCPRegInfo cp_reginfo[] = {
638 /* Define the secure and non-secure FCSE identifier CP registers
639 * separately because there is no secure bank in V8 (no _EL3). This allows
640 * the secure register to be properly reset and migrated. There is also no
641 * v8 EL1 version of the register so the non-secure instance stands alone.
643 { .name = "FCSEIDR",
644 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
645 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
646 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
647 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
648 { .name = "FCSEIDR_S",
649 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
650 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
651 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
652 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
653 /* Define the secure and non-secure context identifier CP registers
654 * separately because there is no secure bank in V8 (no _EL3). This allows
655 * the secure register to be properly reset and migrated. In the
656 * non-secure case, the 32-bit register will have reset and migration
657 * disabled during registration as it is handled by the 64-bit instance.
659 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
660 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
661 .access = PL1_RW, .accessfn = access_tvm_trvm,
662 .secure = ARM_CP_SECSTATE_NS,
663 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
664 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
665 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
666 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
667 .access = PL1_RW, .accessfn = access_tvm_trvm,
668 .secure = ARM_CP_SECSTATE_S,
669 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
670 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
673 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
674 /* NB: Some of these registers exist in v8 but with more precise
675 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
677 /* MMU Domain access control / MPU write buffer control */
678 { .name = "DACR",
679 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
680 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
681 .writefn = dacr_write, .raw_writefn = raw_write,
682 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
683 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
684 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
685 * For v6 and v5, these mappings are overly broad.
687 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
688 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
689 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
690 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
691 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
692 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
693 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
694 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
695 /* Cache maintenance ops; some of this space may be overridden later. */
696 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
697 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
698 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
701 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
702 /* Not all pre-v6 cores implemented this WFI, so this is slightly
703 * over-broad.
705 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
706 .access = PL1_W, .type = ARM_CP_WFI },
709 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
710 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
711 * is UNPREDICTABLE; we choose to NOP as most implementations do).
713 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
714 .access = PL1_W, .type = ARM_CP_WFI },
715 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
716 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
717 * OMAPCP will override this space.
719 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
720 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
721 .resetvalue = 0 },
722 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
723 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
724 .resetvalue = 0 },
725 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
726 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
727 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
728 .resetvalue = 0 },
729 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
730 * implementing it as RAZ means the "debug architecture version" bits
731 * will read as a reserved value, which should cause Linux to not try
732 * to use the debug hardware.
734 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
735 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
736 /* MMU TLB control. Note that the wildcarding means we cover not just
737 * the unified TLB ops but also the dside/iside/inner-shareable variants.
739 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
740 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
741 .type = ARM_CP_NO_RAW },
742 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
743 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
744 .type = ARM_CP_NO_RAW },
745 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
746 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
747 .type = ARM_CP_NO_RAW },
748 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
749 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
750 .type = ARM_CP_NO_RAW },
751 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
752 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
753 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
754 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
757 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
758 uint64_t value)
760 uint32_t mask = 0;
762 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
763 if (!arm_feature(env, ARM_FEATURE_V8)) {
764 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
765 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
766 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
768 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
769 /* VFP coprocessor: cp10 & cp11 [23:20] */
770 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
772 if (!arm_feature(env, ARM_FEATURE_NEON)) {
773 /* ASEDIS [31] bit is RAO/WI */
774 value |= (1 << 31);
777 /* VFPv3 and upwards with NEON implement 32 double precision
778 * registers (D0-D31).
780 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
781 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
782 value |= (1 << 30);
785 value &= mask;
789 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
790 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
792 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
793 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
794 value &= ~(0xf << 20);
795 value |= env->cp15.cpacr_el1 & (0xf << 20);
798 env->cp15.cpacr_el1 = value;
801 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
804 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
805 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
807 uint64_t value = env->cp15.cpacr_el1;
809 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
810 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
811 value &= ~(0xf << 20);
813 return value;
817 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
819 /* Call cpacr_write() so that we reset with the correct RAO bits set
820 * for our CPU features.
822 cpacr_write(env, ri, 0);
825 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
826 bool isread)
828 if (arm_feature(env, ARM_FEATURE_V8)) {
829 /* Check if CPACR accesses are to be trapped to EL2 */
830 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
831 (env->cp15.cptr_el[2] & CPTR_TCPAC)) {
832 return CP_ACCESS_TRAP_EL2;
833 /* Check if CPACR accesses are to be trapped to EL3 */
834 } else if (arm_current_el(env) < 3 &&
835 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
836 return CP_ACCESS_TRAP_EL3;
840 return CP_ACCESS_OK;
843 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
844 bool isread)
846 /* Check if CPTR accesses are set to trap to EL3 */
847 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
848 return CP_ACCESS_TRAP_EL3;
851 return CP_ACCESS_OK;
854 static const ARMCPRegInfo v6_cp_reginfo[] = {
855 /* prefetch by MVA in v6, NOP in v7 */
856 { .name = "MVA_prefetch",
857 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
858 .access = PL1_W, .type = ARM_CP_NOP },
859 /* We need to break the TB after ISB to execute self-modifying code
860 * correctly and also to take any pending interrupts immediately.
861 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
863 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
864 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
865 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
866 .access = PL0_W, .type = ARM_CP_NOP },
867 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
868 .access = PL0_W, .type = ARM_CP_NOP },
869 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
870 .access = PL1_RW, .accessfn = access_tvm_trvm,
871 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
872 offsetof(CPUARMState, cp15.ifar_ns) },
873 .resetvalue = 0, },
874 /* Watchpoint Fault Address Register : should actually only be present
875 * for 1136, 1176, 11MPCore.
877 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
878 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
879 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
880 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
881 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
882 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
885 typedef struct pm_event {
886 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
887 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
888 bool (*supported)(CPUARMState *);
890 * Retrieve the current count of the underlying event. The programmed
891 * counters hold a difference from the return value from this function
893 uint64_t (*get_count)(CPUARMState *);
895 * Return how many nanoseconds it will take (at a minimum) for count events
896 * to occur. A negative value indicates the counter will never overflow, or
897 * that the counter has otherwise arranged for the overflow bit to be set
898 * and the PMU interrupt to be raised on overflow.
900 int64_t (*ns_per_count)(uint64_t);
901 } pm_event;
903 static bool event_always_supported(CPUARMState *env)
905 return true;
908 static uint64_t swinc_get_count(CPUARMState *env)
911 * SW_INCR events are written directly to the pmevcntr's by writes to
912 * PMSWINC, so there is no underlying count maintained by the PMU itself
914 return 0;
917 static int64_t swinc_ns_per(uint64_t ignored)
919 return -1;
923 * Return the underlying cycle count for the PMU cycle counters. If we're in
924 * usermode, simply return 0.
926 static uint64_t cycles_get_count(CPUARMState *env)
928 #ifndef CONFIG_USER_ONLY
929 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
930 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
931 #else
932 return cpu_get_host_ticks();
933 #endif
936 #ifndef CONFIG_USER_ONLY
937 static int64_t cycles_ns_per(uint64_t cycles)
939 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
942 static bool instructions_supported(CPUARMState *env)
944 return icount_enabled() == 1; /* Precise instruction counting */
947 static uint64_t instructions_get_count(CPUARMState *env)
949 return (uint64_t)icount_get_raw();
952 static int64_t instructions_ns_per(uint64_t icount)
954 return icount_to_ns((int64_t)icount);
956 #endif
958 static bool pmu_8_1_events_supported(CPUARMState *env)
960 /* For events which are supported in any v8.1 PMU */
961 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
964 static bool pmu_8_4_events_supported(CPUARMState *env)
966 /* For events which are supported in any v8.1 PMU */
967 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
970 static uint64_t zero_event_get_count(CPUARMState *env)
972 /* For events which on QEMU never fire, so their count is always zero */
973 return 0;
976 static int64_t zero_event_ns_per(uint64_t cycles)
978 /* An event which never fires can never overflow */
979 return -1;
982 static const pm_event pm_events[] = {
983 { .number = 0x000, /* SW_INCR */
984 .supported = event_always_supported,
985 .get_count = swinc_get_count,
986 .ns_per_count = swinc_ns_per,
988 #ifndef CONFIG_USER_ONLY
989 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
990 .supported = instructions_supported,
991 .get_count = instructions_get_count,
992 .ns_per_count = instructions_ns_per,
994 { .number = 0x011, /* CPU_CYCLES, Cycle */
995 .supported = event_always_supported,
996 .get_count = cycles_get_count,
997 .ns_per_count = cycles_ns_per,
999 #endif
1000 { .number = 0x023, /* STALL_FRONTEND */
1001 .supported = pmu_8_1_events_supported,
1002 .get_count = zero_event_get_count,
1003 .ns_per_count = zero_event_ns_per,
1005 { .number = 0x024, /* STALL_BACKEND */
1006 .supported = pmu_8_1_events_supported,
1007 .get_count = zero_event_get_count,
1008 .ns_per_count = zero_event_ns_per,
1010 { .number = 0x03c, /* STALL */
1011 .supported = pmu_8_4_events_supported,
1012 .get_count = zero_event_get_count,
1013 .ns_per_count = zero_event_ns_per,
1018 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1019 * events (i.e. the statistical profiling extension), this implementation
1020 * should first be updated to something sparse instead of the current
1021 * supported_event_map[] array.
1023 #define MAX_EVENT_ID 0x3c
1024 #define UNSUPPORTED_EVENT UINT16_MAX
1025 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1028 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1029 * of ARM event numbers to indices in our pm_events array.
1031 * Note: Events in the 0x40XX range are not currently supported.
1033 void pmu_init(ARMCPU *cpu)
1035 unsigned int i;
1038 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1039 * events to them
1041 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1042 supported_event_map[i] = UNSUPPORTED_EVENT;
1044 cpu->pmceid0 = 0;
1045 cpu->pmceid1 = 0;
1047 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1048 const pm_event *cnt = &pm_events[i];
1049 assert(cnt->number <= MAX_EVENT_ID);
1050 /* We do not currently support events in the 0x40xx range */
1051 assert(cnt->number <= 0x3f);
1053 if (cnt->supported(&cpu->env)) {
1054 supported_event_map[cnt->number] = i;
1055 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1056 if (cnt->number & 0x20) {
1057 cpu->pmceid1 |= event_mask;
1058 } else {
1059 cpu->pmceid0 |= event_mask;
1066 * Check at runtime whether a PMU event is supported for the current machine
1068 static bool event_supported(uint16_t number)
1070 if (number > MAX_EVENT_ID) {
1071 return false;
1073 return supported_event_map[number] != UNSUPPORTED_EVENT;
1076 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1077 bool isread)
1079 /* Performance monitor registers user accessibility is controlled
1080 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1081 * trapping to EL2 or EL3 for other accesses.
1083 int el = arm_current_el(env);
1084 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1086 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1087 return CP_ACCESS_TRAP;
1089 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1090 return CP_ACCESS_TRAP_EL2;
1092 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1093 return CP_ACCESS_TRAP_EL3;
1096 return CP_ACCESS_OK;
1099 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1100 const ARMCPRegInfo *ri,
1101 bool isread)
1103 /* ER: event counter read trap control */
1104 if (arm_feature(env, ARM_FEATURE_V8)
1105 && arm_current_el(env) == 0
1106 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1107 && isread) {
1108 return CP_ACCESS_OK;
1111 return pmreg_access(env, ri, isread);
1114 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1115 const ARMCPRegInfo *ri,
1116 bool isread)
1118 /* SW: software increment write trap control */
1119 if (arm_feature(env, ARM_FEATURE_V8)
1120 && arm_current_el(env) == 0
1121 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1122 && !isread) {
1123 return CP_ACCESS_OK;
1126 return pmreg_access(env, ri, isread);
1129 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1130 const ARMCPRegInfo *ri,
1131 bool isread)
1133 /* ER: event counter read trap control */
1134 if (arm_feature(env, ARM_FEATURE_V8)
1135 && arm_current_el(env) == 0
1136 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1137 return CP_ACCESS_OK;
1140 return pmreg_access(env, ri, isread);
1143 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1144 const ARMCPRegInfo *ri,
1145 bool isread)
1147 /* CR: cycle counter read trap control */
1148 if (arm_feature(env, ARM_FEATURE_V8)
1149 && arm_current_el(env) == 0
1150 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1151 && isread) {
1152 return CP_ACCESS_OK;
1155 return pmreg_access(env, ri, isread);
1158 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1159 * the current EL, security state, and register configuration.
1161 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1163 uint64_t filter;
1164 bool e, p, u, nsk, nsu, nsh, m;
1165 bool enabled, prohibited, filtered;
1166 bool secure = arm_is_secure(env);
1167 int el = arm_current_el(env);
1168 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1169 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1171 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1172 return false;
1175 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1176 (counter < hpmn || counter == 31)) {
1177 e = env->cp15.c9_pmcr & PMCRE;
1178 } else {
1179 e = mdcr_el2 & MDCR_HPME;
1181 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1183 if (!secure) {
1184 if (el == 2 && (counter < hpmn || counter == 31)) {
1185 prohibited = mdcr_el2 & MDCR_HPMD;
1186 } else {
1187 prohibited = false;
1189 } else {
1190 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1191 !(env->cp15.mdcr_el3 & MDCR_SPME);
1194 if (prohibited && counter == 31) {
1195 prohibited = env->cp15.c9_pmcr & PMCRDP;
1198 if (counter == 31) {
1199 filter = env->cp15.pmccfiltr_el0;
1200 } else {
1201 filter = env->cp15.c14_pmevtyper[counter];
1204 p = filter & PMXEVTYPER_P;
1205 u = filter & PMXEVTYPER_U;
1206 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1207 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1208 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1209 m = arm_el_is_aa64(env, 1) &&
1210 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1212 if (el == 0) {
1213 filtered = secure ? u : u != nsu;
1214 } else if (el == 1) {
1215 filtered = secure ? p : p != nsk;
1216 } else if (el == 2) {
1217 filtered = !nsh;
1218 } else { /* EL3 */
1219 filtered = m != p;
1222 if (counter != 31) {
1224 * If not checking PMCCNTR, ensure the counter is setup to an event we
1225 * support
1227 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1228 if (!event_supported(event)) {
1229 return false;
1233 return enabled && !prohibited && !filtered;
1236 static void pmu_update_irq(CPUARMState *env)
1238 ARMCPU *cpu = env_archcpu(env);
1239 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1240 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1244 * Ensure c15_ccnt is the guest-visible count so that operations such as
1245 * enabling/disabling the counter or filtering, modifying the count itself,
1246 * etc. can be done logically. This is essentially a no-op if the counter is
1247 * not enabled at the time of the call.
1249 static void pmccntr_op_start(CPUARMState *env)
1251 uint64_t cycles = cycles_get_count(env);
1253 if (pmu_counter_enabled(env, 31)) {
1254 uint64_t eff_cycles = cycles;
1255 if (env->cp15.c9_pmcr & PMCRD) {
1256 /* Increment once every 64 processor clock cycles */
1257 eff_cycles /= 64;
1260 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1262 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1263 1ull << 63 : 1ull << 31;
1264 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1265 env->cp15.c9_pmovsr |= (1 << 31);
1266 pmu_update_irq(env);
1269 env->cp15.c15_ccnt = new_pmccntr;
1271 env->cp15.c15_ccnt_delta = cycles;
1275 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1276 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1277 * pmccntr_op_start.
1279 static void pmccntr_op_finish(CPUARMState *env)
1281 if (pmu_counter_enabled(env, 31)) {
1282 #ifndef CONFIG_USER_ONLY
1283 /* Calculate when the counter will next overflow */
1284 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1285 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1286 remaining_cycles = (uint32_t)remaining_cycles;
1288 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1290 if (overflow_in > 0) {
1291 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1292 overflow_in;
1293 ARMCPU *cpu = env_archcpu(env);
1294 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1296 #endif
1298 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1299 if (env->cp15.c9_pmcr & PMCRD) {
1300 /* Increment once every 64 processor clock cycles */
1301 prev_cycles /= 64;
1303 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1307 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1310 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1311 uint64_t count = 0;
1312 if (event_supported(event)) {
1313 uint16_t event_idx = supported_event_map[event];
1314 count = pm_events[event_idx].get_count(env);
1317 if (pmu_counter_enabled(env, counter)) {
1318 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1320 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1321 env->cp15.c9_pmovsr |= (1 << counter);
1322 pmu_update_irq(env);
1324 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1326 env->cp15.c14_pmevcntr_delta[counter] = count;
1329 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1331 if (pmu_counter_enabled(env, counter)) {
1332 #ifndef CONFIG_USER_ONLY
1333 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1334 uint16_t event_idx = supported_event_map[event];
1335 uint64_t delta = UINT32_MAX -
1336 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1337 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1339 if (overflow_in > 0) {
1340 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1341 overflow_in;
1342 ARMCPU *cpu = env_archcpu(env);
1343 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1345 #endif
1347 env->cp15.c14_pmevcntr_delta[counter] -=
1348 env->cp15.c14_pmevcntr[counter];
1352 void pmu_op_start(CPUARMState *env)
1354 unsigned int i;
1355 pmccntr_op_start(env);
1356 for (i = 0; i < pmu_num_counters(env); i++) {
1357 pmevcntr_op_start(env, i);
1361 void pmu_op_finish(CPUARMState *env)
1363 unsigned int i;
1364 pmccntr_op_finish(env);
1365 for (i = 0; i < pmu_num_counters(env); i++) {
1366 pmevcntr_op_finish(env, i);
1370 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1372 pmu_op_start(&cpu->env);
1375 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1377 pmu_op_finish(&cpu->env);
1380 void arm_pmu_timer_cb(void *opaque)
1382 ARMCPU *cpu = opaque;
1385 * Update all the counter values based on the current underlying counts,
1386 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1387 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1388 * counter may expire.
1390 pmu_op_start(&cpu->env);
1391 pmu_op_finish(&cpu->env);
1394 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1395 uint64_t value)
1397 pmu_op_start(env);
1399 if (value & PMCRC) {
1400 /* The counter has been reset */
1401 env->cp15.c15_ccnt = 0;
1404 if (value & PMCRP) {
1405 unsigned int i;
1406 for (i = 0; i < pmu_num_counters(env); i++) {
1407 env->cp15.c14_pmevcntr[i] = 0;
1411 env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1412 env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
1414 pmu_op_finish(env);
1417 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1418 uint64_t value)
1420 unsigned int i;
1421 for (i = 0; i < pmu_num_counters(env); i++) {
1422 /* Increment a counter's count iff: */
1423 if ((value & (1 << i)) && /* counter's bit is set */
1424 /* counter is enabled and not filtered */
1425 pmu_counter_enabled(env, i) &&
1426 /* counter is SW_INCR */
1427 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1428 pmevcntr_op_start(env, i);
1431 * Detect if this write causes an overflow since we can't predict
1432 * PMSWINC overflows like we can for other events
1434 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1436 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1437 env->cp15.c9_pmovsr |= (1 << i);
1438 pmu_update_irq(env);
1441 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1443 pmevcntr_op_finish(env, i);
1448 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1450 uint64_t ret;
1451 pmccntr_op_start(env);
1452 ret = env->cp15.c15_ccnt;
1453 pmccntr_op_finish(env);
1454 return ret;
1457 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1458 uint64_t value)
1460 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1461 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1462 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1463 * accessed.
1465 env->cp15.c9_pmselr = value & 0x1f;
1468 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1469 uint64_t value)
1471 pmccntr_op_start(env);
1472 env->cp15.c15_ccnt = value;
1473 pmccntr_op_finish(env);
1476 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1477 uint64_t value)
1479 uint64_t cur_val = pmccntr_read(env, NULL);
1481 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1484 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1485 uint64_t value)
1487 pmccntr_op_start(env);
1488 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1489 pmccntr_op_finish(env);
1492 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1493 uint64_t value)
1495 pmccntr_op_start(env);
1496 /* M is not accessible from AArch32 */
1497 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1498 (value & PMCCFILTR);
1499 pmccntr_op_finish(env);
1502 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1504 /* M is not visible in AArch32 */
1505 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1508 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1509 uint64_t value)
1511 value &= pmu_counter_mask(env);
1512 env->cp15.c9_pmcnten |= value;
1515 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1516 uint64_t value)
1518 value &= pmu_counter_mask(env);
1519 env->cp15.c9_pmcnten &= ~value;
1522 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1523 uint64_t value)
1525 value &= pmu_counter_mask(env);
1526 env->cp15.c9_pmovsr &= ~value;
1527 pmu_update_irq(env);
1530 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1531 uint64_t value)
1533 value &= pmu_counter_mask(env);
1534 env->cp15.c9_pmovsr |= value;
1535 pmu_update_irq(env);
1538 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1539 uint64_t value, const uint8_t counter)
1541 if (counter == 31) {
1542 pmccfiltr_write(env, ri, value);
1543 } else if (counter < pmu_num_counters(env)) {
1544 pmevcntr_op_start(env, counter);
1547 * If this counter's event type is changing, store the current
1548 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1549 * pmevcntr_op_finish has the correct baseline when it converts back to
1550 * a delta.
1552 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1553 PMXEVTYPER_EVTCOUNT;
1554 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1555 if (old_event != new_event) {
1556 uint64_t count = 0;
1557 if (event_supported(new_event)) {
1558 uint16_t event_idx = supported_event_map[new_event];
1559 count = pm_events[event_idx].get_count(env);
1561 env->cp15.c14_pmevcntr_delta[counter] = count;
1564 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1565 pmevcntr_op_finish(env, counter);
1567 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1568 * PMSELR value is equal to or greater than the number of implemented
1569 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1573 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1574 const uint8_t counter)
1576 if (counter == 31) {
1577 return env->cp15.pmccfiltr_el0;
1578 } else if (counter < pmu_num_counters(env)) {
1579 return env->cp15.c14_pmevtyper[counter];
1580 } else {
1582 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1583 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1585 return 0;
1589 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1590 uint64_t value)
1592 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1593 pmevtyper_write(env, ri, value, counter);
1596 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1597 uint64_t value)
1599 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1600 env->cp15.c14_pmevtyper[counter] = value;
1603 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1604 * pmu_op_finish calls when loading saved state for a migration. Because
1605 * we're potentially updating the type of event here, the value written to
1606 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1607 * different counter type. Therefore, we need to set this value to the
1608 * current count for the counter type we're writing so that pmu_op_finish
1609 * has the correct count for its calculation.
1611 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1612 if (event_supported(event)) {
1613 uint16_t event_idx = supported_event_map[event];
1614 env->cp15.c14_pmevcntr_delta[counter] =
1615 pm_events[event_idx].get_count(env);
1619 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1621 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1622 return pmevtyper_read(env, ri, counter);
1625 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1626 uint64_t value)
1628 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1631 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1633 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1636 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1637 uint64_t value, uint8_t counter)
1639 if (counter < pmu_num_counters(env)) {
1640 pmevcntr_op_start(env, counter);
1641 env->cp15.c14_pmevcntr[counter] = value;
1642 pmevcntr_op_finish(env, counter);
1645 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1646 * are CONSTRAINED UNPREDICTABLE.
1650 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1651 uint8_t counter)
1653 if (counter < pmu_num_counters(env)) {
1654 uint64_t ret;
1655 pmevcntr_op_start(env, counter);
1656 ret = env->cp15.c14_pmevcntr[counter];
1657 pmevcntr_op_finish(env, counter);
1658 return ret;
1659 } else {
1660 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1661 * are CONSTRAINED UNPREDICTABLE. */
1662 return 0;
1666 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1667 uint64_t value)
1669 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1670 pmevcntr_write(env, ri, value, counter);
1673 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1675 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1676 return pmevcntr_read(env, ri, counter);
1679 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1680 uint64_t value)
1682 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1683 assert(counter < pmu_num_counters(env));
1684 env->cp15.c14_pmevcntr[counter] = value;
1685 pmevcntr_write(env, ri, value, counter);
1688 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1690 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1691 assert(counter < pmu_num_counters(env));
1692 return env->cp15.c14_pmevcntr[counter];
1695 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1696 uint64_t value)
1698 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1701 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1703 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1706 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1707 uint64_t value)
1709 if (arm_feature(env, ARM_FEATURE_V8)) {
1710 env->cp15.c9_pmuserenr = value & 0xf;
1711 } else {
1712 env->cp15.c9_pmuserenr = value & 1;
1716 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1717 uint64_t value)
1719 /* We have no event counters so only the C bit can be changed */
1720 value &= pmu_counter_mask(env);
1721 env->cp15.c9_pminten |= value;
1722 pmu_update_irq(env);
1725 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1726 uint64_t value)
1728 value &= pmu_counter_mask(env);
1729 env->cp15.c9_pminten &= ~value;
1730 pmu_update_irq(env);
1733 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1734 uint64_t value)
1736 /* Note that even though the AArch64 view of this register has bits
1737 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1738 * architectural requirements for bits which are RES0 only in some
1739 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1740 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1742 raw_write(env, ri, value & ~0x1FULL);
1745 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1747 /* Begin with base v8.0 state. */
1748 uint32_t valid_mask = 0x3fff;
1749 ARMCPU *cpu = env_archcpu(env);
1751 if (ri->state == ARM_CP_STATE_AA64) {
1752 if (arm_feature(env, ARM_FEATURE_AARCH64) &&
1753 !cpu_isar_feature(aa64_aa32_el1, cpu)) {
1754 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
1756 valid_mask &= ~SCR_NET;
1758 if (cpu_isar_feature(aa64_ras, cpu)) {
1759 valid_mask |= SCR_TERR;
1761 if (cpu_isar_feature(aa64_lor, cpu)) {
1762 valid_mask |= SCR_TLOR;
1764 if (cpu_isar_feature(aa64_pauth, cpu)) {
1765 valid_mask |= SCR_API | SCR_APK;
1767 if (cpu_isar_feature(aa64_sel2, cpu)) {
1768 valid_mask |= SCR_EEL2;
1770 if (cpu_isar_feature(aa64_mte, cpu)) {
1771 valid_mask |= SCR_ATA;
1773 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1774 valid_mask |= SCR_ENSCXT;
1776 } else {
1777 valid_mask &= ~(SCR_RW | SCR_ST);
1778 if (cpu_isar_feature(aa32_ras, cpu)) {
1779 valid_mask |= SCR_TERR;
1783 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1784 valid_mask &= ~SCR_HCE;
1786 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1787 * supported if EL2 exists. The bit is UNK/SBZP when
1788 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1789 * when EL2 is unavailable.
1790 * On ARMv8, this bit is always available.
1792 if (arm_feature(env, ARM_FEATURE_V7) &&
1793 !arm_feature(env, ARM_FEATURE_V8)) {
1794 valid_mask &= ~SCR_SMD;
1798 /* Clear all-context RES0 bits. */
1799 value &= valid_mask;
1800 raw_write(env, ri, value);
1803 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1806 * scr_write will set the RES1 bits on an AArch64-only CPU.
1807 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1809 scr_write(env, ri, 0);
1812 static CPAccessResult access_aa64_tid2(CPUARMState *env,
1813 const ARMCPRegInfo *ri,
1814 bool isread)
1816 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1817 return CP_ACCESS_TRAP_EL2;
1820 return CP_ACCESS_OK;
1823 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1825 ARMCPU *cpu = env_archcpu(env);
1827 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1828 * bank
1830 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1831 ri->secure & ARM_CP_SECSTATE_S);
1833 return cpu->ccsidr[index];
1836 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1837 uint64_t value)
1839 raw_write(env, ri, value & 0xf);
1842 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1844 CPUState *cs = env_cpu(env);
1845 bool el1 = arm_current_el(env) == 1;
1846 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1847 uint64_t ret = 0;
1849 if (hcr_el2 & HCR_IMO) {
1850 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1851 ret |= CPSR_I;
1853 } else {
1854 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1855 ret |= CPSR_I;
1859 if (hcr_el2 & HCR_FMO) {
1860 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1861 ret |= CPSR_F;
1863 } else {
1864 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1865 ret |= CPSR_F;
1869 if (hcr_el2 & HCR_AMO) {
1870 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1871 ret |= CPSR_A;
1875 return ret;
1878 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1879 bool isread)
1881 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1882 return CP_ACCESS_TRAP_EL2;
1885 return CP_ACCESS_OK;
1888 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1889 bool isread)
1891 if (arm_feature(env, ARM_FEATURE_V8)) {
1892 return access_aa64_tid1(env, ri, isread);
1895 return CP_ACCESS_OK;
1898 static const ARMCPRegInfo v7_cp_reginfo[] = {
1899 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1900 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1901 .access = PL1_W, .type = ARM_CP_NOP },
1902 /* Performance monitors are implementation defined in v7,
1903 * but with an ARM recommended set of registers, which we
1904 * follow.
1906 * Performance registers fall into three categories:
1907 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1908 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1909 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1910 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1911 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1913 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1914 .access = PL0_RW, .type = ARM_CP_ALIAS,
1915 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1916 .writefn = pmcntenset_write,
1917 .accessfn = pmreg_access,
1918 .raw_writefn = raw_write },
1919 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1920 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1921 .access = PL0_RW, .accessfn = pmreg_access,
1922 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1923 .writefn = pmcntenset_write, .raw_writefn = raw_write },
1924 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1925 .access = PL0_RW,
1926 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1927 .accessfn = pmreg_access,
1928 .writefn = pmcntenclr_write,
1929 .type = ARM_CP_ALIAS },
1930 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1931 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1932 .access = PL0_RW, .accessfn = pmreg_access,
1933 .type = ARM_CP_ALIAS,
1934 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1935 .writefn = pmcntenclr_write },
1936 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1937 .access = PL0_RW, .type = ARM_CP_IO,
1938 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1939 .accessfn = pmreg_access,
1940 .writefn = pmovsr_write,
1941 .raw_writefn = raw_write },
1942 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1943 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1944 .access = PL0_RW, .accessfn = pmreg_access,
1945 .type = ARM_CP_ALIAS | ARM_CP_IO,
1946 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1947 .writefn = pmovsr_write,
1948 .raw_writefn = raw_write },
1949 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1950 .access = PL0_W, .accessfn = pmreg_access_swinc,
1951 .type = ARM_CP_NO_RAW | ARM_CP_IO,
1952 .writefn = pmswinc_write },
1953 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1954 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1955 .access = PL0_W, .accessfn = pmreg_access_swinc,
1956 .type = ARM_CP_NO_RAW | ARM_CP_IO,
1957 .writefn = pmswinc_write },
1958 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1959 .access = PL0_RW, .type = ARM_CP_ALIAS,
1960 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1961 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1962 .raw_writefn = raw_write},
1963 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1964 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1965 .access = PL0_RW, .accessfn = pmreg_access_selr,
1966 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1967 .writefn = pmselr_write, .raw_writefn = raw_write, },
1968 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1969 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1970 .readfn = pmccntr_read, .writefn = pmccntr_write32,
1971 .accessfn = pmreg_access_ccntr },
1972 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1973 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1974 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1975 .type = ARM_CP_IO,
1976 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1977 .readfn = pmccntr_read, .writefn = pmccntr_write,
1978 .raw_readfn = raw_read, .raw_writefn = raw_write, },
1979 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1980 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1981 .access = PL0_RW, .accessfn = pmreg_access,
1982 .type = ARM_CP_ALIAS | ARM_CP_IO,
1983 .resetvalue = 0, },
1984 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1985 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1986 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1987 .access = PL0_RW, .accessfn = pmreg_access,
1988 .type = ARM_CP_IO,
1989 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1990 .resetvalue = 0, },
1991 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1992 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1993 .accessfn = pmreg_access,
1994 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1995 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1996 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1997 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1998 .accessfn = pmreg_access,
1999 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2000 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2001 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2002 .accessfn = pmreg_access_xevcntr,
2003 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2004 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2005 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2006 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2007 .accessfn = pmreg_access_xevcntr,
2008 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2009 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2010 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2011 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2012 .resetvalue = 0,
2013 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2014 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2015 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2016 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2017 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2018 .resetvalue = 0,
2019 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2020 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2021 .access = PL1_RW, .accessfn = access_tpm,
2022 .type = ARM_CP_ALIAS | ARM_CP_IO,
2023 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2024 .resetvalue = 0,
2025 .writefn = pmintenset_write, .raw_writefn = raw_write },
2026 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2027 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2028 .access = PL1_RW, .accessfn = access_tpm,
2029 .type = ARM_CP_IO,
2030 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2031 .writefn = pmintenset_write, .raw_writefn = raw_write,
2032 .resetvalue = 0x0 },
2033 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2034 .access = PL1_RW, .accessfn = access_tpm,
2035 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2036 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2037 .writefn = pmintenclr_write, },
2038 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2039 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2040 .access = PL1_RW, .accessfn = access_tpm,
2041 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2042 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2043 .writefn = pmintenclr_write },
2044 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2045 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2046 .access = PL1_R,
2047 .accessfn = access_aa64_tid2,
2048 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2049 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2050 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2051 .access = PL1_RW,
2052 .accessfn = access_aa64_tid2,
2053 .writefn = csselr_write, .resetvalue = 0,
2054 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2055 offsetof(CPUARMState, cp15.csselr_ns) } },
2056 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2057 * just RAZ for all cores:
2059 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2060 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2061 .access = PL1_R, .type = ARM_CP_CONST,
2062 .accessfn = access_aa64_tid1,
2063 .resetvalue = 0 },
2064 /* Auxiliary fault status registers: these also are IMPDEF, and we
2065 * choose to RAZ/WI for all cores.
2067 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2068 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2069 .access = PL1_RW, .accessfn = access_tvm_trvm,
2070 .type = ARM_CP_CONST, .resetvalue = 0 },
2071 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2072 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2073 .access = PL1_RW, .accessfn = access_tvm_trvm,
2074 .type = ARM_CP_CONST, .resetvalue = 0 },
2075 /* MAIR can just read-as-written because we don't implement caches
2076 * and so don't need to care about memory attributes.
2078 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2079 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2080 .access = PL1_RW, .accessfn = access_tvm_trvm,
2081 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2082 .resetvalue = 0 },
2083 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2084 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2085 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2086 .resetvalue = 0 },
2087 /* For non-long-descriptor page tables these are PRRR and NMRR;
2088 * regardless they still act as reads-as-written for QEMU.
2090 /* MAIR0/1 are defined separately from their 64-bit counterpart which
2091 * allows them to assign the correct fieldoffset based on the endianness
2092 * handled in the field definitions.
2094 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2095 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2096 .access = PL1_RW, .accessfn = access_tvm_trvm,
2097 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2098 offsetof(CPUARMState, cp15.mair0_ns) },
2099 .resetfn = arm_cp_reset_ignore },
2100 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2101 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2102 .access = PL1_RW, .accessfn = access_tvm_trvm,
2103 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2104 offsetof(CPUARMState, cp15.mair1_ns) },
2105 .resetfn = arm_cp_reset_ignore },
2106 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2107 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2108 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2109 /* 32 bit ITLB invalidates */
2110 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2111 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2112 .writefn = tlbiall_write },
2113 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2114 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2115 .writefn = tlbimva_write },
2116 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2117 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2118 .writefn = tlbiasid_write },
2119 /* 32 bit DTLB invalidates */
2120 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2121 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2122 .writefn = tlbiall_write },
2123 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2124 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2125 .writefn = tlbimva_write },
2126 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2127 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2128 .writefn = tlbiasid_write },
2129 /* 32 bit TLB invalidates */
2130 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2131 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2132 .writefn = tlbiall_write },
2133 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2134 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2135 .writefn = tlbimva_write },
2136 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2137 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2138 .writefn = tlbiasid_write },
2139 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2140 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2141 .writefn = tlbimvaa_write },
2144 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2145 /* 32 bit TLB invalidates, Inner Shareable */
2146 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2147 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2148 .writefn = tlbiall_is_write },
2149 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2150 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2151 .writefn = tlbimva_is_write },
2152 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2153 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2154 .writefn = tlbiasid_is_write },
2155 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2156 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2157 .writefn = tlbimvaa_is_write },
2160 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2161 /* PMOVSSET is not implemented in v7 before v7ve */
2162 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2163 .access = PL0_RW, .accessfn = pmreg_access,
2164 .type = ARM_CP_ALIAS | ARM_CP_IO,
2165 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2166 .writefn = pmovsset_write,
2167 .raw_writefn = raw_write },
2168 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2169 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2170 .access = PL0_RW, .accessfn = pmreg_access,
2171 .type = ARM_CP_ALIAS | ARM_CP_IO,
2172 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2173 .writefn = pmovsset_write,
2174 .raw_writefn = raw_write },
2177 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2178 uint64_t value)
2180 value &= 1;
2181 env->teecr = value;
2184 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2185 bool isread)
2188 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2189 * at all, so we don't need to check whether we're v8A.
2191 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2192 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2193 return CP_ACCESS_TRAP_EL2;
2195 return CP_ACCESS_OK;
2198 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2199 bool isread)
2201 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2202 return CP_ACCESS_TRAP;
2204 return teecr_access(env, ri, isread);
2207 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2208 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2209 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2210 .resetvalue = 0,
2211 .writefn = teecr_write, .accessfn = teecr_access },
2212 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2213 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2214 .accessfn = teehbr_access, .resetvalue = 0 },
2217 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2218 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2219 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2220 .access = PL0_RW,
2221 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2222 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2223 .access = PL0_RW,
2224 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2225 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2226 .resetfn = arm_cp_reset_ignore },
2227 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2228 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2229 .access = PL0_R|PL1_W,
2230 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2231 .resetvalue = 0},
2232 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2233 .access = PL0_R|PL1_W,
2234 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2235 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2236 .resetfn = arm_cp_reset_ignore },
2237 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2238 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2239 .access = PL1_RW,
2240 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2241 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2242 .access = PL1_RW,
2243 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2244 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2245 .resetvalue = 0 },
2248 #ifndef CONFIG_USER_ONLY
2250 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2251 bool isread)
2253 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2254 * Writable only at the highest implemented exception level.
2256 int el = arm_current_el(env);
2257 uint64_t hcr;
2258 uint32_t cntkctl;
2260 switch (el) {
2261 case 0:
2262 hcr = arm_hcr_el2_eff(env);
2263 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2264 cntkctl = env->cp15.cnthctl_el2;
2265 } else {
2266 cntkctl = env->cp15.c14_cntkctl;
2268 if (!extract32(cntkctl, 0, 2)) {
2269 return CP_ACCESS_TRAP;
2271 break;
2272 case 1:
2273 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2274 arm_is_secure_below_el3(env)) {
2275 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2276 return CP_ACCESS_TRAP_UNCATEGORIZED;
2278 break;
2279 case 2:
2280 case 3:
2281 break;
2284 if (!isread && el < arm_highest_el(env)) {
2285 return CP_ACCESS_TRAP_UNCATEGORIZED;
2288 return CP_ACCESS_OK;
2291 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2292 bool isread)
2294 unsigned int cur_el = arm_current_el(env);
2295 bool has_el2 = arm_is_el2_enabled(env);
2296 uint64_t hcr = arm_hcr_el2_eff(env);
2298 switch (cur_el) {
2299 case 0:
2300 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2301 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2302 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2303 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2306 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2307 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2308 return CP_ACCESS_TRAP;
2311 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2312 if (hcr & HCR_E2H) {
2313 if (timeridx == GTIMER_PHYS &&
2314 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2315 return CP_ACCESS_TRAP_EL2;
2317 } else {
2318 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2319 if (has_el2 && timeridx == GTIMER_PHYS &&
2320 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2321 return CP_ACCESS_TRAP_EL2;
2324 break;
2326 case 1:
2327 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2328 if (has_el2 && timeridx == GTIMER_PHYS &&
2329 (hcr & HCR_E2H
2330 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2331 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2332 return CP_ACCESS_TRAP_EL2;
2334 break;
2336 return CP_ACCESS_OK;
2339 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2340 bool isread)
2342 unsigned int cur_el = arm_current_el(env);
2343 bool has_el2 = arm_is_el2_enabled(env);
2344 uint64_t hcr = arm_hcr_el2_eff(env);
2346 switch (cur_el) {
2347 case 0:
2348 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2349 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2350 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2351 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2355 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2356 * EL0 if EL0[PV]TEN is zero.
2358 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2359 return CP_ACCESS_TRAP;
2361 /* fall through */
2363 case 1:
2364 if (has_el2 && timeridx == GTIMER_PHYS) {
2365 if (hcr & HCR_E2H) {
2366 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2367 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2368 return CP_ACCESS_TRAP_EL2;
2370 } else {
2371 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2372 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2373 return CP_ACCESS_TRAP_EL2;
2377 break;
2379 return CP_ACCESS_OK;
2382 static CPAccessResult gt_pct_access(CPUARMState *env,
2383 const ARMCPRegInfo *ri,
2384 bool isread)
2386 return gt_counter_access(env, GTIMER_PHYS, isread);
2389 static CPAccessResult gt_vct_access(CPUARMState *env,
2390 const ARMCPRegInfo *ri,
2391 bool isread)
2393 return gt_counter_access(env, GTIMER_VIRT, isread);
2396 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2397 bool isread)
2399 return gt_timer_access(env, GTIMER_PHYS, isread);
2402 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2403 bool isread)
2405 return gt_timer_access(env, GTIMER_VIRT, isread);
2408 static CPAccessResult gt_stimer_access(CPUARMState *env,
2409 const ARMCPRegInfo *ri,
2410 bool isread)
2412 /* The AArch64 register view of the secure physical timer is
2413 * always accessible from EL3, and configurably accessible from
2414 * Secure EL1.
2416 switch (arm_current_el(env)) {
2417 case 1:
2418 if (!arm_is_secure(env)) {
2419 return CP_ACCESS_TRAP;
2421 if (!(env->cp15.scr_el3 & SCR_ST)) {
2422 return CP_ACCESS_TRAP_EL3;
2424 return CP_ACCESS_OK;
2425 case 0:
2426 case 2:
2427 return CP_ACCESS_TRAP;
2428 case 3:
2429 return CP_ACCESS_OK;
2430 default:
2431 g_assert_not_reached();
2435 static uint64_t gt_get_countervalue(CPUARMState *env)
2437 ARMCPU *cpu = env_archcpu(env);
2439 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2442 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2444 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2446 if (gt->ctl & 1) {
2447 /* Timer enabled: calculate and set current ISTATUS, irq, and
2448 * reset timer to when ISTATUS next has to change
2450 uint64_t offset = timeridx == GTIMER_VIRT ?
2451 cpu->env.cp15.cntvoff_el2 : 0;
2452 uint64_t count = gt_get_countervalue(&cpu->env);
2453 /* Note that this must be unsigned 64 bit arithmetic: */
2454 int istatus = count - offset >= gt->cval;
2455 uint64_t nexttick;
2456 int irqstate;
2458 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2460 irqstate = (istatus && !(gt->ctl & 2));
2461 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2463 if (istatus) {
2464 /* Next transition is when count rolls back over to zero */
2465 nexttick = UINT64_MAX;
2466 } else {
2467 /* Next transition is when we hit cval */
2468 nexttick = gt->cval + offset;
2470 /* Note that the desired next expiry time might be beyond the
2471 * signed-64-bit range of a QEMUTimer -- in this case we just
2472 * set the timer for as far in the future as possible. When the
2473 * timer expires we will reset the timer for any remaining period.
2475 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2476 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2477 } else {
2478 timer_mod(cpu->gt_timer[timeridx], nexttick);
2480 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2481 } else {
2482 /* Timer disabled: ISTATUS and timer output always clear */
2483 gt->ctl &= ~4;
2484 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2485 timer_del(cpu->gt_timer[timeridx]);
2486 trace_arm_gt_recalc_disabled(timeridx);
2490 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2491 int timeridx)
2493 ARMCPU *cpu = env_archcpu(env);
2495 timer_del(cpu->gt_timer[timeridx]);
2498 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2500 return gt_get_countervalue(env);
2503 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2505 uint64_t hcr;
2507 switch (arm_current_el(env)) {
2508 case 2:
2509 hcr = arm_hcr_el2_eff(env);
2510 if (hcr & HCR_E2H) {
2511 return 0;
2513 break;
2514 case 0:
2515 hcr = arm_hcr_el2_eff(env);
2516 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2517 return 0;
2519 break;
2522 return env->cp15.cntvoff_el2;
2525 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2527 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2530 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2531 int timeridx,
2532 uint64_t value)
2534 trace_arm_gt_cval_write(timeridx, value);
2535 env->cp15.c14_timer[timeridx].cval = value;
2536 gt_recalc_timer(env_archcpu(env), timeridx);
2539 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2540 int timeridx)
2542 uint64_t offset = 0;
2544 switch (timeridx) {
2545 case GTIMER_VIRT:
2546 case GTIMER_HYPVIRT:
2547 offset = gt_virt_cnt_offset(env);
2548 break;
2551 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2552 (gt_get_countervalue(env) - offset));
2555 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2556 int timeridx,
2557 uint64_t value)
2559 uint64_t offset = 0;
2561 switch (timeridx) {
2562 case GTIMER_VIRT:
2563 case GTIMER_HYPVIRT:
2564 offset = gt_virt_cnt_offset(env);
2565 break;
2568 trace_arm_gt_tval_write(timeridx, value);
2569 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2570 sextract64(value, 0, 32);
2571 gt_recalc_timer(env_archcpu(env), timeridx);
2574 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2575 int timeridx,
2576 uint64_t value)
2578 ARMCPU *cpu = env_archcpu(env);
2579 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2581 trace_arm_gt_ctl_write(timeridx, value);
2582 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2583 if ((oldval ^ value) & 1) {
2584 /* Enable toggled */
2585 gt_recalc_timer(cpu, timeridx);
2586 } else if ((oldval ^ value) & 2) {
2587 /* IMASK toggled: don't need to recalculate,
2588 * just set the interrupt line based on ISTATUS
2590 int irqstate = (oldval & 4) && !(value & 2);
2592 trace_arm_gt_imask_toggle(timeridx, irqstate);
2593 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2597 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2599 gt_timer_reset(env, ri, GTIMER_PHYS);
2602 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2603 uint64_t value)
2605 gt_cval_write(env, ri, GTIMER_PHYS, value);
2608 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2610 return gt_tval_read(env, ri, GTIMER_PHYS);
2613 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2614 uint64_t value)
2616 gt_tval_write(env, ri, GTIMER_PHYS, value);
2619 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2620 uint64_t value)
2622 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2625 static int gt_phys_redir_timeridx(CPUARMState *env)
2627 switch (arm_mmu_idx(env)) {
2628 case ARMMMUIdx_E20_0:
2629 case ARMMMUIdx_E20_2:
2630 case ARMMMUIdx_E20_2_PAN:
2631 case ARMMMUIdx_SE20_0:
2632 case ARMMMUIdx_SE20_2:
2633 case ARMMMUIdx_SE20_2_PAN:
2634 return GTIMER_HYP;
2635 default:
2636 return GTIMER_PHYS;
2640 static int gt_virt_redir_timeridx(CPUARMState *env)
2642 switch (arm_mmu_idx(env)) {
2643 case ARMMMUIdx_E20_0:
2644 case ARMMMUIdx_E20_2:
2645 case ARMMMUIdx_E20_2_PAN:
2646 case ARMMMUIdx_SE20_0:
2647 case ARMMMUIdx_SE20_2:
2648 case ARMMMUIdx_SE20_2_PAN:
2649 return GTIMER_HYPVIRT;
2650 default:
2651 return GTIMER_VIRT;
2655 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2656 const ARMCPRegInfo *ri)
2658 int timeridx = gt_phys_redir_timeridx(env);
2659 return env->cp15.c14_timer[timeridx].cval;
2662 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2663 uint64_t value)
2665 int timeridx = gt_phys_redir_timeridx(env);
2666 gt_cval_write(env, ri, timeridx, value);
2669 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2670 const ARMCPRegInfo *ri)
2672 int timeridx = gt_phys_redir_timeridx(env);
2673 return gt_tval_read(env, ri, timeridx);
2676 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2677 uint64_t value)
2679 int timeridx = gt_phys_redir_timeridx(env);
2680 gt_tval_write(env, ri, timeridx, value);
2683 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2684 const ARMCPRegInfo *ri)
2686 int timeridx = gt_phys_redir_timeridx(env);
2687 return env->cp15.c14_timer[timeridx].ctl;
2690 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2691 uint64_t value)
2693 int timeridx = gt_phys_redir_timeridx(env);
2694 gt_ctl_write(env, ri, timeridx, value);
2697 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2699 gt_timer_reset(env, ri, GTIMER_VIRT);
2702 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2703 uint64_t value)
2705 gt_cval_write(env, ri, GTIMER_VIRT, value);
2708 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2710 return gt_tval_read(env, ri, GTIMER_VIRT);
2713 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2714 uint64_t value)
2716 gt_tval_write(env, ri, GTIMER_VIRT, value);
2719 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2720 uint64_t value)
2722 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2725 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2726 uint64_t value)
2728 ARMCPU *cpu = env_archcpu(env);
2730 trace_arm_gt_cntvoff_write(value);
2731 raw_write(env, ri, value);
2732 gt_recalc_timer(cpu, GTIMER_VIRT);
2735 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2736 const ARMCPRegInfo *ri)
2738 int timeridx = gt_virt_redir_timeridx(env);
2739 return env->cp15.c14_timer[timeridx].cval;
2742 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2743 uint64_t value)
2745 int timeridx = gt_virt_redir_timeridx(env);
2746 gt_cval_write(env, ri, timeridx, value);
2749 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2750 const ARMCPRegInfo *ri)
2752 int timeridx = gt_virt_redir_timeridx(env);
2753 return gt_tval_read(env, ri, timeridx);
2756 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2757 uint64_t value)
2759 int timeridx = gt_virt_redir_timeridx(env);
2760 gt_tval_write(env, ri, timeridx, value);
2763 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2764 const ARMCPRegInfo *ri)
2766 int timeridx = gt_virt_redir_timeridx(env);
2767 return env->cp15.c14_timer[timeridx].ctl;
2770 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2771 uint64_t value)
2773 int timeridx = gt_virt_redir_timeridx(env);
2774 gt_ctl_write(env, ri, timeridx, value);
2777 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2779 gt_timer_reset(env, ri, GTIMER_HYP);
2782 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2783 uint64_t value)
2785 gt_cval_write(env, ri, GTIMER_HYP, value);
2788 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2790 return gt_tval_read(env, ri, GTIMER_HYP);
2793 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2794 uint64_t value)
2796 gt_tval_write(env, ri, GTIMER_HYP, value);
2799 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2800 uint64_t value)
2802 gt_ctl_write(env, ri, GTIMER_HYP, value);
2805 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2807 gt_timer_reset(env, ri, GTIMER_SEC);
2810 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2811 uint64_t value)
2813 gt_cval_write(env, ri, GTIMER_SEC, value);
2816 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2818 return gt_tval_read(env, ri, GTIMER_SEC);
2821 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2822 uint64_t value)
2824 gt_tval_write(env, ri, GTIMER_SEC, value);
2827 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2828 uint64_t value)
2830 gt_ctl_write(env, ri, GTIMER_SEC, value);
2833 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2835 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2838 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2839 uint64_t value)
2841 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2844 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2846 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2849 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2850 uint64_t value)
2852 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2855 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2856 uint64_t value)
2858 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2861 void arm_gt_ptimer_cb(void *opaque)
2863 ARMCPU *cpu = opaque;
2865 gt_recalc_timer(cpu, GTIMER_PHYS);
2868 void arm_gt_vtimer_cb(void *opaque)
2870 ARMCPU *cpu = opaque;
2872 gt_recalc_timer(cpu, GTIMER_VIRT);
2875 void arm_gt_htimer_cb(void *opaque)
2877 ARMCPU *cpu = opaque;
2879 gt_recalc_timer(cpu, GTIMER_HYP);
2882 void arm_gt_stimer_cb(void *opaque)
2884 ARMCPU *cpu = opaque;
2886 gt_recalc_timer(cpu, GTIMER_SEC);
2889 void arm_gt_hvtimer_cb(void *opaque)
2891 ARMCPU *cpu = opaque;
2893 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2896 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2898 ARMCPU *cpu = env_archcpu(env);
2900 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2903 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2904 /* Note that CNTFRQ is purely reads-as-written for the benefit
2905 * of software; writing it doesn't actually change the timer frequency.
2906 * Our reset value matches the fixed frequency we implement the timer at.
2908 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2909 .type = ARM_CP_ALIAS,
2910 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2911 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2913 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2914 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2915 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2916 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2917 .resetfn = arm_gt_cntfrq_reset,
2919 /* overall control: mostly access permissions */
2920 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2921 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2922 .access = PL1_RW,
2923 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2924 .resetvalue = 0,
2926 /* per-timer control */
2927 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2928 .secure = ARM_CP_SECSTATE_NS,
2929 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2930 .accessfn = gt_ptimer_access,
2931 .fieldoffset = offsetoflow32(CPUARMState,
2932 cp15.c14_timer[GTIMER_PHYS].ctl),
2933 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2934 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2936 { .name = "CNTP_CTL_S",
2937 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2938 .secure = ARM_CP_SECSTATE_S,
2939 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2940 .accessfn = gt_ptimer_access,
2941 .fieldoffset = offsetoflow32(CPUARMState,
2942 cp15.c14_timer[GTIMER_SEC].ctl),
2943 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2945 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2946 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2947 .type = ARM_CP_IO, .access = PL0_RW,
2948 .accessfn = gt_ptimer_access,
2949 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2950 .resetvalue = 0,
2951 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2952 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2954 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2955 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2956 .accessfn = gt_vtimer_access,
2957 .fieldoffset = offsetoflow32(CPUARMState,
2958 cp15.c14_timer[GTIMER_VIRT].ctl),
2959 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2960 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2962 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2963 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2964 .type = ARM_CP_IO, .access = PL0_RW,
2965 .accessfn = gt_vtimer_access,
2966 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2967 .resetvalue = 0,
2968 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2969 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2971 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2972 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2973 .secure = ARM_CP_SECSTATE_NS,
2974 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2975 .accessfn = gt_ptimer_access,
2976 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2978 { .name = "CNTP_TVAL_S",
2979 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2980 .secure = ARM_CP_SECSTATE_S,
2981 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2982 .accessfn = gt_ptimer_access,
2983 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2985 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2986 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2987 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2988 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2989 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2991 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2992 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2993 .accessfn = gt_vtimer_access,
2994 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
2996 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2997 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2998 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2999 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3000 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3002 /* The counter itself */
3003 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3004 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3005 .accessfn = gt_pct_access,
3006 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3008 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3009 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3010 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3011 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3013 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3014 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3015 .accessfn = gt_vct_access,
3016 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3018 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3019 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3020 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3021 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3023 /* Comparison value, indicating when the timer goes off */
3024 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3025 .secure = ARM_CP_SECSTATE_NS,
3026 .access = PL0_RW,
3027 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3028 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3029 .accessfn = gt_ptimer_access,
3030 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3031 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3033 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3034 .secure = ARM_CP_SECSTATE_S,
3035 .access = PL0_RW,
3036 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3037 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3038 .accessfn = gt_ptimer_access,
3039 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3041 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3042 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3043 .access = PL0_RW,
3044 .type = ARM_CP_IO,
3045 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3046 .resetvalue = 0, .accessfn = gt_ptimer_access,
3047 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3048 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3050 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3051 .access = PL0_RW,
3052 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3053 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3054 .accessfn = gt_vtimer_access,
3055 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3056 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3058 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3059 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3060 .access = PL0_RW,
3061 .type = ARM_CP_IO,
3062 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3063 .resetvalue = 0, .accessfn = gt_vtimer_access,
3064 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3065 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3067 /* Secure timer -- this is actually restricted to only EL3
3068 * and configurably Secure-EL1 via the accessfn.
3070 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3071 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3072 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3073 .accessfn = gt_stimer_access,
3074 .readfn = gt_sec_tval_read,
3075 .writefn = gt_sec_tval_write,
3076 .resetfn = gt_sec_timer_reset,
3078 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3079 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3080 .type = ARM_CP_IO, .access = PL1_RW,
3081 .accessfn = gt_stimer_access,
3082 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3083 .resetvalue = 0,
3084 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3086 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3087 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3088 .type = ARM_CP_IO, .access = PL1_RW,
3089 .accessfn = gt_stimer_access,
3090 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3091 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3095 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3096 bool isread)
3098 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3099 return CP_ACCESS_TRAP;
3101 return CP_ACCESS_OK;
3104 #else
3106 /* In user-mode most of the generic timer registers are inaccessible
3107 * however modern kernels (4.12+) allow access to cntvct_el0
3110 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3112 ARMCPU *cpu = env_archcpu(env);
3114 /* Currently we have no support for QEMUTimer in linux-user so we
3115 * can't call gt_get_countervalue(env), instead we directly
3116 * call the lower level functions.
3118 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3121 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3122 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3123 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3124 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3125 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3126 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3128 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3129 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3130 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3131 .readfn = gt_virt_cnt_read,
3135 #endif
3137 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3139 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3140 raw_write(env, ri, value);
3141 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3142 raw_write(env, ri, value & 0xfffff6ff);
3143 } else {
3144 raw_write(env, ri, value & 0xfffff1ff);
3148 #ifndef CONFIG_USER_ONLY
3149 /* get_phys_addr() isn't present for user-mode-only targets */
3151 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3152 bool isread)
3154 if (ri->opc2 & 4) {
3155 /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3156 * Secure EL1 (which can only happen if EL3 is AArch64).
3157 * They are simply UNDEF if executed from NS EL1.
3158 * They function normally from EL2 or EL3.
3160 if (arm_current_el(env) == 1) {
3161 if (arm_is_secure_below_el3(env)) {
3162 if (env->cp15.scr_el3 & SCR_EEL2) {
3163 return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3165 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3167 return CP_ACCESS_TRAP_UNCATEGORIZED;
3170 return CP_ACCESS_OK;
3173 #ifdef CONFIG_TCG
3174 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3175 MMUAccessType access_type, ARMMMUIdx mmu_idx)
3177 hwaddr phys_addr;
3178 target_ulong page_size;
3179 int prot;
3180 bool ret;
3181 uint64_t par64;
3182 bool format64 = false;
3183 MemTxAttrs attrs = {};
3184 ARMMMUFaultInfo fi = {};
3185 ARMCacheAttrs cacheattrs = {};
3187 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
3188 &prot, &page_size, &fi, &cacheattrs);
3191 * ATS operations only do S1 or S1+S2 translations, so we never
3192 * have to deal with the ARMCacheAttrs format for S2 only.
3194 assert(!cacheattrs.is_s2_format);
3196 if (ret) {
3198 * Some kinds of translation fault must cause exceptions rather
3199 * than being reported in the PAR.
3201 int current_el = arm_current_el(env);
3202 int target_el;
3203 uint32_t syn, fsr, fsc;
3204 bool take_exc = false;
3206 if (fi.s1ptw && current_el == 1
3207 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3209 * Synchronous stage 2 fault on an access made as part of the
3210 * translation table walk for AT S1E0* or AT S1E1* insn
3211 * executed from NS EL1. If this is a synchronous external abort
3212 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3213 * to EL3. Otherwise the fault is taken as an exception to EL2,
3214 * and HPFAR_EL2 holds the faulting IPA.
3216 if (fi.type == ARMFault_SyncExternalOnWalk &&
3217 (env->cp15.scr_el3 & SCR_EA)) {
3218 target_el = 3;
3219 } else {
3220 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3221 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3222 env->cp15.hpfar_el2 |= HPFAR_NS;
3224 target_el = 2;
3226 take_exc = true;
3227 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3229 * Synchronous external aborts during a translation table walk
3230 * are taken as Data Abort exceptions.
3232 if (fi.stage2) {
3233 if (current_el == 3) {
3234 target_el = 3;
3235 } else {
3236 target_el = 2;
3238 } else {
3239 target_el = exception_target_el(env);
3241 take_exc = true;
3244 if (take_exc) {
3245 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3246 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3247 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3248 fsr = arm_fi_to_lfsc(&fi);
3249 fsc = extract32(fsr, 0, 6);
3250 } else {
3251 fsr = arm_fi_to_sfsc(&fi);
3252 fsc = 0x3f;
3255 * Report exception with ESR indicating a fault due to a
3256 * translation table walk for a cache maintenance instruction.
3258 syn = syn_data_abort_no_iss(current_el == target_el, 0,
3259 fi.ea, 1, fi.s1ptw, 1, fsc);
3260 env->exception.vaddress = value;
3261 env->exception.fsr = fsr;
3262 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3266 if (is_a64(env)) {
3267 format64 = true;
3268 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3270 * ATS1Cxx:
3271 * * TTBCR.EAE determines whether the result is returned using the
3272 * 32-bit or the 64-bit PAR format
3273 * * Instructions executed in Hyp mode always use the 64bit format
3275 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3276 * * The Non-secure TTBCR.EAE bit is set to 1
3277 * * The implementation includes EL2, and the value of HCR.VM is 1
3279 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3281 * ATS1Hx always uses the 64bit format.
3283 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3285 if (arm_feature(env, ARM_FEATURE_EL2)) {
3286 if (mmu_idx == ARMMMUIdx_E10_0 ||
3287 mmu_idx == ARMMMUIdx_E10_1 ||
3288 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3289 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3290 } else {
3291 format64 |= arm_current_el(env) == 2;
3296 if (format64) {
3297 /* Create a 64-bit PAR */
3298 par64 = (1 << 11); /* LPAE bit always set */
3299 if (!ret) {
3300 par64 |= phys_addr & ~0xfffULL;
3301 if (!attrs.secure) {
3302 par64 |= (1 << 9); /* NS */
3304 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3305 par64 |= cacheattrs.shareability << 7; /* SH */
3306 } else {
3307 uint32_t fsr = arm_fi_to_lfsc(&fi);
3309 par64 |= 1; /* F */
3310 par64 |= (fsr & 0x3f) << 1; /* FS */
3311 if (fi.stage2) {
3312 par64 |= (1 << 9); /* S */
3314 if (fi.s1ptw) {
3315 par64 |= (1 << 8); /* PTW */
3318 } else {
3319 /* fsr is a DFSR/IFSR value for the short descriptor
3320 * translation table format (with WnR always clear).
3321 * Convert it to a 32-bit PAR.
3323 if (!ret) {
3324 /* We do not set any attribute bits in the PAR */
3325 if (page_size == (1 << 24)
3326 && arm_feature(env, ARM_FEATURE_V7)) {
3327 par64 = (phys_addr & 0xff000000) | (1 << 1);
3328 } else {
3329 par64 = phys_addr & 0xfffff000;
3331 if (!attrs.secure) {
3332 par64 |= (1 << 9); /* NS */
3334 } else {
3335 uint32_t fsr = arm_fi_to_sfsc(&fi);
3337 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3338 ((fsr & 0xf) << 1) | 1;
3341 return par64;
3343 #endif /* CONFIG_TCG */
3345 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3347 #ifdef CONFIG_TCG
3348 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3349 uint64_t par64;
3350 ARMMMUIdx mmu_idx;
3351 int el = arm_current_el(env);
3352 bool secure = arm_is_secure_below_el3(env);
3354 switch (ri->opc2 & 6) {
3355 case 0:
3356 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3357 switch (el) {
3358 case 3:
3359 mmu_idx = ARMMMUIdx_SE3;
3360 break;
3361 case 2:
3362 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
3363 /* fall through */
3364 case 1:
3365 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3366 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3367 : ARMMMUIdx_Stage1_E1_PAN);
3368 } else {
3369 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3371 break;
3372 default:
3373 g_assert_not_reached();
3375 break;
3376 case 2:
3377 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3378 switch (el) {
3379 case 3:
3380 mmu_idx = ARMMMUIdx_SE10_0;
3381 break;
3382 case 2:
3383 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
3384 mmu_idx = ARMMMUIdx_Stage1_E0;
3385 break;
3386 case 1:
3387 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3388 break;
3389 default:
3390 g_assert_not_reached();
3392 break;
3393 case 4:
3394 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3395 mmu_idx = ARMMMUIdx_E10_1;
3396 break;
3397 case 6:
3398 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3399 mmu_idx = ARMMMUIdx_E10_0;
3400 break;
3401 default:
3402 g_assert_not_reached();
3405 par64 = do_ats_write(env, value, access_type, mmu_idx);
3407 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3408 #else
3409 /* Handled by hardware accelerator. */
3410 g_assert_not_reached();
3411 #endif /* CONFIG_TCG */
3414 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3415 uint64_t value)
3417 #ifdef CONFIG_TCG
3418 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3419 uint64_t par64;
3421 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
3423 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3424 #else
3425 /* Handled by hardware accelerator. */
3426 g_assert_not_reached();
3427 #endif /* CONFIG_TCG */
3430 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3431 bool isread)
3433 if (arm_current_el(env) == 3 &&
3434 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3435 return CP_ACCESS_TRAP;
3437 return CP_ACCESS_OK;
3440 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3441 uint64_t value)
3443 #ifdef CONFIG_TCG
3444 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3445 ARMMMUIdx mmu_idx;
3446 int secure = arm_is_secure_below_el3(env);
3448 switch (ri->opc2 & 6) {
3449 case 0:
3450 switch (ri->opc1) {
3451 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3452 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3453 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3454 : ARMMMUIdx_Stage1_E1_PAN);
3455 } else {
3456 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3458 break;
3459 case 4: /* AT S1E2R, AT S1E2W */
3460 mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
3461 break;
3462 case 6: /* AT S1E3R, AT S1E3W */
3463 mmu_idx = ARMMMUIdx_SE3;
3464 break;
3465 default:
3466 g_assert_not_reached();
3468 break;
3469 case 2: /* AT S1E0R, AT S1E0W */
3470 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3471 break;
3472 case 4: /* AT S12E1R, AT S12E1W */
3473 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
3474 break;
3475 case 6: /* AT S12E0R, AT S12E0W */
3476 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
3477 break;
3478 default:
3479 g_assert_not_reached();
3482 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3483 #else
3484 /* Handled by hardware accelerator. */
3485 g_assert_not_reached();
3486 #endif /* CONFIG_TCG */
3488 #endif
3490 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3491 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3492 .access = PL1_RW, .resetvalue = 0,
3493 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3494 offsetoflow32(CPUARMState, cp15.par_ns) },
3495 .writefn = par_write },
3496 #ifndef CONFIG_USER_ONLY
3497 /* This underdecoding is safe because the reginfo is NO_RAW. */
3498 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3499 .access = PL1_W, .accessfn = ats_access,
3500 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3501 #endif
3504 /* Return basic MPU access permission bits. */
3505 static uint32_t simple_mpu_ap_bits(uint32_t val)
3507 uint32_t ret;
3508 uint32_t mask;
3509 int i;
3510 ret = 0;
3511 mask = 3;
3512 for (i = 0; i < 16; i += 2) {
3513 ret |= (val >> i) & mask;
3514 mask <<= 2;
3516 return ret;
3519 /* Pad basic MPU access permission bits to extended format. */
3520 static uint32_t extended_mpu_ap_bits(uint32_t val)
3522 uint32_t ret;
3523 uint32_t mask;
3524 int i;
3525 ret = 0;
3526 mask = 3;
3527 for (i = 0; i < 16; i += 2) {
3528 ret |= (val & mask) << i;
3529 mask <<= 2;
3531 return ret;
3534 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3535 uint64_t value)
3537 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3540 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3542 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3545 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3546 uint64_t value)
3548 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3551 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3553 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3556 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3558 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3560 if (!u32p) {
3561 return 0;
3564 u32p += env->pmsav7.rnr[M_REG_NS];
3565 return *u32p;
3568 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3569 uint64_t value)
3571 ARMCPU *cpu = env_archcpu(env);
3572 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3574 if (!u32p) {
3575 return;
3578 u32p += env->pmsav7.rnr[M_REG_NS];
3579 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3580 *u32p = value;
3583 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3584 uint64_t value)
3586 ARMCPU *cpu = env_archcpu(env);
3587 uint32_t nrgs = cpu->pmsav7_dregion;
3589 if (value >= nrgs) {
3590 qemu_log_mask(LOG_GUEST_ERROR,
3591 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3592 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3593 return;
3596 raw_write(env, ri, value);
3599 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3600 /* Reset for all these registers is handled in arm_cpu_reset(),
3601 * because the PMSAv7 is also used by M-profile CPUs, which do
3602 * not register cpregs but still need the state to be reset.
3604 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3605 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3606 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3607 .readfn = pmsav7_read, .writefn = pmsav7_write,
3608 .resetfn = arm_cp_reset_ignore },
3609 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3610 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3611 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3612 .readfn = pmsav7_read, .writefn = pmsav7_write,
3613 .resetfn = arm_cp_reset_ignore },
3614 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3615 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3616 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3617 .readfn = pmsav7_read, .writefn = pmsav7_write,
3618 .resetfn = arm_cp_reset_ignore },
3619 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3620 .access = PL1_RW,
3621 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3622 .writefn = pmsav7_rgnr_write,
3623 .resetfn = arm_cp_reset_ignore },
3626 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3627 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3628 .access = PL1_RW, .type = ARM_CP_ALIAS,
3629 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3630 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3631 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3632 .access = PL1_RW, .type = ARM_CP_ALIAS,
3633 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3634 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3635 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3636 .access = PL1_RW,
3637 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3638 .resetvalue = 0, },
3639 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3640 .access = PL1_RW,
3641 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3642 .resetvalue = 0, },
3643 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3644 .access = PL1_RW,
3645 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3646 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3647 .access = PL1_RW,
3648 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3649 /* Protection region base and size registers */
3650 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3651 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3652 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3653 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3654 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3655 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3656 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3657 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3658 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3659 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3660 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3661 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3662 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3663 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3664 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3665 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3666 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3667 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3668 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3669 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3670 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3671 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3672 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3673 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3676 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3677 uint64_t value)
3679 TCR *tcr = raw_ptr(env, ri);
3680 int maskshift = extract32(value, 0, 3);
3682 if (!arm_feature(env, ARM_FEATURE_V8)) {
3683 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3684 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3685 * using Long-desciptor translation table format */
3686 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3687 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3688 /* In an implementation that includes the Security Extensions
3689 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3690 * Short-descriptor translation table format.
3692 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3693 } else {
3694 value &= TTBCR_N;
3698 /* Update the masks corresponding to the TCR bank being written
3699 * Note that we always calculate mask and base_mask, but
3700 * they are only used for short-descriptor tables (ie if EAE is 0);
3701 * for long-descriptor tables the TCR fields are used differently
3702 * and the mask and base_mask values are meaningless.
3704 tcr->raw_tcr = value;
3705 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3706 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
3709 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3710 uint64_t value)
3712 ARMCPU *cpu = env_archcpu(env);
3713 TCR *tcr = raw_ptr(env, ri);
3715 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3716 /* With LPAE the TTBCR could result in a change of ASID
3717 * via the TTBCR.A1 bit, so do a TLB flush.
3719 tlb_flush(CPU(cpu));
3721 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3722 value = deposit64(tcr->raw_tcr, 0, 32, value);
3723 vmsa_ttbcr_raw_write(env, ri, value);
3726 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3728 TCR *tcr = raw_ptr(env, ri);
3730 /* Reset both the TCR as well as the masks corresponding to the bank of
3731 * the TCR being reset.
3733 tcr->raw_tcr = 0;
3734 tcr->mask = 0;
3735 tcr->base_mask = 0xffffc000u;
3738 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3739 uint64_t value)
3741 ARMCPU *cpu = env_archcpu(env);
3742 TCR *tcr = raw_ptr(env, ri);
3744 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3745 tlb_flush(CPU(cpu));
3746 tcr->raw_tcr = value;
3749 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3750 uint64_t value)
3752 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3753 if (cpreg_field_is_64bit(ri) &&
3754 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3755 ARMCPU *cpu = env_archcpu(env);
3756 tlb_flush(CPU(cpu));
3758 raw_write(env, ri, value);
3761 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3762 uint64_t value)
3765 * If we are running with E2&0 regime, then an ASID is active.
3766 * Flush if that might be changing. Note we're not checking
3767 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3768 * holds the active ASID, only checking the field that might.
3770 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3771 (arm_hcr_el2_eff(env) & HCR_E2H)) {
3772 uint16_t mask = ARMMMUIdxBit_E20_2 |
3773 ARMMMUIdxBit_E20_2_PAN |
3774 ARMMMUIdxBit_E20_0;
3776 if (arm_is_secure_below_el3(env)) {
3777 mask >>= ARM_MMU_IDX_A_NS;
3780 tlb_flush_by_mmuidx(env_cpu(env), mask);
3782 raw_write(env, ri, value);
3785 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3786 uint64_t value)
3788 ARMCPU *cpu = env_archcpu(env);
3789 CPUState *cs = CPU(cpu);
3792 * A change in VMID to the stage2 page table (Stage2) invalidates
3793 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3795 if (raw_read(env, ri) != value) {
3796 uint16_t mask = ARMMMUIdxBit_E10_1 |
3797 ARMMMUIdxBit_E10_1_PAN |
3798 ARMMMUIdxBit_E10_0;
3800 if (arm_is_secure_below_el3(env)) {
3801 mask >>= ARM_MMU_IDX_A_NS;
3804 tlb_flush_by_mmuidx(cs, mask);
3805 raw_write(env, ri, value);
3809 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3810 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3811 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
3812 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3813 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3814 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3815 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3816 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3817 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3818 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3819 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3820 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3821 offsetof(CPUARMState, cp15.dfar_ns) } },
3822 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3823 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3824 .access = PL1_RW, .accessfn = access_tvm_trvm,
3825 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3826 .resetvalue = 0, },
3829 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3830 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3831 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3832 .access = PL1_RW, .accessfn = access_tvm_trvm,
3833 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3834 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3835 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3836 .access = PL1_RW, .accessfn = access_tvm_trvm,
3837 .writefn = vmsa_ttbr_write, .resetvalue = 0,
3838 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3839 offsetof(CPUARMState, cp15.ttbr0_ns) } },
3840 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3841 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3842 .access = PL1_RW, .accessfn = access_tvm_trvm,
3843 .writefn = vmsa_ttbr_write, .resetvalue = 0,
3844 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3845 offsetof(CPUARMState, cp15.ttbr1_ns) } },
3846 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3847 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3848 .access = PL1_RW, .accessfn = access_tvm_trvm,
3849 .writefn = vmsa_tcr_el12_write,
3850 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3851 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3852 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3853 .access = PL1_RW, .accessfn = access_tvm_trvm,
3854 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3855 .raw_writefn = vmsa_ttbcr_raw_write,
3856 /* No offsetoflow32 -- pass the entire TCR to writefn/raw_writefn. */
3857 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.tcr_el[3]),
3858 offsetof(CPUARMState, cp15.tcr_el[1])} },
3861 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3862 * qemu tlbs nor adjusting cached masks.
3864 static const ARMCPRegInfo ttbcr2_reginfo = {
3865 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3866 .access = PL1_RW, .accessfn = access_tvm_trvm,
3867 .type = ARM_CP_ALIAS,
3868 .bank_fieldoffsets = {
3869 offsetofhigh32(CPUARMState, cp15.tcr_el[3].raw_tcr),
3870 offsetofhigh32(CPUARMState, cp15.tcr_el[1].raw_tcr),
3874 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3875 uint64_t value)
3877 env->cp15.c15_ticonfig = value & 0xe7;
3878 /* The OS_TYPE bit in this register changes the reported CPUID! */
3879 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3880 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3883 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3884 uint64_t value)
3886 env->cp15.c15_threadid = value & 0xffff;
3889 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3890 uint64_t value)
3892 /* Wait-for-interrupt (deprecated) */
3893 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3896 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3897 uint64_t value)
3899 /* On OMAP there are registers indicating the max/min index of dcache lines
3900 * containing a dirty line; cache flush operations have to reset these.
3902 env->cp15.c15_i_max = 0x000;
3903 env->cp15.c15_i_min = 0xff0;
3906 static const ARMCPRegInfo omap_cp_reginfo[] = {
3907 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3908 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3909 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3910 .resetvalue = 0, },
3911 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3912 .access = PL1_RW, .type = ARM_CP_NOP },
3913 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3914 .access = PL1_RW,
3915 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3916 .writefn = omap_ticonfig_write },
3917 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3918 .access = PL1_RW,
3919 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3920 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3921 .access = PL1_RW, .resetvalue = 0xff0,
3922 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3923 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3924 .access = PL1_RW,
3925 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3926 .writefn = omap_threadid_write },
3927 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3928 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3929 .type = ARM_CP_NO_RAW,
3930 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3931 /* TODO: Peripheral port remap register:
3932 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3933 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3934 * when MMU is off.
3936 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3937 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3938 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3939 .writefn = omap_cachemaint_write },
3940 { .name = "C9", .cp = 15, .crn = 9,
3941 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3942 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3945 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3946 uint64_t value)
3948 env->cp15.c15_cpar = value & 0x3fff;
3951 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3952 { .name = "XSCALE_CPAR",
3953 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3954 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3955 .writefn = xscale_cpar_write, },
3956 { .name = "XSCALE_AUXCR",
3957 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3958 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3959 .resetvalue = 0, },
3960 /* XScale specific cache-lockdown: since we have no cache we NOP these
3961 * and hope the guest does not really rely on cache behaviour.
3963 { .name = "XSCALE_LOCK_ICACHE_LINE",
3964 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3965 .access = PL1_W, .type = ARM_CP_NOP },
3966 { .name = "XSCALE_UNLOCK_ICACHE",
3967 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3968 .access = PL1_W, .type = ARM_CP_NOP },
3969 { .name = "XSCALE_DCACHE_LOCK",
3970 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3971 .access = PL1_RW, .type = ARM_CP_NOP },
3972 { .name = "XSCALE_UNLOCK_DCACHE",
3973 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3974 .access = PL1_W, .type = ARM_CP_NOP },
3977 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3978 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3979 * implementation of this implementation-defined space.
3980 * Ideally this should eventually disappear in favour of actually
3981 * implementing the correct behaviour for all cores.
3983 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3984 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3985 .access = PL1_RW,
3986 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3987 .resetvalue = 0 },
3990 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3991 /* Cache status: RAZ because we have no cache so it's always clean */
3992 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3993 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3994 .resetvalue = 0 },
3997 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3998 /* We never have a a block transfer operation in progress */
3999 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4000 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4001 .resetvalue = 0 },
4002 /* The cache ops themselves: these all NOP for QEMU */
4003 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4004 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4005 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4006 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4007 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4008 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4009 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4010 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4011 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4012 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4013 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4014 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4017 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4018 /* The cache test-and-clean instructions always return (1 << 30)
4019 * to indicate that there are no dirty cache lines.
4021 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4022 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4023 .resetvalue = (1 << 30) },
4024 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4025 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4026 .resetvalue = (1 << 30) },
4029 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4030 /* Ignore ReadBuffer accesses */
4031 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4032 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4033 .access = PL1_RW, .resetvalue = 0,
4034 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4037 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4039 unsigned int cur_el = arm_current_el(env);
4041 if (arm_is_el2_enabled(env) && cur_el == 1) {
4042 return env->cp15.vpidr_el2;
4044 return raw_read(env, ri);
4047 static uint64_t mpidr_read_val(CPUARMState *env)
4049 ARMCPU *cpu = env_archcpu(env);
4050 uint64_t mpidr = cpu->mp_affinity;
4052 if (arm_feature(env, ARM_FEATURE_V7MP)) {
4053 mpidr |= (1U << 31);
4054 /* Cores which are uniprocessor (non-coherent)
4055 * but still implement the MP extensions set
4056 * bit 30. (For instance, Cortex-R5).
4058 if (cpu->mp_is_up) {
4059 mpidr |= (1u << 30);
4062 return mpidr;
4065 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4067 unsigned int cur_el = arm_current_el(env);
4069 if (arm_is_el2_enabled(env) && cur_el == 1) {
4070 return env->cp15.vmpidr_el2;
4072 return mpidr_read_val(env);
4075 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4076 /* NOP AMAIR0/1 */
4077 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4078 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4079 .access = PL1_RW, .accessfn = access_tvm_trvm,
4080 .type = ARM_CP_CONST, .resetvalue = 0 },
4081 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4082 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4083 .access = PL1_RW, .accessfn = access_tvm_trvm,
4084 .type = ARM_CP_CONST, .resetvalue = 0 },
4085 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4086 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4087 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4088 offsetof(CPUARMState, cp15.par_ns)} },
4089 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4090 .access = PL1_RW, .accessfn = access_tvm_trvm,
4091 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4092 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4093 offsetof(CPUARMState, cp15.ttbr0_ns) },
4094 .writefn = vmsa_ttbr_write, },
4095 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4096 .access = PL1_RW, .accessfn = access_tvm_trvm,
4097 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4098 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4099 offsetof(CPUARMState, cp15.ttbr1_ns) },
4100 .writefn = vmsa_ttbr_write, },
4103 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4105 return vfp_get_fpcr(env);
4108 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4109 uint64_t value)
4111 vfp_set_fpcr(env, value);
4114 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4116 return vfp_get_fpsr(env);
4119 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4120 uint64_t value)
4122 vfp_set_fpsr(env, value);
4125 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4126 bool isread)
4128 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4129 return CP_ACCESS_TRAP;
4131 return CP_ACCESS_OK;
4134 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4135 uint64_t value)
4137 env->daif = value & PSTATE_DAIF;
4140 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4142 return env->pstate & PSTATE_PAN;
4145 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4146 uint64_t value)
4148 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4151 static const ARMCPRegInfo pan_reginfo = {
4152 .name = "PAN", .state = ARM_CP_STATE_AA64,
4153 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4154 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4155 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4158 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4160 return env->pstate & PSTATE_UAO;
4163 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4164 uint64_t value)
4166 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4169 static const ARMCPRegInfo uao_reginfo = {
4170 .name = "UAO", .state = ARM_CP_STATE_AA64,
4171 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4172 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4173 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4176 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4178 return env->pstate & PSTATE_DIT;
4181 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4182 uint64_t value)
4184 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4187 static const ARMCPRegInfo dit_reginfo = {
4188 .name = "DIT", .state = ARM_CP_STATE_AA64,
4189 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4190 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4191 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4194 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4196 return env->pstate & PSTATE_SSBS;
4199 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4200 uint64_t value)
4202 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4205 static const ARMCPRegInfo ssbs_reginfo = {
4206 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4207 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4208 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4209 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4212 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4213 const ARMCPRegInfo *ri,
4214 bool isread)
4216 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4217 switch (arm_current_el(env)) {
4218 case 0:
4219 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4220 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4221 return CP_ACCESS_TRAP;
4223 /* fall through */
4224 case 1:
4225 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4226 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4227 return CP_ACCESS_TRAP_EL2;
4229 break;
4231 return CP_ACCESS_OK;
4234 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4235 const ARMCPRegInfo *ri,
4236 bool isread)
4238 /* Cache invalidate/clean to Point of Unification... */
4239 switch (arm_current_el(env)) {
4240 case 0:
4241 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4242 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4243 return CP_ACCESS_TRAP;
4245 /* fall through */
4246 case 1:
4247 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4248 if (arm_hcr_el2_eff(env) & HCR_TPU) {
4249 return CP_ACCESS_TRAP_EL2;
4251 break;
4253 return CP_ACCESS_OK;
4256 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4257 * Page D4-1736 (DDI0487A.b)
4260 static int vae1_tlbmask(CPUARMState *env)
4262 uint64_t hcr = arm_hcr_el2_eff(env);
4263 uint16_t mask;
4265 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4266 mask = ARMMMUIdxBit_E20_2 |
4267 ARMMMUIdxBit_E20_2_PAN |
4268 ARMMMUIdxBit_E20_0;
4269 } else {
4270 mask = ARMMMUIdxBit_E10_1 |
4271 ARMMMUIdxBit_E10_1_PAN |
4272 ARMMMUIdxBit_E10_0;
4275 if (arm_is_secure_below_el3(env)) {
4276 mask >>= ARM_MMU_IDX_A_NS;
4279 return mask;
4282 /* Return 56 if TBI is enabled, 64 otherwise. */
4283 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4284 uint64_t addr)
4286 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
4287 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4288 int select = extract64(addr, 55, 1);
4290 return (tbi >> select) & 1 ? 56 : 64;
4293 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4295 uint64_t hcr = arm_hcr_el2_eff(env);
4296 ARMMMUIdx mmu_idx;
4298 /* Only the regime of the mmu_idx below is significant. */
4299 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4300 mmu_idx = ARMMMUIdx_E20_0;
4301 } else {
4302 mmu_idx = ARMMMUIdx_E10_0;
4305 if (arm_is_secure_below_el3(env)) {
4306 mmu_idx &= ~ARM_MMU_IDX_A_NS;
4309 return tlbbits_for_regime(env, mmu_idx, addr);
4312 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4313 uint64_t value)
4315 CPUState *cs = env_cpu(env);
4316 int mask = vae1_tlbmask(env);
4318 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4321 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4322 uint64_t value)
4324 CPUState *cs = env_cpu(env);
4325 int mask = vae1_tlbmask(env);
4327 if (tlb_force_broadcast(env)) {
4328 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4329 } else {
4330 tlb_flush_by_mmuidx(cs, mask);
4334 static int alle1_tlbmask(CPUARMState *env)
4337 * Note that the 'ALL' scope must invalidate both stage 1 and
4338 * stage 2 translations, whereas most other scopes only invalidate
4339 * stage 1 translations.
4341 if (arm_is_secure_below_el3(env)) {
4342 return ARMMMUIdxBit_SE10_1 |
4343 ARMMMUIdxBit_SE10_1_PAN |
4344 ARMMMUIdxBit_SE10_0;
4345 } else {
4346 return ARMMMUIdxBit_E10_1 |
4347 ARMMMUIdxBit_E10_1_PAN |
4348 ARMMMUIdxBit_E10_0;
4352 static int e2_tlbmask(CPUARMState *env)
4354 if (arm_is_secure_below_el3(env)) {
4355 return ARMMMUIdxBit_SE20_0 |
4356 ARMMMUIdxBit_SE20_2 |
4357 ARMMMUIdxBit_SE20_2_PAN |
4358 ARMMMUIdxBit_SE2;
4359 } else {
4360 return ARMMMUIdxBit_E20_0 |
4361 ARMMMUIdxBit_E20_2 |
4362 ARMMMUIdxBit_E20_2_PAN |
4363 ARMMMUIdxBit_E2;
4367 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4368 uint64_t value)
4370 CPUState *cs = env_cpu(env);
4371 int mask = alle1_tlbmask(env);
4373 tlb_flush_by_mmuidx(cs, mask);
4376 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4377 uint64_t value)
4379 CPUState *cs = env_cpu(env);
4380 int mask = e2_tlbmask(env);
4382 tlb_flush_by_mmuidx(cs, mask);
4385 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4386 uint64_t value)
4388 ARMCPU *cpu = env_archcpu(env);
4389 CPUState *cs = CPU(cpu);
4391 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
4394 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4395 uint64_t value)
4397 CPUState *cs = env_cpu(env);
4398 int mask = alle1_tlbmask(env);
4400 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4403 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4404 uint64_t value)
4406 CPUState *cs = env_cpu(env);
4407 int mask = e2_tlbmask(env);
4409 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4412 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4413 uint64_t value)
4415 CPUState *cs = env_cpu(env);
4417 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
4420 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4421 uint64_t value)
4423 /* Invalidate by VA, EL2
4424 * Currently handles both VAE2 and VALE2, since we don't support
4425 * flush-last-level-only.
4427 CPUState *cs = env_cpu(env);
4428 int mask = e2_tlbmask(env);
4429 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4431 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4434 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4435 uint64_t value)
4437 /* Invalidate by VA, EL3
4438 * Currently handles both VAE3 and VALE3, since we don't support
4439 * flush-last-level-only.
4441 ARMCPU *cpu = env_archcpu(env);
4442 CPUState *cs = CPU(cpu);
4443 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4445 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
4448 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4449 uint64_t value)
4451 CPUState *cs = env_cpu(env);
4452 int mask = vae1_tlbmask(env);
4453 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4454 int bits = vae1_tlbbits(env, pageaddr);
4456 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4459 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4460 uint64_t value)
4462 /* Invalidate by VA, EL1&0 (AArch64 version).
4463 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4464 * since we don't support flush-for-specific-ASID-only or
4465 * flush-last-level-only.
4467 CPUState *cs = env_cpu(env);
4468 int mask = vae1_tlbmask(env);
4469 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4470 int bits = vae1_tlbbits(env, pageaddr);
4472 if (tlb_force_broadcast(env)) {
4473 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4474 } else {
4475 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4479 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4480 uint64_t value)
4482 CPUState *cs = env_cpu(env);
4483 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4484 bool secure = arm_is_secure_below_el3(env);
4485 int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
4486 int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
4487 pageaddr);
4489 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4492 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4493 uint64_t value)
4495 CPUState *cs = env_cpu(env);
4496 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4497 int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
4499 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4500 ARMMMUIdxBit_SE3, bits);
4503 #ifdef TARGET_AARCH64
4504 typedef struct {
4505 uint64_t base;
4506 uint64_t length;
4507 } TLBIRange;
4509 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4510 uint64_t value)
4512 unsigned int page_size_granule, page_shift, num, scale, exponent;
4513 /* Extract one bit to represent the va selector in use. */
4514 uint64_t select = sextract64(value, 36, 1);
4515 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4516 TLBIRange ret = { };
4518 page_size_granule = extract64(value, 46, 2);
4520 /* The granule encoded in value must match the granule in use. */
4521 if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4522 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4523 page_size_granule);
4524 return ret;
4527 page_shift = (page_size_granule - 1) * 2 + 12;
4528 num = extract64(value, 39, 5);
4529 scale = extract64(value, 44, 2);
4530 exponent = (5 * scale) + 1;
4532 ret.length = (num + 1) << (exponent + page_shift);
4534 if (param.select) {
4535 ret.base = sextract64(value, 0, 37);
4536 } else {
4537 ret.base = extract64(value, 0, 37);
4539 if (param.ds) {
4541 * With DS=1, BaseADDR is always shifted 16 so that it is able
4542 * to address all 52 va bits. The input address is perforce
4543 * aligned on a 64k boundary regardless of translation granule.
4545 page_shift = 16;
4547 ret.base <<= page_shift;
4549 return ret;
4552 static void do_rvae_write(CPUARMState *env, uint64_t value,
4553 int idxmap, bool synced)
4555 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4556 TLBIRange range;
4557 int bits;
4559 range = tlbi_aa64_get_range(env, one_idx, value);
4560 bits = tlbbits_for_regime(env, one_idx, range.base);
4562 if (synced) {
4563 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4564 range.base,
4565 range.length,
4566 idxmap,
4567 bits);
4568 } else {
4569 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4570 range.length, idxmap, bits);
4574 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4575 const ARMCPRegInfo *ri,
4576 uint64_t value)
4579 * Invalidate by VA range, EL1&0.
4580 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4581 * since we don't support flush-for-specific-ASID-only or
4582 * flush-last-level-only.
4585 do_rvae_write(env, value, vae1_tlbmask(env),
4586 tlb_force_broadcast(env));
4589 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4590 const ARMCPRegInfo *ri,
4591 uint64_t value)
4594 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4595 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4596 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4597 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4598 * shareable specific flushes.
4601 do_rvae_write(env, value, vae1_tlbmask(env), true);
4604 static int vae2_tlbmask(CPUARMState *env)
4606 return (arm_is_secure_below_el3(env)
4607 ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4610 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4611 const ARMCPRegInfo *ri,
4612 uint64_t value)
4615 * Invalidate by VA range, EL2.
4616 * Currently handles all of RVAE2 and RVALE2,
4617 * since we don't support flush-for-specific-ASID-only or
4618 * flush-last-level-only.
4621 do_rvae_write(env, value, vae2_tlbmask(env),
4622 tlb_force_broadcast(env));
4627 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4628 const ARMCPRegInfo *ri,
4629 uint64_t value)
4632 * Invalidate by VA range, Inner/Outer Shareable, EL2.
4633 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4634 * since we don't support flush-for-specific-ASID-only,
4635 * flush-last-level-only or inner/outer shareable specific flushes.
4638 do_rvae_write(env, value, vae2_tlbmask(env), true);
4642 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4643 const ARMCPRegInfo *ri,
4644 uint64_t value)
4647 * Invalidate by VA range, EL3.
4648 * Currently handles all of RVAE3 and RVALE3,
4649 * since we don't support flush-for-specific-ASID-only or
4650 * flush-last-level-only.
4653 do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4654 tlb_force_broadcast(env));
4657 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4658 const ARMCPRegInfo *ri,
4659 uint64_t value)
4662 * Invalidate by VA range, EL3, Inner/Outer Shareable.
4663 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4664 * since we don't support flush-for-specific-ASID-only,
4665 * flush-last-level-only or inner/outer specific flushes.
4668 do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4670 #endif
4672 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4673 bool isread)
4675 int cur_el = arm_current_el(env);
4677 if (cur_el < 2) {
4678 uint64_t hcr = arm_hcr_el2_eff(env);
4680 if (cur_el == 0) {
4681 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4682 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4683 return CP_ACCESS_TRAP_EL2;
4685 } else {
4686 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4687 return CP_ACCESS_TRAP;
4689 if (hcr & HCR_TDZ) {
4690 return CP_ACCESS_TRAP_EL2;
4693 } else if (hcr & HCR_TDZ) {
4694 return CP_ACCESS_TRAP_EL2;
4697 return CP_ACCESS_OK;
4700 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4702 ARMCPU *cpu = env_archcpu(env);
4703 int dzp_bit = 1 << 4;
4705 /* DZP indicates whether DC ZVA access is allowed */
4706 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4707 dzp_bit = 0;
4709 return cpu->dcz_blocksize | dzp_bit;
4712 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4713 bool isread)
4715 if (!(env->pstate & PSTATE_SP)) {
4716 /* Access to SP_EL0 is undefined if it's being used as
4717 * the stack pointer.
4719 return CP_ACCESS_TRAP_UNCATEGORIZED;
4721 return CP_ACCESS_OK;
4724 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4726 return env->pstate & PSTATE_SP;
4729 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4731 update_spsel(env, val);
4734 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4735 uint64_t value)
4737 ARMCPU *cpu = env_archcpu(env);
4739 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4740 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4741 value &= ~SCTLR_M;
4744 /* ??? Lots of these bits are not implemented. */
4746 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4747 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4748 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4749 } else {
4750 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4751 SCTLR_ATA0 | SCTLR_ATA);
4755 if (raw_read(env, ri) == value) {
4756 /* Skip the TLB flush if nothing actually changed; Linux likes
4757 * to do a lot of pointless SCTLR writes.
4759 return;
4762 raw_write(env, ri, value);
4764 /* This may enable/disable the MMU, so do a TLB flush. */
4765 tlb_flush(CPU(cpu));
4767 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4769 * Normally we would always end the TB on an SCTLR write; see the
4770 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4771 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4772 * of hflags from the translator, so do it here.
4774 arm_rebuild_hflags(env);
4778 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4779 uint64_t value)
4781 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4784 static const ARMCPRegInfo v8_cp_reginfo[] = {
4785 /* Minimal set of EL0-visible registers. This will need to be expanded
4786 * significantly for system emulation of AArch64 CPUs.
4788 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4789 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4790 .access = PL0_RW, .type = ARM_CP_NZCV },
4791 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4792 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4793 .type = ARM_CP_NO_RAW,
4794 .access = PL0_RW, .accessfn = aa64_daif_access,
4795 .fieldoffset = offsetof(CPUARMState, daif),
4796 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4797 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4798 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4799 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4800 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4801 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4802 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4803 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4804 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4805 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4806 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4807 .access = PL0_R, .type = ARM_CP_NO_RAW,
4808 .readfn = aa64_dczid_read },
4809 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4810 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4811 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4812 #ifndef CONFIG_USER_ONLY
4813 /* Avoid overhead of an access check that always passes in user-mode */
4814 .accessfn = aa64_zva_access,
4815 #endif
4817 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4818 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4819 .access = PL1_R, .type = ARM_CP_CURRENTEL },
4820 /* Cache ops: all NOPs since we don't emulate caches */
4821 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4822 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4823 .access = PL1_W, .type = ARM_CP_NOP,
4824 .accessfn = aa64_cacheop_pou_access },
4825 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4826 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4827 .access = PL1_W, .type = ARM_CP_NOP,
4828 .accessfn = aa64_cacheop_pou_access },
4829 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4830 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4831 .access = PL0_W, .type = ARM_CP_NOP,
4832 .accessfn = aa64_cacheop_pou_access },
4833 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4834 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4835 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4836 .type = ARM_CP_NOP },
4837 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4838 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4839 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4840 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4841 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4842 .access = PL0_W, .type = ARM_CP_NOP,
4843 .accessfn = aa64_cacheop_poc_access },
4844 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4845 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4846 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4847 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4848 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4849 .access = PL0_W, .type = ARM_CP_NOP,
4850 .accessfn = aa64_cacheop_pou_access },
4851 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4852 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4853 .access = PL0_W, .type = ARM_CP_NOP,
4854 .accessfn = aa64_cacheop_poc_access },
4855 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4856 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4857 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4858 /* TLBI operations */
4859 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4860 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4861 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4862 .writefn = tlbi_aa64_vmalle1is_write },
4863 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4864 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4865 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4866 .writefn = tlbi_aa64_vae1is_write },
4867 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4868 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4869 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4870 .writefn = tlbi_aa64_vmalle1is_write },
4871 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4872 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4873 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4874 .writefn = tlbi_aa64_vae1is_write },
4875 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4876 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4877 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4878 .writefn = tlbi_aa64_vae1is_write },
4879 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4880 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4881 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4882 .writefn = tlbi_aa64_vae1is_write },
4883 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4884 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4885 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4886 .writefn = tlbi_aa64_vmalle1_write },
4887 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4888 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4889 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4890 .writefn = tlbi_aa64_vae1_write },
4891 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4892 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4893 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4894 .writefn = tlbi_aa64_vmalle1_write },
4895 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4896 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4897 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4898 .writefn = tlbi_aa64_vae1_write },
4899 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4900 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4901 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4902 .writefn = tlbi_aa64_vae1_write },
4903 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4904 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4905 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4906 .writefn = tlbi_aa64_vae1_write },
4907 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4908 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4909 .access = PL2_W, .type = ARM_CP_NOP },
4910 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4911 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4912 .access = PL2_W, .type = ARM_CP_NOP },
4913 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4914 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4915 .access = PL2_W, .type = ARM_CP_NO_RAW,
4916 .writefn = tlbi_aa64_alle1is_write },
4917 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4918 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4919 .access = PL2_W, .type = ARM_CP_NO_RAW,
4920 .writefn = tlbi_aa64_alle1is_write },
4921 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4922 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4923 .access = PL2_W, .type = ARM_CP_NOP },
4924 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4925 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4926 .access = PL2_W, .type = ARM_CP_NOP },
4927 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4928 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4929 .access = PL2_W, .type = ARM_CP_NO_RAW,
4930 .writefn = tlbi_aa64_alle1_write },
4931 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4932 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4933 .access = PL2_W, .type = ARM_CP_NO_RAW,
4934 .writefn = tlbi_aa64_alle1is_write },
4935 #ifndef CONFIG_USER_ONLY
4936 /* 64 bit address translation operations */
4937 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4938 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4939 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4940 .writefn = ats_write64 },
4941 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4942 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4943 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4944 .writefn = ats_write64 },
4945 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4946 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4947 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4948 .writefn = ats_write64 },
4949 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4950 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4951 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4952 .writefn = ats_write64 },
4953 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4954 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4955 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4956 .writefn = ats_write64 },
4957 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4958 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4959 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4960 .writefn = ats_write64 },
4961 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4962 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4963 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4964 .writefn = ats_write64 },
4965 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4966 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4967 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4968 .writefn = ats_write64 },
4969 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4970 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4971 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4972 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4973 .writefn = ats_write64 },
4974 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4975 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4976 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4977 .writefn = ats_write64 },
4978 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4979 .type = ARM_CP_ALIAS,
4980 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4981 .access = PL1_RW, .resetvalue = 0,
4982 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4983 .writefn = par_write },
4984 #endif
4985 /* TLB invalidate last level of translation table walk */
4986 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4987 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4988 .writefn = tlbimva_is_write },
4989 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4990 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4991 .writefn = tlbimvaa_is_write },
4992 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4993 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4994 .writefn = tlbimva_write },
4995 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4996 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4997 .writefn = tlbimvaa_write },
4998 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4999 .type = ARM_CP_NO_RAW, .access = PL2_W,
5000 .writefn = tlbimva_hyp_write },
5001 { .name = "TLBIMVALHIS",
5002 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5003 .type = ARM_CP_NO_RAW, .access = PL2_W,
5004 .writefn = tlbimva_hyp_is_write },
5005 { .name = "TLBIIPAS2",
5006 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5007 .type = ARM_CP_NOP, .access = PL2_W },
5008 { .name = "TLBIIPAS2IS",
5009 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5010 .type = ARM_CP_NOP, .access = PL2_W },
5011 { .name = "TLBIIPAS2L",
5012 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5013 .type = ARM_CP_NOP, .access = PL2_W },
5014 { .name = "TLBIIPAS2LIS",
5015 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5016 .type = ARM_CP_NOP, .access = PL2_W },
5017 /* 32 bit cache operations */
5018 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5019 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5020 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5021 .type = ARM_CP_NOP, .access = PL1_W },
5022 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5023 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5024 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5025 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5026 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5027 .type = ARM_CP_NOP, .access = PL1_W },
5028 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5029 .type = ARM_CP_NOP, .access = PL1_W },
5030 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5031 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5032 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5033 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5034 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5035 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5036 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5037 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5038 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5039 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5040 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5041 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5042 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5043 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5044 /* MMU Domain access control / MPU write buffer control */
5045 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5046 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5047 .writefn = dacr_write, .raw_writefn = raw_write,
5048 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5049 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5050 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5051 .type = ARM_CP_ALIAS,
5052 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5053 .access = PL1_RW,
5054 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5055 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5056 .type = ARM_CP_ALIAS,
5057 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5058 .access = PL1_RW,
5059 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5060 /* We rely on the access checks not allowing the guest to write to the
5061 * state field when SPSel indicates that it's being used as the stack
5062 * pointer.
5064 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5065 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5066 .access = PL1_RW, .accessfn = sp_el0_access,
5067 .type = ARM_CP_ALIAS,
5068 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5069 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5070 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5071 .access = PL2_RW, .type = ARM_CP_ALIAS,
5072 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5073 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5074 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5075 .type = ARM_CP_NO_RAW,
5076 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5077 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5078 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5079 .access = PL2_RW,
5080 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5081 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5082 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5083 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5084 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5085 .writefn = dacr_write, .raw_writefn = raw_write,
5086 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5087 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5088 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5089 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5090 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5091 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5092 .type = ARM_CP_ALIAS,
5093 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5094 .access = PL2_RW,
5095 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5096 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5097 .type = ARM_CP_ALIAS,
5098 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5099 .access = PL2_RW,
5100 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5101 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5102 .type = ARM_CP_ALIAS,
5103 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5104 .access = PL2_RW,
5105 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5106 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5107 .type = ARM_CP_ALIAS,
5108 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5109 .access = PL2_RW,
5110 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5111 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5112 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5113 .resetvalue = 0,
5114 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5115 { .name = "SDCR", .type = ARM_CP_ALIAS,
5116 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5117 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5118 .writefn = sdcr_write,
5119 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5122 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5124 ARMCPU *cpu = env_archcpu(env);
5126 if (arm_feature(env, ARM_FEATURE_V8)) {
5127 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5128 } else {
5129 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5132 if (arm_feature(env, ARM_FEATURE_EL3)) {
5133 valid_mask &= ~HCR_HCD;
5134 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5135 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5136 * However, if we're using the SMC PSCI conduit then QEMU is
5137 * effectively acting like EL3 firmware and so the guest at
5138 * EL2 should retain the ability to prevent EL1 from being
5139 * able to make SMC calls into the ersatz firmware, so in
5140 * that case HCR.TSC should be read/write.
5142 valid_mask &= ~HCR_TSC;
5145 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5146 if (cpu_isar_feature(aa64_vh, cpu)) {
5147 valid_mask |= HCR_E2H;
5149 if (cpu_isar_feature(aa64_ras, cpu)) {
5150 valid_mask |= HCR_TERR | HCR_TEA;
5152 if (cpu_isar_feature(aa64_lor, cpu)) {
5153 valid_mask |= HCR_TLOR;
5155 if (cpu_isar_feature(aa64_pauth, cpu)) {
5156 valid_mask |= HCR_API | HCR_APK;
5158 if (cpu_isar_feature(aa64_mte, cpu)) {
5159 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5161 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5162 valid_mask |= HCR_ENSCXT;
5166 /* Clear RES0 bits. */
5167 value &= valid_mask;
5170 * These bits change the MMU setup:
5171 * HCR_VM enables stage 2 translation
5172 * HCR_PTW forbids certain page-table setups
5173 * HCR_DC disables stage1 and enables stage2 translation
5174 * HCR_DCT enables tagging on (disabled) stage1 translation
5176 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT)) {
5177 tlb_flush(CPU(cpu));
5179 env->cp15.hcr_el2 = value;
5182 * Updates to VI and VF require us to update the status of
5183 * virtual interrupts, which are the logical OR of these bits
5184 * and the state of the input lines from the GIC. (This requires
5185 * that we have the iothread lock, which is done by marking the
5186 * reginfo structs as ARM_CP_IO.)
5187 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5188 * possible for it to be taken immediately, because VIRQ and
5189 * VFIQ are masked unless running at EL0 or EL1, and HCR
5190 * can only be written at EL2.
5192 g_assert(qemu_mutex_iothread_locked());
5193 arm_cpu_update_virq(cpu);
5194 arm_cpu_update_vfiq(cpu);
5195 arm_cpu_update_vserr(cpu);
5198 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5200 do_hcr_write(env, value, 0);
5203 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5204 uint64_t value)
5206 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5207 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5208 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5211 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5212 uint64_t value)
5214 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5215 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5216 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5220 * Return the effective value of HCR_EL2.
5221 * Bits that are not included here:
5222 * RW (read from SCR_EL3.RW as needed)
5224 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5226 uint64_t ret = env->cp15.hcr_el2;
5228 if (!arm_is_el2_enabled(env)) {
5230 * "This register has no effect if EL2 is not enabled in the
5231 * current Security state". This is ARMv8.4-SecEL2 speak for
5232 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5234 * Prior to that, the language was "In an implementation that
5235 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5236 * as if this field is 0 for all purposes other than a direct
5237 * read or write access of HCR_EL2". With lots of enumeration
5238 * on a per-field basis. In current QEMU, this is condition
5239 * is arm_is_secure_below_el3.
5241 * Since the v8.4 language applies to the entire register, and
5242 * appears to be backward compatible, use that.
5244 return 0;
5248 * For a cpu that supports both aarch64 and aarch32, we can set bits
5249 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5250 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5252 if (!arm_el_is_aa64(env, 2)) {
5253 uint64_t aa32_valid;
5256 * These bits are up-to-date as of ARMv8.6.
5257 * For HCR, it's easiest to list just the 2 bits that are invalid.
5258 * For HCR2, list those that are valid.
5260 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5261 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5262 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5263 ret &= aa32_valid;
5266 if (ret & HCR_TGE) {
5267 /* These bits are up-to-date as of ARMv8.6. */
5268 if (ret & HCR_E2H) {
5269 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5270 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5271 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5272 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5273 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5274 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5275 } else {
5276 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5278 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5279 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5280 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5281 HCR_TLOR);
5284 return ret;
5287 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5288 uint64_t value)
5291 * For A-profile AArch32 EL3, if NSACR.CP10
5292 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5294 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5295 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5296 value &= ~(0x3 << 10);
5297 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5299 env->cp15.cptr_el[2] = value;
5302 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5305 * For A-profile AArch32 EL3, if NSACR.CP10
5306 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5308 uint64_t value = env->cp15.cptr_el[2];
5310 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5311 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5312 value |= 0x3 << 10;
5314 return value;
5317 static const ARMCPRegInfo el2_cp_reginfo[] = {
5318 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5319 .type = ARM_CP_IO,
5320 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5321 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5322 .writefn = hcr_write },
5323 { .name = "HCR", .state = ARM_CP_STATE_AA32,
5324 .type = ARM_CP_ALIAS | ARM_CP_IO,
5325 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5326 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5327 .writefn = hcr_writelow },
5328 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5329 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5330 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5331 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5332 .type = ARM_CP_ALIAS,
5333 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5334 .access = PL2_RW,
5335 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5336 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5337 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5338 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5339 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5340 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5341 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5342 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5343 .type = ARM_CP_ALIAS,
5344 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5345 .access = PL2_RW,
5346 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5347 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5348 .type = ARM_CP_ALIAS,
5349 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5350 .access = PL2_RW,
5351 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5352 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5353 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5354 .access = PL2_RW, .writefn = vbar_write,
5355 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5356 .resetvalue = 0 },
5357 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5358 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5359 .access = PL3_RW, .type = ARM_CP_ALIAS,
5360 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5361 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5362 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5363 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5364 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5365 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5366 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5367 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5368 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5369 .resetvalue = 0 },
5370 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5371 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5372 .access = PL2_RW, .type = ARM_CP_ALIAS,
5373 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5374 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5375 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5376 .access = PL2_RW, .type = ARM_CP_CONST,
5377 .resetvalue = 0 },
5378 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5379 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5380 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5381 .access = PL2_RW, .type = ARM_CP_CONST,
5382 .resetvalue = 0 },
5383 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5384 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5385 .access = PL2_RW, .type = ARM_CP_CONST,
5386 .resetvalue = 0 },
5387 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5388 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5389 .access = PL2_RW, .type = ARM_CP_CONST,
5390 .resetvalue = 0 },
5391 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5392 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5393 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5394 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
5395 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5396 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5397 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5398 .type = ARM_CP_ALIAS,
5399 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5400 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5401 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5402 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5403 .access = PL2_RW,
5404 /* no .writefn needed as this can't cause an ASID change;
5405 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5407 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5408 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5409 .cp = 15, .opc1 = 6, .crm = 2,
5410 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5411 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5412 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5413 .writefn = vttbr_write },
5414 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5415 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5416 .access = PL2_RW, .writefn = vttbr_write,
5417 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5418 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5419 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5420 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5421 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5422 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5423 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5424 .access = PL2_RW, .resetvalue = 0,
5425 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5426 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5427 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5428 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5429 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5430 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5431 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5432 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5433 { .name = "TLBIALLNSNH",
5434 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5435 .type = ARM_CP_NO_RAW, .access = PL2_W,
5436 .writefn = tlbiall_nsnh_write },
5437 { .name = "TLBIALLNSNHIS",
5438 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5439 .type = ARM_CP_NO_RAW, .access = PL2_W,
5440 .writefn = tlbiall_nsnh_is_write },
5441 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5442 .type = ARM_CP_NO_RAW, .access = PL2_W,
5443 .writefn = tlbiall_hyp_write },
5444 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5445 .type = ARM_CP_NO_RAW, .access = PL2_W,
5446 .writefn = tlbiall_hyp_is_write },
5447 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5448 .type = ARM_CP_NO_RAW, .access = PL2_W,
5449 .writefn = tlbimva_hyp_write },
5450 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5451 .type = ARM_CP_NO_RAW, .access = PL2_W,
5452 .writefn = tlbimva_hyp_is_write },
5453 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5454 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5455 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5456 .writefn = tlbi_aa64_alle2_write },
5457 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5458 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5459 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5460 .writefn = tlbi_aa64_vae2_write },
5461 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5462 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5463 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5464 .writefn = tlbi_aa64_vae2_write },
5465 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5466 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5467 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5468 .writefn = tlbi_aa64_alle2is_write },
5469 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5470 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5471 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5472 .writefn = tlbi_aa64_vae2is_write },
5473 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5474 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5475 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5476 .writefn = tlbi_aa64_vae2is_write },
5477 #ifndef CONFIG_USER_ONLY
5478 /* Unlike the other EL2-related AT operations, these must
5479 * UNDEF from EL3 if EL2 is not implemented, which is why we
5480 * define them here rather than with the rest of the AT ops.
5482 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5483 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5484 .access = PL2_W, .accessfn = at_s1e2_access,
5485 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5486 .writefn = ats_write64 },
5487 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5488 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5489 .access = PL2_W, .accessfn = at_s1e2_access,
5490 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5491 .writefn = ats_write64 },
5492 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5493 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5494 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5495 * to behave as if SCR.NS was 1.
5497 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5498 .access = PL2_W,
5499 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5500 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5501 .access = PL2_W,
5502 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5503 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5504 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5505 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5506 * reset values as IMPDEF. We choose to reset to 3 to comply with
5507 * both ARMv7 and ARMv8.
5509 .access = PL2_RW, .resetvalue = 3,
5510 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5511 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5512 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5513 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5514 .writefn = gt_cntvoff_write,
5515 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5516 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5517 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5518 .writefn = gt_cntvoff_write,
5519 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5520 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5521 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5522 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5523 .type = ARM_CP_IO, .access = PL2_RW,
5524 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5525 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5526 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5527 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5528 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5529 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5530 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5531 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5532 .resetfn = gt_hyp_timer_reset,
5533 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5534 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5535 .type = ARM_CP_IO,
5536 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5537 .access = PL2_RW,
5538 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5539 .resetvalue = 0,
5540 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5541 #endif
5542 /* The only field of MDCR_EL2 that has a defined architectural reset value
5543 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
5545 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5546 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5547 .access = PL2_RW, .resetvalue = PMCR_NUM_COUNTERS,
5548 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
5549 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5550 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5551 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5552 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5553 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5554 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5555 .access = PL2_RW,
5556 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5557 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5558 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5559 .access = PL2_RW,
5560 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5563 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5564 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5565 .type = ARM_CP_ALIAS | ARM_CP_IO,
5566 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5567 .access = PL2_RW,
5568 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5569 .writefn = hcr_writehigh },
5572 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5573 bool isread)
5575 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5576 return CP_ACCESS_OK;
5578 return CP_ACCESS_TRAP_UNCATEGORIZED;
5581 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5582 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5583 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5584 .access = PL2_RW, .accessfn = sel2_access,
5585 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5586 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5587 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5588 .access = PL2_RW, .accessfn = sel2_access,
5589 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5592 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5593 bool isread)
5595 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5596 * At Secure EL1 it traps to EL3 or EL2.
5598 if (arm_current_el(env) == 3) {
5599 return CP_ACCESS_OK;
5601 if (arm_is_secure_below_el3(env)) {
5602 if (env->cp15.scr_el3 & SCR_EEL2) {
5603 return CP_ACCESS_TRAP_EL2;
5605 return CP_ACCESS_TRAP_EL3;
5607 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5608 if (isread) {
5609 return CP_ACCESS_OK;
5611 return CP_ACCESS_TRAP_UNCATEGORIZED;
5614 static const ARMCPRegInfo el3_cp_reginfo[] = {
5615 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5616 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5617 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5618 .resetfn = scr_reset, .writefn = scr_write },
5619 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5620 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5621 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5622 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5623 .writefn = scr_write },
5624 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5625 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5626 .access = PL3_RW, .resetvalue = 0,
5627 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5628 { .name = "SDER",
5629 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5630 .access = PL3_RW, .resetvalue = 0,
5631 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5632 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5633 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5634 .writefn = vbar_write, .resetvalue = 0,
5635 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5636 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5637 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5638 .access = PL3_RW, .resetvalue = 0,
5639 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5640 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5641 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5642 .access = PL3_RW,
5643 /* no .writefn needed as this can't cause an ASID change;
5644 * we must provide a .raw_writefn and .resetfn because we handle
5645 * reset and migration for the AArch32 TTBCR(S), which might be
5646 * using mask and base_mask.
5648 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
5649 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5650 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5651 .type = ARM_CP_ALIAS,
5652 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5653 .access = PL3_RW,
5654 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5655 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5656 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5657 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5658 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5659 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5660 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5661 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5662 .type = ARM_CP_ALIAS,
5663 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5664 .access = PL3_RW,
5665 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5666 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5667 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5668 .access = PL3_RW, .writefn = vbar_write,
5669 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5670 .resetvalue = 0 },
5671 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5672 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5673 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5674 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5675 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5676 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5677 .access = PL3_RW, .resetvalue = 0,
5678 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5679 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5680 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5681 .access = PL3_RW, .type = ARM_CP_CONST,
5682 .resetvalue = 0 },
5683 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5684 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5685 .access = PL3_RW, .type = ARM_CP_CONST,
5686 .resetvalue = 0 },
5687 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5688 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5689 .access = PL3_RW, .type = ARM_CP_CONST,
5690 .resetvalue = 0 },
5691 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5692 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5693 .access = PL3_W, .type = ARM_CP_NO_RAW,
5694 .writefn = tlbi_aa64_alle3is_write },
5695 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5696 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5697 .access = PL3_W, .type = ARM_CP_NO_RAW,
5698 .writefn = tlbi_aa64_vae3is_write },
5699 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5700 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5701 .access = PL3_W, .type = ARM_CP_NO_RAW,
5702 .writefn = tlbi_aa64_vae3is_write },
5703 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5704 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5705 .access = PL3_W, .type = ARM_CP_NO_RAW,
5706 .writefn = tlbi_aa64_alle3_write },
5707 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5708 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5709 .access = PL3_W, .type = ARM_CP_NO_RAW,
5710 .writefn = tlbi_aa64_vae3_write },
5711 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5712 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5713 .access = PL3_W, .type = ARM_CP_NO_RAW,
5714 .writefn = tlbi_aa64_vae3_write },
5717 #ifndef CONFIG_USER_ONLY
5718 /* Test if system register redirection is to occur in the current state. */
5719 static bool redirect_for_e2h(CPUARMState *env)
5721 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5724 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5726 CPReadFn *readfn;
5728 if (redirect_for_e2h(env)) {
5729 /* Switch to the saved EL2 version of the register. */
5730 ri = ri->opaque;
5731 readfn = ri->readfn;
5732 } else {
5733 readfn = ri->orig_readfn;
5735 if (readfn == NULL) {
5736 readfn = raw_read;
5738 return readfn(env, ri);
5741 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5742 uint64_t value)
5744 CPWriteFn *writefn;
5746 if (redirect_for_e2h(env)) {
5747 /* Switch to the saved EL2 version of the register. */
5748 ri = ri->opaque;
5749 writefn = ri->writefn;
5750 } else {
5751 writefn = ri->orig_writefn;
5753 if (writefn == NULL) {
5754 writefn = raw_write;
5756 writefn(env, ri, value);
5759 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5761 struct E2HAlias {
5762 uint32_t src_key, dst_key, new_key;
5763 const char *src_name, *dst_name, *new_name;
5764 bool (*feature)(const ARMISARegisters *id);
5767 #define K(op0, op1, crn, crm, op2) \
5768 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5770 static const struct E2HAlias aliases[] = {
5771 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5772 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5773 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5774 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5775 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5776 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5777 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5778 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5779 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5780 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5781 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5782 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5783 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5784 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5785 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5786 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5787 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5788 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5789 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5790 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5791 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5792 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5793 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5794 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5795 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5796 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5797 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5798 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5799 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5800 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5801 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5802 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5805 * Note that redirection of ZCR is mentioned in the description
5806 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5807 * not in the summary table.
5809 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5810 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5812 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
5813 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5815 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5816 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5817 isar_feature_aa64_scxtnum },
5819 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5820 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5822 #undef K
5824 size_t i;
5826 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5827 const struct E2HAlias *a = &aliases[i];
5828 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
5829 bool ok;
5831 if (a->feature && !a->feature(&cpu->isar)) {
5832 continue;
5835 src_reg = g_hash_table_lookup(cpu->cp_regs,
5836 (gpointer)(uintptr_t)a->src_key);
5837 dst_reg = g_hash_table_lookup(cpu->cp_regs,
5838 (gpointer)(uintptr_t)a->dst_key);
5839 g_assert(src_reg != NULL);
5840 g_assert(dst_reg != NULL);
5842 /* Cross-compare names to detect typos in the keys. */
5843 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5844 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5846 /* None of the core system registers use opaque; we will. */
5847 g_assert(src_reg->opaque == NULL);
5849 /* Create alias before redirection so we dup the right data. */
5850 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5852 new_reg->name = a->new_name;
5853 new_reg->type |= ARM_CP_ALIAS;
5854 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5855 new_reg->access &= PL2_RW | PL3_RW;
5857 ok = g_hash_table_insert(cpu->cp_regs,
5858 (gpointer)(uintptr_t)a->new_key, new_reg);
5859 g_assert(ok);
5861 src_reg->opaque = dst_reg;
5862 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5863 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5864 if (!src_reg->raw_readfn) {
5865 src_reg->raw_readfn = raw_read;
5867 if (!src_reg->raw_writefn) {
5868 src_reg->raw_writefn = raw_write;
5870 src_reg->readfn = el2_e2h_read;
5871 src_reg->writefn = el2_e2h_write;
5874 #endif
5876 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5877 bool isread)
5879 int cur_el = arm_current_el(env);
5881 if (cur_el < 2) {
5882 uint64_t hcr = arm_hcr_el2_eff(env);
5884 if (cur_el == 0) {
5885 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5886 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5887 return CP_ACCESS_TRAP_EL2;
5889 } else {
5890 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5891 return CP_ACCESS_TRAP;
5893 if (hcr & HCR_TID2) {
5894 return CP_ACCESS_TRAP_EL2;
5897 } else if (hcr & HCR_TID2) {
5898 return CP_ACCESS_TRAP_EL2;
5902 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5903 return CP_ACCESS_TRAP_EL2;
5906 return CP_ACCESS_OK;
5909 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5910 uint64_t value)
5912 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5913 * read via a bit in OSLSR_EL1.
5915 int oslock;
5917 if (ri->state == ARM_CP_STATE_AA32) {
5918 oslock = (value == 0xC5ACCE55);
5919 } else {
5920 oslock = value & 1;
5923 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5926 static const ARMCPRegInfo debug_cp_reginfo[] = {
5927 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
5928 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5929 * unlike DBGDRAR it is never accessible from EL0.
5930 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5931 * accessor.
5933 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
5934 .access = PL0_R, .accessfn = access_tdra,
5935 .type = ARM_CP_CONST, .resetvalue = 0 },
5936 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
5937 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
5938 .access = PL1_R, .accessfn = access_tdra,
5939 .type = ARM_CP_CONST, .resetvalue = 0 },
5940 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
5941 .access = PL0_R, .accessfn = access_tdra,
5942 .type = ARM_CP_CONST, .resetvalue = 0 },
5943 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
5944 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
5945 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
5946 .access = PL1_RW, .accessfn = access_tda,
5947 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
5948 .resetvalue = 0 },
5950 * MDCCSR_EL0[30:29] map to EDSCR[30:29]. Simply RAZ as the external
5951 * Debug Communication Channel is not implemented.
5953 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_AA64,
5954 .opc0 = 2, .opc1 = 3, .crn = 0, .crm = 1, .opc2 = 0,
5955 .access = PL0_R, .accessfn = access_tda,
5956 .type = ARM_CP_CONST, .resetvalue = 0 },
5958 * DBGDSCRint[15,12,5:2] map to MDSCR_EL1[15,12,5:2]. Map all bits as
5959 * it is unlikely a guest will care.
5960 * We don't implement the configurable EL0 access.
5962 { .name = "DBGDSCRint", .state = ARM_CP_STATE_AA32,
5963 .cp = 14, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
5964 .type = ARM_CP_ALIAS,
5965 .access = PL1_R, .accessfn = access_tda,
5966 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
5967 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
5968 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
5969 .access = PL1_W, .type = ARM_CP_NO_RAW,
5970 .accessfn = access_tdosa,
5971 .writefn = oslar_write },
5972 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
5973 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
5974 .access = PL1_R, .resetvalue = 10,
5975 .accessfn = access_tdosa,
5976 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5977 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
5978 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
5979 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
5980 .access = PL1_RW, .accessfn = access_tdosa,
5981 .type = ARM_CP_NOP },
5982 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
5983 * implement vector catch debug events yet.
5985 { .name = "DBGVCR",
5986 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
5987 .access = PL1_RW, .accessfn = access_tda,
5988 .type = ARM_CP_NOP },
5989 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
5990 * to save and restore a 32-bit guest's DBGVCR)
5992 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
5993 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
5994 .access = PL2_RW, .accessfn = access_tda,
5995 .type = ARM_CP_NOP | ARM_CP_EL3_NO_EL2_KEEP },
5996 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
5997 * Channel but Linux may try to access this register. The 32-bit
5998 * alias is DBGDCCINT.
6000 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
6001 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6002 .access = PL1_RW, .accessfn = access_tda,
6003 .type = ARM_CP_NOP },
6006 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6007 /* 64 bit access versions of the (dummy) debug registers */
6008 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6009 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6010 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6011 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6015 * Check for traps to RAS registers, which are controlled
6016 * by HCR_EL2.TERR and SCR_EL3.TERR.
6018 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6019 bool isread)
6021 int el = arm_current_el(env);
6023 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6024 return CP_ACCESS_TRAP_EL2;
6026 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6027 return CP_ACCESS_TRAP_EL3;
6029 return CP_ACCESS_OK;
6032 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6034 int el = arm_current_el(env);
6036 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6037 return env->cp15.vdisr_el2;
6039 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6040 return 0; /* RAZ/WI */
6042 return env->cp15.disr_el1;
6045 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6047 int el = arm_current_el(env);
6049 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6050 env->cp15.vdisr_el2 = val;
6051 return;
6053 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6054 return; /* RAZ/WI */
6056 env->cp15.disr_el1 = val;
6060 * Minimal RAS implementation with no Error Records.
6061 * Which means that all of the Error Record registers:
6062 * ERXADDR_EL1
6063 * ERXCTLR_EL1
6064 * ERXFR_EL1
6065 * ERXMISC0_EL1
6066 * ERXMISC1_EL1
6067 * ERXMISC2_EL1
6068 * ERXMISC3_EL1
6069 * ERXPFGCDN_EL1 (RASv1p1)
6070 * ERXPFGCTL_EL1 (RASv1p1)
6071 * ERXPFGF_EL1 (RASv1p1)
6072 * ERXSTATUS_EL1
6073 * and
6074 * ERRSELR_EL1
6075 * may generate UNDEFINED, which is the effect we get by not
6076 * listing them at all.
6078 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6079 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6080 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6081 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6082 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6083 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6084 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6085 .access = PL1_R, .accessfn = access_terr,
6086 .type = ARM_CP_CONST, .resetvalue = 0 },
6087 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6088 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6089 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6090 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6091 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6092 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6095 /* Return the exception level to which exceptions should be taken
6096 * via SVEAccessTrap. If an exception should be routed through
6097 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
6098 * take care of raising that exception.
6099 * C.f. the ARM pseudocode function CheckSVEEnabled.
6101 int sve_exception_el(CPUARMState *env, int el)
6103 #ifndef CONFIG_USER_ONLY
6104 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
6106 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
6107 /* Check CPACR.ZEN. */
6108 switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
6109 case 1:
6110 if (el != 0) {
6111 break;
6113 /* fall through */
6114 case 0:
6115 case 2:
6116 /* route_to_el2 */
6117 return hcr_el2 & HCR_TGE ? 2 : 1;
6120 /* Check CPACR.FPEN. */
6121 switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
6122 case 1:
6123 if (el != 0) {
6124 break;
6126 /* fall through */
6127 case 0:
6128 case 2:
6129 return 0;
6134 * CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE).
6136 if (el <= 2) {
6137 if (hcr_el2 & HCR_E2H) {
6138 /* Check CPTR_EL2.ZEN. */
6139 switch (extract32(env->cp15.cptr_el[2], 16, 2)) {
6140 case 1:
6141 if (el != 0 || !(hcr_el2 & HCR_TGE)) {
6142 break;
6144 /* fall through */
6145 case 0:
6146 case 2:
6147 return 2;
6150 /* Check CPTR_EL2.FPEN. */
6151 switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
6152 case 1:
6153 if (el == 2 || !(hcr_el2 & HCR_TGE)) {
6154 break;
6156 /* fall through */
6157 case 0:
6158 case 2:
6159 return 0;
6161 } else if (arm_is_el2_enabled(env)) {
6162 if (env->cp15.cptr_el[2] & CPTR_TZ) {
6163 return 2;
6165 if (env->cp15.cptr_el[2] & CPTR_TFP) {
6166 return 0;
6171 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6172 if (arm_feature(env, ARM_FEATURE_EL3)
6173 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
6174 return 3;
6176 #endif
6177 return 0;
6180 uint32_t aarch64_sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
6182 uint32_t end_len;
6184 start_len = MIN(start_len, ARM_MAX_VQ - 1);
6185 end_len = start_len;
6187 if (!test_bit(start_len, cpu->sve_vq_map)) {
6188 end_len = find_last_bit(cpu->sve_vq_map, start_len);
6189 assert(end_len < start_len);
6191 return end_len;
6195 * Given that SVE is enabled, return the vector length for EL.
6197 uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
6199 ARMCPU *cpu = env_archcpu(env);
6200 uint32_t zcr_len = cpu->sve_max_vq - 1;
6202 if (el <= 1 &&
6203 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
6204 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6206 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6207 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6209 if (arm_feature(env, ARM_FEATURE_EL3)) {
6210 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6213 return aarch64_sve_zcr_get_valid_len(cpu, zcr_len);
6216 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6217 uint64_t value)
6219 int cur_el = arm_current_el(env);
6220 int old_len = sve_zcr_len_for_el(env, cur_el);
6221 int new_len;
6223 /* Bits other than [3:0] are RAZ/WI. */
6224 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6225 raw_write(env, ri, value & 0xf);
6228 * Because we arrived here, we know both FP and SVE are enabled;
6229 * otherwise we would have trapped access to the ZCR_ELn register.
6231 new_len = sve_zcr_len_for_el(env, cur_el);
6232 if (new_len < old_len) {
6233 aarch64_sve_narrow_vq(env, new_len + 1);
6237 static const ARMCPRegInfo zcr_reginfo[] = {
6238 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6239 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6240 .access = PL1_RW, .type = ARM_CP_SVE,
6241 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6242 .writefn = zcr_write, .raw_writefn = raw_write },
6243 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6244 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6245 .access = PL2_RW, .type = ARM_CP_SVE,
6246 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6247 .writefn = zcr_write, .raw_writefn = raw_write },
6248 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6249 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6250 .access = PL3_RW, .type = ARM_CP_SVE,
6251 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6252 .writefn = zcr_write, .raw_writefn = raw_write },
6255 void hw_watchpoint_update(ARMCPU *cpu, int n)
6257 CPUARMState *env = &cpu->env;
6258 vaddr len = 0;
6259 vaddr wvr = env->cp15.dbgwvr[n];
6260 uint64_t wcr = env->cp15.dbgwcr[n];
6261 int mask;
6262 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6264 if (env->cpu_watchpoint[n]) {
6265 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6266 env->cpu_watchpoint[n] = NULL;
6269 if (!FIELD_EX64(wcr, DBGWCR, E)) {
6270 /* E bit clear : watchpoint disabled */
6271 return;
6274 switch (FIELD_EX64(wcr, DBGWCR, LSC)) {
6275 case 0:
6276 /* LSC 00 is reserved and must behave as if the wp is disabled */
6277 return;
6278 case 1:
6279 flags |= BP_MEM_READ;
6280 break;
6281 case 2:
6282 flags |= BP_MEM_WRITE;
6283 break;
6284 case 3:
6285 flags |= BP_MEM_ACCESS;
6286 break;
6289 /* Attempts to use both MASK and BAS fields simultaneously are
6290 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6291 * thus generating a watchpoint for every byte in the masked region.
6293 mask = FIELD_EX64(wcr, DBGWCR, MASK);
6294 if (mask == 1 || mask == 2) {
6295 /* Reserved values of MASK; we must act as if the mask value was
6296 * some non-reserved value, or as if the watchpoint were disabled.
6297 * We choose the latter.
6299 return;
6300 } else if (mask) {
6301 /* Watchpoint covers an aligned area up to 2GB in size */
6302 len = 1ULL << mask;
6303 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6304 * whether the watchpoint fires when the unmasked bits match; we opt
6305 * to generate the exceptions.
6307 wvr &= ~(len - 1);
6308 } else {
6309 /* Watchpoint covers bytes defined by the byte address select bits */
6310 int bas = FIELD_EX64(wcr, DBGWCR, BAS);
6311 int basstart;
6313 if (extract64(wvr, 2, 1)) {
6314 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6315 * ignored, and BAS[3:0] define which bytes to watch.
6317 bas &= 0xf;
6320 if (bas == 0) {
6321 /* This must act as if the watchpoint is disabled */
6322 return;
6325 /* The BAS bits are supposed to be programmed to indicate a contiguous
6326 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6327 * we fire for each byte in the word/doubleword addressed by the WVR.
6328 * We choose to ignore any non-zero bits after the first range of 1s.
6330 basstart = ctz32(bas);
6331 len = cto32(bas >> basstart);
6332 wvr += basstart;
6335 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6336 &env->cpu_watchpoint[n]);
6339 void hw_watchpoint_update_all(ARMCPU *cpu)
6341 int i;
6342 CPUARMState *env = &cpu->env;
6344 /* Completely clear out existing QEMU watchpoints and our array, to
6345 * avoid possible stale entries following migration load.
6347 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6348 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6350 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6351 hw_watchpoint_update(cpu, i);
6355 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6356 uint64_t value)
6358 ARMCPU *cpu = env_archcpu(env);
6359 int i = ri->crm;
6362 * Bits [1:0] are RES0.
6364 * It is IMPLEMENTATION DEFINED whether [63:49] ([63:53] with FEAT_LVA)
6365 * are hardwired to the value of bit [48] ([52] with FEAT_LVA), or if
6366 * they contain the value written. It is CONSTRAINED UNPREDICTABLE
6367 * whether the RESS bits are ignored when comparing an address.
6369 * Therefore we are allowed to compare the entire register, which lets
6370 * us avoid considering whether or not FEAT_LVA is actually enabled.
6372 value &= ~3ULL;
6374 raw_write(env, ri, value);
6375 hw_watchpoint_update(cpu, i);
6378 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6379 uint64_t value)
6381 ARMCPU *cpu = env_archcpu(env);
6382 int i = ri->crm;
6384 raw_write(env, ri, value);
6385 hw_watchpoint_update(cpu, i);
6388 void hw_breakpoint_update(ARMCPU *cpu, int n)
6390 CPUARMState *env = &cpu->env;
6391 uint64_t bvr = env->cp15.dbgbvr[n];
6392 uint64_t bcr = env->cp15.dbgbcr[n];
6393 vaddr addr;
6394 int bt;
6395 int flags = BP_CPU;
6397 if (env->cpu_breakpoint[n]) {
6398 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6399 env->cpu_breakpoint[n] = NULL;
6402 if (!extract64(bcr, 0, 1)) {
6403 /* E bit clear : watchpoint disabled */
6404 return;
6407 bt = extract64(bcr, 20, 4);
6409 switch (bt) {
6410 case 4: /* unlinked address mismatch (reserved if AArch64) */
6411 case 5: /* linked address mismatch (reserved if AArch64) */
6412 qemu_log_mask(LOG_UNIMP,
6413 "arm: address mismatch breakpoint types not implemented\n");
6414 return;
6415 case 0: /* unlinked address match */
6416 case 1: /* linked address match */
6419 * Bits [1:0] are RES0.
6421 * It is IMPLEMENTATION DEFINED whether bits [63:49]
6422 * ([63:53] for FEAT_LVA) are hardwired to a copy of the sign bit
6423 * of the VA field ([48] or [52] for FEAT_LVA), or whether the
6424 * value is read as written. It is CONSTRAINED UNPREDICTABLE
6425 * whether the RESS bits are ignored when comparing an address.
6426 * Therefore we are allowed to compare the entire register, which
6427 * lets us avoid considering whether FEAT_LVA is actually enabled.
6429 * The BAS field is used to allow setting breakpoints on 16-bit
6430 * wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6431 * a bp will fire if the addresses covered by the bp and the addresses
6432 * covered by the insn overlap but the insn doesn't start at the
6433 * start of the bp address range. We choose to require the insn and
6434 * the bp to have the same address. The constraints on writing to
6435 * BAS enforced in dbgbcr_write mean we have only four cases:
6436 * 0b0000 => no breakpoint
6437 * 0b0011 => breakpoint on addr
6438 * 0b1100 => breakpoint on addr + 2
6439 * 0b1111 => breakpoint on addr
6440 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6442 int bas = extract64(bcr, 5, 4);
6443 addr = bvr & ~3ULL;
6444 if (bas == 0) {
6445 return;
6447 if (bas == 0xc) {
6448 addr += 2;
6450 break;
6452 case 2: /* unlinked context ID match */
6453 case 8: /* unlinked VMID match (reserved if no EL2) */
6454 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6455 qemu_log_mask(LOG_UNIMP,
6456 "arm: unlinked context breakpoint types not implemented\n");
6457 return;
6458 case 9: /* linked VMID match (reserved if no EL2) */
6459 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6460 case 3: /* linked context ID match */
6461 default:
6462 /* We must generate no events for Linked context matches (unless
6463 * they are linked to by some other bp/wp, which is handled in
6464 * updates for the linking bp/wp). We choose to also generate no events
6465 * for reserved values.
6467 return;
6470 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6473 void hw_breakpoint_update_all(ARMCPU *cpu)
6475 int i;
6476 CPUARMState *env = &cpu->env;
6478 /* Completely clear out existing QEMU breakpoints and our array, to
6479 * avoid possible stale entries following migration load.
6481 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6482 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6484 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6485 hw_breakpoint_update(cpu, i);
6489 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6490 uint64_t value)
6492 ARMCPU *cpu = env_archcpu(env);
6493 int i = ri->crm;
6495 raw_write(env, ri, value);
6496 hw_breakpoint_update(cpu, i);
6499 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6500 uint64_t value)
6502 ARMCPU *cpu = env_archcpu(env);
6503 int i = ri->crm;
6505 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6506 * copy of BAS[0].
6508 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6509 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6511 raw_write(env, ri, value);
6512 hw_breakpoint_update(cpu, i);
6515 static void define_debug_regs(ARMCPU *cpu)
6517 /* Define v7 and v8 architectural debug registers.
6518 * These are just dummy implementations for now.
6520 int i;
6521 int wrps, brps, ctx_cmps;
6524 * The Arm ARM says DBGDIDR is optional and deprecated if EL1 cannot
6525 * use AArch32. Given that bit 15 is RES1, if the value is 0 then
6526 * the register must not exist for this cpu.
6528 if (cpu->isar.dbgdidr != 0) {
6529 ARMCPRegInfo dbgdidr = {
6530 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0,
6531 .opc1 = 0, .opc2 = 0,
6532 .access = PL0_R, .accessfn = access_tda,
6533 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
6535 define_one_arm_cp_reg(cpu, &dbgdidr);
6538 /* Note that all these register fields hold "number of Xs minus 1". */
6539 brps = arm_num_brps(cpu);
6540 wrps = arm_num_wrps(cpu);
6541 ctx_cmps = arm_num_ctx_cmps(cpu);
6543 assert(ctx_cmps <= brps);
6545 define_arm_cp_regs(cpu, debug_cp_reginfo);
6547 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6548 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6551 for (i = 0; i < brps; i++) {
6552 ARMCPRegInfo dbgregs[] = {
6553 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6554 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
6555 .access = PL1_RW, .accessfn = access_tda,
6556 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6557 .writefn = dbgbvr_write, .raw_writefn = raw_write
6559 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6560 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
6561 .access = PL1_RW, .accessfn = access_tda,
6562 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6563 .writefn = dbgbcr_write, .raw_writefn = raw_write
6566 define_arm_cp_regs(cpu, dbgregs);
6569 for (i = 0; i < wrps; i++) {
6570 ARMCPRegInfo dbgregs[] = {
6571 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6572 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
6573 .access = PL1_RW, .accessfn = access_tda,
6574 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6575 .writefn = dbgwvr_write, .raw_writefn = raw_write
6577 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6578 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
6579 .access = PL1_RW, .accessfn = access_tda,
6580 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6581 .writefn = dbgwcr_write, .raw_writefn = raw_write
6584 define_arm_cp_regs(cpu, dbgregs);
6588 static void define_pmu_regs(ARMCPU *cpu)
6591 * v7 performance monitor control register: same implementor
6592 * field as main ID register, and we implement four counters in
6593 * addition to the cycle count register.
6595 unsigned int i, pmcrn = PMCR_NUM_COUNTERS;
6596 ARMCPRegInfo pmcr = {
6597 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6598 .access = PL0_RW,
6599 .type = ARM_CP_IO | ARM_CP_ALIAS,
6600 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6601 .accessfn = pmreg_access, .writefn = pmcr_write,
6602 .raw_writefn = raw_write,
6604 ARMCPRegInfo pmcr64 = {
6605 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6606 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6607 .access = PL0_RW, .accessfn = pmreg_access,
6608 .type = ARM_CP_IO,
6609 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6610 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6611 PMCRLC,
6612 .writefn = pmcr_write, .raw_writefn = raw_write,
6614 define_one_arm_cp_reg(cpu, &pmcr);
6615 define_one_arm_cp_reg(cpu, &pmcr64);
6616 for (i = 0; i < pmcrn; i++) {
6617 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6618 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6619 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6620 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6621 ARMCPRegInfo pmev_regs[] = {
6622 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6623 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6624 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6625 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6626 .accessfn = pmreg_access_xevcntr },
6627 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6628 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6629 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6630 .type = ARM_CP_IO,
6631 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6632 .raw_readfn = pmevcntr_rawread,
6633 .raw_writefn = pmevcntr_rawwrite },
6634 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6635 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6636 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6637 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6638 .accessfn = pmreg_access },
6639 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6640 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6641 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6642 .type = ARM_CP_IO,
6643 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6644 .raw_writefn = pmevtyper_rawwrite },
6646 define_arm_cp_regs(cpu, pmev_regs);
6647 g_free(pmevcntr_name);
6648 g_free(pmevcntr_el0_name);
6649 g_free(pmevtyper_name);
6650 g_free(pmevtyper_el0_name);
6652 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
6653 ARMCPRegInfo v81_pmu_regs[] = {
6654 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6655 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6656 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6657 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6658 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6659 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6660 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6661 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6663 define_arm_cp_regs(cpu, v81_pmu_regs);
6665 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6666 static const ARMCPRegInfo v84_pmmir = {
6667 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6668 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6669 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6670 .resetvalue = 0
6672 define_one_arm_cp_reg(cpu, &v84_pmmir);
6676 /* We don't know until after realize whether there's a GICv3
6677 * attached, and that is what registers the gicv3 sysregs.
6678 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6679 * at runtime.
6681 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6683 ARMCPU *cpu = env_archcpu(env);
6684 uint64_t pfr1 = cpu->isar.id_pfr1;
6686 if (env->gicv3state) {
6687 pfr1 |= 1 << 28;
6689 return pfr1;
6692 #ifndef CONFIG_USER_ONLY
6693 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6695 ARMCPU *cpu = env_archcpu(env);
6696 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6698 if (env->gicv3state) {
6699 pfr0 |= 1 << 24;
6701 return pfr0;
6703 #endif
6705 /* Shared logic between LORID and the rest of the LOR* registers.
6706 * Secure state exclusion has already been dealt with.
6708 static CPAccessResult access_lor_ns(CPUARMState *env,
6709 const ARMCPRegInfo *ri, bool isread)
6711 int el = arm_current_el(env);
6713 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6714 return CP_ACCESS_TRAP_EL2;
6716 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6717 return CP_ACCESS_TRAP_EL3;
6719 return CP_ACCESS_OK;
6722 static CPAccessResult access_lor_other(CPUARMState *env,
6723 const ARMCPRegInfo *ri, bool isread)
6725 if (arm_is_secure_below_el3(env)) {
6726 /* Access denied in secure mode. */
6727 return CP_ACCESS_TRAP;
6729 return access_lor_ns(env, ri, isread);
6733 * A trivial implementation of ARMv8.1-LOR leaves all of these
6734 * registers fixed at 0, which indicates that there are zero
6735 * supported Limited Ordering regions.
6737 static const ARMCPRegInfo lor_reginfo[] = {
6738 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6739 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6740 .access = PL1_RW, .accessfn = access_lor_other,
6741 .type = ARM_CP_CONST, .resetvalue = 0 },
6742 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6743 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6744 .access = PL1_RW, .accessfn = access_lor_other,
6745 .type = ARM_CP_CONST, .resetvalue = 0 },
6746 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6747 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6748 .access = PL1_RW, .accessfn = access_lor_other,
6749 .type = ARM_CP_CONST, .resetvalue = 0 },
6750 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6751 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6752 .access = PL1_RW, .accessfn = access_lor_other,
6753 .type = ARM_CP_CONST, .resetvalue = 0 },
6754 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6755 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6756 .access = PL1_R, .accessfn = access_lor_ns,
6757 .type = ARM_CP_CONST, .resetvalue = 0 },
6760 #ifdef TARGET_AARCH64
6761 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6762 bool isread)
6764 int el = arm_current_el(env);
6766 if (el < 2 &&
6767 arm_feature(env, ARM_FEATURE_EL2) &&
6768 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6769 return CP_ACCESS_TRAP_EL2;
6771 if (el < 3 &&
6772 arm_feature(env, ARM_FEATURE_EL3) &&
6773 !(env->cp15.scr_el3 & SCR_APK)) {
6774 return CP_ACCESS_TRAP_EL3;
6776 return CP_ACCESS_OK;
6779 static const ARMCPRegInfo pauth_reginfo[] = {
6780 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6781 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6782 .access = PL1_RW, .accessfn = access_pauth,
6783 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6784 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6785 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6786 .access = PL1_RW, .accessfn = access_pauth,
6787 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6788 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6789 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6790 .access = PL1_RW, .accessfn = access_pauth,
6791 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6792 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6793 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6794 .access = PL1_RW, .accessfn = access_pauth,
6795 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6796 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6797 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6798 .access = PL1_RW, .accessfn = access_pauth,
6799 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6800 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6801 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6802 .access = PL1_RW, .accessfn = access_pauth,
6803 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6804 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6805 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6806 .access = PL1_RW, .accessfn = access_pauth,
6807 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6808 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6809 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6810 .access = PL1_RW, .accessfn = access_pauth,
6811 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6812 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6813 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6814 .access = PL1_RW, .accessfn = access_pauth,
6815 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6816 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6817 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6818 .access = PL1_RW, .accessfn = access_pauth,
6819 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6822 static const ARMCPRegInfo tlbirange_reginfo[] = {
6823 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6824 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6825 .access = PL1_W, .type = ARM_CP_NO_RAW,
6826 .writefn = tlbi_aa64_rvae1is_write },
6827 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6828 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6829 .access = PL1_W, .type = ARM_CP_NO_RAW,
6830 .writefn = tlbi_aa64_rvae1is_write },
6831 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6832 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6833 .access = PL1_W, .type = ARM_CP_NO_RAW,
6834 .writefn = tlbi_aa64_rvae1is_write },
6835 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6836 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6837 .access = PL1_W, .type = ARM_CP_NO_RAW,
6838 .writefn = tlbi_aa64_rvae1is_write },
6839 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6840 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6841 .access = PL1_W, .type = ARM_CP_NO_RAW,
6842 .writefn = tlbi_aa64_rvae1is_write },
6843 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6844 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6845 .access = PL1_W, .type = ARM_CP_NO_RAW,
6846 .writefn = tlbi_aa64_rvae1is_write },
6847 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6848 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6849 .access = PL1_W, .type = ARM_CP_NO_RAW,
6850 .writefn = tlbi_aa64_rvae1is_write },
6851 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6852 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6853 .access = PL1_W, .type = ARM_CP_NO_RAW,
6854 .writefn = tlbi_aa64_rvae1is_write },
6855 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6856 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6857 .access = PL1_W, .type = ARM_CP_NO_RAW,
6858 .writefn = tlbi_aa64_rvae1_write },
6859 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6860 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6861 .access = PL1_W, .type = ARM_CP_NO_RAW,
6862 .writefn = tlbi_aa64_rvae1_write },
6863 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6864 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6865 .access = PL1_W, .type = ARM_CP_NO_RAW,
6866 .writefn = tlbi_aa64_rvae1_write },
6867 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6868 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6869 .access = PL1_W, .type = ARM_CP_NO_RAW,
6870 .writefn = tlbi_aa64_rvae1_write },
6871 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6872 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6873 .access = PL2_W, .type = ARM_CP_NOP },
6874 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6875 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6876 .access = PL2_W, .type = ARM_CP_NOP },
6877 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6878 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
6879 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6880 .writefn = tlbi_aa64_rvae2is_write },
6881 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6882 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
6883 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6884 .writefn = tlbi_aa64_rvae2is_write },
6885 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6886 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6887 .access = PL2_W, .type = ARM_CP_NOP },
6888 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6889 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6890 .access = PL2_W, .type = ARM_CP_NOP },
6891 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6892 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
6893 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6894 .writefn = tlbi_aa64_rvae2is_write },
6895 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6896 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
6897 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6898 .writefn = tlbi_aa64_rvae2is_write },
6899 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6900 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
6901 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6902 .writefn = tlbi_aa64_rvae2_write },
6903 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6904 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
6905 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6906 .writefn = tlbi_aa64_rvae2_write },
6907 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6908 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6909 .access = PL3_W, .type = ARM_CP_NO_RAW,
6910 .writefn = tlbi_aa64_rvae3is_write },
6911 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6912 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6913 .access = PL3_W, .type = ARM_CP_NO_RAW,
6914 .writefn = tlbi_aa64_rvae3is_write },
6915 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6916 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6917 .access = PL3_W, .type = ARM_CP_NO_RAW,
6918 .writefn = tlbi_aa64_rvae3is_write },
6919 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6920 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6921 .access = PL3_W, .type = ARM_CP_NO_RAW,
6922 .writefn = tlbi_aa64_rvae3is_write },
6923 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6924 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6925 .access = PL3_W, .type = ARM_CP_NO_RAW,
6926 .writefn = tlbi_aa64_rvae3_write },
6927 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6928 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6929 .access = PL3_W, .type = ARM_CP_NO_RAW,
6930 .writefn = tlbi_aa64_rvae3_write },
6933 static const ARMCPRegInfo tlbios_reginfo[] = {
6934 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6935 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6936 .access = PL1_W, .type = ARM_CP_NO_RAW,
6937 .writefn = tlbi_aa64_vmalle1is_write },
6938 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6939 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6940 .access = PL1_W, .type = ARM_CP_NO_RAW,
6941 .writefn = tlbi_aa64_vae1is_write },
6942 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6943 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6944 .access = PL1_W, .type = ARM_CP_NO_RAW,
6945 .writefn = tlbi_aa64_vmalle1is_write },
6946 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6947 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6948 .access = PL1_W, .type = ARM_CP_NO_RAW,
6949 .writefn = tlbi_aa64_vae1is_write },
6950 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6951 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6952 .access = PL1_W, .type = ARM_CP_NO_RAW,
6953 .writefn = tlbi_aa64_vae1is_write },
6954 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6955 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6956 .access = PL1_W, .type = ARM_CP_NO_RAW,
6957 .writefn = tlbi_aa64_vae1is_write },
6958 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6959 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
6960 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6961 .writefn = tlbi_aa64_alle2is_write },
6962 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6963 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
6964 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6965 .writefn = tlbi_aa64_vae2is_write },
6966 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6967 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6968 .access = PL2_W, .type = ARM_CP_NO_RAW,
6969 .writefn = tlbi_aa64_alle1is_write },
6970 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6971 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
6972 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6973 .writefn = tlbi_aa64_vae2is_write },
6974 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6975 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6976 .access = PL2_W, .type = ARM_CP_NO_RAW,
6977 .writefn = tlbi_aa64_alle1is_write },
6978 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6979 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6980 .access = PL2_W, .type = ARM_CP_NOP },
6981 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6982 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6983 .access = PL2_W, .type = ARM_CP_NOP },
6984 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6985 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
6986 .access = PL2_W, .type = ARM_CP_NOP },
6987 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6988 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
6989 .access = PL2_W, .type = ARM_CP_NOP },
6990 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
6991 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
6992 .access = PL3_W, .type = ARM_CP_NO_RAW,
6993 .writefn = tlbi_aa64_alle3is_write },
6994 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
6995 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
6996 .access = PL3_W, .type = ARM_CP_NO_RAW,
6997 .writefn = tlbi_aa64_vae3is_write },
6998 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
6999 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7000 .access = PL3_W, .type = ARM_CP_NO_RAW,
7001 .writefn = tlbi_aa64_vae3is_write },
7004 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7006 Error *err = NULL;
7007 uint64_t ret;
7009 /* Success sets NZCV = 0000. */
7010 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7012 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7014 * ??? Failed, for unknown reasons in the crypto subsystem.
7015 * The best we can do is log the reason and return the
7016 * timed-out indication to the guest. There is no reason
7017 * we know to expect this failure to be transitory, so the
7018 * guest may well hang retrying the operation.
7020 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7021 ri->name, error_get_pretty(err));
7022 error_free(err);
7024 env->ZF = 0; /* NZCF = 0100 */
7025 return 0;
7027 return ret;
7030 /* We do not support re-seeding, so the two registers operate the same. */
7031 static const ARMCPRegInfo rndr_reginfo[] = {
7032 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7033 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7034 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7035 .access = PL0_R, .readfn = rndr_readfn },
7036 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7037 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7038 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7039 .access = PL0_R, .readfn = rndr_readfn },
7042 #ifndef CONFIG_USER_ONLY
7043 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7044 uint64_t value)
7046 ARMCPU *cpu = env_archcpu(env);
7047 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7048 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7049 uint64_t vaddr_in = (uint64_t) value;
7050 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7051 void *haddr;
7052 int mem_idx = cpu_mmu_index(env, false);
7054 /* This won't be crossing page boundaries */
7055 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7056 if (haddr) {
7058 ram_addr_t offset;
7059 MemoryRegion *mr;
7061 /* RCU lock is already being held */
7062 mr = memory_region_from_host(haddr, &offset);
7064 if (mr) {
7065 memory_region_writeback(mr, offset, dline_size);
7070 static const ARMCPRegInfo dcpop_reg[] = {
7071 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7072 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7073 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7074 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7077 static const ARMCPRegInfo dcpodp_reg[] = {
7078 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7079 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7080 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7081 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7083 #endif /*CONFIG_USER_ONLY*/
7085 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7086 bool isread)
7088 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7089 return CP_ACCESS_TRAP_EL2;
7092 return CP_ACCESS_OK;
7095 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7096 bool isread)
7098 int el = arm_current_el(env);
7100 if (el < 2 && arm_is_el2_enabled(env)) {
7101 uint64_t hcr = arm_hcr_el2_eff(env);
7102 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7103 return CP_ACCESS_TRAP_EL2;
7106 if (el < 3 &&
7107 arm_feature(env, ARM_FEATURE_EL3) &&
7108 !(env->cp15.scr_el3 & SCR_ATA)) {
7109 return CP_ACCESS_TRAP_EL3;
7111 return CP_ACCESS_OK;
7114 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7116 return env->pstate & PSTATE_TCO;
7119 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7121 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7124 static const ARMCPRegInfo mte_reginfo[] = {
7125 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7126 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7127 .access = PL1_RW, .accessfn = access_mte,
7128 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7129 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7130 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7131 .access = PL1_RW, .accessfn = access_mte,
7132 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7133 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7134 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7135 .access = PL2_RW, .accessfn = access_mte,
7136 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7137 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7138 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7139 .access = PL3_RW,
7140 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7141 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7142 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7143 .access = PL1_RW, .accessfn = access_mte,
7144 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7145 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7146 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7147 .access = PL1_RW, .accessfn = access_mte,
7148 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7149 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7150 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7151 .access = PL1_R, .accessfn = access_aa64_tid5,
7152 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7153 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7154 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7155 .type = ARM_CP_NO_RAW,
7156 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7157 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7158 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7159 .type = ARM_CP_NOP, .access = PL1_W,
7160 .accessfn = aa64_cacheop_poc_access },
7161 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7162 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7163 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7164 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7165 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7166 .type = ARM_CP_NOP, .access = PL1_W,
7167 .accessfn = aa64_cacheop_poc_access },
7168 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7169 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7170 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7171 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7172 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7173 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7174 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7175 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7176 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7177 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7178 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7179 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7180 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7181 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7182 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7185 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7186 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7187 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7188 .type = ARM_CP_CONST, .access = PL0_RW, },
7191 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7192 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7193 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7194 .type = ARM_CP_NOP, .access = PL0_W,
7195 .accessfn = aa64_cacheop_poc_access },
7196 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7197 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7198 .type = ARM_CP_NOP, .access = PL0_W,
7199 .accessfn = aa64_cacheop_poc_access },
7200 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7201 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7202 .type = ARM_CP_NOP, .access = PL0_W,
7203 .accessfn = aa64_cacheop_poc_access },
7204 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7205 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7206 .type = ARM_CP_NOP, .access = PL0_W,
7207 .accessfn = aa64_cacheop_poc_access },
7208 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7209 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7210 .type = ARM_CP_NOP, .access = PL0_W,
7211 .accessfn = aa64_cacheop_poc_access },
7212 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7213 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7214 .type = ARM_CP_NOP, .access = PL0_W,
7215 .accessfn = aa64_cacheop_poc_access },
7216 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7217 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7218 .type = ARM_CP_NOP, .access = PL0_W,
7219 .accessfn = aa64_cacheop_poc_access },
7220 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7221 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7222 .type = ARM_CP_NOP, .access = PL0_W,
7223 .accessfn = aa64_cacheop_poc_access },
7224 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7225 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7226 .access = PL0_W, .type = ARM_CP_DC_GVA,
7227 #ifndef CONFIG_USER_ONLY
7228 /* Avoid overhead of an access check that always passes in user-mode */
7229 .accessfn = aa64_zva_access,
7230 #endif
7232 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7233 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7234 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7235 #ifndef CONFIG_USER_ONLY
7236 /* Avoid overhead of an access check that always passes in user-mode */
7237 .accessfn = aa64_zva_access,
7238 #endif
7242 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7243 bool isread)
7245 uint64_t hcr = arm_hcr_el2_eff(env);
7246 int el = arm_current_el(env);
7248 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7249 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7250 if (hcr & HCR_TGE) {
7251 return CP_ACCESS_TRAP_EL2;
7253 return CP_ACCESS_TRAP;
7255 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7256 return CP_ACCESS_TRAP_EL2;
7258 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7259 return CP_ACCESS_TRAP_EL2;
7261 if (el < 3
7262 && arm_feature(env, ARM_FEATURE_EL3)
7263 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7264 return CP_ACCESS_TRAP_EL3;
7266 return CP_ACCESS_OK;
7269 static const ARMCPRegInfo scxtnum_reginfo[] = {
7270 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7271 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7272 .access = PL0_RW, .accessfn = access_scxtnum,
7273 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7274 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7275 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7276 .access = PL1_RW, .accessfn = access_scxtnum,
7277 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7278 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7279 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7280 .access = PL2_RW, .accessfn = access_scxtnum,
7281 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7282 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7283 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7284 .access = PL3_RW,
7285 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7287 #endif /* TARGET_AARCH64 */
7289 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7290 bool isread)
7292 int el = arm_current_el(env);
7294 if (el == 0) {
7295 uint64_t sctlr = arm_sctlr(env, el);
7296 if (!(sctlr & SCTLR_EnRCTX)) {
7297 return CP_ACCESS_TRAP;
7299 } else if (el == 1) {
7300 uint64_t hcr = arm_hcr_el2_eff(env);
7301 if (hcr & HCR_NV) {
7302 return CP_ACCESS_TRAP_EL2;
7305 return CP_ACCESS_OK;
7308 static const ARMCPRegInfo predinv_reginfo[] = {
7309 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7310 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7311 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7312 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7313 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7314 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7315 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7316 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7317 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7319 * Note the AArch32 opcodes have a different OPC1.
7321 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7322 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7323 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7324 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7325 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7326 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7327 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7328 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7329 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7332 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7334 /* Read the high 32 bits of the current CCSIDR */
7335 return extract64(ccsidr_read(env, ri), 32, 32);
7338 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7339 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7340 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7341 .access = PL1_R,
7342 .accessfn = access_aa64_tid2,
7343 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7346 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7347 bool isread)
7349 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7350 return CP_ACCESS_TRAP_EL2;
7353 return CP_ACCESS_OK;
7356 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7357 bool isread)
7359 if (arm_feature(env, ARM_FEATURE_V8)) {
7360 return access_aa64_tid3(env, ri, isread);
7363 return CP_ACCESS_OK;
7366 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7367 bool isread)
7369 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7370 return CP_ACCESS_TRAP_EL2;
7373 return CP_ACCESS_OK;
7376 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7377 const ARMCPRegInfo *ri, bool isread)
7380 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7381 * in v7A, not in v8A.
7383 if (!arm_feature(env, ARM_FEATURE_V8) &&
7384 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7385 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7386 return CP_ACCESS_TRAP_EL2;
7388 return CP_ACCESS_OK;
7391 static const ARMCPRegInfo jazelle_regs[] = {
7392 { .name = "JIDR",
7393 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7394 .access = PL1_R, .accessfn = access_jazelle,
7395 .type = ARM_CP_CONST, .resetvalue = 0 },
7396 { .name = "JOSCR",
7397 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7398 .accessfn = access_joscr_jmcr,
7399 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7400 { .name = "JMCR",
7401 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7402 .accessfn = access_joscr_jmcr,
7403 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7406 static const ARMCPRegInfo contextidr_el2 = {
7407 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7408 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7409 .access = PL2_RW,
7410 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7413 static const ARMCPRegInfo vhe_reginfo[] = {
7414 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7415 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7416 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7417 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7418 #ifndef CONFIG_USER_ONLY
7419 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7420 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7421 .fieldoffset =
7422 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7423 .type = ARM_CP_IO, .access = PL2_RW,
7424 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7425 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7426 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7427 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7428 .resetfn = gt_hv_timer_reset,
7429 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7430 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7431 .type = ARM_CP_IO,
7432 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7433 .access = PL2_RW,
7434 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7435 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7436 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7437 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7438 .type = ARM_CP_IO | ARM_CP_ALIAS,
7439 .access = PL2_RW, .accessfn = e2h_access,
7440 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7441 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7442 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7443 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7444 .type = ARM_CP_IO | ARM_CP_ALIAS,
7445 .access = PL2_RW, .accessfn = e2h_access,
7446 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7447 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7448 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7449 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7450 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7451 .access = PL2_RW, .accessfn = e2h_access,
7452 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7453 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7454 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7455 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7456 .access = PL2_RW, .accessfn = e2h_access,
7457 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7458 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7459 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7460 .type = ARM_CP_IO | ARM_CP_ALIAS,
7461 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7462 .access = PL2_RW, .accessfn = e2h_access,
7463 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7464 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7465 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7466 .type = ARM_CP_IO | ARM_CP_ALIAS,
7467 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7468 .access = PL2_RW, .accessfn = e2h_access,
7469 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7470 #endif
7473 #ifndef CONFIG_USER_ONLY
7474 static const ARMCPRegInfo ats1e1_reginfo[] = {
7475 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7476 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7477 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7478 .writefn = ats_write64 },
7479 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7480 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7481 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7482 .writefn = ats_write64 },
7485 static const ARMCPRegInfo ats1cp_reginfo[] = {
7486 { .name = "ATS1CPRP",
7487 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7488 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7489 .writefn = ats_write },
7490 { .name = "ATS1CPWP",
7491 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7492 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7493 .writefn = ats_write },
7495 #endif
7498 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7499 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7500 * is non-zero, which is never for ARMv7, optionally in ARMv8
7501 * and mandatorily for ARMv8.2 and up.
7502 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7503 * implementation is RAZ/WI we can ignore this detail, as we
7504 * do for ACTLR.
7506 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7507 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7508 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7509 .access = PL1_RW, .accessfn = access_tacr,
7510 .type = ARM_CP_CONST, .resetvalue = 0 },
7511 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7512 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7513 .access = PL2_RW, .type = ARM_CP_CONST,
7514 .resetvalue = 0 },
7517 void register_cp_regs_for_features(ARMCPU *cpu)
7519 /* Register all the coprocessor registers based on feature bits */
7520 CPUARMState *env = &cpu->env;
7521 if (arm_feature(env, ARM_FEATURE_M)) {
7522 /* M profile has no coprocessor registers */
7523 return;
7526 define_arm_cp_regs(cpu, cp_reginfo);
7527 if (!arm_feature(env, ARM_FEATURE_V8)) {
7528 /* Must go early as it is full of wildcards that may be
7529 * overridden by later definitions.
7531 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7534 if (arm_feature(env, ARM_FEATURE_V6)) {
7535 /* The ID registers all have impdef reset values */
7536 ARMCPRegInfo v6_idregs[] = {
7537 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7538 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7539 .access = PL1_R, .type = ARM_CP_CONST,
7540 .accessfn = access_aa32_tid3,
7541 .resetvalue = cpu->isar.id_pfr0 },
7542 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7543 * the value of the GIC field until after we define these regs.
7545 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7546 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7547 .access = PL1_R, .type = ARM_CP_NO_RAW,
7548 .accessfn = access_aa32_tid3,
7549 .readfn = id_pfr1_read,
7550 .writefn = arm_cp_write_ignore },
7551 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7552 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7553 .access = PL1_R, .type = ARM_CP_CONST,
7554 .accessfn = access_aa32_tid3,
7555 .resetvalue = cpu->isar.id_dfr0 },
7556 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7557 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7558 .access = PL1_R, .type = ARM_CP_CONST,
7559 .accessfn = access_aa32_tid3,
7560 .resetvalue = cpu->id_afr0 },
7561 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7562 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7563 .access = PL1_R, .type = ARM_CP_CONST,
7564 .accessfn = access_aa32_tid3,
7565 .resetvalue = cpu->isar.id_mmfr0 },
7566 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7567 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7568 .access = PL1_R, .type = ARM_CP_CONST,
7569 .accessfn = access_aa32_tid3,
7570 .resetvalue = cpu->isar.id_mmfr1 },
7571 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7572 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7573 .access = PL1_R, .type = ARM_CP_CONST,
7574 .accessfn = access_aa32_tid3,
7575 .resetvalue = cpu->isar.id_mmfr2 },
7576 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7577 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7578 .access = PL1_R, .type = ARM_CP_CONST,
7579 .accessfn = access_aa32_tid3,
7580 .resetvalue = cpu->isar.id_mmfr3 },
7581 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7582 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7583 .access = PL1_R, .type = ARM_CP_CONST,
7584 .accessfn = access_aa32_tid3,
7585 .resetvalue = cpu->isar.id_isar0 },
7586 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7587 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7588 .access = PL1_R, .type = ARM_CP_CONST,
7589 .accessfn = access_aa32_tid3,
7590 .resetvalue = cpu->isar.id_isar1 },
7591 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7592 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7593 .access = PL1_R, .type = ARM_CP_CONST,
7594 .accessfn = access_aa32_tid3,
7595 .resetvalue = cpu->isar.id_isar2 },
7596 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7597 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7598 .access = PL1_R, .type = ARM_CP_CONST,
7599 .accessfn = access_aa32_tid3,
7600 .resetvalue = cpu->isar.id_isar3 },
7601 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7602 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7603 .access = PL1_R, .type = ARM_CP_CONST,
7604 .accessfn = access_aa32_tid3,
7605 .resetvalue = cpu->isar.id_isar4 },
7606 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7607 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7608 .access = PL1_R, .type = ARM_CP_CONST,
7609 .accessfn = access_aa32_tid3,
7610 .resetvalue = cpu->isar.id_isar5 },
7611 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7612 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7613 .access = PL1_R, .type = ARM_CP_CONST,
7614 .accessfn = access_aa32_tid3,
7615 .resetvalue = cpu->isar.id_mmfr4 },
7616 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7617 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7618 .access = PL1_R, .type = ARM_CP_CONST,
7619 .accessfn = access_aa32_tid3,
7620 .resetvalue = cpu->isar.id_isar6 },
7622 define_arm_cp_regs(cpu, v6_idregs);
7623 define_arm_cp_regs(cpu, v6_cp_reginfo);
7624 } else {
7625 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7627 if (arm_feature(env, ARM_FEATURE_V6K)) {
7628 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7630 if (arm_feature(env, ARM_FEATURE_V7MP) &&
7631 !arm_feature(env, ARM_FEATURE_PMSA)) {
7632 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7634 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7635 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7637 if (arm_feature(env, ARM_FEATURE_V7)) {
7638 ARMCPRegInfo clidr = {
7639 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7640 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7641 .access = PL1_R, .type = ARM_CP_CONST,
7642 .accessfn = access_aa64_tid2,
7643 .resetvalue = cpu->clidr
7645 define_one_arm_cp_reg(cpu, &clidr);
7646 define_arm_cp_regs(cpu, v7_cp_reginfo);
7647 define_debug_regs(cpu);
7648 define_pmu_regs(cpu);
7649 } else {
7650 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7652 if (arm_feature(env, ARM_FEATURE_V8)) {
7653 /* AArch64 ID registers, which all have impdef reset values.
7654 * Note that within the ID register ranges the unused slots
7655 * must all RAZ, not UNDEF; future architecture versions may
7656 * define new registers here.
7658 ARMCPRegInfo v8_idregs[] = {
7660 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7661 * emulation because we don't know the right value for the
7662 * GIC field until after we define these regs.
7664 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7665 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7666 .access = PL1_R,
7667 #ifdef CONFIG_USER_ONLY
7668 .type = ARM_CP_CONST,
7669 .resetvalue = cpu->isar.id_aa64pfr0
7670 #else
7671 .type = ARM_CP_NO_RAW,
7672 .accessfn = access_aa64_tid3,
7673 .readfn = id_aa64pfr0_read,
7674 .writefn = arm_cp_write_ignore
7675 #endif
7677 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7678 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7679 .access = PL1_R, .type = ARM_CP_CONST,
7680 .accessfn = access_aa64_tid3,
7681 .resetvalue = cpu->isar.id_aa64pfr1},
7682 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7683 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7684 .access = PL1_R, .type = ARM_CP_CONST,
7685 .accessfn = access_aa64_tid3,
7686 .resetvalue = 0 },
7687 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7688 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7689 .access = PL1_R, .type = ARM_CP_CONST,
7690 .accessfn = access_aa64_tid3,
7691 .resetvalue = 0 },
7692 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7693 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7694 .access = PL1_R, .type = ARM_CP_CONST,
7695 .accessfn = access_aa64_tid3,
7696 .resetvalue = cpu->isar.id_aa64zfr0 },
7697 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7698 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7699 .access = PL1_R, .type = ARM_CP_CONST,
7700 .accessfn = access_aa64_tid3,
7701 .resetvalue = 0 },
7702 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7703 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7704 .access = PL1_R, .type = ARM_CP_CONST,
7705 .accessfn = access_aa64_tid3,
7706 .resetvalue = 0 },
7707 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7708 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7709 .access = PL1_R, .type = ARM_CP_CONST,
7710 .accessfn = access_aa64_tid3,
7711 .resetvalue = 0 },
7712 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7713 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7714 .access = PL1_R, .type = ARM_CP_CONST,
7715 .accessfn = access_aa64_tid3,
7716 .resetvalue = cpu->isar.id_aa64dfr0 },
7717 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7718 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7719 .access = PL1_R, .type = ARM_CP_CONST,
7720 .accessfn = access_aa64_tid3,
7721 .resetvalue = cpu->isar.id_aa64dfr1 },
7722 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7723 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7724 .access = PL1_R, .type = ARM_CP_CONST,
7725 .accessfn = access_aa64_tid3,
7726 .resetvalue = 0 },
7727 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7728 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7729 .access = PL1_R, .type = ARM_CP_CONST,
7730 .accessfn = access_aa64_tid3,
7731 .resetvalue = 0 },
7732 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7733 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7734 .access = PL1_R, .type = ARM_CP_CONST,
7735 .accessfn = access_aa64_tid3,
7736 .resetvalue = cpu->id_aa64afr0 },
7737 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7738 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7739 .access = PL1_R, .type = ARM_CP_CONST,
7740 .accessfn = access_aa64_tid3,
7741 .resetvalue = cpu->id_aa64afr1 },
7742 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7743 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7744 .access = PL1_R, .type = ARM_CP_CONST,
7745 .accessfn = access_aa64_tid3,
7746 .resetvalue = 0 },
7747 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7748 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7749 .access = PL1_R, .type = ARM_CP_CONST,
7750 .accessfn = access_aa64_tid3,
7751 .resetvalue = 0 },
7752 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7753 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7754 .access = PL1_R, .type = ARM_CP_CONST,
7755 .accessfn = access_aa64_tid3,
7756 .resetvalue = cpu->isar.id_aa64isar0 },
7757 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7758 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7759 .access = PL1_R, .type = ARM_CP_CONST,
7760 .accessfn = access_aa64_tid3,
7761 .resetvalue = cpu->isar.id_aa64isar1 },
7762 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7763 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7764 .access = PL1_R, .type = ARM_CP_CONST,
7765 .accessfn = access_aa64_tid3,
7766 .resetvalue = 0 },
7767 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7768 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7769 .access = PL1_R, .type = ARM_CP_CONST,
7770 .accessfn = access_aa64_tid3,
7771 .resetvalue = 0 },
7772 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7773 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7774 .access = PL1_R, .type = ARM_CP_CONST,
7775 .accessfn = access_aa64_tid3,
7776 .resetvalue = 0 },
7777 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7778 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7779 .access = PL1_R, .type = ARM_CP_CONST,
7780 .accessfn = access_aa64_tid3,
7781 .resetvalue = 0 },
7782 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7783 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7784 .access = PL1_R, .type = ARM_CP_CONST,
7785 .accessfn = access_aa64_tid3,
7786 .resetvalue = 0 },
7787 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7789 .access = PL1_R, .type = ARM_CP_CONST,
7790 .accessfn = access_aa64_tid3,
7791 .resetvalue = 0 },
7792 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7793 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7794 .access = PL1_R, .type = ARM_CP_CONST,
7795 .accessfn = access_aa64_tid3,
7796 .resetvalue = cpu->isar.id_aa64mmfr0 },
7797 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7798 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7799 .access = PL1_R, .type = ARM_CP_CONST,
7800 .accessfn = access_aa64_tid3,
7801 .resetvalue = cpu->isar.id_aa64mmfr1 },
7802 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7803 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7804 .access = PL1_R, .type = ARM_CP_CONST,
7805 .accessfn = access_aa64_tid3,
7806 .resetvalue = cpu->isar.id_aa64mmfr2 },
7807 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7808 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7809 .access = PL1_R, .type = ARM_CP_CONST,
7810 .accessfn = access_aa64_tid3,
7811 .resetvalue = 0 },
7812 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7813 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7814 .access = PL1_R, .type = ARM_CP_CONST,
7815 .accessfn = access_aa64_tid3,
7816 .resetvalue = 0 },
7817 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7818 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7819 .access = PL1_R, .type = ARM_CP_CONST,
7820 .accessfn = access_aa64_tid3,
7821 .resetvalue = 0 },
7822 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7823 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7824 .access = PL1_R, .type = ARM_CP_CONST,
7825 .accessfn = access_aa64_tid3,
7826 .resetvalue = 0 },
7827 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7828 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7829 .access = PL1_R, .type = ARM_CP_CONST,
7830 .accessfn = access_aa64_tid3,
7831 .resetvalue = 0 },
7832 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7833 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7834 .access = PL1_R, .type = ARM_CP_CONST,
7835 .accessfn = access_aa64_tid3,
7836 .resetvalue = cpu->isar.mvfr0 },
7837 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7838 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7839 .access = PL1_R, .type = ARM_CP_CONST,
7840 .accessfn = access_aa64_tid3,
7841 .resetvalue = cpu->isar.mvfr1 },
7842 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7843 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7844 .access = PL1_R, .type = ARM_CP_CONST,
7845 .accessfn = access_aa64_tid3,
7846 .resetvalue = cpu->isar.mvfr2 },
7847 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7848 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7849 .access = PL1_R, .type = ARM_CP_CONST,
7850 .accessfn = access_aa64_tid3,
7851 .resetvalue = 0 },
7852 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
7853 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7854 .access = PL1_R, .type = ARM_CP_CONST,
7855 .accessfn = access_aa64_tid3,
7856 .resetvalue = cpu->isar.id_pfr2 },
7857 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7858 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7859 .access = PL1_R, .type = ARM_CP_CONST,
7860 .accessfn = access_aa64_tid3,
7861 .resetvalue = 0 },
7862 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7863 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7864 .access = PL1_R, .type = ARM_CP_CONST,
7865 .accessfn = access_aa64_tid3,
7866 .resetvalue = 0 },
7867 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7868 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7869 .access = PL1_R, .type = ARM_CP_CONST,
7870 .accessfn = access_aa64_tid3,
7871 .resetvalue = 0 },
7872 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7873 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7874 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7875 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
7876 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7877 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7878 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7879 .resetvalue = cpu->pmceid0 },
7880 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7881 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7882 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7883 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
7884 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7885 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7886 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7887 .resetvalue = cpu->pmceid1 },
7889 #ifdef CONFIG_USER_ONLY
7890 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7891 { .name = "ID_AA64PFR0_EL1",
7892 .exported_bits = 0x000f000f00ff0000,
7893 .fixed_bits = 0x0000000000000011 },
7894 { .name = "ID_AA64PFR1_EL1",
7895 .exported_bits = 0x00000000000000f0 },
7896 { .name = "ID_AA64PFR*_EL1_RESERVED",
7897 .is_glob = true },
7898 { .name = "ID_AA64ZFR0_EL1" },
7899 { .name = "ID_AA64MMFR0_EL1",
7900 .fixed_bits = 0x00000000ff000000 },
7901 { .name = "ID_AA64MMFR1_EL1" },
7902 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7903 .is_glob = true },
7904 { .name = "ID_AA64DFR0_EL1",
7905 .fixed_bits = 0x0000000000000006 },
7906 { .name = "ID_AA64DFR1_EL1" },
7907 { .name = "ID_AA64DFR*_EL1_RESERVED",
7908 .is_glob = true },
7909 { .name = "ID_AA64AFR*",
7910 .is_glob = true },
7911 { .name = "ID_AA64ISAR0_EL1",
7912 .exported_bits = 0x00fffffff0fffff0 },
7913 { .name = "ID_AA64ISAR1_EL1",
7914 .exported_bits = 0x000000f0ffffffff },
7915 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7916 .is_glob = true },
7918 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7919 #endif
7920 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7921 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7922 !arm_feature(env, ARM_FEATURE_EL2)) {
7923 ARMCPRegInfo rvbar = {
7924 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7925 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7926 .access = PL1_R,
7927 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7929 define_one_arm_cp_reg(cpu, &rvbar);
7931 define_arm_cp_regs(cpu, v8_idregs);
7932 define_arm_cp_regs(cpu, v8_cp_reginfo);
7936 * Register the base EL2 cpregs.
7937 * Pre v8, these registers are implemented only as part of the
7938 * Virtualization Extensions (EL2 present). Beginning with v8,
7939 * if EL2 is missing but EL3 is enabled, mostly these become
7940 * RES0 from EL3, with some specific exceptions.
7942 if (arm_feature(env, ARM_FEATURE_EL2)
7943 || (arm_feature(env, ARM_FEATURE_EL3)
7944 && arm_feature(env, ARM_FEATURE_V8))) {
7945 uint64_t vmpidr_def = mpidr_read_val(env);
7946 ARMCPRegInfo vpidr_regs[] = {
7947 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7948 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7949 .access = PL2_RW, .accessfn = access_el3_aa32ns,
7950 .resetvalue = cpu->midr,
7951 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7952 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
7953 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7954 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7955 .access = PL2_RW, .resetvalue = cpu->midr,
7956 .type = ARM_CP_EL3_NO_EL2_C_NZ,
7957 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
7958 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7959 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7960 .access = PL2_RW, .accessfn = access_el3_aa32ns,
7961 .resetvalue = vmpidr_def,
7962 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
7963 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
7964 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7965 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7966 .access = PL2_RW, .resetvalue = vmpidr_def,
7967 .type = ARM_CP_EL3_NO_EL2_C_NZ,
7968 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
7970 define_arm_cp_regs(cpu, vpidr_regs);
7971 define_arm_cp_regs(cpu, el2_cp_reginfo);
7972 if (arm_feature(env, ARM_FEATURE_V8)) {
7973 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7975 if (cpu_isar_feature(aa64_sel2, cpu)) {
7976 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7978 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7979 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7980 ARMCPRegInfo rvbar = {
7981 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7982 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7983 .access = PL2_R,
7984 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7986 define_one_arm_cp_reg(cpu, &rvbar);
7990 /* Register the base EL3 cpregs. */
7991 if (arm_feature(env, ARM_FEATURE_EL3)) {
7992 define_arm_cp_regs(cpu, el3_cp_reginfo);
7993 ARMCPRegInfo el3_regs[] = {
7994 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7995 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7996 .access = PL3_R,
7997 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7999 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8000 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8001 .access = PL3_RW,
8002 .raw_writefn = raw_write, .writefn = sctlr_write,
8003 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8004 .resetvalue = cpu->reset_sctlr },
8007 define_arm_cp_regs(cpu, el3_regs);
8009 /* The behaviour of NSACR is sufficiently various that we don't
8010 * try to describe it in a single reginfo:
8011 * if EL3 is 64 bit, then trap to EL3 from S EL1,
8012 * reads as constant 0xc00 from NS EL1 and NS EL2
8013 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8014 * if v7 without EL3, register doesn't exist
8015 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8017 if (arm_feature(env, ARM_FEATURE_EL3)) {
8018 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8019 static const ARMCPRegInfo nsacr = {
8020 .name = "NSACR", .type = ARM_CP_CONST,
8021 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8022 .access = PL1_RW, .accessfn = nsacr_access,
8023 .resetvalue = 0xc00
8025 define_one_arm_cp_reg(cpu, &nsacr);
8026 } else {
8027 static const ARMCPRegInfo nsacr = {
8028 .name = "NSACR",
8029 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8030 .access = PL3_RW | PL1_R,
8031 .resetvalue = 0,
8032 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8034 define_one_arm_cp_reg(cpu, &nsacr);
8036 } else {
8037 if (arm_feature(env, ARM_FEATURE_V8)) {
8038 static const ARMCPRegInfo nsacr = {
8039 .name = "NSACR", .type = ARM_CP_CONST,
8040 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8041 .access = PL1_R,
8042 .resetvalue = 0xc00
8044 define_one_arm_cp_reg(cpu, &nsacr);
8048 if (arm_feature(env, ARM_FEATURE_PMSA)) {
8049 if (arm_feature(env, ARM_FEATURE_V6)) {
8050 /* PMSAv6 not implemented */
8051 assert(arm_feature(env, ARM_FEATURE_V7));
8052 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8053 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8054 } else {
8055 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8057 } else {
8058 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8059 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8060 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
8061 if (cpu_isar_feature(aa32_hpd, cpu)) {
8062 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8065 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8066 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8068 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8069 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8071 if (arm_feature(env, ARM_FEATURE_VAPA)) {
8072 define_arm_cp_regs(cpu, vapa_cp_reginfo);
8074 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8075 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8077 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8078 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8080 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8081 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8083 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8084 define_arm_cp_regs(cpu, omap_cp_reginfo);
8086 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8087 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8089 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8090 define_arm_cp_regs(cpu, xscale_cp_reginfo);
8092 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8093 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8095 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8096 define_arm_cp_regs(cpu, lpae_cp_reginfo);
8098 if (cpu_isar_feature(aa32_jazelle, cpu)) {
8099 define_arm_cp_regs(cpu, jazelle_regs);
8101 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
8102 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8103 * be read-only (ie write causes UNDEF exception).
8106 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8107 /* Pre-v8 MIDR space.
8108 * Note that the MIDR isn't a simple constant register because
8109 * of the TI925 behaviour where writes to another register can
8110 * cause the MIDR value to change.
8112 * Unimplemented registers in the c15 0 0 0 space default to
8113 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8114 * and friends override accordingly.
8116 { .name = "MIDR",
8117 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8118 .access = PL1_R, .resetvalue = cpu->midr,
8119 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8120 .readfn = midr_read,
8121 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8122 .type = ARM_CP_OVERRIDE },
8123 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8124 { .name = "DUMMY",
8125 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8126 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8127 { .name = "DUMMY",
8128 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8129 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8130 { .name = "DUMMY",
8131 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8132 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8133 { .name = "DUMMY",
8134 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8135 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8136 { .name = "DUMMY",
8137 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8138 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8140 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8141 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8142 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8143 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8144 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8145 .readfn = midr_read },
8146 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8147 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8148 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8149 .access = PL1_R, .resetvalue = cpu->midr },
8150 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8151 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8152 .access = PL1_R, .resetvalue = cpu->midr },
8153 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8154 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8155 .access = PL1_R,
8156 .accessfn = access_aa64_tid1,
8157 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8159 ARMCPRegInfo id_cp_reginfo[] = {
8160 /* These are common to v8 and pre-v8 */
8161 { .name = "CTR",
8162 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8163 .access = PL1_R, .accessfn = ctr_el0_access,
8164 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8165 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8166 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8167 .access = PL0_R, .accessfn = ctr_el0_access,
8168 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8169 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8170 { .name = "TCMTR",
8171 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8172 .access = PL1_R,
8173 .accessfn = access_aa32_tid1,
8174 .type = ARM_CP_CONST, .resetvalue = 0 },
8176 /* TLBTR is specific to VMSA */
8177 ARMCPRegInfo id_tlbtr_reginfo = {
8178 .name = "TLBTR",
8179 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8180 .access = PL1_R,
8181 .accessfn = access_aa32_tid1,
8182 .type = ARM_CP_CONST, .resetvalue = 0,
8184 /* MPUIR is specific to PMSA V6+ */
8185 ARMCPRegInfo id_mpuir_reginfo = {
8186 .name = "MPUIR",
8187 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8188 .access = PL1_R, .type = ARM_CP_CONST,
8189 .resetvalue = cpu->pmsav7_dregion << 8
8191 static const ARMCPRegInfo crn0_wi_reginfo = {
8192 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8193 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8194 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8196 #ifdef CONFIG_USER_ONLY
8197 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8198 { .name = "MIDR_EL1",
8199 .exported_bits = 0x00000000ffffffff },
8200 { .name = "REVIDR_EL1" },
8202 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8203 #endif
8204 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8205 arm_feature(env, ARM_FEATURE_STRONGARM)) {
8206 size_t i;
8207 /* Register the blanket "writes ignored" value first to cover the
8208 * whole space. Then update the specific ID registers to allow write
8209 * access, so that they ignore writes rather than causing them to
8210 * UNDEF.
8212 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8213 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8214 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8216 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8217 id_cp_reginfo[i].access = PL1_RW;
8219 id_mpuir_reginfo.access = PL1_RW;
8220 id_tlbtr_reginfo.access = PL1_RW;
8222 if (arm_feature(env, ARM_FEATURE_V8)) {
8223 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8224 } else {
8225 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8227 define_arm_cp_regs(cpu, id_cp_reginfo);
8228 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8229 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8230 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8231 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8235 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8236 ARMCPRegInfo mpidr_cp_reginfo[] = {
8237 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8238 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8239 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8241 #ifdef CONFIG_USER_ONLY
8242 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8243 { .name = "MPIDR_EL1",
8244 .fixed_bits = 0x0000000080000000 },
8246 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8247 #endif
8248 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8251 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8252 ARMCPRegInfo auxcr_reginfo[] = {
8253 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8254 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8255 .access = PL1_RW, .accessfn = access_tacr,
8256 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8257 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8258 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8259 .access = PL2_RW, .type = ARM_CP_CONST,
8260 .resetvalue = 0 },
8261 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8262 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8263 .access = PL3_RW, .type = ARM_CP_CONST,
8264 .resetvalue = 0 },
8266 define_arm_cp_regs(cpu, auxcr_reginfo);
8267 if (cpu_isar_feature(aa32_ac2, cpu)) {
8268 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8272 if (arm_feature(env, ARM_FEATURE_CBAR)) {
8274 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8275 * There are two flavours:
8276 * (1) older 32-bit only cores have a simple 32-bit CBAR
8277 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8278 * 32-bit register visible to AArch32 at a different encoding
8279 * to the "flavour 1" register and with the bits rearranged to
8280 * be able to squash a 64-bit address into the 32-bit view.
8281 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8282 * in future if we support AArch32-only configs of some of the
8283 * AArch64 cores we might need to add a specific feature flag
8284 * to indicate cores with "flavour 2" CBAR.
8286 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8287 /* 32 bit view is [31:18] 0...0 [43:32]. */
8288 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8289 | extract64(cpu->reset_cbar, 32, 12);
8290 ARMCPRegInfo cbar_reginfo[] = {
8291 { .name = "CBAR",
8292 .type = ARM_CP_CONST,
8293 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8294 .access = PL1_R, .resetvalue = cbar32 },
8295 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8296 .type = ARM_CP_CONST,
8297 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8298 .access = PL1_R, .resetvalue = cpu->reset_cbar },
8300 /* We don't implement a r/w 64 bit CBAR currently */
8301 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8302 define_arm_cp_regs(cpu, cbar_reginfo);
8303 } else {
8304 ARMCPRegInfo cbar = {
8305 .name = "CBAR",
8306 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8307 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8308 .fieldoffset = offsetof(CPUARMState,
8309 cp15.c15_config_base_address)
8311 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8312 cbar.access = PL1_R;
8313 cbar.fieldoffset = 0;
8314 cbar.type = ARM_CP_CONST;
8316 define_one_arm_cp_reg(cpu, &cbar);
8320 if (arm_feature(env, ARM_FEATURE_VBAR)) {
8321 static const ARMCPRegInfo vbar_cp_reginfo[] = {
8322 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8323 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8324 .access = PL1_RW, .writefn = vbar_write,
8325 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8326 offsetof(CPUARMState, cp15.vbar_ns) },
8327 .resetvalue = 0 },
8329 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8332 /* Generic registers whose values depend on the implementation */
8334 ARMCPRegInfo sctlr = {
8335 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8336 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8337 .access = PL1_RW, .accessfn = access_tvm_trvm,
8338 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8339 offsetof(CPUARMState, cp15.sctlr_ns) },
8340 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8341 .raw_writefn = raw_write,
8343 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8344 /* Normally we would always end the TB on an SCTLR write, but Linux
8345 * arch/arm/mach-pxa/sleep.S expects two instructions following
8346 * an MMU enable to execute from cache. Imitate this behaviour.
8348 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8350 define_one_arm_cp_reg(cpu, &sctlr);
8353 if (cpu_isar_feature(aa64_lor, cpu)) {
8354 define_arm_cp_regs(cpu, lor_reginfo);
8356 if (cpu_isar_feature(aa64_pan, cpu)) {
8357 define_one_arm_cp_reg(cpu, &pan_reginfo);
8359 #ifndef CONFIG_USER_ONLY
8360 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8361 define_arm_cp_regs(cpu, ats1e1_reginfo);
8363 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8364 define_arm_cp_regs(cpu, ats1cp_reginfo);
8366 #endif
8367 if (cpu_isar_feature(aa64_uao, cpu)) {
8368 define_one_arm_cp_reg(cpu, &uao_reginfo);
8371 if (cpu_isar_feature(aa64_dit, cpu)) {
8372 define_one_arm_cp_reg(cpu, &dit_reginfo);
8374 if (cpu_isar_feature(aa64_ssbs, cpu)) {
8375 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8377 if (cpu_isar_feature(any_ras, cpu)) {
8378 define_arm_cp_regs(cpu, minimal_ras_reginfo);
8381 if (cpu_isar_feature(aa64_vh, cpu) ||
8382 cpu_isar_feature(aa64_debugv8p2, cpu)) {
8383 define_one_arm_cp_reg(cpu, &contextidr_el2);
8385 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8386 define_arm_cp_regs(cpu, vhe_reginfo);
8389 if (cpu_isar_feature(aa64_sve, cpu)) {
8390 define_arm_cp_regs(cpu, zcr_reginfo);
8393 #ifdef TARGET_AARCH64
8394 if (cpu_isar_feature(aa64_pauth, cpu)) {
8395 define_arm_cp_regs(cpu, pauth_reginfo);
8397 if (cpu_isar_feature(aa64_rndr, cpu)) {
8398 define_arm_cp_regs(cpu, rndr_reginfo);
8400 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8401 define_arm_cp_regs(cpu, tlbirange_reginfo);
8403 if (cpu_isar_feature(aa64_tlbios, cpu)) {
8404 define_arm_cp_regs(cpu, tlbios_reginfo);
8406 #ifndef CONFIG_USER_ONLY
8407 /* Data Cache clean instructions up to PoP */
8408 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8409 define_one_arm_cp_reg(cpu, dcpop_reg);
8411 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8412 define_one_arm_cp_reg(cpu, dcpodp_reg);
8415 #endif /*CONFIG_USER_ONLY*/
8418 * If full MTE is enabled, add all of the system registers.
8419 * If only "instructions available at EL0" are enabled,
8420 * then define only a RAZ/WI version of PSTATE.TCO.
8422 if (cpu_isar_feature(aa64_mte, cpu)) {
8423 define_arm_cp_regs(cpu, mte_reginfo);
8424 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8425 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8426 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8427 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8430 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8431 define_arm_cp_regs(cpu, scxtnum_reginfo);
8433 #endif
8435 if (cpu_isar_feature(any_predinv, cpu)) {
8436 define_arm_cp_regs(cpu, predinv_reginfo);
8439 if (cpu_isar_feature(any_ccidx, cpu)) {
8440 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8443 #ifndef CONFIG_USER_ONLY
8445 * Register redirections and aliases must be done last,
8446 * after the registers from the other extensions have been defined.
8448 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8449 define_arm_vh_e2h_redirects_aliases(cpu);
8451 #endif
8454 /* Sort alphabetically by type name, except for "any". */
8455 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8457 ObjectClass *class_a = (ObjectClass *)a;
8458 ObjectClass *class_b = (ObjectClass *)b;
8459 const char *name_a, *name_b;
8461 name_a = object_class_get_name(class_a);
8462 name_b = object_class_get_name(class_b);
8463 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8464 return 1;
8465 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8466 return -1;
8467 } else {
8468 return strcmp(name_a, name_b);
8472 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8474 ObjectClass *oc = data;
8475 const char *typename;
8476 char *name;
8478 typename = object_class_get_name(oc);
8479 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8480 qemu_printf(" %s\n", name);
8481 g_free(name);
8484 void arm_cpu_list(void)
8486 GSList *list;
8488 list = object_class_get_list(TYPE_ARM_CPU, false);
8489 list = g_slist_sort(list, arm_cpu_list_compare);
8490 qemu_printf("Available CPUs:\n");
8491 g_slist_foreach(list, arm_cpu_list_entry, NULL);
8492 g_slist_free(list);
8495 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8497 ObjectClass *oc = data;
8498 CpuDefinitionInfoList **cpu_list = user_data;
8499 CpuDefinitionInfo *info;
8500 const char *typename;
8502 typename = object_class_get_name(oc);
8503 info = g_malloc0(sizeof(*info));
8504 info->name = g_strndup(typename,
8505 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8506 info->q_typename = g_strdup(typename);
8508 QAPI_LIST_PREPEND(*cpu_list, info);
8511 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8513 CpuDefinitionInfoList *cpu_list = NULL;
8514 GSList *list;
8516 list = object_class_get_list(TYPE_ARM_CPU, false);
8517 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8518 g_slist_free(list);
8520 return cpu_list;
8524 * Private utility function for define_one_arm_cp_reg_with_opaque():
8525 * add a single reginfo struct to the hash table.
8527 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8528 void *opaque, CPState state,
8529 CPSecureState secstate,
8530 int crm, int opc1, int opc2,
8531 const char *name)
8533 CPUARMState *env = &cpu->env;
8534 uint32_t key;
8535 ARMCPRegInfo *r2;
8536 bool is64 = r->type & ARM_CP_64BIT;
8537 bool ns = secstate & ARM_CP_SECSTATE_NS;
8538 int cp = r->cp;
8539 size_t name_len;
8540 bool make_const;
8542 switch (state) {
8543 case ARM_CP_STATE_AA32:
8544 /* We assume it is a cp15 register if the .cp field is left unset. */
8545 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8546 cp = 15;
8548 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8549 break;
8550 case ARM_CP_STATE_AA64:
8552 * To allow abbreviation of ARMCPRegInfo definitions, we treat
8553 * cp == 0 as equivalent to the value for "standard guest-visible
8554 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
8555 * in their AArch64 view (the .cp value may be non-zero for the
8556 * benefit of the AArch32 view).
8558 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8559 cp = CP_REG_ARM64_SYSREG_CP;
8561 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8562 break;
8563 default:
8564 g_assert_not_reached();
8567 /* Overriding of an existing definition must be explicitly requested. */
8568 if (!(r->type & ARM_CP_OVERRIDE)) {
8569 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8570 if (oldreg) {
8571 assert(oldreg->type & ARM_CP_OVERRIDE);
8576 * Eliminate registers that are not present because the EL is missing.
8577 * Doing this here makes it easier to put all registers for a given
8578 * feature into the same ARMCPRegInfo array and define them all at once.
8580 make_const = false;
8581 if (arm_feature(env, ARM_FEATURE_EL3)) {
8583 * An EL2 register without EL2 but with EL3 is (usually) RES0.
8584 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8586 int min_el = ctz32(r->access) / 2;
8587 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8588 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8589 return;
8591 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8593 } else {
8594 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8595 ? PL2_RW : PL1_RW);
8596 if ((r->access & max_el) == 0) {
8597 return;
8601 /* Combine cpreg and name into one allocation. */
8602 name_len = strlen(name) + 1;
8603 r2 = g_malloc(sizeof(*r2) + name_len);
8604 *r2 = *r;
8605 r2->name = memcpy(r2 + 1, name, name_len);
8608 * Update fields to match the instantiation, overwiting wildcards
8609 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8611 r2->cp = cp;
8612 r2->crm = crm;
8613 r2->opc1 = opc1;
8614 r2->opc2 = opc2;
8615 r2->state = state;
8616 r2->secure = secstate;
8617 if (opaque) {
8618 r2->opaque = opaque;
8621 if (make_const) {
8622 /* This should not have been a very special register to begin. */
8623 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8624 assert(old_special == 0 || old_special == ARM_CP_NOP);
8626 * Set the special function to CONST, retaining the other flags.
8627 * This is important for e.g. ARM_CP_SVE so that we still
8628 * take the SVE trap if CPTR_EL3.EZ == 0.
8630 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8632 * Usually, these registers become RES0, but there are a few
8633 * special cases like VPIDR_EL2 which have a constant non-zero
8634 * value with writes ignored.
8636 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8637 r2->resetvalue = 0;
8640 * ARM_CP_CONST has precedence, so removing the callbacks and
8641 * offsets are not strictly necessary, but it is potentially
8642 * less confusing to debug later.
8644 r2->readfn = NULL;
8645 r2->writefn = NULL;
8646 r2->raw_readfn = NULL;
8647 r2->raw_writefn = NULL;
8648 r2->resetfn = NULL;
8649 r2->fieldoffset = 0;
8650 r2->bank_fieldoffsets[0] = 0;
8651 r2->bank_fieldoffsets[1] = 0;
8652 } else {
8653 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8655 if (isbanked) {
8657 * Register is banked (using both entries in array).
8658 * Overwriting fieldoffset as the array is only used to define
8659 * banked registers but later only fieldoffset is used.
8661 r2->fieldoffset = r->bank_fieldoffsets[ns];
8663 if (state == ARM_CP_STATE_AA32) {
8664 if (isbanked) {
8666 * If the register is banked then we don't need to migrate or
8667 * reset the 32-bit instance in certain cases:
8669 * 1) If the register has both 32-bit and 64-bit instances
8670 * then we can count on the 64-bit instance taking care
8671 * of the non-secure bank.
8672 * 2) If ARMv8 is enabled then we can count on a 64-bit
8673 * version taking care of the secure bank. This requires
8674 * that separate 32 and 64-bit definitions are provided.
8676 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8677 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8678 r2->type |= ARM_CP_ALIAS;
8680 } else if ((secstate != r->secure) && !ns) {
8682 * The register is not banked so we only want to allow
8683 * migration of the non-secure instance.
8685 r2->type |= ARM_CP_ALIAS;
8688 if (HOST_BIG_ENDIAN &&
8689 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8690 r2->fieldoffset += sizeof(uint32_t);
8696 * By convention, for wildcarded registers only the first
8697 * entry is used for migration; the others are marked as
8698 * ALIAS so we don't try to transfer the register
8699 * multiple times. Special registers (ie NOP/WFI) are
8700 * never migratable and not even raw-accessible.
8702 if (r2->type & ARM_CP_SPECIAL_MASK) {
8703 r2->type |= ARM_CP_NO_RAW;
8705 if (((r->crm == CP_ANY) && crm != 0) ||
8706 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8707 ((r->opc2 == CP_ANY) && opc2 != 0)) {
8708 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8712 * Check that raw accesses are either forbidden or handled. Note that
8713 * we can't assert this earlier because the setup of fieldoffset for
8714 * banked registers has to be done first.
8716 if (!(r2->type & ARM_CP_NO_RAW)) {
8717 assert(!raw_accessors_invalid(r2));
8720 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
8724 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8725 const ARMCPRegInfo *r, void *opaque)
8727 /* Define implementations of coprocessor registers.
8728 * We store these in a hashtable because typically
8729 * there are less than 150 registers in a space which
8730 * is 16*16*16*8*8 = 262144 in size.
8731 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8732 * If a register is defined twice then the second definition is
8733 * used, so this can be used to define some generic registers and
8734 * then override them with implementation specific variations.
8735 * At least one of the original and the second definition should
8736 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8737 * against accidental use.
8739 * The state field defines whether the register is to be
8740 * visible in the AArch32 or AArch64 execution state. If the
8741 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8742 * reginfo structure for the AArch32 view, which sees the lower
8743 * 32 bits of the 64 bit register.
8745 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8746 * be wildcarded. AArch64 registers are always considered to be 64
8747 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8748 * the register, if any.
8750 int crm, opc1, opc2;
8751 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8752 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8753 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8754 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8755 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8756 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8757 CPState state;
8759 /* 64 bit registers have only CRm and Opc1 fields */
8760 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8761 /* op0 only exists in the AArch64 encodings */
8762 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8763 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8764 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8766 * This API is only for Arm's system coprocessors (14 and 15) or
8767 * (M-profile or v7A-and-earlier only) for implementation defined
8768 * coprocessors in the range 0..7. Our decode assumes this, since
8769 * 8..13 can be used for other insns including VFP and Neon. See
8770 * valid_cp() in translate.c. Assert here that we haven't tried
8771 * to use an invalid coprocessor number.
8773 switch (r->state) {
8774 case ARM_CP_STATE_BOTH:
8775 /* 0 has a special meaning, but otherwise the same rules as AA32. */
8776 if (r->cp == 0) {
8777 break;
8779 /* fall through */
8780 case ARM_CP_STATE_AA32:
8781 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8782 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8783 assert(r->cp >= 14 && r->cp <= 15);
8784 } else {
8785 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8787 break;
8788 case ARM_CP_STATE_AA64:
8789 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8790 break;
8791 default:
8792 g_assert_not_reached();
8794 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8795 * encodes a minimum access level for the register. We roll this
8796 * runtime check into our general permission check code, so check
8797 * here that the reginfo's specified permissions are strict enough
8798 * to encompass the generic architectural permission check.
8800 if (r->state != ARM_CP_STATE_AA32) {
8801 CPAccessRights mask;
8802 switch (r->opc1) {
8803 case 0:
8804 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8805 mask = PL0U_R | PL1_RW;
8806 break;
8807 case 1: case 2:
8808 /* min_EL EL1 */
8809 mask = PL1_RW;
8810 break;
8811 case 3:
8812 /* min_EL EL0 */
8813 mask = PL0_RW;
8814 break;
8815 case 4:
8816 case 5:
8817 /* min_EL EL2 */
8818 mask = PL2_RW;
8819 break;
8820 case 6:
8821 /* min_EL EL3 */
8822 mask = PL3_RW;
8823 break;
8824 case 7:
8825 /* min_EL EL1, secure mode only (we don't check the latter) */
8826 mask = PL1_RW;
8827 break;
8828 default:
8829 /* broken reginfo with out-of-range opc1 */
8830 g_assert_not_reached();
8832 /* assert our permissions are not too lax (stricter is fine) */
8833 assert((r->access & ~mask) == 0);
8836 /* Check that the register definition has enough info to handle
8837 * reads and writes if they are permitted.
8839 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
8840 if (r->access & PL3_R) {
8841 assert((r->fieldoffset ||
8842 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8843 r->readfn);
8845 if (r->access & PL3_W) {
8846 assert((r->fieldoffset ||
8847 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8848 r->writefn);
8852 for (crm = crmmin; crm <= crmmax; crm++) {
8853 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8854 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
8855 for (state = ARM_CP_STATE_AA32;
8856 state <= ARM_CP_STATE_AA64; state++) {
8857 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8858 continue;
8860 if (state == ARM_CP_STATE_AA32) {
8861 /* Under AArch32 CP registers can be common
8862 * (same for secure and non-secure world) or banked.
8864 char *name;
8866 switch (r->secure) {
8867 case ARM_CP_SECSTATE_S:
8868 case ARM_CP_SECSTATE_NS:
8869 add_cpreg_to_hashtable(cpu, r, opaque, state,
8870 r->secure, crm, opc1, opc2,
8871 r->name);
8872 break;
8873 case ARM_CP_SECSTATE_BOTH:
8874 name = g_strdup_printf("%s_S", r->name);
8875 add_cpreg_to_hashtable(cpu, r, opaque, state,
8876 ARM_CP_SECSTATE_S,
8877 crm, opc1, opc2, name);
8878 g_free(name);
8879 add_cpreg_to_hashtable(cpu, r, opaque, state,
8880 ARM_CP_SECSTATE_NS,
8881 crm, opc1, opc2, r->name);
8882 break;
8883 default:
8884 g_assert_not_reached();
8886 } else {
8887 /* AArch64 registers get mapped to non-secure instance
8888 * of AArch32 */
8889 add_cpreg_to_hashtable(cpu, r, opaque, state,
8890 ARM_CP_SECSTATE_NS,
8891 crm, opc1, opc2, r->name);
8899 /* Define a whole list of registers */
8900 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8901 void *opaque, size_t len)
8903 size_t i;
8904 for (i = 0; i < len; ++i) {
8905 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
8910 * Modify ARMCPRegInfo for access from userspace.
8912 * This is a data driven modification directed by
8913 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8914 * user-space cannot alter any values and dynamic values pertaining to
8915 * execution state are hidden from user space view anyway.
8917 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8918 const ARMCPRegUserSpaceInfo *mods,
8919 size_t mods_len)
8921 for (size_t mi = 0; mi < mods_len; ++mi) {
8922 const ARMCPRegUserSpaceInfo *m = mods + mi;
8923 GPatternSpec *pat = NULL;
8925 if (m->is_glob) {
8926 pat = g_pattern_spec_new(m->name);
8928 for (size_t ri = 0; ri < regs_len; ++ri) {
8929 ARMCPRegInfo *r = regs + ri;
8931 if (pat && g_pattern_match_string(pat, r->name)) {
8932 r->type = ARM_CP_CONST;
8933 r->access = PL0U_R;
8934 r->resetvalue = 0;
8935 /* continue */
8936 } else if (strcmp(r->name, m->name) == 0) {
8937 r->type = ARM_CP_CONST;
8938 r->access = PL0U_R;
8939 r->resetvalue &= m->exported_bits;
8940 r->resetvalue |= m->fixed_bits;
8941 break;
8944 if (pat) {
8945 g_pattern_spec_free(pat);
8950 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
8952 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
8955 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8956 uint64_t value)
8958 /* Helper coprocessor write function for write-ignore registers */
8961 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
8963 /* Helper coprocessor write function for read-as-zero registers */
8964 return 0;
8967 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8969 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8972 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
8974 /* Return true if it is not valid for us to switch to
8975 * this CPU mode (ie all the UNPREDICTABLE cases in
8976 * the ARM ARM CPSRWriteByInstr pseudocode).
8979 /* Changes to or from Hyp via MSR and CPS are illegal. */
8980 if (write_type == CPSRWriteByInstr &&
8981 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8982 mode == ARM_CPU_MODE_HYP)) {
8983 return 1;
8986 switch (mode) {
8987 case ARM_CPU_MODE_USR:
8988 return 0;
8989 case ARM_CPU_MODE_SYS:
8990 case ARM_CPU_MODE_SVC:
8991 case ARM_CPU_MODE_ABT:
8992 case ARM_CPU_MODE_UND:
8993 case ARM_CPU_MODE_IRQ:
8994 case ARM_CPU_MODE_FIQ:
8995 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8996 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8998 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8999 * and CPS are treated as illegal mode changes.
9001 if (write_type == CPSRWriteByInstr &&
9002 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9003 (arm_hcr_el2_eff(env) & HCR_TGE)) {
9004 return 1;
9006 return 0;
9007 case ARM_CPU_MODE_HYP:
9008 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9009 case ARM_CPU_MODE_MON:
9010 return arm_current_el(env) < 3;
9011 default:
9012 return 1;
9016 uint32_t cpsr_read(CPUARMState *env)
9018 int ZF;
9019 ZF = (env->ZF == 0);
9020 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9021 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9022 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9023 | ((env->condexec_bits & 0xfc) << 8)
9024 | (env->GE << 16) | (env->daif & CPSR_AIF);
9027 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9028 CPSRWriteType write_type)
9030 uint32_t changed_daif;
9031 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9032 (mask & (CPSR_M | CPSR_E | CPSR_IL));
9034 if (mask & CPSR_NZCV) {
9035 env->ZF = (~val) & CPSR_Z;
9036 env->NF = val;
9037 env->CF = (val >> 29) & 1;
9038 env->VF = (val << 3) & 0x80000000;
9040 if (mask & CPSR_Q)
9041 env->QF = ((val & CPSR_Q) != 0);
9042 if (mask & CPSR_T)
9043 env->thumb = ((val & CPSR_T) != 0);
9044 if (mask & CPSR_IT_0_1) {
9045 env->condexec_bits &= ~3;
9046 env->condexec_bits |= (val >> 25) & 3;
9048 if (mask & CPSR_IT_2_7) {
9049 env->condexec_bits &= 3;
9050 env->condexec_bits |= (val >> 8) & 0xfc;
9052 if (mask & CPSR_GE) {
9053 env->GE = (val >> 16) & 0xf;
9056 /* In a V7 implementation that includes the security extensions but does
9057 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9058 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9059 * bits respectively.
9061 * In a V8 implementation, it is permitted for privileged software to
9062 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9064 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9065 arm_feature(env, ARM_FEATURE_EL3) &&
9066 !arm_feature(env, ARM_FEATURE_EL2) &&
9067 !arm_is_secure(env)) {
9069 changed_daif = (env->daif ^ val) & mask;
9071 if (changed_daif & CPSR_A) {
9072 /* Check to see if we are allowed to change the masking of async
9073 * abort exceptions from a non-secure state.
9075 if (!(env->cp15.scr_el3 & SCR_AW)) {
9076 qemu_log_mask(LOG_GUEST_ERROR,
9077 "Ignoring attempt to switch CPSR_A flag from "
9078 "non-secure world with SCR.AW bit clear\n");
9079 mask &= ~CPSR_A;
9083 if (changed_daif & CPSR_F) {
9084 /* Check to see if we are allowed to change the masking of FIQ
9085 * exceptions from a non-secure state.
9087 if (!(env->cp15.scr_el3 & SCR_FW)) {
9088 qemu_log_mask(LOG_GUEST_ERROR,
9089 "Ignoring attempt to switch CPSR_F flag from "
9090 "non-secure world with SCR.FW bit clear\n");
9091 mask &= ~CPSR_F;
9094 /* Check whether non-maskable FIQ (NMFI) support is enabled.
9095 * If this bit is set software is not allowed to mask
9096 * FIQs, but is allowed to set CPSR_F to 0.
9098 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9099 (val & CPSR_F)) {
9100 qemu_log_mask(LOG_GUEST_ERROR,
9101 "Ignoring attempt to enable CPSR_F flag "
9102 "(non-maskable FIQ [NMFI] support enabled)\n");
9103 mask &= ~CPSR_F;
9108 env->daif &= ~(CPSR_AIF & mask);
9109 env->daif |= val & CPSR_AIF & mask;
9111 if (write_type != CPSRWriteRaw &&
9112 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9113 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9114 /* Note that we can only get here in USR mode if this is a
9115 * gdb stub write; for this case we follow the architectural
9116 * behaviour for guest writes in USR mode of ignoring an attempt
9117 * to switch mode. (Those are caught by translate.c for writes
9118 * triggered by guest instructions.)
9120 mask &= ~CPSR_M;
9121 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9122 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9123 * v7, and has defined behaviour in v8:
9124 * + leave CPSR.M untouched
9125 * + allow changes to the other CPSR fields
9126 * + set PSTATE.IL
9127 * For user changes via the GDB stub, we don't set PSTATE.IL,
9128 * as this would be unnecessarily harsh for a user error.
9130 mask &= ~CPSR_M;
9131 if (write_type != CPSRWriteByGDBStub &&
9132 arm_feature(env, ARM_FEATURE_V8)) {
9133 mask |= CPSR_IL;
9134 val |= CPSR_IL;
9136 qemu_log_mask(LOG_GUEST_ERROR,
9137 "Illegal AArch32 mode switch attempt from %s to %s\n",
9138 aarch32_mode_name(env->uncached_cpsr),
9139 aarch32_mode_name(val));
9140 } else {
9141 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9142 write_type == CPSRWriteExceptionReturn ?
9143 "Exception return from AArch32" :
9144 "AArch32 mode switch from",
9145 aarch32_mode_name(env->uncached_cpsr),
9146 aarch32_mode_name(val), env->regs[15]);
9147 switch_mode(env, val & CPSR_M);
9150 mask &= ~CACHED_CPSR_BITS;
9151 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9152 if (rebuild_hflags) {
9153 arm_rebuild_hflags(env);
9157 /* Sign/zero extend */
9158 uint32_t HELPER(sxtb16)(uint32_t x)
9160 uint32_t res;
9161 res = (uint16_t)(int8_t)x;
9162 res |= (uint32_t)(int8_t)(x >> 16) << 16;
9163 return res;
9166 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9169 * Take a division-by-zero exception if necessary; otherwise return
9170 * to get the usual non-trapping division behaviour (result of 0)
9172 if (arm_feature(env, ARM_FEATURE_M)
9173 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9174 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9178 uint32_t HELPER(uxtb16)(uint32_t x)
9180 uint32_t res;
9181 res = (uint16_t)(uint8_t)x;
9182 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9183 return res;
9186 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9188 if (den == 0) {
9189 handle_possible_div0_trap(env, GETPC());
9190 return 0;
9192 if (num == INT_MIN && den == -1) {
9193 return INT_MIN;
9195 return num / den;
9198 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9200 if (den == 0) {
9201 handle_possible_div0_trap(env, GETPC());
9202 return 0;
9204 return num / den;
9207 uint32_t HELPER(rbit)(uint32_t x)
9209 return revbit32(x);
9212 #ifdef CONFIG_USER_ONLY
9214 static void switch_mode(CPUARMState *env, int mode)
9216 ARMCPU *cpu = env_archcpu(env);
9218 if (mode != ARM_CPU_MODE_USR) {
9219 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9223 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9224 uint32_t cur_el, bool secure)
9226 return 1;
9229 void aarch64_sync_64_to_32(CPUARMState *env)
9231 g_assert_not_reached();
9234 #else
9236 static void switch_mode(CPUARMState *env, int mode)
9238 int old_mode;
9239 int i;
9241 old_mode = env->uncached_cpsr & CPSR_M;
9242 if (mode == old_mode)
9243 return;
9245 if (old_mode == ARM_CPU_MODE_FIQ) {
9246 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9247 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9248 } else if (mode == ARM_CPU_MODE_FIQ) {
9249 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9250 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9253 i = bank_number(old_mode);
9254 env->banked_r13[i] = env->regs[13];
9255 env->banked_spsr[i] = env->spsr;
9257 i = bank_number(mode);
9258 env->regs[13] = env->banked_r13[i];
9259 env->spsr = env->banked_spsr[i];
9261 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9262 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9265 /* Physical Interrupt Target EL Lookup Table
9267 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9269 * The below multi-dimensional table is used for looking up the target
9270 * exception level given numerous condition criteria. Specifically, the
9271 * target EL is based on SCR and HCR routing controls as well as the
9272 * currently executing EL and secure state.
9274 * Dimensions:
9275 * target_el_table[2][2][2][2][2][4]
9276 * | | | | | +--- Current EL
9277 * | | | | +------ Non-secure(0)/Secure(1)
9278 * | | | +--------- HCR mask override
9279 * | | +------------ SCR exec state control
9280 * | +--------------- SCR mask override
9281 * +------------------ 32-bit(0)/64-bit(1) EL3
9283 * The table values are as such:
9284 * 0-3 = EL0-EL3
9285 * -1 = Cannot occur
9287 * The ARM ARM target EL table includes entries indicating that an "exception
9288 * is not taken". The two cases where this is applicable are:
9289 * 1) An exception is taken from EL3 but the SCR does not have the exception
9290 * routed to EL3.
9291 * 2) An exception is taken from EL2 but the HCR does not have the exception
9292 * routed to EL2.
9293 * In these two cases, the below table contain a target of EL1. This value is
9294 * returned as it is expected that the consumer of the table data will check
9295 * for "target EL >= current EL" to ensure the exception is not taken.
9297 * SCR HCR
9298 * 64 EA AMO From
9299 * BIT IRQ IMO Non-secure Secure
9300 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9302 static const int8_t target_el_table[2][2][2][2][2][4] = {
9303 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9304 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9305 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9306 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9307 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9308 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9309 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9310 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9311 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
9312 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9313 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9314 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
9315 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
9316 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
9317 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
9318 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
9322 * Determine the target EL for physical exceptions
9324 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9325 uint32_t cur_el, bool secure)
9327 CPUARMState *env = cs->env_ptr;
9328 bool rw;
9329 bool scr;
9330 bool hcr;
9331 int target_el;
9332 /* Is the highest EL AArch64? */
9333 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9334 uint64_t hcr_el2;
9336 if (arm_feature(env, ARM_FEATURE_EL3)) {
9337 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9338 } else {
9339 /* Either EL2 is the highest EL (and so the EL2 register width
9340 * is given by is64); or there is no EL2 or EL3, in which case
9341 * the value of 'rw' does not affect the table lookup anyway.
9343 rw = is64;
9346 hcr_el2 = arm_hcr_el2_eff(env);
9347 switch (excp_idx) {
9348 case EXCP_IRQ:
9349 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9350 hcr = hcr_el2 & HCR_IMO;
9351 break;
9352 case EXCP_FIQ:
9353 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9354 hcr = hcr_el2 & HCR_FMO;
9355 break;
9356 default:
9357 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9358 hcr = hcr_el2 & HCR_AMO;
9359 break;
9363 * For these purposes, TGE and AMO/IMO/FMO both force the
9364 * interrupt to EL2. Fold TGE into the bit extracted above.
9366 hcr |= (hcr_el2 & HCR_TGE) != 0;
9368 /* Perform a table-lookup for the target EL given the current state */
9369 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9371 assert(target_el > 0);
9373 return target_el;
9376 void arm_log_exception(CPUState *cs)
9378 int idx = cs->exception_index;
9380 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9381 const char *exc = NULL;
9382 static const char * const excnames[] = {
9383 [EXCP_UDEF] = "Undefined Instruction",
9384 [EXCP_SWI] = "SVC",
9385 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9386 [EXCP_DATA_ABORT] = "Data Abort",
9387 [EXCP_IRQ] = "IRQ",
9388 [EXCP_FIQ] = "FIQ",
9389 [EXCP_BKPT] = "Breakpoint",
9390 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9391 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9392 [EXCP_HVC] = "Hypervisor Call",
9393 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9394 [EXCP_SMC] = "Secure Monitor Call",
9395 [EXCP_VIRQ] = "Virtual IRQ",
9396 [EXCP_VFIQ] = "Virtual FIQ",
9397 [EXCP_SEMIHOST] = "Semihosting call",
9398 [EXCP_NOCP] = "v7M NOCP UsageFault",
9399 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9400 [EXCP_STKOF] = "v8M STKOF UsageFault",
9401 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9402 [EXCP_LSERR] = "v8M LSERR UsageFault",
9403 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9404 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9405 [EXCP_VSERR] = "Virtual SERR",
9408 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9409 exc = excnames[idx];
9411 if (!exc) {
9412 exc = "unknown";
9414 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9415 idx, exc, cs->cpu_index);
9420 * Function used to synchronize QEMU's AArch64 register set with AArch32
9421 * register set. This is necessary when switching between AArch32 and AArch64
9422 * execution state.
9424 void aarch64_sync_32_to_64(CPUARMState *env)
9426 int i;
9427 uint32_t mode = env->uncached_cpsr & CPSR_M;
9429 /* We can blanket copy R[0:7] to X[0:7] */
9430 for (i = 0; i < 8; i++) {
9431 env->xregs[i] = env->regs[i];
9435 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9436 * Otherwise, they come from the banked user regs.
9438 if (mode == ARM_CPU_MODE_FIQ) {
9439 for (i = 8; i < 13; i++) {
9440 env->xregs[i] = env->usr_regs[i - 8];
9442 } else {
9443 for (i = 8; i < 13; i++) {
9444 env->xregs[i] = env->regs[i];
9449 * Registers x13-x23 are the various mode SP and FP registers. Registers
9450 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9451 * from the mode banked register.
9453 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9454 env->xregs[13] = env->regs[13];
9455 env->xregs[14] = env->regs[14];
9456 } else {
9457 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9458 /* HYP is an exception in that it is copied from r14 */
9459 if (mode == ARM_CPU_MODE_HYP) {
9460 env->xregs[14] = env->regs[14];
9461 } else {
9462 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9466 if (mode == ARM_CPU_MODE_HYP) {
9467 env->xregs[15] = env->regs[13];
9468 } else {
9469 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9472 if (mode == ARM_CPU_MODE_IRQ) {
9473 env->xregs[16] = env->regs[14];
9474 env->xregs[17] = env->regs[13];
9475 } else {
9476 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9477 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9480 if (mode == ARM_CPU_MODE_SVC) {
9481 env->xregs[18] = env->regs[14];
9482 env->xregs[19] = env->regs[13];
9483 } else {
9484 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9485 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9488 if (mode == ARM_CPU_MODE_ABT) {
9489 env->xregs[20] = env->regs[14];
9490 env->xregs[21] = env->regs[13];
9491 } else {
9492 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9493 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9496 if (mode == ARM_CPU_MODE_UND) {
9497 env->xregs[22] = env->regs[14];
9498 env->xregs[23] = env->regs[13];
9499 } else {
9500 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9501 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9505 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9506 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9507 * FIQ bank for r8-r14.
9509 if (mode == ARM_CPU_MODE_FIQ) {
9510 for (i = 24; i < 31; i++) {
9511 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9513 } else {
9514 for (i = 24; i < 29; i++) {
9515 env->xregs[i] = env->fiq_regs[i - 24];
9517 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9518 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9521 env->pc = env->regs[15];
9525 * Function used to synchronize QEMU's AArch32 register set with AArch64
9526 * register set. This is necessary when switching between AArch32 and AArch64
9527 * execution state.
9529 void aarch64_sync_64_to_32(CPUARMState *env)
9531 int i;
9532 uint32_t mode = env->uncached_cpsr & CPSR_M;
9534 /* We can blanket copy X[0:7] to R[0:7] */
9535 for (i = 0; i < 8; i++) {
9536 env->regs[i] = env->xregs[i];
9540 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9541 * Otherwise, we copy x8-x12 into the banked user regs.
9543 if (mode == ARM_CPU_MODE_FIQ) {
9544 for (i = 8; i < 13; i++) {
9545 env->usr_regs[i - 8] = env->xregs[i];
9547 } else {
9548 for (i = 8; i < 13; i++) {
9549 env->regs[i] = env->xregs[i];
9554 * Registers r13 & r14 depend on the current mode.
9555 * If we are in a given mode, we copy the corresponding x registers to r13
9556 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9557 * for the mode.
9559 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9560 env->regs[13] = env->xregs[13];
9561 env->regs[14] = env->xregs[14];
9562 } else {
9563 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9566 * HYP is an exception in that it does not have its own banked r14 but
9567 * shares the USR r14
9569 if (mode == ARM_CPU_MODE_HYP) {
9570 env->regs[14] = env->xregs[14];
9571 } else {
9572 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9576 if (mode == ARM_CPU_MODE_HYP) {
9577 env->regs[13] = env->xregs[15];
9578 } else {
9579 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9582 if (mode == ARM_CPU_MODE_IRQ) {
9583 env->regs[14] = env->xregs[16];
9584 env->regs[13] = env->xregs[17];
9585 } else {
9586 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9587 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9590 if (mode == ARM_CPU_MODE_SVC) {
9591 env->regs[14] = env->xregs[18];
9592 env->regs[13] = env->xregs[19];
9593 } else {
9594 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9595 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9598 if (mode == ARM_CPU_MODE_ABT) {
9599 env->regs[14] = env->xregs[20];
9600 env->regs[13] = env->xregs[21];
9601 } else {
9602 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9603 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9606 if (mode == ARM_CPU_MODE_UND) {
9607 env->regs[14] = env->xregs[22];
9608 env->regs[13] = env->xregs[23];
9609 } else {
9610 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9611 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9614 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9615 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9616 * FIQ bank for r8-r14.
9618 if (mode == ARM_CPU_MODE_FIQ) {
9619 for (i = 24; i < 31; i++) {
9620 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9622 } else {
9623 for (i = 24; i < 29; i++) {
9624 env->fiq_regs[i - 24] = env->xregs[i];
9626 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9627 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9630 env->regs[15] = env->pc;
9633 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9634 uint32_t mask, uint32_t offset,
9635 uint32_t newpc)
9637 int new_el;
9639 /* Change the CPU state so as to actually take the exception. */
9640 switch_mode(env, new_mode);
9643 * For exceptions taken to AArch32 we must clear the SS bit in both
9644 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9646 env->pstate &= ~PSTATE_SS;
9647 env->spsr = cpsr_read(env);
9648 /* Clear IT bits. */
9649 env->condexec_bits = 0;
9650 /* Switch to the new mode, and to the correct instruction set. */
9651 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9653 /* This must be after mode switching. */
9654 new_el = arm_current_el(env);
9656 /* Set new mode endianness */
9657 env->uncached_cpsr &= ~CPSR_E;
9658 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9659 env->uncached_cpsr |= CPSR_E;
9661 /* J and IL must always be cleared for exception entry */
9662 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9663 env->daif |= mask;
9665 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9666 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9667 env->uncached_cpsr |= CPSR_SSBS;
9668 } else {
9669 env->uncached_cpsr &= ~CPSR_SSBS;
9673 if (new_mode == ARM_CPU_MODE_HYP) {
9674 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9675 env->elr_el[2] = env->regs[15];
9676 } else {
9677 /* CPSR.PAN is normally preserved preserved unless... */
9678 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9679 switch (new_el) {
9680 case 3:
9681 if (!arm_is_secure_below_el3(env)) {
9682 /* ... the target is EL3, from non-secure state. */
9683 env->uncached_cpsr &= ~CPSR_PAN;
9684 break;
9686 /* ... the target is EL3, from secure state ... */
9687 /* fall through */
9688 case 1:
9689 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9690 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9691 env->uncached_cpsr |= CPSR_PAN;
9693 break;
9697 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9698 * and we should just guard the thumb mode on V4
9700 if (arm_feature(env, ARM_FEATURE_V4T)) {
9701 env->thumb =
9702 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9704 env->regs[14] = env->regs[15] + offset;
9706 env->regs[15] = newpc;
9707 arm_rebuild_hflags(env);
9710 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9713 * Handle exception entry to Hyp mode; this is sufficiently
9714 * different to entry to other AArch32 modes that we handle it
9715 * separately here.
9717 * The vector table entry used is always the 0x14 Hyp mode entry point,
9718 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9719 * The offset applied to the preferred return address is always zero
9720 * (see DDI0487C.a section G1.12.3).
9721 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9723 uint32_t addr, mask;
9724 ARMCPU *cpu = ARM_CPU(cs);
9725 CPUARMState *env = &cpu->env;
9727 switch (cs->exception_index) {
9728 case EXCP_UDEF:
9729 addr = 0x04;
9730 break;
9731 case EXCP_SWI:
9732 addr = 0x08;
9733 break;
9734 case EXCP_BKPT:
9735 /* Fall through to prefetch abort. */
9736 case EXCP_PREFETCH_ABORT:
9737 env->cp15.ifar_s = env->exception.vaddress;
9738 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9739 (uint32_t)env->exception.vaddress);
9740 addr = 0x0c;
9741 break;
9742 case EXCP_DATA_ABORT:
9743 env->cp15.dfar_s = env->exception.vaddress;
9744 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9745 (uint32_t)env->exception.vaddress);
9746 addr = 0x10;
9747 break;
9748 case EXCP_IRQ:
9749 addr = 0x18;
9750 break;
9751 case EXCP_FIQ:
9752 addr = 0x1c;
9753 break;
9754 case EXCP_HVC:
9755 addr = 0x08;
9756 break;
9757 case EXCP_HYP_TRAP:
9758 addr = 0x14;
9759 break;
9760 default:
9761 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9764 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9765 if (!arm_feature(env, ARM_FEATURE_V8)) {
9767 * QEMU syndrome values are v8-style. v7 has the IL bit
9768 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9769 * If this is a v7 CPU, squash the IL bit in those cases.
9771 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9772 (cs->exception_index == EXCP_DATA_ABORT &&
9773 !(env->exception.syndrome & ARM_EL_ISV)) ||
9774 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9775 env->exception.syndrome &= ~ARM_EL_IL;
9778 env->cp15.esr_el[2] = env->exception.syndrome;
9781 if (arm_current_el(env) != 2 && addr < 0x14) {
9782 addr = 0x14;
9785 mask = 0;
9786 if (!(env->cp15.scr_el3 & SCR_EA)) {
9787 mask |= CPSR_A;
9789 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9790 mask |= CPSR_I;
9792 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9793 mask |= CPSR_F;
9796 addr += env->cp15.hvbar;
9798 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9801 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9803 ARMCPU *cpu = ARM_CPU(cs);
9804 CPUARMState *env = &cpu->env;
9805 uint32_t addr;
9806 uint32_t mask;
9807 int new_mode;
9808 uint32_t offset;
9809 uint32_t moe;
9811 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9812 switch (syn_get_ec(env->exception.syndrome)) {
9813 case EC_BREAKPOINT:
9814 case EC_BREAKPOINT_SAME_EL:
9815 moe = 1;
9816 break;
9817 case EC_WATCHPOINT:
9818 case EC_WATCHPOINT_SAME_EL:
9819 moe = 10;
9820 break;
9821 case EC_AA32_BKPT:
9822 moe = 3;
9823 break;
9824 case EC_VECTORCATCH:
9825 moe = 5;
9826 break;
9827 default:
9828 moe = 0;
9829 break;
9832 if (moe) {
9833 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9836 if (env->exception.target_el == 2) {
9837 arm_cpu_do_interrupt_aarch32_hyp(cs);
9838 return;
9841 switch (cs->exception_index) {
9842 case EXCP_UDEF:
9843 new_mode = ARM_CPU_MODE_UND;
9844 addr = 0x04;
9845 mask = CPSR_I;
9846 if (env->thumb)
9847 offset = 2;
9848 else
9849 offset = 4;
9850 break;
9851 case EXCP_SWI:
9852 new_mode = ARM_CPU_MODE_SVC;
9853 addr = 0x08;
9854 mask = CPSR_I;
9855 /* The PC already points to the next instruction. */
9856 offset = 0;
9857 break;
9858 case EXCP_BKPT:
9859 /* Fall through to prefetch abort. */
9860 case EXCP_PREFETCH_ABORT:
9861 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
9862 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
9863 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
9864 env->exception.fsr, (uint32_t)env->exception.vaddress);
9865 new_mode = ARM_CPU_MODE_ABT;
9866 addr = 0x0c;
9867 mask = CPSR_A | CPSR_I;
9868 offset = 4;
9869 break;
9870 case EXCP_DATA_ABORT:
9871 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9872 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
9873 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
9874 env->exception.fsr,
9875 (uint32_t)env->exception.vaddress);
9876 new_mode = ARM_CPU_MODE_ABT;
9877 addr = 0x10;
9878 mask = CPSR_A | CPSR_I;
9879 offset = 8;
9880 break;
9881 case EXCP_IRQ:
9882 new_mode = ARM_CPU_MODE_IRQ;
9883 addr = 0x18;
9884 /* Disable IRQ and imprecise data aborts. */
9885 mask = CPSR_A | CPSR_I;
9886 offset = 4;
9887 if (env->cp15.scr_el3 & SCR_IRQ) {
9888 /* IRQ routed to monitor mode */
9889 new_mode = ARM_CPU_MODE_MON;
9890 mask |= CPSR_F;
9892 break;
9893 case EXCP_FIQ:
9894 new_mode = ARM_CPU_MODE_FIQ;
9895 addr = 0x1c;
9896 /* Disable FIQ, IRQ and imprecise data aborts. */
9897 mask = CPSR_A | CPSR_I | CPSR_F;
9898 if (env->cp15.scr_el3 & SCR_FIQ) {
9899 /* FIQ routed to monitor mode */
9900 new_mode = ARM_CPU_MODE_MON;
9902 offset = 4;
9903 break;
9904 case EXCP_VIRQ:
9905 new_mode = ARM_CPU_MODE_IRQ;
9906 addr = 0x18;
9907 /* Disable IRQ and imprecise data aborts. */
9908 mask = CPSR_A | CPSR_I;
9909 offset = 4;
9910 break;
9911 case EXCP_VFIQ:
9912 new_mode = ARM_CPU_MODE_FIQ;
9913 addr = 0x1c;
9914 /* Disable FIQ, IRQ and imprecise data aborts. */
9915 mask = CPSR_A | CPSR_I | CPSR_F;
9916 offset = 4;
9917 break;
9918 case EXCP_VSERR:
9921 * Note that this is reported as a data abort, but the DFAR
9922 * has an UNKNOWN value. Construct the SError syndrome from
9923 * AET and ExT fields.
9925 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
9927 if (extended_addresses_enabled(env)) {
9928 env->exception.fsr = arm_fi_to_lfsc(&fi);
9929 } else {
9930 env->exception.fsr = arm_fi_to_sfsc(&fi);
9932 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
9933 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9934 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
9935 env->exception.fsr);
9937 new_mode = ARM_CPU_MODE_ABT;
9938 addr = 0x10;
9939 mask = CPSR_A | CPSR_I;
9940 offset = 8;
9942 break;
9943 case EXCP_SMC:
9944 new_mode = ARM_CPU_MODE_MON;
9945 addr = 0x08;
9946 mask = CPSR_A | CPSR_I | CPSR_F;
9947 offset = 0;
9948 break;
9949 default:
9950 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9951 return; /* Never happens. Keep compiler happy. */
9954 if (new_mode == ARM_CPU_MODE_MON) {
9955 addr += env->cp15.mvbar;
9956 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
9957 /* High vectors. When enabled, base address cannot be remapped. */
9958 addr += 0xffff0000;
9959 } else {
9960 /* ARM v7 architectures provide a vector base address register to remap
9961 * the interrupt vector table.
9962 * This register is only followed in non-monitor mode, and is banked.
9963 * Note: only bits 31:5 are valid.
9965 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
9968 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9969 env->cp15.scr_el3 &= ~SCR_NS;
9972 take_aarch32_exception(env, new_mode, mask, offset, addr);
9975 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9978 * Return the register number of the AArch64 view of the AArch32
9979 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9980 * be that of the AArch32 mode the exception came from.
9982 int mode = env->uncached_cpsr & CPSR_M;
9984 switch (aarch32_reg) {
9985 case 0 ... 7:
9986 return aarch32_reg;
9987 case 8 ... 12:
9988 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9989 case 13:
9990 switch (mode) {
9991 case ARM_CPU_MODE_USR:
9992 case ARM_CPU_MODE_SYS:
9993 return 13;
9994 case ARM_CPU_MODE_HYP:
9995 return 15;
9996 case ARM_CPU_MODE_IRQ:
9997 return 17;
9998 case ARM_CPU_MODE_SVC:
9999 return 19;
10000 case ARM_CPU_MODE_ABT:
10001 return 21;
10002 case ARM_CPU_MODE_UND:
10003 return 23;
10004 case ARM_CPU_MODE_FIQ:
10005 return 29;
10006 default:
10007 g_assert_not_reached();
10009 case 14:
10010 switch (mode) {
10011 case ARM_CPU_MODE_USR:
10012 case ARM_CPU_MODE_SYS:
10013 case ARM_CPU_MODE_HYP:
10014 return 14;
10015 case ARM_CPU_MODE_IRQ:
10016 return 16;
10017 case ARM_CPU_MODE_SVC:
10018 return 18;
10019 case ARM_CPU_MODE_ABT:
10020 return 20;
10021 case ARM_CPU_MODE_UND:
10022 return 22;
10023 case ARM_CPU_MODE_FIQ:
10024 return 30;
10025 default:
10026 g_assert_not_reached();
10028 case 15:
10029 return 31;
10030 default:
10031 g_assert_not_reached();
10035 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10037 uint32_t ret = cpsr_read(env);
10039 /* Move DIT to the correct location for SPSR_ELx */
10040 if (ret & CPSR_DIT) {
10041 ret &= ~CPSR_DIT;
10042 ret |= PSTATE_DIT;
10044 /* Merge PSTATE.SS into SPSR_ELx */
10045 ret |= env->pstate & PSTATE_SS;
10047 return ret;
10050 /* Handle exception entry to a target EL which is using AArch64 */
10051 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10053 ARMCPU *cpu = ARM_CPU(cs);
10054 CPUARMState *env = &cpu->env;
10055 unsigned int new_el = env->exception.target_el;
10056 target_ulong addr = env->cp15.vbar_el[new_el];
10057 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10058 unsigned int old_mode;
10059 unsigned int cur_el = arm_current_el(env);
10060 int rt;
10063 * Note that new_el can never be 0. If cur_el is 0, then
10064 * el0_a64 is is_a64(), else el0_a64 is ignored.
10066 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10068 if (cur_el < new_el) {
10069 /* Entry vector offset depends on whether the implemented EL
10070 * immediately lower than the target level is using AArch32 or AArch64
10072 bool is_aa64;
10073 uint64_t hcr;
10075 switch (new_el) {
10076 case 3:
10077 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10078 break;
10079 case 2:
10080 hcr = arm_hcr_el2_eff(env);
10081 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10082 is_aa64 = (hcr & HCR_RW) != 0;
10083 break;
10085 /* fall through */
10086 case 1:
10087 is_aa64 = is_a64(env);
10088 break;
10089 default:
10090 g_assert_not_reached();
10093 if (is_aa64) {
10094 addr += 0x400;
10095 } else {
10096 addr += 0x600;
10098 } else if (pstate_read(env) & PSTATE_SP) {
10099 addr += 0x200;
10102 switch (cs->exception_index) {
10103 case EXCP_PREFETCH_ABORT:
10104 case EXCP_DATA_ABORT:
10105 env->cp15.far_el[new_el] = env->exception.vaddress;
10106 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10107 env->cp15.far_el[new_el]);
10108 /* fall through */
10109 case EXCP_BKPT:
10110 case EXCP_UDEF:
10111 case EXCP_SWI:
10112 case EXCP_HVC:
10113 case EXCP_HYP_TRAP:
10114 case EXCP_SMC:
10115 switch (syn_get_ec(env->exception.syndrome)) {
10116 case EC_ADVSIMDFPACCESSTRAP:
10118 * QEMU internal FP/SIMD syndromes from AArch32 include the
10119 * TA and coproc fields which are only exposed if the exception
10120 * is taken to AArch32 Hyp mode. Mask them out to get a valid
10121 * AArch64 format syndrome.
10123 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10124 break;
10125 case EC_CP14RTTRAP:
10126 case EC_CP15RTTRAP:
10127 case EC_CP14DTTRAP:
10129 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10130 * the raw register field from the insn; when taking this to
10131 * AArch64 we must convert it to the AArch64 view of the register
10132 * number. Notice that we read a 4-bit AArch32 register number and
10133 * write back a 5-bit AArch64 one.
10135 rt = extract32(env->exception.syndrome, 5, 4);
10136 rt = aarch64_regnum(env, rt);
10137 env->exception.syndrome = deposit32(env->exception.syndrome,
10138 5, 5, rt);
10139 break;
10140 case EC_CP15RRTTRAP:
10141 case EC_CP14RRTTRAP:
10142 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10143 rt = extract32(env->exception.syndrome, 5, 4);
10144 rt = aarch64_regnum(env, rt);
10145 env->exception.syndrome = deposit32(env->exception.syndrome,
10146 5, 5, rt);
10147 rt = extract32(env->exception.syndrome, 10, 4);
10148 rt = aarch64_regnum(env, rt);
10149 env->exception.syndrome = deposit32(env->exception.syndrome,
10150 10, 5, rt);
10151 break;
10153 env->cp15.esr_el[new_el] = env->exception.syndrome;
10154 break;
10155 case EXCP_IRQ:
10156 case EXCP_VIRQ:
10157 addr += 0x80;
10158 break;
10159 case EXCP_FIQ:
10160 case EXCP_VFIQ:
10161 addr += 0x100;
10162 break;
10163 case EXCP_VSERR:
10164 addr += 0x180;
10165 /* Construct the SError syndrome from IDS and ISS fields. */
10166 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10167 env->cp15.esr_el[new_el] = env->exception.syndrome;
10168 break;
10169 default:
10170 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10173 if (is_a64(env)) {
10174 old_mode = pstate_read(env);
10175 aarch64_save_sp(env, arm_current_el(env));
10176 env->elr_el[new_el] = env->pc;
10177 } else {
10178 old_mode = cpsr_read_for_spsr_elx(env);
10179 env->elr_el[new_el] = env->regs[15];
10181 aarch64_sync_32_to_64(env);
10183 env->condexec_bits = 0;
10185 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10187 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10188 env->elr_el[new_el]);
10190 if (cpu_isar_feature(aa64_pan, cpu)) {
10191 /* The value of PSTATE.PAN is normally preserved, except when ... */
10192 new_mode |= old_mode & PSTATE_PAN;
10193 switch (new_el) {
10194 case 2:
10195 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
10196 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10197 != (HCR_E2H | HCR_TGE)) {
10198 break;
10200 /* fall through */
10201 case 1:
10202 /* ... the target is EL1 ... */
10203 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10204 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10205 new_mode |= PSTATE_PAN;
10207 break;
10210 if (cpu_isar_feature(aa64_mte, cpu)) {
10211 new_mode |= PSTATE_TCO;
10214 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10215 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10216 new_mode |= PSTATE_SSBS;
10217 } else {
10218 new_mode &= ~PSTATE_SSBS;
10222 pstate_write(env, PSTATE_DAIF | new_mode);
10223 env->aarch64 = true;
10224 aarch64_restore_sp(env, new_el);
10225 helper_rebuild_hflags_a64(env, new_el);
10227 env->pc = addr;
10229 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10230 new_el, env->pc, pstate_read(env));
10234 * Do semihosting call and set the appropriate return value. All the
10235 * permission and validity checks have been done at translate time.
10237 * We only see semihosting exceptions in TCG only as they are not
10238 * trapped to the hypervisor in KVM.
10240 #ifdef CONFIG_TCG
10241 static void handle_semihosting(CPUState *cs)
10243 ARMCPU *cpu = ARM_CPU(cs);
10244 CPUARMState *env = &cpu->env;
10246 if (is_a64(env)) {
10247 qemu_log_mask(CPU_LOG_INT,
10248 "...handling as semihosting call 0x%" PRIx64 "\n",
10249 env->xregs[0]);
10250 env->xregs[0] = do_common_semihosting(cs);
10251 env->pc += 4;
10252 } else {
10253 qemu_log_mask(CPU_LOG_INT,
10254 "...handling as semihosting call 0x%x\n",
10255 env->regs[0]);
10256 env->regs[0] = do_common_semihosting(cs);
10257 env->regs[15] += env->thumb ? 2 : 4;
10260 #endif
10262 /* Handle a CPU exception for A and R profile CPUs.
10263 * Do any appropriate logging, handle PSCI calls, and then hand off
10264 * to the AArch64-entry or AArch32-entry function depending on the
10265 * target exception level's register width.
10267 * Note: this is used for both TCG (as the do_interrupt tcg op),
10268 * and KVM to re-inject guest debug exceptions, and to
10269 * inject a Synchronous-External-Abort.
10271 void arm_cpu_do_interrupt(CPUState *cs)
10273 ARMCPU *cpu = ARM_CPU(cs);
10274 CPUARMState *env = &cpu->env;
10275 unsigned int new_el = env->exception.target_el;
10277 assert(!arm_feature(env, ARM_FEATURE_M));
10279 arm_log_exception(cs);
10280 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10281 new_el);
10282 if (qemu_loglevel_mask(CPU_LOG_INT)
10283 && !excp_is_internal(cs->exception_index)) {
10284 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10285 syn_get_ec(env->exception.syndrome),
10286 env->exception.syndrome);
10289 if (arm_is_psci_call(cpu, cs->exception_index)) {
10290 arm_handle_psci_call(cpu);
10291 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10292 return;
10296 * Semihosting semantics depend on the register width of the code
10297 * that caused the exception, not the target exception level, so
10298 * must be handled here.
10300 #ifdef CONFIG_TCG
10301 if (cs->exception_index == EXCP_SEMIHOST) {
10302 handle_semihosting(cs);
10303 return;
10305 #endif
10307 /* Hooks may change global state so BQL should be held, also the
10308 * BQL needs to be held for any modification of
10309 * cs->interrupt_request.
10311 g_assert(qemu_mutex_iothread_locked());
10313 arm_call_pre_el_change_hook(cpu);
10315 assert(!excp_is_internal(cs->exception_index));
10316 if (arm_el_is_aa64(env, new_el)) {
10317 arm_cpu_do_interrupt_aarch64(cs);
10318 } else {
10319 arm_cpu_do_interrupt_aarch32(cs);
10322 arm_call_el_change_hook(cpu);
10324 if (!kvm_enabled()) {
10325 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10328 #endif /* !CONFIG_USER_ONLY */
10330 uint64_t arm_sctlr(CPUARMState *env, int el)
10332 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10333 if (el == 0) {
10334 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10335 el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10336 ? 2 : 1;
10338 return env->cp15.sctlr_el[el];
10341 /* Return the SCTLR value which controls this address translation regime */
10342 static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
10344 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
10347 #ifndef CONFIG_USER_ONLY
10349 /* Return true if the specified stage of address translation is disabled */
10350 static inline bool regime_translation_disabled(CPUARMState *env,
10351 ARMMMUIdx mmu_idx)
10353 uint64_t hcr_el2;
10355 if (arm_feature(env, ARM_FEATURE_M)) {
10356 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
10357 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
10358 case R_V7M_MPU_CTRL_ENABLE_MASK:
10359 /* Enabled, but not for HardFault and NMI */
10360 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
10361 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
10362 /* Enabled for all cases */
10363 return false;
10364 case 0:
10365 default:
10366 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
10367 * we warned about that in armv7m_nvic.c when the guest set it.
10369 return true;
10373 hcr_el2 = arm_hcr_el2_eff(env);
10375 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10376 /* HCR.DC means HCR.VM behaves as 1 */
10377 return (hcr_el2 & (HCR_DC | HCR_VM)) == 0;
10380 if (hcr_el2 & HCR_TGE) {
10381 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
10382 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
10383 return true;
10387 if ((hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
10388 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
10389 return true;
10392 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
10395 static inline bool regime_translation_big_endian(CPUARMState *env,
10396 ARMMMUIdx mmu_idx)
10398 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
10401 /* Return the TTBR associated with this translation regime */
10402 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
10403 int ttbrn)
10405 if (mmu_idx == ARMMMUIdx_Stage2) {
10406 return env->cp15.vttbr_el2;
10408 if (mmu_idx == ARMMMUIdx_Stage2_S) {
10409 return env->cp15.vsttbr_el2;
10411 if (ttbrn == 0) {
10412 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
10413 } else {
10414 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
10418 #endif /* !CONFIG_USER_ONLY */
10420 /* Convert a possible stage1+2 MMU index into the appropriate
10421 * stage 1 MMU index
10423 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
10425 switch (mmu_idx) {
10426 case ARMMMUIdx_SE10_0:
10427 return ARMMMUIdx_Stage1_SE0;
10428 case ARMMMUIdx_SE10_1:
10429 return ARMMMUIdx_Stage1_SE1;
10430 case ARMMMUIdx_SE10_1_PAN:
10431 return ARMMMUIdx_Stage1_SE1_PAN;
10432 case ARMMMUIdx_E10_0:
10433 return ARMMMUIdx_Stage1_E0;
10434 case ARMMMUIdx_E10_1:
10435 return ARMMMUIdx_Stage1_E1;
10436 case ARMMMUIdx_E10_1_PAN:
10437 return ARMMMUIdx_Stage1_E1_PAN;
10438 default:
10439 return mmu_idx;
10443 /* Return true if the translation regime is using LPAE format page tables */
10444 static inline bool regime_using_lpae_format(CPUARMState *env,
10445 ARMMMUIdx mmu_idx)
10447 int el = regime_el(env, mmu_idx);
10448 if (el == 2 || arm_el_is_aa64(env, el)) {
10449 return true;
10451 if (arm_feature(env, ARM_FEATURE_LPAE)
10452 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
10453 return true;
10455 return false;
10458 /* Returns true if the stage 1 translation regime is using LPAE format page
10459 * tables. Used when raising alignment exceptions, whose FSR changes depending
10460 * on whether the long or short descriptor format is in use. */
10461 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
10463 mmu_idx = stage_1_mmu_idx(mmu_idx);
10465 return regime_using_lpae_format(env, mmu_idx);
10468 #ifndef CONFIG_USER_ONLY
10469 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
10471 switch (mmu_idx) {
10472 case ARMMMUIdx_SE10_0:
10473 case ARMMMUIdx_E20_0:
10474 case ARMMMUIdx_SE20_0:
10475 case ARMMMUIdx_Stage1_E0:
10476 case ARMMMUIdx_Stage1_SE0:
10477 case ARMMMUIdx_MUser:
10478 case ARMMMUIdx_MSUser:
10479 case ARMMMUIdx_MUserNegPri:
10480 case ARMMMUIdx_MSUserNegPri:
10481 return true;
10482 default:
10483 return false;
10484 case ARMMMUIdx_E10_0:
10485 case ARMMMUIdx_E10_1:
10486 case ARMMMUIdx_E10_1_PAN:
10487 g_assert_not_reached();
10491 /* Translate section/page access permissions to page
10492 * R/W protection flags
10494 * @env: CPUARMState
10495 * @mmu_idx: MMU index indicating required translation regime
10496 * @ap: The 3-bit access permissions (AP[2:0])
10497 * @domain_prot: The 2-bit domain access permissions
10499 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
10500 int ap, int domain_prot)
10502 bool is_user = regime_is_user(env, mmu_idx);
10504 if (domain_prot == 3) {
10505 return PAGE_READ | PAGE_WRITE;
10508 switch (ap) {
10509 case 0:
10510 if (arm_feature(env, ARM_FEATURE_V7)) {
10511 return 0;
10513 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
10514 case SCTLR_S:
10515 return is_user ? 0 : PAGE_READ;
10516 case SCTLR_R:
10517 return PAGE_READ;
10518 default:
10519 return 0;
10521 case 1:
10522 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10523 case 2:
10524 if (is_user) {
10525 return PAGE_READ;
10526 } else {
10527 return PAGE_READ | PAGE_WRITE;
10529 case 3:
10530 return PAGE_READ | PAGE_WRITE;
10531 case 4: /* Reserved. */
10532 return 0;
10533 case 5:
10534 return is_user ? 0 : PAGE_READ;
10535 case 6:
10536 return PAGE_READ;
10537 case 7:
10538 if (!arm_feature(env, ARM_FEATURE_V6K)) {
10539 return 0;
10541 return PAGE_READ;
10542 default:
10543 g_assert_not_reached();
10547 /* Translate section/page access permissions to page
10548 * R/W protection flags.
10550 * @ap: The 2-bit simple AP (AP[2:1])
10551 * @is_user: TRUE if accessing from PL0
10553 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
10555 switch (ap) {
10556 case 0:
10557 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10558 case 1:
10559 return PAGE_READ | PAGE_WRITE;
10560 case 2:
10561 return is_user ? 0 : PAGE_READ;
10562 case 3:
10563 return PAGE_READ;
10564 default:
10565 g_assert_not_reached();
10569 static inline int
10570 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10572 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10575 /* Translate S2 section/page access permissions to protection flags
10577 * @env: CPUARMState
10578 * @s2ap: The 2-bit stage2 access permissions (S2AP)
10579 * @xn: XN (execute-never) bits
10580 * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
10582 static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
10584 int prot = 0;
10586 if (s2ap & 1) {
10587 prot |= PAGE_READ;
10589 if (s2ap & 2) {
10590 prot |= PAGE_WRITE;
10593 if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
10594 switch (xn) {
10595 case 0:
10596 prot |= PAGE_EXEC;
10597 break;
10598 case 1:
10599 if (s1_is_el0) {
10600 prot |= PAGE_EXEC;
10602 break;
10603 case 2:
10604 break;
10605 case 3:
10606 if (!s1_is_el0) {
10607 prot |= PAGE_EXEC;
10609 break;
10610 default:
10611 g_assert_not_reached();
10613 } else {
10614 if (!extract32(xn, 1, 1)) {
10615 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10616 prot |= PAGE_EXEC;
10620 return prot;
10623 /* Translate section/page access permissions to protection flags
10625 * @env: CPUARMState
10626 * @mmu_idx: MMU index indicating required translation regime
10627 * @is_aa64: TRUE if AArch64
10628 * @ap: The 2-bit simple AP (AP[2:1])
10629 * @ns: NS (non-secure) bit
10630 * @xn: XN (execute-never) bit
10631 * @pxn: PXN (privileged execute-never) bit
10633 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10634 int ap, int ns, int xn, int pxn)
10636 bool is_user = regime_is_user(env, mmu_idx);
10637 int prot_rw, user_rw;
10638 bool have_wxn;
10639 int wxn = 0;
10641 assert(mmu_idx != ARMMMUIdx_Stage2);
10642 assert(mmu_idx != ARMMMUIdx_Stage2_S);
10644 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10645 if (is_user) {
10646 prot_rw = user_rw;
10647 } else {
10648 if (user_rw && regime_is_pan(env, mmu_idx)) {
10649 /* PAN forbids data accesses but doesn't affect insn fetch */
10650 prot_rw = 0;
10651 } else {
10652 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
10656 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10657 return prot_rw;
10660 /* TODO have_wxn should be replaced with
10661 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10662 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10663 * compatible processors have EL2, which is required for [U]WXN.
10665 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10667 if (have_wxn) {
10668 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10671 if (is_aa64) {
10672 if (regime_has_2_ranges(mmu_idx) && !is_user) {
10673 xn = pxn || (user_rw & PAGE_WRITE);
10675 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10676 switch (regime_el(env, mmu_idx)) {
10677 case 1:
10678 case 3:
10679 if (is_user) {
10680 xn = xn || !(user_rw & PAGE_READ);
10681 } else {
10682 int uwxn = 0;
10683 if (have_wxn) {
10684 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10686 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10687 (uwxn && (user_rw & PAGE_WRITE));
10689 break;
10690 case 2:
10691 break;
10693 } else {
10694 xn = wxn = 0;
10697 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10698 return prot_rw;
10700 return prot_rw | PAGE_EXEC;
10703 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10704 uint32_t *table, uint32_t address)
10706 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
10707 TCR *tcr = regime_tcr(env, mmu_idx);
10709 if (address & tcr->mask) {
10710 if (tcr->raw_tcr & TTBCR_PD1) {
10711 /* Translation table walk disabled for TTBR1 */
10712 return false;
10714 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
10715 } else {
10716 if (tcr->raw_tcr & TTBCR_PD0) {
10717 /* Translation table walk disabled for TTBR0 */
10718 return false;
10720 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
10722 *table |= (address >> 18) & 0x3ffc;
10723 return true;
10726 static bool ptw_attrs_are_device(CPUARMState *env, ARMCacheAttrs cacheattrs)
10729 * For an S1 page table walk, the stage 1 attributes are always
10730 * some form of "this is Normal memory". The combined S1+S2
10731 * attributes are therefore only Device if stage 2 specifies Device.
10732 * With HCR_EL2.FWB == 0 this is when descriptor bits [5:4] are 0b00,
10733 * ie when cacheattrs.attrs bits [3:2] are 0b00.
10735 assert(cacheattrs.is_s2_format);
10736 return (cacheattrs.attrs & 0xc) == 0;
10739 /* Translate a S1 pagetable walk through S2 if needed. */
10740 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
10741 hwaddr addr, bool *is_secure,
10742 ARMMMUFaultInfo *fi)
10744 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
10745 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
10746 target_ulong s2size;
10747 hwaddr s2pa;
10748 int s2prot;
10749 int ret;
10750 ARMMMUIdx s2_mmu_idx = *is_secure ? ARMMMUIdx_Stage2_S
10751 : ARMMMUIdx_Stage2;
10752 ARMCacheAttrs cacheattrs = {};
10753 MemTxAttrs txattrs = {};
10755 ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, s2_mmu_idx, false,
10756 &s2pa, &txattrs, &s2prot, &s2size, fi,
10757 &cacheattrs);
10758 if (ret) {
10759 assert(fi->type != ARMFault_None);
10760 fi->s2addr = addr;
10761 fi->stage2 = true;
10762 fi->s1ptw = true;
10763 fi->s1ns = !*is_secure;
10764 return ~0;
10766 if ((arm_hcr_el2_eff(env) & HCR_PTW) &&
10767 ptw_attrs_are_device(env, cacheattrs)) {
10769 * PTW set and S1 walk touched S2 Device memory:
10770 * generate Permission fault.
10772 fi->type = ARMFault_Permission;
10773 fi->s2addr = addr;
10774 fi->stage2 = true;
10775 fi->s1ptw = true;
10776 fi->s1ns = !*is_secure;
10777 return ~0;
10780 if (arm_is_secure_below_el3(env)) {
10781 /* Check if page table walk is to secure or non-secure PA space. */
10782 if (*is_secure) {
10783 *is_secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
10784 } else {
10785 *is_secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
10787 } else {
10788 assert(!*is_secure);
10791 addr = s2pa;
10793 return addr;
10796 /* All loads done in the course of a page table walk go through here. */
10797 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
10798 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
10800 ARMCPU *cpu = ARM_CPU(cs);
10801 CPUARMState *env = &cpu->env;
10802 MemTxAttrs attrs = {};
10803 MemTxResult result = MEMTX_OK;
10804 AddressSpace *as;
10805 uint32_t data;
10807 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
10808 attrs.secure = is_secure;
10809 as = arm_addressspace(cs, attrs);
10810 if (fi->s1ptw) {
10811 return 0;
10813 if (regime_translation_big_endian(env, mmu_idx)) {
10814 data = address_space_ldl_be(as, addr, attrs, &result);
10815 } else {
10816 data = address_space_ldl_le(as, addr, attrs, &result);
10818 if (result == MEMTX_OK) {
10819 return data;
10821 fi->type = ARMFault_SyncExternalOnWalk;
10822 fi->ea = arm_extabort_type(result);
10823 return 0;
10826 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
10827 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
10829 ARMCPU *cpu = ARM_CPU(cs);
10830 CPUARMState *env = &cpu->env;
10831 MemTxAttrs attrs = {};
10832 MemTxResult result = MEMTX_OK;
10833 AddressSpace *as;
10834 uint64_t data;
10836 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
10837 attrs.secure = is_secure;
10838 as = arm_addressspace(cs, attrs);
10839 if (fi->s1ptw) {
10840 return 0;
10842 if (regime_translation_big_endian(env, mmu_idx)) {
10843 data = address_space_ldq_be(as, addr, attrs, &result);
10844 } else {
10845 data = address_space_ldq_le(as, addr, attrs, &result);
10847 if (result == MEMTX_OK) {
10848 return data;
10850 fi->type = ARMFault_SyncExternalOnWalk;
10851 fi->ea = arm_extabort_type(result);
10852 return 0;
10855 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
10856 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10857 hwaddr *phys_ptr, int *prot,
10858 target_ulong *page_size,
10859 ARMMMUFaultInfo *fi)
10861 CPUState *cs = env_cpu(env);
10862 int level = 1;
10863 uint32_t table;
10864 uint32_t desc;
10865 int type;
10866 int ap;
10867 int domain = 0;
10868 int domain_prot;
10869 hwaddr phys_addr;
10870 uint32_t dacr;
10872 /* Pagetable walk. */
10873 /* Lookup l1 descriptor. */
10874 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
10875 /* Section translation fault if page walk is disabled by PD0 or PD1 */
10876 fi->type = ARMFault_Translation;
10877 goto do_fault;
10879 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10880 mmu_idx, fi);
10881 if (fi->type != ARMFault_None) {
10882 goto do_fault;
10884 type = (desc & 3);
10885 domain = (desc >> 5) & 0x0f;
10886 if (regime_el(env, mmu_idx) == 1) {
10887 dacr = env->cp15.dacr_ns;
10888 } else {
10889 dacr = env->cp15.dacr_s;
10891 domain_prot = (dacr >> (domain * 2)) & 3;
10892 if (type == 0) {
10893 /* Section translation fault. */
10894 fi->type = ARMFault_Translation;
10895 goto do_fault;
10897 if (type != 2) {
10898 level = 2;
10900 if (domain_prot == 0 || domain_prot == 2) {
10901 fi->type = ARMFault_Domain;
10902 goto do_fault;
10904 if (type == 2) {
10905 /* 1Mb section. */
10906 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10907 ap = (desc >> 10) & 3;
10908 *page_size = 1024 * 1024;
10909 } else {
10910 /* Lookup l2 entry. */
10911 if (type == 1) {
10912 /* Coarse pagetable. */
10913 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10914 } else {
10915 /* Fine pagetable. */
10916 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10918 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
10919 mmu_idx, fi);
10920 if (fi->type != ARMFault_None) {
10921 goto do_fault;
10923 switch (desc & 3) {
10924 case 0: /* Page translation fault. */
10925 fi->type = ARMFault_Translation;
10926 goto do_fault;
10927 case 1: /* 64k page. */
10928 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10929 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
10930 *page_size = 0x10000;
10931 break;
10932 case 2: /* 4k page. */
10933 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10934 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
10935 *page_size = 0x1000;
10936 break;
10937 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
10938 if (type == 1) {
10939 /* ARMv6/XScale extended small page format */
10940 if (arm_feature(env, ARM_FEATURE_XSCALE)
10941 || arm_feature(env, ARM_FEATURE_V6)) {
10942 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10943 *page_size = 0x1000;
10944 } else {
10945 /* UNPREDICTABLE in ARMv5; we choose to take a
10946 * page translation fault.
10948 fi->type = ARMFault_Translation;
10949 goto do_fault;
10951 } else {
10952 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
10953 *page_size = 0x400;
10955 ap = (desc >> 4) & 3;
10956 break;
10957 default:
10958 /* Never happens, but compiler isn't smart enough to tell. */
10959 g_assert_not_reached();
10962 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10963 *prot |= *prot ? PAGE_EXEC : 0;
10964 if (!(*prot & (1 << access_type))) {
10965 /* Access permission fault. */
10966 fi->type = ARMFault_Permission;
10967 goto do_fault;
10969 *phys_ptr = phys_addr;
10970 return false;
10971 do_fault:
10972 fi->domain = domain;
10973 fi->level = level;
10974 return true;
10977 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
10978 MMUAccessType access_type, ARMMMUIdx mmu_idx,
10979 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
10980 target_ulong *page_size, ARMMMUFaultInfo *fi)
10982 CPUState *cs = env_cpu(env);
10983 ARMCPU *cpu = env_archcpu(env);
10984 int level = 1;
10985 uint32_t table;
10986 uint32_t desc;
10987 uint32_t xn;
10988 uint32_t pxn = 0;
10989 int type;
10990 int ap;
10991 int domain = 0;
10992 int domain_prot;
10993 hwaddr phys_addr;
10994 uint32_t dacr;
10995 bool ns;
10997 /* Pagetable walk. */
10998 /* Lookup l1 descriptor. */
10999 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
11000 /* Section translation fault if page walk is disabled by PD0 or PD1 */
11001 fi->type = ARMFault_Translation;
11002 goto do_fault;
11004 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
11005 mmu_idx, fi);
11006 if (fi->type != ARMFault_None) {
11007 goto do_fault;
11009 type = (desc & 3);
11010 if (type == 0 || (type == 3 && !cpu_isar_feature(aa32_pxn, cpu))) {
11011 /* Section translation fault, or attempt to use the encoding
11012 * which is Reserved on implementations without PXN.
11014 fi->type = ARMFault_Translation;
11015 goto do_fault;
11017 if ((type == 1) || !(desc & (1 << 18))) {
11018 /* Page or Section. */
11019 domain = (desc >> 5) & 0x0f;
11021 if (regime_el(env, mmu_idx) == 1) {
11022 dacr = env->cp15.dacr_ns;
11023 } else {
11024 dacr = env->cp15.dacr_s;
11026 if (type == 1) {
11027 level = 2;
11029 domain_prot = (dacr >> (domain * 2)) & 3;
11030 if (domain_prot == 0 || domain_prot == 2) {
11031 /* Section or Page domain fault */
11032 fi->type = ARMFault_Domain;
11033 goto do_fault;
11035 if (type != 1) {
11036 if (desc & (1 << 18)) {
11037 /* Supersection. */
11038 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
11039 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
11040 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
11041 *page_size = 0x1000000;
11042 } else {
11043 /* Section. */
11044 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
11045 *page_size = 0x100000;
11047 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
11048 xn = desc & (1 << 4);
11049 pxn = desc & 1;
11050 ns = extract32(desc, 19, 1);
11051 } else {
11052 if (cpu_isar_feature(aa32_pxn, cpu)) {
11053 pxn = (desc >> 2) & 1;
11055 ns = extract32(desc, 3, 1);
11056 /* Lookup l2 entry. */
11057 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
11058 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
11059 mmu_idx, fi);
11060 if (fi->type != ARMFault_None) {
11061 goto do_fault;
11063 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
11064 switch (desc & 3) {
11065 case 0: /* Page translation fault. */
11066 fi->type = ARMFault_Translation;
11067 goto do_fault;
11068 case 1: /* 64k page. */
11069 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
11070 xn = desc & (1 << 15);
11071 *page_size = 0x10000;
11072 break;
11073 case 2: case 3: /* 4k page. */
11074 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
11075 xn = desc & 1;
11076 *page_size = 0x1000;
11077 break;
11078 default:
11079 /* Never happens, but compiler isn't smart enough to tell. */
11080 g_assert_not_reached();
11083 if (domain_prot == 3) {
11084 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11085 } else {
11086 if (pxn && !regime_is_user(env, mmu_idx)) {
11087 xn = 1;
11089 if (xn && access_type == MMU_INST_FETCH) {
11090 fi->type = ARMFault_Permission;
11091 goto do_fault;
11094 if (arm_feature(env, ARM_FEATURE_V6K) &&
11095 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
11096 /* The simplified model uses AP[0] as an access control bit. */
11097 if ((ap & 1) == 0) {
11098 /* Access flag fault. */
11099 fi->type = ARMFault_AccessFlag;
11100 goto do_fault;
11102 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
11103 } else {
11104 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
11106 if (*prot && !xn) {
11107 *prot |= PAGE_EXEC;
11109 if (!(*prot & (1 << access_type))) {
11110 /* Access permission fault. */
11111 fi->type = ARMFault_Permission;
11112 goto do_fault;
11115 if (ns) {
11116 /* The NS bit will (as required by the architecture) have no effect if
11117 * the CPU doesn't support TZ or this is a non-secure translation
11118 * regime, because the attribute will already be non-secure.
11120 attrs->secure = false;
11122 *phys_ptr = phys_addr;
11123 return false;
11124 do_fault:
11125 fi->domain = domain;
11126 fi->level = level;
11127 return true;
11131 * check_s2_mmu_setup
11132 * @cpu: ARMCPU
11133 * @is_aa64: True if the translation regime is in AArch64 state
11134 * @startlevel: Suggested starting level
11135 * @inputsize: Bitsize of IPAs
11136 * @stride: Page-table stride (See the ARM ARM)
11138 * Returns true if the suggested S2 translation parameters are OK and
11139 * false otherwise.
11141 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
11142 int inputsize, int stride, int outputsize)
11144 const int grainsize = stride + 3;
11145 int startsizecheck;
11148 * Negative levels are usually not allowed...
11149 * Except for FEAT_LPA2, 4k page table, 52-bit address space, which
11150 * begins with level -1. Note that previous feature tests will have
11151 * eliminated this combination if it is not enabled.
11153 if (level < (inputsize == 52 && stride == 9 ? -1 : 0)) {
11154 return false;
11157 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
11158 if (startsizecheck < 1 || startsizecheck > stride + 4) {
11159 return false;
11162 if (is_aa64) {
11163 switch (stride) {
11164 case 13: /* 64KB Pages. */
11165 if (level == 0 || (level == 1 && outputsize <= 42)) {
11166 return false;
11168 break;
11169 case 11: /* 16KB Pages. */
11170 if (level == 0 || (level == 1 && outputsize <= 40)) {
11171 return false;
11173 break;
11174 case 9: /* 4KB Pages. */
11175 if (level == 0 && outputsize <= 42) {
11176 return false;
11178 break;
11179 default:
11180 g_assert_not_reached();
11183 /* Inputsize checks. */
11184 if (inputsize > outputsize &&
11185 (arm_el_is_aa64(&cpu->env, 1) || inputsize > 40)) {
11186 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
11187 return false;
11189 } else {
11190 /* AArch32 only supports 4KB pages. Assert on that. */
11191 assert(stride == 9);
11193 if (level == 0) {
11194 return false;
11197 return true;
11200 /* Translate from the 4-bit stage 2 representation of
11201 * memory attributes (without cache-allocation hints) to
11202 * the 8-bit representation of the stage 1 MAIR registers
11203 * (which includes allocation hints).
11205 * ref: shared/translation/attrs/S2AttrDecode()
11206 * .../S2ConvertAttrsHints()
11208 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
11210 uint8_t hiattr = extract32(s2attrs, 2, 2);
11211 uint8_t loattr = extract32(s2attrs, 0, 2);
11212 uint8_t hihint = 0, lohint = 0;
11214 if (hiattr != 0) { /* normal memory */
11215 if (arm_hcr_el2_eff(env) & HCR_CD) { /* cache disabled */
11216 hiattr = loattr = 1; /* non-cacheable */
11217 } else {
11218 if (hiattr != 1) { /* Write-through or write-back */
11219 hihint = 3; /* RW allocate */
11221 if (loattr != 1) { /* Write-through or write-back */
11222 lohint = 3; /* RW allocate */
11227 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
11229 #endif /* !CONFIG_USER_ONLY */
11231 /* This mapping is common between ID_AA64MMFR0.PARANGE and TCR_ELx.{I}PS. */
11232 static const uint8_t pamax_map[] = {
11233 [0] = 32,
11234 [1] = 36,
11235 [2] = 40,
11236 [3] = 42,
11237 [4] = 44,
11238 [5] = 48,
11239 [6] = 52,
11242 /* The cpu-specific constant value of PAMax; also used by hw/arm/virt. */
11243 unsigned int arm_pamax(ARMCPU *cpu)
11245 unsigned int parange =
11246 FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11249 * id_aa64mmfr0 is a read-only register so values outside of the
11250 * supported mappings can be considered an implementation error.
11252 assert(parange < ARRAY_SIZE(pamax_map));
11253 return pamax_map[parange];
11256 static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11258 if (regime_has_2_ranges(mmu_idx)) {
11259 return extract64(tcr, 37, 2);
11260 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11261 return 0; /* VTCR_EL2 */
11262 } else {
11263 /* Replicate the single TBI bit so we always have 2 bits. */
11264 return extract32(tcr, 20, 1) * 3;
11268 static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11270 if (regime_has_2_ranges(mmu_idx)) {
11271 return extract64(tcr, 51, 2);
11272 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11273 return 0; /* VTCR_EL2 */
11274 } else {
11275 /* Replicate the single TBID bit so we always have 2 bits. */
11276 return extract32(tcr, 29, 1) * 3;
11280 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11282 if (regime_has_2_ranges(mmu_idx)) {
11283 return extract64(tcr, 57, 2);
11284 } else {
11285 /* Replicate the single TCMA bit so we always have 2 bits. */
11286 return extract32(tcr, 30, 1) * 3;
11290 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11291 ARMMMUIdx mmu_idx, bool data)
11293 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11294 bool epd, hpd, using16k, using64k, tsz_oob, ds;
11295 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11296 ARMCPU *cpu = env_archcpu(env);
11298 if (!regime_has_2_ranges(mmu_idx)) {
11299 select = 0;
11300 tsz = extract32(tcr, 0, 6);
11301 using64k = extract32(tcr, 14, 1);
11302 using16k = extract32(tcr, 15, 1);
11303 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11304 /* VTCR_EL2 */
11305 hpd = false;
11306 } else {
11307 hpd = extract32(tcr, 24, 1);
11309 epd = false;
11310 sh = extract32(tcr, 12, 2);
11311 ps = extract32(tcr, 16, 3);
11312 ds = extract64(tcr, 32, 1);
11313 } else {
11315 * Bit 55 is always between the two regions, and is canonical for
11316 * determining if address tagging is enabled.
11318 select = extract64(va, 55, 1);
11319 if (!select) {
11320 tsz = extract32(tcr, 0, 6);
11321 epd = extract32(tcr, 7, 1);
11322 sh = extract32(tcr, 12, 2);
11323 using64k = extract32(tcr, 14, 1);
11324 using16k = extract32(tcr, 15, 1);
11325 hpd = extract64(tcr, 41, 1);
11326 } else {
11327 int tg = extract32(tcr, 30, 2);
11328 using16k = tg == 1;
11329 using64k = tg == 3;
11330 tsz = extract32(tcr, 16, 6);
11331 epd = extract32(tcr, 23, 1);
11332 sh = extract32(tcr, 28, 2);
11333 hpd = extract64(tcr, 42, 1);
11335 ps = extract64(tcr, 32, 3);
11336 ds = extract64(tcr, 59, 1);
11339 if (cpu_isar_feature(aa64_st, cpu)) {
11340 max_tsz = 48 - using64k;
11341 } else {
11342 max_tsz = 39;
11346 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11347 * adjust the effective value of DS, as documented.
11349 min_tsz = 16;
11350 if (using64k) {
11351 if (cpu_isar_feature(aa64_lva, cpu)) {
11352 min_tsz = 12;
11354 ds = false;
11355 } else if (ds) {
11356 switch (mmu_idx) {
11357 case ARMMMUIdx_Stage2:
11358 case ARMMMUIdx_Stage2_S:
11359 if (using16k) {
11360 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11361 } else {
11362 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11364 break;
11365 default:
11366 if (using16k) {
11367 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11368 } else {
11369 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11371 break;
11373 if (ds) {
11374 min_tsz = 12;
11378 if (tsz > max_tsz) {
11379 tsz = max_tsz;
11380 tsz_oob = true;
11381 } else if (tsz < min_tsz) {
11382 tsz = min_tsz;
11383 tsz_oob = true;
11384 } else {
11385 tsz_oob = false;
11388 /* Present TBI as a composite with TBID. */
11389 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11390 if (!data) {
11391 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11393 tbi = (tbi >> select) & 1;
11395 return (ARMVAParameters) {
11396 .tsz = tsz,
11397 .ps = ps,
11398 .sh = sh,
11399 .select = select,
11400 .tbi = tbi,
11401 .epd = epd,
11402 .hpd = hpd,
11403 .using16k = using16k,
11404 .using64k = using64k,
11405 .tsz_oob = tsz_oob,
11406 .ds = ds,
11410 #ifndef CONFIG_USER_ONLY
11411 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
11412 ARMMMUIdx mmu_idx)
11414 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11415 uint32_t el = regime_el(env, mmu_idx);
11416 int select, tsz;
11417 bool epd, hpd;
11419 assert(mmu_idx != ARMMMUIdx_Stage2_S);
11421 if (mmu_idx == ARMMMUIdx_Stage2) {
11422 /* VTCR */
11423 bool sext = extract32(tcr, 4, 1);
11424 bool sign = extract32(tcr, 3, 1);
11427 * If the sign-extend bit is not the same as t0sz[3], the result
11428 * is unpredictable. Flag this as a guest error.
11430 if (sign != sext) {
11431 qemu_log_mask(LOG_GUEST_ERROR,
11432 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
11434 tsz = sextract32(tcr, 0, 4) + 8;
11435 select = 0;
11436 hpd = false;
11437 epd = false;
11438 } else if (el == 2) {
11439 /* HTCR */
11440 tsz = extract32(tcr, 0, 3);
11441 select = 0;
11442 hpd = extract64(tcr, 24, 1);
11443 epd = false;
11444 } else {
11445 int t0sz = extract32(tcr, 0, 3);
11446 int t1sz = extract32(tcr, 16, 3);
11448 if (t1sz == 0) {
11449 select = va > (0xffffffffu >> t0sz);
11450 } else {
11451 /* Note that we will detect errors later. */
11452 select = va >= ~(0xffffffffu >> t1sz);
11454 if (!select) {
11455 tsz = t0sz;
11456 epd = extract32(tcr, 7, 1);
11457 hpd = extract64(tcr, 41, 1);
11458 } else {
11459 tsz = t1sz;
11460 epd = extract32(tcr, 23, 1);
11461 hpd = extract64(tcr, 42, 1);
11463 /* For aarch32, hpd0 is not enabled without t2e as well. */
11464 hpd &= extract32(tcr, 6, 1);
11467 return (ARMVAParameters) {
11468 .tsz = tsz,
11469 .select = select,
11470 .epd = epd,
11471 .hpd = hpd,
11476 * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
11478 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11479 * prot and page_size may not be filled in, and the populated fsr value provides
11480 * information on why the translation aborted, in the format of a long-format
11481 * DFSR/IFSR fault register, with the following caveats:
11482 * * the WnR bit is never set (the caller must do this).
11484 * @env: CPUARMState
11485 * @address: virtual address to get physical address for
11486 * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
11487 * @mmu_idx: MMU index indicating required translation regime
11488 * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table
11489 * walk), must be true if this is stage 2 of a stage 1+2 walk for an
11490 * EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored.
11491 * @phys_ptr: set to the physical address corresponding to the virtual address
11492 * @attrs: set to the memory transaction attributes to use
11493 * @prot: set to the permissions for the page containing phys_ptr
11494 * @page_size_ptr: set to the size of the page containing phys_ptr
11495 * @fi: set to fault info if the translation fails
11496 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
11498 static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
11499 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11500 bool s1_is_el0,
11501 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
11502 target_ulong *page_size_ptr,
11503 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
11505 ARMCPU *cpu = env_archcpu(env);
11506 CPUState *cs = CPU(cpu);
11507 /* Read an LPAE long-descriptor translation table. */
11508 ARMFaultType fault_type = ARMFault_Translation;
11509 uint32_t level;
11510 ARMVAParameters param;
11511 uint64_t ttbr;
11512 hwaddr descaddr, indexmask, indexmask_grainsize;
11513 uint32_t tableattrs;
11514 target_ulong page_size;
11515 uint32_t attrs;
11516 int32_t stride;
11517 int addrsize, inputsize, outputsize;
11518 TCR *tcr = regime_tcr(env, mmu_idx);
11519 int ap, ns, xn, pxn;
11520 uint32_t el = regime_el(env, mmu_idx);
11521 uint64_t descaddrmask;
11522 bool aarch64 = arm_el_is_aa64(env, el);
11523 bool guarded = false;
11525 /* TODO: This code does not support shareability levels. */
11526 if (aarch64) {
11527 int ps;
11529 param = aa64_va_parameters(env, address, mmu_idx,
11530 access_type != MMU_INST_FETCH);
11531 level = 0;
11534 * If TxSZ is programmed to a value larger than the maximum,
11535 * or smaller than the effective minimum, it is IMPLEMENTATION
11536 * DEFINED whether we behave as if the field were programmed
11537 * within bounds, or if a level 0 Translation fault is generated.
11539 * With FEAT_LVA, fault on less than minimum becomes required,
11540 * so our choice is to always raise the fault.
11542 if (param.tsz_oob) {
11543 fault_type = ARMFault_Translation;
11544 goto do_fault;
11547 addrsize = 64 - 8 * param.tbi;
11548 inputsize = 64 - param.tsz;
11551 * Bound PS by PARANGE to find the effective output address size.
11552 * ID_AA64MMFR0 is a read-only register so values outside of the
11553 * supported mappings can be considered an implementation error.
11555 ps = FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11556 ps = MIN(ps, param.ps);
11557 assert(ps < ARRAY_SIZE(pamax_map));
11558 outputsize = pamax_map[ps];
11559 } else {
11560 param = aa32_va_parameters(env, address, mmu_idx);
11561 level = 1;
11562 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
11563 inputsize = addrsize - param.tsz;
11564 outputsize = 40;
11568 * We determined the region when collecting the parameters, but we
11569 * have not yet validated that the address is valid for the region.
11570 * Extract the top bits and verify that they all match select.
11572 * For aa32, if inputsize == addrsize, then we have selected the
11573 * region by exclusion in aa32_va_parameters and there is no more
11574 * validation to do here.
11576 if (inputsize < addrsize) {
11577 target_ulong top_bits = sextract64(address, inputsize,
11578 addrsize - inputsize);
11579 if (-top_bits != param.select) {
11580 /* The gap between the two regions is a Translation fault */
11581 fault_type = ARMFault_Translation;
11582 goto do_fault;
11586 if (param.using64k) {
11587 stride = 13;
11588 } else if (param.using16k) {
11589 stride = 11;
11590 } else {
11591 stride = 9;
11594 /* Note that QEMU ignores shareability and cacheability attributes,
11595 * so we don't need to do anything with the SH, ORGN, IRGN fields
11596 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
11597 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
11598 * implement any ASID-like capability so we can ignore it (instead
11599 * we will always flush the TLB any time the ASID is changed).
11601 ttbr = regime_ttbr(env, mmu_idx, param.select);
11603 /* Here we should have set up all the parameters for the translation:
11604 * inputsize, ttbr, epd, stride, tbi
11607 if (param.epd) {
11608 /* Translation table walk disabled => Translation fault on TLB miss
11609 * Note: This is always 0 on 64-bit EL2 and EL3.
11611 goto do_fault;
11614 if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
11615 /* The starting level depends on the virtual address size (which can
11616 * be up to 48 bits) and the translation granule size. It indicates
11617 * the number of strides (stride bits at a time) needed to
11618 * consume the bits of the input address. In the pseudocode this is:
11619 * level = 4 - RoundUp((inputsize - grainsize) / stride)
11620 * where their 'inputsize' is our 'inputsize', 'grainsize' is
11621 * our 'stride + 3' and 'stride' is our 'stride'.
11622 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
11623 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
11624 * = 4 - (inputsize - 4) / stride;
11626 level = 4 - (inputsize - 4) / stride;
11627 } else {
11628 /* For stage 2 translations the starting level is specified by the
11629 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
11631 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
11632 uint32_t sl2 = extract64(tcr->raw_tcr, 33, 1);
11633 uint32_t startlevel;
11634 bool ok;
11636 /* SL2 is RES0 unless DS=1 & 4kb granule. */
11637 if (param.ds && stride == 9 && sl2) {
11638 if (sl0 != 0) {
11639 level = 0;
11640 fault_type = ARMFault_Translation;
11641 goto do_fault;
11643 startlevel = -1;
11644 } else if (!aarch64 || stride == 9) {
11645 /* AArch32 or 4KB pages */
11646 startlevel = 2 - sl0;
11648 if (cpu_isar_feature(aa64_st, cpu)) {
11649 startlevel &= 3;
11651 } else {
11652 /* 16KB or 64KB pages */
11653 startlevel = 3 - sl0;
11656 /* Check that the starting level is valid. */
11657 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
11658 inputsize, stride, outputsize);
11659 if (!ok) {
11660 fault_type = ARMFault_Translation;
11661 goto do_fault;
11663 level = startlevel;
11666 indexmask_grainsize = MAKE_64BIT_MASK(0, stride + 3);
11667 indexmask = MAKE_64BIT_MASK(0, inputsize - (stride * (4 - level)));
11669 /* Now we can extract the actual base address from the TTBR */
11670 descaddr = extract64(ttbr, 0, 48);
11673 * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [5:2] of TTBR.
11675 * Otherwise, if the base address is out of range, raise AddressSizeFault.
11676 * In the pseudocode, this is !IsZero(baseregister<47:outputsize>),
11677 * but we've just cleared the bits above 47, so simplify the test.
11679 if (outputsize > 48) {
11680 descaddr |= extract64(ttbr, 2, 4) << 48;
11681 } else if (descaddr >> outputsize) {
11682 level = 0;
11683 fault_type = ARMFault_AddressSize;
11684 goto do_fault;
11688 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
11689 * and also to mask out CnP (bit 0) which could validly be non-zero.
11691 descaddr &= ~indexmask;
11694 * For AArch32, the address field in the descriptor goes up to bit 39
11695 * for both v7 and v8. However, for v8 the SBZ bits [47:40] must be 0
11696 * or an AddressSize fault is raised. So for v8 we extract those SBZ
11697 * bits as part of the address, which will be checked via outputsize.
11698 * For AArch64, the address field goes up to bit 47, or 49 with FEAT_LPA2;
11699 * the highest bits of a 52-bit output are placed elsewhere.
11701 if (param.ds) {
11702 descaddrmask = MAKE_64BIT_MASK(0, 50);
11703 } else if (arm_feature(env, ARM_FEATURE_V8)) {
11704 descaddrmask = MAKE_64BIT_MASK(0, 48);
11705 } else {
11706 descaddrmask = MAKE_64BIT_MASK(0, 40);
11708 descaddrmask &= ~indexmask_grainsize;
11710 /* Secure accesses start with the page table in secure memory and
11711 * can be downgraded to non-secure at any step. Non-secure accesses
11712 * remain non-secure. We implement this by just ORing in the NSTable/NS
11713 * bits at each step.
11715 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
11716 for (;;) {
11717 uint64_t descriptor;
11718 bool nstable;
11720 descaddr |= (address >> (stride * (4 - level))) & indexmask;
11721 descaddr &= ~7ULL;
11722 nstable = extract32(tableattrs, 4, 1);
11723 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
11724 if (fi->type != ARMFault_None) {
11725 goto do_fault;
11728 if (!(descriptor & 1) ||
11729 (!(descriptor & 2) && (level == 3))) {
11730 /* Invalid, or the Reserved level 3 encoding */
11731 goto do_fault;
11734 descaddr = descriptor & descaddrmask;
11737 * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [15:12]
11738 * of descriptor. For FEAT_LPA2 and effective DS, bits [51:50] of
11739 * descaddr are in [9:8]. Otherwise, if descaddr is out of range,
11740 * raise AddressSizeFault.
11742 if (outputsize > 48) {
11743 if (param.ds) {
11744 descaddr |= extract64(descriptor, 8, 2) << 50;
11745 } else {
11746 descaddr |= extract64(descriptor, 12, 4) << 48;
11748 } else if (descaddr >> outputsize) {
11749 fault_type = ARMFault_AddressSize;
11750 goto do_fault;
11753 if ((descriptor & 2) && (level < 3)) {
11754 /* Table entry. The top five bits are attributes which may
11755 * propagate down through lower levels of the table (and
11756 * which are all arranged so that 0 means "no effect", so
11757 * we can gather them up by ORing in the bits at each level).
11759 tableattrs |= extract64(descriptor, 59, 5);
11760 level++;
11761 indexmask = indexmask_grainsize;
11762 continue;
11765 * Block entry at level 1 or 2, or page entry at level 3.
11766 * These are basically the same thing, although the number
11767 * of bits we pull in from the vaddr varies. Note that although
11768 * descaddrmask masks enough of the low bits of the descriptor
11769 * to give a correct page or table address, the address field
11770 * in a block descriptor is smaller; so we need to explicitly
11771 * clear the lower bits here before ORing in the low vaddr bits.
11773 page_size = (1ULL << ((stride * (4 - level)) + 3));
11774 descaddr &= ~(page_size - 1);
11775 descaddr |= (address & (page_size - 1));
11776 /* Extract attributes from the descriptor */
11777 attrs = extract64(descriptor, 2, 10)
11778 | (extract64(descriptor, 52, 12) << 10);
11780 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11781 /* Stage 2 table descriptors do not include any attribute fields */
11782 break;
11784 /* Merge in attributes from table descriptors */
11785 attrs |= nstable << 3; /* NS */
11786 guarded = extract64(descriptor, 50, 1); /* GP */
11787 if (param.hpd) {
11788 /* HPD disables all the table attributes except NSTable. */
11789 break;
11791 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
11792 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11793 * means "force PL1 access only", which means forcing AP[1] to 0.
11795 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
11796 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
11797 break;
11799 /* Here descaddr is the final physical address, and attributes
11800 * are all in attrs.
11802 fault_type = ARMFault_AccessFlag;
11803 if ((attrs & (1 << 8)) == 0) {
11804 /* Access flag */
11805 goto do_fault;
11808 ap = extract32(attrs, 4, 2);
11810 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11811 ns = mmu_idx == ARMMMUIdx_Stage2;
11812 xn = extract32(attrs, 11, 2);
11813 *prot = get_S2prot(env, ap, xn, s1_is_el0);
11814 } else {
11815 ns = extract32(attrs, 3, 1);
11816 xn = extract32(attrs, 12, 1);
11817 pxn = extract32(attrs, 11, 1);
11818 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
11821 fault_type = ARMFault_Permission;
11822 if (!(*prot & (1 << access_type))) {
11823 goto do_fault;
11826 if (ns) {
11827 /* The NS bit will (as required by the architecture) have no effect if
11828 * the CPU doesn't support TZ or this is a non-secure translation
11829 * regime, because the attribute will already be non-secure.
11831 txattrs->secure = false;
11833 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
11834 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
11835 arm_tlb_bti_gp(txattrs) = true;
11838 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11839 cacheattrs->is_s2_format = true;
11840 cacheattrs->attrs = extract32(attrs, 0, 4);
11841 } else {
11842 /* Index into MAIR registers for cache attributes */
11843 uint8_t attrindx = extract32(attrs, 0, 3);
11844 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11845 assert(attrindx <= 7);
11846 cacheattrs->is_s2_format = false;
11847 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
11851 * For FEAT_LPA2 and effective DS, the SH field in the attributes
11852 * was re-purposed for output address bits. The SH attribute in
11853 * that case comes from TCR_ELx, which we extracted earlier.
11855 if (param.ds) {
11856 cacheattrs->shareability = param.sh;
11857 } else {
11858 cacheattrs->shareability = extract32(attrs, 6, 2);
11861 *phys_ptr = descaddr;
11862 *page_size_ptr = page_size;
11863 return false;
11865 do_fault:
11866 fi->type = fault_type;
11867 fi->level = level;
11868 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
11869 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2 ||
11870 mmu_idx == ARMMMUIdx_Stage2_S);
11871 fi->s1ns = mmu_idx == ARMMMUIdx_Stage2;
11872 return true;
11875 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11876 ARMMMUIdx mmu_idx,
11877 int32_t address, int *prot)
11879 if (!arm_feature(env, ARM_FEATURE_M)) {
11880 *prot = PAGE_READ | PAGE_WRITE;
11881 switch (address) {
11882 case 0xF0000000 ... 0xFFFFFFFF:
11883 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11884 /* hivecs execing is ok */
11885 *prot |= PAGE_EXEC;
11887 break;
11888 case 0x00000000 ... 0x7FFFFFFF:
11889 *prot |= PAGE_EXEC;
11890 break;
11892 } else {
11893 /* Default system address map for M profile cores.
11894 * The architecture specifies which regions are execute-never;
11895 * at the MPU level no other checks are defined.
11897 switch (address) {
11898 case 0x00000000 ... 0x1fffffff: /* ROM */
11899 case 0x20000000 ... 0x3fffffff: /* SRAM */
11900 case 0x60000000 ... 0x7fffffff: /* RAM */
11901 case 0x80000000 ... 0x9fffffff: /* RAM */
11902 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11903 break;
11904 case 0x40000000 ... 0x5fffffff: /* Peripheral */
11905 case 0xa0000000 ... 0xbfffffff: /* Device */
11906 case 0xc0000000 ... 0xdfffffff: /* Device */
11907 case 0xe0000000 ... 0xffffffff: /* System */
11908 *prot = PAGE_READ | PAGE_WRITE;
11909 break;
11910 default:
11911 g_assert_not_reached();
11916 static bool pmsav7_use_background_region(ARMCPU *cpu,
11917 ARMMMUIdx mmu_idx, bool is_user)
11919 /* Return true if we should use the default memory map as a
11920 * "background" region if there are no hits against any MPU regions.
11922 CPUARMState *env = &cpu->env;
11924 if (is_user) {
11925 return false;
11928 if (arm_feature(env, ARM_FEATURE_M)) {
11929 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11930 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
11931 } else {
11932 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11936 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11938 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11939 return arm_feature(env, ARM_FEATURE_M) &&
11940 extract32(address, 20, 12) == 0xe00;
11943 static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11945 /* True if address is in the M profile system region
11946 * 0xe0000000 - 0xffffffff
11948 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11951 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
11952 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11953 hwaddr *phys_ptr, int *prot,
11954 target_ulong *page_size,
11955 ARMMMUFaultInfo *fi)
11957 ARMCPU *cpu = env_archcpu(env);
11958 int n;
11959 bool is_user = regime_is_user(env, mmu_idx);
11961 *phys_ptr = address;
11962 *page_size = TARGET_PAGE_SIZE;
11963 *prot = 0;
11965 if (regime_translation_disabled(env, mmu_idx) ||
11966 m_is_ppb_region(env, address)) {
11967 /* MPU disabled or M profile PPB access: use default memory map.
11968 * The other case which uses the default memory map in the
11969 * v7M ARM ARM pseudocode is exception vector reads from the vector
11970 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11971 * which always does a direct read using address_space_ldl(), rather
11972 * than going via this function, so we don't need to check that here.
11974 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11975 } else { /* MPU enabled */
11976 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11977 /* region search */
11978 uint32_t base = env->pmsav7.drbar[n];
11979 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11980 uint32_t rmask;
11981 bool srdis = false;
11983 if (!(env->pmsav7.drsr[n] & 0x1)) {
11984 continue;
11987 if (!rsize) {
11988 qemu_log_mask(LOG_GUEST_ERROR,
11989 "DRSR[%d]: Rsize field cannot be 0\n", n);
11990 continue;
11992 rsize++;
11993 rmask = (1ull << rsize) - 1;
11995 if (base & rmask) {
11996 qemu_log_mask(LOG_GUEST_ERROR,
11997 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11998 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11999 n, base, rmask);
12000 continue;
12003 if (address < base || address > base + rmask) {
12005 * Address not in this region. We must check whether the
12006 * region covers addresses in the same page as our address.
12007 * In that case we must not report a size that covers the
12008 * whole page for a subsequent hit against a different MPU
12009 * region or the background region, because it would result in
12010 * incorrect TLB hits for subsequent accesses to addresses that
12011 * are in this MPU region.
12013 if (ranges_overlap(base, rmask,
12014 address & TARGET_PAGE_MASK,
12015 TARGET_PAGE_SIZE)) {
12016 *page_size = 1;
12018 continue;
12021 /* Region matched */
12023 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
12024 int i, snd;
12025 uint32_t srdis_mask;
12027 rsize -= 3; /* sub region size (power of 2) */
12028 snd = ((address - base) >> rsize) & 0x7;
12029 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
12031 srdis_mask = srdis ? 0x3 : 0x0;
12032 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
12033 /* This will check in groups of 2, 4 and then 8, whether
12034 * the subregion bits are consistent. rsize is incremented
12035 * back up to give the region size, considering consistent
12036 * adjacent subregions as one region. Stop testing if rsize
12037 * is already big enough for an entire QEMU page.
12039 int snd_rounded = snd & ~(i - 1);
12040 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
12041 snd_rounded + 8, i);
12042 if (srdis_mask ^ srdis_multi) {
12043 break;
12045 srdis_mask = (srdis_mask << i) | srdis_mask;
12046 rsize++;
12049 if (srdis) {
12050 continue;
12052 if (rsize < TARGET_PAGE_BITS) {
12053 *page_size = 1 << rsize;
12055 break;
12058 if (n == -1) { /* no hits */
12059 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
12060 /* background fault */
12061 fi->type = ARMFault_Background;
12062 return true;
12064 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
12065 } else { /* a MPU hit! */
12066 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
12067 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
12069 if (m_is_system_region(env, address)) {
12070 /* System space is always execute never */
12071 xn = 1;
12074 if (is_user) { /* User mode AP bit decoding */
12075 switch (ap) {
12076 case 0:
12077 case 1:
12078 case 5:
12079 break; /* no access */
12080 case 3:
12081 *prot |= PAGE_WRITE;
12082 /* fall through */
12083 case 2:
12084 case 6:
12085 *prot |= PAGE_READ | PAGE_EXEC;
12086 break;
12087 case 7:
12088 /* for v7M, same as 6; for R profile a reserved value */
12089 if (arm_feature(env, ARM_FEATURE_M)) {
12090 *prot |= PAGE_READ | PAGE_EXEC;
12091 break;
12093 /* fall through */
12094 default:
12095 qemu_log_mask(LOG_GUEST_ERROR,
12096 "DRACR[%d]: Bad value for AP bits: 0x%"
12097 PRIx32 "\n", n, ap);
12099 } else { /* Priv. mode AP bits decoding */
12100 switch (ap) {
12101 case 0:
12102 break; /* no access */
12103 case 1:
12104 case 2:
12105 case 3:
12106 *prot |= PAGE_WRITE;
12107 /* fall through */
12108 case 5:
12109 case 6:
12110 *prot |= PAGE_READ | PAGE_EXEC;
12111 break;
12112 case 7:
12113 /* for v7M, same as 6; for R profile a reserved value */
12114 if (arm_feature(env, ARM_FEATURE_M)) {
12115 *prot |= PAGE_READ | PAGE_EXEC;
12116 break;
12118 /* fall through */
12119 default:
12120 qemu_log_mask(LOG_GUEST_ERROR,
12121 "DRACR[%d]: Bad value for AP bits: 0x%"
12122 PRIx32 "\n", n, ap);
12126 /* execute never */
12127 if (xn) {
12128 *prot &= ~PAGE_EXEC;
12133 fi->type = ARMFault_Permission;
12134 fi->level = 1;
12135 return !(*prot & (1 << access_type));
12138 static bool v8m_is_sau_exempt(CPUARMState *env,
12139 uint32_t address, MMUAccessType access_type)
12141 /* The architecture specifies that certain address ranges are
12142 * exempt from v8M SAU/IDAU checks.
12144 return
12145 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
12146 (address >= 0xe0000000 && address <= 0xe0002fff) ||
12147 (address >= 0xe000e000 && address <= 0xe000efff) ||
12148 (address >= 0xe002e000 && address <= 0xe002efff) ||
12149 (address >= 0xe0040000 && address <= 0xe0041fff) ||
12150 (address >= 0xe00ff000 && address <= 0xe00fffff);
12153 void v8m_security_lookup(CPUARMState *env, uint32_t address,
12154 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12155 V8M_SAttributes *sattrs)
12157 /* Look up the security attributes for this address. Compare the
12158 * pseudocode SecurityCheck() function.
12159 * We assume the caller has zero-initialized *sattrs.
12161 ARMCPU *cpu = env_archcpu(env);
12162 int r;
12163 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
12164 int idau_region = IREGION_NOTVALID;
12165 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12166 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
12168 if (cpu->idau) {
12169 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
12170 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
12172 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
12173 &idau_nsc);
12176 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
12177 /* 0xf0000000..0xffffffff is always S for insn fetches */
12178 return;
12181 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
12182 sattrs->ns = !regime_is_secure(env, mmu_idx);
12183 return;
12186 if (idau_region != IREGION_NOTVALID) {
12187 sattrs->irvalid = true;
12188 sattrs->iregion = idau_region;
12191 switch (env->sau.ctrl & 3) {
12192 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
12193 break;
12194 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
12195 sattrs->ns = true;
12196 break;
12197 default: /* SAU.ENABLE == 1 */
12198 for (r = 0; r < cpu->sau_sregion; r++) {
12199 if (env->sau.rlar[r] & 1) {
12200 uint32_t base = env->sau.rbar[r] & ~0x1f;
12201 uint32_t limit = env->sau.rlar[r] | 0x1f;
12203 if (base <= address && limit >= address) {
12204 if (base > addr_page_base || limit < addr_page_limit) {
12205 sattrs->subpage = true;
12207 if (sattrs->srvalid) {
12208 /* If we hit in more than one region then we must report
12209 * as Secure, not NS-Callable, with no valid region
12210 * number info.
12212 sattrs->ns = false;
12213 sattrs->nsc = false;
12214 sattrs->sregion = 0;
12215 sattrs->srvalid = false;
12216 break;
12217 } else {
12218 if (env->sau.rlar[r] & 2) {
12219 sattrs->nsc = true;
12220 } else {
12221 sattrs->ns = true;
12223 sattrs->srvalid = true;
12224 sattrs->sregion = r;
12226 } else {
12228 * Address not in this region. We must check whether the
12229 * region covers addresses in the same page as our address.
12230 * In that case we must not report a size that covers the
12231 * whole page for a subsequent hit against a different MPU
12232 * region or the background region, because it would result
12233 * in incorrect TLB hits for subsequent accesses to
12234 * addresses that are in this MPU region.
12236 if (limit >= base &&
12237 ranges_overlap(base, limit - base + 1,
12238 addr_page_base,
12239 TARGET_PAGE_SIZE)) {
12240 sattrs->subpage = true;
12245 break;
12249 * The IDAU will override the SAU lookup results if it specifies
12250 * higher security than the SAU does.
12252 if (!idau_ns) {
12253 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
12254 sattrs->ns = false;
12255 sattrs->nsc = idau_nsc;
12260 bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
12261 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12262 hwaddr *phys_ptr, MemTxAttrs *txattrs,
12263 int *prot, bool *is_subpage,
12264 ARMMMUFaultInfo *fi, uint32_t *mregion)
12266 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
12267 * that a full phys-to-virt translation does).
12268 * mregion is (if not NULL) set to the region number which matched,
12269 * or -1 if no region number is returned (MPU off, address did not
12270 * hit a region, address hit in multiple regions).
12271 * We set is_subpage to true if the region hit doesn't cover the
12272 * entire TARGET_PAGE the address is within.
12274 ARMCPU *cpu = env_archcpu(env);
12275 bool is_user = regime_is_user(env, mmu_idx);
12276 uint32_t secure = regime_is_secure(env, mmu_idx);
12277 int n;
12278 int matchregion = -1;
12279 bool hit = false;
12280 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12281 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
12283 *is_subpage = false;
12284 *phys_ptr = address;
12285 *prot = 0;
12286 if (mregion) {
12287 *mregion = -1;
12290 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
12291 * was an exception vector read from the vector table (which is always
12292 * done using the default system address map), because those accesses
12293 * are done in arm_v7m_load_vector(), which always does a direct
12294 * read using address_space_ldl(), rather than going via this function.
12296 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
12297 hit = true;
12298 } else if (m_is_ppb_region(env, address)) {
12299 hit = true;
12300 } else {
12301 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
12302 hit = true;
12305 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
12306 /* region search */
12307 /* Note that the base address is bits [31:5] from the register
12308 * with bits [4:0] all zeroes, but the limit address is bits
12309 * [31:5] from the register with bits [4:0] all ones.
12311 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
12312 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
12314 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
12315 /* Region disabled */
12316 continue;
12319 if (address < base || address > limit) {
12321 * Address not in this region. We must check whether the
12322 * region covers addresses in the same page as our address.
12323 * In that case we must not report a size that covers the
12324 * whole page for a subsequent hit against a different MPU
12325 * region or the background region, because it would result in
12326 * incorrect TLB hits for subsequent accesses to addresses that
12327 * are in this MPU region.
12329 if (limit >= base &&
12330 ranges_overlap(base, limit - base + 1,
12331 addr_page_base,
12332 TARGET_PAGE_SIZE)) {
12333 *is_subpage = true;
12335 continue;
12338 if (base > addr_page_base || limit < addr_page_limit) {
12339 *is_subpage = true;
12342 if (matchregion != -1) {
12343 /* Multiple regions match -- always a failure (unlike
12344 * PMSAv7 where highest-numbered-region wins)
12346 fi->type = ARMFault_Permission;
12347 fi->level = 1;
12348 return true;
12351 matchregion = n;
12352 hit = true;
12356 if (!hit) {
12357 /* background fault */
12358 fi->type = ARMFault_Background;
12359 return true;
12362 if (matchregion == -1) {
12363 /* hit using the background region */
12364 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
12365 } else {
12366 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
12367 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
12368 bool pxn = false;
12370 if (arm_feature(env, ARM_FEATURE_V8_1M)) {
12371 pxn = extract32(env->pmsav8.rlar[secure][matchregion], 4, 1);
12374 if (m_is_system_region(env, address)) {
12375 /* System space is always execute never */
12376 xn = 1;
12379 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
12380 if (*prot && !xn && !(pxn && !is_user)) {
12381 *prot |= PAGE_EXEC;
12383 /* We don't need to look the attribute up in the MAIR0/MAIR1
12384 * registers because that only tells us about cacheability.
12386 if (mregion) {
12387 *mregion = matchregion;
12391 fi->type = ARMFault_Permission;
12392 fi->level = 1;
12393 return !(*prot & (1 << access_type));
12397 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
12398 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12399 hwaddr *phys_ptr, MemTxAttrs *txattrs,
12400 int *prot, target_ulong *page_size,
12401 ARMMMUFaultInfo *fi)
12403 uint32_t secure = regime_is_secure(env, mmu_idx);
12404 V8M_SAttributes sattrs = {};
12405 bool ret;
12406 bool mpu_is_subpage;
12408 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
12409 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
12410 if (access_type == MMU_INST_FETCH) {
12411 /* Instruction fetches always use the MMU bank and the
12412 * transaction attribute determined by the fetch address,
12413 * regardless of CPU state. This is painful for QEMU
12414 * to handle, because it would mean we need to encode
12415 * into the mmu_idx not just the (user, negpri) information
12416 * for the current security state but also that for the
12417 * other security state, which would balloon the number
12418 * of mmu_idx values needed alarmingly.
12419 * Fortunately we can avoid this because it's not actually
12420 * possible to arbitrarily execute code from memory with
12421 * the wrong security attribute: it will always generate
12422 * an exception of some kind or another, apart from the
12423 * special case of an NS CPU executing an SG instruction
12424 * in S&NSC memory. So we always just fail the translation
12425 * here and sort things out in the exception handler
12426 * (including possibly emulating an SG instruction).
12428 if (sattrs.ns != !secure) {
12429 if (sattrs.nsc) {
12430 fi->type = ARMFault_QEMU_NSCExec;
12431 } else {
12432 fi->type = ARMFault_QEMU_SFault;
12434 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
12435 *phys_ptr = address;
12436 *prot = 0;
12437 return true;
12439 } else {
12440 /* For data accesses we always use the MMU bank indicated
12441 * by the current CPU state, but the security attributes
12442 * might downgrade a secure access to nonsecure.
12444 if (sattrs.ns) {
12445 txattrs->secure = false;
12446 } else if (!secure) {
12447 /* NS access to S memory must fault.
12448 * Architecturally we should first check whether the
12449 * MPU information for this address indicates that we
12450 * are doing an unaligned access to Device memory, which
12451 * should generate a UsageFault instead. QEMU does not
12452 * currently check for that kind of unaligned access though.
12453 * If we added it we would need to do so as a special case
12454 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
12456 fi->type = ARMFault_QEMU_SFault;
12457 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
12458 *phys_ptr = address;
12459 *prot = 0;
12460 return true;
12465 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
12466 txattrs, prot, &mpu_is_subpage, fi, NULL);
12467 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
12468 return ret;
12471 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
12472 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12473 hwaddr *phys_ptr, int *prot,
12474 ARMMMUFaultInfo *fi)
12476 int n;
12477 uint32_t mask;
12478 uint32_t base;
12479 bool is_user = regime_is_user(env, mmu_idx);
12481 if (regime_translation_disabled(env, mmu_idx)) {
12482 /* MPU disabled. */
12483 *phys_ptr = address;
12484 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12485 return false;
12488 *phys_ptr = address;
12489 for (n = 7; n >= 0; n--) {
12490 base = env->cp15.c6_region[n];
12491 if ((base & 1) == 0) {
12492 continue;
12494 mask = 1 << ((base >> 1) & 0x1f);
12495 /* Keep this shift separate from the above to avoid an
12496 (undefined) << 32. */
12497 mask = (mask << 1) - 1;
12498 if (((base ^ address) & ~mask) == 0) {
12499 break;
12502 if (n < 0) {
12503 fi->type = ARMFault_Background;
12504 return true;
12507 if (access_type == MMU_INST_FETCH) {
12508 mask = env->cp15.pmsav5_insn_ap;
12509 } else {
12510 mask = env->cp15.pmsav5_data_ap;
12512 mask = (mask >> (n * 4)) & 0xf;
12513 switch (mask) {
12514 case 0:
12515 fi->type = ARMFault_Permission;
12516 fi->level = 1;
12517 return true;
12518 case 1:
12519 if (is_user) {
12520 fi->type = ARMFault_Permission;
12521 fi->level = 1;
12522 return true;
12524 *prot = PAGE_READ | PAGE_WRITE;
12525 break;
12526 case 2:
12527 *prot = PAGE_READ;
12528 if (!is_user) {
12529 *prot |= PAGE_WRITE;
12531 break;
12532 case 3:
12533 *prot = PAGE_READ | PAGE_WRITE;
12534 break;
12535 case 5:
12536 if (is_user) {
12537 fi->type = ARMFault_Permission;
12538 fi->level = 1;
12539 return true;
12541 *prot = PAGE_READ;
12542 break;
12543 case 6:
12544 *prot = PAGE_READ;
12545 break;
12546 default:
12547 /* Bad permission. */
12548 fi->type = ARMFault_Permission;
12549 fi->level = 1;
12550 return true;
12552 *prot |= PAGE_EXEC;
12553 return false;
12556 /* Combine either inner or outer cacheability attributes for normal
12557 * memory, according to table D4-42 and pseudocode procedure
12558 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
12560 * NB: only stage 1 includes allocation hints (RW bits), leading to
12561 * some asymmetry.
12563 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
12565 if (s1 == 4 || s2 == 4) {
12566 /* non-cacheable has precedence */
12567 return 4;
12568 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
12569 /* stage 1 write-through takes precedence */
12570 return s1;
12571 } else if (extract32(s2, 2, 2) == 2) {
12572 /* stage 2 write-through takes precedence, but the allocation hint
12573 * is still taken from stage 1
12575 return (2 << 2) | extract32(s1, 0, 2);
12576 } else { /* write-back */
12577 return s1;
12581 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
12582 * and CombineS1S2Desc()
12584 * @env: CPUARMState
12585 * @s1: Attributes from stage 1 walk
12586 * @s2: Attributes from stage 2 walk
12588 static ARMCacheAttrs combine_cacheattrs(CPUARMState *env,
12589 ARMCacheAttrs s1, ARMCacheAttrs s2)
12591 uint8_t s1lo, s2lo, s1hi, s2hi;
12592 ARMCacheAttrs ret;
12593 bool tagged = false;
12594 uint8_t s2_mair_attrs;
12596 assert(s2.is_s2_format && !s1.is_s2_format);
12597 ret.is_s2_format = false;
12599 s2_mair_attrs = convert_stage2_attrs(env, s2.attrs);
12601 if (s1.attrs == 0xf0) {
12602 tagged = true;
12603 s1.attrs = 0xff;
12606 s1lo = extract32(s1.attrs, 0, 4);
12607 s2lo = extract32(s2_mair_attrs, 0, 4);
12608 s1hi = extract32(s1.attrs, 4, 4);
12609 s2hi = extract32(s2_mair_attrs, 4, 4);
12611 /* Combine shareability attributes (table D4-43) */
12612 if (s1.shareability == 2 || s2.shareability == 2) {
12613 /* if either are outer-shareable, the result is outer-shareable */
12614 ret.shareability = 2;
12615 } else if (s1.shareability == 3 || s2.shareability == 3) {
12616 /* if either are inner-shareable, the result is inner-shareable */
12617 ret.shareability = 3;
12618 } else {
12619 /* both non-shareable */
12620 ret.shareability = 0;
12623 /* Combine memory type and cacheability attributes */
12624 if (s1hi == 0 || s2hi == 0) {
12625 /* Device has precedence over normal */
12626 if (s1lo == 0 || s2lo == 0) {
12627 /* nGnRnE has precedence over anything */
12628 ret.attrs = 0;
12629 } else if (s1lo == 4 || s2lo == 4) {
12630 /* non-Reordering has precedence over Reordering */
12631 ret.attrs = 4; /* nGnRE */
12632 } else if (s1lo == 8 || s2lo == 8) {
12633 /* non-Gathering has precedence over Gathering */
12634 ret.attrs = 8; /* nGRE */
12635 } else {
12636 ret.attrs = 0xc; /* GRE */
12639 /* Any location for which the resultant memory type is any
12640 * type of Device memory is always treated as Outer Shareable.
12642 ret.shareability = 2;
12643 } else { /* Normal memory */
12644 /* Outer/inner cacheability combine independently */
12645 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
12646 | combine_cacheattr_nibble(s1lo, s2lo);
12648 if (ret.attrs == 0x44) {
12649 /* Any location for which the resultant memory type is Normal
12650 * Inner Non-cacheable, Outer Non-cacheable is always treated
12651 * as Outer Shareable.
12653 ret.shareability = 2;
12657 /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
12658 if (tagged && ret.attrs == 0xff) {
12659 ret.attrs = 0xf0;
12662 return ret;
12666 /* get_phys_addr - get the physical address for this virtual address
12668 * Find the physical address corresponding to the given virtual address,
12669 * by doing a translation table walk on MMU based systems or using the
12670 * MPU state on MPU based systems.
12672 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
12673 * prot and page_size may not be filled in, and the populated fsr value provides
12674 * information on why the translation aborted, in the format of a
12675 * DFSR/IFSR fault register, with the following caveats:
12676 * * we honour the short vs long DFSR format differences.
12677 * * the WnR bit is never set (the caller must do this).
12678 * * for PSMAv5 based systems we don't bother to return a full FSR format
12679 * value.
12681 * @env: CPUARMState
12682 * @address: virtual address to get physical address for
12683 * @access_type: 0 for read, 1 for write, 2 for execute
12684 * @mmu_idx: MMU index indicating required translation regime
12685 * @phys_ptr: set to the physical address corresponding to the virtual address
12686 * @attrs: set to the memory transaction attributes to use
12687 * @prot: set to the permissions for the page containing phys_ptr
12688 * @page_size: set to the size of the page containing phys_ptr
12689 * @fi: set to fault info if the translation fails
12690 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
12692 bool get_phys_addr(CPUARMState *env, target_ulong address,
12693 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12694 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
12695 target_ulong *page_size,
12696 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
12698 ARMMMUIdx s1_mmu_idx = stage_1_mmu_idx(mmu_idx);
12700 if (mmu_idx != s1_mmu_idx) {
12701 /* Call ourselves recursively to do the stage 1 and then stage 2
12702 * translations if mmu_idx is a two-stage regime.
12704 if (arm_feature(env, ARM_FEATURE_EL2)) {
12705 hwaddr ipa;
12706 int s2_prot;
12707 int ret;
12708 bool ipa_secure;
12709 ARMCacheAttrs cacheattrs2 = {};
12710 ARMMMUIdx s2_mmu_idx;
12711 bool is_el0;
12713 ret = get_phys_addr(env, address, access_type, s1_mmu_idx, &ipa,
12714 attrs, prot, page_size, fi, cacheattrs);
12716 /* If S1 fails or S2 is disabled, return early. */
12717 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
12718 *phys_ptr = ipa;
12719 return ret;
12722 ipa_secure = attrs->secure;
12723 if (arm_is_secure_below_el3(env)) {
12724 if (ipa_secure) {
12725 attrs->secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
12726 } else {
12727 attrs->secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
12729 } else {
12730 assert(!ipa_secure);
12733 s2_mmu_idx = attrs->secure ? ARMMMUIdx_Stage2_S : ARMMMUIdx_Stage2;
12734 is_el0 = mmu_idx == ARMMMUIdx_E10_0 || mmu_idx == ARMMMUIdx_SE10_0;
12736 /* S1 is done. Now do S2 translation. */
12737 ret = get_phys_addr_lpae(env, ipa, access_type, s2_mmu_idx, is_el0,
12738 phys_ptr, attrs, &s2_prot,
12739 page_size, fi, &cacheattrs2);
12740 fi->s2addr = ipa;
12741 /* Combine the S1 and S2 perms. */
12742 *prot &= s2_prot;
12744 /* If S2 fails, return early. */
12745 if (ret) {
12746 return ret;
12749 /* Combine the S1 and S2 cache attributes. */
12750 if (arm_hcr_el2_eff(env) & HCR_DC) {
12752 * HCR.DC forces the first stage attributes to
12753 * Normal Non-Shareable,
12754 * Inner Write-Back Read-Allocate Write-Allocate,
12755 * Outer Write-Back Read-Allocate Write-Allocate.
12756 * Do not overwrite Tagged within attrs.
12758 if (cacheattrs->attrs != 0xf0) {
12759 cacheattrs->attrs = 0xff;
12761 cacheattrs->shareability = 0;
12763 *cacheattrs = combine_cacheattrs(env, *cacheattrs, cacheattrs2);
12765 /* Check if IPA translates to secure or non-secure PA space. */
12766 if (arm_is_secure_below_el3(env)) {
12767 if (ipa_secure) {
12768 attrs->secure =
12769 !(env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW));
12770 } else {
12771 attrs->secure =
12772 !((env->cp15.vtcr_el2.raw_tcr & (VTCR_NSA | VTCR_NSW))
12773 || (env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW)));
12776 return 0;
12777 } else {
12779 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
12781 mmu_idx = stage_1_mmu_idx(mmu_idx);
12785 /* The page table entries may downgrade secure to non-secure, but
12786 * cannot upgrade an non-secure translation regime's attributes
12787 * to secure.
12789 attrs->secure = regime_is_secure(env, mmu_idx);
12790 attrs->user = regime_is_user(env, mmu_idx);
12792 /* Fast Context Switch Extension. This doesn't exist at all in v8.
12793 * In v7 and earlier it affects all stage 1 translations.
12795 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
12796 && !arm_feature(env, ARM_FEATURE_V8)) {
12797 if (regime_el(env, mmu_idx) == 3) {
12798 address += env->cp15.fcseidr_s;
12799 } else {
12800 address += env->cp15.fcseidr_ns;
12804 if (arm_feature(env, ARM_FEATURE_PMSA)) {
12805 bool ret;
12806 *page_size = TARGET_PAGE_SIZE;
12808 if (arm_feature(env, ARM_FEATURE_V8)) {
12809 /* PMSAv8 */
12810 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
12811 phys_ptr, attrs, prot, page_size, fi);
12812 } else if (arm_feature(env, ARM_FEATURE_V7)) {
12813 /* PMSAv7 */
12814 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
12815 phys_ptr, prot, page_size, fi);
12816 } else {
12817 /* Pre-v7 MPU */
12818 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
12819 phys_ptr, prot, fi);
12821 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
12822 " mmu_idx %u -> %s (prot %c%c%c)\n",
12823 access_type == MMU_DATA_LOAD ? "reading" :
12824 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
12825 (uint32_t)address, mmu_idx,
12826 ret ? "Miss" : "Hit",
12827 *prot & PAGE_READ ? 'r' : '-',
12828 *prot & PAGE_WRITE ? 'w' : '-',
12829 *prot & PAGE_EXEC ? 'x' : '-');
12831 return ret;
12834 /* Definitely a real MMU, not an MPU */
12836 if (regime_translation_disabled(env, mmu_idx)) {
12837 uint64_t hcr;
12838 uint8_t memattr;
12841 * MMU disabled. S1 addresses within aa64 translation regimes are
12842 * still checked for bounds -- see AArch64.TranslateAddressS1Off.
12844 if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
12845 int r_el = regime_el(env, mmu_idx);
12846 if (arm_el_is_aa64(env, r_el)) {
12847 int pamax = arm_pamax(env_archcpu(env));
12848 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
12849 int addrtop, tbi;
12851 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12852 if (access_type == MMU_INST_FETCH) {
12853 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12855 tbi = (tbi >> extract64(address, 55, 1)) & 1;
12856 addrtop = (tbi ? 55 : 63);
12858 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
12859 fi->type = ARMFault_AddressSize;
12860 fi->level = 0;
12861 fi->stage2 = false;
12862 return 1;
12866 * When TBI is disabled, we've just validated that all of the
12867 * bits above PAMax are zero, so logically we only need to
12868 * clear the top byte for TBI. But it's clearer to follow
12869 * the pseudocode set of addrdesc.paddress.
12871 address = extract64(address, 0, 52);
12874 *phys_ptr = address;
12875 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12876 *page_size = TARGET_PAGE_SIZE;
12878 /* Fill in cacheattr a-la AArch64.TranslateAddressS1Off. */
12879 hcr = arm_hcr_el2_eff(env);
12880 cacheattrs->shareability = 0;
12881 cacheattrs->is_s2_format = false;
12882 if (hcr & HCR_DC) {
12883 if (hcr & HCR_DCT) {
12884 memattr = 0xf0; /* Tagged, Normal, WB, RWA */
12885 } else {
12886 memattr = 0xff; /* Normal, WB, RWA */
12888 } else if (access_type == MMU_INST_FETCH) {
12889 if (regime_sctlr(env, mmu_idx) & SCTLR_I) {
12890 memattr = 0xee; /* Normal, WT, RA, NT */
12891 } else {
12892 memattr = 0x44; /* Normal, NC, No */
12894 cacheattrs->shareability = 2; /* outer sharable */
12895 } else {
12896 memattr = 0x00; /* Device, nGnRnE */
12898 cacheattrs->attrs = memattr;
12899 return 0;
12902 if (regime_using_lpae_format(env, mmu_idx)) {
12903 return get_phys_addr_lpae(env, address, access_type, mmu_idx, false,
12904 phys_ptr, attrs, prot, page_size,
12905 fi, cacheattrs);
12906 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
12907 return get_phys_addr_v6(env, address, access_type, mmu_idx,
12908 phys_ptr, attrs, prot, page_size, fi);
12909 } else {
12910 return get_phys_addr_v5(env, address, access_type, mmu_idx,
12911 phys_ptr, prot, page_size, fi);
12915 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12916 MemTxAttrs *attrs)
12918 ARMCPU *cpu = ARM_CPU(cs);
12919 CPUARMState *env = &cpu->env;
12920 hwaddr phys_addr;
12921 target_ulong page_size;
12922 int prot;
12923 bool ret;
12924 ARMMMUFaultInfo fi = {};
12925 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
12926 ARMCacheAttrs cacheattrs = {};
12928 *attrs = (MemTxAttrs) {};
12930 ret = get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &phys_addr,
12931 attrs, &prot, &page_size, &fi, &cacheattrs);
12933 if (ret) {
12934 return -1;
12936 return phys_addr;
12939 #endif
12941 /* Note that signed overflow is undefined in C. The following routines are
12942 careful to use unsigned types where modulo arithmetic is required.
12943 Failure to do so _will_ break on newer gcc. */
12945 /* Signed saturating arithmetic. */
12947 /* Perform 16-bit signed saturating addition. */
12948 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12950 uint16_t res;
12952 res = a + b;
12953 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12954 if (a & 0x8000)
12955 res = 0x8000;
12956 else
12957 res = 0x7fff;
12959 return res;
12962 /* Perform 8-bit signed saturating addition. */
12963 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12965 uint8_t res;
12967 res = a + b;
12968 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12969 if (a & 0x80)
12970 res = 0x80;
12971 else
12972 res = 0x7f;
12974 return res;
12977 /* Perform 16-bit signed saturating subtraction. */
12978 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12980 uint16_t res;
12982 res = a - b;
12983 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12984 if (a & 0x8000)
12985 res = 0x8000;
12986 else
12987 res = 0x7fff;
12989 return res;
12992 /* Perform 8-bit signed saturating subtraction. */
12993 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12995 uint8_t res;
12997 res = a - b;
12998 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12999 if (a & 0x80)
13000 res = 0x80;
13001 else
13002 res = 0x7f;
13004 return res;
13007 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
13008 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
13009 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
13010 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
13011 #define PFX q
13013 #include "op_addsub.h"
13015 /* Unsigned saturating arithmetic. */
13016 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
13018 uint16_t res;
13019 res = a + b;
13020 if (res < a)
13021 res = 0xffff;
13022 return res;
13025 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
13027 if (a > b)
13028 return a - b;
13029 else
13030 return 0;
13033 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
13035 uint8_t res;
13036 res = a + b;
13037 if (res < a)
13038 res = 0xff;
13039 return res;
13042 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
13044 if (a > b)
13045 return a - b;
13046 else
13047 return 0;
13050 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
13051 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
13052 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
13053 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
13054 #define PFX uq
13056 #include "op_addsub.h"
13058 /* Signed modulo arithmetic. */
13059 #define SARITH16(a, b, n, op) do { \
13060 int32_t sum; \
13061 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
13062 RESULT(sum, n, 16); \
13063 if (sum >= 0) \
13064 ge |= 3 << (n * 2); \
13065 } while(0)
13067 #define SARITH8(a, b, n, op) do { \
13068 int32_t sum; \
13069 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
13070 RESULT(sum, n, 8); \
13071 if (sum >= 0) \
13072 ge |= 1 << n; \
13073 } while(0)
13076 #define ADD16(a, b, n) SARITH16(a, b, n, +)
13077 #define SUB16(a, b, n) SARITH16(a, b, n, -)
13078 #define ADD8(a, b, n) SARITH8(a, b, n, +)
13079 #define SUB8(a, b, n) SARITH8(a, b, n, -)
13080 #define PFX s
13081 #define ARITH_GE
13083 #include "op_addsub.h"
13085 /* Unsigned modulo arithmetic. */
13086 #define ADD16(a, b, n) do { \
13087 uint32_t sum; \
13088 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
13089 RESULT(sum, n, 16); \
13090 if ((sum >> 16) == 1) \
13091 ge |= 3 << (n * 2); \
13092 } while(0)
13094 #define ADD8(a, b, n) do { \
13095 uint32_t sum; \
13096 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
13097 RESULT(sum, n, 8); \
13098 if ((sum >> 8) == 1) \
13099 ge |= 1 << n; \
13100 } while(0)
13102 #define SUB16(a, b, n) do { \
13103 uint32_t sum; \
13104 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
13105 RESULT(sum, n, 16); \
13106 if ((sum >> 16) == 0) \
13107 ge |= 3 << (n * 2); \
13108 } while(0)
13110 #define SUB8(a, b, n) do { \
13111 uint32_t sum; \
13112 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
13113 RESULT(sum, n, 8); \
13114 if ((sum >> 8) == 0) \
13115 ge |= 1 << n; \
13116 } while(0)
13118 #define PFX u
13119 #define ARITH_GE
13121 #include "op_addsub.h"
13123 /* Halved signed arithmetic. */
13124 #define ADD16(a, b, n) \
13125 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
13126 #define SUB16(a, b, n) \
13127 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
13128 #define ADD8(a, b, n) \
13129 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
13130 #define SUB8(a, b, n) \
13131 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
13132 #define PFX sh
13134 #include "op_addsub.h"
13136 /* Halved unsigned arithmetic. */
13137 #define ADD16(a, b, n) \
13138 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
13139 #define SUB16(a, b, n) \
13140 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
13141 #define ADD8(a, b, n) \
13142 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
13143 #define SUB8(a, b, n) \
13144 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
13145 #define PFX uh
13147 #include "op_addsub.h"
13149 static inline uint8_t do_usad(uint8_t a, uint8_t b)
13151 if (a > b)
13152 return a - b;
13153 else
13154 return b - a;
13157 /* Unsigned sum of absolute byte differences. */
13158 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
13160 uint32_t sum;
13161 sum = do_usad(a, b);
13162 sum += do_usad(a >> 8, b >> 8);
13163 sum += do_usad(a >> 16, b >> 16);
13164 sum += do_usad(a >> 24, b >> 24);
13165 return sum;
13168 /* For ARMv6 SEL instruction. */
13169 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
13171 uint32_t mask;
13173 mask = 0;
13174 if (flags & 1)
13175 mask |= 0xff;
13176 if (flags & 2)
13177 mask |= 0xff00;
13178 if (flags & 4)
13179 mask |= 0xff0000;
13180 if (flags & 8)
13181 mask |= 0xff000000;
13182 return (a & mask) | (b & ~mask);
13185 /* CRC helpers.
13186 * The upper bytes of val (above the number specified by 'bytes') must have
13187 * been zeroed out by the caller.
13189 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
13191 uint8_t buf[4];
13193 stl_le_p(buf, val);
13195 /* zlib crc32 converts the accumulator and output to one's complement. */
13196 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
13199 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
13201 uint8_t buf[4];
13203 stl_le_p(buf, val);
13205 /* Linux crc32c converts the output to one's complement. */
13206 return crc32c(acc, buf, bytes) ^ 0xffffffff;
13209 /* Return the exception level to which FP-disabled exceptions should
13210 * be taken, or 0 if FP is enabled.
13212 int fp_exception_el(CPUARMState *env, int cur_el)
13214 #ifndef CONFIG_USER_ONLY
13215 uint64_t hcr_el2;
13217 /* CPACR and the CPTR registers don't exist before v6, so FP is
13218 * always accessible
13220 if (!arm_feature(env, ARM_FEATURE_V6)) {
13221 return 0;
13224 if (arm_feature(env, ARM_FEATURE_M)) {
13225 /* CPACR can cause a NOCP UsageFault taken to current security state */
13226 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
13227 return 1;
13230 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
13231 if (!extract32(env->v7m.nsacr, 10, 1)) {
13232 /* FP insns cause a NOCP UsageFault taken to Secure */
13233 return 3;
13237 return 0;
13240 hcr_el2 = arm_hcr_el2_eff(env);
13242 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
13243 * 0, 2 : trap EL0 and EL1/PL1 accesses
13244 * 1 : trap only EL0 accesses
13245 * 3 : trap no accesses
13246 * This register is ignored if E2H+TGE are both set.
13248 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
13249 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
13251 switch (fpen) {
13252 case 0:
13253 case 2:
13254 if (cur_el == 0 || cur_el == 1) {
13255 /* Trap to PL1, which might be EL1 or EL3 */
13256 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
13257 return 3;
13259 return 1;
13261 if (cur_el == 3 && !is_a64(env)) {
13262 /* Secure PL1 running at EL3 */
13263 return 3;
13265 break;
13266 case 1:
13267 if (cur_el == 0) {
13268 return 1;
13270 break;
13271 case 3:
13272 break;
13277 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
13278 * to control non-secure access to the FPU. It doesn't have any
13279 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
13281 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
13282 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
13283 if (!extract32(env->cp15.nsacr, 10, 1)) {
13284 /* FP insns act as UNDEF */
13285 return cur_el == 2 ? 2 : 1;
13290 * CPTR_EL2 is present in v7VE or v8, and changes format
13291 * with HCR_EL2.E2H (regardless of TGE).
13293 if (cur_el <= 2) {
13294 if (hcr_el2 & HCR_E2H) {
13295 /* Check CPTR_EL2.FPEN. */
13296 switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
13297 case 1:
13298 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
13299 break;
13301 /* fall through */
13302 case 0:
13303 case 2:
13304 return 2;
13306 } else if (arm_is_el2_enabled(env)) {
13307 if (env->cp15.cptr_el[2] & CPTR_TFP) {
13308 return 2;
13313 /* CPTR_EL3 : present in v8 */
13314 if (env->cp15.cptr_el[3] & CPTR_TFP) {
13315 /* Trap all FP ops to EL3 */
13316 return 3;
13318 #endif
13319 return 0;
13322 /* Return the exception level we're running at if this is our mmu_idx */
13323 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
13325 if (mmu_idx & ARM_MMU_IDX_M) {
13326 return mmu_idx & ARM_MMU_IDX_M_PRIV;
13329 switch (mmu_idx) {
13330 case ARMMMUIdx_E10_0:
13331 case ARMMMUIdx_E20_0:
13332 case ARMMMUIdx_SE10_0:
13333 case ARMMMUIdx_SE20_0:
13334 return 0;
13335 case ARMMMUIdx_E10_1:
13336 case ARMMMUIdx_E10_1_PAN:
13337 case ARMMMUIdx_SE10_1:
13338 case ARMMMUIdx_SE10_1_PAN:
13339 return 1;
13340 case ARMMMUIdx_E2:
13341 case ARMMMUIdx_E20_2:
13342 case ARMMMUIdx_E20_2_PAN:
13343 case ARMMMUIdx_SE2:
13344 case ARMMMUIdx_SE20_2:
13345 case ARMMMUIdx_SE20_2_PAN:
13346 return 2;
13347 case ARMMMUIdx_SE3:
13348 return 3;
13349 default:
13350 g_assert_not_reached();
13354 #ifndef CONFIG_TCG
13355 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
13357 g_assert_not_reached();
13359 #endif
13361 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
13363 ARMMMUIdx idx;
13364 uint64_t hcr;
13366 if (arm_feature(env, ARM_FEATURE_M)) {
13367 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
13370 /* See ARM pseudo-function ELIsInHost. */
13371 switch (el) {
13372 case 0:
13373 hcr = arm_hcr_el2_eff(env);
13374 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
13375 idx = ARMMMUIdx_E20_0;
13376 } else {
13377 idx = ARMMMUIdx_E10_0;
13379 break;
13380 case 1:
13381 if (env->pstate & PSTATE_PAN) {
13382 idx = ARMMMUIdx_E10_1_PAN;
13383 } else {
13384 idx = ARMMMUIdx_E10_1;
13386 break;
13387 case 2:
13388 /* Note that TGE does not apply at EL2. */
13389 if (arm_hcr_el2_eff(env) & HCR_E2H) {
13390 if (env->pstate & PSTATE_PAN) {
13391 idx = ARMMMUIdx_E20_2_PAN;
13392 } else {
13393 idx = ARMMMUIdx_E20_2;
13395 } else {
13396 idx = ARMMMUIdx_E2;
13398 break;
13399 case 3:
13400 return ARMMMUIdx_SE3;
13401 default:
13402 g_assert_not_reached();
13405 if (arm_is_secure_below_el3(env)) {
13406 idx &= ~ARM_MMU_IDX_A_NS;
13409 return idx;
13412 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
13414 return arm_mmu_idx_el(env, arm_current_el(env));
13417 #ifndef CONFIG_USER_ONLY
13418 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
13420 return stage_1_mmu_idx(arm_mmu_idx(env));
13422 #endif
13424 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
13425 ARMMMUIdx mmu_idx,
13426 CPUARMTBFlags flags)
13428 DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
13429 DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
13431 if (arm_singlestep_active(env)) {
13432 DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
13434 return flags;
13437 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
13438 ARMMMUIdx mmu_idx,
13439 CPUARMTBFlags flags)
13441 bool sctlr_b = arm_sctlr_b(env);
13443 if (sctlr_b) {
13444 DP_TBFLAG_A32(flags, SCTLR__B, 1);
13446 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
13447 DP_TBFLAG_ANY(flags, BE_DATA, 1);
13449 DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
13451 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13454 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
13455 ARMMMUIdx mmu_idx)
13457 CPUARMTBFlags flags = {};
13458 uint32_t ccr = env->v7m.ccr[env->v7m.secure];
13460 /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
13461 if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
13462 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13465 if (arm_v7m_is_handler_mode(env)) {
13466 DP_TBFLAG_M32(flags, HANDLER, 1);
13470 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
13471 * is suppressing them because the requested execution priority
13472 * is less than 0.
13474 if (arm_feature(env, ARM_FEATURE_V8) &&
13475 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
13476 (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
13477 DP_TBFLAG_M32(flags, STACKCHECK, 1);
13480 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
13483 static CPUARMTBFlags rebuild_hflags_aprofile(CPUARMState *env)
13485 CPUARMTBFlags flags = {};
13487 DP_TBFLAG_ANY(flags, DEBUG_TARGET_EL, arm_debug_target_el(env));
13488 return flags;
13491 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
13492 ARMMMUIdx mmu_idx)
13494 CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
13495 int el = arm_current_el(env);
13497 if (arm_sctlr(env, el) & SCTLR_A) {
13498 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13501 if (arm_el_is_aa64(env, 1)) {
13502 DP_TBFLAG_A32(flags, VFPEN, 1);
13505 if (el < 2 && env->cp15.hstr_el2 &&
13506 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
13507 DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
13510 if (env->uncached_cpsr & CPSR_IL) {
13511 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13514 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
13517 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
13518 ARMMMUIdx mmu_idx)
13520 CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
13521 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
13522 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
13523 uint64_t sctlr;
13524 int tbii, tbid;
13526 DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
13528 /* Get control bits for tagged addresses. */
13529 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
13530 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
13532 DP_TBFLAG_A64(flags, TBII, tbii);
13533 DP_TBFLAG_A64(flags, TBID, tbid);
13535 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
13536 int sve_el = sve_exception_el(env, el);
13537 uint32_t zcr_len;
13540 * If SVE is disabled, but FP is enabled,
13541 * then the effective len is 0.
13543 if (sve_el != 0 && fp_el == 0) {
13544 zcr_len = 0;
13545 } else {
13546 zcr_len = sve_zcr_len_for_el(env, el);
13548 DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
13549 DP_TBFLAG_A64(flags, ZCR_LEN, zcr_len);
13552 sctlr = regime_sctlr(env, stage1);
13554 if (sctlr & SCTLR_A) {
13555 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13558 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
13559 DP_TBFLAG_ANY(flags, BE_DATA, 1);
13562 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
13564 * In order to save space in flags, we record only whether
13565 * pauth is "inactive", meaning all insns are implemented as
13566 * a nop, or "active" when some action must be performed.
13567 * The decision of which action to take is left to a helper.
13569 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
13570 DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
13574 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
13575 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
13576 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
13577 DP_TBFLAG_A64(flags, BT, 1);
13581 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
13582 if (!(env->pstate & PSTATE_UAO)) {
13583 switch (mmu_idx) {
13584 case ARMMMUIdx_E10_1:
13585 case ARMMMUIdx_E10_1_PAN:
13586 case ARMMMUIdx_SE10_1:
13587 case ARMMMUIdx_SE10_1_PAN:
13588 /* TODO: ARMv8.3-NV */
13589 DP_TBFLAG_A64(flags, UNPRIV, 1);
13590 break;
13591 case ARMMMUIdx_E20_2:
13592 case ARMMMUIdx_E20_2_PAN:
13593 case ARMMMUIdx_SE20_2:
13594 case ARMMMUIdx_SE20_2_PAN:
13596 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
13597 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
13599 if (env->cp15.hcr_el2 & HCR_TGE) {
13600 DP_TBFLAG_A64(flags, UNPRIV, 1);
13602 break;
13603 default:
13604 break;
13608 if (env->pstate & PSTATE_IL) {
13609 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13612 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
13614 * Set MTE_ACTIVE if any access may be Checked, and leave clear
13615 * if all accesses must be Unchecked:
13616 * 1) If no TBI, then there are no tags in the address to check,
13617 * 2) If Tag Check Override, then all accesses are Unchecked,
13618 * 3) If Tag Check Fail == 0, then Checked access have no effect,
13619 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
13621 if (allocation_tag_access_enabled(env, el, sctlr)) {
13622 DP_TBFLAG_A64(flags, ATA, 1);
13623 if (tbid
13624 && !(env->pstate & PSTATE_TCO)
13625 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
13626 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
13629 /* And again for unprivileged accesses, if required. */
13630 if (EX_TBFLAG_A64(flags, UNPRIV)
13631 && tbid
13632 && !(env->pstate & PSTATE_TCO)
13633 && (sctlr & SCTLR_TCF0)
13634 && allocation_tag_access_enabled(env, 0, sctlr)) {
13635 DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
13637 /* Cache TCMA as well as TBI. */
13638 DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
13641 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13644 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
13646 int el = arm_current_el(env);
13647 int fp_el = fp_exception_el(env, el);
13648 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13650 if (is_a64(env)) {
13651 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13652 } else if (arm_feature(env, ARM_FEATURE_M)) {
13653 return rebuild_hflags_m32(env, fp_el, mmu_idx);
13654 } else {
13655 return rebuild_hflags_a32(env, fp_el, mmu_idx);
13659 void arm_rebuild_hflags(CPUARMState *env)
13661 env->hflags = rebuild_hflags_internal(env);
13665 * If we have triggered a EL state change we can't rely on the
13666 * translator having passed it to us, we need to recompute.
13668 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
13670 int el = arm_current_el(env);
13671 int fp_el = fp_exception_el(env, el);
13672 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13674 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13677 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
13679 int fp_el = fp_exception_el(env, el);
13680 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13682 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13686 * If we have triggered a EL state change we can't rely on the
13687 * translator having passed it to us, we need to recompute.
13689 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
13691 int el = arm_current_el(env);
13692 int fp_el = fp_exception_el(env, el);
13693 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13694 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13697 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
13699 int fp_el = fp_exception_el(env, el);
13700 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13702 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13705 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
13707 int fp_el = fp_exception_el(env, el);
13708 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13710 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13713 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
13715 #ifdef CONFIG_DEBUG_TCG
13716 CPUARMTBFlags c = env->hflags;
13717 CPUARMTBFlags r = rebuild_hflags_internal(env);
13719 if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
13720 fprintf(stderr, "TCG hflags mismatch "
13721 "(current:(0x%08x,0x" TARGET_FMT_lx ")"
13722 " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
13723 c.flags, c.flags2, r.flags, r.flags2);
13724 abort();
13726 #endif
13729 static bool mve_no_pred(CPUARMState *env)
13732 * Return true if there is definitely no predication of MVE
13733 * instructions by VPR or LTPSIZE. (Returning false even if there
13734 * isn't any predication is OK; generated code will just be
13735 * a little worse.)
13736 * If the CPU does not implement MVE then this TB flag is always 0.
13738 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
13739 * logic in gen_update_fp_context() needs to be updated to match.
13741 * We do not include the effect of the ECI bits here -- they are
13742 * tracked in other TB flags. This simplifies the logic for
13743 * "when did we emit code that changes the MVE_NO_PRED TB flag
13744 * and thus need to end the TB?".
13746 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
13747 return false;
13749 if (env->v7m.vpr) {
13750 return false;
13752 if (env->v7m.ltpsize < 4) {
13753 return false;
13755 return true;
13758 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
13759 target_ulong *cs_base, uint32_t *pflags)
13761 CPUARMTBFlags flags;
13763 assert_hflags_rebuild_correctly(env);
13764 flags = env->hflags;
13766 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
13767 *pc = env->pc;
13768 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
13769 DP_TBFLAG_A64(flags, BTYPE, env->btype);
13771 } else {
13772 *pc = env->regs[15];
13774 if (arm_feature(env, ARM_FEATURE_M)) {
13775 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
13776 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
13777 != env->v7m.secure) {
13778 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
13781 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
13782 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
13783 (env->v7m.secure &&
13784 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
13786 * ASPEN is set, but FPCA/SFPA indicate that there is no
13787 * active FP context; we must create a new FP context before
13788 * executing any FP insn.
13790 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
13793 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
13794 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
13795 DP_TBFLAG_M32(flags, LSPACT, 1);
13798 if (mve_no_pred(env)) {
13799 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
13801 } else {
13803 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
13804 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
13806 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
13807 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
13808 } else {
13809 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
13810 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
13812 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
13813 DP_TBFLAG_A32(flags, VFPEN, 1);
13817 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
13818 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
13822 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
13823 * states defined in the ARM ARM for software singlestep:
13824 * SS_ACTIVE PSTATE.SS State
13825 * 0 x Inactive (the TB flag for SS is always 0)
13826 * 1 0 Active-pending
13827 * 1 1 Active-not-pending
13828 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
13830 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
13831 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
13834 *pflags = flags.flags;
13835 *cs_base = flags.flags2;
13838 #ifdef TARGET_AARCH64
13840 * The manual says that when SVE is enabled and VQ is widened the
13841 * implementation is allowed to zero the previously inaccessible
13842 * portion of the registers. The corollary to that is that when
13843 * SVE is enabled and VQ is narrowed we are also allowed to zero
13844 * the now inaccessible portion of the registers.
13846 * The intent of this is that no predicate bit beyond VQ is ever set.
13847 * Which means that some operations on predicate registers themselves
13848 * may operate on full uint64_t or even unrolled across the maximum
13849 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
13850 * may well be cheaper than conditionals to restrict the operation
13851 * to the relevant portion of a uint16_t[16].
13853 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
13855 int i, j;
13856 uint64_t pmask;
13858 assert(vq >= 1 && vq <= ARM_MAX_VQ);
13859 assert(vq <= env_archcpu(env)->sve_max_vq);
13861 /* Zap the high bits of the zregs. */
13862 for (i = 0; i < 32; i++) {
13863 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
13866 /* Zap the high bits of the pregs and ffr. */
13867 pmask = 0;
13868 if (vq & 3) {
13869 pmask = ~(-1ULL << (16 * (vq & 3)));
13871 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13872 for (i = 0; i < 17; ++i) {
13873 env->vfp.pregs[i].p[j] &= pmask;
13875 pmask = 0;
13880 * Notice a change in SVE vector size when changing EL.
13882 void aarch64_sve_change_el(CPUARMState *env, int old_el,
13883 int new_el, bool el0_a64)
13885 ARMCPU *cpu = env_archcpu(env);
13886 int old_len, new_len;
13887 bool old_a64, new_a64;
13889 /* Nothing to do if no SVE. */
13890 if (!cpu_isar_feature(aa64_sve, cpu)) {
13891 return;
13894 /* Nothing to do if FP is disabled in either EL. */
13895 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
13896 return;
13900 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
13901 * at ELx, or not available because the EL is in AArch32 state, then
13902 * for all purposes other than a direct read, the ZCR_ELx.LEN field
13903 * has an effective value of 0".
13905 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
13906 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
13907 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
13908 * we already have the correct register contents when encountering the
13909 * vq0->vq0 transition between EL0->EL1.
13911 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
13912 old_len = (old_a64 && !sve_exception_el(env, old_el)
13913 ? sve_zcr_len_for_el(env, old_el) : 0);
13914 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
13915 new_len = (new_a64 && !sve_exception_el(env, new_el)
13916 ? sve_zcr_len_for_el(env, new_el) : 0);
13918 /* When changing vector length, clear inaccessible state. */
13919 if (new_len < old_len) {
13920 aarch64_sve_narrow_vq(env, new_len + 1);
13923 #endif