migration: split migration hooks out of QEMUFileOps
[qemu/ar7.git] / target-arm / helper.c
blobe3ea26f8c86bea80ffff153b861a0d8731785599
1 #include "qemu/osdep.h"
2 #include "cpu.h"
3 #include "internals.h"
4 #include "exec/gdbstub.h"
5 #include "exec/helper-proto.h"
6 #include "qemu/host-utils.h"
7 #include "sysemu/arch_init.h"
8 #include "sysemu/sysemu.h"
9 #include "qemu/bitops.h"
10 #include "qemu/crc32c.h"
11 #include "exec/exec-all.h"
12 #include "exec/cpu_ldst.h"
13 #include "arm_ldst.h"
14 #include <zlib.h> /* For crc32 */
15 #include "exec/semihost.h"
16 #include "sysemu/kvm.h"
18 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
20 #ifndef CONFIG_USER_ONLY
21 static bool get_phys_addr(CPUARMState *env, target_ulong address,
22 int access_type, ARMMMUIdx mmu_idx,
23 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
24 target_ulong *page_size, uint32_t *fsr,
25 ARMMMUFaultInfo *fi);
27 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
28 int access_type, ARMMMUIdx mmu_idx,
29 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
30 target_ulong *page_size_ptr, uint32_t *fsr,
31 ARMMMUFaultInfo *fi);
33 /* Definitions for the PMCCNTR and PMCR registers */
34 #define PMCRD 0x8
35 #define PMCRC 0x4
36 #define PMCRE 0x1
37 #endif
39 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
41 int nregs;
43 /* VFP data registers are always little-endian. */
44 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
45 if (reg < nregs) {
46 stfq_le_p(buf, env->vfp.regs[reg]);
47 return 8;
49 if (arm_feature(env, ARM_FEATURE_NEON)) {
50 /* Aliases for Q regs. */
51 nregs += 16;
52 if (reg < nregs) {
53 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
54 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
55 return 16;
58 switch (reg - nregs) {
59 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
60 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
61 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
63 return 0;
66 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
68 int nregs;
70 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
71 if (reg < nregs) {
72 env->vfp.regs[reg] = ldfq_le_p(buf);
73 return 8;
75 if (arm_feature(env, ARM_FEATURE_NEON)) {
76 nregs += 16;
77 if (reg < nregs) {
78 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
79 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
80 return 16;
83 switch (reg - nregs) {
84 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
85 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
86 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
88 return 0;
91 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
93 switch (reg) {
94 case 0 ... 31:
95 /* 128 bit FP register */
96 stfq_le_p(buf, env->vfp.regs[reg * 2]);
97 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
98 return 16;
99 case 32:
100 /* FPSR */
101 stl_p(buf, vfp_get_fpsr(env));
102 return 4;
103 case 33:
104 /* FPCR */
105 stl_p(buf, vfp_get_fpcr(env));
106 return 4;
107 default:
108 return 0;
112 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
114 switch (reg) {
115 case 0 ... 31:
116 /* 128 bit FP register */
117 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
118 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
119 return 16;
120 case 32:
121 /* FPSR */
122 vfp_set_fpsr(env, ldl_p(buf));
123 return 4;
124 case 33:
125 /* FPCR */
126 vfp_set_fpcr(env, ldl_p(buf));
127 return 4;
128 default:
129 return 0;
133 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
135 assert(ri->fieldoffset);
136 if (cpreg_field_is_64bit(ri)) {
137 return CPREG_FIELD64(env, ri);
138 } else {
139 return CPREG_FIELD32(env, ri);
143 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
144 uint64_t value)
146 assert(ri->fieldoffset);
147 if (cpreg_field_is_64bit(ri)) {
148 CPREG_FIELD64(env, ri) = value;
149 } else {
150 CPREG_FIELD32(env, ri) = value;
154 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
156 return (char *)env + ri->fieldoffset;
159 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
161 /* Raw read of a coprocessor register (as needed for migration, etc). */
162 if (ri->type & ARM_CP_CONST) {
163 return ri->resetvalue;
164 } else if (ri->raw_readfn) {
165 return ri->raw_readfn(env, ri);
166 } else if (ri->readfn) {
167 return ri->readfn(env, ri);
168 } else {
169 return raw_read(env, ri);
173 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
174 uint64_t v)
176 /* Raw write of a coprocessor register (as needed for migration, etc).
177 * Note that constant registers are treated as write-ignored; the
178 * caller should check for success by whether a readback gives the
179 * value written.
181 if (ri->type & ARM_CP_CONST) {
182 return;
183 } else if (ri->raw_writefn) {
184 ri->raw_writefn(env, ri, v);
185 } else if (ri->writefn) {
186 ri->writefn(env, ri, v);
187 } else {
188 raw_write(env, ri, v);
192 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
194 /* Return true if the regdef would cause an assertion if you called
195 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
196 * program bug for it not to have the NO_RAW flag).
197 * NB that returning false here doesn't necessarily mean that calling
198 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
199 * read/write access functions which are safe for raw use" from "has
200 * read/write access functions which have side effects but has forgotten
201 * to provide raw access functions".
202 * The tests here line up with the conditions in read/write_raw_cp_reg()
203 * and assertions in raw_read()/raw_write().
205 if ((ri->type & ARM_CP_CONST) ||
206 ri->fieldoffset ||
207 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
208 return false;
210 return true;
213 bool write_cpustate_to_list(ARMCPU *cpu)
215 /* Write the coprocessor state from cpu->env to the (index,value) list. */
216 int i;
217 bool ok = true;
219 for (i = 0; i < cpu->cpreg_array_len; i++) {
220 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
221 const ARMCPRegInfo *ri;
223 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
224 if (!ri) {
225 ok = false;
226 continue;
228 if (ri->type & ARM_CP_NO_RAW) {
229 continue;
231 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
233 return ok;
236 bool write_list_to_cpustate(ARMCPU *cpu)
238 int i;
239 bool ok = true;
241 for (i = 0; i < cpu->cpreg_array_len; i++) {
242 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
243 uint64_t v = cpu->cpreg_values[i];
244 const ARMCPRegInfo *ri;
246 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
247 if (!ri) {
248 ok = false;
249 continue;
251 if (ri->type & ARM_CP_NO_RAW) {
252 continue;
254 /* Write value and confirm it reads back as written
255 * (to catch read-only registers and partially read-only
256 * registers where the incoming migration value doesn't match)
258 write_raw_cp_reg(&cpu->env, ri, v);
259 if (read_raw_cp_reg(&cpu->env, ri) != v) {
260 ok = false;
263 return ok;
266 static void add_cpreg_to_list(gpointer key, gpointer opaque)
268 ARMCPU *cpu = opaque;
269 uint64_t regidx;
270 const ARMCPRegInfo *ri;
272 regidx = *(uint32_t *)key;
273 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
275 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
276 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
277 /* The value array need not be initialized at this point */
278 cpu->cpreg_array_len++;
282 static void count_cpreg(gpointer key, gpointer opaque)
284 ARMCPU *cpu = opaque;
285 uint64_t regidx;
286 const ARMCPRegInfo *ri;
288 regidx = *(uint32_t *)key;
289 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
291 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
292 cpu->cpreg_array_len++;
296 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
298 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
299 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
301 if (aidx > bidx) {
302 return 1;
304 if (aidx < bidx) {
305 return -1;
307 return 0;
310 void init_cpreg_list(ARMCPU *cpu)
312 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
313 * Note that we require cpreg_tuples[] to be sorted by key ID.
315 GList *keys;
316 int arraylen;
318 keys = g_hash_table_get_keys(cpu->cp_regs);
319 keys = g_list_sort(keys, cpreg_key_compare);
321 cpu->cpreg_array_len = 0;
323 g_list_foreach(keys, count_cpreg, cpu);
325 arraylen = cpu->cpreg_array_len;
326 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
327 cpu->cpreg_values = g_new(uint64_t, arraylen);
328 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
329 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
330 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
331 cpu->cpreg_array_len = 0;
333 g_list_foreach(keys, add_cpreg_to_list, cpu);
335 assert(cpu->cpreg_array_len == arraylen);
337 g_list_free(keys);
341 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
342 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
344 * access_el3_aa32ns: Used to check AArch32 register views.
345 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
347 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
348 const ARMCPRegInfo *ri,
349 bool isread)
351 bool secure = arm_is_secure_below_el3(env);
353 assert(!arm_el_is_aa64(env, 3));
354 if (secure) {
355 return CP_ACCESS_TRAP_UNCATEGORIZED;
357 return CP_ACCESS_OK;
360 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
361 const ARMCPRegInfo *ri,
362 bool isread)
364 if (!arm_el_is_aa64(env, 3)) {
365 return access_el3_aa32ns(env, ri, isread);
367 return CP_ACCESS_OK;
370 /* Some secure-only AArch32 registers trap to EL3 if used from
371 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
372 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
373 * We assume that the .access field is set to PL1_RW.
375 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
376 const ARMCPRegInfo *ri,
377 bool isread)
379 if (arm_current_el(env) == 3) {
380 return CP_ACCESS_OK;
382 if (arm_is_secure_below_el3(env)) {
383 return CP_ACCESS_TRAP_EL3;
385 /* This will be EL1 NS and EL2 NS, which just UNDEF */
386 return CP_ACCESS_TRAP_UNCATEGORIZED;
389 /* Check for traps to "powerdown debug" registers, which are controlled
390 * by MDCR.TDOSA
392 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
393 bool isread)
395 int el = arm_current_el(env);
397 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
398 && !arm_is_secure_below_el3(env)) {
399 return CP_ACCESS_TRAP_EL2;
401 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
402 return CP_ACCESS_TRAP_EL3;
404 return CP_ACCESS_OK;
407 /* Check for traps to "debug ROM" registers, which are controlled
408 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
410 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
411 bool isread)
413 int el = arm_current_el(env);
415 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
416 && !arm_is_secure_below_el3(env)) {
417 return CP_ACCESS_TRAP_EL2;
419 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
420 return CP_ACCESS_TRAP_EL3;
422 return CP_ACCESS_OK;
425 /* Check for traps to general debug registers, which are controlled
426 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
428 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
429 bool isread)
431 int el = arm_current_el(env);
433 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
434 && !arm_is_secure_below_el3(env)) {
435 return CP_ACCESS_TRAP_EL2;
437 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
438 return CP_ACCESS_TRAP_EL3;
440 return CP_ACCESS_OK;
443 /* Check for traps to performance monitor registers, which are controlled
444 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
446 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
447 bool isread)
449 int el = arm_current_el(env);
451 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
452 && !arm_is_secure_below_el3(env)) {
453 return CP_ACCESS_TRAP_EL2;
455 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
456 return CP_ACCESS_TRAP_EL3;
458 return CP_ACCESS_OK;
461 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
463 ARMCPU *cpu = arm_env_get_cpu(env);
465 raw_write(env, ri, value);
466 tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
469 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
471 ARMCPU *cpu = arm_env_get_cpu(env);
473 if (raw_read(env, ri) != value) {
474 /* Unlike real hardware the qemu TLB uses virtual addresses,
475 * not modified virtual addresses, so this causes a TLB flush.
477 tlb_flush(CPU(cpu), 1);
478 raw_write(env, ri, value);
482 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
483 uint64_t value)
485 ARMCPU *cpu = arm_env_get_cpu(env);
487 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU)
488 && !extended_addresses_enabled(env)) {
489 /* For VMSA (when not using the LPAE long descriptor page table
490 * format) this register includes the ASID, so do a TLB flush.
491 * For PMSA it is purely a process ID and no action is needed.
493 tlb_flush(CPU(cpu), 1);
495 raw_write(env, ri, value);
498 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
499 uint64_t value)
501 /* Invalidate all (TLBIALL) */
502 ARMCPU *cpu = arm_env_get_cpu(env);
504 tlb_flush(CPU(cpu), 1);
507 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
508 uint64_t value)
510 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
511 ARMCPU *cpu = arm_env_get_cpu(env);
513 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
516 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
517 uint64_t value)
519 /* Invalidate by ASID (TLBIASID) */
520 ARMCPU *cpu = arm_env_get_cpu(env);
522 tlb_flush(CPU(cpu), value == 0);
525 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
526 uint64_t value)
528 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
529 ARMCPU *cpu = arm_env_get_cpu(env);
531 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
534 /* IS variants of TLB operations must affect all cores */
535 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
536 uint64_t value)
538 CPUState *other_cs;
540 CPU_FOREACH(other_cs) {
541 tlb_flush(other_cs, 1);
545 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
546 uint64_t value)
548 CPUState *other_cs;
550 CPU_FOREACH(other_cs) {
551 tlb_flush(other_cs, value == 0);
555 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
556 uint64_t value)
558 CPUState *other_cs;
560 CPU_FOREACH(other_cs) {
561 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
565 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 uint64_t value)
568 CPUState *other_cs;
570 CPU_FOREACH(other_cs) {
571 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
575 static const ARMCPRegInfo cp_reginfo[] = {
576 /* Define the secure and non-secure FCSE identifier CP registers
577 * separately because there is no secure bank in V8 (no _EL3). This allows
578 * the secure register to be properly reset and migrated. There is also no
579 * v8 EL1 version of the register so the non-secure instance stands alone.
581 { .name = "FCSEIDR(NS)",
582 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
583 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
584 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
585 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
586 { .name = "FCSEIDR(S)",
587 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
588 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
589 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
590 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
591 /* Define the secure and non-secure context identifier CP registers
592 * separately because there is no secure bank in V8 (no _EL3). This allows
593 * the secure register to be properly reset and migrated. In the
594 * non-secure case, the 32-bit register will have reset and migration
595 * disabled during registration as it is handled by the 64-bit instance.
597 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
598 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
599 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
600 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
601 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
602 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
603 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
604 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
605 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
606 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
607 REGINFO_SENTINEL
610 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
611 /* NB: Some of these registers exist in v8 but with more precise
612 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
614 /* MMU Domain access control / MPU write buffer control */
615 { .name = "DACR",
616 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
617 .access = PL1_RW, .resetvalue = 0,
618 .writefn = dacr_write, .raw_writefn = raw_write,
619 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
620 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
621 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
622 * For v6 and v5, these mappings are overly broad.
624 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
625 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
626 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
627 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
628 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
629 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
630 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
631 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
632 /* Cache maintenance ops; some of this space may be overridden later. */
633 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
634 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
635 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
636 REGINFO_SENTINEL
639 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
640 /* Not all pre-v6 cores implemented this WFI, so this is slightly
641 * over-broad.
643 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
644 .access = PL1_W, .type = ARM_CP_WFI },
645 REGINFO_SENTINEL
648 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
649 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
650 * is UNPREDICTABLE; we choose to NOP as most implementations do).
652 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
653 .access = PL1_W, .type = ARM_CP_WFI },
654 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
655 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
656 * OMAPCP will override this space.
658 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
659 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
660 .resetvalue = 0 },
661 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
662 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
663 .resetvalue = 0 },
664 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
665 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
666 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
667 .resetvalue = 0 },
668 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
669 * implementing it as RAZ means the "debug architecture version" bits
670 * will read as a reserved value, which should cause Linux to not try
671 * to use the debug hardware.
673 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
674 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
675 /* MMU TLB control. Note that the wildcarding means we cover not just
676 * the unified TLB ops but also the dside/iside/inner-shareable variants.
678 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
679 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
680 .type = ARM_CP_NO_RAW },
681 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
682 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
683 .type = ARM_CP_NO_RAW },
684 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
685 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
686 .type = ARM_CP_NO_RAW },
687 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
688 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
689 .type = ARM_CP_NO_RAW },
690 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
691 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
692 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
693 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
694 REGINFO_SENTINEL
697 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
698 uint64_t value)
700 uint32_t mask = 0;
702 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
703 if (!arm_feature(env, ARM_FEATURE_V8)) {
704 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
705 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
706 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
708 if (arm_feature(env, ARM_FEATURE_VFP)) {
709 /* VFP coprocessor: cp10 & cp11 [23:20] */
710 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
712 if (!arm_feature(env, ARM_FEATURE_NEON)) {
713 /* ASEDIS [31] bit is RAO/WI */
714 value |= (1 << 31);
717 /* VFPv3 and upwards with NEON implement 32 double precision
718 * registers (D0-D31).
720 if (!arm_feature(env, ARM_FEATURE_NEON) ||
721 !arm_feature(env, ARM_FEATURE_VFP3)) {
722 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
723 value |= (1 << 30);
726 value &= mask;
728 env->cp15.cpacr_el1 = value;
731 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
732 bool isread)
734 if (arm_feature(env, ARM_FEATURE_V8)) {
735 /* Check if CPACR accesses are to be trapped to EL2 */
736 if (arm_current_el(env) == 1 &&
737 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
738 return CP_ACCESS_TRAP_EL2;
739 /* Check if CPACR accesses are to be trapped to EL3 */
740 } else if (arm_current_el(env) < 3 &&
741 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
742 return CP_ACCESS_TRAP_EL3;
746 return CP_ACCESS_OK;
749 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
750 bool isread)
752 /* Check if CPTR accesses are set to trap to EL3 */
753 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
754 return CP_ACCESS_TRAP_EL3;
757 return CP_ACCESS_OK;
760 static const ARMCPRegInfo v6_cp_reginfo[] = {
761 /* prefetch by MVA in v6, NOP in v7 */
762 { .name = "MVA_prefetch",
763 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
764 .access = PL1_W, .type = ARM_CP_NOP },
765 /* We need to break the TB after ISB to execute self-modifying code
766 * correctly and also to take any pending interrupts immediately.
767 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
769 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
770 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
771 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
772 .access = PL0_W, .type = ARM_CP_NOP },
773 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
774 .access = PL0_W, .type = ARM_CP_NOP },
775 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
776 .access = PL1_RW,
777 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
778 offsetof(CPUARMState, cp15.ifar_ns) },
779 .resetvalue = 0, },
780 /* Watchpoint Fault Address Register : should actually only be present
781 * for 1136, 1176, 11MPCore.
783 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
784 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
785 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
786 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
787 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
788 .resetvalue = 0, .writefn = cpacr_write },
789 REGINFO_SENTINEL
792 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
793 bool isread)
795 /* Performance monitor registers user accessibility is controlled
796 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
797 * trapping to EL2 or EL3 for other accesses.
799 int el = arm_current_el(env);
801 if (el == 0 && !env->cp15.c9_pmuserenr) {
802 return CP_ACCESS_TRAP;
804 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
805 && !arm_is_secure_below_el3(env)) {
806 return CP_ACCESS_TRAP_EL2;
808 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
809 return CP_ACCESS_TRAP_EL3;
812 return CP_ACCESS_OK;
815 #ifndef CONFIG_USER_ONLY
817 static inline bool arm_ccnt_enabled(CPUARMState *env)
819 /* This does not support checking PMCCFILTR_EL0 register */
821 if (!(env->cp15.c9_pmcr & PMCRE)) {
822 return false;
825 return true;
828 void pmccntr_sync(CPUARMState *env)
830 uint64_t temp_ticks;
832 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
833 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
835 if (env->cp15.c9_pmcr & PMCRD) {
836 /* Increment once every 64 processor clock cycles */
837 temp_ticks /= 64;
840 if (arm_ccnt_enabled(env)) {
841 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
845 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
846 uint64_t value)
848 pmccntr_sync(env);
850 if (value & PMCRC) {
851 /* The counter has been reset */
852 env->cp15.c15_ccnt = 0;
855 /* only the DP, X, D and E bits are writable */
856 env->cp15.c9_pmcr &= ~0x39;
857 env->cp15.c9_pmcr |= (value & 0x39);
859 pmccntr_sync(env);
862 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
864 uint64_t total_ticks;
866 if (!arm_ccnt_enabled(env)) {
867 /* Counter is disabled, do not change value */
868 return env->cp15.c15_ccnt;
871 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
872 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
874 if (env->cp15.c9_pmcr & PMCRD) {
875 /* Increment once every 64 processor clock cycles */
876 total_ticks /= 64;
878 return total_ticks - env->cp15.c15_ccnt;
881 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
882 uint64_t value)
884 uint64_t total_ticks;
886 if (!arm_ccnt_enabled(env)) {
887 /* Counter is disabled, set the absolute value */
888 env->cp15.c15_ccnt = value;
889 return;
892 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
893 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
895 if (env->cp15.c9_pmcr & PMCRD) {
896 /* Increment once every 64 processor clock cycles */
897 total_ticks /= 64;
899 env->cp15.c15_ccnt = total_ticks - value;
902 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
903 uint64_t value)
905 uint64_t cur_val = pmccntr_read(env, NULL);
907 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
910 #else /* CONFIG_USER_ONLY */
912 void pmccntr_sync(CPUARMState *env)
916 #endif
918 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
919 uint64_t value)
921 pmccntr_sync(env);
922 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
923 pmccntr_sync(env);
926 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
927 uint64_t value)
929 value &= (1 << 31);
930 env->cp15.c9_pmcnten |= value;
933 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
934 uint64_t value)
936 value &= (1 << 31);
937 env->cp15.c9_pmcnten &= ~value;
940 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
941 uint64_t value)
943 env->cp15.c9_pmovsr &= ~value;
946 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
947 uint64_t value)
949 env->cp15.c9_pmxevtyper = value & 0xff;
952 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
953 uint64_t value)
955 env->cp15.c9_pmuserenr = value & 1;
958 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
959 uint64_t value)
961 /* We have no event counters so only the C bit can be changed */
962 value &= (1 << 31);
963 env->cp15.c9_pminten |= value;
966 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
967 uint64_t value)
969 value &= (1 << 31);
970 env->cp15.c9_pminten &= ~value;
973 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
974 uint64_t value)
976 /* Note that even though the AArch64 view of this register has bits
977 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
978 * architectural requirements for bits which are RES0 only in some
979 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
980 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
982 raw_write(env, ri, value & ~0x1FULL);
985 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
987 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
988 * For bits that vary between AArch32/64, code needs to check the
989 * current execution mode before directly using the feature bit.
991 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
993 if (!arm_feature(env, ARM_FEATURE_EL2)) {
994 valid_mask &= ~SCR_HCE;
996 /* On ARMv7, SMD (or SCD as it is called in v7) is only
997 * supported if EL2 exists. The bit is UNK/SBZP when
998 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
999 * when EL2 is unavailable.
1000 * On ARMv8, this bit is always available.
1002 if (arm_feature(env, ARM_FEATURE_V7) &&
1003 !arm_feature(env, ARM_FEATURE_V8)) {
1004 valid_mask &= ~SCR_SMD;
1008 /* Clear all-context RES0 bits. */
1009 value &= valid_mask;
1010 raw_write(env, ri, value);
1013 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1015 ARMCPU *cpu = arm_env_get_cpu(env);
1017 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1018 * bank
1020 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1021 ri->secure & ARM_CP_SECSTATE_S);
1023 return cpu->ccsidr[index];
1026 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1027 uint64_t value)
1029 raw_write(env, ri, value & 0xf);
1032 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1034 CPUState *cs = ENV_GET_CPU(env);
1035 uint64_t ret = 0;
1037 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1038 ret |= CPSR_I;
1040 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1041 ret |= CPSR_F;
1043 /* External aborts are not possible in QEMU so A bit is always clear */
1044 return ret;
1047 static const ARMCPRegInfo v7_cp_reginfo[] = {
1048 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1049 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1050 .access = PL1_W, .type = ARM_CP_NOP },
1051 /* Performance monitors are implementation defined in v7,
1052 * but with an ARM recommended set of registers, which we
1053 * follow (although we don't actually implement any counters)
1055 * Performance registers fall into three categories:
1056 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1057 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1058 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1059 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1060 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1062 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1063 .access = PL0_RW, .type = ARM_CP_ALIAS,
1064 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1065 .writefn = pmcntenset_write,
1066 .accessfn = pmreg_access,
1067 .raw_writefn = raw_write },
1068 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1069 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1070 .access = PL0_RW, .accessfn = pmreg_access,
1071 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1072 .writefn = pmcntenset_write, .raw_writefn = raw_write },
1073 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1074 .access = PL0_RW,
1075 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1076 .accessfn = pmreg_access,
1077 .writefn = pmcntenclr_write,
1078 .type = ARM_CP_ALIAS },
1079 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1080 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1081 .access = PL0_RW, .accessfn = pmreg_access,
1082 .type = ARM_CP_ALIAS,
1083 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1084 .writefn = pmcntenclr_write },
1085 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1086 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1087 .accessfn = pmreg_access,
1088 .writefn = pmovsr_write,
1089 .raw_writefn = raw_write },
1090 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1091 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1092 .access = PL0_RW, .accessfn = pmreg_access,
1093 .type = ARM_CP_ALIAS,
1094 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1095 .writefn = pmovsr_write,
1096 .raw_writefn = raw_write },
1097 /* Unimplemented so WI. */
1098 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1099 .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
1100 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
1101 * We choose to RAZ/WI.
1103 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1104 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1105 .accessfn = pmreg_access },
1106 #ifndef CONFIG_USER_ONLY
1107 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1108 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
1109 .readfn = pmccntr_read, .writefn = pmccntr_write32,
1110 .accessfn = pmreg_access },
1111 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1112 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1113 .access = PL0_RW, .accessfn = pmreg_access,
1114 .type = ARM_CP_IO,
1115 .readfn = pmccntr_read, .writefn = pmccntr_write, },
1116 #endif
1117 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1118 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1119 .writefn = pmccfiltr_write,
1120 .access = PL0_RW, .accessfn = pmreg_access,
1121 .type = ARM_CP_IO,
1122 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1123 .resetvalue = 0, },
1124 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1125 .access = PL0_RW,
1126 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
1127 .accessfn = pmreg_access, .writefn = pmxevtyper_write,
1128 .raw_writefn = raw_write },
1129 /* Unimplemented, RAZ/WI. */
1130 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1131 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1132 .accessfn = pmreg_access },
1133 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1134 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
1135 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1136 .resetvalue = 0,
1137 .writefn = pmuserenr_write, .raw_writefn = raw_write },
1138 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1139 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1140 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1141 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1142 .resetvalue = 0,
1143 .writefn = pmuserenr_write, .raw_writefn = raw_write },
1144 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1145 .access = PL1_RW, .accessfn = access_tpm,
1146 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1147 .resetvalue = 0,
1148 .writefn = pmintenset_write, .raw_writefn = raw_write },
1149 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1150 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1151 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1152 .writefn = pmintenclr_write, },
1153 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1154 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1155 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
1156 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1157 .writefn = pmintenclr_write },
1158 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
1159 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
1160 .access = PL1_RW, .writefn = vbar_write,
1161 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
1162 offsetof(CPUARMState, cp15.vbar_ns) },
1163 .resetvalue = 0 },
1164 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1165 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
1166 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
1167 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1168 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
1169 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1170 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1171 offsetof(CPUARMState, cp15.csselr_ns) } },
1172 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1173 * just RAZ for all cores:
1175 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1176 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
1177 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1178 /* Auxiliary fault status registers: these also are IMPDEF, and we
1179 * choose to RAZ/WI for all cores.
1181 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1182 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1183 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1184 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1185 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1186 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1187 /* MAIR can just read-as-written because we don't implement caches
1188 * and so don't need to care about memory attributes.
1190 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1191 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
1192 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
1193 .resetvalue = 0 },
1194 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1195 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1196 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1197 .resetvalue = 0 },
1198 /* For non-long-descriptor page tables these are PRRR and NMRR;
1199 * regardless they still act as reads-as-written for QEMU.
1201 /* MAIR0/1 are defined separately from their 64-bit counterpart which
1202 * allows them to assign the correct fieldoffset based on the endianness
1203 * handled in the field definitions.
1205 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
1206 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
1207 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1208 offsetof(CPUARMState, cp15.mair0_ns) },
1209 .resetfn = arm_cp_reset_ignore },
1210 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
1211 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
1212 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1213 offsetof(CPUARMState, cp15.mair1_ns) },
1214 .resetfn = arm_cp_reset_ignore },
1215 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1216 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
1217 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
1218 /* 32 bit ITLB invalidates */
1219 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
1220 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1221 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
1222 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1223 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
1224 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1225 /* 32 bit DTLB invalidates */
1226 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
1227 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1228 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
1229 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1230 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
1231 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1232 /* 32 bit TLB invalidates */
1233 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
1234 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1235 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
1236 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1237 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
1238 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1239 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
1240 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
1241 REGINFO_SENTINEL
1244 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1245 /* 32 bit TLB invalidates, Inner Shareable */
1246 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
1247 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
1248 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
1249 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
1250 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
1251 .type = ARM_CP_NO_RAW, .access = PL1_W,
1252 .writefn = tlbiasid_is_write },
1253 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
1254 .type = ARM_CP_NO_RAW, .access = PL1_W,
1255 .writefn = tlbimvaa_is_write },
1256 REGINFO_SENTINEL
1259 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1260 uint64_t value)
1262 value &= 1;
1263 env->teecr = value;
1266 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1267 bool isread)
1269 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
1270 return CP_ACCESS_TRAP;
1272 return CP_ACCESS_OK;
1275 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1276 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1277 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1278 .resetvalue = 0,
1279 .writefn = teecr_write },
1280 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1281 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
1282 .accessfn = teehbr_access, .resetvalue = 0 },
1283 REGINFO_SENTINEL
1286 static const ARMCPRegInfo v6k_cp_reginfo[] = {
1287 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1288 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1289 .access = PL0_RW,
1290 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
1291 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1292 .access = PL0_RW,
1293 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1294 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
1295 .resetfn = arm_cp_reset_ignore },
1296 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1297 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1298 .access = PL0_R|PL1_W,
1299 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1300 .resetvalue = 0},
1301 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1302 .access = PL0_R|PL1_W,
1303 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1304 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
1305 .resetfn = arm_cp_reset_ignore },
1306 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
1307 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
1308 .access = PL1_RW,
1309 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1310 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1311 .access = PL1_RW,
1312 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1313 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1314 .resetvalue = 0 },
1315 REGINFO_SENTINEL
1318 #ifndef CONFIG_USER_ONLY
1320 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1321 bool isread)
1323 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1324 * Writable only at the highest implemented exception level.
1326 int el = arm_current_el(env);
1328 switch (el) {
1329 case 0:
1330 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1331 return CP_ACCESS_TRAP;
1333 break;
1334 case 1:
1335 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1336 arm_is_secure_below_el3(env)) {
1337 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1338 return CP_ACCESS_TRAP_UNCATEGORIZED;
1340 break;
1341 case 2:
1342 case 3:
1343 break;
1346 if (!isread && el < arm_highest_el(env)) {
1347 return CP_ACCESS_TRAP_UNCATEGORIZED;
1350 return CP_ACCESS_OK;
1353 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1354 bool isread)
1356 unsigned int cur_el = arm_current_el(env);
1357 bool secure = arm_is_secure(env);
1359 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
1360 if (cur_el == 0 &&
1361 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1362 return CP_ACCESS_TRAP;
1365 if (arm_feature(env, ARM_FEATURE_EL2) &&
1366 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1367 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1368 return CP_ACCESS_TRAP_EL2;
1370 return CP_ACCESS_OK;
1373 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1374 bool isread)
1376 unsigned int cur_el = arm_current_el(env);
1377 bool secure = arm_is_secure(env);
1379 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1380 * EL0[PV]TEN is zero.
1382 if (cur_el == 0 &&
1383 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1384 return CP_ACCESS_TRAP;
1387 if (arm_feature(env, ARM_FEATURE_EL2) &&
1388 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1389 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1390 return CP_ACCESS_TRAP_EL2;
1392 return CP_ACCESS_OK;
1395 static CPAccessResult gt_pct_access(CPUARMState *env,
1396 const ARMCPRegInfo *ri,
1397 bool isread)
1399 return gt_counter_access(env, GTIMER_PHYS, isread);
1402 static CPAccessResult gt_vct_access(CPUARMState *env,
1403 const ARMCPRegInfo *ri,
1404 bool isread)
1406 return gt_counter_access(env, GTIMER_VIRT, isread);
1409 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1410 bool isread)
1412 return gt_timer_access(env, GTIMER_PHYS, isread);
1415 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1416 bool isread)
1418 return gt_timer_access(env, GTIMER_VIRT, isread);
1421 static CPAccessResult gt_stimer_access(CPUARMState *env,
1422 const ARMCPRegInfo *ri,
1423 bool isread)
1425 /* The AArch64 register view of the secure physical timer is
1426 * always accessible from EL3, and configurably accessible from
1427 * Secure EL1.
1429 switch (arm_current_el(env)) {
1430 case 1:
1431 if (!arm_is_secure(env)) {
1432 return CP_ACCESS_TRAP;
1434 if (!(env->cp15.scr_el3 & SCR_ST)) {
1435 return CP_ACCESS_TRAP_EL3;
1437 return CP_ACCESS_OK;
1438 case 0:
1439 case 2:
1440 return CP_ACCESS_TRAP;
1441 case 3:
1442 return CP_ACCESS_OK;
1443 default:
1444 g_assert_not_reached();
1448 static uint64_t gt_get_countervalue(CPUARMState *env)
1450 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
1453 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1455 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1457 if (gt->ctl & 1) {
1458 /* Timer enabled: calculate and set current ISTATUS, irq, and
1459 * reset timer to when ISTATUS next has to change
1461 uint64_t offset = timeridx == GTIMER_VIRT ?
1462 cpu->env.cp15.cntvoff_el2 : 0;
1463 uint64_t count = gt_get_countervalue(&cpu->env);
1464 /* Note that this must be unsigned 64 bit arithmetic: */
1465 int istatus = count - offset >= gt->cval;
1466 uint64_t nexttick;
1468 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1469 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1470 (istatus && !(gt->ctl & 2)));
1471 if (istatus) {
1472 /* Next transition is when count rolls back over to zero */
1473 nexttick = UINT64_MAX;
1474 } else {
1475 /* Next transition is when we hit cval */
1476 nexttick = gt->cval + offset;
1478 /* Note that the desired next expiry time might be beyond the
1479 * signed-64-bit range of a QEMUTimer -- in this case we just
1480 * set the timer for as far in the future as possible. When the
1481 * timer expires we will reset the timer for any remaining period.
1483 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1484 nexttick = INT64_MAX / GTIMER_SCALE;
1486 timer_mod(cpu->gt_timer[timeridx], nexttick);
1487 } else {
1488 /* Timer disabled: ISTATUS and timer output always clear */
1489 gt->ctl &= ~4;
1490 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
1491 timer_del(cpu->gt_timer[timeridx]);
1495 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1496 int timeridx)
1498 ARMCPU *cpu = arm_env_get_cpu(env);
1500 timer_del(cpu->gt_timer[timeridx]);
1503 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1505 return gt_get_countervalue(env);
1508 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1510 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1513 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1514 int timeridx,
1515 uint64_t value)
1517 env->cp15.c14_timer[timeridx].cval = value;
1518 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1521 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1522 int timeridx)
1524 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1526 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
1527 (gt_get_countervalue(env) - offset));
1530 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1531 int timeridx,
1532 uint64_t value)
1534 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1536 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
1537 sextract64(value, 0, 32);
1538 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1541 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1542 int timeridx,
1543 uint64_t value)
1545 ARMCPU *cpu = arm_env_get_cpu(env);
1546 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1548 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
1549 if ((oldval ^ value) & 1) {
1550 /* Enable toggled */
1551 gt_recalc_timer(cpu, timeridx);
1552 } else if ((oldval ^ value) & 2) {
1553 /* IMASK toggled: don't need to recalculate,
1554 * just set the interrupt line based on ISTATUS
1556 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1557 (oldval & 4) && !(value & 2));
1561 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1563 gt_timer_reset(env, ri, GTIMER_PHYS);
1566 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1567 uint64_t value)
1569 gt_cval_write(env, ri, GTIMER_PHYS, value);
1572 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1574 return gt_tval_read(env, ri, GTIMER_PHYS);
1577 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1578 uint64_t value)
1580 gt_tval_write(env, ri, GTIMER_PHYS, value);
1583 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1584 uint64_t value)
1586 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1589 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1591 gt_timer_reset(env, ri, GTIMER_VIRT);
1594 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1595 uint64_t value)
1597 gt_cval_write(env, ri, GTIMER_VIRT, value);
1600 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1602 return gt_tval_read(env, ri, GTIMER_VIRT);
1605 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1606 uint64_t value)
1608 gt_tval_write(env, ri, GTIMER_VIRT, value);
1611 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1612 uint64_t value)
1614 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1617 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1618 uint64_t value)
1620 ARMCPU *cpu = arm_env_get_cpu(env);
1622 raw_write(env, ri, value);
1623 gt_recalc_timer(cpu, GTIMER_VIRT);
1626 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1628 gt_timer_reset(env, ri, GTIMER_HYP);
1631 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1632 uint64_t value)
1634 gt_cval_write(env, ri, GTIMER_HYP, value);
1637 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1639 return gt_tval_read(env, ri, GTIMER_HYP);
1642 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1643 uint64_t value)
1645 gt_tval_write(env, ri, GTIMER_HYP, value);
1648 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1649 uint64_t value)
1651 gt_ctl_write(env, ri, GTIMER_HYP, value);
1654 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1656 gt_timer_reset(env, ri, GTIMER_SEC);
1659 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1660 uint64_t value)
1662 gt_cval_write(env, ri, GTIMER_SEC, value);
1665 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1667 return gt_tval_read(env, ri, GTIMER_SEC);
1670 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1671 uint64_t value)
1673 gt_tval_write(env, ri, GTIMER_SEC, value);
1676 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1677 uint64_t value)
1679 gt_ctl_write(env, ri, GTIMER_SEC, value);
1682 void arm_gt_ptimer_cb(void *opaque)
1684 ARMCPU *cpu = opaque;
1686 gt_recalc_timer(cpu, GTIMER_PHYS);
1689 void arm_gt_vtimer_cb(void *opaque)
1691 ARMCPU *cpu = opaque;
1693 gt_recalc_timer(cpu, GTIMER_VIRT);
1696 void arm_gt_htimer_cb(void *opaque)
1698 ARMCPU *cpu = opaque;
1700 gt_recalc_timer(cpu, GTIMER_HYP);
1703 void arm_gt_stimer_cb(void *opaque)
1705 ARMCPU *cpu = opaque;
1707 gt_recalc_timer(cpu, GTIMER_SEC);
1710 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1711 /* Note that CNTFRQ is purely reads-as-written for the benefit
1712 * of software; writing it doesn't actually change the timer frequency.
1713 * Our reset value matches the fixed frequency we implement the timer at.
1715 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
1716 .type = ARM_CP_ALIAS,
1717 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1718 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
1720 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1721 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1722 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1723 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1724 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
1726 /* overall control: mostly access permissions */
1727 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1728 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
1729 .access = PL1_RW,
1730 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1731 .resetvalue = 0,
1733 /* per-timer control */
1734 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1735 .secure = ARM_CP_SECSTATE_NS,
1736 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1737 .accessfn = gt_ptimer_access,
1738 .fieldoffset = offsetoflow32(CPUARMState,
1739 cp15.c14_timer[GTIMER_PHYS].ctl),
1740 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
1742 { .name = "CNTP_CTL(S)",
1743 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1744 .secure = ARM_CP_SECSTATE_S,
1745 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1746 .accessfn = gt_ptimer_access,
1747 .fieldoffset = offsetoflow32(CPUARMState,
1748 cp15.c14_timer[GTIMER_SEC].ctl),
1749 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1751 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1752 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
1753 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1754 .accessfn = gt_ptimer_access,
1755 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1756 .resetvalue = 0,
1757 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
1759 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
1760 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1761 .accessfn = gt_vtimer_access,
1762 .fieldoffset = offsetoflow32(CPUARMState,
1763 cp15.c14_timer[GTIMER_VIRT].ctl),
1764 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
1766 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1767 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
1768 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1769 .accessfn = gt_vtimer_access,
1770 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1771 .resetvalue = 0,
1772 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
1774 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1775 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1776 .secure = ARM_CP_SECSTATE_NS,
1777 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1778 .accessfn = gt_ptimer_access,
1779 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
1781 { .name = "CNTP_TVAL(S)",
1782 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1783 .secure = ARM_CP_SECSTATE_S,
1784 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1785 .accessfn = gt_ptimer_access,
1786 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
1788 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1789 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
1790 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1791 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
1792 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
1794 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
1795 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1796 .accessfn = gt_vtimer_access,
1797 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
1799 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1800 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
1801 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1802 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
1803 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
1805 /* The counter itself */
1806 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
1807 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
1808 .accessfn = gt_pct_access,
1809 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1811 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1812 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
1813 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1814 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
1816 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
1817 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
1818 .accessfn = gt_vct_access,
1819 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
1821 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1822 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
1823 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1824 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
1826 /* Comparison value, indicating when the timer goes off */
1827 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
1828 .secure = ARM_CP_SECSTATE_NS,
1829 .access = PL1_RW | PL0_R,
1830 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1831 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1832 .accessfn = gt_ptimer_access,
1833 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
1835 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
1836 .secure = ARM_CP_SECSTATE_S,
1837 .access = PL1_RW | PL0_R,
1838 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1839 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1840 .accessfn = gt_ptimer_access,
1841 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1843 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1844 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1845 .access = PL1_RW | PL0_R,
1846 .type = ARM_CP_IO,
1847 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1848 .resetvalue = 0, .accessfn = gt_ptimer_access,
1849 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
1851 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1852 .access = PL1_RW | PL0_R,
1853 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1854 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1855 .accessfn = gt_vtimer_access,
1856 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
1858 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1859 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1860 .access = PL1_RW | PL0_R,
1861 .type = ARM_CP_IO,
1862 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1863 .resetvalue = 0, .accessfn = gt_vtimer_access,
1864 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
1866 /* Secure timer -- this is actually restricted to only EL3
1867 * and configurably Secure-EL1 via the accessfn.
1869 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
1870 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
1871 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
1872 .accessfn = gt_stimer_access,
1873 .readfn = gt_sec_tval_read,
1874 .writefn = gt_sec_tval_write,
1875 .resetfn = gt_sec_timer_reset,
1877 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
1878 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
1879 .type = ARM_CP_IO, .access = PL1_RW,
1880 .accessfn = gt_stimer_access,
1881 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
1882 .resetvalue = 0,
1883 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1885 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
1886 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
1887 .type = ARM_CP_IO, .access = PL1_RW,
1888 .accessfn = gt_stimer_access,
1889 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1890 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1892 REGINFO_SENTINEL
1895 #else
1896 /* In user-mode none of the generic timer registers are accessible,
1897 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
1898 * so instead just don't register any of them.
1900 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1901 REGINFO_SENTINEL
1904 #endif
1906 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1908 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1909 raw_write(env, ri, value);
1910 } else if (arm_feature(env, ARM_FEATURE_V7)) {
1911 raw_write(env, ri, value & 0xfffff6ff);
1912 } else {
1913 raw_write(env, ri, value & 0xfffff1ff);
1917 #ifndef CONFIG_USER_ONLY
1918 /* get_phys_addr() isn't present for user-mode-only targets */
1920 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
1921 bool isread)
1923 if (ri->opc2 & 4) {
1924 /* The ATS12NSO* operations must trap to EL3 if executed in
1925 * Secure EL1 (which can only happen if EL3 is AArch64).
1926 * They are simply UNDEF if executed from NS EL1.
1927 * They function normally from EL2 or EL3.
1929 if (arm_current_el(env) == 1) {
1930 if (arm_is_secure_below_el3(env)) {
1931 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
1933 return CP_ACCESS_TRAP_UNCATEGORIZED;
1936 return CP_ACCESS_OK;
1939 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
1940 int access_type, ARMMMUIdx mmu_idx)
1942 hwaddr phys_addr;
1943 target_ulong page_size;
1944 int prot;
1945 uint32_t fsr;
1946 bool ret;
1947 uint64_t par64;
1948 MemTxAttrs attrs = {};
1949 ARMMMUFaultInfo fi = {};
1951 ret = get_phys_addr(env, value, access_type, mmu_idx,
1952 &phys_addr, &attrs, &prot, &page_size, &fsr, &fi);
1953 if (extended_addresses_enabled(env)) {
1954 /* fsr is a DFSR/IFSR value for the long descriptor
1955 * translation table format, but with WnR always clear.
1956 * Convert it to a 64-bit PAR.
1958 par64 = (1 << 11); /* LPAE bit always set */
1959 if (!ret) {
1960 par64 |= phys_addr & ~0xfffULL;
1961 if (!attrs.secure) {
1962 par64 |= (1 << 9); /* NS */
1964 /* We don't set the ATTR or SH fields in the PAR. */
1965 } else {
1966 par64 |= 1; /* F */
1967 par64 |= (fsr & 0x3f) << 1; /* FS */
1968 /* Note that S2WLK and FSTAGE are always zero, because we don't
1969 * implement virtualization and therefore there can't be a stage 2
1970 * fault.
1973 } else {
1974 /* fsr is a DFSR/IFSR value for the short descriptor
1975 * translation table format (with WnR always clear).
1976 * Convert it to a 32-bit PAR.
1978 if (!ret) {
1979 /* We do not set any attribute bits in the PAR */
1980 if (page_size == (1 << 24)
1981 && arm_feature(env, ARM_FEATURE_V7)) {
1982 par64 = (phys_addr & 0xff000000) | (1 << 1);
1983 } else {
1984 par64 = phys_addr & 0xfffff000;
1986 if (!attrs.secure) {
1987 par64 |= (1 << 9); /* NS */
1989 } else {
1990 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
1991 ((fsr & 0xf) << 1) | 1;
1994 return par64;
1997 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1999 int access_type = ri->opc2 & 1;
2000 uint64_t par64;
2001 ARMMMUIdx mmu_idx;
2002 int el = arm_current_el(env);
2003 bool secure = arm_is_secure_below_el3(env);
2005 switch (ri->opc2 & 6) {
2006 case 0:
2007 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2008 switch (el) {
2009 case 3:
2010 mmu_idx = ARMMMUIdx_S1E3;
2011 break;
2012 case 2:
2013 mmu_idx = ARMMMUIdx_S1NSE1;
2014 break;
2015 case 1:
2016 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2017 break;
2018 default:
2019 g_assert_not_reached();
2021 break;
2022 case 2:
2023 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2024 switch (el) {
2025 case 3:
2026 mmu_idx = ARMMMUIdx_S1SE0;
2027 break;
2028 case 2:
2029 mmu_idx = ARMMMUIdx_S1NSE0;
2030 break;
2031 case 1:
2032 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2033 break;
2034 default:
2035 g_assert_not_reached();
2037 break;
2038 case 4:
2039 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2040 mmu_idx = ARMMMUIdx_S12NSE1;
2041 break;
2042 case 6:
2043 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2044 mmu_idx = ARMMMUIdx_S12NSE0;
2045 break;
2046 default:
2047 g_assert_not_reached();
2050 par64 = do_ats_write(env, value, access_type, mmu_idx);
2052 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2055 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2056 uint64_t value)
2058 int access_type = ri->opc2 & 1;
2059 uint64_t par64;
2061 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2063 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2066 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2067 bool isread)
2069 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2070 return CP_ACCESS_TRAP;
2072 return CP_ACCESS_OK;
2075 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2076 uint64_t value)
2078 int access_type = ri->opc2 & 1;
2079 ARMMMUIdx mmu_idx;
2080 int secure = arm_is_secure_below_el3(env);
2082 switch (ri->opc2 & 6) {
2083 case 0:
2084 switch (ri->opc1) {
2085 case 0: /* AT S1E1R, AT S1E1W */
2086 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2087 break;
2088 case 4: /* AT S1E2R, AT S1E2W */
2089 mmu_idx = ARMMMUIdx_S1E2;
2090 break;
2091 case 6: /* AT S1E3R, AT S1E3W */
2092 mmu_idx = ARMMMUIdx_S1E3;
2093 break;
2094 default:
2095 g_assert_not_reached();
2097 break;
2098 case 2: /* AT S1E0R, AT S1E0W */
2099 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2100 break;
2101 case 4: /* AT S12E1R, AT S12E1W */
2102 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
2103 break;
2104 case 6: /* AT S12E0R, AT S12E0W */
2105 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
2106 break;
2107 default:
2108 g_assert_not_reached();
2111 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
2113 #endif
2115 static const ARMCPRegInfo vapa_cp_reginfo[] = {
2116 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2117 .access = PL1_RW, .resetvalue = 0,
2118 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2119 offsetoflow32(CPUARMState, cp15.par_ns) },
2120 .writefn = par_write },
2121 #ifndef CONFIG_USER_ONLY
2122 /* This underdecoding is safe because the reginfo is NO_RAW. */
2123 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
2124 .access = PL1_W, .accessfn = ats_access,
2125 .writefn = ats_write, .type = ARM_CP_NO_RAW },
2126 #endif
2127 REGINFO_SENTINEL
2130 /* Return basic MPU access permission bits. */
2131 static uint32_t simple_mpu_ap_bits(uint32_t val)
2133 uint32_t ret;
2134 uint32_t mask;
2135 int i;
2136 ret = 0;
2137 mask = 3;
2138 for (i = 0; i < 16; i += 2) {
2139 ret |= (val >> i) & mask;
2140 mask <<= 2;
2142 return ret;
2145 /* Pad basic MPU access permission bits to extended format. */
2146 static uint32_t extended_mpu_ap_bits(uint32_t val)
2148 uint32_t ret;
2149 uint32_t mask;
2150 int i;
2151 ret = 0;
2152 mask = 3;
2153 for (i = 0; i < 16; i += 2) {
2154 ret |= (val & mask) << i;
2155 mask <<= 2;
2157 return ret;
2160 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2161 uint64_t value)
2163 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
2166 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2168 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
2171 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2172 uint64_t value)
2174 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
2177 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2179 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
2182 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2184 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2186 if (!u32p) {
2187 return 0;
2190 u32p += env->cp15.c6_rgnr;
2191 return *u32p;
2194 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2195 uint64_t value)
2197 ARMCPU *cpu = arm_env_get_cpu(env);
2198 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2200 if (!u32p) {
2201 return;
2204 u32p += env->cp15.c6_rgnr;
2205 tlb_flush(CPU(cpu), 1); /* Mappings may have changed - purge! */
2206 *u32p = value;
2209 static void pmsav7_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2211 ARMCPU *cpu = arm_env_get_cpu(env);
2212 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2214 if (!u32p) {
2215 return;
2218 memset(u32p, 0, sizeof(*u32p) * cpu->pmsav7_dregion);
2221 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2222 uint64_t value)
2224 ARMCPU *cpu = arm_env_get_cpu(env);
2225 uint32_t nrgs = cpu->pmsav7_dregion;
2227 if (value >= nrgs) {
2228 qemu_log_mask(LOG_GUEST_ERROR,
2229 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2230 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2231 return;
2234 raw_write(env, ri, value);
2237 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
2238 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2239 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2240 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
2241 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2242 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2243 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2244 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
2245 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2246 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2247 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2248 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
2249 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2250 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2251 .access = PL1_RW,
2252 .fieldoffset = offsetof(CPUARMState, cp15.c6_rgnr),
2253 .writefn = pmsav7_rgnr_write },
2254 REGINFO_SENTINEL
2257 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2258 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2259 .access = PL1_RW, .type = ARM_CP_ALIAS,
2260 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2261 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2262 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2263 .access = PL1_RW, .type = ARM_CP_ALIAS,
2264 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2265 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2266 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2267 .access = PL1_RW,
2268 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2269 .resetvalue = 0, },
2270 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2271 .access = PL1_RW,
2272 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2273 .resetvalue = 0, },
2274 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2275 .access = PL1_RW,
2276 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2277 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2278 .access = PL1_RW,
2279 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
2280 /* Protection region base and size registers */
2281 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2282 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2283 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2284 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2285 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2286 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2287 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2288 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2289 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2290 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2291 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2292 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2293 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2294 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2295 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2296 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2297 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2298 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2299 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2300 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2301 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2302 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2303 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2304 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
2305 REGINFO_SENTINEL
2308 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2309 uint64_t value)
2311 TCR *tcr = raw_ptr(env, ri);
2312 int maskshift = extract32(value, 0, 3);
2314 if (!arm_feature(env, ARM_FEATURE_V8)) {
2315 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2316 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2317 * using Long-desciptor translation table format */
2318 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2319 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2320 /* In an implementation that includes the Security Extensions
2321 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2322 * Short-descriptor translation table format.
2324 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2325 } else {
2326 value &= TTBCR_N;
2330 /* Update the masks corresponding to the TCR bank being written
2331 * Note that we always calculate mask and base_mask, but
2332 * they are only used for short-descriptor tables (ie if EAE is 0);
2333 * for long-descriptor tables the TCR fields are used differently
2334 * and the mask and base_mask values are meaningless.
2336 tcr->raw_tcr = value;
2337 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2338 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
2341 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2342 uint64_t value)
2344 ARMCPU *cpu = arm_env_get_cpu(env);
2346 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2347 /* With LPAE the TTBCR could result in a change of ASID
2348 * via the TTBCR.A1 bit, so do a TLB flush.
2350 tlb_flush(CPU(cpu), 1);
2352 vmsa_ttbcr_raw_write(env, ri, value);
2355 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2357 TCR *tcr = raw_ptr(env, ri);
2359 /* Reset both the TCR as well as the masks corresponding to the bank of
2360 * the TCR being reset.
2362 tcr->raw_tcr = 0;
2363 tcr->mask = 0;
2364 tcr->base_mask = 0xffffc000u;
2367 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2368 uint64_t value)
2370 ARMCPU *cpu = arm_env_get_cpu(env);
2371 TCR *tcr = raw_ptr(env, ri);
2373 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
2374 tlb_flush(CPU(cpu), 1);
2375 tcr->raw_tcr = value;
2378 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2379 uint64_t value)
2381 /* 64 bit accesses to the TTBRs can change the ASID and so we
2382 * must flush the TLB.
2384 if (cpreg_field_is_64bit(ri)) {
2385 ARMCPU *cpu = arm_env_get_cpu(env);
2387 tlb_flush(CPU(cpu), 1);
2389 raw_write(env, ri, value);
2392 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2393 uint64_t value)
2395 ARMCPU *cpu = arm_env_get_cpu(env);
2396 CPUState *cs = CPU(cpu);
2398 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2399 if (raw_read(env, ri) != value) {
2400 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2401 ARMMMUIdx_S2NS, -1);
2402 raw_write(env, ri, value);
2406 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
2407 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2408 .access = PL1_RW, .type = ARM_CP_ALIAS,
2409 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
2410 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
2411 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2412 .access = PL1_RW, .resetvalue = 0,
2413 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2414 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
2415 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2416 .access = PL1_RW, .resetvalue = 0,
2417 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2418 offsetof(CPUARMState, cp15.dfar_ns) } },
2419 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2420 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2421 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2422 .resetvalue = 0, },
2423 REGINFO_SENTINEL
2426 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
2427 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2428 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2429 .access = PL1_RW,
2430 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
2431 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
2432 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2433 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2434 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2435 offsetof(CPUARMState, cp15.ttbr0_ns) } },
2436 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
2437 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2438 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2439 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2440 offsetof(CPUARMState, cp15.ttbr1_ns) } },
2441 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2442 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2443 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2444 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
2445 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
2446 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2447 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
2448 .raw_writefn = vmsa_ttbcr_raw_write,
2449 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2450 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
2451 REGINFO_SENTINEL
2454 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2455 uint64_t value)
2457 env->cp15.c15_ticonfig = value & 0xe7;
2458 /* The OS_TYPE bit in this register changes the reported CPUID! */
2459 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2460 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
2463 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2464 uint64_t value)
2466 env->cp15.c15_threadid = value & 0xffff;
2469 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2470 uint64_t value)
2472 /* Wait-for-interrupt (deprecated) */
2473 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
2476 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2477 uint64_t value)
2479 /* On OMAP there are registers indicating the max/min index of dcache lines
2480 * containing a dirty line; cache flush operations have to reset these.
2482 env->cp15.c15_i_max = 0x000;
2483 env->cp15.c15_i_min = 0xff0;
2486 static const ARMCPRegInfo omap_cp_reginfo[] = {
2487 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2488 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
2489 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
2490 .resetvalue = 0, },
2491 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2492 .access = PL1_RW, .type = ARM_CP_NOP },
2493 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2494 .access = PL1_RW,
2495 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2496 .writefn = omap_ticonfig_write },
2497 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2498 .access = PL1_RW,
2499 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2500 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2501 .access = PL1_RW, .resetvalue = 0xff0,
2502 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2503 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2504 .access = PL1_RW,
2505 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2506 .writefn = omap_threadid_write },
2507 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2508 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2509 .type = ARM_CP_NO_RAW,
2510 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2511 /* TODO: Peripheral port remap register:
2512 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2513 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2514 * when MMU is off.
2516 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
2517 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
2518 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
2519 .writefn = omap_cachemaint_write },
2520 { .name = "C9", .cp = 15, .crn = 9,
2521 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2522 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
2523 REGINFO_SENTINEL
2526 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2527 uint64_t value)
2529 env->cp15.c15_cpar = value & 0x3fff;
2532 static const ARMCPRegInfo xscale_cp_reginfo[] = {
2533 { .name = "XSCALE_CPAR",
2534 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2535 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2536 .writefn = xscale_cpar_write, },
2537 { .name = "XSCALE_AUXCR",
2538 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2539 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2540 .resetvalue = 0, },
2541 /* XScale specific cache-lockdown: since we have no cache we NOP these
2542 * and hope the guest does not really rely on cache behaviour.
2544 { .name = "XSCALE_LOCK_ICACHE_LINE",
2545 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2546 .access = PL1_W, .type = ARM_CP_NOP },
2547 { .name = "XSCALE_UNLOCK_ICACHE",
2548 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2549 .access = PL1_W, .type = ARM_CP_NOP },
2550 { .name = "XSCALE_DCACHE_LOCK",
2551 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2552 .access = PL1_RW, .type = ARM_CP_NOP },
2553 { .name = "XSCALE_UNLOCK_DCACHE",
2554 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2555 .access = PL1_W, .type = ARM_CP_NOP },
2556 REGINFO_SENTINEL
2559 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2560 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2561 * implementation of this implementation-defined space.
2562 * Ideally this should eventually disappear in favour of actually
2563 * implementing the correct behaviour for all cores.
2565 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2566 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2567 .access = PL1_RW,
2568 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
2569 .resetvalue = 0 },
2570 REGINFO_SENTINEL
2573 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2574 /* Cache status: RAZ because we have no cache so it's always clean */
2575 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
2576 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2577 .resetvalue = 0 },
2578 REGINFO_SENTINEL
2581 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2582 /* We never have a a block transfer operation in progress */
2583 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
2584 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2585 .resetvalue = 0 },
2586 /* The cache ops themselves: these all NOP for QEMU */
2587 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2588 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2589 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2590 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2591 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2592 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2593 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2594 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2595 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2596 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2597 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2598 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2599 REGINFO_SENTINEL
2602 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2603 /* The cache test-and-clean instructions always return (1 << 30)
2604 * to indicate that there are no dirty cache lines.
2606 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
2607 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2608 .resetvalue = (1 << 30) },
2609 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
2610 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2611 .resetvalue = (1 << 30) },
2612 REGINFO_SENTINEL
2615 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2616 /* Ignore ReadBuffer accesses */
2617 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2618 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2619 .access = PL1_RW, .resetvalue = 0,
2620 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
2621 REGINFO_SENTINEL
2624 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2626 ARMCPU *cpu = arm_env_get_cpu(env);
2627 unsigned int cur_el = arm_current_el(env);
2628 bool secure = arm_is_secure(env);
2630 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2631 return env->cp15.vpidr_el2;
2633 return raw_read(env, ri);
2636 static uint64_t mpidr_read_val(CPUARMState *env)
2638 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2639 uint64_t mpidr = cpu->mp_affinity;
2641 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2642 mpidr |= (1U << 31);
2643 /* Cores which are uniprocessor (non-coherent)
2644 * but still implement the MP extensions set
2645 * bit 30. (For instance, Cortex-R5).
2647 if (cpu->mp_is_up) {
2648 mpidr |= (1u << 30);
2651 return mpidr;
2654 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2656 unsigned int cur_el = arm_current_el(env);
2657 bool secure = arm_is_secure(env);
2659 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2660 return env->cp15.vmpidr_el2;
2662 return mpidr_read_val(env);
2665 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
2666 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2667 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
2668 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
2669 REGINFO_SENTINEL
2672 static const ARMCPRegInfo lpae_cp_reginfo[] = {
2673 /* NOP AMAIR0/1 */
2674 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2675 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
2676 .access = PL1_RW, .type = ARM_CP_CONST,
2677 .resetvalue = 0 },
2678 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
2679 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
2680 .access = PL1_RW, .type = ARM_CP_CONST,
2681 .resetvalue = 0 },
2682 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
2683 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2684 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2685 offsetof(CPUARMState, cp15.par_ns)} },
2686 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
2687 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2688 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2689 offsetof(CPUARMState, cp15.ttbr0_ns) },
2690 .writefn = vmsa_ttbr_write, },
2691 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
2692 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2693 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2694 offsetof(CPUARMState, cp15.ttbr1_ns) },
2695 .writefn = vmsa_ttbr_write, },
2696 REGINFO_SENTINEL
2699 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2701 return vfp_get_fpcr(env);
2704 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2705 uint64_t value)
2707 vfp_set_fpcr(env, value);
2710 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2712 return vfp_get_fpsr(env);
2715 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2716 uint64_t value)
2718 vfp_set_fpsr(env, value);
2721 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2722 bool isread)
2724 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
2725 return CP_ACCESS_TRAP;
2727 return CP_ACCESS_OK;
2730 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2731 uint64_t value)
2733 env->daif = value & PSTATE_DAIF;
2736 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
2737 const ARMCPRegInfo *ri,
2738 bool isread)
2740 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2741 * SCTLR_EL1.UCI is set.
2743 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
2744 return CP_ACCESS_TRAP;
2746 return CP_ACCESS_OK;
2749 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2750 * Page D4-1736 (DDI0487A.b)
2753 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2754 uint64_t value)
2756 ARMCPU *cpu = arm_env_get_cpu(env);
2757 CPUState *cs = CPU(cpu);
2759 if (arm_is_secure_below_el3(env)) {
2760 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2761 } else {
2762 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2766 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2767 uint64_t value)
2769 bool sec = arm_is_secure_below_el3(env);
2770 CPUState *other_cs;
2772 CPU_FOREACH(other_cs) {
2773 if (sec) {
2774 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2775 } else {
2776 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2777 ARMMMUIdx_S12NSE0, -1);
2782 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2783 uint64_t value)
2785 /* Note that the 'ALL' scope must invalidate both stage 1 and
2786 * stage 2 translations, whereas most other scopes only invalidate
2787 * stage 1 translations.
2789 ARMCPU *cpu = arm_env_get_cpu(env);
2790 CPUState *cs = CPU(cpu);
2792 if (arm_is_secure_below_el3(env)) {
2793 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2794 } else {
2795 if (arm_feature(env, ARM_FEATURE_EL2)) {
2796 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2797 ARMMMUIdx_S2NS, -1);
2798 } else {
2799 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2804 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2805 uint64_t value)
2807 ARMCPU *cpu = arm_env_get_cpu(env);
2808 CPUState *cs = CPU(cpu);
2810 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E2, -1);
2813 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2814 uint64_t value)
2816 ARMCPU *cpu = arm_env_get_cpu(env);
2817 CPUState *cs = CPU(cpu);
2819 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E3, -1);
2822 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2823 uint64_t value)
2825 /* Note that the 'ALL' scope must invalidate both stage 1 and
2826 * stage 2 translations, whereas most other scopes only invalidate
2827 * stage 1 translations.
2829 bool sec = arm_is_secure_below_el3(env);
2830 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
2831 CPUState *other_cs;
2833 CPU_FOREACH(other_cs) {
2834 if (sec) {
2835 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2836 } else if (has_el2) {
2837 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2838 ARMMMUIdx_S12NSE0, ARMMMUIdx_S2NS, -1);
2839 } else {
2840 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2841 ARMMMUIdx_S12NSE0, -1);
2846 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2847 uint64_t value)
2849 CPUState *other_cs;
2851 CPU_FOREACH(other_cs) {
2852 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E2, -1);
2856 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2857 uint64_t value)
2859 CPUState *other_cs;
2861 CPU_FOREACH(other_cs) {
2862 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E3, -1);
2866 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2867 uint64_t value)
2869 /* Invalidate by VA, EL1&0 (AArch64 version).
2870 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
2871 * since we don't support flush-for-specific-ASID-only or
2872 * flush-last-level-only.
2874 ARMCPU *cpu = arm_env_get_cpu(env);
2875 CPUState *cs = CPU(cpu);
2876 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2878 if (arm_is_secure_below_el3(env)) {
2879 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1SE1,
2880 ARMMMUIdx_S1SE0, -1);
2881 } else {
2882 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S12NSE1,
2883 ARMMMUIdx_S12NSE0, -1);
2887 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2888 uint64_t value)
2890 /* Invalidate by VA, EL2
2891 * Currently handles both VAE2 and VALE2, since we don't support
2892 * flush-last-level-only.
2894 ARMCPU *cpu = arm_env_get_cpu(env);
2895 CPUState *cs = CPU(cpu);
2896 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2898 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E2, -1);
2901 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2902 uint64_t value)
2904 /* Invalidate by VA, EL3
2905 * Currently handles both VAE3 and VALE3, since we don't support
2906 * flush-last-level-only.
2908 ARMCPU *cpu = arm_env_get_cpu(env);
2909 CPUState *cs = CPU(cpu);
2910 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2912 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E3, -1);
2915 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2916 uint64_t value)
2918 bool sec = arm_is_secure_below_el3(env);
2919 CPUState *other_cs;
2920 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2922 CPU_FOREACH(other_cs) {
2923 if (sec) {
2924 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1SE1,
2925 ARMMMUIdx_S1SE0, -1);
2926 } else {
2927 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S12NSE1,
2928 ARMMMUIdx_S12NSE0, -1);
2933 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2934 uint64_t value)
2936 CPUState *other_cs;
2937 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2939 CPU_FOREACH(other_cs) {
2940 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E2, -1);
2944 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2945 uint64_t value)
2947 CPUState *other_cs;
2948 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2950 CPU_FOREACH(other_cs) {
2951 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E3, -1);
2955 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2956 uint64_t value)
2958 /* Invalidate by IPA. This has to invalidate any structures that
2959 * contain only stage 2 translation information, but does not need
2960 * to apply to structures that contain combined stage 1 and stage 2
2961 * translation information.
2962 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
2964 ARMCPU *cpu = arm_env_get_cpu(env);
2965 CPUState *cs = CPU(cpu);
2966 uint64_t pageaddr;
2968 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2969 return;
2972 pageaddr = sextract64(value << 12, 0, 48);
2974 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S2NS, -1);
2977 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2978 uint64_t value)
2980 CPUState *other_cs;
2981 uint64_t pageaddr;
2983 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2984 return;
2987 pageaddr = sextract64(value << 12, 0, 48);
2989 CPU_FOREACH(other_cs) {
2990 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S2NS, -1);
2994 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
2995 bool isread)
2997 /* We don't implement EL2, so the only control on DC ZVA is the
2998 * bit in the SCTLR which can prohibit access for EL0.
3000 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
3001 return CP_ACCESS_TRAP;
3003 return CP_ACCESS_OK;
3006 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3008 ARMCPU *cpu = arm_env_get_cpu(env);
3009 int dzp_bit = 1 << 4;
3011 /* DZP indicates whether DC ZVA access is allowed */
3012 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
3013 dzp_bit = 0;
3015 return cpu->dcz_blocksize | dzp_bit;
3018 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3019 bool isread)
3021 if (!(env->pstate & PSTATE_SP)) {
3022 /* Access to SP_EL0 is undefined if it's being used as
3023 * the stack pointer.
3025 return CP_ACCESS_TRAP_UNCATEGORIZED;
3027 return CP_ACCESS_OK;
3030 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3032 return env->pstate & PSTATE_SP;
3035 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3037 update_spsel(env, val);
3040 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3041 uint64_t value)
3043 ARMCPU *cpu = arm_env_get_cpu(env);
3045 if (raw_read(env, ri) == value) {
3046 /* Skip the TLB flush if nothing actually changed; Linux likes
3047 * to do a lot of pointless SCTLR writes.
3049 return;
3052 raw_write(env, ri, value);
3053 /* ??? Lots of these bits are not implemented. */
3054 /* This may enable/disable the MMU, so do a TLB flush. */
3055 tlb_flush(CPU(cpu), 1);
3058 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3059 bool isread)
3061 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
3062 return CP_ACCESS_TRAP_FP_EL2;
3064 if (env->cp15.cptr_el[3] & CPTR_TFP) {
3065 return CP_ACCESS_TRAP_FP_EL3;
3067 return CP_ACCESS_OK;
3070 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3071 uint64_t value)
3073 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3076 static const ARMCPRegInfo v8_cp_reginfo[] = {
3077 /* Minimal set of EL0-visible registers. This will need to be expanded
3078 * significantly for system emulation of AArch64 CPUs.
3080 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3081 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3082 .access = PL0_RW, .type = ARM_CP_NZCV },
3083 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3084 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
3085 .type = ARM_CP_NO_RAW,
3086 .access = PL0_RW, .accessfn = aa64_daif_access,
3087 .fieldoffset = offsetof(CPUARMState, daif),
3088 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
3089 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3090 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
3091 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
3092 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3093 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
3094 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
3095 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3096 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
3097 .access = PL0_R, .type = ARM_CP_NO_RAW,
3098 .readfn = aa64_dczid_read },
3099 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3100 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3101 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3102 #ifndef CONFIG_USER_ONLY
3103 /* Avoid overhead of an access check that always passes in user-mode */
3104 .accessfn = aa64_zva_access,
3105 #endif
3107 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3108 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3109 .access = PL1_R, .type = ARM_CP_CURRENTEL },
3110 /* Cache ops: all NOPs since we don't emulate caches */
3111 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3112 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3113 .access = PL1_W, .type = ARM_CP_NOP },
3114 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3115 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3116 .access = PL1_W, .type = ARM_CP_NOP },
3117 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3118 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3119 .access = PL0_W, .type = ARM_CP_NOP,
3120 .accessfn = aa64_cacheop_access },
3121 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3122 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3123 .access = PL1_W, .type = ARM_CP_NOP },
3124 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3125 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3126 .access = PL1_W, .type = ARM_CP_NOP },
3127 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3128 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3129 .access = PL0_W, .type = ARM_CP_NOP,
3130 .accessfn = aa64_cacheop_access },
3131 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3132 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3133 .access = PL1_W, .type = ARM_CP_NOP },
3134 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3135 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3136 .access = PL0_W, .type = ARM_CP_NOP,
3137 .accessfn = aa64_cacheop_access },
3138 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3139 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3140 .access = PL0_W, .type = ARM_CP_NOP,
3141 .accessfn = aa64_cacheop_access },
3142 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3143 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3144 .access = PL1_W, .type = ARM_CP_NOP },
3145 /* TLBI operations */
3146 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
3147 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
3148 .access = PL1_W, .type = ARM_CP_NO_RAW,
3149 .writefn = tlbi_aa64_vmalle1is_write },
3150 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
3151 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
3152 .access = PL1_W, .type = ARM_CP_NO_RAW,
3153 .writefn = tlbi_aa64_vae1is_write },
3154 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
3155 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
3156 .access = PL1_W, .type = ARM_CP_NO_RAW,
3157 .writefn = tlbi_aa64_vmalle1is_write },
3158 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
3159 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
3160 .access = PL1_W, .type = ARM_CP_NO_RAW,
3161 .writefn = tlbi_aa64_vae1is_write },
3162 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
3163 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3164 .access = PL1_W, .type = ARM_CP_NO_RAW,
3165 .writefn = tlbi_aa64_vae1is_write },
3166 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
3167 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3168 .access = PL1_W, .type = ARM_CP_NO_RAW,
3169 .writefn = tlbi_aa64_vae1is_write },
3170 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
3171 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
3172 .access = PL1_W, .type = ARM_CP_NO_RAW,
3173 .writefn = tlbi_aa64_vmalle1_write },
3174 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
3175 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
3176 .access = PL1_W, .type = ARM_CP_NO_RAW,
3177 .writefn = tlbi_aa64_vae1_write },
3178 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
3179 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
3180 .access = PL1_W, .type = ARM_CP_NO_RAW,
3181 .writefn = tlbi_aa64_vmalle1_write },
3182 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
3183 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
3184 .access = PL1_W, .type = ARM_CP_NO_RAW,
3185 .writefn = tlbi_aa64_vae1_write },
3186 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
3187 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3188 .access = PL1_W, .type = ARM_CP_NO_RAW,
3189 .writefn = tlbi_aa64_vae1_write },
3190 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
3191 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3192 .access = PL1_W, .type = ARM_CP_NO_RAW,
3193 .writefn = tlbi_aa64_vae1_write },
3194 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3195 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3196 .access = PL2_W, .type = ARM_CP_NO_RAW,
3197 .writefn = tlbi_aa64_ipas2e1is_write },
3198 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3199 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3200 .access = PL2_W, .type = ARM_CP_NO_RAW,
3201 .writefn = tlbi_aa64_ipas2e1is_write },
3202 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3203 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3204 .access = PL2_W, .type = ARM_CP_NO_RAW,
3205 .writefn = tlbi_aa64_alle1is_write },
3206 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3207 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3208 .access = PL2_W, .type = ARM_CP_NO_RAW,
3209 .writefn = tlbi_aa64_alle1is_write },
3210 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3211 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3212 .access = PL2_W, .type = ARM_CP_NO_RAW,
3213 .writefn = tlbi_aa64_ipas2e1_write },
3214 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3215 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3216 .access = PL2_W, .type = ARM_CP_NO_RAW,
3217 .writefn = tlbi_aa64_ipas2e1_write },
3218 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3219 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3220 .access = PL2_W, .type = ARM_CP_NO_RAW,
3221 .writefn = tlbi_aa64_alle1_write },
3222 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3223 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3224 .access = PL2_W, .type = ARM_CP_NO_RAW,
3225 .writefn = tlbi_aa64_alle1is_write },
3226 #ifndef CONFIG_USER_ONLY
3227 /* 64 bit address translation operations */
3228 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3229 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
3230 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3231 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3232 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
3233 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3234 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3235 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
3236 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3237 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3238 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
3239 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3240 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
3241 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
3242 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3243 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
3244 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
3245 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3246 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
3247 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
3248 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3249 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
3250 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
3251 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3252 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3253 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3254 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3255 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3256 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3257 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3258 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3259 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3260 .type = ARM_CP_ALIAS,
3261 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3262 .access = PL1_RW, .resetvalue = 0,
3263 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3264 .writefn = par_write },
3265 #endif
3266 /* TLB invalidate last level of translation table walk */
3267 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3268 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
3269 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3270 .type = ARM_CP_NO_RAW, .access = PL1_W,
3271 .writefn = tlbimvaa_is_write },
3272 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3273 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
3274 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3275 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
3276 /* 32 bit cache operations */
3277 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3278 .type = ARM_CP_NOP, .access = PL1_W },
3279 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3280 .type = ARM_CP_NOP, .access = PL1_W },
3281 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3282 .type = ARM_CP_NOP, .access = PL1_W },
3283 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3284 .type = ARM_CP_NOP, .access = PL1_W },
3285 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3286 .type = ARM_CP_NOP, .access = PL1_W },
3287 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3288 .type = ARM_CP_NOP, .access = PL1_W },
3289 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3290 .type = ARM_CP_NOP, .access = PL1_W },
3291 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3292 .type = ARM_CP_NOP, .access = PL1_W },
3293 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3294 .type = ARM_CP_NOP, .access = PL1_W },
3295 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3296 .type = ARM_CP_NOP, .access = PL1_W },
3297 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3298 .type = ARM_CP_NOP, .access = PL1_W },
3299 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3300 .type = ARM_CP_NOP, .access = PL1_W },
3301 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3302 .type = ARM_CP_NOP, .access = PL1_W },
3303 /* MMU Domain access control / MPU write buffer control */
3304 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3305 .access = PL1_RW, .resetvalue = 0,
3306 .writefn = dacr_write, .raw_writefn = raw_write,
3307 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3308 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
3309 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
3310 .type = ARM_CP_ALIAS,
3311 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
3312 .access = PL1_RW,
3313 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
3314 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
3315 .type = ARM_CP_ALIAS,
3316 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
3317 .access = PL1_RW,
3318 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
3319 /* We rely on the access checks not allowing the guest to write to the
3320 * state field when SPSel indicates that it's being used as the stack
3321 * pointer.
3323 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3324 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3325 .access = PL1_RW, .accessfn = sp_el0_access,
3326 .type = ARM_CP_ALIAS,
3327 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
3328 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3329 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
3330 .access = PL2_RW, .type = ARM_CP_ALIAS,
3331 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
3332 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3333 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
3334 .type = ARM_CP_NO_RAW,
3335 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
3336 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3337 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3338 .type = ARM_CP_ALIAS,
3339 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3340 .access = PL2_RW, .accessfn = fpexc32_access },
3341 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3342 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3343 .access = PL2_RW, .resetvalue = 0,
3344 .writefn = dacr_write, .raw_writefn = raw_write,
3345 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3346 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3347 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3348 .access = PL2_RW, .resetvalue = 0,
3349 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3350 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3351 .type = ARM_CP_ALIAS,
3352 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3353 .access = PL2_RW,
3354 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3355 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3356 .type = ARM_CP_ALIAS,
3357 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3358 .access = PL2_RW,
3359 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3360 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3361 .type = ARM_CP_ALIAS,
3362 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3363 .access = PL2_RW,
3364 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3365 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3366 .type = ARM_CP_ALIAS,
3367 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3368 .access = PL2_RW,
3369 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
3370 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3371 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3372 .resetvalue = 0,
3373 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3374 { .name = "SDCR", .type = ARM_CP_ALIAS,
3375 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3376 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3377 .writefn = sdcr_write,
3378 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
3379 REGINFO_SENTINEL
3382 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
3383 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
3384 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3385 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3386 .access = PL2_RW,
3387 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3388 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3389 .type = ARM_CP_NO_RAW,
3390 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3391 .access = PL2_RW,
3392 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3393 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3394 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3395 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3396 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3397 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3398 .access = PL2_RW, .type = ARM_CP_CONST,
3399 .resetvalue = 0 },
3400 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3401 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3402 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3403 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3404 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3405 .access = PL2_RW, .type = ARM_CP_CONST,
3406 .resetvalue = 0 },
3407 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3408 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3409 .access = PL2_RW, .type = ARM_CP_CONST,
3410 .resetvalue = 0 },
3411 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3412 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3413 .access = PL2_RW, .type = ARM_CP_CONST,
3414 .resetvalue = 0 },
3415 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3416 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3417 .access = PL2_RW, .type = ARM_CP_CONST,
3418 .resetvalue = 0 },
3419 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3420 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3421 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3422 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3423 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3424 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3425 .type = ARM_CP_CONST, .resetvalue = 0 },
3426 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3427 .cp = 15, .opc1 = 6, .crm = 2,
3428 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3429 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3430 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3431 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3432 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3433 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3434 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3435 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3436 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3437 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3438 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3439 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3440 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3441 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3442 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3443 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3444 .resetvalue = 0 },
3445 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3446 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3447 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3448 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3449 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3450 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3451 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3452 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3453 .resetvalue = 0 },
3454 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3455 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3456 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3457 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3458 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3459 .resetvalue = 0 },
3460 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3461 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3462 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3463 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3464 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3465 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3466 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3467 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3468 .access = PL2_RW, .accessfn = access_tda,
3469 .type = ARM_CP_CONST, .resetvalue = 0 },
3470 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3471 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3472 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3473 .type = ARM_CP_CONST, .resetvalue = 0 },
3474 REGINFO_SENTINEL
3477 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3479 ARMCPU *cpu = arm_env_get_cpu(env);
3480 uint64_t valid_mask = HCR_MASK;
3482 if (arm_feature(env, ARM_FEATURE_EL3)) {
3483 valid_mask &= ~HCR_HCD;
3484 } else {
3485 valid_mask &= ~HCR_TSC;
3488 /* Clear RES0 bits. */
3489 value &= valid_mask;
3491 /* These bits change the MMU setup:
3492 * HCR_VM enables stage 2 translation
3493 * HCR_PTW forbids certain page-table setups
3494 * HCR_DC Disables stage1 and enables stage2 translation
3496 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3497 tlb_flush(CPU(cpu), 1);
3499 raw_write(env, ri, value);
3502 static const ARMCPRegInfo el2_cp_reginfo[] = {
3503 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3504 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3505 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3506 .writefn = hcr_write },
3507 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
3508 .type = ARM_CP_ALIAS,
3509 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3510 .access = PL2_RW,
3511 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
3512 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
3513 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3514 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
3515 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3516 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3517 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3518 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
3519 .type = ARM_CP_ALIAS,
3520 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
3521 .access = PL2_RW,
3522 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
3523 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3524 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3525 .access = PL2_RW, .writefn = vbar_write,
3526 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3527 .resetvalue = 0 },
3528 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3529 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
3530 .access = PL3_RW, .type = ARM_CP_ALIAS,
3531 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
3532 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3533 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3534 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3535 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
3536 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3537 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3538 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3539 .resetvalue = 0 },
3540 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3541 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3542 .access = PL2_RW, .type = ARM_CP_ALIAS,
3543 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
3544 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3545 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3546 .access = PL2_RW, .type = ARM_CP_CONST,
3547 .resetvalue = 0 },
3548 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3549 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3550 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3551 .access = PL2_RW, .type = ARM_CP_CONST,
3552 .resetvalue = 0 },
3553 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3554 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3555 .access = PL2_RW, .type = ARM_CP_CONST,
3556 .resetvalue = 0 },
3557 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3558 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3559 .access = PL2_RW, .type = ARM_CP_CONST,
3560 .resetvalue = 0 },
3561 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3562 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3563 .access = PL2_RW,
3564 /* no .writefn needed as this can't cause an ASID change;
3565 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3567 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
3568 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3569 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3570 .type = ARM_CP_ALIAS,
3571 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3572 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3573 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3574 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3575 .access = PL2_RW,
3576 /* no .writefn needed as this can't cause an ASID change;
3577 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3579 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3580 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3581 .cp = 15, .opc1 = 6, .crm = 2,
3582 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3583 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3584 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3585 .writefn = vttbr_write },
3586 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3587 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3588 .access = PL2_RW, .writefn = vttbr_write,
3589 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
3590 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3591 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3592 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3593 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
3594 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3595 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3596 .access = PL2_RW, .resetvalue = 0,
3597 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
3598 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3599 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3600 .access = PL2_RW, .resetvalue = 0,
3601 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3602 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3603 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3604 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3605 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3606 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3607 .type = ARM_CP_NO_RAW, .access = PL2_W,
3608 .writefn = tlbi_aa64_alle2_write },
3609 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3610 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3611 .type = ARM_CP_NO_RAW, .access = PL2_W,
3612 .writefn = tlbi_aa64_vae2_write },
3613 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3614 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3615 .access = PL2_W, .type = ARM_CP_NO_RAW,
3616 .writefn = tlbi_aa64_vae2_write },
3617 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3618 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3619 .access = PL2_W, .type = ARM_CP_NO_RAW,
3620 .writefn = tlbi_aa64_alle2is_write },
3621 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3622 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3623 .type = ARM_CP_NO_RAW, .access = PL2_W,
3624 .writefn = tlbi_aa64_vae2is_write },
3625 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3626 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3627 .access = PL2_W, .type = ARM_CP_NO_RAW,
3628 .writefn = tlbi_aa64_vae2is_write },
3629 #ifndef CONFIG_USER_ONLY
3630 /* Unlike the other EL2-related AT operations, these must
3631 * UNDEF from EL3 if EL2 is not implemented, which is why we
3632 * define them here rather than with the rest of the AT ops.
3634 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3635 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3636 .access = PL2_W, .accessfn = at_s1e2_access,
3637 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3638 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3639 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3640 .access = PL2_W, .accessfn = at_s1e2_access,
3641 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3642 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3643 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3644 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3645 * to behave as if SCR.NS was 1.
3647 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3648 .access = PL2_W,
3649 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3650 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3651 .access = PL2_W,
3652 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3653 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3654 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3655 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3656 * reset values as IMPDEF. We choose to reset to 3 to comply with
3657 * both ARMv7 and ARMv8.
3659 .access = PL2_RW, .resetvalue = 3,
3660 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
3661 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3662 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3663 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3664 .writefn = gt_cntvoff_write,
3665 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3666 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3667 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3668 .writefn = gt_cntvoff_write,
3669 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3670 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3671 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3672 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3673 .type = ARM_CP_IO, .access = PL2_RW,
3674 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3675 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3676 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3677 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3678 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3679 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3680 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3681 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
3682 .resetfn = gt_hyp_timer_reset,
3683 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3684 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3685 .type = ARM_CP_IO,
3686 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3687 .access = PL2_RW,
3688 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3689 .resetvalue = 0,
3690 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
3691 #endif
3692 /* The only field of MDCR_EL2 that has a defined architectural reset value
3693 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
3694 * don't impelment any PMU event counters, so using zero as a reset
3695 * value for MDCR_EL2 is okay
3697 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3698 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3699 .access = PL2_RW, .resetvalue = 0,
3700 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
3701 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
3702 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3703 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3704 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3705 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
3706 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3707 .access = PL2_RW,
3708 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3709 REGINFO_SENTINEL
3712 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
3713 bool isread)
3715 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
3716 * At Secure EL1 it traps to EL3.
3718 if (arm_current_el(env) == 3) {
3719 return CP_ACCESS_OK;
3721 if (arm_is_secure_below_el3(env)) {
3722 return CP_ACCESS_TRAP_EL3;
3724 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
3725 if (isread) {
3726 return CP_ACCESS_OK;
3728 return CP_ACCESS_TRAP_UNCATEGORIZED;
3731 static const ARMCPRegInfo el3_cp_reginfo[] = {
3732 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
3733 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
3734 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
3735 .resetvalue = 0, .writefn = scr_write },
3736 { .name = "SCR", .type = ARM_CP_ALIAS,
3737 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
3738 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3739 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
3740 .writefn = scr_write },
3741 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
3742 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
3743 .access = PL3_RW, .resetvalue = 0,
3744 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
3745 { .name = "SDER",
3746 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
3747 .access = PL3_RW, .resetvalue = 0,
3748 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
3749 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
3750 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3751 .writefn = vbar_write, .resetvalue = 0,
3752 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
3753 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
3754 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
3755 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3756 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
3757 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
3758 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
3759 .access = PL3_RW,
3760 /* no .writefn needed as this can't cause an ASID change;
3761 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3763 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
3764 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
3765 .type = ARM_CP_ALIAS,
3766 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
3767 .access = PL3_RW,
3768 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
3769 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
3770 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
3771 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
3772 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
3773 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
3774 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
3775 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
3776 .type = ARM_CP_ALIAS,
3777 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
3778 .access = PL3_RW,
3779 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
3780 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
3781 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
3782 .access = PL3_RW, .writefn = vbar_write,
3783 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
3784 .resetvalue = 0 },
3785 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
3786 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
3787 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
3788 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
3789 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
3790 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
3791 .access = PL3_RW, .resetvalue = 0,
3792 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
3793 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
3794 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
3795 .access = PL3_RW, .type = ARM_CP_CONST,
3796 .resetvalue = 0 },
3797 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
3798 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
3799 .access = PL3_RW, .type = ARM_CP_CONST,
3800 .resetvalue = 0 },
3801 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
3802 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
3803 .access = PL3_RW, .type = ARM_CP_CONST,
3804 .resetvalue = 0 },
3805 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
3806 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
3807 .access = PL3_W, .type = ARM_CP_NO_RAW,
3808 .writefn = tlbi_aa64_alle3is_write },
3809 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
3810 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
3811 .access = PL3_W, .type = ARM_CP_NO_RAW,
3812 .writefn = tlbi_aa64_vae3is_write },
3813 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
3814 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
3815 .access = PL3_W, .type = ARM_CP_NO_RAW,
3816 .writefn = tlbi_aa64_vae3is_write },
3817 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
3818 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
3819 .access = PL3_W, .type = ARM_CP_NO_RAW,
3820 .writefn = tlbi_aa64_alle3_write },
3821 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
3822 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
3823 .access = PL3_W, .type = ARM_CP_NO_RAW,
3824 .writefn = tlbi_aa64_vae3_write },
3825 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
3826 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
3827 .access = PL3_W, .type = ARM_CP_NO_RAW,
3828 .writefn = tlbi_aa64_vae3_write },
3829 REGINFO_SENTINEL
3832 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3833 bool isread)
3835 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
3836 * but the AArch32 CTR has its own reginfo struct)
3838 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
3839 return CP_ACCESS_TRAP;
3841 return CP_ACCESS_OK;
3844 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3845 uint64_t value)
3847 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
3848 * read via a bit in OSLSR_EL1.
3850 int oslock;
3852 if (ri->state == ARM_CP_STATE_AA32) {
3853 oslock = (value == 0xC5ACCE55);
3854 } else {
3855 oslock = value & 1;
3858 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
3861 static const ARMCPRegInfo debug_cp_reginfo[] = {
3862 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
3863 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
3864 * unlike DBGDRAR it is never accessible from EL0.
3865 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
3866 * accessor.
3868 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
3869 .access = PL0_R, .accessfn = access_tdra,
3870 .type = ARM_CP_CONST, .resetvalue = 0 },
3871 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
3872 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
3873 .access = PL1_R, .accessfn = access_tdra,
3874 .type = ARM_CP_CONST, .resetvalue = 0 },
3875 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3876 .access = PL0_R, .accessfn = access_tdra,
3877 .type = ARM_CP_CONST, .resetvalue = 0 },
3878 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
3879 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
3880 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
3881 .access = PL1_RW, .accessfn = access_tda,
3882 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
3883 .resetvalue = 0 },
3884 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
3885 * We don't implement the configurable EL0 access.
3887 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
3888 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
3889 .type = ARM_CP_ALIAS,
3890 .access = PL1_R, .accessfn = access_tda,
3891 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
3892 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
3893 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
3894 .access = PL1_W, .type = ARM_CP_NO_RAW,
3895 .accessfn = access_tdosa,
3896 .writefn = oslar_write },
3897 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
3898 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
3899 .access = PL1_R, .resetvalue = 10,
3900 .accessfn = access_tdosa,
3901 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
3902 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
3903 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
3904 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
3905 .access = PL1_RW, .accessfn = access_tdosa,
3906 .type = ARM_CP_NOP },
3907 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
3908 * implement vector catch debug events yet.
3910 { .name = "DBGVCR",
3911 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
3912 .access = PL1_RW, .accessfn = access_tda,
3913 .type = ARM_CP_NOP },
3914 REGINFO_SENTINEL
3917 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
3918 /* 64 bit access versions of the (dummy) debug registers */
3919 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
3920 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3921 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
3922 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3923 REGINFO_SENTINEL
3926 void hw_watchpoint_update(ARMCPU *cpu, int n)
3928 CPUARMState *env = &cpu->env;
3929 vaddr len = 0;
3930 vaddr wvr = env->cp15.dbgwvr[n];
3931 uint64_t wcr = env->cp15.dbgwcr[n];
3932 int mask;
3933 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
3935 if (env->cpu_watchpoint[n]) {
3936 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
3937 env->cpu_watchpoint[n] = NULL;
3940 if (!extract64(wcr, 0, 1)) {
3941 /* E bit clear : watchpoint disabled */
3942 return;
3945 switch (extract64(wcr, 3, 2)) {
3946 case 0:
3947 /* LSC 00 is reserved and must behave as if the wp is disabled */
3948 return;
3949 case 1:
3950 flags |= BP_MEM_READ;
3951 break;
3952 case 2:
3953 flags |= BP_MEM_WRITE;
3954 break;
3955 case 3:
3956 flags |= BP_MEM_ACCESS;
3957 break;
3960 /* Attempts to use both MASK and BAS fields simultaneously are
3961 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
3962 * thus generating a watchpoint for every byte in the masked region.
3964 mask = extract64(wcr, 24, 4);
3965 if (mask == 1 || mask == 2) {
3966 /* Reserved values of MASK; we must act as if the mask value was
3967 * some non-reserved value, or as if the watchpoint were disabled.
3968 * We choose the latter.
3970 return;
3971 } else if (mask) {
3972 /* Watchpoint covers an aligned area up to 2GB in size */
3973 len = 1ULL << mask;
3974 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
3975 * whether the watchpoint fires when the unmasked bits match; we opt
3976 * to generate the exceptions.
3978 wvr &= ~(len - 1);
3979 } else {
3980 /* Watchpoint covers bytes defined by the byte address select bits */
3981 int bas = extract64(wcr, 5, 8);
3982 int basstart;
3984 if (bas == 0) {
3985 /* This must act as if the watchpoint is disabled */
3986 return;
3989 if (extract64(wvr, 2, 1)) {
3990 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
3991 * ignored, and BAS[3:0] define which bytes to watch.
3993 bas &= 0xf;
3995 /* The BAS bits are supposed to be programmed to indicate a contiguous
3996 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
3997 * we fire for each byte in the word/doubleword addressed by the WVR.
3998 * We choose to ignore any non-zero bits after the first range of 1s.
4000 basstart = ctz32(bas);
4001 len = cto32(bas >> basstart);
4002 wvr += basstart;
4005 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4006 &env->cpu_watchpoint[n]);
4009 void hw_watchpoint_update_all(ARMCPU *cpu)
4011 int i;
4012 CPUARMState *env = &cpu->env;
4014 /* Completely clear out existing QEMU watchpoints and our array, to
4015 * avoid possible stale entries following migration load.
4017 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4018 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4020 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4021 hw_watchpoint_update(cpu, i);
4025 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4026 uint64_t value)
4028 ARMCPU *cpu = arm_env_get_cpu(env);
4029 int i = ri->crm;
4031 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4032 * register reads and behaves as if values written are sign extended.
4033 * Bits [1:0] are RES0.
4035 value = sextract64(value, 0, 49) & ~3ULL;
4037 raw_write(env, ri, value);
4038 hw_watchpoint_update(cpu, i);
4041 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4042 uint64_t value)
4044 ARMCPU *cpu = arm_env_get_cpu(env);
4045 int i = ri->crm;
4047 raw_write(env, ri, value);
4048 hw_watchpoint_update(cpu, i);
4051 void hw_breakpoint_update(ARMCPU *cpu, int n)
4053 CPUARMState *env = &cpu->env;
4054 uint64_t bvr = env->cp15.dbgbvr[n];
4055 uint64_t bcr = env->cp15.dbgbcr[n];
4056 vaddr addr;
4057 int bt;
4058 int flags = BP_CPU;
4060 if (env->cpu_breakpoint[n]) {
4061 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4062 env->cpu_breakpoint[n] = NULL;
4065 if (!extract64(bcr, 0, 1)) {
4066 /* E bit clear : watchpoint disabled */
4067 return;
4070 bt = extract64(bcr, 20, 4);
4072 switch (bt) {
4073 case 4: /* unlinked address mismatch (reserved if AArch64) */
4074 case 5: /* linked address mismatch (reserved if AArch64) */
4075 qemu_log_mask(LOG_UNIMP,
4076 "arm: address mismatch breakpoint types not implemented");
4077 return;
4078 case 0: /* unlinked address match */
4079 case 1: /* linked address match */
4081 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4082 * we behave as if the register was sign extended. Bits [1:0] are
4083 * RES0. The BAS field is used to allow setting breakpoints on 16
4084 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4085 * a bp will fire if the addresses covered by the bp and the addresses
4086 * covered by the insn overlap but the insn doesn't start at the
4087 * start of the bp address range. We choose to require the insn and
4088 * the bp to have the same address. The constraints on writing to
4089 * BAS enforced in dbgbcr_write mean we have only four cases:
4090 * 0b0000 => no breakpoint
4091 * 0b0011 => breakpoint on addr
4092 * 0b1100 => breakpoint on addr + 2
4093 * 0b1111 => breakpoint on addr
4094 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4096 int bas = extract64(bcr, 5, 4);
4097 addr = sextract64(bvr, 0, 49) & ~3ULL;
4098 if (bas == 0) {
4099 return;
4101 if (bas == 0xc) {
4102 addr += 2;
4104 break;
4106 case 2: /* unlinked context ID match */
4107 case 8: /* unlinked VMID match (reserved if no EL2) */
4108 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4109 qemu_log_mask(LOG_UNIMP,
4110 "arm: unlinked context breakpoint types not implemented");
4111 return;
4112 case 9: /* linked VMID match (reserved if no EL2) */
4113 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4114 case 3: /* linked context ID match */
4115 default:
4116 /* We must generate no events for Linked context matches (unless
4117 * they are linked to by some other bp/wp, which is handled in
4118 * updates for the linking bp/wp). We choose to also generate no events
4119 * for reserved values.
4121 return;
4124 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4127 void hw_breakpoint_update_all(ARMCPU *cpu)
4129 int i;
4130 CPUARMState *env = &cpu->env;
4132 /* Completely clear out existing QEMU breakpoints and our array, to
4133 * avoid possible stale entries following migration load.
4135 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4136 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4138 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4139 hw_breakpoint_update(cpu, i);
4143 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4144 uint64_t value)
4146 ARMCPU *cpu = arm_env_get_cpu(env);
4147 int i = ri->crm;
4149 raw_write(env, ri, value);
4150 hw_breakpoint_update(cpu, i);
4153 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4154 uint64_t value)
4156 ARMCPU *cpu = arm_env_get_cpu(env);
4157 int i = ri->crm;
4159 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4160 * copy of BAS[0].
4162 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4163 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4165 raw_write(env, ri, value);
4166 hw_breakpoint_update(cpu, i);
4169 static void define_debug_regs(ARMCPU *cpu)
4171 /* Define v7 and v8 architectural debug registers.
4172 * These are just dummy implementations for now.
4174 int i;
4175 int wrps, brps, ctx_cmps;
4176 ARMCPRegInfo dbgdidr = {
4177 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
4178 .access = PL0_R, .accessfn = access_tda,
4179 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
4182 /* Note that all these register fields hold "number of Xs minus 1". */
4183 brps = extract32(cpu->dbgdidr, 24, 4);
4184 wrps = extract32(cpu->dbgdidr, 28, 4);
4185 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4187 assert(ctx_cmps <= brps);
4189 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4190 * of the debug registers such as number of breakpoints;
4191 * check that if they both exist then they agree.
4193 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4194 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4195 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
4196 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
4199 define_one_arm_cp_reg(cpu, &dbgdidr);
4200 define_arm_cp_regs(cpu, debug_cp_reginfo);
4202 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4203 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4206 for (i = 0; i < brps + 1; i++) {
4207 ARMCPRegInfo dbgregs[] = {
4208 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4209 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
4210 .access = PL1_RW, .accessfn = access_tda,
4211 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4212 .writefn = dbgbvr_write, .raw_writefn = raw_write
4214 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4215 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
4216 .access = PL1_RW, .accessfn = access_tda,
4217 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4218 .writefn = dbgbcr_write, .raw_writefn = raw_write
4220 REGINFO_SENTINEL
4222 define_arm_cp_regs(cpu, dbgregs);
4225 for (i = 0; i < wrps + 1; i++) {
4226 ARMCPRegInfo dbgregs[] = {
4227 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4228 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
4229 .access = PL1_RW, .accessfn = access_tda,
4230 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4231 .writefn = dbgwvr_write, .raw_writefn = raw_write
4233 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4234 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
4235 .access = PL1_RW, .accessfn = access_tda,
4236 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4237 .writefn = dbgwcr_write, .raw_writefn = raw_write
4239 REGINFO_SENTINEL
4241 define_arm_cp_regs(cpu, dbgregs);
4245 void register_cp_regs_for_features(ARMCPU *cpu)
4247 /* Register all the coprocessor registers based on feature bits */
4248 CPUARMState *env = &cpu->env;
4249 if (arm_feature(env, ARM_FEATURE_M)) {
4250 /* M profile has no coprocessor registers */
4251 return;
4254 define_arm_cp_regs(cpu, cp_reginfo);
4255 if (!arm_feature(env, ARM_FEATURE_V8)) {
4256 /* Must go early as it is full of wildcards that may be
4257 * overridden by later definitions.
4259 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4262 if (arm_feature(env, ARM_FEATURE_V6)) {
4263 /* The ID registers all have impdef reset values */
4264 ARMCPRegInfo v6_idregs[] = {
4265 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4266 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4267 .access = PL1_R, .type = ARM_CP_CONST,
4268 .resetvalue = cpu->id_pfr0 },
4269 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4270 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
4271 .access = PL1_R, .type = ARM_CP_CONST,
4272 .resetvalue = cpu->id_pfr1 },
4273 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4274 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4275 .access = PL1_R, .type = ARM_CP_CONST,
4276 .resetvalue = cpu->id_dfr0 },
4277 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4278 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4279 .access = PL1_R, .type = ARM_CP_CONST,
4280 .resetvalue = cpu->id_afr0 },
4281 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4282 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4283 .access = PL1_R, .type = ARM_CP_CONST,
4284 .resetvalue = cpu->id_mmfr0 },
4285 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4286 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4287 .access = PL1_R, .type = ARM_CP_CONST,
4288 .resetvalue = cpu->id_mmfr1 },
4289 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4290 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4291 .access = PL1_R, .type = ARM_CP_CONST,
4292 .resetvalue = cpu->id_mmfr2 },
4293 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4294 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4295 .access = PL1_R, .type = ARM_CP_CONST,
4296 .resetvalue = cpu->id_mmfr3 },
4297 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4298 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4299 .access = PL1_R, .type = ARM_CP_CONST,
4300 .resetvalue = cpu->id_isar0 },
4301 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4302 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4303 .access = PL1_R, .type = ARM_CP_CONST,
4304 .resetvalue = cpu->id_isar1 },
4305 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4306 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4307 .access = PL1_R, .type = ARM_CP_CONST,
4308 .resetvalue = cpu->id_isar2 },
4309 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4310 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4311 .access = PL1_R, .type = ARM_CP_CONST,
4312 .resetvalue = cpu->id_isar3 },
4313 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4314 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4315 .access = PL1_R, .type = ARM_CP_CONST,
4316 .resetvalue = cpu->id_isar4 },
4317 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4318 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4319 .access = PL1_R, .type = ARM_CP_CONST,
4320 .resetvalue = cpu->id_isar5 },
4321 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4322 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4323 .access = PL1_R, .type = ARM_CP_CONST,
4324 .resetvalue = cpu->id_mmfr4 },
4325 /* 7 is as yet unallocated and must RAZ */
4326 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4327 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4328 .access = PL1_R, .type = ARM_CP_CONST,
4329 .resetvalue = 0 },
4330 REGINFO_SENTINEL
4332 define_arm_cp_regs(cpu, v6_idregs);
4333 define_arm_cp_regs(cpu, v6_cp_reginfo);
4334 } else {
4335 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4337 if (arm_feature(env, ARM_FEATURE_V6K)) {
4338 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4340 if (arm_feature(env, ARM_FEATURE_V7MP) &&
4341 !arm_feature(env, ARM_FEATURE_MPU)) {
4342 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4344 if (arm_feature(env, ARM_FEATURE_V7)) {
4345 /* v7 performance monitor control register: same implementor
4346 * field as main ID register, and we implement only the cycle
4347 * count register.
4349 #ifndef CONFIG_USER_ONLY
4350 ARMCPRegInfo pmcr = {
4351 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
4352 .access = PL0_RW,
4353 .type = ARM_CP_IO | ARM_CP_ALIAS,
4354 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
4355 .accessfn = pmreg_access, .writefn = pmcr_write,
4356 .raw_writefn = raw_write,
4358 ARMCPRegInfo pmcr64 = {
4359 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4360 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4361 .access = PL0_RW, .accessfn = pmreg_access,
4362 .type = ARM_CP_IO,
4363 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4364 .resetvalue = cpu->midr & 0xff000000,
4365 .writefn = pmcr_write, .raw_writefn = raw_write,
4367 define_one_arm_cp_reg(cpu, &pmcr);
4368 define_one_arm_cp_reg(cpu, &pmcr64);
4369 #endif
4370 ARMCPRegInfo clidr = {
4371 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4372 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
4373 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4375 define_one_arm_cp_reg(cpu, &clidr);
4376 define_arm_cp_regs(cpu, v7_cp_reginfo);
4377 define_debug_regs(cpu);
4378 } else {
4379 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
4381 if (arm_feature(env, ARM_FEATURE_V8)) {
4382 /* AArch64 ID registers, which all have impdef reset values.
4383 * Note that within the ID register ranges the unused slots
4384 * must all RAZ, not UNDEF; future architecture versions may
4385 * define new registers here.
4387 ARMCPRegInfo v8_idregs[] = {
4388 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4389 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4390 .access = PL1_R, .type = ARM_CP_CONST,
4391 .resetvalue = cpu->id_aa64pfr0 },
4392 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4393 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4394 .access = PL1_R, .type = ARM_CP_CONST,
4395 .resetvalue = cpu->id_aa64pfr1},
4396 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4397 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4398 .access = PL1_R, .type = ARM_CP_CONST,
4399 .resetvalue = 0 },
4400 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4401 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4402 .access = PL1_R, .type = ARM_CP_CONST,
4403 .resetvalue = 0 },
4404 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4405 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4406 .access = PL1_R, .type = ARM_CP_CONST,
4407 .resetvalue = 0 },
4408 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4409 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4410 .access = PL1_R, .type = ARM_CP_CONST,
4411 .resetvalue = 0 },
4412 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4413 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4414 .access = PL1_R, .type = ARM_CP_CONST,
4415 .resetvalue = 0 },
4416 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4417 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4418 .access = PL1_R, .type = ARM_CP_CONST,
4419 .resetvalue = 0 },
4420 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4421 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4422 .access = PL1_R, .type = ARM_CP_CONST,
4423 /* We mask out the PMUVer field, because we don't currently
4424 * implement the PMU. Not advertising it prevents the guest
4425 * from trying to use it and getting UNDEFs on registers we
4426 * don't implement.
4428 .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
4429 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4430 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4431 .access = PL1_R, .type = ARM_CP_CONST,
4432 .resetvalue = cpu->id_aa64dfr1 },
4433 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4434 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4435 .access = PL1_R, .type = ARM_CP_CONST,
4436 .resetvalue = 0 },
4437 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4438 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4439 .access = PL1_R, .type = ARM_CP_CONST,
4440 .resetvalue = 0 },
4441 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4442 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4443 .access = PL1_R, .type = ARM_CP_CONST,
4444 .resetvalue = cpu->id_aa64afr0 },
4445 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4446 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4447 .access = PL1_R, .type = ARM_CP_CONST,
4448 .resetvalue = cpu->id_aa64afr1 },
4449 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4450 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4451 .access = PL1_R, .type = ARM_CP_CONST,
4452 .resetvalue = 0 },
4453 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4454 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4455 .access = PL1_R, .type = ARM_CP_CONST,
4456 .resetvalue = 0 },
4457 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4458 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4459 .access = PL1_R, .type = ARM_CP_CONST,
4460 .resetvalue = cpu->id_aa64isar0 },
4461 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4462 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4463 .access = PL1_R, .type = ARM_CP_CONST,
4464 .resetvalue = cpu->id_aa64isar1 },
4465 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4466 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4467 .access = PL1_R, .type = ARM_CP_CONST,
4468 .resetvalue = 0 },
4469 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4470 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4471 .access = PL1_R, .type = ARM_CP_CONST,
4472 .resetvalue = 0 },
4473 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4474 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4475 .access = PL1_R, .type = ARM_CP_CONST,
4476 .resetvalue = 0 },
4477 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4478 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4479 .access = PL1_R, .type = ARM_CP_CONST,
4480 .resetvalue = 0 },
4481 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4482 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4483 .access = PL1_R, .type = ARM_CP_CONST,
4484 .resetvalue = 0 },
4485 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4486 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4487 .access = PL1_R, .type = ARM_CP_CONST,
4488 .resetvalue = 0 },
4489 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4490 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4491 .access = PL1_R, .type = ARM_CP_CONST,
4492 .resetvalue = cpu->id_aa64mmfr0 },
4493 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4494 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4495 .access = PL1_R, .type = ARM_CP_CONST,
4496 .resetvalue = cpu->id_aa64mmfr1 },
4497 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4498 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4499 .access = PL1_R, .type = ARM_CP_CONST,
4500 .resetvalue = 0 },
4501 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4502 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4503 .access = PL1_R, .type = ARM_CP_CONST,
4504 .resetvalue = 0 },
4505 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4506 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
4507 .access = PL1_R, .type = ARM_CP_CONST,
4508 .resetvalue = 0 },
4509 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4510 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
4511 .access = PL1_R, .type = ARM_CP_CONST,
4512 .resetvalue = 0 },
4513 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4514 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
4515 .access = PL1_R, .type = ARM_CP_CONST,
4516 .resetvalue = 0 },
4517 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4518 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
4519 .access = PL1_R, .type = ARM_CP_CONST,
4520 .resetvalue = 0 },
4521 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4522 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4523 .access = PL1_R, .type = ARM_CP_CONST,
4524 .resetvalue = cpu->mvfr0 },
4525 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4526 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4527 .access = PL1_R, .type = ARM_CP_CONST,
4528 .resetvalue = cpu->mvfr1 },
4529 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4530 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4531 .access = PL1_R, .type = ARM_CP_CONST,
4532 .resetvalue = cpu->mvfr2 },
4533 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4534 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
4535 .access = PL1_R, .type = ARM_CP_CONST,
4536 .resetvalue = 0 },
4537 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4538 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
4539 .access = PL1_R, .type = ARM_CP_CONST,
4540 .resetvalue = 0 },
4541 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4542 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
4543 .access = PL1_R, .type = ARM_CP_CONST,
4544 .resetvalue = 0 },
4545 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4546 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
4547 .access = PL1_R, .type = ARM_CP_CONST,
4548 .resetvalue = 0 },
4549 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4550 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
4551 .access = PL1_R, .type = ARM_CP_CONST,
4552 .resetvalue = 0 },
4553 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
4554 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
4555 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4556 .resetvalue = cpu->pmceid0 },
4557 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
4558 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
4559 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4560 .resetvalue = cpu->pmceid0 },
4561 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
4562 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
4563 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4564 .resetvalue = cpu->pmceid1 },
4565 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
4566 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
4567 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4568 .resetvalue = cpu->pmceid1 },
4569 REGINFO_SENTINEL
4571 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4572 if (!arm_feature(env, ARM_FEATURE_EL3) &&
4573 !arm_feature(env, ARM_FEATURE_EL2)) {
4574 ARMCPRegInfo rvbar = {
4575 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4576 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4577 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4579 define_one_arm_cp_reg(cpu, &rvbar);
4581 define_arm_cp_regs(cpu, v8_idregs);
4582 define_arm_cp_regs(cpu, v8_cp_reginfo);
4584 if (arm_feature(env, ARM_FEATURE_EL2)) {
4585 uint64_t vmpidr_def = mpidr_read_val(env);
4586 ARMCPRegInfo vpidr_regs[] = {
4587 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
4588 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4589 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4590 .resetvalue = cpu->midr,
4591 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4592 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
4593 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4594 .access = PL2_RW, .resetvalue = cpu->midr,
4595 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4596 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
4597 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4598 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4599 .resetvalue = vmpidr_def,
4600 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4601 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
4602 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4603 .access = PL2_RW,
4604 .resetvalue = vmpidr_def,
4605 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4606 REGINFO_SENTINEL
4608 define_arm_cp_regs(cpu, vpidr_regs);
4609 define_arm_cp_regs(cpu, el2_cp_reginfo);
4610 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4611 if (!arm_feature(env, ARM_FEATURE_EL3)) {
4612 ARMCPRegInfo rvbar = {
4613 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4614 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4615 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4617 define_one_arm_cp_reg(cpu, &rvbar);
4619 } else {
4620 /* If EL2 is missing but higher ELs are enabled, we need to
4621 * register the no_el2 reginfos.
4623 if (arm_feature(env, ARM_FEATURE_EL3)) {
4624 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
4625 * of MIDR_EL1 and MPIDR_EL1.
4627 ARMCPRegInfo vpidr_regs[] = {
4628 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4629 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4630 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4631 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
4632 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4633 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4634 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4635 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4636 .type = ARM_CP_NO_RAW,
4637 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
4638 REGINFO_SENTINEL
4640 define_arm_cp_regs(cpu, vpidr_regs);
4641 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
4644 if (arm_feature(env, ARM_FEATURE_EL3)) {
4645 define_arm_cp_regs(cpu, el3_cp_reginfo);
4646 ARMCPRegInfo el3_regs[] = {
4647 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4648 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4649 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
4650 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
4651 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
4652 .access = PL3_RW,
4653 .raw_writefn = raw_write, .writefn = sctlr_write,
4654 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
4655 .resetvalue = cpu->reset_sctlr },
4656 REGINFO_SENTINEL
4659 define_arm_cp_regs(cpu, el3_regs);
4661 /* The behaviour of NSACR is sufficiently various that we don't
4662 * try to describe it in a single reginfo:
4663 * if EL3 is 64 bit, then trap to EL3 from S EL1,
4664 * reads as constant 0xc00 from NS EL1 and NS EL2
4665 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
4666 * if v7 without EL3, register doesn't exist
4667 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
4669 if (arm_feature(env, ARM_FEATURE_EL3)) {
4670 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4671 ARMCPRegInfo nsacr = {
4672 .name = "NSACR", .type = ARM_CP_CONST,
4673 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4674 .access = PL1_RW, .accessfn = nsacr_access,
4675 .resetvalue = 0xc00
4677 define_one_arm_cp_reg(cpu, &nsacr);
4678 } else {
4679 ARMCPRegInfo nsacr = {
4680 .name = "NSACR",
4681 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4682 .access = PL3_RW | PL1_R,
4683 .resetvalue = 0,
4684 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
4686 define_one_arm_cp_reg(cpu, &nsacr);
4688 } else {
4689 if (arm_feature(env, ARM_FEATURE_V8)) {
4690 ARMCPRegInfo nsacr = {
4691 .name = "NSACR", .type = ARM_CP_CONST,
4692 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4693 .access = PL1_R,
4694 .resetvalue = 0xc00
4696 define_one_arm_cp_reg(cpu, &nsacr);
4700 if (arm_feature(env, ARM_FEATURE_MPU)) {
4701 if (arm_feature(env, ARM_FEATURE_V6)) {
4702 /* PMSAv6 not implemented */
4703 assert(arm_feature(env, ARM_FEATURE_V7));
4704 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4705 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
4706 } else {
4707 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
4709 } else {
4710 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4711 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4713 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
4714 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
4716 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
4717 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
4719 if (arm_feature(env, ARM_FEATURE_VAPA)) {
4720 define_arm_cp_regs(cpu, vapa_cp_reginfo);
4722 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
4723 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
4725 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
4726 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
4728 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
4729 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
4731 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
4732 define_arm_cp_regs(cpu, omap_cp_reginfo);
4734 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
4735 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
4737 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4738 define_arm_cp_regs(cpu, xscale_cp_reginfo);
4740 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
4741 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
4743 if (arm_feature(env, ARM_FEATURE_LPAE)) {
4744 define_arm_cp_regs(cpu, lpae_cp_reginfo);
4746 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
4747 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
4748 * be read-only (ie write causes UNDEF exception).
4751 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
4752 /* Pre-v8 MIDR space.
4753 * Note that the MIDR isn't a simple constant register because
4754 * of the TI925 behaviour where writes to another register can
4755 * cause the MIDR value to change.
4757 * Unimplemented registers in the c15 0 0 0 space default to
4758 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
4759 * and friends override accordingly.
4761 { .name = "MIDR",
4762 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
4763 .access = PL1_R, .resetvalue = cpu->midr,
4764 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
4765 .readfn = midr_read,
4766 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4767 .type = ARM_CP_OVERRIDE },
4768 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
4769 { .name = "DUMMY",
4770 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
4771 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4772 { .name = "DUMMY",
4773 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
4774 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4775 { .name = "DUMMY",
4776 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
4777 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4778 { .name = "DUMMY",
4779 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
4780 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4781 { .name = "DUMMY",
4782 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
4783 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4784 REGINFO_SENTINEL
4786 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
4787 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
4788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
4789 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
4790 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4791 .readfn = midr_read },
4792 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
4793 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4794 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4795 .access = PL1_R, .resetvalue = cpu->midr },
4796 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4797 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
4798 .access = PL1_R, .resetvalue = cpu->midr },
4799 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
4800 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
4801 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
4802 REGINFO_SENTINEL
4804 ARMCPRegInfo id_cp_reginfo[] = {
4805 /* These are common to v8 and pre-v8 */
4806 { .name = "CTR",
4807 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
4808 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4809 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
4810 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
4811 .access = PL0_R, .accessfn = ctr_el0_access,
4812 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4813 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
4814 { .name = "TCMTR",
4815 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
4816 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4817 REGINFO_SENTINEL
4819 /* TLBTR is specific to VMSA */
4820 ARMCPRegInfo id_tlbtr_reginfo = {
4821 .name = "TLBTR",
4822 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
4823 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
4825 /* MPUIR is specific to PMSA V6+ */
4826 ARMCPRegInfo id_mpuir_reginfo = {
4827 .name = "MPUIR",
4828 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4829 .access = PL1_R, .type = ARM_CP_CONST,
4830 .resetvalue = cpu->pmsav7_dregion << 8
4832 ARMCPRegInfo crn0_wi_reginfo = {
4833 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
4834 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
4835 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
4837 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
4838 arm_feature(env, ARM_FEATURE_STRONGARM)) {
4839 ARMCPRegInfo *r;
4840 /* Register the blanket "writes ignored" value first to cover the
4841 * whole space. Then update the specific ID registers to allow write
4842 * access, so that they ignore writes rather than causing them to
4843 * UNDEF.
4845 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
4846 for (r = id_pre_v8_midr_cp_reginfo;
4847 r->type != ARM_CP_SENTINEL; r++) {
4848 r->access = PL1_RW;
4850 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
4851 r->access = PL1_RW;
4853 id_tlbtr_reginfo.access = PL1_RW;
4854 id_tlbtr_reginfo.access = PL1_RW;
4856 if (arm_feature(env, ARM_FEATURE_V8)) {
4857 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
4858 } else {
4859 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
4861 define_arm_cp_regs(cpu, id_cp_reginfo);
4862 if (!arm_feature(env, ARM_FEATURE_MPU)) {
4863 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
4864 } else if (arm_feature(env, ARM_FEATURE_V7)) {
4865 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
4869 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
4870 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
4873 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
4874 ARMCPRegInfo auxcr_reginfo[] = {
4875 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
4876 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
4877 .access = PL1_RW, .type = ARM_CP_CONST,
4878 .resetvalue = cpu->reset_auxcr },
4879 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
4880 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
4881 .access = PL2_RW, .type = ARM_CP_CONST,
4882 .resetvalue = 0 },
4883 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
4884 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
4885 .access = PL3_RW, .type = ARM_CP_CONST,
4886 .resetvalue = 0 },
4887 REGINFO_SENTINEL
4889 define_arm_cp_regs(cpu, auxcr_reginfo);
4892 if (arm_feature(env, ARM_FEATURE_CBAR)) {
4893 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4894 /* 32 bit view is [31:18] 0...0 [43:32]. */
4895 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
4896 | extract64(cpu->reset_cbar, 32, 12);
4897 ARMCPRegInfo cbar_reginfo[] = {
4898 { .name = "CBAR",
4899 .type = ARM_CP_CONST,
4900 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4901 .access = PL1_R, .resetvalue = cpu->reset_cbar },
4902 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
4903 .type = ARM_CP_CONST,
4904 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
4905 .access = PL1_R, .resetvalue = cbar32 },
4906 REGINFO_SENTINEL
4908 /* We don't implement a r/w 64 bit CBAR currently */
4909 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
4910 define_arm_cp_regs(cpu, cbar_reginfo);
4911 } else {
4912 ARMCPRegInfo cbar = {
4913 .name = "CBAR",
4914 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4915 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
4916 .fieldoffset = offsetof(CPUARMState,
4917 cp15.c15_config_base_address)
4919 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
4920 cbar.access = PL1_R;
4921 cbar.fieldoffset = 0;
4922 cbar.type = ARM_CP_CONST;
4924 define_one_arm_cp_reg(cpu, &cbar);
4928 /* Generic registers whose values depend on the implementation */
4930 ARMCPRegInfo sctlr = {
4931 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
4932 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4933 .access = PL1_RW,
4934 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
4935 offsetof(CPUARMState, cp15.sctlr_ns) },
4936 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
4937 .raw_writefn = raw_write,
4939 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4940 /* Normally we would always end the TB on an SCTLR write, but Linux
4941 * arch/arm/mach-pxa/sleep.S expects two instructions following
4942 * an MMU enable to execute from cache. Imitate this behaviour.
4944 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
4946 define_one_arm_cp_reg(cpu, &sctlr);
4950 ARMCPU *cpu_arm_init(const char *cpu_model)
4952 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
4955 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
4957 CPUState *cs = CPU(cpu);
4958 CPUARMState *env = &cpu->env;
4960 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4961 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
4962 aarch64_fpu_gdb_set_reg,
4963 34, "aarch64-fpu.xml", 0);
4964 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
4965 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4966 51, "arm-neon.xml", 0);
4967 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
4968 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4969 35, "arm-vfp3.xml", 0);
4970 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
4971 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4972 19, "arm-vfp.xml", 0);
4976 /* Sort alphabetically by type name, except for "any". */
4977 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
4979 ObjectClass *class_a = (ObjectClass *)a;
4980 ObjectClass *class_b = (ObjectClass *)b;
4981 const char *name_a, *name_b;
4983 name_a = object_class_get_name(class_a);
4984 name_b = object_class_get_name(class_b);
4985 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
4986 return 1;
4987 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
4988 return -1;
4989 } else {
4990 return strcmp(name_a, name_b);
4994 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
4996 ObjectClass *oc = data;
4997 CPUListState *s = user_data;
4998 const char *typename;
4999 char *name;
5001 typename = object_class_get_name(oc);
5002 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
5003 (*s->cpu_fprintf)(s->file, " %s\n",
5004 name);
5005 g_free(name);
5008 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5010 CPUListState s = {
5011 .file = f,
5012 .cpu_fprintf = cpu_fprintf,
5014 GSList *list;
5016 list = object_class_get_list(TYPE_ARM_CPU, false);
5017 list = g_slist_sort(list, arm_cpu_list_compare);
5018 (*cpu_fprintf)(f, "Available CPUs:\n");
5019 g_slist_foreach(list, arm_cpu_list_entry, &s);
5020 g_slist_free(list);
5021 #ifdef CONFIG_KVM
5022 /* The 'host' CPU type is dynamically registered only if KVM is
5023 * enabled, so we have to special-case it here:
5025 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5026 #endif
5029 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5031 ObjectClass *oc = data;
5032 CpuDefinitionInfoList **cpu_list = user_data;
5033 CpuDefinitionInfoList *entry;
5034 CpuDefinitionInfo *info;
5035 const char *typename;
5037 typename = object_class_get_name(oc);
5038 info = g_malloc0(sizeof(*info));
5039 info->name = g_strndup(typename,
5040 strlen(typename) - strlen("-" TYPE_ARM_CPU));
5042 entry = g_malloc0(sizeof(*entry));
5043 entry->value = info;
5044 entry->next = *cpu_list;
5045 *cpu_list = entry;
5048 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5050 CpuDefinitionInfoList *cpu_list = NULL;
5051 GSList *list;
5053 list = object_class_get_list(TYPE_ARM_CPU, false);
5054 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5055 g_slist_free(list);
5057 return cpu_list;
5060 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
5061 void *opaque, int state, int secstate,
5062 int crm, int opc1, int opc2)
5064 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5065 * add a single reginfo struct to the hash table.
5067 uint32_t *key = g_new(uint32_t, 1);
5068 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5069 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
5070 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5072 /* Reset the secure state to the specific incoming state. This is
5073 * necessary as the register may have been defined with both states.
5075 r2->secure = secstate;
5077 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5078 /* Register is banked (using both entries in array).
5079 * Overwriting fieldoffset as the array is only used to define
5080 * banked registers but later only fieldoffset is used.
5082 r2->fieldoffset = r->bank_fieldoffsets[ns];
5085 if (state == ARM_CP_STATE_AA32) {
5086 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5087 /* If the register is banked then we don't need to migrate or
5088 * reset the 32-bit instance in certain cases:
5090 * 1) If the register has both 32-bit and 64-bit instances then we
5091 * can count on the 64-bit instance taking care of the
5092 * non-secure bank.
5093 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5094 * taking care of the secure bank. This requires that separate
5095 * 32 and 64-bit definitions are provided.
5097 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5098 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
5099 r2->type |= ARM_CP_ALIAS;
5101 } else if ((secstate != r->secure) && !ns) {
5102 /* The register is not banked so we only want to allow migration of
5103 * the non-secure instance.
5105 r2->type |= ARM_CP_ALIAS;
5108 if (r->state == ARM_CP_STATE_BOTH) {
5109 /* We assume it is a cp15 register if the .cp field is left unset.
5111 if (r2->cp == 0) {
5112 r2->cp = 15;
5115 #ifdef HOST_WORDS_BIGENDIAN
5116 if (r2->fieldoffset) {
5117 r2->fieldoffset += sizeof(uint32_t);
5119 #endif
5122 if (state == ARM_CP_STATE_AA64) {
5123 /* To allow abbreviation of ARMCPRegInfo
5124 * definitions, we treat cp == 0 as equivalent to
5125 * the value for "standard guest-visible sysreg".
5126 * STATE_BOTH definitions are also always "standard
5127 * sysreg" in their AArch64 view (the .cp value may
5128 * be non-zero for the benefit of the AArch32 view).
5130 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
5131 r2->cp = CP_REG_ARM64_SYSREG_CP;
5133 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5134 r2->opc0, opc1, opc2);
5135 } else {
5136 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
5138 if (opaque) {
5139 r2->opaque = opaque;
5141 /* reginfo passed to helpers is correct for the actual access,
5142 * and is never ARM_CP_STATE_BOTH:
5144 r2->state = state;
5145 /* Make sure reginfo passed to helpers for wildcarded regs
5146 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5148 r2->crm = crm;
5149 r2->opc1 = opc1;
5150 r2->opc2 = opc2;
5151 /* By convention, for wildcarded registers only the first
5152 * entry is used for migration; the others are marked as
5153 * ALIAS so we don't try to transfer the register
5154 * multiple times. Special registers (ie NOP/WFI) are
5155 * never migratable and not even raw-accessible.
5157 if ((r->type & ARM_CP_SPECIAL)) {
5158 r2->type |= ARM_CP_NO_RAW;
5160 if (((r->crm == CP_ANY) && crm != 0) ||
5161 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5162 ((r->opc2 == CP_ANY) && opc2 != 0)) {
5163 r2->type |= ARM_CP_ALIAS;
5166 /* Check that raw accesses are either forbidden or handled. Note that
5167 * we can't assert this earlier because the setup of fieldoffset for
5168 * banked registers has to be done first.
5170 if (!(r2->type & ARM_CP_NO_RAW)) {
5171 assert(!raw_accessors_invalid(r2));
5174 /* Overriding of an existing definition must be explicitly
5175 * requested.
5177 if (!(r->type & ARM_CP_OVERRIDE)) {
5178 ARMCPRegInfo *oldreg;
5179 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5180 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5181 fprintf(stderr, "Register redefined: cp=%d %d bit "
5182 "crn=%d crm=%d opc1=%d opc2=%d, "
5183 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5184 r2->crn, r2->crm, r2->opc1, r2->opc2,
5185 oldreg->name, r2->name);
5186 g_assert_not_reached();
5189 g_hash_table_insert(cpu->cp_regs, key, r2);
5193 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5194 const ARMCPRegInfo *r, void *opaque)
5196 /* Define implementations of coprocessor registers.
5197 * We store these in a hashtable because typically
5198 * there are less than 150 registers in a space which
5199 * is 16*16*16*8*8 = 262144 in size.
5200 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5201 * If a register is defined twice then the second definition is
5202 * used, so this can be used to define some generic registers and
5203 * then override them with implementation specific variations.
5204 * At least one of the original and the second definition should
5205 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5206 * against accidental use.
5208 * The state field defines whether the register is to be
5209 * visible in the AArch32 or AArch64 execution state. If the
5210 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5211 * reginfo structure for the AArch32 view, which sees the lower
5212 * 32 bits of the 64 bit register.
5214 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5215 * be wildcarded. AArch64 registers are always considered to be 64
5216 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5217 * the register, if any.
5219 int crm, opc1, opc2, state;
5220 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5221 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5222 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5223 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5224 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5225 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5226 /* 64 bit registers have only CRm and Opc1 fields */
5227 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
5228 /* op0 only exists in the AArch64 encodings */
5229 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5230 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5231 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5232 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5233 * encodes a minimum access level for the register. We roll this
5234 * runtime check into our general permission check code, so check
5235 * here that the reginfo's specified permissions are strict enough
5236 * to encompass the generic architectural permission check.
5238 if (r->state != ARM_CP_STATE_AA32) {
5239 int mask = 0;
5240 switch (r->opc1) {
5241 case 0: case 1: case 2:
5242 /* min_EL EL1 */
5243 mask = PL1_RW;
5244 break;
5245 case 3:
5246 /* min_EL EL0 */
5247 mask = PL0_RW;
5248 break;
5249 case 4:
5250 /* min_EL EL2 */
5251 mask = PL2_RW;
5252 break;
5253 case 5:
5254 /* unallocated encoding, so not possible */
5255 assert(false);
5256 break;
5257 case 6:
5258 /* min_EL EL3 */
5259 mask = PL3_RW;
5260 break;
5261 case 7:
5262 /* min_EL EL1, secure mode only (we don't check the latter) */
5263 mask = PL1_RW;
5264 break;
5265 default:
5266 /* broken reginfo with out-of-range opc1 */
5267 assert(false);
5268 break;
5270 /* assert our permissions are not too lax (stricter is fine) */
5271 assert((r->access & ~mask) == 0);
5274 /* Check that the register definition has enough info to handle
5275 * reads and writes if they are permitted.
5277 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5278 if (r->access & PL3_R) {
5279 assert((r->fieldoffset ||
5280 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5281 r->readfn);
5283 if (r->access & PL3_W) {
5284 assert((r->fieldoffset ||
5285 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5286 r->writefn);
5289 /* Bad type field probably means missing sentinel at end of reg list */
5290 assert(cptype_valid(r->type));
5291 for (crm = crmmin; crm <= crmmax; crm++) {
5292 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5293 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
5294 for (state = ARM_CP_STATE_AA32;
5295 state <= ARM_CP_STATE_AA64; state++) {
5296 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5297 continue;
5299 if (state == ARM_CP_STATE_AA32) {
5300 /* Under AArch32 CP registers can be common
5301 * (same for secure and non-secure world) or banked.
5303 switch (r->secure) {
5304 case ARM_CP_SECSTATE_S:
5305 case ARM_CP_SECSTATE_NS:
5306 add_cpreg_to_hashtable(cpu, r, opaque, state,
5307 r->secure, crm, opc1, opc2);
5308 break;
5309 default:
5310 add_cpreg_to_hashtable(cpu, r, opaque, state,
5311 ARM_CP_SECSTATE_S,
5312 crm, opc1, opc2);
5313 add_cpreg_to_hashtable(cpu, r, opaque, state,
5314 ARM_CP_SECSTATE_NS,
5315 crm, opc1, opc2);
5316 break;
5318 } else {
5319 /* AArch64 registers get mapped to non-secure instance
5320 * of AArch32 */
5321 add_cpreg_to_hashtable(cpu, r, opaque, state,
5322 ARM_CP_SECSTATE_NS,
5323 crm, opc1, opc2);
5331 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5332 const ARMCPRegInfo *regs, void *opaque)
5334 /* Define a whole list of registers */
5335 const ARMCPRegInfo *r;
5336 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5337 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5341 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
5343 return g_hash_table_lookup(cpregs, &encoded_cp);
5346 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5347 uint64_t value)
5349 /* Helper coprocessor write function for write-ignore registers */
5352 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
5354 /* Helper coprocessor write function for read-as-zero registers */
5355 return 0;
5358 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5360 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5363 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
5365 /* Return true if it is not valid for us to switch to
5366 * this CPU mode (ie all the UNPREDICTABLE cases in
5367 * the ARM ARM CPSRWriteByInstr pseudocode).
5370 /* Changes to or from Hyp via MSR and CPS are illegal. */
5371 if (write_type == CPSRWriteByInstr &&
5372 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5373 mode == ARM_CPU_MODE_HYP)) {
5374 return 1;
5377 switch (mode) {
5378 case ARM_CPU_MODE_USR:
5379 return 0;
5380 case ARM_CPU_MODE_SYS:
5381 case ARM_CPU_MODE_SVC:
5382 case ARM_CPU_MODE_ABT:
5383 case ARM_CPU_MODE_UND:
5384 case ARM_CPU_MODE_IRQ:
5385 case ARM_CPU_MODE_FIQ:
5386 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5387 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5389 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5390 * and CPS are treated as illegal mode changes.
5392 if (write_type == CPSRWriteByInstr &&
5393 (env->cp15.hcr_el2 & HCR_TGE) &&
5394 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5395 !arm_is_secure_below_el3(env)) {
5396 return 1;
5398 return 0;
5399 case ARM_CPU_MODE_HYP:
5400 return !arm_feature(env, ARM_FEATURE_EL2)
5401 || arm_current_el(env) < 2 || arm_is_secure(env);
5402 case ARM_CPU_MODE_MON:
5403 return arm_current_el(env) < 3;
5404 default:
5405 return 1;
5409 uint32_t cpsr_read(CPUARMState *env)
5411 int ZF;
5412 ZF = (env->ZF == 0);
5413 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
5414 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5415 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5416 | ((env->condexec_bits & 0xfc) << 8)
5417 | (env->GE << 16) | (env->daif & CPSR_AIF);
5420 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5421 CPSRWriteType write_type)
5423 uint32_t changed_daif;
5425 if (mask & CPSR_NZCV) {
5426 env->ZF = (~val) & CPSR_Z;
5427 env->NF = val;
5428 env->CF = (val >> 29) & 1;
5429 env->VF = (val << 3) & 0x80000000;
5431 if (mask & CPSR_Q)
5432 env->QF = ((val & CPSR_Q) != 0);
5433 if (mask & CPSR_T)
5434 env->thumb = ((val & CPSR_T) != 0);
5435 if (mask & CPSR_IT_0_1) {
5436 env->condexec_bits &= ~3;
5437 env->condexec_bits |= (val >> 25) & 3;
5439 if (mask & CPSR_IT_2_7) {
5440 env->condexec_bits &= 3;
5441 env->condexec_bits |= (val >> 8) & 0xfc;
5443 if (mask & CPSR_GE) {
5444 env->GE = (val >> 16) & 0xf;
5447 /* In a V7 implementation that includes the security extensions but does
5448 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5449 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5450 * bits respectively.
5452 * In a V8 implementation, it is permitted for privileged software to
5453 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5455 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
5456 arm_feature(env, ARM_FEATURE_EL3) &&
5457 !arm_feature(env, ARM_FEATURE_EL2) &&
5458 !arm_is_secure(env)) {
5460 changed_daif = (env->daif ^ val) & mask;
5462 if (changed_daif & CPSR_A) {
5463 /* Check to see if we are allowed to change the masking of async
5464 * abort exceptions from a non-secure state.
5466 if (!(env->cp15.scr_el3 & SCR_AW)) {
5467 qemu_log_mask(LOG_GUEST_ERROR,
5468 "Ignoring attempt to switch CPSR_A flag from "
5469 "non-secure world with SCR.AW bit clear\n");
5470 mask &= ~CPSR_A;
5474 if (changed_daif & CPSR_F) {
5475 /* Check to see if we are allowed to change the masking of FIQ
5476 * exceptions from a non-secure state.
5478 if (!(env->cp15.scr_el3 & SCR_FW)) {
5479 qemu_log_mask(LOG_GUEST_ERROR,
5480 "Ignoring attempt to switch CPSR_F flag from "
5481 "non-secure world with SCR.FW bit clear\n");
5482 mask &= ~CPSR_F;
5485 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5486 * If this bit is set software is not allowed to mask
5487 * FIQs, but is allowed to set CPSR_F to 0.
5489 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5490 (val & CPSR_F)) {
5491 qemu_log_mask(LOG_GUEST_ERROR,
5492 "Ignoring attempt to enable CPSR_F flag "
5493 "(non-maskable FIQ [NMFI] support enabled)\n");
5494 mask &= ~CPSR_F;
5499 env->daif &= ~(CPSR_AIF & mask);
5500 env->daif |= val & CPSR_AIF & mask;
5502 if (write_type != CPSRWriteRaw &&
5503 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
5504 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
5505 /* Note that we can only get here in USR mode if this is a
5506 * gdb stub write; for this case we follow the architectural
5507 * behaviour for guest writes in USR mode of ignoring an attempt
5508 * to switch mode. (Those are caught by translate.c for writes
5509 * triggered by guest instructions.)
5511 mask &= ~CPSR_M;
5512 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
5513 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
5514 * v7, and has defined behaviour in v8:
5515 * + leave CPSR.M untouched
5516 * + allow changes to the other CPSR fields
5517 * + set PSTATE.IL
5518 * For user changes via the GDB stub, we don't set PSTATE.IL,
5519 * as this would be unnecessarily harsh for a user error.
5521 mask &= ~CPSR_M;
5522 if (write_type != CPSRWriteByGDBStub &&
5523 arm_feature(env, ARM_FEATURE_V8)) {
5524 mask |= CPSR_IL;
5525 val |= CPSR_IL;
5527 } else {
5528 switch_mode(env, val & CPSR_M);
5531 mask &= ~CACHED_CPSR_BITS;
5532 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
5535 /* Sign/zero extend */
5536 uint32_t HELPER(sxtb16)(uint32_t x)
5538 uint32_t res;
5539 res = (uint16_t)(int8_t)x;
5540 res |= (uint32_t)(int8_t)(x >> 16) << 16;
5541 return res;
5544 uint32_t HELPER(uxtb16)(uint32_t x)
5546 uint32_t res;
5547 res = (uint16_t)(uint8_t)x;
5548 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
5549 return res;
5552 uint32_t HELPER(clz)(uint32_t x)
5554 return clz32(x);
5557 int32_t HELPER(sdiv)(int32_t num, int32_t den)
5559 if (den == 0)
5560 return 0;
5561 if (num == INT_MIN && den == -1)
5562 return INT_MIN;
5563 return num / den;
5566 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
5568 if (den == 0)
5569 return 0;
5570 return num / den;
5573 uint32_t HELPER(rbit)(uint32_t x)
5575 return revbit32(x);
5578 #if defined(CONFIG_USER_ONLY)
5580 /* These should probably raise undefined insn exceptions. */
5581 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
5583 ARMCPU *cpu = arm_env_get_cpu(env);
5585 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
5588 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
5590 ARMCPU *cpu = arm_env_get_cpu(env);
5592 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
5593 return 0;
5596 void switch_mode(CPUARMState *env, int mode)
5598 ARMCPU *cpu = arm_env_get_cpu(env);
5600 if (mode != ARM_CPU_MODE_USR) {
5601 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5605 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5606 uint32_t cur_el, bool secure)
5608 return 1;
5611 void aarch64_sync_64_to_32(CPUARMState *env)
5613 g_assert_not_reached();
5616 #else
5618 void switch_mode(CPUARMState *env, int mode)
5620 int old_mode;
5621 int i;
5623 old_mode = env->uncached_cpsr & CPSR_M;
5624 if (mode == old_mode)
5625 return;
5627 if (old_mode == ARM_CPU_MODE_FIQ) {
5628 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
5629 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
5630 } else if (mode == ARM_CPU_MODE_FIQ) {
5631 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
5632 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
5635 i = bank_number(old_mode);
5636 env->banked_r13[i] = env->regs[13];
5637 env->banked_r14[i] = env->regs[14];
5638 env->banked_spsr[i] = env->spsr;
5640 i = bank_number(mode);
5641 env->regs[13] = env->banked_r13[i];
5642 env->regs[14] = env->banked_r14[i];
5643 env->spsr = env->banked_spsr[i];
5646 /* Physical Interrupt Target EL Lookup Table
5648 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
5650 * The below multi-dimensional table is used for looking up the target
5651 * exception level given numerous condition criteria. Specifically, the
5652 * target EL is based on SCR and HCR routing controls as well as the
5653 * currently executing EL and secure state.
5655 * Dimensions:
5656 * target_el_table[2][2][2][2][2][4]
5657 * | | | | | +--- Current EL
5658 * | | | | +------ Non-secure(0)/Secure(1)
5659 * | | | +--------- HCR mask override
5660 * | | +------------ SCR exec state control
5661 * | +--------------- SCR mask override
5662 * +------------------ 32-bit(0)/64-bit(1) EL3
5664 * The table values are as such:
5665 * 0-3 = EL0-EL3
5666 * -1 = Cannot occur
5668 * The ARM ARM target EL table includes entries indicating that an "exception
5669 * is not taken". The two cases where this is applicable are:
5670 * 1) An exception is taken from EL3 but the SCR does not have the exception
5671 * routed to EL3.
5672 * 2) An exception is taken from EL2 but the HCR does not have the exception
5673 * routed to EL2.
5674 * In these two cases, the below table contain a target of EL1. This value is
5675 * returned as it is expected that the consumer of the table data will check
5676 * for "target EL >= current EL" to ensure the exception is not taken.
5678 * SCR HCR
5679 * 64 EA AMO From
5680 * BIT IRQ IMO Non-secure Secure
5681 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
5683 static const int8_t target_el_table[2][2][2][2][2][4] = {
5684 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5685 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
5686 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5687 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
5688 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5689 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
5690 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5691 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
5692 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
5693 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
5694 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
5695 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
5696 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5697 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
5698 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5699 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
5703 * Determine the target EL for physical exceptions
5705 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5706 uint32_t cur_el, bool secure)
5708 CPUARMState *env = cs->env_ptr;
5709 int rw;
5710 int scr;
5711 int hcr;
5712 int target_el;
5713 /* Is the highest EL AArch64? */
5714 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
5716 if (arm_feature(env, ARM_FEATURE_EL3)) {
5717 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
5718 } else {
5719 /* Either EL2 is the highest EL (and so the EL2 register width
5720 * is given by is64); or there is no EL2 or EL3, in which case
5721 * the value of 'rw' does not affect the table lookup anyway.
5723 rw = is64;
5726 switch (excp_idx) {
5727 case EXCP_IRQ:
5728 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
5729 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
5730 break;
5731 case EXCP_FIQ:
5732 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
5733 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
5734 break;
5735 default:
5736 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
5737 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
5738 break;
5741 /* If HCR.TGE is set then HCR is treated as being 1 */
5742 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
5744 /* Perform a table-lookup for the target EL given the current state */
5745 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
5747 assert(target_el > 0);
5749 return target_el;
5752 static void v7m_push(CPUARMState *env, uint32_t val)
5754 CPUState *cs = CPU(arm_env_get_cpu(env));
5756 env->regs[13] -= 4;
5757 stl_phys(cs->as, env->regs[13], val);
5760 static uint32_t v7m_pop(CPUARMState *env)
5762 CPUState *cs = CPU(arm_env_get_cpu(env));
5763 uint32_t val;
5765 val = ldl_phys(cs->as, env->regs[13]);
5766 env->regs[13] += 4;
5767 return val;
5770 /* Switch to V7M main or process stack pointer. */
5771 static void switch_v7m_sp(CPUARMState *env, int process)
5773 uint32_t tmp;
5774 if (env->v7m.current_sp != process) {
5775 tmp = env->v7m.other_sp;
5776 env->v7m.other_sp = env->regs[13];
5777 env->regs[13] = tmp;
5778 env->v7m.current_sp = process;
5782 static void do_v7m_exception_exit(CPUARMState *env)
5784 uint32_t type;
5785 uint32_t xpsr;
5787 type = env->regs[15];
5788 if (env->v7m.exception != 0)
5789 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
5791 /* Switch to the target stack. */
5792 switch_v7m_sp(env, (type & 4) != 0);
5793 /* Pop registers. */
5794 env->regs[0] = v7m_pop(env);
5795 env->regs[1] = v7m_pop(env);
5796 env->regs[2] = v7m_pop(env);
5797 env->regs[3] = v7m_pop(env);
5798 env->regs[12] = v7m_pop(env);
5799 env->regs[14] = v7m_pop(env);
5800 env->regs[15] = v7m_pop(env);
5801 if (env->regs[15] & 1) {
5802 qemu_log_mask(LOG_GUEST_ERROR,
5803 "M profile return from interrupt with misaligned "
5804 "PC is UNPREDICTABLE\n");
5805 /* Actual hardware seems to ignore the lsbit, and there are several
5806 * RTOSes out there which incorrectly assume the r15 in the stack
5807 * frame should be a Thumb-style "lsbit indicates ARM/Thumb" value.
5809 env->regs[15] &= ~1U;
5811 xpsr = v7m_pop(env);
5812 xpsr_write(env, xpsr, 0xfffffdff);
5813 /* Undo stack alignment. */
5814 if (xpsr & 0x200)
5815 env->regs[13] |= 4;
5816 /* ??? The exception return type specifies Thread/Handler mode. However
5817 this is also implied by the xPSR value. Not sure what to do
5818 if there is a mismatch. */
5819 /* ??? Likewise for mismatches between the CONTROL register and the stack
5820 pointer. */
5823 static void arm_log_exception(int idx)
5825 if (qemu_loglevel_mask(CPU_LOG_INT)) {
5826 const char *exc = NULL;
5828 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
5829 exc = excnames[idx];
5831 if (!exc) {
5832 exc = "unknown";
5834 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
5838 void arm_v7m_cpu_do_interrupt(CPUState *cs)
5840 ARMCPU *cpu = ARM_CPU(cs);
5841 CPUARMState *env = &cpu->env;
5842 uint32_t xpsr = xpsr_read(env);
5843 uint32_t lr;
5844 uint32_t addr;
5846 arm_log_exception(cs->exception_index);
5848 lr = 0xfffffff1;
5849 if (env->v7m.current_sp)
5850 lr |= 4;
5851 if (env->v7m.exception == 0)
5852 lr |= 8;
5854 /* For exceptions we just mark as pending on the NVIC, and let that
5855 handle it. */
5856 /* TODO: Need to escalate if the current priority is higher than the
5857 one we're raising. */
5858 switch (cs->exception_index) {
5859 case EXCP_UDEF:
5860 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
5861 return;
5862 case EXCP_SWI:
5863 /* The PC already points to the next instruction. */
5864 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
5865 return;
5866 case EXCP_PREFETCH_ABORT:
5867 case EXCP_DATA_ABORT:
5868 /* TODO: if we implemented the MPU registers, this is where we
5869 * should set the MMFAR, etc from exception.fsr and exception.vaddress.
5871 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
5872 return;
5873 case EXCP_BKPT:
5874 if (semihosting_enabled()) {
5875 int nr;
5876 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
5877 if (nr == 0xab) {
5878 env->regs[15] += 2;
5879 qemu_log_mask(CPU_LOG_INT,
5880 "...handling as semihosting call 0x%x\n",
5881 env->regs[0]);
5882 env->regs[0] = do_arm_semihosting(env);
5883 return;
5886 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
5887 return;
5888 case EXCP_IRQ:
5889 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
5890 break;
5891 case EXCP_EXCEPTION_EXIT:
5892 do_v7m_exception_exit(env);
5893 return;
5894 default:
5895 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
5896 return; /* Never happens. Keep compiler happy. */
5899 /* Align stack pointer. */
5900 /* ??? Should only do this if Configuration Control Register
5901 STACKALIGN bit is set. */
5902 if (env->regs[13] & 4) {
5903 env->regs[13] -= 4;
5904 xpsr |= 0x200;
5906 /* Switch to the handler mode. */
5907 v7m_push(env, xpsr);
5908 v7m_push(env, env->regs[15]);
5909 v7m_push(env, env->regs[14]);
5910 v7m_push(env, env->regs[12]);
5911 v7m_push(env, env->regs[3]);
5912 v7m_push(env, env->regs[2]);
5913 v7m_push(env, env->regs[1]);
5914 v7m_push(env, env->regs[0]);
5915 switch_v7m_sp(env, 0);
5916 /* Clear IT bits */
5917 env->condexec_bits = 0;
5918 env->regs[14] = lr;
5919 addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
5920 env->regs[15] = addr & 0xfffffffe;
5921 env->thumb = addr & 1;
5924 /* Function used to synchronize QEMU's AArch64 register set with AArch32
5925 * register set. This is necessary when switching between AArch32 and AArch64
5926 * execution state.
5928 void aarch64_sync_32_to_64(CPUARMState *env)
5930 int i;
5931 uint32_t mode = env->uncached_cpsr & CPSR_M;
5933 /* We can blanket copy R[0:7] to X[0:7] */
5934 for (i = 0; i < 8; i++) {
5935 env->xregs[i] = env->regs[i];
5938 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
5939 * Otherwise, they come from the banked user regs.
5941 if (mode == ARM_CPU_MODE_FIQ) {
5942 for (i = 8; i < 13; i++) {
5943 env->xregs[i] = env->usr_regs[i - 8];
5945 } else {
5946 for (i = 8; i < 13; i++) {
5947 env->xregs[i] = env->regs[i];
5951 /* Registers x13-x23 are the various mode SP and FP registers. Registers
5952 * r13 and r14 are only copied if we are in that mode, otherwise we copy
5953 * from the mode banked register.
5955 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5956 env->xregs[13] = env->regs[13];
5957 env->xregs[14] = env->regs[14];
5958 } else {
5959 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
5960 /* HYP is an exception in that it is copied from r14 */
5961 if (mode == ARM_CPU_MODE_HYP) {
5962 env->xregs[14] = env->regs[14];
5963 } else {
5964 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
5968 if (mode == ARM_CPU_MODE_HYP) {
5969 env->xregs[15] = env->regs[13];
5970 } else {
5971 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
5974 if (mode == ARM_CPU_MODE_IRQ) {
5975 env->xregs[16] = env->regs[14];
5976 env->xregs[17] = env->regs[13];
5977 } else {
5978 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
5979 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
5982 if (mode == ARM_CPU_MODE_SVC) {
5983 env->xregs[18] = env->regs[14];
5984 env->xregs[19] = env->regs[13];
5985 } else {
5986 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
5987 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
5990 if (mode == ARM_CPU_MODE_ABT) {
5991 env->xregs[20] = env->regs[14];
5992 env->xregs[21] = env->regs[13];
5993 } else {
5994 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
5995 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
5998 if (mode == ARM_CPU_MODE_UND) {
5999 env->xregs[22] = env->regs[14];
6000 env->xregs[23] = env->regs[13];
6001 } else {
6002 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
6003 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
6006 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
6007 * mode, then we can copy from r8-r14. Otherwise, we copy from the
6008 * FIQ bank for r8-r14.
6010 if (mode == ARM_CPU_MODE_FIQ) {
6011 for (i = 24; i < 31; i++) {
6012 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
6014 } else {
6015 for (i = 24; i < 29; i++) {
6016 env->xregs[i] = env->fiq_regs[i - 24];
6018 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
6019 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
6022 env->pc = env->regs[15];
6025 /* Function used to synchronize QEMU's AArch32 register set with AArch64
6026 * register set. This is necessary when switching between AArch32 and AArch64
6027 * execution state.
6029 void aarch64_sync_64_to_32(CPUARMState *env)
6031 int i;
6032 uint32_t mode = env->uncached_cpsr & CPSR_M;
6034 /* We can blanket copy X[0:7] to R[0:7] */
6035 for (i = 0; i < 8; i++) {
6036 env->regs[i] = env->xregs[i];
6039 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
6040 * Otherwise, we copy x8-x12 into the banked user regs.
6042 if (mode == ARM_CPU_MODE_FIQ) {
6043 for (i = 8; i < 13; i++) {
6044 env->usr_regs[i - 8] = env->xregs[i];
6046 } else {
6047 for (i = 8; i < 13; i++) {
6048 env->regs[i] = env->xregs[i];
6052 /* Registers r13 & r14 depend on the current mode.
6053 * If we are in a given mode, we copy the corresponding x registers to r13
6054 * and r14. Otherwise, we copy the x register to the banked r13 and r14
6055 * for the mode.
6057 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
6058 env->regs[13] = env->xregs[13];
6059 env->regs[14] = env->xregs[14];
6060 } else {
6061 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
6063 /* HYP is an exception in that it does not have its own banked r14 but
6064 * shares the USR r14
6066 if (mode == ARM_CPU_MODE_HYP) {
6067 env->regs[14] = env->xregs[14];
6068 } else {
6069 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
6073 if (mode == ARM_CPU_MODE_HYP) {
6074 env->regs[13] = env->xregs[15];
6075 } else {
6076 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
6079 if (mode == ARM_CPU_MODE_IRQ) {
6080 env->regs[14] = env->xregs[16];
6081 env->regs[13] = env->xregs[17];
6082 } else {
6083 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
6084 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
6087 if (mode == ARM_CPU_MODE_SVC) {
6088 env->regs[14] = env->xregs[18];
6089 env->regs[13] = env->xregs[19];
6090 } else {
6091 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
6092 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
6095 if (mode == ARM_CPU_MODE_ABT) {
6096 env->regs[14] = env->xregs[20];
6097 env->regs[13] = env->xregs[21];
6098 } else {
6099 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
6100 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
6103 if (mode == ARM_CPU_MODE_UND) {
6104 env->regs[14] = env->xregs[22];
6105 env->regs[13] = env->xregs[23];
6106 } else {
6107 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
6108 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
6111 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
6112 * mode, then we can copy to r8-r14. Otherwise, we copy to the
6113 * FIQ bank for r8-r14.
6115 if (mode == ARM_CPU_MODE_FIQ) {
6116 for (i = 24; i < 31; i++) {
6117 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
6119 } else {
6120 for (i = 24; i < 29; i++) {
6121 env->fiq_regs[i - 24] = env->xregs[i];
6123 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
6124 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
6127 env->regs[15] = env->pc;
6130 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
6132 ARMCPU *cpu = ARM_CPU(cs);
6133 CPUARMState *env = &cpu->env;
6134 uint32_t addr;
6135 uint32_t mask;
6136 int new_mode;
6137 uint32_t offset;
6138 uint32_t moe;
6140 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
6141 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
6142 case EC_BREAKPOINT:
6143 case EC_BREAKPOINT_SAME_EL:
6144 moe = 1;
6145 break;
6146 case EC_WATCHPOINT:
6147 case EC_WATCHPOINT_SAME_EL:
6148 moe = 10;
6149 break;
6150 case EC_AA32_BKPT:
6151 moe = 3;
6152 break;
6153 case EC_VECTORCATCH:
6154 moe = 5;
6155 break;
6156 default:
6157 moe = 0;
6158 break;
6161 if (moe) {
6162 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
6165 /* TODO: Vectored interrupt controller. */
6166 switch (cs->exception_index) {
6167 case EXCP_UDEF:
6168 new_mode = ARM_CPU_MODE_UND;
6169 addr = 0x04;
6170 mask = CPSR_I;
6171 if (env->thumb)
6172 offset = 2;
6173 else
6174 offset = 4;
6175 break;
6176 case EXCP_SWI:
6177 new_mode = ARM_CPU_MODE_SVC;
6178 addr = 0x08;
6179 mask = CPSR_I;
6180 /* The PC already points to the next instruction. */
6181 offset = 0;
6182 break;
6183 case EXCP_BKPT:
6184 env->exception.fsr = 2;
6185 /* Fall through to prefetch abort. */
6186 case EXCP_PREFETCH_ABORT:
6187 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
6188 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
6189 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
6190 env->exception.fsr, (uint32_t)env->exception.vaddress);
6191 new_mode = ARM_CPU_MODE_ABT;
6192 addr = 0x0c;
6193 mask = CPSR_A | CPSR_I;
6194 offset = 4;
6195 break;
6196 case EXCP_DATA_ABORT:
6197 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
6198 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
6199 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
6200 env->exception.fsr,
6201 (uint32_t)env->exception.vaddress);
6202 new_mode = ARM_CPU_MODE_ABT;
6203 addr = 0x10;
6204 mask = CPSR_A | CPSR_I;
6205 offset = 8;
6206 break;
6207 case EXCP_IRQ:
6208 new_mode = ARM_CPU_MODE_IRQ;
6209 addr = 0x18;
6210 /* Disable IRQ and imprecise data aborts. */
6211 mask = CPSR_A | CPSR_I;
6212 offset = 4;
6213 if (env->cp15.scr_el3 & SCR_IRQ) {
6214 /* IRQ routed to monitor mode */
6215 new_mode = ARM_CPU_MODE_MON;
6216 mask |= CPSR_F;
6218 break;
6219 case EXCP_FIQ:
6220 new_mode = ARM_CPU_MODE_FIQ;
6221 addr = 0x1c;
6222 /* Disable FIQ, IRQ and imprecise data aborts. */
6223 mask = CPSR_A | CPSR_I | CPSR_F;
6224 if (env->cp15.scr_el3 & SCR_FIQ) {
6225 /* FIQ routed to monitor mode */
6226 new_mode = ARM_CPU_MODE_MON;
6228 offset = 4;
6229 break;
6230 case EXCP_SMC:
6231 new_mode = ARM_CPU_MODE_MON;
6232 addr = 0x08;
6233 mask = CPSR_A | CPSR_I | CPSR_F;
6234 offset = 0;
6235 break;
6236 default:
6237 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
6238 return; /* Never happens. Keep compiler happy. */
6241 if (new_mode == ARM_CPU_MODE_MON) {
6242 addr += env->cp15.mvbar;
6243 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
6244 /* High vectors. When enabled, base address cannot be remapped. */
6245 addr += 0xffff0000;
6246 } else {
6247 /* ARM v7 architectures provide a vector base address register to remap
6248 * the interrupt vector table.
6249 * This register is only followed in non-monitor mode, and is banked.
6250 * Note: only bits 31:5 are valid.
6252 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
6255 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
6256 env->cp15.scr_el3 &= ~SCR_NS;
6259 switch_mode (env, new_mode);
6260 /* For exceptions taken to AArch32 we must clear the SS bit in both
6261 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
6263 env->uncached_cpsr &= ~PSTATE_SS;
6264 env->spsr = cpsr_read(env);
6265 /* Clear IT bits. */
6266 env->condexec_bits = 0;
6267 /* Switch to the new mode, and to the correct instruction set. */
6268 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
6269 /* Set new mode endianness */
6270 env->uncached_cpsr &= ~CPSR_E;
6271 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
6272 env->uncached_cpsr |= ~CPSR_E;
6274 env->daif |= mask;
6275 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
6276 * and we should just guard the thumb mode on V4 */
6277 if (arm_feature(env, ARM_FEATURE_V4T)) {
6278 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
6280 env->regs[14] = env->regs[15] + offset;
6281 env->regs[15] = addr;
6284 /* Handle exception entry to a target EL which is using AArch64 */
6285 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
6287 ARMCPU *cpu = ARM_CPU(cs);
6288 CPUARMState *env = &cpu->env;
6289 unsigned int new_el = env->exception.target_el;
6290 target_ulong addr = env->cp15.vbar_el[new_el];
6291 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
6293 if (arm_current_el(env) < new_el) {
6294 /* Entry vector offset depends on whether the implemented EL
6295 * immediately lower than the target level is using AArch32 or AArch64
6297 bool is_aa64;
6299 switch (new_el) {
6300 case 3:
6301 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
6302 break;
6303 case 2:
6304 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
6305 break;
6306 case 1:
6307 is_aa64 = is_a64(env);
6308 break;
6309 default:
6310 g_assert_not_reached();
6313 if (is_aa64) {
6314 addr += 0x400;
6315 } else {
6316 addr += 0x600;
6318 } else if (pstate_read(env) & PSTATE_SP) {
6319 addr += 0x200;
6322 switch (cs->exception_index) {
6323 case EXCP_PREFETCH_ABORT:
6324 case EXCP_DATA_ABORT:
6325 env->cp15.far_el[new_el] = env->exception.vaddress;
6326 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
6327 env->cp15.far_el[new_el]);
6328 /* fall through */
6329 case EXCP_BKPT:
6330 case EXCP_UDEF:
6331 case EXCP_SWI:
6332 case EXCP_HVC:
6333 case EXCP_HYP_TRAP:
6334 case EXCP_SMC:
6335 env->cp15.esr_el[new_el] = env->exception.syndrome;
6336 break;
6337 case EXCP_IRQ:
6338 case EXCP_VIRQ:
6339 addr += 0x80;
6340 break;
6341 case EXCP_FIQ:
6342 case EXCP_VFIQ:
6343 addr += 0x100;
6344 break;
6345 case EXCP_SEMIHOST:
6346 qemu_log_mask(CPU_LOG_INT,
6347 "...handling as semihosting call 0x%" PRIx64 "\n",
6348 env->xregs[0]);
6349 env->xregs[0] = do_arm_semihosting(env);
6350 return;
6351 default:
6352 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
6355 if (is_a64(env)) {
6356 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
6357 aarch64_save_sp(env, arm_current_el(env));
6358 env->elr_el[new_el] = env->pc;
6359 } else {
6360 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
6361 if (!env->thumb) {
6362 env->cp15.esr_el[new_el] |= 1 << 25;
6364 env->elr_el[new_el] = env->regs[15];
6366 aarch64_sync_32_to_64(env);
6368 env->condexec_bits = 0;
6370 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
6371 env->elr_el[new_el]);
6373 pstate_write(env, PSTATE_DAIF | new_mode);
6374 env->aarch64 = 1;
6375 aarch64_restore_sp(env, new_el);
6377 env->pc = addr;
6379 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
6380 new_el, env->pc, pstate_read(env));
6383 static inline bool check_for_semihosting(CPUState *cs)
6385 /* Check whether this exception is a semihosting call; if so
6386 * then handle it and return true; otherwise return false.
6388 ARMCPU *cpu = ARM_CPU(cs);
6389 CPUARMState *env = &cpu->env;
6391 if (is_a64(env)) {
6392 if (cs->exception_index == EXCP_SEMIHOST) {
6393 /* This is always the 64-bit semihosting exception.
6394 * The "is this usermode" and "is semihosting enabled"
6395 * checks have been done at translate time.
6397 qemu_log_mask(CPU_LOG_INT,
6398 "...handling as semihosting call 0x%" PRIx64 "\n",
6399 env->xregs[0]);
6400 env->xregs[0] = do_arm_semihosting(env);
6401 return true;
6403 return false;
6404 } else {
6405 uint32_t imm;
6407 /* Only intercept calls from privileged modes, to provide some
6408 * semblance of security.
6410 if (!semihosting_enabled() ||
6411 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR)) {
6412 return false;
6415 switch (cs->exception_index) {
6416 case EXCP_SWI:
6417 /* Check for semihosting interrupt. */
6418 if (env->thumb) {
6419 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
6420 & 0xff;
6421 if (imm == 0xab) {
6422 break;
6424 } else {
6425 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
6426 & 0xffffff;
6427 if (imm == 0x123456) {
6428 break;
6431 return false;
6432 case EXCP_BKPT:
6433 /* See if this is a semihosting syscall. */
6434 if (env->thumb) {
6435 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
6436 & 0xff;
6437 if (imm == 0xab) {
6438 env->regs[15] += 2;
6439 break;
6442 return false;
6443 default:
6444 return false;
6447 qemu_log_mask(CPU_LOG_INT,
6448 "...handling as semihosting call 0x%x\n",
6449 env->regs[0]);
6450 env->regs[0] = do_arm_semihosting(env);
6451 return true;
6455 /* Handle a CPU exception for A and R profile CPUs.
6456 * Do any appropriate logging, handle PSCI calls, and then hand off
6457 * to the AArch64-entry or AArch32-entry function depending on the
6458 * target exception level's register width.
6460 void arm_cpu_do_interrupt(CPUState *cs)
6462 ARMCPU *cpu = ARM_CPU(cs);
6463 CPUARMState *env = &cpu->env;
6464 unsigned int new_el = env->exception.target_el;
6466 assert(!IS_M(env));
6468 arm_log_exception(cs->exception_index);
6469 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
6470 new_el);
6471 if (qemu_loglevel_mask(CPU_LOG_INT)
6472 && !excp_is_internal(cs->exception_index)) {
6473 qemu_log_mask(CPU_LOG_INT, "...with ESR %x/0x%" PRIx32 "\n",
6474 env->exception.syndrome >> ARM_EL_EC_SHIFT,
6475 env->exception.syndrome);
6478 if (arm_is_psci_call(cpu, cs->exception_index)) {
6479 arm_handle_psci_call(cpu);
6480 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
6481 return;
6484 /* Semihosting semantics depend on the register width of the
6485 * code that caused the exception, not the target exception level,
6486 * so must be handled here.
6488 if (check_for_semihosting(cs)) {
6489 return;
6492 assert(!excp_is_internal(cs->exception_index));
6493 if (arm_el_is_aa64(env, new_el)) {
6494 arm_cpu_do_interrupt_aarch64(cs);
6495 } else {
6496 arm_cpu_do_interrupt_aarch32(cs);
6499 if (!kvm_enabled()) {
6500 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
6504 /* Return the exception level which controls this address translation regime */
6505 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
6507 switch (mmu_idx) {
6508 case ARMMMUIdx_S2NS:
6509 case ARMMMUIdx_S1E2:
6510 return 2;
6511 case ARMMMUIdx_S1E3:
6512 return 3;
6513 case ARMMMUIdx_S1SE0:
6514 return arm_el_is_aa64(env, 3) ? 1 : 3;
6515 case ARMMMUIdx_S1SE1:
6516 case ARMMMUIdx_S1NSE0:
6517 case ARMMMUIdx_S1NSE1:
6518 return 1;
6519 default:
6520 g_assert_not_reached();
6524 /* Return true if this address translation regime is secure */
6525 static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx)
6527 switch (mmu_idx) {
6528 case ARMMMUIdx_S12NSE0:
6529 case ARMMMUIdx_S12NSE1:
6530 case ARMMMUIdx_S1NSE0:
6531 case ARMMMUIdx_S1NSE1:
6532 case ARMMMUIdx_S1E2:
6533 case ARMMMUIdx_S2NS:
6534 return false;
6535 case ARMMMUIdx_S1E3:
6536 case ARMMMUIdx_S1SE0:
6537 case ARMMMUIdx_S1SE1:
6538 return true;
6539 default:
6540 g_assert_not_reached();
6544 /* Return the SCTLR value which controls this address translation regime */
6545 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
6547 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
6550 /* Return true if the specified stage of address translation is disabled */
6551 static inline bool regime_translation_disabled(CPUARMState *env,
6552 ARMMMUIdx mmu_idx)
6554 if (mmu_idx == ARMMMUIdx_S2NS) {
6555 return (env->cp15.hcr_el2 & HCR_VM) == 0;
6557 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
6560 static inline bool regime_translation_big_endian(CPUARMState *env,
6561 ARMMMUIdx mmu_idx)
6563 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
6566 /* Return the TCR controlling this translation regime */
6567 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
6569 if (mmu_idx == ARMMMUIdx_S2NS) {
6570 return &env->cp15.vtcr_el2;
6572 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
6575 /* Return the TTBR associated with this translation regime */
6576 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
6577 int ttbrn)
6579 if (mmu_idx == ARMMMUIdx_S2NS) {
6580 return env->cp15.vttbr_el2;
6582 if (ttbrn == 0) {
6583 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
6584 } else {
6585 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
6589 /* Return true if the translation regime is using LPAE format page tables */
6590 static inline bool regime_using_lpae_format(CPUARMState *env,
6591 ARMMMUIdx mmu_idx)
6593 int el = regime_el(env, mmu_idx);
6594 if (el == 2 || arm_el_is_aa64(env, el)) {
6595 return true;
6597 if (arm_feature(env, ARM_FEATURE_LPAE)
6598 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
6599 return true;
6601 return false;
6604 /* Returns true if the stage 1 translation regime is using LPAE format page
6605 * tables. Used when raising alignment exceptions, whose FSR changes depending
6606 * on whether the long or short descriptor format is in use. */
6607 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
6609 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
6610 mmu_idx += ARMMMUIdx_S1NSE0;
6613 return regime_using_lpae_format(env, mmu_idx);
6616 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
6618 switch (mmu_idx) {
6619 case ARMMMUIdx_S1SE0:
6620 case ARMMMUIdx_S1NSE0:
6621 return true;
6622 default:
6623 return false;
6624 case ARMMMUIdx_S12NSE0:
6625 case ARMMMUIdx_S12NSE1:
6626 g_assert_not_reached();
6630 /* Translate section/page access permissions to page
6631 * R/W protection flags
6633 * @env: CPUARMState
6634 * @mmu_idx: MMU index indicating required translation regime
6635 * @ap: The 3-bit access permissions (AP[2:0])
6636 * @domain_prot: The 2-bit domain access permissions
6638 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
6639 int ap, int domain_prot)
6641 bool is_user = regime_is_user(env, mmu_idx);
6643 if (domain_prot == 3) {
6644 return PAGE_READ | PAGE_WRITE;
6647 switch (ap) {
6648 case 0:
6649 if (arm_feature(env, ARM_FEATURE_V7)) {
6650 return 0;
6652 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
6653 case SCTLR_S:
6654 return is_user ? 0 : PAGE_READ;
6655 case SCTLR_R:
6656 return PAGE_READ;
6657 default:
6658 return 0;
6660 case 1:
6661 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
6662 case 2:
6663 if (is_user) {
6664 return PAGE_READ;
6665 } else {
6666 return PAGE_READ | PAGE_WRITE;
6668 case 3:
6669 return PAGE_READ | PAGE_WRITE;
6670 case 4: /* Reserved. */
6671 return 0;
6672 case 5:
6673 return is_user ? 0 : PAGE_READ;
6674 case 6:
6675 return PAGE_READ;
6676 case 7:
6677 if (!arm_feature(env, ARM_FEATURE_V6K)) {
6678 return 0;
6680 return PAGE_READ;
6681 default:
6682 g_assert_not_reached();
6686 /* Translate section/page access permissions to page
6687 * R/W protection flags.
6689 * @ap: The 2-bit simple AP (AP[2:1])
6690 * @is_user: TRUE if accessing from PL0
6692 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
6694 switch (ap) {
6695 case 0:
6696 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
6697 case 1:
6698 return PAGE_READ | PAGE_WRITE;
6699 case 2:
6700 return is_user ? 0 : PAGE_READ;
6701 case 3:
6702 return PAGE_READ;
6703 default:
6704 g_assert_not_reached();
6708 static inline int
6709 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
6711 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
6714 /* Translate S2 section/page access permissions to protection flags
6716 * @env: CPUARMState
6717 * @s2ap: The 2-bit stage2 access permissions (S2AP)
6718 * @xn: XN (execute-never) bit
6720 static int get_S2prot(CPUARMState *env, int s2ap, int xn)
6722 int prot = 0;
6724 if (s2ap & 1) {
6725 prot |= PAGE_READ;
6727 if (s2ap & 2) {
6728 prot |= PAGE_WRITE;
6730 if (!xn) {
6731 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
6732 prot |= PAGE_EXEC;
6735 return prot;
6738 /* Translate section/page access permissions to protection flags
6740 * @env: CPUARMState
6741 * @mmu_idx: MMU index indicating required translation regime
6742 * @is_aa64: TRUE if AArch64
6743 * @ap: The 2-bit simple AP (AP[2:1])
6744 * @ns: NS (non-secure) bit
6745 * @xn: XN (execute-never) bit
6746 * @pxn: PXN (privileged execute-never) bit
6748 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
6749 int ap, int ns, int xn, int pxn)
6751 bool is_user = regime_is_user(env, mmu_idx);
6752 int prot_rw, user_rw;
6753 bool have_wxn;
6754 int wxn = 0;
6756 assert(mmu_idx != ARMMMUIdx_S2NS);
6758 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
6759 if (is_user) {
6760 prot_rw = user_rw;
6761 } else {
6762 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
6765 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
6766 return prot_rw;
6769 /* TODO have_wxn should be replaced with
6770 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
6771 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
6772 * compatible processors have EL2, which is required for [U]WXN.
6774 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
6776 if (have_wxn) {
6777 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
6780 if (is_aa64) {
6781 switch (regime_el(env, mmu_idx)) {
6782 case 1:
6783 if (!is_user) {
6784 xn = pxn || (user_rw & PAGE_WRITE);
6786 break;
6787 case 2:
6788 case 3:
6789 break;
6791 } else if (arm_feature(env, ARM_FEATURE_V7)) {
6792 switch (regime_el(env, mmu_idx)) {
6793 case 1:
6794 case 3:
6795 if (is_user) {
6796 xn = xn || !(user_rw & PAGE_READ);
6797 } else {
6798 int uwxn = 0;
6799 if (have_wxn) {
6800 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
6802 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
6803 (uwxn && (user_rw & PAGE_WRITE));
6805 break;
6806 case 2:
6807 break;
6809 } else {
6810 xn = wxn = 0;
6813 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
6814 return prot_rw;
6816 return prot_rw | PAGE_EXEC;
6819 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
6820 uint32_t *table, uint32_t address)
6822 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
6823 TCR *tcr = regime_tcr(env, mmu_idx);
6825 if (address & tcr->mask) {
6826 if (tcr->raw_tcr & TTBCR_PD1) {
6827 /* Translation table walk disabled for TTBR1 */
6828 return false;
6830 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
6831 } else {
6832 if (tcr->raw_tcr & TTBCR_PD0) {
6833 /* Translation table walk disabled for TTBR0 */
6834 return false;
6836 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
6838 *table |= (address >> 18) & 0x3ffc;
6839 return true;
6842 /* Translate a S1 pagetable walk through S2 if needed. */
6843 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
6844 hwaddr addr, MemTxAttrs txattrs,
6845 uint32_t *fsr,
6846 ARMMMUFaultInfo *fi)
6848 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
6849 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
6850 target_ulong s2size;
6851 hwaddr s2pa;
6852 int s2prot;
6853 int ret;
6855 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
6856 &txattrs, &s2prot, &s2size, fsr, fi);
6857 if (ret) {
6858 fi->s2addr = addr;
6859 fi->stage2 = true;
6860 fi->s1ptw = true;
6861 return ~0;
6863 addr = s2pa;
6865 return addr;
6868 /* All loads done in the course of a page table walk go through here.
6869 * TODO: rather than ignoring errors from physical memory reads (which
6870 * are external aborts in ARM terminology) we should propagate this
6871 * error out so that we can turn it into a Data Abort if this walk
6872 * was being done for a CPU load/store or an address translation instruction
6873 * (but not if it was for a debug access).
6875 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
6876 ARMMMUIdx mmu_idx, uint32_t *fsr,
6877 ARMMMUFaultInfo *fi)
6879 ARMCPU *cpu = ARM_CPU(cs);
6880 CPUARMState *env = &cpu->env;
6881 MemTxAttrs attrs = {};
6882 AddressSpace *as;
6884 attrs.secure = is_secure;
6885 as = arm_addressspace(cs, attrs);
6886 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
6887 if (fi->s1ptw) {
6888 return 0;
6890 if (regime_translation_big_endian(env, mmu_idx)) {
6891 return address_space_ldl_be(as, addr, attrs, NULL);
6892 } else {
6893 return address_space_ldl_le(as, addr, attrs, NULL);
6897 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
6898 ARMMMUIdx mmu_idx, uint32_t *fsr,
6899 ARMMMUFaultInfo *fi)
6901 ARMCPU *cpu = ARM_CPU(cs);
6902 CPUARMState *env = &cpu->env;
6903 MemTxAttrs attrs = {};
6904 AddressSpace *as;
6906 attrs.secure = is_secure;
6907 as = arm_addressspace(cs, attrs);
6908 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
6909 if (fi->s1ptw) {
6910 return 0;
6912 if (regime_translation_big_endian(env, mmu_idx)) {
6913 return address_space_ldq_be(as, addr, attrs, NULL);
6914 } else {
6915 return address_space_ldq_le(as, addr, attrs, NULL);
6919 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
6920 int access_type, ARMMMUIdx mmu_idx,
6921 hwaddr *phys_ptr, int *prot,
6922 target_ulong *page_size, uint32_t *fsr,
6923 ARMMMUFaultInfo *fi)
6925 CPUState *cs = CPU(arm_env_get_cpu(env));
6926 int code;
6927 uint32_t table;
6928 uint32_t desc;
6929 int type;
6930 int ap;
6931 int domain = 0;
6932 int domain_prot;
6933 hwaddr phys_addr;
6934 uint32_t dacr;
6936 /* Pagetable walk. */
6937 /* Lookup l1 descriptor. */
6938 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
6939 /* Section translation fault if page walk is disabled by PD0 or PD1 */
6940 code = 5;
6941 goto do_fault;
6943 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6944 mmu_idx, fsr, fi);
6945 type = (desc & 3);
6946 domain = (desc >> 5) & 0x0f;
6947 if (regime_el(env, mmu_idx) == 1) {
6948 dacr = env->cp15.dacr_ns;
6949 } else {
6950 dacr = env->cp15.dacr_s;
6952 domain_prot = (dacr >> (domain * 2)) & 3;
6953 if (type == 0) {
6954 /* Section translation fault. */
6955 code = 5;
6956 goto do_fault;
6958 if (domain_prot == 0 || domain_prot == 2) {
6959 if (type == 2)
6960 code = 9; /* Section domain fault. */
6961 else
6962 code = 11; /* Page domain fault. */
6963 goto do_fault;
6965 if (type == 2) {
6966 /* 1Mb section. */
6967 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
6968 ap = (desc >> 10) & 3;
6969 code = 13;
6970 *page_size = 1024 * 1024;
6971 } else {
6972 /* Lookup l2 entry. */
6973 if (type == 1) {
6974 /* Coarse pagetable. */
6975 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
6976 } else {
6977 /* Fine pagetable. */
6978 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
6980 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6981 mmu_idx, fsr, fi);
6982 switch (desc & 3) {
6983 case 0: /* Page translation fault. */
6984 code = 7;
6985 goto do_fault;
6986 case 1: /* 64k page. */
6987 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6988 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
6989 *page_size = 0x10000;
6990 break;
6991 case 2: /* 4k page. */
6992 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
6993 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
6994 *page_size = 0x1000;
6995 break;
6996 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
6997 if (type == 1) {
6998 /* ARMv6/XScale extended small page format */
6999 if (arm_feature(env, ARM_FEATURE_XSCALE)
7000 || arm_feature(env, ARM_FEATURE_V6)) {
7001 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
7002 *page_size = 0x1000;
7003 } else {
7004 /* UNPREDICTABLE in ARMv5; we choose to take a
7005 * page translation fault.
7007 code = 7;
7008 goto do_fault;
7010 } else {
7011 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
7012 *page_size = 0x400;
7014 ap = (desc >> 4) & 3;
7015 break;
7016 default:
7017 /* Never happens, but compiler isn't smart enough to tell. */
7018 abort();
7020 code = 15;
7022 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
7023 *prot |= *prot ? PAGE_EXEC : 0;
7024 if (!(*prot & (1 << access_type))) {
7025 /* Access permission fault. */
7026 goto do_fault;
7028 *phys_ptr = phys_addr;
7029 return false;
7030 do_fault:
7031 *fsr = code | (domain << 4);
7032 return true;
7035 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
7036 int access_type, ARMMMUIdx mmu_idx,
7037 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
7038 target_ulong *page_size, uint32_t *fsr,
7039 ARMMMUFaultInfo *fi)
7041 CPUState *cs = CPU(arm_env_get_cpu(env));
7042 int code;
7043 uint32_t table;
7044 uint32_t desc;
7045 uint32_t xn;
7046 uint32_t pxn = 0;
7047 int type;
7048 int ap;
7049 int domain = 0;
7050 int domain_prot;
7051 hwaddr phys_addr;
7052 uint32_t dacr;
7053 bool ns;
7055 /* Pagetable walk. */
7056 /* Lookup l1 descriptor. */
7057 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
7058 /* Section translation fault if page walk is disabled by PD0 or PD1 */
7059 code = 5;
7060 goto do_fault;
7062 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
7063 mmu_idx, fsr, fi);
7064 type = (desc & 3);
7065 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
7066 /* Section translation fault, or attempt to use the encoding
7067 * which is Reserved on implementations without PXN.
7069 code = 5;
7070 goto do_fault;
7072 if ((type == 1) || !(desc & (1 << 18))) {
7073 /* Page or Section. */
7074 domain = (desc >> 5) & 0x0f;
7076 if (regime_el(env, mmu_idx) == 1) {
7077 dacr = env->cp15.dacr_ns;
7078 } else {
7079 dacr = env->cp15.dacr_s;
7081 domain_prot = (dacr >> (domain * 2)) & 3;
7082 if (domain_prot == 0 || domain_prot == 2) {
7083 if (type != 1) {
7084 code = 9; /* Section domain fault. */
7085 } else {
7086 code = 11; /* Page domain fault. */
7088 goto do_fault;
7090 if (type != 1) {
7091 if (desc & (1 << 18)) {
7092 /* Supersection. */
7093 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
7094 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
7095 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
7096 *page_size = 0x1000000;
7097 } else {
7098 /* Section. */
7099 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
7100 *page_size = 0x100000;
7102 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
7103 xn = desc & (1 << 4);
7104 pxn = desc & 1;
7105 code = 13;
7106 ns = extract32(desc, 19, 1);
7107 } else {
7108 if (arm_feature(env, ARM_FEATURE_PXN)) {
7109 pxn = (desc >> 2) & 1;
7111 ns = extract32(desc, 3, 1);
7112 /* Lookup l2 entry. */
7113 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
7114 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
7115 mmu_idx, fsr, fi);
7116 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
7117 switch (desc & 3) {
7118 case 0: /* Page translation fault. */
7119 code = 7;
7120 goto do_fault;
7121 case 1: /* 64k page. */
7122 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
7123 xn = desc & (1 << 15);
7124 *page_size = 0x10000;
7125 break;
7126 case 2: case 3: /* 4k page. */
7127 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
7128 xn = desc & 1;
7129 *page_size = 0x1000;
7130 break;
7131 default:
7132 /* Never happens, but compiler isn't smart enough to tell. */
7133 abort();
7135 code = 15;
7137 if (domain_prot == 3) {
7138 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
7139 } else {
7140 if (pxn && !regime_is_user(env, mmu_idx)) {
7141 xn = 1;
7143 if (xn && access_type == 2)
7144 goto do_fault;
7146 if (arm_feature(env, ARM_FEATURE_V6K) &&
7147 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
7148 /* The simplified model uses AP[0] as an access control bit. */
7149 if ((ap & 1) == 0) {
7150 /* Access flag fault. */
7151 code = (code == 15) ? 6 : 3;
7152 goto do_fault;
7154 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
7155 } else {
7156 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
7158 if (*prot && !xn) {
7159 *prot |= PAGE_EXEC;
7161 if (!(*prot & (1 << access_type))) {
7162 /* Access permission fault. */
7163 goto do_fault;
7166 if (ns) {
7167 /* The NS bit will (as required by the architecture) have no effect if
7168 * the CPU doesn't support TZ or this is a non-secure translation
7169 * regime, because the attribute will already be non-secure.
7171 attrs->secure = false;
7173 *phys_ptr = phys_addr;
7174 return false;
7175 do_fault:
7176 *fsr = code | (domain << 4);
7177 return true;
7180 /* Fault type for long-descriptor MMU fault reporting; this corresponds
7181 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
7183 typedef enum {
7184 translation_fault = 1,
7185 access_fault = 2,
7186 permission_fault = 3,
7187 } MMUFaultType;
7190 * check_s2_mmu_setup
7191 * @cpu: ARMCPU
7192 * @is_aa64: True if the translation regime is in AArch64 state
7193 * @startlevel: Suggested starting level
7194 * @inputsize: Bitsize of IPAs
7195 * @stride: Page-table stride (See the ARM ARM)
7197 * Returns true if the suggested S2 translation parameters are OK and
7198 * false otherwise.
7200 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
7201 int inputsize, int stride)
7203 const int grainsize = stride + 3;
7204 int startsizecheck;
7206 /* Negative levels are never allowed. */
7207 if (level < 0) {
7208 return false;
7211 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
7212 if (startsizecheck < 1 || startsizecheck > stride + 4) {
7213 return false;
7216 if (is_aa64) {
7217 CPUARMState *env = &cpu->env;
7218 unsigned int pamax = arm_pamax(cpu);
7220 switch (stride) {
7221 case 13: /* 64KB Pages. */
7222 if (level == 0 || (level == 1 && pamax <= 42)) {
7223 return false;
7225 break;
7226 case 11: /* 16KB Pages. */
7227 if (level == 0 || (level == 1 && pamax <= 40)) {
7228 return false;
7230 break;
7231 case 9: /* 4KB Pages. */
7232 if (level == 0 && pamax <= 42) {
7233 return false;
7235 break;
7236 default:
7237 g_assert_not_reached();
7240 /* Inputsize checks. */
7241 if (inputsize > pamax &&
7242 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
7243 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
7244 return false;
7246 } else {
7247 /* AArch32 only supports 4KB pages. Assert on that. */
7248 assert(stride == 9);
7250 if (level == 0) {
7251 return false;
7254 return true;
7257 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
7258 int access_type, ARMMMUIdx mmu_idx,
7259 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
7260 target_ulong *page_size_ptr, uint32_t *fsr,
7261 ARMMMUFaultInfo *fi)
7263 ARMCPU *cpu = arm_env_get_cpu(env);
7264 CPUState *cs = CPU(cpu);
7265 /* Read an LPAE long-descriptor translation table. */
7266 MMUFaultType fault_type = translation_fault;
7267 uint32_t level;
7268 uint32_t epd = 0;
7269 int32_t t0sz, t1sz;
7270 uint32_t tg;
7271 uint64_t ttbr;
7272 int ttbr_select;
7273 hwaddr descaddr, indexmask, indexmask_grainsize;
7274 uint32_t tableattrs;
7275 target_ulong page_size;
7276 uint32_t attrs;
7277 int32_t stride = 9;
7278 int32_t va_size;
7279 int inputsize;
7280 int32_t tbi = 0;
7281 TCR *tcr = regime_tcr(env, mmu_idx);
7282 int ap, ns, xn, pxn;
7283 uint32_t el = regime_el(env, mmu_idx);
7284 bool ttbr1_valid = true;
7285 uint64_t descaddrmask;
7287 /* TODO:
7288 * This code does not handle the different format TCR for VTCR_EL2.
7289 * This code also does not support shareability levels.
7290 * Attribute and permission bit handling should also be checked when adding
7291 * support for those page table walks.
7293 if (arm_el_is_aa64(env, el)) {
7294 level = 0;
7295 va_size = 64;
7296 if (el > 1) {
7297 if (mmu_idx != ARMMMUIdx_S2NS) {
7298 tbi = extract64(tcr->raw_tcr, 20, 1);
7300 } else {
7301 if (extract64(address, 55, 1)) {
7302 tbi = extract64(tcr->raw_tcr, 38, 1);
7303 } else {
7304 tbi = extract64(tcr->raw_tcr, 37, 1);
7307 tbi *= 8;
7309 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
7310 * invalid.
7312 if (el > 1) {
7313 ttbr1_valid = false;
7315 } else {
7316 level = 1;
7317 va_size = 32;
7318 /* There is no TTBR1 for EL2 */
7319 if (el == 2) {
7320 ttbr1_valid = false;
7324 /* Determine whether this address is in the region controlled by
7325 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
7326 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
7327 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
7329 if (va_size == 64) {
7330 /* AArch64 translation. */
7331 t0sz = extract32(tcr->raw_tcr, 0, 6);
7332 t0sz = MIN(t0sz, 39);
7333 t0sz = MAX(t0sz, 16);
7334 } else if (mmu_idx != ARMMMUIdx_S2NS) {
7335 /* AArch32 stage 1 translation. */
7336 t0sz = extract32(tcr->raw_tcr, 0, 3);
7337 } else {
7338 /* AArch32 stage 2 translation. */
7339 bool sext = extract32(tcr->raw_tcr, 4, 1);
7340 bool sign = extract32(tcr->raw_tcr, 3, 1);
7341 t0sz = sextract32(tcr->raw_tcr, 0, 4);
7343 /* If the sign-extend bit is not the same as t0sz[3], the result
7344 * is unpredictable. Flag this as a guest error. */
7345 if (sign != sext) {
7346 qemu_log_mask(LOG_GUEST_ERROR,
7347 "AArch32: VTCR.S / VTCR.T0SZ[3] missmatch\n");
7350 t1sz = extract32(tcr->raw_tcr, 16, 6);
7351 if (va_size == 64) {
7352 t1sz = MIN(t1sz, 39);
7353 t1sz = MAX(t1sz, 16);
7355 if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
7356 /* there is a ttbr0 region and we are in it (high bits all zero) */
7357 ttbr_select = 0;
7358 } else if (ttbr1_valid && t1sz &&
7359 !extract64(~address, va_size - t1sz, t1sz - tbi)) {
7360 /* there is a ttbr1 region and we are in it (high bits all one) */
7361 ttbr_select = 1;
7362 } else if (!t0sz) {
7363 /* ttbr0 region is "everything not in the ttbr1 region" */
7364 ttbr_select = 0;
7365 } else if (!t1sz && ttbr1_valid) {
7366 /* ttbr1 region is "everything not in the ttbr0 region" */
7367 ttbr_select = 1;
7368 } else {
7369 /* in the gap between the two regions, this is a Translation fault */
7370 fault_type = translation_fault;
7371 goto do_fault;
7374 /* Note that QEMU ignores shareability and cacheability attributes,
7375 * so we don't need to do anything with the SH, ORGN, IRGN fields
7376 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
7377 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
7378 * implement any ASID-like capability so we can ignore it (instead
7379 * we will always flush the TLB any time the ASID is changed).
7381 if (ttbr_select == 0) {
7382 ttbr = regime_ttbr(env, mmu_idx, 0);
7383 if (el < 2) {
7384 epd = extract32(tcr->raw_tcr, 7, 1);
7386 inputsize = va_size - t0sz;
7388 tg = extract32(tcr->raw_tcr, 14, 2);
7389 if (tg == 1) { /* 64KB pages */
7390 stride = 13;
7392 if (tg == 2) { /* 16KB pages */
7393 stride = 11;
7395 } else {
7396 /* We should only be here if TTBR1 is valid */
7397 assert(ttbr1_valid);
7399 ttbr = regime_ttbr(env, mmu_idx, 1);
7400 epd = extract32(tcr->raw_tcr, 23, 1);
7401 inputsize = va_size - t1sz;
7403 tg = extract32(tcr->raw_tcr, 30, 2);
7404 if (tg == 3) { /* 64KB pages */
7405 stride = 13;
7407 if (tg == 1) { /* 16KB pages */
7408 stride = 11;
7412 /* Here we should have set up all the parameters for the translation:
7413 * va_size, inputsize, ttbr, epd, stride, tbi
7416 if (epd) {
7417 /* Translation table walk disabled => Translation fault on TLB miss
7418 * Note: This is always 0 on 64-bit EL2 and EL3.
7420 goto do_fault;
7423 if (mmu_idx != ARMMMUIdx_S2NS) {
7424 /* The starting level depends on the virtual address size (which can
7425 * be up to 48 bits) and the translation granule size. It indicates
7426 * the number of strides (stride bits at a time) needed to
7427 * consume the bits of the input address. In the pseudocode this is:
7428 * level = 4 - RoundUp((inputsize - grainsize) / stride)
7429 * where their 'inputsize' is our 'inputsize', 'grainsize' is
7430 * our 'stride + 3' and 'stride' is our 'stride'.
7431 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
7432 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
7433 * = 4 - (inputsize - 4) / stride;
7435 level = 4 - (inputsize - 4) / stride;
7436 } else {
7437 /* For stage 2 translations the starting level is specified by the
7438 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
7440 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
7441 uint32_t startlevel;
7442 bool ok;
7444 if (va_size == 32 || stride == 9) {
7445 /* AArch32 or 4KB pages */
7446 startlevel = 2 - sl0;
7447 } else {
7448 /* 16KB or 64KB pages */
7449 startlevel = 3 - sl0;
7452 /* Check that the starting level is valid. */
7453 ok = check_s2_mmu_setup(cpu, va_size == 64, startlevel,
7454 inputsize, stride);
7455 if (!ok) {
7456 fault_type = translation_fault;
7457 goto do_fault;
7459 level = startlevel;
7462 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
7463 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
7465 /* Now we can extract the actual base address from the TTBR */
7466 descaddr = extract64(ttbr, 0, 48);
7467 descaddr &= ~indexmask;
7469 /* The address field in the descriptor goes up to bit 39 for ARMv7
7470 * but up to bit 47 for ARMv8, but we use the descaddrmask
7471 * up to bit 39 for AArch32, because we don't need other bits in that case
7472 * to construct next descriptor address (anyway they should be all zeroes).
7474 descaddrmask = ((1ull << (va_size == 64 ? 48 : 40)) - 1) &
7475 ~indexmask_grainsize;
7477 /* Secure accesses start with the page table in secure memory and
7478 * can be downgraded to non-secure at any step. Non-secure accesses
7479 * remain non-secure. We implement this by just ORing in the NSTable/NS
7480 * bits at each step.
7482 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
7483 for (;;) {
7484 uint64_t descriptor;
7485 bool nstable;
7487 descaddr |= (address >> (stride * (4 - level))) & indexmask;
7488 descaddr &= ~7ULL;
7489 nstable = extract32(tableattrs, 4, 1);
7490 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fsr, fi);
7491 if (fi->s1ptw) {
7492 goto do_fault;
7495 if (!(descriptor & 1) ||
7496 (!(descriptor & 2) && (level == 3))) {
7497 /* Invalid, or the Reserved level 3 encoding */
7498 goto do_fault;
7500 descaddr = descriptor & descaddrmask;
7502 if ((descriptor & 2) && (level < 3)) {
7503 /* Table entry. The top five bits are attributes which may
7504 * propagate down through lower levels of the table (and
7505 * which are all arranged so that 0 means "no effect", so
7506 * we can gather them up by ORing in the bits at each level).
7508 tableattrs |= extract64(descriptor, 59, 5);
7509 level++;
7510 indexmask = indexmask_grainsize;
7511 continue;
7513 /* Block entry at level 1 or 2, or page entry at level 3.
7514 * These are basically the same thing, although the number
7515 * of bits we pull in from the vaddr varies.
7517 page_size = (1ULL << ((stride * (4 - level)) + 3));
7518 descaddr |= (address & (page_size - 1));
7519 /* Extract attributes from the descriptor */
7520 attrs = extract64(descriptor, 2, 10)
7521 | (extract64(descriptor, 52, 12) << 10);
7523 if (mmu_idx == ARMMMUIdx_S2NS) {
7524 /* Stage 2 table descriptors do not include any attribute fields */
7525 break;
7527 /* Merge in attributes from table descriptors */
7528 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
7529 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
7530 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
7531 * means "force PL1 access only", which means forcing AP[1] to 0.
7533 if (extract32(tableattrs, 2, 1)) {
7534 attrs &= ~(1 << 4);
7536 attrs |= nstable << 3; /* NS */
7537 break;
7539 /* Here descaddr is the final physical address, and attributes
7540 * are all in attrs.
7542 fault_type = access_fault;
7543 if ((attrs & (1 << 8)) == 0) {
7544 /* Access flag */
7545 goto do_fault;
7548 ap = extract32(attrs, 4, 2);
7549 xn = extract32(attrs, 12, 1);
7551 if (mmu_idx == ARMMMUIdx_S2NS) {
7552 ns = true;
7553 *prot = get_S2prot(env, ap, xn);
7554 } else {
7555 ns = extract32(attrs, 3, 1);
7556 pxn = extract32(attrs, 11, 1);
7557 *prot = get_S1prot(env, mmu_idx, va_size == 64, ap, ns, xn, pxn);
7560 fault_type = permission_fault;
7561 if (!(*prot & (1 << access_type))) {
7562 goto do_fault;
7565 if (ns) {
7566 /* The NS bit will (as required by the architecture) have no effect if
7567 * the CPU doesn't support TZ or this is a non-secure translation
7568 * regime, because the attribute will already be non-secure.
7570 txattrs->secure = false;
7572 *phys_ptr = descaddr;
7573 *page_size_ptr = page_size;
7574 return false;
7576 do_fault:
7577 /* Long-descriptor format IFSR/DFSR value */
7578 *fsr = (1 << 9) | (fault_type << 2) | level;
7579 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
7580 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
7581 return true;
7584 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
7585 ARMMMUIdx mmu_idx,
7586 int32_t address, int *prot)
7588 *prot = PAGE_READ | PAGE_WRITE;
7589 switch (address) {
7590 case 0xF0000000 ... 0xFFFFFFFF:
7591 if (regime_sctlr(env, mmu_idx) & SCTLR_V) { /* hivecs execing is ok */
7592 *prot |= PAGE_EXEC;
7594 break;
7595 case 0x00000000 ... 0x7FFFFFFF:
7596 *prot |= PAGE_EXEC;
7597 break;
7602 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
7603 int access_type, ARMMMUIdx mmu_idx,
7604 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
7606 ARMCPU *cpu = arm_env_get_cpu(env);
7607 int n;
7608 bool is_user = regime_is_user(env, mmu_idx);
7610 *phys_ptr = address;
7611 *prot = 0;
7613 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
7614 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
7615 } else { /* MPU enabled */
7616 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
7617 /* region search */
7618 uint32_t base = env->pmsav7.drbar[n];
7619 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
7620 uint32_t rmask;
7621 bool srdis = false;
7623 if (!(env->pmsav7.drsr[n] & 0x1)) {
7624 continue;
7627 if (!rsize) {
7628 qemu_log_mask(LOG_GUEST_ERROR, "DRSR.Rsize field can not be 0");
7629 continue;
7631 rsize++;
7632 rmask = (1ull << rsize) - 1;
7634 if (base & rmask) {
7635 qemu_log_mask(LOG_GUEST_ERROR, "DRBAR %" PRIx32 " misaligned "
7636 "to DRSR region size, mask = %" PRIx32,
7637 base, rmask);
7638 continue;
7641 if (address < base || address > base + rmask) {
7642 continue;
7645 /* Region matched */
7647 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
7648 int i, snd;
7649 uint32_t srdis_mask;
7651 rsize -= 3; /* sub region size (power of 2) */
7652 snd = ((address - base) >> rsize) & 0x7;
7653 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
7655 srdis_mask = srdis ? 0x3 : 0x0;
7656 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
7657 /* This will check in groups of 2, 4 and then 8, whether
7658 * the subregion bits are consistent. rsize is incremented
7659 * back up to give the region size, considering consistent
7660 * adjacent subregions as one region. Stop testing if rsize
7661 * is already big enough for an entire QEMU page.
7663 int snd_rounded = snd & ~(i - 1);
7664 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
7665 snd_rounded + 8, i);
7666 if (srdis_mask ^ srdis_multi) {
7667 break;
7669 srdis_mask = (srdis_mask << i) | srdis_mask;
7670 rsize++;
7673 if (rsize < TARGET_PAGE_BITS) {
7674 qemu_log_mask(LOG_UNIMP, "No support for MPU (sub)region"
7675 "alignment of %" PRIu32 " bits. Minimum is %d\n",
7676 rsize, TARGET_PAGE_BITS);
7677 continue;
7679 if (srdis) {
7680 continue;
7682 break;
7685 if (n == -1) { /* no hits */
7686 if (cpu->pmsav7_dregion &&
7687 (is_user || !(regime_sctlr(env, mmu_idx) & SCTLR_BR))) {
7688 /* background fault */
7689 *fsr = 0;
7690 return true;
7692 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
7693 } else { /* a MPU hit! */
7694 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
7696 if (is_user) { /* User mode AP bit decoding */
7697 switch (ap) {
7698 case 0:
7699 case 1:
7700 case 5:
7701 break; /* no access */
7702 case 3:
7703 *prot |= PAGE_WRITE;
7704 /* fall through */
7705 case 2:
7706 case 6:
7707 *prot |= PAGE_READ | PAGE_EXEC;
7708 break;
7709 default:
7710 qemu_log_mask(LOG_GUEST_ERROR,
7711 "Bad value for AP bits in DRACR %"
7712 PRIx32 "\n", ap);
7714 } else { /* Priv. mode AP bits decoding */
7715 switch (ap) {
7716 case 0:
7717 break; /* no access */
7718 case 1:
7719 case 2:
7720 case 3:
7721 *prot |= PAGE_WRITE;
7722 /* fall through */
7723 case 5:
7724 case 6:
7725 *prot |= PAGE_READ | PAGE_EXEC;
7726 break;
7727 default:
7728 qemu_log_mask(LOG_GUEST_ERROR,
7729 "Bad value for AP bits in DRACR %"
7730 PRIx32 "\n", ap);
7734 /* execute never */
7735 if (env->pmsav7.dracr[n] & (1 << 12)) {
7736 *prot &= ~PAGE_EXEC;
7741 *fsr = 0x00d; /* Permission fault */
7742 return !(*prot & (1 << access_type));
7745 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
7746 int access_type, ARMMMUIdx mmu_idx,
7747 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
7749 int n;
7750 uint32_t mask;
7751 uint32_t base;
7752 bool is_user = regime_is_user(env, mmu_idx);
7754 *phys_ptr = address;
7755 for (n = 7; n >= 0; n--) {
7756 base = env->cp15.c6_region[n];
7757 if ((base & 1) == 0) {
7758 continue;
7760 mask = 1 << ((base >> 1) & 0x1f);
7761 /* Keep this shift separate from the above to avoid an
7762 (undefined) << 32. */
7763 mask = (mask << 1) - 1;
7764 if (((base ^ address) & ~mask) == 0) {
7765 break;
7768 if (n < 0) {
7769 *fsr = 2;
7770 return true;
7773 if (access_type == 2) {
7774 mask = env->cp15.pmsav5_insn_ap;
7775 } else {
7776 mask = env->cp15.pmsav5_data_ap;
7778 mask = (mask >> (n * 4)) & 0xf;
7779 switch (mask) {
7780 case 0:
7781 *fsr = 1;
7782 return true;
7783 case 1:
7784 if (is_user) {
7785 *fsr = 1;
7786 return true;
7788 *prot = PAGE_READ | PAGE_WRITE;
7789 break;
7790 case 2:
7791 *prot = PAGE_READ;
7792 if (!is_user) {
7793 *prot |= PAGE_WRITE;
7795 break;
7796 case 3:
7797 *prot = PAGE_READ | PAGE_WRITE;
7798 break;
7799 case 5:
7800 if (is_user) {
7801 *fsr = 1;
7802 return true;
7804 *prot = PAGE_READ;
7805 break;
7806 case 6:
7807 *prot = PAGE_READ;
7808 break;
7809 default:
7810 /* Bad permission. */
7811 *fsr = 1;
7812 return true;
7814 *prot |= PAGE_EXEC;
7815 return false;
7818 /* get_phys_addr - get the physical address for this virtual address
7820 * Find the physical address corresponding to the given virtual address,
7821 * by doing a translation table walk on MMU based systems or using the
7822 * MPU state on MPU based systems.
7824 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
7825 * prot and page_size may not be filled in, and the populated fsr value provides
7826 * information on why the translation aborted, in the format of a
7827 * DFSR/IFSR fault register, with the following caveats:
7828 * * we honour the short vs long DFSR format differences.
7829 * * the WnR bit is never set (the caller must do this).
7830 * * for PSMAv5 based systems we don't bother to return a full FSR format
7831 * value.
7833 * @env: CPUARMState
7834 * @address: virtual address to get physical address for
7835 * @access_type: 0 for read, 1 for write, 2 for execute
7836 * @mmu_idx: MMU index indicating required translation regime
7837 * @phys_ptr: set to the physical address corresponding to the virtual address
7838 * @attrs: set to the memory transaction attributes to use
7839 * @prot: set to the permissions for the page containing phys_ptr
7840 * @page_size: set to the size of the page containing phys_ptr
7841 * @fsr: set to the DFSR/IFSR value on failure
7843 static bool get_phys_addr(CPUARMState *env, target_ulong address,
7844 int access_type, ARMMMUIdx mmu_idx,
7845 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
7846 target_ulong *page_size, uint32_t *fsr,
7847 ARMMMUFaultInfo *fi)
7849 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
7850 /* Call ourselves recursively to do the stage 1 and then stage 2
7851 * translations.
7853 if (arm_feature(env, ARM_FEATURE_EL2)) {
7854 hwaddr ipa;
7855 int s2_prot;
7856 int ret;
7858 ret = get_phys_addr(env, address, access_type,
7859 mmu_idx + ARMMMUIdx_S1NSE0, &ipa, attrs,
7860 prot, page_size, fsr, fi);
7862 /* If S1 fails or S2 is disabled, return early. */
7863 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
7864 *phys_ptr = ipa;
7865 return ret;
7868 /* S1 is done. Now do S2 translation. */
7869 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
7870 phys_ptr, attrs, &s2_prot,
7871 page_size, fsr, fi);
7872 fi->s2addr = ipa;
7873 /* Combine the S1 and S2 perms. */
7874 *prot &= s2_prot;
7875 return ret;
7876 } else {
7878 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
7880 mmu_idx += ARMMMUIdx_S1NSE0;
7884 /* The page table entries may downgrade secure to non-secure, but
7885 * cannot upgrade an non-secure translation regime's attributes
7886 * to secure.
7888 attrs->secure = regime_is_secure(env, mmu_idx);
7889 attrs->user = regime_is_user(env, mmu_idx);
7891 /* Fast Context Switch Extension. This doesn't exist at all in v8.
7892 * In v7 and earlier it affects all stage 1 translations.
7894 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
7895 && !arm_feature(env, ARM_FEATURE_V8)) {
7896 if (regime_el(env, mmu_idx) == 3) {
7897 address += env->cp15.fcseidr_s;
7898 } else {
7899 address += env->cp15.fcseidr_ns;
7903 /* pmsav7 has special handling for when MPU is disabled so call it before
7904 * the common MMU/MPU disabled check below.
7906 if (arm_feature(env, ARM_FEATURE_MPU) &&
7907 arm_feature(env, ARM_FEATURE_V7)) {
7908 *page_size = TARGET_PAGE_SIZE;
7909 return get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
7910 phys_ptr, prot, fsr);
7913 if (regime_translation_disabled(env, mmu_idx)) {
7914 /* MMU/MPU disabled. */
7915 *phys_ptr = address;
7916 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
7917 *page_size = TARGET_PAGE_SIZE;
7918 return 0;
7921 if (arm_feature(env, ARM_FEATURE_MPU)) {
7922 /* Pre-v7 MPU */
7923 *page_size = TARGET_PAGE_SIZE;
7924 return get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
7925 phys_ptr, prot, fsr);
7928 if (regime_using_lpae_format(env, mmu_idx)) {
7929 return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
7930 attrs, prot, page_size, fsr, fi);
7931 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
7932 return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
7933 attrs, prot, page_size, fsr, fi);
7934 } else {
7935 return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
7936 prot, page_size, fsr, fi);
7940 /* Walk the page table and (if the mapping exists) add the page
7941 * to the TLB. Return false on success, or true on failure. Populate
7942 * fsr with ARM DFSR/IFSR fault register format value on failure.
7944 bool arm_tlb_fill(CPUState *cs, vaddr address,
7945 int access_type, int mmu_idx, uint32_t *fsr,
7946 ARMMMUFaultInfo *fi)
7948 ARMCPU *cpu = ARM_CPU(cs);
7949 CPUARMState *env = &cpu->env;
7950 hwaddr phys_addr;
7951 target_ulong page_size;
7952 int prot;
7953 int ret;
7954 MemTxAttrs attrs = {};
7956 ret = get_phys_addr(env, address, access_type, mmu_idx, &phys_addr,
7957 &attrs, &prot, &page_size, fsr, fi);
7958 if (!ret) {
7959 /* Map a single [sub]page. */
7960 phys_addr &= TARGET_PAGE_MASK;
7961 address &= TARGET_PAGE_MASK;
7962 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
7963 prot, mmu_idx, page_size);
7964 return 0;
7967 return ret;
7970 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
7971 MemTxAttrs *attrs)
7973 ARMCPU *cpu = ARM_CPU(cs);
7974 CPUARMState *env = &cpu->env;
7975 hwaddr phys_addr;
7976 target_ulong page_size;
7977 int prot;
7978 bool ret;
7979 uint32_t fsr;
7980 ARMMMUFaultInfo fi = {};
7982 *attrs = (MemTxAttrs) {};
7984 ret = get_phys_addr(env, addr, 0, cpu_mmu_index(env, false), &phys_addr,
7985 attrs, &prot, &page_size, &fsr, &fi);
7987 if (ret) {
7988 return -1;
7990 return phys_addr;
7993 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
7995 ARMCPU *cpu = arm_env_get_cpu(env);
7997 switch (reg) {
7998 case 0: /* APSR */
7999 return xpsr_read(env) & 0xf8000000;
8000 case 1: /* IAPSR */
8001 return xpsr_read(env) & 0xf80001ff;
8002 case 2: /* EAPSR */
8003 return xpsr_read(env) & 0xff00fc00;
8004 case 3: /* xPSR */
8005 return xpsr_read(env) & 0xff00fdff;
8006 case 5: /* IPSR */
8007 return xpsr_read(env) & 0x000001ff;
8008 case 6: /* EPSR */
8009 return xpsr_read(env) & 0x0700fc00;
8010 case 7: /* IEPSR */
8011 return xpsr_read(env) & 0x0700edff;
8012 case 8: /* MSP */
8013 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
8014 case 9: /* PSP */
8015 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
8016 case 16: /* PRIMASK */
8017 return (env->daif & PSTATE_I) != 0;
8018 case 17: /* BASEPRI */
8019 case 18: /* BASEPRI_MAX */
8020 return env->v7m.basepri;
8021 case 19: /* FAULTMASK */
8022 return (env->daif & PSTATE_F) != 0;
8023 case 20: /* CONTROL */
8024 return env->v7m.control;
8025 default:
8026 /* ??? For debugging only. */
8027 cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
8028 return 0;
8032 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
8034 ARMCPU *cpu = arm_env_get_cpu(env);
8036 switch (reg) {
8037 case 0: /* APSR */
8038 xpsr_write(env, val, 0xf8000000);
8039 break;
8040 case 1: /* IAPSR */
8041 xpsr_write(env, val, 0xf8000000);
8042 break;
8043 case 2: /* EAPSR */
8044 xpsr_write(env, val, 0xfe00fc00);
8045 break;
8046 case 3: /* xPSR */
8047 xpsr_write(env, val, 0xfe00fc00);
8048 break;
8049 case 5: /* IPSR */
8050 /* IPSR bits are readonly. */
8051 break;
8052 case 6: /* EPSR */
8053 xpsr_write(env, val, 0x0600fc00);
8054 break;
8055 case 7: /* IEPSR */
8056 xpsr_write(env, val, 0x0600fc00);
8057 break;
8058 case 8: /* MSP */
8059 if (env->v7m.current_sp)
8060 env->v7m.other_sp = val;
8061 else
8062 env->regs[13] = val;
8063 break;
8064 case 9: /* PSP */
8065 if (env->v7m.current_sp)
8066 env->regs[13] = val;
8067 else
8068 env->v7m.other_sp = val;
8069 break;
8070 case 16: /* PRIMASK */
8071 if (val & 1) {
8072 env->daif |= PSTATE_I;
8073 } else {
8074 env->daif &= ~PSTATE_I;
8076 break;
8077 case 17: /* BASEPRI */
8078 env->v7m.basepri = val & 0xff;
8079 break;
8080 case 18: /* BASEPRI_MAX */
8081 val &= 0xff;
8082 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
8083 env->v7m.basepri = val;
8084 break;
8085 case 19: /* FAULTMASK */
8086 if (val & 1) {
8087 env->daif |= PSTATE_F;
8088 } else {
8089 env->daif &= ~PSTATE_F;
8091 break;
8092 case 20: /* CONTROL */
8093 env->v7m.control = val & 3;
8094 switch_v7m_sp(env, (val & 2) != 0);
8095 break;
8096 default:
8097 /* ??? For debugging only. */
8098 cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
8099 return;
8103 #endif
8105 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
8107 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
8108 * Note that we do not implement the (architecturally mandated)
8109 * alignment fault for attempts to use this on Device memory
8110 * (which matches the usual QEMU behaviour of not implementing either
8111 * alignment faults or any memory attribute handling).
8114 ARMCPU *cpu = arm_env_get_cpu(env);
8115 uint64_t blocklen = 4 << cpu->dcz_blocksize;
8116 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
8118 #ifndef CONFIG_USER_ONLY
8120 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
8121 * the block size so we might have to do more than one TLB lookup.
8122 * We know that in fact for any v8 CPU the page size is at least 4K
8123 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
8124 * 1K as an artefact of legacy v5 subpage support being present in the
8125 * same QEMU executable.
8127 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
8128 void *hostaddr[maxidx];
8129 int try, i;
8130 unsigned mmu_idx = cpu_mmu_index(env, false);
8131 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
8133 for (try = 0; try < 2; try++) {
8135 for (i = 0; i < maxidx; i++) {
8136 hostaddr[i] = tlb_vaddr_to_host(env,
8137 vaddr + TARGET_PAGE_SIZE * i,
8138 1, mmu_idx);
8139 if (!hostaddr[i]) {
8140 break;
8143 if (i == maxidx) {
8144 /* If it's all in the TLB it's fair game for just writing to;
8145 * we know we don't need to update dirty status, etc.
8147 for (i = 0; i < maxidx - 1; i++) {
8148 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
8150 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
8151 return;
8153 /* OK, try a store and see if we can populate the tlb. This
8154 * might cause an exception if the memory isn't writable,
8155 * in which case we will longjmp out of here. We must for
8156 * this purpose use the actual register value passed to us
8157 * so that we get the fault address right.
8159 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETRA());
8160 /* Now we can populate the other TLB entries, if any */
8161 for (i = 0; i < maxidx; i++) {
8162 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
8163 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
8164 helper_ret_stb_mmu(env, va, 0, oi, GETRA());
8169 /* Slow path (probably attempt to do this to an I/O device or
8170 * similar, or clearing of a block of code we have translations
8171 * cached for). Just do a series of byte writes as the architecture
8172 * demands. It's not worth trying to use a cpu_physical_memory_map(),
8173 * memset(), unmap() sequence here because:
8174 * + we'd need to account for the blocksize being larger than a page
8175 * + the direct-RAM access case is almost always going to be dealt
8176 * with in the fastpath code above, so there's no speed benefit
8177 * + we would have to deal with the map returning NULL because the
8178 * bounce buffer was in use
8180 for (i = 0; i < blocklen; i++) {
8181 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETRA());
8184 #else
8185 memset(g2h(vaddr), 0, blocklen);
8186 #endif
8189 /* Note that signed overflow is undefined in C. The following routines are
8190 careful to use unsigned types where modulo arithmetic is required.
8191 Failure to do so _will_ break on newer gcc. */
8193 /* Signed saturating arithmetic. */
8195 /* Perform 16-bit signed saturating addition. */
8196 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
8198 uint16_t res;
8200 res = a + b;
8201 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
8202 if (a & 0x8000)
8203 res = 0x8000;
8204 else
8205 res = 0x7fff;
8207 return res;
8210 /* Perform 8-bit signed saturating addition. */
8211 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
8213 uint8_t res;
8215 res = a + b;
8216 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
8217 if (a & 0x80)
8218 res = 0x80;
8219 else
8220 res = 0x7f;
8222 return res;
8225 /* Perform 16-bit signed saturating subtraction. */
8226 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
8228 uint16_t res;
8230 res = a - b;
8231 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
8232 if (a & 0x8000)
8233 res = 0x8000;
8234 else
8235 res = 0x7fff;
8237 return res;
8240 /* Perform 8-bit signed saturating subtraction. */
8241 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
8243 uint8_t res;
8245 res = a - b;
8246 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
8247 if (a & 0x80)
8248 res = 0x80;
8249 else
8250 res = 0x7f;
8252 return res;
8255 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
8256 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
8257 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
8258 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
8259 #define PFX q
8261 #include "op_addsub.h"
8263 /* Unsigned saturating arithmetic. */
8264 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
8266 uint16_t res;
8267 res = a + b;
8268 if (res < a)
8269 res = 0xffff;
8270 return res;
8273 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
8275 if (a > b)
8276 return a - b;
8277 else
8278 return 0;
8281 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
8283 uint8_t res;
8284 res = a + b;
8285 if (res < a)
8286 res = 0xff;
8287 return res;
8290 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
8292 if (a > b)
8293 return a - b;
8294 else
8295 return 0;
8298 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
8299 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
8300 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
8301 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
8302 #define PFX uq
8304 #include "op_addsub.h"
8306 /* Signed modulo arithmetic. */
8307 #define SARITH16(a, b, n, op) do { \
8308 int32_t sum; \
8309 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
8310 RESULT(sum, n, 16); \
8311 if (sum >= 0) \
8312 ge |= 3 << (n * 2); \
8313 } while(0)
8315 #define SARITH8(a, b, n, op) do { \
8316 int32_t sum; \
8317 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
8318 RESULT(sum, n, 8); \
8319 if (sum >= 0) \
8320 ge |= 1 << n; \
8321 } while(0)
8324 #define ADD16(a, b, n) SARITH16(a, b, n, +)
8325 #define SUB16(a, b, n) SARITH16(a, b, n, -)
8326 #define ADD8(a, b, n) SARITH8(a, b, n, +)
8327 #define SUB8(a, b, n) SARITH8(a, b, n, -)
8328 #define PFX s
8329 #define ARITH_GE
8331 #include "op_addsub.h"
8333 /* Unsigned modulo arithmetic. */
8334 #define ADD16(a, b, n) do { \
8335 uint32_t sum; \
8336 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
8337 RESULT(sum, n, 16); \
8338 if ((sum >> 16) == 1) \
8339 ge |= 3 << (n * 2); \
8340 } while(0)
8342 #define ADD8(a, b, n) do { \
8343 uint32_t sum; \
8344 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
8345 RESULT(sum, n, 8); \
8346 if ((sum >> 8) == 1) \
8347 ge |= 1 << n; \
8348 } while(0)
8350 #define SUB16(a, b, n) do { \
8351 uint32_t sum; \
8352 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
8353 RESULT(sum, n, 16); \
8354 if ((sum >> 16) == 0) \
8355 ge |= 3 << (n * 2); \
8356 } while(0)
8358 #define SUB8(a, b, n) do { \
8359 uint32_t sum; \
8360 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
8361 RESULT(sum, n, 8); \
8362 if ((sum >> 8) == 0) \
8363 ge |= 1 << n; \
8364 } while(0)
8366 #define PFX u
8367 #define ARITH_GE
8369 #include "op_addsub.h"
8371 /* Halved signed arithmetic. */
8372 #define ADD16(a, b, n) \
8373 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
8374 #define SUB16(a, b, n) \
8375 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
8376 #define ADD8(a, b, n) \
8377 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
8378 #define SUB8(a, b, n) \
8379 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
8380 #define PFX sh
8382 #include "op_addsub.h"
8384 /* Halved unsigned arithmetic. */
8385 #define ADD16(a, b, n) \
8386 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
8387 #define SUB16(a, b, n) \
8388 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
8389 #define ADD8(a, b, n) \
8390 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
8391 #define SUB8(a, b, n) \
8392 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
8393 #define PFX uh
8395 #include "op_addsub.h"
8397 static inline uint8_t do_usad(uint8_t a, uint8_t b)
8399 if (a > b)
8400 return a - b;
8401 else
8402 return b - a;
8405 /* Unsigned sum of absolute byte differences. */
8406 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
8408 uint32_t sum;
8409 sum = do_usad(a, b);
8410 sum += do_usad(a >> 8, b >> 8);
8411 sum += do_usad(a >> 16, b >>16);
8412 sum += do_usad(a >> 24, b >> 24);
8413 return sum;
8416 /* For ARMv6 SEL instruction. */
8417 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
8419 uint32_t mask;
8421 mask = 0;
8422 if (flags & 1)
8423 mask |= 0xff;
8424 if (flags & 2)
8425 mask |= 0xff00;
8426 if (flags & 4)
8427 mask |= 0xff0000;
8428 if (flags & 8)
8429 mask |= 0xff000000;
8430 return (a & mask) | (b & ~mask);
8433 /* VFP support. We follow the convention used for VFP instructions:
8434 Single precision routines have a "s" suffix, double precision a
8435 "d" suffix. */
8437 /* Convert host exception flags to vfp form. */
8438 static inline int vfp_exceptbits_from_host(int host_bits)
8440 int target_bits = 0;
8442 if (host_bits & float_flag_invalid)
8443 target_bits |= 1;
8444 if (host_bits & float_flag_divbyzero)
8445 target_bits |= 2;
8446 if (host_bits & float_flag_overflow)
8447 target_bits |= 4;
8448 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
8449 target_bits |= 8;
8450 if (host_bits & float_flag_inexact)
8451 target_bits |= 0x10;
8452 if (host_bits & float_flag_input_denormal)
8453 target_bits |= 0x80;
8454 return target_bits;
8457 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
8459 int i;
8460 uint32_t fpscr;
8462 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
8463 | (env->vfp.vec_len << 16)
8464 | (env->vfp.vec_stride << 20);
8465 i = get_float_exception_flags(&env->vfp.fp_status);
8466 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
8467 fpscr |= vfp_exceptbits_from_host(i);
8468 return fpscr;
8471 uint32_t vfp_get_fpscr(CPUARMState *env)
8473 return HELPER(vfp_get_fpscr)(env);
8476 /* Convert vfp exception flags to target form. */
8477 static inline int vfp_exceptbits_to_host(int target_bits)
8479 int host_bits = 0;
8481 if (target_bits & 1)
8482 host_bits |= float_flag_invalid;
8483 if (target_bits & 2)
8484 host_bits |= float_flag_divbyzero;
8485 if (target_bits & 4)
8486 host_bits |= float_flag_overflow;
8487 if (target_bits & 8)
8488 host_bits |= float_flag_underflow;
8489 if (target_bits & 0x10)
8490 host_bits |= float_flag_inexact;
8491 if (target_bits & 0x80)
8492 host_bits |= float_flag_input_denormal;
8493 return host_bits;
8496 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
8498 int i;
8499 uint32_t changed;
8501 changed = env->vfp.xregs[ARM_VFP_FPSCR];
8502 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
8503 env->vfp.vec_len = (val >> 16) & 7;
8504 env->vfp.vec_stride = (val >> 20) & 3;
8506 changed ^= val;
8507 if (changed & (3 << 22)) {
8508 i = (val >> 22) & 3;
8509 switch (i) {
8510 case FPROUNDING_TIEEVEN:
8511 i = float_round_nearest_even;
8512 break;
8513 case FPROUNDING_POSINF:
8514 i = float_round_up;
8515 break;
8516 case FPROUNDING_NEGINF:
8517 i = float_round_down;
8518 break;
8519 case FPROUNDING_ZERO:
8520 i = float_round_to_zero;
8521 break;
8523 set_float_rounding_mode(i, &env->vfp.fp_status);
8525 if (changed & (1 << 24)) {
8526 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
8527 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
8529 if (changed & (1 << 25))
8530 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
8532 i = vfp_exceptbits_to_host(val);
8533 set_float_exception_flags(i, &env->vfp.fp_status);
8534 set_float_exception_flags(0, &env->vfp.standard_fp_status);
8537 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
8539 HELPER(vfp_set_fpscr)(env, val);
8542 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
8544 #define VFP_BINOP(name) \
8545 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
8547 float_status *fpst = fpstp; \
8548 return float32_ ## name(a, b, fpst); \
8550 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
8552 float_status *fpst = fpstp; \
8553 return float64_ ## name(a, b, fpst); \
8555 VFP_BINOP(add)
8556 VFP_BINOP(sub)
8557 VFP_BINOP(mul)
8558 VFP_BINOP(div)
8559 VFP_BINOP(min)
8560 VFP_BINOP(max)
8561 VFP_BINOP(minnum)
8562 VFP_BINOP(maxnum)
8563 #undef VFP_BINOP
8565 float32 VFP_HELPER(neg, s)(float32 a)
8567 return float32_chs(a);
8570 float64 VFP_HELPER(neg, d)(float64 a)
8572 return float64_chs(a);
8575 float32 VFP_HELPER(abs, s)(float32 a)
8577 return float32_abs(a);
8580 float64 VFP_HELPER(abs, d)(float64 a)
8582 return float64_abs(a);
8585 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
8587 return float32_sqrt(a, &env->vfp.fp_status);
8590 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
8592 return float64_sqrt(a, &env->vfp.fp_status);
8595 /* XXX: check quiet/signaling case */
8596 #define DO_VFP_cmp(p, type) \
8597 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
8599 uint32_t flags; \
8600 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
8601 case 0: flags = 0x6; break; \
8602 case -1: flags = 0x8; break; \
8603 case 1: flags = 0x2; break; \
8604 default: case 2: flags = 0x3; break; \
8606 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
8607 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
8609 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
8611 uint32_t flags; \
8612 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
8613 case 0: flags = 0x6; break; \
8614 case -1: flags = 0x8; break; \
8615 case 1: flags = 0x2; break; \
8616 default: case 2: flags = 0x3; break; \
8618 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
8619 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
8621 DO_VFP_cmp(s, float32)
8622 DO_VFP_cmp(d, float64)
8623 #undef DO_VFP_cmp
8625 /* Integer to float and float to integer conversions */
8627 #define CONV_ITOF(name, fsz, sign) \
8628 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
8630 float_status *fpst = fpstp; \
8631 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
8634 #define CONV_FTOI(name, fsz, sign, round) \
8635 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
8637 float_status *fpst = fpstp; \
8638 if (float##fsz##_is_any_nan(x)) { \
8639 float_raise(float_flag_invalid, fpst); \
8640 return 0; \
8642 return float##fsz##_to_##sign##int32##round(x, fpst); \
8645 #define FLOAT_CONVS(name, p, fsz, sign) \
8646 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
8647 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
8648 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
8650 FLOAT_CONVS(si, s, 32, )
8651 FLOAT_CONVS(si, d, 64, )
8652 FLOAT_CONVS(ui, s, 32, u)
8653 FLOAT_CONVS(ui, d, 64, u)
8655 #undef CONV_ITOF
8656 #undef CONV_FTOI
8657 #undef FLOAT_CONVS
8659 /* floating point conversion */
8660 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
8662 float64 r = float32_to_float64(x, &env->vfp.fp_status);
8663 /* ARM requires that S<->D conversion of any kind of NaN generates
8664 * a quiet NaN by forcing the most significant frac bit to 1.
8666 return float64_maybe_silence_nan(r);
8669 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
8671 float32 r = float64_to_float32(x, &env->vfp.fp_status);
8672 /* ARM requires that S<->D conversion of any kind of NaN generates
8673 * a quiet NaN by forcing the most significant frac bit to 1.
8675 return float32_maybe_silence_nan(r);
8678 /* VFP3 fixed point conversion. */
8679 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8680 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
8681 void *fpstp) \
8683 float_status *fpst = fpstp; \
8684 float##fsz tmp; \
8685 tmp = itype##_to_##float##fsz(x, fpst); \
8686 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
8689 /* Notice that we want only input-denormal exception flags from the
8690 * scalbn operation: the other possible flags (overflow+inexact if
8691 * we overflow to infinity, output-denormal) aren't correct for the
8692 * complete scale-and-convert operation.
8694 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
8695 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
8696 uint32_t shift, \
8697 void *fpstp) \
8699 float_status *fpst = fpstp; \
8700 int old_exc_flags = get_float_exception_flags(fpst); \
8701 float##fsz tmp; \
8702 if (float##fsz##_is_any_nan(x)) { \
8703 float_raise(float_flag_invalid, fpst); \
8704 return 0; \
8706 tmp = float##fsz##_scalbn(x, shift, fpst); \
8707 old_exc_flags |= get_float_exception_flags(fpst) \
8708 & float_flag_input_denormal; \
8709 set_float_exception_flags(old_exc_flags, fpst); \
8710 return float##fsz##_to_##itype##round(tmp, fpst); \
8713 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \
8714 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8715 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
8716 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
8718 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
8719 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8720 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
8722 VFP_CONV_FIX(sh, d, 64, 64, int16)
8723 VFP_CONV_FIX(sl, d, 64, 64, int32)
8724 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8725 VFP_CONV_FIX(uh, d, 64, 64, uint16)
8726 VFP_CONV_FIX(ul, d, 64, 64, uint32)
8727 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8728 VFP_CONV_FIX(sh, s, 32, 32, int16)
8729 VFP_CONV_FIX(sl, s, 32, 32, int32)
8730 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8731 VFP_CONV_FIX(uh, s, 32, 32, uint16)
8732 VFP_CONV_FIX(ul, s, 32, 32, uint32)
8733 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
8734 #undef VFP_CONV_FIX
8735 #undef VFP_CONV_FIX_FLOAT
8736 #undef VFP_CONV_FLOAT_FIX_ROUND
8738 /* Set the current fp rounding mode and return the old one.
8739 * The argument is a softfloat float_round_ value.
8741 uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
8743 float_status *fp_status = &env->vfp.fp_status;
8745 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
8746 set_float_rounding_mode(rmode, fp_status);
8748 return prev_rmode;
8751 /* Set the current fp rounding mode in the standard fp status and return
8752 * the old one. This is for NEON instructions that need to change the
8753 * rounding mode but wish to use the standard FPSCR values for everything
8754 * else. Always set the rounding mode back to the correct value after
8755 * modifying it.
8756 * The argument is a softfloat float_round_ value.
8758 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
8760 float_status *fp_status = &env->vfp.standard_fp_status;
8762 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
8763 set_float_rounding_mode(rmode, fp_status);
8765 return prev_rmode;
8768 /* Half precision conversions. */
8769 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
8771 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8772 float32 r = float16_to_float32(make_float16(a), ieee, s);
8773 if (ieee) {
8774 return float32_maybe_silence_nan(r);
8776 return r;
8779 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
8781 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8782 float16 r = float32_to_float16(a, ieee, s);
8783 if (ieee) {
8784 r = float16_maybe_silence_nan(r);
8786 return float16_val(r);
8789 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
8791 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
8794 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
8796 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
8799 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
8801 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
8804 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
8806 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
8809 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
8811 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8812 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
8813 if (ieee) {
8814 return float64_maybe_silence_nan(r);
8816 return r;
8819 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
8821 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8822 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
8823 if (ieee) {
8824 r = float16_maybe_silence_nan(r);
8826 return float16_val(r);
8829 #define float32_two make_float32(0x40000000)
8830 #define float32_three make_float32(0x40400000)
8831 #define float32_one_point_five make_float32(0x3fc00000)
8833 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
8835 float_status *s = &env->vfp.standard_fp_status;
8836 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
8837 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
8838 if (!(float32_is_zero(a) || float32_is_zero(b))) {
8839 float_raise(float_flag_input_denormal, s);
8841 return float32_two;
8843 return float32_sub(float32_two, float32_mul(a, b, s), s);
8846 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
8848 float_status *s = &env->vfp.standard_fp_status;
8849 float32 product;
8850 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
8851 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
8852 if (!(float32_is_zero(a) || float32_is_zero(b))) {
8853 float_raise(float_flag_input_denormal, s);
8855 return float32_one_point_five;
8857 product = float32_mul(a, b, s);
8858 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
8861 /* NEON helpers. */
8863 /* Constants 256 and 512 are used in some helpers; we avoid relying on
8864 * int->float conversions at run-time. */
8865 #define float64_256 make_float64(0x4070000000000000LL)
8866 #define float64_512 make_float64(0x4080000000000000LL)
8867 #define float32_maxnorm make_float32(0x7f7fffff)
8868 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
8870 /* Reciprocal functions
8872 * The algorithm that must be used to calculate the estimate
8873 * is specified by the ARM ARM, see FPRecipEstimate()
8876 static float64 recip_estimate(float64 a, float_status *real_fp_status)
8878 /* These calculations mustn't set any fp exception flags,
8879 * so we use a local copy of the fp_status.
8881 float_status dummy_status = *real_fp_status;
8882 float_status *s = &dummy_status;
8883 /* q = (int)(a * 512.0) */
8884 float64 q = float64_mul(float64_512, a, s);
8885 int64_t q_int = float64_to_int64_round_to_zero(q, s);
8887 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
8888 q = int64_to_float64(q_int, s);
8889 q = float64_add(q, float64_half, s);
8890 q = float64_div(q, float64_512, s);
8891 q = float64_div(float64_one, q, s);
8893 /* s = (int)(256.0 * r + 0.5) */
8894 q = float64_mul(q, float64_256, s);
8895 q = float64_add(q, float64_half, s);
8896 q_int = float64_to_int64_round_to_zero(q, s);
8898 /* return (double)s / 256.0 */
8899 return float64_div(int64_to_float64(q_int, s), float64_256, s);
8902 /* Common wrapper to call recip_estimate */
8903 static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
8905 uint64_t val64 = float64_val(num);
8906 uint64_t frac = extract64(val64, 0, 52);
8907 int64_t exp = extract64(val64, 52, 11);
8908 uint64_t sbit;
8909 float64 scaled, estimate;
8911 /* Generate the scaled number for the estimate function */
8912 if (exp == 0) {
8913 if (extract64(frac, 51, 1) == 0) {
8914 exp = -1;
8915 frac = extract64(frac, 0, 50) << 2;
8916 } else {
8917 frac = extract64(frac, 0, 51) << 1;
8921 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
8922 scaled = make_float64((0x3feULL << 52)
8923 | extract64(frac, 44, 8) << 44);
8925 estimate = recip_estimate(scaled, fpst);
8927 /* Build new result */
8928 val64 = float64_val(estimate);
8929 sbit = 0x8000000000000000ULL & val64;
8930 exp = off - exp;
8931 frac = extract64(val64, 0, 52);
8933 if (exp == 0) {
8934 frac = 1ULL << 51 | extract64(frac, 1, 51);
8935 } else if (exp == -1) {
8936 frac = 1ULL << 50 | extract64(frac, 2, 50);
8937 exp = 0;
8940 return make_float64(sbit | (exp << 52) | frac);
8943 static bool round_to_inf(float_status *fpst, bool sign_bit)
8945 switch (fpst->float_rounding_mode) {
8946 case float_round_nearest_even: /* Round to Nearest */
8947 return true;
8948 case float_round_up: /* Round to +Inf */
8949 return !sign_bit;
8950 case float_round_down: /* Round to -Inf */
8951 return sign_bit;
8952 case float_round_to_zero: /* Round to Zero */
8953 return false;
8956 g_assert_not_reached();
8959 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
8961 float_status *fpst = fpstp;
8962 float32 f32 = float32_squash_input_denormal(input, fpst);
8963 uint32_t f32_val = float32_val(f32);
8964 uint32_t f32_sbit = 0x80000000ULL & f32_val;
8965 int32_t f32_exp = extract32(f32_val, 23, 8);
8966 uint32_t f32_frac = extract32(f32_val, 0, 23);
8967 float64 f64, r64;
8968 uint64_t r64_val;
8969 int64_t r64_exp;
8970 uint64_t r64_frac;
8972 if (float32_is_any_nan(f32)) {
8973 float32 nan = f32;
8974 if (float32_is_signaling_nan(f32)) {
8975 float_raise(float_flag_invalid, fpst);
8976 nan = float32_maybe_silence_nan(f32);
8978 if (fpst->default_nan_mode) {
8979 nan = float32_default_nan;
8981 return nan;
8982 } else if (float32_is_infinity(f32)) {
8983 return float32_set_sign(float32_zero, float32_is_neg(f32));
8984 } else if (float32_is_zero(f32)) {
8985 float_raise(float_flag_divbyzero, fpst);
8986 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8987 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
8988 /* Abs(value) < 2.0^-128 */
8989 float_raise(float_flag_overflow | float_flag_inexact, fpst);
8990 if (round_to_inf(fpst, f32_sbit)) {
8991 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8992 } else {
8993 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
8995 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
8996 float_raise(float_flag_underflow, fpst);
8997 return float32_set_sign(float32_zero, float32_is_neg(f32));
9001 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
9002 r64 = call_recip_estimate(f64, 253, fpst);
9003 r64_val = float64_val(r64);
9004 r64_exp = extract64(r64_val, 52, 11);
9005 r64_frac = extract64(r64_val, 0, 52);
9007 /* result = sign : result_exp<7:0> : fraction<51:29>; */
9008 return make_float32(f32_sbit |
9009 (r64_exp & 0xff) << 23 |
9010 extract64(r64_frac, 29, 24));
9013 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
9015 float_status *fpst = fpstp;
9016 float64 f64 = float64_squash_input_denormal(input, fpst);
9017 uint64_t f64_val = float64_val(f64);
9018 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
9019 int64_t f64_exp = extract64(f64_val, 52, 11);
9020 float64 r64;
9021 uint64_t r64_val;
9022 int64_t r64_exp;
9023 uint64_t r64_frac;
9025 /* Deal with any special cases */
9026 if (float64_is_any_nan(f64)) {
9027 float64 nan = f64;
9028 if (float64_is_signaling_nan(f64)) {
9029 float_raise(float_flag_invalid, fpst);
9030 nan = float64_maybe_silence_nan(f64);
9032 if (fpst->default_nan_mode) {
9033 nan = float64_default_nan;
9035 return nan;
9036 } else if (float64_is_infinity(f64)) {
9037 return float64_set_sign(float64_zero, float64_is_neg(f64));
9038 } else if (float64_is_zero(f64)) {
9039 float_raise(float_flag_divbyzero, fpst);
9040 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9041 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
9042 /* Abs(value) < 2.0^-1024 */
9043 float_raise(float_flag_overflow | float_flag_inexact, fpst);
9044 if (round_to_inf(fpst, f64_sbit)) {
9045 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9046 } else {
9047 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
9049 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
9050 float_raise(float_flag_underflow, fpst);
9051 return float64_set_sign(float64_zero, float64_is_neg(f64));
9054 r64 = call_recip_estimate(f64, 2045, fpst);
9055 r64_val = float64_val(r64);
9056 r64_exp = extract64(r64_val, 52, 11);
9057 r64_frac = extract64(r64_val, 0, 52);
9059 /* result = sign : result_exp<10:0> : fraction<51:0> */
9060 return make_float64(f64_sbit |
9061 ((r64_exp & 0x7ff) << 52) |
9062 r64_frac);
9065 /* The algorithm that must be used to calculate the estimate
9066 * is specified by the ARM ARM.
9068 static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
9070 /* These calculations mustn't set any fp exception flags,
9071 * so we use a local copy of the fp_status.
9073 float_status dummy_status = *real_fp_status;
9074 float_status *s = &dummy_status;
9075 float64 q;
9076 int64_t q_int;
9078 if (float64_lt(a, float64_half, s)) {
9079 /* range 0.25 <= a < 0.5 */
9081 /* a in units of 1/512 rounded down */
9082 /* q0 = (int)(a * 512.0); */
9083 q = float64_mul(float64_512, a, s);
9084 q_int = float64_to_int64_round_to_zero(q, s);
9086 /* reciprocal root r */
9087 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
9088 q = int64_to_float64(q_int, s);
9089 q = float64_add(q, float64_half, s);
9090 q = float64_div(q, float64_512, s);
9091 q = float64_sqrt(q, s);
9092 q = float64_div(float64_one, q, s);
9093 } else {
9094 /* range 0.5 <= a < 1.0 */
9096 /* a in units of 1/256 rounded down */
9097 /* q1 = (int)(a * 256.0); */
9098 q = float64_mul(float64_256, a, s);
9099 int64_t q_int = float64_to_int64_round_to_zero(q, s);
9101 /* reciprocal root r */
9102 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
9103 q = int64_to_float64(q_int, s);
9104 q = float64_add(q, float64_half, s);
9105 q = float64_div(q, float64_256, s);
9106 q = float64_sqrt(q, s);
9107 q = float64_div(float64_one, q, s);
9109 /* r in units of 1/256 rounded to nearest */
9110 /* s = (int)(256.0 * r + 0.5); */
9112 q = float64_mul(q, float64_256,s );
9113 q = float64_add(q, float64_half, s);
9114 q_int = float64_to_int64_round_to_zero(q, s);
9116 /* return (double)s / 256.0;*/
9117 return float64_div(int64_to_float64(q_int, s), float64_256, s);
9120 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
9122 float_status *s = fpstp;
9123 float32 f32 = float32_squash_input_denormal(input, s);
9124 uint32_t val = float32_val(f32);
9125 uint32_t f32_sbit = 0x80000000 & val;
9126 int32_t f32_exp = extract32(val, 23, 8);
9127 uint32_t f32_frac = extract32(val, 0, 23);
9128 uint64_t f64_frac;
9129 uint64_t val64;
9130 int result_exp;
9131 float64 f64;
9133 if (float32_is_any_nan(f32)) {
9134 float32 nan = f32;
9135 if (float32_is_signaling_nan(f32)) {
9136 float_raise(float_flag_invalid, s);
9137 nan = float32_maybe_silence_nan(f32);
9139 if (s->default_nan_mode) {
9140 nan = float32_default_nan;
9142 return nan;
9143 } else if (float32_is_zero(f32)) {
9144 float_raise(float_flag_divbyzero, s);
9145 return float32_set_sign(float32_infinity, float32_is_neg(f32));
9146 } else if (float32_is_neg(f32)) {
9147 float_raise(float_flag_invalid, s);
9148 return float32_default_nan;
9149 } else if (float32_is_infinity(f32)) {
9150 return float32_zero;
9153 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
9154 * preserving the parity of the exponent. */
9156 f64_frac = ((uint64_t) f32_frac) << 29;
9157 if (f32_exp == 0) {
9158 while (extract64(f64_frac, 51, 1) == 0) {
9159 f64_frac = f64_frac << 1;
9160 f32_exp = f32_exp-1;
9162 f64_frac = extract64(f64_frac, 0, 51) << 1;
9165 if (extract64(f32_exp, 0, 1) == 0) {
9166 f64 = make_float64(((uint64_t) f32_sbit) << 32
9167 | (0x3feULL << 52)
9168 | f64_frac);
9169 } else {
9170 f64 = make_float64(((uint64_t) f32_sbit) << 32
9171 | (0x3fdULL << 52)
9172 | f64_frac);
9175 result_exp = (380 - f32_exp) / 2;
9177 f64 = recip_sqrt_estimate(f64, s);
9179 val64 = float64_val(f64);
9181 val = ((result_exp & 0xff) << 23)
9182 | ((val64 >> 29) & 0x7fffff);
9183 return make_float32(val);
9186 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
9188 float_status *s = fpstp;
9189 float64 f64 = float64_squash_input_denormal(input, s);
9190 uint64_t val = float64_val(f64);
9191 uint64_t f64_sbit = 0x8000000000000000ULL & val;
9192 int64_t f64_exp = extract64(val, 52, 11);
9193 uint64_t f64_frac = extract64(val, 0, 52);
9194 int64_t result_exp;
9195 uint64_t result_frac;
9197 if (float64_is_any_nan(f64)) {
9198 float64 nan = f64;
9199 if (float64_is_signaling_nan(f64)) {
9200 float_raise(float_flag_invalid, s);
9201 nan = float64_maybe_silence_nan(f64);
9203 if (s->default_nan_mode) {
9204 nan = float64_default_nan;
9206 return nan;
9207 } else if (float64_is_zero(f64)) {
9208 float_raise(float_flag_divbyzero, s);
9209 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9210 } else if (float64_is_neg(f64)) {
9211 float_raise(float_flag_invalid, s);
9212 return float64_default_nan;
9213 } else if (float64_is_infinity(f64)) {
9214 return float64_zero;
9217 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
9218 * preserving the parity of the exponent. */
9220 if (f64_exp == 0) {
9221 while (extract64(f64_frac, 51, 1) == 0) {
9222 f64_frac = f64_frac << 1;
9223 f64_exp = f64_exp - 1;
9225 f64_frac = extract64(f64_frac, 0, 51) << 1;
9228 if (extract64(f64_exp, 0, 1) == 0) {
9229 f64 = make_float64(f64_sbit
9230 | (0x3feULL << 52)
9231 | f64_frac);
9232 } else {
9233 f64 = make_float64(f64_sbit
9234 | (0x3fdULL << 52)
9235 | f64_frac);
9238 result_exp = (3068 - f64_exp) / 2;
9240 f64 = recip_sqrt_estimate(f64, s);
9242 result_frac = extract64(float64_val(f64), 0, 52);
9244 return make_float64(f64_sbit |
9245 ((result_exp & 0x7ff) << 52) |
9246 result_frac);
9249 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
9251 float_status *s = fpstp;
9252 float64 f64;
9254 if ((a & 0x80000000) == 0) {
9255 return 0xffffffff;
9258 f64 = make_float64((0x3feULL << 52)
9259 | ((int64_t)(a & 0x7fffffff) << 21));
9261 f64 = recip_estimate(f64, s);
9263 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
9266 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
9268 float_status *fpst = fpstp;
9269 float64 f64;
9271 if ((a & 0xc0000000) == 0) {
9272 return 0xffffffff;
9275 if (a & 0x80000000) {
9276 f64 = make_float64((0x3feULL << 52)
9277 | ((uint64_t)(a & 0x7fffffff) << 21));
9278 } else { /* bits 31-30 == '01' */
9279 f64 = make_float64((0x3fdULL << 52)
9280 | ((uint64_t)(a & 0x3fffffff) << 22));
9283 f64 = recip_sqrt_estimate(f64, fpst);
9285 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
9288 /* VFPv4 fused multiply-accumulate */
9289 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
9291 float_status *fpst = fpstp;
9292 return float32_muladd(a, b, c, 0, fpst);
9295 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
9297 float_status *fpst = fpstp;
9298 return float64_muladd(a, b, c, 0, fpst);
9301 /* ARMv8 round to integral */
9302 float32 HELPER(rints_exact)(float32 x, void *fp_status)
9304 return float32_round_to_int(x, fp_status);
9307 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
9309 return float64_round_to_int(x, fp_status);
9312 float32 HELPER(rints)(float32 x, void *fp_status)
9314 int old_flags = get_float_exception_flags(fp_status), new_flags;
9315 float32 ret;
9317 ret = float32_round_to_int(x, fp_status);
9319 /* Suppress any inexact exceptions the conversion produced */
9320 if (!(old_flags & float_flag_inexact)) {
9321 new_flags = get_float_exception_flags(fp_status);
9322 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
9325 return ret;
9328 float64 HELPER(rintd)(float64 x, void *fp_status)
9330 int old_flags = get_float_exception_flags(fp_status), new_flags;
9331 float64 ret;
9333 ret = float64_round_to_int(x, fp_status);
9335 new_flags = get_float_exception_flags(fp_status);
9337 /* Suppress any inexact exceptions the conversion produced */
9338 if (!(old_flags & float_flag_inexact)) {
9339 new_flags = get_float_exception_flags(fp_status);
9340 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
9343 return ret;
9346 /* Convert ARM rounding mode to softfloat */
9347 int arm_rmode_to_sf(int rmode)
9349 switch (rmode) {
9350 case FPROUNDING_TIEAWAY:
9351 rmode = float_round_ties_away;
9352 break;
9353 case FPROUNDING_ODD:
9354 /* FIXME: add support for TIEAWAY and ODD */
9355 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
9356 rmode);
9357 case FPROUNDING_TIEEVEN:
9358 default:
9359 rmode = float_round_nearest_even;
9360 break;
9361 case FPROUNDING_POSINF:
9362 rmode = float_round_up;
9363 break;
9364 case FPROUNDING_NEGINF:
9365 rmode = float_round_down;
9366 break;
9367 case FPROUNDING_ZERO:
9368 rmode = float_round_to_zero;
9369 break;
9371 return rmode;
9374 /* CRC helpers.
9375 * The upper bytes of val (above the number specified by 'bytes') must have
9376 * been zeroed out by the caller.
9378 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
9380 uint8_t buf[4];
9382 stl_le_p(buf, val);
9384 /* zlib crc32 converts the accumulator and output to one's complement. */
9385 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
9388 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
9390 uint8_t buf[4];
9392 stl_le_p(buf, val);
9394 /* Linux crc32c converts the output to one's complement. */
9395 return crc32c(acc, buf, bytes) ^ 0xffffffff;