Revert "gdbstub: Do not kill target in system emulation mode"
[qemu/qmp-unstable.git] / target-arm / helper.c
blob5d0f01182aae7a34be492861d2c64f67ffac56c6
1 #include "cpu.h"
2 #include "internals.h"
3 #include "exec/gdbstub.h"
4 #include "exec/helper-proto.h"
5 #include "qemu/host-utils.h"
6 #include "sysemu/arch_init.h"
7 #include "sysemu/sysemu.h"
8 #include "qemu/bitops.h"
9 #include "qemu/crc32c.h"
10 #include "exec/cpu_ldst.h"
11 #include "arm_ldst.h"
12 #include <zlib.h> /* For crc32 */
14 #ifndef CONFIG_USER_ONLY
15 static inline int get_phys_addr(CPUARMState *env, target_ulong address,
16 int access_type, ARMMMUIdx mmu_idx,
17 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
18 target_ulong *page_size);
20 /* Definitions for the PMCCNTR and PMCR registers */
21 #define PMCRD 0x8
22 #define PMCRC 0x4
23 #define PMCRE 0x1
24 #endif
26 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
28 int nregs;
30 /* VFP data registers are always little-endian. */
31 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
32 if (reg < nregs) {
33 stfq_le_p(buf, env->vfp.regs[reg]);
34 return 8;
36 if (arm_feature(env, ARM_FEATURE_NEON)) {
37 /* Aliases for Q regs. */
38 nregs += 16;
39 if (reg < nregs) {
40 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
41 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
42 return 16;
45 switch (reg - nregs) {
46 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
47 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
48 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
50 return 0;
53 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
55 int nregs;
57 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
58 if (reg < nregs) {
59 env->vfp.regs[reg] = ldfq_le_p(buf);
60 return 8;
62 if (arm_feature(env, ARM_FEATURE_NEON)) {
63 nregs += 16;
64 if (reg < nregs) {
65 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
66 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
67 return 16;
70 switch (reg - nregs) {
71 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
72 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
73 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
75 return 0;
78 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
80 switch (reg) {
81 case 0 ... 31:
82 /* 128 bit FP register */
83 stfq_le_p(buf, env->vfp.regs[reg * 2]);
84 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
85 return 16;
86 case 32:
87 /* FPSR */
88 stl_p(buf, vfp_get_fpsr(env));
89 return 4;
90 case 33:
91 /* FPCR */
92 stl_p(buf, vfp_get_fpcr(env));
93 return 4;
94 default:
95 return 0;
99 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
101 switch (reg) {
102 case 0 ... 31:
103 /* 128 bit FP register */
104 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
105 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
106 return 16;
107 case 32:
108 /* FPSR */
109 vfp_set_fpsr(env, ldl_p(buf));
110 return 4;
111 case 33:
112 /* FPCR */
113 vfp_set_fpcr(env, ldl_p(buf));
114 return 4;
115 default:
116 return 0;
120 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
122 assert(ri->fieldoffset);
123 if (cpreg_field_is_64bit(ri)) {
124 return CPREG_FIELD64(env, ri);
125 } else {
126 return CPREG_FIELD32(env, ri);
130 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
131 uint64_t value)
133 assert(ri->fieldoffset);
134 if (cpreg_field_is_64bit(ri)) {
135 CPREG_FIELD64(env, ri) = value;
136 } else {
137 CPREG_FIELD32(env, ri) = value;
141 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
143 return (char *)env + ri->fieldoffset;
146 static uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
148 /* Raw read of a coprocessor register (as needed for migration, etc). */
149 if (ri->type & ARM_CP_CONST) {
150 return ri->resetvalue;
151 } else if (ri->raw_readfn) {
152 return ri->raw_readfn(env, ri);
153 } else if (ri->readfn) {
154 return ri->readfn(env, ri);
155 } else {
156 return raw_read(env, ri);
160 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
161 uint64_t v)
163 /* Raw write of a coprocessor register (as needed for migration, etc).
164 * Note that constant registers are treated as write-ignored; the
165 * caller should check for success by whether a readback gives the
166 * value written.
168 if (ri->type & ARM_CP_CONST) {
169 return;
170 } else if (ri->raw_writefn) {
171 ri->raw_writefn(env, ri, v);
172 } else if (ri->writefn) {
173 ri->writefn(env, ri, v);
174 } else {
175 raw_write(env, ri, v);
179 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
181 /* Return true if the regdef would cause an assertion if you called
182 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
183 * program bug for it not to have the NO_RAW flag).
184 * NB that returning false here doesn't necessarily mean that calling
185 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
186 * read/write access functions which are safe for raw use" from "has
187 * read/write access functions which have side effects but has forgotten
188 * to provide raw access functions".
189 * The tests here line up with the conditions in read/write_raw_cp_reg()
190 * and assertions in raw_read()/raw_write().
192 if ((ri->type & ARM_CP_CONST) ||
193 ri->fieldoffset ||
194 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
195 return false;
197 return true;
200 bool write_cpustate_to_list(ARMCPU *cpu)
202 /* Write the coprocessor state from cpu->env to the (index,value) list. */
203 int i;
204 bool ok = true;
206 for (i = 0; i < cpu->cpreg_array_len; i++) {
207 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
208 const ARMCPRegInfo *ri;
210 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
211 if (!ri) {
212 ok = false;
213 continue;
215 if (ri->type & ARM_CP_NO_RAW) {
216 continue;
218 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
220 return ok;
223 bool write_list_to_cpustate(ARMCPU *cpu)
225 int i;
226 bool ok = true;
228 for (i = 0; i < cpu->cpreg_array_len; i++) {
229 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
230 uint64_t v = cpu->cpreg_values[i];
231 const ARMCPRegInfo *ri;
233 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
234 if (!ri) {
235 ok = false;
236 continue;
238 if (ri->type & ARM_CP_NO_RAW) {
239 continue;
241 /* Write value and confirm it reads back as written
242 * (to catch read-only registers and partially read-only
243 * registers where the incoming migration value doesn't match)
245 write_raw_cp_reg(&cpu->env, ri, v);
246 if (read_raw_cp_reg(&cpu->env, ri) != v) {
247 ok = false;
250 return ok;
253 static void add_cpreg_to_list(gpointer key, gpointer opaque)
255 ARMCPU *cpu = opaque;
256 uint64_t regidx;
257 const ARMCPRegInfo *ri;
259 regidx = *(uint32_t *)key;
260 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
262 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
263 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
264 /* The value array need not be initialized at this point */
265 cpu->cpreg_array_len++;
269 static void count_cpreg(gpointer key, gpointer opaque)
271 ARMCPU *cpu = opaque;
272 uint64_t regidx;
273 const ARMCPRegInfo *ri;
275 regidx = *(uint32_t *)key;
276 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
278 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
279 cpu->cpreg_array_len++;
283 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
285 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
286 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
288 if (aidx > bidx) {
289 return 1;
291 if (aidx < bidx) {
292 return -1;
294 return 0;
297 static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata)
299 GList **plist = udata;
301 *plist = g_list_prepend(*plist, key);
304 void init_cpreg_list(ARMCPU *cpu)
306 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
307 * Note that we require cpreg_tuples[] to be sorted by key ID.
309 GList *keys = NULL;
310 int arraylen;
312 g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys);
314 keys = g_list_sort(keys, cpreg_key_compare);
316 cpu->cpreg_array_len = 0;
318 g_list_foreach(keys, count_cpreg, cpu);
320 arraylen = cpu->cpreg_array_len;
321 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
322 cpu->cpreg_values = g_new(uint64_t, arraylen);
323 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
324 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
325 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
326 cpu->cpreg_array_len = 0;
328 g_list_foreach(keys, add_cpreg_to_list, cpu);
330 assert(cpu->cpreg_array_len == arraylen);
332 g_list_free(keys);
335 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
337 ARMCPU *cpu = arm_env_get_cpu(env);
339 raw_write(env, ri, value);
340 tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
343 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
345 ARMCPU *cpu = arm_env_get_cpu(env);
347 if (raw_read(env, ri) != value) {
348 /* Unlike real hardware the qemu TLB uses virtual addresses,
349 * not modified virtual addresses, so this causes a TLB flush.
351 tlb_flush(CPU(cpu), 1);
352 raw_write(env, ri, value);
356 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
357 uint64_t value)
359 ARMCPU *cpu = arm_env_get_cpu(env);
361 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU)
362 && !extended_addresses_enabled(env)) {
363 /* For VMSA (when not using the LPAE long descriptor page table
364 * format) this register includes the ASID, so do a TLB flush.
365 * For PMSA it is purely a process ID and no action is needed.
367 tlb_flush(CPU(cpu), 1);
369 raw_write(env, ri, value);
372 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
373 uint64_t value)
375 /* Invalidate all (TLBIALL) */
376 ARMCPU *cpu = arm_env_get_cpu(env);
378 tlb_flush(CPU(cpu), 1);
381 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
382 uint64_t value)
384 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
385 ARMCPU *cpu = arm_env_get_cpu(env);
387 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
390 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
391 uint64_t value)
393 /* Invalidate by ASID (TLBIASID) */
394 ARMCPU *cpu = arm_env_get_cpu(env);
396 tlb_flush(CPU(cpu), value == 0);
399 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
400 uint64_t value)
402 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
403 ARMCPU *cpu = arm_env_get_cpu(env);
405 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
408 /* IS variants of TLB operations must affect all cores */
409 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
410 uint64_t value)
412 CPUState *other_cs;
414 CPU_FOREACH(other_cs) {
415 tlb_flush(other_cs, 1);
419 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
420 uint64_t value)
422 CPUState *other_cs;
424 CPU_FOREACH(other_cs) {
425 tlb_flush(other_cs, value == 0);
429 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
430 uint64_t value)
432 CPUState *other_cs;
434 CPU_FOREACH(other_cs) {
435 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
439 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
440 uint64_t value)
442 CPUState *other_cs;
444 CPU_FOREACH(other_cs) {
445 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
449 static const ARMCPRegInfo cp_reginfo[] = {
450 /* Define the secure and non-secure FCSE identifier CP registers
451 * separately because there is no secure bank in V8 (no _EL3). This allows
452 * the secure register to be properly reset and migrated. There is also no
453 * v8 EL1 version of the register so the non-secure instance stands alone.
455 { .name = "FCSEIDR(NS)",
456 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
457 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
458 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
459 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
460 { .name = "FCSEIDR(S)",
461 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
462 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
463 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
464 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
465 /* Define the secure and non-secure context identifier CP registers
466 * separately because there is no secure bank in V8 (no _EL3). This allows
467 * the secure register to be properly reset and migrated. In the
468 * non-secure case, the 32-bit register will have reset and migration
469 * disabled during registration as it is handled by the 64-bit instance.
471 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
472 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
473 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
474 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
475 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
476 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
477 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
478 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
479 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
480 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
481 REGINFO_SENTINEL
484 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
485 /* NB: Some of these registers exist in v8 but with more precise
486 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
488 /* MMU Domain access control / MPU write buffer control */
489 { .name = "DACR",
490 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
491 .access = PL1_RW, .resetvalue = 0,
492 .writefn = dacr_write, .raw_writefn = raw_write,
493 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
494 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
495 /* ??? This covers not just the impdef TLB lockdown registers but also
496 * some v7VMSA registers relating to TEX remap, so it is overly broad.
498 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
499 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
500 /* Cache maintenance ops; some of this space may be overridden later. */
501 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
502 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
503 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
504 REGINFO_SENTINEL
507 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
508 /* Not all pre-v6 cores implemented this WFI, so this is slightly
509 * over-broad.
511 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
512 .access = PL1_W, .type = ARM_CP_WFI },
513 REGINFO_SENTINEL
516 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
517 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
518 * is UNPREDICTABLE; we choose to NOP as most implementations do).
520 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
521 .access = PL1_W, .type = ARM_CP_WFI },
522 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
523 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
524 * OMAPCP will override this space.
526 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
527 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
528 .resetvalue = 0 },
529 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
530 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
531 .resetvalue = 0 },
532 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
533 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
534 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
535 .resetvalue = 0 },
536 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
537 * implementing it as RAZ means the "debug architecture version" bits
538 * will read as a reserved value, which should cause Linux to not try
539 * to use the debug hardware.
541 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
542 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
543 /* MMU TLB control. Note that the wildcarding means we cover not just
544 * the unified TLB ops but also the dside/iside/inner-shareable variants.
546 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
547 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
548 .type = ARM_CP_NO_RAW },
549 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
550 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
551 .type = ARM_CP_NO_RAW },
552 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
553 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
554 .type = ARM_CP_NO_RAW },
555 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
556 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
557 .type = ARM_CP_NO_RAW },
558 REGINFO_SENTINEL
561 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
562 uint64_t value)
564 uint32_t mask = 0;
566 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
567 if (!arm_feature(env, ARM_FEATURE_V8)) {
568 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
569 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
570 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
572 if (arm_feature(env, ARM_FEATURE_VFP)) {
573 /* VFP coprocessor: cp10 & cp11 [23:20] */
574 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
576 if (!arm_feature(env, ARM_FEATURE_NEON)) {
577 /* ASEDIS [31] bit is RAO/WI */
578 value |= (1 << 31);
581 /* VFPv3 and upwards with NEON implement 32 double precision
582 * registers (D0-D31).
584 if (!arm_feature(env, ARM_FEATURE_NEON) ||
585 !arm_feature(env, ARM_FEATURE_VFP3)) {
586 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
587 value |= (1 << 30);
590 value &= mask;
592 env->cp15.cpacr_el1 = value;
595 static const ARMCPRegInfo v6_cp_reginfo[] = {
596 /* prefetch by MVA in v6, NOP in v7 */
597 { .name = "MVA_prefetch",
598 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
599 .access = PL1_W, .type = ARM_CP_NOP },
600 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
601 .access = PL0_W, .type = ARM_CP_NOP },
602 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
603 .access = PL0_W, .type = ARM_CP_NOP },
604 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
605 .access = PL0_W, .type = ARM_CP_NOP },
606 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
607 .access = PL1_RW,
608 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
609 offsetof(CPUARMState, cp15.ifar_ns) },
610 .resetvalue = 0, },
611 /* Watchpoint Fault Address Register : should actually only be present
612 * for 1136, 1176, 11MPCore.
614 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
615 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
616 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
617 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
618 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
619 .resetvalue = 0, .writefn = cpacr_write },
620 REGINFO_SENTINEL
623 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri)
625 /* Performance monitor registers user accessibility is controlled
626 * by PMUSERENR.
628 if (arm_current_el(env) == 0 && !env->cp15.c9_pmuserenr) {
629 return CP_ACCESS_TRAP;
631 return CP_ACCESS_OK;
634 #ifndef CONFIG_USER_ONLY
636 static inline bool arm_ccnt_enabled(CPUARMState *env)
638 /* This does not support checking PMCCFILTR_EL0 register */
640 if (!(env->cp15.c9_pmcr & PMCRE)) {
641 return false;
644 return true;
647 void pmccntr_sync(CPUARMState *env)
649 uint64_t temp_ticks;
651 temp_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
652 get_ticks_per_sec(), 1000000);
654 if (env->cp15.c9_pmcr & PMCRD) {
655 /* Increment once every 64 processor clock cycles */
656 temp_ticks /= 64;
659 if (arm_ccnt_enabled(env)) {
660 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
664 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
665 uint64_t value)
667 pmccntr_sync(env);
669 if (value & PMCRC) {
670 /* The counter has been reset */
671 env->cp15.c15_ccnt = 0;
674 /* only the DP, X, D and E bits are writable */
675 env->cp15.c9_pmcr &= ~0x39;
676 env->cp15.c9_pmcr |= (value & 0x39);
678 pmccntr_sync(env);
681 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
683 uint64_t total_ticks;
685 if (!arm_ccnt_enabled(env)) {
686 /* Counter is disabled, do not change value */
687 return env->cp15.c15_ccnt;
690 total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
691 get_ticks_per_sec(), 1000000);
693 if (env->cp15.c9_pmcr & PMCRD) {
694 /* Increment once every 64 processor clock cycles */
695 total_ticks /= 64;
697 return total_ticks - env->cp15.c15_ccnt;
700 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
701 uint64_t value)
703 uint64_t total_ticks;
705 if (!arm_ccnt_enabled(env)) {
706 /* Counter is disabled, set the absolute value */
707 env->cp15.c15_ccnt = value;
708 return;
711 total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
712 get_ticks_per_sec(), 1000000);
714 if (env->cp15.c9_pmcr & PMCRD) {
715 /* Increment once every 64 processor clock cycles */
716 total_ticks /= 64;
718 env->cp15.c15_ccnt = total_ticks - value;
721 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
722 uint64_t value)
724 uint64_t cur_val = pmccntr_read(env, NULL);
726 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
729 #else /* CONFIG_USER_ONLY */
731 void pmccntr_sync(CPUARMState *env)
735 #endif
737 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
738 uint64_t value)
740 pmccntr_sync(env);
741 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
742 pmccntr_sync(env);
745 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
746 uint64_t value)
748 value &= (1 << 31);
749 env->cp15.c9_pmcnten |= value;
752 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
753 uint64_t value)
755 value &= (1 << 31);
756 env->cp15.c9_pmcnten &= ~value;
759 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
760 uint64_t value)
762 env->cp15.c9_pmovsr &= ~value;
765 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
766 uint64_t value)
768 env->cp15.c9_pmxevtyper = value & 0xff;
771 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
772 uint64_t value)
774 env->cp15.c9_pmuserenr = value & 1;
777 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
778 uint64_t value)
780 /* We have no event counters so only the C bit can be changed */
781 value &= (1 << 31);
782 env->cp15.c9_pminten |= value;
785 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
786 uint64_t value)
788 value &= (1 << 31);
789 env->cp15.c9_pminten &= ~value;
792 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
793 uint64_t value)
795 /* Note that even though the AArch64 view of this register has bits
796 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
797 * architectural requirements for bits which are RES0 only in some
798 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
799 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
801 raw_write(env, ri, value & ~0x1FULL);
804 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
806 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
807 * For bits that vary between AArch32/64, code needs to check the
808 * current execution mode before directly using the feature bit.
810 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
812 if (!arm_feature(env, ARM_FEATURE_EL2)) {
813 valid_mask &= ~SCR_HCE;
815 /* On ARMv7, SMD (or SCD as it is called in v7) is only
816 * supported if EL2 exists. The bit is UNK/SBZP when
817 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
818 * when EL2 is unavailable.
819 * On ARMv8, this bit is always available.
821 if (arm_feature(env, ARM_FEATURE_V7) &&
822 !arm_feature(env, ARM_FEATURE_V8)) {
823 valid_mask &= ~SCR_SMD;
827 /* Clear all-context RES0 bits. */
828 value &= valid_mask;
829 raw_write(env, ri, value);
832 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
834 ARMCPU *cpu = arm_env_get_cpu(env);
836 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
837 * bank
839 uint32_t index = A32_BANKED_REG_GET(env, csselr,
840 ri->secure & ARM_CP_SECSTATE_S);
842 return cpu->ccsidr[index];
845 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
846 uint64_t value)
848 raw_write(env, ri, value & 0xf);
851 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
853 CPUState *cs = ENV_GET_CPU(env);
854 uint64_t ret = 0;
856 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
857 ret |= CPSR_I;
859 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
860 ret |= CPSR_F;
862 /* External aborts are not possible in QEMU so A bit is always clear */
863 return ret;
866 static const ARMCPRegInfo v7_cp_reginfo[] = {
867 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
868 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
869 .access = PL1_W, .type = ARM_CP_NOP },
870 /* Performance monitors are implementation defined in v7,
871 * but with an ARM recommended set of registers, which we
872 * follow (although we don't actually implement any counters)
874 * Performance registers fall into three categories:
875 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
876 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
877 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
878 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
879 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
881 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
882 .access = PL0_RW, .type = ARM_CP_ALIAS,
883 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
884 .writefn = pmcntenset_write,
885 .accessfn = pmreg_access,
886 .raw_writefn = raw_write },
887 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
888 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
889 .access = PL0_RW, .accessfn = pmreg_access,
890 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
891 .writefn = pmcntenset_write, .raw_writefn = raw_write },
892 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
893 .access = PL0_RW,
894 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
895 .accessfn = pmreg_access,
896 .writefn = pmcntenclr_write,
897 .type = ARM_CP_ALIAS },
898 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
899 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
900 .access = PL0_RW, .accessfn = pmreg_access,
901 .type = ARM_CP_ALIAS,
902 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
903 .writefn = pmcntenclr_write },
904 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
905 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
906 .accessfn = pmreg_access,
907 .writefn = pmovsr_write,
908 .raw_writefn = raw_write },
909 /* Unimplemented so WI. */
910 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
911 .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
912 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
913 * We choose to RAZ/WI.
915 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
916 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
917 .accessfn = pmreg_access },
918 #ifndef CONFIG_USER_ONLY
919 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
920 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
921 .readfn = pmccntr_read, .writefn = pmccntr_write32,
922 .accessfn = pmreg_access },
923 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
924 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
925 .access = PL0_RW, .accessfn = pmreg_access,
926 .type = ARM_CP_IO,
927 .readfn = pmccntr_read, .writefn = pmccntr_write, },
928 #endif
929 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
930 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
931 .writefn = pmccfiltr_write,
932 .access = PL0_RW, .accessfn = pmreg_access,
933 .type = ARM_CP_IO,
934 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
935 .resetvalue = 0, },
936 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
937 .access = PL0_RW,
938 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
939 .accessfn = pmreg_access, .writefn = pmxevtyper_write,
940 .raw_writefn = raw_write },
941 /* Unimplemented, RAZ/WI. */
942 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
943 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
944 .accessfn = pmreg_access },
945 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
946 .access = PL0_R | PL1_RW,
947 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
948 .resetvalue = 0,
949 .writefn = pmuserenr_write, .raw_writefn = raw_write },
950 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
951 .access = PL1_RW,
952 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
953 .resetvalue = 0,
954 .writefn = pmintenset_write, .raw_writefn = raw_write },
955 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
956 .access = PL1_RW, .type = ARM_CP_ALIAS,
957 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
958 .resetvalue = 0, .writefn = pmintenclr_write, },
959 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
960 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
961 .access = PL1_RW, .writefn = vbar_write,
962 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
963 offsetof(CPUARMState, cp15.vbar_ns) },
964 .resetvalue = 0 },
965 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
966 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
967 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
968 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
969 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
970 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
971 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
972 offsetof(CPUARMState, cp15.csselr_ns) } },
973 /* Auxiliary ID register: this actually has an IMPDEF value but for now
974 * just RAZ for all cores:
976 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
977 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
978 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
979 /* Auxiliary fault status registers: these also are IMPDEF, and we
980 * choose to RAZ/WI for all cores.
982 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
983 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
984 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
985 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
986 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
987 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
988 /* MAIR can just read-as-written because we don't implement caches
989 * and so don't need to care about memory attributes.
991 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
992 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
993 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
994 .resetvalue = 0 },
995 /* For non-long-descriptor page tables these are PRRR and NMRR;
996 * regardless they still act as reads-as-written for QEMU.
997 * The override is necessary because of the overly-broad TLB_LOCKDOWN
998 * definition.
1000 /* MAIR0/1 are defined separately from their 64-bit counterpart which
1001 * allows them to assign the correct fieldoffset based on the endianness
1002 * handled in the field definitions.
1004 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
1005 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
1006 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1007 offsetof(CPUARMState, cp15.mair0_ns) },
1008 .resetfn = arm_cp_reset_ignore },
1009 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
1010 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
1011 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1012 offsetof(CPUARMState, cp15.mair1_ns) },
1013 .resetfn = arm_cp_reset_ignore },
1014 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1015 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
1016 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
1017 /* 32 bit ITLB invalidates */
1018 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
1019 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1020 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
1021 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1022 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
1023 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1024 /* 32 bit DTLB invalidates */
1025 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
1026 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1027 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
1028 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1029 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
1030 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1031 /* 32 bit TLB invalidates */
1032 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
1033 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
1034 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
1035 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
1036 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
1037 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
1038 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
1039 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
1040 REGINFO_SENTINEL
1043 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1044 /* 32 bit TLB invalidates, Inner Shareable */
1045 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
1046 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
1047 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
1048 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
1049 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
1050 .type = ARM_CP_NO_RAW, .access = PL1_W,
1051 .writefn = tlbiasid_is_write },
1052 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
1053 .type = ARM_CP_NO_RAW, .access = PL1_W,
1054 .writefn = tlbimvaa_is_write },
1055 REGINFO_SENTINEL
1058 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1059 uint64_t value)
1061 value &= 1;
1062 env->teecr = value;
1065 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri)
1067 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
1068 return CP_ACCESS_TRAP;
1070 return CP_ACCESS_OK;
1073 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1074 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1075 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1076 .resetvalue = 0,
1077 .writefn = teecr_write },
1078 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1079 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
1080 .accessfn = teehbr_access, .resetvalue = 0 },
1081 REGINFO_SENTINEL
1084 static const ARMCPRegInfo v6k_cp_reginfo[] = {
1085 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1086 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1087 .access = PL0_RW,
1088 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
1089 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1090 .access = PL0_RW,
1091 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1092 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
1093 .resetfn = arm_cp_reset_ignore },
1094 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1095 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1096 .access = PL0_R|PL1_W,
1097 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1098 .resetvalue = 0},
1099 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1100 .access = PL0_R|PL1_W,
1101 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1102 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
1103 .resetfn = arm_cp_reset_ignore },
1104 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
1105 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
1106 .access = PL1_RW,
1107 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1108 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1109 .access = PL1_RW,
1110 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1111 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1112 .resetvalue = 0 },
1113 REGINFO_SENTINEL
1116 #ifndef CONFIG_USER_ONLY
1118 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri)
1120 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
1121 if (arm_current_el(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
1122 return CP_ACCESS_TRAP;
1124 return CP_ACCESS_OK;
1127 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx)
1129 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
1130 if (arm_current_el(env) == 0 &&
1131 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1132 return CP_ACCESS_TRAP;
1134 return CP_ACCESS_OK;
1137 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx)
1139 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1140 * EL0[PV]TEN is zero.
1142 if (arm_current_el(env) == 0 &&
1143 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1144 return CP_ACCESS_TRAP;
1146 return CP_ACCESS_OK;
1149 static CPAccessResult gt_pct_access(CPUARMState *env,
1150 const ARMCPRegInfo *ri)
1152 return gt_counter_access(env, GTIMER_PHYS);
1155 static CPAccessResult gt_vct_access(CPUARMState *env,
1156 const ARMCPRegInfo *ri)
1158 return gt_counter_access(env, GTIMER_VIRT);
1161 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
1163 return gt_timer_access(env, GTIMER_PHYS);
1166 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
1168 return gt_timer_access(env, GTIMER_VIRT);
1171 static uint64_t gt_get_countervalue(CPUARMState *env)
1173 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
1176 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1178 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1180 if (gt->ctl & 1) {
1181 /* Timer enabled: calculate and set current ISTATUS, irq, and
1182 * reset timer to when ISTATUS next has to change
1184 uint64_t count = gt_get_countervalue(&cpu->env);
1185 /* Note that this must be unsigned 64 bit arithmetic: */
1186 int istatus = count >= gt->cval;
1187 uint64_t nexttick;
1189 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1190 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1191 (istatus && !(gt->ctl & 2)));
1192 if (istatus) {
1193 /* Next transition is when count rolls back over to zero */
1194 nexttick = UINT64_MAX;
1195 } else {
1196 /* Next transition is when we hit cval */
1197 nexttick = gt->cval;
1199 /* Note that the desired next expiry time might be beyond the
1200 * signed-64-bit range of a QEMUTimer -- in this case we just
1201 * set the timer for as far in the future as possible. When the
1202 * timer expires we will reset the timer for any remaining period.
1204 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1205 nexttick = INT64_MAX / GTIMER_SCALE;
1207 timer_mod(cpu->gt_timer[timeridx], nexttick);
1208 } else {
1209 /* Timer disabled: ISTATUS and timer output always clear */
1210 gt->ctl &= ~4;
1211 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
1212 timer_del(cpu->gt_timer[timeridx]);
1216 static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1218 ARMCPU *cpu = arm_env_get_cpu(env);
1219 int timeridx = ri->opc1 & 1;
1221 timer_del(cpu->gt_timer[timeridx]);
1224 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1226 return gt_get_countervalue(env);
1229 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1230 uint64_t value)
1232 int timeridx = ri->opc1 & 1;
1234 env->cp15.c14_timer[timeridx].cval = value;
1235 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1238 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1240 int timeridx = ri->crm & 1;
1242 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
1243 gt_get_countervalue(env));
1246 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1247 uint64_t value)
1249 int timeridx = ri->crm & 1;
1251 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
1252 sextract64(value, 0, 32);
1253 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1256 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1257 uint64_t value)
1259 ARMCPU *cpu = arm_env_get_cpu(env);
1260 int timeridx = ri->crm & 1;
1261 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1263 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
1264 if ((oldval ^ value) & 1) {
1265 /* Enable toggled */
1266 gt_recalc_timer(cpu, timeridx);
1267 } else if ((oldval ^ value) & 2) {
1268 /* IMASK toggled: don't need to recalculate,
1269 * just set the interrupt line based on ISTATUS
1271 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1272 (oldval & 4) && !(value & 2));
1276 void arm_gt_ptimer_cb(void *opaque)
1278 ARMCPU *cpu = opaque;
1280 gt_recalc_timer(cpu, GTIMER_PHYS);
1283 void arm_gt_vtimer_cb(void *opaque)
1285 ARMCPU *cpu = opaque;
1287 gt_recalc_timer(cpu, GTIMER_VIRT);
1290 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1291 /* Note that CNTFRQ is purely reads-as-written for the benefit
1292 * of software; writing it doesn't actually change the timer frequency.
1293 * Our reset value matches the fixed frequency we implement the timer at.
1295 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
1296 .type = ARM_CP_ALIAS,
1297 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1298 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
1299 .resetfn = arm_cp_reset_ignore,
1301 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1302 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1303 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1304 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1305 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
1307 /* overall control: mostly access permissions */
1308 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1309 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
1310 .access = PL1_RW,
1311 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1312 .resetvalue = 0,
1314 /* per-timer control */
1315 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1316 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1317 .accessfn = gt_ptimer_access,
1318 .fieldoffset = offsetoflow32(CPUARMState,
1319 cp15.c14_timer[GTIMER_PHYS].ctl),
1320 .resetfn = arm_cp_reset_ignore,
1321 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1323 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1324 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
1325 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1326 .accessfn = gt_ptimer_access,
1327 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1328 .resetvalue = 0,
1329 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1331 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
1332 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1333 .accessfn = gt_vtimer_access,
1334 .fieldoffset = offsetoflow32(CPUARMState,
1335 cp15.c14_timer[GTIMER_VIRT].ctl),
1336 .resetfn = arm_cp_reset_ignore,
1337 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1339 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1340 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
1341 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1342 .accessfn = gt_vtimer_access,
1343 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1344 .resetvalue = 0,
1345 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1347 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1348 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1349 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1350 .accessfn = gt_ptimer_access,
1351 .readfn = gt_tval_read, .writefn = gt_tval_write,
1353 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1354 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
1355 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1356 .accessfn = gt_ptimer_access,
1357 .readfn = gt_tval_read, .writefn = gt_tval_write,
1359 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
1360 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1361 .accessfn = gt_vtimer_access,
1362 .readfn = gt_tval_read, .writefn = gt_tval_write,
1364 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1365 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
1366 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1367 .accessfn = gt_vtimer_access,
1368 .readfn = gt_tval_read, .writefn = gt_tval_write,
1370 /* The counter itself */
1371 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
1372 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
1373 .accessfn = gt_pct_access,
1374 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1376 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1377 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
1378 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1379 .accessfn = gt_pct_access,
1380 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
1382 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
1383 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
1384 .accessfn = gt_vct_access,
1385 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1387 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1388 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
1389 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1390 .accessfn = gt_vct_access,
1391 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
1393 /* Comparison value, indicating when the timer goes off */
1394 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
1395 .access = PL1_RW | PL0_R,
1396 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1397 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1398 .accessfn = gt_ptimer_access, .resetfn = arm_cp_reset_ignore,
1399 .writefn = gt_cval_write, .raw_writefn = raw_write,
1401 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1402 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1403 .access = PL1_RW | PL0_R,
1404 .type = ARM_CP_IO,
1405 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1406 .resetvalue = 0, .accessfn = gt_ptimer_access,
1407 .writefn = gt_cval_write, .raw_writefn = raw_write,
1409 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1410 .access = PL1_RW | PL0_R,
1411 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1412 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1413 .accessfn = gt_vtimer_access, .resetfn = arm_cp_reset_ignore,
1414 .writefn = gt_cval_write, .raw_writefn = raw_write,
1416 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1417 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1418 .access = PL1_RW | PL0_R,
1419 .type = ARM_CP_IO,
1420 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1421 .resetvalue = 0, .accessfn = gt_vtimer_access,
1422 .writefn = gt_cval_write, .raw_writefn = raw_write,
1424 REGINFO_SENTINEL
1427 #else
1428 /* In user-mode none of the generic timer registers are accessible,
1429 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
1430 * so instead just don't register any of them.
1432 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1433 REGINFO_SENTINEL
1436 #endif
1438 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1440 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1441 raw_write(env, ri, value);
1442 } else if (arm_feature(env, ARM_FEATURE_V7)) {
1443 raw_write(env, ri, value & 0xfffff6ff);
1444 } else {
1445 raw_write(env, ri, value & 0xfffff1ff);
1449 #ifndef CONFIG_USER_ONLY
1450 /* get_phys_addr() isn't present for user-mode-only targets */
1452 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri)
1454 if (ri->opc2 & 4) {
1455 /* Other states are only available with TrustZone; in
1456 * a non-TZ implementation these registers don't exist
1457 * at all, which is an Uncategorized trap. This underdecoding
1458 * is safe because the reginfo is NO_RAW.
1460 return CP_ACCESS_TRAP_UNCATEGORIZED;
1462 return CP_ACCESS_OK;
1465 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
1466 int access_type, ARMMMUIdx mmu_idx)
1468 hwaddr phys_addr;
1469 target_ulong page_size;
1470 int prot;
1471 int ret;
1472 uint64_t par64;
1473 MemTxAttrs attrs = {};
1475 ret = get_phys_addr(env, value, access_type, mmu_idx,
1476 &phys_addr, &attrs, &prot, &page_size);
1477 if (extended_addresses_enabled(env)) {
1478 /* ret is a DFSR/IFSR value for the long descriptor
1479 * translation table format, but with WnR always clear.
1480 * Convert it to a 64-bit PAR.
1482 par64 = (1 << 11); /* LPAE bit always set */
1483 if (ret == 0) {
1484 par64 |= phys_addr & ~0xfffULL;
1485 if (!attrs.secure) {
1486 par64 |= (1 << 9); /* NS */
1488 /* We don't set the ATTR or SH fields in the PAR. */
1489 } else {
1490 par64 |= 1; /* F */
1491 par64 |= (ret & 0x3f) << 1; /* FS */
1492 /* Note that S2WLK and FSTAGE are always zero, because we don't
1493 * implement virtualization and therefore there can't be a stage 2
1494 * fault.
1497 } else {
1498 /* ret is a DFSR/IFSR value for the short descriptor
1499 * translation table format (with WnR always clear).
1500 * Convert it to a 32-bit PAR.
1502 if (ret == 0) {
1503 /* We do not set any attribute bits in the PAR */
1504 if (page_size == (1 << 24)
1505 && arm_feature(env, ARM_FEATURE_V7)) {
1506 par64 = (phys_addr & 0xff000000) | (1 << 1);
1507 } else {
1508 par64 = phys_addr & 0xfffff000;
1510 if (!attrs.secure) {
1511 par64 |= (1 << 9); /* NS */
1513 } else {
1514 par64 = ((ret & (1 << 10)) >> 5) | ((ret & (1 << 12)) >> 6) |
1515 ((ret & 0xf) << 1) | 1;
1518 return par64;
1521 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1523 int access_type = ri->opc2 & 1;
1524 uint64_t par64;
1525 ARMMMUIdx mmu_idx;
1526 int el = arm_current_el(env);
1527 bool secure = arm_is_secure_below_el3(env);
1529 switch (ri->opc2 & 6) {
1530 case 0:
1531 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
1532 switch (el) {
1533 case 3:
1534 mmu_idx = ARMMMUIdx_S1E3;
1535 break;
1536 case 2:
1537 mmu_idx = ARMMMUIdx_S1NSE1;
1538 break;
1539 case 1:
1540 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
1541 break;
1542 default:
1543 g_assert_not_reached();
1545 break;
1546 case 2:
1547 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
1548 switch (el) {
1549 case 3:
1550 mmu_idx = ARMMMUIdx_S1SE0;
1551 break;
1552 case 2:
1553 mmu_idx = ARMMMUIdx_S1NSE0;
1554 break;
1555 case 1:
1556 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
1557 break;
1558 default:
1559 g_assert_not_reached();
1561 break;
1562 case 4:
1563 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
1564 mmu_idx = ARMMMUIdx_S12NSE1;
1565 break;
1566 case 6:
1567 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
1568 mmu_idx = ARMMMUIdx_S12NSE0;
1569 break;
1570 default:
1571 g_assert_not_reached();
1574 par64 = do_ats_write(env, value, access_type, mmu_idx);
1576 A32_BANKED_CURRENT_REG_SET(env, par, par64);
1579 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
1580 uint64_t value)
1582 int access_type = ri->opc2 & 1;
1583 ARMMMUIdx mmu_idx;
1584 int secure = arm_is_secure_below_el3(env);
1586 switch (ri->opc2 & 6) {
1587 case 0:
1588 switch (ri->opc1) {
1589 case 0: /* AT S1E1R, AT S1E1W */
1590 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
1591 break;
1592 case 4: /* AT S1E2R, AT S1E2W */
1593 mmu_idx = ARMMMUIdx_S1E2;
1594 break;
1595 case 6: /* AT S1E3R, AT S1E3W */
1596 mmu_idx = ARMMMUIdx_S1E3;
1597 break;
1598 default:
1599 g_assert_not_reached();
1601 break;
1602 case 2: /* AT S1E0R, AT S1E0W */
1603 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
1604 break;
1605 case 4: /* AT S12E1R, AT S12E1W */
1606 mmu_idx = ARMMMUIdx_S12NSE1;
1607 break;
1608 case 6: /* AT S12E0R, AT S12E0W */
1609 mmu_idx = ARMMMUIdx_S12NSE0;
1610 break;
1611 default:
1612 g_assert_not_reached();
1615 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
1617 #endif
1619 static const ARMCPRegInfo vapa_cp_reginfo[] = {
1620 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1621 .access = PL1_RW, .resetvalue = 0,
1622 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
1623 offsetoflow32(CPUARMState, cp15.par_ns) },
1624 .writefn = par_write },
1625 #ifndef CONFIG_USER_ONLY
1626 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
1627 .access = PL1_W, .accessfn = ats_access,
1628 .writefn = ats_write, .type = ARM_CP_NO_RAW },
1629 #endif
1630 REGINFO_SENTINEL
1633 /* Return basic MPU access permission bits. */
1634 static uint32_t simple_mpu_ap_bits(uint32_t val)
1636 uint32_t ret;
1637 uint32_t mask;
1638 int i;
1639 ret = 0;
1640 mask = 3;
1641 for (i = 0; i < 16; i += 2) {
1642 ret |= (val >> i) & mask;
1643 mask <<= 2;
1645 return ret;
1648 /* Pad basic MPU access permission bits to extended format. */
1649 static uint32_t extended_mpu_ap_bits(uint32_t val)
1651 uint32_t ret;
1652 uint32_t mask;
1653 int i;
1654 ret = 0;
1655 mask = 3;
1656 for (i = 0; i < 16; i += 2) {
1657 ret |= (val & mask) << i;
1658 mask <<= 2;
1660 return ret;
1663 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1664 uint64_t value)
1666 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
1669 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1671 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
1674 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1675 uint64_t value)
1677 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
1680 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1682 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
1685 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1686 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1687 .access = PL1_RW, .type = ARM_CP_ALIAS,
1688 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
1689 .resetvalue = 0,
1690 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
1691 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1692 .access = PL1_RW, .type = ARM_CP_ALIAS,
1693 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
1694 .resetvalue = 0,
1695 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
1696 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
1697 .access = PL1_RW,
1698 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
1699 .resetvalue = 0, },
1700 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
1701 .access = PL1_RW,
1702 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
1703 .resetvalue = 0, },
1704 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1705 .access = PL1_RW,
1706 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
1707 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1708 .access = PL1_RW,
1709 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
1710 /* Protection region base and size registers */
1711 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
1712 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1713 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
1714 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
1715 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1716 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
1717 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
1718 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1719 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
1720 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
1721 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1722 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
1723 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
1724 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1725 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
1726 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
1727 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1728 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
1729 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
1730 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1731 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
1732 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
1733 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1734 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
1735 REGINFO_SENTINEL
1738 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1739 uint64_t value)
1741 TCR *tcr = raw_ptr(env, ri);
1742 int maskshift = extract32(value, 0, 3);
1744 if (!arm_feature(env, ARM_FEATURE_V8)) {
1745 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
1746 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
1747 * using Long-desciptor translation table format */
1748 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
1749 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
1750 /* In an implementation that includes the Security Extensions
1751 * TTBCR has additional fields PD0 [4] and PD1 [5] for
1752 * Short-descriptor translation table format.
1754 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
1755 } else {
1756 value &= TTBCR_N;
1760 /* Update the masks corresponding to the the TCR bank being written
1761 * Note that we always calculate mask and base_mask, but
1762 * they are only used for short-descriptor tables (ie if EAE is 0);
1763 * for long-descriptor tables the TCR fields are used differently
1764 * and the mask and base_mask values are meaningless.
1766 tcr->raw_tcr = value;
1767 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
1768 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
1771 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1772 uint64_t value)
1774 ARMCPU *cpu = arm_env_get_cpu(env);
1776 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1777 /* With LPAE the TTBCR could result in a change of ASID
1778 * via the TTBCR.A1 bit, so do a TLB flush.
1780 tlb_flush(CPU(cpu), 1);
1782 vmsa_ttbcr_raw_write(env, ri, value);
1785 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1787 TCR *tcr = raw_ptr(env, ri);
1789 /* Reset both the TCR as well as the masks corresponding to the bank of
1790 * the TCR being reset.
1792 tcr->raw_tcr = 0;
1793 tcr->mask = 0;
1794 tcr->base_mask = 0xffffc000u;
1797 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
1798 uint64_t value)
1800 ARMCPU *cpu = arm_env_get_cpu(env);
1801 TCR *tcr = raw_ptr(env, ri);
1803 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
1804 tlb_flush(CPU(cpu), 1);
1805 tcr->raw_tcr = value;
1808 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1809 uint64_t value)
1811 /* 64 bit accesses to the TTBRs can change the ASID and so we
1812 * must flush the TLB.
1814 if (cpreg_field_is_64bit(ri)) {
1815 ARMCPU *cpu = arm_env_get_cpu(env);
1817 tlb_flush(CPU(cpu), 1);
1819 raw_write(env, ri, value);
1822 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
1823 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1824 .access = PL1_RW, .type = ARM_CP_ALIAS,
1825 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
1826 offsetoflow32(CPUARMState, cp15.dfsr_ns) },
1827 .resetfn = arm_cp_reset_ignore, },
1828 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1829 .access = PL1_RW, .resetvalue = 0,
1830 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
1831 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
1832 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
1833 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
1834 .access = PL1_RW,
1835 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
1836 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
1837 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
1838 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
1839 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
1840 offsetof(CPUARMState, cp15.ttbr0_ns) } },
1841 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
1842 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
1843 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
1844 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
1845 offsetof(CPUARMState, cp15.ttbr1_ns) } },
1846 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
1847 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1848 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
1849 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
1850 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
1851 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1852 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
1853 .resetfn = arm_cp_reset_ignore, .raw_writefn = vmsa_ttbcr_raw_write,
1854 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
1855 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
1856 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
1857 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
1858 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
1859 .resetvalue = 0, },
1860 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
1861 .access = PL1_RW, .resetvalue = 0,
1862 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
1863 offsetof(CPUARMState, cp15.dfar_ns) } },
1864 REGINFO_SENTINEL
1867 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
1868 uint64_t value)
1870 env->cp15.c15_ticonfig = value & 0xe7;
1871 /* The OS_TYPE bit in this register changes the reported CPUID! */
1872 env->cp15.c0_cpuid = (value & (1 << 5)) ?
1873 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1876 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1877 uint64_t value)
1879 env->cp15.c15_threadid = value & 0xffff;
1882 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
1883 uint64_t value)
1885 /* Wait-for-interrupt (deprecated) */
1886 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1889 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
1890 uint64_t value)
1892 /* On OMAP there are registers indicating the max/min index of dcache lines
1893 * containing a dirty line; cache flush operations have to reset these.
1895 env->cp15.c15_i_max = 0x000;
1896 env->cp15.c15_i_min = 0xff0;
1899 static const ARMCPRegInfo omap_cp_reginfo[] = {
1900 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
1901 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
1902 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
1903 .resetvalue = 0, },
1904 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
1905 .access = PL1_RW, .type = ARM_CP_NOP },
1906 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
1907 .access = PL1_RW,
1908 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
1909 .writefn = omap_ticonfig_write },
1910 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
1911 .access = PL1_RW,
1912 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
1913 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
1914 .access = PL1_RW, .resetvalue = 0xff0,
1915 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
1916 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
1917 .access = PL1_RW,
1918 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
1919 .writefn = omap_threadid_write },
1920 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
1921 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1922 .type = ARM_CP_NO_RAW,
1923 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
1924 /* TODO: Peripheral port remap register:
1925 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
1926 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
1927 * when MMU is off.
1929 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
1930 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
1931 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
1932 .writefn = omap_cachemaint_write },
1933 { .name = "C9", .cp = 15, .crn = 9,
1934 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
1935 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1936 REGINFO_SENTINEL
1939 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1940 uint64_t value)
1942 env->cp15.c15_cpar = value & 0x3fff;
1945 static const ARMCPRegInfo xscale_cp_reginfo[] = {
1946 { .name = "XSCALE_CPAR",
1947 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1948 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
1949 .writefn = xscale_cpar_write, },
1950 { .name = "XSCALE_AUXCR",
1951 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
1952 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
1953 .resetvalue = 0, },
1954 /* XScale specific cache-lockdown: since we have no cache we NOP these
1955 * and hope the guest does not really rely on cache behaviour.
1957 { .name = "XSCALE_LOCK_ICACHE_LINE",
1958 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
1959 .access = PL1_W, .type = ARM_CP_NOP },
1960 { .name = "XSCALE_UNLOCK_ICACHE",
1961 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
1962 .access = PL1_W, .type = ARM_CP_NOP },
1963 { .name = "XSCALE_DCACHE_LOCK",
1964 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
1965 .access = PL1_RW, .type = ARM_CP_NOP },
1966 { .name = "XSCALE_UNLOCK_DCACHE",
1967 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
1968 .access = PL1_W, .type = ARM_CP_NOP },
1969 REGINFO_SENTINEL
1972 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
1973 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1974 * implementation of this implementation-defined space.
1975 * Ideally this should eventually disappear in favour of actually
1976 * implementing the correct behaviour for all cores.
1978 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1979 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1980 .access = PL1_RW,
1981 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
1982 .resetvalue = 0 },
1983 REGINFO_SENTINEL
1986 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
1987 /* Cache status: RAZ because we have no cache so it's always clean */
1988 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
1989 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
1990 .resetvalue = 0 },
1991 REGINFO_SENTINEL
1994 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
1995 /* We never have a a block transfer operation in progress */
1996 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
1997 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
1998 .resetvalue = 0 },
1999 /* The cache ops themselves: these all NOP for QEMU */
2000 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2001 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2002 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2003 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2004 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2005 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2006 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2007 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2008 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2009 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2010 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2011 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2012 REGINFO_SENTINEL
2015 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2016 /* The cache test-and-clean instructions always return (1 << 30)
2017 * to indicate that there are no dirty cache lines.
2019 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
2020 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2021 .resetvalue = (1 << 30) },
2022 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
2023 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
2024 .resetvalue = (1 << 30) },
2025 REGINFO_SENTINEL
2028 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2029 /* Ignore ReadBuffer accesses */
2030 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2031 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
2032 .access = PL1_RW, .resetvalue = 0,
2033 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
2034 REGINFO_SENTINEL
2037 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2039 CPUState *cs = CPU(arm_env_get_cpu(env));
2040 uint32_t mpidr = cs->cpu_index;
2041 /* We don't support setting cluster ID ([8..11]) (known as Aff1
2042 * in later ARM ARM versions), or any of the higher affinity level fields,
2043 * so these bits always RAZ.
2045 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2046 mpidr |= (1U << 31);
2047 /* Cores which are uniprocessor (non-coherent)
2048 * but still implement the MP extensions set
2049 * bit 30. (For instance, A9UP.) However we do
2050 * not currently model any of those cores.
2053 return mpidr;
2056 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
2057 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2058 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
2059 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
2060 REGINFO_SENTINEL
2063 static const ARMCPRegInfo lpae_cp_reginfo[] = {
2064 /* NOP AMAIR0/1: the override is because these clash with the rather
2065 * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
2067 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2068 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
2069 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
2070 .resetvalue = 0 },
2071 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
2072 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
2073 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
2074 .resetvalue = 0 },
2075 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
2076 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2077 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2078 offsetof(CPUARMState, cp15.par_ns)} },
2079 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
2080 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2081 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2082 offsetof(CPUARMState, cp15.ttbr0_ns) },
2083 .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
2084 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
2085 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
2086 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2087 offsetof(CPUARMState, cp15.ttbr1_ns) },
2088 .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
2089 REGINFO_SENTINEL
2092 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2094 return vfp_get_fpcr(env);
2097 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2098 uint64_t value)
2100 vfp_set_fpcr(env, value);
2103 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2105 return vfp_get_fpsr(env);
2108 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2109 uint64_t value)
2111 vfp_set_fpsr(env, value);
2114 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri)
2116 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
2117 return CP_ACCESS_TRAP;
2119 return CP_ACCESS_OK;
2122 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2123 uint64_t value)
2125 env->daif = value & PSTATE_DAIF;
2128 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
2129 const ARMCPRegInfo *ri)
2131 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2132 * SCTLR_EL1.UCI is set.
2134 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
2135 return CP_ACCESS_TRAP;
2137 return CP_ACCESS_OK;
2140 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2141 * Page D4-1736 (DDI0487A.b)
2144 static void tlbi_aa64_va_write(CPUARMState *env, const ARMCPRegInfo *ri,
2145 uint64_t value)
2147 /* Invalidate by VA (AArch64 version) */
2148 ARMCPU *cpu = arm_env_get_cpu(env);
2149 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2151 tlb_flush_page(CPU(cpu), pageaddr);
2154 static void tlbi_aa64_vaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
2155 uint64_t value)
2157 /* Invalidate by VA, all ASIDs (AArch64 version) */
2158 ARMCPU *cpu = arm_env_get_cpu(env);
2159 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2161 tlb_flush_page(CPU(cpu), pageaddr);
2164 static void tlbi_aa64_asid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2165 uint64_t value)
2167 /* Invalidate by ASID (AArch64 version) */
2168 ARMCPU *cpu = arm_env_get_cpu(env);
2169 int asid = extract64(value, 48, 16);
2170 tlb_flush(CPU(cpu), asid == 0);
2173 static void tlbi_aa64_va_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2174 uint64_t value)
2176 CPUState *other_cs;
2177 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2179 CPU_FOREACH(other_cs) {
2180 tlb_flush_page(other_cs, pageaddr);
2184 static void tlbi_aa64_vaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2185 uint64_t value)
2187 CPUState *other_cs;
2188 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2190 CPU_FOREACH(other_cs) {
2191 tlb_flush_page(other_cs, pageaddr);
2195 static void tlbi_aa64_asid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2196 uint64_t value)
2198 CPUState *other_cs;
2199 int asid = extract64(value, 48, 16);
2201 CPU_FOREACH(other_cs) {
2202 tlb_flush(other_cs, asid == 0);
2206 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri)
2208 /* We don't implement EL2, so the only control on DC ZVA is the
2209 * bit in the SCTLR which can prohibit access for EL0.
2211 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
2212 return CP_ACCESS_TRAP;
2214 return CP_ACCESS_OK;
2217 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
2219 ARMCPU *cpu = arm_env_get_cpu(env);
2220 int dzp_bit = 1 << 4;
2222 /* DZP indicates whether DC ZVA access is allowed */
2223 if (aa64_zva_access(env, NULL) == CP_ACCESS_OK) {
2224 dzp_bit = 0;
2226 return cpu->dcz_blocksize | dzp_bit;
2229 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
2231 if (!(env->pstate & PSTATE_SP)) {
2232 /* Access to SP_EL0 is undefined if it's being used as
2233 * the stack pointer.
2235 return CP_ACCESS_TRAP_UNCATEGORIZED;
2237 return CP_ACCESS_OK;
2240 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
2242 return env->pstate & PSTATE_SP;
2245 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
2247 update_spsel(env, val);
2250 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2251 uint64_t value)
2253 ARMCPU *cpu = arm_env_get_cpu(env);
2255 if (raw_read(env, ri) == value) {
2256 /* Skip the TLB flush if nothing actually changed; Linux likes
2257 * to do a lot of pointless SCTLR writes.
2259 return;
2262 raw_write(env, ri, value);
2263 /* ??? Lots of these bits are not implemented. */
2264 /* This may enable/disable the MMU, so do a TLB flush. */
2265 tlb_flush(CPU(cpu), 1);
2268 static const ARMCPRegInfo v8_cp_reginfo[] = {
2269 /* Minimal set of EL0-visible registers. This will need to be expanded
2270 * significantly for system emulation of AArch64 CPUs.
2272 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
2273 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
2274 .access = PL0_RW, .type = ARM_CP_NZCV },
2275 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
2276 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
2277 .type = ARM_CP_NO_RAW,
2278 .access = PL0_RW, .accessfn = aa64_daif_access,
2279 .fieldoffset = offsetof(CPUARMState, daif),
2280 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
2281 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
2282 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
2283 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
2284 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
2285 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
2286 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
2287 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
2288 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
2289 .access = PL0_R, .type = ARM_CP_NO_RAW,
2290 .readfn = aa64_dczid_read },
2291 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
2292 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
2293 .access = PL0_W, .type = ARM_CP_DC_ZVA,
2294 #ifndef CONFIG_USER_ONLY
2295 /* Avoid overhead of an access check that always passes in user-mode */
2296 .accessfn = aa64_zva_access,
2297 #endif
2299 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
2300 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
2301 .access = PL1_R, .type = ARM_CP_CURRENTEL },
2302 /* Cache ops: all NOPs since we don't emulate caches */
2303 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
2304 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2305 .access = PL1_W, .type = ARM_CP_NOP },
2306 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
2307 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2308 .access = PL1_W, .type = ARM_CP_NOP },
2309 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
2310 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
2311 .access = PL0_W, .type = ARM_CP_NOP,
2312 .accessfn = aa64_cacheop_access },
2313 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
2314 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
2315 .access = PL1_W, .type = ARM_CP_NOP },
2316 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
2317 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
2318 .access = PL1_W, .type = ARM_CP_NOP },
2319 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
2320 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
2321 .access = PL0_W, .type = ARM_CP_NOP,
2322 .accessfn = aa64_cacheop_access },
2323 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
2324 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
2325 .access = PL1_W, .type = ARM_CP_NOP },
2326 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
2327 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
2328 .access = PL0_W, .type = ARM_CP_NOP,
2329 .accessfn = aa64_cacheop_access },
2330 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
2331 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
2332 .access = PL0_W, .type = ARM_CP_NOP,
2333 .accessfn = aa64_cacheop_access },
2334 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
2335 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
2336 .access = PL1_W, .type = ARM_CP_NOP },
2337 /* TLBI operations */
2338 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
2339 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2340 .access = PL1_W, .type = ARM_CP_NO_RAW,
2341 .writefn = tlbiall_is_write },
2342 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
2343 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2344 .access = PL1_W, .type = ARM_CP_NO_RAW,
2345 .writefn = tlbi_aa64_va_is_write },
2346 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
2347 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2348 .access = PL1_W, .type = ARM_CP_NO_RAW,
2349 .writefn = tlbi_aa64_asid_is_write },
2350 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
2351 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2352 .access = PL1_W, .type = ARM_CP_NO_RAW,
2353 .writefn = tlbi_aa64_vaa_is_write },
2354 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
2355 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
2356 .access = PL1_W, .type = ARM_CP_NO_RAW,
2357 .writefn = tlbi_aa64_va_is_write },
2358 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
2359 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
2360 .access = PL1_W, .type = ARM_CP_NO_RAW,
2361 .writefn = tlbi_aa64_vaa_is_write },
2362 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
2363 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2364 .access = PL1_W, .type = ARM_CP_NO_RAW,
2365 .writefn = tlbiall_write },
2366 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
2367 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2368 .access = PL1_W, .type = ARM_CP_NO_RAW,
2369 .writefn = tlbi_aa64_va_write },
2370 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
2371 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2372 .access = PL1_W, .type = ARM_CP_NO_RAW,
2373 .writefn = tlbi_aa64_asid_write },
2374 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
2375 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2376 .access = PL1_W, .type = ARM_CP_NO_RAW,
2377 .writefn = tlbi_aa64_vaa_write },
2378 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
2379 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
2380 .access = PL1_W, .type = ARM_CP_NO_RAW,
2381 .writefn = tlbi_aa64_va_write },
2382 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
2383 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
2384 .access = PL1_W, .type = ARM_CP_NO_RAW,
2385 .writefn = tlbi_aa64_vaa_write },
2386 #ifndef CONFIG_USER_ONLY
2387 /* 64 bit address translation operations */
2388 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
2389 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
2390 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2391 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
2392 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
2393 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2394 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
2395 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
2396 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2397 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
2398 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
2399 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
2400 #endif
2401 /* TLB invalidate last level of translation table walk */
2402 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
2403 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
2404 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
2405 .type = ARM_CP_NO_RAW, .access = PL1_W,
2406 .writefn = tlbimvaa_is_write },
2407 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
2408 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
2409 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
2410 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
2411 /* 32 bit cache operations */
2412 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2413 .type = ARM_CP_NOP, .access = PL1_W },
2414 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
2415 .type = ARM_CP_NOP, .access = PL1_W },
2416 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2417 .type = ARM_CP_NOP, .access = PL1_W },
2418 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
2419 .type = ARM_CP_NOP, .access = PL1_W },
2420 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
2421 .type = ARM_CP_NOP, .access = PL1_W },
2422 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
2423 .type = ARM_CP_NOP, .access = PL1_W },
2424 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
2425 .type = ARM_CP_NOP, .access = PL1_W },
2426 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
2427 .type = ARM_CP_NOP, .access = PL1_W },
2428 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
2429 .type = ARM_CP_NOP, .access = PL1_W },
2430 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
2431 .type = ARM_CP_NOP, .access = PL1_W },
2432 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
2433 .type = ARM_CP_NOP, .access = PL1_W },
2434 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
2435 .type = ARM_CP_NOP, .access = PL1_W },
2436 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
2437 .type = ARM_CP_NOP, .access = PL1_W },
2438 /* MMU Domain access control / MPU write buffer control */
2439 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
2440 .access = PL1_RW, .resetvalue = 0,
2441 .writefn = dacr_write, .raw_writefn = raw_write,
2442 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
2443 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
2444 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
2445 .type = ARM_CP_ALIAS,
2446 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
2447 .access = PL1_RW,
2448 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
2449 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
2450 .type = ARM_CP_ALIAS,
2451 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
2452 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[1]) },
2453 /* We rely on the access checks not allowing the guest to write to the
2454 * state field when SPSel indicates that it's being used as the stack
2455 * pointer.
2457 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
2458 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
2459 .access = PL1_RW, .accessfn = sp_el0_access,
2460 .type = ARM_CP_ALIAS,
2461 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
2462 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
2463 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
2464 .access = PL2_RW, .type = ARM_CP_ALIAS,
2465 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
2466 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
2467 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
2468 .type = ARM_CP_NO_RAW,
2469 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
2470 REGINFO_SENTINEL
2473 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
2474 static const ARMCPRegInfo v8_el3_no_el2_cp_reginfo[] = {
2475 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
2476 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
2477 .access = PL2_RW,
2478 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
2479 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
2480 .type = ARM_CP_NO_RAW,
2481 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
2482 .access = PL2_RW,
2483 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
2484 REGINFO_SENTINEL
2487 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2489 ARMCPU *cpu = arm_env_get_cpu(env);
2490 uint64_t valid_mask = HCR_MASK;
2492 if (arm_feature(env, ARM_FEATURE_EL3)) {
2493 valid_mask &= ~HCR_HCD;
2494 } else {
2495 valid_mask &= ~HCR_TSC;
2498 /* Clear RES0 bits. */
2499 value &= valid_mask;
2501 /* These bits change the MMU setup:
2502 * HCR_VM enables stage 2 translation
2503 * HCR_PTW forbids certain page-table setups
2504 * HCR_DC Disables stage1 and enables stage2 translation
2506 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
2507 tlb_flush(CPU(cpu), 1);
2509 raw_write(env, ri, value);
2512 static const ARMCPRegInfo v8_el2_cp_reginfo[] = {
2513 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
2514 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
2515 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
2516 .writefn = hcr_write },
2517 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
2518 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
2519 .access = PL2_RW, .resetvalue = 0,
2520 .writefn = dacr_write, .raw_writefn = raw_write,
2521 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
2522 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
2523 .type = ARM_CP_ALIAS,
2524 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
2525 .access = PL2_RW,
2526 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
2527 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
2528 .type = ARM_CP_ALIAS,
2529 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
2530 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
2531 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
2532 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
2533 .access = PL2_RW, .resetvalue = 0,
2534 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
2535 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
2536 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
2537 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
2538 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
2539 .type = ARM_CP_ALIAS,
2540 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
2541 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[6]) },
2542 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
2543 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
2544 .access = PL2_RW, .writefn = vbar_write,
2545 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
2546 .resetvalue = 0 },
2547 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
2548 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
2549 .access = PL3_RW, .type = ARM_CP_ALIAS,
2550 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
2551 REGINFO_SENTINEL
2554 static const ARMCPRegInfo el3_cp_reginfo[] = {
2555 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
2556 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
2557 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
2558 .resetvalue = 0, .writefn = scr_write },
2559 { .name = "SCR", .type = ARM_CP_ALIAS,
2560 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
2561 .access = PL3_RW, .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
2562 .resetfn = arm_cp_reset_ignore, .writefn = scr_write },
2563 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
2564 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
2565 .access = PL3_RW, .resetvalue = 0,
2566 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
2567 { .name = "SDER",
2568 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
2569 .access = PL3_RW, .resetvalue = 0,
2570 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
2571 /* TODO: Implement NSACR trapping of secure EL1 accesses to EL3 */
2572 { .name = "NSACR", .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
2573 .access = PL3_W | PL1_R, .resetvalue = 0,
2574 .fieldoffset = offsetof(CPUARMState, cp15.nsacr) },
2575 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
2576 .access = PL3_RW, .writefn = vbar_write, .resetvalue = 0,
2577 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
2578 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
2579 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
2580 .access = PL3_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
2581 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]) },
2582 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
2583 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
2584 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2585 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
2586 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
2587 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
2588 .access = PL3_RW, .writefn = vmsa_tcr_el1_write,
2589 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
2590 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
2591 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
2592 .type = ARM_CP_ALIAS,
2593 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
2594 .access = PL3_RW,
2595 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
2596 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
2597 .type = ARM_CP_ALIAS,
2598 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
2599 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
2600 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
2601 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
2602 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
2603 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
2604 .type = ARM_CP_ALIAS,
2605 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
2606 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[7]) },
2607 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
2608 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
2609 .access = PL3_RW, .writefn = vbar_write,
2610 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
2611 .resetvalue = 0 },
2612 REGINFO_SENTINEL
2615 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
2617 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
2618 * but the AArch32 CTR has its own reginfo struct)
2620 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
2621 return CP_ACCESS_TRAP;
2623 return CP_ACCESS_OK;
2626 static const ARMCPRegInfo debug_cp_reginfo[] = {
2627 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
2628 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
2629 * unlike DBGDRAR it is never accessible from EL0.
2630 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
2631 * accessor.
2633 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
2634 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2635 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
2636 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
2637 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2638 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2639 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2640 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
2641 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
2642 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
2643 .access = PL1_RW,
2644 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
2645 .resetvalue = 0 },
2646 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
2647 * We don't implement the configurable EL0 access.
2649 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
2650 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
2651 .type = ARM_CP_ALIAS,
2652 .access = PL1_R,
2653 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
2654 .resetfn = arm_cp_reset_ignore },
2655 /* We define a dummy WI OSLAR_EL1, because Linux writes to it. */
2656 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
2657 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
2658 .access = PL1_W, .type = ARM_CP_NOP },
2659 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
2660 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
2661 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
2662 .access = PL1_RW, .type = ARM_CP_NOP },
2663 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
2664 * implement vector catch debug events yet.
2666 { .name = "DBGVCR",
2667 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
2668 .access = PL1_RW, .type = ARM_CP_NOP },
2669 REGINFO_SENTINEL
2672 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
2673 /* 64 bit access versions of the (dummy) debug registers */
2674 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
2675 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
2676 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
2677 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
2678 REGINFO_SENTINEL
2681 void hw_watchpoint_update(ARMCPU *cpu, int n)
2683 CPUARMState *env = &cpu->env;
2684 vaddr len = 0;
2685 vaddr wvr = env->cp15.dbgwvr[n];
2686 uint64_t wcr = env->cp15.dbgwcr[n];
2687 int mask;
2688 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
2690 if (env->cpu_watchpoint[n]) {
2691 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
2692 env->cpu_watchpoint[n] = NULL;
2695 if (!extract64(wcr, 0, 1)) {
2696 /* E bit clear : watchpoint disabled */
2697 return;
2700 switch (extract64(wcr, 3, 2)) {
2701 case 0:
2702 /* LSC 00 is reserved and must behave as if the wp is disabled */
2703 return;
2704 case 1:
2705 flags |= BP_MEM_READ;
2706 break;
2707 case 2:
2708 flags |= BP_MEM_WRITE;
2709 break;
2710 case 3:
2711 flags |= BP_MEM_ACCESS;
2712 break;
2715 /* Attempts to use both MASK and BAS fields simultaneously are
2716 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
2717 * thus generating a watchpoint for every byte in the masked region.
2719 mask = extract64(wcr, 24, 4);
2720 if (mask == 1 || mask == 2) {
2721 /* Reserved values of MASK; we must act as if the mask value was
2722 * some non-reserved value, or as if the watchpoint were disabled.
2723 * We choose the latter.
2725 return;
2726 } else if (mask) {
2727 /* Watchpoint covers an aligned area up to 2GB in size */
2728 len = 1ULL << mask;
2729 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
2730 * whether the watchpoint fires when the unmasked bits match; we opt
2731 * to generate the exceptions.
2733 wvr &= ~(len - 1);
2734 } else {
2735 /* Watchpoint covers bytes defined by the byte address select bits */
2736 int bas = extract64(wcr, 5, 8);
2737 int basstart;
2739 if (bas == 0) {
2740 /* This must act as if the watchpoint is disabled */
2741 return;
2744 if (extract64(wvr, 2, 1)) {
2745 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
2746 * ignored, and BAS[3:0] define which bytes to watch.
2748 bas &= 0xf;
2750 /* The BAS bits are supposed to be programmed to indicate a contiguous
2751 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
2752 * we fire for each byte in the word/doubleword addressed by the WVR.
2753 * We choose to ignore any non-zero bits after the first range of 1s.
2755 basstart = ctz32(bas);
2756 len = cto32(bas >> basstart);
2757 wvr += basstart;
2760 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
2761 &env->cpu_watchpoint[n]);
2764 void hw_watchpoint_update_all(ARMCPU *cpu)
2766 int i;
2767 CPUARMState *env = &cpu->env;
2769 /* Completely clear out existing QEMU watchpoints and our array, to
2770 * avoid possible stale entries following migration load.
2772 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
2773 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
2775 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
2776 hw_watchpoint_update(cpu, i);
2780 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2781 uint64_t value)
2783 ARMCPU *cpu = arm_env_get_cpu(env);
2784 int i = ri->crm;
2786 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
2787 * register reads and behaves as if values written are sign extended.
2788 * Bits [1:0] are RES0.
2790 value = sextract64(value, 0, 49) & ~3ULL;
2792 raw_write(env, ri, value);
2793 hw_watchpoint_update(cpu, i);
2796 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2797 uint64_t value)
2799 ARMCPU *cpu = arm_env_get_cpu(env);
2800 int i = ri->crm;
2802 raw_write(env, ri, value);
2803 hw_watchpoint_update(cpu, i);
2806 void hw_breakpoint_update(ARMCPU *cpu, int n)
2808 CPUARMState *env = &cpu->env;
2809 uint64_t bvr = env->cp15.dbgbvr[n];
2810 uint64_t bcr = env->cp15.dbgbcr[n];
2811 vaddr addr;
2812 int bt;
2813 int flags = BP_CPU;
2815 if (env->cpu_breakpoint[n]) {
2816 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
2817 env->cpu_breakpoint[n] = NULL;
2820 if (!extract64(bcr, 0, 1)) {
2821 /* E bit clear : watchpoint disabled */
2822 return;
2825 bt = extract64(bcr, 20, 4);
2827 switch (bt) {
2828 case 4: /* unlinked address mismatch (reserved if AArch64) */
2829 case 5: /* linked address mismatch (reserved if AArch64) */
2830 qemu_log_mask(LOG_UNIMP,
2831 "arm: address mismatch breakpoint types not implemented");
2832 return;
2833 case 0: /* unlinked address match */
2834 case 1: /* linked address match */
2836 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
2837 * we behave as if the register was sign extended. Bits [1:0] are
2838 * RES0. The BAS field is used to allow setting breakpoints on 16
2839 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
2840 * a bp will fire if the addresses covered by the bp and the addresses
2841 * covered by the insn overlap but the insn doesn't start at the
2842 * start of the bp address range. We choose to require the insn and
2843 * the bp to have the same address. The constraints on writing to
2844 * BAS enforced in dbgbcr_write mean we have only four cases:
2845 * 0b0000 => no breakpoint
2846 * 0b0011 => breakpoint on addr
2847 * 0b1100 => breakpoint on addr + 2
2848 * 0b1111 => breakpoint on addr
2849 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
2851 int bas = extract64(bcr, 5, 4);
2852 addr = sextract64(bvr, 0, 49) & ~3ULL;
2853 if (bas == 0) {
2854 return;
2856 if (bas == 0xc) {
2857 addr += 2;
2859 break;
2861 case 2: /* unlinked context ID match */
2862 case 8: /* unlinked VMID match (reserved if no EL2) */
2863 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
2864 qemu_log_mask(LOG_UNIMP,
2865 "arm: unlinked context breakpoint types not implemented");
2866 return;
2867 case 9: /* linked VMID match (reserved if no EL2) */
2868 case 11: /* linked context ID and VMID match (reserved if no EL2) */
2869 case 3: /* linked context ID match */
2870 default:
2871 /* We must generate no events for Linked context matches (unless
2872 * they are linked to by some other bp/wp, which is handled in
2873 * updates for the linking bp/wp). We choose to also generate no events
2874 * for reserved values.
2876 return;
2879 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
2882 void hw_breakpoint_update_all(ARMCPU *cpu)
2884 int i;
2885 CPUARMState *env = &cpu->env;
2887 /* Completely clear out existing QEMU breakpoints and our array, to
2888 * avoid possible stale entries following migration load.
2890 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
2891 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
2893 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
2894 hw_breakpoint_update(cpu, i);
2898 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2899 uint64_t value)
2901 ARMCPU *cpu = arm_env_get_cpu(env);
2902 int i = ri->crm;
2904 raw_write(env, ri, value);
2905 hw_breakpoint_update(cpu, i);
2908 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2909 uint64_t value)
2911 ARMCPU *cpu = arm_env_get_cpu(env);
2912 int i = ri->crm;
2914 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
2915 * copy of BAS[0].
2917 value = deposit64(value, 6, 1, extract64(value, 5, 1));
2918 value = deposit64(value, 8, 1, extract64(value, 7, 1));
2920 raw_write(env, ri, value);
2921 hw_breakpoint_update(cpu, i);
2924 static void define_debug_regs(ARMCPU *cpu)
2926 /* Define v7 and v8 architectural debug registers.
2927 * These are just dummy implementations for now.
2929 int i;
2930 int wrps, brps, ctx_cmps;
2931 ARMCPRegInfo dbgdidr = {
2932 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
2933 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
2936 /* Note that all these register fields hold "number of Xs minus 1". */
2937 brps = extract32(cpu->dbgdidr, 24, 4);
2938 wrps = extract32(cpu->dbgdidr, 28, 4);
2939 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
2941 assert(ctx_cmps <= brps);
2943 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
2944 * of the debug registers such as number of breakpoints;
2945 * check that if they both exist then they agree.
2947 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
2948 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
2949 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
2950 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
2953 define_one_arm_cp_reg(cpu, &dbgdidr);
2954 define_arm_cp_regs(cpu, debug_cp_reginfo);
2956 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
2957 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
2960 for (i = 0; i < brps + 1; i++) {
2961 ARMCPRegInfo dbgregs[] = {
2962 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
2963 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
2964 .access = PL1_RW,
2965 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
2966 .writefn = dbgbvr_write, .raw_writefn = raw_write
2968 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
2969 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
2970 .access = PL1_RW,
2971 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
2972 .writefn = dbgbcr_write, .raw_writefn = raw_write
2974 REGINFO_SENTINEL
2976 define_arm_cp_regs(cpu, dbgregs);
2979 for (i = 0; i < wrps + 1; i++) {
2980 ARMCPRegInfo dbgregs[] = {
2981 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
2982 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
2983 .access = PL1_RW,
2984 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
2985 .writefn = dbgwvr_write, .raw_writefn = raw_write
2987 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
2988 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
2989 .access = PL1_RW,
2990 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
2991 .writefn = dbgwcr_write, .raw_writefn = raw_write
2993 REGINFO_SENTINEL
2995 define_arm_cp_regs(cpu, dbgregs);
2999 void register_cp_regs_for_features(ARMCPU *cpu)
3001 /* Register all the coprocessor registers based on feature bits */
3002 CPUARMState *env = &cpu->env;
3003 if (arm_feature(env, ARM_FEATURE_M)) {
3004 /* M profile has no coprocessor registers */
3005 return;
3008 define_arm_cp_regs(cpu, cp_reginfo);
3009 if (!arm_feature(env, ARM_FEATURE_V8)) {
3010 /* Must go early as it is full of wildcards that may be
3011 * overridden by later definitions.
3013 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
3016 if (arm_feature(env, ARM_FEATURE_V6)) {
3017 /* The ID registers all have impdef reset values */
3018 ARMCPRegInfo v6_idregs[] = {
3019 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
3020 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
3021 .access = PL1_R, .type = ARM_CP_CONST,
3022 .resetvalue = cpu->id_pfr0 },
3023 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
3024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
3025 .access = PL1_R, .type = ARM_CP_CONST,
3026 .resetvalue = cpu->id_pfr1 },
3027 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
3028 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
3029 .access = PL1_R, .type = ARM_CP_CONST,
3030 .resetvalue = cpu->id_dfr0 },
3031 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
3032 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
3033 .access = PL1_R, .type = ARM_CP_CONST,
3034 .resetvalue = cpu->id_afr0 },
3035 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
3036 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
3037 .access = PL1_R, .type = ARM_CP_CONST,
3038 .resetvalue = cpu->id_mmfr0 },
3039 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
3040 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
3041 .access = PL1_R, .type = ARM_CP_CONST,
3042 .resetvalue = cpu->id_mmfr1 },
3043 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
3044 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
3045 .access = PL1_R, .type = ARM_CP_CONST,
3046 .resetvalue = cpu->id_mmfr2 },
3047 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
3048 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
3049 .access = PL1_R, .type = ARM_CP_CONST,
3050 .resetvalue = cpu->id_mmfr3 },
3051 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
3052 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
3053 .access = PL1_R, .type = ARM_CP_CONST,
3054 .resetvalue = cpu->id_isar0 },
3055 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
3056 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
3057 .access = PL1_R, .type = ARM_CP_CONST,
3058 .resetvalue = cpu->id_isar1 },
3059 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
3060 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
3061 .access = PL1_R, .type = ARM_CP_CONST,
3062 .resetvalue = cpu->id_isar2 },
3063 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
3064 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
3065 .access = PL1_R, .type = ARM_CP_CONST,
3066 .resetvalue = cpu->id_isar3 },
3067 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
3068 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
3069 .access = PL1_R, .type = ARM_CP_CONST,
3070 .resetvalue = cpu->id_isar4 },
3071 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
3072 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
3073 .access = PL1_R, .type = ARM_CP_CONST,
3074 .resetvalue = cpu->id_isar5 },
3075 /* 6..7 are as yet unallocated and must RAZ */
3076 { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
3077 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
3078 .resetvalue = 0 },
3079 { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
3080 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
3081 .resetvalue = 0 },
3082 REGINFO_SENTINEL
3084 define_arm_cp_regs(cpu, v6_idregs);
3085 define_arm_cp_regs(cpu, v6_cp_reginfo);
3086 } else {
3087 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
3089 if (arm_feature(env, ARM_FEATURE_V6K)) {
3090 define_arm_cp_regs(cpu, v6k_cp_reginfo);
3092 if (arm_feature(env, ARM_FEATURE_V7MP)) {
3093 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
3095 if (arm_feature(env, ARM_FEATURE_V7)) {
3096 /* v7 performance monitor control register: same implementor
3097 * field as main ID register, and we implement only the cycle
3098 * count register.
3100 #ifndef CONFIG_USER_ONLY
3101 ARMCPRegInfo pmcr = {
3102 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
3103 .access = PL0_RW,
3104 .type = ARM_CP_IO | ARM_CP_ALIAS,
3105 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
3106 .accessfn = pmreg_access, .writefn = pmcr_write,
3107 .raw_writefn = raw_write,
3109 ARMCPRegInfo pmcr64 = {
3110 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
3111 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
3112 .access = PL0_RW, .accessfn = pmreg_access,
3113 .type = ARM_CP_IO,
3114 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
3115 .resetvalue = cpu->midr & 0xff000000,
3116 .writefn = pmcr_write, .raw_writefn = raw_write,
3118 define_one_arm_cp_reg(cpu, &pmcr);
3119 define_one_arm_cp_reg(cpu, &pmcr64);
3120 #endif
3121 ARMCPRegInfo clidr = {
3122 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
3123 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
3124 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
3126 define_one_arm_cp_reg(cpu, &clidr);
3127 define_arm_cp_regs(cpu, v7_cp_reginfo);
3128 define_debug_regs(cpu);
3129 } else {
3130 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
3132 if (arm_feature(env, ARM_FEATURE_V8)) {
3133 /* AArch64 ID registers, which all have impdef reset values */
3134 ARMCPRegInfo v8_idregs[] = {
3135 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
3136 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
3137 .access = PL1_R, .type = ARM_CP_CONST,
3138 .resetvalue = cpu->id_aa64pfr0 },
3139 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
3140 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
3141 .access = PL1_R, .type = ARM_CP_CONST,
3142 .resetvalue = cpu->id_aa64pfr1},
3143 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
3144 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
3145 .access = PL1_R, .type = ARM_CP_CONST,
3146 /* We mask out the PMUVer field, because we don't currently
3147 * implement the PMU. Not advertising it prevents the guest
3148 * from trying to use it and getting UNDEFs on registers we
3149 * don't implement.
3151 .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
3152 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
3153 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
3154 .access = PL1_R, .type = ARM_CP_CONST,
3155 .resetvalue = cpu->id_aa64dfr1 },
3156 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
3157 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
3158 .access = PL1_R, .type = ARM_CP_CONST,
3159 .resetvalue = cpu->id_aa64afr0 },
3160 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
3161 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
3162 .access = PL1_R, .type = ARM_CP_CONST,
3163 .resetvalue = cpu->id_aa64afr1 },
3164 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
3165 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
3166 .access = PL1_R, .type = ARM_CP_CONST,
3167 .resetvalue = cpu->id_aa64isar0 },
3168 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
3169 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
3170 .access = PL1_R, .type = ARM_CP_CONST,
3171 .resetvalue = cpu->id_aa64isar1 },
3172 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
3173 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
3174 .access = PL1_R, .type = ARM_CP_CONST,
3175 .resetvalue = cpu->id_aa64mmfr0 },
3176 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
3177 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
3178 .access = PL1_R, .type = ARM_CP_CONST,
3179 .resetvalue = cpu->id_aa64mmfr1 },
3180 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
3181 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
3182 .access = PL1_R, .type = ARM_CP_CONST,
3183 .resetvalue = cpu->mvfr0 },
3184 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
3185 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
3186 .access = PL1_R, .type = ARM_CP_CONST,
3187 .resetvalue = cpu->mvfr1 },
3188 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
3189 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
3190 .access = PL1_R, .type = ARM_CP_CONST,
3191 .resetvalue = cpu->mvfr2 },
3192 REGINFO_SENTINEL
3194 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
3195 if (!arm_feature(env, ARM_FEATURE_EL3) &&
3196 !arm_feature(env, ARM_FEATURE_EL2)) {
3197 ARMCPRegInfo rvbar = {
3198 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
3199 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
3200 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
3202 define_one_arm_cp_reg(cpu, &rvbar);
3204 define_arm_cp_regs(cpu, v8_idregs);
3205 define_arm_cp_regs(cpu, v8_cp_reginfo);
3207 if (arm_feature(env, ARM_FEATURE_EL2)) {
3208 define_arm_cp_regs(cpu, v8_el2_cp_reginfo);
3209 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
3210 if (!arm_feature(env, ARM_FEATURE_EL3)) {
3211 ARMCPRegInfo rvbar = {
3212 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
3213 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
3214 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
3216 define_one_arm_cp_reg(cpu, &rvbar);
3218 } else {
3219 /* If EL2 is missing but higher ELs are enabled, we need to
3220 * register the no_el2 reginfos.
3222 if (arm_feature(env, ARM_FEATURE_EL3)) {
3223 define_arm_cp_regs(cpu, v8_el3_no_el2_cp_reginfo);
3226 if (arm_feature(env, ARM_FEATURE_EL3)) {
3227 define_arm_cp_regs(cpu, el3_cp_reginfo);
3228 ARMCPRegInfo rvbar = {
3229 .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
3230 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
3231 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar
3233 define_one_arm_cp_reg(cpu, &rvbar);
3235 if (arm_feature(env, ARM_FEATURE_MPU)) {
3236 /* These are the MPU registers prior to PMSAv6. Any new
3237 * PMSA core later than the ARM946 will require that we
3238 * implement the PMSAv6 or PMSAv7 registers, which are
3239 * completely different.
3241 assert(!arm_feature(env, ARM_FEATURE_V6));
3242 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
3243 } else {
3244 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
3246 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
3247 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
3249 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
3250 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
3252 if (arm_feature(env, ARM_FEATURE_VAPA)) {
3253 define_arm_cp_regs(cpu, vapa_cp_reginfo);
3255 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
3256 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
3258 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
3259 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
3261 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
3262 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
3264 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
3265 define_arm_cp_regs(cpu, omap_cp_reginfo);
3267 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
3268 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
3270 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
3271 define_arm_cp_regs(cpu, xscale_cp_reginfo);
3273 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
3274 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
3276 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3277 define_arm_cp_regs(cpu, lpae_cp_reginfo);
3279 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
3280 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
3281 * be read-only (ie write causes UNDEF exception).
3284 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
3285 /* Pre-v8 MIDR space.
3286 * Note that the MIDR isn't a simple constant register because
3287 * of the TI925 behaviour where writes to another register can
3288 * cause the MIDR value to change.
3290 * Unimplemented registers in the c15 0 0 0 space default to
3291 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
3292 * and friends override accordingly.
3294 { .name = "MIDR",
3295 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
3296 .access = PL1_R, .resetvalue = cpu->midr,
3297 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
3298 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
3299 .type = ARM_CP_OVERRIDE },
3300 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
3301 { .name = "DUMMY",
3302 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
3303 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3304 { .name = "DUMMY",
3305 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
3306 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3307 { .name = "DUMMY",
3308 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
3309 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3310 { .name = "DUMMY",
3311 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
3312 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3313 { .name = "DUMMY",
3314 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
3315 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3316 REGINFO_SENTINEL
3318 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
3319 /* v8 MIDR -- the wildcard isn't necessary, and nor is the
3320 * variable-MIDR TI925 behaviour. Instead we have a single
3321 * (strictly speaking IMPDEF) alias of the MIDR, REVIDR.
3323 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
3324 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
3325 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr },
3326 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
3327 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
3328 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr },
3329 REGINFO_SENTINEL
3331 ARMCPRegInfo id_cp_reginfo[] = {
3332 /* These are common to v8 and pre-v8 */
3333 { .name = "CTR",
3334 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
3335 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
3336 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
3337 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
3338 .access = PL0_R, .accessfn = ctr_el0_access,
3339 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
3340 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
3341 { .name = "TCMTR",
3342 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
3343 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3344 { .name = "TLBTR",
3345 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
3346 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3347 REGINFO_SENTINEL
3349 ARMCPRegInfo crn0_wi_reginfo = {
3350 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
3351 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
3352 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
3354 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
3355 arm_feature(env, ARM_FEATURE_STRONGARM)) {
3356 ARMCPRegInfo *r;
3357 /* Register the blanket "writes ignored" value first to cover the
3358 * whole space. Then update the specific ID registers to allow write
3359 * access, so that they ignore writes rather than causing them to
3360 * UNDEF.
3362 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
3363 for (r = id_pre_v8_midr_cp_reginfo;
3364 r->type != ARM_CP_SENTINEL; r++) {
3365 r->access = PL1_RW;
3367 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
3368 r->access = PL1_RW;
3371 if (arm_feature(env, ARM_FEATURE_V8)) {
3372 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
3373 } else {
3374 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
3376 define_arm_cp_regs(cpu, id_cp_reginfo);
3379 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
3380 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
3383 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
3384 ARMCPRegInfo auxcr = {
3385 .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
3386 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
3387 .access = PL1_RW, .type = ARM_CP_CONST,
3388 .resetvalue = cpu->reset_auxcr
3390 define_one_arm_cp_reg(cpu, &auxcr);
3393 if (arm_feature(env, ARM_FEATURE_CBAR)) {
3394 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
3395 /* 32 bit view is [31:18] 0...0 [43:32]. */
3396 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
3397 | extract64(cpu->reset_cbar, 32, 12);
3398 ARMCPRegInfo cbar_reginfo[] = {
3399 { .name = "CBAR",
3400 .type = ARM_CP_CONST,
3401 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
3402 .access = PL1_R, .resetvalue = cpu->reset_cbar },
3403 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
3404 .type = ARM_CP_CONST,
3405 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
3406 .access = PL1_R, .resetvalue = cbar32 },
3407 REGINFO_SENTINEL
3409 /* We don't implement a r/w 64 bit CBAR currently */
3410 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
3411 define_arm_cp_regs(cpu, cbar_reginfo);
3412 } else {
3413 ARMCPRegInfo cbar = {
3414 .name = "CBAR",
3415 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
3416 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
3417 .fieldoffset = offsetof(CPUARMState,
3418 cp15.c15_config_base_address)
3420 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
3421 cbar.access = PL1_R;
3422 cbar.fieldoffset = 0;
3423 cbar.type = ARM_CP_CONST;
3425 define_one_arm_cp_reg(cpu, &cbar);
3429 /* Generic registers whose values depend on the implementation */
3431 ARMCPRegInfo sctlr = {
3432 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
3433 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
3434 .access = PL1_RW,
3435 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
3436 offsetof(CPUARMState, cp15.sctlr_ns) },
3437 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
3438 .raw_writefn = raw_write,
3440 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
3441 /* Normally we would always end the TB on an SCTLR write, but Linux
3442 * arch/arm/mach-pxa/sleep.S expects two instructions following
3443 * an MMU enable to execute from cache. Imitate this behaviour.
3445 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
3447 define_one_arm_cp_reg(cpu, &sctlr);
3451 ARMCPU *cpu_arm_init(const char *cpu_model)
3453 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
3456 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
3458 CPUState *cs = CPU(cpu);
3459 CPUARMState *env = &cpu->env;
3461 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
3462 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
3463 aarch64_fpu_gdb_set_reg,
3464 34, "aarch64-fpu.xml", 0);
3465 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
3466 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
3467 51, "arm-neon.xml", 0);
3468 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
3469 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
3470 35, "arm-vfp3.xml", 0);
3471 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
3472 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
3473 19, "arm-vfp.xml", 0);
3477 /* Sort alphabetically by type name, except for "any". */
3478 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
3480 ObjectClass *class_a = (ObjectClass *)a;
3481 ObjectClass *class_b = (ObjectClass *)b;
3482 const char *name_a, *name_b;
3484 name_a = object_class_get_name(class_a);
3485 name_b = object_class_get_name(class_b);
3486 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
3487 return 1;
3488 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
3489 return -1;
3490 } else {
3491 return strcmp(name_a, name_b);
3495 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
3497 ObjectClass *oc = data;
3498 CPUListState *s = user_data;
3499 const char *typename;
3500 char *name;
3502 typename = object_class_get_name(oc);
3503 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
3504 (*s->cpu_fprintf)(s->file, " %s\n",
3505 name);
3506 g_free(name);
3509 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
3511 CPUListState s = {
3512 .file = f,
3513 .cpu_fprintf = cpu_fprintf,
3515 GSList *list;
3517 list = object_class_get_list(TYPE_ARM_CPU, false);
3518 list = g_slist_sort(list, arm_cpu_list_compare);
3519 (*cpu_fprintf)(f, "Available CPUs:\n");
3520 g_slist_foreach(list, arm_cpu_list_entry, &s);
3521 g_slist_free(list);
3522 #ifdef CONFIG_KVM
3523 /* The 'host' CPU type is dynamically registered only if KVM is
3524 * enabled, so we have to special-case it here:
3526 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
3527 #endif
3530 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
3532 ObjectClass *oc = data;
3533 CpuDefinitionInfoList **cpu_list = user_data;
3534 CpuDefinitionInfoList *entry;
3535 CpuDefinitionInfo *info;
3536 const char *typename;
3538 typename = object_class_get_name(oc);
3539 info = g_malloc0(sizeof(*info));
3540 info->name = g_strndup(typename,
3541 strlen(typename) - strlen("-" TYPE_ARM_CPU));
3543 entry = g_malloc0(sizeof(*entry));
3544 entry->value = info;
3545 entry->next = *cpu_list;
3546 *cpu_list = entry;
3549 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
3551 CpuDefinitionInfoList *cpu_list = NULL;
3552 GSList *list;
3554 list = object_class_get_list(TYPE_ARM_CPU, false);
3555 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
3556 g_slist_free(list);
3558 return cpu_list;
3561 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
3562 void *opaque, int state, int secstate,
3563 int crm, int opc1, int opc2)
3565 /* Private utility function for define_one_arm_cp_reg_with_opaque():
3566 * add a single reginfo struct to the hash table.
3568 uint32_t *key = g_new(uint32_t, 1);
3569 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
3570 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3571 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
3573 /* Reset the secure state to the specific incoming state. This is
3574 * necessary as the register may have been defined with both states.
3576 r2->secure = secstate;
3578 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
3579 /* Register is banked (using both entries in array).
3580 * Overwriting fieldoffset as the array is only used to define
3581 * banked registers but later only fieldoffset is used.
3583 r2->fieldoffset = r->bank_fieldoffsets[ns];
3586 if (state == ARM_CP_STATE_AA32) {
3587 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
3588 /* If the register is banked then we don't need to migrate or
3589 * reset the 32-bit instance in certain cases:
3591 * 1) If the register has both 32-bit and 64-bit instances then we
3592 * can count on the 64-bit instance taking care of the
3593 * non-secure bank.
3594 * 2) If ARMv8 is enabled then we can count on a 64-bit version
3595 * taking care of the secure bank. This requires that separate
3596 * 32 and 64-bit definitions are provided.
3598 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
3599 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
3600 r2->type |= ARM_CP_ALIAS;
3601 r2->resetfn = arm_cp_reset_ignore;
3603 } else if ((secstate != r->secure) && !ns) {
3604 /* The register is not banked so we only want to allow migration of
3605 * the non-secure instance.
3607 r2->type |= ARM_CP_ALIAS;
3608 r2->resetfn = arm_cp_reset_ignore;
3611 if (r->state == ARM_CP_STATE_BOTH) {
3612 /* We assume it is a cp15 register if the .cp field is left unset.
3614 if (r2->cp == 0) {
3615 r2->cp = 15;
3618 #ifdef HOST_WORDS_BIGENDIAN
3619 if (r2->fieldoffset) {
3620 r2->fieldoffset += sizeof(uint32_t);
3622 #endif
3625 if (state == ARM_CP_STATE_AA64) {
3626 /* To allow abbreviation of ARMCPRegInfo
3627 * definitions, we treat cp == 0 as equivalent to
3628 * the value for "standard guest-visible sysreg".
3629 * STATE_BOTH definitions are also always "standard
3630 * sysreg" in their AArch64 view (the .cp value may
3631 * be non-zero for the benefit of the AArch32 view).
3633 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
3634 r2->cp = CP_REG_ARM64_SYSREG_CP;
3636 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
3637 r2->opc0, opc1, opc2);
3638 } else {
3639 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
3641 if (opaque) {
3642 r2->opaque = opaque;
3644 /* reginfo passed to helpers is correct for the actual access,
3645 * and is never ARM_CP_STATE_BOTH:
3647 r2->state = state;
3648 /* Make sure reginfo passed to helpers for wildcarded regs
3649 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
3651 r2->crm = crm;
3652 r2->opc1 = opc1;
3653 r2->opc2 = opc2;
3654 /* By convention, for wildcarded registers only the first
3655 * entry is used for migration; the others are marked as
3656 * ALIAS so we don't try to transfer the register
3657 * multiple times. Special registers (ie NOP/WFI) are
3658 * never migratable and not even raw-accessible.
3660 if ((r->type & ARM_CP_SPECIAL)) {
3661 r2->type |= ARM_CP_NO_RAW;
3663 if (((r->crm == CP_ANY) && crm != 0) ||
3664 ((r->opc1 == CP_ANY) && opc1 != 0) ||
3665 ((r->opc2 == CP_ANY) && opc2 != 0)) {
3666 r2->type |= ARM_CP_ALIAS;
3669 /* Check that raw accesses are either forbidden or handled. Note that
3670 * we can't assert this earlier because the setup of fieldoffset for
3671 * banked registers has to be done first.
3673 if (!(r2->type & ARM_CP_NO_RAW)) {
3674 assert(!raw_accessors_invalid(r2));
3677 /* Overriding of an existing definition must be explicitly
3678 * requested.
3680 if (!(r->type & ARM_CP_OVERRIDE)) {
3681 ARMCPRegInfo *oldreg;
3682 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
3683 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
3684 fprintf(stderr, "Register redefined: cp=%d %d bit "
3685 "crn=%d crm=%d opc1=%d opc2=%d, "
3686 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
3687 r2->crn, r2->crm, r2->opc1, r2->opc2,
3688 oldreg->name, r2->name);
3689 g_assert_not_reached();
3692 g_hash_table_insert(cpu->cp_regs, key, r2);
3696 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
3697 const ARMCPRegInfo *r, void *opaque)
3699 /* Define implementations of coprocessor registers.
3700 * We store these in a hashtable because typically
3701 * there are less than 150 registers in a space which
3702 * is 16*16*16*8*8 = 262144 in size.
3703 * Wildcarding is supported for the crm, opc1 and opc2 fields.
3704 * If a register is defined twice then the second definition is
3705 * used, so this can be used to define some generic registers and
3706 * then override them with implementation specific variations.
3707 * At least one of the original and the second definition should
3708 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
3709 * against accidental use.
3711 * The state field defines whether the register is to be
3712 * visible in the AArch32 or AArch64 execution state. If the
3713 * state is set to ARM_CP_STATE_BOTH then we synthesise a
3714 * reginfo structure for the AArch32 view, which sees the lower
3715 * 32 bits of the 64 bit register.
3717 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
3718 * be wildcarded. AArch64 registers are always considered to be 64
3719 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
3720 * the register, if any.
3722 int crm, opc1, opc2, state;
3723 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
3724 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
3725 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
3726 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
3727 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
3728 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
3729 /* 64 bit registers have only CRm and Opc1 fields */
3730 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
3731 /* op0 only exists in the AArch64 encodings */
3732 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
3733 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
3734 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
3735 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
3736 * encodes a minimum access level for the register. We roll this
3737 * runtime check into our general permission check code, so check
3738 * here that the reginfo's specified permissions are strict enough
3739 * to encompass the generic architectural permission check.
3741 if (r->state != ARM_CP_STATE_AA32) {
3742 int mask = 0;
3743 switch (r->opc1) {
3744 case 0: case 1: case 2:
3745 /* min_EL EL1 */
3746 mask = PL1_RW;
3747 break;
3748 case 3:
3749 /* min_EL EL0 */
3750 mask = PL0_RW;
3751 break;
3752 case 4:
3753 /* min_EL EL2 */
3754 mask = PL2_RW;
3755 break;
3756 case 5:
3757 /* unallocated encoding, so not possible */
3758 assert(false);
3759 break;
3760 case 6:
3761 /* min_EL EL3 */
3762 mask = PL3_RW;
3763 break;
3764 case 7:
3765 /* min_EL EL1, secure mode only (we don't check the latter) */
3766 mask = PL1_RW;
3767 break;
3768 default:
3769 /* broken reginfo with out-of-range opc1 */
3770 assert(false);
3771 break;
3773 /* assert our permissions are not too lax (stricter is fine) */
3774 assert((r->access & ~mask) == 0);
3777 /* Check that the register definition has enough info to handle
3778 * reads and writes if they are permitted.
3780 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
3781 if (r->access & PL3_R) {
3782 assert((r->fieldoffset ||
3783 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
3784 r->readfn);
3786 if (r->access & PL3_W) {
3787 assert((r->fieldoffset ||
3788 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
3789 r->writefn);
3792 /* Bad type field probably means missing sentinel at end of reg list */
3793 assert(cptype_valid(r->type));
3794 for (crm = crmmin; crm <= crmmax; crm++) {
3795 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
3796 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
3797 for (state = ARM_CP_STATE_AA32;
3798 state <= ARM_CP_STATE_AA64; state++) {
3799 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
3800 continue;
3802 if (state == ARM_CP_STATE_AA32) {
3803 /* Under AArch32 CP registers can be common
3804 * (same for secure and non-secure world) or banked.
3806 switch (r->secure) {
3807 case ARM_CP_SECSTATE_S:
3808 case ARM_CP_SECSTATE_NS:
3809 add_cpreg_to_hashtable(cpu, r, opaque, state,
3810 r->secure, crm, opc1, opc2);
3811 break;
3812 default:
3813 add_cpreg_to_hashtable(cpu, r, opaque, state,
3814 ARM_CP_SECSTATE_S,
3815 crm, opc1, opc2);
3816 add_cpreg_to_hashtable(cpu, r, opaque, state,
3817 ARM_CP_SECSTATE_NS,
3818 crm, opc1, opc2);
3819 break;
3821 } else {
3822 /* AArch64 registers get mapped to non-secure instance
3823 * of AArch32 */
3824 add_cpreg_to_hashtable(cpu, r, opaque, state,
3825 ARM_CP_SECSTATE_NS,
3826 crm, opc1, opc2);
3834 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
3835 const ARMCPRegInfo *regs, void *opaque)
3837 /* Define a whole list of registers */
3838 const ARMCPRegInfo *r;
3839 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
3840 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
3844 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
3846 return g_hash_table_lookup(cpregs, &encoded_cp);
3849 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
3850 uint64_t value)
3852 /* Helper coprocessor write function for write-ignore registers */
3855 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
3857 /* Helper coprocessor write function for read-as-zero registers */
3858 return 0;
3861 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
3863 /* Helper coprocessor reset function for do-nothing-on-reset registers */
3866 static int bad_mode_switch(CPUARMState *env, int mode)
3868 /* Return true if it is not valid for us to switch to
3869 * this CPU mode (ie all the UNPREDICTABLE cases in
3870 * the ARM ARM CPSRWriteByInstr pseudocode).
3872 switch (mode) {
3873 case ARM_CPU_MODE_USR:
3874 case ARM_CPU_MODE_SYS:
3875 case ARM_CPU_MODE_SVC:
3876 case ARM_CPU_MODE_ABT:
3877 case ARM_CPU_MODE_UND:
3878 case ARM_CPU_MODE_IRQ:
3879 case ARM_CPU_MODE_FIQ:
3880 return 0;
3881 case ARM_CPU_MODE_MON:
3882 return !arm_is_secure(env);
3883 default:
3884 return 1;
3888 uint32_t cpsr_read(CPUARMState *env)
3890 int ZF;
3891 ZF = (env->ZF == 0);
3892 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
3893 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
3894 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
3895 | ((env->condexec_bits & 0xfc) << 8)
3896 | (env->GE << 16) | (env->daif & CPSR_AIF);
3899 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
3901 uint32_t changed_daif;
3903 if (mask & CPSR_NZCV) {
3904 env->ZF = (~val) & CPSR_Z;
3905 env->NF = val;
3906 env->CF = (val >> 29) & 1;
3907 env->VF = (val << 3) & 0x80000000;
3909 if (mask & CPSR_Q)
3910 env->QF = ((val & CPSR_Q) != 0);
3911 if (mask & CPSR_T)
3912 env->thumb = ((val & CPSR_T) != 0);
3913 if (mask & CPSR_IT_0_1) {
3914 env->condexec_bits &= ~3;
3915 env->condexec_bits |= (val >> 25) & 3;
3917 if (mask & CPSR_IT_2_7) {
3918 env->condexec_bits &= 3;
3919 env->condexec_bits |= (val >> 8) & 0xfc;
3921 if (mask & CPSR_GE) {
3922 env->GE = (val >> 16) & 0xf;
3925 /* In a V7 implementation that includes the security extensions but does
3926 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
3927 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
3928 * bits respectively.
3930 * In a V8 implementation, it is permitted for privileged software to
3931 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
3933 if (!arm_feature(env, ARM_FEATURE_V8) &&
3934 arm_feature(env, ARM_FEATURE_EL3) &&
3935 !arm_feature(env, ARM_FEATURE_EL2) &&
3936 !arm_is_secure(env)) {
3938 changed_daif = (env->daif ^ val) & mask;
3940 if (changed_daif & CPSR_A) {
3941 /* Check to see if we are allowed to change the masking of async
3942 * abort exceptions from a non-secure state.
3944 if (!(env->cp15.scr_el3 & SCR_AW)) {
3945 qemu_log_mask(LOG_GUEST_ERROR,
3946 "Ignoring attempt to switch CPSR_A flag from "
3947 "non-secure world with SCR.AW bit clear\n");
3948 mask &= ~CPSR_A;
3952 if (changed_daif & CPSR_F) {
3953 /* Check to see if we are allowed to change the masking of FIQ
3954 * exceptions from a non-secure state.
3956 if (!(env->cp15.scr_el3 & SCR_FW)) {
3957 qemu_log_mask(LOG_GUEST_ERROR,
3958 "Ignoring attempt to switch CPSR_F flag from "
3959 "non-secure world with SCR.FW bit clear\n");
3960 mask &= ~CPSR_F;
3963 /* Check whether non-maskable FIQ (NMFI) support is enabled.
3964 * If this bit is set software is not allowed to mask
3965 * FIQs, but is allowed to set CPSR_F to 0.
3967 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
3968 (val & CPSR_F)) {
3969 qemu_log_mask(LOG_GUEST_ERROR,
3970 "Ignoring attempt to enable CPSR_F flag "
3971 "(non-maskable FIQ [NMFI] support enabled)\n");
3972 mask &= ~CPSR_F;
3977 env->daif &= ~(CPSR_AIF & mask);
3978 env->daif |= val & CPSR_AIF & mask;
3980 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
3981 if (bad_mode_switch(env, val & CPSR_M)) {
3982 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
3983 * We choose to ignore the attempt and leave the CPSR M field
3984 * untouched.
3986 mask &= ~CPSR_M;
3987 } else {
3988 switch_mode(env, val & CPSR_M);
3991 mask &= ~CACHED_CPSR_BITS;
3992 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
3995 /* Sign/zero extend */
3996 uint32_t HELPER(sxtb16)(uint32_t x)
3998 uint32_t res;
3999 res = (uint16_t)(int8_t)x;
4000 res |= (uint32_t)(int8_t)(x >> 16) << 16;
4001 return res;
4004 uint32_t HELPER(uxtb16)(uint32_t x)
4006 uint32_t res;
4007 res = (uint16_t)(uint8_t)x;
4008 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
4009 return res;
4012 uint32_t HELPER(clz)(uint32_t x)
4014 return clz32(x);
4017 int32_t HELPER(sdiv)(int32_t num, int32_t den)
4019 if (den == 0)
4020 return 0;
4021 if (num == INT_MIN && den == -1)
4022 return INT_MIN;
4023 return num / den;
4026 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
4028 if (den == 0)
4029 return 0;
4030 return num / den;
4033 uint32_t HELPER(rbit)(uint32_t x)
4035 x = ((x & 0xff000000) >> 24)
4036 | ((x & 0x00ff0000) >> 8)
4037 | ((x & 0x0000ff00) << 8)
4038 | ((x & 0x000000ff) << 24);
4039 x = ((x & 0xf0f0f0f0) >> 4)
4040 | ((x & 0x0f0f0f0f) << 4);
4041 x = ((x & 0x88888888) >> 3)
4042 | ((x & 0x44444444) >> 1)
4043 | ((x & 0x22222222) << 1)
4044 | ((x & 0x11111111) << 3);
4045 return x;
4048 #if defined(CONFIG_USER_ONLY)
4050 int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
4051 int mmu_idx)
4053 ARMCPU *cpu = ARM_CPU(cs);
4054 CPUARMState *env = &cpu->env;
4056 env->exception.vaddress = address;
4057 if (rw == 2) {
4058 cs->exception_index = EXCP_PREFETCH_ABORT;
4059 } else {
4060 cs->exception_index = EXCP_DATA_ABORT;
4062 return 1;
4065 /* These should probably raise undefined insn exceptions. */
4066 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
4068 ARMCPU *cpu = arm_env_get_cpu(env);
4070 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
4073 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
4075 ARMCPU *cpu = arm_env_get_cpu(env);
4077 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
4078 return 0;
4081 void switch_mode(CPUARMState *env, int mode)
4083 ARMCPU *cpu = arm_env_get_cpu(env);
4085 if (mode != ARM_CPU_MODE_USR) {
4086 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
4090 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
4092 ARMCPU *cpu = arm_env_get_cpu(env);
4094 cpu_abort(CPU(cpu), "banked r13 write\n");
4097 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
4099 ARMCPU *cpu = arm_env_get_cpu(env);
4101 cpu_abort(CPU(cpu), "banked r13 read\n");
4102 return 0;
4105 unsigned int arm_excp_target_el(CPUState *cs, unsigned int excp_idx)
4107 return 1;
4110 void aarch64_sync_64_to_32(CPUARMState *env)
4112 g_assert_not_reached();
4115 #else
4117 /* Map CPU modes onto saved register banks. */
4118 int bank_number(int mode)
4120 switch (mode) {
4121 case ARM_CPU_MODE_USR:
4122 case ARM_CPU_MODE_SYS:
4123 return 0;
4124 case ARM_CPU_MODE_SVC:
4125 return 1;
4126 case ARM_CPU_MODE_ABT:
4127 return 2;
4128 case ARM_CPU_MODE_UND:
4129 return 3;
4130 case ARM_CPU_MODE_IRQ:
4131 return 4;
4132 case ARM_CPU_MODE_FIQ:
4133 return 5;
4134 case ARM_CPU_MODE_HYP:
4135 return 6;
4136 case ARM_CPU_MODE_MON:
4137 return 7;
4139 hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
4142 void switch_mode(CPUARMState *env, int mode)
4144 int old_mode;
4145 int i;
4147 old_mode = env->uncached_cpsr & CPSR_M;
4148 if (mode == old_mode)
4149 return;
4151 if (old_mode == ARM_CPU_MODE_FIQ) {
4152 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
4153 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
4154 } else if (mode == ARM_CPU_MODE_FIQ) {
4155 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
4156 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
4159 i = bank_number(old_mode);
4160 env->banked_r13[i] = env->regs[13];
4161 env->banked_r14[i] = env->regs[14];
4162 env->banked_spsr[i] = env->spsr;
4164 i = bank_number(mode);
4165 env->regs[13] = env->banked_r13[i];
4166 env->regs[14] = env->banked_r14[i];
4167 env->spsr = env->banked_spsr[i];
4170 /* Physical Interrupt Target EL Lookup Table
4172 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
4174 * The below multi-dimensional table is used for looking up the target
4175 * exception level given numerous condition criteria. Specifically, the
4176 * target EL is based on SCR and HCR routing controls as well as the
4177 * currently executing EL and secure state.
4179 * Dimensions:
4180 * target_el_table[2][2][2][2][2][4]
4181 * | | | | | +--- Current EL
4182 * | | | | +------ Non-secure(0)/Secure(1)
4183 * | | | +--------- HCR mask override
4184 * | | +------------ SCR exec state control
4185 * | +--------------- SCR mask override
4186 * +------------------ 32-bit(0)/64-bit(1) EL3
4188 * The table values are as such:
4189 * 0-3 = EL0-EL3
4190 * -1 = Cannot occur
4192 * The ARM ARM target EL table includes entries indicating that an "exception
4193 * is not taken". The two cases where this is applicable are:
4194 * 1) An exception is taken from EL3 but the SCR does not have the exception
4195 * routed to EL3.
4196 * 2) An exception is taken from EL2 but the HCR does not have the exception
4197 * routed to EL2.
4198 * In these two cases, the below table contain a target of EL1. This value is
4199 * returned as it is expected that the consumer of the table data will check
4200 * for "target EL >= current EL" to ensure the exception is not taken.
4202 * SCR HCR
4203 * 64 EA AMO From
4204 * BIT IRQ IMO Non-secure Secure
4205 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
4207 const int8_t target_el_table[2][2][2][2][2][4] = {
4208 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
4209 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
4210 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
4211 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
4212 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
4213 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
4214 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
4215 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
4216 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
4217 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
4218 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
4219 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
4220 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
4221 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
4222 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
4223 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
4227 * Determine the target EL for physical exceptions
4229 static inline uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
4230 uint32_t cur_el, bool secure)
4232 CPUARMState *env = cs->env_ptr;
4233 int rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
4234 int scr;
4235 int hcr;
4236 int target_el;
4237 int is64 = arm_el_is_aa64(env, 3);
4239 switch (excp_idx) {
4240 case EXCP_IRQ:
4241 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
4242 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
4243 break;
4244 case EXCP_FIQ:
4245 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
4246 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
4247 break;
4248 default:
4249 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
4250 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
4251 break;
4254 /* If HCR.TGE is set then HCR is treated as being 1 */
4255 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
4257 /* Perform a table-lookup for the target EL given the current state */
4258 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
4260 assert(target_el > 0);
4262 return target_el;
4266 * Determine the target EL for a given exception type.
4268 unsigned int arm_excp_target_el(CPUState *cs, unsigned int excp_idx)
4270 ARMCPU *cpu = ARM_CPU(cs);
4271 CPUARMState *env = &cpu->env;
4272 unsigned int cur_el = arm_current_el(env);
4273 unsigned int target_el;
4274 bool secure = arm_is_secure(env);
4276 switch (excp_idx) {
4277 case EXCP_HVC:
4278 case EXCP_HYP_TRAP:
4279 target_el = 2;
4280 break;
4281 case EXCP_SMC:
4282 target_el = 3;
4283 break;
4284 case EXCP_FIQ:
4285 case EXCP_IRQ:
4286 target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure);
4287 break;
4288 case EXCP_VIRQ:
4289 case EXCP_VFIQ:
4290 target_el = 1;
4291 break;
4292 default:
4293 target_el = MAX(cur_el, 1);
4294 break;
4296 return target_el;
4299 static void v7m_push(CPUARMState *env, uint32_t val)
4301 CPUState *cs = CPU(arm_env_get_cpu(env));
4303 env->regs[13] -= 4;
4304 stl_phys(cs->as, env->regs[13], val);
4307 static uint32_t v7m_pop(CPUARMState *env)
4309 CPUState *cs = CPU(arm_env_get_cpu(env));
4310 uint32_t val;
4312 val = ldl_phys(cs->as, env->regs[13]);
4313 env->regs[13] += 4;
4314 return val;
4317 /* Switch to V7M main or process stack pointer. */
4318 static void switch_v7m_sp(CPUARMState *env, int process)
4320 uint32_t tmp;
4321 if (env->v7m.current_sp != process) {
4322 tmp = env->v7m.other_sp;
4323 env->v7m.other_sp = env->regs[13];
4324 env->regs[13] = tmp;
4325 env->v7m.current_sp = process;
4329 static void do_v7m_exception_exit(CPUARMState *env)
4331 uint32_t type;
4332 uint32_t xpsr;
4334 type = env->regs[15];
4335 if (env->v7m.exception != 0)
4336 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
4338 /* Switch to the target stack. */
4339 switch_v7m_sp(env, (type & 4) != 0);
4340 /* Pop registers. */
4341 env->regs[0] = v7m_pop(env);
4342 env->regs[1] = v7m_pop(env);
4343 env->regs[2] = v7m_pop(env);
4344 env->regs[3] = v7m_pop(env);
4345 env->regs[12] = v7m_pop(env);
4346 env->regs[14] = v7m_pop(env);
4347 env->regs[15] = v7m_pop(env);
4348 if (env->regs[15] & 1) {
4349 qemu_log_mask(LOG_GUEST_ERROR,
4350 "M profile return from interrupt with misaligned "
4351 "PC is UNPREDICTABLE\n");
4352 /* Actual hardware seems to ignore the lsbit, and there are several
4353 * RTOSes out there which incorrectly assume the r15 in the stack
4354 * frame should be a Thumb-style "lsbit indicates ARM/Thumb" value.
4356 env->regs[15] &= ~1U;
4358 xpsr = v7m_pop(env);
4359 xpsr_write(env, xpsr, 0xfffffdff);
4360 /* Undo stack alignment. */
4361 if (xpsr & 0x200)
4362 env->regs[13] |= 4;
4363 /* ??? The exception return type specifies Thread/Handler mode. However
4364 this is also implied by the xPSR value. Not sure what to do
4365 if there is a mismatch. */
4366 /* ??? Likewise for mismatches between the CONTROL register and the stack
4367 pointer. */
4370 void arm_v7m_cpu_do_interrupt(CPUState *cs)
4372 ARMCPU *cpu = ARM_CPU(cs);
4373 CPUARMState *env = &cpu->env;
4374 uint32_t xpsr = xpsr_read(env);
4375 uint32_t lr;
4376 uint32_t addr;
4378 arm_log_exception(cs->exception_index);
4380 lr = 0xfffffff1;
4381 if (env->v7m.current_sp)
4382 lr |= 4;
4383 if (env->v7m.exception == 0)
4384 lr |= 8;
4386 /* For exceptions we just mark as pending on the NVIC, and let that
4387 handle it. */
4388 /* TODO: Need to escalate if the current priority is higher than the
4389 one we're raising. */
4390 switch (cs->exception_index) {
4391 case EXCP_UDEF:
4392 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
4393 return;
4394 case EXCP_SWI:
4395 /* The PC already points to the next instruction. */
4396 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
4397 return;
4398 case EXCP_PREFETCH_ABORT:
4399 case EXCP_DATA_ABORT:
4400 /* TODO: if we implemented the MPU registers, this is where we
4401 * should set the MMFAR, etc from exception.fsr and exception.vaddress.
4403 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
4404 return;
4405 case EXCP_BKPT:
4406 if (semihosting_enabled) {
4407 int nr;
4408 nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
4409 if (nr == 0xab) {
4410 env->regs[15] += 2;
4411 env->regs[0] = do_arm_semihosting(env);
4412 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
4413 return;
4416 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
4417 return;
4418 case EXCP_IRQ:
4419 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
4420 break;
4421 case EXCP_EXCEPTION_EXIT:
4422 do_v7m_exception_exit(env);
4423 return;
4424 default:
4425 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
4426 return; /* Never happens. Keep compiler happy. */
4429 /* Align stack pointer. */
4430 /* ??? Should only do this if Configuration Control Register
4431 STACKALIGN bit is set. */
4432 if (env->regs[13] & 4) {
4433 env->regs[13] -= 4;
4434 xpsr |= 0x200;
4436 /* Switch to the handler mode. */
4437 v7m_push(env, xpsr);
4438 v7m_push(env, env->regs[15]);
4439 v7m_push(env, env->regs[14]);
4440 v7m_push(env, env->regs[12]);
4441 v7m_push(env, env->regs[3]);
4442 v7m_push(env, env->regs[2]);
4443 v7m_push(env, env->regs[1]);
4444 v7m_push(env, env->regs[0]);
4445 switch_v7m_sp(env, 0);
4446 /* Clear IT bits */
4447 env->condexec_bits = 0;
4448 env->regs[14] = lr;
4449 addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
4450 env->regs[15] = addr & 0xfffffffe;
4451 env->thumb = addr & 1;
4454 /* Function used to synchronize QEMU's AArch64 register set with AArch32
4455 * register set. This is necessary when switching between AArch32 and AArch64
4456 * execution state.
4458 void aarch64_sync_32_to_64(CPUARMState *env)
4460 int i;
4461 uint32_t mode = env->uncached_cpsr & CPSR_M;
4463 /* We can blanket copy R[0:7] to X[0:7] */
4464 for (i = 0; i < 8; i++) {
4465 env->xregs[i] = env->regs[i];
4468 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
4469 * Otherwise, they come from the banked user regs.
4471 if (mode == ARM_CPU_MODE_FIQ) {
4472 for (i = 8; i < 13; i++) {
4473 env->xregs[i] = env->usr_regs[i - 8];
4475 } else {
4476 for (i = 8; i < 13; i++) {
4477 env->xregs[i] = env->regs[i];
4481 /* Registers x13-x23 are the various mode SP and FP registers. Registers
4482 * r13 and r14 are only copied if we are in that mode, otherwise we copy
4483 * from the mode banked register.
4485 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
4486 env->xregs[13] = env->regs[13];
4487 env->xregs[14] = env->regs[14];
4488 } else {
4489 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
4490 /* HYP is an exception in that it is copied from r14 */
4491 if (mode == ARM_CPU_MODE_HYP) {
4492 env->xregs[14] = env->regs[14];
4493 } else {
4494 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
4498 if (mode == ARM_CPU_MODE_HYP) {
4499 env->xregs[15] = env->regs[13];
4500 } else {
4501 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
4504 if (mode == ARM_CPU_MODE_IRQ) {
4505 env->xregs[16] = env->regs[13];
4506 env->xregs[17] = env->regs[14];
4507 } else {
4508 env->xregs[16] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
4509 env->xregs[17] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
4512 if (mode == ARM_CPU_MODE_SVC) {
4513 env->xregs[18] = env->regs[13];
4514 env->xregs[19] = env->regs[14];
4515 } else {
4516 env->xregs[18] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
4517 env->xregs[19] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
4520 if (mode == ARM_CPU_MODE_ABT) {
4521 env->xregs[20] = env->regs[13];
4522 env->xregs[21] = env->regs[14];
4523 } else {
4524 env->xregs[20] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
4525 env->xregs[21] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
4528 if (mode == ARM_CPU_MODE_UND) {
4529 env->xregs[22] = env->regs[13];
4530 env->xregs[23] = env->regs[14];
4531 } else {
4532 env->xregs[22] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
4533 env->xregs[23] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
4536 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
4537 * mode, then we can copy from r8-r14. Otherwise, we copy from the
4538 * FIQ bank for r8-r14.
4540 if (mode == ARM_CPU_MODE_FIQ) {
4541 for (i = 24; i < 31; i++) {
4542 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
4544 } else {
4545 for (i = 24; i < 29; i++) {
4546 env->xregs[i] = env->fiq_regs[i - 24];
4548 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
4549 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
4552 env->pc = env->regs[15];
4555 /* Function used to synchronize QEMU's AArch32 register set with AArch64
4556 * register set. This is necessary when switching between AArch32 and AArch64
4557 * execution state.
4559 void aarch64_sync_64_to_32(CPUARMState *env)
4561 int i;
4562 uint32_t mode = env->uncached_cpsr & CPSR_M;
4564 /* We can blanket copy X[0:7] to R[0:7] */
4565 for (i = 0; i < 8; i++) {
4566 env->regs[i] = env->xregs[i];
4569 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
4570 * Otherwise, we copy x8-x12 into the banked user regs.
4572 if (mode == ARM_CPU_MODE_FIQ) {
4573 for (i = 8; i < 13; i++) {
4574 env->usr_regs[i - 8] = env->xregs[i];
4576 } else {
4577 for (i = 8; i < 13; i++) {
4578 env->regs[i] = env->xregs[i];
4582 /* Registers r13 & r14 depend on the current mode.
4583 * If we are in a given mode, we copy the corresponding x registers to r13
4584 * and r14. Otherwise, we copy the x register to the banked r13 and r14
4585 * for the mode.
4587 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
4588 env->regs[13] = env->xregs[13];
4589 env->regs[14] = env->xregs[14];
4590 } else {
4591 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
4593 /* HYP is an exception in that it does not have its own banked r14 but
4594 * shares the USR r14
4596 if (mode == ARM_CPU_MODE_HYP) {
4597 env->regs[14] = env->xregs[14];
4598 } else {
4599 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
4603 if (mode == ARM_CPU_MODE_HYP) {
4604 env->regs[13] = env->xregs[15];
4605 } else {
4606 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
4609 if (mode == ARM_CPU_MODE_IRQ) {
4610 env->regs[13] = env->xregs[16];
4611 env->regs[14] = env->xregs[17];
4612 } else {
4613 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
4614 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
4617 if (mode == ARM_CPU_MODE_SVC) {
4618 env->regs[13] = env->xregs[18];
4619 env->regs[14] = env->xregs[19];
4620 } else {
4621 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
4622 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
4625 if (mode == ARM_CPU_MODE_ABT) {
4626 env->regs[13] = env->xregs[20];
4627 env->regs[14] = env->xregs[21];
4628 } else {
4629 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
4630 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
4633 if (mode == ARM_CPU_MODE_UND) {
4634 env->regs[13] = env->xregs[22];
4635 env->regs[14] = env->xregs[23];
4636 } else {
4637 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
4638 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
4641 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
4642 * mode, then we can copy to r8-r14. Otherwise, we copy to the
4643 * FIQ bank for r8-r14.
4645 if (mode == ARM_CPU_MODE_FIQ) {
4646 for (i = 24; i < 31; i++) {
4647 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
4649 } else {
4650 for (i = 24; i < 29; i++) {
4651 env->fiq_regs[i - 24] = env->xregs[i];
4653 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
4654 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
4657 env->regs[15] = env->pc;
4660 /* Handle a CPU exception. */
4661 void arm_cpu_do_interrupt(CPUState *cs)
4663 ARMCPU *cpu = ARM_CPU(cs);
4664 CPUARMState *env = &cpu->env;
4665 uint32_t addr;
4666 uint32_t mask;
4667 int new_mode;
4668 uint32_t offset;
4669 uint32_t moe;
4671 assert(!IS_M(env));
4673 arm_log_exception(cs->exception_index);
4675 if (arm_is_psci_call(cpu, cs->exception_index)) {
4676 arm_handle_psci_call(cpu);
4677 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
4678 return;
4681 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
4682 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
4683 case EC_BREAKPOINT:
4684 case EC_BREAKPOINT_SAME_EL:
4685 moe = 1;
4686 break;
4687 case EC_WATCHPOINT:
4688 case EC_WATCHPOINT_SAME_EL:
4689 moe = 10;
4690 break;
4691 case EC_AA32_BKPT:
4692 moe = 3;
4693 break;
4694 case EC_VECTORCATCH:
4695 moe = 5;
4696 break;
4697 default:
4698 moe = 0;
4699 break;
4702 if (moe) {
4703 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
4706 /* TODO: Vectored interrupt controller. */
4707 switch (cs->exception_index) {
4708 case EXCP_UDEF:
4709 new_mode = ARM_CPU_MODE_UND;
4710 addr = 0x04;
4711 mask = CPSR_I;
4712 if (env->thumb)
4713 offset = 2;
4714 else
4715 offset = 4;
4716 break;
4717 case EXCP_SWI:
4718 if (semihosting_enabled) {
4719 /* Check for semihosting interrupt. */
4720 if (env->thumb) {
4721 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
4722 & 0xff;
4723 } else {
4724 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
4725 & 0xffffff;
4727 /* Only intercept calls from privileged modes, to provide some
4728 semblance of security. */
4729 if (((mask == 0x123456 && !env->thumb)
4730 || (mask == 0xab && env->thumb))
4731 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
4732 env->regs[0] = do_arm_semihosting(env);
4733 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
4734 return;
4737 new_mode = ARM_CPU_MODE_SVC;
4738 addr = 0x08;
4739 mask = CPSR_I;
4740 /* The PC already points to the next instruction. */
4741 offset = 0;
4742 break;
4743 case EXCP_BKPT:
4744 /* See if this is a semihosting syscall. */
4745 if (env->thumb && semihosting_enabled) {
4746 mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
4747 if (mask == 0xab
4748 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
4749 env->regs[15] += 2;
4750 env->regs[0] = do_arm_semihosting(env);
4751 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
4752 return;
4755 env->exception.fsr = 2;
4756 /* Fall through to prefetch abort. */
4757 case EXCP_PREFETCH_ABORT:
4758 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
4759 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
4760 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
4761 env->exception.fsr, (uint32_t)env->exception.vaddress);
4762 new_mode = ARM_CPU_MODE_ABT;
4763 addr = 0x0c;
4764 mask = CPSR_A | CPSR_I;
4765 offset = 4;
4766 break;
4767 case EXCP_DATA_ABORT:
4768 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
4769 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
4770 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4771 env->exception.fsr,
4772 (uint32_t)env->exception.vaddress);
4773 new_mode = ARM_CPU_MODE_ABT;
4774 addr = 0x10;
4775 mask = CPSR_A | CPSR_I;
4776 offset = 8;
4777 break;
4778 case EXCP_IRQ:
4779 new_mode = ARM_CPU_MODE_IRQ;
4780 addr = 0x18;
4781 /* Disable IRQ and imprecise data aborts. */
4782 mask = CPSR_A | CPSR_I;
4783 offset = 4;
4784 if (env->cp15.scr_el3 & SCR_IRQ) {
4785 /* IRQ routed to monitor mode */
4786 new_mode = ARM_CPU_MODE_MON;
4787 mask |= CPSR_F;
4789 break;
4790 case EXCP_FIQ:
4791 new_mode = ARM_CPU_MODE_FIQ;
4792 addr = 0x1c;
4793 /* Disable FIQ, IRQ and imprecise data aborts. */
4794 mask = CPSR_A | CPSR_I | CPSR_F;
4795 if (env->cp15.scr_el3 & SCR_FIQ) {
4796 /* FIQ routed to monitor mode */
4797 new_mode = ARM_CPU_MODE_MON;
4799 offset = 4;
4800 break;
4801 case EXCP_SMC:
4802 new_mode = ARM_CPU_MODE_MON;
4803 addr = 0x08;
4804 mask = CPSR_A | CPSR_I | CPSR_F;
4805 offset = 0;
4806 break;
4807 default:
4808 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
4809 return; /* Never happens. Keep compiler happy. */
4812 if (new_mode == ARM_CPU_MODE_MON) {
4813 addr += env->cp15.mvbar;
4814 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
4815 /* High vectors. When enabled, base address cannot be remapped. */
4816 addr += 0xffff0000;
4817 } else {
4818 /* ARM v7 architectures provide a vector base address register to remap
4819 * the interrupt vector table.
4820 * This register is only followed in non-monitor mode, and is banked.
4821 * Note: only bits 31:5 are valid.
4823 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
4826 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
4827 env->cp15.scr_el3 &= ~SCR_NS;
4830 switch_mode (env, new_mode);
4831 /* For exceptions taken to AArch32 we must clear the SS bit in both
4832 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
4834 env->uncached_cpsr &= ~PSTATE_SS;
4835 env->spsr = cpsr_read(env);
4836 /* Clear IT bits. */
4837 env->condexec_bits = 0;
4838 /* Switch to the new mode, and to the correct instruction set. */
4839 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
4840 env->daif |= mask;
4841 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
4842 * and we should just guard the thumb mode on V4 */
4843 if (arm_feature(env, ARM_FEATURE_V4T)) {
4844 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
4846 env->regs[14] = env->regs[15] + offset;
4847 env->regs[15] = addr;
4848 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
4852 /* Return the exception level which controls this address translation regime */
4853 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
4855 switch (mmu_idx) {
4856 case ARMMMUIdx_S2NS:
4857 case ARMMMUIdx_S1E2:
4858 return 2;
4859 case ARMMMUIdx_S1E3:
4860 return 3;
4861 case ARMMMUIdx_S1SE0:
4862 return arm_el_is_aa64(env, 3) ? 1 : 3;
4863 case ARMMMUIdx_S1SE1:
4864 case ARMMMUIdx_S1NSE0:
4865 case ARMMMUIdx_S1NSE1:
4866 return 1;
4867 default:
4868 g_assert_not_reached();
4872 /* Return true if this address translation regime is secure */
4873 static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx)
4875 switch (mmu_idx) {
4876 case ARMMMUIdx_S12NSE0:
4877 case ARMMMUIdx_S12NSE1:
4878 case ARMMMUIdx_S1NSE0:
4879 case ARMMMUIdx_S1NSE1:
4880 case ARMMMUIdx_S1E2:
4881 case ARMMMUIdx_S2NS:
4882 return false;
4883 case ARMMMUIdx_S1E3:
4884 case ARMMMUIdx_S1SE0:
4885 case ARMMMUIdx_S1SE1:
4886 return true;
4887 default:
4888 g_assert_not_reached();
4892 /* Return the SCTLR value which controls this address translation regime */
4893 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
4895 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
4898 /* Return true if the specified stage of address translation is disabled */
4899 static inline bool regime_translation_disabled(CPUARMState *env,
4900 ARMMMUIdx mmu_idx)
4902 if (mmu_idx == ARMMMUIdx_S2NS) {
4903 return (env->cp15.hcr_el2 & HCR_VM) == 0;
4905 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
4908 /* Return the TCR controlling this translation regime */
4909 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
4911 if (mmu_idx == ARMMMUIdx_S2NS) {
4912 /* TODO: return VTCR_EL2 */
4913 g_assert_not_reached();
4915 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
4918 /* Return the TTBR associated with this translation regime */
4919 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
4920 int ttbrn)
4922 if (mmu_idx == ARMMMUIdx_S2NS) {
4923 /* TODO: return VTTBR_EL2 */
4924 g_assert_not_reached();
4926 if (ttbrn == 0) {
4927 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
4928 } else {
4929 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
4933 /* Return true if the translation regime is using LPAE format page tables */
4934 static inline bool regime_using_lpae_format(CPUARMState *env,
4935 ARMMMUIdx mmu_idx)
4937 int el = regime_el(env, mmu_idx);
4938 if (el == 2 || arm_el_is_aa64(env, el)) {
4939 return true;
4941 if (arm_feature(env, ARM_FEATURE_LPAE)
4942 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
4943 return true;
4945 return false;
4948 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
4950 switch (mmu_idx) {
4951 case ARMMMUIdx_S1SE0:
4952 case ARMMMUIdx_S1NSE0:
4953 return true;
4954 default:
4955 return false;
4956 case ARMMMUIdx_S12NSE0:
4957 case ARMMMUIdx_S12NSE1:
4958 g_assert_not_reached();
4962 /* Translate section/page access permissions to page
4963 * R/W protection flags
4965 * @env: CPUARMState
4966 * @mmu_idx: MMU index indicating required translation regime
4967 * @ap: The 3-bit access permissions (AP[2:0])
4968 * @domain_prot: The 2-bit domain access permissions
4970 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
4971 int ap, int domain_prot)
4973 bool is_user = regime_is_user(env, mmu_idx);
4975 if (domain_prot == 3) {
4976 return PAGE_READ | PAGE_WRITE;
4979 switch (ap) {
4980 case 0:
4981 if (arm_feature(env, ARM_FEATURE_V7)) {
4982 return 0;
4984 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
4985 case SCTLR_S:
4986 return is_user ? 0 : PAGE_READ;
4987 case SCTLR_R:
4988 return PAGE_READ;
4989 default:
4990 return 0;
4992 case 1:
4993 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
4994 case 2:
4995 if (is_user) {
4996 return PAGE_READ;
4997 } else {
4998 return PAGE_READ | PAGE_WRITE;
5000 case 3:
5001 return PAGE_READ | PAGE_WRITE;
5002 case 4: /* Reserved. */
5003 return 0;
5004 case 5:
5005 return is_user ? 0 : PAGE_READ;
5006 case 6:
5007 return PAGE_READ;
5008 case 7:
5009 if (!arm_feature(env, ARM_FEATURE_V6K)) {
5010 return 0;
5012 return PAGE_READ;
5013 default:
5014 g_assert_not_reached();
5018 /* Translate section/page access permissions to page
5019 * R/W protection flags.
5021 * @ap: The 2-bit simple AP (AP[2:1])
5022 * @is_user: TRUE if accessing from PL0
5024 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
5026 switch (ap) {
5027 case 0:
5028 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
5029 case 1:
5030 return PAGE_READ | PAGE_WRITE;
5031 case 2:
5032 return is_user ? 0 : PAGE_READ;
5033 case 3:
5034 return PAGE_READ;
5035 default:
5036 g_assert_not_reached();
5040 static inline int
5041 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
5043 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
5046 /* Translate section/page access permissions to protection flags
5048 * @env: CPUARMState
5049 * @mmu_idx: MMU index indicating required translation regime
5050 * @is_aa64: TRUE if AArch64
5051 * @ap: The 2-bit simple AP (AP[2:1])
5052 * @ns: NS (non-secure) bit
5053 * @xn: XN (execute-never) bit
5054 * @pxn: PXN (privileged execute-never) bit
5056 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
5057 int ap, int ns, int xn, int pxn)
5059 bool is_user = regime_is_user(env, mmu_idx);
5060 int prot_rw, user_rw;
5061 bool have_wxn;
5062 int wxn = 0;
5064 assert(mmu_idx != ARMMMUIdx_S2NS);
5066 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
5067 if (is_user) {
5068 prot_rw = user_rw;
5069 } else {
5070 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
5073 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
5074 return prot_rw;
5077 /* TODO have_wxn should be replaced with
5078 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
5079 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
5080 * compatible processors have EL2, which is required for [U]WXN.
5082 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
5084 if (have_wxn) {
5085 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
5088 if (is_aa64) {
5089 switch (regime_el(env, mmu_idx)) {
5090 case 1:
5091 if (!is_user) {
5092 xn = pxn || (user_rw & PAGE_WRITE);
5094 break;
5095 case 2:
5096 case 3:
5097 break;
5099 } else if (arm_feature(env, ARM_FEATURE_V7)) {
5100 switch (regime_el(env, mmu_idx)) {
5101 case 1:
5102 case 3:
5103 if (is_user) {
5104 xn = xn || !(user_rw & PAGE_READ);
5105 } else {
5106 int uwxn = 0;
5107 if (have_wxn) {
5108 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
5110 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
5111 (uwxn && (user_rw & PAGE_WRITE));
5113 break;
5114 case 2:
5115 break;
5117 } else {
5118 xn = wxn = 0;
5121 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
5122 return prot_rw;
5124 return prot_rw | PAGE_EXEC;
5127 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
5128 uint32_t *table, uint32_t address)
5130 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
5131 TCR *tcr = regime_tcr(env, mmu_idx);
5133 if (address & tcr->mask) {
5134 if (tcr->raw_tcr & TTBCR_PD1) {
5135 /* Translation table walk disabled for TTBR1 */
5136 return false;
5138 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
5139 } else {
5140 if (tcr->raw_tcr & TTBCR_PD0) {
5141 /* Translation table walk disabled for TTBR0 */
5142 return false;
5144 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
5146 *table |= (address >> 18) & 0x3ffc;
5147 return true;
5150 /* All loads done in the course of a page table walk go through here.
5151 * TODO: rather than ignoring errors from physical memory reads (which
5152 * are external aborts in ARM terminology) we should propagate this
5153 * error out so that we can turn it into a Data Abort if this walk
5154 * was being done for a CPU load/store or an address translation instruction
5155 * (but not if it was for a debug access).
5157 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure)
5159 MemTxAttrs attrs = {};
5161 attrs.secure = is_secure;
5162 return address_space_ldl(cs->as, addr, attrs, NULL);
5165 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure)
5167 MemTxAttrs attrs = {};
5169 attrs.secure = is_secure;
5170 return address_space_ldq(cs->as, addr, attrs, NULL);
5173 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
5174 ARMMMUIdx mmu_idx, hwaddr *phys_ptr,
5175 int *prot, target_ulong *page_size)
5177 CPUState *cs = CPU(arm_env_get_cpu(env));
5178 int code;
5179 uint32_t table;
5180 uint32_t desc;
5181 int type;
5182 int ap;
5183 int domain = 0;
5184 int domain_prot;
5185 hwaddr phys_addr;
5186 uint32_t dacr;
5188 /* Pagetable walk. */
5189 /* Lookup l1 descriptor. */
5190 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
5191 /* Section translation fault if page walk is disabled by PD0 or PD1 */
5192 code = 5;
5193 goto do_fault;
5195 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
5196 type = (desc & 3);
5197 domain = (desc >> 5) & 0x0f;
5198 if (regime_el(env, mmu_idx) == 1) {
5199 dacr = env->cp15.dacr_ns;
5200 } else {
5201 dacr = env->cp15.dacr_s;
5203 domain_prot = (dacr >> (domain * 2)) & 3;
5204 if (type == 0) {
5205 /* Section translation fault. */
5206 code = 5;
5207 goto do_fault;
5209 if (domain_prot == 0 || domain_prot == 2) {
5210 if (type == 2)
5211 code = 9; /* Section domain fault. */
5212 else
5213 code = 11; /* Page domain fault. */
5214 goto do_fault;
5216 if (type == 2) {
5217 /* 1Mb section. */
5218 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
5219 ap = (desc >> 10) & 3;
5220 code = 13;
5221 *page_size = 1024 * 1024;
5222 } else {
5223 /* Lookup l2 entry. */
5224 if (type == 1) {
5225 /* Coarse pagetable. */
5226 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
5227 } else {
5228 /* Fine pagetable. */
5229 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
5231 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
5232 switch (desc & 3) {
5233 case 0: /* Page translation fault. */
5234 code = 7;
5235 goto do_fault;
5236 case 1: /* 64k page. */
5237 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
5238 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
5239 *page_size = 0x10000;
5240 break;
5241 case 2: /* 4k page. */
5242 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
5243 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
5244 *page_size = 0x1000;
5245 break;
5246 case 3: /* 1k page. */
5247 if (type == 1) {
5248 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
5249 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
5250 } else {
5251 /* Page translation fault. */
5252 code = 7;
5253 goto do_fault;
5255 } else {
5256 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
5258 ap = (desc >> 4) & 3;
5259 *page_size = 0x400;
5260 break;
5261 default:
5262 /* Never happens, but compiler isn't smart enough to tell. */
5263 abort();
5265 code = 15;
5267 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
5268 *prot |= *prot ? PAGE_EXEC : 0;
5269 if (!(*prot & (1 << access_type))) {
5270 /* Access permission fault. */
5271 goto do_fault;
5273 *phys_ptr = phys_addr;
5274 return 0;
5275 do_fault:
5276 return code | (domain << 4);
5279 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
5280 ARMMMUIdx mmu_idx, hwaddr *phys_ptr,
5281 MemTxAttrs *attrs,
5282 int *prot, target_ulong *page_size)
5284 CPUState *cs = CPU(arm_env_get_cpu(env));
5285 int code;
5286 uint32_t table;
5287 uint32_t desc;
5288 uint32_t xn;
5289 uint32_t pxn = 0;
5290 int type;
5291 int ap;
5292 int domain = 0;
5293 int domain_prot;
5294 hwaddr phys_addr;
5295 uint32_t dacr;
5296 bool ns;
5298 /* Pagetable walk. */
5299 /* Lookup l1 descriptor. */
5300 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
5301 /* Section translation fault if page walk is disabled by PD0 or PD1 */
5302 code = 5;
5303 goto do_fault;
5305 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
5306 type = (desc & 3);
5307 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
5308 /* Section translation fault, or attempt to use the encoding
5309 * which is Reserved on implementations without PXN.
5311 code = 5;
5312 goto do_fault;
5314 if ((type == 1) || !(desc & (1 << 18))) {
5315 /* Page or Section. */
5316 domain = (desc >> 5) & 0x0f;
5318 if (regime_el(env, mmu_idx) == 1) {
5319 dacr = env->cp15.dacr_ns;
5320 } else {
5321 dacr = env->cp15.dacr_s;
5323 domain_prot = (dacr >> (domain * 2)) & 3;
5324 if (domain_prot == 0 || domain_prot == 2) {
5325 if (type != 1) {
5326 code = 9; /* Section domain fault. */
5327 } else {
5328 code = 11; /* Page domain fault. */
5330 goto do_fault;
5332 if (type != 1) {
5333 if (desc & (1 << 18)) {
5334 /* Supersection. */
5335 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
5336 *page_size = 0x1000000;
5337 } else {
5338 /* Section. */
5339 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
5340 *page_size = 0x100000;
5342 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
5343 xn = desc & (1 << 4);
5344 pxn = desc & 1;
5345 code = 13;
5346 ns = extract32(desc, 19, 1);
5347 } else {
5348 if (arm_feature(env, ARM_FEATURE_PXN)) {
5349 pxn = (desc >> 2) & 1;
5351 ns = extract32(desc, 3, 1);
5352 /* Lookup l2 entry. */
5353 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
5354 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx));
5355 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
5356 switch (desc & 3) {
5357 case 0: /* Page translation fault. */
5358 code = 7;
5359 goto do_fault;
5360 case 1: /* 64k page. */
5361 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
5362 xn = desc & (1 << 15);
5363 *page_size = 0x10000;
5364 break;
5365 case 2: case 3: /* 4k page. */
5366 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
5367 xn = desc & 1;
5368 *page_size = 0x1000;
5369 break;
5370 default:
5371 /* Never happens, but compiler isn't smart enough to tell. */
5372 abort();
5374 code = 15;
5376 if (domain_prot == 3) {
5377 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
5378 } else {
5379 if (pxn && !regime_is_user(env, mmu_idx)) {
5380 xn = 1;
5382 if (xn && access_type == 2)
5383 goto do_fault;
5385 if (arm_feature(env, ARM_FEATURE_V6K) &&
5386 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
5387 /* The simplified model uses AP[0] as an access control bit. */
5388 if ((ap & 1) == 0) {
5389 /* Access flag fault. */
5390 code = (code == 15) ? 6 : 3;
5391 goto do_fault;
5393 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
5394 } else {
5395 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
5397 if (*prot && !xn) {
5398 *prot |= PAGE_EXEC;
5400 if (!(*prot & (1 << access_type))) {
5401 /* Access permission fault. */
5402 goto do_fault;
5405 if (ns) {
5406 /* The NS bit will (as required by the architecture) have no effect if
5407 * the CPU doesn't support TZ or this is a non-secure translation
5408 * regime, because the attribute will already be non-secure.
5410 attrs->secure = false;
5412 *phys_ptr = phys_addr;
5413 return 0;
5414 do_fault:
5415 return code | (domain << 4);
5418 /* Fault type for long-descriptor MMU fault reporting; this corresponds
5419 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
5421 typedef enum {
5422 translation_fault = 1,
5423 access_fault = 2,
5424 permission_fault = 3,
5425 } MMUFaultType;
5427 static int get_phys_addr_lpae(CPUARMState *env, target_ulong address,
5428 int access_type, ARMMMUIdx mmu_idx,
5429 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
5430 target_ulong *page_size_ptr)
5432 CPUState *cs = CPU(arm_env_get_cpu(env));
5433 /* Read an LPAE long-descriptor translation table. */
5434 MMUFaultType fault_type = translation_fault;
5435 uint32_t level = 1;
5436 uint32_t epd;
5437 int32_t tsz;
5438 uint32_t tg;
5439 uint64_t ttbr;
5440 int ttbr_select;
5441 hwaddr descaddr, descmask;
5442 uint32_t tableattrs;
5443 target_ulong page_size;
5444 uint32_t attrs;
5445 int32_t granule_sz = 9;
5446 int32_t va_size = 32;
5447 int32_t tbi = 0;
5448 TCR *tcr = regime_tcr(env, mmu_idx);
5449 int ap, ns, xn, pxn;
5450 uint32_t el = regime_el(env, mmu_idx);
5451 bool ttbr1_valid = true;
5453 /* TODO:
5454 * This code does not handle the different format TCR for VTCR_EL2.
5455 * This code also does not support shareability levels.
5456 * Attribute and permission bit handling should also be checked when adding
5457 * support for those page table walks.
5459 if (arm_el_is_aa64(env, el)) {
5460 va_size = 64;
5461 if (el > 1) {
5462 tbi = extract64(tcr->raw_tcr, 20, 1);
5463 } else {
5464 if (extract64(address, 55, 1)) {
5465 tbi = extract64(tcr->raw_tcr, 38, 1);
5466 } else {
5467 tbi = extract64(tcr->raw_tcr, 37, 1);
5470 tbi *= 8;
5472 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
5473 * invalid.
5475 if (el > 1) {
5476 ttbr1_valid = false;
5480 /* Determine whether this address is in the region controlled by
5481 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
5482 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
5483 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
5485 uint32_t t0sz = extract32(tcr->raw_tcr, 0, 6);
5486 if (va_size == 64) {
5487 t0sz = MIN(t0sz, 39);
5488 t0sz = MAX(t0sz, 16);
5490 uint32_t t1sz = extract32(tcr->raw_tcr, 16, 6);
5491 if (va_size == 64) {
5492 t1sz = MIN(t1sz, 39);
5493 t1sz = MAX(t1sz, 16);
5495 if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
5496 /* there is a ttbr0 region and we are in it (high bits all zero) */
5497 ttbr_select = 0;
5498 } else if (ttbr1_valid && t1sz &&
5499 !extract64(~address, va_size - t1sz, t1sz - tbi)) {
5500 /* there is a ttbr1 region and we are in it (high bits all one) */
5501 ttbr_select = 1;
5502 } else if (!t0sz) {
5503 /* ttbr0 region is "everything not in the ttbr1 region" */
5504 ttbr_select = 0;
5505 } else if (!t1sz && ttbr1_valid) {
5506 /* ttbr1 region is "everything not in the ttbr0 region" */
5507 ttbr_select = 1;
5508 } else {
5509 /* in the gap between the two regions, this is a Translation fault */
5510 fault_type = translation_fault;
5511 goto do_fault;
5514 /* Note that QEMU ignores shareability and cacheability attributes,
5515 * so we don't need to do anything with the SH, ORGN, IRGN fields
5516 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
5517 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
5518 * implement any ASID-like capability so we can ignore it (instead
5519 * we will always flush the TLB any time the ASID is changed).
5521 if (ttbr_select == 0) {
5522 ttbr = regime_ttbr(env, mmu_idx, 0);
5523 epd = extract32(tcr->raw_tcr, 7, 1);
5524 tsz = t0sz;
5526 tg = extract32(tcr->raw_tcr, 14, 2);
5527 if (tg == 1) { /* 64KB pages */
5528 granule_sz = 13;
5530 if (tg == 2) { /* 16KB pages */
5531 granule_sz = 11;
5533 } else {
5534 /* We should only be here if TTBR1 is valid */
5535 assert(ttbr1_valid);
5537 ttbr = regime_ttbr(env, mmu_idx, 1);
5538 epd = extract32(tcr->raw_tcr, 23, 1);
5539 tsz = t1sz;
5541 tg = extract32(tcr->raw_tcr, 30, 2);
5542 if (tg == 3) { /* 64KB pages */
5543 granule_sz = 13;
5545 if (tg == 1) { /* 16KB pages */
5546 granule_sz = 11;
5550 /* Here we should have set up all the parameters for the translation:
5551 * va_size, ttbr, epd, tsz, granule_sz, tbi
5554 if (epd) {
5555 /* Translation table walk disabled => Translation fault on TLB miss
5556 * Note: This is always 0 on 64-bit EL2 and EL3.
5558 goto do_fault;
5561 /* The starting level depends on the virtual address size (which can be
5562 * up to 48 bits) and the translation granule size. It indicates the number
5563 * of strides (granule_sz bits at a time) needed to consume the bits
5564 * of the input address. In the pseudocode this is:
5565 * level = 4 - RoundUp((inputsize - grainsize) / stride)
5566 * where their 'inputsize' is our 'va_size - tsz', 'grainsize' is
5567 * our 'granule_sz + 3' and 'stride' is our 'granule_sz'.
5568 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
5569 * = 4 - (va_size - tsz - granule_sz - 3 + granule_sz - 1) / granule_sz
5570 * = 4 - (va_size - tsz - 4) / granule_sz;
5572 level = 4 - (va_size - tsz - 4) / granule_sz;
5574 /* Clear the vaddr bits which aren't part of the within-region address,
5575 * so that we don't have to special case things when calculating the
5576 * first descriptor address.
5578 if (tsz) {
5579 address &= (1ULL << (va_size - tsz)) - 1;
5582 descmask = (1ULL << (granule_sz + 3)) - 1;
5584 /* Now we can extract the actual base address from the TTBR */
5585 descaddr = extract64(ttbr, 0, 48);
5586 descaddr &= ~((1ULL << (va_size - tsz - (granule_sz * (4 - level)))) - 1);
5588 /* Secure accesses start with the page table in secure memory and
5589 * can be downgraded to non-secure at any step. Non-secure accesses
5590 * remain non-secure. We implement this by just ORing in the NSTable/NS
5591 * bits at each step.
5593 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
5594 for (;;) {
5595 uint64_t descriptor;
5596 bool nstable;
5598 descaddr |= (address >> (granule_sz * (4 - level))) & descmask;
5599 descaddr &= ~7ULL;
5600 nstable = extract32(tableattrs, 4, 1);
5601 descriptor = arm_ldq_ptw(cs, descaddr, !nstable);
5602 if (!(descriptor & 1) ||
5603 (!(descriptor & 2) && (level == 3))) {
5604 /* Invalid, or the Reserved level 3 encoding */
5605 goto do_fault;
5607 descaddr = descriptor & 0xfffffff000ULL;
5609 if ((descriptor & 2) && (level < 3)) {
5610 /* Table entry. The top five bits are attributes which may
5611 * propagate down through lower levels of the table (and
5612 * which are all arranged so that 0 means "no effect", so
5613 * we can gather them up by ORing in the bits at each level).
5615 tableattrs |= extract64(descriptor, 59, 5);
5616 level++;
5617 continue;
5619 /* Block entry at level 1 or 2, or page entry at level 3.
5620 * These are basically the same thing, although the number
5621 * of bits we pull in from the vaddr varies.
5623 page_size = (1ULL << ((granule_sz * (4 - level)) + 3));
5624 descaddr |= (address & (page_size - 1));
5625 /* Extract attributes from the descriptor and merge with table attrs */
5626 attrs = extract64(descriptor, 2, 10)
5627 | (extract64(descriptor, 52, 12) << 10);
5628 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
5629 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
5630 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
5631 * means "force PL1 access only", which means forcing AP[1] to 0.
5633 if (extract32(tableattrs, 2, 1)) {
5634 attrs &= ~(1 << 4);
5636 attrs |= nstable << 3; /* NS */
5637 break;
5639 /* Here descaddr is the final physical address, and attributes
5640 * are all in attrs.
5642 fault_type = access_fault;
5643 if ((attrs & (1 << 8)) == 0) {
5644 /* Access flag */
5645 goto do_fault;
5648 ap = extract32(attrs, 4, 2);
5649 ns = extract32(attrs, 3, 1);
5650 xn = extract32(attrs, 12, 1);
5651 pxn = extract32(attrs, 11, 1);
5653 *prot = get_S1prot(env, mmu_idx, va_size == 64, ap, ns, xn, pxn);
5655 fault_type = permission_fault;
5656 if (!(*prot & (1 << access_type))) {
5657 goto do_fault;
5660 if (ns) {
5661 /* The NS bit will (as required by the architecture) have no effect if
5662 * the CPU doesn't support TZ or this is a non-secure translation
5663 * regime, because the attribute will already be non-secure.
5665 txattrs->secure = false;
5667 *phys_ptr = descaddr;
5668 *page_size_ptr = page_size;
5669 return 0;
5671 do_fault:
5672 /* Long-descriptor format IFSR/DFSR value */
5673 return (1 << 9) | (fault_type << 2) | level;
5676 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
5677 int access_type, ARMMMUIdx mmu_idx,
5678 hwaddr *phys_ptr, int *prot)
5680 int n;
5681 uint32_t mask;
5682 uint32_t base;
5683 bool is_user = regime_is_user(env, mmu_idx);
5685 *phys_ptr = address;
5686 for (n = 7; n >= 0; n--) {
5687 base = env->cp15.c6_region[n];
5688 if ((base & 1) == 0) {
5689 continue;
5691 mask = 1 << ((base >> 1) & 0x1f);
5692 /* Keep this shift separate from the above to avoid an
5693 (undefined) << 32. */
5694 mask = (mask << 1) - 1;
5695 if (((base ^ address) & ~mask) == 0) {
5696 break;
5699 if (n < 0) {
5700 return 2;
5703 if (access_type == 2) {
5704 mask = env->cp15.pmsav5_insn_ap;
5705 } else {
5706 mask = env->cp15.pmsav5_data_ap;
5708 mask = (mask >> (n * 4)) & 0xf;
5709 switch (mask) {
5710 case 0:
5711 return 1;
5712 case 1:
5713 if (is_user) {
5714 return 1;
5716 *prot = PAGE_READ | PAGE_WRITE;
5717 break;
5718 case 2:
5719 *prot = PAGE_READ;
5720 if (!is_user) {
5721 *prot |= PAGE_WRITE;
5723 break;
5724 case 3:
5725 *prot = PAGE_READ | PAGE_WRITE;
5726 break;
5727 case 5:
5728 if (is_user) {
5729 return 1;
5731 *prot = PAGE_READ;
5732 break;
5733 case 6:
5734 *prot = PAGE_READ;
5735 break;
5736 default:
5737 /* Bad permission. */
5738 return 1;
5740 *prot |= PAGE_EXEC;
5741 return 0;
5744 /* get_phys_addr - get the physical address for this virtual address
5746 * Find the physical address corresponding to the given virtual address,
5747 * by doing a translation table walk on MMU based systems or using the
5748 * MPU state on MPU based systems.
5750 * Returns 0 if the translation was successful. Otherwise, phys_ptr, attrs,
5751 * prot and page_size may not be filled in, and the return value provides
5752 * information on why the translation aborted, in the format of a
5753 * DFSR/IFSR fault register, with the following caveats:
5754 * * we honour the short vs long DFSR format differences.
5755 * * the WnR bit is never set (the caller must do this).
5756 * * for MPU based systems we don't bother to return a full FSR format
5757 * value.
5759 * @env: CPUARMState
5760 * @address: virtual address to get physical address for
5761 * @access_type: 0 for read, 1 for write, 2 for execute
5762 * @mmu_idx: MMU index indicating required translation regime
5763 * @phys_ptr: set to the physical address corresponding to the virtual address
5764 * @attrs: set to the memory transaction attributes to use
5765 * @prot: set to the permissions for the page containing phys_ptr
5766 * @page_size: set to the size of the page containing phys_ptr
5768 static inline int get_phys_addr(CPUARMState *env, target_ulong address,
5769 int access_type, ARMMMUIdx mmu_idx,
5770 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
5771 target_ulong *page_size)
5773 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
5774 /* TODO: when we support EL2 we should here call ourselves recursively
5775 * to do the stage 1 and then stage 2 translations. The arm_ld*_ptw
5776 * functions will also need changing to perform ARMMMUIdx_S2NS loads
5777 * rather than direct physical memory loads when appropriate.
5778 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
5780 assert(!arm_feature(env, ARM_FEATURE_EL2));
5781 mmu_idx += ARMMMUIdx_S1NSE0;
5784 /* The page table entries may downgrade secure to non-secure, but
5785 * cannot upgrade an non-secure translation regime's attributes
5786 * to secure.
5788 attrs->secure = regime_is_secure(env, mmu_idx);
5789 attrs->user = regime_is_user(env, mmu_idx);
5791 /* Fast Context Switch Extension. This doesn't exist at all in v8.
5792 * In v7 and earlier it affects all stage 1 translations.
5794 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
5795 && !arm_feature(env, ARM_FEATURE_V8)) {
5796 if (regime_el(env, mmu_idx) == 3) {
5797 address += env->cp15.fcseidr_s;
5798 } else {
5799 address += env->cp15.fcseidr_ns;
5803 if (regime_translation_disabled(env, mmu_idx)) {
5804 /* MMU/MPU disabled. */
5805 *phys_ptr = address;
5806 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
5807 *page_size = TARGET_PAGE_SIZE;
5808 return 0;
5811 if (arm_feature(env, ARM_FEATURE_MPU)) {
5812 *page_size = TARGET_PAGE_SIZE;
5813 return get_phys_addr_mpu(env, address, access_type, mmu_idx, phys_ptr,
5814 prot);
5817 if (regime_using_lpae_format(env, mmu_idx)) {
5818 return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
5819 attrs, prot, page_size);
5820 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
5821 return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
5822 attrs, prot, page_size);
5823 } else {
5824 return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
5825 prot, page_size);
5829 int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
5830 int access_type, int mmu_idx)
5832 ARMCPU *cpu = ARM_CPU(cs);
5833 CPUARMState *env = &cpu->env;
5834 hwaddr phys_addr;
5835 target_ulong page_size;
5836 int prot;
5837 int ret;
5838 uint32_t syn;
5839 bool same_el = (arm_current_el(env) != 0);
5840 MemTxAttrs attrs = {};
5842 ret = get_phys_addr(env, address, access_type, mmu_idx, &phys_addr,
5843 &attrs, &prot, &page_size);
5844 if (ret == 0) {
5845 /* Map a single [sub]page. */
5846 phys_addr &= TARGET_PAGE_MASK;
5847 address &= TARGET_PAGE_MASK;
5848 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
5849 prot, mmu_idx, page_size);
5850 return 0;
5853 /* AArch64 syndrome does not have an LPAE bit */
5854 syn = ret & ~(1 << 9);
5856 /* For insn and data aborts we assume there is no instruction syndrome
5857 * information; this is always true for exceptions reported to EL1.
5859 if (access_type == 2) {
5860 syn = syn_insn_abort(same_el, 0, 0, syn);
5861 cs->exception_index = EXCP_PREFETCH_ABORT;
5862 } else {
5863 syn = syn_data_abort(same_el, 0, 0, 0, access_type == 1, syn);
5864 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6)) {
5865 ret |= (1 << 11);
5867 cs->exception_index = EXCP_DATA_ABORT;
5870 env->exception.syndrome = syn;
5871 env->exception.vaddress = address;
5872 env->exception.fsr = ret;
5873 return 1;
5876 hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
5878 ARMCPU *cpu = ARM_CPU(cs);
5879 CPUARMState *env = &cpu->env;
5880 hwaddr phys_addr;
5881 target_ulong page_size;
5882 int prot;
5883 int ret;
5884 MemTxAttrs attrs = {};
5886 ret = get_phys_addr(env, addr, 0, cpu_mmu_index(env), &phys_addr,
5887 &attrs, &prot, &page_size);
5889 if (ret != 0) {
5890 return -1;
5893 return phys_addr;
5896 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
5898 if ((env->uncached_cpsr & CPSR_M) == mode) {
5899 env->regs[13] = val;
5900 } else {
5901 env->banked_r13[bank_number(mode)] = val;
5905 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
5907 if ((env->uncached_cpsr & CPSR_M) == mode) {
5908 return env->regs[13];
5909 } else {
5910 return env->banked_r13[bank_number(mode)];
5914 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
5916 ARMCPU *cpu = arm_env_get_cpu(env);
5918 switch (reg) {
5919 case 0: /* APSR */
5920 return xpsr_read(env) & 0xf8000000;
5921 case 1: /* IAPSR */
5922 return xpsr_read(env) & 0xf80001ff;
5923 case 2: /* EAPSR */
5924 return xpsr_read(env) & 0xff00fc00;
5925 case 3: /* xPSR */
5926 return xpsr_read(env) & 0xff00fdff;
5927 case 5: /* IPSR */
5928 return xpsr_read(env) & 0x000001ff;
5929 case 6: /* EPSR */
5930 return xpsr_read(env) & 0x0700fc00;
5931 case 7: /* IEPSR */
5932 return xpsr_read(env) & 0x0700edff;
5933 case 8: /* MSP */
5934 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
5935 case 9: /* PSP */
5936 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
5937 case 16: /* PRIMASK */
5938 return (env->daif & PSTATE_I) != 0;
5939 case 17: /* BASEPRI */
5940 case 18: /* BASEPRI_MAX */
5941 return env->v7m.basepri;
5942 case 19: /* FAULTMASK */
5943 return (env->daif & PSTATE_F) != 0;
5944 case 20: /* CONTROL */
5945 return env->v7m.control;
5946 default:
5947 /* ??? For debugging only. */
5948 cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
5949 return 0;
5953 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
5955 ARMCPU *cpu = arm_env_get_cpu(env);
5957 switch (reg) {
5958 case 0: /* APSR */
5959 xpsr_write(env, val, 0xf8000000);
5960 break;
5961 case 1: /* IAPSR */
5962 xpsr_write(env, val, 0xf8000000);
5963 break;
5964 case 2: /* EAPSR */
5965 xpsr_write(env, val, 0xfe00fc00);
5966 break;
5967 case 3: /* xPSR */
5968 xpsr_write(env, val, 0xfe00fc00);
5969 break;
5970 case 5: /* IPSR */
5971 /* IPSR bits are readonly. */
5972 break;
5973 case 6: /* EPSR */
5974 xpsr_write(env, val, 0x0600fc00);
5975 break;
5976 case 7: /* IEPSR */
5977 xpsr_write(env, val, 0x0600fc00);
5978 break;
5979 case 8: /* MSP */
5980 if (env->v7m.current_sp)
5981 env->v7m.other_sp = val;
5982 else
5983 env->regs[13] = val;
5984 break;
5985 case 9: /* PSP */
5986 if (env->v7m.current_sp)
5987 env->regs[13] = val;
5988 else
5989 env->v7m.other_sp = val;
5990 break;
5991 case 16: /* PRIMASK */
5992 if (val & 1) {
5993 env->daif |= PSTATE_I;
5994 } else {
5995 env->daif &= ~PSTATE_I;
5997 break;
5998 case 17: /* BASEPRI */
5999 env->v7m.basepri = val & 0xff;
6000 break;
6001 case 18: /* BASEPRI_MAX */
6002 val &= 0xff;
6003 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
6004 env->v7m.basepri = val;
6005 break;
6006 case 19: /* FAULTMASK */
6007 if (val & 1) {
6008 env->daif |= PSTATE_F;
6009 } else {
6010 env->daif &= ~PSTATE_F;
6012 break;
6013 case 20: /* CONTROL */
6014 env->v7m.control = val & 3;
6015 switch_v7m_sp(env, (val & 2) != 0);
6016 break;
6017 default:
6018 /* ??? For debugging only. */
6019 cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
6020 return;
6024 #endif
6026 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
6028 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
6029 * Note that we do not implement the (architecturally mandated)
6030 * alignment fault for attempts to use this on Device memory
6031 * (which matches the usual QEMU behaviour of not implementing either
6032 * alignment faults or any memory attribute handling).
6035 ARMCPU *cpu = arm_env_get_cpu(env);
6036 uint64_t blocklen = 4 << cpu->dcz_blocksize;
6037 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
6039 #ifndef CONFIG_USER_ONLY
6041 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
6042 * the block size so we might have to do more than one TLB lookup.
6043 * We know that in fact for any v8 CPU the page size is at least 4K
6044 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
6045 * 1K as an artefact of legacy v5 subpage support being present in the
6046 * same QEMU executable.
6048 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
6049 void *hostaddr[maxidx];
6050 int try, i;
6051 unsigned mmu_idx = cpu_mmu_index(env);
6052 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
6054 for (try = 0; try < 2; try++) {
6056 for (i = 0; i < maxidx; i++) {
6057 hostaddr[i] = tlb_vaddr_to_host(env,
6058 vaddr + TARGET_PAGE_SIZE * i,
6059 1, mmu_idx);
6060 if (!hostaddr[i]) {
6061 break;
6064 if (i == maxidx) {
6065 /* If it's all in the TLB it's fair game for just writing to;
6066 * we know we don't need to update dirty status, etc.
6068 for (i = 0; i < maxidx - 1; i++) {
6069 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
6071 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
6072 return;
6074 /* OK, try a store and see if we can populate the tlb. This
6075 * might cause an exception if the memory isn't writable,
6076 * in which case we will longjmp out of here. We must for
6077 * this purpose use the actual register value passed to us
6078 * so that we get the fault address right.
6080 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETRA());
6081 /* Now we can populate the other TLB entries, if any */
6082 for (i = 0; i < maxidx; i++) {
6083 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
6084 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
6085 helper_ret_stb_mmu(env, va, 0, oi, GETRA());
6090 /* Slow path (probably attempt to do this to an I/O device or
6091 * similar, or clearing of a block of code we have translations
6092 * cached for). Just do a series of byte writes as the architecture
6093 * demands. It's not worth trying to use a cpu_physical_memory_map(),
6094 * memset(), unmap() sequence here because:
6095 * + we'd need to account for the blocksize being larger than a page
6096 * + the direct-RAM access case is almost always going to be dealt
6097 * with in the fastpath code above, so there's no speed benefit
6098 * + we would have to deal with the map returning NULL because the
6099 * bounce buffer was in use
6101 for (i = 0; i < blocklen; i++) {
6102 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETRA());
6105 #else
6106 memset(g2h(vaddr), 0, blocklen);
6107 #endif
6110 /* Note that signed overflow is undefined in C. The following routines are
6111 careful to use unsigned types where modulo arithmetic is required.
6112 Failure to do so _will_ break on newer gcc. */
6114 /* Signed saturating arithmetic. */
6116 /* Perform 16-bit signed saturating addition. */
6117 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
6119 uint16_t res;
6121 res = a + b;
6122 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
6123 if (a & 0x8000)
6124 res = 0x8000;
6125 else
6126 res = 0x7fff;
6128 return res;
6131 /* Perform 8-bit signed saturating addition. */
6132 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
6134 uint8_t res;
6136 res = a + b;
6137 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
6138 if (a & 0x80)
6139 res = 0x80;
6140 else
6141 res = 0x7f;
6143 return res;
6146 /* Perform 16-bit signed saturating subtraction. */
6147 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
6149 uint16_t res;
6151 res = a - b;
6152 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
6153 if (a & 0x8000)
6154 res = 0x8000;
6155 else
6156 res = 0x7fff;
6158 return res;
6161 /* Perform 8-bit signed saturating subtraction. */
6162 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
6164 uint8_t res;
6166 res = a - b;
6167 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
6168 if (a & 0x80)
6169 res = 0x80;
6170 else
6171 res = 0x7f;
6173 return res;
6176 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
6177 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
6178 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
6179 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
6180 #define PFX q
6182 #include "op_addsub.h"
6184 /* Unsigned saturating arithmetic. */
6185 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6187 uint16_t res;
6188 res = a + b;
6189 if (res < a)
6190 res = 0xffff;
6191 return res;
6194 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6196 if (a > b)
6197 return a - b;
6198 else
6199 return 0;
6202 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
6204 uint8_t res;
6205 res = a + b;
6206 if (res < a)
6207 res = 0xff;
6208 return res;
6211 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
6213 if (a > b)
6214 return a - b;
6215 else
6216 return 0;
6219 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
6220 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
6221 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
6222 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
6223 #define PFX uq
6225 #include "op_addsub.h"
6227 /* Signed modulo arithmetic. */
6228 #define SARITH16(a, b, n, op) do { \
6229 int32_t sum; \
6230 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6231 RESULT(sum, n, 16); \
6232 if (sum >= 0) \
6233 ge |= 3 << (n * 2); \
6234 } while(0)
6236 #define SARITH8(a, b, n, op) do { \
6237 int32_t sum; \
6238 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6239 RESULT(sum, n, 8); \
6240 if (sum >= 0) \
6241 ge |= 1 << n; \
6242 } while(0)
6245 #define ADD16(a, b, n) SARITH16(a, b, n, +)
6246 #define SUB16(a, b, n) SARITH16(a, b, n, -)
6247 #define ADD8(a, b, n) SARITH8(a, b, n, +)
6248 #define SUB8(a, b, n) SARITH8(a, b, n, -)
6249 #define PFX s
6250 #define ARITH_GE
6252 #include "op_addsub.h"
6254 /* Unsigned modulo arithmetic. */
6255 #define ADD16(a, b, n) do { \
6256 uint32_t sum; \
6257 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
6258 RESULT(sum, n, 16); \
6259 if ((sum >> 16) == 1) \
6260 ge |= 3 << (n * 2); \
6261 } while(0)
6263 #define ADD8(a, b, n) do { \
6264 uint32_t sum; \
6265 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
6266 RESULT(sum, n, 8); \
6267 if ((sum >> 8) == 1) \
6268 ge |= 1 << n; \
6269 } while(0)
6271 #define SUB16(a, b, n) do { \
6272 uint32_t sum; \
6273 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
6274 RESULT(sum, n, 16); \
6275 if ((sum >> 16) == 0) \
6276 ge |= 3 << (n * 2); \
6277 } while(0)
6279 #define SUB8(a, b, n) do { \
6280 uint32_t sum; \
6281 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
6282 RESULT(sum, n, 8); \
6283 if ((sum >> 8) == 0) \
6284 ge |= 1 << n; \
6285 } while(0)
6287 #define PFX u
6288 #define ARITH_GE
6290 #include "op_addsub.h"
6292 /* Halved signed arithmetic. */
6293 #define ADD16(a, b, n) \
6294 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
6295 #define SUB16(a, b, n) \
6296 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
6297 #define ADD8(a, b, n) \
6298 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
6299 #define SUB8(a, b, n) \
6300 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
6301 #define PFX sh
6303 #include "op_addsub.h"
6305 /* Halved unsigned arithmetic. */
6306 #define ADD16(a, b, n) \
6307 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
6308 #define SUB16(a, b, n) \
6309 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
6310 #define ADD8(a, b, n) \
6311 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
6312 #define SUB8(a, b, n) \
6313 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
6314 #define PFX uh
6316 #include "op_addsub.h"
6318 static inline uint8_t do_usad(uint8_t a, uint8_t b)
6320 if (a > b)
6321 return a - b;
6322 else
6323 return b - a;
6326 /* Unsigned sum of absolute byte differences. */
6327 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
6329 uint32_t sum;
6330 sum = do_usad(a, b);
6331 sum += do_usad(a >> 8, b >> 8);
6332 sum += do_usad(a >> 16, b >>16);
6333 sum += do_usad(a >> 24, b >> 24);
6334 return sum;
6337 /* For ARMv6 SEL instruction. */
6338 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
6340 uint32_t mask;
6342 mask = 0;
6343 if (flags & 1)
6344 mask |= 0xff;
6345 if (flags & 2)
6346 mask |= 0xff00;
6347 if (flags & 4)
6348 mask |= 0xff0000;
6349 if (flags & 8)
6350 mask |= 0xff000000;
6351 return (a & mask) | (b & ~mask);
6354 /* VFP support. We follow the convention used for VFP instructions:
6355 Single precision routines have a "s" suffix, double precision a
6356 "d" suffix. */
6358 /* Convert host exception flags to vfp form. */
6359 static inline int vfp_exceptbits_from_host(int host_bits)
6361 int target_bits = 0;
6363 if (host_bits & float_flag_invalid)
6364 target_bits |= 1;
6365 if (host_bits & float_flag_divbyzero)
6366 target_bits |= 2;
6367 if (host_bits & float_flag_overflow)
6368 target_bits |= 4;
6369 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
6370 target_bits |= 8;
6371 if (host_bits & float_flag_inexact)
6372 target_bits |= 0x10;
6373 if (host_bits & float_flag_input_denormal)
6374 target_bits |= 0x80;
6375 return target_bits;
6378 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
6380 int i;
6381 uint32_t fpscr;
6383 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
6384 | (env->vfp.vec_len << 16)
6385 | (env->vfp.vec_stride << 20);
6386 i = get_float_exception_flags(&env->vfp.fp_status);
6387 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
6388 fpscr |= vfp_exceptbits_from_host(i);
6389 return fpscr;
6392 uint32_t vfp_get_fpscr(CPUARMState *env)
6394 return HELPER(vfp_get_fpscr)(env);
6397 /* Convert vfp exception flags to target form. */
6398 static inline int vfp_exceptbits_to_host(int target_bits)
6400 int host_bits = 0;
6402 if (target_bits & 1)
6403 host_bits |= float_flag_invalid;
6404 if (target_bits & 2)
6405 host_bits |= float_flag_divbyzero;
6406 if (target_bits & 4)
6407 host_bits |= float_flag_overflow;
6408 if (target_bits & 8)
6409 host_bits |= float_flag_underflow;
6410 if (target_bits & 0x10)
6411 host_bits |= float_flag_inexact;
6412 if (target_bits & 0x80)
6413 host_bits |= float_flag_input_denormal;
6414 return host_bits;
6417 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
6419 int i;
6420 uint32_t changed;
6422 changed = env->vfp.xregs[ARM_VFP_FPSCR];
6423 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
6424 env->vfp.vec_len = (val >> 16) & 7;
6425 env->vfp.vec_stride = (val >> 20) & 3;
6427 changed ^= val;
6428 if (changed & (3 << 22)) {
6429 i = (val >> 22) & 3;
6430 switch (i) {
6431 case FPROUNDING_TIEEVEN:
6432 i = float_round_nearest_even;
6433 break;
6434 case FPROUNDING_POSINF:
6435 i = float_round_up;
6436 break;
6437 case FPROUNDING_NEGINF:
6438 i = float_round_down;
6439 break;
6440 case FPROUNDING_ZERO:
6441 i = float_round_to_zero;
6442 break;
6444 set_float_rounding_mode(i, &env->vfp.fp_status);
6446 if (changed & (1 << 24)) {
6447 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
6448 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
6450 if (changed & (1 << 25))
6451 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
6453 i = vfp_exceptbits_to_host(val);
6454 set_float_exception_flags(i, &env->vfp.fp_status);
6455 set_float_exception_flags(0, &env->vfp.standard_fp_status);
6458 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
6460 HELPER(vfp_set_fpscr)(env, val);
6463 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
6465 #define VFP_BINOP(name) \
6466 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
6468 float_status *fpst = fpstp; \
6469 return float32_ ## name(a, b, fpst); \
6471 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
6473 float_status *fpst = fpstp; \
6474 return float64_ ## name(a, b, fpst); \
6476 VFP_BINOP(add)
6477 VFP_BINOP(sub)
6478 VFP_BINOP(mul)
6479 VFP_BINOP(div)
6480 VFP_BINOP(min)
6481 VFP_BINOP(max)
6482 VFP_BINOP(minnum)
6483 VFP_BINOP(maxnum)
6484 #undef VFP_BINOP
6486 float32 VFP_HELPER(neg, s)(float32 a)
6488 return float32_chs(a);
6491 float64 VFP_HELPER(neg, d)(float64 a)
6493 return float64_chs(a);
6496 float32 VFP_HELPER(abs, s)(float32 a)
6498 return float32_abs(a);
6501 float64 VFP_HELPER(abs, d)(float64 a)
6503 return float64_abs(a);
6506 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
6508 return float32_sqrt(a, &env->vfp.fp_status);
6511 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
6513 return float64_sqrt(a, &env->vfp.fp_status);
6516 /* XXX: check quiet/signaling case */
6517 #define DO_VFP_cmp(p, type) \
6518 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
6520 uint32_t flags; \
6521 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
6522 case 0: flags = 0x6; break; \
6523 case -1: flags = 0x8; break; \
6524 case 1: flags = 0x2; break; \
6525 default: case 2: flags = 0x3; break; \
6527 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
6528 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
6530 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
6532 uint32_t flags; \
6533 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
6534 case 0: flags = 0x6; break; \
6535 case -1: flags = 0x8; break; \
6536 case 1: flags = 0x2; break; \
6537 default: case 2: flags = 0x3; break; \
6539 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
6540 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
6542 DO_VFP_cmp(s, float32)
6543 DO_VFP_cmp(d, float64)
6544 #undef DO_VFP_cmp
6546 /* Integer to float and float to integer conversions */
6548 #define CONV_ITOF(name, fsz, sign) \
6549 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
6551 float_status *fpst = fpstp; \
6552 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
6555 #define CONV_FTOI(name, fsz, sign, round) \
6556 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
6558 float_status *fpst = fpstp; \
6559 if (float##fsz##_is_any_nan(x)) { \
6560 float_raise(float_flag_invalid, fpst); \
6561 return 0; \
6563 return float##fsz##_to_##sign##int32##round(x, fpst); \
6566 #define FLOAT_CONVS(name, p, fsz, sign) \
6567 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
6568 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
6569 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
6571 FLOAT_CONVS(si, s, 32, )
6572 FLOAT_CONVS(si, d, 64, )
6573 FLOAT_CONVS(ui, s, 32, u)
6574 FLOAT_CONVS(ui, d, 64, u)
6576 #undef CONV_ITOF
6577 #undef CONV_FTOI
6578 #undef FLOAT_CONVS
6580 /* floating point conversion */
6581 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
6583 float64 r = float32_to_float64(x, &env->vfp.fp_status);
6584 /* ARM requires that S<->D conversion of any kind of NaN generates
6585 * a quiet NaN by forcing the most significant frac bit to 1.
6587 return float64_maybe_silence_nan(r);
6590 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
6592 float32 r = float64_to_float32(x, &env->vfp.fp_status);
6593 /* ARM requires that S<->D conversion of any kind of NaN generates
6594 * a quiet NaN by forcing the most significant frac bit to 1.
6596 return float32_maybe_silence_nan(r);
6599 /* VFP3 fixed point conversion. */
6600 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
6601 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
6602 void *fpstp) \
6604 float_status *fpst = fpstp; \
6605 float##fsz tmp; \
6606 tmp = itype##_to_##float##fsz(x, fpst); \
6607 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
6610 /* Notice that we want only input-denormal exception flags from the
6611 * scalbn operation: the other possible flags (overflow+inexact if
6612 * we overflow to infinity, output-denormal) aren't correct for the
6613 * complete scale-and-convert operation.
6615 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
6616 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
6617 uint32_t shift, \
6618 void *fpstp) \
6620 float_status *fpst = fpstp; \
6621 int old_exc_flags = get_float_exception_flags(fpst); \
6622 float##fsz tmp; \
6623 if (float##fsz##_is_any_nan(x)) { \
6624 float_raise(float_flag_invalid, fpst); \
6625 return 0; \
6627 tmp = float##fsz##_scalbn(x, shift, fpst); \
6628 old_exc_flags |= get_float_exception_flags(fpst) \
6629 & float_flag_input_denormal; \
6630 set_float_exception_flags(old_exc_flags, fpst); \
6631 return float##fsz##_to_##itype##round(tmp, fpst); \
6634 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \
6635 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
6636 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
6637 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
6639 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
6640 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
6641 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
6643 VFP_CONV_FIX(sh, d, 64, 64, int16)
6644 VFP_CONV_FIX(sl, d, 64, 64, int32)
6645 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
6646 VFP_CONV_FIX(uh, d, 64, 64, uint16)
6647 VFP_CONV_FIX(ul, d, 64, 64, uint32)
6648 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
6649 VFP_CONV_FIX(sh, s, 32, 32, int16)
6650 VFP_CONV_FIX(sl, s, 32, 32, int32)
6651 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
6652 VFP_CONV_FIX(uh, s, 32, 32, uint16)
6653 VFP_CONV_FIX(ul, s, 32, 32, uint32)
6654 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
6655 #undef VFP_CONV_FIX
6656 #undef VFP_CONV_FIX_FLOAT
6657 #undef VFP_CONV_FLOAT_FIX_ROUND
6659 /* Set the current fp rounding mode and return the old one.
6660 * The argument is a softfloat float_round_ value.
6662 uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
6664 float_status *fp_status = &env->vfp.fp_status;
6666 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
6667 set_float_rounding_mode(rmode, fp_status);
6669 return prev_rmode;
6672 /* Set the current fp rounding mode in the standard fp status and return
6673 * the old one. This is for NEON instructions that need to change the
6674 * rounding mode but wish to use the standard FPSCR values for everything
6675 * else. Always set the rounding mode back to the correct value after
6676 * modifying it.
6677 * The argument is a softfloat float_round_ value.
6679 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
6681 float_status *fp_status = &env->vfp.standard_fp_status;
6683 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
6684 set_float_rounding_mode(rmode, fp_status);
6686 return prev_rmode;
6689 /* Half precision conversions. */
6690 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
6692 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
6693 float32 r = float16_to_float32(make_float16(a), ieee, s);
6694 if (ieee) {
6695 return float32_maybe_silence_nan(r);
6697 return r;
6700 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
6702 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
6703 float16 r = float32_to_float16(a, ieee, s);
6704 if (ieee) {
6705 r = float16_maybe_silence_nan(r);
6707 return float16_val(r);
6710 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
6712 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
6715 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
6717 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
6720 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
6722 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
6725 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
6727 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
6730 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
6732 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
6733 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
6734 if (ieee) {
6735 return float64_maybe_silence_nan(r);
6737 return r;
6740 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
6742 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
6743 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
6744 if (ieee) {
6745 r = float16_maybe_silence_nan(r);
6747 return float16_val(r);
6750 #define float32_two make_float32(0x40000000)
6751 #define float32_three make_float32(0x40400000)
6752 #define float32_one_point_five make_float32(0x3fc00000)
6754 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
6756 float_status *s = &env->vfp.standard_fp_status;
6757 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
6758 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
6759 if (!(float32_is_zero(a) || float32_is_zero(b))) {
6760 float_raise(float_flag_input_denormal, s);
6762 return float32_two;
6764 return float32_sub(float32_two, float32_mul(a, b, s), s);
6767 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
6769 float_status *s = &env->vfp.standard_fp_status;
6770 float32 product;
6771 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
6772 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
6773 if (!(float32_is_zero(a) || float32_is_zero(b))) {
6774 float_raise(float_flag_input_denormal, s);
6776 return float32_one_point_five;
6778 product = float32_mul(a, b, s);
6779 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
6782 /* NEON helpers. */
6784 /* Constants 256 and 512 are used in some helpers; we avoid relying on
6785 * int->float conversions at run-time. */
6786 #define float64_256 make_float64(0x4070000000000000LL)
6787 #define float64_512 make_float64(0x4080000000000000LL)
6788 #define float32_maxnorm make_float32(0x7f7fffff)
6789 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
6791 /* Reciprocal functions
6793 * The algorithm that must be used to calculate the estimate
6794 * is specified by the ARM ARM, see FPRecipEstimate()
6797 static float64 recip_estimate(float64 a, float_status *real_fp_status)
6799 /* These calculations mustn't set any fp exception flags,
6800 * so we use a local copy of the fp_status.
6802 float_status dummy_status = *real_fp_status;
6803 float_status *s = &dummy_status;
6804 /* q = (int)(a * 512.0) */
6805 float64 q = float64_mul(float64_512, a, s);
6806 int64_t q_int = float64_to_int64_round_to_zero(q, s);
6808 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
6809 q = int64_to_float64(q_int, s);
6810 q = float64_add(q, float64_half, s);
6811 q = float64_div(q, float64_512, s);
6812 q = float64_div(float64_one, q, s);
6814 /* s = (int)(256.0 * r + 0.5) */
6815 q = float64_mul(q, float64_256, s);
6816 q = float64_add(q, float64_half, s);
6817 q_int = float64_to_int64_round_to_zero(q, s);
6819 /* return (double)s / 256.0 */
6820 return float64_div(int64_to_float64(q_int, s), float64_256, s);
6823 /* Common wrapper to call recip_estimate */
6824 static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
6826 uint64_t val64 = float64_val(num);
6827 uint64_t frac = extract64(val64, 0, 52);
6828 int64_t exp = extract64(val64, 52, 11);
6829 uint64_t sbit;
6830 float64 scaled, estimate;
6832 /* Generate the scaled number for the estimate function */
6833 if (exp == 0) {
6834 if (extract64(frac, 51, 1) == 0) {
6835 exp = -1;
6836 frac = extract64(frac, 0, 50) << 2;
6837 } else {
6838 frac = extract64(frac, 0, 51) << 1;
6842 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
6843 scaled = make_float64((0x3feULL << 52)
6844 | extract64(frac, 44, 8) << 44);
6846 estimate = recip_estimate(scaled, fpst);
6848 /* Build new result */
6849 val64 = float64_val(estimate);
6850 sbit = 0x8000000000000000ULL & val64;
6851 exp = off - exp;
6852 frac = extract64(val64, 0, 52);
6854 if (exp == 0) {
6855 frac = 1ULL << 51 | extract64(frac, 1, 51);
6856 } else if (exp == -1) {
6857 frac = 1ULL << 50 | extract64(frac, 2, 50);
6858 exp = 0;
6861 return make_float64(sbit | (exp << 52) | frac);
6864 static bool round_to_inf(float_status *fpst, bool sign_bit)
6866 switch (fpst->float_rounding_mode) {
6867 case float_round_nearest_even: /* Round to Nearest */
6868 return true;
6869 case float_round_up: /* Round to +Inf */
6870 return !sign_bit;
6871 case float_round_down: /* Round to -Inf */
6872 return sign_bit;
6873 case float_round_to_zero: /* Round to Zero */
6874 return false;
6877 g_assert_not_reached();
6880 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
6882 float_status *fpst = fpstp;
6883 float32 f32 = float32_squash_input_denormal(input, fpst);
6884 uint32_t f32_val = float32_val(f32);
6885 uint32_t f32_sbit = 0x80000000ULL & f32_val;
6886 int32_t f32_exp = extract32(f32_val, 23, 8);
6887 uint32_t f32_frac = extract32(f32_val, 0, 23);
6888 float64 f64, r64;
6889 uint64_t r64_val;
6890 int64_t r64_exp;
6891 uint64_t r64_frac;
6893 if (float32_is_any_nan(f32)) {
6894 float32 nan = f32;
6895 if (float32_is_signaling_nan(f32)) {
6896 float_raise(float_flag_invalid, fpst);
6897 nan = float32_maybe_silence_nan(f32);
6899 if (fpst->default_nan_mode) {
6900 nan = float32_default_nan;
6902 return nan;
6903 } else if (float32_is_infinity(f32)) {
6904 return float32_set_sign(float32_zero, float32_is_neg(f32));
6905 } else if (float32_is_zero(f32)) {
6906 float_raise(float_flag_divbyzero, fpst);
6907 return float32_set_sign(float32_infinity, float32_is_neg(f32));
6908 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
6909 /* Abs(value) < 2.0^-128 */
6910 float_raise(float_flag_overflow | float_flag_inexact, fpst);
6911 if (round_to_inf(fpst, f32_sbit)) {
6912 return float32_set_sign(float32_infinity, float32_is_neg(f32));
6913 } else {
6914 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
6916 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
6917 float_raise(float_flag_underflow, fpst);
6918 return float32_set_sign(float32_zero, float32_is_neg(f32));
6922 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
6923 r64 = call_recip_estimate(f64, 253, fpst);
6924 r64_val = float64_val(r64);
6925 r64_exp = extract64(r64_val, 52, 11);
6926 r64_frac = extract64(r64_val, 0, 52);
6928 /* result = sign : result_exp<7:0> : fraction<51:29>; */
6929 return make_float32(f32_sbit |
6930 (r64_exp & 0xff) << 23 |
6931 extract64(r64_frac, 29, 24));
6934 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
6936 float_status *fpst = fpstp;
6937 float64 f64 = float64_squash_input_denormal(input, fpst);
6938 uint64_t f64_val = float64_val(f64);
6939 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
6940 int64_t f64_exp = extract64(f64_val, 52, 11);
6941 float64 r64;
6942 uint64_t r64_val;
6943 int64_t r64_exp;
6944 uint64_t r64_frac;
6946 /* Deal with any special cases */
6947 if (float64_is_any_nan(f64)) {
6948 float64 nan = f64;
6949 if (float64_is_signaling_nan(f64)) {
6950 float_raise(float_flag_invalid, fpst);
6951 nan = float64_maybe_silence_nan(f64);
6953 if (fpst->default_nan_mode) {
6954 nan = float64_default_nan;
6956 return nan;
6957 } else if (float64_is_infinity(f64)) {
6958 return float64_set_sign(float64_zero, float64_is_neg(f64));
6959 } else if (float64_is_zero(f64)) {
6960 float_raise(float_flag_divbyzero, fpst);
6961 return float64_set_sign(float64_infinity, float64_is_neg(f64));
6962 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
6963 /* Abs(value) < 2.0^-1024 */
6964 float_raise(float_flag_overflow | float_flag_inexact, fpst);
6965 if (round_to_inf(fpst, f64_sbit)) {
6966 return float64_set_sign(float64_infinity, float64_is_neg(f64));
6967 } else {
6968 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
6970 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
6971 float_raise(float_flag_underflow, fpst);
6972 return float64_set_sign(float64_zero, float64_is_neg(f64));
6975 r64 = call_recip_estimate(f64, 2045, fpst);
6976 r64_val = float64_val(r64);
6977 r64_exp = extract64(r64_val, 52, 11);
6978 r64_frac = extract64(r64_val, 0, 52);
6980 /* result = sign : result_exp<10:0> : fraction<51:0> */
6981 return make_float64(f64_sbit |
6982 ((r64_exp & 0x7ff) << 52) |
6983 r64_frac);
6986 /* The algorithm that must be used to calculate the estimate
6987 * is specified by the ARM ARM.
6989 static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
6991 /* These calculations mustn't set any fp exception flags,
6992 * so we use a local copy of the fp_status.
6994 float_status dummy_status = *real_fp_status;
6995 float_status *s = &dummy_status;
6996 float64 q;
6997 int64_t q_int;
6999 if (float64_lt(a, float64_half, s)) {
7000 /* range 0.25 <= a < 0.5 */
7002 /* a in units of 1/512 rounded down */
7003 /* q0 = (int)(a * 512.0); */
7004 q = float64_mul(float64_512, a, s);
7005 q_int = float64_to_int64_round_to_zero(q, s);
7007 /* reciprocal root r */
7008 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
7009 q = int64_to_float64(q_int, s);
7010 q = float64_add(q, float64_half, s);
7011 q = float64_div(q, float64_512, s);
7012 q = float64_sqrt(q, s);
7013 q = float64_div(float64_one, q, s);
7014 } else {
7015 /* range 0.5 <= a < 1.0 */
7017 /* a in units of 1/256 rounded down */
7018 /* q1 = (int)(a * 256.0); */
7019 q = float64_mul(float64_256, a, s);
7020 int64_t q_int = float64_to_int64_round_to_zero(q, s);
7022 /* reciprocal root r */
7023 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
7024 q = int64_to_float64(q_int, s);
7025 q = float64_add(q, float64_half, s);
7026 q = float64_div(q, float64_256, s);
7027 q = float64_sqrt(q, s);
7028 q = float64_div(float64_one, q, s);
7030 /* r in units of 1/256 rounded to nearest */
7031 /* s = (int)(256.0 * r + 0.5); */
7033 q = float64_mul(q, float64_256,s );
7034 q = float64_add(q, float64_half, s);
7035 q_int = float64_to_int64_round_to_zero(q, s);
7037 /* return (double)s / 256.0;*/
7038 return float64_div(int64_to_float64(q_int, s), float64_256, s);
7041 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
7043 float_status *s = fpstp;
7044 float32 f32 = float32_squash_input_denormal(input, s);
7045 uint32_t val = float32_val(f32);
7046 uint32_t f32_sbit = 0x80000000 & val;
7047 int32_t f32_exp = extract32(val, 23, 8);
7048 uint32_t f32_frac = extract32(val, 0, 23);
7049 uint64_t f64_frac;
7050 uint64_t val64;
7051 int result_exp;
7052 float64 f64;
7054 if (float32_is_any_nan(f32)) {
7055 float32 nan = f32;
7056 if (float32_is_signaling_nan(f32)) {
7057 float_raise(float_flag_invalid, s);
7058 nan = float32_maybe_silence_nan(f32);
7060 if (s->default_nan_mode) {
7061 nan = float32_default_nan;
7063 return nan;
7064 } else if (float32_is_zero(f32)) {
7065 float_raise(float_flag_divbyzero, s);
7066 return float32_set_sign(float32_infinity, float32_is_neg(f32));
7067 } else if (float32_is_neg(f32)) {
7068 float_raise(float_flag_invalid, s);
7069 return float32_default_nan;
7070 } else if (float32_is_infinity(f32)) {
7071 return float32_zero;
7074 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
7075 * preserving the parity of the exponent. */
7077 f64_frac = ((uint64_t) f32_frac) << 29;
7078 if (f32_exp == 0) {
7079 while (extract64(f64_frac, 51, 1) == 0) {
7080 f64_frac = f64_frac << 1;
7081 f32_exp = f32_exp-1;
7083 f64_frac = extract64(f64_frac, 0, 51) << 1;
7086 if (extract64(f32_exp, 0, 1) == 0) {
7087 f64 = make_float64(((uint64_t) f32_sbit) << 32
7088 | (0x3feULL << 52)
7089 | f64_frac);
7090 } else {
7091 f64 = make_float64(((uint64_t) f32_sbit) << 32
7092 | (0x3fdULL << 52)
7093 | f64_frac);
7096 result_exp = (380 - f32_exp) / 2;
7098 f64 = recip_sqrt_estimate(f64, s);
7100 val64 = float64_val(f64);
7102 val = ((result_exp & 0xff) << 23)
7103 | ((val64 >> 29) & 0x7fffff);
7104 return make_float32(val);
7107 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
7109 float_status *s = fpstp;
7110 float64 f64 = float64_squash_input_denormal(input, s);
7111 uint64_t val = float64_val(f64);
7112 uint64_t f64_sbit = 0x8000000000000000ULL & val;
7113 int64_t f64_exp = extract64(val, 52, 11);
7114 uint64_t f64_frac = extract64(val, 0, 52);
7115 int64_t result_exp;
7116 uint64_t result_frac;
7118 if (float64_is_any_nan(f64)) {
7119 float64 nan = f64;
7120 if (float64_is_signaling_nan(f64)) {
7121 float_raise(float_flag_invalid, s);
7122 nan = float64_maybe_silence_nan(f64);
7124 if (s->default_nan_mode) {
7125 nan = float64_default_nan;
7127 return nan;
7128 } else if (float64_is_zero(f64)) {
7129 float_raise(float_flag_divbyzero, s);
7130 return float64_set_sign(float64_infinity, float64_is_neg(f64));
7131 } else if (float64_is_neg(f64)) {
7132 float_raise(float_flag_invalid, s);
7133 return float64_default_nan;
7134 } else if (float64_is_infinity(f64)) {
7135 return float64_zero;
7138 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
7139 * preserving the parity of the exponent. */
7141 if (f64_exp == 0) {
7142 while (extract64(f64_frac, 51, 1) == 0) {
7143 f64_frac = f64_frac << 1;
7144 f64_exp = f64_exp - 1;
7146 f64_frac = extract64(f64_frac, 0, 51) << 1;
7149 if (extract64(f64_exp, 0, 1) == 0) {
7150 f64 = make_float64(f64_sbit
7151 | (0x3feULL << 52)
7152 | f64_frac);
7153 } else {
7154 f64 = make_float64(f64_sbit
7155 | (0x3fdULL << 52)
7156 | f64_frac);
7159 result_exp = (3068 - f64_exp) / 2;
7161 f64 = recip_sqrt_estimate(f64, s);
7163 result_frac = extract64(float64_val(f64), 0, 52);
7165 return make_float64(f64_sbit |
7166 ((result_exp & 0x7ff) << 52) |
7167 result_frac);
7170 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
7172 float_status *s = fpstp;
7173 float64 f64;
7175 if ((a & 0x80000000) == 0) {
7176 return 0xffffffff;
7179 f64 = make_float64((0x3feULL << 52)
7180 | ((int64_t)(a & 0x7fffffff) << 21));
7182 f64 = recip_estimate(f64, s);
7184 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
7187 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
7189 float_status *fpst = fpstp;
7190 float64 f64;
7192 if ((a & 0xc0000000) == 0) {
7193 return 0xffffffff;
7196 if (a & 0x80000000) {
7197 f64 = make_float64((0x3feULL << 52)
7198 | ((uint64_t)(a & 0x7fffffff) << 21));
7199 } else { /* bits 31-30 == '01' */
7200 f64 = make_float64((0x3fdULL << 52)
7201 | ((uint64_t)(a & 0x3fffffff) << 22));
7204 f64 = recip_sqrt_estimate(f64, fpst);
7206 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
7209 /* VFPv4 fused multiply-accumulate */
7210 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
7212 float_status *fpst = fpstp;
7213 return float32_muladd(a, b, c, 0, fpst);
7216 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
7218 float_status *fpst = fpstp;
7219 return float64_muladd(a, b, c, 0, fpst);
7222 /* ARMv8 round to integral */
7223 float32 HELPER(rints_exact)(float32 x, void *fp_status)
7225 return float32_round_to_int(x, fp_status);
7228 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
7230 return float64_round_to_int(x, fp_status);
7233 float32 HELPER(rints)(float32 x, void *fp_status)
7235 int old_flags = get_float_exception_flags(fp_status), new_flags;
7236 float32 ret;
7238 ret = float32_round_to_int(x, fp_status);
7240 /* Suppress any inexact exceptions the conversion produced */
7241 if (!(old_flags & float_flag_inexact)) {
7242 new_flags = get_float_exception_flags(fp_status);
7243 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
7246 return ret;
7249 float64 HELPER(rintd)(float64 x, void *fp_status)
7251 int old_flags = get_float_exception_flags(fp_status), new_flags;
7252 float64 ret;
7254 ret = float64_round_to_int(x, fp_status);
7256 new_flags = get_float_exception_flags(fp_status);
7258 /* Suppress any inexact exceptions the conversion produced */
7259 if (!(old_flags & float_flag_inexact)) {
7260 new_flags = get_float_exception_flags(fp_status);
7261 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
7264 return ret;
7267 /* Convert ARM rounding mode to softfloat */
7268 int arm_rmode_to_sf(int rmode)
7270 switch (rmode) {
7271 case FPROUNDING_TIEAWAY:
7272 rmode = float_round_ties_away;
7273 break;
7274 case FPROUNDING_ODD:
7275 /* FIXME: add support for TIEAWAY and ODD */
7276 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
7277 rmode);
7278 case FPROUNDING_TIEEVEN:
7279 default:
7280 rmode = float_round_nearest_even;
7281 break;
7282 case FPROUNDING_POSINF:
7283 rmode = float_round_up;
7284 break;
7285 case FPROUNDING_NEGINF:
7286 rmode = float_round_down;
7287 break;
7288 case FPROUNDING_ZERO:
7289 rmode = float_round_to_zero;
7290 break;
7292 return rmode;
7295 /* CRC helpers.
7296 * The upper bytes of val (above the number specified by 'bytes') must have
7297 * been zeroed out by the caller.
7299 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
7301 uint8_t buf[4];
7303 stl_le_p(buf, val);
7305 /* zlib crc32 converts the accumulator and output to one's complement. */
7306 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
7309 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
7311 uint8_t buf[4];
7313 stl_le_p(buf, val);
7315 /* Linux crc32c converts the output to one's complement. */
7316 return crc32c(acc, buf, bytes) ^ 0xffffffff;