target-arm: Add the pmovsclr_el0 and pmintenclr_el1 registers
[qemu/ar7.git] / target-arm / helper.c
blob9e47f3d8cb43e6d8faf076fb87d593d407fce806
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/cpu_ldst.h"
12 #include "arm_ldst.h"
13 #include <zlib.h> /* For crc32 */
14 #include "exec/semihost.h"
15 #include "sysemu/kvm.h"
17 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
19 #ifndef CONFIG_USER_ONLY
20 static bool get_phys_addr(CPUARMState *env, target_ulong address,
21 int access_type, ARMMMUIdx mmu_idx,
22 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
23 target_ulong *page_size, uint32_t *fsr,
24 ARMMMUFaultInfo *fi);
26 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
27 int access_type, ARMMMUIdx mmu_idx,
28 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
29 target_ulong *page_size_ptr, uint32_t *fsr,
30 ARMMMUFaultInfo *fi);
32 /* Definitions for the PMCCNTR and PMCR registers */
33 #define PMCRD 0x8
34 #define PMCRC 0x4
35 #define PMCRE 0x1
36 #endif
38 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
40 int nregs;
42 /* VFP data registers are always little-endian. */
43 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
44 if (reg < nregs) {
45 stfq_le_p(buf, env->vfp.regs[reg]);
46 return 8;
48 if (arm_feature(env, ARM_FEATURE_NEON)) {
49 /* Aliases for Q regs. */
50 nregs += 16;
51 if (reg < nregs) {
52 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
53 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
54 return 16;
57 switch (reg - nregs) {
58 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
59 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
60 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
62 return 0;
65 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
67 int nregs;
69 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
70 if (reg < nregs) {
71 env->vfp.regs[reg] = ldfq_le_p(buf);
72 return 8;
74 if (arm_feature(env, ARM_FEATURE_NEON)) {
75 nregs += 16;
76 if (reg < nregs) {
77 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
78 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
79 return 16;
82 switch (reg - nregs) {
83 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
84 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
85 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
87 return 0;
90 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
92 switch (reg) {
93 case 0 ... 31:
94 /* 128 bit FP register */
95 stfq_le_p(buf, env->vfp.regs[reg * 2]);
96 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
97 return 16;
98 case 32:
99 /* FPSR */
100 stl_p(buf, vfp_get_fpsr(env));
101 return 4;
102 case 33:
103 /* FPCR */
104 stl_p(buf, vfp_get_fpcr(env));
105 return 4;
106 default:
107 return 0;
111 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
113 switch (reg) {
114 case 0 ... 31:
115 /* 128 bit FP register */
116 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
117 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
118 return 16;
119 case 32:
120 /* FPSR */
121 vfp_set_fpsr(env, ldl_p(buf));
122 return 4;
123 case 33:
124 /* FPCR */
125 vfp_set_fpcr(env, ldl_p(buf));
126 return 4;
127 default:
128 return 0;
132 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
134 assert(ri->fieldoffset);
135 if (cpreg_field_is_64bit(ri)) {
136 return CPREG_FIELD64(env, ri);
137 } else {
138 return CPREG_FIELD32(env, ri);
142 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
143 uint64_t value)
145 assert(ri->fieldoffset);
146 if (cpreg_field_is_64bit(ri)) {
147 CPREG_FIELD64(env, ri) = value;
148 } else {
149 CPREG_FIELD32(env, ri) = value;
153 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
155 return (char *)env + ri->fieldoffset;
158 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
160 /* Raw read of a coprocessor register (as needed for migration, etc). */
161 if (ri->type & ARM_CP_CONST) {
162 return ri->resetvalue;
163 } else if (ri->raw_readfn) {
164 return ri->raw_readfn(env, ri);
165 } else if (ri->readfn) {
166 return ri->readfn(env, ri);
167 } else {
168 return raw_read(env, ri);
172 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
173 uint64_t v)
175 /* Raw write of a coprocessor register (as needed for migration, etc).
176 * Note that constant registers are treated as write-ignored; the
177 * caller should check for success by whether a readback gives the
178 * value written.
180 if (ri->type & ARM_CP_CONST) {
181 return;
182 } else if (ri->raw_writefn) {
183 ri->raw_writefn(env, ri, v);
184 } else if (ri->writefn) {
185 ri->writefn(env, ri, v);
186 } else {
187 raw_write(env, ri, v);
191 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
193 /* Return true if the regdef would cause an assertion if you called
194 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
195 * program bug for it not to have the NO_RAW flag).
196 * NB that returning false here doesn't necessarily mean that calling
197 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
198 * read/write access functions which are safe for raw use" from "has
199 * read/write access functions which have side effects but has forgotten
200 * to provide raw access functions".
201 * The tests here line up with the conditions in read/write_raw_cp_reg()
202 * and assertions in raw_read()/raw_write().
204 if ((ri->type & ARM_CP_CONST) ||
205 ri->fieldoffset ||
206 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
207 return false;
209 return true;
212 bool write_cpustate_to_list(ARMCPU *cpu)
214 /* Write the coprocessor state from cpu->env to the (index,value) list. */
215 int i;
216 bool ok = true;
218 for (i = 0; i < cpu->cpreg_array_len; i++) {
219 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
220 const ARMCPRegInfo *ri;
222 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
223 if (!ri) {
224 ok = false;
225 continue;
227 if (ri->type & ARM_CP_NO_RAW) {
228 continue;
230 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
232 return ok;
235 bool write_list_to_cpustate(ARMCPU *cpu)
237 int i;
238 bool ok = true;
240 for (i = 0; i < cpu->cpreg_array_len; i++) {
241 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
242 uint64_t v = cpu->cpreg_values[i];
243 const ARMCPRegInfo *ri;
245 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
246 if (!ri) {
247 ok = false;
248 continue;
250 if (ri->type & ARM_CP_NO_RAW) {
251 continue;
253 /* Write value and confirm it reads back as written
254 * (to catch read-only registers and partially read-only
255 * registers where the incoming migration value doesn't match)
257 write_raw_cp_reg(&cpu->env, ri, v);
258 if (read_raw_cp_reg(&cpu->env, ri) != v) {
259 ok = false;
262 return ok;
265 static void add_cpreg_to_list(gpointer key, gpointer opaque)
267 ARMCPU *cpu = opaque;
268 uint64_t regidx;
269 const ARMCPRegInfo *ri;
271 regidx = *(uint32_t *)key;
272 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
274 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
275 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
276 /* The value array need not be initialized at this point */
277 cpu->cpreg_array_len++;
281 static void count_cpreg(gpointer key, gpointer opaque)
283 ARMCPU *cpu = opaque;
284 uint64_t regidx;
285 const ARMCPRegInfo *ri;
287 regidx = *(uint32_t *)key;
288 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
290 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
291 cpu->cpreg_array_len++;
295 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
297 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
298 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
300 if (aidx > bidx) {
301 return 1;
303 if (aidx < bidx) {
304 return -1;
306 return 0;
309 void init_cpreg_list(ARMCPU *cpu)
311 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
312 * Note that we require cpreg_tuples[] to be sorted by key ID.
314 GList *keys;
315 int arraylen;
317 keys = g_hash_table_get_keys(cpu->cp_regs);
318 keys = g_list_sort(keys, cpreg_key_compare);
320 cpu->cpreg_array_len = 0;
322 g_list_foreach(keys, count_cpreg, cpu);
324 arraylen = cpu->cpreg_array_len;
325 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
326 cpu->cpreg_values = g_new(uint64_t, arraylen);
327 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
328 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
329 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
330 cpu->cpreg_array_len = 0;
332 g_list_foreach(keys, add_cpreg_to_list, cpu);
334 assert(cpu->cpreg_array_len == arraylen);
336 g_list_free(keys);
340 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
341 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
343 * access_el3_aa32ns: Used to check AArch32 register views.
344 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
346 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
347 const ARMCPRegInfo *ri,
348 bool isread)
350 bool secure = arm_is_secure_below_el3(env);
352 assert(!arm_el_is_aa64(env, 3));
353 if (secure) {
354 return CP_ACCESS_TRAP_UNCATEGORIZED;
356 return CP_ACCESS_OK;
359 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
360 const ARMCPRegInfo *ri,
361 bool isread)
363 if (!arm_el_is_aa64(env, 3)) {
364 return access_el3_aa32ns(env, ri, isread);
366 return CP_ACCESS_OK;
369 /* Some secure-only AArch32 registers trap to EL3 if used from
370 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
371 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
372 * We assume that the .access field is set to PL1_RW.
374 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
375 const ARMCPRegInfo *ri,
376 bool isread)
378 if (arm_current_el(env) == 3) {
379 return CP_ACCESS_OK;
381 if (arm_is_secure_below_el3(env)) {
382 return CP_ACCESS_TRAP_EL3;
384 /* This will be EL1 NS and EL2 NS, which just UNDEF */
385 return CP_ACCESS_TRAP_UNCATEGORIZED;
388 /* Check for traps to "powerdown debug" registers, which are controlled
389 * by MDCR.TDOSA
391 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
392 bool isread)
394 int el = arm_current_el(env);
396 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
397 && !arm_is_secure_below_el3(env)) {
398 return CP_ACCESS_TRAP_EL2;
400 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
401 return CP_ACCESS_TRAP_EL3;
403 return CP_ACCESS_OK;
406 /* Check for traps to "debug ROM" registers, which are controlled
407 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
409 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
410 bool isread)
412 int el = arm_current_el(env);
414 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
415 && !arm_is_secure_below_el3(env)) {
416 return CP_ACCESS_TRAP_EL2;
418 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
419 return CP_ACCESS_TRAP_EL3;
421 return CP_ACCESS_OK;
424 /* Check for traps to general debug registers, which are controlled
425 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
427 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
428 bool isread)
430 int el = arm_current_el(env);
432 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
433 && !arm_is_secure_below_el3(env)) {
434 return CP_ACCESS_TRAP_EL2;
436 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
437 return CP_ACCESS_TRAP_EL3;
439 return CP_ACCESS_OK;
442 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
444 ARMCPU *cpu = arm_env_get_cpu(env);
446 raw_write(env, ri, value);
447 tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
450 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
452 ARMCPU *cpu = arm_env_get_cpu(env);
454 if (raw_read(env, ri) != value) {
455 /* Unlike real hardware the qemu TLB uses virtual addresses,
456 * not modified virtual addresses, so this causes a TLB flush.
458 tlb_flush(CPU(cpu), 1);
459 raw_write(env, ri, value);
463 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
464 uint64_t value)
466 ARMCPU *cpu = arm_env_get_cpu(env);
468 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU)
469 && !extended_addresses_enabled(env)) {
470 /* For VMSA (when not using the LPAE long descriptor page table
471 * format) this register includes the ASID, so do a TLB flush.
472 * For PMSA it is purely a process ID and no action is needed.
474 tlb_flush(CPU(cpu), 1);
476 raw_write(env, ri, value);
479 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
480 uint64_t value)
482 /* Invalidate all (TLBIALL) */
483 ARMCPU *cpu = arm_env_get_cpu(env);
485 tlb_flush(CPU(cpu), 1);
488 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
489 uint64_t value)
491 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
492 ARMCPU *cpu = arm_env_get_cpu(env);
494 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
497 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
498 uint64_t value)
500 /* Invalidate by ASID (TLBIASID) */
501 ARMCPU *cpu = arm_env_get_cpu(env);
503 tlb_flush(CPU(cpu), value == 0);
506 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
507 uint64_t value)
509 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
510 ARMCPU *cpu = arm_env_get_cpu(env);
512 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
515 /* IS variants of TLB operations must affect all cores */
516 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
517 uint64_t value)
519 CPUState *other_cs;
521 CPU_FOREACH(other_cs) {
522 tlb_flush(other_cs, 1);
526 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
527 uint64_t value)
529 CPUState *other_cs;
531 CPU_FOREACH(other_cs) {
532 tlb_flush(other_cs, value == 0);
536 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
537 uint64_t value)
539 CPUState *other_cs;
541 CPU_FOREACH(other_cs) {
542 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
546 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
547 uint64_t value)
549 CPUState *other_cs;
551 CPU_FOREACH(other_cs) {
552 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
556 static const ARMCPRegInfo cp_reginfo[] = {
557 /* Define the secure and non-secure FCSE identifier CP registers
558 * separately because there is no secure bank in V8 (no _EL3). This allows
559 * the secure register to be properly reset and migrated. There is also no
560 * v8 EL1 version of the register so the non-secure instance stands alone.
562 { .name = "FCSEIDR(NS)",
563 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
564 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
565 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
566 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
567 { .name = "FCSEIDR(S)",
568 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
569 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
570 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
571 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
572 /* Define the secure and non-secure context identifier CP registers
573 * separately because there is no secure bank in V8 (no _EL3). This allows
574 * the secure register to be properly reset and migrated. In the
575 * non-secure case, the 32-bit register will have reset and migration
576 * disabled during registration as it is handled by the 64-bit instance.
578 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
579 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
580 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
581 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
582 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
583 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
584 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
585 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
586 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
587 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
588 REGINFO_SENTINEL
591 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
592 /* NB: Some of these registers exist in v8 but with more precise
593 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
595 /* MMU Domain access control / MPU write buffer control */
596 { .name = "DACR",
597 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
598 .access = PL1_RW, .resetvalue = 0,
599 .writefn = dacr_write, .raw_writefn = raw_write,
600 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
601 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
602 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
603 * For v6 and v5, these mappings are overly broad.
605 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
606 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
607 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
608 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
609 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
610 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
611 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
612 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
613 /* Cache maintenance ops; some of this space may be overridden later. */
614 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
615 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
616 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
617 REGINFO_SENTINEL
620 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
621 /* Not all pre-v6 cores implemented this WFI, so this is slightly
622 * over-broad.
624 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
625 .access = PL1_W, .type = ARM_CP_WFI },
626 REGINFO_SENTINEL
629 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
630 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
631 * is UNPREDICTABLE; we choose to NOP as most implementations do).
633 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
634 .access = PL1_W, .type = ARM_CP_WFI },
635 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
636 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
637 * OMAPCP will override this space.
639 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
640 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
641 .resetvalue = 0 },
642 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
643 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
644 .resetvalue = 0 },
645 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
646 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
647 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
648 .resetvalue = 0 },
649 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
650 * implementing it as RAZ means the "debug architecture version" bits
651 * will read as a reserved value, which should cause Linux to not try
652 * to use the debug hardware.
654 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
655 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
656 /* MMU TLB control. Note that the wildcarding means we cover not just
657 * the unified TLB ops but also the dside/iside/inner-shareable variants.
659 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
660 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
661 .type = ARM_CP_NO_RAW },
662 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
663 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
664 .type = ARM_CP_NO_RAW },
665 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
666 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
667 .type = ARM_CP_NO_RAW },
668 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
669 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
670 .type = ARM_CP_NO_RAW },
671 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
672 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
673 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
674 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
675 REGINFO_SENTINEL
678 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
679 uint64_t value)
681 uint32_t mask = 0;
683 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
684 if (!arm_feature(env, ARM_FEATURE_V8)) {
685 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
686 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
687 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
689 if (arm_feature(env, ARM_FEATURE_VFP)) {
690 /* VFP coprocessor: cp10 & cp11 [23:20] */
691 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
693 if (!arm_feature(env, ARM_FEATURE_NEON)) {
694 /* ASEDIS [31] bit is RAO/WI */
695 value |= (1 << 31);
698 /* VFPv3 and upwards with NEON implement 32 double precision
699 * registers (D0-D31).
701 if (!arm_feature(env, ARM_FEATURE_NEON) ||
702 !arm_feature(env, ARM_FEATURE_VFP3)) {
703 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
704 value |= (1 << 30);
707 value &= mask;
709 env->cp15.cpacr_el1 = value;
712 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
713 bool isread)
715 if (arm_feature(env, ARM_FEATURE_V8)) {
716 /* Check if CPACR accesses are to be trapped to EL2 */
717 if (arm_current_el(env) == 1 &&
718 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
719 return CP_ACCESS_TRAP_EL2;
720 /* Check if CPACR accesses are to be trapped to EL3 */
721 } else if (arm_current_el(env) < 3 &&
722 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
723 return CP_ACCESS_TRAP_EL3;
727 return CP_ACCESS_OK;
730 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
731 bool isread)
733 /* Check if CPTR accesses are set to trap to EL3 */
734 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
735 return CP_ACCESS_TRAP_EL3;
738 return CP_ACCESS_OK;
741 static const ARMCPRegInfo v6_cp_reginfo[] = {
742 /* prefetch by MVA in v6, NOP in v7 */
743 { .name = "MVA_prefetch",
744 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
745 .access = PL1_W, .type = ARM_CP_NOP },
746 /* We need to break the TB after ISB to execute self-modifying code
747 * correctly and also to take any pending interrupts immediately.
748 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
750 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
751 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
752 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
753 .access = PL0_W, .type = ARM_CP_NOP },
754 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
755 .access = PL0_W, .type = ARM_CP_NOP },
756 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
757 .access = PL1_RW,
758 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
759 offsetof(CPUARMState, cp15.ifar_ns) },
760 .resetvalue = 0, },
761 /* Watchpoint Fault Address Register : should actually only be present
762 * for 1136, 1176, 11MPCore.
764 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
765 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
766 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
767 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
768 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
769 .resetvalue = 0, .writefn = cpacr_write },
770 REGINFO_SENTINEL
773 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
774 bool isread)
776 /* Performance monitor registers user accessibility is controlled
777 * by PMUSERENR.
779 if (arm_current_el(env) == 0 && !env->cp15.c9_pmuserenr) {
780 return CP_ACCESS_TRAP;
782 return CP_ACCESS_OK;
785 #ifndef CONFIG_USER_ONLY
787 static inline bool arm_ccnt_enabled(CPUARMState *env)
789 /* This does not support checking PMCCFILTR_EL0 register */
791 if (!(env->cp15.c9_pmcr & PMCRE)) {
792 return false;
795 return true;
798 void pmccntr_sync(CPUARMState *env)
800 uint64_t temp_ticks;
802 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
803 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
805 if (env->cp15.c9_pmcr & PMCRD) {
806 /* Increment once every 64 processor clock cycles */
807 temp_ticks /= 64;
810 if (arm_ccnt_enabled(env)) {
811 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
815 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
816 uint64_t value)
818 pmccntr_sync(env);
820 if (value & PMCRC) {
821 /* The counter has been reset */
822 env->cp15.c15_ccnt = 0;
825 /* only the DP, X, D and E bits are writable */
826 env->cp15.c9_pmcr &= ~0x39;
827 env->cp15.c9_pmcr |= (value & 0x39);
829 pmccntr_sync(env);
832 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
834 uint64_t total_ticks;
836 if (!arm_ccnt_enabled(env)) {
837 /* Counter is disabled, do not change value */
838 return env->cp15.c15_ccnt;
841 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
842 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
844 if (env->cp15.c9_pmcr & PMCRD) {
845 /* Increment once every 64 processor clock cycles */
846 total_ticks /= 64;
848 return total_ticks - env->cp15.c15_ccnt;
851 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
852 uint64_t value)
854 uint64_t total_ticks;
856 if (!arm_ccnt_enabled(env)) {
857 /* Counter is disabled, set the absolute value */
858 env->cp15.c15_ccnt = value;
859 return;
862 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
863 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
865 if (env->cp15.c9_pmcr & PMCRD) {
866 /* Increment once every 64 processor clock cycles */
867 total_ticks /= 64;
869 env->cp15.c15_ccnt = total_ticks - value;
872 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
873 uint64_t value)
875 uint64_t cur_val = pmccntr_read(env, NULL);
877 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
880 #else /* CONFIG_USER_ONLY */
882 void pmccntr_sync(CPUARMState *env)
886 #endif
888 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
889 uint64_t value)
891 pmccntr_sync(env);
892 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
893 pmccntr_sync(env);
896 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
897 uint64_t value)
899 value &= (1 << 31);
900 env->cp15.c9_pmcnten |= value;
903 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
904 uint64_t value)
906 value &= (1 << 31);
907 env->cp15.c9_pmcnten &= ~value;
910 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
911 uint64_t value)
913 env->cp15.c9_pmovsr &= ~value;
916 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
917 uint64_t value)
919 env->cp15.c9_pmxevtyper = value & 0xff;
922 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
923 uint64_t value)
925 env->cp15.c9_pmuserenr = value & 1;
928 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
929 uint64_t value)
931 /* We have no event counters so only the C bit can be changed */
932 value &= (1 << 31);
933 env->cp15.c9_pminten |= value;
936 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
937 uint64_t value)
939 value &= (1 << 31);
940 env->cp15.c9_pminten &= ~value;
943 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
944 uint64_t value)
946 /* Note that even though the AArch64 view of this register has bits
947 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
948 * architectural requirements for bits which are RES0 only in some
949 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
950 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
952 raw_write(env, ri, value & ~0x1FULL);
955 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
957 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
958 * For bits that vary between AArch32/64, code needs to check the
959 * current execution mode before directly using the feature bit.
961 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
963 if (!arm_feature(env, ARM_FEATURE_EL2)) {
964 valid_mask &= ~SCR_HCE;
966 /* On ARMv7, SMD (or SCD as it is called in v7) is only
967 * supported if EL2 exists. The bit is UNK/SBZP when
968 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
969 * when EL2 is unavailable.
970 * On ARMv8, this bit is always available.
972 if (arm_feature(env, ARM_FEATURE_V7) &&
973 !arm_feature(env, ARM_FEATURE_V8)) {
974 valid_mask &= ~SCR_SMD;
978 /* Clear all-context RES0 bits. */
979 value &= valid_mask;
980 raw_write(env, ri, value);
983 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
985 ARMCPU *cpu = arm_env_get_cpu(env);
987 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
988 * bank
990 uint32_t index = A32_BANKED_REG_GET(env, csselr,
991 ri->secure & ARM_CP_SECSTATE_S);
993 return cpu->ccsidr[index];
996 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
997 uint64_t value)
999 raw_write(env, ri, value & 0xf);
1002 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1004 CPUState *cs = ENV_GET_CPU(env);
1005 uint64_t ret = 0;
1007 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1008 ret |= CPSR_I;
1010 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1011 ret |= CPSR_F;
1013 /* External aborts are not possible in QEMU so A bit is always clear */
1014 return ret;
1017 static const ARMCPRegInfo v7_cp_reginfo[] = {
1018 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1019 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1020 .access = PL1_W, .type = ARM_CP_NOP },
1021 /* Performance monitors are implementation defined in v7,
1022 * but with an ARM recommended set of registers, which we
1023 * follow (although we don't actually implement any counters)
1025 * Performance registers fall into three categories:
1026 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1027 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1028 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1029 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1030 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1032 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1033 .access = PL0_RW, .type = ARM_CP_ALIAS,
1034 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1035 .writefn = pmcntenset_write,
1036 .accessfn = pmreg_access,
1037 .raw_writefn = raw_write },
1038 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1039 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1040 .access = PL0_RW, .accessfn = pmreg_access,
1041 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1042 .writefn = pmcntenset_write, .raw_writefn = raw_write },
1043 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1044 .access = PL0_RW,
1045 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1046 .accessfn = pmreg_access,
1047 .writefn = pmcntenclr_write,
1048 .type = ARM_CP_ALIAS },
1049 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1050 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1051 .access = PL0_RW, .accessfn = pmreg_access,
1052 .type = ARM_CP_ALIAS,
1053 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1054 .writefn = pmcntenclr_write },
1055 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1056 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1057 .accessfn = pmreg_access,
1058 .writefn = pmovsr_write,
1059 .raw_writefn = raw_write },
1060 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1061 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1062 .access = PL0_RW, .accessfn = pmreg_access,
1063 .type = ARM_CP_ALIAS,
1064 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1065 .writefn = pmovsr_write,
1066 .raw_writefn = raw_write },
1067 /* Unimplemented so WI. */
1068 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1069 .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
1070 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
1071 * We choose to RAZ/WI.
1073 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1074 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1075 .accessfn = pmreg_access },
1076 #ifndef CONFIG_USER_ONLY
1077 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1078 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
1079 .readfn = pmccntr_read, .writefn = pmccntr_write32,
1080 .accessfn = pmreg_access },
1081 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1082 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1083 .access = PL0_RW, .accessfn = pmreg_access,
1084 .type = ARM_CP_IO,
1085 .readfn = pmccntr_read, .writefn = pmccntr_write, },
1086 #endif
1087 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1088 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1089 .writefn = pmccfiltr_write,
1090 .access = PL0_RW, .accessfn = pmreg_access,
1091 .type = ARM_CP_IO,
1092 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1093 .resetvalue = 0, },
1094 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1095 .access = PL0_RW,
1096 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
1097 .accessfn = pmreg_access, .writefn = pmxevtyper_write,
1098 .raw_writefn = raw_write },
1099 /* Unimplemented, RAZ/WI. */
1100 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1101 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1102 .accessfn = pmreg_access },
1103 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1104 .access = PL0_R | PL1_RW,
1105 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1106 .resetvalue = 0,
1107 .writefn = pmuserenr_write, .raw_writefn = raw_write },
1108 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1109 .access = PL1_RW,
1110 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1111 .resetvalue = 0,
1112 .writefn = pmintenset_write, .raw_writefn = raw_write },
1113 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
1114 .access = PL1_RW, .type = ARM_CP_ALIAS,
1115 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1116 .writefn = pmintenclr_write, },
1117 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1118 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
1119 .access = PL1_RW, .type = ARM_CP_ALIAS,
1120 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1121 .writefn = pmintenclr_write },
1122 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
1123 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
1124 .access = PL1_RW, .writefn = vbar_write,
1125 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
1126 offsetof(CPUARMState, cp15.vbar_ns) },
1127 .resetvalue = 0 },
1128 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1129 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
1130 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
1131 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1132 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
1133 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1134 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1135 offsetof(CPUARMState, cp15.csselr_ns) } },
1136 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1137 * just RAZ for all cores:
1139 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1140 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
1141 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
1142 /* Auxiliary fault status registers: these also are IMPDEF, and we
1143 * choose to RAZ/WI for all cores.
1145 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1146 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1147 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1148 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1149 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1150 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1151 /* MAIR can just read-as-written because we don't implement caches
1152 * and so don't need to care about memory attributes.
1154 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1155 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
1156 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
1157 .resetvalue = 0 },
1158 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1159 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1160 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1161 .resetvalue = 0 },
1162 /* For non-long-descriptor page tables these are PRRR and NMRR;
1163 * regardless they still act as reads-as-written for QEMU.
1165 /* MAIR0/1 are defined separately from their 64-bit counterpart which
1166 * allows them to assign the correct fieldoffset based on the endianness
1167 * handled in the field definitions.
1169 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
1170 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
1171 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1172 offsetof(CPUARMState, cp15.mair0_ns) },
1173 .resetfn = arm_cp_reset_ignore },
1174 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
1175 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
1176 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1177 offsetof(CPUARMState, cp15.mair1_ns) },
1178 .resetfn = arm_cp_reset_ignore },
1179 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1180 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
1181 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
1182 /* 32 bit ITLB invalidates */
1183 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
1184 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1185 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
1186 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1187 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
1188 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1189 /* 32 bit DTLB invalidates */
1190 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
1191 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1192 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
1193 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1194 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
1195 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1196 /* 32 bit TLB invalidates */
1197 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
1198 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1199 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
1200 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1201 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
1202 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1203 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
1204 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
1205 REGINFO_SENTINEL
1208 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1209 /* 32 bit TLB invalidates, Inner Shareable */
1210 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
1211 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
1212 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
1213 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
1214 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
1215 .type = ARM_CP_NO_RAW, .access = PL1_W,
1216 .writefn = tlbiasid_is_write },
1217 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
1218 .type = ARM_CP_NO_RAW, .access = PL1_W,
1219 .writefn = tlbimvaa_is_write },
1220 REGINFO_SENTINEL
1223 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1224 uint64_t value)
1226 value &= 1;
1227 env->teecr = value;
1230 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1231 bool isread)
1233 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
1234 return CP_ACCESS_TRAP;
1236 return CP_ACCESS_OK;
1239 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1240 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1241 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1242 .resetvalue = 0,
1243 .writefn = teecr_write },
1244 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1245 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
1246 .accessfn = teehbr_access, .resetvalue = 0 },
1247 REGINFO_SENTINEL
1250 static const ARMCPRegInfo v6k_cp_reginfo[] = {
1251 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1252 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1253 .access = PL0_RW,
1254 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
1255 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1256 .access = PL0_RW,
1257 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1258 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
1259 .resetfn = arm_cp_reset_ignore },
1260 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1261 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1262 .access = PL0_R|PL1_W,
1263 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1264 .resetvalue = 0},
1265 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1266 .access = PL0_R|PL1_W,
1267 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1268 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
1269 .resetfn = arm_cp_reset_ignore },
1270 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
1271 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
1272 .access = PL1_RW,
1273 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1274 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1275 .access = PL1_RW,
1276 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1277 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1278 .resetvalue = 0 },
1279 REGINFO_SENTINEL
1282 #ifndef CONFIG_USER_ONLY
1284 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1285 bool isread)
1287 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1288 * Writable only at the highest implemented exception level.
1290 int el = arm_current_el(env);
1292 switch (el) {
1293 case 0:
1294 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1295 return CP_ACCESS_TRAP;
1297 break;
1298 case 1:
1299 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1300 arm_is_secure_below_el3(env)) {
1301 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1302 return CP_ACCESS_TRAP_UNCATEGORIZED;
1304 break;
1305 case 2:
1306 case 3:
1307 break;
1310 if (!isread && el < arm_highest_el(env)) {
1311 return CP_ACCESS_TRAP_UNCATEGORIZED;
1314 return CP_ACCESS_OK;
1317 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1318 bool isread)
1320 unsigned int cur_el = arm_current_el(env);
1321 bool secure = arm_is_secure(env);
1323 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
1324 if (cur_el == 0 &&
1325 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1326 return CP_ACCESS_TRAP;
1329 if (arm_feature(env, ARM_FEATURE_EL2) &&
1330 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1331 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1332 return CP_ACCESS_TRAP_EL2;
1334 return CP_ACCESS_OK;
1337 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1338 bool isread)
1340 unsigned int cur_el = arm_current_el(env);
1341 bool secure = arm_is_secure(env);
1343 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1344 * EL0[PV]TEN is zero.
1346 if (cur_el == 0 &&
1347 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1348 return CP_ACCESS_TRAP;
1351 if (arm_feature(env, ARM_FEATURE_EL2) &&
1352 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1353 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1354 return CP_ACCESS_TRAP_EL2;
1356 return CP_ACCESS_OK;
1359 static CPAccessResult gt_pct_access(CPUARMState *env,
1360 const ARMCPRegInfo *ri,
1361 bool isread)
1363 return gt_counter_access(env, GTIMER_PHYS, isread);
1366 static CPAccessResult gt_vct_access(CPUARMState *env,
1367 const ARMCPRegInfo *ri,
1368 bool isread)
1370 return gt_counter_access(env, GTIMER_VIRT, isread);
1373 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1374 bool isread)
1376 return gt_timer_access(env, GTIMER_PHYS, isread);
1379 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1380 bool isread)
1382 return gt_timer_access(env, GTIMER_VIRT, isread);
1385 static CPAccessResult gt_stimer_access(CPUARMState *env,
1386 const ARMCPRegInfo *ri,
1387 bool isread)
1389 /* The AArch64 register view of the secure physical timer is
1390 * always accessible from EL3, and configurably accessible from
1391 * Secure EL1.
1393 switch (arm_current_el(env)) {
1394 case 1:
1395 if (!arm_is_secure(env)) {
1396 return CP_ACCESS_TRAP;
1398 if (!(env->cp15.scr_el3 & SCR_ST)) {
1399 return CP_ACCESS_TRAP_EL3;
1401 return CP_ACCESS_OK;
1402 case 0:
1403 case 2:
1404 return CP_ACCESS_TRAP;
1405 case 3:
1406 return CP_ACCESS_OK;
1407 default:
1408 g_assert_not_reached();
1412 static uint64_t gt_get_countervalue(CPUARMState *env)
1414 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
1417 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1419 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1421 if (gt->ctl & 1) {
1422 /* Timer enabled: calculate and set current ISTATUS, irq, and
1423 * reset timer to when ISTATUS next has to change
1425 uint64_t offset = timeridx == GTIMER_VIRT ?
1426 cpu->env.cp15.cntvoff_el2 : 0;
1427 uint64_t count = gt_get_countervalue(&cpu->env);
1428 /* Note that this must be unsigned 64 bit arithmetic: */
1429 int istatus = count - offset >= gt->cval;
1430 uint64_t nexttick;
1432 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1433 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1434 (istatus && !(gt->ctl & 2)));
1435 if (istatus) {
1436 /* Next transition is when count rolls back over to zero */
1437 nexttick = UINT64_MAX;
1438 } else {
1439 /* Next transition is when we hit cval */
1440 nexttick = gt->cval + offset;
1442 /* Note that the desired next expiry time might be beyond the
1443 * signed-64-bit range of a QEMUTimer -- in this case we just
1444 * set the timer for as far in the future as possible. When the
1445 * timer expires we will reset the timer for any remaining period.
1447 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1448 nexttick = INT64_MAX / GTIMER_SCALE;
1450 timer_mod(cpu->gt_timer[timeridx], nexttick);
1451 } else {
1452 /* Timer disabled: ISTATUS and timer output always clear */
1453 gt->ctl &= ~4;
1454 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
1455 timer_del(cpu->gt_timer[timeridx]);
1459 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1460 int timeridx)
1462 ARMCPU *cpu = arm_env_get_cpu(env);
1464 timer_del(cpu->gt_timer[timeridx]);
1467 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1469 return gt_get_countervalue(env);
1472 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1474 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1477 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1478 int timeridx,
1479 uint64_t value)
1481 env->cp15.c14_timer[timeridx].cval = value;
1482 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1485 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1486 int timeridx)
1488 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1490 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
1491 (gt_get_countervalue(env) - offset));
1494 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1495 int timeridx,
1496 uint64_t value)
1498 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
1500 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
1501 sextract64(value, 0, 32);
1502 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1505 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1506 int timeridx,
1507 uint64_t value)
1509 ARMCPU *cpu = arm_env_get_cpu(env);
1510 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1512 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
1513 if ((oldval ^ value) & 1) {
1514 /* Enable toggled */
1515 gt_recalc_timer(cpu, timeridx);
1516 } else if ((oldval ^ value) & 2) {
1517 /* IMASK toggled: don't need to recalculate,
1518 * just set the interrupt line based on ISTATUS
1520 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1521 (oldval & 4) && !(value & 2));
1525 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1527 gt_timer_reset(env, ri, GTIMER_PHYS);
1530 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1531 uint64_t value)
1533 gt_cval_write(env, ri, GTIMER_PHYS, value);
1536 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1538 return gt_tval_read(env, ri, GTIMER_PHYS);
1541 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1542 uint64_t value)
1544 gt_tval_write(env, ri, GTIMER_PHYS, value);
1547 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1548 uint64_t value)
1550 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1553 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1555 gt_timer_reset(env, ri, GTIMER_VIRT);
1558 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1559 uint64_t value)
1561 gt_cval_write(env, ri, GTIMER_VIRT, value);
1564 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1566 return gt_tval_read(env, ri, GTIMER_VIRT);
1569 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1570 uint64_t value)
1572 gt_tval_write(env, ri, GTIMER_VIRT, value);
1575 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1576 uint64_t value)
1578 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1581 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1582 uint64_t value)
1584 ARMCPU *cpu = arm_env_get_cpu(env);
1586 raw_write(env, ri, value);
1587 gt_recalc_timer(cpu, GTIMER_VIRT);
1590 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1592 gt_timer_reset(env, ri, GTIMER_HYP);
1595 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1596 uint64_t value)
1598 gt_cval_write(env, ri, GTIMER_HYP, value);
1601 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1603 return gt_tval_read(env, ri, GTIMER_HYP);
1606 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1607 uint64_t value)
1609 gt_tval_write(env, ri, GTIMER_HYP, value);
1612 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1613 uint64_t value)
1615 gt_ctl_write(env, ri, GTIMER_HYP, value);
1618 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1620 gt_timer_reset(env, ri, GTIMER_SEC);
1623 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1624 uint64_t value)
1626 gt_cval_write(env, ri, GTIMER_SEC, value);
1629 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1631 return gt_tval_read(env, ri, GTIMER_SEC);
1634 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1635 uint64_t value)
1637 gt_tval_write(env, ri, GTIMER_SEC, value);
1640 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1641 uint64_t value)
1643 gt_ctl_write(env, ri, GTIMER_SEC, value);
1646 void arm_gt_ptimer_cb(void *opaque)
1648 ARMCPU *cpu = opaque;
1650 gt_recalc_timer(cpu, GTIMER_PHYS);
1653 void arm_gt_vtimer_cb(void *opaque)
1655 ARMCPU *cpu = opaque;
1657 gt_recalc_timer(cpu, GTIMER_VIRT);
1660 void arm_gt_htimer_cb(void *opaque)
1662 ARMCPU *cpu = opaque;
1664 gt_recalc_timer(cpu, GTIMER_HYP);
1667 void arm_gt_stimer_cb(void *opaque)
1669 ARMCPU *cpu = opaque;
1671 gt_recalc_timer(cpu, GTIMER_SEC);
1674 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1675 /* Note that CNTFRQ is purely reads-as-written for the benefit
1676 * of software; writing it doesn't actually change the timer frequency.
1677 * Our reset value matches the fixed frequency we implement the timer at.
1679 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
1680 .type = ARM_CP_ALIAS,
1681 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1682 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
1684 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1685 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1686 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1687 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1688 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
1690 /* overall control: mostly access permissions */
1691 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1692 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
1693 .access = PL1_RW,
1694 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1695 .resetvalue = 0,
1697 /* per-timer control */
1698 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1699 .secure = ARM_CP_SECSTATE_NS,
1700 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1701 .accessfn = gt_ptimer_access,
1702 .fieldoffset = offsetoflow32(CPUARMState,
1703 cp15.c14_timer[GTIMER_PHYS].ctl),
1704 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
1706 { .name = "CNTP_CTL(S)",
1707 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1708 .secure = ARM_CP_SECSTATE_S,
1709 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1710 .accessfn = gt_ptimer_access,
1711 .fieldoffset = offsetoflow32(CPUARMState,
1712 cp15.c14_timer[GTIMER_SEC].ctl),
1713 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1715 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1716 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
1717 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1718 .accessfn = gt_ptimer_access,
1719 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1720 .resetvalue = 0,
1721 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
1723 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
1724 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1725 .accessfn = gt_vtimer_access,
1726 .fieldoffset = offsetoflow32(CPUARMState,
1727 cp15.c14_timer[GTIMER_VIRT].ctl),
1728 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
1730 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1731 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
1732 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1733 .accessfn = gt_vtimer_access,
1734 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1735 .resetvalue = 0,
1736 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
1738 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1739 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1740 .secure = ARM_CP_SECSTATE_NS,
1741 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1742 .accessfn = gt_ptimer_access,
1743 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
1745 { .name = "CNTP_TVAL(S)",
1746 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1747 .secure = ARM_CP_SECSTATE_S,
1748 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1749 .accessfn = gt_ptimer_access,
1750 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
1752 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1753 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
1754 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1755 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
1756 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
1758 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
1759 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1760 .accessfn = gt_vtimer_access,
1761 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
1763 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1764 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
1765 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1766 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
1767 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
1769 /* The counter itself */
1770 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
1771 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
1772 .accessfn = gt_pct_access,
1773 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1775 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1776 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
1777 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1778 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
1780 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
1781 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
1782 .accessfn = gt_vct_access,
1783 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
1785 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1786 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
1787 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1788 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
1790 /* Comparison value, indicating when the timer goes off */
1791 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
1792 .secure = ARM_CP_SECSTATE_NS,
1793 .access = PL1_RW | PL0_R,
1794 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1795 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1796 .accessfn = gt_ptimer_access,
1797 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
1799 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
1800 .secure = ARM_CP_SECSTATE_S,
1801 .access = PL1_RW | PL0_R,
1802 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1803 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1804 .accessfn = gt_ptimer_access,
1805 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1807 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1808 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1809 .access = PL1_RW | PL0_R,
1810 .type = ARM_CP_IO,
1811 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1812 .resetvalue = 0, .accessfn = gt_ptimer_access,
1813 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
1815 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1816 .access = PL1_RW | PL0_R,
1817 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1818 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1819 .accessfn = gt_vtimer_access,
1820 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
1822 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1823 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1824 .access = PL1_RW | PL0_R,
1825 .type = ARM_CP_IO,
1826 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1827 .resetvalue = 0, .accessfn = gt_vtimer_access,
1828 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
1830 /* Secure timer -- this is actually restricted to only EL3
1831 * and configurably Secure-EL1 via the accessfn.
1833 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
1834 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
1835 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
1836 .accessfn = gt_stimer_access,
1837 .readfn = gt_sec_tval_read,
1838 .writefn = gt_sec_tval_write,
1839 .resetfn = gt_sec_timer_reset,
1841 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
1842 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
1843 .type = ARM_CP_IO, .access = PL1_RW,
1844 .accessfn = gt_stimer_access,
1845 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
1846 .resetvalue = 0,
1847 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1849 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
1850 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
1851 .type = ARM_CP_IO, .access = PL1_RW,
1852 .accessfn = gt_stimer_access,
1853 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1854 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1856 REGINFO_SENTINEL
1859 #else
1860 /* In user-mode none of the generic timer registers are accessible,
1861 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
1862 * so instead just don't register any of them.
1864 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1865 REGINFO_SENTINEL
1868 #endif
1870 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1872 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1873 raw_write(env, ri, value);
1874 } else if (arm_feature(env, ARM_FEATURE_V7)) {
1875 raw_write(env, ri, value & 0xfffff6ff);
1876 } else {
1877 raw_write(env, ri, value & 0xfffff1ff);
1881 #ifndef CONFIG_USER_ONLY
1882 /* get_phys_addr() isn't present for user-mode-only targets */
1884 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
1885 bool isread)
1887 if (ri->opc2 & 4) {
1888 /* The ATS12NSO* operations must trap to EL3 if executed in
1889 * Secure EL1 (which can only happen if EL3 is AArch64).
1890 * They are simply UNDEF if executed from NS EL1.
1891 * They function normally from EL2 or EL3.
1893 if (arm_current_el(env) == 1) {
1894 if (arm_is_secure_below_el3(env)) {
1895 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
1897 return CP_ACCESS_TRAP_UNCATEGORIZED;
1900 return CP_ACCESS_OK;
1903 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
1904 int access_type, ARMMMUIdx mmu_idx)
1906 hwaddr phys_addr;
1907 target_ulong page_size;
1908 int prot;
1909 uint32_t fsr;
1910 bool ret;
1911 uint64_t par64;
1912 MemTxAttrs attrs = {};
1913 ARMMMUFaultInfo fi = {};
1915 ret = get_phys_addr(env, value, access_type, mmu_idx,
1916 &phys_addr, &attrs, &prot, &page_size, &fsr, &fi);
1917 if (extended_addresses_enabled(env)) {
1918 /* fsr is a DFSR/IFSR value for the long descriptor
1919 * translation table format, but with WnR always clear.
1920 * Convert it to a 64-bit PAR.
1922 par64 = (1 << 11); /* LPAE bit always set */
1923 if (!ret) {
1924 par64 |= phys_addr & ~0xfffULL;
1925 if (!attrs.secure) {
1926 par64 |= (1 << 9); /* NS */
1928 /* We don't set the ATTR or SH fields in the PAR. */
1929 } else {
1930 par64 |= 1; /* F */
1931 par64 |= (fsr & 0x3f) << 1; /* FS */
1932 /* Note that S2WLK and FSTAGE are always zero, because we don't
1933 * implement virtualization and therefore there can't be a stage 2
1934 * fault.
1937 } else {
1938 /* fsr is a DFSR/IFSR value for the short descriptor
1939 * translation table format (with WnR always clear).
1940 * Convert it to a 32-bit PAR.
1942 if (!ret) {
1943 /* We do not set any attribute bits in the PAR */
1944 if (page_size == (1 << 24)
1945 && arm_feature(env, ARM_FEATURE_V7)) {
1946 par64 = (phys_addr & 0xff000000) | (1 << 1);
1947 } else {
1948 par64 = phys_addr & 0xfffff000;
1950 if (!attrs.secure) {
1951 par64 |= (1 << 9); /* NS */
1953 } else {
1954 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
1955 ((fsr & 0xf) << 1) | 1;
1958 return par64;
1961 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1963 int access_type = ri->opc2 & 1;
1964 uint64_t par64;
1965 ARMMMUIdx mmu_idx;
1966 int el = arm_current_el(env);
1967 bool secure = arm_is_secure_below_el3(env);
1969 switch (ri->opc2 & 6) {
1970 case 0:
1971 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
1972 switch (el) {
1973 case 3:
1974 mmu_idx = ARMMMUIdx_S1E3;
1975 break;
1976 case 2:
1977 mmu_idx = ARMMMUIdx_S1NSE1;
1978 break;
1979 case 1:
1980 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
1981 break;
1982 default:
1983 g_assert_not_reached();
1985 break;
1986 case 2:
1987 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
1988 switch (el) {
1989 case 3:
1990 mmu_idx = ARMMMUIdx_S1SE0;
1991 break;
1992 case 2:
1993 mmu_idx = ARMMMUIdx_S1NSE0;
1994 break;
1995 case 1:
1996 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
1997 break;
1998 default:
1999 g_assert_not_reached();
2001 break;
2002 case 4:
2003 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2004 mmu_idx = ARMMMUIdx_S12NSE1;
2005 break;
2006 case 6:
2007 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2008 mmu_idx = ARMMMUIdx_S12NSE0;
2009 break;
2010 default:
2011 g_assert_not_reached();
2014 par64 = do_ats_write(env, value, access_type, mmu_idx);
2016 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2019 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2020 uint64_t value)
2022 int access_type = ri->opc2 & 1;
2023 uint64_t par64;
2025 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2027 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2030 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2031 bool isread)
2033 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2034 return CP_ACCESS_TRAP;
2036 return CP_ACCESS_OK;
2039 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2040 uint64_t value)
2042 int access_type = ri->opc2 & 1;
2043 ARMMMUIdx mmu_idx;
2044 int secure = arm_is_secure_below_el3(env);
2046 switch (ri->opc2 & 6) {
2047 case 0:
2048 switch (ri->opc1) {
2049 case 0: /* AT S1E1R, AT S1E1W */
2050 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2051 break;
2052 case 4: /* AT S1E2R, AT S1E2W */
2053 mmu_idx = ARMMMUIdx_S1E2;
2054 break;
2055 case 6: /* AT S1E3R, AT S1E3W */
2056 mmu_idx = ARMMMUIdx_S1E3;
2057 break;
2058 default:
2059 g_assert_not_reached();
2061 break;
2062 case 2: /* AT S1E0R, AT S1E0W */
2063 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2064 break;
2065 case 4: /* AT S12E1R, AT S12E1W */
2066 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
2067 break;
2068 case 6: /* AT S12E0R, AT S12E0W */
2069 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
2070 break;
2071 default:
2072 g_assert_not_reached();
2075 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
2077 #endif
2079 static const ARMCPRegInfo vapa_cp_reginfo[] = {
2080 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2081 .access = PL1_RW, .resetvalue = 0,
2082 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2083 offsetoflow32(CPUARMState, cp15.par_ns) },
2084 .writefn = par_write },
2085 #ifndef CONFIG_USER_ONLY
2086 /* This underdecoding is safe because the reginfo is NO_RAW. */
2087 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
2088 .access = PL1_W, .accessfn = ats_access,
2089 .writefn = ats_write, .type = ARM_CP_NO_RAW },
2090 #endif
2091 REGINFO_SENTINEL
2094 /* Return basic MPU access permission bits. */
2095 static uint32_t simple_mpu_ap_bits(uint32_t val)
2097 uint32_t ret;
2098 uint32_t mask;
2099 int i;
2100 ret = 0;
2101 mask = 3;
2102 for (i = 0; i < 16; i += 2) {
2103 ret |= (val >> i) & mask;
2104 mask <<= 2;
2106 return ret;
2109 /* Pad basic MPU access permission bits to extended format. */
2110 static uint32_t extended_mpu_ap_bits(uint32_t val)
2112 uint32_t ret;
2113 uint32_t mask;
2114 int i;
2115 ret = 0;
2116 mask = 3;
2117 for (i = 0; i < 16; i += 2) {
2118 ret |= (val & mask) << i;
2119 mask <<= 2;
2121 return ret;
2124 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2125 uint64_t value)
2127 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
2130 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2132 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
2135 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2136 uint64_t value)
2138 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
2141 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2143 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
2146 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2148 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2150 if (!u32p) {
2151 return 0;
2154 u32p += env->cp15.c6_rgnr;
2155 return *u32p;
2158 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2159 uint64_t value)
2161 ARMCPU *cpu = arm_env_get_cpu(env);
2162 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2164 if (!u32p) {
2165 return;
2168 u32p += env->cp15.c6_rgnr;
2169 tlb_flush(CPU(cpu), 1); /* Mappings may have changed - purge! */
2170 *u32p = value;
2173 static void pmsav7_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2175 ARMCPU *cpu = arm_env_get_cpu(env);
2176 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2178 if (!u32p) {
2179 return;
2182 memset(u32p, 0, sizeof(*u32p) * cpu->pmsav7_dregion);
2185 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2186 uint64_t value)
2188 ARMCPU *cpu = arm_env_get_cpu(env);
2189 uint32_t nrgs = cpu->pmsav7_dregion;
2191 if (value >= nrgs) {
2192 qemu_log_mask(LOG_GUEST_ERROR,
2193 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2194 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2195 return;
2198 raw_write(env, ri, value);
2201 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
2202 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2203 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2204 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
2205 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2206 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2207 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2208 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
2209 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2210 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2211 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2212 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
2213 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2214 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2215 .access = PL1_RW,
2216 .fieldoffset = offsetof(CPUARMState, cp15.c6_rgnr),
2217 .writefn = pmsav7_rgnr_write },
2218 REGINFO_SENTINEL
2221 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2222 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2223 .access = PL1_RW, .type = ARM_CP_ALIAS,
2224 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2225 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2226 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2227 .access = PL1_RW, .type = ARM_CP_ALIAS,
2228 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2229 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2230 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2231 .access = PL1_RW,
2232 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2233 .resetvalue = 0, },
2234 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2235 .access = PL1_RW,
2236 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2237 .resetvalue = 0, },
2238 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2239 .access = PL1_RW,
2240 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2241 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2242 .access = PL1_RW,
2243 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
2244 /* Protection region base and size registers */
2245 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2246 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2247 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2248 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2249 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2250 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2251 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2252 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2253 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2254 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2255 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2256 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2257 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2258 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2259 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2260 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2261 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2262 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2263 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2264 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2265 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2266 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2267 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2268 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
2269 REGINFO_SENTINEL
2272 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2273 uint64_t value)
2275 TCR *tcr = raw_ptr(env, ri);
2276 int maskshift = extract32(value, 0, 3);
2278 if (!arm_feature(env, ARM_FEATURE_V8)) {
2279 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2280 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2281 * using Long-desciptor translation table format */
2282 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2283 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2284 /* In an implementation that includes the Security Extensions
2285 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2286 * Short-descriptor translation table format.
2288 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2289 } else {
2290 value &= TTBCR_N;
2294 /* Update the masks corresponding to the TCR bank being written
2295 * Note that we always calculate mask and base_mask, but
2296 * they are only used for short-descriptor tables (ie if EAE is 0);
2297 * for long-descriptor tables the TCR fields are used differently
2298 * and the mask and base_mask values are meaningless.
2300 tcr->raw_tcr = value;
2301 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2302 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
2305 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2306 uint64_t value)
2308 ARMCPU *cpu = arm_env_get_cpu(env);
2310 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2311 /* With LPAE the TTBCR could result in a change of ASID
2312 * via the TTBCR.A1 bit, so do a TLB flush.
2314 tlb_flush(CPU(cpu), 1);
2316 vmsa_ttbcr_raw_write(env, ri, value);
2319 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2321 TCR *tcr = raw_ptr(env, ri);
2323 /* Reset both the TCR as well as the masks corresponding to the bank of
2324 * the TCR being reset.
2326 tcr->raw_tcr = 0;
2327 tcr->mask = 0;
2328 tcr->base_mask = 0xffffc000u;
2331 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2332 uint64_t value)
2334 ARMCPU *cpu = arm_env_get_cpu(env);
2335 TCR *tcr = raw_ptr(env, ri);
2337 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
2338 tlb_flush(CPU(cpu), 1);
2339 tcr->raw_tcr = value;
2342 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2343 uint64_t value)
2345 /* 64 bit accesses to the TTBRs can change the ASID and so we
2346 * must flush the TLB.
2348 if (cpreg_field_is_64bit(ri)) {
2349 ARMCPU *cpu = arm_env_get_cpu(env);
2351 tlb_flush(CPU(cpu), 1);
2353 raw_write(env, ri, value);
2356 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2357 uint64_t value)
2359 ARMCPU *cpu = arm_env_get_cpu(env);
2360 CPUState *cs = CPU(cpu);
2362 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2363 if (raw_read(env, ri) != value) {
2364 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2365 ARMMMUIdx_S2NS, -1);
2366 raw_write(env, ri, value);
2370 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
2371 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
2372 .access = PL1_RW, .type = ARM_CP_ALIAS,
2373 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
2374 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
2375 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
2376 .access = PL1_RW, .resetvalue = 0,
2377 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2378 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
2379 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2380 .access = PL1_RW, .resetvalue = 0,
2381 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2382 offsetof(CPUARMState, cp15.dfar_ns) } },
2383 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2384 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2385 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2386 .resetvalue = 0, },
2387 REGINFO_SENTINEL
2390 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
2391 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2392 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2393 .access = PL1_RW,
2394 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
2395 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
2396 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2397 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2398 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2399 offsetof(CPUARMState, cp15.ttbr0_ns) } },
2400 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
2401 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2402 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2403 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2404 offsetof(CPUARMState, cp15.ttbr1_ns) } },
2405 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2406 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2407 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2408 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
2409 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
2410 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2411 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
2412 .raw_writefn = vmsa_ttbcr_raw_write,
2413 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2414 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
2415 REGINFO_SENTINEL
2418 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2419 uint64_t value)
2421 env->cp15.c15_ticonfig = value & 0xe7;
2422 /* The OS_TYPE bit in this register changes the reported CPUID! */
2423 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2424 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
2427 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2428 uint64_t value)
2430 env->cp15.c15_threadid = value & 0xffff;
2433 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2434 uint64_t value)
2436 /* Wait-for-interrupt (deprecated) */
2437 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
2440 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2441 uint64_t value)
2443 /* On OMAP there are registers indicating the max/min index of dcache lines
2444 * containing a dirty line; cache flush operations have to reset these.
2446 env->cp15.c15_i_max = 0x000;
2447 env->cp15.c15_i_min = 0xff0;
2450 static const ARMCPRegInfo omap_cp_reginfo[] = {
2451 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2452 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
2453 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
2454 .resetvalue = 0, },
2455 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2456 .access = PL1_RW, .type = ARM_CP_NOP },
2457 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2458 .access = PL1_RW,
2459 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2460 .writefn = omap_ticonfig_write },
2461 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2462 .access = PL1_RW,
2463 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2464 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2465 .access = PL1_RW, .resetvalue = 0xff0,
2466 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2467 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2468 .access = PL1_RW,
2469 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2470 .writefn = omap_threadid_write },
2471 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2472 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2473 .type = ARM_CP_NO_RAW,
2474 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2475 /* TODO: Peripheral port remap register:
2476 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2477 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2478 * when MMU is off.
2480 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
2481 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
2482 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
2483 .writefn = omap_cachemaint_write },
2484 { .name = "C9", .cp = 15, .crn = 9,
2485 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2486 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
2487 REGINFO_SENTINEL
2490 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2491 uint64_t value)
2493 env->cp15.c15_cpar = value & 0x3fff;
2496 static const ARMCPRegInfo xscale_cp_reginfo[] = {
2497 { .name = "XSCALE_CPAR",
2498 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2499 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2500 .writefn = xscale_cpar_write, },
2501 { .name = "XSCALE_AUXCR",
2502 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2503 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2504 .resetvalue = 0, },
2505 /* XScale specific cache-lockdown: since we have no cache we NOP these
2506 * and hope the guest does not really rely on cache behaviour.
2508 { .name = "XSCALE_LOCK_ICACHE_LINE",
2509 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2510 .access = PL1_W, .type = ARM_CP_NOP },
2511 { .name = "XSCALE_UNLOCK_ICACHE",
2512 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2513 .access = PL1_W, .type = ARM_CP_NOP },
2514 { .name = "XSCALE_DCACHE_LOCK",
2515 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2516 .access = PL1_RW, .type = ARM_CP_NOP },
2517 { .name = "XSCALE_UNLOCK_DCACHE",
2518 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2519 .access = PL1_W, .type = ARM_CP_NOP },
2520 REGINFO_SENTINEL
2523 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2524 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2525 * implementation of this implementation-defined space.
2526 * Ideally this should eventually disappear in favour of actually
2527 * implementing the correct behaviour for all cores.
2529 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2530 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2531 .access = PL1_RW,
2532 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
2533 .resetvalue = 0 },
2534 REGINFO_SENTINEL
2537 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2538 /* Cache status: RAZ because we have no cache so it's always clean */
2539 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
2540 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2541 .resetvalue = 0 },
2542 REGINFO_SENTINEL
2545 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2546 /* We never have a a block transfer operation in progress */
2547 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
2548 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2549 .resetvalue = 0 },
2550 /* The cache ops themselves: these all NOP for QEMU */
2551 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2552 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2553 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2554 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2555 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2556 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2557 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2558 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2559 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2560 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2561 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2562 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2563 REGINFO_SENTINEL
2566 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2567 /* The cache test-and-clean instructions always return (1 << 30)
2568 * to indicate that there are no dirty cache lines.
2570 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
2571 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2572 .resetvalue = (1 << 30) },
2573 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
2574 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2575 .resetvalue = (1 << 30) },
2576 REGINFO_SENTINEL
2579 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2580 /* Ignore ReadBuffer accesses */
2581 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2582 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2583 .access = PL1_RW, .resetvalue = 0,
2584 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
2585 REGINFO_SENTINEL
2588 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2590 ARMCPU *cpu = arm_env_get_cpu(env);
2591 unsigned int cur_el = arm_current_el(env);
2592 bool secure = arm_is_secure(env);
2594 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2595 return env->cp15.vpidr_el2;
2597 return raw_read(env, ri);
2600 static uint64_t mpidr_read_val(CPUARMState *env)
2602 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2603 uint64_t mpidr = cpu->mp_affinity;
2605 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2606 mpidr |= (1U << 31);
2607 /* Cores which are uniprocessor (non-coherent)
2608 * but still implement the MP extensions set
2609 * bit 30. (For instance, Cortex-R5).
2611 if (cpu->mp_is_up) {
2612 mpidr |= (1u << 30);
2615 return mpidr;
2618 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2620 unsigned int cur_el = arm_current_el(env);
2621 bool secure = arm_is_secure(env);
2623 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2624 return env->cp15.vmpidr_el2;
2626 return mpidr_read_val(env);
2629 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
2630 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2631 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
2632 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
2633 REGINFO_SENTINEL
2636 static const ARMCPRegInfo lpae_cp_reginfo[] = {
2637 /* NOP AMAIR0/1 */
2638 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2639 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
2640 .access = PL1_RW, .type = ARM_CP_CONST,
2641 .resetvalue = 0 },
2642 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
2643 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
2644 .access = PL1_RW, .type = ARM_CP_CONST,
2645 .resetvalue = 0 },
2646 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
2647 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2648 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2649 offsetof(CPUARMState, cp15.par_ns)} },
2650 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
2651 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2652 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2653 offsetof(CPUARMState, cp15.ttbr0_ns) },
2654 .writefn = vmsa_ttbr_write, },
2655 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
2656 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2657 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2658 offsetof(CPUARMState, cp15.ttbr1_ns) },
2659 .writefn = vmsa_ttbr_write, },
2660 REGINFO_SENTINEL
2663 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2665 return vfp_get_fpcr(env);
2668 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2669 uint64_t value)
2671 vfp_set_fpcr(env, value);
2674 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2676 return vfp_get_fpsr(env);
2679 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2680 uint64_t value)
2682 vfp_set_fpsr(env, value);
2685 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2686 bool isread)
2688 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
2689 return CP_ACCESS_TRAP;
2691 return CP_ACCESS_OK;
2694 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2695 uint64_t value)
2697 env->daif = value & PSTATE_DAIF;
2700 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
2701 const ARMCPRegInfo *ri,
2702 bool isread)
2704 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2705 * SCTLR_EL1.UCI is set.
2707 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
2708 return CP_ACCESS_TRAP;
2710 return CP_ACCESS_OK;
2713 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2714 * Page D4-1736 (DDI0487A.b)
2717 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2718 uint64_t value)
2720 ARMCPU *cpu = arm_env_get_cpu(env);
2721 CPUState *cs = CPU(cpu);
2723 if (arm_is_secure_below_el3(env)) {
2724 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2725 } else {
2726 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2730 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2731 uint64_t value)
2733 bool sec = arm_is_secure_below_el3(env);
2734 CPUState *other_cs;
2736 CPU_FOREACH(other_cs) {
2737 if (sec) {
2738 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2739 } else {
2740 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2741 ARMMMUIdx_S12NSE0, -1);
2746 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2747 uint64_t value)
2749 /* Note that the 'ALL' scope must invalidate both stage 1 and
2750 * stage 2 translations, whereas most other scopes only invalidate
2751 * stage 1 translations.
2753 ARMCPU *cpu = arm_env_get_cpu(env);
2754 CPUState *cs = CPU(cpu);
2756 if (arm_is_secure_below_el3(env)) {
2757 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2758 } else {
2759 if (arm_feature(env, ARM_FEATURE_EL2)) {
2760 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2761 ARMMMUIdx_S2NS, -1);
2762 } else {
2763 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2768 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2769 uint64_t value)
2771 ARMCPU *cpu = arm_env_get_cpu(env);
2772 CPUState *cs = CPU(cpu);
2774 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E2, -1);
2777 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2778 uint64_t value)
2780 ARMCPU *cpu = arm_env_get_cpu(env);
2781 CPUState *cs = CPU(cpu);
2783 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E3, -1);
2786 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2787 uint64_t value)
2789 /* Note that the 'ALL' scope must invalidate both stage 1 and
2790 * stage 2 translations, whereas most other scopes only invalidate
2791 * stage 1 translations.
2793 bool sec = arm_is_secure_below_el3(env);
2794 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
2795 CPUState *other_cs;
2797 CPU_FOREACH(other_cs) {
2798 if (sec) {
2799 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2800 } else if (has_el2) {
2801 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2802 ARMMMUIdx_S12NSE0, ARMMMUIdx_S2NS, -1);
2803 } else {
2804 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2805 ARMMMUIdx_S12NSE0, -1);
2810 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2811 uint64_t value)
2813 CPUState *other_cs;
2815 CPU_FOREACH(other_cs) {
2816 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E2, -1);
2820 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2821 uint64_t value)
2823 CPUState *other_cs;
2825 CPU_FOREACH(other_cs) {
2826 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E3, -1);
2830 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2831 uint64_t value)
2833 /* Invalidate by VA, EL1&0 (AArch64 version).
2834 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
2835 * since we don't support flush-for-specific-ASID-only or
2836 * flush-last-level-only.
2838 ARMCPU *cpu = arm_env_get_cpu(env);
2839 CPUState *cs = CPU(cpu);
2840 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2842 if (arm_is_secure_below_el3(env)) {
2843 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1SE1,
2844 ARMMMUIdx_S1SE0, -1);
2845 } else {
2846 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S12NSE1,
2847 ARMMMUIdx_S12NSE0, -1);
2851 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2852 uint64_t value)
2854 /* Invalidate by VA, EL2
2855 * Currently handles both VAE2 and VALE2, since we don't support
2856 * flush-last-level-only.
2858 ARMCPU *cpu = arm_env_get_cpu(env);
2859 CPUState *cs = CPU(cpu);
2860 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2862 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E2, -1);
2865 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2866 uint64_t value)
2868 /* Invalidate by VA, EL3
2869 * Currently handles both VAE3 and VALE3, since we don't support
2870 * flush-last-level-only.
2872 ARMCPU *cpu = arm_env_get_cpu(env);
2873 CPUState *cs = CPU(cpu);
2874 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2876 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E3, -1);
2879 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2880 uint64_t value)
2882 bool sec = arm_is_secure_below_el3(env);
2883 CPUState *other_cs;
2884 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2886 CPU_FOREACH(other_cs) {
2887 if (sec) {
2888 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1SE1,
2889 ARMMMUIdx_S1SE0, -1);
2890 } else {
2891 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S12NSE1,
2892 ARMMMUIdx_S12NSE0, -1);
2897 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2898 uint64_t value)
2900 CPUState *other_cs;
2901 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2903 CPU_FOREACH(other_cs) {
2904 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E2, -1);
2908 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2909 uint64_t value)
2911 CPUState *other_cs;
2912 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2914 CPU_FOREACH(other_cs) {
2915 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E3, -1);
2919 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2920 uint64_t value)
2922 /* Invalidate by IPA. This has to invalidate any structures that
2923 * contain only stage 2 translation information, but does not need
2924 * to apply to structures that contain combined stage 1 and stage 2
2925 * translation information.
2926 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
2928 ARMCPU *cpu = arm_env_get_cpu(env);
2929 CPUState *cs = CPU(cpu);
2930 uint64_t pageaddr;
2932 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2933 return;
2936 pageaddr = sextract64(value << 12, 0, 48);
2938 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S2NS, -1);
2941 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2942 uint64_t value)
2944 CPUState *other_cs;
2945 uint64_t pageaddr;
2947 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2948 return;
2951 pageaddr = sextract64(value << 12, 0, 48);
2953 CPU_FOREACH(other_cs) {
2954 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S2NS, -1);
2958 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
2959 bool isread)
2961 /* We don't implement EL2, so the only control on DC ZVA is the
2962 * bit in the SCTLR which can prohibit access for EL0.
2964 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
2965 return CP_ACCESS_TRAP;
2967 return CP_ACCESS_OK;
2970 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
2972 ARMCPU *cpu = arm_env_get_cpu(env);
2973 int dzp_bit = 1 << 4;
2975 /* DZP indicates whether DC ZVA access is allowed */
2976 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
2977 dzp_bit = 0;
2979 return cpu->dcz_blocksize | dzp_bit;
2982 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
2983 bool isread)
2985 if (!(env->pstate & PSTATE_SP)) {
2986 /* Access to SP_EL0 is undefined if it's being used as
2987 * the stack pointer.
2989 return CP_ACCESS_TRAP_UNCATEGORIZED;
2991 return CP_ACCESS_OK;
2994 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
2996 return env->pstate & PSTATE_SP;
2999 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3001 update_spsel(env, val);
3004 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3005 uint64_t value)
3007 ARMCPU *cpu = arm_env_get_cpu(env);
3009 if (raw_read(env, ri) == value) {
3010 /* Skip the TLB flush if nothing actually changed; Linux likes
3011 * to do a lot of pointless SCTLR writes.
3013 return;
3016 raw_write(env, ri, value);
3017 /* ??? Lots of these bits are not implemented. */
3018 /* This may enable/disable the MMU, so do a TLB flush. */
3019 tlb_flush(CPU(cpu), 1);
3022 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3023 bool isread)
3025 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
3026 return CP_ACCESS_TRAP_FP_EL2;
3028 if (env->cp15.cptr_el[3] & CPTR_TFP) {
3029 return CP_ACCESS_TRAP_FP_EL3;
3031 return CP_ACCESS_OK;
3034 static const ARMCPRegInfo v8_cp_reginfo[] = {
3035 /* Minimal set of EL0-visible registers. This will need to be expanded
3036 * significantly for system emulation of AArch64 CPUs.
3038 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3039 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3040 .access = PL0_RW, .type = ARM_CP_NZCV },
3041 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3042 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
3043 .type = ARM_CP_NO_RAW,
3044 .access = PL0_RW, .accessfn = aa64_daif_access,
3045 .fieldoffset = offsetof(CPUARMState, daif),
3046 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
3047 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3048 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
3049 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
3050 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3051 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
3052 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
3053 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3054 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
3055 .access = PL0_R, .type = ARM_CP_NO_RAW,
3056 .readfn = aa64_dczid_read },
3057 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3058 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3059 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3060 #ifndef CONFIG_USER_ONLY
3061 /* Avoid overhead of an access check that always passes in user-mode */
3062 .accessfn = aa64_zva_access,
3063 #endif
3065 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3066 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3067 .access = PL1_R, .type = ARM_CP_CURRENTEL },
3068 /* Cache ops: all NOPs since we don't emulate caches */
3069 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3070 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3071 .access = PL1_W, .type = ARM_CP_NOP },
3072 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3073 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3074 .access = PL1_W, .type = ARM_CP_NOP },
3075 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3076 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3077 .access = PL0_W, .type = ARM_CP_NOP,
3078 .accessfn = aa64_cacheop_access },
3079 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3080 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3081 .access = PL1_W, .type = ARM_CP_NOP },
3082 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3083 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3084 .access = PL1_W, .type = ARM_CP_NOP },
3085 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3086 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3087 .access = PL0_W, .type = ARM_CP_NOP,
3088 .accessfn = aa64_cacheop_access },
3089 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3090 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3091 .access = PL1_W, .type = ARM_CP_NOP },
3092 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3093 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3094 .access = PL0_W, .type = ARM_CP_NOP,
3095 .accessfn = aa64_cacheop_access },
3096 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3097 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3098 .access = PL0_W, .type = ARM_CP_NOP,
3099 .accessfn = aa64_cacheop_access },
3100 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3101 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3102 .access = PL1_W, .type = ARM_CP_NOP },
3103 /* TLBI operations */
3104 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
3105 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
3106 .access = PL1_W, .type = ARM_CP_NO_RAW,
3107 .writefn = tlbi_aa64_vmalle1is_write },
3108 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
3109 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
3110 .access = PL1_W, .type = ARM_CP_NO_RAW,
3111 .writefn = tlbi_aa64_vae1is_write },
3112 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
3113 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
3114 .access = PL1_W, .type = ARM_CP_NO_RAW,
3115 .writefn = tlbi_aa64_vmalle1is_write },
3116 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
3117 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
3118 .access = PL1_W, .type = ARM_CP_NO_RAW,
3119 .writefn = tlbi_aa64_vae1is_write },
3120 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
3121 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3122 .access = PL1_W, .type = ARM_CP_NO_RAW,
3123 .writefn = tlbi_aa64_vae1is_write },
3124 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
3125 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3126 .access = PL1_W, .type = ARM_CP_NO_RAW,
3127 .writefn = tlbi_aa64_vae1is_write },
3128 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
3129 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
3130 .access = PL1_W, .type = ARM_CP_NO_RAW,
3131 .writefn = tlbi_aa64_vmalle1_write },
3132 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
3133 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
3134 .access = PL1_W, .type = ARM_CP_NO_RAW,
3135 .writefn = tlbi_aa64_vae1_write },
3136 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
3137 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
3138 .access = PL1_W, .type = ARM_CP_NO_RAW,
3139 .writefn = tlbi_aa64_vmalle1_write },
3140 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
3141 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
3142 .access = PL1_W, .type = ARM_CP_NO_RAW,
3143 .writefn = tlbi_aa64_vae1_write },
3144 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
3145 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3146 .access = PL1_W, .type = ARM_CP_NO_RAW,
3147 .writefn = tlbi_aa64_vae1_write },
3148 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
3149 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3150 .access = PL1_W, .type = ARM_CP_NO_RAW,
3151 .writefn = tlbi_aa64_vae1_write },
3152 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3153 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3154 .access = PL2_W, .type = ARM_CP_NO_RAW,
3155 .writefn = tlbi_aa64_ipas2e1is_write },
3156 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3157 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3158 .access = PL2_W, .type = ARM_CP_NO_RAW,
3159 .writefn = tlbi_aa64_ipas2e1is_write },
3160 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3161 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3162 .access = PL2_W, .type = ARM_CP_NO_RAW,
3163 .writefn = tlbi_aa64_alle1is_write },
3164 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3165 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3166 .access = PL2_W, .type = ARM_CP_NO_RAW,
3167 .writefn = tlbi_aa64_alle1is_write },
3168 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3169 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3170 .access = PL2_W, .type = ARM_CP_NO_RAW,
3171 .writefn = tlbi_aa64_ipas2e1_write },
3172 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3173 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3174 .access = PL2_W, .type = ARM_CP_NO_RAW,
3175 .writefn = tlbi_aa64_ipas2e1_write },
3176 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3177 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3178 .access = PL2_W, .type = ARM_CP_NO_RAW,
3179 .writefn = tlbi_aa64_alle1_write },
3180 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3181 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3182 .access = PL2_W, .type = ARM_CP_NO_RAW,
3183 .writefn = tlbi_aa64_alle1is_write },
3184 #ifndef CONFIG_USER_ONLY
3185 /* 64 bit address translation operations */
3186 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3187 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
3188 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3189 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3190 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
3191 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3192 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3193 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
3194 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3195 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3196 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
3197 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3198 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
3199 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
3200 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3201 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
3202 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
3203 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3204 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
3205 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
3206 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3207 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
3208 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
3209 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3210 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3211 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3212 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3213 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3214 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3215 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3216 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3217 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3218 .type = ARM_CP_ALIAS,
3219 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3220 .access = PL1_RW, .resetvalue = 0,
3221 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3222 .writefn = par_write },
3223 #endif
3224 /* TLB invalidate last level of translation table walk */
3225 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
3226 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
3227 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
3228 .type = ARM_CP_NO_RAW, .access = PL1_W,
3229 .writefn = tlbimvaa_is_write },
3230 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
3231 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
3232 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
3233 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
3234 /* 32 bit cache operations */
3235 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3236 .type = ARM_CP_NOP, .access = PL1_W },
3237 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3238 .type = ARM_CP_NOP, .access = PL1_W },
3239 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3240 .type = ARM_CP_NOP, .access = PL1_W },
3241 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3242 .type = ARM_CP_NOP, .access = PL1_W },
3243 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3244 .type = ARM_CP_NOP, .access = PL1_W },
3245 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3246 .type = ARM_CP_NOP, .access = PL1_W },
3247 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3248 .type = ARM_CP_NOP, .access = PL1_W },
3249 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3250 .type = ARM_CP_NOP, .access = PL1_W },
3251 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3252 .type = ARM_CP_NOP, .access = PL1_W },
3253 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3254 .type = ARM_CP_NOP, .access = PL1_W },
3255 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3256 .type = ARM_CP_NOP, .access = PL1_W },
3257 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3258 .type = ARM_CP_NOP, .access = PL1_W },
3259 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3260 .type = ARM_CP_NOP, .access = PL1_W },
3261 /* MMU Domain access control / MPU write buffer control */
3262 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3263 .access = PL1_RW, .resetvalue = 0,
3264 .writefn = dacr_write, .raw_writefn = raw_write,
3265 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3266 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
3267 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
3268 .type = ARM_CP_ALIAS,
3269 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
3270 .access = PL1_RW,
3271 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
3272 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
3273 .type = ARM_CP_ALIAS,
3274 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
3275 .access = PL1_RW,
3276 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
3277 /* We rely on the access checks not allowing the guest to write to the
3278 * state field when SPSel indicates that it's being used as the stack
3279 * pointer.
3281 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3282 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3283 .access = PL1_RW, .accessfn = sp_el0_access,
3284 .type = ARM_CP_ALIAS,
3285 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
3286 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3287 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
3288 .access = PL2_RW, .type = ARM_CP_ALIAS,
3289 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
3290 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3291 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
3292 .type = ARM_CP_NO_RAW,
3293 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
3294 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3295 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3296 .type = ARM_CP_ALIAS,
3297 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3298 .access = PL2_RW, .accessfn = fpexc32_access },
3299 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3300 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3301 .access = PL2_RW, .resetvalue = 0,
3302 .writefn = dacr_write, .raw_writefn = raw_write,
3303 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3304 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3305 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3306 .access = PL2_RW, .resetvalue = 0,
3307 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3308 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3309 .type = ARM_CP_ALIAS,
3310 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3311 .access = PL2_RW,
3312 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3313 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3314 .type = ARM_CP_ALIAS,
3315 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3316 .access = PL2_RW,
3317 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3318 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3319 .type = ARM_CP_ALIAS,
3320 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3321 .access = PL2_RW,
3322 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3323 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3324 .type = ARM_CP_ALIAS,
3325 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3326 .access = PL2_RW,
3327 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
3328 REGINFO_SENTINEL
3331 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
3332 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
3333 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3334 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3335 .access = PL2_RW,
3336 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3337 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3338 .type = ARM_CP_NO_RAW,
3339 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3340 .access = PL2_RW,
3341 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
3342 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3343 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3344 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3345 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3346 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3347 .access = PL2_RW, .type = ARM_CP_CONST,
3348 .resetvalue = 0 },
3349 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3350 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3351 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3352 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3353 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3354 .access = PL2_RW, .type = ARM_CP_CONST,
3355 .resetvalue = 0 },
3356 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3357 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3358 .access = PL2_RW, .type = ARM_CP_CONST,
3359 .resetvalue = 0 },
3360 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3361 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3362 .access = PL2_RW, .type = ARM_CP_CONST,
3363 .resetvalue = 0 },
3364 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3365 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3366 .access = PL2_RW, .type = ARM_CP_CONST,
3367 .resetvalue = 0 },
3368 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3369 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3370 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3371 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3372 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3373 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3374 .type = ARM_CP_CONST, .resetvalue = 0 },
3375 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3376 .cp = 15, .opc1 = 6, .crm = 2,
3377 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3378 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3379 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3380 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3381 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3382 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3383 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3384 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3385 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3386 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3387 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3388 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3389 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3390 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3391 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3392 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3393 .resetvalue = 0 },
3394 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3395 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3396 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3397 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3398 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3399 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3400 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3401 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3402 .resetvalue = 0 },
3403 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3404 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3405 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3406 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3407 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3408 .resetvalue = 0 },
3409 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3410 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3411 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3412 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3413 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3414 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3415 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3416 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3417 .access = PL2_RW, .accessfn = access_tda,
3418 .type = ARM_CP_CONST, .resetvalue = 0 },
3419 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3420 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3421 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3422 .type = ARM_CP_CONST, .resetvalue = 0 },
3423 REGINFO_SENTINEL
3426 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3428 ARMCPU *cpu = arm_env_get_cpu(env);
3429 uint64_t valid_mask = HCR_MASK;
3431 if (arm_feature(env, ARM_FEATURE_EL3)) {
3432 valid_mask &= ~HCR_HCD;
3433 } else {
3434 valid_mask &= ~HCR_TSC;
3437 /* Clear RES0 bits. */
3438 value &= valid_mask;
3440 /* These bits change the MMU setup:
3441 * HCR_VM enables stage 2 translation
3442 * HCR_PTW forbids certain page-table setups
3443 * HCR_DC Disables stage1 and enables stage2 translation
3445 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3446 tlb_flush(CPU(cpu), 1);
3448 raw_write(env, ri, value);
3451 static const ARMCPRegInfo el2_cp_reginfo[] = {
3452 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3453 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3454 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3455 .writefn = hcr_write },
3456 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
3457 .type = ARM_CP_ALIAS,
3458 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3459 .access = PL2_RW,
3460 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
3461 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
3462 .type = ARM_CP_ALIAS,
3463 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3464 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
3465 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3466 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3467 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
3468 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
3469 .type = ARM_CP_ALIAS,
3470 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
3471 .access = PL2_RW,
3472 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
3473 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3474 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3475 .access = PL2_RW, .writefn = vbar_write,
3476 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3477 .resetvalue = 0 },
3478 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3479 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
3480 .access = PL3_RW, .type = ARM_CP_ALIAS,
3481 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
3482 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3483 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3484 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3485 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
3486 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3487 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3488 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3489 .resetvalue = 0 },
3490 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3491 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3492 .access = PL2_RW, .type = ARM_CP_ALIAS,
3493 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
3494 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3495 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3496 .access = PL2_RW, .type = ARM_CP_CONST,
3497 .resetvalue = 0 },
3498 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3499 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3500 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3501 .access = PL2_RW, .type = ARM_CP_CONST,
3502 .resetvalue = 0 },
3503 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3504 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3505 .access = PL2_RW, .type = ARM_CP_CONST,
3506 .resetvalue = 0 },
3507 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3508 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3509 .access = PL2_RW, .type = ARM_CP_CONST,
3510 .resetvalue = 0 },
3511 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3512 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3513 .access = PL2_RW, .writefn = vmsa_tcr_el1_write,
3514 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3515 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
3516 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3517 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3518 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3519 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3520 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3521 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3522 .access = PL2_RW, .type = ARM_CP_ALIAS,
3523 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3524 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3525 .cp = 15, .opc1 = 6, .crm = 2,
3526 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3527 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3528 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3529 .writefn = vttbr_write },
3530 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3531 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3532 .access = PL2_RW, .writefn = vttbr_write,
3533 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
3534 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3535 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3536 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3537 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
3538 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3539 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3540 .access = PL2_RW, .resetvalue = 0,
3541 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
3542 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3543 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3544 .access = PL2_RW, .resetvalue = 0,
3545 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3546 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3547 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3548 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3549 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3550 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3551 .type = ARM_CP_NO_RAW, .access = PL2_W,
3552 .writefn = tlbi_aa64_alle2_write },
3553 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3554 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3555 .type = ARM_CP_NO_RAW, .access = PL2_W,
3556 .writefn = tlbi_aa64_vae2_write },
3557 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3558 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3559 .access = PL2_W, .type = ARM_CP_NO_RAW,
3560 .writefn = tlbi_aa64_vae2_write },
3561 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3562 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3563 .access = PL2_W, .type = ARM_CP_NO_RAW,
3564 .writefn = tlbi_aa64_alle2is_write },
3565 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3566 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3567 .type = ARM_CP_NO_RAW, .access = PL2_W,
3568 .writefn = tlbi_aa64_vae2is_write },
3569 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3570 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3571 .access = PL2_W, .type = ARM_CP_NO_RAW,
3572 .writefn = tlbi_aa64_vae2is_write },
3573 #ifndef CONFIG_USER_ONLY
3574 /* Unlike the other EL2-related AT operations, these must
3575 * UNDEF from EL3 if EL2 is not implemented, which is why we
3576 * define them here rather than with the rest of the AT ops.
3578 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3579 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3580 .access = PL2_W, .accessfn = at_s1e2_access,
3581 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3582 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3583 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3584 .access = PL2_W, .accessfn = at_s1e2_access,
3585 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3586 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3587 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3588 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3589 * to behave as if SCR.NS was 1.
3591 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3592 .access = PL2_W,
3593 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3594 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3595 .access = PL2_W,
3596 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3597 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3598 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3599 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3600 * reset values as IMPDEF. We choose to reset to 3 to comply with
3601 * both ARMv7 and ARMv8.
3603 .access = PL2_RW, .resetvalue = 3,
3604 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
3605 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3606 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3607 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3608 .writefn = gt_cntvoff_write,
3609 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3610 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3611 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3612 .writefn = gt_cntvoff_write,
3613 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3614 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3615 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3616 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3617 .type = ARM_CP_IO, .access = PL2_RW,
3618 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3619 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3620 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3621 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3622 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3623 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3624 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3625 .type = ARM_CP_IO, .access = PL2_RW,
3626 .resetfn = gt_hyp_timer_reset,
3627 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3628 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3629 .type = ARM_CP_IO,
3630 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3631 .access = PL2_RW,
3632 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3633 .resetvalue = 0,
3634 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
3635 #endif
3636 /* The only field of MDCR_EL2 that has a defined architectural reset value
3637 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
3638 * don't impelment any PMU event counters, so using zero as a reset
3639 * value for MDCR_EL2 is okay
3641 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3642 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3643 .access = PL2_RW, .resetvalue = 0,
3644 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
3645 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
3646 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3647 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3648 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3649 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
3650 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3651 .access = PL2_RW,
3652 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3653 REGINFO_SENTINEL
3656 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
3657 bool isread)
3659 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
3660 * At Secure EL1 it traps to EL3.
3662 if (arm_current_el(env) == 3) {
3663 return CP_ACCESS_OK;
3665 if (arm_is_secure_below_el3(env)) {
3666 return CP_ACCESS_TRAP_EL3;
3668 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
3669 if (isread) {
3670 return CP_ACCESS_OK;
3672 return CP_ACCESS_TRAP_UNCATEGORIZED;
3675 static const ARMCPRegInfo el3_cp_reginfo[] = {
3676 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
3677 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
3678 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
3679 .resetvalue = 0, .writefn = scr_write },
3680 { .name = "SCR", .type = ARM_CP_ALIAS,
3681 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
3682 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3683 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
3684 .writefn = scr_write },
3685 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3686 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3687 .resetvalue = 0,
3688 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3689 { .name = "SDCR", .type = ARM_CP_ALIAS,
3690 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3691 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3692 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
3693 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
3694 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
3695 .access = PL3_RW, .resetvalue = 0,
3696 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
3697 { .name = "SDER",
3698 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
3699 .access = PL3_RW, .resetvalue = 0,
3700 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
3701 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
3702 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3703 .writefn = vbar_write, .resetvalue = 0,
3704 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
3705 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
3706 .type = ARM_CP_ALIAS, /* reset handled by AArch32 view */
3707 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
3708 .access = PL3_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3709 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]) },
3710 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
3711 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
3712 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3713 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
3714 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
3715 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
3716 .access = PL3_RW, .writefn = vmsa_tcr_el1_write,
3717 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
3718 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
3719 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
3720 .type = ARM_CP_ALIAS,
3721 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
3722 .access = PL3_RW,
3723 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
3724 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
3725 .type = ARM_CP_ALIAS,
3726 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
3727 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
3728 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
3729 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
3730 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
3731 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
3732 .type = ARM_CP_ALIAS,
3733 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
3734 .access = PL3_RW,
3735 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
3736 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
3737 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
3738 .access = PL3_RW, .writefn = vbar_write,
3739 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
3740 .resetvalue = 0 },
3741 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
3742 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
3743 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
3744 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
3745 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
3746 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
3747 .access = PL3_RW, .resetvalue = 0,
3748 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
3749 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
3750 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
3751 .access = PL3_RW, .type = ARM_CP_CONST,
3752 .resetvalue = 0 },
3753 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
3754 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
3755 .access = PL3_RW, .type = ARM_CP_CONST,
3756 .resetvalue = 0 },
3757 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
3758 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
3759 .access = PL3_RW, .type = ARM_CP_CONST,
3760 .resetvalue = 0 },
3761 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
3762 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
3763 .access = PL3_W, .type = ARM_CP_NO_RAW,
3764 .writefn = tlbi_aa64_alle3is_write },
3765 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
3766 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
3767 .access = PL3_W, .type = ARM_CP_NO_RAW,
3768 .writefn = tlbi_aa64_vae3is_write },
3769 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
3770 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
3771 .access = PL3_W, .type = ARM_CP_NO_RAW,
3772 .writefn = tlbi_aa64_vae3is_write },
3773 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
3774 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
3775 .access = PL3_W, .type = ARM_CP_NO_RAW,
3776 .writefn = tlbi_aa64_alle3_write },
3777 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
3778 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
3779 .access = PL3_W, .type = ARM_CP_NO_RAW,
3780 .writefn = tlbi_aa64_vae3_write },
3781 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
3782 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
3783 .access = PL3_W, .type = ARM_CP_NO_RAW,
3784 .writefn = tlbi_aa64_vae3_write },
3785 REGINFO_SENTINEL
3788 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3789 bool isread)
3791 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
3792 * but the AArch32 CTR has its own reginfo struct)
3794 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
3795 return CP_ACCESS_TRAP;
3797 return CP_ACCESS_OK;
3800 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3801 uint64_t value)
3803 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
3804 * read via a bit in OSLSR_EL1.
3806 int oslock;
3808 if (ri->state == ARM_CP_STATE_AA32) {
3809 oslock = (value == 0xC5ACCE55);
3810 } else {
3811 oslock = value & 1;
3814 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
3817 static const ARMCPRegInfo debug_cp_reginfo[] = {
3818 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
3819 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
3820 * unlike DBGDRAR it is never accessible from EL0.
3821 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
3822 * accessor.
3824 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
3825 .access = PL0_R, .accessfn = access_tdra,
3826 .type = ARM_CP_CONST, .resetvalue = 0 },
3827 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
3828 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
3829 .access = PL1_R, .accessfn = access_tdra,
3830 .type = ARM_CP_CONST, .resetvalue = 0 },
3831 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3832 .access = PL0_R, .accessfn = access_tdra,
3833 .type = ARM_CP_CONST, .resetvalue = 0 },
3834 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
3835 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
3836 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
3837 .access = PL1_RW, .accessfn = access_tda,
3838 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
3839 .resetvalue = 0 },
3840 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
3841 * We don't implement the configurable EL0 access.
3843 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
3844 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
3845 .type = ARM_CP_ALIAS,
3846 .access = PL1_R, .accessfn = access_tda,
3847 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
3848 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
3849 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
3850 .access = PL1_W, .type = ARM_CP_NO_RAW,
3851 .accessfn = access_tdosa,
3852 .writefn = oslar_write },
3853 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
3854 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
3855 .access = PL1_R, .resetvalue = 10,
3856 .accessfn = access_tdosa,
3857 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
3858 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
3859 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
3860 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
3861 .access = PL1_RW, .accessfn = access_tdosa,
3862 .type = ARM_CP_NOP },
3863 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
3864 * implement vector catch debug events yet.
3866 { .name = "DBGVCR",
3867 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
3868 .access = PL1_RW, .accessfn = access_tda,
3869 .type = ARM_CP_NOP },
3870 REGINFO_SENTINEL
3873 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
3874 /* 64 bit access versions of the (dummy) debug registers */
3875 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
3876 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3877 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
3878 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3879 REGINFO_SENTINEL
3882 void hw_watchpoint_update(ARMCPU *cpu, int n)
3884 CPUARMState *env = &cpu->env;
3885 vaddr len = 0;
3886 vaddr wvr = env->cp15.dbgwvr[n];
3887 uint64_t wcr = env->cp15.dbgwcr[n];
3888 int mask;
3889 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
3891 if (env->cpu_watchpoint[n]) {
3892 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
3893 env->cpu_watchpoint[n] = NULL;
3896 if (!extract64(wcr, 0, 1)) {
3897 /* E bit clear : watchpoint disabled */
3898 return;
3901 switch (extract64(wcr, 3, 2)) {
3902 case 0:
3903 /* LSC 00 is reserved and must behave as if the wp is disabled */
3904 return;
3905 case 1:
3906 flags |= BP_MEM_READ;
3907 break;
3908 case 2:
3909 flags |= BP_MEM_WRITE;
3910 break;
3911 case 3:
3912 flags |= BP_MEM_ACCESS;
3913 break;
3916 /* Attempts to use both MASK and BAS fields simultaneously are
3917 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
3918 * thus generating a watchpoint for every byte in the masked region.
3920 mask = extract64(wcr, 24, 4);
3921 if (mask == 1 || mask == 2) {
3922 /* Reserved values of MASK; we must act as if the mask value was
3923 * some non-reserved value, or as if the watchpoint were disabled.
3924 * We choose the latter.
3926 return;
3927 } else if (mask) {
3928 /* Watchpoint covers an aligned area up to 2GB in size */
3929 len = 1ULL << mask;
3930 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
3931 * whether the watchpoint fires when the unmasked bits match; we opt
3932 * to generate the exceptions.
3934 wvr &= ~(len - 1);
3935 } else {
3936 /* Watchpoint covers bytes defined by the byte address select bits */
3937 int bas = extract64(wcr, 5, 8);
3938 int basstart;
3940 if (bas == 0) {
3941 /* This must act as if the watchpoint is disabled */
3942 return;
3945 if (extract64(wvr, 2, 1)) {
3946 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
3947 * ignored, and BAS[3:0] define which bytes to watch.
3949 bas &= 0xf;
3951 /* The BAS bits are supposed to be programmed to indicate a contiguous
3952 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
3953 * we fire for each byte in the word/doubleword addressed by the WVR.
3954 * We choose to ignore any non-zero bits after the first range of 1s.
3956 basstart = ctz32(bas);
3957 len = cto32(bas >> basstart);
3958 wvr += basstart;
3961 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
3962 &env->cpu_watchpoint[n]);
3965 void hw_watchpoint_update_all(ARMCPU *cpu)
3967 int i;
3968 CPUARMState *env = &cpu->env;
3970 /* Completely clear out existing QEMU watchpoints and our array, to
3971 * avoid possible stale entries following migration load.
3973 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
3974 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
3976 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
3977 hw_watchpoint_update(cpu, i);
3981 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3982 uint64_t value)
3984 ARMCPU *cpu = arm_env_get_cpu(env);
3985 int i = ri->crm;
3987 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
3988 * register reads and behaves as if values written are sign extended.
3989 * Bits [1:0] are RES0.
3991 value = sextract64(value, 0, 49) & ~3ULL;
3993 raw_write(env, ri, value);
3994 hw_watchpoint_update(cpu, i);
3997 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3998 uint64_t value)
4000 ARMCPU *cpu = arm_env_get_cpu(env);
4001 int i = ri->crm;
4003 raw_write(env, ri, value);
4004 hw_watchpoint_update(cpu, i);
4007 void hw_breakpoint_update(ARMCPU *cpu, int n)
4009 CPUARMState *env = &cpu->env;
4010 uint64_t bvr = env->cp15.dbgbvr[n];
4011 uint64_t bcr = env->cp15.dbgbcr[n];
4012 vaddr addr;
4013 int bt;
4014 int flags = BP_CPU;
4016 if (env->cpu_breakpoint[n]) {
4017 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4018 env->cpu_breakpoint[n] = NULL;
4021 if (!extract64(bcr, 0, 1)) {
4022 /* E bit clear : watchpoint disabled */
4023 return;
4026 bt = extract64(bcr, 20, 4);
4028 switch (bt) {
4029 case 4: /* unlinked address mismatch (reserved if AArch64) */
4030 case 5: /* linked address mismatch (reserved if AArch64) */
4031 qemu_log_mask(LOG_UNIMP,
4032 "arm: address mismatch breakpoint types not implemented");
4033 return;
4034 case 0: /* unlinked address match */
4035 case 1: /* linked address match */
4037 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4038 * we behave as if the register was sign extended. Bits [1:0] are
4039 * RES0. The BAS field is used to allow setting breakpoints on 16
4040 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4041 * a bp will fire if the addresses covered by the bp and the addresses
4042 * covered by the insn overlap but the insn doesn't start at the
4043 * start of the bp address range. We choose to require the insn and
4044 * the bp to have the same address. The constraints on writing to
4045 * BAS enforced in dbgbcr_write mean we have only four cases:
4046 * 0b0000 => no breakpoint
4047 * 0b0011 => breakpoint on addr
4048 * 0b1100 => breakpoint on addr + 2
4049 * 0b1111 => breakpoint on addr
4050 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4052 int bas = extract64(bcr, 5, 4);
4053 addr = sextract64(bvr, 0, 49) & ~3ULL;
4054 if (bas == 0) {
4055 return;
4057 if (bas == 0xc) {
4058 addr += 2;
4060 break;
4062 case 2: /* unlinked context ID match */
4063 case 8: /* unlinked VMID match (reserved if no EL2) */
4064 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4065 qemu_log_mask(LOG_UNIMP,
4066 "arm: unlinked context breakpoint types not implemented");
4067 return;
4068 case 9: /* linked VMID match (reserved if no EL2) */
4069 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4070 case 3: /* linked context ID match */
4071 default:
4072 /* We must generate no events for Linked context matches (unless
4073 * they are linked to by some other bp/wp, which is handled in
4074 * updates for the linking bp/wp). We choose to also generate no events
4075 * for reserved values.
4077 return;
4080 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4083 void hw_breakpoint_update_all(ARMCPU *cpu)
4085 int i;
4086 CPUARMState *env = &cpu->env;
4088 /* Completely clear out existing QEMU breakpoints and our array, to
4089 * avoid possible stale entries following migration load.
4091 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4092 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4094 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4095 hw_breakpoint_update(cpu, i);
4099 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4100 uint64_t value)
4102 ARMCPU *cpu = arm_env_get_cpu(env);
4103 int i = ri->crm;
4105 raw_write(env, ri, value);
4106 hw_breakpoint_update(cpu, i);
4109 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4110 uint64_t value)
4112 ARMCPU *cpu = arm_env_get_cpu(env);
4113 int i = ri->crm;
4115 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4116 * copy of BAS[0].
4118 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4119 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4121 raw_write(env, ri, value);
4122 hw_breakpoint_update(cpu, i);
4125 static void define_debug_regs(ARMCPU *cpu)
4127 /* Define v7 and v8 architectural debug registers.
4128 * These are just dummy implementations for now.
4130 int i;
4131 int wrps, brps, ctx_cmps;
4132 ARMCPRegInfo dbgdidr = {
4133 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
4134 .access = PL0_R, .accessfn = access_tda,
4135 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
4138 /* Note that all these register fields hold "number of Xs minus 1". */
4139 brps = extract32(cpu->dbgdidr, 24, 4);
4140 wrps = extract32(cpu->dbgdidr, 28, 4);
4141 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4143 assert(ctx_cmps <= brps);
4145 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4146 * of the debug registers such as number of breakpoints;
4147 * check that if they both exist then they agree.
4149 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4150 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4151 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
4152 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
4155 define_one_arm_cp_reg(cpu, &dbgdidr);
4156 define_arm_cp_regs(cpu, debug_cp_reginfo);
4158 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4159 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4162 for (i = 0; i < brps + 1; i++) {
4163 ARMCPRegInfo dbgregs[] = {
4164 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4165 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
4166 .access = PL1_RW, .accessfn = access_tda,
4167 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4168 .writefn = dbgbvr_write, .raw_writefn = raw_write
4170 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4171 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
4172 .access = PL1_RW, .accessfn = access_tda,
4173 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4174 .writefn = dbgbcr_write, .raw_writefn = raw_write
4176 REGINFO_SENTINEL
4178 define_arm_cp_regs(cpu, dbgregs);
4181 for (i = 0; i < wrps + 1; i++) {
4182 ARMCPRegInfo dbgregs[] = {
4183 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4184 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
4185 .access = PL1_RW, .accessfn = access_tda,
4186 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4187 .writefn = dbgwvr_write, .raw_writefn = raw_write
4189 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4190 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
4191 .access = PL1_RW, .accessfn = access_tda,
4192 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4193 .writefn = dbgwcr_write, .raw_writefn = raw_write
4195 REGINFO_SENTINEL
4197 define_arm_cp_regs(cpu, dbgregs);
4201 void register_cp_regs_for_features(ARMCPU *cpu)
4203 /* Register all the coprocessor registers based on feature bits */
4204 CPUARMState *env = &cpu->env;
4205 if (arm_feature(env, ARM_FEATURE_M)) {
4206 /* M profile has no coprocessor registers */
4207 return;
4210 define_arm_cp_regs(cpu, cp_reginfo);
4211 if (!arm_feature(env, ARM_FEATURE_V8)) {
4212 /* Must go early as it is full of wildcards that may be
4213 * overridden by later definitions.
4215 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4218 if (arm_feature(env, ARM_FEATURE_V6)) {
4219 /* The ID registers all have impdef reset values */
4220 ARMCPRegInfo v6_idregs[] = {
4221 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4222 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4223 .access = PL1_R, .type = ARM_CP_CONST,
4224 .resetvalue = cpu->id_pfr0 },
4225 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4226 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
4227 .access = PL1_R, .type = ARM_CP_CONST,
4228 .resetvalue = cpu->id_pfr1 },
4229 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4230 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4231 .access = PL1_R, .type = ARM_CP_CONST,
4232 .resetvalue = cpu->id_dfr0 },
4233 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4234 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4235 .access = PL1_R, .type = ARM_CP_CONST,
4236 .resetvalue = cpu->id_afr0 },
4237 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4238 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4239 .access = PL1_R, .type = ARM_CP_CONST,
4240 .resetvalue = cpu->id_mmfr0 },
4241 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4242 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4243 .access = PL1_R, .type = ARM_CP_CONST,
4244 .resetvalue = cpu->id_mmfr1 },
4245 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4246 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4247 .access = PL1_R, .type = ARM_CP_CONST,
4248 .resetvalue = cpu->id_mmfr2 },
4249 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4250 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4251 .access = PL1_R, .type = ARM_CP_CONST,
4252 .resetvalue = cpu->id_mmfr3 },
4253 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4254 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4255 .access = PL1_R, .type = ARM_CP_CONST,
4256 .resetvalue = cpu->id_isar0 },
4257 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4258 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4259 .access = PL1_R, .type = ARM_CP_CONST,
4260 .resetvalue = cpu->id_isar1 },
4261 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4262 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4263 .access = PL1_R, .type = ARM_CP_CONST,
4264 .resetvalue = cpu->id_isar2 },
4265 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4266 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4267 .access = PL1_R, .type = ARM_CP_CONST,
4268 .resetvalue = cpu->id_isar3 },
4269 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4270 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4271 .access = PL1_R, .type = ARM_CP_CONST,
4272 .resetvalue = cpu->id_isar4 },
4273 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4274 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4275 .access = PL1_R, .type = ARM_CP_CONST,
4276 .resetvalue = cpu->id_isar5 },
4277 /* 6..7 are as yet unallocated and must RAZ */
4278 { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
4279 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
4280 .resetvalue = 0 },
4281 { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
4282 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
4283 .resetvalue = 0 },
4284 REGINFO_SENTINEL
4286 define_arm_cp_regs(cpu, v6_idregs);
4287 define_arm_cp_regs(cpu, v6_cp_reginfo);
4288 } else {
4289 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4291 if (arm_feature(env, ARM_FEATURE_V6K)) {
4292 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4294 if (arm_feature(env, ARM_FEATURE_V7MP) &&
4295 !arm_feature(env, ARM_FEATURE_MPU)) {
4296 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4298 if (arm_feature(env, ARM_FEATURE_V7)) {
4299 /* v7 performance monitor control register: same implementor
4300 * field as main ID register, and we implement only the cycle
4301 * count register.
4303 #ifndef CONFIG_USER_ONLY
4304 ARMCPRegInfo pmcr = {
4305 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
4306 .access = PL0_RW,
4307 .type = ARM_CP_IO | ARM_CP_ALIAS,
4308 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
4309 .accessfn = pmreg_access, .writefn = pmcr_write,
4310 .raw_writefn = raw_write,
4312 ARMCPRegInfo pmcr64 = {
4313 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4314 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4315 .access = PL0_RW, .accessfn = pmreg_access,
4316 .type = ARM_CP_IO,
4317 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4318 .resetvalue = cpu->midr & 0xff000000,
4319 .writefn = pmcr_write, .raw_writefn = raw_write,
4321 define_one_arm_cp_reg(cpu, &pmcr);
4322 define_one_arm_cp_reg(cpu, &pmcr64);
4323 #endif
4324 ARMCPRegInfo clidr = {
4325 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4326 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
4327 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4329 define_one_arm_cp_reg(cpu, &clidr);
4330 define_arm_cp_regs(cpu, v7_cp_reginfo);
4331 define_debug_regs(cpu);
4332 } else {
4333 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
4335 if (arm_feature(env, ARM_FEATURE_V8)) {
4336 /* AArch64 ID registers, which all have impdef reset values */
4337 ARMCPRegInfo v8_idregs[] = {
4338 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4339 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4340 .access = PL1_R, .type = ARM_CP_CONST,
4341 .resetvalue = cpu->id_aa64pfr0 },
4342 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4343 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4344 .access = PL1_R, .type = ARM_CP_CONST,
4345 .resetvalue = cpu->id_aa64pfr1},
4346 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4347 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4348 .access = PL1_R, .type = ARM_CP_CONST,
4349 /* We mask out the PMUVer field, because we don't currently
4350 * implement the PMU. Not advertising it prevents the guest
4351 * from trying to use it and getting UNDEFs on registers we
4352 * don't implement.
4354 .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
4355 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4356 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4357 .access = PL1_R, .type = ARM_CP_CONST,
4358 .resetvalue = cpu->id_aa64dfr1 },
4359 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4360 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4361 .access = PL1_R, .type = ARM_CP_CONST,
4362 .resetvalue = cpu->id_aa64afr0 },
4363 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4364 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4365 .access = PL1_R, .type = ARM_CP_CONST,
4366 .resetvalue = cpu->id_aa64afr1 },
4367 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4368 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4369 .access = PL1_R, .type = ARM_CP_CONST,
4370 .resetvalue = cpu->id_aa64isar0 },
4371 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4372 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4373 .access = PL1_R, .type = ARM_CP_CONST,
4374 .resetvalue = cpu->id_aa64isar1 },
4375 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4376 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4377 .access = PL1_R, .type = ARM_CP_CONST,
4378 .resetvalue = cpu->id_aa64mmfr0 },
4379 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4380 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4381 .access = PL1_R, .type = ARM_CP_CONST,
4382 .resetvalue = cpu->id_aa64mmfr1 },
4383 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4384 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4385 .access = PL1_R, .type = ARM_CP_CONST,
4386 .resetvalue = cpu->mvfr0 },
4387 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4388 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4389 .access = PL1_R, .type = ARM_CP_CONST,
4390 .resetvalue = cpu->mvfr1 },
4391 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4392 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4393 .access = PL1_R, .type = ARM_CP_CONST,
4394 .resetvalue = cpu->mvfr2 },
4395 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
4396 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
4397 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4398 .resetvalue = cpu->pmceid0 },
4399 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
4400 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
4401 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4402 .resetvalue = cpu->pmceid0 },
4403 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
4404 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
4405 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4406 .resetvalue = cpu->pmceid1 },
4407 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
4408 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
4409 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4410 .resetvalue = cpu->pmceid1 },
4411 REGINFO_SENTINEL
4413 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4414 if (!arm_feature(env, ARM_FEATURE_EL3) &&
4415 !arm_feature(env, ARM_FEATURE_EL2)) {
4416 ARMCPRegInfo rvbar = {
4417 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4418 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4419 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4421 define_one_arm_cp_reg(cpu, &rvbar);
4423 define_arm_cp_regs(cpu, v8_idregs);
4424 define_arm_cp_regs(cpu, v8_cp_reginfo);
4426 if (arm_feature(env, ARM_FEATURE_EL2)) {
4427 uint64_t vmpidr_def = mpidr_read_val(env);
4428 ARMCPRegInfo vpidr_regs[] = {
4429 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
4430 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4431 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4432 .resetvalue = cpu->midr,
4433 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4434 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
4435 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4436 .access = PL2_RW, .resetvalue = cpu->midr,
4437 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4438 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
4439 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4440 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4441 .resetvalue = vmpidr_def,
4442 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4443 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
4444 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4445 .access = PL2_RW,
4446 .resetvalue = vmpidr_def,
4447 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4448 REGINFO_SENTINEL
4450 define_arm_cp_regs(cpu, vpidr_regs);
4451 define_arm_cp_regs(cpu, el2_cp_reginfo);
4452 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4453 if (!arm_feature(env, ARM_FEATURE_EL3)) {
4454 ARMCPRegInfo rvbar = {
4455 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4456 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4457 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4459 define_one_arm_cp_reg(cpu, &rvbar);
4461 } else {
4462 /* If EL2 is missing but higher ELs are enabled, we need to
4463 * register the no_el2 reginfos.
4465 if (arm_feature(env, ARM_FEATURE_EL3)) {
4466 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
4467 * of MIDR_EL1 and MPIDR_EL1.
4469 ARMCPRegInfo vpidr_regs[] = {
4470 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4471 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4472 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4473 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
4474 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4475 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4476 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4477 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4478 .type = ARM_CP_NO_RAW,
4479 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
4480 REGINFO_SENTINEL
4482 define_arm_cp_regs(cpu, vpidr_regs);
4483 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
4486 if (arm_feature(env, ARM_FEATURE_EL3)) {
4487 define_arm_cp_regs(cpu, el3_cp_reginfo);
4488 ARMCPRegInfo rvbar = {
4489 .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4490 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4491 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar
4493 define_one_arm_cp_reg(cpu, &rvbar);
4495 /* The behaviour of NSACR is sufficiently various that we don't
4496 * try to describe it in a single reginfo:
4497 * if EL3 is 64 bit, then trap to EL3 from S EL1,
4498 * reads as constant 0xc00 from NS EL1 and NS EL2
4499 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
4500 * if v7 without EL3, register doesn't exist
4501 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
4503 if (arm_feature(env, ARM_FEATURE_EL3)) {
4504 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4505 ARMCPRegInfo nsacr = {
4506 .name = "NSACR", .type = ARM_CP_CONST,
4507 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4508 .access = PL1_RW, .accessfn = nsacr_access,
4509 .resetvalue = 0xc00
4511 define_one_arm_cp_reg(cpu, &nsacr);
4512 } else {
4513 ARMCPRegInfo nsacr = {
4514 .name = "NSACR",
4515 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4516 .access = PL3_RW | PL1_R,
4517 .resetvalue = 0,
4518 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
4520 define_one_arm_cp_reg(cpu, &nsacr);
4522 } else {
4523 if (arm_feature(env, ARM_FEATURE_V8)) {
4524 ARMCPRegInfo nsacr = {
4525 .name = "NSACR", .type = ARM_CP_CONST,
4526 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4527 .access = PL1_R,
4528 .resetvalue = 0xc00
4530 define_one_arm_cp_reg(cpu, &nsacr);
4534 if (arm_feature(env, ARM_FEATURE_MPU)) {
4535 if (arm_feature(env, ARM_FEATURE_V6)) {
4536 /* PMSAv6 not implemented */
4537 assert(arm_feature(env, ARM_FEATURE_V7));
4538 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4539 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
4540 } else {
4541 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
4543 } else {
4544 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4545 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4547 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
4548 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
4550 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
4551 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
4553 if (arm_feature(env, ARM_FEATURE_VAPA)) {
4554 define_arm_cp_regs(cpu, vapa_cp_reginfo);
4556 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
4557 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
4559 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
4560 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
4562 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
4563 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
4565 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
4566 define_arm_cp_regs(cpu, omap_cp_reginfo);
4568 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
4569 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
4571 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4572 define_arm_cp_regs(cpu, xscale_cp_reginfo);
4574 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
4575 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
4577 if (arm_feature(env, ARM_FEATURE_LPAE)) {
4578 define_arm_cp_regs(cpu, lpae_cp_reginfo);
4580 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
4581 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
4582 * be read-only (ie write causes UNDEF exception).
4585 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
4586 /* Pre-v8 MIDR space.
4587 * Note that the MIDR isn't a simple constant register because
4588 * of the TI925 behaviour where writes to another register can
4589 * cause the MIDR value to change.
4591 * Unimplemented registers in the c15 0 0 0 space default to
4592 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
4593 * and friends override accordingly.
4595 { .name = "MIDR",
4596 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
4597 .access = PL1_R, .resetvalue = cpu->midr,
4598 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
4599 .readfn = midr_read,
4600 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4601 .type = ARM_CP_OVERRIDE },
4602 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
4603 { .name = "DUMMY",
4604 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
4605 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4606 { .name = "DUMMY",
4607 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
4608 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4609 { .name = "DUMMY",
4610 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
4611 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4612 { .name = "DUMMY",
4613 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
4614 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4615 { .name = "DUMMY",
4616 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
4617 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4618 REGINFO_SENTINEL
4620 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
4621 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
4622 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
4623 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
4624 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4625 .readfn = midr_read },
4626 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
4627 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4628 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4629 .access = PL1_R, .resetvalue = cpu->midr },
4630 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4631 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
4632 .access = PL1_R, .resetvalue = cpu->midr },
4633 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
4634 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
4635 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
4636 REGINFO_SENTINEL
4638 ARMCPRegInfo id_cp_reginfo[] = {
4639 /* These are common to v8 and pre-v8 */
4640 { .name = "CTR",
4641 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
4642 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4643 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
4644 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
4645 .access = PL0_R, .accessfn = ctr_el0_access,
4646 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4647 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
4648 { .name = "TCMTR",
4649 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
4650 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4651 REGINFO_SENTINEL
4653 /* TLBTR is specific to VMSA */
4654 ARMCPRegInfo id_tlbtr_reginfo = {
4655 .name = "TLBTR",
4656 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
4657 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
4659 /* MPUIR is specific to PMSA V6+ */
4660 ARMCPRegInfo id_mpuir_reginfo = {
4661 .name = "MPUIR",
4662 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4663 .access = PL1_R, .type = ARM_CP_CONST,
4664 .resetvalue = cpu->pmsav7_dregion << 8
4666 ARMCPRegInfo crn0_wi_reginfo = {
4667 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
4668 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
4669 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
4671 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
4672 arm_feature(env, ARM_FEATURE_STRONGARM)) {
4673 ARMCPRegInfo *r;
4674 /* Register the blanket "writes ignored" value first to cover the
4675 * whole space. Then update the specific ID registers to allow write
4676 * access, so that they ignore writes rather than causing them to
4677 * UNDEF.
4679 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
4680 for (r = id_pre_v8_midr_cp_reginfo;
4681 r->type != ARM_CP_SENTINEL; r++) {
4682 r->access = PL1_RW;
4684 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
4685 r->access = PL1_RW;
4687 id_tlbtr_reginfo.access = PL1_RW;
4688 id_tlbtr_reginfo.access = PL1_RW;
4690 if (arm_feature(env, ARM_FEATURE_V8)) {
4691 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
4692 } else {
4693 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
4695 define_arm_cp_regs(cpu, id_cp_reginfo);
4696 if (!arm_feature(env, ARM_FEATURE_MPU)) {
4697 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
4698 } else if (arm_feature(env, ARM_FEATURE_V7)) {
4699 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
4703 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
4704 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
4707 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
4708 ARMCPRegInfo auxcr_reginfo[] = {
4709 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
4710 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
4711 .access = PL1_RW, .type = ARM_CP_CONST,
4712 .resetvalue = cpu->reset_auxcr },
4713 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
4714 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
4715 .access = PL2_RW, .type = ARM_CP_CONST,
4716 .resetvalue = 0 },
4717 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
4718 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
4719 .access = PL3_RW, .type = ARM_CP_CONST,
4720 .resetvalue = 0 },
4721 REGINFO_SENTINEL
4723 define_arm_cp_regs(cpu, auxcr_reginfo);
4726 if (arm_feature(env, ARM_FEATURE_CBAR)) {
4727 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4728 /* 32 bit view is [31:18] 0...0 [43:32]. */
4729 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
4730 | extract64(cpu->reset_cbar, 32, 12);
4731 ARMCPRegInfo cbar_reginfo[] = {
4732 { .name = "CBAR",
4733 .type = ARM_CP_CONST,
4734 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4735 .access = PL1_R, .resetvalue = cpu->reset_cbar },
4736 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
4737 .type = ARM_CP_CONST,
4738 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
4739 .access = PL1_R, .resetvalue = cbar32 },
4740 REGINFO_SENTINEL
4742 /* We don't implement a r/w 64 bit CBAR currently */
4743 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
4744 define_arm_cp_regs(cpu, cbar_reginfo);
4745 } else {
4746 ARMCPRegInfo cbar = {
4747 .name = "CBAR",
4748 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4749 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
4750 .fieldoffset = offsetof(CPUARMState,
4751 cp15.c15_config_base_address)
4753 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
4754 cbar.access = PL1_R;
4755 cbar.fieldoffset = 0;
4756 cbar.type = ARM_CP_CONST;
4758 define_one_arm_cp_reg(cpu, &cbar);
4762 /* Generic registers whose values depend on the implementation */
4764 ARMCPRegInfo sctlr = {
4765 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
4766 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4767 .access = PL1_RW,
4768 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
4769 offsetof(CPUARMState, cp15.sctlr_ns) },
4770 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
4771 .raw_writefn = raw_write,
4773 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4774 /* Normally we would always end the TB on an SCTLR write, but Linux
4775 * arch/arm/mach-pxa/sleep.S expects two instructions following
4776 * an MMU enable to execute from cache. Imitate this behaviour.
4778 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
4780 define_one_arm_cp_reg(cpu, &sctlr);
4784 ARMCPU *cpu_arm_init(const char *cpu_model)
4786 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
4789 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
4791 CPUState *cs = CPU(cpu);
4792 CPUARMState *env = &cpu->env;
4794 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4795 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
4796 aarch64_fpu_gdb_set_reg,
4797 34, "aarch64-fpu.xml", 0);
4798 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
4799 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4800 51, "arm-neon.xml", 0);
4801 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
4802 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4803 35, "arm-vfp3.xml", 0);
4804 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
4805 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
4806 19, "arm-vfp.xml", 0);
4810 /* Sort alphabetically by type name, except for "any". */
4811 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
4813 ObjectClass *class_a = (ObjectClass *)a;
4814 ObjectClass *class_b = (ObjectClass *)b;
4815 const char *name_a, *name_b;
4817 name_a = object_class_get_name(class_a);
4818 name_b = object_class_get_name(class_b);
4819 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
4820 return 1;
4821 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
4822 return -1;
4823 } else {
4824 return strcmp(name_a, name_b);
4828 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
4830 ObjectClass *oc = data;
4831 CPUListState *s = user_data;
4832 const char *typename;
4833 char *name;
4835 typename = object_class_get_name(oc);
4836 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
4837 (*s->cpu_fprintf)(s->file, " %s\n",
4838 name);
4839 g_free(name);
4842 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
4844 CPUListState s = {
4845 .file = f,
4846 .cpu_fprintf = cpu_fprintf,
4848 GSList *list;
4850 list = object_class_get_list(TYPE_ARM_CPU, false);
4851 list = g_slist_sort(list, arm_cpu_list_compare);
4852 (*cpu_fprintf)(f, "Available CPUs:\n");
4853 g_slist_foreach(list, arm_cpu_list_entry, &s);
4854 g_slist_free(list);
4855 #ifdef CONFIG_KVM
4856 /* The 'host' CPU type is dynamically registered only if KVM is
4857 * enabled, so we have to special-case it here:
4859 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
4860 #endif
4863 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
4865 ObjectClass *oc = data;
4866 CpuDefinitionInfoList **cpu_list = user_data;
4867 CpuDefinitionInfoList *entry;
4868 CpuDefinitionInfo *info;
4869 const char *typename;
4871 typename = object_class_get_name(oc);
4872 info = g_malloc0(sizeof(*info));
4873 info->name = g_strndup(typename,
4874 strlen(typename) - strlen("-" TYPE_ARM_CPU));
4876 entry = g_malloc0(sizeof(*entry));
4877 entry->value = info;
4878 entry->next = *cpu_list;
4879 *cpu_list = entry;
4882 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
4884 CpuDefinitionInfoList *cpu_list = NULL;
4885 GSList *list;
4887 list = object_class_get_list(TYPE_ARM_CPU, false);
4888 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
4889 g_slist_free(list);
4891 return cpu_list;
4894 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
4895 void *opaque, int state, int secstate,
4896 int crm, int opc1, int opc2)
4898 /* Private utility function for define_one_arm_cp_reg_with_opaque():
4899 * add a single reginfo struct to the hash table.
4901 uint32_t *key = g_new(uint32_t, 1);
4902 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
4903 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
4904 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
4906 /* Reset the secure state to the specific incoming state. This is
4907 * necessary as the register may have been defined with both states.
4909 r2->secure = secstate;
4911 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
4912 /* Register is banked (using both entries in array).
4913 * Overwriting fieldoffset as the array is only used to define
4914 * banked registers but later only fieldoffset is used.
4916 r2->fieldoffset = r->bank_fieldoffsets[ns];
4919 if (state == ARM_CP_STATE_AA32) {
4920 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
4921 /* If the register is banked then we don't need to migrate or
4922 * reset the 32-bit instance in certain cases:
4924 * 1) If the register has both 32-bit and 64-bit instances then we
4925 * can count on the 64-bit instance taking care of the
4926 * non-secure bank.
4927 * 2) If ARMv8 is enabled then we can count on a 64-bit version
4928 * taking care of the secure bank. This requires that separate
4929 * 32 and 64-bit definitions are provided.
4931 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
4932 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
4933 r2->type |= ARM_CP_ALIAS;
4935 } else if ((secstate != r->secure) && !ns) {
4936 /* The register is not banked so we only want to allow migration of
4937 * the non-secure instance.
4939 r2->type |= ARM_CP_ALIAS;
4942 if (r->state == ARM_CP_STATE_BOTH) {
4943 /* We assume it is a cp15 register if the .cp field is left unset.
4945 if (r2->cp == 0) {
4946 r2->cp = 15;
4949 #ifdef HOST_WORDS_BIGENDIAN
4950 if (r2->fieldoffset) {
4951 r2->fieldoffset += sizeof(uint32_t);
4953 #endif
4956 if (state == ARM_CP_STATE_AA64) {
4957 /* To allow abbreviation of ARMCPRegInfo
4958 * definitions, we treat cp == 0 as equivalent to
4959 * the value for "standard guest-visible sysreg".
4960 * STATE_BOTH definitions are also always "standard
4961 * sysreg" in their AArch64 view (the .cp value may
4962 * be non-zero for the benefit of the AArch32 view).
4964 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
4965 r2->cp = CP_REG_ARM64_SYSREG_CP;
4967 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
4968 r2->opc0, opc1, opc2);
4969 } else {
4970 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
4972 if (opaque) {
4973 r2->opaque = opaque;
4975 /* reginfo passed to helpers is correct for the actual access,
4976 * and is never ARM_CP_STATE_BOTH:
4978 r2->state = state;
4979 /* Make sure reginfo passed to helpers for wildcarded regs
4980 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
4982 r2->crm = crm;
4983 r2->opc1 = opc1;
4984 r2->opc2 = opc2;
4985 /* By convention, for wildcarded registers only the first
4986 * entry is used for migration; the others are marked as
4987 * ALIAS so we don't try to transfer the register
4988 * multiple times. Special registers (ie NOP/WFI) are
4989 * never migratable and not even raw-accessible.
4991 if ((r->type & ARM_CP_SPECIAL)) {
4992 r2->type |= ARM_CP_NO_RAW;
4994 if (((r->crm == CP_ANY) && crm != 0) ||
4995 ((r->opc1 == CP_ANY) && opc1 != 0) ||
4996 ((r->opc2 == CP_ANY) && opc2 != 0)) {
4997 r2->type |= ARM_CP_ALIAS;
5000 /* Check that raw accesses are either forbidden or handled. Note that
5001 * we can't assert this earlier because the setup of fieldoffset for
5002 * banked registers has to be done first.
5004 if (!(r2->type & ARM_CP_NO_RAW)) {
5005 assert(!raw_accessors_invalid(r2));
5008 /* Overriding of an existing definition must be explicitly
5009 * requested.
5011 if (!(r->type & ARM_CP_OVERRIDE)) {
5012 ARMCPRegInfo *oldreg;
5013 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5014 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5015 fprintf(stderr, "Register redefined: cp=%d %d bit "
5016 "crn=%d crm=%d opc1=%d opc2=%d, "
5017 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5018 r2->crn, r2->crm, r2->opc1, r2->opc2,
5019 oldreg->name, r2->name);
5020 g_assert_not_reached();
5023 g_hash_table_insert(cpu->cp_regs, key, r2);
5027 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5028 const ARMCPRegInfo *r, void *opaque)
5030 /* Define implementations of coprocessor registers.
5031 * We store these in a hashtable because typically
5032 * there are less than 150 registers in a space which
5033 * is 16*16*16*8*8 = 262144 in size.
5034 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5035 * If a register is defined twice then the second definition is
5036 * used, so this can be used to define some generic registers and
5037 * then override them with implementation specific variations.
5038 * At least one of the original and the second definition should
5039 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5040 * against accidental use.
5042 * The state field defines whether the register is to be
5043 * visible in the AArch32 or AArch64 execution state. If the
5044 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5045 * reginfo structure for the AArch32 view, which sees the lower
5046 * 32 bits of the 64 bit register.
5048 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5049 * be wildcarded. AArch64 registers are always considered to be 64
5050 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5051 * the register, if any.
5053 int crm, opc1, opc2, state;
5054 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5055 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5056 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5057 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5058 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5059 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5060 /* 64 bit registers have only CRm and Opc1 fields */
5061 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
5062 /* op0 only exists in the AArch64 encodings */
5063 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5064 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5065 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5066 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5067 * encodes a minimum access level for the register. We roll this
5068 * runtime check into our general permission check code, so check
5069 * here that the reginfo's specified permissions are strict enough
5070 * to encompass the generic architectural permission check.
5072 if (r->state != ARM_CP_STATE_AA32) {
5073 int mask = 0;
5074 switch (r->opc1) {
5075 case 0: case 1: case 2:
5076 /* min_EL EL1 */
5077 mask = PL1_RW;
5078 break;
5079 case 3:
5080 /* min_EL EL0 */
5081 mask = PL0_RW;
5082 break;
5083 case 4:
5084 /* min_EL EL2 */
5085 mask = PL2_RW;
5086 break;
5087 case 5:
5088 /* unallocated encoding, so not possible */
5089 assert(false);
5090 break;
5091 case 6:
5092 /* min_EL EL3 */
5093 mask = PL3_RW;
5094 break;
5095 case 7:
5096 /* min_EL EL1, secure mode only (we don't check the latter) */
5097 mask = PL1_RW;
5098 break;
5099 default:
5100 /* broken reginfo with out-of-range opc1 */
5101 assert(false);
5102 break;
5104 /* assert our permissions are not too lax (stricter is fine) */
5105 assert((r->access & ~mask) == 0);
5108 /* Check that the register definition has enough info to handle
5109 * reads and writes if they are permitted.
5111 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5112 if (r->access & PL3_R) {
5113 assert((r->fieldoffset ||
5114 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5115 r->readfn);
5117 if (r->access & PL3_W) {
5118 assert((r->fieldoffset ||
5119 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5120 r->writefn);
5123 /* Bad type field probably means missing sentinel at end of reg list */
5124 assert(cptype_valid(r->type));
5125 for (crm = crmmin; crm <= crmmax; crm++) {
5126 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5127 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
5128 for (state = ARM_CP_STATE_AA32;
5129 state <= ARM_CP_STATE_AA64; state++) {
5130 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5131 continue;
5133 if (state == ARM_CP_STATE_AA32) {
5134 /* Under AArch32 CP registers can be common
5135 * (same for secure and non-secure world) or banked.
5137 switch (r->secure) {
5138 case ARM_CP_SECSTATE_S:
5139 case ARM_CP_SECSTATE_NS:
5140 add_cpreg_to_hashtable(cpu, r, opaque, state,
5141 r->secure, crm, opc1, opc2);
5142 break;
5143 default:
5144 add_cpreg_to_hashtable(cpu, r, opaque, state,
5145 ARM_CP_SECSTATE_S,
5146 crm, opc1, opc2);
5147 add_cpreg_to_hashtable(cpu, r, opaque, state,
5148 ARM_CP_SECSTATE_NS,
5149 crm, opc1, opc2);
5150 break;
5152 } else {
5153 /* AArch64 registers get mapped to non-secure instance
5154 * of AArch32 */
5155 add_cpreg_to_hashtable(cpu, r, opaque, state,
5156 ARM_CP_SECSTATE_NS,
5157 crm, opc1, opc2);
5165 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5166 const ARMCPRegInfo *regs, void *opaque)
5168 /* Define a whole list of registers */
5169 const ARMCPRegInfo *r;
5170 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5171 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5175 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
5177 return g_hash_table_lookup(cpregs, &encoded_cp);
5180 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5181 uint64_t value)
5183 /* Helper coprocessor write function for write-ignore registers */
5186 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
5188 /* Helper coprocessor write function for read-as-zero registers */
5189 return 0;
5192 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5194 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5197 static int bad_mode_switch(CPUARMState *env, int mode)
5199 /* Return true if it is not valid for us to switch to
5200 * this CPU mode (ie all the UNPREDICTABLE cases in
5201 * the ARM ARM CPSRWriteByInstr pseudocode).
5203 switch (mode) {
5204 case ARM_CPU_MODE_USR:
5205 case ARM_CPU_MODE_SYS:
5206 case ARM_CPU_MODE_SVC:
5207 case ARM_CPU_MODE_ABT:
5208 case ARM_CPU_MODE_UND:
5209 case ARM_CPU_MODE_IRQ:
5210 case ARM_CPU_MODE_FIQ:
5211 return 0;
5212 case ARM_CPU_MODE_MON:
5213 return !arm_is_secure(env);
5214 default:
5215 return 1;
5219 uint32_t cpsr_read(CPUARMState *env)
5221 int ZF;
5222 ZF = (env->ZF == 0);
5223 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
5224 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5225 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5226 | ((env->condexec_bits & 0xfc) << 8)
5227 | (env->GE << 16) | (env->daif & CPSR_AIF);
5230 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
5232 uint32_t changed_daif;
5234 if (mask & CPSR_NZCV) {
5235 env->ZF = (~val) & CPSR_Z;
5236 env->NF = val;
5237 env->CF = (val >> 29) & 1;
5238 env->VF = (val << 3) & 0x80000000;
5240 if (mask & CPSR_Q)
5241 env->QF = ((val & CPSR_Q) != 0);
5242 if (mask & CPSR_T)
5243 env->thumb = ((val & CPSR_T) != 0);
5244 if (mask & CPSR_IT_0_1) {
5245 env->condexec_bits &= ~3;
5246 env->condexec_bits |= (val >> 25) & 3;
5248 if (mask & CPSR_IT_2_7) {
5249 env->condexec_bits &= 3;
5250 env->condexec_bits |= (val >> 8) & 0xfc;
5252 if (mask & CPSR_GE) {
5253 env->GE = (val >> 16) & 0xf;
5256 /* In a V7 implementation that includes the security extensions but does
5257 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5258 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5259 * bits respectively.
5261 * In a V8 implementation, it is permitted for privileged software to
5262 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5264 if (!arm_feature(env, ARM_FEATURE_V8) &&
5265 arm_feature(env, ARM_FEATURE_EL3) &&
5266 !arm_feature(env, ARM_FEATURE_EL2) &&
5267 !arm_is_secure(env)) {
5269 changed_daif = (env->daif ^ val) & mask;
5271 if (changed_daif & CPSR_A) {
5272 /* Check to see if we are allowed to change the masking of async
5273 * abort exceptions from a non-secure state.
5275 if (!(env->cp15.scr_el3 & SCR_AW)) {
5276 qemu_log_mask(LOG_GUEST_ERROR,
5277 "Ignoring attempt to switch CPSR_A flag from "
5278 "non-secure world with SCR.AW bit clear\n");
5279 mask &= ~CPSR_A;
5283 if (changed_daif & CPSR_F) {
5284 /* Check to see if we are allowed to change the masking of FIQ
5285 * exceptions from a non-secure state.
5287 if (!(env->cp15.scr_el3 & SCR_FW)) {
5288 qemu_log_mask(LOG_GUEST_ERROR,
5289 "Ignoring attempt to switch CPSR_F flag from "
5290 "non-secure world with SCR.FW bit clear\n");
5291 mask &= ~CPSR_F;
5294 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5295 * If this bit is set software is not allowed to mask
5296 * FIQs, but is allowed to set CPSR_F to 0.
5298 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5299 (val & CPSR_F)) {
5300 qemu_log_mask(LOG_GUEST_ERROR,
5301 "Ignoring attempt to enable CPSR_F flag "
5302 "(non-maskable FIQ [NMFI] support enabled)\n");
5303 mask &= ~CPSR_F;
5308 env->daif &= ~(CPSR_AIF & mask);
5309 env->daif |= val & CPSR_AIF & mask;
5311 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
5312 if (bad_mode_switch(env, val & CPSR_M)) {
5313 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
5314 * We choose to ignore the attempt and leave the CPSR M field
5315 * untouched.
5317 mask &= ~CPSR_M;
5318 } else {
5319 switch_mode(env, val & CPSR_M);
5322 mask &= ~CACHED_CPSR_BITS;
5323 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
5326 /* Sign/zero extend */
5327 uint32_t HELPER(sxtb16)(uint32_t x)
5329 uint32_t res;
5330 res = (uint16_t)(int8_t)x;
5331 res |= (uint32_t)(int8_t)(x >> 16) << 16;
5332 return res;
5335 uint32_t HELPER(uxtb16)(uint32_t x)
5337 uint32_t res;
5338 res = (uint16_t)(uint8_t)x;
5339 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
5340 return res;
5343 uint32_t HELPER(clz)(uint32_t x)
5345 return clz32(x);
5348 int32_t HELPER(sdiv)(int32_t num, int32_t den)
5350 if (den == 0)
5351 return 0;
5352 if (num == INT_MIN && den == -1)
5353 return INT_MIN;
5354 return num / den;
5357 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
5359 if (den == 0)
5360 return 0;
5361 return num / den;
5364 uint32_t HELPER(rbit)(uint32_t x)
5366 return revbit32(x);
5369 #if defined(CONFIG_USER_ONLY)
5371 /* These should probably raise undefined insn exceptions. */
5372 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
5374 ARMCPU *cpu = arm_env_get_cpu(env);
5376 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
5379 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
5381 ARMCPU *cpu = arm_env_get_cpu(env);
5383 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
5384 return 0;
5387 void switch_mode(CPUARMState *env, int mode)
5389 ARMCPU *cpu = arm_env_get_cpu(env);
5391 if (mode != ARM_CPU_MODE_USR) {
5392 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5396 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5397 uint32_t cur_el, bool secure)
5399 return 1;
5402 void aarch64_sync_64_to_32(CPUARMState *env)
5404 g_assert_not_reached();
5407 #else
5409 void switch_mode(CPUARMState *env, int mode)
5411 int old_mode;
5412 int i;
5414 old_mode = env->uncached_cpsr & CPSR_M;
5415 if (mode == old_mode)
5416 return;
5418 if (old_mode == ARM_CPU_MODE_FIQ) {
5419 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
5420 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
5421 } else if (mode == ARM_CPU_MODE_FIQ) {
5422 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
5423 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
5426 i = bank_number(old_mode);
5427 env->banked_r13[i] = env->regs[13];
5428 env->banked_r14[i] = env->regs[14];
5429 env->banked_spsr[i] = env->spsr;
5431 i = bank_number(mode);
5432 env->regs[13] = env->banked_r13[i];
5433 env->regs[14] = env->banked_r14[i];
5434 env->spsr = env->banked_spsr[i];
5437 /* Physical Interrupt Target EL Lookup Table
5439 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
5441 * The below multi-dimensional table is used for looking up the target
5442 * exception level given numerous condition criteria. Specifically, the
5443 * target EL is based on SCR and HCR routing controls as well as the
5444 * currently executing EL and secure state.
5446 * Dimensions:
5447 * target_el_table[2][2][2][2][2][4]
5448 * | | | | | +--- Current EL
5449 * | | | | +------ Non-secure(0)/Secure(1)
5450 * | | | +--------- HCR mask override
5451 * | | +------------ SCR exec state control
5452 * | +--------------- SCR mask override
5453 * +------------------ 32-bit(0)/64-bit(1) EL3
5455 * The table values are as such:
5456 * 0-3 = EL0-EL3
5457 * -1 = Cannot occur
5459 * The ARM ARM target EL table includes entries indicating that an "exception
5460 * is not taken". The two cases where this is applicable are:
5461 * 1) An exception is taken from EL3 but the SCR does not have the exception
5462 * routed to EL3.
5463 * 2) An exception is taken from EL2 but the HCR does not have the exception
5464 * routed to EL2.
5465 * In these two cases, the below table contain a target of EL1. This value is
5466 * returned as it is expected that the consumer of the table data will check
5467 * for "target EL >= current EL" to ensure the exception is not taken.
5469 * SCR HCR
5470 * 64 EA AMO From
5471 * BIT IRQ IMO Non-secure Secure
5472 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
5474 static const int8_t target_el_table[2][2][2][2][2][4] = {
5475 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5476 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
5477 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5478 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
5479 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5480 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
5481 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5482 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
5483 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
5484 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
5485 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
5486 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
5487 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5488 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
5489 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5490 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
5494 * Determine the target EL for physical exceptions
5496 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5497 uint32_t cur_el, bool secure)
5499 CPUARMState *env = cs->env_ptr;
5500 int rw;
5501 int scr;
5502 int hcr;
5503 int target_el;
5504 /* Is the highest EL AArch64? */
5505 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
5507 if (arm_feature(env, ARM_FEATURE_EL3)) {
5508 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
5509 } else {
5510 /* Either EL2 is the highest EL (and so the EL2 register width
5511 * is given by is64); or there is no EL2 or EL3, in which case
5512 * the value of 'rw' does not affect the table lookup anyway.
5514 rw = is64;
5517 switch (excp_idx) {
5518 case EXCP_IRQ:
5519 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
5520 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
5521 break;
5522 case EXCP_FIQ:
5523 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
5524 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
5525 break;
5526 default:
5527 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
5528 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
5529 break;
5532 /* If HCR.TGE is set then HCR is treated as being 1 */
5533 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
5535 /* Perform a table-lookup for the target EL given the current state */
5536 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
5538 assert(target_el > 0);
5540 return target_el;
5543 static void v7m_push(CPUARMState *env, uint32_t val)
5545 CPUState *cs = CPU(arm_env_get_cpu(env));
5547 env->regs[13] -= 4;
5548 stl_phys(cs->as, env->regs[13], val);
5551 static uint32_t v7m_pop(CPUARMState *env)
5553 CPUState *cs = CPU(arm_env_get_cpu(env));
5554 uint32_t val;
5556 val = ldl_phys(cs->as, env->regs[13]);
5557 env->regs[13] += 4;
5558 return val;
5561 /* Switch to V7M main or process stack pointer. */
5562 static void switch_v7m_sp(CPUARMState *env, int process)
5564 uint32_t tmp;
5565 if (env->v7m.current_sp != process) {
5566 tmp = env->v7m.other_sp;
5567 env->v7m.other_sp = env->regs[13];
5568 env->regs[13] = tmp;
5569 env->v7m.current_sp = process;
5573 static void do_v7m_exception_exit(CPUARMState *env)
5575 uint32_t type;
5576 uint32_t xpsr;
5578 type = env->regs[15];
5579 if (env->v7m.exception != 0)
5580 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
5582 /* Switch to the target stack. */
5583 switch_v7m_sp(env, (type & 4) != 0);
5584 /* Pop registers. */
5585 env->regs[0] = v7m_pop(env);
5586 env->regs[1] = v7m_pop(env);
5587 env->regs[2] = v7m_pop(env);
5588 env->regs[3] = v7m_pop(env);
5589 env->regs[12] = v7m_pop(env);
5590 env->regs[14] = v7m_pop(env);
5591 env->regs[15] = v7m_pop(env);
5592 if (env->regs[15] & 1) {
5593 qemu_log_mask(LOG_GUEST_ERROR,
5594 "M profile return from interrupt with misaligned "
5595 "PC is UNPREDICTABLE\n");
5596 /* Actual hardware seems to ignore the lsbit, and there are several
5597 * RTOSes out there which incorrectly assume the r15 in the stack
5598 * frame should be a Thumb-style "lsbit indicates ARM/Thumb" value.
5600 env->regs[15] &= ~1U;
5602 xpsr = v7m_pop(env);
5603 xpsr_write(env, xpsr, 0xfffffdff);
5604 /* Undo stack alignment. */
5605 if (xpsr & 0x200)
5606 env->regs[13] |= 4;
5607 /* ??? The exception return type specifies Thread/Handler mode. However
5608 this is also implied by the xPSR value. Not sure what to do
5609 if there is a mismatch. */
5610 /* ??? Likewise for mismatches between the CONTROL register and the stack
5611 pointer. */
5614 void arm_v7m_cpu_do_interrupt(CPUState *cs)
5616 ARMCPU *cpu = ARM_CPU(cs);
5617 CPUARMState *env = &cpu->env;
5618 uint32_t xpsr = xpsr_read(env);
5619 uint32_t lr;
5620 uint32_t addr;
5622 arm_log_exception(cs->exception_index);
5624 lr = 0xfffffff1;
5625 if (env->v7m.current_sp)
5626 lr |= 4;
5627 if (env->v7m.exception == 0)
5628 lr |= 8;
5630 /* For exceptions we just mark as pending on the NVIC, and let that
5631 handle it. */
5632 /* TODO: Need to escalate if the current priority is higher than the
5633 one we're raising. */
5634 switch (cs->exception_index) {
5635 case EXCP_UDEF:
5636 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
5637 return;
5638 case EXCP_SWI:
5639 /* The PC already points to the next instruction. */
5640 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
5641 return;
5642 case EXCP_PREFETCH_ABORT:
5643 case EXCP_DATA_ABORT:
5644 /* TODO: if we implemented the MPU registers, this is where we
5645 * should set the MMFAR, etc from exception.fsr and exception.vaddress.
5647 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
5648 return;
5649 case EXCP_BKPT:
5650 if (semihosting_enabled()) {
5651 int nr;
5652 nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
5653 if (nr == 0xab) {
5654 env->regs[15] += 2;
5655 qemu_log_mask(CPU_LOG_INT,
5656 "...handling as semihosting call 0x%x\n",
5657 env->regs[0]);
5658 env->regs[0] = do_arm_semihosting(env);
5659 return;
5662 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
5663 return;
5664 case EXCP_IRQ:
5665 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
5666 break;
5667 case EXCP_EXCEPTION_EXIT:
5668 do_v7m_exception_exit(env);
5669 return;
5670 default:
5671 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
5672 return; /* Never happens. Keep compiler happy. */
5675 /* Align stack pointer. */
5676 /* ??? Should only do this if Configuration Control Register
5677 STACKALIGN bit is set. */
5678 if (env->regs[13] & 4) {
5679 env->regs[13] -= 4;
5680 xpsr |= 0x200;
5682 /* Switch to the handler mode. */
5683 v7m_push(env, xpsr);
5684 v7m_push(env, env->regs[15]);
5685 v7m_push(env, env->regs[14]);
5686 v7m_push(env, env->regs[12]);
5687 v7m_push(env, env->regs[3]);
5688 v7m_push(env, env->regs[2]);
5689 v7m_push(env, env->regs[1]);
5690 v7m_push(env, env->regs[0]);
5691 switch_v7m_sp(env, 0);
5692 /* Clear IT bits */
5693 env->condexec_bits = 0;
5694 env->regs[14] = lr;
5695 addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
5696 env->regs[15] = addr & 0xfffffffe;
5697 env->thumb = addr & 1;
5700 /* Function used to synchronize QEMU's AArch64 register set with AArch32
5701 * register set. This is necessary when switching between AArch32 and AArch64
5702 * execution state.
5704 void aarch64_sync_32_to_64(CPUARMState *env)
5706 int i;
5707 uint32_t mode = env->uncached_cpsr & CPSR_M;
5709 /* We can blanket copy R[0:7] to X[0:7] */
5710 for (i = 0; i < 8; i++) {
5711 env->xregs[i] = env->regs[i];
5714 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
5715 * Otherwise, they come from the banked user regs.
5717 if (mode == ARM_CPU_MODE_FIQ) {
5718 for (i = 8; i < 13; i++) {
5719 env->xregs[i] = env->usr_regs[i - 8];
5721 } else {
5722 for (i = 8; i < 13; i++) {
5723 env->xregs[i] = env->regs[i];
5727 /* Registers x13-x23 are the various mode SP and FP registers. Registers
5728 * r13 and r14 are only copied if we are in that mode, otherwise we copy
5729 * from the mode banked register.
5731 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5732 env->xregs[13] = env->regs[13];
5733 env->xregs[14] = env->regs[14];
5734 } else {
5735 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
5736 /* HYP is an exception in that it is copied from r14 */
5737 if (mode == ARM_CPU_MODE_HYP) {
5738 env->xregs[14] = env->regs[14];
5739 } else {
5740 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
5744 if (mode == ARM_CPU_MODE_HYP) {
5745 env->xregs[15] = env->regs[13];
5746 } else {
5747 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
5750 if (mode == ARM_CPU_MODE_IRQ) {
5751 env->xregs[16] = env->regs[14];
5752 env->xregs[17] = env->regs[13];
5753 } else {
5754 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
5755 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
5758 if (mode == ARM_CPU_MODE_SVC) {
5759 env->xregs[18] = env->regs[14];
5760 env->xregs[19] = env->regs[13];
5761 } else {
5762 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
5763 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
5766 if (mode == ARM_CPU_MODE_ABT) {
5767 env->xregs[20] = env->regs[14];
5768 env->xregs[21] = env->regs[13];
5769 } else {
5770 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
5771 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
5774 if (mode == ARM_CPU_MODE_UND) {
5775 env->xregs[22] = env->regs[14];
5776 env->xregs[23] = env->regs[13];
5777 } else {
5778 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
5779 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
5782 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
5783 * mode, then we can copy from r8-r14. Otherwise, we copy from the
5784 * FIQ bank for r8-r14.
5786 if (mode == ARM_CPU_MODE_FIQ) {
5787 for (i = 24; i < 31; i++) {
5788 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
5790 } else {
5791 for (i = 24; i < 29; i++) {
5792 env->xregs[i] = env->fiq_regs[i - 24];
5794 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
5795 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
5798 env->pc = env->regs[15];
5801 /* Function used to synchronize QEMU's AArch32 register set with AArch64
5802 * register set. This is necessary when switching between AArch32 and AArch64
5803 * execution state.
5805 void aarch64_sync_64_to_32(CPUARMState *env)
5807 int i;
5808 uint32_t mode = env->uncached_cpsr & CPSR_M;
5810 /* We can blanket copy X[0:7] to R[0:7] */
5811 for (i = 0; i < 8; i++) {
5812 env->regs[i] = env->xregs[i];
5815 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
5816 * Otherwise, we copy x8-x12 into the banked user regs.
5818 if (mode == ARM_CPU_MODE_FIQ) {
5819 for (i = 8; i < 13; i++) {
5820 env->usr_regs[i - 8] = env->xregs[i];
5822 } else {
5823 for (i = 8; i < 13; i++) {
5824 env->regs[i] = env->xregs[i];
5828 /* Registers r13 & r14 depend on the current mode.
5829 * If we are in a given mode, we copy the corresponding x registers to r13
5830 * and r14. Otherwise, we copy the x register to the banked r13 and r14
5831 * for the mode.
5833 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5834 env->regs[13] = env->xregs[13];
5835 env->regs[14] = env->xregs[14];
5836 } else {
5837 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
5839 /* HYP is an exception in that it does not have its own banked r14 but
5840 * shares the USR r14
5842 if (mode == ARM_CPU_MODE_HYP) {
5843 env->regs[14] = env->xregs[14];
5844 } else {
5845 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
5849 if (mode == ARM_CPU_MODE_HYP) {
5850 env->regs[13] = env->xregs[15];
5851 } else {
5852 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
5855 if (mode == ARM_CPU_MODE_IRQ) {
5856 env->regs[14] = env->xregs[16];
5857 env->regs[13] = env->xregs[17];
5858 } else {
5859 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
5860 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
5863 if (mode == ARM_CPU_MODE_SVC) {
5864 env->regs[14] = env->xregs[18];
5865 env->regs[13] = env->xregs[19];
5866 } else {
5867 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
5868 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
5871 if (mode == ARM_CPU_MODE_ABT) {
5872 env->regs[14] = env->xregs[20];
5873 env->regs[13] = env->xregs[21];
5874 } else {
5875 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
5876 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
5879 if (mode == ARM_CPU_MODE_UND) {
5880 env->regs[14] = env->xregs[22];
5881 env->regs[13] = env->xregs[23];
5882 } else {
5883 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
5884 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
5887 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
5888 * mode, then we can copy to r8-r14. Otherwise, we copy to the
5889 * FIQ bank for r8-r14.
5891 if (mode == ARM_CPU_MODE_FIQ) {
5892 for (i = 24; i < 31; i++) {
5893 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
5895 } else {
5896 for (i = 24; i < 29; i++) {
5897 env->fiq_regs[i - 24] = env->xregs[i];
5899 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
5900 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
5903 env->regs[15] = env->pc;
5906 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
5908 ARMCPU *cpu = ARM_CPU(cs);
5909 CPUARMState *env = &cpu->env;
5910 uint32_t addr;
5911 uint32_t mask;
5912 int new_mode;
5913 uint32_t offset;
5914 uint32_t moe;
5916 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
5917 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
5918 case EC_BREAKPOINT:
5919 case EC_BREAKPOINT_SAME_EL:
5920 moe = 1;
5921 break;
5922 case EC_WATCHPOINT:
5923 case EC_WATCHPOINT_SAME_EL:
5924 moe = 10;
5925 break;
5926 case EC_AA32_BKPT:
5927 moe = 3;
5928 break;
5929 case EC_VECTORCATCH:
5930 moe = 5;
5931 break;
5932 default:
5933 moe = 0;
5934 break;
5937 if (moe) {
5938 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
5941 /* TODO: Vectored interrupt controller. */
5942 switch (cs->exception_index) {
5943 case EXCP_UDEF:
5944 new_mode = ARM_CPU_MODE_UND;
5945 addr = 0x04;
5946 mask = CPSR_I;
5947 if (env->thumb)
5948 offset = 2;
5949 else
5950 offset = 4;
5951 break;
5952 case EXCP_SWI:
5953 new_mode = ARM_CPU_MODE_SVC;
5954 addr = 0x08;
5955 mask = CPSR_I;
5956 /* The PC already points to the next instruction. */
5957 offset = 0;
5958 break;
5959 case EXCP_BKPT:
5960 env->exception.fsr = 2;
5961 /* Fall through to prefetch abort. */
5962 case EXCP_PREFETCH_ABORT:
5963 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
5964 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
5965 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
5966 env->exception.fsr, (uint32_t)env->exception.vaddress);
5967 new_mode = ARM_CPU_MODE_ABT;
5968 addr = 0x0c;
5969 mask = CPSR_A | CPSR_I;
5970 offset = 4;
5971 break;
5972 case EXCP_DATA_ABORT:
5973 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
5974 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
5975 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
5976 env->exception.fsr,
5977 (uint32_t)env->exception.vaddress);
5978 new_mode = ARM_CPU_MODE_ABT;
5979 addr = 0x10;
5980 mask = CPSR_A | CPSR_I;
5981 offset = 8;
5982 break;
5983 case EXCP_IRQ:
5984 new_mode = ARM_CPU_MODE_IRQ;
5985 addr = 0x18;
5986 /* Disable IRQ and imprecise data aborts. */
5987 mask = CPSR_A | CPSR_I;
5988 offset = 4;
5989 if (env->cp15.scr_el3 & SCR_IRQ) {
5990 /* IRQ routed to monitor mode */
5991 new_mode = ARM_CPU_MODE_MON;
5992 mask |= CPSR_F;
5994 break;
5995 case EXCP_FIQ:
5996 new_mode = ARM_CPU_MODE_FIQ;
5997 addr = 0x1c;
5998 /* Disable FIQ, IRQ and imprecise data aborts. */
5999 mask = CPSR_A | CPSR_I | CPSR_F;
6000 if (env->cp15.scr_el3 & SCR_FIQ) {
6001 /* FIQ routed to monitor mode */
6002 new_mode = ARM_CPU_MODE_MON;
6004 offset = 4;
6005 break;
6006 case EXCP_SMC:
6007 new_mode = ARM_CPU_MODE_MON;
6008 addr = 0x08;
6009 mask = CPSR_A | CPSR_I | CPSR_F;
6010 offset = 0;
6011 break;
6012 default:
6013 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
6014 return; /* Never happens. Keep compiler happy. */
6017 if (new_mode == ARM_CPU_MODE_MON) {
6018 addr += env->cp15.mvbar;
6019 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
6020 /* High vectors. When enabled, base address cannot be remapped. */
6021 addr += 0xffff0000;
6022 } else {
6023 /* ARM v7 architectures provide a vector base address register to remap
6024 * the interrupt vector table.
6025 * This register is only followed in non-monitor mode, and is banked.
6026 * Note: only bits 31:5 are valid.
6028 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
6031 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
6032 env->cp15.scr_el3 &= ~SCR_NS;
6035 switch_mode (env, new_mode);
6036 /* For exceptions taken to AArch32 we must clear the SS bit in both
6037 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
6039 env->uncached_cpsr &= ~PSTATE_SS;
6040 env->spsr = cpsr_read(env);
6041 /* Clear IT bits. */
6042 env->condexec_bits = 0;
6043 /* Switch to the new mode, and to the correct instruction set. */
6044 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
6045 env->daif |= mask;
6046 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
6047 * and we should just guard the thumb mode on V4 */
6048 if (arm_feature(env, ARM_FEATURE_V4T)) {
6049 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
6051 env->regs[14] = env->regs[15] + offset;
6052 env->regs[15] = addr;
6055 /* Handle exception entry to a target EL which is using AArch64 */
6056 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
6058 ARMCPU *cpu = ARM_CPU(cs);
6059 CPUARMState *env = &cpu->env;
6060 unsigned int new_el = env->exception.target_el;
6061 target_ulong addr = env->cp15.vbar_el[new_el];
6062 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
6064 if (arm_current_el(env) < new_el) {
6065 /* Entry vector offset depends on whether the implemented EL
6066 * immediately lower than the target level is using AArch32 or AArch64
6068 bool is_aa64;
6070 switch (new_el) {
6071 case 3:
6072 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
6073 break;
6074 case 2:
6075 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
6076 break;
6077 case 1:
6078 is_aa64 = is_a64(env);
6079 break;
6080 default:
6081 g_assert_not_reached();
6084 if (is_aa64) {
6085 addr += 0x400;
6086 } else {
6087 addr += 0x600;
6089 } else if (pstate_read(env) & PSTATE_SP) {
6090 addr += 0x200;
6093 switch (cs->exception_index) {
6094 case EXCP_PREFETCH_ABORT:
6095 case EXCP_DATA_ABORT:
6096 env->cp15.far_el[new_el] = env->exception.vaddress;
6097 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
6098 env->cp15.far_el[new_el]);
6099 /* fall through */
6100 case EXCP_BKPT:
6101 case EXCP_UDEF:
6102 case EXCP_SWI:
6103 case EXCP_HVC:
6104 case EXCP_HYP_TRAP:
6105 case EXCP_SMC:
6106 env->cp15.esr_el[new_el] = env->exception.syndrome;
6107 break;
6108 case EXCP_IRQ:
6109 case EXCP_VIRQ:
6110 addr += 0x80;
6111 break;
6112 case EXCP_FIQ:
6113 case EXCP_VFIQ:
6114 addr += 0x100;
6115 break;
6116 case EXCP_SEMIHOST:
6117 qemu_log_mask(CPU_LOG_INT,
6118 "...handling as semihosting call 0x%" PRIx64 "\n",
6119 env->xregs[0]);
6120 env->xregs[0] = do_arm_semihosting(env);
6121 return;
6122 default:
6123 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
6126 if (is_a64(env)) {
6127 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
6128 aarch64_save_sp(env, arm_current_el(env));
6129 env->elr_el[new_el] = env->pc;
6130 } else {
6131 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
6132 if (!env->thumb) {
6133 env->cp15.esr_el[new_el] |= 1 << 25;
6135 env->elr_el[new_el] = env->regs[15];
6137 aarch64_sync_32_to_64(env);
6139 env->condexec_bits = 0;
6141 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
6142 env->elr_el[new_el]);
6144 pstate_write(env, PSTATE_DAIF | new_mode);
6145 env->aarch64 = 1;
6146 aarch64_restore_sp(env, new_el);
6148 env->pc = addr;
6150 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
6151 new_el, env->pc, pstate_read(env));
6154 static inline bool check_for_semihosting(CPUState *cs)
6156 /* Check whether this exception is a semihosting call; if so
6157 * then handle it and return true; otherwise return false.
6159 ARMCPU *cpu = ARM_CPU(cs);
6160 CPUARMState *env = &cpu->env;
6162 if (is_a64(env)) {
6163 if (cs->exception_index == EXCP_SEMIHOST) {
6164 /* This is always the 64-bit semihosting exception.
6165 * The "is this usermode" and "is semihosting enabled"
6166 * checks have been done at translate time.
6168 qemu_log_mask(CPU_LOG_INT,
6169 "...handling as semihosting call 0x%" PRIx64 "\n",
6170 env->xregs[0]);
6171 env->xregs[0] = do_arm_semihosting(env);
6172 return true;
6174 return false;
6175 } else {
6176 uint32_t imm;
6178 /* Only intercept calls from privileged modes, to provide some
6179 * semblance of security.
6181 if (!semihosting_enabled() ||
6182 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR)) {
6183 return false;
6186 switch (cs->exception_index) {
6187 case EXCP_SWI:
6188 /* Check for semihosting interrupt. */
6189 if (env->thumb) {
6190 imm = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
6191 & 0xff;
6192 if (imm == 0xab) {
6193 break;
6195 } else {
6196 imm = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
6197 & 0xffffff;
6198 if (imm == 0x123456) {
6199 break;
6202 return false;
6203 case EXCP_BKPT:
6204 /* See if this is a semihosting syscall. */
6205 if (env->thumb) {
6206 imm = arm_lduw_code(env, env->regs[15], env->bswap_code)
6207 & 0xff;
6208 if (imm == 0xab) {
6209 env->regs[15] += 2;
6210 break;
6213 return false;
6214 default:
6215 return false;
6218 qemu_log_mask(CPU_LOG_INT,
6219 "...handling as semihosting call 0x%x\n",
6220 env->regs[0]);
6221 env->regs[0] = do_arm_semihosting(env);
6222 return true;
6226 /* Handle a CPU exception for A and R profile CPUs.
6227 * Do any appropriate logging, handle PSCI calls, and then hand off
6228 * to the AArch64-entry or AArch32-entry function depending on the
6229 * target exception level's register width.
6231 void arm_cpu_do_interrupt(CPUState *cs)
6233 ARMCPU *cpu = ARM_CPU(cs);
6234 CPUARMState *env = &cpu->env;
6235 unsigned int new_el = env->exception.target_el;
6237 assert(!IS_M(env));
6239 arm_log_exception(cs->exception_index);
6240 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
6241 new_el);
6242 if (qemu_loglevel_mask(CPU_LOG_INT)
6243 && !excp_is_internal(cs->exception_index)) {
6244 qemu_log_mask(CPU_LOG_INT, "...with ESR %x/0x%" PRIx32 "\n",
6245 env->exception.syndrome >> ARM_EL_EC_SHIFT,
6246 env->exception.syndrome);
6249 if (arm_is_psci_call(cpu, cs->exception_index)) {
6250 arm_handle_psci_call(cpu);
6251 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
6252 return;
6255 /* Semihosting semantics depend on the register width of the
6256 * code that caused the exception, not the target exception level,
6257 * so must be handled here.
6259 if (check_for_semihosting(cs)) {
6260 return;
6263 assert(!excp_is_internal(cs->exception_index));
6264 if (arm_el_is_aa64(env, new_el)) {
6265 arm_cpu_do_interrupt_aarch64(cs);
6266 } else {
6267 arm_cpu_do_interrupt_aarch32(cs);
6270 if (!kvm_enabled()) {
6271 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
6275 /* Return the exception level which controls this address translation regime */
6276 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
6278 switch (mmu_idx) {
6279 case ARMMMUIdx_S2NS:
6280 case ARMMMUIdx_S1E2:
6281 return 2;
6282 case ARMMMUIdx_S1E3:
6283 return 3;
6284 case ARMMMUIdx_S1SE0:
6285 return arm_el_is_aa64(env, 3) ? 1 : 3;
6286 case ARMMMUIdx_S1SE1:
6287 case ARMMMUIdx_S1NSE0:
6288 case ARMMMUIdx_S1NSE1:
6289 return 1;
6290 default:
6291 g_assert_not_reached();
6295 /* Return true if this address translation regime is secure */
6296 static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx)
6298 switch (mmu_idx) {
6299 case ARMMMUIdx_S12NSE0:
6300 case ARMMMUIdx_S12NSE1:
6301 case ARMMMUIdx_S1NSE0:
6302 case ARMMMUIdx_S1NSE1:
6303 case ARMMMUIdx_S1E2:
6304 case ARMMMUIdx_S2NS:
6305 return false;
6306 case ARMMMUIdx_S1E3:
6307 case ARMMMUIdx_S1SE0:
6308 case ARMMMUIdx_S1SE1:
6309 return true;
6310 default:
6311 g_assert_not_reached();
6315 /* Return the SCTLR value which controls this address translation regime */
6316 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
6318 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
6321 /* Return true if the specified stage of address translation is disabled */
6322 static inline bool regime_translation_disabled(CPUARMState *env,
6323 ARMMMUIdx mmu_idx)
6325 if (mmu_idx == ARMMMUIdx_S2NS) {
6326 return (env->cp15.hcr_el2 & HCR_VM) == 0;
6328 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
6331 /* Return the TCR controlling this translation regime */
6332 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
6334 if (mmu_idx == ARMMMUIdx_S2NS) {
6335 return &env->cp15.vtcr_el2;
6337 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
6340 /* Return the TTBR associated with this translation regime */
6341 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
6342 int ttbrn)
6344 if (mmu_idx == ARMMMUIdx_S2NS) {
6345 return env->cp15.vttbr_el2;
6347 if (ttbrn == 0) {
6348 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
6349 } else {
6350 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
6354 /* Return true if the translation regime is using LPAE format page tables */
6355 static inline bool regime_using_lpae_format(CPUARMState *env,
6356 ARMMMUIdx mmu_idx)
6358 int el = regime_el(env, mmu_idx);
6359 if (el == 2 || arm_el_is_aa64(env, el)) {
6360 return true;
6362 if (arm_feature(env, ARM_FEATURE_LPAE)
6363 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
6364 return true;
6366 return false;
6369 /* Returns true if the stage 1 translation regime is using LPAE format page
6370 * tables. Used when raising alignment exceptions, whose FSR changes depending
6371 * on whether the long or short descriptor format is in use. */
6372 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
6374 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
6375 mmu_idx += ARMMMUIdx_S1NSE0;
6378 return regime_using_lpae_format(env, mmu_idx);
6381 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
6383 switch (mmu_idx) {
6384 case ARMMMUIdx_S1SE0:
6385 case ARMMMUIdx_S1NSE0:
6386 return true;
6387 default:
6388 return false;
6389 case ARMMMUIdx_S12NSE0:
6390 case ARMMMUIdx_S12NSE1:
6391 g_assert_not_reached();
6395 /* Translate section/page access permissions to page
6396 * R/W protection flags
6398 * @env: CPUARMState
6399 * @mmu_idx: MMU index indicating required translation regime
6400 * @ap: The 3-bit access permissions (AP[2:0])
6401 * @domain_prot: The 2-bit domain access permissions
6403 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
6404 int ap, int domain_prot)
6406 bool is_user = regime_is_user(env, mmu_idx);
6408 if (domain_prot == 3) {
6409 return PAGE_READ | PAGE_WRITE;
6412 switch (ap) {
6413 case 0:
6414 if (arm_feature(env, ARM_FEATURE_V7)) {
6415 return 0;
6417 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
6418 case SCTLR_S:
6419 return is_user ? 0 : PAGE_READ;
6420 case SCTLR_R:
6421 return PAGE_READ;
6422 default:
6423 return 0;
6425 case 1:
6426 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
6427 case 2:
6428 if (is_user) {
6429 return PAGE_READ;
6430 } else {
6431 return PAGE_READ | PAGE_WRITE;
6433 case 3:
6434 return PAGE_READ | PAGE_WRITE;
6435 case 4: /* Reserved. */
6436 return 0;
6437 case 5:
6438 return is_user ? 0 : PAGE_READ;
6439 case 6:
6440 return PAGE_READ;
6441 case 7:
6442 if (!arm_feature(env, ARM_FEATURE_V6K)) {
6443 return 0;
6445 return PAGE_READ;
6446 default:
6447 g_assert_not_reached();
6451 /* Translate section/page access permissions to page
6452 * R/W protection flags.
6454 * @ap: The 2-bit simple AP (AP[2:1])
6455 * @is_user: TRUE if accessing from PL0
6457 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
6459 switch (ap) {
6460 case 0:
6461 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
6462 case 1:
6463 return PAGE_READ | PAGE_WRITE;
6464 case 2:
6465 return is_user ? 0 : PAGE_READ;
6466 case 3:
6467 return PAGE_READ;
6468 default:
6469 g_assert_not_reached();
6473 static inline int
6474 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
6476 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
6479 /* Translate S2 section/page access permissions to protection flags
6481 * @env: CPUARMState
6482 * @s2ap: The 2-bit stage2 access permissions (S2AP)
6483 * @xn: XN (execute-never) bit
6485 static int get_S2prot(CPUARMState *env, int s2ap, int xn)
6487 int prot = 0;
6489 if (s2ap & 1) {
6490 prot |= PAGE_READ;
6492 if (s2ap & 2) {
6493 prot |= PAGE_WRITE;
6495 if (!xn) {
6496 prot |= PAGE_EXEC;
6498 return prot;
6501 /* Translate section/page access permissions to protection flags
6503 * @env: CPUARMState
6504 * @mmu_idx: MMU index indicating required translation regime
6505 * @is_aa64: TRUE if AArch64
6506 * @ap: The 2-bit simple AP (AP[2:1])
6507 * @ns: NS (non-secure) bit
6508 * @xn: XN (execute-never) bit
6509 * @pxn: PXN (privileged execute-never) bit
6511 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
6512 int ap, int ns, int xn, int pxn)
6514 bool is_user = regime_is_user(env, mmu_idx);
6515 int prot_rw, user_rw;
6516 bool have_wxn;
6517 int wxn = 0;
6519 assert(mmu_idx != ARMMMUIdx_S2NS);
6521 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
6522 if (is_user) {
6523 prot_rw = user_rw;
6524 } else {
6525 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
6528 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
6529 return prot_rw;
6532 /* TODO have_wxn should be replaced with
6533 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
6534 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
6535 * compatible processors have EL2, which is required for [U]WXN.
6537 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
6539 if (have_wxn) {
6540 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
6543 if (is_aa64) {
6544 switch (regime_el(env, mmu_idx)) {
6545 case 1:
6546 if (!is_user) {
6547 xn = pxn || (user_rw & PAGE_WRITE);
6549 break;
6550 case 2:
6551 case 3:
6552 break;
6554 } else if (arm_feature(env, ARM_FEATURE_V7)) {
6555 switch (regime_el(env, mmu_idx)) {
6556 case 1:
6557 case 3:
6558 if (is_user) {
6559 xn = xn || !(user_rw & PAGE_READ);
6560 } else {
6561 int uwxn = 0;
6562 if (have_wxn) {
6563 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
6565 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
6566 (uwxn && (user_rw & PAGE_WRITE));
6568 break;
6569 case 2:
6570 break;
6572 } else {
6573 xn = wxn = 0;
6576 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
6577 return prot_rw;
6579 return prot_rw | PAGE_EXEC;
6582 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
6583 uint32_t *table, uint32_t address)
6585 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
6586 TCR *tcr = regime_tcr(env, mmu_idx);
6588 if (address & tcr->mask) {
6589 if (tcr->raw_tcr & TTBCR_PD1) {
6590 /* Translation table walk disabled for TTBR1 */
6591 return false;
6593 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
6594 } else {
6595 if (tcr->raw_tcr & TTBCR_PD0) {
6596 /* Translation table walk disabled for TTBR0 */
6597 return false;
6599 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
6601 *table |= (address >> 18) & 0x3ffc;
6602 return true;
6605 /* Translate a S1 pagetable walk through S2 if needed. */
6606 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
6607 hwaddr addr, MemTxAttrs txattrs,
6608 uint32_t *fsr,
6609 ARMMMUFaultInfo *fi)
6611 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
6612 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
6613 target_ulong s2size;
6614 hwaddr s2pa;
6615 int s2prot;
6616 int ret;
6618 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
6619 &txattrs, &s2prot, &s2size, fsr, fi);
6620 if (ret) {
6621 fi->s2addr = addr;
6622 fi->stage2 = true;
6623 fi->s1ptw = true;
6624 return ~0;
6626 addr = s2pa;
6628 return addr;
6631 /* All loads done in the course of a page table walk go through here.
6632 * TODO: rather than ignoring errors from physical memory reads (which
6633 * are external aborts in ARM terminology) we should propagate this
6634 * error out so that we can turn it into a Data Abort if this walk
6635 * was being done for a CPU load/store or an address translation instruction
6636 * (but not if it was for a debug access).
6638 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
6639 ARMMMUIdx mmu_idx, uint32_t *fsr,
6640 ARMMMUFaultInfo *fi)
6642 ARMCPU *cpu = ARM_CPU(cs);
6643 CPUARMState *env = &cpu->env;
6644 MemTxAttrs attrs = {};
6645 AddressSpace *as;
6647 attrs.secure = is_secure;
6648 as = arm_addressspace(cs, attrs);
6649 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
6650 if (fi->s1ptw) {
6651 return 0;
6653 return address_space_ldl(as, addr, attrs, NULL);
6656 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
6657 ARMMMUIdx mmu_idx, uint32_t *fsr,
6658 ARMMMUFaultInfo *fi)
6660 ARMCPU *cpu = ARM_CPU(cs);
6661 CPUARMState *env = &cpu->env;
6662 MemTxAttrs attrs = {};
6663 AddressSpace *as;
6665 attrs.secure = is_secure;
6666 as = arm_addressspace(cs, attrs);
6667 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
6668 if (fi->s1ptw) {
6669 return 0;
6671 return address_space_ldq(as, addr, attrs, NULL);
6674 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
6675 int access_type, ARMMMUIdx mmu_idx,
6676 hwaddr *phys_ptr, int *prot,
6677 target_ulong *page_size, uint32_t *fsr,
6678 ARMMMUFaultInfo *fi)
6680 CPUState *cs = CPU(arm_env_get_cpu(env));
6681 int code;
6682 uint32_t table;
6683 uint32_t desc;
6684 int type;
6685 int ap;
6686 int domain = 0;
6687 int domain_prot;
6688 hwaddr phys_addr;
6689 uint32_t dacr;
6691 /* Pagetable walk. */
6692 /* Lookup l1 descriptor. */
6693 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
6694 /* Section translation fault if page walk is disabled by PD0 or PD1 */
6695 code = 5;
6696 goto do_fault;
6698 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6699 mmu_idx, fsr, fi);
6700 type = (desc & 3);
6701 domain = (desc >> 5) & 0x0f;
6702 if (regime_el(env, mmu_idx) == 1) {
6703 dacr = env->cp15.dacr_ns;
6704 } else {
6705 dacr = env->cp15.dacr_s;
6707 domain_prot = (dacr >> (domain * 2)) & 3;
6708 if (type == 0) {
6709 /* Section translation fault. */
6710 code = 5;
6711 goto do_fault;
6713 if (domain_prot == 0 || domain_prot == 2) {
6714 if (type == 2)
6715 code = 9; /* Section domain fault. */
6716 else
6717 code = 11; /* Page domain fault. */
6718 goto do_fault;
6720 if (type == 2) {
6721 /* 1Mb section. */
6722 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
6723 ap = (desc >> 10) & 3;
6724 code = 13;
6725 *page_size = 1024 * 1024;
6726 } else {
6727 /* Lookup l2 entry. */
6728 if (type == 1) {
6729 /* Coarse pagetable. */
6730 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
6731 } else {
6732 /* Fine pagetable. */
6733 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
6735 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6736 mmu_idx, fsr, fi);
6737 switch (desc & 3) {
6738 case 0: /* Page translation fault. */
6739 code = 7;
6740 goto do_fault;
6741 case 1: /* 64k page. */
6742 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6743 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
6744 *page_size = 0x10000;
6745 break;
6746 case 2: /* 4k page. */
6747 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
6748 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
6749 *page_size = 0x1000;
6750 break;
6751 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
6752 if (type == 1) {
6753 /* ARMv6/XScale extended small page format */
6754 if (arm_feature(env, ARM_FEATURE_XSCALE)
6755 || arm_feature(env, ARM_FEATURE_V6)) {
6756 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
6757 *page_size = 0x1000;
6758 } else {
6759 /* UNPREDICTABLE in ARMv5; we choose to take a
6760 * page translation fault.
6762 code = 7;
6763 goto do_fault;
6765 } else {
6766 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
6767 *page_size = 0x400;
6769 ap = (desc >> 4) & 3;
6770 break;
6771 default:
6772 /* Never happens, but compiler isn't smart enough to tell. */
6773 abort();
6775 code = 15;
6777 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
6778 *prot |= *prot ? PAGE_EXEC : 0;
6779 if (!(*prot & (1 << access_type))) {
6780 /* Access permission fault. */
6781 goto do_fault;
6783 *phys_ptr = phys_addr;
6784 return false;
6785 do_fault:
6786 *fsr = code | (domain << 4);
6787 return true;
6790 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
6791 int access_type, ARMMMUIdx mmu_idx,
6792 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
6793 target_ulong *page_size, uint32_t *fsr,
6794 ARMMMUFaultInfo *fi)
6796 CPUState *cs = CPU(arm_env_get_cpu(env));
6797 int code;
6798 uint32_t table;
6799 uint32_t desc;
6800 uint32_t xn;
6801 uint32_t pxn = 0;
6802 int type;
6803 int ap;
6804 int domain = 0;
6805 int domain_prot;
6806 hwaddr phys_addr;
6807 uint32_t dacr;
6808 bool ns;
6810 /* Pagetable walk. */
6811 /* Lookup l1 descriptor. */
6812 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
6813 /* Section translation fault if page walk is disabled by PD0 or PD1 */
6814 code = 5;
6815 goto do_fault;
6817 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6818 mmu_idx, fsr, fi);
6819 type = (desc & 3);
6820 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
6821 /* Section translation fault, or attempt to use the encoding
6822 * which is Reserved on implementations without PXN.
6824 code = 5;
6825 goto do_fault;
6827 if ((type == 1) || !(desc & (1 << 18))) {
6828 /* Page or Section. */
6829 domain = (desc >> 5) & 0x0f;
6831 if (regime_el(env, mmu_idx) == 1) {
6832 dacr = env->cp15.dacr_ns;
6833 } else {
6834 dacr = env->cp15.dacr_s;
6836 domain_prot = (dacr >> (domain * 2)) & 3;
6837 if (domain_prot == 0 || domain_prot == 2) {
6838 if (type != 1) {
6839 code = 9; /* Section domain fault. */
6840 } else {
6841 code = 11; /* Page domain fault. */
6843 goto do_fault;
6845 if (type != 1) {
6846 if (desc & (1 << 18)) {
6847 /* Supersection. */
6848 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
6849 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
6850 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
6851 *page_size = 0x1000000;
6852 } else {
6853 /* Section. */
6854 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
6855 *page_size = 0x100000;
6857 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
6858 xn = desc & (1 << 4);
6859 pxn = desc & 1;
6860 code = 13;
6861 ns = extract32(desc, 19, 1);
6862 } else {
6863 if (arm_feature(env, ARM_FEATURE_PXN)) {
6864 pxn = (desc >> 2) & 1;
6866 ns = extract32(desc, 3, 1);
6867 /* Lookup l2 entry. */
6868 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
6869 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6870 mmu_idx, fsr, fi);
6871 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
6872 switch (desc & 3) {
6873 case 0: /* Page translation fault. */
6874 code = 7;
6875 goto do_fault;
6876 case 1: /* 64k page. */
6877 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6878 xn = desc & (1 << 15);
6879 *page_size = 0x10000;
6880 break;
6881 case 2: case 3: /* 4k page. */
6882 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
6883 xn = desc & 1;
6884 *page_size = 0x1000;
6885 break;
6886 default:
6887 /* Never happens, but compiler isn't smart enough to tell. */
6888 abort();
6890 code = 15;
6892 if (domain_prot == 3) {
6893 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
6894 } else {
6895 if (pxn && !regime_is_user(env, mmu_idx)) {
6896 xn = 1;
6898 if (xn && access_type == 2)
6899 goto do_fault;
6901 if (arm_feature(env, ARM_FEATURE_V6K) &&
6902 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
6903 /* The simplified model uses AP[0] as an access control bit. */
6904 if ((ap & 1) == 0) {
6905 /* Access flag fault. */
6906 code = (code == 15) ? 6 : 3;
6907 goto do_fault;
6909 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
6910 } else {
6911 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
6913 if (*prot && !xn) {
6914 *prot |= PAGE_EXEC;
6916 if (!(*prot & (1 << access_type))) {
6917 /* Access permission fault. */
6918 goto do_fault;
6921 if (ns) {
6922 /* The NS bit will (as required by the architecture) have no effect if
6923 * the CPU doesn't support TZ or this is a non-secure translation
6924 * regime, because the attribute will already be non-secure.
6926 attrs->secure = false;
6928 *phys_ptr = phys_addr;
6929 return false;
6930 do_fault:
6931 *fsr = code | (domain << 4);
6932 return true;
6935 /* Fault type for long-descriptor MMU fault reporting; this corresponds
6936 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
6938 typedef enum {
6939 translation_fault = 1,
6940 access_fault = 2,
6941 permission_fault = 3,
6942 } MMUFaultType;
6945 * check_s2_mmu_setup
6946 * @cpu: ARMCPU
6947 * @is_aa64: True if the translation regime is in AArch64 state
6948 * @startlevel: Suggested starting level
6949 * @inputsize: Bitsize of IPAs
6950 * @stride: Page-table stride (See the ARM ARM)
6952 * Returns true if the suggested S2 translation parameters are OK and
6953 * false otherwise.
6955 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
6956 int inputsize, int stride)
6958 const int grainsize = stride + 3;
6959 int startsizecheck;
6961 /* Negative levels are never allowed. */
6962 if (level < 0) {
6963 return false;
6966 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
6967 if (startsizecheck < 1 || startsizecheck > stride + 4) {
6968 return false;
6971 if (is_aa64) {
6972 CPUARMState *env = &cpu->env;
6973 unsigned int pamax = arm_pamax(cpu);
6975 switch (stride) {
6976 case 13: /* 64KB Pages. */
6977 if (level == 0 || (level == 1 && pamax <= 42)) {
6978 return false;
6980 break;
6981 case 11: /* 16KB Pages. */
6982 if (level == 0 || (level == 1 && pamax <= 40)) {
6983 return false;
6985 break;
6986 case 9: /* 4KB Pages. */
6987 if (level == 0 && pamax <= 42) {
6988 return false;
6990 break;
6991 default:
6992 g_assert_not_reached();
6995 /* Inputsize checks. */
6996 if (inputsize > pamax &&
6997 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
6998 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
6999 return false;
7001 } else {
7002 /* AArch32 only supports 4KB pages. Assert on that. */
7003 assert(stride == 9);
7005 if (level == 0) {
7006 return false;
7009 return true;
7012 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
7013 int access_type, ARMMMUIdx mmu_idx,
7014 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
7015 target_ulong *page_size_ptr, uint32_t *fsr,
7016 ARMMMUFaultInfo *fi)
7018 ARMCPU *cpu = arm_env_get_cpu(env);
7019 CPUState *cs = CPU(cpu);
7020 /* Read an LPAE long-descriptor translation table. */
7021 MMUFaultType fault_type = translation_fault;
7022 uint32_t level = 1;
7023 uint32_t epd = 0;
7024 int32_t t0sz, t1sz;
7025 uint32_t tg;
7026 uint64_t ttbr;
7027 int ttbr_select;
7028 hwaddr descaddr, descmask;
7029 uint32_t tableattrs;
7030 target_ulong page_size;
7031 uint32_t attrs;
7032 int32_t stride = 9;
7033 int32_t va_size = 32;
7034 int inputsize;
7035 int32_t tbi = 0;
7036 TCR *tcr = regime_tcr(env, mmu_idx);
7037 int ap, ns, xn, pxn;
7038 uint32_t el = regime_el(env, mmu_idx);
7039 bool ttbr1_valid = true;
7040 uint64_t descaddrmask;
7042 /* TODO:
7043 * This code does not handle the different format TCR for VTCR_EL2.
7044 * This code also does not support shareability levels.
7045 * Attribute and permission bit handling should also be checked when adding
7046 * support for those page table walks.
7048 if (arm_el_is_aa64(env, el)) {
7049 va_size = 64;
7050 if (el > 1) {
7051 if (mmu_idx != ARMMMUIdx_S2NS) {
7052 tbi = extract64(tcr->raw_tcr, 20, 1);
7054 } else {
7055 if (extract64(address, 55, 1)) {
7056 tbi = extract64(tcr->raw_tcr, 38, 1);
7057 } else {
7058 tbi = extract64(tcr->raw_tcr, 37, 1);
7061 tbi *= 8;
7063 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
7064 * invalid.
7066 if (el > 1) {
7067 ttbr1_valid = false;
7069 } else {
7070 /* There is no TTBR1 for EL2 */
7071 if (el == 2) {
7072 ttbr1_valid = false;
7076 /* Determine whether this address is in the region controlled by
7077 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
7078 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
7079 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
7081 if (va_size == 64) {
7082 /* AArch64 translation. */
7083 t0sz = extract32(tcr->raw_tcr, 0, 6);
7084 t0sz = MIN(t0sz, 39);
7085 t0sz = MAX(t0sz, 16);
7086 } else if (mmu_idx != ARMMMUIdx_S2NS) {
7087 /* AArch32 stage 1 translation. */
7088 t0sz = extract32(tcr->raw_tcr, 0, 3);
7089 } else {
7090 /* AArch32 stage 2 translation. */
7091 bool sext = extract32(tcr->raw_tcr, 4, 1);
7092 bool sign = extract32(tcr->raw_tcr, 3, 1);
7093 t0sz = sextract32(tcr->raw_tcr, 0, 4);
7095 /* If the sign-extend bit is not the same as t0sz[3], the result
7096 * is unpredictable. Flag this as a guest error. */
7097 if (sign != sext) {
7098 qemu_log_mask(LOG_GUEST_ERROR,
7099 "AArch32: VTCR.S / VTCR.T0SZ[3] missmatch\n");
7102 t1sz = extract32(tcr->raw_tcr, 16, 6);
7103 if (va_size == 64) {
7104 t1sz = MIN(t1sz, 39);
7105 t1sz = MAX(t1sz, 16);
7107 if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
7108 /* there is a ttbr0 region and we are in it (high bits all zero) */
7109 ttbr_select = 0;
7110 } else if (ttbr1_valid && t1sz &&
7111 !extract64(~address, va_size - t1sz, t1sz - tbi)) {
7112 /* there is a ttbr1 region and we are in it (high bits all one) */
7113 ttbr_select = 1;
7114 } else if (!t0sz) {
7115 /* ttbr0 region is "everything not in the ttbr1 region" */
7116 ttbr_select = 0;
7117 } else if (!t1sz && ttbr1_valid) {
7118 /* ttbr1 region is "everything not in the ttbr0 region" */
7119 ttbr_select = 1;
7120 } else {
7121 /* in the gap between the two regions, this is a Translation fault */
7122 fault_type = translation_fault;
7123 goto do_fault;
7126 /* Note that QEMU ignores shareability and cacheability attributes,
7127 * so we don't need to do anything with the SH, ORGN, IRGN fields
7128 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
7129 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
7130 * implement any ASID-like capability so we can ignore it (instead
7131 * we will always flush the TLB any time the ASID is changed).
7133 if (ttbr_select == 0) {
7134 ttbr = regime_ttbr(env, mmu_idx, 0);
7135 if (el < 2) {
7136 epd = extract32(tcr->raw_tcr, 7, 1);
7138 inputsize = va_size - t0sz;
7140 tg = extract32(tcr->raw_tcr, 14, 2);
7141 if (tg == 1) { /* 64KB pages */
7142 stride = 13;
7144 if (tg == 2) { /* 16KB pages */
7145 stride = 11;
7147 } else {
7148 /* We should only be here if TTBR1 is valid */
7149 assert(ttbr1_valid);
7151 ttbr = regime_ttbr(env, mmu_idx, 1);
7152 epd = extract32(tcr->raw_tcr, 23, 1);
7153 inputsize = va_size - t1sz;
7155 tg = extract32(tcr->raw_tcr, 30, 2);
7156 if (tg == 3) { /* 64KB pages */
7157 stride = 13;
7159 if (tg == 1) { /* 16KB pages */
7160 stride = 11;
7164 /* Here we should have set up all the parameters for the translation:
7165 * va_size, inputsize, ttbr, epd, stride, tbi
7168 if (epd) {
7169 /* Translation table walk disabled => Translation fault on TLB miss
7170 * Note: This is always 0 on 64-bit EL2 and EL3.
7172 goto do_fault;
7175 if (mmu_idx != ARMMMUIdx_S2NS) {
7176 /* The starting level depends on the virtual address size (which can
7177 * be up to 48 bits) and the translation granule size. It indicates
7178 * the number of strides (stride bits at a time) needed to
7179 * consume the bits of the input address. In the pseudocode this is:
7180 * level = 4 - RoundUp((inputsize - grainsize) / stride)
7181 * where their 'inputsize' is our 'inputsize', 'grainsize' is
7182 * our 'stride + 3' and 'stride' is our 'stride'.
7183 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
7184 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
7185 * = 4 - (inputsize - 4) / stride;
7187 level = 4 - (inputsize - 4) / stride;
7188 } else {
7189 /* For stage 2 translations the starting level is specified by the
7190 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
7192 int startlevel = extract32(tcr->raw_tcr, 6, 2);
7193 bool ok;
7195 if (va_size == 32 || stride == 9) {
7196 /* AArch32 or 4KB pages */
7197 level = 2 - startlevel;
7198 } else {
7199 /* 16KB or 64KB pages */
7200 level = 3 - startlevel;
7203 /* Check that the starting level is valid. */
7204 ok = check_s2_mmu_setup(cpu, va_size == 64, level, inputsize, stride);
7205 if (!ok) {
7206 /* AArch64 reports these as level 0 faults.
7207 * AArch32 reports these as level 1 faults.
7209 level = va_size == 64 ? 0 : 1;
7210 fault_type = translation_fault;
7211 goto do_fault;
7215 /* Clear the vaddr bits which aren't part of the within-region address,
7216 * so that we don't have to special case things when calculating the
7217 * first descriptor address.
7219 if (va_size != inputsize) {
7220 address &= (1ULL << inputsize) - 1;
7223 descmask = (1ULL << (stride + 3)) - 1;
7225 /* Now we can extract the actual base address from the TTBR */
7226 descaddr = extract64(ttbr, 0, 48);
7227 descaddr &= ~((1ULL << (inputsize - (stride * (4 - level)))) - 1);
7229 /* The address field in the descriptor goes up to bit 39 for ARMv7
7230 * but up to bit 47 for ARMv8.
7232 if (arm_feature(env, ARM_FEATURE_V8)) {
7233 descaddrmask = 0xfffffffff000ULL;
7234 } else {
7235 descaddrmask = 0xfffffff000ULL;
7238 /* Secure accesses start with the page table in secure memory and
7239 * can be downgraded to non-secure at any step. Non-secure accesses
7240 * remain non-secure. We implement this by just ORing in the NSTable/NS
7241 * bits at each step.
7243 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
7244 for (;;) {
7245 uint64_t descriptor;
7246 bool nstable;
7248 descaddr |= (address >> (stride * (4 - level))) & descmask;
7249 descaddr &= ~7ULL;
7250 nstable = extract32(tableattrs, 4, 1);
7251 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fsr, fi);
7252 if (fi->s1ptw) {
7253 goto do_fault;
7256 if (!(descriptor & 1) ||
7257 (!(descriptor & 2) && (level == 3))) {
7258 /* Invalid, or the Reserved level 3 encoding */
7259 goto do_fault;
7261 descaddr = descriptor & descaddrmask;
7263 if ((descriptor & 2) && (level < 3)) {
7264 /* Table entry. The top five bits are attributes which may
7265 * propagate down through lower levels of the table (and
7266 * which are all arranged so that 0 means "no effect", so
7267 * we can gather them up by ORing in the bits at each level).
7269 tableattrs |= extract64(descriptor, 59, 5);
7270 level++;
7271 continue;
7273 /* Block entry at level 1 or 2, or page entry at level 3.
7274 * These are basically the same thing, although the number
7275 * of bits we pull in from the vaddr varies.
7277 page_size = (1ULL << ((stride * (4 - level)) + 3));
7278 descaddr |= (address & (page_size - 1));
7279 /* Extract attributes from the descriptor */
7280 attrs = extract64(descriptor, 2, 10)
7281 | (extract64(descriptor, 52, 12) << 10);
7283 if (mmu_idx == ARMMMUIdx_S2NS) {
7284 /* Stage 2 table descriptors do not include any attribute fields */
7285 break;
7287 /* Merge in attributes from table descriptors */
7288 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
7289 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
7290 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
7291 * means "force PL1 access only", which means forcing AP[1] to 0.
7293 if (extract32(tableattrs, 2, 1)) {
7294 attrs &= ~(1 << 4);
7296 attrs |= nstable << 3; /* NS */
7297 break;
7299 /* Here descaddr is the final physical address, and attributes
7300 * are all in attrs.
7302 fault_type = access_fault;
7303 if ((attrs & (1 << 8)) == 0) {
7304 /* Access flag */
7305 goto do_fault;
7308 ap = extract32(attrs, 4, 2);
7309 xn = extract32(attrs, 12, 1);
7311 if (mmu_idx == ARMMMUIdx_S2NS) {
7312 ns = true;
7313 *prot = get_S2prot(env, ap, xn);
7314 } else {
7315 ns = extract32(attrs, 3, 1);
7316 pxn = extract32(attrs, 11, 1);
7317 *prot = get_S1prot(env, mmu_idx, va_size == 64, ap, ns, xn, pxn);
7320 fault_type = permission_fault;
7321 if (!(*prot & (1 << access_type))) {
7322 goto do_fault;
7325 if (ns) {
7326 /* The NS bit will (as required by the architecture) have no effect if
7327 * the CPU doesn't support TZ or this is a non-secure translation
7328 * regime, because the attribute will already be non-secure.
7330 txattrs->secure = false;
7332 *phys_ptr = descaddr;
7333 *page_size_ptr = page_size;
7334 return false;
7336 do_fault:
7337 /* Long-descriptor format IFSR/DFSR value */
7338 *fsr = (1 << 9) | (fault_type << 2) | level;
7339 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
7340 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
7341 return true;
7344 static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
7345 ARMMMUIdx mmu_idx,
7346 int32_t address, int *prot)
7348 *prot = PAGE_READ | PAGE_WRITE;
7349 switch (address) {
7350 case 0xF0000000 ... 0xFFFFFFFF:
7351 if (regime_sctlr(env, mmu_idx) & SCTLR_V) { /* hivecs execing is ok */
7352 *prot |= PAGE_EXEC;
7354 break;
7355 case 0x00000000 ... 0x7FFFFFFF:
7356 *prot |= PAGE_EXEC;
7357 break;
7362 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
7363 int access_type, ARMMMUIdx mmu_idx,
7364 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
7366 ARMCPU *cpu = arm_env_get_cpu(env);
7367 int n;
7368 bool is_user = regime_is_user(env, mmu_idx);
7370 *phys_ptr = address;
7371 *prot = 0;
7373 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
7374 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
7375 } else { /* MPU enabled */
7376 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
7377 /* region search */
7378 uint32_t base = env->pmsav7.drbar[n];
7379 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
7380 uint32_t rmask;
7381 bool srdis = false;
7383 if (!(env->pmsav7.drsr[n] & 0x1)) {
7384 continue;
7387 if (!rsize) {
7388 qemu_log_mask(LOG_GUEST_ERROR, "DRSR.Rsize field can not be 0");
7389 continue;
7391 rsize++;
7392 rmask = (1ull << rsize) - 1;
7394 if (base & rmask) {
7395 qemu_log_mask(LOG_GUEST_ERROR, "DRBAR %" PRIx32 " misaligned "
7396 "to DRSR region size, mask = %" PRIx32,
7397 base, rmask);
7398 continue;
7401 if (address < base || address > base + rmask) {
7402 continue;
7405 /* Region matched */
7407 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
7408 int i, snd;
7409 uint32_t srdis_mask;
7411 rsize -= 3; /* sub region size (power of 2) */
7412 snd = ((address - base) >> rsize) & 0x7;
7413 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
7415 srdis_mask = srdis ? 0x3 : 0x0;
7416 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
7417 /* This will check in groups of 2, 4 and then 8, whether
7418 * the subregion bits are consistent. rsize is incremented
7419 * back up to give the region size, considering consistent
7420 * adjacent subregions as one region. Stop testing if rsize
7421 * is already big enough for an entire QEMU page.
7423 int snd_rounded = snd & ~(i - 1);
7424 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
7425 snd_rounded + 8, i);
7426 if (srdis_mask ^ srdis_multi) {
7427 break;
7429 srdis_mask = (srdis_mask << i) | srdis_mask;
7430 rsize++;
7433 if (rsize < TARGET_PAGE_BITS) {
7434 qemu_log_mask(LOG_UNIMP, "No support for MPU (sub)region"
7435 "alignment of %" PRIu32 " bits. Minimum is %d\n",
7436 rsize, TARGET_PAGE_BITS);
7437 continue;
7439 if (srdis) {
7440 continue;
7442 break;
7445 if (n == -1) { /* no hits */
7446 if (cpu->pmsav7_dregion &&
7447 (is_user || !(regime_sctlr(env, mmu_idx) & SCTLR_BR))) {
7448 /* background fault */
7449 *fsr = 0;
7450 return true;
7452 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
7453 } else { /* a MPU hit! */
7454 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
7456 if (is_user) { /* User mode AP bit decoding */
7457 switch (ap) {
7458 case 0:
7459 case 1:
7460 case 5:
7461 break; /* no access */
7462 case 3:
7463 *prot |= PAGE_WRITE;
7464 /* fall through */
7465 case 2:
7466 case 6:
7467 *prot |= PAGE_READ | PAGE_EXEC;
7468 break;
7469 default:
7470 qemu_log_mask(LOG_GUEST_ERROR,
7471 "Bad value for AP bits in DRACR %"
7472 PRIx32 "\n", ap);
7474 } else { /* Priv. mode AP bits decoding */
7475 switch (ap) {
7476 case 0:
7477 break; /* no access */
7478 case 1:
7479 case 2:
7480 case 3:
7481 *prot |= PAGE_WRITE;
7482 /* fall through */
7483 case 5:
7484 case 6:
7485 *prot |= PAGE_READ | PAGE_EXEC;
7486 break;
7487 default:
7488 qemu_log_mask(LOG_GUEST_ERROR,
7489 "Bad value for AP bits in DRACR %"
7490 PRIx32 "\n", ap);
7494 /* execute never */
7495 if (env->pmsav7.dracr[n] & (1 << 12)) {
7496 *prot &= ~PAGE_EXEC;
7501 *fsr = 0x00d; /* Permission fault */
7502 return !(*prot & (1 << access_type));
7505 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
7506 int access_type, ARMMMUIdx mmu_idx,
7507 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
7509 int n;
7510 uint32_t mask;
7511 uint32_t base;
7512 bool is_user = regime_is_user(env, mmu_idx);
7514 *phys_ptr = address;
7515 for (n = 7; n >= 0; n--) {
7516 base = env->cp15.c6_region[n];
7517 if ((base & 1) == 0) {
7518 continue;
7520 mask = 1 << ((base >> 1) & 0x1f);
7521 /* Keep this shift separate from the above to avoid an
7522 (undefined) << 32. */
7523 mask = (mask << 1) - 1;
7524 if (((base ^ address) & ~mask) == 0) {
7525 break;
7528 if (n < 0) {
7529 *fsr = 2;
7530 return true;
7533 if (access_type == 2) {
7534 mask = env->cp15.pmsav5_insn_ap;
7535 } else {
7536 mask = env->cp15.pmsav5_data_ap;
7538 mask = (mask >> (n * 4)) & 0xf;
7539 switch (mask) {
7540 case 0:
7541 *fsr = 1;
7542 return true;
7543 case 1:
7544 if (is_user) {
7545 *fsr = 1;
7546 return true;
7548 *prot = PAGE_READ | PAGE_WRITE;
7549 break;
7550 case 2:
7551 *prot = PAGE_READ;
7552 if (!is_user) {
7553 *prot |= PAGE_WRITE;
7555 break;
7556 case 3:
7557 *prot = PAGE_READ | PAGE_WRITE;
7558 break;
7559 case 5:
7560 if (is_user) {
7561 *fsr = 1;
7562 return true;
7564 *prot = PAGE_READ;
7565 break;
7566 case 6:
7567 *prot = PAGE_READ;
7568 break;
7569 default:
7570 /* Bad permission. */
7571 *fsr = 1;
7572 return true;
7574 *prot |= PAGE_EXEC;
7575 return false;
7578 /* get_phys_addr - get the physical address for this virtual address
7580 * Find the physical address corresponding to the given virtual address,
7581 * by doing a translation table walk on MMU based systems or using the
7582 * MPU state on MPU based systems.
7584 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
7585 * prot and page_size may not be filled in, and the populated fsr value provides
7586 * information on why the translation aborted, in the format of a
7587 * DFSR/IFSR fault register, with the following caveats:
7588 * * we honour the short vs long DFSR format differences.
7589 * * the WnR bit is never set (the caller must do this).
7590 * * for PSMAv5 based systems we don't bother to return a full FSR format
7591 * value.
7593 * @env: CPUARMState
7594 * @address: virtual address to get physical address for
7595 * @access_type: 0 for read, 1 for write, 2 for execute
7596 * @mmu_idx: MMU index indicating required translation regime
7597 * @phys_ptr: set to the physical address corresponding to the virtual address
7598 * @attrs: set to the memory transaction attributes to use
7599 * @prot: set to the permissions for the page containing phys_ptr
7600 * @page_size: set to the size of the page containing phys_ptr
7601 * @fsr: set to the DFSR/IFSR value on failure
7603 static bool get_phys_addr(CPUARMState *env, target_ulong address,
7604 int access_type, ARMMMUIdx mmu_idx,
7605 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
7606 target_ulong *page_size, uint32_t *fsr,
7607 ARMMMUFaultInfo *fi)
7609 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
7610 /* Call ourselves recursively to do the stage 1 and then stage 2
7611 * translations.
7613 if (arm_feature(env, ARM_FEATURE_EL2)) {
7614 hwaddr ipa;
7615 int s2_prot;
7616 int ret;
7618 ret = get_phys_addr(env, address, access_type,
7619 mmu_idx + ARMMMUIdx_S1NSE0, &ipa, attrs,
7620 prot, page_size, fsr, fi);
7622 /* If S1 fails or S2 is disabled, return early. */
7623 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
7624 *phys_ptr = ipa;
7625 return ret;
7628 /* S1 is done. Now do S2 translation. */
7629 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
7630 phys_ptr, attrs, &s2_prot,
7631 page_size, fsr, fi);
7632 fi->s2addr = ipa;
7633 /* Combine the S1 and S2 perms. */
7634 *prot &= s2_prot;
7635 return ret;
7636 } else {
7638 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
7640 mmu_idx += ARMMMUIdx_S1NSE0;
7644 /* The page table entries may downgrade secure to non-secure, but
7645 * cannot upgrade an non-secure translation regime's attributes
7646 * to secure.
7648 attrs->secure = regime_is_secure(env, mmu_idx);
7649 attrs->user = regime_is_user(env, mmu_idx);
7651 /* Fast Context Switch Extension. This doesn't exist at all in v8.
7652 * In v7 and earlier it affects all stage 1 translations.
7654 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
7655 && !arm_feature(env, ARM_FEATURE_V8)) {
7656 if (regime_el(env, mmu_idx) == 3) {
7657 address += env->cp15.fcseidr_s;
7658 } else {
7659 address += env->cp15.fcseidr_ns;
7663 /* pmsav7 has special handling for when MPU is disabled so call it before
7664 * the common MMU/MPU disabled check below.
7666 if (arm_feature(env, ARM_FEATURE_MPU) &&
7667 arm_feature(env, ARM_FEATURE_V7)) {
7668 *page_size = TARGET_PAGE_SIZE;
7669 return get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
7670 phys_ptr, prot, fsr);
7673 if (regime_translation_disabled(env, mmu_idx)) {
7674 /* MMU/MPU disabled. */
7675 *phys_ptr = address;
7676 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
7677 *page_size = TARGET_PAGE_SIZE;
7678 return 0;
7681 if (arm_feature(env, ARM_FEATURE_MPU)) {
7682 /* Pre-v7 MPU */
7683 *page_size = TARGET_PAGE_SIZE;
7684 return get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
7685 phys_ptr, prot, fsr);
7688 if (regime_using_lpae_format(env, mmu_idx)) {
7689 return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
7690 attrs, prot, page_size, fsr, fi);
7691 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
7692 return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
7693 attrs, prot, page_size, fsr, fi);
7694 } else {
7695 return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
7696 prot, page_size, fsr, fi);
7700 /* Walk the page table and (if the mapping exists) add the page
7701 * to the TLB. Return false on success, or true on failure. Populate
7702 * fsr with ARM DFSR/IFSR fault register format value on failure.
7704 bool arm_tlb_fill(CPUState *cs, vaddr address,
7705 int access_type, int mmu_idx, uint32_t *fsr,
7706 ARMMMUFaultInfo *fi)
7708 ARMCPU *cpu = ARM_CPU(cs);
7709 CPUARMState *env = &cpu->env;
7710 hwaddr phys_addr;
7711 target_ulong page_size;
7712 int prot;
7713 int ret;
7714 MemTxAttrs attrs = {};
7716 ret = get_phys_addr(env, address, access_type, mmu_idx, &phys_addr,
7717 &attrs, &prot, &page_size, fsr, fi);
7718 if (!ret) {
7719 /* Map a single [sub]page. */
7720 phys_addr &= TARGET_PAGE_MASK;
7721 address &= TARGET_PAGE_MASK;
7722 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
7723 prot, mmu_idx, page_size);
7724 return 0;
7727 return ret;
7730 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
7731 MemTxAttrs *attrs)
7733 ARMCPU *cpu = ARM_CPU(cs);
7734 CPUARMState *env = &cpu->env;
7735 hwaddr phys_addr;
7736 target_ulong page_size;
7737 int prot;
7738 bool ret;
7739 uint32_t fsr;
7740 ARMMMUFaultInfo fi = {};
7742 *attrs = (MemTxAttrs) {};
7744 ret = get_phys_addr(env, addr, 0, cpu_mmu_index(env, false), &phys_addr,
7745 attrs, &prot, &page_size, &fsr, &fi);
7747 if (ret) {
7748 return -1;
7750 return phys_addr;
7753 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
7755 ARMCPU *cpu = arm_env_get_cpu(env);
7757 switch (reg) {
7758 case 0: /* APSR */
7759 return xpsr_read(env) & 0xf8000000;
7760 case 1: /* IAPSR */
7761 return xpsr_read(env) & 0xf80001ff;
7762 case 2: /* EAPSR */
7763 return xpsr_read(env) & 0xff00fc00;
7764 case 3: /* xPSR */
7765 return xpsr_read(env) & 0xff00fdff;
7766 case 5: /* IPSR */
7767 return xpsr_read(env) & 0x000001ff;
7768 case 6: /* EPSR */
7769 return xpsr_read(env) & 0x0700fc00;
7770 case 7: /* IEPSR */
7771 return xpsr_read(env) & 0x0700edff;
7772 case 8: /* MSP */
7773 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
7774 case 9: /* PSP */
7775 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
7776 case 16: /* PRIMASK */
7777 return (env->daif & PSTATE_I) != 0;
7778 case 17: /* BASEPRI */
7779 case 18: /* BASEPRI_MAX */
7780 return env->v7m.basepri;
7781 case 19: /* FAULTMASK */
7782 return (env->daif & PSTATE_F) != 0;
7783 case 20: /* CONTROL */
7784 return env->v7m.control;
7785 default:
7786 /* ??? For debugging only. */
7787 cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
7788 return 0;
7792 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
7794 ARMCPU *cpu = arm_env_get_cpu(env);
7796 switch (reg) {
7797 case 0: /* APSR */
7798 xpsr_write(env, val, 0xf8000000);
7799 break;
7800 case 1: /* IAPSR */
7801 xpsr_write(env, val, 0xf8000000);
7802 break;
7803 case 2: /* EAPSR */
7804 xpsr_write(env, val, 0xfe00fc00);
7805 break;
7806 case 3: /* xPSR */
7807 xpsr_write(env, val, 0xfe00fc00);
7808 break;
7809 case 5: /* IPSR */
7810 /* IPSR bits are readonly. */
7811 break;
7812 case 6: /* EPSR */
7813 xpsr_write(env, val, 0x0600fc00);
7814 break;
7815 case 7: /* IEPSR */
7816 xpsr_write(env, val, 0x0600fc00);
7817 break;
7818 case 8: /* MSP */
7819 if (env->v7m.current_sp)
7820 env->v7m.other_sp = val;
7821 else
7822 env->regs[13] = val;
7823 break;
7824 case 9: /* PSP */
7825 if (env->v7m.current_sp)
7826 env->regs[13] = val;
7827 else
7828 env->v7m.other_sp = val;
7829 break;
7830 case 16: /* PRIMASK */
7831 if (val & 1) {
7832 env->daif |= PSTATE_I;
7833 } else {
7834 env->daif &= ~PSTATE_I;
7836 break;
7837 case 17: /* BASEPRI */
7838 env->v7m.basepri = val & 0xff;
7839 break;
7840 case 18: /* BASEPRI_MAX */
7841 val &= 0xff;
7842 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
7843 env->v7m.basepri = val;
7844 break;
7845 case 19: /* FAULTMASK */
7846 if (val & 1) {
7847 env->daif |= PSTATE_F;
7848 } else {
7849 env->daif &= ~PSTATE_F;
7851 break;
7852 case 20: /* CONTROL */
7853 env->v7m.control = val & 3;
7854 switch_v7m_sp(env, (val & 2) != 0);
7855 break;
7856 default:
7857 /* ??? For debugging only. */
7858 cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
7859 return;
7863 #endif
7865 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
7867 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
7868 * Note that we do not implement the (architecturally mandated)
7869 * alignment fault for attempts to use this on Device memory
7870 * (which matches the usual QEMU behaviour of not implementing either
7871 * alignment faults or any memory attribute handling).
7874 ARMCPU *cpu = arm_env_get_cpu(env);
7875 uint64_t blocklen = 4 << cpu->dcz_blocksize;
7876 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
7878 #ifndef CONFIG_USER_ONLY
7880 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
7881 * the block size so we might have to do more than one TLB lookup.
7882 * We know that in fact for any v8 CPU the page size is at least 4K
7883 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
7884 * 1K as an artefact of legacy v5 subpage support being present in the
7885 * same QEMU executable.
7887 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
7888 void *hostaddr[maxidx];
7889 int try, i;
7890 unsigned mmu_idx = cpu_mmu_index(env, false);
7891 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
7893 for (try = 0; try < 2; try++) {
7895 for (i = 0; i < maxidx; i++) {
7896 hostaddr[i] = tlb_vaddr_to_host(env,
7897 vaddr + TARGET_PAGE_SIZE * i,
7898 1, mmu_idx);
7899 if (!hostaddr[i]) {
7900 break;
7903 if (i == maxidx) {
7904 /* If it's all in the TLB it's fair game for just writing to;
7905 * we know we don't need to update dirty status, etc.
7907 for (i = 0; i < maxidx - 1; i++) {
7908 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
7910 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
7911 return;
7913 /* OK, try a store and see if we can populate the tlb. This
7914 * might cause an exception if the memory isn't writable,
7915 * in which case we will longjmp out of here. We must for
7916 * this purpose use the actual register value passed to us
7917 * so that we get the fault address right.
7919 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETRA());
7920 /* Now we can populate the other TLB entries, if any */
7921 for (i = 0; i < maxidx; i++) {
7922 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
7923 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
7924 helper_ret_stb_mmu(env, va, 0, oi, GETRA());
7929 /* Slow path (probably attempt to do this to an I/O device or
7930 * similar, or clearing of a block of code we have translations
7931 * cached for). Just do a series of byte writes as the architecture
7932 * demands. It's not worth trying to use a cpu_physical_memory_map(),
7933 * memset(), unmap() sequence here because:
7934 * + we'd need to account for the blocksize being larger than a page
7935 * + the direct-RAM access case is almost always going to be dealt
7936 * with in the fastpath code above, so there's no speed benefit
7937 * + we would have to deal with the map returning NULL because the
7938 * bounce buffer was in use
7940 for (i = 0; i < blocklen; i++) {
7941 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETRA());
7944 #else
7945 memset(g2h(vaddr), 0, blocklen);
7946 #endif
7949 /* Note that signed overflow is undefined in C. The following routines are
7950 careful to use unsigned types where modulo arithmetic is required.
7951 Failure to do so _will_ break on newer gcc. */
7953 /* Signed saturating arithmetic. */
7955 /* Perform 16-bit signed saturating addition. */
7956 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
7958 uint16_t res;
7960 res = a + b;
7961 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
7962 if (a & 0x8000)
7963 res = 0x8000;
7964 else
7965 res = 0x7fff;
7967 return res;
7970 /* Perform 8-bit signed saturating addition. */
7971 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
7973 uint8_t res;
7975 res = a + b;
7976 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
7977 if (a & 0x80)
7978 res = 0x80;
7979 else
7980 res = 0x7f;
7982 return res;
7985 /* Perform 16-bit signed saturating subtraction. */
7986 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
7988 uint16_t res;
7990 res = a - b;
7991 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
7992 if (a & 0x8000)
7993 res = 0x8000;
7994 else
7995 res = 0x7fff;
7997 return res;
8000 /* Perform 8-bit signed saturating subtraction. */
8001 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
8003 uint8_t res;
8005 res = a - b;
8006 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
8007 if (a & 0x80)
8008 res = 0x80;
8009 else
8010 res = 0x7f;
8012 return res;
8015 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
8016 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
8017 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
8018 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
8019 #define PFX q
8021 #include "op_addsub.h"
8023 /* Unsigned saturating arithmetic. */
8024 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
8026 uint16_t res;
8027 res = a + b;
8028 if (res < a)
8029 res = 0xffff;
8030 return res;
8033 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
8035 if (a > b)
8036 return a - b;
8037 else
8038 return 0;
8041 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
8043 uint8_t res;
8044 res = a + b;
8045 if (res < a)
8046 res = 0xff;
8047 return res;
8050 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
8052 if (a > b)
8053 return a - b;
8054 else
8055 return 0;
8058 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
8059 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
8060 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
8061 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
8062 #define PFX uq
8064 #include "op_addsub.h"
8066 /* Signed modulo arithmetic. */
8067 #define SARITH16(a, b, n, op) do { \
8068 int32_t sum; \
8069 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
8070 RESULT(sum, n, 16); \
8071 if (sum >= 0) \
8072 ge |= 3 << (n * 2); \
8073 } while(0)
8075 #define SARITH8(a, b, n, op) do { \
8076 int32_t sum; \
8077 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
8078 RESULT(sum, n, 8); \
8079 if (sum >= 0) \
8080 ge |= 1 << n; \
8081 } while(0)
8084 #define ADD16(a, b, n) SARITH16(a, b, n, +)
8085 #define SUB16(a, b, n) SARITH16(a, b, n, -)
8086 #define ADD8(a, b, n) SARITH8(a, b, n, +)
8087 #define SUB8(a, b, n) SARITH8(a, b, n, -)
8088 #define PFX s
8089 #define ARITH_GE
8091 #include "op_addsub.h"
8093 /* Unsigned modulo arithmetic. */
8094 #define ADD16(a, b, n) do { \
8095 uint32_t sum; \
8096 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
8097 RESULT(sum, n, 16); \
8098 if ((sum >> 16) == 1) \
8099 ge |= 3 << (n * 2); \
8100 } while(0)
8102 #define ADD8(a, b, n) do { \
8103 uint32_t sum; \
8104 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
8105 RESULT(sum, n, 8); \
8106 if ((sum >> 8) == 1) \
8107 ge |= 1 << n; \
8108 } while(0)
8110 #define SUB16(a, b, n) do { \
8111 uint32_t sum; \
8112 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
8113 RESULT(sum, n, 16); \
8114 if ((sum >> 16) == 0) \
8115 ge |= 3 << (n * 2); \
8116 } while(0)
8118 #define SUB8(a, b, n) do { \
8119 uint32_t sum; \
8120 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
8121 RESULT(sum, n, 8); \
8122 if ((sum >> 8) == 0) \
8123 ge |= 1 << n; \
8124 } while(0)
8126 #define PFX u
8127 #define ARITH_GE
8129 #include "op_addsub.h"
8131 /* Halved signed arithmetic. */
8132 #define ADD16(a, b, n) \
8133 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
8134 #define SUB16(a, b, n) \
8135 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
8136 #define ADD8(a, b, n) \
8137 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
8138 #define SUB8(a, b, n) \
8139 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
8140 #define PFX sh
8142 #include "op_addsub.h"
8144 /* Halved unsigned arithmetic. */
8145 #define ADD16(a, b, n) \
8146 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
8147 #define SUB16(a, b, n) \
8148 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
8149 #define ADD8(a, b, n) \
8150 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
8151 #define SUB8(a, b, n) \
8152 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
8153 #define PFX uh
8155 #include "op_addsub.h"
8157 static inline uint8_t do_usad(uint8_t a, uint8_t b)
8159 if (a > b)
8160 return a - b;
8161 else
8162 return b - a;
8165 /* Unsigned sum of absolute byte differences. */
8166 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
8168 uint32_t sum;
8169 sum = do_usad(a, b);
8170 sum += do_usad(a >> 8, b >> 8);
8171 sum += do_usad(a >> 16, b >>16);
8172 sum += do_usad(a >> 24, b >> 24);
8173 return sum;
8176 /* For ARMv6 SEL instruction. */
8177 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
8179 uint32_t mask;
8181 mask = 0;
8182 if (flags & 1)
8183 mask |= 0xff;
8184 if (flags & 2)
8185 mask |= 0xff00;
8186 if (flags & 4)
8187 mask |= 0xff0000;
8188 if (flags & 8)
8189 mask |= 0xff000000;
8190 return (a & mask) | (b & ~mask);
8193 /* VFP support. We follow the convention used for VFP instructions:
8194 Single precision routines have a "s" suffix, double precision a
8195 "d" suffix. */
8197 /* Convert host exception flags to vfp form. */
8198 static inline int vfp_exceptbits_from_host(int host_bits)
8200 int target_bits = 0;
8202 if (host_bits & float_flag_invalid)
8203 target_bits |= 1;
8204 if (host_bits & float_flag_divbyzero)
8205 target_bits |= 2;
8206 if (host_bits & float_flag_overflow)
8207 target_bits |= 4;
8208 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
8209 target_bits |= 8;
8210 if (host_bits & float_flag_inexact)
8211 target_bits |= 0x10;
8212 if (host_bits & float_flag_input_denormal)
8213 target_bits |= 0x80;
8214 return target_bits;
8217 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
8219 int i;
8220 uint32_t fpscr;
8222 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
8223 | (env->vfp.vec_len << 16)
8224 | (env->vfp.vec_stride << 20);
8225 i = get_float_exception_flags(&env->vfp.fp_status);
8226 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
8227 fpscr |= vfp_exceptbits_from_host(i);
8228 return fpscr;
8231 uint32_t vfp_get_fpscr(CPUARMState *env)
8233 return HELPER(vfp_get_fpscr)(env);
8236 /* Convert vfp exception flags to target form. */
8237 static inline int vfp_exceptbits_to_host(int target_bits)
8239 int host_bits = 0;
8241 if (target_bits & 1)
8242 host_bits |= float_flag_invalid;
8243 if (target_bits & 2)
8244 host_bits |= float_flag_divbyzero;
8245 if (target_bits & 4)
8246 host_bits |= float_flag_overflow;
8247 if (target_bits & 8)
8248 host_bits |= float_flag_underflow;
8249 if (target_bits & 0x10)
8250 host_bits |= float_flag_inexact;
8251 if (target_bits & 0x80)
8252 host_bits |= float_flag_input_denormal;
8253 return host_bits;
8256 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
8258 int i;
8259 uint32_t changed;
8261 changed = env->vfp.xregs[ARM_VFP_FPSCR];
8262 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
8263 env->vfp.vec_len = (val >> 16) & 7;
8264 env->vfp.vec_stride = (val >> 20) & 3;
8266 changed ^= val;
8267 if (changed & (3 << 22)) {
8268 i = (val >> 22) & 3;
8269 switch (i) {
8270 case FPROUNDING_TIEEVEN:
8271 i = float_round_nearest_even;
8272 break;
8273 case FPROUNDING_POSINF:
8274 i = float_round_up;
8275 break;
8276 case FPROUNDING_NEGINF:
8277 i = float_round_down;
8278 break;
8279 case FPROUNDING_ZERO:
8280 i = float_round_to_zero;
8281 break;
8283 set_float_rounding_mode(i, &env->vfp.fp_status);
8285 if (changed & (1 << 24)) {
8286 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
8287 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
8289 if (changed & (1 << 25))
8290 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
8292 i = vfp_exceptbits_to_host(val);
8293 set_float_exception_flags(i, &env->vfp.fp_status);
8294 set_float_exception_flags(0, &env->vfp.standard_fp_status);
8297 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
8299 HELPER(vfp_set_fpscr)(env, val);
8302 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
8304 #define VFP_BINOP(name) \
8305 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
8307 float_status *fpst = fpstp; \
8308 return float32_ ## name(a, b, fpst); \
8310 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
8312 float_status *fpst = fpstp; \
8313 return float64_ ## name(a, b, fpst); \
8315 VFP_BINOP(add)
8316 VFP_BINOP(sub)
8317 VFP_BINOP(mul)
8318 VFP_BINOP(div)
8319 VFP_BINOP(min)
8320 VFP_BINOP(max)
8321 VFP_BINOP(minnum)
8322 VFP_BINOP(maxnum)
8323 #undef VFP_BINOP
8325 float32 VFP_HELPER(neg, s)(float32 a)
8327 return float32_chs(a);
8330 float64 VFP_HELPER(neg, d)(float64 a)
8332 return float64_chs(a);
8335 float32 VFP_HELPER(abs, s)(float32 a)
8337 return float32_abs(a);
8340 float64 VFP_HELPER(abs, d)(float64 a)
8342 return float64_abs(a);
8345 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
8347 return float32_sqrt(a, &env->vfp.fp_status);
8350 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
8352 return float64_sqrt(a, &env->vfp.fp_status);
8355 /* XXX: check quiet/signaling case */
8356 #define DO_VFP_cmp(p, type) \
8357 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
8359 uint32_t flags; \
8360 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
8361 case 0: flags = 0x6; break; \
8362 case -1: flags = 0x8; break; \
8363 case 1: flags = 0x2; break; \
8364 default: case 2: flags = 0x3; break; \
8366 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
8367 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
8369 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
8371 uint32_t flags; \
8372 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
8373 case 0: flags = 0x6; break; \
8374 case -1: flags = 0x8; break; \
8375 case 1: flags = 0x2; break; \
8376 default: case 2: flags = 0x3; break; \
8378 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
8379 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
8381 DO_VFP_cmp(s, float32)
8382 DO_VFP_cmp(d, float64)
8383 #undef DO_VFP_cmp
8385 /* Integer to float and float to integer conversions */
8387 #define CONV_ITOF(name, fsz, sign) \
8388 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
8390 float_status *fpst = fpstp; \
8391 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
8394 #define CONV_FTOI(name, fsz, sign, round) \
8395 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
8397 float_status *fpst = fpstp; \
8398 if (float##fsz##_is_any_nan(x)) { \
8399 float_raise(float_flag_invalid, fpst); \
8400 return 0; \
8402 return float##fsz##_to_##sign##int32##round(x, fpst); \
8405 #define FLOAT_CONVS(name, p, fsz, sign) \
8406 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
8407 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
8408 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
8410 FLOAT_CONVS(si, s, 32, )
8411 FLOAT_CONVS(si, d, 64, )
8412 FLOAT_CONVS(ui, s, 32, u)
8413 FLOAT_CONVS(ui, d, 64, u)
8415 #undef CONV_ITOF
8416 #undef CONV_FTOI
8417 #undef FLOAT_CONVS
8419 /* floating point conversion */
8420 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
8422 float64 r = float32_to_float64(x, &env->vfp.fp_status);
8423 /* ARM requires that S<->D conversion of any kind of NaN generates
8424 * a quiet NaN by forcing the most significant frac bit to 1.
8426 return float64_maybe_silence_nan(r);
8429 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
8431 float32 r = float64_to_float32(x, &env->vfp.fp_status);
8432 /* ARM requires that S<->D conversion of any kind of NaN generates
8433 * a quiet NaN by forcing the most significant frac bit to 1.
8435 return float32_maybe_silence_nan(r);
8438 /* VFP3 fixed point conversion. */
8439 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8440 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
8441 void *fpstp) \
8443 float_status *fpst = fpstp; \
8444 float##fsz tmp; \
8445 tmp = itype##_to_##float##fsz(x, fpst); \
8446 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
8449 /* Notice that we want only input-denormal exception flags from the
8450 * scalbn operation: the other possible flags (overflow+inexact if
8451 * we overflow to infinity, output-denormal) aren't correct for the
8452 * complete scale-and-convert operation.
8454 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
8455 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
8456 uint32_t shift, \
8457 void *fpstp) \
8459 float_status *fpst = fpstp; \
8460 int old_exc_flags = get_float_exception_flags(fpst); \
8461 float##fsz tmp; \
8462 if (float##fsz##_is_any_nan(x)) { \
8463 float_raise(float_flag_invalid, fpst); \
8464 return 0; \
8466 tmp = float##fsz##_scalbn(x, shift, fpst); \
8467 old_exc_flags |= get_float_exception_flags(fpst) \
8468 & float_flag_input_denormal; \
8469 set_float_exception_flags(old_exc_flags, fpst); \
8470 return float##fsz##_to_##itype##round(tmp, fpst); \
8473 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \
8474 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8475 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
8476 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
8478 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
8479 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8480 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
8482 VFP_CONV_FIX(sh, d, 64, 64, int16)
8483 VFP_CONV_FIX(sl, d, 64, 64, int32)
8484 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
8485 VFP_CONV_FIX(uh, d, 64, 64, uint16)
8486 VFP_CONV_FIX(ul, d, 64, 64, uint32)
8487 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
8488 VFP_CONV_FIX(sh, s, 32, 32, int16)
8489 VFP_CONV_FIX(sl, s, 32, 32, int32)
8490 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
8491 VFP_CONV_FIX(uh, s, 32, 32, uint16)
8492 VFP_CONV_FIX(ul, s, 32, 32, uint32)
8493 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
8494 #undef VFP_CONV_FIX
8495 #undef VFP_CONV_FIX_FLOAT
8496 #undef VFP_CONV_FLOAT_FIX_ROUND
8498 /* Set the current fp rounding mode and return the old one.
8499 * The argument is a softfloat float_round_ value.
8501 uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
8503 float_status *fp_status = &env->vfp.fp_status;
8505 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
8506 set_float_rounding_mode(rmode, fp_status);
8508 return prev_rmode;
8511 /* Set the current fp rounding mode in the standard fp status and return
8512 * the old one. This is for NEON instructions that need to change the
8513 * rounding mode but wish to use the standard FPSCR values for everything
8514 * else. Always set the rounding mode back to the correct value after
8515 * modifying it.
8516 * The argument is a softfloat float_round_ value.
8518 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
8520 float_status *fp_status = &env->vfp.standard_fp_status;
8522 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
8523 set_float_rounding_mode(rmode, fp_status);
8525 return prev_rmode;
8528 /* Half precision conversions. */
8529 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
8531 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8532 float32 r = float16_to_float32(make_float16(a), ieee, s);
8533 if (ieee) {
8534 return float32_maybe_silence_nan(r);
8536 return r;
8539 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
8541 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8542 float16 r = float32_to_float16(a, ieee, s);
8543 if (ieee) {
8544 r = float16_maybe_silence_nan(r);
8546 return float16_val(r);
8549 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
8551 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
8554 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
8556 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
8559 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
8561 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
8564 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
8566 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
8569 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
8571 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8572 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
8573 if (ieee) {
8574 return float64_maybe_silence_nan(r);
8576 return r;
8579 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
8581 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8582 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
8583 if (ieee) {
8584 r = float16_maybe_silence_nan(r);
8586 return float16_val(r);
8589 #define float32_two make_float32(0x40000000)
8590 #define float32_three make_float32(0x40400000)
8591 #define float32_one_point_five make_float32(0x3fc00000)
8593 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
8595 float_status *s = &env->vfp.standard_fp_status;
8596 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
8597 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
8598 if (!(float32_is_zero(a) || float32_is_zero(b))) {
8599 float_raise(float_flag_input_denormal, s);
8601 return float32_two;
8603 return float32_sub(float32_two, float32_mul(a, b, s), s);
8606 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
8608 float_status *s = &env->vfp.standard_fp_status;
8609 float32 product;
8610 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
8611 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
8612 if (!(float32_is_zero(a) || float32_is_zero(b))) {
8613 float_raise(float_flag_input_denormal, s);
8615 return float32_one_point_five;
8617 product = float32_mul(a, b, s);
8618 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
8621 /* NEON helpers. */
8623 /* Constants 256 and 512 are used in some helpers; we avoid relying on
8624 * int->float conversions at run-time. */
8625 #define float64_256 make_float64(0x4070000000000000LL)
8626 #define float64_512 make_float64(0x4080000000000000LL)
8627 #define float32_maxnorm make_float32(0x7f7fffff)
8628 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
8630 /* Reciprocal functions
8632 * The algorithm that must be used to calculate the estimate
8633 * is specified by the ARM ARM, see FPRecipEstimate()
8636 static float64 recip_estimate(float64 a, float_status *real_fp_status)
8638 /* These calculations mustn't set any fp exception flags,
8639 * so we use a local copy of the fp_status.
8641 float_status dummy_status = *real_fp_status;
8642 float_status *s = &dummy_status;
8643 /* q = (int)(a * 512.0) */
8644 float64 q = float64_mul(float64_512, a, s);
8645 int64_t q_int = float64_to_int64_round_to_zero(q, s);
8647 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
8648 q = int64_to_float64(q_int, s);
8649 q = float64_add(q, float64_half, s);
8650 q = float64_div(q, float64_512, s);
8651 q = float64_div(float64_one, q, s);
8653 /* s = (int)(256.0 * r + 0.5) */
8654 q = float64_mul(q, float64_256, s);
8655 q = float64_add(q, float64_half, s);
8656 q_int = float64_to_int64_round_to_zero(q, s);
8658 /* return (double)s / 256.0 */
8659 return float64_div(int64_to_float64(q_int, s), float64_256, s);
8662 /* Common wrapper to call recip_estimate */
8663 static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
8665 uint64_t val64 = float64_val(num);
8666 uint64_t frac = extract64(val64, 0, 52);
8667 int64_t exp = extract64(val64, 52, 11);
8668 uint64_t sbit;
8669 float64 scaled, estimate;
8671 /* Generate the scaled number for the estimate function */
8672 if (exp == 0) {
8673 if (extract64(frac, 51, 1) == 0) {
8674 exp = -1;
8675 frac = extract64(frac, 0, 50) << 2;
8676 } else {
8677 frac = extract64(frac, 0, 51) << 1;
8681 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
8682 scaled = make_float64((0x3feULL << 52)
8683 | extract64(frac, 44, 8) << 44);
8685 estimate = recip_estimate(scaled, fpst);
8687 /* Build new result */
8688 val64 = float64_val(estimate);
8689 sbit = 0x8000000000000000ULL & val64;
8690 exp = off - exp;
8691 frac = extract64(val64, 0, 52);
8693 if (exp == 0) {
8694 frac = 1ULL << 51 | extract64(frac, 1, 51);
8695 } else if (exp == -1) {
8696 frac = 1ULL << 50 | extract64(frac, 2, 50);
8697 exp = 0;
8700 return make_float64(sbit | (exp << 52) | frac);
8703 static bool round_to_inf(float_status *fpst, bool sign_bit)
8705 switch (fpst->float_rounding_mode) {
8706 case float_round_nearest_even: /* Round to Nearest */
8707 return true;
8708 case float_round_up: /* Round to +Inf */
8709 return !sign_bit;
8710 case float_round_down: /* Round to -Inf */
8711 return sign_bit;
8712 case float_round_to_zero: /* Round to Zero */
8713 return false;
8716 g_assert_not_reached();
8719 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
8721 float_status *fpst = fpstp;
8722 float32 f32 = float32_squash_input_denormal(input, fpst);
8723 uint32_t f32_val = float32_val(f32);
8724 uint32_t f32_sbit = 0x80000000ULL & f32_val;
8725 int32_t f32_exp = extract32(f32_val, 23, 8);
8726 uint32_t f32_frac = extract32(f32_val, 0, 23);
8727 float64 f64, r64;
8728 uint64_t r64_val;
8729 int64_t r64_exp;
8730 uint64_t r64_frac;
8732 if (float32_is_any_nan(f32)) {
8733 float32 nan = f32;
8734 if (float32_is_signaling_nan(f32)) {
8735 float_raise(float_flag_invalid, fpst);
8736 nan = float32_maybe_silence_nan(f32);
8738 if (fpst->default_nan_mode) {
8739 nan = float32_default_nan;
8741 return nan;
8742 } else if (float32_is_infinity(f32)) {
8743 return float32_set_sign(float32_zero, float32_is_neg(f32));
8744 } else if (float32_is_zero(f32)) {
8745 float_raise(float_flag_divbyzero, fpst);
8746 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8747 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
8748 /* Abs(value) < 2.0^-128 */
8749 float_raise(float_flag_overflow | float_flag_inexact, fpst);
8750 if (round_to_inf(fpst, f32_sbit)) {
8751 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8752 } else {
8753 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
8755 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
8756 float_raise(float_flag_underflow, fpst);
8757 return float32_set_sign(float32_zero, float32_is_neg(f32));
8761 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
8762 r64 = call_recip_estimate(f64, 253, fpst);
8763 r64_val = float64_val(r64);
8764 r64_exp = extract64(r64_val, 52, 11);
8765 r64_frac = extract64(r64_val, 0, 52);
8767 /* result = sign : result_exp<7:0> : fraction<51:29>; */
8768 return make_float32(f32_sbit |
8769 (r64_exp & 0xff) << 23 |
8770 extract64(r64_frac, 29, 24));
8773 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
8775 float_status *fpst = fpstp;
8776 float64 f64 = float64_squash_input_denormal(input, fpst);
8777 uint64_t f64_val = float64_val(f64);
8778 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
8779 int64_t f64_exp = extract64(f64_val, 52, 11);
8780 float64 r64;
8781 uint64_t r64_val;
8782 int64_t r64_exp;
8783 uint64_t r64_frac;
8785 /* Deal with any special cases */
8786 if (float64_is_any_nan(f64)) {
8787 float64 nan = f64;
8788 if (float64_is_signaling_nan(f64)) {
8789 float_raise(float_flag_invalid, fpst);
8790 nan = float64_maybe_silence_nan(f64);
8792 if (fpst->default_nan_mode) {
8793 nan = float64_default_nan;
8795 return nan;
8796 } else if (float64_is_infinity(f64)) {
8797 return float64_set_sign(float64_zero, float64_is_neg(f64));
8798 } else if (float64_is_zero(f64)) {
8799 float_raise(float_flag_divbyzero, fpst);
8800 return float64_set_sign(float64_infinity, float64_is_neg(f64));
8801 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
8802 /* Abs(value) < 2.0^-1024 */
8803 float_raise(float_flag_overflow | float_flag_inexact, fpst);
8804 if (round_to_inf(fpst, f64_sbit)) {
8805 return float64_set_sign(float64_infinity, float64_is_neg(f64));
8806 } else {
8807 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
8809 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
8810 float_raise(float_flag_underflow, fpst);
8811 return float64_set_sign(float64_zero, float64_is_neg(f64));
8814 r64 = call_recip_estimate(f64, 2045, fpst);
8815 r64_val = float64_val(r64);
8816 r64_exp = extract64(r64_val, 52, 11);
8817 r64_frac = extract64(r64_val, 0, 52);
8819 /* result = sign : result_exp<10:0> : fraction<51:0> */
8820 return make_float64(f64_sbit |
8821 ((r64_exp & 0x7ff) << 52) |
8822 r64_frac);
8825 /* The algorithm that must be used to calculate the estimate
8826 * is specified by the ARM ARM.
8828 static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
8830 /* These calculations mustn't set any fp exception flags,
8831 * so we use a local copy of the fp_status.
8833 float_status dummy_status = *real_fp_status;
8834 float_status *s = &dummy_status;
8835 float64 q;
8836 int64_t q_int;
8838 if (float64_lt(a, float64_half, s)) {
8839 /* range 0.25 <= a < 0.5 */
8841 /* a in units of 1/512 rounded down */
8842 /* q0 = (int)(a * 512.0); */
8843 q = float64_mul(float64_512, a, s);
8844 q_int = float64_to_int64_round_to_zero(q, s);
8846 /* reciprocal root r */
8847 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
8848 q = int64_to_float64(q_int, s);
8849 q = float64_add(q, float64_half, s);
8850 q = float64_div(q, float64_512, s);
8851 q = float64_sqrt(q, s);
8852 q = float64_div(float64_one, q, s);
8853 } else {
8854 /* range 0.5 <= a < 1.0 */
8856 /* a in units of 1/256 rounded down */
8857 /* q1 = (int)(a * 256.0); */
8858 q = float64_mul(float64_256, a, s);
8859 int64_t q_int = float64_to_int64_round_to_zero(q, s);
8861 /* reciprocal root r */
8862 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
8863 q = int64_to_float64(q_int, s);
8864 q = float64_add(q, float64_half, s);
8865 q = float64_div(q, float64_256, s);
8866 q = float64_sqrt(q, s);
8867 q = float64_div(float64_one, q, s);
8869 /* r in units of 1/256 rounded to nearest */
8870 /* s = (int)(256.0 * r + 0.5); */
8872 q = float64_mul(q, float64_256,s );
8873 q = float64_add(q, float64_half, s);
8874 q_int = float64_to_int64_round_to_zero(q, s);
8876 /* return (double)s / 256.0;*/
8877 return float64_div(int64_to_float64(q_int, s), float64_256, s);
8880 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
8882 float_status *s = fpstp;
8883 float32 f32 = float32_squash_input_denormal(input, s);
8884 uint32_t val = float32_val(f32);
8885 uint32_t f32_sbit = 0x80000000 & val;
8886 int32_t f32_exp = extract32(val, 23, 8);
8887 uint32_t f32_frac = extract32(val, 0, 23);
8888 uint64_t f64_frac;
8889 uint64_t val64;
8890 int result_exp;
8891 float64 f64;
8893 if (float32_is_any_nan(f32)) {
8894 float32 nan = f32;
8895 if (float32_is_signaling_nan(f32)) {
8896 float_raise(float_flag_invalid, s);
8897 nan = float32_maybe_silence_nan(f32);
8899 if (s->default_nan_mode) {
8900 nan = float32_default_nan;
8902 return nan;
8903 } else if (float32_is_zero(f32)) {
8904 float_raise(float_flag_divbyzero, s);
8905 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8906 } else if (float32_is_neg(f32)) {
8907 float_raise(float_flag_invalid, s);
8908 return float32_default_nan;
8909 } else if (float32_is_infinity(f32)) {
8910 return float32_zero;
8913 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
8914 * preserving the parity of the exponent. */
8916 f64_frac = ((uint64_t) f32_frac) << 29;
8917 if (f32_exp == 0) {
8918 while (extract64(f64_frac, 51, 1) == 0) {
8919 f64_frac = f64_frac << 1;
8920 f32_exp = f32_exp-1;
8922 f64_frac = extract64(f64_frac, 0, 51) << 1;
8925 if (extract64(f32_exp, 0, 1) == 0) {
8926 f64 = make_float64(((uint64_t) f32_sbit) << 32
8927 | (0x3feULL << 52)
8928 | f64_frac);
8929 } else {
8930 f64 = make_float64(((uint64_t) f32_sbit) << 32
8931 | (0x3fdULL << 52)
8932 | f64_frac);
8935 result_exp = (380 - f32_exp) / 2;
8937 f64 = recip_sqrt_estimate(f64, s);
8939 val64 = float64_val(f64);
8941 val = ((result_exp & 0xff) << 23)
8942 | ((val64 >> 29) & 0x7fffff);
8943 return make_float32(val);
8946 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
8948 float_status *s = fpstp;
8949 float64 f64 = float64_squash_input_denormal(input, s);
8950 uint64_t val = float64_val(f64);
8951 uint64_t f64_sbit = 0x8000000000000000ULL & val;
8952 int64_t f64_exp = extract64(val, 52, 11);
8953 uint64_t f64_frac = extract64(val, 0, 52);
8954 int64_t result_exp;
8955 uint64_t result_frac;
8957 if (float64_is_any_nan(f64)) {
8958 float64 nan = f64;
8959 if (float64_is_signaling_nan(f64)) {
8960 float_raise(float_flag_invalid, s);
8961 nan = float64_maybe_silence_nan(f64);
8963 if (s->default_nan_mode) {
8964 nan = float64_default_nan;
8966 return nan;
8967 } else if (float64_is_zero(f64)) {
8968 float_raise(float_flag_divbyzero, s);
8969 return float64_set_sign(float64_infinity, float64_is_neg(f64));
8970 } else if (float64_is_neg(f64)) {
8971 float_raise(float_flag_invalid, s);
8972 return float64_default_nan;
8973 } else if (float64_is_infinity(f64)) {
8974 return float64_zero;
8977 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
8978 * preserving the parity of the exponent. */
8980 if (f64_exp == 0) {
8981 while (extract64(f64_frac, 51, 1) == 0) {
8982 f64_frac = f64_frac << 1;
8983 f64_exp = f64_exp - 1;
8985 f64_frac = extract64(f64_frac, 0, 51) << 1;
8988 if (extract64(f64_exp, 0, 1) == 0) {
8989 f64 = make_float64(f64_sbit
8990 | (0x3feULL << 52)
8991 | f64_frac);
8992 } else {
8993 f64 = make_float64(f64_sbit
8994 | (0x3fdULL << 52)
8995 | f64_frac);
8998 result_exp = (3068 - f64_exp) / 2;
9000 f64 = recip_sqrt_estimate(f64, s);
9002 result_frac = extract64(float64_val(f64), 0, 52);
9004 return make_float64(f64_sbit |
9005 ((result_exp & 0x7ff) << 52) |
9006 result_frac);
9009 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
9011 float_status *s = fpstp;
9012 float64 f64;
9014 if ((a & 0x80000000) == 0) {
9015 return 0xffffffff;
9018 f64 = make_float64((0x3feULL << 52)
9019 | ((int64_t)(a & 0x7fffffff) << 21));
9021 f64 = recip_estimate(f64, s);
9023 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
9026 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
9028 float_status *fpst = fpstp;
9029 float64 f64;
9031 if ((a & 0xc0000000) == 0) {
9032 return 0xffffffff;
9035 if (a & 0x80000000) {
9036 f64 = make_float64((0x3feULL << 52)
9037 | ((uint64_t)(a & 0x7fffffff) << 21));
9038 } else { /* bits 31-30 == '01' */
9039 f64 = make_float64((0x3fdULL << 52)
9040 | ((uint64_t)(a & 0x3fffffff) << 22));
9043 f64 = recip_sqrt_estimate(f64, fpst);
9045 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
9048 /* VFPv4 fused multiply-accumulate */
9049 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
9051 float_status *fpst = fpstp;
9052 return float32_muladd(a, b, c, 0, fpst);
9055 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
9057 float_status *fpst = fpstp;
9058 return float64_muladd(a, b, c, 0, fpst);
9061 /* ARMv8 round to integral */
9062 float32 HELPER(rints_exact)(float32 x, void *fp_status)
9064 return float32_round_to_int(x, fp_status);
9067 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
9069 return float64_round_to_int(x, fp_status);
9072 float32 HELPER(rints)(float32 x, void *fp_status)
9074 int old_flags = get_float_exception_flags(fp_status), new_flags;
9075 float32 ret;
9077 ret = float32_round_to_int(x, fp_status);
9079 /* Suppress any inexact exceptions the conversion produced */
9080 if (!(old_flags & float_flag_inexact)) {
9081 new_flags = get_float_exception_flags(fp_status);
9082 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
9085 return ret;
9088 float64 HELPER(rintd)(float64 x, void *fp_status)
9090 int old_flags = get_float_exception_flags(fp_status), new_flags;
9091 float64 ret;
9093 ret = float64_round_to_int(x, fp_status);
9095 new_flags = get_float_exception_flags(fp_status);
9097 /* Suppress any inexact exceptions the conversion produced */
9098 if (!(old_flags & float_flag_inexact)) {
9099 new_flags = get_float_exception_flags(fp_status);
9100 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
9103 return ret;
9106 /* Convert ARM rounding mode to softfloat */
9107 int arm_rmode_to_sf(int rmode)
9109 switch (rmode) {
9110 case FPROUNDING_TIEAWAY:
9111 rmode = float_round_ties_away;
9112 break;
9113 case FPROUNDING_ODD:
9114 /* FIXME: add support for TIEAWAY and ODD */
9115 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
9116 rmode);
9117 case FPROUNDING_TIEEVEN:
9118 default:
9119 rmode = float_round_nearest_even;
9120 break;
9121 case FPROUNDING_POSINF:
9122 rmode = float_round_up;
9123 break;
9124 case FPROUNDING_NEGINF:
9125 rmode = float_round_down;
9126 break;
9127 case FPROUNDING_ZERO:
9128 rmode = float_round_to_zero;
9129 break;
9131 return rmode;
9134 /* CRC helpers.
9135 * The upper bytes of val (above the number specified by 'bytes') must have
9136 * been zeroed out by the caller.
9138 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
9140 uint8_t buf[4];
9142 stl_le_p(buf, val);
9144 /* zlib crc32 converts the accumulator and output to one's complement. */
9145 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
9148 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
9150 uint8_t buf[4];
9152 stl_le_p(buf, val);
9154 /* Linux crc32c converts the output to one's complement. */
9155 return crc32c(acc, buf, bytes) ^ 0xffffffff;