target/arm: Add SMCR_ELx
[qemu/rayw.git] / target / arm / helper.c
blob2072f2a550e1f8e302ad064c3545d4055448fb5e
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 "trace.h"
13 #include "cpu.h"
14 #include "internals.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/host-utils.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/timer.h"
19 #include "qemu/bitops.h"
20 #include "qemu/crc32c.h"
21 #include "qemu/qemu-print.h"
22 #include "exec/exec-all.h"
23 #include <zlib.h> /* For crc32 */
24 #include "hw/irq.h"
25 #include "semihosting/semihost.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/cpu-timers.h"
28 #include "sysemu/kvm.h"
29 #include "qemu/range.h"
30 #include "qapi/qapi-commands-machine-target.h"
31 #include "qapi/error.h"
32 #include "qemu/guest-random.h"
33 #ifdef CONFIG_TCG
34 #include "arm_ldst.h"
35 #include "exec/cpu_ldst.h"
36 #include "semihosting/common-semi.h"
37 #endif
38 #include "cpregs.h"
40 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
42 static void switch_mode(CPUARMState *env, int mode);
44 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
46 assert(ri->fieldoffset);
47 if (cpreg_field_is_64bit(ri)) {
48 return CPREG_FIELD64(env, ri);
49 } else {
50 return CPREG_FIELD32(env, ri);
54 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
55 uint64_t value)
57 assert(ri->fieldoffset);
58 if (cpreg_field_is_64bit(ri)) {
59 CPREG_FIELD64(env, ri) = value;
60 } else {
61 CPREG_FIELD32(env, ri) = value;
65 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
67 return (char *)env + ri->fieldoffset;
70 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
72 /* Raw read of a coprocessor register (as needed for migration, etc). */
73 if (ri->type & ARM_CP_CONST) {
74 return ri->resetvalue;
75 } else if (ri->raw_readfn) {
76 return ri->raw_readfn(env, ri);
77 } else if (ri->readfn) {
78 return ri->readfn(env, ri);
79 } else {
80 return raw_read(env, ri);
84 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
85 uint64_t v)
87 /* Raw write of a coprocessor register (as needed for migration, etc).
88 * Note that constant registers are treated as write-ignored; the
89 * caller should check for success by whether a readback gives the
90 * value written.
92 if (ri->type & ARM_CP_CONST) {
93 return;
94 } else if (ri->raw_writefn) {
95 ri->raw_writefn(env, ri, v);
96 } else if (ri->writefn) {
97 ri->writefn(env, ri, v);
98 } else {
99 raw_write(env, ri, v);
103 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
105 /* Return true if the regdef would cause an assertion if you called
106 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
107 * program bug for it not to have the NO_RAW flag).
108 * NB that returning false here doesn't necessarily mean that calling
109 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
110 * read/write access functions which are safe for raw use" from "has
111 * read/write access functions which have side effects but has forgotten
112 * to provide raw access functions".
113 * The tests here line up with the conditions in read/write_raw_cp_reg()
114 * and assertions in raw_read()/raw_write().
116 if ((ri->type & ARM_CP_CONST) ||
117 ri->fieldoffset ||
118 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
119 return false;
121 return true;
124 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
126 /* Write the coprocessor state from cpu->env to the (index,value) list. */
127 int i;
128 bool ok = true;
130 for (i = 0; i < cpu->cpreg_array_len; i++) {
131 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
132 const ARMCPRegInfo *ri;
133 uint64_t newval;
135 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
136 if (!ri) {
137 ok = false;
138 continue;
140 if (ri->type & ARM_CP_NO_RAW) {
141 continue;
144 newval = read_raw_cp_reg(&cpu->env, ri);
145 if (kvm_sync) {
147 * Only sync if the previous list->cpustate sync succeeded.
148 * Rather than tracking the success/failure state for every
149 * item in the list, we just recheck "does the raw write we must
150 * have made in write_list_to_cpustate() read back OK" here.
152 uint64_t oldval = cpu->cpreg_values[i];
154 if (oldval == newval) {
155 continue;
158 write_raw_cp_reg(&cpu->env, ri, oldval);
159 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
160 continue;
163 write_raw_cp_reg(&cpu->env, ri, newval);
165 cpu->cpreg_values[i] = newval;
167 return ok;
170 bool write_list_to_cpustate(ARMCPU *cpu)
172 int i;
173 bool ok = true;
175 for (i = 0; i < cpu->cpreg_array_len; i++) {
176 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
177 uint64_t v = cpu->cpreg_values[i];
178 const ARMCPRegInfo *ri;
180 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
181 if (!ri) {
182 ok = false;
183 continue;
185 if (ri->type & ARM_CP_NO_RAW) {
186 continue;
188 /* Write value and confirm it reads back as written
189 * (to catch read-only registers and partially read-only
190 * registers where the incoming migration value doesn't match)
192 write_raw_cp_reg(&cpu->env, ri, v);
193 if (read_raw_cp_reg(&cpu->env, ri) != v) {
194 ok = false;
197 return ok;
200 static void add_cpreg_to_list(gpointer key, gpointer opaque)
202 ARMCPU *cpu = opaque;
203 uint32_t regidx = (uintptr_t)key;
204 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
206 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
207 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
208 /* The value array need not be initialized at this point */
209 cpu->cpreg_array_len++;
213 static void count_cpreg(gpointer key, gpointer opaque)
215 ARMCPU *cpu = opaque;
216 const ARMCPRegInfo *ri;
218 ri = g_hash_table_lookup(cpu->cp_regs, key);
220 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
221 cpu->cpreg_array_len++;
225 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
227 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
228 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
230 if (aidx > bidx) {
231 return 1;
233 if (aidx < bidx) {
234 return -1;
236 return 0;
239 void init_cpreg_list(ARMCPU *cpu)
241 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
242 * Note that we require cpreg_tuples[] to be sorted by key ID.
244 GList *keys;
245 int arraylen;
247 keys = g_hash_table_get_keys(cpu->cp_regs);
248 keys = g_list_sort(keys, cpreg_key_compare);
250 cpu->cpreg_array_len = 0;
252 g_list_foreach(keys, count_cpreg, cpu);
254 arraylen = cpu->cpreg_array_len;
255 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
256 cpu->cpreg_values = g_new(uint64_t, arraylen);
257 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
258 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
259 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
260 cpu->cpreg_array_len = 0;
262 g_list_foreach(keys, add_cpreg_to_list, cpu);
264 assert(cpu->cpreg_array_len == arraylen);
266 g_list_free(keys);
270 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
272 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
273 const ARMCPRegInfo *ri,
274 bool isread)
276 if (!is_a64(env) && arm_current_el(env) == 3 &&
277 arm_is_secure_below_el3(env)) {
278 return CP_ACCESS_TRAP_UNCATEGORIZED;
280 return CP_ACCESS_OK;
283 /* Some secure-only AArch32 registers trap to EL3 if used from
284 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
285 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
286 * We assume that the .access field is set to PL1_RW.
288 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
289 const ARMCPRegInfo *ri,
290 bool isread)
292 if (arm_current_el(env) == 3) {
293 return CP_ACCESS_OK;
295 if (arm_is_secure_below_el3(env)) {
296 if (env->cp15.scr_el3 & SCR_EEL2) {
297 return CP_ACCESS_TRAP_EL2;
299 return CP_ACCESS_TRAP_EL3;
301 /* This will be EL1 NS and EL2 NS, which just UNDEF */
302 return CP_ACCESS_TRAP_UNCATEGORIZED;
305 static uint64_t arm_mdcr_el2_eff(CPUARMState *env)
307 return arm_is_el2_enabled(env) ? env->cp15.mdcr_el2 : 0;
310 /* Check for traps to "powerdown debug" registers, which are controlled
311 * by MDCR.TDOSA
313 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
314 bool isread)
316 int el = arm_current_el(env);
317 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
318 bool mdcr_el2_tdosa = (mdcr_el2 & MDCR_TDOSA) || (mdcr_el2 & MDCR_TDE) ||
319 (arm_hcr_el2_eff(env) & HCR_TGE);
321 if (el < 2 && mdcr_el2_tdosa) {
322 return CP_ACCESS_TRAP_EL2;
324 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
325 return CP_ACCESS_TRAP_EL3;
327 return CP_ACCESS_OK;
330 /* Check for traps to "debug ROM" registers, which are controlled
331 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
333 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
334 bool isread)
336 int el = arm_current_el(env);
337 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
338 bool mdcr_el2_tdra = (mdcr_el2 & MDCR_TDRA) || (mdcr_el2 & MDCR_TDE) ||
339 (arm_hcr_el2_eff(env) & HCR_TGE);
341 if (el < 2 && mdcr_el2_tdra) {
342 return CP_ACCESS_TRAP_EL2;
344 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
345 return CP_ACCESS_TRAP_EL3;
347 return CP_ACCESS_OK;
350 /* Check for traps to general debug registers, which are controlled
351 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
353 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
354 bool isread)
356 int el = arm_current_el(env);
357 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
358 bool mdcr_el2_tda = (mdcr_el2 & MDCR_TDA) || (mdcr_el2 & MDCR_TDE) ||
359 (arm_hcr_el2_eff(env) & HCR_TGE);
361 if (el < 2 && mdcr_el2_tda) {
362 return CP_ACCESS_TRAP_EL2;
364 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
365 return CP_ACCESS_TRAP_EL3;
367 return CP_ACCESS_OK;
370 /* Check for traps to performance monitor registers, which are controlled
371 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
373 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
374 bool isread)
376 int el = arm_current_el(env);
377 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
379 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
380 return CP_ACCESS_TRAP_EL2;
382 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
383 return CP_ACCESS_TRAP_EL3;
385 return CP_ACCESS_OK;
388 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
389 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
390 bool isread)
392 if (arm_current_el(env) == 1) {
393 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
394 if (arm_hcr_el2_eff(env) & trap) {
395 return CP_ACCESS_TRAP_EL2;
398 return CP_ACCESS_OK;
401 /* Check for traps from EL1 due to HCR_EL2.TSW. */
402 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
403 bool isread)
405 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
406 return CP_ACCESS_TRAP_EL2;
408 return CP_ACCESS_OK;
411 /* Check for traps from EL1 due to HCR_EL2.TACR. */
412 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
413 bool isread)
415 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
416 return CP_ACCESS_TRAP_EL2;
418 return CP_ACCESS_OK;
421 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
422 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
423 bool isread)
425 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
426 return CP_ACCESS_TRAP_EL2;
428 return CP_ACCESS_OK;
431 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
433 ARMCPU *cpu = env_archcpu(env);
435 raw_write(env, ri, value);
436 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
439 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
441 ARMCPU *cpu = env_archcpu(env);
443 if (raw_read(env, ri) != value) {
444 /* Unlike real hardware the qemu TLB uses virtual addresses,
445 * not modified virtual addresses, so this causes a TLB flush.
447 tlb_flush(CPU(cpu));
448 raw_write(env, ri, value);
452 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
453 uint64_t value)
455 ARMCPU *cpu = env_archcpu(env);
457 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
458 && !extended_addresses_enabled(env)) {
459 /* For VMSA (when not using the LPAE long descriptor page table
460 * format) this register includes the ASID, so do a TLB flush.
461 * For PMSA it is purely a process ID and no action is needed.
463 tlb_flush(CPU(cpu));
465 raw_write(env, ri, value);
468 /* IS variants of TLB operations must affect all cores */
469 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
470 uint64_t value)
472 CPUState *cs = env_cpu(env);
474 tlb_flush_all_cpus_synced(cs);
477 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
478 uint64_t value)
480 CPUState *cs = env_cpu(env);
482 tlb_flush_all_cpus_synced(cs);
485 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
486 uint64_t value)
488 CPUState *cs = env_cpu(env);
490 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
493 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
494 uint64_t value)
496 CPUState *cs = env_cpu(env);
498 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
502 * Non-IS variants of TLB operations are upgraded to
503 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
504 * force broadcast of these operations.
506 static bool tlb_force_broadcast(CPUARMState *env)
508 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
511 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
512 uint64_t value)
514 /* Invalidate all (TLBIALL) */
515 CPUState *cs = env_cpu(env);
517 if (tlb_force_broadcast(env)) {
518 tlb_flush_all_cpus_synced(cs);
519 } else {
520 tlb_flush(cs);
524 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
525 uint64_t value)
527 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
528 CPUState *cs = env_cpu(env);
530 value &= TARGET_PAGE_MASK;
531 if (tlb_force_broadcast(env)) {
532 tlb_flush_page_all_cpus_synced(cs, value);
533 } else {
534 tlb_flush_page(cs, value);
538 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
539 uint64_t value)
541 /* Invalidate by ASID (TLBIASID) */
542 CPUState *cs = env_cpu(env);
544 if (tlb_force_broadcast(env)) {
545 tlb_flush_all_cpus_synced(cs);
546 } else {
547 tlb_flush(cs);
551 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
552 uint64_t value)
554 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
555 CPUState *cs = env_cpu(env);
557 value &= TARGET_PAGE_MASK;
558 if (tlb_force_broadcast(env)) {
559 tlb_flush_page_all_cpus_synced(cs, value);
560 } else {
561 tlb_flush_page(cs, value);
565 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 uint64_t value)
568 CPUState *cs = env_cpu(env);
570 tlb_flush_by_mmuidx(cs,
571 ARMMMUIdxBit_E10_1 |
572 ARMMMUIdxBit_E10_1_PAN |
573 ARMMMUIdxBit_E10_0);
576 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
577 uint64_t value)
579 CPUState *cs = env_cpu(env);
581 tlb_flush_by_mmuidx_all_cpus_synced(cs,
582 ARMMMUIdxBit_E10_1 |
583 ARMMMUIdxBit_E10_1_PAN |
584 ARMMMUIdxBit_E10_0);
588 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
589 uint64_t value)
591 CPUState *cs = env_cpu(env);
593 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
596 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
597 uint64_t value)
599 CPUState *cs = env_cpu(env);
601 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
604 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
605 uint64_t value)
607 CPUState *cs = env_cpu(env);
608 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
610 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
613 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
614 uint64_t value)
616 CPUState *cs = env_cpu(env);
617 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
619 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
620 ARMMMUIdxBit_E2);
623 static const ARMCPRegInfo cp_reginfo[] = {
624 /* Define the secure and non-secure FCSE identifier CP registers
625 * separately because there is no secure bank in V8 (no _EL3). This allows
626 * the secure register to be properly reset and migrated. There is also no
627 * v8 EL1 version of the register so the non-secure instance stands alone.
629 { .name = "FCSEIDR",
630 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
631 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
632 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
633 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
634 { .name = "FCSEIDR_S",
635 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
636 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
637 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
638 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
639 /* Define the secure and non-secure context identifier CP registers
640 * separately because there is no secure bank in V8 (no _EL3). This allows
641 * the secure register to be properly reset and migrated. In the
642 * non-secure case, the 32-bit register will have reset and migration
643 * disabled during registration as it is handled by the 64-bit instance.
645 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
646 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
647 .access = PL1_RW, .accessfn = access_tvm_trvm,
648 .secure = ARM_CP_SECSTATE_NS,
649 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
650 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
651 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
652 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
653 .access = PL1_RW, .accessfn = access_tvm_trvm,
654 .secure = ARM_CP_SECSTATE_S,
655 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
656 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
659 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
660 /* NB: Some of these registers exist in v8 but with more precise
661 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
663 /* MMU Domain access control / MPU write buffer control */
664 { .name = "DACR",
665 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
666 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
667 .writefn = dacr_write, .raw_writefn = raw_write,
668 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
669 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
670 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
671 * For v6 and v5, these mappings are overly broad.
673 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
674 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
675 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
676 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
677 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
678 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
679 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
680 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
681 /* Cache maintenance ops; some of this space may be overridden later. */
682 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
683 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
684 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
687 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
688 /* Not all pre-v6 cores implemented this WFI, so this is slightly
689 * over-broad.
691 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
692 .access = PL1_W, .type = ARM_CP_WFI },
695 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
696 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
697 * is UNPREDICTABLE; we choose to NOP as most implementations do).
699 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
700 .access = PL1_W, .type = ARM_CP_WFI },
701 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
702 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
703 * OMAPCP will override this space.
705 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
706 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
707 .resetvalue = 0 },
708 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
709 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
710 .resetvalue = 0 },
711 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
712 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
713 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
714 .resetvalue = 0 },
715 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
716 * implementing it as RAZ means the "debug architecture version" bits
717 * will read as a reserved value, which should cause Linux to not try
718 * to use the debug hardware.
720 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
721 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
722 /* MMU TLB control. Note that the wildcarding means we cover not just
723 * the unified TLB ops but also the dside/iside/inner-shareable variants.
725 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
726 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
727 .type = ARM_CP_NO_RAW },
728 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
729 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
730 .type = ARM_CP_NO_RAW },
731 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
732 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
733 .type = ARM_CP_NO_RAW },
734 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
735 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
736 .type = ARM_CP_NO_RAW },
737 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
738 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
739 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
740 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
743 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
744 uint64_t value)
746 uint32_t mask = 0;
748 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
749 if (!arm_feature(env, ARM_FEATURE_V8)) {
750 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
751 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
752 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
754 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
755 /* VFP coprocessor: cp10 & cp11 [23:20] */
756 mask |= R_CPACR_ASEDIS_MASK |
757 R_CPACR_D32DIS_MASK |
758 R_CPACR_CP11_MASK |
759 R_CPACR_CP10_MASK;
761 if (!arm_feature(env, ARM_FEATURE_NEON)) {
762 /* ASEDIS [31] bit is RAO/WI */
763 value |= R_CPACR_ASEDIS_MASK;
766 /* VFPv3 and upwards with NEON implement 32 double precision
767 * registers (D0-D31).
769 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
770 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
771 value |= R_CPACR_D32DIS_MASK;
774 value &= mask;
778 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
779 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
781 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
782 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
783 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
784 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
787 env->cp15.cpacr_el1 = value;
790 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
793 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
794 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
796 uint64_t value = env->cp15.cpacr_el1;
798 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
799 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
800 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
802 return value;
806 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
808 /* Call cpacr_write() so that we reset with the correct RAO bits set
809 * for our CPU features.
811 cpacr_write(env, ri, 0);
814 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
815 bool isread)
817 if (arm_feature(env, ARM_FEATURE_V8)) {
818 /* Check if CPACR accesses are to be trapped to EL2 */
819 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
820 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
821 return CP_ACCESS_TRAP_EL2;
822 /* Check if CPACR accesses are to be trapped to EL3 */
823 } else if (arm_current_el(env) < 3 &&
824 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
825 return CP_ACCESS_TRAP_EL3;
829 return CP_ACCESS_OK;
832 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
833 bool isread)
835 /* Check if CPTR accesses are set to trap to EL3 */
836 if (arm_current_el(env) == 2 &&
837 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
838 return CP_ACCESS_TRAP_EL3;
841 return CP_ACCESS_OK;
844 static const ARMCPRegInfo v6_cp_reginfo[] = {
845 /* prefetch by MVA in v6, NOP in v7 */
846 { .name = "MVA_prefetch",
847 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
848 .access = PL1_W, .type = ARM_CP_NOP },
849 /* We need to break the TB after ISB to execute self-modifying code
850 * correctly and also to take any pending interrupts immediately.
851 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
853 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
854 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
855 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
856 .access = PL0_W, .type = ARM_CP_NOP },
857 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
858 .access = PL0_W, .type = ARM_CP_NOP },
859 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
860 .access = PL1_RW, .accessfn = access_tvm_trvm,
861 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
862 offsetof(CPUARMState, cp15.ifar_ns) },
863 .resetvalue = 0, },
864 /* Watchpoint Fault Address Register : should actually only be present
865 * for 1136, 1176, 11MPCore.
867 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
868 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
869 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
870 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
871 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
872 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
875 typedef struct pm_event {
876 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
877 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
878 bool (*supported)(CPUARMState *);
880 * Retrieve the current count of the underlying event. The programmed
881 * counters hold a difference from the return value from this function
883 uint64_t (*get_count)(CPUARMState *);
885 * Return how many nanoseconds it will take (at a minimum) for count events
886 * to occur. A negative value indicates the counter will never overflow, or
887 * that the counter has otherwise arranged for the overflow bit to be set
888 * and the PMU interrupt to be raised on overflow.
890 int64_t (*ns_per_count)(uint64_t);
891 } pm_event;
893 static bool event_always_supported(CPUARMState *env)
895 return true;
898 static uint64_t swinc_get_count(CPUARMState *env)
901 * SW_INCR events are written directly to the pmevcntr's by writes to
902 * PMSWINC, so there is no underlying count maintained by the PMU itself
904 return 0;
907 static int64_t swinc_ns_per(uint64_t ignored)
909 return -1;
913 * Return the underlying cycle count for the PMU cycle counters. If we're in
914 * usermode, simply return 0.
916 static uint64_t cycles_get_count(CPUARMState *env)
918 #ifndef CONFIG_USER_ONLY
919 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
920 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
921 #else
922 return cpu_get_host_ticks();
923 #endif
926 #ifndef CONFIG_USER_ONLY
927 static int64_t cycles_ns_per(uint64_t cycles)
929 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
932 static bool instructions_supported(CPUARMState *env)
934 return icount_enabled() == 1; /* Precise instruction counting */
937 static uint64_t instructions_get_count(CPUARMState *env)
939 return (uint64_t)icount_get_raw();
942 static int64_t instructions_ns_per(uint64_t icount)
944 return icount_to_ns((int64_t)icount);
946 #endif
948 static bool pmu_8_1_events_supported(CPUARMState *env)
950 /* For events which are supported in any v8.1 PMU */
951 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
954 static bool pmu_8_4_events_supported(CPUARMState *env)
956 /* For events which are supported in any v8.1 PMU */
957 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
960 static uint64_t zero_event_get_count(CPUARMState *env)
962 /* For events which on QEMU never fire, so their count is always zero */
963 return 0;
966 static int64_t zero_event_ns_per(uint64_t cycles)
968 /* An event which never fires can never overflow */
969 return -1;
972 static const pm_event pm_events[] = {
973 { .number = 0x000, /* SW_INCR */
974 .supported = event_always_supported,
975 .get_count = swinc_get_count,
976 .ns_per_count = swinc_ns_per,
978 #ifndef CONFIG_USER_ONLY
979 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
980 .supported = instructions_supported,
981 .get_count = instructions_get_count,
982 .ns_per_count = instructions_ns_per,
984 { .number = 0x011, /* CPU_CYCLES, Cycle */
985 .supported = event_always_supported,
986 .get_count = cycles_get_count,
987 .ns_per_count = cycles_ns_per,
989 #endif
990 { .number = 0x023, /* STALL_FRONTEND */
991 .supported = pmu_8_1_events_supported,
992 .get_count = zero_event_get_count,
993 .ns_per_count = zero_event_ns_per,
995 { .number = 0x024, /* STALL_BACKEND */
996 .supported = pmu_8_1_events_supported,
997 .get_count = zero_event_get_count,
998 .ns_per_count = zero_event_ns_per,
1000 { .number = 0x03c, /* STALL */
1001 .supported = pmu_8_4_events_supported,
1002 .get_count = zero_event_get_count,
1003 .ns_per_count = zero_event_ns_per,
1008 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1009 * events (i.e. the statistical profiling extension), this implementation
1010 * should first be updated to something sparse instead of the current
1011 * supported_event_map[] array.
1013 #define MAX_EVENT_ID 0x3c
1014 #define UNSUPPORTED_EVENT UINT16_MAX
1015 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1018 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1019 * of ARM event numbers to indices in our pm_events array.
1021 * Note: Events in the 0x40XX range are not currently supported.
1023 void pmu_init(ARMCPU *cpu)
1025 unsigned int i;
1028 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1029 * events to them
1031 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1032 supported_event_map[i] = UNSUPPORTED_EVENT;
1034 cpu->pmceid0 = 0;
1035 cpu->pmceid1 = 0;
1037 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1038 const pm_event *cnt = &pm_events[i];
1039 assert(cnt->number <= MAX_EVENT_ID);
1040 /* We do not currently support events in the 0x40xx range */
1041 assert(cnt->number <= 0x3f);
1043 if (cnt->supported(&cpu->env)) {
1044 supported_event_map[cnt->number] = i;
1045 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1046 if (cnt->number & 0x20) {
1047 cpu->pmceid1 |= event_mask;
1048 } else {
1049 cpu->pmceid0 |= event_mask;
1056 * Check at runtime whether a PMU event is supported for the current machine
1058 static bool event_supported(uint16_t number)
1060 if (number > MAX_EVENT_ID) {
1061 return false;
1063 return supported_event_map[number] != UNSUPPORTED_EVENT;
1066 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1067 bool isread)
1069 /* Performance monitor registers user accessibility is controlled
1070 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1071 * trapping to EL2 or EL3 for other accesses.
1073 int el = arm_current_el(env);
1074 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1076 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1077 return CP_ACCESS_TRAP;
1079 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1080 return CP_ACCESS_TRAP_EL2;
1082 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1083 return CP_ACCESS_TRAP_EL3;
1086 return CP_ACCESS_OK;
1089 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1090 const ARMCPRegInfo *ri,
1091 bool isread)
1093 /* ER: event counter read trap control */
1094 if (arm_feature(env, ARM_FEATURE_V8)
1095 && arm_current_el(env) == 0
1096 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1097 && isread) {
1098 return CP_ACCESS_OK;
1101 return pmreg_access(env, ri, isread);
1104 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1105 const ARMCPRegInfo *ri,
1106 bool isread)
1108 /* SW: software increment write trap control */
1109 if (arm_feature(env, ARM_FEATURE_V8)
1110 && arm_current_el(env) == 0
1111 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1112 && !isread) {
1113 return CP_ACCESS_OK;
1116 return pmreg_access(env, ri, isread);
1119 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1120 const ARMCPRegInfo *ri,
1121 bool isread)
1123 /* ER: event counter read trap control */
1124 if (arm_feature(env, ARM_FEATURE_V8)
1125 && arm_current_el(env) == 0
1126 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1127 return CP_ACCESS_OK;
1130 return pmreg_access(env, ri, isread);
1133 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1134 const ARMCPRegInfo *ri,
1135 bool isread)
1137 /* CR: cycle counter read trap control */
1138 if (arm_feature(env, ARM_FEATURE_V8)
1139 && arm_current_el(env) == 0
1140 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1141 && isread) {
1142 return CP_ACCESS_OK;
1145 return pmreg_access(env, ri, isread);
1148 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1149 * the current EL, security state, and register configuration.
1151 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1153 uint64_t filter;
1154 bool e, p, u, nsk, nsu, nsh, m;
1155 bool enabled, prohibited, filtered;
1156 bool secure = arm_is_secure(env);
1157 int el = arm_current_el(env);
1158 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1159 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1161 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1162 return false;
1165 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1166 (counter < hpmn || counter == 31)) {
1167 e = env->cp15.c9_pmcr & PMCRE;
1168 } else {
1169 e = mdcr_el2 & MDCR_HPME;
1171 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1173 if (!secure) {
1174 if (el == 2 && (counter < hpmn || counter == 31)) {
1175 prohibited = mdcr_el2 & MDCR_HPMD;
1176 } else {
1177 prohibited = false;
1179 } else {
1180 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1181 !(env->cp15.mdcr_el3 & MDCR_SPME);
1184 if (prohibited && counter == 31) {
1185 prohibited = env->cp15.c9_pmcr & PMCRDP;
1188 if (counter == 31) {
1189 filter = env->cp15.pmccfiltr_el0;
1190 } else {
1191 filter = env->cp15.c14_pmevtyper[counter];
1194 p = filter & PMXEVTYPER_P;
1195 u = filter & PMXEVTYPER_U;
1196 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1197 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1198 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1199 m = arm_el_is_aa64(env, 1) &&
1200 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1202 if (el == 0) {
1203 filtered = secure ? u : u != nsu;
1204 } else if (el == 1) {
1205 filtered = secure ? p : p != nsk;
1206 } else if (el == 2) {
1207 filtered = !nsh;
1208 } else { /* EL3 */
1209 filtered = m != p;
1212 if (counter != 31) {
1214 * If not checking PMCCNTR, ensure the counter is setup to an event we
1215 * support
1217 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1218 if (!event_supported(event)) {
1219 return false;
1223 return enabled && !prohibited && !filtered;
1226 static void pmu_update_irq(CPUARMState *env)
1228 ARMCPU *cpu = env_archcpu(env);
1229 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1230 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1234 * Ensure c15_ccnt is the guest-visible count so that operations such as
1235 * enabling/disabling the counter or filtering, modifying the count itself,
1236 * etc. can be done logically. This is essentially a no-op if the counter is
1237 * not enabled at the time of the call.
1239 static void pmccntr_op_start(CPUARMState *env)
1241 uint64_t cycles = cycles_get_count(env);
1243 if (pmu_counter_enabled(env, 31)) {
1244 uint64_t eff_cycles = cycles;
1245 if (env->cp15.c9_pmcr & PMCRD) {
1246 /* Increment once every 64 processor clock cycles */
1247 eff_cycles /= 64;
1250 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1252 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1253 1ull << 63 : 1ull << 31;
1254 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1255 env->cp15.c9_pmovsr |= (1 << 31);
1256 pmu_update_irq(env);
1259 env->cp15.c15_ccnt = new_pmccntr;
1261 env->cp15.c15_ccnt_delta = cycles;
1265 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1266 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1267 * pmccntr_op_start.
1269 static void pmccntr_op_finish(CPUARMState *env)
1271 if (pmu_counter_enabled(env, 31)) {
1272 #ifndef CONFIG_USER_ONLY
1273 /* Calculate when the counter will next overflow */
1274 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1275 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1276 remaining_cycles = (uint32_t)remaining_cycles;
1278 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1280 if (overflow_in > 0) {
1281 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1282 overflow_in;
1283 ARMCPU *cpu = env_archcpu(env);
1284 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1286 #endif
1288 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1289 if (env->cp15.c9_pmcr & PMCRD) {
1290 /* Increment once every 64 processor clock cycles */
1291 prev_cycles /= 64;
1293 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1297 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1300 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1301 uint64_t count = 0;
1302 if (event_supported(event)) {
1303 uint16_t event_idx = supported_event_map[event];
1304 count = pm_events[event_idx].get_count(env);
1307 if (pmu_counter_enabled(env, counter)) {
1308 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1310 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1311 env->cp15.c9_pmovsr |= (1 << counter);
1312 pmu_update_irq(env);
1314 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1316 env->cp15.c14_pmevcntr_delta[counter] = count;
1319 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1321 if (pmu_counter_enabled(env, counter)) {
1322 #ifndef CONFIG_USER_ONLY
1323 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1324 uint16_t event_idx = supported_event_map[event];
1325 uint64_t delta = UINT32_MAX -
1326 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1327 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1329 if (overflow_in > 0) {
1330 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1331 overflow_in;
1332 ARMCPU *cpu = env_archcpu(env);
1333 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1335 #endif
1337 env->cp15.c14_pmevcntr_delta[counter] -=
1338 env->cp15.c14_pmevcntr[counter];
1342 void pmu_op_start(CPUARMState *env)
1344 unsigned int i;
1345 pmccntr_op_start(env);
1346 for (i = 0; i < pmu_num_counters(env); i++) {
1347 pmevcntr_op_start(env, i);
1351 void pmu_op_finish(CPUARMState *env)
1353 unsigned int i;
1354 pmccntr_op_finish(env);
1355 for (i = 0; i < pmu_num_counters(env); i++) {
1356 pmevcntr_op_finish(env, i);
1360 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1362 pmu_op_start(&cpu->env);
1365 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1367 pmu_op_finish(&cpu->env);
1370 void arm_pmu_timer_cb(void *opaque)
1372 ARMCPU *cpu = opaque;
1375 * Update all the counter values based on the current underlying counts,
1376 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1377 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1378 * counter may expire.
1380 pmu_op_start(&cpu->env);
1381 pmu_op_finish(&cpu->env);
1384 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1385 uint64_t value)
1387 pmu_op_start(env);
1389 if (value & PMCRC) {
1390 /* The counter has been reset */
1391 env->cp15.c15_ccnt = 0;
1394 if (value & PMCRP) {
1395 unsigned int i;
1396 for (i = 0; i < pmu_num_counters(env); i++) {
1397 env->cp15.c14_pmevcntr[i] = 0;
1401 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1402 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1404 pmu_op_finish(env);
1407 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1408 uint64_t value)
1410 unsigned int i;
1411 for (i = 0; i < pmu_num_counters(env); i++) {
1412 /* Increment a counter's count iff: */
1413 if ((value & (1 << i)) && /* counter's bit is set */
1414 /* counter is enabled and not filtered */
1415 pmu_counter_enabled(env, i) &&
1416 /* counter is SW_INCR */
1417 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1418 pmevcntr_op_start(env, i);
1421 * Detect if this write causes an overflow since we can't predict
1422 * PMSWINC overflows like we can for other events
1424 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1426 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1427 env->cp15.c9_pmovsr |= (1 << i);
1428 pmu_update_irq(env);
1431 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1433 pmevcntr_op_finish(env, i);
1438 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1440 uint64_t ret;
1441 pmccntr_op_start(env);
1442 ret = env->cp15.c15_ccnt;
1443 pmccntr_op_finish(env);
1444 return ret;
1447 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1448 uint64_t value)
1450 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1451 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1452 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1453 * accessed.
1455 env->cp15.c9_pmselr = value & 0x1f;
1458 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1459 uint64_t value)
1461 pmccntr_op_start(env);
1462 env->cp15.c15_ccnt = value;
1463 pmccntr_op_finish(env);
1466 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1467 uint64_t value)
1469 uint64_t cur_val = pmccntr_read(env, NULL);
1471 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1474 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1475 uint64_t value)
1477 pmccntr_op_start(env);
1478 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1479 pmccntr_op_finish(env);
1482 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1483 uint64_t value)
1485 pmccntr_op_start(env);
1486 /* M is not accessible from AArch32 */
1487 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1488 (value & PMCCFILTR);
1489 pmccntr_op_finish(env);
1492 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1494 /* M is not visible in AArch32 */
1495 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1498 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1499 uint64_t value)
1501 value &= pmu_counter_mask(env);
1502 env->cp15.c9_pmcnten |= value;
1505 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1506 uint64_t value)
1508 value &= pmu_counter_mask(env);
1509 env->cp15.c9_pmcnten &= ~value;
1512 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1513 uint64_t value)
1515 value &= pmu_counter_mask(env);
1516 env->cp15.c9_pmovsr &= ~value;
1517 pmu_update_irq(env);
1520 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1521 uint64_t value)
1523 value &= pmu_counter_mask(env);
1524 env->cp15.c9_pmovsr |= value;
1525 pmu_update_irq(env);
1528 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1529 uint64_t value, const uint8_t counter)
1531 if (counter == 31) {
1532 pmccfiltr_write(env, ri, value);
1533 } else if (counter < pmu_num_counters(env)) {
1534 pmevcntr_op_start(env, counter);
1537 * If this counter's event type is changing, store the current
1538 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1539 * pmevcntr_op_finish has the correct baseline when it converts back to
1540 * a delta.
1542 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1543 PMXEVTYPER_EVTCOUNT;
1544 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1545 if (old_event != new_event) {
1546 uint64_t count = 0;
1547 if (event_supported(new_event)) {
1548 uint16_t event_idx = supported_event_map[new_event];
1549 count = pm_events[event_idx].get_count(env);
1551 env->cp15.c14_pmevcntr_delta[counter] = count;
1554 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1555 pmevcntr_op_finish(env, counter);
1557 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1558 * PMSELR value is equal to or greater than the number of implemented
1559 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1563 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1564 const uint8_t counter)
1566 if (counter == 31) {
1567 return env->cp15.pmccfiltr_el0;
1568 } else if (counter < pmu_num_counters(env)) {
1569 return env->cp15.c14_pmevtyper[counter];
1570 } else {
1572 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1573 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1575 return 0;
1579 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1580 uint64_t value)
1582 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1583 pmevtyper_write(env, ri, value, counter);
1586 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1587 uint64_t value)
1589 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1590 env->cp15.c14_pmevtyper[counter] = value;
1593 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1594 * pmu_op_finish calls when loading saved state for a migration. Because
1595 * we're potentially updating the type of event here, the value written to
1596 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1597 * different counter type. Therefore, we need to set this value to the
1598 * current count for the counter type we're writing so that pmu_op_finish
1599 * has the correct count for its calculation.
1601 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1602 if (event_supported(event)) {
1603 uint16_t event_idx = supported_event_map[event];
1604 env->cp15.c14_pmevcntr_delta[counter] =
1605 pm_events[event_idx].get_count(env);
1609 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1611 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1612 return pmevtyper_read(env, ri, counter);
1615 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1616 uint64_t value)
1618 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1621 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1623 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1626 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1627 uint64_t value, uint8_t counter)
1629 if (counter < pmu_num_counters(env)) {
1630 pmevcntr_op_start(env, counter);
1631 env->cp15.c14_pmevcntr[counter] = value;
1632 pmevcntr_op_finish(env, counter);
1635 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1636 * are CONSTRAINED UNPREDICTABLE.
1640 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1641 uint8_t counter)
1643 if (counter < pmu_num_counters(env)) {
1644 uint64_t ret;
1645 pmevcntr_op_start(env, counter);
1646 ret = env->cp15.c14_pmevcntr[counter];
1647 pmevcntr_op_finish(env, counter);
1648 return ret;
1649 } else {
1650 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1651 * are CONSTRAINED UNPREDICTABLE. */
1652 return 0;
1656 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1657 uint64_t value)
1659 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1660 pmevcntr_write(env, ri, value, counter);
1663 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1665 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1666 return pmevcntr_read(env, ri, counter);
1669 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1670 uint64_t value)
1672 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1673 assert(counter < pmu_num_counters(env));
1674 env->cp15.c14_pmevcntr[counter] = value;
1675 pmevcntr_write(env, ri, value, counter);
1678 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1680 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1681 assert(counter < pmu_num_counters(env));
1682 return env->cp15.c14_pmevcntr[counter];
1685 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1686 uint64_t value)
1688 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1691 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1693 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1696 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1697 uint64_t value)
1699 if (arm_feature(env, ARM_FEATURE_V8)) {
1700 env->cp15.c9_pmuserenr = value & 0xf;
1701 } else {
1702 env->cp15.c9_pmuserenr = value & 1;
1706 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1707 uint64_t value)
1709 /* We have no event counters so only the C bit can be changed */
1710 value &= pmu_counter_mask(env);
1711 env->cp15.c9_pminten |= value;
1712 pmu_update_irq(env);
1715 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1716 uint64_t value)
1718 value &= pmu_counter_mask(env);
1719 env->cp15.c9_pminten &= ~value;
1720 pmu_update_irq(env);
1723 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1724 uint64_t value)
1726 /* Note that even though the AArch64 view of this register has bits
1727 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1728 * architectural requirements for bits which are RES0 only in some
1729 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1730 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1732 raw_write(env, ri, value & ~0x1FULL);
1735 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1737 /* Begin with base v8.0 state. */
1738 uint32_t valid_mask = 0x3fff;
1739 ARMCPU *cpu = env_archcpu(env);
1742 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1743 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1744 * Instead, choose the format based on the mode of EL3.
1746 if (arm_el_is_aa64(env, 3)) {
1747 value |= SCR_FW | SCR_AW; /* RES1 */
1748 valid_mask &= ~SCR_NET; /* RES0 */
1750 if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1751 !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1752 value |= SCR_RW; /* RAO/WI */
1754 if (cpu_isar_feature(aa64_ras, cpu)) {
1755 valid_mask |= SCR_TERR;
1757 if (cpu_isar_feature(aa64_lor, cpu)) {
1758 valid_mask |= SCR_TLOR;
1760 if (cpu_isar_feature(aa64_pauth, cpu)) {
1761 valid_mask |= SCR_API | SCR_APK;
1763 if (cpu_isar_feature(aa64_sel2, cpu)) {
1764 valid_mask |= SCR_EEL2;
1766 if (cpu_isar_feature(aa64_mte, cpu)) {
1767 valid_mask |= SCR_ATA;
1769 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1770 valid_mask |= SCR_ENSCXT;
1772 if (cpu_isar_feature(aa64_doublefault, cpu)) {
1773 valid_mask |= SCR_EASE | SCR_NMEA;
1775 } else {
1776 valid_mask &= ~(SCR_RW | SCR_ST);
1777 if (cpu_isar_feature(aa32_ras, cpu)) {
1778 valid_mask |= SCR_TERR;
1782 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1783 valid_mask &= ~SCR_HCE;
1785 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1786 * supported if EL2 exists. The bit is UNK/SBZP when
1787 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1788 * when EL2 is unavailable.
1789 * On ARMv8, this bit is always available.
1791 if (arm_feature(env, ARM_FEATURE_V7) &&
1792 !arm_feature(env, ARM_FEATURE_V8)) {
1793 valid_mask &= ~SCR_SMD;
1797 /* Clear all-context RES0 bits. */
1798 value &= valid_mask;
1799 raw_write(env, ri, value);
1802 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1805 * scr_write will set the RES1 bits on an AArch64-only CPU.
1806 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1808 scr_write(env, ri, 0);
1811 static CPAccessResult access_aa64_tid2(CPUARMState *env,
1812 const ARMCPRegInfo *ri,
1813 bool isread)
1815 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1816 return CP_ACCESS_TRAP_EL2;
1819 return CP_ACCESS_OK;
1822 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1824 ARMCPU *cpu = env_archcpu(env);
1826 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1827 * bank
1829 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1830 ri->secure & ARM_CP_SECSTATE_S);
1832 return cpu->ccsidr[index];
1835 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1836 uint64_t value)
1838 raw_write(env, ri, value & 0xf);
1841 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1843 CPUState *cs = env_cpu(env);
1844 bool el1 = arm_current_el(env) == 1;
1845 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1846 uint64_t ret = 0;
1848 if (hcr_el2 & HCR_IMO) {
1849 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1850 ret |= CPSR_I;
1852 } else {
1853 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1854 ret |= CPSR_I;
1858 if (hcr_el2 & HCR_FMO) {
1859 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1860 ret |= CPSR_F;
1862 } else {
1863 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1864 ret |= CPSR_F;
1868 if (hcr_el2 & HCR_AMO) {
1869 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1870 ret |= CPSR_A;
1874 return ret;
1877 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1878 bool isread)
1880 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1881 return CP_ACCESS_TRAP_EL2;
1884 return CP_ACCESS_OK;
1887 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1888 bool isread)
1890 if (arm_feature(env, ARM_FEATURE_V8)) {
1891 return access_aa64_tid1(env, ri, isread);
1894 return CP_ACCESS_OK;
1897 static const ARMCPRegInfo v7_cp_reginfo[] = {
1898 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1899 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1900 .access = PL1_W, .type = ARM_CP_NOP },
1901 /* Performance monitors are implementation defined in v7,
1902 * but with an ARM recommended set of registers, which we
1903 * follow.
1905 * Performance registers fall into three categories:
1906 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1907 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1908 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1909 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1910 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1912 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1913 .access = PL0_RW, .type = ARM_CP_ALIAS,
1914 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1915 .writefn = pmcntenset_write,
1916 .accessfn = pmreg_access,
1917 .raw_writefn = raw_write },
1918 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1919 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1920 .access = PL0_RW, .accessfn = pmreg_access,
1921 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1922 .writefn = pmcntenset_write, .raw_writefn = raw_write },
1923 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1924 .access = PL0_RW,
1925 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1926 .accessfn = pmreg_access,
1927 .writefn = pmcntenclr_write,
1928 .type = ARM_CP_ALIAS },
1929 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1930 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1931 .access = PL0_RW, .accessfn = pmreg_access,
1932 .type = ARM_CP_ALIAS,
1933 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1934 .writefn = pmcntenclr_write },
1935 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1936 .access = PL0_RW, .type = ARM_CP_IO,
1937 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1938 .accessfn = pmreg_access,
1939 .writefn = pmovsr_write,
1940 .raw_writefn = raw_write },
1941 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1942 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1943 .access = PL0_RW, .accessfn = pmreg_access,
1944 .type = ARM_CP_ALIAS | ARM_CP_IO,
1945 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1946 .writefn = pmovsr_write,
1947 .raw_writefn = raw_write },
1948 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1949 .access = PL0_W, .accessfn = pmreg_access_swinc,
1950 .type = ARM_CP_NO_RAW | ARM_CP_IO,
1951 .writefn = pmswinc_write },
1952 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1953 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1954 .access = PL0_W, .accessfn = pmreg_access_swinc,
1955 .type = ARM_CP_NO_RAW | ARM_CP_IO,
1956 .writefn = pmswinc_write },
1957 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1958 .access = PL0_RW, .type = ARM_CP_ALIAS,
1959 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1960 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1961 .raw_writefn = raw_write},
1962 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1963 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1964 .access = PL0_RW, .accessfn = pmreg_access_selr,
1965 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1966 .writefn = pmselr_write, .raw_writefn = raw_write, },
1967 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1968 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1969 .readfn = pmccntr_read, .writefn = pmccntr_write32,
1970 .accessfn = pmreg_access_ccntr },
1971 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1972 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1973 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1974 .type = ARM_CP_IO,
1975 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1976 .readfn = pmccntr_read, .writefn = pmccntr_write,
1977 .raw_readfn = raw_read, .raw_writefn = raw_write, },
1978 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1979 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1980 .access = PL0_RW, .accessfn = pmreg_access,
1981 .type = ARM_CP_ALIAS | ARM_CP_IO,
1982 .resetvalue = 0, },
1983 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1984 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1985 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1986 .access = PL0_RW, .accessfn = pmreg_access,
1987 .type = ARM_CP_IO,
1988 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1989 .resetvalue = 0, },
1990 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1991 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1992 .accessfn = pmreg_access,
1993 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1994 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1995 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1996 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1997 .accessfn = pmreg_access,
1998 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1999 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2000 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2001 .accessfn = pmreg_access_xevcntr,
2002 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2003 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2004 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2005 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2006 .accessfn = pmreg_access_xevcntr,
2007 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2008 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2009 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2010 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2011 .resetvalue = 0,
2012 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2013 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2014 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2015 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2016 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2017 .resetvalue = 0,
2018 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2019 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2020 .access = PL1_RW, .accessfn = access_tpm,
2021 .type = ARM_CP_ALIAS | ARM_CP_IO,
2022 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2023 .resetvalue = 0,
2024 .writefn = pmintenset_write, .raw_writefn = raw_write },
2025 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2026 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2027 .access = PL1_RW, .accessfn = access_tpm,
2028 .type = ARM_CP_IO,
2029 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2030 .writefn = pmintenset_write, .raw_writefn = raw_write,
2031 .resetvalue = 0x0 },
2032 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2033 .access = PL1_RW, .accessfn = access_tpm,
2034 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2035 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2036 .writefn = pmintenclr_write, },
2037 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2038 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2039 .access = PL1_RW, .accessfn = access_tpm,
2040 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2041 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2042 .writefn = pmintenclr_write },
2043 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2044 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2045 .access = PL1_R,
2046 .accessfn = access_aa64_tid2,
2047 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2048 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2049 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2050 .access = PL1_RW,
2051 .accessfn = access_aa64_tid2,
2052 .writefn = csselr_write, .resetvalue = 0,
2053 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2054 offsetof(CPUARMState, cp15.csselr_ns) } },
2055 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2056 * just RAZ for all cores:
2058 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2059 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2060 .access = PL1_R, .type = ARM_CP_CONST,
2061 .accessfn = access_aa64_tid1,
2062 .resetvalue = 0 },
2063 /* Auxiliary fault status registers: these also are IMPDEF, and we
2064 * choose to RAZ/WI for all cores.
2066 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2067 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2068 .access = PL1_RW, .accessfn = access_tvm_trvm,
2069 .type = ARM_CP_CONST, .resetvalue = 0 },
2070 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2071 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2072 .access = PL1_RW, .accessfn = access_tvm_trvm,
2073 .type = ARM_CP_CONST, .resetvalue = 0 },
2074 /* MAIR can just read-as-written because we don't implement caches
2075 * and so don't need to care about memory attributes.
2077 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2078 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2079 .access = PL1_RW, .accessfn = access_tvm_trvm,
2080 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2081 .resetvalue = 0 },
2082 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2083 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2084 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2085 .resetvalue = 0 },
2086 /* For non-long-descriptor page tables these are PRRR and NMRR;
2087 * regardless they still act as reads-as-written for QEMU.
2089 /* MAIR0/1 are defined separately from their 64-bit counterpart which
2090 * allows them to assign the correct fieldoffset based on the endianness
2091 * handled in the field definitions.
2093 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2094 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2095 .access = PL1_RW, .accessfn = access_tvm_trvm,
2096 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2097 offsetof(CPUARMState, cp15.mair0_ns) },
2098 .resetfn = arm_cp_reset_ignore },
2099 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2100 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2101 .access = PL1_RW, .accessfn = access_tvm_trvm,
2102 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2103 offsetof(CPUARMState, cp15.mair1_ns) },
2104 .resetfn = arm_cp_reset_ignore },
2105 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2106 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2107 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2108 /* 32 bit ITLB invalidates */
2109 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2110 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2111 .writefn = tlbiall_write },
2112 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2113 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2114 .writefn = tlbimva_write },
2115 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2116 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2117 .writefn = tlbiasid_write },
2118 /* 32 bit DTLB invalidates */
2119 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2120 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2121 .writefn = tlbiall_write },
2122 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2123 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2124 .writefn = tlbimva_write },
2125 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2126 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2127 .writefn = tlbiasid_write },
2128 /* 32 bit TLB invalidates */
2129 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2130 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2131 .writefn = tlbiall_write },
2132 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2133 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2134 .writefn = tlbimva_write },
2135 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2136 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2137 .writefn = tlbiasid_write },
2138 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2139 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2140 .writefn = tlbimvaa_write },
2143 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2144 /* 32 bit TLB invalidates, Inner Shareable */
2145 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2146 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2147 .writefn = tlbiall_is_write },
2148 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2149 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2150 .writefn = tlbimva_is_write },
2151 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2152 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2153 .writefn = tlbiasid_is_write },
2154 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2155 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2156 .writefn = tlbimvaa_is_write },
2159 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2160 /* PMOVSSET is not implemented in v7 before v7ve */
2161 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2162 .access = PL0_RW, .accessfn = pmreg_access,
2163 .type = ARM_CP_ALIAS | ARM_CP_IO,
2164 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2165 .writefn = pmovsset_write,
2166 .raw_writefn = raw_write },
2167 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2168 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2169 .access = PL0_RW, .accessfn = pmreg_access,
2170 .type = ARM_CP_ALIAS | ARM_CP_IO,
2171 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2172 .writefn = pmovsset_write,
2173 .raw_writefn = raw_write },
2176 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2177 uint64_t value)
2179 value &= 1;
2180 env->teecr = value;
2183 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2184 bool isread)
2187 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2188 * at all, so we don't need to check whether we're v8A.
2190 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2191 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2192 return CP_ACCESS_TRAP_EL2;
2194 return CP_ACCESS_OK;
2197 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2198 bool isread)
2200 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2201 return CP_ACCESS_TRAP;
2203 return teecr_access(env, ri, isread);
2206 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2207 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2208 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2209 .resetvalue = 0,
2210 .writefn = teecr_write, .accessfn = teecr_access },
2211 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2212 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2213 .accessfn = teehbr_access, .resetvalue = 0 },
2216 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2217 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2218 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2219 .access = PL0_RW,
2220 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2221 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2222 .access = PL0_RW,
2223 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2224 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2225 .resetfn = arm_cp_reset_ignore },
2226 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2227 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2228 .access = PL0_R|PL1_W,
2229 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2230 .resetvalue = 0},
2231 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2232 .access = PL0_R|PL1_W,
2233 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2234 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2235 .resetfn = arm_cp_reset_ignore },
2236 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2237 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2238 .access = PL1_RW,
2239 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2240 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2241 .access = PL1_RW,
2242 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2243 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2244 .resetvalue = 0 },
2247 #ifndef CONFIG_USER_ONLY
2249 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2250 bool isread)
2252 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2253 * Writable only at the highest implemented exception level.
2255 int el = arm_current_el(env);
2256 uint64_t hcr;
2257 uint32_t cntkctl;
2259 switch (el) {
2260 case 0:
2261 hcr = arm_hcr_el2_eff(env);
2262 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2263 cntkctl = env->cp15.cnthctl_el2;
2264 } else {
2265 cntkctl = env->cp15.c14_cntkctl;
2267 if (!extract32(cntkctl, 0, 2)) {
2268 return CP_ACCESS_TRAP;
2270 break;
2271 case 1:
2272 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2273 arm_is_secure_below_el3(env)) {
2274 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2275 return CP_ACCESS_TRAP_UNCATEGORIZED;
2277 break;
2278 case 2:
2279 case 3:
2280 break;
2283 if (!isread && el < arm_highest_el(env)) {
2284 return CP_ACCESS_TRAP_UNCATEGORIZED;
2287 return CP_ACCESS_OK;
2290 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2291 bool isread)
2293 unsigned int cur_el = arm_current_el(env);
2294 bool has_el2 = arm_is_el2_enabled(env);
2295 uint64_t hcr = arm_hcr_el2_eff(env);
2297 switch (cur_el) {
2298 case 0:
2299 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2300 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2301 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2302 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2305 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2306 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2307 return CP_ACCESS_TRAP;
2310 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2311 if (hcr & HCR_E2H) {
2312 if (timeridx == GTIMER_PHYS &&
2313 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2314 return CP_ACCESS_TRAP_EL2;
2316 } else {
2317 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2318 if (has_el2 && timeridx == GTIMER_PHYS &&
2319 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2320 return CP_ACCESS_TRAP_EL2;
2323 break;
2325 case 1:
2326 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2327 if (has_el2 && timeridx == GTIMER_PHYS &&
2328 (hcr & HCR_E2H
2329 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2330 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2331 return CP_ACCESS_TRAP_EL2;
2333 break;
2335 return CP_ACCESS_OK;
2338 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2339 bool isread)
2341 unsigned int cur_el = arm_current_el(env);
2342 bool has_el2 = arm_is_el2_enabled(env);
2343 uint64_t hcr = arm_hcr_el2_eff(env);
2345 switch (cur_el) {
2346 case 0:
2347 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2348 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2349 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2350 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2354 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2355 * EL0 if EL0[PV]TEN is zero.
2357 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2358 return CP_ACCESS_TRAP;
2360 /* fall through */
2362 case 1:
2363 if (has_el2 && timeridx == GTIMER_PHYS) {
2364 if (hcr & HCR_E2H) {
2365 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2366 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2367 return CP_ACCESS_TRAP_EL2;
2369 } else {
2370 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2371 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2372 return CP_ACCESS_TRAP_EL2;
2376 break;
2378 return CP_ACCESS_OK;
2381 static CPAccessResult gt_pct_access(CPUARMState *env,
2382 const ARMCPRegInfo *ri,
2383 bool isread)
2385 return gt_counter_access(env, GTIMER_PHYS, isread);
2388 static CPAccessResult gt_vct_access(CPUARMState *env,
2389 const ARMCPRegInfo *ri,
2390 bool isread)
2392 return gt_counter_access(env, GTIMER_VIRT, isread);
2395 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2396 bool isread)
2398 return gt_timer_access(env, GTIMER_PHYS, isread);
2401 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2402 bool isread)
2404 return gt_timer_access(env, GTIMER_VIRT, isread);
2407 static CPAccessResult gt_stimer_access(CPUARMState *env,
2408 const ARMCPRegInfo *ri,
2409 bool isread)
2411 /* The AArch64 register view of the secure physical timer is
2412 * always accessible from EL3, and configurably accessible from
2413 * Secure EL1.
2415 switch (arm_current_el(env)) {
2416 case 1:
2417 if (!arm_is_secure(env)) {
2418 return CP_ACCESS_TRAP;
2420 if (!(env->cp15.scr_el3 & SCR_ST)) {
2421 return CP_ACCESS_TRAP_EL3;
2423 return CP_ACCESS_OK;
2424 case 0:
2425 case 2:
2426 return CP_ACCESS_TRAP;
2427 case 3:
2428 return CP_ACCESS_OK;
2429 default:
2430 g_assert_not_reached();
2434 static uint64_t gt_get_countervalue(CPUARMState *env)
2436 ARMCPU *cpu = env_archcpu(env);
2438 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2441 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2443 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2445 if (gt->ctl & 1) {
2446 /* Timer enabled: calculate and set current ISTATUS, irq, and
2447 * reset timer to when ISTATUS next has to change
2449 uint64_t offset = timeridx == GTIMER_VIRT ?
2450 cpu->env.cp15.cntvoff_el2 : 0;
2451 uint64_t count = gt_get_countervalue(&cpu->env);
2452 /* Note that this must be unsigned 64 bit arithmetic: */
2453 int istatus = count - offset >= gt->cval;
2454 uint64_t nexttick;
2455 int irqstate;
2457 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2459 irqstate = (istatus && !(gt->ctl & 2));
2460 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2462 if (istatus) {
2463 /* Next transition is when count rolls back over to zero */
2464 nexttick = UINT64_MAX;
2465 } else {
2466 /* Next transition is when we hit cval */
2467 nexttick = gt->cval + offset;
2469 /* Note that the desired next expiry time might be beyond the
2470 * signed-64-bit range of a QEMUTimer -- in this case we just
2471 * set the timer for as far in the future as possible. When the
2472 * timer expires we will reset the timer for any remaining period.
2474 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2475 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2476 } else {
2477 timer_mod(cpu->gt_timer[timeridx], nexttick);
2479 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
2480 } else {
2481 /* Timer disabled: ISTATUS and timer output always clear */
2482 gt->ctl &= ~4;
2483 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
2484 timer_del(cpu->gt_timer[timeridx]);
2485 trace_arm_gt_recalc_disabled(timeridx);
2489 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2490 int timeridx)
2492 ARMCPU *cpu = env_archcpu(env);
2494 timer_del(cpu->gt_timer[timeridx]);
2497 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2499 return gt_get_countervalue(env);
2502 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2504 uint64_t hcr;
2506 switch (arm_current_el(env)) {
2507 case 2:
2508 hcr = arm_hcr_el2_eff(env);
2509 if (hcr & HCR_E2H) {
2510 return 0;
2512 break;
2513 case 0:
2514 hcr = arm_hcr_el2_eff(env);
2515 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2516 return 0;
2518 break;
2521 return env->cp15.cntvoff_el2;
2524 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2526 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2529 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2530 int timeridx,
2531 uint64_t value)
2533 trace_arm_gt_cval_write(timeridx, value);
2534 env->cp15.c14_timer[timeridx].cval = value;
2535 gt_recalc_timer(env_archcpu(env), timeridx);
2538 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2539 int timeridx)
2541 uint64_t offset = 0;
2543 switch (timeridx) {
2544 case GTIMER_VIRT:
2545 case GTIMER_HYPVIRT:
2546 offset = gt_virt_cnt_offset(env);
2547 break;
2550 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2551 (gt_get_countervalue(env) - offset));
2554 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2555 int timeridx,
2556 uint64_t value)
2558 uint64_t offset = 0;
2560 switch (timeridx) {
2561 case GTIMER_VIRT:
2562 case GTIMER_HYPVIRT:
2563 offset = gt_virt_cnt_offset(env);
2564 break;
2567 trace_arm_gt_tval_write(timeridx, value);
2568 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2569 sextract64(value, 0, 32);
2570 gt_recalc_timer(env_archcpu(env), timeridx);
2573 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2574 int timeridx,
2575 uint64_t value)
2577 ARMCPU *cpu = env_archcpu(env);
2578 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2580 trace_arm_gt_ctl_write(timeridx, value);
2581 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2582 if ((oldval ^ value) & 1) {
2583 /* Enable toggled */
2584 gt_recalc_timer(cpu, timeridx);
2585 } else if ((oldval ^ value) & 2) {
2586 /* IMASK toggled: don't need to recalculate,
2587 * just set the interrupt line based on ISTATUS
2589 int irqstate = (oldval & 4) && !(value & 2);
2591 trace_arm_gt_imask_toggle(timeridx, irqstate);
2592 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2596 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2598 gt_timer_reset(env, ri, GTIMER_PHYS);
2601 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2602 uint64_t value)
2604 gt_cval_write(env, ri, GTIMER_PHYS, value);
2607 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2609 return gt_tval_read(env, ri, GTIMER_PHYS);
2612 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2613 uint64_t value)
2615 gt_tval_write(env, ri, GTIMER_PHYS, value);
2618 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2619 uint64_t value)
2621 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2624 static int gt_phys_redir_timeridx(CPUARMState *env)
2626 switch (arm_mmu_idx(env)) {
2627 case ARMMMUIdx_E20_0:
2628 case ARMMMUIdx_E20_2:
2629 case ARMMMUIdx_E20_2_PAN:
2630 case ARMMMUIdx_SE20_0:
2631 case ARMMMUIdx_SE20_2:
2632 case ARMMMUIdx_SE20_2_PAN:
2633 return GTIMER_HYP;
2634 default:
2635 return GTIMER_PHYS;
2639 static int gt_virt_redir_timeridx(CPUARMState *env)
2641 switch (arm_mmu_idx(env)) {
2642 case ARMMMUIdx_E20_0:
2643 case ARMMMUIdx_E20_2:
2644 case ARMMMUIdx_E20_2_PAN:
2645 case ARMMMUIdx_SE20_0:
2646 case ARMMMUIdx_SE20_2:
2647 case ARMMMUIdx_SE20_2_PAN:
2648 return GTIMER_HYPVIRT;
2649 default:
2650 return GTIMER_VIRT;
2654 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2655 const ARMCPRegInfo *ri)
2657 int timeridx = gt_phys_redir_timeridx(env);
2658 return env->cp15.c14_timer[timeridx].cval;
2661 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2662 uint64_t value)
2664 int timeridx = gt_phys_redir_timeridx(env);
2665 gt_cval_write(env, ri, timeridx, value);
2668 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2669 const ARMCPRegInfo *ri)
2671 int timeridx = gt_phys_redir_timeridx(env);
2672 return gt_tval_read(env, ri, timeridx);
2675 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2676 uint64_t value)
2678 int timeridx = gt_phys_redir_timeridx(env);
2679 gt_tval_write(env, ri, timeridx, value);
2682 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2683 const ARMCPRegInfo *ri)
2685 int timeridx = gt_phys_redir_timeridx(env);
2686 return env->cp15.c14_timer[timeridx].ctl;
2689 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2690 uint64_t value)
2692 int timeridx = gt_phys_redir_timeridx(env);
2693 gt_ctl_write(env, ri, timeridx, value);
2696 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2698 gt_timer_reset(env, ri, GTIMER_VIRT);
2701 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2702 uint64_t value)
2704 gt_cval_write(env, ri, GTIMER_VIRT, value);
2707 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2709 return gt_tval_read(env, ri, GTIMER_VIRT);
2712 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2713 uint64_t value)
2715 gt_tval_write(env, ri, GTIMER_VIRT, value);
2718 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2719 uint64_t value)
2721 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2724 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2725 uint64_t value)
2727 ARMCPU *cpu = env_archcpu(env);
2729 trace_arm_gt_cntvoff_write(value);
2730 raw_write(env, ri, value);
2731 gt_recalc_timer(cpu, GTIMER_VIRT);
2734 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2735 const ARMCPRegInfo *ri)
2737 int timeridx = gt_virt_redir_timeridx(env);
2738 return env->cp15.c14_timer[timeridx].cval;
2741 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2742 uint64_t value)
2744 int timeridx = gt_virt_redir_timeridx(env);
2745 gt_cval_write(env, ri, timeridx, value);
2748 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2749 const ARMCPRegInfo *ri)
2751 int timeridx = gt_virt_redir_timeridx(env);
2752 return gt_tval_read(env, ri, timeridx);
2755 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2756 uint64_t value)
2758 int timeridx = gt_virt_redir_timeridx(env);
2759 gt_tval_write(env, ri, timeridx, value);
2762 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2763 const ARMCPRegInfo *ri)
2765 int timeridx = gt_virt_redir_timeridx(env);
2766 return env->cp15.c14_timer[timeridx].ctl;
2769 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2770 uint64_t value)
2772 int timeridx = gt_virt_redir_timeridx(env);
2773 gt_ctl_write(env, ri, timeridx, value);
2776 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2778 gt_timer_reset(env, ri, GTIMER_HYP);
2781 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2782 uint64_t value)
2784 gt_cval_write(env, ri, GTIMER_HYP, value);
2787 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2789 return gt_tval_read(env, ri, GTIMER_HYP);
2792 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2793 uint64_t value)
2795 gt_tval_write(env, ri, GTIMER_HYP, value);
2798 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2799 uint64_t value)
2801 gt_ctl_write(env, ri, GTIMER_HYP, value);
2804 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2806 gt_timer_reset(env, ri, GTIMER_SEC);
2809 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2810 uint64_t value)
2812 gt_cval_write(env, ri, GTIMER_SEC, value);
2815 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2817 return gt_tval_read(env, ri, GTIMER_SEC);
2820 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2821 uint64_t value)
2823 gt_tval_write(env, ri, GTIMER_SEC, value);
2826 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2827 uint64_t value)
2829 gt_ctl_write(env, ri, GTIMER_SEC, value);
2832 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2834 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2837 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2838 uint64_t value)
2840 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2843 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2845 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2848 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2849 uint64_t value)
2851 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2854 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2855 uint64_t value)
2857 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2860 void arm_gt_ptimer_cb(void *opaque)
2862 ARMCPU *cpu = opaque;
2864 gt_recalc_timer(cpu, GTIMER_PHYS);
2867 void arm_gt_vtimer_cb(void *opaque)
2869 ARMCPU *cpu = opaque;
2871 gt_recalc_timer(cpu, GTIMER_VIRT);
2874 void arm_gt_htimer_cb(void *opaque)
2876 ARMCPU *cpu = opaque;
2878 gt_recalc_timer(cpu, GTIMER_HYP);
2881 void arm_gt_stimer_cb(void *opaque)
2883 ARMCPU *cpu = opaque;
2885 gt_recalc_timer(cpu, GTIMER_SEC);
2888 void arm_gt_hvtimer_cb(void *opaque)
2890 ARMCPU *cpu = opaque;
2892 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2895 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2897 ARMCPU *cpu = env_archcpu(env);
2899 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2902 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2903 /* Note that CNTFRQ is purely reads-as-written for the benefit
2904 * of software; writing it doesn't actually change the timer frequency.
2905 * Our reset value matches the fixed frequency we implement the timer at.
2907 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
2908 .type = ARM_CP_ALIAS,
2909 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2910 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
2912 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2913 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2914 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2915 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
2916 .resetfn = arm_gt_cntfrq_reset,
2918 /* overall control: mostly access permissions */
2919 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2920 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
2921 .access = PL1_RW,
2922 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2923 .resetvalue = 0,
2925 /* per-timer control */
2926 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2927 .secure = ARM_CP_SECSTATE_NS,
2928 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2929 .accessfn = gt_ptimer_access,
2930 .fieldoffset = offsetoflow32(CPUARMState,
2931 cp15.c14_timer[GTIMER_PHYS].ctl),
2932 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2933 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2935 { .name = "CNTP_CTL_S",
2936 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2937 .secure = ARM_CP_SECSTATE_S,
2938 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2939 .accessfn = gt_ptimer_access,
2940 .fieldoffset = offsetoflow32(CPUARMState,
2941 cp15.c14_timer[GTIMER_SEC].ctl),
2942 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2944 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2945 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
2946 .type = ARM_CP_IO, .access = PL0_RW,
2947 .accessfn = gt_ptimer_access,
2948 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2949 .resetvalue = 0,
2950 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2951 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
2953 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
2954 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
2955 .accessfn = gt_vtimer_access,
2956 .fieldoffset = offsetoflow32(CPUARMState,
2957 cp15.c14_timer[GTIMER_VIRT].ctl),
2958 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2959 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2961 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2962 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
2963 .type = ARM_CP_IO, .access = PL0_RW,
2964 .accessfn = gt_vtimer_access,
2965 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2966 .resetvalue = 0,
2967 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2968 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
2970 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2971 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2972 .secure = ARM_CP_SECSTATE_NS,
2973 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2974 .accessfn = gt_ptimer_access,
2975 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2977 { .name = "CNTP_TVAL_S",
2978 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2979 .secure = ARM_CP_SECSTATE_S,
2980 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2981 .accessfn = gt_ptimer_access,
2982 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2984 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2985 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
2986 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2987 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
2988 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
2990 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
2991 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2992 .accessfn = gt_vtimer_access,
2993 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
2995 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2996 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
2997 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
2998 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
2999 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3001 /* The counter itself */
3002 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3003 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3004 .accessfn = gt_pct_access,
3005 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3007 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3008 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3009 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3010 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3012 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3013 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3014 .accessfn = gt_vct_access,
3015 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3017 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3018 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3019 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3020 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3022 /* Comparison value, indicating when the timer goes off */
3023 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3024 .secure = ARM_CP_SECSTATE_NS,
3025 .access = PL0_RW,
3026 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3027 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3028 .accessfn = gt_ptimer_access,
3029 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3030 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3032 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3033 .secure = ARM_CP_SECSTATE_S,
3034 .access = PL0_RW,
3035 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3036 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3037 .accessfn = gt_ptimer_access,
3038 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3040 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3041 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3042 .access = PL0_RW,
3043 .type = ARM_CP_IO,
3044 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3045 .resetvalue = 0, .accessfn = gt_ptimer_access,
3046 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3047 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3049 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3050 .access = PL0_RW,
3051 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3052 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3053 .accessfn = gt_vtimer_access,
3054 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3055 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3057 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3058 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3059 .access = PL0_RW,
3060 .type = ARM_CP_IO,
3061 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3062 .resetvalue = 0, .accessfn = gt_vtimer_access,
3063 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3064 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3066 /* Secure timer -- this is actually restricted to only EL3
3067 * and configurably Secure-EL1 via the accessfn.
3069 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3070 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3071 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3072 .accessfn = gt_stimer_access,
3073 .readfn = gt_sec_tval_read,
3074 .writefn = gt_sec_tval_write,
3075 .resetfn = gt_sec_timer_reset,
3077 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3078 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3079 .type = ARM_CP_IO, .access = PL1_RW,
3080 .accessfn = gt_stimer_access,
3081 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3082 .resetvalue = 0,
3083 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3085 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3086 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3087 .type = ARM_CP_IO, .access = PL1_RW,
3088 .accessfn = gt_stimer_access,
3089 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3090 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3094 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3095 bool isread)
3097 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3098 return CP_ACCESS_TRAP;
3100 return CP_ACCESS_OK;
3103 #else
3105 /* In user-mode most of the generic timer registers are inaccessible
3106 * however modern kernels (4.12+) allow access to cntvct_el0
3109 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3111 ARMCPU *cpu = env_archcpu(env);
3113 /* Currently we have no support for QEMUTimer in linux-user so we
3114 * can't call gt_get_countervalue(env), instead we directly
3115 * call the lower level functions.
3117 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3120 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3121 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3122 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3123 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3124 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3125 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3127 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3128 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3129 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3130 .readfn = gt_virt_cnt_read,
3134 #endif
3136 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3138 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3139 raw_write(env, ri, value);
3140 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3141 raw_write(env, ri, value & 0xfffff6ff);
3142 } else {
3143 raw_write(env, ri, value & 0xfffff1ff);
3147 #ifndef CONFIG_USER_ONLY
3148 /* get_phys_addr() isn't present for user-mode-only targets */
3150 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3151 bool isread)
3153 if (ri->opc2 & 4) {
3154 /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3155 * Secure EL1 (which can only happen if EL3 is AArch64).
3156 * They are simply UNDEF if executed from NS EL1.
3157 * They function normally from EL2 or EL3.
3159 if (arm_current_el(env) == 1) {
3160 if (arm_is_secure_below_el3(env)) {
3161 if (env->cp15.scr_el3 & SCR_EEL2) {
3162 return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3164 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3166 return CP_ACCESS_TRAP_UNCATEGORIZED;
3169 return CP_ACCESS_OK;
3172 #ifdef CONFIG_TCG
3173 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3174 MMUAccessType access_type, ARMMMUIdx mmu_idx)
3176 hwaddr phys_addr;
3177 target_ulong page_size;
3178 int prot;
3179 bool ret;
3180 uint64_t par64;
3181 bool format64 = false;
3182 MemTxAttrs attrs = {};
3183 ARMMMUFaultInfo fi = {};
3184 ARMCacheAttrs cacheattrs = {};
3186 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
3187 &prot, &page_size, &fi, &cacheattrs);
3190 * ATS operations only do S1 or S1+S2 translations, so we never
3191 * have to deal with the ARMCacheAttrs format for S2 only.
3193 assert(!cacheattrs.is_s2_format);
3195 if (ret) {
3197 * Some kinds of translation fault must cause exceptions rather
3198 * than being reported in the PAR.
3200 int current_el = arm_current_el(env);
3201 int target_el;
3202 uint32_t syn, fsr, fsc;
3203 bool take_exc = false;
3205 if (fi.s1ptw && current_el == 1
3206 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3208 * Synchronous stage 2 fault on an access made as part of the
3209 * translation table walk for AT S1E0* or AT S1E1* insn
3210 * executed from NS EL1. If this is a synchronous external abort
3211 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3212 * to EL3. Otherwise the fault is taken as an exception to EL2,
3213 * and HPFAR_EL2 holds the faulting IPA.
3215 if (fi.type == ARMFault_SyncExternalOnWalk &&
3216 (env->cp15.scr_el3 & SCR_EA)) {
3217 target_el = 3;
3218 } else {
3219 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3220 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3221 env->cp15.hpfar_el2 |= HPFAR_NS;
3223 target_el = 2;
3225 take_exc = true;
3226 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3228 * Synchronous external aborts during a translation table walk
3229 * are taken as Data Abort exceptions.
3231 if (fi.stage2) {
3232 if (current_el == 3) {
3233 target_el = 3;
3234 } else {
3235 target_el = 2;
3237 } else {
3238 target_el = exception_target_el(env);
3240 take_exc = true;
3243 if (take_exc) {
3244 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3245 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3246 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3247 fsr = arm_fi_to_lfsc(&fi);
3248 fsc = extract32(fsr, 0, 6);
3249 } else {
3250 fsr = arm_fi_to_sfsc(&fi);
3251 fsc = 0x3f;
3254 * Report exception with ESR indicating a fault due to a
3255 * translation table walk for a cache maintenance instruction.
3257 syn = syn_data_abort_no_iss(current_el == target_el, 0,
3258 fi.ea, 1, fi.s1ptw, 1, fsc);
3259 env->exception.vaddress = value;
3260 env->exception.fsr = fsr;
3261 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3265 if (is_a64(env)) {
3266 format64 = true;
3267 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3269 * ATS1Cxx:
3270 * * TTBCR.EAE determines whether the result is returned using the
3271 * 32-bit or the 64-bit PAR format
3272 * * Instructions executed in Hyp mode always use the 64bit format
3274 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3275 * * The Non-secure TTBCR.EAE bit is set to 1
3276 * * The implementation includes EL2, and the value of HCR.VM is 1
3278 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3280 * ATS1Hx always uses the 64bit format.
3282 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3284 if (arm_feature(env, ARM_FEATURE_EL2)) {
3285 if (mmu_idx == ARMMMUIdx_E10_0 ||
3286 mmu_idx == ARMMMUIdx_E10_1 ||
3287 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3288 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3289 } else {
3290 format64 |= arm_current_el(env) == 2;
3295 if (format64) {
3296 /* Create a 64-bit PAR */
3297 par64 = (1 << 11); /* LPAE bit always set */
3298 if (!ret) {
3299 par64 |= phys_addr & ~0xfffULL;
3300 if (!attrs.secure) {
3301 par64 |= (1 << 9); /* NS */
3303 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3304 par64 |= cacheattrs.shareability << 7; /* SH */
3305 } else {
3306 uint32_t fsr = arm_fi_to_lfsc(&fi);
3308 par64 |= 1; /* F */
3309 par64 |= (fsr & 0x3f) << 1; /* FS */
3310 if (fi.stage2) {
3311 par64 |= (1 << 9); /* S */
3313 if (fi.s1ptw) {
3314 par64 |= (1 << 8); /* PTW */
3317 } else {
3318 /* fsr is a DFSR/IFSR value for the short descriptor
3319 * translation table format (with WnR always clear).
3320 * Convert it to a 32-bit PAR.
3322 if (!ret) {
3323 /* We do not set any attribute bits in the PAR */
3324 if (page_size == (1 << 24)
3325 && arm_feature(env, ARM_FEATURE_V7)) {
3326 par64 = (phys_addr & 0xff000000) | (1 << 1);
3327 } else {
3328 par64 = phys_addr & 0xfffff000;
3330 if (!attrs.secure) {
3331 par64 |= (1 << 9); /* NS */
3333 } else {
3334 uint32_t fsr = arm_fi_to_sfsc(&fi);
3336 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3337 ((fsr & 0xf) << 1) | 1;
3340 return par64;
3342 #endif /* CONFIG_TCG */
3344 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3346 #ifdef CONFIG_TCG
3347 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3348 uint64_t par64;
3349 ARMMMUIdx mmu_idx;
3350 int el = arm_current_el(env);
3351 bool secure = arm_is_secure_below_el3(env);
3353 switch (ri->opc2 & 6) {
3354 case 0:
3355 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3356 switch (el) {
3357 case 3:
3358 mmu_idx = ARMMMUIdx_SE3;
3359 break;
3360 case 2:
3361 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
3362 /* fall through */
3363 case 1:
3364 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3365 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3366 : ARMMMUIdx_Stage1_E1_PAN);
3367 } else {
3368 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3370 break;
3371 default:
3372 g_assert_not_reached();
3374 break;
3375 case 2:
3376 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3377 switch (el) {
3378 case 3:
3379 mmu_idx = ARMMMUIdx_SE10_0;
3380 break;
3381 case 2:
3382 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
3383 mmu_idx = ARMMMUIdx_Stage1_E0;
3384 break;
3385 case 1:
3386 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3387 break;
3388 default:
3389 g_assert_not_reached();
3391 break;
3392 case 4:
3393 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3394 mmu_idx = ARMMMUIdx_E10_1;
3395 break;
3396 case 6:
3397 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3398 mmu_idx = ARMMMUIdx_E10_0;
3399 break;
3400 default:
3401 g_assert_not_reached();
3404 par64 = do_ats_write(env, value, access_type, mmu_idx);
3406 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3407 #else
3408 /* Handled by hardware accelerator. */
3409 g_assert_not_reached();
3410 #endif /* CONFIG_TCG */
3413 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3414 uint64_t value)
3416 #ifdef CONFIG_TCG
3417 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3418 uint64_t par64;
3420 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
3422 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3423 #else
3424 /* Handled by hardware accelerator. */
3425 g_assert_not_reached();
3426 #endif /* CONFIG_TCG */
3429 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3430 bool isread)
3432 if (arm_current_el(env) == 3 &&
3433 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3434 return CP_ACCESS_TRAP;
3436 return CP_ACCESS_OK;
3439 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3440 uint64_t value)
3442 #ifdef CONFIG_TCG
3443 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3444 ARMMMUIdx mmu_idx;
3445 int secure = arm_is_secure_below_el3(env);
3447 switch (ri->opc2 & 6) {
3448 case 0:
3449 switch (ri->opc1) {
3450 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3451 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3452 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
3453 : ARMMMUIdx_Stage1_E1_PAN);
3454 } else {
3455 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
3457 break;
3458 case 4: /* AT S1E2R, AT S1E2W */
3459 mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
3460 break;
3461 case 6: /* AT S1E3R, AT S1E3W */
3462 mmu_idx = ARMMMUIdx_SE3;
3463 break;
3464 default:
3465 g_assert_not_reached();
3467 break;
3468 case 2: /* AT S1E0R, AT S1E0W */
3469 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
3470 break;
3471 case 4: /* AT S12E1R, AT S12E1W */
3472 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
3473 break;
3474 case 6: /* AT S12E0R, AT S12E0W */
3475 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
3476 break;
3477 default:
3478 g_assert_not_reached();
3481 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
3482 #else
3483 /* Handled by hardware accelerator. */
3484 g_assert_not_reached();
3485 #endif /* CONFIG_TCG */
3487 #endif
3489 static const ARMCPRegInfo vapa_cp_reginfo[] = {
3490 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3491 .access = PL1_RW, .resetvalue = 0,
3492 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3493 offsetoflow32(CPUARMState, cp15.par_ns) },
3494 .writefn = par_write },
3495 #ifndef CONFIG_USER_ONLY
3496 /* This underdecoding is safe because the reginfo is NO_RAW. */
3497 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
3498 .access = PL1_W, .accessfn = ats_access,
3499 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
3500 #endif
3503 /* Return basic MPU access permission bits. */
3504 static uint32_t simple_mpu_ap_bits(uint32_t val)
3506 uint32_t ret;
3507 uint32_t mask;
3508 int i;
3509 ret = 0;
3510 mask = 3;
3511 for (i = 0; i < 16; i += 2) {
3512 ret |= (val >> i) & mask;
3513 mask <<= 2;
3515 return ret;
3518 /* Pad basic MPU access permission bits to extended format. */
3519 static uint32_t extended_mpu_ap_bits(uint32_t val)
3521 uint32_t ret;
3522 uint32_t mask;
3523 int i;
3524 ret = 0;
3525 mask = 3;
3526 for (i = 0; i < 16; i += 2) {
3527 ret |= (val & mask) << i;
3528 mask <<= 2;
3530 return ret;
3533 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3534 uint64_t value)
3536 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3539 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3541 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3544 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3545 uint64_t value)
3547 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3550 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3552 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3555 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3557 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3559 if (!u32p) {
3560 return 0;
3563 u32p += env->pmsav7.rnr[M_REG_NS];
3564 return *u32p;
3567 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3568 uint64_t value)
3570 ARMCPU *cpu = env_archcpu(env);
3571 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3573 if (!u32p) {
3574 return;
3577 u32p += env->pmsav7.rnr[M_REG_NS];
3578 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3579 *u32p = value;
3582 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3583 uint64_t value)
3585 ARMCPU *cpu = env_archcpu(env);
3586 uint32_t nrgs = cpu->pmsav7_dregion;
3588 if (value >= nrgs) {
3589 qemu_log_mask(LOG_GUEST_ERROR,
3590 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3591 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3592 return;
3595 raw_write(env, ri, value);
3598 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
3599 /* Reset for all these registers is handled in arm_cpu_reset(),
3600 * because the PMSAv7 is also used by M-profile CPUs, which do
3601 * not register cpregs but still need the state to be reset.
3603 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3604 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3605 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
3606 .readfn = pmsav7_read, .writefn = pmsav7_write,
3607 .resetfn = arm_cp_reset_ignore },
3608 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3609 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3610 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
3611 .readfn = pmsav7_read, .writefn = pmsav7_write,
3612 .resetfn = arm_cp_reset_ignore },
3613 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3614 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3615 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
3616 .readfn = pmsav7_read, .writefn = pmsav7_write,
3617 .resetfn = arm_cp_reset_ignore },
3618 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3619 .access = PL1_RW,
3620 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
3621 .writefn = pmsav7_rgnr_write,
3622 .resetfn = arm_cp_reset_ignore },
3625 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3626 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3627 .access = PL1_RW, .type = ARM_CP_ALIAS,
3628 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3629 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3630 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3631 .access = PL1_RW, .type = ARM_CP_ALIAS,
3632 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3633 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3634 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3635 .access = PL1_RW,
3636 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3637 .resetvalue = 0, },
3638 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3639 .access = PL1_RW,
3640 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3641 .resetvalue = 0, },
3642 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3643 .access = PL1_RW,
3644 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3645 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3646 .access = PL1_RW,
3647 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
3648 /* Protection region base and size registers */
3649 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3650 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3651 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3652 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3653 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3654 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3655 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3656 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3657 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3658 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3659 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3660 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3661 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3662 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3663 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3664 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3665 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3666 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3667 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3668 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3669 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3670 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3671 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3672 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
3675 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3676 uint64_t value)
3678 TCR *tcr = raw_ptr(env, ri);
3679 int maskshift = extract32(value, 0, 3);
3681 if (!arm_feature(env, ARM_FEATURE_V8)) {
3682 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3683 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3684 * using Long-desciptor translation table format */
3685 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3686 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3687 /* In an implementation that includes the Security Extensions
3688 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3689 * Short-descriptor translation table format.
3691 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3692 } else {
3693 value &= TTBCR_N;
3697 /* Update the masks corresponding to the TCR bank being written
3698 * Note that we always calculate mask and base_mask, but
3699 * they are only used for short-descriptor tables (ie if EAE is 0);
3700 * for long-descriptor tables the TCR fields are used differently
3701 * and the mask and base_mask values are meaningless.
3703 tcr->raw_tcr = value;
3704 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3705 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
3708 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3709 uint64_t value)
3711 ARMCPU *cpu = env_archcpu(env);
3712 TCR *tcr = raw_ptr(env, ri);
3714 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3715 /* With LPAE the TTBCR could result in a change of ASID
3716 * via the TTBCR.A1 bit, so do a TLB flush.
3718 tlb_flush(CPU(cpu));
3720 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3721 value = deposit64(tcr->raw_tcr, 0, 32, value);
3722 vmsa_ttbcr_raw_write(env, ri, value);
3725 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3727 TCR *tcr = raw_ptr(env, ri);
3729 /* Reset both the TCR as well as the masks corresponding to the bank of
3730 * the TCR being reset.
3732 tcr->raw_tcr = 0;
3733 tcr->mask = 0;
3734 tcr->base_mask = 0xffffc000u;
3737 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
3738 uint64_t value)
3740 ARMCPU *cpu = env_archcpu(env);
3741 TCR *tcr = raw_ptr(env, ri);
3743 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
3744 tlb_flush(CPU(cpu));
3745 tcr->raw_tcr = value;
3748 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3749 uint64_t value)
3751 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3752 if (cpreg_field_is_64bit(ri) &&
3753 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
3754 ARMCPU *cpu = env_archcpu(env);
3755 tlb_flush(CPU(cpu));
3757 raw_write(env, ri, value);
3760 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3761 uint64_t value)
3764 * If we are running with E2&0 regime, then an ASID is active.
3765 * Flush if that might be changing. Note we're not checking
3766 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3767 * holds the active ASID, only checking the field that might.
3769 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3770 (arm_hcr_el2_eff(env) & HCR_E2H)) {
3771 uint16_t mask = ARMMMUIdxBit_E20_2 |
3772 ARMMMUIdxBit_E20_2_PAN |
3773 ARMMMUIdxBit_E20_0;
3775 if (arm_is_secure_below_el3(env)) {
3776 mask >>= ARM_MMU_IDX_A_NS;
3779 tlb_flush_by_mmuidx(env_cpu(env), mask);
3781 raw_write(env, ri, value);
3784 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3785 uint64_t value)
3787 ARMCPU *cpu = env_archcpu(env);
3788 CPUState *cs = CPU(cpu);
3791 * A change in VMID to the stage2 page table (Stage2) invalidates
3792 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3794 if (raw_read(env, ri) != value) {
3795 uint16_t mask = ARMMMUIdxBit_E10_1 |
3796 ARMMMUIdxBit_E10_1_PAN |
3797 ARMMMUIdxBit_E10_0;
3799 if (arm_is_secure_below_el3(env)) {
3800 mask >>= ARM_MMU_IDX_A_NS;
3803 tlb_flush_by_mmuidx(cs, mask);
3804 raw_write(env, ri, value);
3808 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
3809 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
3810 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
3811 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
3812 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
3813 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
3814 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3815 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3816 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
3817 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
3818 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
3819 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3820 offsetof(CPUARMState, cp15.dfar_ns) } },
3821 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3822 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
3823 .access = PL1_RW, .accessfn = access_tvm_trvm,
3824 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
3825 .resetvalue = 0, },
3828 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
3829 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3830 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
3831 .access = PL1_RW, .accessfn = access_tvm_trvm,
3832 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
3833 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
3834 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
3835 .access = PL1_RW, .accessfn = access_tvm_trvm,
3836 .writefn = vmsa_ttbr_write, .resetvalue = 0,
3837 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3838 offsetof(CPUARMState, cp15.ttbr0_ns) } },
3839 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
3840 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
3841 .access = PL1_RW, .accessfn = access_tvm_trvm,
3842 .writefn = vmsa_ttbr_write, .resetvalue = 0,
3843 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3844 offsetof(CPUARMState, cp15.ttbr1_ns) } },
3845 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3846 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3847 .access = PL1_RW, .accessfn = access_tvm_trvm,
3848 .writefn = vmsa_tcr_el12_write,
3849 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3850 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
3851 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
3852 .access = PL1_RW, .accessfn = access_tvm_trvm,
3853 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
3854 .raw_writefn = vmsa_ttbcr_raw_write,
3855 /* No offsetoflow32 -- pass the entire TCR to writefn/raw_writefn. */
3856 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.tcr_el[3]),
3857 offsetof(CPUARMState, cp15.tcr_el[1])} },
3860 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3861 * qemu tlbs nor adjusting cached masks.
3863 static const ARMCPRegInfo ttbcr2_reginfo = {
3864 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
3865 .access = PL1_RW, .accessfn = access_tvm_trvm,
3866 .type = ARM_CP_ALIAS,
3867 .bank_fieldoffsets = {
3868 offsetofhigh32(CPUARMState, cp15.tcr_el[3].raw_tcr),
3869 offsetofhigh32(CPUARMState, cp15.tcr_el[1].raw_tcr),
3873 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3874 uint64_t value)
3876 env->cp15.c15_ticonfig = value & 0xe7;
3877 /* The OS_TYPE bit in this register changes the reported CPUID! */
3878 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3879 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
3882 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3883 uint64_t value)
3885 env->cp15.c15_threadid = value & 0xffff;
3888 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3889 uint64_t value)
3891 /* Wait-for-interrupt (deprecated) */
3892 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
3895 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3896 uint64_t value)
3898 /* On OMAP there are registers indicating the max/min index of dcache lines
3899 * containing a dirty line; cache flush operations have to reset these.
3901 env->cp15.c15_i_max = 0x000;
3902 env->cp15.c15_i_min = 0xff0;
3905 static const ARMCPRegInfo omap_cp_reginfo[] = {
3906 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3907 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
3908 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
3909 .resetvalue = 0, },
3910 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3911 .access = PL1_RW, .type = ARM_CP_NOP },
3912 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3913 .access = PL1_RW,
3914 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3915 .writefn = omap_ticonfig_write },
3916 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3917 .access = PL1_RW,
3918 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3919 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3920 .access = PL1_RW, .resetvalue = 0xff0,
3921 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3922 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3923 .access = PL1_RW,
3924 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3925 .writefn = omap_threadid_write },
3926 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3927 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3928 .type = ARM_CP_NO_RAW,
3929 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3930 /* TODO: Peripheral port remap register:
3931 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3932 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3933 * when MMU is off.
3935 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
3936 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
3937 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
3938 .writefn = omap_cachemaint_write },
3939 { .name = "C9", .cp = 15, .crn = 9,
3940 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3941 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
3944 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3945 uint64_t value)
3947 env->cp15.c15_cpar = value & 0x3fff;
3950 static const ARMCPRegInfo xscale_cp_reginfo[] = {
3951 { .name = "XSCALE_CPAR",
3952 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3953 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3954 .writefn = xscale_cpar_write, },
3955 { .name = "XSCALE_AUXCR",
3956 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3957 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3958 .resetvalue = 0, },
3959 /* XScale specific cache-lockdown: since we have no cache we NOP these
3960 * and hope the guest does not really rely on cache behaviour.
3962 { .name = "XSCALE_LOCK_ICACHE_LINE",
3963 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3964 .access = PL1_W, .type = ARM_CP_NOP },
3965 { .name = "XSCALE_UNLOCK_ICACHE",
3966 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3967 .access = PL1_W, .type = ARM_CP_NOP },
3968 { .name = "XSCALE_DCACHE_LOCK",
3969 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3970 .access = PL1_RW, .type = ARM_CP_NOP },
3971 { .name = "XSCALE_UNLOCK_DCACHE",
3972 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3973 .access = PL1_W, .type = ARM_CP_NOP },
3976 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3977 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3978 * implementation of this implementation-defined space.
3979 * Ideally this should eventually disappear in favour of actually
3980 * implementing the correct behaviour for all cores.
3982 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3983 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3984 .access = PL1_RW,
3985 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
3986 .resetvalue = 0 },
3989 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3990 /* Cache status: RAZ because we have no cache so it's always clean */
3991 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
3992 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
3993 .resetvalue = 0 },
3996 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3997 /* We never have a a block transfer operation in progress */
3998 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
3999 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4000 .resetvalue = 0 },
4001 /* The cache ops themselves: these all NOP for QEMU */
4002 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4003 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4004 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4005 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4006 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4007 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4008 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4009 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4010 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4011 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4012 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4013 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4016 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4017 /* The cache test-and-clean instructions always return (1 << 30)
4018 * to indicate that there are no dirty cache lines.
4020 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4021 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4022 .resetvalue = (1 << 30) },
4023 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4024 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4025 .resetvalue = (1 << 30) },
4028 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4029 /* Ignore ReadBuffer accesses */
4030 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4031 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4032 .access = PL1_RW, .resetvalue = 0,
4033 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4036 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4038 unsigned int cur_el = arm_current_el(env);
4040 if (arm_is_el2_enabled(env) && cur_el == 1) {
4041 return env->cp15.vpidr_el2;
4043 return raw_read(env, ri);
4046 static uint64_t mpidr_read_val(CPUARMState *env)
4048 ARMCPU *cpu = env_archcpu(env);
4049 uint64_t mpidr = cpu->mp_affinity;
4051 if (arm_feature(env, ARM_FEATURE_V7MP)) {
4052 mpidr |= (1U << 31);
4053 /* Cores which are uniprocessor (non-coherent)
4054 * but still implement the MP extensions set
4055 * bit 30. (For instance, Cortex-R5).
4057 if (cpu->mp_is_up) {
4058 mpidr |= (1u << 30);
4061 return mpidr;
4064 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4066 unsigned int cur_el = arm_current_el(env);
4068 if (arm_is_el2_enabled(env) && cur_el == 1) {
4069 return env->cp15.vmpidr_el2;
4071 return mpidr_read_val(env);
4074 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4075 /* NOP AMAIR0/1 */
4076 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4077 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4078 .access = PL1_RW, .accessfn = access_tvm_trvm,
4079 .type = ARM_CP_CONST, .resetvalue = 0 },
4080 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4081 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4082 .access = PL1_RW, .accessfn = access_tvm_trvm,
4083 .type = ARM_CP_CONST, .resetvalue = 0 },
4084 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4085 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4086 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4087 offsetof(CPUARMState, cp15.par_ns)} },
4088 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4089 .access = PL1_RW, .accessfn = access_tvm_trvm,
4090 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4091 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4092 offsetof(CPUARMState, cp15.ttbr0_ns) },
4093 .writefn = vmsa_ttbr_write, },
4094 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4095 .access = PL1_RW, .accessfn = access_tvm_trvm,
4096 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4097 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4098 offsetof(CPUARMState, cp15.ttbr1_ns) },
4099 .writefn = vmsa_ttbr_write, },
4102 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4104 return vfp_get_fpcr(env);
4107 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4108 uint64_t value)
4110 vfp_set_fpcr(env, value);
4113 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4115 return vfp_get_fpsr(env);
4118 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4119 uint64_t value)
4121 vfp_set_fpsr(env, value);
4124 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4125 bool isread)
4127 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4128 return CP_ACCESS_TRAP;
4130 return CP_ACCESS_OK;
4133 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4134 uint64_t value)
4136 env->daif = value & PSTATE_DAIF;
4139 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4141 return env->pstate & PSTATE_PAN;
4144 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4145 uint64_t value)
4147 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4150 static const ARMCPRegInfo pan_reginfo = {
4151 .name = "PAN", .state = ARM_CP_STATE_AA64,
4152 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4153 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4154 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4157 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4159 return env->pstate & PSTATE_UAO;
4162 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4163 uint64_t value)
4165 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4168 static const ARMCPRegInfo uao_reginfo = {
4169 .name = "UAO", .state = ARM_CP_STATE_AA64,
4170 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4171 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4172 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4175 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4177 return env->pstate & PSTATE_DIT;
4180 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4181 uint64_t value)
4183 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4186 static const ARMCPRegInfo dit_reginfo = {
4187 .name = "DIT", .state = ARM_CP_STATE_AA64,
4188 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4189 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4190 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4193 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4195 return env->pstate & PSTATE_SSBS;
4198 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4199 uint64_t value)
4201 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4204 static const ARMCPRegInfo ssbs_reginfo = {
4205 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4206 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4207 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4208 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4211 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4212 const ARMCPRegInfo *ri,
4213 bool isread)
4215 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4216 switch (arm_current_el(env)) {
4217 case 0:
4218 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4219 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4220 return CP_ACCESS_TRAP;
4222 /* fall through */
4223 case 1:
4224 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4225 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4226 return CP_ACCESS_TRAP_EL2;
4228 break;
4230 return CP_ACCESS_OK;
4233 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4234 const ARMCPRegInfo *ri,
4235 bool isread)
4237 /* Cache invalidate/clean to Point of Unification... */
4238 switch (arm_current_el(env)) {
4239 case 0:
4240 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4241 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4242 return CP_ACCESS_TRAP;
4244 /* fall through */
4245 case 1:
4246 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4247 if (arm_hcr_el2_eff(env) & HCR_TPU) {
4248 return CP_ACCESS_TRAP_EL2;
4250 break;
4252 return CP_ACCESS_OK;
4255 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4256 * Page D4-1736 (DDI0487A.b)
4259 static int vae1_tlbmask(CPUARMState *env)
4261 uint64_t hcr = arm_hcr_el2_eff(env);
4262 uint16_t mask;
4264 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4265 mask = ARMMMUIdxBit_E20_2 |
4266 ARMMMUIdxBit_E20_2_PAN |
4267 ARMMMUIdxBit_E20_0;
4268 } else {
4269 mask = ARMMMUIdxBit_E10_1 |
4270 ARMMMUIdxBit_E10_1_PAN |
4271 ARMMMUIdxBit_E10_0;
4274 if (arm_is_secure_below_el3(env)) {
4275 mask >>= ARM_MMU_IDX_A_NS;
4278 return mask;
4281 /* Return 56 if TBI is enabled, 64 otherwise. */
4282 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4283 uint64_t addr)
4285 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
4286 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4287 int select = extract64(addr, 55, 1);
4289 return (tbi >> select) & 1 ? 56 : 64;
4292 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4294 uint64_t hcr = arm_hcr_el2_eff(env);
4295 ARMMMUIdx mmu_idx;
4297 /* Only the regime of the mmu_idx below is significant. */
4298 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4299 mmu_idx = ARMMMUIdx_E20_0;
4300 } else {
4301 mmu_idx = ARMMMUIdx_E10_0;
4304 if (arm_is_secure_below_el3(env)) {
4305 mmu_idx &= ~ARM_MMU_IDX_A_NS;
4308 return tlbbits_for_regime(env, mmu_idx, addr);
4311 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4312 uint64_t value)
4314 CPUState *cs = env_cpu(env);
4315 int mask = vae1_tlbmask(env);
4317 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4320 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4321 uint64_t value)
4323 CPUState *cs = env_cpu(env);
4324 int mask = vae1_tlbmask(env);
4326 if (tlb_force_broadcast(env)) {
4327 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4328 } else {
4329 tlb_flush_by_mmuidx(cs, mask);
4333 static int alle1_tlbmask(CPUARMState *env)
4336 * Note that the 'ALL' scope must invalidate both stage 1 and
4337 * stage 2 translations, whereas most other scopes only invalidate
4338 * stage 1 translations.
4340 if (arm_is_secure_below_el3(env)) {
4341 return ARMMMUIdxBit_SE10_1 |
4342 ARMMMUIdxBit_SE10_1_PAN |
4343 ARMMMUIdxBit_SE10_0;
4344 } else {
4345 return ARMMMUIdxBit_E10_1 |
4346 ARMMMUIdxBit_E10_1_PAN |
4347 ARMMMUIdxBit_E10_0;
4351 static int e2_tlbmask(CPUARMState *env)
4353 if (arm_is_secure_below_el3(env)) {
4354 return ARMMMUIdxBit_SE20_0 |
4355 ARMMMUIdxBit_SE20_2 |
4356 ARMMMUIdxBit_SE20_2_PAN |
4357 ARMMMUIdxBit_SE2;
4358 } else {
4359 return ARMMMUIdxBit_E20_0 |
4360 ARMMMUIdxBit_E20_2 |
4361 ARMMMUIdxBit_E20_2_PAN |
4362 ARMMMUIdxBit_E2;
4366 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4367 uint64_t value)
4369 CPUState *cs = env_cpu(env);
4370 int mask = alle1_tlbmask(env);
4372 tlb_flush_by_mmuidx(cs, mask);
4375 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4376 uint64_t value)
4378 CPUState *cs = env_cpu(env);
4379 int mask = e2_tlbmask(env);
4381 tlb_flush_by_mmuidx(cs, mask);
4384 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4385 uint64_t value)
4387 ARMCPU *cpu = env_archcpu(env);
4388 CPUState *cs = CPU(cpu);
4390 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
4393 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4394 uint64_t value)
4396 CPUState *cs = env_cpu(env);
4397 int mask = alle1_tlbmask(env);
4399 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4402 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4403 uint64_t value)
4405 CPUState *cs = env_cpu(env);
4406 int mask = e2_tlbmask(env);
4408 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4411 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4412 uint64_t value)
4414 CPUState *cs = env_cpu(env);
4416 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
4419 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4420 uint64_t value)
4422 /* Invalidate by VA, EL2
4423 * Currently handles both VAE2 and VALE2, since we don't support
4424 * flush-last-level-only.
4426 CPUState *cs = env_cpu(env);
4427 int mask = e2_tlbmask(env);
4428 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4430 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4433 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4434 uint64_t value)
4436 /* Invalidate by VA, EL3
4437 * Currently handles both VAE3 and VALE3, since we don't support
4438 * flush-last-level-only.
4440 ARMCPU *cpu = env_archcpu(env);
4441 CPUState *cs = CPU(cpu);
4442 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4444 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
4447 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4448 uint64_t value)
4450 CPUState *cs = env_cpu(env);
4451 int mask = vae1_tlbmask(env);
4452 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4453 int bits = vae1_tlbbits(env, pageaddr);
4455 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4458 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4459 uint64_t value)
4461 /* Invalidate by VA, EL1&0 (AArch64 version).
4462 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4463 * since we don't support flush-for-specific-ASID-only or
4464 * flush-last-level-only.
4466 CPUState *cs = env_cpu(env);
4467 int mask = vae1_tlbmask(env);
4468 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4469 int bits = vae1_tlbbits(env, pageaddr);
4471 if (tlb_force_broadcast(env)) {
4472 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4473 } else {
4474 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4478 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4479 uint64_t value)
4481 CPUState *cs = env_cpu(env);
4482 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4483 bool secure = arm_is_secure_below_el3(env);
4484 int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
4485 int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
4486 pageaddr);
4488 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4491 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4492 uint64_t value)
4494 CPUState *cs = env_cpu(env);
4495 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4496 int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
4498 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4499 ARMMMUIdxBit_SE3, bits);
4502 #ifdef TARGET_AARCH64
4503 typedef struct {
4504 uint64_t base;
4505 uint64_t length;
4506 } TLBIRange;
4508 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4509 uint64_t value)
4511 unsigned int page_size_granule, page_shift, num, scale, exponent;
4512 /* Extract one bit to represent the va selector in use. */
4513 uint64_t select = sextract64(value, 36, 1);
4514 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
4515 TLBIRange ret = { };
4517 page_size_granule = extract64(value, 46, 2);
4519 /* The granule encoded in value must match the granule in use. */
4520 if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4521 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
4522 page_size_granule);
4523 return ret;
4526 page_shift = (page_size_granule - 1) * 2 + 12;
4527 num = extract64(value, 39, 5);
4528 scale = extract64(value, 44, 2);
4529 exponent = (5 * scale) + 1;
4531 ret.length = (num + 1) << (exponent + page_shift);
4533 if (param.select) {
4534 ret.base = sextract64(value, 0, 37);
4535 } else {
4536 ret.base = extract64(value, 0, 37);
4538 if (param.ds) {
4540 * With DS=1, BaseADDR is always shifted 16 so that it is able
4541 * to address all 52 va bits. The input address is perforce
4542 * aligned on a 64k boundary regardless of translation granule.
4544 page_shift = 16;
4546 ret.base <<= page_shift;
4548 return ret;
4551 static void do_rvae_write(CPUARMState *env, uint64_t value,
4552 int idxmap, bool synced)
4554 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
4555 TLBIRange range;
4556 int bits;
4558 range = tlbi_aa64_get_range(env, one_idx, value);
4559 bits = tlbbits_for_regime(env, one_idx, range.base);
4561 if (synced) {
4562 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
4563 range.base,
4564 range.length,
4565 idxmap,
4566 bits);
4567 } else {
4568 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4569 range.length, idxmap, bits);
4573 static void tlbi_aa64_rvae1_write(CPUARMState *env,
4574 const ARMCPRegInfo *ri,
4575 uint64_t value)
4578 * Invalidate by VA range, EL1&0.
4579 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4580 * since we don't support flush-for-specific-ASID-only or
4581 * flush-last-level-only.
4584 do_rvae_write(env, value, vae1_tlbmask(env),
4585 tlb_force_broadcast(env));
4588 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4589 const ARMCPRegInfo *ri,
4590 uint64_t value)
4593 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4594 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4595 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4596 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4597 * shareable specific flushes.
4600 do_rvae_write(env, value, vae1_tlbmask(env), true);
4603 static int vae2_tlbmask(CPUARMState *env)
4605 return (arm_is_secure_below_el3(env)
4606 ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4609 static void tlbi_aa64_rvae2_write(CPUARMState *env,
4610 const ARMCPRegInfo *ri,
4611 uint64_t value)
4614 * Invalidate by VA range, EL2.
4615 * Currently handles all of RVAE2 and RVALE2,
4616 * since we don't support flush-for-specific-ASID-only or
4617 * flush-last-level-only.
4620 do_rvae_write(env, value, vae2_tlbmask(env),
4621 tlb_force_broadcast(env));
4626 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4627 const ARMCPRegInfo *ri,
4628 uint64_t value)
4631 * Invalidate by VA range, Inner/Outer Shareable, EL2.
4632 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4633 * since we don't support flush-for-specific-ASID-only,
4634 * flush-last-level-only or inner/outer shareable specific flushes.
4637 do_rvae_write(env, value, vae2_tlbmask(env), true);
4641 static void tlbi_aa64_rvae3_write(CPUARMState *env,
4642 const ARMCPRegInfo *ri,
4643 uint64_t value)
4646 * Invalidate by VA range, EL3.
4647 * Currently handles all of RVAE3 and RVALE3,
4648 * since we don't support flush-for-specific-ASID-only or
4649 * flush-last-level-only.
4652 do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4653 tlb_force_broadcast(env));
4656 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4657 const ARMCPRegInfo *ri,
4658 uint64_t value)
4661 * Invalidate by VA range, EL3, Inner/Outer Shareable.
4662 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4663 * since we don't support flush-for-specific-ASID-only,
4664 * flush-last-level-only or inner/outer specific flushes.
4667 do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4669 #endif
4671 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4672 bool isread)
4674 int cur_el = arm_current_el(env);
4676 if (cur_el < 2) {
4677 uint64_t hcr = arm_hcr_el2_eff(env);
4679 if (cur_el == 0) {
4680 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4681 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4682 return CP_ACCESS_TRAP_EL2;
4684 } else {
4685 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4686 return CP_ACCESS_TRAP;
4688 if (hcr & HCR_TDZ) {
4689 return CP_ACCESS_TRAP_EL2;
4692 } else if (hcr & HCR_TDZ) {
4693 return CP_ACCESS_TRAP_EL2;
4696 return CP_ACCESS_OK;
4699 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4701 ARMCPU *cpu = env_archcpu(env);
4702 int dzp_bit = 1 << 4;
4704 /* DZP indicates whether DC ZVA access is allowed */
4705 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4706 dzp_bit = 0;
4708 return cpu->dcz_blocksize | dzp_bit;
4711 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4712 bool isread)
4714 if (!(env->pstate & PSTATE_SP)) {
4715 /* Access to SP_EL0 is undefined if it's being used as
4716 * the stack pointer.
4718 return CP_ACCESS_TRAP_UNCATEGORIZED;
4720 return CP_ACCESS_OK;
4723 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4725 return env->pstate & PSTATE_SP;
4728 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4730 update_spsel(env, val);
4733 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4734 uint64_t value)
4736 ARMCPU *cpu = env_archcpu(env);
4738 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4739 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4740 value &= ~SCTLR_M;
4743 /* ??? Lots of these bits are not implemented. */
4745 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4746 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4747 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4748 } else {
4749 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4750 SCTLR_ATA0 | SCTLR_ATA);
4754 if (raw_read(env, ri) == value) {
4755 /* Skip the TLB flush if nothing actually changed; Linux likes
4756 * to do a lot of pointless SCTLR writes.
4758 return;
4761 raw_write(env, ri, value);
4763 /* This may enable/disable the MMU, so do a TLB flush. */
4764 tlb_flush(CPU(cpu));
4766 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4768 * Normally we would always end the TB on an SCTLR write; see the
4769 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4770 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4771 * of hflags from the translator, so do it here.
4773 arm_rebuild_hflags(env);
4777 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4778 uint64_t value)
4780 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4783 static const ARMCPRegInfo v8_cp_reginfo[] = {
4784 /* Minimal set of EL0-visible registers. This will need to be expanded
4785 * significantly for system emulation of AArch64 CPUs.
4787 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4788 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4789 .access = PL0_RW, .type = ARM_CP_NZCV },
4790 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4791 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
4792 .type = ARM_CP_NO_RAW,
4793 .access = PL0_RW, .accessfn = aa64_daif_access,
4794 .fieldoffset = offsetof(CPUARMState, daif),
4795 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
4796 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4797 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
4798 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4799 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
4800 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4801 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
4802 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
4803 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
4804 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4805 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
4806 .access = PL0_R, .type = ARM_CP_NO_RAW,
4807 .readfn = aa64_dczid_read },
4808 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4809 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4810 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4811 #ifndef CONFIG_USER_ONLY
4812 /* Avoid overhead of an access check that always passes in user-mode */
4813 .accessfn = aa64_zva_access,
4814 #endif
4816 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4817 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4818 .access = PL1_R, .type = ARM_CP_CURRENTEL },
4819 /* Cache ops: all NOPs since we don't emulate caches */
4820 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4821 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
4822 .access = PL1_W, .type = ARM_CP_NOP,
4823 .accessfn = aa64_cacheop_pou_access },
4824 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4825 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
4826 .access = PL1_W, .type = ARM_CP_NOP,
4827 .accessfn = aa64_cacheop_pou_access },
4828 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4829 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4830 .access = PL0_W, .type = ARM_CP_NOP,
4831 .accessfn = aa64_cacheop_pou_access },
4832 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4833 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
4834 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4835 .type = ARM_CP_NOP },
4836 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4837 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
4838 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4839 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4840 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4841 .access = PL0_W, .type = ARM_CP_NOP,
4842 .accessfn = aa64_cacheop_poc_access },
4843 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4844 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
4845 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4846 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4847 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4848 .access = PL0_W, .type = ARM_CP_NOP,
4849 .accessfn = aa64_cacheop_pou_access },
4850 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4851 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4852 .access = PL0_W, .type = ARM_CP_NOP,
4853 .accessfn = aa64_cacheop_poc_access },
4854 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4855 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
4856 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
4857 /* TLBI operations */
4858 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
4859 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
4860 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4861 .writefn = tlbi_aa64_vmalle1is_write },
4862 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
4863 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
4864 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4865 .writefn = tlbi_aa64_vae1is_write },
4866 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
4867 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
4868 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4869 .writefn = tlbi_aa64_vmalle1is_write },
4870 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
4871 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
4872 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4873 .writefn = tlbi_aa64_vae1is_write },
4874 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
4875 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4876 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4877 .writefn = tlbi_aa64_vae1is_write },
4878 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
4879 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4880 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4881 .writefn = tlbi_aa64_vae1is_write },
4882 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
4883 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
4884 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4885 .writefn = tlbi_aa64_vmalle1_write },
4886 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
4887 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
4888 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4889 .writefn = tlbi_aa64_vae1_write },
4890 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
4891 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
4892 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4893 .writefn = tlbi_aa64_vmalle1_write },
4894 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
4895 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
4896 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4897 .writefn = tlbi_aa64_vae1_write },
4898 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
4899 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4900 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4901 .writefn = tlbi_aa64_vae1_write },
4902 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
4903 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4904 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
4905 .writefn = tlbi_aa64_vae1_write },
4906 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4907 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
4908 .access = PL2_W, .type = ARM_CP_NOP },
4909 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4910 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
4911 .access = PL2_W, .type = ARM_CP_NOP },
4912 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4913 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4914 .access = PL2_W, .type = ARM_CP_NO_RAW,
4915 .writefn = tlbi_aa64_alle1is_write },
4916 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4917 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4918 .access = PL2_W, .type = ARM_CP_NO_RAW,
4919 .writefn = tlbi_aa64_alle1is_write },
4920 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4921 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
4922 .access = PL2_W, .type = ARM_CP_NOP },
4923 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4924 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
4925 .access = PL2_W, .type = ARM_CP_NOP },
4926 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4927 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4928 .access = PL2_W, .type = ARM_CP_NO_RAW,
4929 .writefn = tlbi_aa64_alle1_write },
4930 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4931 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4932 .access = PL2_W, .type = ARM_CP_NO_RAW,
4933 .writefn = tlbi_aa64_alle1is_write },
4934 #ifndef CONFIG_USER_ONLY
4935 /* 64 bit address translation operations */
4936 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4937 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
4938 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4939 .writefn = ats_write64 },
4940 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4941 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
4942 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4943 .writefn = ats_write64 },
4944 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4945 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
4946 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4947 .writefn = ats_write64 },
4948 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4949 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
4950 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4951 .writefn = ats_write64 },
4952 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
4953 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
4954 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4955 .writefn = ats_write64 },
4956 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
4957 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
4958 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4959 .writefn = ats_write64 },
4960 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
4961 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
4962 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4963 .writefn = ats_write64 },
4964 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
4965 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
4966 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4967 .writefn = ats_write64 },
4968 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4969 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4970 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
4971 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4972 .writefn = ats_write64 },
4973 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4974 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
4975 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4976 .writefn = ats_write64 },
4977 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4978 .type = ARM_CP_ALIAS,
4979 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4980 .access = PL1_RW, .resetvalue = 0,
4981 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4982 .writefn = par_write },
4983 #endif
4984 /* TLB invalidate last level of translation table walk */
4985 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
4986 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4987 .writefn = tlbimva_is_write },
4988 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
4989 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4990 .writefn = tlbimvaa_is_write },
4991 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
4992 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4993 .writefn = tlbimva_write },
4994 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
4995 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4996 .writefn = tlbimvaa_write },
4997 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4998 .type = ARM_CP_NO_RAW, .access = PL2_W,
4999 .writefn = tlbimva_hyp_write },
5000 { .name = "TLBIMVALHIS",
5001 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5002 .type = ARM_CP_NO_RAW, .access = PL2_W,
5003 .writefn = tlbimva_hyp_is_write },
5004 { .name = "TLBIIPAS2",
5005 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5006 .type = ARM_CP_NOP, .access = PL2_W },
5007 { .name = "TLBIIPAS2IS",
5008 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5009 .type = ARM_CP_NOP, .access = PL2_W },
5010 { .name = "TLBIIPAS2L",
5011 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5012 .type = ARM_CP_NOP, .access = PL2_W },
5013 { .name = "TLBIIPAS2LIS",
5014 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5015 .type = ARM_CP_NOP, .access = PL2_W },
5016 /* 32 bit cache operations */
5017 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5018 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5019 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5020 .type = ARM_CP_NOP, .access = PL1_W },
5021 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5022 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5023 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5024 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5025 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5026 .type = ARM_CP_NOP, .access = PL1_W },
5027 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5028 .type = ARM_CP_NOP, .access = PL1_W },
5029 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5030 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5031 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5032 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5033 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5034 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5035 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5036 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5037 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5038 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
5039 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5040 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5041 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5042 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5043 /* MMU Domain access control / MPU write buffer control */
5044 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5045 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5046 .writefn = dacr_write, .raw_writefn = raw_write,
5047 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5048 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5049 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5050 .type = ARM_CP_ALIAS,
5051 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5052 .access = PL1_RW,
5053 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5054 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5055 .type = ARM_CP_ALIAS,
5056 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5057 .access = PL1_RW,
5058 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5059 /* We rely on the access checks not allowing the guest to write to the
5060 * state field when SPSel indicates that it's being used as the stack
5061 * pointer.
5063 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5064 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5065 .access = PL1_RW, .accessfn = sp_el0_access,
5066 .type = ARM_CP_ALIAS,
5067 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5068 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5069 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5070 .access = PL2_RW, .type = ARM_CP_ALIAS,
5071 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5072 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5073 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5074 .type = ARM_CP_NO_RAW,
5075 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5076 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5077 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5078 .access = PL2_RW,
5079 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5080 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5081 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5082 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5083 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5084 .writefn = dacr_write, .raw_writefn = raw_write,
5085 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5086 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5087 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5088 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5089 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5090 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5091 .type = ARM_CP_ALIAS,
5092 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5093 .access = PL2_RW,
5094 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5095 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5096 .type = ARM_CP_ALIAS,
5097 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5098 .access = PL2_RW,
5099 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5100 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5101 .type = ARM_CP_ALIAS,
5102 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5103 .access = PL2_RW,
5104 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5105 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5106 .type = ARM_CP_ALIAS,
5107 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5108 .access = PL2_RW,
5109 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5110 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5111 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5112 .resetvalue = 0,
5113 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5114 { .name = "SDCR", .type = ARM_CP_ALIAS,
5115 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5116 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5117 .writefn = sdcr_write,
5118 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5121 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5123 ARMCPU *cpu = env_archcpu(env);
5125 if (arm_feature(env, ARM_FEATURE_V8)) {
5126 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5127 } else {
5128 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5131 if (arm_feature(env, ARM_FEATURE_EL3)) {
5132 valid_mask &= ~HCR_HCD;
5133 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5134 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5135 * However, if we're using the SMC PSCI conduit then QEMU is
5136 * effectively acting like EL3 firmware and so the guest at
5137 * EL2 should retain the ability to prevent EL1 from being
5138 * able to make SMC calls into the ersatz firmware, so in
5139 * that case HCR.TSC should be read/write.
5141 valid_mask &= ~HCR_TSC;
5144 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5145 if (cpu_isar_feature(aa64_vh, cpu)) {
5146 valid_mask |= HCR_E2H;
5148 if (cpu_isar_feature(aa64_ras, cpu)) {
5149 valid_mask |= HCR_TERR | HCR_TEA;
5151 if (cpu_isar_feature(aa64_lor, cpu)) {
5152 valid_mask |= HCR_TLOR;
5154 if (cpu_isar_feature(aa64_pauth, cpu)) {
5155 valid_mask |= HCR_API | HCR_APK;
5157 if (cpu_isar_feature(aa64_mte, cpu)) {
5158 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5160 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5161 valid_mask |= HCR_ENSCXT;
5163 if (cpu_isar_feature(aa64_fwb, cpu)) {
5164 valid_mask |= HCR_FWB;
5168 /* Clear RES0 bits. */
5169 value &= valid_mask;
5172 * These bits change the MMU setup:
5173 * HCR_VM enables stage 2 translation
5174 * HCR_PTW forbids certain page-table setups
5175 * HCR_DC disables stage1 and enables stage2 translation
5176 * HCR_DCT enables tagging on (disabled) stage1 translation
5177 * HCR_FWB changes the interpretation of stage2 descriptor bits
5179 if ((env->cp15.hcr_el2 ^ value) &
5180 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
5181 tlb_flush(CPU(cpu));
5183 env->cp15.hcr_el2 = value;
5186 * Updates to VI and VF require us to update the status of
5187 * virtual interrupts, which are the logical OR of these bits
5188 * and the state of the input lines from the GIC. (This requires
5189 * that we have the iothread lock, which is done by marking the
5190 * reginfo structs as ARM_CP_IO.)
5191 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5192 * possible for it to be taken immediately, because VIRQ and
5193 * VFIQ are masked unless running at EL0 or EL1, and HCR
5194 * can only be written at EL2.
5196 g_assert(qemu_mutex_iothread_locked());
5197 arm_cpu_update_virq(cpu);
5198 arm_cpu_update_vfiq(cpu);
5199 arm_cpu_update_vserr(cpu);
5202 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5204 do_hcr_write(env, value, 0);
5207 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5208 uint64_t value)
5210 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5211 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5212 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5215 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5216 uint64_t value)
5218 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5219 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5220 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5224 * Return the effective value of HCR_EL2.
5225 * Bits that are not included here:
5226 * RW (read from SCR_EL3.RW as needed)
5228 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5230 uint64_t ret = env->cp15.hcr_el2;
5232 if (!arm_is_el2_enabled(env)) {
5234 * "This register has no effect if EL2 is not enabled in the
5235 * current Security state". This is ARMv8.4-SecEL2 speak for
5236 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5238 * Prior to that, the language was "In an implementation that
5239 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5240 * as if this field is 0 for all purposes other than a direct
5241 * read or write access of HCR_EL2". With lots of enumeration
5242 * on a per-field basis. In current QEMU, this is condition
5243 * is arm_is_secure_below_el3.
5245 * Since the v8.4 language applies to the entire register, and
5246 * appears to be backward compatible, use that.
5248 return 0;
5252 * For a cpu that supports both aarch64 and aarch32, we can set bits
5253 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5254 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5256 if (!arm_el_is_aa64(env, 2)) {
5257 uint64_t aa32_valid;
5260 * These bits are up-to-date as of ARMv8.6.
5261 * For HCR, it's easiest to list just the 2 bits that are invalid.
5262 * For HCR2, list those that are valid.
5264 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5265 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5266 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5267 ret &= aa32_valid;
5270 if (ret & HCR_TGE) {
5271 /* These bits are up-to-date as of ARMv8.6. */
5272 if (ret & HCR_E2H) {
5273 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5274 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5275 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5276 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5277 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5278 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5279 } else {
5280 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5282 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5283 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5284 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5285 HCR_TLOR);
5288 return ret;
5292 * Corresponds to ARM pseudocode function ELIsInHost().
5294 bool el_is_in_host(CPUARMState *env, int el)
5296 uint64_t mask;
5299 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5300 * Perform the simplest bit tests first, and validate EL2 afterward.
5302 if (el & 1) {
5303 return false; /* EL1 or EL3 */
5307 * Note that hcr_write() checks isar_feature_aa64_vh(),
5308 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5310 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5311 if ((env->cp15.hcr_el2 & mask) != mask) {
5312 return false;
5315 /* TGE and/or E2H set: double check those bits are currently legal. */
5316 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5319 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5320 uint64_t value)
5322 uint64_t valid_mask = 0;
5324 /* No features adding bits to HCRX are implemented. */
5326 /* Clear RES0 bits. */
5327 env->cp15.hcrx_el2 = value & valid_mask;
5330 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5331 bool isread)
5333 if (arm_current_el(env) < 3
5334 && arm_feature(env, ARM_FEATURE_EL3)
5335 && !(env->cp15.scr_el3 & SCR_HXEN)) {
5336 return CP_ACCESS_TRAP_EL3;
5338 return CP_ACCESS_OK;
5341 static const ARMCPRegInfo hcrx_el2_reginfo = {
5342 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5343 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5344 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5345 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5348 /* Return the effective value of HCRX_EL2. */
5349 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5352 * The bits in this register behave as 0 for all purposes other than
5353 * direct reads of the register if:
5354 * - EL2 is not enabled in the current security state,
5355 * - SCR_EL3.HXEn is 0.
5357 if (!arm_is_el2_enabled(env)
5358 || (arm_feature(env, ARM_FEATURE_EL3)
5359 && !(env->cp15.scr_el3 & SCR_HXEN))) {
5360 return 0;
5362 return env->cp15.hcrx_el2;
5365 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5366 uint64_t value)
5369 * For A-profile AArch32 EL3, if NSACR.CP10
5370 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5372 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5373 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5374 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5375 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5377 env->cp15.cptr_el[2] = value;
5380 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5383 * For A-profile AArch32 EL3, if NSACR.CP10
5384 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5386 uint64_t value = env->cp15.cptr_el[2];
5388 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5389 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5390 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5392 return value;
5395 static const ARMCPRegInfo el2_cp_reginfo[] = {
5396 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5397 .type = ARM_CP_IO,
5398 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5399 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5400 .writefn = hcr_write },
5401 { .name = "HCR", .state = ARM_CP_STATE_AA32,
5402 .type = ARM_CP_ALIAS | ARM_CP_IO,
5403 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5404 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5405 .writefn = hcr_writelow },
5406 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5407 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5408 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5409 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5410 .type = ARM_CP_ALIAS,
5411 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5412 .access = PL2_RW,
5413 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5414 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5415 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5416 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5417 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5418 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5419 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5420 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5421 .type = ARM_CP_ALIAS,
5422 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5423 .access = PL2_RW,
5424 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5425 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5426 .type = ARM_CP_ALIAS,
5427 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5428 .access = PL2_RW,
5429 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5430 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5431 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5432 .access = PL2_RW, .writefn = vbar_write,
5433 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5434 .resetvalue = 0 },
5435 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5436 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5437 .access = PL3_RW, .type = ARM_CP_ALIAS,
5438 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5439 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5440 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5441 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5442 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5443 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5444 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5445 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5446 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5447 .resetvalue = 0 },
5448 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5449 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5450 .access = PL2_RW, .type = ARM_CP_ALIAS,
5451 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5452 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5453 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5454 .access = PL2_RW, .type = ARM_CP_CONST,
5455 .resetvalue = 0 },
5456 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5457 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5458 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5459 .access = PL2_RW, .type = ARM_CP_CONST,
5460 .resetvalue = 0 },
5461 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5462 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5463 .access = PL2_RW, .type = ARM_CP_CONST,
5464 .resetvalue = 0 },
5465 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5466 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5467 .access = PL2_RW, .type = ARM_CP_CONST,
5468 .resetvalue = 0 },
5469 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5470 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5471 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5472 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
5473 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5474 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5475 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5476 .type = ARM_CP_ALIAS,
5477 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5478 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5479 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5480 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5481 .access = PL2_RW,
5482 /* no .writefn needed as this can't cause an ASID change;
5483 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5485 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5486 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5487 .cp = 15, .opc1 = 6, .crm = 2,
5488 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5489 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5490 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5491 .writefn = vttbr_write },
5492 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5493 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5494 .access = PL2_RW, .writefn = vttbr_write,
5495 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5496 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5497 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5498 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5499 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5500 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5501 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5502 .access = PL2_RW, .resetvalue = 0,
5503 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5504 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5505 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5506 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
5507 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5508 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5509 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5510 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5511 { .name = "TLBIALLNSNH",
5512 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5513 .type = ARM_CP_NO_RAW, .access = PL2_W,
5514 .writefn = tlbiall_nsnh_write },
5515 { .name = "TLBIALLNSNHIS",
5516 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5517 .type = ARM_CP_NO_RAW, .access = PL2_W,
5518 .writefn = tlbiall_nsnh_is_write },
5519 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5520 .type = ARM_CP_NO_RAW, .access = PL2_W,
5521 .writefn = tlbiall_hyp_write },
5522 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5523 .type = ARM_CP_NO_RAW, .access = PL2_W,
5524 .writefn = tlbiall_hyp_is_write },
5525 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5526 .type = ARM_CP_NO_RAW, .access = PL2_W,
5527 .writefn = tlbimva_hyp_write },
5528 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5529 .type = ARM_CP_NO_RAW, .access = PL2_W,
5530 .writefn = tlbimva_hyp_is_write },
5531 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5532 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5533 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5534 .writefn = tlbi_aa64_alle2_write },
5535 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5536 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5537 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5538 .writefn = tlbi_aa64_vae2_write },
5539 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5540 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5541 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5542 .writefn = tlbi_aa64_vae2_write },
5543 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5544 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5545 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5546 .writefn = tlbi_aa64_alle2is_write },
5547 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5548 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5549 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5550 .writefn = tlbi_aa64_vae2is_write },
5551 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5552 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5553 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
5554 .writefn = tlbi_aa64_vae2is_write },
5555 #ifndef CONFIG_USER_ONLY
5556 /* Unlike the other EL2-related AT operations, these must
5557 * UNDEF from EL3 if EL2 is not implemented, which is why we
5558 * define them here rather than with the rest of the AT ops.
5560 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5561 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5562 .access = PL2_W, .accessfn = at_s1e2_access,
5563 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5564 .writefn = ats_write64 },
5565 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5566 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5567 .access = PL2_W, .accessfn = at_s1e2_access,
5568 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5569 .writefn = ats_write64 },
5570 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5571 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5572 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5573 * to behave as if SCR.NS was 1.
5575 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5576 .access = PL2_W,
5577 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5578 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5579 .access = PL2_W,
5580 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5581 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5582 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5583 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5584 * reset values as IMPDEF. We choose to reset to 3 to comply with
5585 * both ARMv7 and ARMv8.
5587 .access = PL2_RW, .resetvalue = 3,
5588 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5589 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5590 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5591 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5592 .writefn = gt_cntvoff_write,
5593 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5594 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5595 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5596 .writefn = gt_cntvoff_write,
5597 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5598 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5599 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5600 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5601 .type = ARM_CP_IO, .access = PL2_RW,
5602 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5603 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5604 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5605 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5606 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5607 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5608 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5609 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5610 .resetfn = gt_hyp_timer_reset,
5611 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5612 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5613 .type = ARM_CP_IO,
5614 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5615 .access = PL2_RW,
5616 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5617 .resetvalue = 0,
5618 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5619 #endif
5620 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5621 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5622 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5623 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5624 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5625 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5626 .access = PL2_RW,
5627 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5628 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5629 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5630 .access = PL2_RW,
5631 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5634 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5635 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5636 .type = ARM_CP_ALIAS | ARM_CP_IO,
5637 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5638 .access = PL2_RW,
5639 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5640 .writefn = hcr_writehigh },
5643 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5644 bool isread)
5646 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5647 return CP_ACCESS_OK;
5649 return CP_ACCESS_TRAP_UNCATEGORIZED;
5652 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5653 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5654 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5655 .access = PL2_RW, .accessfn = sel2_access,
5656 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5657 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5658 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5659 .access = PL2_RW, .accessfn = sel2_access,
5660 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5663 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5664 bool isread)
5666 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5667 * At Secure EL1 it traps to EL3 or EL2.
5669 if (arm_current_el(env) == 3) {
5670 return CP_ACCESS_OK;
5672 if (arm_is_secure_below_el3(env)) {
5673 if (env->cp15.scr_el3 & SCR_EEL2) {
5674 return CP_ACCESS_TRAP_EL2;
5676 return CP_ACCESS_TRAP_EL3;
5678 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5679 if (isread) {
5680 return CP_ACCESS_OK;
5682 return CP_ACCESS_TRAP_UNCATEGORIZED;
5685 static const ARMCPRegInfo el3_cp_reginfo[] = {
5686 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5687 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5688 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5689 .resetfn = scr_reset, .writefn = scr_write },
5690 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5691 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5692 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5693 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5694 .writefn = scr_write },
5695 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5696 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5697 .access = PL3_RW, .resetvalue = 0,
5698 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5699 { .name = "SDER",
5700 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5701 .access = PL3_RW, .resetvalue = 0,
5702 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5703 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5704 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5705 .writefn = vbar_write, .resetvalue = 0,
5706 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5707 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5708 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5709 .access = PL3_RW, .resetvalue = 0,
5710 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5711 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5712 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5713 .access = PL3_RW,
5714 /* no .writefn needed as this can't cause an ASID change;
5715 * we must provide a .raw_writefn and .resetfn because we handle
5716 * reset and migration for the AArch32 TTBCR(S), which might be
5717 * using mask and base_mask.
5719 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
5720 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5721 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5722 .type = ARM_CP_ALIAS,
5723 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5724 .access = PL3_RW,
5725 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5726 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5727 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5728 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5729 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5730 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5731 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5732 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5733 .type = ARM_CP_ALIAS,
5734 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5735 .access = PL3_RW,
5736 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5737 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5738 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5739 .access = PL3_RW, .writefn = vbar_write,
5740 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5741 .resetvalue = 0 },
5742 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5743 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5744 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5745 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5746 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5747 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5748 .access = PL3_RW, .resetvalue = 0,
5749 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5750 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5751 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5752 .access = PL3_RW, .type = ARM_CP_CONST,
5753 .resetvalue = 0 },
5754 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5755 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5756 .access = PL3_RW, .type = ARM_CP_CONST,
5757 .resetvalue = 0 },
5758 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5759 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5760 .access = PL3_RW, .type = ARM_CP_CONST,
5761 .resetvalue = 0 },
5762 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5763 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5764 .access = PL3_W, .type = ARM_CP_NO_RAW,
5765 .writefn = tlbi_aa64_alle3is_write },
5766 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5767 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5768 .access = PL3_W, .type = ARM_CP_NO_RAW,
5769 .writefn = tlbi_aa64_vae3is_write },
5770 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5771 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5772 .access = PL3_W, .type = ARM_CP_NO_RAW,
5773 .writefn = tlbi_aa64_vae3is_write },
5774 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5775 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5776 .access = PL3_W, .type = ARM_CP_NO_RAW,
5777 .writefn = tlbi_aa64_alle3_write },
5778 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5779 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5780 .access = PL3_W, .type = ARM_CP_NO_RAW,
5781 .writefn = tlbi_aa64_vae3_write },
5782 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5783 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5784 .access = PL3_W, .type = ARM_CP_NO_RAW,
5785 .writefn = tlbi_aa64_vae3_write },
5788 #ifndef CONFIG_USER_ONLY
5789 /* Test if system register redirection is to occur in the current state. */
5790 static bool redirect_for_e2h(CPUARMState *env)
5792 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5795 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5797 CPReadFn *readfn;
5799 if (redirect_for_e2h(env)) {
5800 /* Switch to the saved EL2 version of the register. */
5801 ri = ri->opaque;
5802 readfn = ri->readfn;
5803 } else {
5804 readfn = ri->orig_readfn;
5806 if (readfn == NULL) {
5807 readfn = raw_read;
5809 return readfn(env, ri);
5812 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5813 uint64_t value)
5815 CPWriteFn *writefn;
5817 if (redirect_for_e2h(env)) {
5818 /* Switch to the saved EL2 version of the register. */
5819 ri = ri->opaque;
5820 writefn = ri->writefn;
5821 } else {
5822 writefn = ri->orig_writefn;
5824 if (writefn == NULL) {
5825 writefn = raw_write;
5827 writefn(env, ri, value);
5830 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5832 struct E2HAlias {
5833 uint32_t src_key, dst_key, new_key;
5834 const char *src_name, *dst_name, *new_name;
5835 bool (*feature)(const ARMISARegisters *id);
5838 #define K(op0, op1, crn, crm, op2) \
5839 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5841 static const struct E2HAlias aliases[] = {
5842 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5843 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5844 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5845 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5846 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5847 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5848 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5849 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5850 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5851 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5852 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5853 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5854 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5855 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5856 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5857 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5858 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5859 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5860 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5861 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5862 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5863 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5864 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5865 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5866 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5867 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5868 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5869 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5870 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5871 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5872 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5873 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5876 * Note that redirection of ZCR is mentioned in the description
5877 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5878 * not in the summary table.
5880 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5881 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5882 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
5883 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
5885 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
5886 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5888 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5889 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5890 isar_feature_aa64_scxtnum },
5892 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5893 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5895 #undef K
5897 size_t i;
5899 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5900 const struct E2HAlias *a = &aliases[i];
5901 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
5902 bool ok;
5904 if (a->feature && !a->feature(&cpu->isar)) {
5905 continue;
5908 src_reg = g_hash_table_lookup(cpu->cp_regs,
5909 (gpointer)(uintptr_t)a->src_key);
5910 dst_reg = g_hash_table_lookup(cpu->cp_regs,
5911 (gpointer)(uintptr_t)a->dst_key);
5912 g_assert(src_reg != NULL);
5913 g_assert(dst_reg != NULL);
5915 /* Cross-compare names to detect typos in the keys. */
5916 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5917 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5919 /* None of the core system registers use opaque; we will. */
5920 g_assert(src_reg->opaque == NULL);
5922 /* Create alias before redirection so we dup the right data. */
5923 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5925 new_reg->name = a->new_name;
5926 new_reg->type |= ARM_CP_ALIAS;
5927 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5928 new_reg->access &= PL2_RW | PL3_RW;
5930 ok = g_hash_table_insert(cpu->cp_regs,
5931 (gpointer)(uintptr_t)a->new_key, new_reg);
5932 g_assert(ok);
5934 src_reg->opaque = dst_reg;
5935 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5936 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5937 if (!src_reg->raw_readfn) {
5938 src_reg->raw_readfn = raw_read;
5940 if (!src_reg->raw_writefn) {
5941 src_reg->raw_writefn = raw_write;
5943 src_reg->readfn = el2_e2h_read;
5944 src_reg->writefn = el2_e2h_write;
5947 #endif
5949 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5950 bool isread)
5952 int cur_el = arm_current_el(env);
5954 if (cur_el < 2) {
5955 uint64_t hcr = arm_hcr_el2_eff(env);
5957 if (cur_el == 0) {
5958 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5959 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5960 return CP_ACCESS_TRAP_EL2;
5962 } else {
5963 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5964 return CP_ACCESS_TRAP;
5966 if (hcr & HCR_TID2) {
5967 return CP_ACCESS_TRAP_EL2;
5970 } else if (hcr & HCR_TID2) {
5971 return CP_ACCESS_TRAP_EL2;
5975 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5976 return CP_ACCESS_TRAP_EL2;
5979 return CP_ACCESS_OK;
5982 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5983 uint64_t value)
5985 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5986 * read via a bit in OSLSR_EL1.
5988 int oslock;
5990 if (ri->state == ARM_CP_STATE_AA32) {
5991 oslock = (value == 0xC5ACCE55);
5992 } else {
5993 oslock = value & 1;
5996 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5999 static const ARMCPRegInfo debug_cp_reginfo[] = {
6000 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
6001 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
6002 * unlike DBGDRAR it is never accessible from EL0.
6003 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
6004 * accessor.
6006 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
6007 .access = PL0_R, .accessfn = access_tdra,
6008 .type = ARM_CP_CONST, .resetvalue = 0 },
6009 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
6010 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
6011 .access = PL1_R, .accessfn = access_tdra,
6012 .type = ARM_CP_CONST, .resetvalue = 0 },
6013 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
6014 .access = PL0_R, .accessfn = access_tdra,
6015 .type = ARM_CP_CONST, .resetvalue = 0 },
6016 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
6017 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
6018 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
6019 .access = PL1_RW, .accessfn = access_tda,
6020 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
6021 .resetvalue = 0 },
6023 * MDCCSR_EL0[30:29] map to EDSCR[30:29]. Simply RAZ as the external
6024 * Debug Communication Channel is not implemented.
6026 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_AA64,
6027 .opc0 = 2, .opc1 = 3, .crn = 0, .crm = 1, .opc2 = 0,
6028 .access = PL0_R, .accessfn = access_tda,
6029 .type = ARM_CP_CONST, .resetvalue = 0 },
6031 * DBGDSCRint[15,12,5:2] map to MDSCR_EL1[15,12,5:2]. Map all bits as
6032 * it is unlikely a guest will care.
6033 * We don't implement the configurable EL0 access.
6035 { .name = "DBGDSCRint", .state = ARM_CP_STATE_AA32,
6036 .cp = 14, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
6037 .type = ARM_CP_ALIAS,
6038 .access = PL1_R, .accessfn = access_tda,
6039 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
6040 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
6041 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
6042 .access = PL1_W, .type = ARM_CP_NO_RAW,
6043 .accessfn = access_tdosa,
6044 .writefn = oslar_write },
6045 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
6046 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
6047 .access = PL1_R, .resetvalue = 10,
6048 .accessfn = access_tdosa,
6049 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
6050 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
6051 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
6052 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
6053 .access = PL1_RW, .accessfn = access_tdosa,
6054 .type = ARM_CP_NOP },
6055 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
6056 * implement vector catch debug events yet.
6058 { .name = "DBGVCR",
6059 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
6060 .access = PL1_RW, .accessfn = access_tda,
6061 .type = ARM_CP_NOP },
6062 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
6063 * to save and restore a 32-bit guest's DBGVCR)
6065 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
6066 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
6067 .access = PL2_RW, .accessfn = access_tda,
6068 .type = ARM_CP_NOP | ARM_CP_EL3_NO_EL2_KEEP },
6069 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
6070 * Channel but Linux may try to access this register. The 32-bit
6071 * alias is DBGDCCINT.
6073 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
6074 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6075 .access = PL1_RW, .accessfn = access_tda,
6076 .type = ARM_CP_NOP },
6079 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6080 /* 64 bit access versions of the (dummy) debug registers */
6081 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6082 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6083 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6084 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6088 * Check for traps to RAS registers, which are controlled
6089 * by HCR_EL2.TERR and SCR_EL3.TERR.
6091 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6092 bool isread)
6094 int el = arm_current_el(env);
6096 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6097 return CP_ACCESS_TRAP_EL2;
6099 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6100 return CP_ACCESS_TRAP_EL3;
6102 return CP_ACCESS_OK;
6105 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6107 int el = arm_current_el(env);
6109 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6110 return env->cp15.vdisr_el2;
6112 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6113 return 0; /* RAZ/WI */
6115 return env->cp15.disr_el1;
6118 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6120 int el = arm_current_el(env);
6122 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6123 env->cp15.vdisr_el2 = val;
6124 return;
6126 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6127 return; /* RAZ/WI */
6129 env->cp15.disr_el1 = val;
6133 * Minimal RAS implementation with no Error Records.
6134 * Which means that all of the Error Record registers:
6135 * ERXADDR_EL1
6136 * ERXCTLR_EL1
6137 * ERXFR_EL1
6138 * ERXMISC0_EL1
6139 * ERXMISC1_EL1
6140 * ERXMISC2_EL1
6141 * ERXMISC3_EL1
6142 * ERXPFGCDN_EL1 (RASv1p1)
6143 * ERXPFGCTL_EL1 (RASv1p1)
6144 * ERXPFGF_EL1 (RASv1p1)
6145 * ERXSTATUS_EL1
6146 * and
6147 * ERRSELR_EL1
6148 * may generate UNDEFINED, which is the effect we get by not
6149 * listing them at all.
6151 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6152 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6153 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6154 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6155 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6156 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6157 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6158 .access = PL1_R, .accessfn = access_terr,
6159 .type = ARM_CP_CONST, .resetvalue = 0 },
6160 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6161 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6162 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6163 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6164 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6165 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6169 * Return the exception level to which exceptions should be taken
6170 * via SVEAccessTrap. This excludes the check for whether the exception
6171 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
6172 * be found by testing 0 < fp_exception_el < sve_exception_el.
6174 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
6175 * pseudocode does *not* separate out the FP trap checks, but has them
6176 * all in one function.
6178 int sve_exception_el(CPUARMState *env, int el)
6180 #ifndef CONFIG_USER_ONLY
6181 if (el <= 1 && !el_is_in_host(env, el)) {
6182 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6183 case 1:
6184 if (el != 0) {
6185 break;
6187 /* fall through */
6188 case 0:
6189 case 2:
6190 return 1;
6194 if (el <= 2 && arm_is_el2_enabled(env)) {
6195 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6196 if (env->cp15.hcr_el2 & HCR_E2H) {
6197 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6198 case 1:
6199 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6200 break;
6202 /* fall through */
6203 case 0:
6204 case 2:
6205 return 2;
6207 } else {
6208 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6209 return 2;
6214 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6215 if (arm_feature(env, ARM_FEATURE_EL3)
6216 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6217 return 3;
6219 #endif
6220 return 0;
6224 * Return the exception level to which exceptions should be taken for SME.
6225 * C.f. the ARM pseudocode function CheckSMEAccess.
6227 int sme_exception_el(CPUARMState *env, int el)
6229 #ifndef CONFIG_USER_ONLY
6230 if (el <= 1 && !el_is_in_host(env, el)) {
6231 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6232 case 1:
6233 if (el != 0) {
6234 break;
6236 /* fall through */
6237 case 0:
6238 case 2:
6239 return 1;
6243 if (el <= 2 && arm_is_el2_enabled(env)) {
6244 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6245 if (env->cp15.hcr_el2 & HCR_E2H) {
6246 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6247 case 1:
6248 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6249 break;
6251 /* fall through */
6252 case 0:
6253 case 2:
6254 return 2;
6256 } else {
6257 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6258 return 2;
6263 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
6264 if (arm_feature(env, ARM_FEATURE_EL3)
6265 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6266 return 3;
6268 #endif
6269 return 0;
6273 * Given that SVE is enabled, return the vector length for EL.
6275 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6277 ARMCPU *cpu = env_archcpu(env);
6278 uint32_t len = cpu->sve_max_vq - 1;
6280 if (el <= 1 && !el_is_in_host(env, el)) {
6281 len = MIN(len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6283 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6284 len = MIN(len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6286 if (arm_feature(env, ARM_FEATURE_EL3)) {
6287 len = MIN(len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6290 len = 31 - clz32(cpu->sve_vq_map & MAKE_64BIT_MASK(0, len + 1));
6291 return len;
6294 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6295 uint64_t value)
6297 int cur_el = arm_current_el(env);
6298 int old_len = sve_vqm1_for_el(env, cur_el);
6299 int new_len;
6301 /* Bits other than [3:0] are RAZ/WI. */
6302 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6303 raw_write(env, ri, value & 0xf);
6306 * Because we arrived here, we know both FP and SVE are enabled;
6307 * otherwise we would have trapped access to the ZCR_ELn register.
6309 new_len = sve_vqm1_for_el(env, cur_el);
6310 if (new_len < old_len) {
6311 aarch64_sve_narrow_vq(env, new_len + 1);
6315 static const ARMCPRegInfo zcr_reginfo[] = {
6316 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6317 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6318 .access = PL1_RW, .type = ARM_CP_SVE,
6319 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6320 .writefn = zcr_write, .raw_writefn = raw_write },
6321 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6322 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6323 .access = PL2_RW, .type = ARM_CP_SVE,
6324 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6325 .writefn = zcr_write, .raw_writefn = raw_write },
6326 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6327 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6328 .access = PL3_RW, .type = ARM_CP_SVE,
6329 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6330 .writefn = zcr_write, .raw_writefn = raw_write },
6333 #ifdef TARGET_AARCH64
6334 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6335 bool isread)
6337 int el = arm_current_el(env);
6339 if (el == 0) {
6340 uint64_t sctlr = arm_sctlr(env, el);
6341 if (!(sctlr & SCTLR_EnTP2)) {
6342 return CP_ACCESS_TRAP;
6345 /* TODO: FEAT_FGT */
6346 if (el < 3
6347 && arm_feature(env, ARM_FEATURE_EL3)
6348 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6349 return CP_ACCESS_TRAP_EL3;
6351 return CP_ACCESS_OK;
6354 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6355 uint64_t value)
6357 value &= R_SVCR_SM_MASK | R_SVCR_ZA_MASK;
6358 /* TODO: Side effects. */
6359 env->svcr = value;
6362 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6363 uint64_t value)
6365 int cur_el = arm_current_el(env);
6366 int old_len = sve_vqm1_for_el(env, cur_el);
6367 int new_len;
6369 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6370 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6371 raw_write(env, ri, value);
6374 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6375 * when SVL is widened (old values kept, or zeros). Choose to keep the
6376 * current values for simplicity. But for QEMU internals, we must still
6377 * apply the narrower SVL to the Zregs and Pregs -- see the comment
6378 * above aarch64_sve_narrow_vq.
6380 new_len = sve_vqm1_for_el(env, cur_el);
6381 if (new_len < old_len) {
6382 aarch64_sve_narrow_vq(env, new_len + 1);
6386 static const ARMCPRegInfo sme_reginfo[] = {
6387 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6388 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6389 .access = PL0_RW, .accessfn = access_tpidr2,
6390 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6391 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6392 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6393 .access = PL0_RW, .type = ARM_CP_SME,
6394 .fieldoffset = offsetof(CPUARMState, svcr),
6395 .writefn = svcr_write, .raw_writefn = raw_write },
6396 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6397 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6398 .access = PL1_RW, .type = ARM_CP_SME,
6399 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6400 .writefn = smcr_write, .raw_writefn = raw_write },
6401 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6402 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6403 .access = PL2_RW, .type = ARM_CP_SME,
6404 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6405 .writefn = smcr_write, .raw_writefn = raw_write },
6406 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6407 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6408 .access = PL3_RW, .type = ARM_CP_SME,
6409 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6410 .writefn = smcr_write, .raw_writefn = raw_write },
6412 #endif /* TARGET_AARCH64 */
6414 void hw_watchpoint_update(ARMCPU *cpu, int n)
6416 CPUARMState *env = &cpu->env;
6417 vaddr len = 0;
6418 vaddr wvr = env->cp15.dbgwvr[n];
6419 uint64_t wcr = env->cp15.dbgwcr[n];
6420 int mask;
6421 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6423 if (env->cpu_watchpoint[n]) {
6424 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6425 env->cpu_watchpoint[n] = NULL;
6428 if (!FIELD_EX64(wcr, DBGWCR, E)) {
6429 /* E bit clear : watchpoint disabled */
6430 return;
6433 switch (FIELD_EX64(wcr, DBGWCR, LSC)) {
6434 case 0:
6435 /* LSC 00 is reserved and must behave as if the wp is disabled */
6436 return;
6437 case 1:
6438 flags |= BP_MEM_READ;
6439 break;
6440 case 2:
6441 flags |= BP_MEM_WRITE;
6442 break;
6443 case 3:
6444 flags |= BP_MEM_ACCESS;
6445 break;
6448 /* Attempts to use both MASK and BAS fields simultaneously are
6449 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6450 * thus generating a watchpoint for every byte in the masked region.
6452 mask = FIELD_EX64(wcr, DBGWCR, MASK);
6453 if (mask == 1 || mask == 2) {
6454 /* Reserved values of MASK; we must act as if the mask value was
6455 * some non-reserved value, or as if the watchpoint were disabled.
6456 * We choose the latter.
6458 return;
6459 } else if (mask) {
6460 /* Watchpoint covers an aligned area up to 2GB in size */
6461 len = 1ULL << mask;
6462 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6463 * whether the watchpoint fires when the unmasked bits match; we opt
6464 * to generate the exceptions.
6466 wvr &= ~(len - 1);
6467 } else {
6468 /* Watchpoint covers bytes defined by the byte address select bits */
6469 int bas = FIELD_EX64(wcr, DBGWCR, BAS);
6470 int basstart;
6472 if (extract64(wvr, 2, 1)) {
6473 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6474 * ignored, and BAS[3:0] define which bytes to watch.
6476 bas &= 0xf;
6479 if (bas == 0) {
6480 /* This must act as if the watchpoint is disabled */
6481 return;
6484 /* The BAS bits are supposed to be programmed to indicate a contiguous
6485 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6486 * we fire for each byte in the word/doubleword addressed by the WVR.
6487 * We choose to ignore any non-zero bits after the first range of 1s.
6489 basstart = ctz32(bas);
6490 len = cto32(bas >> basstart);
6491 wvr += basstart;
6494 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6495 &env->cpu_watchpoint[n]);
6498 void hw_watchpoint_update_all(ARMCPU *cpu)
6500 int i;
6501 CPUARMState *env = &cpu->env;
6503 /* Completely clear out existing QEMU watchpoints and our array, to
6504 * avoid possible stale entries following migration load.
6506 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6507 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6509 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6510 hw_watchpoint_update(cpu, i);
6514 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6515 uint64_t value)
6517 ARMCPU *cpu = env_archcpu(env);
6518 int i = ri->crm;
6521 * Bits [1:0] are RES0.
6523 * It is IMPLEMENTATION DEFINED whether [63:49] ([63:53] with FEAT_LVA)
6524 * are hardwired to the value of bit [48] ([52] with FEAT_LVA), or if
6525 * they contain the value written. It is CONSTRAINED UNPREDICTABLE
6526 * whether the RESS bits are ignored when comparing an address.
6528 * Therefore we are allowed to compare the entire register, which lets
6529 * us avoid considering whether or not FEAT_LVA is actually enabled.
6531 value &= ~3ULL;
6533 raw_write(env, ri, value);
6534 hw_watchpoint_update(cpu, i);
6537 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6538 uint64_t value)
6540 ARMCPU *cpu = env_archcpu(env);
6541 int i = ri->crm;
6543 raw_write(env, ri, value);
6544 hw_watchpoint_update(cpu, i);
6547 void hw_breakpoint_update(ARMCPU *cpu, int n)
6549 CPUARMState *env = &cpu->env;
6550 uint64_t bvr = env->cp15.dbgbvr[n];
6551 uint64_t bcr = env->cp15.dbgbcr[n];
6552 vaddr addr;
6553 int bt;
6554 int flags = BP_CPU;
6556 if (env->cpu_breakpoint[n]) {
6557 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6558 env->cpu_breakpoint[n] = NULL;
6561 if (!extract64(bcr, 0, 1)) {
6562 /* E bit clear : watchpoint disabled */
6563 return;
6566 bt = extract64(bcr, 20, 4);
6568 switch (bt) {
6569 case 4: /* unlinked address mismatch (reserved if AArch64) */
6570 case 5: /* linked address mismatch (reserved if AArch64) */
6571 qemu_log_mask(LOG_UNIMP,
6572 "arm: address mismatch breakpoint types not implemented\n");
6573 return;
6574 case 0: /* unlinked address match */
6575 case 1: /* linked address match */
6578 * Bits [1:0] are RES0.
6580 * It is IMPLEMENTATION DEFINED whether bits [63:49]
6581 * ([63:53] for FEAT_LVA) are hardwired to a copy of the sign bit
6582 * of the VA field ([48] or [52] for FEAT_LVA), or whether the
6583 * value is read as written. It is CONSTRAINED UNPREDICTABLE
6584 * whether the RESS bits are ignored when comparing an address.
6585 * Therefore we are allowed to compare the entire register, which
6586 * lets us avoid considering whether FEAT_LVA is actually enabled.
6588 * The BAS field is used to allow setting breakpoints on 16-bit
6589 * wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6590 * a bp will fire if the addresses covered by the bp and the addresses
6591 * covered by the insn overlap but the insn doesn't start at the
6592 * start of the bp address range. We choose to require the insn and
6593 * the bp to have the same address. The constraints on writing to
6594 * BAS enforced in dbgbcr_write mean we have only four cases:
6595 * 0b0000 => no breakpoint
6596 * 0b0011 => breakpoint on addr
6597 * 0b1100 => breakpoint on addr + 2
6598 * 0b1111 => breakpoint on addr
6599 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6601 int bas = extract64(bcr, 5, 4);
6602 addr = bvr & ~3ULL;
6603 if (bas == 0) {
6604 return;
6606 if (bas == 0xc) {
6607 addr += 2;
6609 break;
6611 case 2: /* unlinked context ID match */
6612 case 8: /* unlinked VMID match (reserved if no EL2) */
6613 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6614 qemu_log_mask(LOG_UNIMP,
6615 "arm: unlinked context breakpoint types not implemented\n");
6616 return;
6617 case 9: /* linked VMID match (reserved if no EL2) */
6618 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6619 case 3: /* linked context ID match */
6620 default:
6621 /* We must generate no events for Linked context matches (unless
6622 * they are linked to by some other bp/wp, which is handled in
6623 * updates for the linking bp/wp). We choose to also generate no events
6624 * for reserved values.
6626 return;
6629 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6632 void hw_breakpoint_update_all(ARMCPU *cpu)
6634 int i;
6635 CPUARMState *env = &cpu->env;
6637 /* Completely clear out existing QEMU breakpoints and our array, to
6638 * avoid possible stale entries following migration load.
6640 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6641 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6643 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6644 hw_breakpoint_update(cpu, i);
6648 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6649 uint64_t value)
6651 ARMCPU *cpu = env_archcpu(env);
6652 int i = ri->crm;
6654 raw_write(env, ri, value);
6655 hw_breakpoint_update(cpu, i);
6658 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6659 uint64_t value)
6661 ARMCPU *cpu = env_archcpu(env);
6662 int i = ri->crm;
6664 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6665 * copy of BAS[0].
6667 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6668 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6670 raw_write(env, ri, value);
6671 hw_breakpoint_update(cpu, i);
6674 static void define_debug_regs(ARMCPU *cpu)
6676 /* Define v7 and v8 architectural debug registers.
6677 * These are just dummy implementations for now.
6679 int i;
6680 int wrps, brps, ctx_cmps;
6683 * The Arm ARM says DBGDIDR is optional and deprecated if EL1 cannot
6684 * use AArch32. Given that bit 15 is RES1, if the value is 0 then
6685 * the register must not exist for this cpu.
6687 if (cpu->isar.dbgdidr != 0) {
6688 ARMCPRegInfo dbgdidr = {
6689 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0,
6690 .opc1 = 0, .opc2 = 0,
6691 .access = PL0_R, .accessfn = access_tda,
6692 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
6694 define_one_arm_cp_reg(cpu, &dbgdidr);
6697 brps = arm_num_brps(cpu);
6698 wrps = arm_num_wrps(cpu);
6699 ctx_cmps = arm_num_ctx_cmps(cpu);
6701 assert(ctx_cmps <= brps);
6703 define_arm_cp_regs(cpu, debug_cp_reginfo);
6705 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6706 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6709 for (i = 0; i < brps; i++) {
6710 char *dbgbvr_el1_name = g_strdup_printf("DBGBVR%d_EL1", i);
6711 char *dbgbcr_el1_name = g_strdup_printf("DBGBCR%d_EL1", i);
6712 ARMCPRegInfo dbgregs[] = {
6713 { .name = dbgbvr_el1_name, .state = ARM_CP_STATE_BOTH,
6714 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
6715 .access = PL1_RW, .accessfn = access_tda,
6716 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6717 .writefn = dbgbvr_write, .raw_writefn = raw_write
6719 { .name = dbgbcr_el1_name, .state = ARM_CP_STATE_BOTH,
6720 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
6721 .access = PL1_RW, .accessfn = access_tda,
6722 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6723 .writefn = dbgbcr_write, .raw_writefn = raw_write
6726 define_arm_cp_regs(cpu, dbgregs);
6727 g_free(dbgbvr_el1_name);
6728 g_free(dbgbcr_el1_name);
6731 for (i = 0; i < wrps; i++) {
6732 char *dbgwvr_el1_name = g_strdup_printf("DBGWVR%d_EL1", i);
6733 char *dbgwcr_el1_name = g_strdup_printf("DBGWCR%d_EL1", i);
6734 ARMCPRegInfo dbgregs[] = {
6735 { .name = dbgwvr_el1_name, .state = ARM_CP_STATE_BOTH,
6736 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
6737 .access = PL1_RW, .accessfn = access_tda,
6738 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6739 .writefn = dbgwvr_write, .raw_writefn = raw_write
6741 { .name = dbgwcr_el1_name, .state = ARM_CP_STATE_BOTH,
6742 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
6743 .access = PL1_RW, .accessfn = access_tda,
6744 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6745 .writefn = dbgwcr_write, .raw_writefn = raw_write
6748 define_arm_cp_regs(cpu, dbgregs);
6749 g_free(dbgwvr_el1_name);
6750 g_free(dbgwcr_el1_name);
6754 static void define_pmu_regs(ARMCPU *cpu)
6757 * v7 performance monitor control register: same implementor
6758 * field as main ID register, and we implement four counters in
6759 * addition to the cycle count register.
6761 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6762 ARMCPRegInfo pmcr = {
6763 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6764 .access = PL0_RW,
6765 .type = ARM_CP_IO | ARM_CP_ALIAS,
6766 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6767 .accessfn = pmreg_access, .writefn = pmcr_write,
6768 .raw_writefn = raw_write,
6770 ARMCPRegInfo pmcr64 = {
6771 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6772 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6773 .access = PL0_RW, .accessfn = pmreg_access,
6774 .type = ARM_CP_IO,
6775 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6776 .resetvalue = cpu->isar.reset_pmcr_el0,
6777 .writefn = pmcr_write, .raw_writefn = raw_write,
6780 define_one_arm_cp_reg(cpu, &pmcr);
6781 define_one_arm_cp_reg(cpu, &pmcr64);
6782 for (i = 0; i < pmcrn; i++) {
6783 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6784 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6785 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6786 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6787 ARMCPRegInfo pmev_regs[] = {
6788 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6789 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6790 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6791 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6792 .accessfn = pmreg_access_xevcntr },
6793 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6794 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6795 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6796 .type = ARM_CP_IO,
6797 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6798 .raw_readfn = pmevcntr_rawread,
6799 .raw_writefn = pmevcntr_rawwrite },
6800 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6801 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6802 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6803 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6804 .accessfn = pmreg_access },
6805 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6806 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6807 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6808 .type = ARM_CP_IO,
6809 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6810 .raw_writefn = pmevtyper_rawwrite },
6812 define_arm_cp_regs(cpu, pmev_regs);
6813 g_free(pmevcntr_name);
6814 g_free(pmevcntr_el0_name);
6815 g_free(pmevtyper_name);
6816 g_free(pmevtyper_el0_name);
6818 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
6819 ARMCPRegInfo v81_pmu_regs[] = {
6820 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6821 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6822 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6823 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6824 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6825 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6826 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6827 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6829 define_arm_cp_regs(cpu, v81_pmu_regs);
6831 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6832 static const ARMCPRegInfo v84_pmmir = {
6833 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6834 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6835 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6836 .resetvalue = 0
6838 define_one_arm_cp_reg(cpu, &v84_pmmir);
6842 /* We don't know until after realize whether there's a GICv3
6843 * attached, and that is what registers the gicv3 sysregs.
6844 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6845 * at runtime.
6847 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6849 ARMCPU *cpu = env_archcpu(env);
6850 uint64_t pfr1 = cpu->isar.id_pfr1;
6852 if (env->gicv3state) {
6853 pfr1 |= 1 << 28;
6855 return pfr1;
6858 #ifndef CONFIG_USER_ONLY
6859 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6861 ARMCPU *cpu = env_archcpu(env);
6862 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6864 if (env->gicv3state) {
6865 pfr0 |= 1 << 24;
6867 return pfr0;
6869 #endif
6871 /* Shared logic between LORID and the rest of the LOR* registers.
6872 * Secure state exclusion has already been dealt with.
6874 static CPAccessResult access_lor_ns(CPUARMState *env,
6875 const ARMCPRegInfo *ri, bool isread)
6877 int el = arm_current_el(env);
6879 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6880 return CP_ACCESS_TRAP_EL2;
6882 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6883 return CP_ACCESS_TRAP_EL3;
6885 return CP_ACCESS_OK;
6888 static CPAccessResult access_lor_other(CPUARMState *env,
6889 const ARMCPRegInfo *ri, bool isread)
6891 if (arm_is_secure_below_el3(env)) {
6892 /* Access denied in secure mode. */
6893 return CP_ACCESS_TRAP;
6895 return access_lor_ns(env, ri, isread);
6899 * A trivial implementation of ARMv8.1-LOR leaves all of these
6900 * registers fixed at 0, which indicates that there are zero
6901 * supported Limited Ordering regions.
6903 static const ARMCPRegInfo lor_reginfo[] = {
6904 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6905 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6906 .access = PL1_RW, .accessfn = access_lor_other,
6907 .type = ARM_CP_CONST, .resetvalue = 0 },
6908 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6909 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6910 .access = PL1_RW, .accessfn = access_lor_other,
6911 .type = ARM_CP_CONST, .resetvalue = 0 },
6912 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6913 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6914 .access = PL1_RW, .accessfn = access_lor_other,
6915 .type = ARM_CP_CONST, .resetvalue = 0 },
6916 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6917 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6918 .access = PL1_RW, .accessfn = access_lor_other,
6919 .type = ARM_CP_CONST, .resetvalue = 0 },
6920 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6921 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6922 .access = PL1_R, .accessfn = access_lor_ns,
6923 .type = ARM_CP_CONST, .resetvalue = 0 },
6926 #ifdef TARGET_AARCH64
6927 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6928 bool isread)
6930 int el = arm_current_el(env);
6932 if (el < 2 &&
6933 arm_is_el2_enabled(env) &&
6934 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6935 return CP_ACCESS_TRAP_EL2;
6937 if (el < 3 &&
6938 arm_feature(env, ARM_FEATURE_EL3) &&
6939 !(env->cp15.scr_el3 & SCR_APK)) {
6940 return CP_ACCESS_TRAP_EL3;
6942 return CP_ACCESS_OK;
6945 static const ARMCPRegInfo pauth_reginfo[] = {
6946 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6947 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6948 .access = PL1_RW, .accessfn = access_pauth,
6949 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
6950 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6951 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6952 .access = PL1_RW, .accessfn = access_pauth,
6953 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
6954 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6955 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6956 .access = PL1_RW, .accessfn = access_pauth,
6957 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
6958 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6959 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6960 .access = PL1_RW, .accessfn = access_pauth,
6961 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
6962 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6963 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6964 .access = PL1_RW, .accessfn = access_pauth,
6965 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
6966 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6967 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6968 .access = PL1_RW, .accessfn = access_pauth,
6969 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
6970 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6971 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6972 .access = PL1_RW, .accessfn = access_pauth,
6973 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
6974 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6975 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6976 .access = PL1_RW, .accessfn = access_pauth,
6977 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
6978 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6979 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6980 .access = PL1_RW, .accessfn = access_pauth,
6981 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
6982 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6983 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6984 .access = PL1_RW, .accessfn = access_pauth,
6985 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
6988 static const ARMCPRegInfo tlbirange_reginfo[] = {
6989 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6990 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6991 .access = PL1_W, .type = ARM_CP_NO_RAW,
6992 .writefn = tlbi_aa64_rvae1is_write },
6993 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6994 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6995 .access = PL1_W, .type = ARM_CP_NO_RAW,
6996 .writefn = tlbi_aa64_rvae1is_write },
6997 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6998 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6999 .access = PL1_W, .type = ARM_CP_NO_RAW,
7000 .writefn = tlbi_aa64_rvae1is_write },
7001 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7002 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7003 .access = PL1_W, .type = ARM_CP_NO_RAW,
7004 .writefn = tlbi_aa64_rvae1is_write },
7005 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7006 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7007 .access = PL1_W, .type = ARM_CP_NO_RAW,
7008 .writefn = tlbi_aa64_rvae1is_write },
7009 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7010 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7011 .access = PL1_W, .type = ARM_CP_NO_RAW,
7012 .writefn = tlbi_aa64_rvae1is_write },
7013 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7014 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7015 .access = PL1_W, .type = ARM_CP_NO_RAW,
7016 .writefn = tlbi_aa64_rvae1is_write },
7017 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7018 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7019 .access = PL1_W, .type = ARM_CP_NO_RAW,
7020 .writefn = tlbi_aa64_rvae1is_write },
7021 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7022 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7023 .access = PL1_W, .type = ARM_CP_NO_RAW,
7024 .writefn = tlbi_aa64_rvae1_write },
7025 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7026 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7027 .access = PL1_W, .type = ARM_CP_NO_RAW,
7028 .writefn = tlbi_aa64_rvae1_write },
7029 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7030 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7031 .access = PL1_W, .type = ARM_CP_NO_RAW,
7032 .writefn = tlbi_aa64_rvae1_write },
7033 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7034 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7035 .access = PL1_W, .type = ARM_CP_NO_RAW,
7036 .writefn = tlbi_aa64_rvae1_write },
7037 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7038 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7039 .access = PL2_W, .type = ARM_CP_NOP },
7040 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7041 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7042 .access = PL2_W, .type = ARM_CP_NOP },
7043 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7044 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7045 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7046 .writefn = tlbi_aa64_rvae2is_write },
7047 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7048 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7049 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7050 .writefn = tlbi_aa64_rvae2is_write },
7051 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7052 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7053 .access = PL2_W, .type = ARM_CP_NOP },
7054 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7055 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7056 .access = PL2_W, .type = ARM_CP_NOP },
7057 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7058 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7059 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7060 .writefn = tlbi_aa64_rvae2is_write },
7061 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7062 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7063 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7064 .writefn = tlbi_aa64_rvae2is_write },
7065 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7066 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7067 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7068 .writefn = tlbi_aa64_rvae2_write },
7069 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7070 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7071 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7072 .writefn = tlbi_aa64_rvae2_write },
7073 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7074 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7075 .access = PL3_W, .type = ARM_CP_NO_RAW,
7076 .writefn = tlbi_aa64_rvae3is_write },
7077 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7078 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7079 .access = PL3_W, .type = ARM_CP_NO_RAW,
7080 .writefn = tlbi_aa64_rvae3is_write },
7081 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7082 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7083 .access = PL3_W, .type = ARM_CP_NO_RAW,
7084 .writefn = tlbi_aa64_rvae3is_write },
7085 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7086 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7087 .access = PL3_W, .type = ARM_CP_NO_RAW,
7088 .writefn = tlbi_aa64_rvae3is_write },
7089 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7090 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7091 .access = PL3_W, .type = ARM_CP_NO_RAW,
7092 .writefn = tlbi_aa64_rvae3_write },
7093 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7094 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7095 .access = PL3_W, .type = ARM_CP_NO_RAW,
7096 .writefn = tlbi_aa64_rvae3_write },
7099 static const ARMCPRegInfo tlbios_reginfo[] = {
7100 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7101 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7102 .access = PL1_W, .type = ARM_CP_NO_RAW,
7103 .writefn = tlbi_aa64_vmalle1is_write },
7104 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7105 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7106 .access = PL1_W, .type = ARM_CP_NO_RAW,
7107 .writefn = tlbi_aa64_vae1is_write },
7108 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7109 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7110 .access = PL1_W, .type = ARM_CP_NO_RAW,
7111 .writefn = tlbi_aa64_vmalle1is_write },
7112 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7113 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7114 .access = PL1_W, .type = ARM_CP_NO_RAW,
7115 .writefn = tlbi_aa64_vae1is_write },
7116 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7117 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7118 .access = PL1_W, .type = ARM_CP_NO_RAW,
7119 .writefn = tlbi_aa64_vae1is_write },
7120 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7121 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7122 .access = PL1_W, .type = ARM_CP_NO_RAW,
7123 .writefn = tlbi_aa64_vae1is_write },
7124 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7125 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7126 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7127 .writefn = tlbi_aa64_alle2is_write },
7128 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7129 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7130 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7131 .writefn = tlbi_aa64_vae2is_write },
7132 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7133 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7134 .access = PL2_W, .type = ARM_CP_NO_RAW,
7135 .writefn = tlbi_aa64_alle1is_write },
7136 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7137 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7138 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7139 .writefn = tlbi_aa64_vae2is_write },
7140 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7141 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7142 .access = PL2_W, .type = ARM_CP_NO_RAW,
7143 .writefn = tlbi_aa64_alle1is_write },
7144 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7145 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7146 .access = PL2_W, .type = ARM_CP_NOP },
7147 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7148 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7149 .access = PL2_W, .type = ARM_CP_NOP },
7150 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7151 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7152 .access = PL2_W, .type = ARM_CP_NOP },
7153 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7154 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7155 .access = PL2_W, .type = ARM_CP_NOP },
7156 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7157 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7158 .access = PL3_W, .type = ARM_CP_NO_RAW,
7159 .writefn = tlbi_aa64_alle3is_write },
7160 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7161 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7162 .access = PL3_W, .type = ARM_CP_NO_RAW,
7163 .writefn = tlbi_aa64_vae3is_write },
7164 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7165 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7166 .access = PL3_W, .type = ARM_CP_NO_RAW,
7167 .writefn = tlbi_aa64_vae3is_write },
7170 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7172 Error *err = NULL;
7173 uint64_t ret;
7175 /* Success sets NZCV = 0000. */
7176 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7178 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7180 * ??? Failed, for unknown reasons in the crypto subsystem.
7181 * The best we can do is log the reason and return the
7182 * timed-out indication to the guest. There is no reason
7183 * we know to expect this failure to be transitory, so the
7184 * guest may well hang retrying the operation.
7186 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7187 ri->name, error_get_pretty(err));
7188 error_free(err);
7190 env->ZF = 0; /* NZCF = 0100 */
7191 return 0;
7193 return ret;
7196 /* We do not support re-seeding, so the two registers operate the same. */
7197 static const ARMCPRegInfo rndr_reginfo[] = {
7198 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7199 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7200 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7201 .access = PL0_R, .readfn = rndr_readfn },
7202 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7203 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7204 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7205 .access = PL0_R, .readfn = rndr_readfn },
7208 #ifndef CONFIG_USER_ONLY
7209 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7210 uint64_t value)
7212 ARMCPU *cpu = env_archcpu(env);
7213 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7214 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7215 uint64_t vaddr_in = (uint64_t) value;
7216 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7217 void *haddr;
7218 int mem_idx = cpu_mmu_index(env, false);
7220 /* This won't be crossing page boundaries */
7221 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7222 if (haddr) {
7224 ram_addr_t offset;
7225 MemoryRegion *mr;
7227 /* RCU lock is already being held */
7228 mr = memory_region_from_host(haddr, &offset);
7230 if (mr) {
7231 memory_region_writeback(mr, offset, dline_size);
7236 static const ARMCPRegInfo dcpop_reg[] = {
7237 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7238 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7239 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7240 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7243 static const ARMCPRegInfo dcpodp_reg[] = {
7244 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7245 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7246 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7247 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7249 #endif /*CONFIG_USER_ONLY*/
7251 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7252 bool isread)
7254 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7255 return CP_ACCESS_TRAP_EL2;
7258 return CP_ACCESS_OK;
7261 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7262 bool isread)
7264 int el = arm_current_el(env);
7266 if (el < 2 && arm_is_el2_enabled(env)) {
7267 uint64_t hcr = arm_hcr_el2_eff(env);
7268 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7269 return CP_ACCESS_TRAP_EL2;
7272 if (el < 3 &&
7273 arm_feature(env, ARM_FEATURE_EL3) &&
7274 !(env->cp15.scr_el3 & SCR_ATA)) {
7275 return CP_ACCESS_TRAP_EL3;
7277 return CP_ACCESS_OK;
7280 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7282 return env->pstate & PSTATE_TCO;
7285 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7287 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7290 static const ARMCPRegInfo mte_reginfo[] = {
7291 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7292 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7293 .access = PL1_RW, .accessfn = access_mte,
7294 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7295 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7296 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7297 .access = PL1_RW, .accessfn = access_mte,
7298 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7299 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7300 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7301 .access = PL2_RW, .accessfn = access_mte,
7302 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7303 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7304 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7305 .access = PL3_RW,
7306 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7307 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7308 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7309 .access = PL1_RW, .accessfn = access_mte,
7310 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7311 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7312 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7313 .access = PL1_RW, .accessfn = access_mte,
7314 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7315 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7316 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7317 .access = PL1_R, .accessfn = access_aa64_tid5,
7318 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7319 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7320 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7321 .type = ARM_CP_NO_RAW,
7322 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7323 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7324 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7325 .type = ARM_CP_NOP, .access = PL1_W,
7326 .accessfn = aa64_cacheop_poc_access },
7327 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7328 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7329 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7330 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7331 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7332 .type = ARM_CP_NOP, .access = PL1_W,
7333 .accessfn = aa64_cacheop_poc_access },
7334 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7335 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7336 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7337 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7338 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7339 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7340 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7341 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7342 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7343 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7344 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7345 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7346 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7347 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7348 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7351 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7352 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7353 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7354 .type = ARM_CP_CONST, .access = PL0_RW, },
7357 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7358 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7359 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7360 .type = ARM_CP_NOP, .access = PL0_W,
7361 .accessfn = aa64_cacheop_poc_access },
7362 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7363 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7364 .type = ARM_CP_NOP, .access = PL0_W,
7365 .accessfn = aa64_cacheop_poc_access },
7366 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7367 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7368 .type = ARM_CP_NOP, .access = PL0_W,
7369 .accessfn = aa64_cacheop_poc_access },
7370 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7371 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7372 .type = ARM_CP_NOP, .access = PL0_W,
7373 .accessfn = aa64_cacheop_poc_access },
7374 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7375 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7376 .type = ARM_CP_NOP, .access = PL0_W,
7377 .accessfn = aa64_cacheop_poc_access },
7378 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7379 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7380 .type = ARM_CP_NOP, .access = PL0_W,
7381 .accessfn = aa64_cacheop_poc_access },
7382 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7383 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7384 .type = ARM_CP_NOP, .access = PL0_W,
7385 .accessfn = aa64_cacheop_poc_access },
7386 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7387 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7388 .type = ARM_CP_NOP, .access = PL0_W,
7389 .accessfn = aa64_cacheop_poc_access },
7390 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7391 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7392 .access = PL0_W, .type = ARM_CP_DC_GVA,
7393 #ifndef CONFIG_USER_ONLY
7394 /* Avoid overhead of an access check that always passes in user-mode */
7395 .accessfn = aa64_zva_access,
7396 #endif
7398 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7399 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7400 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7401 #ifndef CONFIG_USER_ONLY
7402 /* Avoid overhead of an access check that always passes in user-mode */
7403 .accessfn = aa64_zva_access,
7404 #endif
7408 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7409 bool isread)
7411 uint64_t hcr = arm_hcr_el2_eff(env);
7412 int el = arm_current_el(env);
7414 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7415 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7416 if (hcr & HCR_TGE) {
7417 return CP_ACCESS_TRAP_EL2;
7419 return CP_ACCESS_TRAP;
7421 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7422 return CP_ACCESS_TRAP_EL2;
7424 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7425 return CP_ACCESS_TRAP_EL2;
7427 if (el < 3
7428 && arm_feature(env, ARM_FEATURE_EL3)
7429 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7430 return CP_ACCESS_TRAP_EL3;
7432 return CP_ACCESS_OK;
7435 static const ARMCPRegInfo scxtnum_reginfo[] = {
7436 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7437 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7438 .access = PL0_RW, .accessfn = access_scxtnum,
7439 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7440 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7441 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7442 .access = PL1_RW, .accessfn = access_scxtnum,
7443 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7444 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7445 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7446 .access = PL2_RW, .accessfn = access_scxtnum,
7447 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7448 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7449 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7450 .access = PL3_RW,
7451 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7453 #endif /* TARGET_AARCH64 */
7455 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7456 bool isread)
7458 int el = arm_current_el(env);
7460 if (el == 0) {
7461 uint64_t sctlr = arm_sctlr(env, el);
7462 if (!(sctlr & SCTLR_EnRCTX)) {
7463 return CP_ACCESS_TRAP;
7465 } else if (el == 1) {
7466 uint64_t hcr = arm_hcr_el2_eff(env);
7467 if (hcr & HCR_NV) {
7468 return CP_ACCESS_TRAP_EL2;
7471 return CP_ACCESS_OK;
7474 static const ARMCPRegInfo predinv_reginfo[] = {
7475 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7476 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7477 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7478 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7479 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7480 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7481 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7482 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7483 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7485 * Note the AArch32 opcodes have a different OPC1.
7487 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7488 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7489 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7490 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7491 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7492 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7493 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7494 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7495 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7498 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7500 /* Read the high 32 bits of the current CCSIDR */
7501 return extract64(ccsidr_read(env, ri), 32, 32);
7504 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7505 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7506 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7507 .access = PL1_R,
7508 .accessfn = access_aa64_tid2,
7509 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7512 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7513 bool isread)
7515 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7516 return CP_ACCESS_TRAP_EL2;
7519 return CP_ACCESS_OK;
7522 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7523 bool isread)
7525 if (arm_feature(env, ARM_FEATURE_V8)) {
7526 return access_aa64_tid3(env, ri, isread);
7529 return CP_ACCESS_OK;
7532 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7533 bool isread)
7535 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7536 return CP_ACCESS_TRAP_EL2;
7539 return CP_ACCESS_OK;
7542 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7543 const ARMCPRegInfo *ri, bool isread)
7546 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7547 * in v7A, not in v8A.
7549 if (!arm_feature(env, ARM_FEATURE_V8) &&
7550 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7551 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7552 return CP_ACCESS_TRAP_EL2;
7554 return CP_ACCESS_OK;
7557 static const ARMCPRegInfo jazelle_regs[] = {
7558 { .name = "JIDR",
7559 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7560 .access = PL1_R, .accessfn = access_jazelle,
7561 .type = ARM_CP_CONST, .resetvalue = 0 },
7562 { .name = "JOSCR",
7563 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7564 .accessfn = access_joscr_jmcr,
7565 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7566 { .name = "JMCR",
7567 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7568 .accessfn = access_joscr_jmcr,
7569 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7572 static const ARMCPRegInfo contextidr_el2 = {
7573 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7574 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7575 .access = PL2_RW,
7576 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7579 static const ARMCPRegInfo vhe_reginfo[] = {
7580 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7581 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7582 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7583 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7584 #ifndef CONFIG_USER_ONLY
7585 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7586 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7587 .fieldoffset =
7588 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7589 .type = ARM_CP_IO, .access = PL2_RW,
7590 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7591 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7592 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7593 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7594 .resetfn = gt_hv_timer_reset,
7595 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7596 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7597 .type = ARM_CP_IO,
7598 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7599 .access = PL2_RW,
7600 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7601 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7602 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7603 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7604 .type = ARM_CP_IO | ARM_CP_ALIAS,
7605 .access = PL2_RW, .accessfn = e2h_access,
7606 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7607 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7608 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7609 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7610 .type = ARM_CP_IO | ARM_CP_ALIAS,
7611 .access = PL2_RW, .accessfn = e2h_access,
7612 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7613 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7614 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7615 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7616 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7617 .access = PL2_RW, .accessfn = e2h_access,
7618 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7619 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7620 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7621 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7622 .access = PL2_RW, .accessfn = e2h_access,
7623 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7624 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7625 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7626 .type = ARM_CP_IO | ARM_CP_ALIAS,
7627 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7628 .access = PL2_RW, .accessfn = e2h_access,
7629 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7630 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7631 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7632 .type = ARM_CP_IO | ARM_CP_ALIAS,
7633 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7634 .access = PL2_RW, .accessfn = e2h_access,
7635 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7636 #endif
7639 #ifndef CONFIG_USER_ONLY
7640 static const ARMCPRegInfo ats1e1_reginfo[] = {
7641 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7642 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7643 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7644 .writefn = ats_write64 },
7645 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7646 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7647 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7648 .writefn = ats_write64 },
7651 static const ARMCPRegInfo ats1cp_reginfo[] = {
7652 { .name = "ATS1CPRP",
7653 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7654 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7655 .writefn = ats_write },
7656 { .name = "ATS1CPWP",
7657 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7658 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7659 .writefn = ats_write },
7661 #endif
7664 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7665 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7666 * is non-zero, which is never for ARMv7, optionally in ARMv8
7667 * and mandatorily for ARMv8.2 and up.
7668 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7669 * implementation is RAZ/WI we can ignore this detail, as we
7670 * do for ACTLR.
7672 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7673 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7674 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7675 .access = PL1_RW, .accessfn = access_tacr,
7676 .type = ARM_CP_CONST, .resetvalue = 0 },
7677 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7678 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7679 .access = PL2_RW, .type = ARM_CP_CONST,
7680 .resetvalue = 0 },
7683 void register_cp_regs_for_features(ARMCPU *cpu)
7685 /* Register all the coprocessor registers based on feature bits */
7686 CPUARMState *env = &cpu->env;
7687 if (arm_feature(env, ARM_FEATURE_M)) {
7688 /* M profile has no coprocessor registers */
7689 return;
7692 define_arm_cp_regs(cpu, cp_reginfo);
7693 if (!arm_feature(env, ARM_FEATURE_V8)) {
7694 /* Must go early as it is full of wildcards that may be
7695 * overridden by later definitions.
7697 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7700 if (arm_feature(env, ARM_FEATURE_V6)) {
7701 /* The ID registers all have impdef reset values */
7702 ARMCPRegInfo v6_idregs[] = {
7703 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7704 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7705 .access = PL1_R, .type = ARM_CP_CONST,
7706 .accessfn = access_aa32_tid3,
7707 .resetvalue = cpu->isar.id_pfr0 },
7708 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7709 * the value of the GIC field until after we define these regs.
7711 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7712 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7713 .access = PL1_R, .type = ARM_CP_NO_RAW,
7714 .accessfn = access_aa32_tid3,
7715 .readfn = id_pfr1_read,
7716 .writefn = arm_cp_write_ignore },
7717 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7718 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7719 .access = PL1_R, .type = ARM_CP_CONST,
7720 .accessfn = access_aa32_tid3,
7721 .resetvalue = cpu->isar.id_dfr0 },
7722 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7723 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7724 .access = PL1_R, .type = ARM_CP_CONST,
7725 .accessfn = access_aa32_tid3,
7726 .resetvalue = cpu->id_afr0 },
7727 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7728 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7729 .access = PL1_R, .type = ARM_CP_CONST,
7730 .accessfn = access_aa32_tid3,
7731 .resetvalue = cpu->isar.id_mmfr0 },
7732 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7733 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7734 .access = PL1_R, .type = ARM_CP_CONST,
7735 .accessfn = access_aa32_tid3,
7736 .resetvalue = cpu->isar.id_mmfr1 },
7737 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7738 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7739 .access = PL1_R, .type = ARM_CP_CONST,
7740 .accessfn = access_aa32_tid3,
7741 .resetvalue = cpu->isar.id_mmfr2 },
7742 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7743 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7744 .access = PL1_R, .type = ARM_CP_CONST,
7745 .accessfn = access_aa32_tid3,
7746 .resetvalue = cpu->isar.id_mmfr3 },
7747 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7748 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7749 .access = PL1_R, .type = ARM_CP_CONST,
7750 .accessfn = access_aa32_tid3,
7751 .resetvalue = cpu->isar.id_isar0 },
7752 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7753 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7754 .access = PL1_R, .type = ARM_CP_CONST,
7755 .accessfn = access_aa32_tid3,
7756 .resetvalue = cpu->isar.id_isar1 },
7757 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7758 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7759 .access = PL1_R, .type = ARM_CP_CONST,
7760 .accessfn = access_aa32_tid3,
7761 .resetvalue = cpu->isar.id_isar2 },
7762 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7763 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7764 .access = PL1_R, .type = ARM_CP_CONST,
7765 .accessfn = access_aa32_tid3,
7766 .resetvalue = cpu->isar.id_isar3 },
7767 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7768 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7769 .access = PL1_R, .type = ARM_CP_CONST,
7770 .accessfn = access_aa32_tid3,
7771 .resetvalue = cpu->isar.id_isar4 },
7772 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7773 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7774 .access = PL1_R, .type = ARM_CP_CONST,
7775 .accessfn = access_aa32_tid3,
7776 .resetvalue = cpu->isar.id_isar5 },
7777 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7778 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7779 .access = PL1_R, .type = ARM_CP_CONST,
7780 .accessfn = access_aa32_tid3,
7781 .resetvalue = cpu->isar.id_mmfr4 },
7782 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7783 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7784 .access = PL1_R, .type = ARM_CP_CONST,
7785 .accessfn = access_aa32_tid3,
7786 .resetvalue = cpu->isar.id_isar6 },
7788 define_arm_cp_regs(cpu, v6_idregs);
7789 define_arm_cp_regs(cpu, v6_cp_reginfo);
7790 } else {
7791 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7793 if (arm_feature(env, ARM_FEATURE_V6K)) {
7794 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7796 if (arm_feature(env, ARM_FEATURE_V7MP) &&
7797 !arm_feature(env, ARM_FEATURE_PMSA)) {
7798 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7800 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7801 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7803 if (arm_feature(env, ARM_FEATURE_V7)) {
7804 ARMCPRegInfo clidr = {
7805 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7806 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7807 .access = PL1_R, .type = ARM_CP_CONST,
7808 .accessfn = access_aa64_tid2,
7809 .resetvalue = cpu->clidr
7811 define_one_arm_cp_reg(cpu, &clidr);
7812 define_arm_cp_regs(cpu, v7_cp_reginfo);
7813 define_debug_regs(cpu);
7814 define_pmu_regs(cpu);
7815 } else {
7816 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7818 if (arm_feature(env, ARM_FEATURE_V8)) {
7819 /* AArch64 ID registers, which all have impdef reset values.
7820 * Note that within the ID register ranges the unused slots
7821 * must all RAZ, not UNDEF; future architecture versions may
7822 * define new registers here.
7824 ARMCPRegInfo v8_idregs[] = {
7826 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7827 * emulation because we don't know the right value for the
7828 * GIC field until after we define these regs.
7830 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7831 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7832 .access = PL1_R,
7833 #ifdef CONFIG_USER_ONLY
7834 .type = ARM_CP_CONST,
7835 .resetvalue = cpu->isar.id_aa64pfr0
7836 #else
7837 .type = ARM_CP_NO_RAW,
7838 .accessfn = access_aa64_tid3,
7839 .readfn = id_aa64pfr0_read,
7840 .writefn = arm_cp_write_ignore
7841 #endif
7843 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7844 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7845 .access = PL1_R, .type = ARM_CP_CONST,
7846 .accessfn = access_aa64_tid3,
7847 .resetvalue = cpu->isar.id_aa64pfr1},
7848 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7849 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7850 .access = PL1_R, .type = ARM_CP_CONST,
7851 .accessfn = access_aa64_tid3,
7852 .resetvalue = 0 },
7853 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7854 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7855 .access = PL1_R, .type = ARM_CP_CONST,
7856 .accessfn = access_aa64_tid3,
7857 .resetvalue = 0 },
7858 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7859 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7860 .access = PL1_R, .type = ARM_CP_CONST,
7861 .accessfn = access_aa64_tid3,
7862 .resetvalue = cpu->isar.id_aa64zfr0 },
7863 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7864 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7865 .access = PL1_R, .type = ARM_CP_CONST,
7866 .accessfn = access_aa64_tid3,
7867 .resetvalue = cpu->isar.id_aa64smfr0 },
7868 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7869 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7870 .access = PL1_R, .type = ARM_CP_CONST,
7871 .accessfn = access_aa64_tid3,
7872 .resetvalue = 0 },
7873 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7874 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7875 .access = PL1_R, .type = ARM_CP_CONST,
7876 .accessfn = access_aa64_tid3,
7877 .resetvalue = 0 },
7878 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7879 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7880 .access = PL1_R, .type = ARM_CP_CONST,
7881 .accessfn = access_aa64_tid3,
7882 .resetvalue = cpu->isar.id_aa64dfr0 },
7883 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7884 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7885 .access = PL1_R, .type = ARM_CP_CONST,
7886 .accessfn = access_aa64_tid3,
7887 .resetvalue = cpu->isar.id_aa64dfr1 },
7888 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7889 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7890 .access = PL1_R, .type = ARM_CP_CONST,
7891 .accessfn = access_aa64_tid3,
7892 .resetvalue = 0 },
7893 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7894 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7895 .access = PL1_R, .type = ARM_CP_CONST,
7896 .accessfn = access_aa64_tid3,
7897 .resetvalue = 0 },
7898 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7899 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7900 .access = PL1_R, .type = ARM_CP_CONST,
7901 .accessfn = access_aa64_tid3,
7902 .resetvalue = cpu->id_aa64afr0 },
7903 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7904 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7905 .access = PL1_R, .type = ARM_CP_CONST,
7906 .accessfn = access_aa64_tid3,
7907 .resetvalue = cpu->id_aa64afr1 },
7908 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7909 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7910 .access = PL1_R, .type = ARM_CP_CONST,
7911 .accessfn = access_aa64_tid3,
7912 .resetvalue = 0 },
7913 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7914 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7915 .access = PL1_R, .type = ARM_CP_CONST,
7916 .accessfn = access_aa64_tid3,
7917 .resetvalue = 0 },
7918 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7919 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7920 .access = PL1_R, .type = ARM_CP_CONST,
7921 .accessfn = access_aa64_tid3,
7922 .resetvalue = cpu->isar.id_aa64isar0 },
7923 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7924 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7925 .access = PL1_R, .type = ARM_CP_CONST,
7926 .accessfn = access_aa64_tid3,
7927 .resetvalue = cpu->isar.id_aa64isar1 },
7928 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7929 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7930 .access = PL1_R, .type = ARM_CP_CONST,
7931 .accessfn = access_aa64_tid3,
7932 .resetvalue = 0 },
7933 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7934 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7935 .access = PL1_R, .type = ARM_CP_CONST,
7936 .accessfn = access_aa64_tid3,
7937 .resetvalue = 0 },
7938 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7939 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7940 .access = PL1_R, .type = ARM_CP_CONST,
7941 .accessfn = access_aa64_tid3,
7942 .resetvalue = 0 },
7943 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7944 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7945 .access = PL1_R, .type = ARM_CP_CONST,
7946 .accessfn = access_aa64_tid3,
7947 .resetvalue = 0 },
7948 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7949 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7950 .access = PL1_R, .type = ARM_CP_CONST,
7951 .accessfn = access_aa64_tid3,
7952 .resetvalue = 0 },
7953 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7954 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7955 .access = PL1_R, .type = ARM_CP_CONST,
7956 .accessfn = access_aa64_tid3,
7957 .resetvalue = 0 },
7958 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7959 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7960 .access = PL1_R, .type = ARM_CP_CONST,
7961 .accessfn = access_aa64_tid3,
7962 .resetvalue = cpu->isar.id_aa64mmfr0 },
7963 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7965 .access = PL1_R, .type = ARM_CP_CONST,
7966 .accessfn = access_aa64_tid3,
7967 .resetvalue = cpu->isar.id_aa64mmfr1 },
7968 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
7969 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7970 .access = PL1_R, .type = ARM_CP_CONST,
7971 .accessfn = access_aa64_tid3,
7972 .resetvalue = cpu->isar.id_aa64mmfr2 },
7973 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7974 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7975 .access = PL1_R, .type = ARM_CP_CONST,
7976 .accessfn = access_aa64_tid3,
7977 .resetvalue = 0 },
7978 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7979 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7980 .access = PL1_R, .type = ARM_CP_CONST,
7981 .accessfn = access_aa64_tid3,
7982 .resetvalue = 0 },
7983 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7985 .access = PL1_R, .type = ARM_CP_CONST,
7986 .accessfn = access_aa64_tid3,
7987 .resetvalue = 0 },
7988 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7989 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7990 .access = PL1_R, .type = ARM_CP_CONST,
7991 .accessfn = access_aa64_tid3,
7992 .resetvalue = 0 },
7993 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7994 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7995 .access = PL1_R, .type = ARM_CP_CONST,
7996 .accessfn = access_aa64_tid3,
7997 .resetvalue = 0 },
7998 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7999 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8000 .access = PL1_R, .type = ARM_CP_CONST,
8001 .accessfn = access_aa64_tid3,
8002 .resetvalue = cpu->isar.mvfr0 },
8003 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8005 .access = PL1_R, .type = ARM_CP_CONST,
8006 .accessfn = access_aa64_tid3,
8007 .resetvalue = cpu->isar.mvfr1 },
8008 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8009 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8010 .access = PL1_R, .type = ARM_CP_CONST,
8011 .accessfn = access_aa64_tid3,
8012 .resetvalue = cpu->isar.mvfr2 },
8013 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8014 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8015 .access = PL1_R, .type = ARM_CP_CONST,
8016 .accessfn = access_aa64_tid3,
8017 .resetvalue = 0 },
8018 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8019 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8020 .access = PL1_R, .type = ARM_CP_CONST,
8021 .accessfn = access_aa64_tid3,
8022 .resetvalue = cpu->isar.id_pfr2 },
8023 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8025 .access = PL1_R, .type = ARM_CP_CONST,
8026 .accessfn = access_aa64_tid3,
8027 .resetvalue = 0 },
8028 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8029 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8030 .access = PL1_R, .type = ARM_CP_CONST,
8031 .accessfn = access_aa64_tid3,
8032 .resetvalue = 0 },
8033 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8034 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8035 .access = PL1_R, .type = ARM_CP_CONST,
8036 .accessfn = access_aa64_tid3,
8037 .resetvalue = 0 },
8038 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8039 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8040 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8041 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8042 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8043 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8044 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8045 .resetvalue = cpu->pmceid0 },
8046 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8047 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8048 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8049 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8050 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8051 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8052 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8053 .resetvalue = cpu->pmceid1 },
8055 #ifdef CONFIG_USER_ONLY
8056 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8057 { .name = "ID_AA64PFR0_EL1",
8058 .exported_bits = 0x000f000f00ff0000,
8059 .fixed_bits = 0x0000000000000011 },
8060 { .name = "ID_AA64PFR1_EL1",
8061 .exported_bits = 0x00000000000000f0 },
8062 { .name = "ID_AA64PFR*_EL1_RESERVED",
8063 .is_glob = true },
8064 { .name = "ID_AA64ZFR0_EL1" },
8065 { .name = "ID_AA64MMFR0_EL1",
8066 .fixed_bits = 0x00000000ff000000 },
8067 { .name = "ID_AA64MMFR1_EL1" },
8068 { .name = "ID_AA64MMFR*_EL1_RESERVED",
8069 .is_glob = true },
8070 { .name = "ID_AA64DFR0_EL1",
8071 .fixed_bits = 0x0000000000000006 },
8072 { .name = "ID_AA64DFR1_EL1" },
8073 { .name = "ID_AA64DFR*_EL1_RESERVED",
8074 .is_glob = true },
8075 { .name = "ID_AA64AFR*",
8076 .is_glob = true },
8077 { .name = "ID_AA64ISAR0_EL1",
8078 .exported_bits = 0x00fffffff0fffff0 },
8079 { .name = "ID_AA64ISAR1_EL1",
8080 .exported_bits = 0x000000f0ffffffff },
8081 { .name = "ID_AA64ISAR*_EL1_RESERVED",
8082 .is_glob = true },
8084 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8085 #endif
8086 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
8087 if (!arm_feature(env, ARM_FEATURE_EL3) &&
8088 !arm_feature(env, ARM_FEATURE_EL2)) {
8089 ARMCPRegInfo rvbar = {
8090 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
8091 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8092 .access = PL1_R,
8093 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8095 define_one_arm_cp_reg(cpu, &rvbar);
8097 define_arm_cp_regs(cpu, v8_idregs);
8098 define_arm_cp_regs(cpu, v8_cp_reginfo);
8102 * Register the base EL2 cpregs.
8103 * Pre v8, these registers are implemented only as part of the
8104 * Virtualization Extensions (EL2 present). Beginning with v8,
8105 * if EL2 is missing but EL3 is enabled, mostly these become
8106 * RES0 from EL3, with some specific exceptions.
8108 if (arm_feature(env, ARM_FEATURE_EL2)
8109 || (arm_feature(env, ARM_FEATURE_EL3)
8110 && arm_feature(env, ARM_FEATURE_V8))) {
8111 uint64_t vmpidr_def = mpidr_read_val(env);
8112 ARMCPRegInfo vpidr_regs[] = {
8113 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8114 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8115 .access = PL2_RW, .accessfn = access_el3_aa32ns,
8116 .resetvalue = cpu->midr,
8117 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8118 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8119 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8120 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8121 .access = PL2_RW, .resetvalue = cpu->midr,
8122 .type = ARM_CP_EL3_NO_EL2_C_NZ,
8123 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8124 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8125 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8126 .access = PL2_RW, .accessfn = access_el3_aa32ns,
8127 .resetvalue = vmpidr_def,
8128 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8129 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8130 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8131 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8132 .access = PL2_RW, .resetvalue = vmpidr_def,
8133 .type = ARM_CP_EL3_NO_EL2_C_NZ,
8134 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8137 * The only field of MDCR_EL2 that has a defined architectural reset
8138 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8140 ARMCPRegInfo mdcr_el2 = {
8141 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
8142 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8143 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8144 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8146 define_one_arm_cp_reg(cpu, &mdcr_el2);
8147 define_arm_cp_regs(cpu, vpidr_regs);
8148 define_arm_cp_regs(cpu, el2_cp_reginfo);
8149 if (arm_feature(env, ARM_FEATURE_V8)) {
8150 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8152 if (cpu_isar_feature(aa64_sel2, cpu)) {
8153 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8155 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
8156 if (!arm_feature(env, ARM_FEATURE_EL3)) {
8157 ARMCPRegInfo rvbar = {
8158 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8159 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8160 .access = PL2_R,
8161 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8163 define_one_arm_cp_reg(cpu, &rvbar);
8167 /* Register the base EL3 cpregs. */
8168 if (arm_feature(env, ARM_FEATURE_EL3)) {
8169 define_arm_cp_regs(cpu, el3_cp_reginfo);
8170 ARMCPRegInfo el3_regs[] = {
8171 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8172 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8173 .access = PL3_R,
8174 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8176 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8177 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8178 .access = PL3_RW,
8179 .raw_writefn = raw_write, .writefn = sctlr_write,
8180 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8181 .resetvalue = cpu->reset_sctlr },
8184 define_arm_cp_regs(cpu, el3_regs);
8186 /* The behaviour of NSACR is sufficiently various that we don't
8187 * try to describe it in a single reginfo:
8188 * if EL3 is 64 bit, then trap to EL3 from S EL1,
8189 * reads as constant 0xc00 from NS EL1 and NS EL2
8190 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8191 * if v7 without EL3, register doesn't exist
8192 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8194 if (arm_feature(env, ARM_FEATURE_EL3)) {
8195 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8196 static const ARMCPRegInfo nsacr = {
8197 .name = "NSACR", .type = ARM_CP_CONST,
8198 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8199 .access = PL1_RW, .accessfn = nsacr_access,
8200 .resetvalue = 0xc00
8202 define_one_arm_cp_reg(cpu, &nsacr);
8203 } else {
8204 static const ARMCPRegInfo nsacr = {
8205 .name = "NSACR",
8206 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8207 .access = PL3_RW | PL1_R,
8208 .resetvalue = 0,
8209 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8211 define_one_arm_cp_reg(cpu, &nsacr);
8213 } else {
8214 if (arm_feature(env, ARM_FEATURE_V8)) {
8215 static const ARMCPRegInfo nsacr = {
8216 .name = "NSACR", .type = ARM_CP_CONST,
8217 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8218 .access = PL1_R,
8219 .resetvalue = 0xc00
8221 define_one_arm_cp_reg(cpu, &nsacr);
8225 if (arm_feature(env, ARM_FEATURE_PMSA)) {
8226 if (arm_feature(env, ARM_FEATURE_V6)) {
8227 /* PMSAv6 not implemented */
8228 assert(arm_feature(env, ARM_FEATURE_V7));
8229 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8230 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8231 } else {
8232 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8234 } else {
8235 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8236 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8237 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
8238 if (cpu_isar_feature(aa32_hpd, cpu)) {
8239 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8242 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8243 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8245 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8246 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8248 if (arm_feature(env, ARM_FEATURE_VAPA)) {
8249 define_arm_cp_regs(cpu, vapa_cp_reginfo);
8251 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8252 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8254 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8255 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8257 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8258 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8260 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8261 define_arm_cp_regs(cpu, omap_cp_reginfo);
8263 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8264 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8266 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8267 define_arm_cp_regs(cpu, xscale_cp_reginfo);
8269 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8270 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8272 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8273 define_arm_cp_regs(cpu, lpae_cp_reginfo);
8275 if (cpu_isar_feature(aa32_jazelle, cpu)) {
8276 define_arm_cp_regs(cpu, jazelle_regs);
8278 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
8279 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8280 * be read-only (ie write causes UNDEF exception).
8283 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8284 /* Pre-v8 MIDR space.
8285 * Note that the MIDR isn't a simple constant register because
8286 * of the TI925 behaviour where writes to another register can
8287 * cause the MIDR value to change.
8289 * Unimplemented registers in the c15 0 0 0 space default to
8290 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8291 * and friends override accordingly.
8293 { .name = "MIDR",
8294 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8295 .access = PL1_R, .resetvalue = cpu->midr,
8296 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8297 .readfn = midr_read,
8298 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8299 .type = ARM_CP_OVERRIDE },
8300 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8301 { .name = "DUMMY",
8302 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8303 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8304 { .name = "DUMMY",
8305 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8306 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8307 { .name = "DUMMY",
8308 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8309 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8310 { .name = "DUMMY",
8311 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8312 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8313 { .name = "DUMMY",
8314 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8315 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8317 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8318 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8319 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8320 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8321 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8322 .readfn = midr_read },
8323 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8324 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8325 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8326 .access = PL1_R, .resetvalue = cpu->midr },
8327 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8328 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8329 .access = PL1_R, .resetvalue = cpu->midr },
8330 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8331 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8332 .access = PL1_R,
8333 .accessfn = access_aa64_tid1,
8334 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8336 ARMCPRegInfo id_cp_reginfo[] = {
8337 /* These are common to v8 and pre-v8 */
8338 { .name = "CTR",
8339 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8340 .access = PL1_R, .accessfn = ctr_el0_access,
8341 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8342 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8343 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8344 .access = PL0_R, .accessfn = ctr_el0_access,
8345 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8346 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8347 { .name = "TCMTR",
8348 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8349 .access = PL1_R,
8350 .accessfn = access_aa32_tid1,
8351 .type = ARM_CP_CONST, .resetvalue = 0 },
8353 /* TLBTR is specific to VMSA */
8354 ARMCPRegInfo id_tlbtr_reginfo = {
8355 .name = "TLBTR",
8356 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8357 .access = PL1_R,
8358 .accessfn = access_aa32_tid1,
8359 .type = ARM_CP_CONST, .resetvalue = 0,
8361 /* MPUIR is specific to PMSA V6+ */
8362 ARMCPRegInfo id_mpuir_reginfo = {
8363 .name = "MPUIR",
8364 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8365 .access = PL1_R, .type = ARM_CP_CONST,
8366 .resetvalue = cpu->pmsav7_dregion << 8
8368 static const ARMCPRegInfo crn0_wi_reginfo = {
8369 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8370 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8371 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8373 #ifdef CONFIG_USER_ONLY
8374 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8375 { .name = "MIDR_EL1",
8376 .exported_bits = 0x00000000ffffffff },
8377 { .name = "REVIDR_EL1" },
8379 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8380 #endif
8381 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8382 arm_feature(env, ARM_FEATURE_STRONGARM)) {
8383 size_t i;
8384 /* Register the blanket "writes ignored" value first to cover the
8385 * whole space. Then update the specific ID registers to allow write
8386 * access, so that they ignore writes rather than causing them to
8387 * UNDEF.
8389 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8390 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8391 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8393 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8394 id_cp_reginfo[i].access = PL1_RW;
8396 id_mpuir_reginfo.access = PL1_RW;
8397 id_tlbtr_reginfo.access = PL1_RW;
8399 if (arm_feature(env, ARM_FEATURE_V8)) {
8400 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8401 } else {
8402 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8404 define_arm_cp_regs(cpu, id_cp_reginfo);
8405 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8406 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8407 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8408 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8412 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8413 ARMCPRegInfo mpidr_cp_reginfo[] = {
8414 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8415 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8416 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8418 #ifdef CONFIG_USER_ONLY
8419 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8420 { .name = "MPIDR_EL1",
8421 .fixed_bits = 0x0000000080000000 },
8423 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8424 #endif
8425 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8428 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8429 ARMCPRegInfo auxcr_reginfo[] = {
8430 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8431 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8432 .access = PL1_RW, .accessfn = access_tacr,
8433 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8434 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8435 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8436 .access = PL2_RW, .type = ARM_CP_CONST,
8437 .resetvalue = 0 },
8438 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8439 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8440 .access = PL3_RW, .type = ARM_CP_CONST,
8441 .resetvalue = 0 },
8443 define_arm_cp_regs(cpu, auxcr_reginfo);
8444 if (cpu_isar_feature(aa32_ac2, cpu)) {
8445 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8449 if (arm_feature(env, ARM_FEATURE_CBAR)) {
8451 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8452 * There are two flavours:
8453 * (1) older 32-bit only cores have a simple 32-bit CBAR
8454 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8455 * 32-bit register visible to AArch32 at a different encoding
8456 * to the "flavour 1" register and with the bits rearranged to
8457 * be able to squash a 64-bit address into the 32-bit view.
8458 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8459 * in future if we support AArch32-only configs of some of the
8460 * AArch64 cores we might need to add a specific feature flag
8461 * to indicate cores with "flavour 2" CBAR.
8463 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8464 /* 32 bit view is [31:18] 0...0 [43:32]. */
8465 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8466 | extract64(cpu->reset_cbar, 32, 12);
8467 ARMCPRegInfo cbar_reginfo[] = {
8468 { .name = "CBAR",
8469 .type = ARM_CP_CONST,
8470 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8471 .access = PL1_R, .resetvalue = cbar32 },
8472 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8473 .type = ARM_CP_CONST,
8474 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8475 .access = PL1_R, .resetvalue = cpu->reset_cbar },
8477 /* We don't implement a r/w 64 bit CBAR currently */
8478 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8479 define_arm_cp_regs(cpu, cbar_reginfo);
8480 } else {
8481 ARMCPRegInfo cbar = {
8482 .name = "CBAR",
8483 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8484 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8485 .fieldoffset = offsetof(CPUARMState,
8486 cp15.c15_config_base_address)
8488 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8489 cbar.access = PL1_R;
8490 cbar.fieldoffset = 0;
8491 cbar.type = ARM_CP_CONST;
8493 define_one_arm_cp_reg(cpu, &cbar);
8497 if (arm_feature(env, ARM_FEATURE_VBAR)) {
8498 static const ARMCPRegInfo vbar_cp_reginfo[] = {
8499 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8500 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8501 .access = PL1_RW, .writefn = vbar_write,
8502 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8503 offsetof(CPUARMState, cp15.vbar_ns) },
8504 .resetvalue = 0 },
8506 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8509 /* Generic registers whose values depend on the implementation */
8511 ARMCPRegInfo sctlr = {
8512 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8513 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8514 .access = PL1_RW, .accessfn = access_tvm_trvm,
8515 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8516 offsetof(CPUARMState, cp15.sctlr_ns) },
8517 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8518 .raw_writefn = raw_write,
8520 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8521 /* Normally we would always end the TB on an SCTLR write, but Linux
8522 * arch/arm/mach-pxa/sleep.S expects two instructions following
8523 * an MMU enable to execute from cache. Imitate this behaviour.
8525 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8527 define_one_arm_cp_reg(cpu, &sctlr);
8530 if (cpu_isar_feature(aa64_lor, cpu)) {
8531 define_arm_cp_regs(cpu, lor_reginfo);
8533 if (cpu_isar_feature(aa64_pan, cpu)) {
8534 define_one_arm_cp_reg(cpu, &pan_reginfo);
8536 #ifndef CONFIG_USER_ONLY
8537 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8538 define_arm_cp_regs(cpu, ats1e1_reginfo);
8540 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8541 define_arm_cp_regs(cpu, ats1cp_reginfo);
8543 #endif
8544 if (cpu_isar_feature(aa64_uao, cpu)) {
8545 define_one_arm_cp_reg(cpu, &uao_reginfo);
8548 if (cpu_isar_feature(aa64_dit, cpu)) {
8549 define_one_arm_cp_reg(cpu, &dit_reginfo);
8551 if (cpu_isar_feature(aa64_ssbs, cpu)) {
8552 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8554 if (cpu_isar_feature(any_ras, cpu)) {
8555 define_arm_cp_regs(cpu, minimal_ras_reginfo);
8558 if (cpu_isar_feature(aa64_vh, cpu) ||
8559 cpu_isar_feature(aa64_debugv8p2, cpu)) {
8560 define_one_arm_cp_reg(cpu, &contextidr_el2);
8562 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8563 define_arm_cp_regs(cpu, vhe_reginfo);
8566 if (cpu_isar_feature(aa64_sve, cpu)) {
8567 define_arm_cp_regs(cpu, zcr_reginfo);
8570 if (cpu_isar_feature(aa64_hcx, cpu)) {
8571 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8574 #ifdef TARGET_AARCH64
8575 if (cpu_isar_feature(aa64_sme, cpu)) {
8576 define_arm_cp_regs(cpu, sme_reginfo);
8578 if (cpu_isar_feature(aa64_pauth, cpu)) {
8579 define_arm_cp_regs(cpu, pauth_reginfo);
8581 if (cpu_isar_feature(aa64_rndr, cpu)) {
8582 define_arm_cp_regs(cpu, rndr_reginfo);
8584 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8585 define_arm_cp_regs(cpu, tlbirange_reginfo);
8587 if (cpu_isar_feature(aa64_tlbios, cpu)) {
8588 define_arm_cp_regs(cpu, tlbios_reginfo);
8590 #ifndef CONFIG_USER_ONLY
8591 /* Data Cache clean instructions up to PoP */
8592 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8593 define_one_arm_cp_reg(cpu, dcpop_reg);
8595 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8596 define_one_arm_cp_reg(cpu, dcpodp_reg);
8599 #endif /*CONFIG_USER_ONLY*/
8602 * If full MTE is enabled, add all of the system registers.
8603 * If only "instructions available at EL0" are enabled,
8604 * then define only a RAZ/WI version of PSTATE.TCO.
8606 if (cpu_isar_feature(aa64_mte, cpu)) {
8607 define_arm_cp_regs(cpu, mte_reginfo);
8608 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8609 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8610 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8611 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8614 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8615 define_arm_cp_regs(cpu, scxtnum_reginfo);
8617 #endif
8619 if (cpu_isar_feature(any_predinv, cpu)) {
8620 define_arm_cp_regs(cpu, predinv_reginfo);
8623 if (cpu_isar_feature(any_ccidx, cpu)) {
8624 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8627 #ifndef CONFIG_USER_ONLY
8629 * Register redirections and aliases must be done last,
8630 * after the registers from the other extensions have been defined.
8632 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8633 define_arm_vh_e2h_redirects_aliases(cpu);
8635 #endif
8638 /* Sort alphabetically by type name, except for "any". */
8639 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
8641 ObjectClass *class_a = (ObjectClass *)a;
8642 ObjectClass *class_b = (ObjectClass *)b;
8643 const char *name_a, *name_b;
8645 name_a = object_class_get_name(class_a);
8646 name_b = object_class_get_name(class_b);
8647 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
8648 return 1;
8649 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
8650 return -1;
8651 } else {
8652 return strcmp(name_a, name_b);
8656 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
8658 ObjectClass *oc = data;
8659 const char *typename;
8660 char *name;
8662 typename = object_class_get_name(oc);
8663 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
8664 qemu_printf(" %s\n", name);
8665 g_free(name);
8668 void arm_cpu_list(void)
8670 GSList *list;
8672 list = object_class_get_list(TYPE_ARM_CPU, false);
8673 list = g_slist_sort(list, arm_cpu_list_compare);
8674 qemu_printf("Available CPUs:\n");
8675 g_slist_foreach(list, arm_cpu_list_entry, NULL);
8676 g_slist_free(list);
8679 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8681 ObjectClass *oc = data;
8682 CpuDefinitionInfoList **cpu_list = user_data;
8683 CpuDefinitionInfo *info;
8684 const char *typename;
8686 typename = object_class_get_name(oc);
8687 info = g_malloc0(sizeof(*info));
8688 info->name = g_strndup(typename,
8689 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8690 info->q_typename = g_strdup(typename);
8692 QAPI_LIST_PREPEND(*cpu_list, info);
8695 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
8697 CpuDefinitionInfoList *cpu_list = NULL;
8698 GSList *list;
8700 list = object_class_get_list(TYPE_ARM_CPU, false);
8701 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8702 g_slist_free(list);
8704 return cpu_list;
8708 * Private utility function for define_one_arm_cp_reg_with_opaque():
8709 * add a single reginfo struct to the hash table.
8711 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
8712 void *opaque, CPState state,
8713 CPSecureState secstate,
8714 int crm, int opc1, int opc2,
8715 const char *name)
8717 CPUARMState *env = &cpu->env;
8718 uint32_t key;
8719 ARMCPRegInfo *r2;
8720 bool is64 = r->type & ARM_CP_64BIT;
8721 bool ns = secstate & ARM_CP_SECSTATE_NS;
8722 int cp = r->cp;
8723 size_t name_len;
8724 bool make_const;
8726 switch (state) {
8727 case ARM_CP_STATE_AA32:
8728 /* We assume it is a cp15 register if the .cp field is left unset. */
8729 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8730 cp = 15;
8732 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8733 break;
8734 case ARM_CP_STATE_AA64:
8736 * To allow abbreviation of ARMCPRegInfo definitions, we treat
8737 * cp == 0 as equivalent to the value for "standard guest-visible
8738 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
8739 * in their AArch64 view (the .cp value may be non-zero for the
8740 * benefit of the AArch32 view).
8742 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8743 cp = CP_REG_ARM64_SYSREG_CP;
8745 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8746 break;
8747 default:
8748 g_assert_not_reached();
8751 /* Overriding of an existing definition must be explicitly requested. */
8752 if (!(r->type & ARM_CP_OVERRIDE)) {
8753 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8754 if (oldreg) {
8755 assert(oldreg->type & ARM_CP_OVERRIDE);
8760 * Eliminate registers that are not present because the EL is missing.
8761 * Doing this here makes it easier to put all registers for a given
8762 * feature into the same ARMCPRegInfo array and define them all at once.
8764 make_const = false;
8765 if (arm_feature(env, ARM_FEATURE_EL3)) {
8767 * An EL2 register without EL2 but with EL3 is (usually) RES0.
8768 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8770 int min_el = ctz32(r->access) / 2;
8771 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8772 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8773 return;
8775 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8777 } else {
8778 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8779 ? PL2_RW : PL1_RW);
8780 if ((r->access & max_el) == 0) {
8781 return;
8785 /* Combine cpreg and name into one allocation. */
8786 name_len = strlen(name) + 1;
8787 r2 = g_malloc(sizeof(*r2) + name_len);
8788 *r2 = *r;
8789 r2->name = memcpy(r2 + 1, name, name_len);
8792 * Update fields to match the instantiation, overwiting wildcards
8793 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
8795 r2->cp = cp;
8796 r2->crm = crm;
8797 r2->opc1 = opc1;
8798 r2->opc2 = opc2;
8799 r2->state = state;
8800 r2->secure = secstate;
8801 if (opaque) {
8802 r2->opaque = opaque;
8805 if (make_const) {
8806 /* This should not have been a very special register to begin. */
8807 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8808 assert(old_special == 0 || old_special == ARM_CP_NOP);
8810 * Set the special function to CONST, retaining the other flags.
8811 * This is important for e.g. ARM_CP_SVE so that we still
8812 * take the SVE trap if CPTR_EL3.EZ == 0.
8814 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8816 * Usually, these registers become RES0, but there are a few
8817 * special cases like VPIDR_EL2 which have a constant non-zero
8818 * value with writes ignored.
8820 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8821 r2->resetvalue = 0;
8824 * ARM_CP_CONST has precedence, so removing the callbacks and
8825 * offsets are not strictly necessary, but it is potentially
8826 * less confusing to debug later.
8828 r2->readfn = NULL;
8829 r2->writefn = NULL;
8830 r2->raw_readfn = NULL;
8831 r2->raw_writefn = NULL;
8832 r2->resetfn = NULL;
8833 r2->fieldoffset = 0;
8834 r2->bank_fieldoffsets[0] = 0;
8835 r2->bank_fieldoffsets[1] = 0;
8836 } else {
8837 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
8839 if (isbanked) {
8841 * Register is banked (using both entries in array).
8842 * Overwriting fieldoffset as the array is only used to define
8843 * banked registers but later only fieldoffset is used.
8845 r2->fieldoffset = r->bank_fieldoffsets[ns];
8847 if (state == ARM_CP_STATE_AA32) {
8848 if (isbanked) {
8850 * If the register is banked then we don't need to migrate or
8851 * reset the 32-bit instance in certain cases:
8853 * 1) If the register has both 32-bit and 64-bit instances
8854 * then we can count on the 64-bit instance taking care
8855 * of the non-secure bank.
8856 * 2) If ARMv8 is enabled then we can count on a 64-bit
8857 * version taking care of the secure bank. This requires
8858 * that separate 32 and 64-bit definitions are provided.
8860 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8861 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8862 r2->type |= ARM_CP_ALIAS;
8864 } else if ((secstate != r->secure) && !ns) {
8866 * The register is not banked so we only want to allow
8867 * migration of the non-secure instance.
8869 r2->type |= ARM_CP_ALIAS;
8872 if (HOST_BIG_ENDIAN &&
8873 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8874 r2->fieldoffset += sizeof(uint32_t);
8880 * By convention, for wildcarded registers only the first
8881 * entry is used for migration; the others are marked as
8882 * ALIAS so we don't try to transfer the register
8883 * multiple times. Special registers (ie NOP/WFI) are
8884 * never migratable and not even raw-accessible.
8886 if (r2->type & ARM_CP_SPECIAL_MASK) {
8887 r2->type |= ARM_CP_NO_RAW;
8889 if (((r->crm == CP_ANY) && crm != 0) ||
8890 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8891 ((r->opc2 == CP_ANY) && opc2 != 0)) {
8892 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
8896 * Check that raw accesses are either forbidden or handled. Note that
8897 * we can't assert this earlier because the setup of fieldoffset for
8898 * banked registers has to be done first.
8900 if (!(r2->type & ARM_CP_NO_RAW)) {
8901 assert(!raw_accessors_invalid(r2));
8904 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
8908 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8909 const ARMCPRegInfo *r, void *opaque)
8911 /* Define implementations of coprocessor registers.
8912 * We store these in a hashtable because typically
8913 * there are less than 150 registers in a space which
8914 * is 16*16*16*8*8 = 262144 in size.
8915 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8916 * If a register is defined twice then the second definition is
8917 * used, so this can be used to define some generic registers and
8918 * then override them with implementation specific variations.
8919 * At least one of the original and the second definition should
8920 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8921 * against accidental use.
8923 * The state field defines whether the register is to be
8924 * visible in the AArch32 or AArch64 execution state. If the
8925 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8926 * reginfo structure for the AArch32 view, which sees the lower
8927 * 32 bits of the 64 bit register.
8929 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8930 * be wildcarded. AArch64 registers are always considered to be 64
8931 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8932 * the register, if any.
8934 int crm, opc1, opc2;
8935 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8936 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8937 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8938 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8939 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8940 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8941 CPState state;
8943 /* 64 bit registers have only CRm and Opc1 fields */
8944 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
8945 /* op0 only exists in the AArch64 encodings */
8946 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8947 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8948 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8950 * This API is only for Arm's system coprocessors (14 and 15) or
8951 * (M-profile or v7A-and-earlier only) for implementation defined
8952 * coprocessors in the range 0..7. Our decode assumes this, since
8953 * 8..13 can be used for other insns including VFP and Neon. See
8954 * valid_cp() in translate.c. Assert here that we haven't tried
8955 * to use an invalid coprocessor number.
8957 switch (r->state) {
8958 case ARM_CP_STATE_BOTH:
8959 /* 0 has a special meaning, but otherwise the same rules as AA32. */
8960 if (r->cp == 0) {
8961 break;
8963 /* fall through */
8964 case ARM_CP_STATE_AA32:
8965 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8966 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8967 assert(r->cp >= 14 && r->cp <= 15);
8968 } else {
8969 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8971 break;
8972 case ARM_CP_STATE_AA64:
8973 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8974 break;
8975 default:
8976 g_assert_not_reached();
8978 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8979 * encodes a minimum access level for the register. We roll this
8980 * runtime check into our general permission check code, so check
8981 * here that the reginfo's specified permissions are strict enough
8982 * to encompass the generic architectural permission check.
8984 if (r->state != ARM_CP_STATE_AA32) {
8985 CPAccessRights mask;
8986 switch (r->opc1) {
8987 case 0:
8988 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8989 mask = PL0U_R | PL1_RW;
8990 break;
8991 case 1: case 2:
8992 /* min_EL EL1 */
8993 mask = PL1_RW;
8994 break;
8995 case 3:
8996 /* min_EL EL0 */
8997 mask = PL0_RW;
8998 break;
8999 case 4:
9000 case 5:
9001 /* min_EL EL2 */
9002 mask = PL2_RW;
9003 break;
9004 case 6:
9005 /* min_EL EL3 */
9006 mask = PL3_RW;
9007 break;
9008 case 7:
9009 /* min_EL EL1, secure mode only (we don't check the latter) */
9010 mask = PL1_RW;
9011 break;
9012 default:
9013 /* broken reginfo with out-of-range opc1 */
9014 g_assert_not_reached();
9016 /* assert our permissions are not too lax (stricter is fine) */
9017 assert((r->access & ~mask) == 0);
9020 /* Check that the register definition has enough info to handle
9021 * reads and writes if they are permitted.
9023 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9024 if (r->access & PL3_R) {
9025 assert((r->fieldoffset ||
9026 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9027 r->readfn);
9029 if (r->access & PL3_W) {
9030 assert((r->fieldoffset ||
9031 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9032 r->writefn);
9036 for (crm = crmmin; crm <= crmmax; crm++) {
9037 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9038 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9039 for (state = ARM_CP_STATE_AA32;
9040 state <= ARM_CP_STATE_AA64; state++) {
9041 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9042 continue;
9044 if (state == ARM_CP_STATE_AA32) {
9045 /* Under AArch32 CP registers can be common
9046 * (same for secure and non-secure world) or banked.
9048 char *name;
9050 switch (r->secure) {
9051 case ARM_CP_SECSTATE_S:
9052 case ARM_CP_SECSTATE_NS:
9053 add_cpreg_to_hashtable(cpu, r, opaque, state,
9054 r->secure, crm, opc1, opc2,
9055 r->name);
9056 break;
9057 case ARM_CP_SECSTATE_BOTH:
9058 name = g_strdup_printf("%s_S", r->name);
9059 add_cpreg_to_hashtable(cpu, r, opaque, state,
9060 ARM_CP_SECSTATE_S,
9061 crm, opc1, opc2, name);
9062 g_free(name);
9063 add_cpreg_to_hashtable(cpu, r, opaque, state,
9064 ARM_CP_SECSTATE_NS,
9065 crm, opc1, opc2, r->name);
9066 break;
9067 default:
9068 g_assert_not_reached();
9070 } else {
9071 /* AArch64 registers get mapped to non-secure instance
9072 * of AArch32 */
9073 add_cpreg_to_hashtable(cpu, r, opaque, state,
9074 ARM_CP_SECSTATE_NS,
9075 crm, opc1, opc2, r->name);
9083 /* Define a whole list of registers */
9084 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9085 void *opaque, size_t len)
9087 size_t i;
9088 for (i = 0; i < len; ++i) {
9089 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9094 * Modify ARMCPRegInfo for access from userspace.
9096 * This is a data driven modification directed by
9097 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9098 * user-space cannot alter any values and dynamic values pertaining to
9099 * execution state are hidden from user space view anyway.
9101 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9102 const ARMCPRegUserSpaceInfo *mods,
9103 size_t mods_len)
9105 for (size_t mi = 0; mi < mods_len; ++mi) {
9106 const ARMCPRegUserSpaceInfo *m = mods + mi;
9107 GPatternSpec *pat = NULL;
9109 if (m->is_glob) {
9110 pat = g_pattern_spec_new(m->name);
9112 for (size_t ri = 0; ri < regs_len; ++ri) {
9113 ARMCPRegInfo *r = regs + ri;
9115 if (pat && g_pattern_match_string(pat, r->name)) {
9116 r->type = ARM_CP_CONST;
9117 r->access = PL0U_R;
9118 r->resetvalue = 0;
9119 /* continue */
9120 } else if (strcmp(r->name, m->name) == 0) {
9121 r->type = ARM_CP_CONST;
9122 r->access = PL0U_R;
9123 r->resetvalue &= m->exported_bits;
9124 r->resetvalue |= m->fixed_bits;
9125 break;
9128 if (pat) {
9129 g_pattern_spec_free(pat);
9134 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9136 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9139 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9140 uint64_t value)
9142 /* Helper coprocessor write function for write-ignore registers */
9145 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9147 /* Helper coprocessor write function for read-as-zero registers */
9148 return 0;
9151 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9153 /* Helper coprocessor reset function for do-nothing-on-reset registers */
9156 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9158 /* Return true if it is not valid for us to switch to
9159 * this CPU mode (ie all the UNPREDICTABLE cases in
9160 * the ARM ARM CPSRWriteByInstr pseudocode).
9163 /* Changes to or from Hyp via MSR and CPS are illegal. */
9164 if (write_type == CPSRWriteByInstr &&
9165 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9166 mode == ARM_CPU_MODE_HYP)) {
9167 return 1;
9170 switch (mode) {
9171 case ARM_CPU_MODE_USR:
9172 return 0;
9173 case ARM_CPU_MODE_SYS:
9174 case ARM_CPU_MODE_SVC:
9175 case ARM_CPU_MODE_ABT:
9176 case ARM_CPU_MODE_UND:
9177 case ARM_CPU_MODE_IRQ:
9178 case ARM_CPU_MODE_FIQ:
9179 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
9180 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9182 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9183 * and CPS are treated as illegal mode changes.
9185 if (write_type == CPSRWriteByInstr &&
9186 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9187 (arm_hcr_el2_eff(env) & HCR_TGE)) {
9188 return 1;
9190 return 0;
9191 case ARM_CPU_MODE_HYP:
9192 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9193 case ARM_CPU_MODE_MON:
9194 return arm_current_el(env) < 3;
9195 default:
9196 return 1;
9200 uint32_t cpsr_read(CPUARMState *env)
9202 int ZF;
9203 ZF = (env->ZF == 0);
9204 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9205 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9206 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9207 | ((env->condexec_bits & 0xfc) << 8)
9208 | (env->GE << 16) | (env->daif & CPSR_AIF);
9211 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9212 CPSRWriteType write_type)
9214 uint32_t changed_daif;
9215 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9216 (mask & (CPSR_M | CPSR_E | CPSR_IL));
9218 if (mask & CPSR_NZCV) {
9219 env->ZF = (~val) & CPSR_Z;
9220 env->NF = val;
9221 env->CF = (val >> 29) & 1;
9222 env->VF = (val << 3) & 0x80000000;
9224 if (mask & CPSR_Q)
9225 env->QF = ((val & CPSR_Q) != 0);
9226 if (mask & CPSR_T)
9227 env->thumb = ((val & CPSR_T) != 0);
9228 if (mask & CPSR_IT_0_1) {
9229 env->condexec_bits &= ~3;
9230 env->condexec_bits |= (val >> 25) & 3;
9232 if (mask & CPSR_IT_2_7) {
9233 env->condexec_bits &= 3;
9234 env->condexec_bits |= (val >> 8) & 0xfc;
9236 if (mask & CPSR_GE) {
9237 env->GE = (val >> 16) & 0xf;
9240 /* In a V7 implementation that includes the security extensions but does
9241 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9242 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9243 * bits respectively.
9245 * In a V8 implementation, it is permitted for privileged software to
9246 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9248 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9249 arm_feature(env, ARM_FEATURE_EL3) &&
9250 !arm_feature(env, ARM_FEATURE_EL2) &&
9251 !arm_is_secure(env)) {
9253 changed_daif = (env->daif ^ val) & mask;
9255 if (changed_daif & CPSR_A) {
9256 /* Check to see if we are allowed to change the masking of async
9257 * abort exceptions from a non-secure state.
9259 if (!(env->cp15.scr_el3 & SCR_AW)) {
9260 qemu_log_mask(LOG_GUEST_ERROR,
9261 "Ignoring attempt to switch CPSR_A flag from "
9262 "non-secure world with SCR.AW bit clear\n");
9263 mask &= ~CPSR_A;
9267 if (changed_daif & CPSR_F) {
9268 /* Check to see if we are allowed to change the masking of FIQ
9269 * exceptions from a non-secure state.
9271 if (!(env->cp15.scr_el3 & SCR_FW)) {
9272 qemu_log_mask(LOG_GUEST_ERROR,
9273 "Ignoring attempt to switch CPSR_F flag from "
9274 "non-secure world with SCR.FW bit clear\n");
9275 mask &= ~CPSR_F;
9278 /* Check whether non-maskable FIQ (NMFI) support is enabled.
9279 * If this bit is set software is not allowed to mask
9280 * FIQs, but is allowed to set CPSR_F to 0.
9282 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9283 (val & CPSR_F)) {
9284 qemu_log_mask(LOG_GUEST_ERROR,
9285 "Ignoring attempt to enable CPSR_F flag "
9286 "(non-maskable FIQ [NMFI] support enabled)\n");
9287 mask &= ~CPSR_F;
9292 env->daif &= ~(CPSR_AIF & mask);
9293 env->daif |= val & CPSR_AIF & mask;
9295 if (write_type != CPSRWriteRaw &&
9296 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9297 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9298 /* Note that we can only get here in USR mode if this is a
9299 * gdb stub write; for this case we follow the architectural
9300 * behaviour for guest writes in USR mode of ignoring an attempt
9301 * to switch mode. (Those are caught by translate.c for writes
9302 * triggered by guest instructions.)
9304 mask &= ~CPSR_M;
9305 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9306 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9307 * v7, and has defined behaviour in v8:
9308 * + leave CPSR.M untouched
9309 * + allow changes to the other CPSR fields
9310 * + set PSTATE.IL
9311 * For user changes via the GDB stub, we don't set PSTATE.IL,
9312 * as this would be unnecessarily harsh for a user error.
9314 mask &= ~CPSR_M;
9315 if (write_type != CPSRWriteByGDBStub &&
9316 arm_feature(env, ARM_FEATURE_V8)) {
9317 mask |= CPSR_IL;
9318 val |= CPSR_IL;
9320 qemu_log_mask(LOG_GUEST_ERROR,
9321 "Illegal AArch32 mode switch attempt from %s to %s\n",
9322 aarch32_mode_name(env->uncached_cpsr),
9323 aarch32_mode_name(val));
9324 } else {
9325 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9326 write_type == CPSRWriteExceptionReturn ?
9327 "Exception return from AArch32" :
9328 "AArch32 mode switch from",
9329 aarch32_mode_name(env->uncached_cpsr),
9330 aarch32_mode_name(val), env->regs[15]);
9331 switch_mode(env, val & CPSR_M);
9334 mask &= ~CACHED_CPSR_BITS;
9335 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9336 if (rebuild_hflags) {
9337 arm_rebuild_hflags(env);
9341 /* Sign/zero extend */
9342 uint32_t HELPER(sxtb16)(uint32_t x)
9344 uint32_t res;
9345 res = (uint16_t)(int8_t)x;
9346 res |= (uint32_t)(int8_t)(x >> 16) << 16;
9347 return res;
9350 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9353 * Take a division-by-zero exception if necessary; otherwise return
9354 * to get the usual non-trapping division behaviour (result of 0)
9356 if (arm_feature(env, ARM_FEATURE_M)
9357 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9358 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9362 uint32_t HELPER(uxtb16)(uint32_t x)
9364 uint32_t res;
9365 res = (uint16_t)(uint8_t)x;
9366 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9367 return res;
9370 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
9372 if (den == 0) {
9373 handle_possible_div0_trap(env, GETPC());
9374 return 0;
9376 if (num == INT_MIN && den == -1) {
9377 return INT_MIN;
9379 return num / den;
9382 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
9384 if (den == 0) {
9385 handle_possible_div0_trap(env, GETPC());
9386 return 0;
9388 return num / den;
9391 uint32_t HELPER(rbit)(uint32_t x)
9393 return revbit32(x);
9396 #ifdef CONFIG_USER_ONLY
9398 static void switch_mode(CPUARMState *env, int mode)
9400 ARMCPU *cpu = env_archcpu(env);
9402 if (mode != ARM_CPU_MODE_USR) {
9403 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9407 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9408 uint32_t cur_el, bool secure)
9410 return 1;
9413 void aarch64_sync_64_to_32(CPUARMState *env)
9415 g_assert_not_reached();
9418 #else
9420 static void switch_mode(CPUARMState *env, int mode)
9422 int old_mode;
9423 int i;
9425 old_mode = env->uncached_cpsr & CPSR_M;
9426 if (mode == old_mode)
9427 return;
9429 if (old_mode == ARM_CPU_MODE_FIQ) {
9430 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9431 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9432 } else if (mode == ARM_CPU_MODE_FIQ) {
9433 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9434 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9437 i = bank_number(old_mode);
9438 env->banked_r13[i] = env->regs[13];
9439 env->banked_spsr[i] = env->spsr;
9441 i = bank_number(mode);
9442 env->regs[13] = env->banked_r13[i];
9443 env->spsr = env->banked_spsr[i];
9445 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9446 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9449 /* Physical Interrupt Target EL Lookup Table
9451 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9453 * The below multi-dimensional table is used for looking up the target
9454 * exception level given numerous condition criteria. Specifically, the
9455 * target EL is based on SCR and HCR routing controls as well as the
9456 * currently executing EL and secure state.
9458 * Dimensions:
9459 * target_el_table[2][2][2][2][2][4]
9460 * | | | | | +--- Current EL
9461 * | | | | +------ Non-secure(0)/Secure(1)
9462 * | | | +--------- HCR mask override
9463 * | | +------------ SCR exec state control
9464 * | +--------------- SCR mask override
9465 * +------------------ 32-bit(0)/64-bit(1) EL3
9467 * The table values are as such:
9468 * 0-3 = EL0-EL3
9469 * -1 = Cannot occur
9471 * The ARM ARM target EL table includes entries indicating that an "exception
9472 * is not taken". The two cases where this is applicable are:
9473 * 1) An exception is taken from EL3 but the SCR does not have the exception
9474 * routed to EL3.
9475 * 2) An exception is taken from EL2 but the HCR does not have the exception
9476 * routed to EL2.
9477 * In these two cases, the below table contain a target of EL1. This value is
9478 * returned as it is expected that the consumer of the table data will check
9479 * for "target EL >= current EL" to ensure the exception is not taken.
9481 * SCR HCR
9482 * 64 EA AMO From
9483 * BIT IRQ IMO Non-secure Secure
9484 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9486 static const int8_t target_el_table[2][2][2][2][2][4] = {
9487 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9488 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9489 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9490 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9491 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9492 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9493 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9494 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9495 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
9496 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9497 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9498 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
9499 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
9500 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
9501 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
9502 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
9506 * Determine the target EL for physical exceptions
9508 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9509 uint32_t cur_el, bool secure)
9511 CPUARMState *env = cs->env_ptr;
9512 bool rw;
9513 bool scr;
9514 bool hcr;
9515 int target_el;
9516 /* Is the highest EL AArch64? */
9517 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9518 uint64_t hcr_el2;
9520 if (arm_feature(env, ARM_FEATURE_EL3)) {
9521 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9522 } else {
9523 /* Either EL2 is the highest EL (and so the EL2 register width
9524 * is given by is64); or there is no EL2 or EL3, in which case
9525 * the value of 'rw' does not affect the table lookup anyway.
9527 rw = is64;
9530 hcr_el2 = arm_hcr_el2_eff(env);
9531 switch (excp_idx) {
9532 case EXCP_IRQ:
9533 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9534 hcr = hcr_el2 & HCR_IMO;
9535 break;
9536 case EXCP_FIQ:
9537 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9538 hcr = hcr_el2 & HCR_FMO;
9539 break;
9540 default:
9541 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9542 hcr = hcr_el2 & HCR_AMO;
9543 break;
9547 * For these purposes, TGE and AMO/IMO/FMO both force the
9548 * interrupt to EL2. Fold TGE into the bit extracted above.
9550 hcr |= (hcr_el2 & HCR_TGE) != 0;
9552 /* Perform a table-lookup for the target EL given the current state */
9553 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9555 assert(target_el > 0);
9557 return target_el;
9560 void arm_log_exception(CPUState *cs)
9562 int idx = cs->exception_index;
9564 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9565 const char *exc = NULL;
9566 static const char * const excnames[] = {
9567 [EXCP_UDEF] = "Undefined Instruction",
9568 [EXCP_SWI] = "SVC",
9569 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9570 [EXCP_DATA_ABORT] = "Data Abort",
9571 [EXCP_IRQ] = "IRQ",
9572 [EXCP_FIQ] = "FIQ",
9573 [EXCP_BKPT] = "Breakpoint",
9574 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9575 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9576 [EXCP_HVC] = "Hypervisor Call",
9577 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9578 [EXCP_SMC] = "Secure Monitor Call",
9579 [EXCP_VIRQ] = "Virtual IRQ",
9580 [EXCP_VFIQ] = "Virtual FIQ",
9581 [EXCP_SEMIHOST] = "Semihosting call",
9582 [EXCP_NOCP] = "v7M NOCP UsageFault",
9583 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9584 [EXCP_STKOF] = "v8M STKOF UsageFault",
9585 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9586 [EXCP_LSERR] = "v8M LSERR UsageFault",
9587 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9588 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9589 [EXCP_VSERR] = "Virtual SERR",
9592 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9593 exc = excnames[idx];
9595 if (!exc) {
9596 exc = "unknown";
9598 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9599 idx, exc, cs->cpu_index);
9604 * Function used to synchronize QEMU's AArch64 register set with AArch32
9605 * register set. This is necessary when switching between AArch32 and AArch64
9606 * execution state.
9608 void aarch64_sync_32_to_64(CPUARMState *env)
9610 int i;
9611 uint32_t mode = env->uncached_cpsr & CPSR_M;
9613 /* We can blanket copy R[0:7] to X[0:7] */
9614 for (i = 0; i < 8; i++) {
9615 env->xregs[i] = env->regs[i];
9619 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9620 * Otherwise, they come from the banked user regs.
9622 if (mode == ARM_CPU_MODE_FIQ) {
9623 for (i = 8; i < 13; i++) {
9624 env->xregs[i] = env->usr_regs[i - 8];
9626 } else {
9627 for (i = 8; i < 13; i++) {
9628 env->xregs[i] = env->regs[i];
9633 * Registers x13-x23 are the various mode SP and FP registers. Registers
9634 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9635 * from the mode banked register.
9637 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9638 env->xregs[13] = env->regs[13];
9639 env->xregs[14] = env->regs[14];
9640 } else {
9641 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9642 /* HYP is an exception in that it is copied from r14 */
9643 if (mode == ARM_CPU_MODE_HYP) {
9644 env->xregs[14] = env->regs[14];
9645 } else {
9646 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9650 if (mode == ARM_CPU_MODE_HYP) {
9651 env->xregs[15] = env->regs[13];
9652 } else {
9653 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9656 if (mode == ARM_CPU_MODE_IRQ) {
9657 env->xregs[16] = env->regs[14];
9658 env->xregs[17] = env->regs[13];
9659 } else {
9660 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9661 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9664 if (mode == ARM_CPU_MODE_SVC) {
9665 env->xregs[18] = env->regs[14];
9666 env->xregs[19] = env->regs[13];
9667 } else {
9668 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9669 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9672 if (mode == ARM_CPU_MODE_ABT) {
9673 env->xregs[20] = env->regs[14];
9674 env->xregs[21] = env->regs[13];
9675 } else {
9676 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9677 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9680 if (mode == ARM_CPU_MODE_UND) {
9681 env->xregs[22] = env->regs[14];
9682 env->xregs[23] = env->regs[13];
9683 } else {
9684 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9685 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
9689 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9690 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9691 * FIQ bank for r8-r14.
9693 if (mode == ARM_CPU_MODE_FIQ) {
9694 for (i = 24; i < 31; i++) {
9695 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9697 } else {
9698 for (i = 24; i < 29; i++) {
9699 env->xregs[i] = env->fiq_regs[i - 24];
9701 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9702 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
9705 env->pc = env->regs[15];
9709 * Function used to synchronize QEMU's AArch32 register set with AArch64
9710 * register set. This is necessary when switching between AArch32 and AArch64
9711 * execution state.
9713 void aarch64_sync_64_to_32(CPUARMState *env)
9715 int i;
9716 uint32_t mode = env->uncached_cpsr & CPSR_M;
9718 /* We can blanket copy X[0:7] to R[0:7] */
9719 for (i = 0; i < 8; i++) {
9720 env->regs[i] = env->xregs[i];
9724 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9725 * Otherwise, we copy x8-x12 into the banked user regs.
9727 if (mode == ARM_CPU_MODE_FIQ) {
9728 for (i = 8; i < 13; i++) {
9729 env->usr_regs[i - 8] = env->xregs[i];
9731 } else {
9732 for (i = 8; i < 13; i++) {
9733 env->regs[i] = env->xregs[i];
9738 * Registers r13 & r14 depend on the current mode.
9739 * If we are in a given mode, we copy the corresponding x registers to r13
9740 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9741 * for the mode.
9743 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9744 env->regs[13] = env->xregs[13];
9745 env->regs[14] = env->xregs[14];
9746 } else {
9747 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9750 * HYP is an exception in that it does not have its own banked r14 but
9751 * shares the USR r14
9753 if (mode == ARM_CPU_MODE_HYP) {
9754 env->regs[14] = env->xregs[14];
9755 } else {
9756 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9760 if (mode == ARM_CPU_MODE_HYP) {
9761 env->regs[13] = env->xregs[15];
9762 } else {
9763 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9766 if (mode == ARM_CPU_MODE_IRQ) {
9767 env->regs[14] = env->xregs[16];
9768 env->regs[13] = env->xregs[17];
9769 } else {
9770 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9771 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
9774 if (mode == ARM_CPU_MODE_SVC) {
9775 env->regs[14] = env->xregs[18];
9776 env->regs[13] = env->xregs[19];
9777 } else {
9778 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9779 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
9782 if (mode == ARM_CPU_MODE_ABT) {
9783 env->regs[14] = env->xregs[20];
9784 env->regs[13] = env->xregs[21];
9785 } else {
9786 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9787 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
9790 if (mode == ARM_CPU_MODE_UND) {
9791 env->regs[14] = env->xregs[22];
9792 env->regs[13] = env->xregs[23];
9793 } else {
9794 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
9795 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
9798 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9799 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9800 * FIQ bank for r8-r14.
9802 if (mode == ARM_CPU_MODE_FIQ) {
9803 for (i = 24; i < 31; i++) {
9804 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9806 } else {
9807 for (i = 24; i < 29; i++) {
9808 env->fiq_regs[i - 24] = env->xregs[i];
9810 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
9811 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
9814 env->regs[15] = env->pc;
9817 static void take_aarch32_exception(CPUARMState *env, int new_mode,
9818 uint32_t mask, uint32_t offset,
9819 uint32_t newpc)
9821 int new_el;
9823 /* Change the CPU state so as to actually take the exception. */
9824 switch_mode(env, new_mode);
9827 * For exceptions taken to AArch32 we must clear the SS bit in both
9828 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9830 env->pstate &= ~PSTATE_SS;
9831 env->spsr = cpsr_read(env);
9832 /* Clear IT bits. */
9833 env->condexec_bits = 0;
9834 /* Switch to the new mode, and to the correct instruction set. */
9835 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
9837 /* This must be after mode switching. */
9838 new_el = arm_current_el(env);
9840 /* Set new mode endianness */
9841 env->uncached_cpsr &= ~CPSR_E;
9842 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
9843 env->uncached_cpsr |= CPSR_E;
9845 /* J and IL must always be cleared for exception entry */
9846 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
9847 env->daif |= mask;
9849 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9850 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9851 env->uncached_cpsr |= CPSR_SSBS;
9852 } else {
9853 env->uncached_cpsr &= ~CPSR_SSBS;
9857 if (new_mode == ARM_CPU_MODE_HYP) {
9858 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9859 env->elr_el[2] = env->regs[15];
9860 } else {
9861 /* CPSR.PAN is normally preserved preserved unless... */
9862 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
9863 switch (new_el) {
9864 case 3:
9865 if (!arm_is_secure_below_el3(env)) {
9866 /* ... the target is EL3, from non-secure state. */
9867 env->uncached_cpsr &= ~CPSR_PAN;
9868 break;
9870 /* ... the target is EL3, from secure state ... */
9871 /* fall through */
9872 case 1:
9873 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9874 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9875 env->uncached_cpsr |= CPSR_PAN;
9877 break;
9881 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9882 * and we should just guard the thumb mode on V4
9884 if (arm_feature(env, ARM_FEATURE_V4T)) {
9885 env->thumb =
9886 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9888 env->regs[14] = env->regs[15] + offset;
9890 env->regs[15] = newpc;
9891 arm_rebuild_hflags(env);
9894 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9897 * Handle exception entry to Hyp mode; this is sufficiently
9898 * different to entry to other AArch32 modes that we handle it
9899 * separately here.
9901 * The vector table entry used is always the 0x14 Hyp mode entry point,
9902 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
9903 * The offset applied to the preferred return address is always zero
9904 * (see DDI0487C.a section G1.12.3).
9905 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9907 uint32_t addr, mask;
9908 ARMCPU *cpu = ARM_CPU(cs);
9909 CPUARMState *env = &cpu->env;
9911 switch (cs->exception_index) {
9912 case EXCP_UDEF:
9913 addr = 0x04;
9914 break;
9915 case EXCP_SWI:
9916 addr = 0x08;
9917 break;
9918 case EXCP_BKPT:
9919 /* Fall through to prefetch abort. */
9920 case EXCP_PREFETCH_ABORT:
9921 env->cp15.ifar_s = env->exception.vaddress;
9922 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9923 (uint32_t)env->exception.vaddress);
9924 addr = 0x0c;
9925 break;
9926 case EXCP_DATA_ABORT:
9927 env->cp15.dfar_s = env->exception.vaddress;
9928 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9929 (uint32_t)env->exception.vaddress);
9930 addr = 0x10;
9931 break;
9932 case EXCP_IRQ:
9933 addr = 0x18;
9934 break;
9935 case EXCP_FIQ:
9936 addr = 0x1c;
9937 break;
9938 case EXCP_HVC:
9939 addr = 0x08;
9940 break;
9941 case EXCP_HYP_TRAP:
9942 addr = 0x14;
9943 break;
9944 default:
9945 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9948 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
9949 if (!arm_feature(env, ARM_FEATURE_V8)) {
9951 * QEMU syndrome values are v8-style. v7 has the IL bit
9952 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9953 * If this is a v7 CPU, squash the IL bit in those cases.
9955 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9956 (cs->exception_index == EXCP_DATA_ABORT &&
9957 !(env->exception.syndrome & ARM_EL_ISV)) ||
9958 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9959 env->exception.syndrome &= ~ARM_EL_IL;
9962 env->cp15.esr_el[2] = env->exception.syndrome;
9965 if (arm_current_el(env) != 2 && addr < 0x14) {
9966 addr = 0x14;
9969 mask = 0;
9970 if (!(env->cp15.scr_el3 & SCR_EA)) {
9971 mask |= CPSR_A;
9973 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9974 mask |= CPSR_I;
9976 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9977 mask |= CPSR_F;
9980 addr += env->cp15.hvbar;
9982 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9985 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
9987 ARMCPU *cpu = ARM_CPU(cs);
9988 CPUARMState *env = &cpu->env;
9989 uint32_t addr;
9990 uint32_t mask;
9991 int new_mode;
9992 uint32_t offset;
9993 uint32_t moe;
9995 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
9996 switch (syn_get_ec(env->exception.syndrome)) {
9997 case EC_BREAKPOINT:
9998 case EC_BREAKPOINT_SAME_EL:
9999 moe = 1;
10000 break;
10001 case EC_WATCHPOINT:
10002 case EC_WATCHPOINT_SAME_EL:
10003 moe = 10;
10004 break;
10005 case EC_AA32_BKPT:
10006 moe = 3;
10007 break;
10008 case EC_VECTORCATCH:
10009 moe = 5;
10010 break;
10011 default:
10012 moe = 0;
10013 break;
10016 if (moe) {
10017 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10020 if (env->exception.target_el == 2) {
10021 arm_cpu_do_interrupt_aarch32_hyp(cs);
10022 return;
10025 switch (cs->exception_index) {
10026 case EXCP_UDEF:
10027 new_mode = ARM_CPU_MODE_UND;
10028 addr = 0x04;
10029 mask = CPSR_I;
10030 if (env->thumb)
10031 offset = 2;
10032 else
10033 offset = 4;
10034 break;
10035 case EXCP_SWI:
10036 new_mode = ARM_CPU_MODE_SVC;
10037 addr = 0x08;
10038 mask = CPSR_I;
10039 /* The PC already points to the next instruction. */
10040 offset = 0;
10041 break;
10042 case EXCP_BKPT:
10043 /* Fall through to prefetch abort. */
10044 case EXCP_PREFETCH_ABORT:
10045 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10046 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10047 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10048 env->exception.fsr, (uint32_t)env->exception.vaddress);
10049 new_mode = ARM_CPU_MODE_ABT;
10050 addr = 0x0c;
10051 mask = CPSR_A | CPSR_I;
10052 offset = 4;
10053 break;
10054 case EXCP_DATA_ABORT:
10055 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10056 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10057 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10058 env->exception.fsr,
10059 (uint32_t)env->exception.vaddress);
10060 new_mode = ARM_CPU_MODE_ABT;
10061 addr = 0x10;
10062 mask = CPSR_A | CPSR_I;
10063 offset = 8;
10064 break;
10065 case EXCP_IRQ:
10066 new_mode = ARM_CPU_MODE_IRQ;
10067 addr = 0x18;
10068 /* Disable IRQ and imprecise data aborts. */
10069 mask = CPSR_A | CPSR_I;
10070 offset = 4;
10071 if (env->cp15.scr_el3 & SCR_IRQ) {
10072 /* IRQ routed to monitor mode */
10073 new_mode = ARM_CPU_MODE_MON;
10074 mask |= CPSR_F;
10076 break;
10077 case EXCP_FIQ:
10078 new_mode = ARM_CPU_MODE_FIQ;
10079 addr = 0x1c;
10080 /* Disable FIQ, IRQ and imprecise data aborts. */
10081 mask = CPSR_A | CPSR_I | CPSR_F;
10082 if (env->cp15.scr_el3 & SCR_FIQ) {
10083 /* FIQ routed to monitor mode */
10084 new_mode = ARM_CPU_MODE_MON;
10086 offset = 4;
10087 break;
10088 case EXCP_VIRQ:
10089 new_mode = ARM_CPU_MODE_IRQ;
10090 addr = 0x18;
10091 /* Disable IRQ and imprecise data aborts. */
10092 mask = CPSR_A | CPSR_I;
10093 offset = 4;
10094 break;
10095 case EXCP_VFIQ:
10096 new_mode = ARM_CPU_MODE_FIQ;
10097 addr = 0x1c;
10098 /* Disable FIQ, IRQ and imprecise data aborts. */
10099 mask = CPSR_A | CPSR_I | CPSR_F;
10100 offset = 4;
10101 break;
10102 case EXCP_VSERR:
10105 * Note that this is reported as a data abort, but the DFAR
10106 * has an UNKNOWN value. Construct the SError syndrome from
10107 * AET and ExT fields.
10109 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10111 if (extended_addresses_enabled(env)) {
10112 env->exception.fsr = arm_fi_to_lfsc(&fi);
10113 } else {
10114 env->exception.fsr = arm_fi_to_sfsc(&fi);
10116 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10117 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10118 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10119 env->exception.fsr);
10121 new_mode = ARM_CPU_MODE_ABT;
10122 addr = 0x10;
10123 mask = CPSR_A | CPSR_I;
10124 offset = 8;
10126 break;
10127 case EXCP_SMC:
10128 new_mode = ARM_CPU_MODE_MON;
10129 addr = 0x08;
10130 mask = CPSR_A | CPSR_I | CPSR_F;
10131 offset = 0;
10132 break;
10133 default:
10134 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10135 return; /* Never happens. Keep compiler happy. */
10138 if (new_mode == ARM_CPU_MODE_MON) {
10139 addr += env->cp15.mvbar;
10140 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10141 /* High vectors. When enabled, base address cannot be remapped. */
10142 addr += 0xffff0000;
10143 } else {
10144 /* ARM v7 architectures provide a vector base address register to remap
10145 * the interrupt vector table.
10146 * This register is only followed in non-monitor mode, and is banked.
10147 * Note: only bits 31:5 are valid.
10149 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10152 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10153 env->cp15.scr_el3 &= ~SCR_NS;
10156 take_aarch32_exception(env, new_mode, mask, offset, addr);
10159 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10162 * Return the register number of the AArch64 view of the AArch32
10163 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10164 * be that of the AArch32 mode the exception came from.
10166 int mode = env->uncached_cpsr & CPSR_M;
10168 switch (aarch32_reg) {
10169 case 0 ... 7:
10170 return aarch32_reg;
10171 case 8 ... 12:
10172 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10173 case 13:
10174 switch (mode) {
10175 case ARM_CPU_MODE_USR:
10176 case ARM_CPU_MODE_SYS:
10177 return 13;
10178 case ARM_CPU_MODE_HYP:
10179 return 15;
10180 case ARM_CPU_MODE_IRQ:
10181 return 17;
10182 case ARM_CPU_MODE_SVC:
10183 return 19;
10184 case ARM_CPU_MODE_ABT:
10185 return 21;
10186 case ARM_CPU_MODE_UND:
10187 return 23;
10188 case ARM_CPU_MODE_FIQ:
10189 return 29;
10190 default:
10191 g_assert_not_reached();
10193 case 14:
10194 switch (mode) {
10195 case ARM_CPU_MODE_USR:
10196 case ARM_CPU_MODE_SYS:
10197 case ARM_CPU_MODE_HYP:
10198 return 14;
10199 case ARM_CPU_MODE_IRQ:
10200 return 16;
10201 case ARM_CPU_MODE_SVC:
10202 return 18;
10203 case ARM_CPU_MODE_ABT:
10204 return 20;
10205 case ARM_CPU_MODE_UND:
10206 return 22;
10207 case ARM_CPU_MODE_FIQ:
10208 return 30;
10209 default:
10210 g_assert_not_reached();
10212 case 15:
10213 return 31;
10214 default:
10215 g_assert_not_reached();
10219 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10221 uint32_t ret = cpsr_read(env);
10223 /* Move DIT to the correct location for SPSR_ELx */
10224 if (ret & CPSR_DIT) {
10225 ret &= ~CPSR_DIT;
10226 ret |= PSTATE_DIT;
10228 /* Merge PSTATE.SS into SPSR_ELx */
10229 ret |= env->pstate & PSTATE_SS;
10231 return ret;
10234 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10236 /* Return true if this syndrome value is a synchronous external abort */
10237 switch (syn_get_ec(syndrome)) {
10238 case EC_INSNABORT:
10239 case EC_INSNABORT_SAME_EL:
10240 case EC_DATAABORT:
10241 case EC_DATAABORT_SAME_EL:
10242 /* Look at fault status code for all the synchronous ext abort cases */
10243 switch (syndrome & 0x3f) {
10244 case 0x10:
10245 case 0x13:
10246 case 0x14:
10247 case 0x15:
10248 case 0x16:
10249 case 0x17:
10250 return true;
10251 default:
10252 return false;
10254 default:
10255 return false;
10259 /* Handle exception entry to a target EL which is using AArch64 */
10260 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10262 ARMCPU *cpu = ARM_CPU(cs);
10263 CPUARMState *env = &cpu->env;
10264 unsigned int new_el = env->exception.target_el;
10265 target_ulong addr = env->cp15.vbar_el[new_el];
10266 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10267 unsigned int old_mode;
10268 unsigned int cur_el = arm_current_el(env);
10269 int rt;
10272 * Note that new_el can never be 0. If cur_el is 0, then
10273 * el0_a64 is is_a64(), else el0_a64 is ignored.
10275 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10277 if (cur_el < new_el) {
10278 /* Entry vector offset depends on whether the implemented EL
10279 * immediately lower than the target level is using AArch32 or AArch64
10281 bool is_aa64;
10282 uint64_t hcr;
10284 switch (new_el) {
10285 case 3:
10286 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10287 break;
10288 case 2:
10289 hcr = arm_hcr_el2_eff(env);
10290 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10291 is_aa64 = (hcr & HCR_RW) != 0;
10292 break;
10294 /* fall through */
10295 case 1:
10296 is_aa64 = is_a64(env);
10297 break;
10298 default:
10299 g_assert_not_reached();
10302 if (is_aa64) {
10303 addr += 0x400;
10304 } else {
10305 addr += 0x600;
10307 } else if (pstate_read(env) & PSTATE_SP) {
10308 addr += 0x200;
10311 switch (cs->exception_index) {
10312 case EXCP_PREFETCH_ABORT:
10313 case EXCP_DATA_ABORT:
10315 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10316 * to be taken to the SError vector entrypoint.
10318 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10319 syndrome_is_sync_extabt(env->exception.syndrome)) {
10320 addr += 0x180;
10322 env->cp15.far_el[new_el] = env->exception.vaddress;
10323 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10324 env->cp15.far_el[new_el]);
10325 /* fall through */
10326 case EXCP_BKPT:
10327 case EXCP_UDEF:
10328 case EXCP_SWI:
10329 case EXCP_HVC:
10330 case EXCP_HYP_TRAP:
10331 case EXCP_SMC:
10332 switch (syn_get_ec(env->exception.syndrome)) {
10333 case EC_ADVSIMDFPACCESSTRAP:
10335 * QEMU internal FP/SIMD syndromes from AArch32 include the
10336 * TA and coproc fields which are only exposed if the exception
10337 * is taken to AArch32 Hyp mode. Mask them out to get a valid
10338 * AArch64 format syndrome.
10340 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10341 break;
10342 case EC_CP14RTTRAP:
10343 case EC_CP15RTTRAP:
10344 case EC_CP14DTTRAP:
10346 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10347 * the raw register field from the insn; when taking this to
10348 * AArch64 we must convert it to the AArch64 view of the register
10349 * number. Notice that we read a 4-bit AArch32 register number and
10350 * write back a 5-bit AArch64 one.
10352 rt = extract32(env->exception.syndrome, 5, 4);
10353 rt = aarch64_regnum(env, rt);
10354 env->exception.syndrome = deposit32(env->exception.syndrome,
10355 5, 5, rt);
10356 break;
10357 case EC_CP15RRTTRAP:
10358 case EC_CP14RRTTRAP:
10359 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10360 rt = extract32(env->exception.syndrome, 5, 4);
10361 rt = aarch64_regnum(env, rt);
10362 env->exception.syndrome = deposit32(env->exception.syndrome,
10363 5, 5, rt);
10364 rt = extract32(env->exception.syndrome, 10, 4);
10365 rt = aarch64_regnum(env, rt);
10366 env->exception.syndrome = deposit32(env->exception.syndrome,
10367 10, 5, rt);
10368 break;
10370 env->cp15.esr_el[new_el] = env->exception.syndrome;
10371 break;
10372 case EXCP_IRQ:
10373 case EXCP_VIRQ:
10374 addr += 0x80;
10375 break;
10376 case EXCP_FIQ:
10377 case EXCP_VFIQ:
10378 addr += 0x100;
10379 break;
10380 case EXCP_VSERR:
10381 addr += 0x180;
10382 /* Construct the SError syndrome from IDS and ISS fields. */
10383 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10384 env->cp15.esr_el[new_el] = env->exception.syndrome;
10385 break;
10386 default:
10387 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10390 if (is_a64(env)) {
10391 old_mode = pstate_read(env);
10392 aarch64_save_sp(env, arm_current_el(env));
10393 env->elr_el[new_el] = env->pc;
10394 } else {
10395 old_mode = cpsr_read_for_spsr_elx(env);
10396 env->elr_el[new_el] = env->regs[15];
10398 aarch64_sync_32_to_64(env);
10400 env->condexec_bits = 0;
10402 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10404 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10405 env->elr_el[new_el]);
10407 if (cpu_isar_feature(aa64_pan, cpu)) {
10408 /* The value of PSTATE.PAN is normally preserved, except when ... */
10409 new_mode |= old_mode & PSTATE_PAN;
10410 switch (new_el) {
10411 case 2:
10412 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
10413 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10414 != (HCR_E2H | HCR_TGE)) {
10415 break;
10417 /* fall through */
10418 case 1:
10419 /* ... the target is EL1 ... */
10420 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10421 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10422 new_mode |= PSTATE_PAN;
10424 break;
10427 if (cpu_isar_feature(aa64_mte, cpu)) {
10428 new_mode |= PSTATE_TCO;
10431 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10432 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10433 new_mode |= PSTATE_SSBS;
10434 } else {
10435 new_mode &= ~PSTATE_SSBS;
10439 pstate_write(env, PSTATE_DAIF | new_mode);
10440 env->aarch64 = true;
10441 aarch64_restore_sp(env, new_el);
10442 helper_rebuild_hflags_a64(env, new_el);
10444 env->pc = addr;
10446 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10447 new_el, env->pc, pstate_read(env));
10451 * Do semihosting call and set the appropriate return value. All the
10452 * permission and validity checks have been done at translate time.
10454 * We only see semihosting exceptions in TCG only as they are not
10455 * trapped to the hypervisor in KVM.
10457 #ifdef CONFIG_TCG
10458 static void handle_semihosting(CPUState *cs)
10460 ARMCPU *cpu = ARM_CPU(cs);
10461 CPUARMState *env = &cpu->env;
10463 if (is_a64(env)) {
10464 qemu_log_mask(CPU_LOG_INT,
10465 "...handling as semihosting call 0x%" PRIx64 "\n",
10466 env->xregs[0]);
10467 env->xregs[0] = do_common_semihosting(cs);
10468 env->pc += 4;
10469 } else {
10470 qemu_log_mask(CPU_LOG_INT,
10471 "...handling as semihosting call 0x%x\n",
10472 env->regs[0]);
10473 env->regs[0] = do_common_semihosting(cs);
10474 env->regs[15] += env->thumb ? 2 : 4;
10477 #endif
10479 /* Handle a CPU exception for A and R profile CPUs.
10480 * Do any appropriate logging, handle PSCI calls, and then hand off
10481 * to the AArch64-entry or AArch32-entry function depending on the
10482 * target exception level's register width.
10484 * Note: this is used for both TCG (as the do_interrupt tcg op),
10485 * and KVM to re-inject guest debug exceptions, and to
10486 * inject a Synchronous-External-Abort.
10488 void arm_cpu_do_interrupt(CPUState *cs)
10490 ARMCPU *cpu = ARM_CPU(cs);
10491 CPUARMState *env = &cpu->env;
10492 unsigned int new_el = env->exception.target_el;
10494 assert(!arm_feature(env, ARM_FEATURE_M));
10496 arm_log_exception(cs);
10497 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10498 new_el);
10499 if (qemu_loglevel_mask(CPU_LOG_INT)
10500 && !excp_is_internal(cs->exception_index)) {
10501 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10502 syn_get_ec(env->exception.syndrome),
10503 env->exception.syndrome);
10506 if (arm_is_psci_call(cpu, cs->exception_index)) {
10507 arm_handle_psci_call(cpu);
10508 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10509 return;
10513 * Semihosting semantics depend on the register width of the code
10514 * that caused the exception, not the target exception level, so
10515 * must be handled here.
10517 #ifdef CONFIG_TCG
10518 if (cs->exception_index == EXCP_SEMIHOST) {
10519 handle_semihosting(cs);
10520 return;
10522 #endif
10524 /* Hooks may change global state so BQL should be held, also the
10525 * BQL needs to be held for any modification of
10526 * cs->interrupt_request.
10528 g_assert(qemu_mutex_iothread_locked());
10530 arm_call_pre_el_change_hook(cpu);
10532 assert(!excp_is_internal(cs->exception_index));
10533 if (arm_el_is_aa64(env, new_el)) {
10534 arm_cpu_do_interrupt_aarch64(cs);
10535 } else {
10536 arm_cpu_do_interrupt_aarch32(cs);
10539 arm_call_el_change_hook(cpu);
10541 if (!kvm_enabled()) {
10542 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10545 #endif /* !CONFIG_USER_ONLY */
10547 uint64_t arm_sctlr(CPUARMState *env, int el)
10549 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10550 if (el == 0) {
10551 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10552 el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10553 ? 2 : 1;
10555 return env->cp15.sctlr_el[el];
10558 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10560 if (regime_has_2_ranges(mmu_idx)) {
10561 return extract64(tcr, 37, 2);
10562 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10563 return 0; /* VTCR_EL2 */
10564 } else {
10565 /* Replicate the single TBI bit so we always have 2 bits. */
10566 return extract32(tcr, 20, 1) * 3;
10570 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10572 if (regime_has_2_ranges(mmu_idx)) {
10573 return extract64(tcr, 51, 2);
10574 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10575 return 0; /* VTCR_EL2 */
10576 } else {
10577 /* Replicate the single TBID bit so we always have 2 bits. */
10578 return extract32(tcr, 29, 1) * 3;
10582 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10584 if (regime_has_2_ranges(mmu_idx)) {
10585 return extract64(tcr, 57, 2);
10586 } else {
10587 /* Replicate the single TCMA bit so we always have 2 bits. */
10588 return extract32(tcr, 30, 1) * 3;
10592 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10593 ARMMMUIdx mmu_idx, bool data)
10595 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10596 bool epd, hpd, using16k, using64k, tsz_oob, ds;
10597 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10598 ARMCPU *cpu = env_archcpu(env);
10600 if (!regime_has_2_ranges(mmu_idx)) {
10601 select = 0;
10602 tsz = extract32(tcr, 0, 6);
10603 using64k = extract32(tcr, 14, 1);
10604 using16k = extract32(tcr, 15, 1);
10605 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
10606 /* VTCR_EL2 */
10607 hpd = false;
10608 } else {
10609 hpd = extract32(tcr, 24, 1);
10611 epd = false;
10612 sh = extract32(tcr, 12, 2);
10613 ps = extract32(tcr, 16, 3);
10614 ds = extract64(tcr, 32, 1);
10615 } else {
10617 * Bit 55 is always between the two regions, and is canonical for
10618 * determining if address tagging is enabled.
10620 select = extract64(va, 55, 1);
10621 if (!select) {
10622 tsz = extract32(tcr, 0, 6);
10623 epd = extract32(tcr, 7, 1);
10624 sh = extract32(tcr, 12, 2);
10625 using64k = extract32(tcr, 14, 1);
10626 using16k = extract32(tcr, 15, 1);
10627 hpd = extract64(tcr, 41, 1);
10628 } else {
10629 int tg = extract32(tcr, 30, 2);
10630 using16k = tg == 1;
10631 using64k = tg == 3;
10632 tsz = extract32(tcr, 16, 6);
10633 epd = extract32(tcr, 23, 1);
10634 sh = extract32(tcr, 28, 2);
10635 hpd = extract64(tcr, 42, 1);
10637 ps = extract64(tcr, 32, 3);
10638 ds = extract64(tcr, 59, 1);
10641 if (cpu_isar_feature(aa64_st, cpu)) {
10642 max_tsz = 48 - using64k;
10643 } else {
10644 max_tsz = 39;
10648 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10649 * adjust the effective value of DS, as documented.
10651 min_tsz = 16;
10652 if (using64k) {
10653 if (cpu_isar_feature(aa64_lva, cpu)) {
10654 min_tsz = 12;
10656 ds = false;
10657 } else if (ds) {
10658 switch (mmu_idx) {
10659 case ARMMMUIdx_Stage2:
10660 case ARMMMUIdx_Stage2_S:
10661 if (using16k) {
10662 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10663 } else {
10664 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10666 break;
10667 default:
10668 if (using16k) {
10669 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10670 } else {
10671 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10673 break;
10675 if (ds) {
10676 min_tsz = 12;
10680 if (tsz > max_tsz) {
10681 tsz = max_tsz;
10682 tsz_oob = true;
10683 } else if (tsz < min_tsz) {
10684 tsz = min_tsz;
10685 tsz_oob = true;
10686 } else {
10687 tsz_oob = false;
10690 /* Present TBI as a composite with TBID. */
10691 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10692 if (!data) {
10693 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10695 tbi = (tbi >> select) & 1;
10697 return (ARMVAParameters) {
10698 .tsz = tsz,
10699 .ps = ps,
10700 .sh = sh,
10701 .select = select,
10702 .tbi = tbi,
10703 .epd = epd,
10704 .hpd = hpd,
10705 .using16k = using16k,
10706 .using64k = using64k,
10707 .tsz_oob = tsz_oob,
10708 .ds = ds,
10712 /* Note that signed overflow is undefined in C. The following routines are
10713 careful to use unsigned types where modulo arithmetic is required.
10714 Failure to do so _will_ break on newer gcc. */
10716 /* Signed saturating arithmetic. */
10718 /* Perform 16-bit signed saturating addition. */
10719 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10721 uint16_t res;
10723 res = a + b;
10724 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10725 if (a & 0x8000)
10726 res = 0x8000;
10727 else
10728 res = 0x7fff;
10730 return res;
10733 /* Perform 8-bit signed saturating addition. */
10734 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10736 uint8_t res;
10738 res = a + b;
10739 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10740 if (a & 0x80)
10741 res = 0x80;
10742 else
10743 res = 0x7f;
10745 return res;
10748 /* Perform 16-bit signed saturating subtraction. */
10749 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10751 uint16_t res;
10753 res = a - b;
10754 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10755 if (a & 0x8000)
10756 res = 0x8000;
10757 else
10758 res = 0x7fff;
10760 return res;
10763 /* Perform 8-bit signed saturating subtraction. */
10764 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10766 uint8_t res;
10768 res = a - b;
10769 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10770 if (a & 0x80)
10771 res = 0x80;
10772 else
10773 res = 0x7f;
10775 return res;
10778 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10779 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10780 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10781 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10782 #define PFX q
10784 #include "op_addsub.h"
10786 /* Unsigned saturating arithmetic. */
10787 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
10789 uint16_t res;
10790 res = a + b;
10791 if (res < a)
10792 res = 0xffff;
10793 return res;
10796 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
10798 if (a > b)
10799 return a - b;
10800 else
10801 return 0;
10804 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10806 uint8_t res;
10807 res = a + b;
10808 if (res < a)
10809 res = 0xff;
10810 return res;
10813 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10815 if (a > b)
10816 return a - b;
10817 else
10818 return 0;
10821 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10822 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10823 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10824 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10825 #define PFX uq
10827 #include "op_addsub.h"
10829 /* Signed modulo arithmetic. */
10830 #define SARITH16(a, b, n, op) do { \
10831 int32_t sum; \
10832 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
10833 RESULT(sum, n, 16); \
10834 if (sum >= 0) \
10835 ge |= 3 << (n * 2); \
10836 } while(0)
10838 #define SARITH8(a, b, n, op) do { \
10839 int32_t sum; \
10840 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
10841 RESULT(sum, n, 8); \
10842 if (sum >= 0) \
10843 ge |= 1 << n; \
10844 } while(0)
10847 #define ADD16(a, b, n) SARITH16(a, b, n, +)
10848 #define SUB16(a, b, n) SARITH16(a, b, n, -)
10849 #define ADD8(a, b, n) SARITH8(a, b, n, +)
10850 #define SUB8(a, b, n) SARITH8(a, b, n, -)
10851 #define PFX s
10852 #define ARITH_GE
10854 #include "op_addsub.h"
10856 /* Unsigned modulo arithmetic. */
10857 #define ADD16(a, b, n) do { \
10858 uint32_t sum; \
10859 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10860 RESULT(sum, n, 16); \
10861 if ((sum >> 16) == 1) \
10862 ge |= 3 << (n * 2); \
10863 } while(0)
10865 #define ADD8(a, b, n) do { \
10866 uint32_t sum; \
10867 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10868 RESULT(sum, n, 8); \
10869 if ((sum >> 8) == 1) \
10870 ge |= 1 << n; \
10871 } while(0)
10873 #define SUB16(a, b, n) do { \
10874 uint32_t sum; \
10875 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10876 RESULT(sum, n, 16); \
10877 if ((sum >> 16) == 0) \
10878 ge |= 3 << (n * 2); \
10879 } while(0)
10881 #define SUB8(a, b, n) do { \
10882 uint32_t sum; \
10883 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10884 RESULT(sum, n, 8); \
10885 if ((sum >> 8) == 0) \
10886 ge |= 1 << n; \
10887 } while(0)
10889 #define PFX u
10890 #define ARITH_GE
10892 #include "op_addsub.h"
10894 /* Halved signed arithmetic. */
10895 #define ADD16(a, b, n) \
10896 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10897 #define SUB16(a, b, n) \
10898 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10899 #define ADD8(a, b, n) \
10900 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10901 #define SUB8(a, b, n) \
10902 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10903 #define PFX sh
10905 #include "op_addsub.h"
10907 /* Halved unsigned arithmetic. */
10908 #define ADD16(a, b, n) \
10909 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10910 #define SUB16(a, b, n) \
10911 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10912 #define ADD8(a, b, n) \
10913 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10914 #define SUB8(a, b, n) \
10915 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10916 #define PFX uh
10918 #include "op_addsub.h"
10920 static inline uint8_t do_usad(uint8_t a, uint8_t b)
10922 if (a > b)
10923 return a - b;
10924 else
10925 return b - a;
10928 /* Unsigned sum of absolute byte differences. */
10929 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10931 uint32_t sum;
10932 sum = do_usad(a, b);
10933 sum += do_usad(a >> 8, b >> 8);
10934 sum += do_usad(a >> 16, b >> 16);
10935 sum += do_usad(a >> 24, b >> 24);
10936 return sum;
10939 /* For ARMv6 SEL instruction. */
10940 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10942 uint32_t mask;
10944 mask = 0;
10945 if (flags & 1)
10946 mask |= 0xff;
10947 if (flags & 2)
10948 mask |= 0xff00;
10949 if (flags & 4)
10950 mask |= 0xff0000;
10951 if (flags & 8)
10952 mask |= 0xff000000;
10953 return (a & mask) | (b & ~mask);
10956 /* CRC helpers.
10957 * The upper bytes of val (above the number specified by 'bytes') must have
10958 * been zeroed out by the caller.
10960 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
10962 uint8_t buf[4];
10964 stl_le_p(buf, val);
10966 /* zlib crc32 converts the accumulator and output to one's complement. */
10967 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
10970 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
10972 uint8_t buf[4];
10974 stl_le_p(buf, val);
10976 /* Linux crc32c converts the output to one's complement. */
10977 return crc32c(acc, buf, bytes) ^ 0xffffffff;
10980 /* Return the exception level to which FP-disabled exceptions should
10981 * be taken, or 0 if FP is enabled.
10983 int fp_exception_el(CPUARMState *env, int cur_el)
10985 #ifndef CONFIG_USER_ONLY
10986 uint64_t hcr_el2;
10988 /* CPACR and the CPTR registers don't exist before v6, so FP is
10989 * always accessible
10991 if (!arm_feature(env, ARM_FEATURE_V6)) {
10992 return 0;
10995 if (arm_feature(env, ARM_FEATURE_M)) {
10996 /* CPACR can cause a NOCP UsageFault taken to current security state */
10997 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
10998 return 1;
11001 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11002 if (!extract32(env->v7m.nsacr, 10, 1)) {
11003 /* FP insns cause a NOCP UsageFault taken to Secure */
11004 return 3;
11008 return 0;
11011 hcr_el2 = arm_hcr_el2_eff(env);
11013 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11014 * 0, 2 : trap EL0 and EL1/PL1 accesses
11015 * 1 : trap only EL0 accesses
11016 * 3 : trap no accesses
11017 * This register is ignored if E2H+TGE are both set.
11019 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11020 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11022 switch (fpen) {
11023 case 1:
11024 if (cur_el != 0) {
11025 break;
11027 /* fall through */
11028 case 0:
11029 case 2:
11030 /* Trap from Secure PL0 or PL1 to Secure PL1. */
11031 if (!arm_el_is_aa64(env, 3)
11032 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11033 return 3;
11035 if (cur_el <= 1) {
11036 return 1;
11038 break;
11043 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11044 * to control non-secure access to the FPU. It doesn't have any
11045 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11047 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11048 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11049 if (!extract32(env->cp15.nsacr, 10, 1)) {
11050 /* FP insns act as UNDEF */
11051 return cur_el == 2 ? 2 : 1;
11056 * CPTR_EL2 is present in v7VE or v8, and changes format
11057 * with HCR_EL2.E2H (regardless of TGE).
11059 if (cur_el <= 2) {
11060 if (hcr_el2 & HCR_E2H) {
11061 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11062 case 1:
11063 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11064 break;
11066 /* fall through */
11067 case 0:
11068 case 2:
11069 return 2;
11071 } else if (arm_is_el2_enabled(env)) {
11072 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11073 return 2;
11078 /* CPTR_EL3 : present in v8 */
11079 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11080 /* Trap all FP ops to EL3 */
11081 return 3;
11083 #endif
11084 return 0;
11087 /* Return the exception level we're running at if this is our mmu_idx */
11088 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11090 if (mmu_idx & ARM_MMU_IDX_M) {
11091 return mmu_idx & ARM_MMU_IDX_M_PRIV;
11094 switch (mmu_idx) {
11095 case ARMMMUIdx_E10_0:
11096 case ARMMMUIdx_E20_0:
11097 case ARMMMUIdx_SE10_0:
11098 case ARMMMUIdx_SE20_0:
11099 return 0;
11100 case ARMMMUIdx_E10_1:
11101 case ARMMMUIdx_E10_1_PAN:
11102 case ARMMMUIdx_SE10_1:
11103 case ARMMMUIdx_SE10_1_PAN:
11104 return 1;
11105 case ARMMMUIdx_E2:
11106 case ARMMMUIdx_E20_2:
11107 case ARMMMUIdx_E20_2_PAN:
11108 case ARMMMUIdx_SE2:
11109 case ARMMMUIdx_SE20_2:
11110 case ARMMMUIdx_SE20_2_PAN:
11111 return 2;
11112 case ARMMMUIdx_SE3:
11113 return 3;
11114 default:
11115 g_assert_not_reached();
11119 #ifndef CONFIG_TCG
11120 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11122 g_assert_not_reached();
11124 #endif
11126 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11128 ARMMMUIdx idx;
11129 uint64_t hcr;
11131 if (arm_feature(env, ARM_FEATURE_M)) {
11132 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11135 /* See ARM pseudo-function ELIsInHost. */
11136 switch (el) {
11137 case 0:
11138 hcr = arm_hcr_el2_eff(env);
11139 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11140 idx = ARMMMUIdx_E20_0;
11141 } else {
11142 idx = ARMMMUIdx_E10_0;
11144 break;
11145 case 1:
11146 if (env->pstate & PSTATE_PAN) {
11147 idx = ARMMMUIdx_E10_1_PAN;
11148 } else {
11149 idx = ARMMMUIdx_E10_1;
11151 break;
11152 case 2:
11153 /* Note that TGE does not apply at EL2. */
11154 if (arm_hcr_el2_eff(env) & HCR_E2H) {
11155 if (env->pstate & PSTATE_PAN) {
11156 idx = ARMMMUIdx_E20_2_PAN;
11157 } else {
11158 idx = ARMMMUIdx_E20_2;
11160 } else {
11161 idx = ARMMMUIdx_E2;
11163 break;
11164 case 3:
11165 return ARMMMUIdx_SE3;
11166 default:
11167 g_assert_not_reached();
11170 if (arm_is_secure_below_el3(env)) {
11171 idx &= ~ARM_MMU_IDX_A_NS;
11174 return idx;
11177 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11179 return arm_mmu_idx_el(env, arm_current_el(env));
11182 static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
11183 ARMMMUIdx mmu_idx,
11184 CPUARMTBFlags flags)
11186 DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
11187 DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
11189 if (arm_singlestep_active(env)) {
11190 DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
11192 return flags;
11195 static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11196 ARMMMUIdx mmu_idx,
11197 CPUARMTBFlags flags)
11199 bool sctlr_b = arm_sctlr_b(env);
11201 if (sctlr_b) {
11202 DP_TBFLAG_A32(flags, SCTLR__B, 1);
11204 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
11205 DP_TBFLAG_ANY(flags, BE_DATA, 1);
11207 DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
11209 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11212 static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11213 ARMMMUIdx mmu_idx)
11215 CPUARMTBFlags flags = {};
11216 uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11218 /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11219 if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11220 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11223 if (arm_v7m_is_handler_mode(env)) {
11224 DP_TBFLAG_M32(flags, HANDLER, 1);
11228 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11229 * is suppressing them because the requested execution priority
11230 * is less than 0.
11232 if (arm_feature(env, ARM_FEATURE_V8) &&
11233 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
11234 (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
11235 DP_TBFLAG_M32(flags, STACKCHECK, 1);
11238 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11241 static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11242 ARMMMUIdx mmu_idx)
11244 CPUARMTBFlags flags = {};
11245 int el = arm_current_el(env);
11247 if (arm_sctlr(env, el) & SCTLR_A) {
11248 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11251 if (arm_el_is_aa64(env, 1)) {
11252 DP_TBFLAG_A32(flags, VFPEN, 1);
11255 if (el < 2 && env->cp15.hstr_el2 &&
11256 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11257 DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
11260 if (env->uncached_cpsr & CPSR_IL) {
11261 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11264 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11267 static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11268 ARMMMUIdx mmu_idx)
11270 CPUARMTBFlags flags = {};
11271 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
11272 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11273 uint64_t sctlr;
11274 int tbii, tbid;
11276 DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
11278 /* Get control bits for tagged addresses. */
11279 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11280 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
11282 DP_TBFLAG_A64(flags, TBII, tbii);
11283 DP_TBFLAG_A64(flags, TBID, tbid);
11285 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11286 int sve_el = sve_exception_el(env, el);
11289 * If either FP or SVE are disabled, translator does not need len.
11290 * If SVE EL > FP EL, FP exception has precedence, and translator
11291 * does not need SVE EL. Save potential re-translations by forcing
11292 * the unneeded data to zero.
11294 if (fp_el != 0) {
11295 if (sve_el > fp_el) {
11296 sve_el = 0;
11298 } else if (sve_el == 0) {
11299 DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
11301 DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
11303 if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
11304 DP_TBFLAG_A64(flags, SMEEXC_EL, sme_exception_el(env, el));
11307 sctlr = regime_sctlr(env, stage1);
11309 if (sctlr & SCTLR_A) {
11310 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11313 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
11314 DP_TBFLAG_ANY(flags, BE_DATA, 1);
11317 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11319 * In order to save space in flags, we record only whether
11320 * pauth is "inactive", meaning all insns are implemented as
11321 * a nop, or "active" when some action must be performed.
11322 * The decision of which action to take is left to a helper.
11324 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
11325 DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
11329 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11330 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
11331 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
11332 DP_TBFLAG_A64(flags, BT, 1);
11336 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
11337 if (!(env->pstate & PSTATE_UAO)) {
11338 switch (mmu_idx) {
11339 case ARMMMUIdx_E10_1:
11340 case ARMMMUIdx_E10_1_PAN:
11341 case ARMMMUIdx_SE10_1:
11342 case ARMMMUIdx_SE10_1_PAN:
11343 /* TODO: ARMv8.3-NV */
11344 DP_TBFLAG_A64(flags, UNPRIV, 1);
11345 break;
11346 case ARMMMUIdx_E20_2:
11347 case ARMMMUIdx_E20_2_PAN:
11348 case ARMMMUIdx_SE20_2:
11349 case ARMMMUIdx_SE20_2_PAN:
11351 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11352 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11354 if (env->cp15.hcr_el2 & HCR_TGE) {
11355 DP_TBFLAG_A64(flags, UNPRIV, 1);
11357 break;
11358 default:
11359 break;
11363 if (env->pstate & PSTATE_IL) {
11364 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11367 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11369 * Set MTE_ACTIVE if any access may be Checked, and leave clear
11370 * if all accesses must be Unchecked:
11371 * 1) If no TBI, then there are no tags in the address to check,
11372 * 2) If Tag Check Override, then all accesses are Unchecked,
11373 * 3) If Tag Check Fail == 0, then Checked access have no effect,
11374 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11376 if (allocation_tag_access_enabled(env, el, sctlr)) {
11377 DP_TBFLAG_A64(flags, ATA, 1);
11378 if (tbid
11379 && !(env->pstate & PSTATE_TCO)
11380 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
11381 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
11384 /* And again for unprivileged accesses, if required. */
11385 if (EX_TBFLAG_A64(flags, UNPRIV)
11386 && tbid
11387 && !(env->pstate & PSTATE_TCO)
11388 && (sctlr & SCTLR_TCF0)
11389 && allocation_tag_access_enabled(env, 0, sctlr)) {
11390 DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
11392 /* Cache TCMA as well as TBI. */
11393 DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
11396 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11399 static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
11401 int el = arm_current_el(env);
11402 int fp_el = fp_exception_el(env, el);
11403 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11405 if (is_a64(env)) {
11406 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11407 } else if (arm_feature(env, ARM_FEATURE_M)) {
11408 return rebuild_hflags_m32(env, fp_el, mmu_idx);
11409 } else {
11410 return rebuild_hflags_a32(env, fp_el, mmu_idx);
11414 void arm_rebuild_hflags(CPUARMState *env)
11416 env->hflags = rebuild_hflags_internal(env);
11420 * If we have triggered a EL state change we can't rely on the
11421 * translator having passed it to us, we need to recompute.
11423 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11425 int el = arm_current_el(env);
11426 int fp_el = fp_exception_el(env, el);
11427 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11429 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11432 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11434 int fp_el = fp_exception_el(env, el);
11435 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11437 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11441 * If we have triggered a EL state change we can't rely on the
11442 * translator having passed it to us, we need to recompute.
11444 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11446 int el = arm_current_el(env);
11447 int fp_el = fp_exception_el(env, el);
11448 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11449 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11452 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11454 int fp_el = fp_exception_el(env, el);
11455 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11457 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11460 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11462 int fp_el = fp_exception_el(env, el);
11463 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11465 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11468 static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11470 #ifdef CONFIG_DEBUG_TCG
11471 CPUARMTBFlags c = env->hflags;
11472 CPUARMTBFlags r = rebuild_hflags_internal(env);
11474 if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11475 fprintf(stderr, "TCG hflags mismatch "
11476 "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11477 " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11478 c.flags, c.flags2, r.flags, r.flags2);
11479 abort();
11481 #endif
11484 static bool mve_no_pred(CPUARMState *env)
11487 * Return true if there is definitely no predication of MVE
11488 * instructions by VPR or LTPSIZE. (Returning false even if there
11489 * isn't any predication is OK; generated code will just be
11490 * a little worse.)
11491 * If the CPU does not implement MVE then this TB flag is always 0.
11493 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11494 * logic in gen_update_fp_context() needs to be updated to match.
11496 * We do not include the effect of the ECI bits here -- they are
11497 * tracked in other TB flags. This simplifies the logic for
11498 * "when did we emit code that changes the MVE_NO_PRED TB flag
11499 * and thus need to end the TB?".
11501 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11502 return false;
11504 if (env->v7m.vpr) {
11505 return false;
11507 if (env->v7m.ltpsize < 4) {
11508 return false;
11510 return true;
11513 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11514 target_ulong *cs_base, uint32_t *pflags)
11516 CPUARMTBFlags flags;
11518 assert_hflags_rebuild_correctly(env);
11519 flags = env->hflags;
11521 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11522 *pc = env->pc;
11523 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11524 DP_TBFLAG_A64(flags, BTYPE, env->btype);
11526 } else {
11527 *pc = env->regs[15];
11529 if (arm_feature(env, ARM_FEATURE_M)) {
11530 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11531 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11532 != env->v7m.secure) {
11533 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11536 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11537 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11538 (env->v7m.secure &&
11539 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11541 * ASPEN is set, but FPCA/SFPA indicate that there is no
11542 * active FP context; we must create a new FP context before
11543 * executing any FP insn.
11545 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11548 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11549 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11550 DP_TBFLAG_M32(flags, LSPACT, 1);
11553 if (mve_no_pred(env)) {
11554 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11556 } else {
11558 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11559 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11561 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11562 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11563 } else {
11564 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11565 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11567 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11568 DP_TBFLAG_A32(flags, VFPEN, 1);
11572 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11573 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11577 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11578 * states defined in the ARM ARM for software singlestep:
11579 * SS_ACTIVE PSTATE.SS State
11580 * 0 x Inactive (the TB flag for SS is always 0)
11581 * 1 0 Active-pending
11582 * 1 1 Active-not-pending
11583 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11585 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11586 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11589 *pflags = flags.flags;
11590 *cs_base = flags.flags2;
11593 #ifdef TARGET_AARCH64
11595 * The manual says that when SVE is enabled and VQ is widened the
11596 * implementation is allowed to zero the previously inaccessible
11597 * portion of the registers. The corollary to that is that when
11598 * SVE is enabled and VQ is narrowed we are also allowed to zero
11599 * the now inaccessible portion of the registers.
11601 * The intent of this is that no predicate bit beyond VQ is ever set.
11602 * Which means that some operations on predicate registers themselves
11603 * may operate on full uint64_t or even unrolled across the maximum
11604 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
11605 * may well be cheaper than conditionals to restrict the operation
11606 * to the relevant portion of a uint16_t[16].
11608 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11610 int i, j;
11611 uint64_t pmask;
11613 assert(vq >= 1 && vq <= ARM_MAX_VQ);
11614 assert(vq <= env_archcpu(env)->sve_max_vq);
11616 /* Zap the high bits of the zregs. */
11617 for (i = 0; i < 32; i++) {
11618 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11621 /* Zap the high bits of the pregs and ffr. */
11622 pmask = 0;
11623 if (vq & 3) {
11624 pmask = ~(-1ULL << (16 * (vq & 3)));
11626 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11627 for (i = 0; i < 17; ++i) {
11628 env->vfp.pregs[i].p[j] &= pmask;
11630 pmask = 0;
11635 * Notice a change in SVE vector size when changing EL.
11637 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11638 int new_el, bool el0_a64)
11640 ARMCPU *cpu = env_archcpu(env);
11641 int old_len, new_len;
11642 bool old_a64, new_a64;
11644 /* Nothing to do if no SVE. */
11645 if (!cpu_isar_feature(aa64_sve, cpu)) {
11646 return;
11649 /* Nothing to do if FP is disabled in either EL. */
11650 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11651 return;
11655 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11656 * at ELx, or not available because the EL is in AArch32 state, then
11657 * for all purposes other than a direct read, the ZCR_ELx.LEN field
11658 * has an effective value of 0".
11660 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11661 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11662 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
11663 * we already have the correct register contents when encountering the
11664 * vq0->vq0 transition between EL0->EL1.
11666 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11667 old_len = (old_a64 && !sve_exception_el(env, old_el)
11668 ? sve_vqm1_for_el(env, old_el) : 0);
11669 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11670 new_len = (new_a64 && !sve_exception_el(env, new_el)
11671 ? sve_vqm1_for_el(env, new_el) : 0);
11673 /* When changing vector length, clear inaccessible state. */
11674 if (new_len < old_len) {
11675 aarch64_sve_narrow_vq(env, new_len + 1);
11678 #endif