target-arm: A64: Emulate the HVC insn
[qemu/ar7.git] / target-arm / helper.c
blob4d5a65329cb8c44874530106b61a042c82772075
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, int is_user,
17 hwaddr *phys_ptr, 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 if (cpreg_field_is_64bit(ri)) {
123 return CPREG_FIELD64(env, ri);
124 } else {
125 return CPREG_FIELD32(env, ri);
129 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
130 uint64_t value)
132 if (cpreg_field_is_64bit(ri)) {
133 CPREG_FIELD64(env, ri) = value;
134 } else {
135 CPREG_FIELD32(env, ri) = value;
139 static uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
141 /* Raw read of a coprocessor register (as needed for migration, etc). */
142 if (ri->type & ARM_CP_CONST) {
143 return ri->resetvalue;
144 } else if (ri->raw_readfn) {
145 return ri->raw_readfn(env, ri);
146 } else if (ri->readfn) {
147 return ri->readfn(env, ri);
148 } else {
149 return raw_read(env, ri);
153 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
154 uint64_t v)
156 /* Raw write of a coprocessor register (as needed for migration, etc).
157 * Note that constant registers are treated as write-ignored; the
158 * caller should check for success by whether a readback gives the
159 * value written.
161 if (ri->type & ARM_CP_CONST) {
162 return;
163 } else if (ri->raw_writefn) {
164 ri->raw_writefn(env, ri, v);
165 } else if (ri->writefn) {
166 ri->writefn(env, ri, v);
167 } else {
168 raw_write(env, ri, v);
172 bool write_cpustate_to_list(ARMCPU *cpu)
174 /* Write the coprocessor state from cpu->env to the (index,value) list. */
175 int i;
176 bool ok = true;
178 for (i = 0; i < cpu->cpreg_array_len; i++) {
179 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
180 const ARMCPRegInfo *ri;
182 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
183 if (!ri) {
184 ok = false;
185 continue;
187 if (ri->type & ARM_CP_NO_MIGRATE) {
188 continue;
190 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
192 return ok;
195 bool write_list_to_cpustate(ARMCPU *cpu)
197 int i;
198 bool ok = true;
200 for (i = 0; i < cpu->cpreg_array_len; i++) {
201 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
202 uint64_t v = cpu->cpreg_values[i];
203 const ARMCPRegInfo *ri;
205 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
206 if (!ri) {
207 ok = false;
208 continue;
210 if (ri->type & ARM_CP_NO_MIGRATE) {
211 continue;
213 /* Write value and confirm it reads back as written
214 * (to catch read-only registers and partially read-only
215 * registers where the incoming migration value doesn't match)
217 write_raw_cp_reg(&cpu->env, ri, v);
218 if (read_raw_cp_reg(&cpu->env, ri) != v) {
219 ok = false;
222 return ok;
225 static void add_cpreg_to_list(gpointer key, gpointer opaque)
227 ARMCPU *cpu = opaque;
228 uint64_t regidx;
229 const ARMCPRegInfo *ri;
231 regidx = *(uint32_t *)key;
232 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
234 if (!(ri->type & ARM_CP_NO_MIGRATE)) {
235 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
236 /* The value array need not be initialized at this point */
237 cpu->cpreg_array_len++;
241 static void count_cpreg(gpointer key, gpointer opaque)
243 ARMCPU *cpu = opaque;
244 uint64_t regidx;
245 const ARMCPRegInfo *ri;
247 regidx = *(uint32_t *)key;
248 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
250 if (!(ri->type & ARM_CP_NO_MIGRATE)) {
251 cpu->cpreg_array_len++;
255 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
257 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
258 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
260 if (aidx > bidx) {
261 return 1;
263 if (aidx < bidx) {
264 return -1;
266 return 0;
269 static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata)
271 GList **plist = udata;
273 *plist = g_list_prepend(*plist, key);
276 void init_cpreg_list(ARMCPU *cpu)
278 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
279 * Note that we require cpreg_tuples[] to be sorted by key ID.
281 GList *keys = NULL;
282 int arraylen;
284 g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys);
286 keys = g_list_sort(keys, cpreg_key_compare);
288 cpu->cpreg_array_len = 0;
290 g_list_foreach(keys, count_cpreg, cpu);
292 arraylen = cpu->cpreg_array_len;
293 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
294 cpu->cpreg_values = g_new(uint64_t, arraylen);
295 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
296 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
297 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
298 cpu->cpreg_array_len = 0;
300 g_list_foreach(keys, add_cpreg_to_list, cpu);
302 assert(cpu->cpreg_array_len == arraylen);
304 g_list_free(keys);
307 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
309 ARMCPU *cpu = arm_env_get_cpu(env);
311 raw_write(env, ri, value);
312 tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
315 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
317 ARMCPU *cpu = arm_env_get_cpu(env);
319 if (raw_read(env, ri) != value) {
320 /* Unlike real hardware the qemu TLB uses virtual addresses,
321 * not modified virtual addresses, so this causes a TLB flush.
323 tlb_flush(CPU(cpu), 1);
324 raw_write(env, ri, value);
328 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
329 uint64_t value)
331 ARMCPU *cpu = arm_env_get_cpu(env);
333 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU)
334 && !extended_addresses_enabled(env)) {
335 /* For VMSA (when not using the LPAE long descriptor page table
336 * format) this register includes the ASID, so do a TLB flush.
337 * For PMSA it is purely a process ID and no action is needed.
339 tlb_flush(CPU(cpu), 1);
341 raw_write(env, ri, value);
344 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
345 uint64_t value)
347 /* Invalidate all (TLBIALL) */
348 ARMCPU *cpu = arm_env_get_cpu(env);
350 tlb_flush(CPU(cpu), 1);
353 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
354 uint64_t value)
356 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
357 ARMCPU *cpu = arm_env_get_cpu(env);
359 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
362 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
363 uint64_t value)
365 /* Invalidate by ASID (TLBIASID) */
366 ARMCPU *cpu = arm_env_get_cpu(env);
368 tlb_flush(CPU(cpu), value == 0);
371 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
372 uint64_t value)
374 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
375 ARMCPU *cpu = arm_env_get_cpu(env);
377 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
380 /* IS variants of TLB operations must affect all cores */
381 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
382 uint64_t value)
384 CPUState *other_cs;
386 CPU_FOREACH(other_cs) {
387 tlb_flush(other_cs, 1);
391 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
392 uint64_t value)
394 CPUState *other_cs;
396 CPU_FOREACH(other_cs) {
397 tlb_flush(other_cs, value == 0);
401 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
402 uint64_t value)
404 CPUState *other_cs;
406 CPU_FOREACH(other_cs) {
407 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
411 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
412 uint64_t value)
414 CPUState *other_cs;
416 CPU_FOREACH(other_cs) {
417 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
421 static const ARMCPRegInfo cp_reginfo[] = {
422 { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0,
423 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
424 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
425 { .name = "CONTEXTIDR", .state = ARM_CP_STATE_BOTH,
426 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
427 .access = PL1_RW,
428 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el1),
429 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
430 REGINFO_SENTINEL
433 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
434 /* NB: Some of these registers exist in v8 but with more precise
435 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
437 /* MMU Domain access control / MPU write buffer control */
438 { .name = "DACR", .cp = 15,
439 .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
440 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
441 .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
442 /* ??? This covers not just the impdef TLB lockdown registers but also
443 * some v7VMSA registers relating to TEX remap, so it is overly broad.
445 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
446 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
447 /* Cache maintenance ops; some of this space may be overridden later. */
448 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
449 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
450 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
451 REGINFO_SENTINEL
454 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
455 /* Not all pre-v6 cores implemented this WFI, so this is slightly
456 * over-broad.
458 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
459 .access = PL1_W, .type = ARM_CP_WFI },
460 REGINFO_SENTINEL
463 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
464 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
465 * is UNPREDICTABLE; we choose to NOP as most implementations do).
467 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
468 .access = PL1_W, .type = ARM_CP_WFI },
469 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
470 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
471 * OMAPCP will override this space.
473 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
474 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
475 .resetvalue = 0 },
476 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
477 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
478 .resetvalue = 0 },
479 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
480 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
481 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
482 .resetvalue = 0 },
483 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
484 * implementing it as RAZ means the "debug architecture version" bits
485 * will read as a reserved value, which should cause Linux to not try
486 * to use the debug hardware.
488 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
489 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
490 /* MMU TLB control. Note that the wildcarding means we cover not just
491 * the unified TLB ops but also the dside/iside/inner-shareable variants.
493 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
494 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
495 .type = ARM_CP_NO_MIGRATE },
496 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
497 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
498 .type = ARM_CP_NO_MIGRATE },
499 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
500 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
501 .type = ARM_CP_NO_MIGRATE },
502 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
503 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
504 .type = ARM_CP_NO_MIGRATE },
505 REGINFO_SENTINEL
508 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
509 uint64_t value)
511 uint32_t mask = 0;
513 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
514 if (!arm_feature(env, ARM_FEATURE_V8)) {
515 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
516 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
517 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
519 if (arm_feature(env, ARM_FEATURE_VFP)) {
520 /* VFP coprocessor: cp10 & cp11 [23:20] */
521 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
523 if (!arm_feature(env, ARM_FEATURE_NEON)) {
524 /* ASEDIS [31] bit is RAO/WI */
525 value |= (1 << 31);
528 /* VFPv3 and upwards with NEON implement 32 double precision
529 * registers (D0-D31).
531 if (!arm_feature(env, ARM_FEATURE_NEON) ||
532 !arm_feature(env, ARM_FEATURE_VFP3)) {
533 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
534 value |= (1 << 30);
537 value &= mask;
539 env->cp15.c1_coproc = value;
542 static const ARMCPRegInfo v6_cp_reginfo[] = {
543 /* prefetch by MVA in v6, NOP in v7 */
544 { .name = "MVA_prefetch",
545 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
546 .access = PL1_W, .type = ARM_CP_NOP },
547 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
548 .access = PL0_W, .type = ARM_CP_NOP },
549 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
550 .access = PL0_W, .type = ARM_CP_NOP },
551 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
552 .access = PL0_W, .type = ARM_CP_NOP },
553 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
554 .access = PL1_RW,
555 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[1]),
556 .resetvalue = 0, },
557 /* Watchpoint Fault Address Register : should actually only be present
558 * for 1136, 1176, 11MPCore.
560 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
561 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
562 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
563 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
564 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
565 .resetvalue = 0, .writefn = cpacr_write },
566 REGINFO_SENTINEL
569 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri)
571 /* Performance monitor registers user accessibility is controlled
572 * by PMUSERENR.
574 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
575 return CP_ACCESS_TRAP;
577 return CP_ACCESS_OK;
580 #ifndef CONFIG_USER_ONLY
582 static inline bool arm_ccnt_enabled(CPUARMState *env)
584 /* This does not support checking PMCCFILTR_EL0 register */
586 if (!(env->cp15.c9_pmcr & PMCRE)) {
587 return false;
590 return true;
593 void pmccntr_sync(CPUARMState *env)
595 uint64_t temp_ticks;
597 temp_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
598 get_ticks_per_sec(), 1000000);
600 if (env->cp15.c9_pmcr & PMCRD) {
601 /* Increment once every 64 processor clock cycles */
602 temp_ticks /= 64;
605 if (arm_ccnt_enabled(env)) {
606 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
610 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
611 uint64_t value)
613 pmccntr_sync(env);
615 if (value & PMCRC) {
616 /* The counter has been reset */
617 env->cp15.c15_ccnt = 0;
620 /* only the DP, X, D and E bits are writable */
621 env->cp15.c9_pmcr &= ~0x39;
622 env->cp15.c9_pmcr |= (value & 0x39);
624 pmccntr_sync(env);
627 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
629 uint64_t total_ticks;
631 if (!arm_ccnt_enabled(env)) {
632 /* Counter is disabled, do not change value */
633 return env->cp15.c15_ccnt;
636 total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
637 get_ticks_per_sec(), 1000000);
639 if (env->cp15.c9_pmcr & PMCRD) {
640 /* Increment once every 64 processor clock cycles */
641 total_ticks /= 64;
643 return total_ticks - env->cp15.c15_ccnt;
646 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
647 uint64_t value)
649 uint64_t total_ticks;
651 if (!arm_ccnt_enabled(env)) {
652 /* Counter is disabled, set the absolute value */
653 env->cp15.c15_ccnt = value;
654 return;
657 total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
658 get_ticks_per_sec(), 1000000);
660 if (env->cp15.c9_pmcr & PMCRD) {
661 /* Increment once every 64 processor clock cycles */
662 total_ticks /= 64;
664 env->cp15.c15_ccnt = total_ticks - value;
667 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
668 uint64_t value)
670 uint64_t cur_val = pmccntr_read(env, NULL);
672 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
675 #else /* CONFIG_USER_ONLY */
677 void pmccntr_sync(CPUARMState *env)
681 #endif
683 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
684 uint64_t value)
686 pmccntr_sync(env);
687 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
688 pmccntr_sync(env);
691 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
692 uint64_t value)
694 value &= (1 << 31);
695 env->cp15.c9_pmcnten |= value;
698 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
699 uint64_t value)
701 value &= (1 << 31);
702 env->cp15.c9_pmcnten &= ~value;
705 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
706 uint64_t value)
708 env->cp15.c9_pmovsr &= ~value;
711 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
712 uint64_t value)
714 env->cp15.c9_pmxevtyper = value & 0xff;
717 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
718 uint64_t value)
720 env->cp15.c9_pmuserenr = value & 1;
723 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
724 uint64_t value)
726 /* We have no event counters so only the C bit can be changed */
727 value &= (1 << 31);
728 env->cp15.c9_pminten |= value;
731 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
732 uint64_t value)
734 value &= (1 << 31);
735 env->cp15.c9_pminten &= ~value;
738 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
739 uint64_t value)
741 /* Note that even though the AArch64 view of this register has bits
742 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
743 * architectural requirements for bits which are RES0 only in some
744 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
745 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
747 raw_write(env, ri, value & ~0x1FULL);
750 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
752 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
753 * For bits that vary between AArch32/64, code needs to check the
754 * current execution mode before directly using the feature bit.
756 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
758 if (!arm_feature(env, ARM_FEATURE_EL2)) {
759 valid_mask &= ~SCR_HCE;
761 /* On ARMv7, SMD (or SCD as it is called in v7) is only
762 * supported if EL2 exists. The bit is UNK/SBZP when
763 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
764 * when EL2 is unavailable.
766 if (arm_feature(env, ARM_FEATURE_V7)) {
767 valid_mask &= ~SCR_SMD;
771 /* Clear all-context RES0 bits. */
772 value &= valid_mask;
773 raw_write(env, ri, value);
776 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
778 ARMCPU *cpu = arm_env_get_cpu(env);
779 return cpu->ccsidr[env->cp15.c0_cssel];
782 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
783 uint64_t value)
785 raw_write(env, ri, value & 0xf);
788 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
790 CPUState *cs = ENV_GET_CPU(env);
791 uint64_t ret = 0;
793 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
794 ret |= CPSR_I;
796 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
797 ret |= CPSR_F;
799 /* External aborts are not possible in QEMU so A bit is always clear */
800 return ret;
803 static const ARMCPRegInfo v7_cp_reginfo[] = {
804 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
805 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
806 .access = PL1_W, .type = ARM_CP_NOP },
807 /* Performance monitors are implementation defined in v7,
808 * but with an ARM recommended set of registers, which we
809 * follow (although we don't actually implement any counters)
811 * Performance registers fall into three categories:
812 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
813 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
814 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
815 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
816 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
818 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
819 .access = PL0_RW, .type = ARM_CP_NO_MIGRATE,
820 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
821 .writefn = pmcntenset_write,
822 .accessfn = pmreg_access,
823 .raw_writefn = raw_write },
824 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
825 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
826 .access = PL0_RW, .accessfn = pmreg_access,
827 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
828 .writefn = pmcntenset_write, .raw_writefn = raw_write },
829 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
830 .access = PL0_RW,
831 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
832 .accessfn = pmreg_access,
833 .writefn = pmcntenclr_write,
834 .type = ARM_CP_NO_MIGRATE },
835 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
836 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
837 .access = PL0_RW, .accessfn = pmreg_access,
838 .type = ARM_CP_NO_MIGRATE,
839 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
840 .writefn = pmcntenclr_write },
841 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
842 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
843 .accessfn = pmreg_access,
844 .writefn = pmovsr_write,
845 .raw_writefn = raw_write },
846 /* Unimplemented so WI. */
847 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
848 .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
849 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
850 * We choose to RAZ/WI.
852 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
853 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
854 .accessfn = pmreg_access },
855 #ifndef CONFIG_USER_ONLY
856 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
857 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
858 .readfn = pmccntr_read, .writefn = pmccntr_write32,
859 .accessfn = pmreg_access },
860 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
861 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
862 .access = PL0_RW, .accessfn = pmreg_access,
863 .type = ARM_CP_IO,
864 .readfn = pmccntr_read, .writefn = pmccntr_write, },
865 #endif
866 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
867 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
868 .writefn = pmccfiltr_write,
869 .access = PL0_RW, .accessfn = pmreg_access,
870 .type = ARM_CP_IO,
871 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
872 .resetvalue = 0, },
873 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
874 .access = PL0_RW,
875 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
876 .accessfn = pmreg_access, .writefn = pmxevtyper_write,
877 .raw_writefn = raw_write },
878 /* Unimplemented, RAZ/WI. */
879 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
880 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
881 .accessfn = pmreg_access },
882 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
883 .access = PL0_R | PL1_RW,
884 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
885 .resetvalue = 0,
886 .writefn = pmuserenr_write, .raw_writefn = raw_write },
887 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
888 .access = PL1_RW,
889 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
890 .resetvalue = 0,
891 .writefn = pmintenset_write, .raw_writefn = raw_write },
892 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
893 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
894 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
895 .resetvalue = 0, .writefn = pmintenclr_write, },
896 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
897 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
898 .access = PL1_RW, .writefn = vbar_write,
899 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[1]),
900 .resetvalue = 0 },
901 { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
902 .access = PL1_RW, .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
903 .resetvalue = 0, .writefn = scr_write },
904 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
905 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
906 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
907 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
908 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
909 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c0_cssel),
910 .writefn = csselr_write, .resetvalue = 0 },
911 /* Auxiliary ID register: this actually has an IMPDEF value but for now
912 * just RAZ for all cores:
914 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
915 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
916 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
917 /* Auxiliary fault status registers: these also are IMPDEF, and we
918 * choose to RAZ/WI for all cores.
920 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
921 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
922 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
923 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
924 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
925 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
926 /* MAIR can just read-as-written because we don't implement caches
927 * and so don't need to care about memory attributes.
929 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
930 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
931 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el1),
932 .resetvalue = 0 },
933 /* For non-long-descriptor page tables these are PRRR and NMRR;
934 * regardless they still act as reads-as-written for QEMU.
935 * The override is necessary because of the overly-broad TLB_LOCKDOWN
936 * definition.
938 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
939 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
940 .fieldoffset = offsetoflow32(CPUARMState, cp15.mair_el1),
941 .resetfn = arm_cp_reset_ignore },
942 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
943 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
944 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el1),
945 .resetfn = arm_cp_reset_ignore },
946 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
947 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
948 .type = ARM_CP_NO_MIGRATE, .access = PL1_R, .readfn = isr_read },
949 /* 32 bit ITLB invalidates */
950 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
951 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write },
952 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
953 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
954 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
955 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write },
956 /* 32 bit DTLB invalidates */
957 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
958 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write },
959 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
960 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
961 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
962 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write },
963 /* 32 bit TLB invalidates */
964 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
965 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write },
966 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
967 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
968 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
969 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write },
970 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
971 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write },
972 REGINFO_SENTINEL
975 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
976 /* 32 bit TLB invalidates, Inner Shareable */
977 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
978 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_is_write },
979 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
980 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_is_write },
981 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
982 .type = ARM_CP_NO_MIGRATE, .access = PL1_W,
983 .writefn = tlbiasid_is_write },
984 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
985 .type = ARM_CP_NO_MIGRATE, .access = PL1_W,
986 .writefn = tlbimvaa_is_write },
987 REGINFO_SENTINEL
990 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
991 uint64_t value)
993 value &= 1;
994 env->teecr = value;
997 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri)
999 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
1000 return CP_ACCESS_TRAP;
1002 return CP_ACCESS_OK;
1005 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1006 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1007 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1008 .resetvalue = 0,
1009 .writefn = teecr_write },
1010 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1011 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
1012 .accessfn = teehbr_access, .resetvalue = 0 },
1013 REGINFO_SENTINEL
1016 static const ARMCPRegInfo v6k_cp_reginfo[] = {
1017 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1018 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1019 .access = PL0_RW,
1020 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el0), .resetvalue = 0 },
1021 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1022 .access = PL0_RW,
1023 .fieldoffset = offsetoflow32(CPUARMState, cp15.tpidr_el0),
1024 .resetfn = arm_cp_reset_ignore },
1025 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1026 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1027 .access = PL0_R|PL1_W,
1028 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el0), .resetvalue = 0 },
1029 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1030 .access = PL0_R|PL1_W,
1031 .fieldoffset = offsetoflow32(CPUARMState, cp15.tpidrro_el0),
1032 .resetfn = arm_cp_reset_ignore },
1033 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_BOTH,
1034 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
1035 .access = PL1_RW,
1036 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el1), .resetvalue = 0 },
1037 REGINFO_SENTINEL
1040 #ifndef CONFIG_USER_ONLY
1042 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri)
1044 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
1045 if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
1046 return CP_ACCESS_TRAP;
1048 return CP_ACCESS_OK;
1051 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx)
1053 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
1054 if (arm_current_pl(env) == 0 &&
1055 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1056 return CP_ACCESS_TRAP;
1058 return CP_ACCESS_OK;
1061 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx)
1063 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1064 * EL0[PV]TEN is zero.
1066 if (arm_current_pl(env) == 0 &&
1067 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1068 return CP_ACCESS_TRAP;
1070 return CP_ACCESS_OK;
1073 static CPAccessResult gt_pct_access(CPUARMState *env,
1074 const ARMCPRegInfo *ri)
1076 return gt_counter_access(env, GTIMER_PHYS);
1079 static CPAccessResult gt_vct_access(CPUARMState *env,
1080 const ARMCPRegInfo *ri)
1082 return gt_counter_access(env, GTIMER_VIRT);
1085 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
1087 return gt_timer_access(env, GTIMER_PHYS);
1090 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
1092 return gt_timer_access(env, GTIMER_VIRT);
1095 static uint64_t gt_get_countervalue(CPUARMState *env)
1097 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
1100 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1102 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1104 if (gt->ctl & 1) {
1105 /* Timer enabled: calculate and set current ISTATUS, irq, and
1106 * reset timer to when ISTATUS next has to change
1108 uint64_t count = gt_get_countervalue(&cpu->env);
1109 /* Note that this must be unsigned 64 bit arithmetic: */
1110 int istatus = count >= gt->cval;
1111 uint64_t nexttick;
1113 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1114 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1115 (istatus && !(gt->ctl & 2)));
1116 if (istatus) {
1117 /* Next transition is when count rolls back over to zero */
1118 nexttick = UINT64_MAX;
1119 } else {
1120 /* Next transition is when we hit cval */
1121 nexttick = gt->cval;
1123 /* Note that the desired next expiry time might be beyond the
1124 * signed-64-bit range of a QEMUTimer -- in this case we just
1125 * set the timer for as far in the future as possible. When the
1126 * timer expires we will reset the timer for any remaining period.
1128 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1129 nexttick = INT64_MAX / GTIMER_SCALE;
1131 timer_mod(cpu->gt_timer[timeridx], nexttick);
1132 } else {
1133 /* Timer disabled: ISTATUS and timer output always clear */
1134 gt->ctl &= ~4;
1135 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
1136 timer_del(cpu->gt_timer[timeridx]);
1140 static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1142 ARMCPU *cpu = arm_env_get_cpu(env);
1143 int timeridx = ri->opc1 & 1;
1145 timer_del(cpu->gt_timer[timeridx]);
1148 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1150 return gt_get_countervalue(env);
1153 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1154 uint64_t value)
1156 int timeridx = ri->opc1 & 1;
1158 env->cp15.c14_timer[timeridx].cval = value;
1159 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1162 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1164 int timeridx = ri->crm & 1;
1166 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
1167 gt_get_countervalue(env));
1170 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1171 uint64_t value)
1173 int timeridx = ri->crm & 1;
1175 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
1176 + sextract64(value, 0, 32);
1177 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1180 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1181 uint64_t value)
1183 ARMCPU *cpu = arm_env_get_cpu(env);
1184 int timeridx = ri->crm & 1;
1185 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1187 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
1188 if ((oldval ^ value) & 1) {
1189 /* Enable toggled */
1190 gt_recalc_timer(cpu, timeridx);
1191 } else if ((oldval ^ value) & 2) {
1192 /* IMASK toggled: don't need to recalculate,
1193 * just set the interrupt line based on ISTATUS
1195 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1196 (oldval & 4) && !(value & 2));
1200 void arm_gt_ptimer_cb(void *opaque)
1202 ARMCPU *cpu = opaque;
1204 gt_recalc_timer(cpu, GTIMER_PHYS);
1207 void arm_gt_vtimer_cb(void *opaque)
1209 ARMCPU *cpu = opaque;
1211 gt_recalc_timer(cpu, GTIMER_VIRT);
1214 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1215 /* Note that CNTFRQ is purely reads-as-written for the benefit
1216 * of software; writing it doesn't actually change the timer frequency.
1217 * Our reset value matches the fixed frequency we implement the timer at.
1219 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
1220 .type = ARM_CP_NO_MIGRATE,
1221 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1222 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
1223 .resetfn = arm_cp_reset_ignore,
1225 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1226 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1227 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1228 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1229 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
1231 /* overall control: mostly access permissions */
1232 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1233 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
1234 .access = PL1_RW,
1235 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1236 .resetvalue = 0,
1238 /* per-timer control */
1239 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1240 .type = ARM_CP_IO | ARM_CP_NO_MIGRATE, .access = PL1_RW | PL0_R,
1241 .accessfn = gt_ptimer_access,
1242 .fieldoffset = offsetoflow32(CPUARMState,
1243 cp15.c14_timer[GTIMER_PHYS].ctl),
1244 .resetfn = arm_cp_reset_ignore,
1245 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1247 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1248 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
1249 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1250 .accessfn = gt_ptimer_access,
1251 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1252 .resetvalue = 0,
1253 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1255 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
1256 .type = ARM_CP_IO | ARM_CP_NO_MIGRATE, .access = PL1_RW | PL0_R,
1257 .accessfn = gt_vtimer_access,
1258 .fieldoffset = offsetoflow32(CPUARMState,
1259 cp15.c14_timer[GTIMER_VIRT].ctl),
1260 .resetfn = arm_cp_reset_ignore,
1261 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1263 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1264 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
1265 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1266 .accessfn = gt_vtimer_access,
1267 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1268 .resetvalue = 0,
1269 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1271 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1272 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1273 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
1274 .accessfn = gt_ptimer_access,
1275 .readfn = gt_tval_read, .writefn = gt_tval_write,
1277 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1278 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
1279 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
1280 .readfn = gt_tval_read, .writefn = gt_tval_write,
1282 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
1283 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
1284 .accessfn = gt_vtimer_access,
1285 .readfn = gt_tval_read, .writefn = gt_tval_write,
1287 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1288 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
1289 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
1290 .readfn = gt_tval_read, .writefn = gt_tval_write,
1292 /* The counter itself */
1293 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
1294 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
1295 .accessfn = gt_pct_access,
1296 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1298 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1299 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
1300 .access = PL0_R, .type = ARM_CP_NO_MIGRATE | ARM_CP_IO,
1301 .accessfn = gt_pct_access,
1302 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
1304 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
1305 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
1306 .accessfn = gt_vct_access,
1307 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1309 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1310 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
1311 .access = PL0_R, .type = ARM_CP_NO_MIGRATE | ARM_CP_IO,
1312 .accessfn = gt_vct_access,
1313 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
1315 /* Comparison value, indicating when the timer goes off */
1316 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
1317 .access = PL1_RW | PL0_R,
1318 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_MIGRATE,
1319 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1320 .accessfn = gt_ptimer_access, .resetfn = arm_cp_reset_ignore,
1321 .writefn = gt_cval_write, .raw_writefn = raw_write,
1323 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1324 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1325 .access = PL1_RW | PL0_R,
1326 .type = ARM_CP_IO,
1327 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1328 .resetvalue = 0, .accessfn = gt_vtimer_access,
1329 .writefn = gt_cval_write, .raw_writefn = raw_write,
1331 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1332 .access = PL1_RW | PL0_R,
1333 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_MIGRATE,
1334 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1335 .accessfn = gt_vtimer_access, .resetfn = arm_cp_reset_ignore,
1336 .writefn = gt_cval_write, .raw_writefn = raw_write,
1338 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1339 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1340 .access = PL1_RW | PL0_R,
1341 .type = ARM_CP_IO,
1342 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1343 .resetvalue = 0, .accessfn = gt_vtimer_access,
1344 .writefn = gt_cval_write, .raw_writefn = raw_write,
1346 REGINFO_SENTINEL
1349 #else
1350 /* In user-mode none of the generic timer registers are accessible,
1351 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
1352 * so instead just don't register any of them.
1354 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1355 REGINFO_SENTINEL
1358 #endif
1360 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1362 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1363 raw_write(env, ri, value);
1364 } else if (arm_feature(env, ARM_FEATURE_V7)) {
1365 raw_write(env, ri, value & 0xfffff6ff);
1366 } else {
1367 raw_write(env, ri, value & 0xfffff1ff);
1371 #ifndef CONFIG_USER_ONLY
1372 /* get_phys_addr() isn't present for user-mode-only targets */
1374 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri)
1376 if (ri->opc2 & 4) {
1377 /* Other states are only available with TrustZone; in
1378 * a non-TZ implementation these registers don't exist
1379 * at all, which is an Uncategorized trap. This underdecoding
1380 * is safe because the reginfo is NO_MIGRATE.
1382 return CP_ACCESS_TRAP_UNCATEGORIZED;
1384 return CP_ACCESS_OK;
1387 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1389 hwaddr phys_addr;
1390 target_ulong page_size;
1391 int prot;
1392 int ret, is_user = ri->opc2 & 2;
1393 int access_type = ri->opc2 & 1;
1395 ret = get_phys_addr(env, value, access_type, is_user,
1396 &phys_addr, &prot, &page_size);
1397 if (extended_addresses_enabled(env)) {
1398 /* ret is a DFSR/IFSR value for the long descriptor
1399 * translation table format, but with WnR always clear.
1400 * Convert it to a 64-bit PAR.
1402 uint64_t par64 = (1 << 11); /* LPAE bit always set */
1403 if (ret == 0) {
1404 par64 |= phys_addr & ~0xfffULL;
1405 /* We don't set the ATTR or SH fields in the PAR. */
1406 } else {
1407 par64 |= 1; /* F */
1408 par64 |= (ret & 0x3f) << 1; /* FS */
1409 /* Note that S2WLK and FSTAGE are always zero, because we don't
1410 * implement virtualization and therefore there can't be a stage 2
1411 * fault.
1414 env->cp15.par_el1 = par64;
1415 } else {
1416 /* ret is a DFSR/IFSR value for the short descriptor
1417 * translation table format (with WnR always clear).
1418 * Convert it to a 32-bit PAR.
1420 if (ret == 0) {
1421 /* We do not set any attribute bits in the PAR */
1422 if (page_size == (1 << 24)
1423 && arm_feature(env, ARM_FEATURE_V7)) {
1424 env->cp15.par_el1 = (phys_addr & 0xff000000) | 1 << 1;
1425 } else {
1426 env->cp15.par_el1 = phys_addr & 0xfffff000;
1428 } else {
1429 env->cp15.par_el1 = ((ret & (1 << 10)) >> 5) |
1430 ((ret & (1 << 12)) >> 6) |
1431 ((ret & 0xf) << 1) | 1;
1435 #endif
1437 static const ARMCPRegInfo vapa_cp_reginfo[] = {
1438 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1439 .access = PL1_RW, .resetvalue = 0,
1440 .fieldoffset = offsetoflow32(CPUARMState, cp15.par_el1),
1441 .writefn = par_write },
1442 #ifndef CONFIG_USER_ONLY
1443 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
1444 .access = PL1_W, .accessfn = ats_access,
1445 .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
1446 #endif
1447 REGINFO_SENTINEL
1450 /* Return basic MPU access permission bits. */
1451 static uint32_t simple_mpu_ap_bits(uint32_t val)
1453 uint32_t ret;
1454 uint32_t mask;
1455 int i;
1456 ret = 0;
1457 mask = 3;
1458 for (i = 0; i < 16; i += 2) {
1459 ret |= (val >> i) & mask;
1460 mask <<= 2;
1462 return ret;
1465 /* Pad basic MPU access permission bits to extended format. */
1466 static uint32_t extended_mpu_ap_bits(uint32_t val)
1468 uint32_t ret;
1469 uint32_t mask;
1470 int i;
1471 ret = 0;
1472 mask = 3;
1473 for (i = 0; i < 16; i += 2) {
1474 ret |= (val & mask) << i;
1475 mask <<= 2;
1477 return ret;
1480 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1481 uint64_t value)
1483 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
1486 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1488 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
1491 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1492 uint64_t value)
1494 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
1497 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1499 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
1502 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1503 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1504 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1505 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
1506 .resetvalue = 0,
1507 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
1508 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1509 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1510 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
1511 .resetvalue = 0,
1512 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
1513 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
1514 .access = PL1_RW,
1515 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
1516 .resetvalue = 0, },
1517 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
1518 .access = PL1_RW,
1519 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
1520 .resetvalue = 0, },
1521 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1522 .access = PL1_RW,
1523 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
1524 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1525 .access = PL1_RW,
1526 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
1527 /* Protection region base and size registers */
1528 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
1529 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1530 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
1531 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
1532 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1533 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
1534 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
1535 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1536 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
1537 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
1538 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1539 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
1540 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
1541 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1542 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
1543 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
1544 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1545 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
1546 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
1547 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1548 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
1549 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
1550 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1551 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
1552 REGINFO_SENTINEL
1555 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1556 uint64_t value)
1558 int maskshift = extract32(value, 0, 3);
1560 if (!arm_feature(env, ARM_FEATURE_V8)) {
1561 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
1562 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
1563 * using Long-desciptor translation table format */
1564 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
1565 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
1566 /* In an implementation that includes the Security Extensions
1567 * TTBCR has additional fields PD0 [4] and PD1 [5] for
1568 * Short-descriptor translation table format.
1570 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
1571 } else {
1572 value &= TTBCR_N;
1576 /* Note that we always calculate c2_mask and c2_base_mask, but
1577 * they are only used for short-descriptor tables (ie if EAE is 0);
1578 * for long-descriptor tables the TTBCR fields are used differently
1579 * and the c2_mask and c2_base_mask values are meaningless.
1581 raw_write(env, ri, value);
1582 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
1583 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
1586 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1587 uint64_t value)
1589 ARMCPU *cpu = arm_env_get_cpu(env);
1591 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1592 /* With LPAE the TTBCR could result in a change of ASID
1593 * via the TTBCR.A1 bit, so do a TLB flush.
1595 tlb_flush(CPU(cpu), 1);
1597 vmsa_ttbcr_raw_write(env, ri, value);
1600 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1602 env->cp15.c2_base_mask = 0xffffc000u;
1603 raw_write(env, ri, 0);
1604 env->cp15.c2_mask = 0;
1607 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
1608 uint64_t value)
1610 ARMCPU *cpu = arm_env_get_cpu(env);
1612 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
1613 tlb_flush(CPU(cpu), 1);
1614 raw_write(env, ri, value);
1617 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1618 uint64_t value)
1620 /* 64 bit accesses to the TTBRs can change the ASID and so we
1621 * must flush the TLB.
1623 if (cpreg_field_is_64bit(ri)) {
1624 ARMCPU *cpu = arm_env_get_cpu(env);
1626 tlb_flush(CPU(cpu), 1);
1628 raw_write(env, ri, value);
1631 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
1632 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1633 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1634 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
1635 .resetfn = arm_cp_reset_ignore, },
1636 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1637 .access = PL1_RW,
1638 .fieldoffset = offsetof(CPUARMState, cp15.ifsr_el2), .resetvalue = 0, },
1639 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
1640 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
1641 .access = PL1_RW,
1642 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
1643 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
1644 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1645 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el1),
1646 .writefn = vmsa_ttbr_write, .resetvalue = 0 },
1647 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
1648 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1649 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el1),
1650 .writefn = vmsa_ttbr_write, .resetvalue = 0 },
1651 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
1652 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1653 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
1654 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
1655 .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
1656 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1657 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, .writefn = vmsa_ttbcr_write,
1658 .resetfn = arm_cp_reset_ignore, .raw_writefn = vmsa_ttbcr_raw_write,
1659 .fieldoffset = offsetoflow32(CPUARMState, cp15.c2_control) },
1660 /* 64-bit FAR; this entry also gives us the AArch32 DFAR */
1661 { .name = "FAR_EL1", .state = ARM_CP_STATE_BOTH,
1662 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
1663 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
1664 .resetvalue = 0, },
1665 REGINFO_SENTINEL
1668 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
1669 uint64_t value)
1671 env->cp15.c15_ticonfig = value & 0xe7;
1672 /* The OS_TYPE bit in this register changes the reported CPUID! */
1673 env->cp15.c0_cpuid = (value & (1 << 5)) ?
1674 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1677 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1678 uint64_t value)
1680 env->cp15.c15_threadid = value & 0xffff;
1683 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
1684 uint64_t value)
1686 /* Wait-for-interrupt (deprecated) */
1687 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1690 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
1691 uint64_t value)
1693 /* On OMAP there are registers indicating the max/min index of dcache lines
1694 * containing a dirty line; cache flush operations have to reset these.
1696 env->cp15.c15_i_max = 0x000;
1697 env->cp15.c15_i_min = 0xff0;
1700 static const ARMCPRegInfo omap_cp_reginfo[] = {
1701 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
1702 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
1703 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
1704 .resetvalue = 0, },
1705 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
1706 .access = PL1_RW, .type = ARM_CP_NOP },
1707 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
1708 .access = PL1_RW,
1709 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
1710 .writefn = omap_ticonfig_write },
1711 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
1712 .access = PL1_RW,
1713 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
1714 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
1715 .access = PL1_RW, .resetvalue = 0xff0,
1716 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
1717 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
1718 .access = PL1_RW,
1719 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
1720 .writefn = omap_threadid_write },
1721 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
1722 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1723 .type = ARM_CP_NO_MIGRATE,
1724 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
1725 /* TODO: Peripheral port remap register:
1726 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
1727 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
1728 * when MMU is off.
1730 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
1731 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
1732 .type = ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE,
1733 .writefn = omap_cachemaint_write },
1734 { .name = "C9", .cp = 15, .crn = 9,
1735 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
1736 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1737 REGINFO_SENTINEL
1740 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1741 uint64_t value)
1743 env->cp15.c15_cpar = value & 0x3fff;
1746 static const ARMCPRegInfo xscale_cp_reginfo[] = {
1747 { .name = "XSCALE_CPAR",
1748 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1749 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
1750 .writefn = xscale_cpar_write, },
1751 { .name = "XSCALE_AUXCR",
1752 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
1753 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
1754 .resetvalue = 0, },
1755 /* XScale specific cache-lockdown: since we have no cache we NOP these
1756 * and hope the guest does not really rely on cache behaviour.
1758 { .name = "XSCALE_LOCK_ICACHE_LINE",
1759 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
1760 .access = PL1_W, .type = ARM_CP_NOP },
1761 { .name = "XSCALE_UNLOCK_ICACHE",
1762 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
1763 .access = PL1_W, .type = ARM_CP_NOP },
1764 { .name = "XSCALE_DCACHE_LOCK",
1765 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
1766 .access = PL1_RW, .type = ARM_CP_NOP },
1767 { .name = "XSCALE_UNLOCK_DCACHE",
1768 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
1769 .access = PL1_W, .type = ARM_CP_NOP },
1770 REGINFO_SENTINEL
1773 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
1774 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1775 * implementation of this implementation-defined space.
1776 * Ideally this should eventually disappear in favour of actually
1777 * implementing the correct behaviour for all cores.
1779 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1780 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1781 .access = PL1_RW,
1782 .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE | ARM_CP_OVERRIDE,
1783 .resetvalue = 0 },
1784 REGINFO_SENTINEL
1787 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
1788 /* Cache status: RAZ because we have no cache so it's always clean */
1789 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
1790 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1791 .resetvalue = 0 },
1792 REGINFO_SENTINEL
1795 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
1796 /* We never have a a block transfer operation in progress */
1797 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
1798 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1799 .resetvalue = 0 },
1800 /* The cache ops themselves: these all NOP for QEMU */
1801 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
1802 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1803 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
1804 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1805 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
1806 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1807 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
1808 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1809 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
1810 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1811 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
1812 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1813 REGINFO_SENTINEL
1816 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
1817 /* The cache test-and-clean instructions always return (1 << 30)
1818 * to indicate that there are no dirty cache lines.
1820 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
1821 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1822 .resetvalue = (1 << 30) },
1823 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
1824 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1825 .resetvalue = (1 << 30) },
1826 REGINFO_SENTINEL
1829 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
1830 /* Ignore ReadBuffer accesses */
1831 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
1832 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1833 .access = PL1_RW, .resetvalue = 0,
1834 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE },
1835 REGINFO_SENTINEL
1838 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1840 CPUState *cs = CPU(arm_env_get_cpu(env));
1841 uint32_t mpidr = cs->cpu_index;
1842 /* We don't support setting cluster ID ([8..11]) (known as Aff1
1843 * in later ARM ARM versions), or any of the higher affinity level fields,
1844 * so these bits always RAZ.
1846 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1847 mpidr |= (1U << 31);
1848 /* Cores which are uniprocessor (non-coherent)
1849 * but still implement the MP extensions set
1850 * bit 30. (For instance, A9UP.) However we do
1851 * not currently model any of those cores.
1854 return mpidr;
1857 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
1858 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
1859 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
1860 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE },
1861 REGINFO_SENTINEL
1864 static const ARMCPRegInfo lpae_cp_reginfo[] = {
1865 /* NOP AMAIR0/1: the override is because these clash with the rather
1866 * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
1868 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
1869 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
1870 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1871 .resetvalue = 0 },
1872 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
1873 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
1874 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1875 .resetvalue = 0 },
1876 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
1877 .access = PL1_RW, .type = ARM_CP_64BIT,
1878 .fieldoffset = offsetof(CPUARMState, cp15.par_el1), .resetvalue = 0 },
1879 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
1880 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE,
1881 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el1),
1882 .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
1883 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
1884 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE,
1885 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el1),
1886 .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
1887 REGINFO_SENTINEL
1890 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1892 return vfp_get_fpcr(env);
1895 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1896 uint64_t value)
1898 vfp_set_fpcr(env, value);
1901 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1903 return vfp_get_fpsr(env);
1906 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1907 uint64_t value)
1909 vfp_set_fpsr(env, value);
1912 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri)
1914 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UMA)) {
1915 return CP_ACCESS_TRAP;
1917 return CP_ACCESS_OK;
1920 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
1921 uint64_t value)
1923 env->daif = value & PSTATE_DAIF;
1926 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
1927 const ARMCPRegInfo *ri)
1929 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
1930 * SCTLR_EL1.UCI is set.
1932 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UCI)) {
1933 return CP_ACCESS_TRAP;
1935 return CP_ACCESS_OK;
1938 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
1939 * Page D4-1736 (DDI0487A.b)
1942 static void tlbi_aa64_va_write(CPUARMState *env, const ARMCPRegInfo *ri,
1943 uint64_t value)
1945 /* Invalidate by VA (AArch64 version) */
1946 ARMCPU *cpu = arm_env_get_cpu(env);
1947 uint64_t pageaddr = sextract64(value << 12, 0, 56);
1949 tlb_flush_page(CPU(cpu), pageaddr);
1952 static void tlbi_aa64_vaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
1953 uint64_t value)
1955 /* Invalidate by VA, all ASIDs (AArch64 version) */
1956 ARMCPU *cpu = arm_env_get_cpu(env);
1957 uint64_t pageaddr = sextract64(value << 12, 0, 56);
1959 tlb_flush_page(CPU(cpu), pageaddr);
1962 static void tlbi_aa64_asid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1963 uint64_t value)
1965 /* Invalidate by ASID (AArch64 version) */
1966 ARMCPU *cpu = arm_env_get_cpu(env);
1967 int asid = extract64(value, 48, 16);
1968 tlb_flush(CPU(cpu), asid == 0);
1971 static void tlbi_aa64_va_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
1972 uint64_t value)
1974 CPUState *other_cs;
1975 uint64_t pageaddr = sextract64(value << 12, 0, 56);
1977 CPU_FOREACH(other_cs) {
1978 tlb_flush_page(other_cs, pageaddr);
1982 static void tlbi_aa64_vaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
1983 uint64_t value)
1985 CPUState *other_cs;
1986 uint64_t pageaddr = sextract64(value << 12, 0, 56);
1988 CPU_FOREACH(other_cs) {
1989 tlb_flush_page(other_cs, pageaddr);
1993 static void tlbi_aa64_asid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
1994 uint64_t value)
1996 CPUState *other_cs;
1997 int asid = extract64(value, 48, 16);
1999 CPU_FOREACH(other_cs) {
2000 tlb_flush(other_cs, asid == 0);
2004 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri)
2006 /* We don't implement EL2, so the only control on DC ZVA is the
2007 * bit in the SCTLR which can prohibit access for EL0.
2009 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_DZE)) {
2010 return CP_ACCESS_TRAP;
2012 return CP_ACCESS_OK;
2015 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
2017 ARMCPU *cpu = arm_env_get_cpu(env);
2018 int dzp_bit = 1 << 4;
2020 /* DZP indicates whether DC ZVA access is allowed */
2021 if (aa64_zva_access(env, NULL) != CP_ACCESS_OK) {
2022 dzp_bit = 0;
2024 return cpu->dcz_blocksize | dzp_bit;
2027 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
2029 if (!(env->pstate & PSTATE_SP)) {
2030 /* Access to SP_EL0 is undefined if it's being used as
2031 * the stack pointer.
2033 return CP_ACCESS_TRAP_UNCATEGORIZED;
2035 return CP_ACCESS_OK;
2038 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
2040 return env->pstate & PSTATE_SP;
2043 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
2045 update_spsel(env, val);
2048 static const ARMCPRegInfo v8_cp_reginfo[] = {
2049 /* Minimal set of EL0-visible registers. This will need to be expanded
2050 * significantly for system emulation of AArch64 CPUs.
2052 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
2053 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
2054 .access = PL0_RW, .type = ARM_CP_NZCV },
2055 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
2056 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
2057 .type = ARM_CP_NO_MIGRATE,
2058 .access = PL0_RW, .accessfn = aa64_daif_access,
2059 .fieldoffset = offsetof(CPUARMState, daif),
2060 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
2061 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
2062 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
2063 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
2064 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
2065 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
2066 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
2067 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
2068 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
2069 .access = PL0_R, .type = ARM_CP_NO_MIGRATE,
2070 .readfn = aa64_dczid_read },
2071 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
2072 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
2073 .access = PL0_W, .type = ARM_CP_DC_ZVA,
2074 #ifndef CONFIG_USER_ONLY
2075 /* Avoid overhead of an access check that always passes in user-mode */
2076 .accessfn = aa64_zva_access,
2077 #endif
2079 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
2080 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
2081 .access = PL1_R, .type = ARM_CP_CURRENTEL },
2082 /* Cache ops: all NOPs since we don't emulate caches */
2083 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
2084 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2085 .access = PL1_W, .type = ARM_CP_NOP },
2086 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
2087 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2088 .access = PL1_W, .type = ARM_CP_NOP },
2089 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
2090 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
2091 .access = PL0_W, .type = ARM_CP_NOP,
2092 .accessfn = aa64_cacheop_access },
2093 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
2094 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
2095 .access = PL1_W, .type = ARM_CP_NOP },
2096 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
2097 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
2098 .access = PL1_W, .type = ARM_CP_NOP },
2099 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
2100 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
2101 .access = PL0_W, .type = ARM_CP_NOP,
2102 .accessfn = aa64_cacheop_access },
2103 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
2104 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
2105 .access = PL1_W, .type = ARM_CP_NOP },
2106 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
2107 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
2108 .access = PL0_W, .type = ARM_CP_NOP,
2109 .accessfn = aa64_cacheop_access },
2110 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
2111 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
2112 .access = PL0_W, .type = ARM_CP_NOP,
2113 .accessfn = aa64_cacheop_access },
2114 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
2115 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
2116 .access = PL1_W, .type = ARM_CP_NOP },
2117 /* TLBI operations */
2118 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
2119 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2120 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2121 .writefn = tlbiall_is_write },
2122 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
2123 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2124 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2125 .writefn = tlbi_aa64_va_is_write },
2126 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
2127 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2128 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2129 .writefn = tlbi_aa64_asid_is_write },
2130 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
2131 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2132 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2133 .writefn = tlbi_aa64_vaa_is_write },
2134 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
2135 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
2136 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2137 .writefn = tlbi_aa64_va_is_write },
2138 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
2139 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
2140 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2141 .writefn = tlbi_aa64_vaa_is_write },
2142 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
2143 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2144 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2145 .writefn = tlbiall_write },
2146 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
2147 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2148 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2149 .writefn = tlbi_aa64_va_write },
2150 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
2151 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2152 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2153 .writefn = tlbi_aa64_asid_write },
2154 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
2155 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2156 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2157 .writefn = tlbi_aa64_vaa_write },
2158 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
2159 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
2160 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2161 .writefn = tlbi_aa64_va_write },
2162 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
2163 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
2164 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2165 .writefn = tlbi_aa64_vaa_write },
2166 #ifndef CONFIG_USER_ONLY
2167 /* 64 bit address translation operations */
2168 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
2169 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
2170 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
2171 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
2172 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
2173 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
2174 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
2175 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
2176 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
2177 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
2178 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
2179 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
2180 #endif
2181 /* TLB invalidate last level of translation table walk */
2182 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
2183 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_is_write },
2184 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
2185 .type = ARM_CP_NO_MIGRATE, .access = PL1_W,
2186 .writefn = tlbimvaa_is_write },
2187 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
2188 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2189 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
2190 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write },
2191 /* 32 bit cache operations */
2192 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2193 .type = ARM_CP_NOP, .access = PL1_W },
2194 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
2195 .type = ARM_CP_NOP, .access = PL1_W },
2196 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2197 .type = ARM_CP_NOP, .access = PL1_W },
2198 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
2199 .type = ARM_CP_NOP, .access = PL1_W },
2200 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
2201 .type = ARM_CP_NOP, .access = PL1_W },
2202 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
2203 .type = ARM_CP_NOP, .access = PL1_W },
2204 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
2205 .type = ARM_CP_NOP, .access = PL1_W },
2206 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
2207 .type = ARM_CP_NOP, .access = PL1_W },
2208 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
2209 .type = ARM_CP_NOP, .access = PL1_W },
2210 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
2211 .type = ARM_CP_NOP, .access = PL1_W },
2212 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
2213 .type = ARM_CP_NOP, .access = PL1_W },
2214 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
2215 .type = ARM_CP_NOP, .access = PL1_W },
2216 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
2217 .type = ARM_CP_NOP, .access = PL1_W },
2218 /* MMU Domain access control / MPU write buffer control */
2219 { .name = "DACR", .cp = 15,
2220 .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
2221 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
2222 .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
2223 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
2224 .type = ARM_CP_NO_MIGRATE,
2225 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
2226 .access = PL1_RW,
2227 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
2228 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
2229 .type = ARM_CP_NO_MIGRATE,
2230 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
2231 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[0]) },
2232 /* We rely on the access checks not allowing the guest to write to the
2233 * state field when SPSel indicates that it's being used as the stack
2234 * pointer.
2236 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
2237 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
2238 .access = PL1_RW, .accessfn = sp_el0_access,
2239 .type = ARM_CP_NO_MIGRATE,
2240 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
2241 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
2242 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
2243 .type = ARM_CP_NO_MIGRATE,
2244 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
2245 REGINFO_SENTINEL
2248 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
2249 static const ARMCPRegInfo v8_el3_no_el2_cp_reginfo[] = {
2250 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
2251 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
2252 .access = PL2_RW,
2253 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
2254 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
2255 .type = ARM_CP_NO_MIGRATE,
2256 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
2257 .access = PL2_RW,
2258 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
2259 REGINFO_SENTINEL
2262 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2264 ARMCPU *cpu = arm_env_get_cpu(env);
2265 uint64_t valid_mask = HCR_MASK;
2267 if (arm_feature(env, ARM_FEATURE_EL3)) {
2268 valid_mask &= ~HCR_HCD;
2269 } else {
2270 valid_mask &= ~HCR_TSC;
2273 /* Clear RES0 bits. */
2274 value &= valid_mask;
2276 /* These bits change the MMU setup:
2277 * HCR_VM enables stage 2 translation
2278 * HCR_PTW forbids certain page-table setups
2279 * HCR_DC Disables stage1 and enables stage2 translation
2281 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
2282 tlb_flush(CPU(cpu), 1);
2284 raw_write(env, ri, value);
2287 static const ARMCPRegInfo v8_el2_cp_reginfo[] = {
2288 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
2289 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
2290 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
2291 .writefn = hcr_write },
2292 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
2293 .type = ARM_CP_NO_MIGRATE,
2294 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
2295 .access = PL2_RW,
2296 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
2297 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
2298 .type = ARM_CP_NO_MIGRATE,
2299 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
2300 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
2301 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
2302 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
2303 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
2304 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
2305 .type = ARM_CP_NO_MIGRATE,
2306 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
2307 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[6]) },
2308 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
2309 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
2310 .access = PL2_RW, .writefn = vbar_write,
2311 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
2312 .resetvalue = 0 },
2313 REGINFO_SENTINEL
2316 static const ARMCPRegInfo v8_el3_cp_reginfo[] = {
2317 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
2318 .type = ARM_CP_NO_MIGRATE,
2319 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
2320 .access = PL3_RW,
2321 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
2322 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
2323 .type = ARM_CP_NO_MIGRATE,
2324 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
2325 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
2326 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
2327 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
2328 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
2329 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
2330 .type = ARM_CP_NO_MIGRATE,
2331 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
2332 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[7]) },
2333 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
2334 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
2335 .access = PL3_RW, .writefn = vbar_write,
2336 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
2337 .resetvalue = 0 },
2338 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
2339 .type = ARM_CP_NO_MIGRATE,
2340 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
2341 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
2342 .writefn = scr_write },
2343 REGINFO_SENTINEL
2346 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2347 uint64_t value)
2349 ARMCPU *cpu = arm_env_get_cpu(env);
2351 if (raw_read(env, ri) == value) {
2352 /* Skip the TLB flush if nothing actually changed; Linux likes
2353 * to do a lot of pointless SCTLR writes.
2355 return;
2358 raw_write(env, ri, value);
2359 /* ??? Lots of these bits are not implemented. */
2360 /* This may enable/disable the MMU, so do a TLB flush. */
2361 tlb_flush(CPU(cpu), 1);
2364 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
2366 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
2367 * but the AArch32 CTR has its own reginfo struct)
2369 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UCT)) {
2370 return CP_ACCESS_TRAP;
2372 return CP_ACCESS_OK;
2375 static const ARMCPRegInfo debug_cp_reginfo[] = {
2376 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
2377 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
2378 * unlike DBGDRAR it is never accessible from EL0.
2379 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
2380 * accessor.
2382 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
2383 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2384 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
2385 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
2386 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2387 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2388 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2389 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
2390 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
2391 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
2392 .access = PL1_RW,
2393 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
2394 .resetvalue = 0 },
2395 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
2396 * We don't implement the configurable EL0 access.
2398 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
2399 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
2400 .type = ARM_CP_NO_MIGRATE,
2401 .access = PL1_R,
2402 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
2403 .resetfn = arm_cp_reset_ignore },
2404 /* We define a dummy WI OSLAR_EL1, because Linux writes to it. */
2405 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
2406 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
2407 .access = PL1_W, .type = ARM_CP_NOP },
2408 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
2409 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
2410 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
2411 .access = PL1_RW, .type = ARM_CP_NOP },
2412 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
2413 * implement vector catch debug events yet.
2415 { .name = "DBGVCR",
2416 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
2417 .access = PL1_RW, .type = ARM_CP_NOP },
2418 REGINFO_SENTINEL
2421 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
2422 /* 64 bit access versions of the (dummy) debug registers */
2423 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
2424 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
2425 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
2426 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
2427 REGINFO_SENTINEL
2430 void hw_watchpoint_update(ARMCPU *cpu, int n)
2432 CPUARMState *env = &cpu->env;
2433 vaddr len = 0;
2434 vaddr wvr = env->cp15.dbgwvr[n];
2435 uint64_t wcr = env->cp15.dbgwcr[n];
2436 int mask;
2437 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
2439 if (env->cpu_watchpoint[n]) {
2440 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
2441 env->cpu_watchpoint[n] = NULL;
2444 if (!extract64(wcr, 0, 1)) {
2445 /* E bit clear : watchpoint disabled */
2446 return;
2449 switch (extract64(wcr, 3, 2)) {
2450 case 0:
2451 /* LSC 00 is reserved and must behave as if the wp is disabled */
2452 return;
2453 case 1:
2454 flags |= BP_MEM_READ;
2455 break;
2456 case 2:
2457 flags |= BP_MEM_WRITE;
2458 break;
2459 case 3:
2460 flags |= BP_MEM_ACCESS;
2461 break;
2464 /* Attempts to use both MASK and BAS fields simultaneously are
2465 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
2466 * thus generating a watchpoint for every byte in the masked region.
2468 mask = extract64(wcr, 24, 4);
2469 if (mask == 1 || mask == 2) {
2470 /* Reserved values of MASK; we must act as if the mask value was
2471 * some non-reserved value, or as if the watchpoint were disabled.
2472 * We choose the latter.
2474 return;
2475 } else if (mask) {
2476 /* Watchpoint covers an aligned area up to 2GB in size */
2477 len = 1ULL << mask;
2478 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
2479 * whether the watchpoint fires when the unmasked bits match; we opt
2480 * to generate the exceptions.
2482 wvr &= ~(len - 1);
2483 } else {
2484 /* Watchpoint covers bytes defined by the byte address select bits */
2485 int bas = extract64(wcr, 5, 8);
2486 int basstart;
2488 if (bas == 0) {
2489 /* This must act as if the watchpoint is disabled */
2490 return;
2493 if (extract64(wvr, 2, 1)) {
2494 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
2495 * ignored, and BAS[3:0] define which bytes to watch.
2497 bas &= 0xf;
2499 /* The BAS bits are supposed to be programmed to indicate a contiguous
2500 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
2501 * we fire for each byte in the word/doubleword addressed by the WVR.
2502 * We choose to ignore any non-zero bits after the first range of 1s.
2504 basstart = ctz32(bas);
2505 len = cto32(bas >> basstart);
2506 wvr += basstart;
2509 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
2510 &env->cpu_watchpoint[n]);
2513 void hw_watchpoint_update_all(ARMCPU *cpu)
2515 int i;
2516 CPUARMState *env = &cpu->env;
2518 /* Completely clear out existing QEMU watchpoints and our array, to
2519 * avoid possible stale entries following migration load.
2521 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
2522 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
2524 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
2525 hw_watchpoint_update(cpu, i);
2529 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2530 uint64_t value)
2532 ARMCPU *cpu = arm_env_get_cpu(env);
2533 int i = ri->crm;
2535 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
2536 * register reads and behaves as if values written are sign extended.
2537 * Bits [1:0] are RES0.
2539 value = sextract64(value, 0, 49) & ~3ULL;
2541 raw_write(env, ri, value);
2542 hw_watchpoint_update(cpu, i);
2545 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2546 uint64_t value)
2548 ARMCPU *cpu = arm_env_get_cpu(env);
2549 int i = ri->crm;
2551 raw_write(env, ri, value);
2552 hw_watchpoint_update(cpu, i);
2555 void hw_breakpoint_update(ARMCPU *cpu, int n)
2557 CPUARMState *env = &cpu->env;
2558 uint64_t bvr = env->cp15.dbgbvr[n];
2559 uint64_t bcr = env->cp15.dbgbcr[n];
2560 vaddr addr;
2561 int bt;
2562 int flags = BP_CPU;
2564 if (env->cpu_breakpoint[n]) {
2565 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
2566 env->cpu_breakpoint[n] = NULL;
2569 if (!extract64(bcr, 0, 1)) {
2570 /* E bit clear : watchpoint disabled */
2571 return;
2574 bt = extract64(bcr, 20, 4);
2576 switch (bt) {
2577 case 4: /* unlinked address mismatch (reserved if AArch64) */
2578 case 5: /* linked address mismatch (reserved if AArch64) */
2579 qemu_log_mask(LOG_UNIMP,
2580 "arm: address mismatch breakpoint types not implemented");
2581 return;
2582 case 0: /* unlinked address match */
2583 case 1: /* linked address match */
2585 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
2586 * we behave as if the register was sign extended. Bits [1:0] are
2587 * RES0. The BAS field is used to allow setting breakpoints on 16
2588 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
2589 * a bp will fire if the addresses covered by the bp and the addresses
2590 * covered by the insn overlap but the insn doesn't start at the
2591 * start of the bp address range. We choose to require the insn and
2592 * the bp to have the same address. The constraints on writing to
2593 * BAS enforced in dbgbcr_write mean we have only four cases:
2594 * 0b0000 => no breakpoint
2595 * 0b0011 => breakpoint on addr
2596 * 0b1100 => breakpoint on addr + 2
2597 * 0b1111 => breakpoint on addr
2598 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
2600 int bas = extract64(bcr, 5, 4);
2601 addr = sextract64(bvr, 0, 49) & ~3ULL;
2602 if (bas == 0) {
2603 return;
2605 if (bas == 0xc) {
2606 addr += 2;
2608 break;
2610 case 2: /* unlinked context ID match */
2611 case 8: /* unlinked VMID match (reserved if no EL2) */
2612 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
2613 qemu_log_mask(LOG_UNIMP,
2614 "arm: unlinked context breakpoint types not implemented");
2615 return;
2616 case 9: /* linked VMID match (reserved if no EL2) */
2617 case 11: /* linked context ID and VMID match (reserved if no EL2) */
2618 case 3: /* linked context ID match */
2619 default:
2620 /* We must generate no events for Linked context matches (unless
2621 * they are linked to by some other bp/wp, which is handled in
2622 * updates for the linking bp/wp). We choose to also generate no events
2623 * for reserved values.
2625 return;
2628 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
2631 void hw_breakpoint_update_all(ARMCPU *cpu)
2633 int i;
2634 CPUARMState *env = &cpu->env;
2636 /* Completely clear out existing QEMU breakpoints and our array, to
2637 * avoid possible stale entries following migration load.
2639 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
2640 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
2642 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
2643 hw_breakpoint_update(cpu, i);
2647 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2648 uint64_t value)
2650 ARMCPU *cpu = arm_env_get_cpu(env);
2651 int i = ri->crm;
2653 raw_write(env, ri, value);
2654 hw_breakpoint_update(cpu, i);
2657 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2658 uint64_t value)
2660 ARMCPU *cpu = arm_env_get_cpu(env);
2661 int i = ri->crm;
2663 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
2664 * copy of BAS[0].
2666 value = deposit64(value, 6, 1, extract64(value, 5, 1));
2667 value = deposit64(value, 8, 1, extract64(value, 7, 1));
2669 raw_write(env, ri, value);
2670 hw_breakpoint_update(cpu, i);
2673 static void define_debug_regs(ARMCPU *cpu)
2675 /* Define v7 and v8 architectural debug registers.
2676 * These are just dummy implementations for now.
2678 int i;
2679 int wrps, brps, ctx_cmps;
2680 ARMCPRegInfo dbgdidr = {
2681 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
2682 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
2685 /* Note that all these register fields hold "number of Xs minus 1". */
2686 brps = extract32(cpu->dbgdidr, 24, 4);
2687 wrps = extract32(cpu->dbgdidr, 28, 4);
2688 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
2690 assert(ctx_cmps <= brps);
2692 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
2693 * of the debug registers such as number of breakpoints;
2694 * check that if they both exist then they agree.
2696 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
2697 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
2698 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
2699 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
2702 define_one_arm_cp_reg(cpu, &dbgdidr);
2703 define_arm_cp_regs(cpu, debug_cp_reginfo);
2705 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
2706 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
2709 for (i = 0; i < brps + 1; i++) {
2710 ARMCPRegInfo dbgregs[] = {
2711 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
2712 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
2713 .access = PL1_RW,
2714 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
2715 .writefn = dbgbvr_write, .raw_writefn = raw_write
2717 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
2718 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
2719 .access = PL1_RW,
2720 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
2721 .writefn = dbgbcr_write, .raw_writefn = raw_write
2723 REGINFO_SENTINEL
2725 define_arm_cp_regs(cpu, dbgregs);
2728 for (i = 0; i < wrps + 1; i++) {
2729 ARMCPRegInfo dbgregs[] = {
2730 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
2731 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
2732 .access = PL1_RW,
2733 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
2734 .writefn = dbgwvr_write, .raw_writefn = raw_write
2736 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
2737 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
2738 .access = PL1_RW,
2739 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
2740 .writefn = dbgwcr_write, .raw_writefn = raw_write
2742 REGINFO_SENTINEL
2744 define_arm_cp_regs(cpu, dbgregs);
2748 void register_cp_regs_for_features(ARMCPU *cpu)
2750 /* Register all the coprocessor registers based on feature bits */
2751 CPUARMState *env = &cpu->env;
2752 if (arm_feature(env, ARM_FEATURE_M)) {
2753 /* M profile has no coprocessor registers */
2754 return;
2757 define_arm_cp_regs(cpu, cp_reginfo);
2758 if (!arm_feature(env, ARM_FEATURE_V8)) {
2759 /* Must go early as it is full of wildcards that may be
2760 * overridden by later definitions.
2762 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
2765 if (arm_feature(env, ARM_FEATURE_V6)) {
2766 /* The ID registers all have impdef reset values */
2767 ARMCPRegInfo v6_idregs[] = {
2768 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
2769 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
2770 .access = PL1_R, .type = ARM_CP_CONST,
2771 .resetvalue = cpu->id_pfr0 },
2772 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
2773 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
2774 .access = PL1_R, .type = ARM_CP_CONST,
2775 .resetvalue = cpu->id_pfr1 },
2776 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
2777 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
2778 .access = PL1_R, .type = ARM_CP_CONST,
2779 .resetvalue = cpu->id_dfr0 },
2780 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
2781 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
2782 .access = PL1_R, .type = ARM_CP_CONST,
2783 .resetvalue = cpu->id_afr0 },
2784 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
2785 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
2786 .access = PL1_R, .type = ARM_CP_CONST,
2787 .resetvalue = cpu->id_mmfr0 },
2788 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
2789 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
2790 .access = PL1_R, .type = ARM_CP_CONST,
2791 .resetvalue = cpu->id_mmfr1 },
2792 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
2793 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
2794 .access = PL1_R, .type = ARM_CP_CONST,
2795 .resetvalue = cpu->id_mmfr2 },
2796 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
2797 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
2798 .access = PL1_R, .type = ARM_CP_CONST,
2799 .resetvalue = cpu->id_mmfr3 },
2800 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
2801 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
2802 .access = PL1_R, .type = ARM_CP_CONST,
2803 .resetvalue = cpu->id_isar0 },
2804 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
2805 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
2806 .access = PL1_R, .type = ARM_CP_CONST,
2807 .resetvalue = cpu->id_isar1 },
2808 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
2809 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
2810 .access = PL1_R, .type = ARM_CP_CONST,
2811 .resetvalue = cpu->id_isar2 },
2812 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
2813 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
2814 .access = PL1_R, .type = ARM_CP_CONST,
2815 .resetvalue = cpu->id_isar3 },
2816 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
2817 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
2818 .access = PL1_R, .type = ARM_CP_CONST,
2819 .resetvalue = cpu->id_isar4 },
2820 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
2821 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
2822 .access = PL1_R, .type = ARM_CP_CONST,
2823 .resetvalue = cpu->id_isar5 },
2824 /* 6..7 are as yet unallocated and must RAZ */
2825 { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
2826 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
2827 .resetvalue = 0 },
2828 { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
2829 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
2830 .resetvalue = 0 },
2831 REGINFO_SENTINEL
2833 define_arm_cp_regs(cpu, v6_idregs);
2834 define_arm_cp_regs(cpu, v6_cp_reginfo);
2835 } else {
2836 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
2838 if (arm_feature(env, ARM_FEATURE_V6K)) {
2839 define_arm_cp_regs(cpu, v6k_cp_reginfo);
2841 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2842 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
2844 if (arm_feature(env, ARM_FEATURE_V7)) {
2845 /* v7 performance monitor control register: same implementor
2846 * field as main ID register, and we implement only the cycle
2847 * count register.
2849 #ifndef CONFIG_USER_ONLY
2850 ARMCPRegInfo pmcr = {
2851 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
2852 .access = PL0_RW,
2853 .type = ARM_CP_IO | ARM_CP_NO_MIGRATE,
2854 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
2855 .accessfn = pmreg_access, .writefn = pmcr_write,
2856 .raw_writefn = raw_write,
2858 ARMCPRegInfo pmcr64 = {
2859 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
2860 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
2861 .access = PL0_RW, .accessfn = pmreg_access,
2862 .type = ARM_CP_IO,
2863 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
2864 .resetvalue = cpu->midr & 0xff000000,
2865 .writefn = pmcr_write, .raw_writefn = raw_write,
2867 define_one_arm_cp_reg(cpu, &pmcr);
2868 define_one_arm_cp_reg(cpu, &pmcr64);
2869 #endif
2870 ARMCPRegInfo clidr = {
2871 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
2872 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
2873 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
2875 define_one_arm_cp_reg(cpu, &clidr);
2876 define_arm_cp_regs(cpu, v7_cp_reginfo);
2877 define_debug_regs(cpu);
2878 } else {
2879 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
2881 if (arm_feature(env, ARM_FEATURE_V8)) {
2882 /* AArch64 ID registers, which all have impdef reset values */
2883 ARMCPRegInfo v8_idregs[] = {
2884 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
2885 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
2886 .access = PL1_R, .type = ARM_CP_CONST,
2887 .resetvalue = cpu->id_aa64pfr0 },
2888 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
2889 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
2890 .access = PL1_R, .type = ARM_CP_CONST,
2891 .resetvalue = cpu->id_aa64pfr1},
2892 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
2893 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
2894 .access = PL1_R, .type = ARM_CP_CONST,
2895 /* We mask out the PMUVer field, because we don't currently
2896 * implement the PMU. Not advertising it prevents the guest
2897 * from trying to use it and getting UNDEFs on registers we
2898 * don't implement.
2900 .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
2901 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
2902 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
2903 .access = PL1_R, .type = ARM_CP_CONST,
2904 .resetvalue = cpu->id_aa64dfr1 },
2905 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
2906 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
2907 .access = PL1_R, .type = ARM_CP_CONST,
2908 .resetvalue = cpu->id_aa64afr0 },
2909 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
2910 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
2911 .access = PL1_R, .type = ARM_CP_CONST,
2912 .resetvalue = cpu->id_aa64afr1 },
2913 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
2914 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
2915 .access = PL1_R, .type = ARM_CP_CONST,
2916 .resetvalue = cpu->id_aa64isar0 },
2917 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
2918 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
2919 .access = PL1_R, .type = ARM_CP_CONST,
2920 .resetvalue = cpu->id_aa64isar1 },
2921 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
2922 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
2923 .access = PL1_R, .type = ARM_CP_CONST,
2924 .resetvalue = cpu->id_aa64mmfr0 },
2925 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
2926 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
2927 .access = PL1_R, .type = ARM_CP_CONST,
2928 .resetvalue = cpu->id_aa64mmfr1 },
2929 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
2930 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
2931 .access = PL1_R, .type = ARM_CP_CONST,
2932 .resetvalue = cpu->mvfr0 },
2933 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
2934 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
2935 .access = PL1_R, .type = ARM_CP_CONST,
2936 .resetvalue = cpu->mvfr1 },
2937 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
2938 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
2939 .access = PL1_R, .type = ARM_CP_CONST,
2940 .resetvalue = cpu->mvfr2 },
2941 REGINFO_SENTINEL
2943 ARMCPRegInfo rvbar = {
2944 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
2945 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
2946 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
2948 define_one_arm_cp_reg(cpu, &rvbar);
2949 define_arm_cp_regs(cpu, v8_idregs);
2950 define_arm_cp_regs(cpu, v8_cp_reginfo);
2952 if (arm_feature(env, ARM_FEATURE_EL2)) {
2953 define_arm_cp_regs(cpu, v8_el2_cp_reginfo);
2954 } else {
2955 /* If EL2 is missing but higher ELs are enabled, we need to
2956 * register the no_el2 reginfos.
2958 if (arm_feature(env, ARM_FEATURE_EL3)) {
2959 define_arm_cp_regs(cpu, v8_el3_no_el2_cp_reginfo);
2962 if (arm_feature(env, ARM_FEATURE_EL3)) {
2963 define_arm_cp_regs(cpu, v8_el3_cp_reginfo);
2965 if (arm_feature(env, ARM_FEATURE_MPU)) {
2966 /* These are the MPU registers prior to PMSAv6. Any new
2967 * PMSA core later than the ARM946 will require that we
2968 * implement the PMSAv6 or PMSAv7 registers, which are
2969 * completely different.
2971 assert(!arm_feature(env, ARM_FEATURE_V6));
2972 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
2973 } else {
2974 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
2976 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
2977 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
2979 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
2980 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
2982 if (arm_feature(env, ARM_FEATURE_VAPA)) {
2983 define_arm_cp_regs(cpu, vapa_cp_reginfo);
2985 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
2986 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
2988 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
2989 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
2991 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
2992 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
2994 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2995 define_arm_cp_regs(cpu, omap_cp_reginfo);
2997 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
2998 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
3000 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
3001 define_arm_cp_regs(cpu, xscale_cp_reginfo);
3003 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
3004 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
3006 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3007 define_arm_cp_regs(cpu, lpae_cp_reginfo);
3009 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
3010 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
3011 * be read-only (ie write causes UNDEF exception).
3014 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
3015 /* Pre-v8 MIDR space.
3016 * Note that the MIDR isn't a simple constant register because
3017 * of the TI925 behaviour where writes to another register can
3018 * cause the MIDR value to change.
3020 * Unimplemented registers in the c15 0 0 0 space default to
3021 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
3022 * and friends override accordingly.
3024 { .name = "MIDR",
3025 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
3026 .access = PL1_R, .resetvalue = cpu->midr,
3027 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
3028 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
3029 .type = ARM_CP_OVERRIDE },
3030 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
3031 { .name = "DUMMY",
3032 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
3033 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3034 { .name = "DUMMY",
3035 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
3036 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3037 { .name = "DUMMY",
3038 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
3039 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3040 { .name = "DUMMY",
3041 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
3042 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3043 { .name = "DUMMY",
3044 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
3045 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3046 REGINFO_SENTINEL
3048 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
3049 /* v8 MIDR -- the wildcard isn't necessary, and nor is the
3050 * variable-MIDR TI925 behaviour. Instead we have a single
3051 * (strictly speaking IMPDEF) alias of the MIDR, REVIDR.
3053 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
3054 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
3055 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr },
3056 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
3057 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
3058 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr },
3059 REGINFO_SENTINEL
3061 ARMCPRegInfo id_cp_reginfo[] = {
3062 /* These are common to v8 and pre-v8 */
3063 { .name = "CTR",
3064 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
3065 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
3066 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
3067 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
3068 .access = PL0_R, .accessfn = ctr_el0_access,
3069 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
3070 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
3071 { .name = "TCMTR",
3072 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
3073 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3074 { .name = "TLBTR",
3075 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
3076 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
3077 REGINFO_SENTINEL
3079 ARMCPRegInfo crn0_wi_reginfo = {
3080 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
3081 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
3082 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
3084 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
3085 arm_feature(env, ARM_FEATURE_STRONGARM)) {
3086 ARMCPRegInfo *r;
3087 /* Register the blanket "writes ignored" value first to cover the
3088 * whole space. Then update the specific ID registers to allow write
3089 * access, so that they ignore writes rather than causing them to
3090 * UNDEF.
3092 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
3093 for (r = id_pre_v8_midr_cp_reginfo;
3094 r->type != ARM_CP_SENTINEL; r++) {
3095 r->access = PL1_RW;
3097 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
3098 r->access = PL1_RW;
3101 if (arm_feature(env, ARM_FEATURE_V8)) {
3102 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
3103 } else {
3104 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
3106 define_arm_cp_regs(cpu, id_cp_reginfo);
3109 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
3110 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
3113 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
3114 ARMCPRegInfo auxcr = {
3115 .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
3116 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
3117 .access = PL1_RW, .type = ARM_CP_CONST,
3118 .resetvalue = cpu->reset_auxcr
3120 define_one_arm_cp_reg(cpu, &auxcr);
3123 if (arm_feature(env, ARM_FEATURE_CBAR)) {
3124 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
3125 /* 32 bit view is [31:18] 0...0 [43:32]. */
3126 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
3127 | extract64(cpu->reset_cbar, 32, 12);
3128 ARMCPRegInfo cbar_reginfo[] = {
3129 { .name = "CBAR",
3130 .type = ARM_CP_CONST,
3131 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
3132 .access = PL1_R, .resetvalue = cpu->reset_cbar },
3133 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
3134 .type = ARM_CP_CONST,
3135 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
3136 .access = PL1_R, .resetvalue = cbar32 },
3137 REGINFO_SENTINEL
3139 /* We don't implement a r/w 64 bit CBAR currently */
3140 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
3141 define_arm_cp_regs(cpu, cbar_reginfo);
3142 } else {
3143 ARMCPRegInfo cbar = {
3144 .name = "CBAR",
3145 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
3146 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
3147 .fieldoffset = offsetof(CPUARMState,
3148 cp15.c15_config_base_address)
3150 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
3151 cbar.access = PL1_R;
3152 cbar.fieldoffset = 0;
3153 cbar.type = ARM_CP_CONST;
3155 define_one_arm_cp_reg(cpu, &cbar);
3159 /* Generic registers whose values depend on the implementation */
3161 ARMCPRegInfo sctlr = {
3162 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
3163 .opc0 = 3, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
3164 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys),
3165 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
3166 .raw_writefn = raw_write,
3168 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
3169 /* Normally we would always end the TB on an SCTLR write, but Linux
3170 * arch/arm/mach-pxa/sleep.S expects two instructions following
3171 * an MMU enable to execute from cache. Imitate this behaviour.
3173 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
3175 define_one_arm_cp_reg(cpu, &sctlr);
3179 ARMCPU *cpu_arm_init(const char *cpu_model)
3181 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
3184 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
3186 CPUState *cs = CPU(cpu);
3187 CPUARMState *env = &cpu->env;
3189 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
3190 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
3191 aarch64_fpu_gdb_set_reg,
3192 34, "aarch64-fpu.xml", 0);
3193 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
3194 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
3195 51, "arm-neon.xml", 0);
3196 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
3197 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
3198 35, "arm-vfp3.xml", 0);
3199 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
3200 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
3201 19, "arm-vfp.xml", 0);
3205 /* Sort alphabetically by type name, except for "any". */
3206 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
3208 ObjectClass *class_a = (ObjectClass *)a;
3209 ObjectClass *class_b = (ObjectClass *)b;
3210 const char *name_a, *name_b;
3212 name_a = object_class_get_name(class_a);
3213 name_b = object_class_get_name(class_b);
3214 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
3215 return 1;
3216 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
3217 return -1;
3218 } else {
3219 return strcmp(name_a, name_b);
3223 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
3225 ObjectClass *oc = data;
3226 CPUListState *s = user_data;
3227 const char *typename;
3228 char *name;
3230 typename = object_class_get_name(oc);
3231 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
3232 (*s->cpu_fprintf)(s->file, " %s\n",
3233 name);
3234 g_free(name);
3237 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
3239 CPUListState s = {
3240 .file = f,
3241 .cpu_fprintf = cpu_fprintf,
3243 GSList *list;
3245 list = object_class_get_list(TYPE_ARM_CPU, false);
3246 list = g_slist_sort(list, arm_cpu_list_compare);
3247 (*cpu_fprintf)(f, "Available CPUs:\n");
3248 g_slist_foreach(list, arm_cpu_list_entry, &s);
3249 g_slist_free(list);
3250 #ifdef CONFIG_KVM
3251 /* The 'host' CPU type is dynamically registered only if KVM is
3252 * enabled, so we have to special-case it here:
3254 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
3255 #endif
3258 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
3260 ObjectClass *oc = data;
3261 CpuDefinitionInfoList **cpu_list = user_data;
3262 CpuDefinitionInfoList *entry;
3263 CpuDefinitionInfo *info;
3264 const char *typename;
3266 typename = object_class_get_name(oc);
3267 info = g_malloc0(sizeof(*info));
3268 info->name = g_strndup(typename,
3269 strlen(typename) - strlen("-" TYPE_ARM_CPU));
3271 entry = g_malloc0(sizeof(*entry));
3272 entry->value = info;
3273 entry->next = *cpu_list;
3274 *cpu_list = entry;
3277 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
3279 CpuDefinitionInfoList *cpu_list = NULL;
3280 GSList *list;
3282 list = object_class_get_list(TYPE_ARM_CPU, false);
3283 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
3284 g_slist_free(list);
3286 return cpu_list;
3289 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
3290 void *opaque, int state,
3291 int crm, int opc1, int opc2)
3293 /* Private utility function for define_one_arm_cp_reg_with_opaque():
3294 * add a single reginfo struct to the hash table.
3296 uint32_t *key = g_new(uint32_t, 1);
3297 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
3298 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3299 if (r->state == ARM_CP_STATE_BOTH && state == ARM_CP_STATE_AA32) {
3300 /* The AArch32 view of a shared register sees the lower 32 bits
3301 * of a 64 bit backing field. It is not migratable as the AArch64
3302 * view handles that. AArch64 also handles reset.
3303 * We assume it is a cp15 register if the .cp field is left unset.
3305 if (r2->cp == 0) {
3306 r2->cp = 15;
3308 r2->type |= ARM_CP_NO_MIGRATE;
3309 r2->resetfn = arm_cp_reset_ignore;
3310 #ifdef HOST_WORDS_BIGENDIAN
3311 if (r2->fieldoffset) {
3312 r2->fieldoffset += sizeof(uint32_t);
3314 #endif
3316 if (state == ARM_CP_STATE_AA64) {
3317 /* To allow abbreviation of ARMCPRegInfo
3318 * definitions, we treat cp == 0 as equivalent to
3319 * the value for "standard guest-visible sysreg".
3320 * STATE_BOTH definitions are also always "standard
3321 * sysreg" in their AArch64 view (the .cp value may
3322 * be non-zero for the benefit of the AArch32 view).
3324 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
3325 r2->cp = CP_REG_ARM64_SYSREG_CP;
3327 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
3328 r2->opc0, opc1, opc2);
3329 } else {
3330 *key = ENCODE_CP_REG(r2->cp, is64, r2->crn, crm, opc1, opc2);
3332 if (opaque) {
3333 r2->opaque = opaque;
3335 /* reginfo passed to helpers is correct for the actual access,
3336 * and is never ARM_CP_STATE_BOTH:
3338 r2->state = state;
3339 /* Make sure reginfo passed to helpers for wildcarded regs
3340 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
3342 r2->crm = crm;
3343 r2->opc1 = opc1;
3344 r2->opc2 = opc2;
3345 /* By convention, for wildcarded registers only the first
3346 * entry is used for migration; the others are marked as
3347 * NO_MIGRATE so we don't try to transfer the register
3348 * multiple times. Special registers (ie NOP/WFI) are
3349 * never migratable.
3351 if ((r->type & ARM_CP_SPECIAL) ||
3352 ((r->crm == CP_ANY) && crm != 0) ||
3353 ((r->opc1 == CP_ANY) && opc1 != 0) ||
3354 ((r->opc2 == CP_ANY) && opc2 != 0)) {
3355 r2->type |= ARM_CP_NO_MIGRATE;
3358 /* Overriding of an existing definition must be explicitly
3359 * requested.
3361 if (!(r->type & ARM_CP_OVERRIDE)) {
3362 ARMCPRegInfo *oldreg;
3363 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
3364 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
3365 fprintf(stderr, "Register redefined: cp=%d %d bit "
3366 "crn=%d crm=%d opc1=%d opc2=%d, "
3367 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
3368 r2->crn, r2->crm, r2->opc1, r2->opc2,
3369 oldreg->name, r2->name);
3370 g_assert_not_reached();
3373 g_hash_table_insert(cpu->cp_regs, key, r2);
3377 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
3378 const ARMCPRegInfo *r, void *opaque)
3380 /* Define implementations of coprocessor registers.
3381 * We store these in a hashtable because typically
3382 * there are less than 150 registers in a space which
3383 * is 16*16*16*8*8 = 262144 in size.
3384 * Wildcarding is supported for the crm, opc1 and opc2 fields.
3385 * If a register is defined twice then the second definition is
3386 * used, so this can be used to define some generic registers and
3387 * then override them with implementation specific variations.
3388 * At least one of the original and the second definition should
3389 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
3390 * against accidental use.
3392 * The state field defines whether the register is to be
3393 * visible in the AArch32 or AArch64 execution state. If the
3394 * state is set to ARM_CP_STATE_BOTH then we synthesise a
3395 * reginfo structure for the AArch32 view, which sees the lower
3396 * 32 bits of the 64 bit register.
3398 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
3399 * be wildcarded. AArch64 registers are always considered to be 64
3400 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
3401 * the register, if any.
3403 int crm, opc1, opc2, state;
3404 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
3405 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
3406 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
3407 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
3408 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
3409 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
3410 /* 64 bit registers have only CRm and Opc1 fields */
3411 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
3412 /* op0 only exists in the AArch64 encodings */
3413 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
3414 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
3415 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
3416 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
3417 * encodes a minimum access level for the register. We roll this
3418 * runtime check into our general permission check code, so check
3419 * here that the reginfo's specified permissions are strict enough
3420 * to encompass the generic architectural permission check.
3422 if (r->state != ARM_CP_STATE_AA32) {
3423 int mask = 0;
3424 switch (r->opc1) {
3425 case 0: case 1: case 2:
3426 /* min_EL EL1 */
3427 mask = PL1_RW;
3428 break;
3429 case 3:
3430 /* min_EL EL0 */
3431 mask = PL0_RW;
3432 break;
3433 case 4:
3434 /* min_EL EL2 */
3435 mask = PL2_RW;
3436 break;
3437 case 5:
3438 /* unallocated encoding, so not possible */
3439 assert(false);
3440 break;
3441 case 6:
3442 /* min_EL EL3 */
3443 mask = PL3_RW;
3444 break;
3445 case 7:
3446 /* min_EL EL1, secure mode only (we don't check the latter) */
3447 mask = PL1_RW;
3448 break;
3449 default:
3450 /* broken reginfo with out-of-range opc1 */
3451 assert(false);
3452 break;
3454 /* assert our permissions are not too lax (stricter is fine) */
3455 assert((r->access & ~mask) == 0);
3458 /* Check that the register definition has enough info to handle
3459 * reads and writes if they are permitted.
3461 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
3462 if (r->access & PL3_R) {
3463 assert(r->fieldoffset || r->readfn);
3465 if (r->access & PL3_W) {
3466 assert(r->fieldoffset || r->writefn);
3469 /* Bad type field probably means missing sentinel at end of reg list */
3470 assert(cptype_valid(r->type));
3471 for (crm = crmmin; crm <= crmmax; crm++) {
3472 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
3473 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
3474 for (state = ARM_CP_STATE_AA32;
3475 state <= ARM_CP_STATE_AA64; state++) {
3476 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
3477 continue;
3479 add_cpreg_to_hashtable(cpu, r, opaque, state,
3480 crm, opc1, opc2);
3487 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
3488 const ARMCPRegInfo *regs, void *opaque)
3490 /* Define a whole list of registers */
3491 const ARMCPRegInfo *r;
3492 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
3493 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
3497 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
3499 return g_hash_table_lookup(cpregs, &encoded_cp);
3502 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
3503 uint64_t value)
3505 /* Helper coprocessor write function for write-ignore registers */
3508 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
3510 /* Helper coprocessor write function for read-as-zero registers */
3511 return 0;
3514 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
3516 /* Helper coprocessor reset function for do-nothing-on-reset registers */
3519 static int bad_mode_switch(CPUARMState *env, int mode)
3521 /* Return true if it is not valid for us to switch to
3522 * this CPU mode (ie all the UNPREDICTABLE cases in
3523 * the ARM ARM CPSRWriteByInstr pseudocode).
3525 switch (mode) {
3526 case ARM_CPU_MODE_USR:
3527 case ARM_CPU_MODE_SYS:
3528 case ARM_CPU_MODE_SVC:
3529 case ARM_CPU_MODE_ABT:
3530 case ARM_CPU_MODE_UND:
3531 case ARM_CPU_MODE_IRQ:
3532 case ARM_CPU_MODE_FIQ:
3533 return 0;
3534 default:
3535 return 1;
3539 uint32_t cpsr_read(CPUARMState *env)
3541 int ZF;
3542 ZF = (env->ZF == 0);
3543 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
3544 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
3545 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
3546 | ((env->condexec_bits & 0xfc) << 8)
3547 | (env->GE << 16) | (env->daif & CPSR_AIF);
3550 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
3552 if (mask & CPSR_NZCV) {
3553 env->ZF = (~val) & CPSR_Z;
3554 env->NF = val;
3555 env->CF = (val >> 29) & 1;
3556 env->VF = (val << 3) & 0x80000000;
3558 if (mask & CPSR_Q)
3559 env->QF = ((val & CPSR_Q) != 0);
3560 if (mask & CPSR_T)
3561 env->thumb = ((val & CPSR_T) != 0);
3562 if (mask & CPSR_IT_0_1) {
3563 env->condexec_bits &= ~3;
3564 env->condexec_bits |= (val >> 25) & 3;
3566 if (mask & CPSR_IT_2_7) {
3567 env->condexec_bits &= 3;
3568 env->condexec_bits |= (val >> 8) & 0xfc;
3570 if (mask & CPSR_GE) {
3571 env->GE = (val >> 16) & 0xf;
3574 env->daif &= ~(CPSR_AIF & mask);
3575 env->daif |= val & CPSR_AIF & mask;
3577 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
3578 if (bad_mode_switch(env, val & CPSR_M)) {
3579 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
3580 * We choose to ignore the attempt and leave the CPSR M field
3581 * untouched.
3583 mask &= ~CPSR_M;
3584 } else {
3585 switch_mode(env, val & CPSR_M);
3588 mask &= ~CACHED_CPSR_BITS;
3589 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
3592 /* Sign/zero extend */
3593 uint32_t HELPER(sxtb16)(uint32_t x)
3595 uint32_t res;
3596 res = (uint16_t)(int8_t)x;
3597 res |= (uint32_t)(int8_t)(x >> 16) << 16;
3598 return res;
3601 uint32_t HELPER(uxtb16)(uint32_t x)
3603 uint32_t res;
3604 res = (uint16_t)(uint8_t)x;
3605 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
3606 return res;
3609 uint32_t HELPER(clz)(uint32_t x)
3611 return clz32(x);
3614 int32_t HELPER(sdiv)(int32_t num, int32_t den)
3616 if (den == 0)
3617 return 0;
3618 if (num == INT_MIN && den == -1)
3619 return INT_MIN;
3620 return num / den;
3623 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
3625 if (den == 0)
3626 return 0;
3627 return num / den;
3630 uint32_t HELPER(rbit)(uint32_t x)
3632 x = ((x & 0xff000000) >> 24)
3633 | ((x & 0x00ff0000) >> 8)
3634 | ((x & 0x0000ff00) << 8)
3635 | ((x & 0x000000ff) << 24);
3636 x = ((x & 0xf0f0f0f0) >> 4)
3637 | ((x & 0x0f0f0f0f) << 4);
3638 x = ((x & 0x88888888) >> 3)
3639 | ((x & 0x44444444) >> 1)
3640 | ((x & 0x22222222) << 1)
3641 | ((x & 0x11111111) << 3);
3642 return x;
3645 #if defined(CONFIG_USER_ONLY)
3647 void arm_cpu_do_interrupt(CPUState *cs)
3649 cs->exception_index = -1;
3652 int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
3653 int mmu_idx)
3655 ARMCPU *cpu = ARM_CPU(cs);
3656 CPUARMState *env = &cpu->env;
3658 env->exception.vaddress = address;
3659 if (rw == 2) {
3660 cs->exception_index = EXCP_PREFETCH_ABORT;
3661 } else {
3662 cs->exception_index = EXCP_DATA_ABORT;
3664 return 1;
3667 /* These should probably raise undefined insn exceptions. */
3668 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
3670 ARMCPU *cpu = arm_env_get_cpu(env);
3672 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
3675 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
3677 ARMCPU *cpu = arm_env_get_cpu(env);
3679 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
3680 return 0;
3683 void switch_mode(CPUARMState *env, int mode)
3685 ARMCPU *cpu = arm_env_get_cpu(env);
3687 if (mode != ARM_CPU_MODE_USR) {
3688 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
3692 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
3694 ARMCPU *cpu = arm_env_get_cpu(env);
3696 cpu_abort(CPU(cpu), "banked r13 write\n");
3699 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
3701 ARMCPU *cpu = arm_env_get_cpu(env);
3703 cpu_abort(CPU(cpu), "banked r13 read\n");
3704 return 0;
3707 unsigned int arm_excp_target_el(CPUState *cs, unsigned int excp_idx)
3709 return 1;
3712 #else
3714 /* Map CPU modes onto saved register banks. */
3715 int bank_number(int mode)
3717 switch (mode) {
3718 case ARM_CPU_MODE_USR:
3719 case ARM_CPU_MODE_SYS:
3720 return 0;
3721 case ARM_CPU_MODE_SVC:
3722 return 1;
3723 case ARM_CPU_MODE_ABT:
3724 return 2;
3725 case ARM_CPU_MODE_UND:
3726 return 3;
3727 case ARM_CPU_MODE_IRQ:
3728 return 4;
3729 case ARM_CPU_MODE_FIQ:
3730 return 5;
3731 case ARM_CPU_MODE_HYP:
3732 return 6;
3733 case ARM_CPU_MODE_MON:
3734 return 7;
3736 hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
3739 void switch_mode(CPUARMState *env, int mode)
3741 int old_mode;
3742 int i;
3744 old_mode = env->uncached_cpsr & CPSR_M;
3745 if (mode == old_mode)
3746 return;
3748 if (old_mode == ARM_CPU_MODE_FIQ) {
3749 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
3750 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
3751 } else if (mode == ARM_CPU_MODE_FIQ) {
3752 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
3753 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
3756 i = bank_number(old_mode);
3757 env->banked_r13[i] = env->regs[13];
3758 env->banked_r14[i] = env->regs[14];
3759 env->banked_spsr[i] = env->spsr;
3761 i = bank_number(mode);
3762 env->regs[13] = env->banked_r13[i];
3763 env->regs[14] = env->banked_r14[i];
3764 env->spsr = env->banked_spsr[i];
3768 * Determine the target EL for a given exception type.
3770 unsigned int arm_excp_target_el(CPUState *cs, unsigned int excp_idx)
3772 ARMCPU *cpu = ARM_CPU(cs);
3773 CPUARMState *env = &cpu->env;
3774 unsigned int cur_el = arm_current_pl(env);
3775 unsigned int target_el;
3777 if (!env->aarch64) {
3778 /* TODO: Add EL2 and 3 exception handling for AArch32. */
3779 return 1;
3782 switch (excp_idx) {
3783 case EXCP_HVC:
3784 target_el = 2;
3785 break;
3786 default:
3787 target_el = MAX(cur_el, 1);
3788 break;
3790 return target_el;
3793 static void v7m_push(CPUARMState *env, uint32_t val)
3795 CPUState *cs = CPU(arm_env_get_cpu(env));
3797 env->regs[13] -= 4;
3798 stl_phys(cs->as, env->regs[13], val);
3801 static uint32_t v7m_pop(CPUARMState *env)
3803 CPUState *cs = CPU(arm_env_get_cpu(env));
3804 uint32_t val;
3806 val = ldl_phys(cs->as, env->regs[13]);
3807 env->regs[13] += 4;
3808 return val;
3811 /* Switch to V7M main or process stack pointer. */
3812 static void switch_v7m_sp(CPUARMState *env, int process)
3814 uint32_t tmp;
3815 if (env->v7m.current_sp != process) {
3816 tmp = env->v7m.other_sp;
3817 env->v7m.other_sp = env->regs[13];
3818 env->regs[13] = tmp;
3819 env->v7m.current_sp = process;
3823 static void do_v7m_exception_exit(CPUARMState *env)
3825 uint32_t type;
3826 uint32_t xpsr;
3828 type = env->regs[15];
3829 if (env->v7m.exception != 0)
3830 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
3832 /* Switch to the target stack. */
3833 switch_v7m_sp(env, (type & 4) != 0);
3834 /* Pop registers. */
3835 env->regs[0] = v7m_pop(env);
3836 env->regs[1] = v7m_pop(env);
3837 env->regs[2] = v7m_pop(env);
3838 env->regs[3] = v7m_pop(env);
3839 env->regs[12] = v7m_pop(env);
3840 env->regs[14] = v7m_pop(env);
3841 env->regs[15] = v7m_pop(env);
3842 xpsr = v7m_pop(env);
3843 xpsr_write(env, xpsr, 0xfffffdff);
3844 /* Undo stack alignment. */
3845 if (xpsr & 0x200)
3846 env->regs[13] |= 4;
3847 /* ??? The exception return type specifies Thread/Handler mode. However
3848 this is also implied by the xPSR value. Not sure what to do
3849 if there is a mismatch. */
3850 /* ??? Likewise for mismatches between the CONTROL register and the stack
3851 pointer. */
3854 void arm_v7m_cpu_do_interrupt(CPUState *cs)
3856 ARMCPU *cpu = ARM_CPU(cs);
3857 CPUARMState *env = &cpu->env;
3858 uint32_t xpsr = xpsr_read(env);
3859 uint32_t lr;
3860 uint32_t addr;
3862 arm_log_exception(cs->exception_index);
3864 lr = 0xfffffff1;
3865 if (env->v7m.current_sp)
3866 lr |= 4;
3867 if (env->v7m.exception == 0)
3868 lr |= 8;
3870 /* For exceptions we just mark as pending on the NVIC, and let that
3871 handle it. */
3872 /* TODO: Need to escalate if the current priority is higher than the
3873 one we're raising. */
3874 switch (cs->exception_index) {
3875 case EXCP_UDEF:
3876 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
3877 return;
3878 case EXCP_SWI:
3879 /* The PC already points to the next instruction. */
3880 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
3881 return;
3882 case EXCP_PREFETCH_ABORT:
3883 case EXCP_DATA_ABORT:
3884 /* TODO: if we implemented the MPU registers, this is where we
3885 * should set the MMFAR, etc from exception.fsr and exception.vaddress.
3887 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
3888 return;
3889 case EXCP_BKPT:
3890 if (semihosting_enabled) {
3891 int nr;
3892 nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
3893 if (nr == 0xab) {
3894 env->regs[15] += 2;
3895 env->regs[0] = do_arm_semihosting(env);
3896 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
3897 return;
3900 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
3901 return;
3902 case EXCP_IRQ:
3903 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
3904 break;
3905 case EXCP_EXCEPTION_EXIT:
3906 do_v7m_exception_exit(env);
3907 return;
3908 default:
3909 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
3910 return; /* Never happens. Keep compiler happy. */
3913 /* Align stack pointer. */
3914 /* ??? Should only do this if Configuration Control Register
3915 STACKALIGN bit is set. */
3916 if (env->regs[13] & 4) {
3917 env->regs[13] -= 4;
3918 xpsr |= 0x200;
3920 /* Switch to the handler mode. */
3921 v7m_push(env, xpsr);
3922 v7m_push(env, env->regs[15]);
3923 v7m_push(env, env->regs[14]);
3924 v7m_push(env, env->regs[12]);
3925 v7m_push(env, env->regs[3]);
3926 v7m_push(env, env->regs[2]);
3927 v7m_push(env, env->regs[1]);
3928 v7m_push(env, env->regs[0]);
3929 switch_v7m_sp(env, 0);
3930 /* Clear IT bits */
3931 env->condexec_bits = 0;
3932 env->regs[14] = lr;
3933 addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
3934 env->regs[15] = addr & 0xfffffffe;
3935 env->thumb = addr & 1;
3938 /* Handle a CPU exception. */
3939 void arm_cpu_do_interrupt(CPUState *cs)
3941 ARMCPU *cpu = ARM_CPU(cs);
3942 CPUARMState *env = &cpu->env;
3943 uint32_t addr;
3944 uint32_t mask;
3945 int new_mode;
3946 uint32_t offset;
3947 uint32_t moe;
3949 assert(!IS_M(env));
3951 arm_log_exception(cs->exception_index);
3953 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
3954 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
3955 case EC_BREAKPOINT:
3956 case EC_BREAKPOINT_SAME_EL:
3957 moe = 1;
3958 break;
3959 case EC_WATCHPOINT:
3960 case EC_WATCHPOINT_SAME_EL:
3961 moe = 10;
3962 break;
3963 case EC_AA32_BKPT:
3964 moe = 3;
3965 break;
3966 case EC_VECTORCATCH:
3967 moe = 5;
3968 break;
3969 default:
3970 moe = 0;
3971 break;
3974 if (moe) {
3975 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
3978 /* TODO: Vectored interrupt controller. */
3979 switch (cs->exception_index) {
3980 case EXCP_UDEF:
3981 new_mode = ARM_CPU_MODE_UND;
3982 addr = 0x04;
3983 mask = CPSR_I;
3984 if (env->thumb)
3985 offset = 2;
3986 else
3987 offset = 4;
3988 break;
3989 case EXCP_SWI:
3990 if (semihosting_enabled) {
3991 /* Check for semihosting interrupt. */
3992 if (env->thumb) {
3993 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
3994 & 0xff;
3995 } else {
3996 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
3997 & 0xffffff;
3999 /* Only intercept calls from privileged modes, to provide some
4000 semblance of security. */
4001 if (((mask == 0x123456 && !env->thumb)
4002 || (mask == 0xab && env->thumb))
4003 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
4004 env->regs[0] = do_arm_semihosting(env);
4005 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
4006 return;
4009 new_mode = ARM_CPU_MODE_SVC;
4010 addr = 0x08;
4011 mask = CPSR_I;
4012 /* The PC already points to the next instruction. */
4013 offset = 0;
4014 break;
4015 case EXCP_BKPT:
4016 /* See if this is a semihosting syscall. */
4017 if (env->thumb && semihosting_enabled) {
4018 mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
4019 if (mask == 0xab
4020 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
4021 env->regs[15] += 2;
4022 env->regs[0] = do_arm_semihosting(env);
4023 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
4024 return;
4027 env->exception.fsr = 2;
4028 /* Fall through to prefetch abort. */
4029 case EXCP_PREFETCH_ABORT:
4030 env->cp15.ifsr_el2 = env->exception.fsr;
4031 env->cp15.far_el[1] = deposit64(env->cp15.far_el[1], 32, 32,
4032 env->exception.vaddress);
4033 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
4034 env->cp15.ifsr_el2, (uint32_t)env->exception.vaddress);
4035 new_mode = ARM_CPU_MODE_ABT;
4036 addr = 0x0c;
4037 mask = CPSR_A | CPSR_I;
4038 offset = 4;
4039 break;
4040 case EXCP_DATA_ABORT:
4041 env->cp15.esr_el[1] = env->exception.fsr;
4042 env->cp15.far_el[1] = deposit64(env->cp15.far_el[1], 0, 32,
4043 env->exception.vaddress);
4044 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4045 (uint32_t)env->cp15.esr_el[1],
4046 (uint32_t)env->exception.vaddress);
4047 new_mode = ARM_CPU_MODE_ABT;
4048 addr = 0x10;
4049 mask = CPSR_A | CPSR_I;
4050 offset = 8;
4051 break;
4052 case EXCP_IRQ:
4053 new_mode = ARM_CPU_MODE_IRQ;
4054 addr = 0x18;
4055 /* Disable IRQ and imprecise data aborts. */
4056 mask = CPSR_A | CPSR_I;
4057 offset = 4;
4058 break;
4059 case EXCP_FIQ:
4060 new_mode = ARM_CPU_MODE_FIQ;
4061 addr = 0x1c;
4062 /* Disable FIQ, IRQ and imprecise data aborts. */
4063 mask = CPSR_A | CPSR_I | CPSR_F;
4064 offset = 4;
4065 break;
4066 default:
4067 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
4068 return; /* Never happens. Keep compiler happy. */
4070 /* High vectors. */
4071 if (env->cp15.c1_sys & SCTLR_V) {
4072 /* when enabled, base address cannot be remapped. */
4073 addr += 0xffff0000;
4074 } else {
4075 /* ARM v7 architectures provide a vector base address register to remap
4076 * the interrupt vector table.
4077 * This register is only followed in non-monitor mode, and has a secure
4078 * and un-secure copy. Since the cpu is always in a un-secure operation
4079 * and is never in monitor mode this feature is always active.
4080 * Note: only bits 31:5 are valid.
4082 addr += env->cp15.vbar_el[1];
4084 switch_mode (env, new_mode);
4085 /* For exceptions taken to AArch32 we must clear the SS bit in both
4086 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
4088 env->uncached_cpsr &= ~PSTATE_SS;
4089 env->spsr = cpsr_read(env);
4090 /* Clear IT bits. */
4091 env->condexec_bits = 0;
4092 /* Switch to the new mode, and to the correct instruction set. */
4093 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
4094 env->daif |= mask;
4095 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
4096 * and we should just guard the thumb mode on V4 */
4097 if (arm_feature(env, ARM_FEATURE_V4T)) {
4098 env->thumb = (env->cp15.c1_sys & SCTLR_TE) != 0;
4100 env->regs[14] = env->regs[15] + offset;
4101 env->regs[15] = addr;
4102 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
4105 /* Check section/page access permissions.
4106 Returns the page protection flags, or zero if the access is not
4107 permitted. */
4108 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
4109 int access_type, int is_user)
4111 int prot_ro;
4113 if (domain_prot == 3) {
4114 return PAGE_READ | PAGE_WRITE;
4117 if (access_type == 1)
4118 prot_ro = 0;
4119 else
4120 prot_ro = PAGE_READ;
4122 switch (ap) {
4123 case 0:
4124 if (arm_feature(env, ARM_FEATURE_V7)) {
4125 return 0;
4127 if (access_type == 1)
4128 return 0;
4129 switch (env->cp15.c1_sys & (SCTLR_S | SCTLR_R)) {
4130 case SCTLR_S:
4131 return is_user ? 0 : PAGE_READ;
4132 case SCTLR_R:
4133 return PAGE_READ;
4134 default:
4135 return 0;
4137 case 1:
4138 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
4139 case 2:
4140 if (is_user)
4141 return prot_ro;
4142 else
4143 return PAGE_READ | PAGE_WRITE;
4144 case 3:
4145 return PAGE_READ | PAGE_WRITE;
4146 case 4: /* Reserved. */
4147 return 0;
4148 case 5:
4149 return is_user ? 0 : prot_ro;
4150 case 6:
4151 return prot_ro;
4152 case 7:
4153 if (!arm_feature (env, ARM_FEATURE_V6K))
4154 return 0;
4155 return prot_ro;
4156 default:
4157 abort();
4161 static bool get_level1_table_address(CPUARMState *env, uint32_t *table,
4162 uint32_t address)
4164 if (address & env->cp15.c2_mask) {
4165 if ((env->cp15.c2_control & TTBCR_PD1)) {
4166 /* Translation table walk disabled for TTBR1 */
4167 return false;
4169 *table = env->cp15.ttbr1_el1 & 0xffffc000;
4170 } else {
4171 if ((env->cp15.c2_control & TTBCR_PD0)) {
4172 /* Translation table walk disabled for TTBR0 */
4173 return false;
4175 *table = env->cp15.ttbr0_el1 & env->cp15.c2_base_mask;
4177 *table |= (address >> 18) & 0x3ffc;
4178 return true;
4181 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
4182 int is_user, hwaddr *phys_ptr,
4183 int *prot, target_ulong *page_size)
4185 CPUState *cs = CPU(arm_env_get_cpu(env));
4186 int code;
4187 uint32_t table;
4188 uint32_t desc;
4189 int type;
4190 int ap;
4191 int domain = 0;
4192 int domain_prot;
4193 hwaddr phys_addr;
4195 /* Pagetable walk. */
4196 /* Lookup l1 descriptor. */
4197 if (!get_level1_table_address(env, &table, address)) {
4198 /* Section translation fault if page walk is disabled by PD0 or PD1 */
4199 code = 5;
4200 goto do_fault;
4202 desc = ldl_phys(cs->as, table);
4203 type = (desc & 3);
4204 domain = (desc >> 5) & 0x0f;
4205 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
4206 if (type == 0) {
4207 /* Section translation fault. */
4208 code = 5;
4209 goto do_fault;
4211 if (domain_prot == 0 || domain_prot == 2) {
4212 if (type == 2)
4213 code = 9; /* Section domain fault. */
4214 else
4215 code = 11; /* Page domain fault. */
4216 goto do_fault;
4218 if (type == 2) {
4219 /* 1Mb section. */
4220 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
4221 ap = (desc >> 10) & 3;
4222 code = 13;
4223 *page_size = 1024 * 1024;
4224 } else {
4225 /* Lookup l2 entry. */
4226 if (type == 1) {
4227 /* Coarse pagetable. */
4228 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
4229 } else {
4230 /* Fine pagetable. */
4231 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
4233 desc = ldl_phys(cs->as, table);
4234 switch (desc & 3) {
4235 case 0: /* Page translation fault. */
4236 code = 7;
4237 goto do_fault;
4238 case 1: /* 64k page. */
4239 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
4240 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
4241 *page_size = 0x10000;
4242 break;
4243 case 2: /* 4k page. */
4244 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
4245 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
4246 *page_size = 0x1000;
4247 break;
4248 case 3: /* 1k page. */
4249 if (type == 1) {
4250 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4251 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
4252 } else {
4253 /* Page translation fault. */
4254 code = 7;
4255 goto do_fault;
4257 } else {
4258 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
4260 ap = (desc >> 4) & 3;
4261 *page_size = 0x400;
4262 break;
4263 default:
4264 /* Never happens, but compiler isn't smart enough to tell. */
4265 abort();
4267 code = 15;
4269 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
4270 if (!*prot) {
4271 /* Access permission fault. */
4272 goto do_fault;
4274 *prot |= PAGE_EXEC;
4275 *phys_ptr = phys_addr;
4276 return 0;
4277 do_fault:
4278 return code | (domain << 4);
4281 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
4282 int is_user, hwaddr *phys_ptr,
4283 int *prot, target_ulong *page_size)
4285 CPUState *cs = CPU(arm_env_get_cpu(env));
4286 int code;
4287 uint32_t table;
4288 uint32_t desc;
4289 uint32_t xn;
4290 uint32_t pxn = 0;
4291 int type;
4292 int ap;
4293 int domain = 0;
4294 int domain_prot;
4295 hwaddr phys_addr;
4297 /* Pagetable walk. */
4298 /* Lookup l1 descriptor. */
4299 if (!get_level1_table_address(env, &table, address)) {
4300 /* Section translation fault if page walk is disabled by PD0 or PD1 */
4301 code = 5;
4302 goto do_fault;
4304 desc = ldl_phys(cs->as, table);
4305 type = (desc & 3);
4306 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
4307 /* Section translation fault, or attempt to use the encoding
4308 * which is Reserved on implementations without PXN.
4310 code = 5;
4311 goto do_fault;
4313 if ((type == 1) || !(desc & (1 << 18))) {
4314 /* Page or Section. */
4315 domain = (desc >> 5) & 0x0f;
4317 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
4318 if (domain_prot == 0 || domain_prot == 2) {
4319 if (type != 1) {
4320 code = 9; /* Section domain fault. */
4321 } else {
4322 code = 11; /* Page domain fault. */
4324 goto do_fault;
4326 if (type != 1) {
4327 if (desc & (1 << 18)) {
4328 /* Supersection. */
4329 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4330 *page_size = 0x1000000;
4331 } else {
4332 /* Section. */
4333 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
4334 *page_size = 0x100000;
4336 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
4337 xn = desc & (1 << 4);
4338 pxn = desc & 1;
4339 code = 13;
4340 } else {
4341 if (arm_feature(env, ARM_FEATURE_PXN)) {
4342 pxn = (desc >> 2) & 1;
4344 /* Lookup l2 entry. */
4345 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
4346 desc = ldl_phys(cs->as, table);
4347 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
4348 switch (desc & 3) {
4349 case 0: /* Page translation fault. */
4350 code = 7;
4351 goto do_fault;
4352 case 1: /* 64k page. */
4353 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
4354 xn = desc & (1 << 15);
4355 *page_size = 0x10000;
4356 break;
4357 case 2: case 3: /* 4k page. */
4358 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
4359 xn = desc & 1;
4360 *page_size = 0x1000;
4361 break;
4362 default:
4363 /* Never happens, but compiler isn't smart enough to tell. */
4364 abort();
4366 code = 15;
4368 if (domain_prot == 3) {
4369 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
4370 } else {
4371 if (pxn && !is_user) {
4372 xn = 1;
4374 if (xn && access_type == 2)
4375 goto do_fault;
4377 /* The simplified model uses AP[0] as an access control bit. */
4378 if ((env->cp15.c1_sys & SCTLR_AFE) && (ap & 1) == 0) {
4379 /* Access flag fault. */
4380 code = (code == 15) ? 6 : 3;
4381 goto do_fault;
4383 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
4384 if (!*prot) {
4385 /* Access permission fault. */
4386 goto do_fault;
4388 if (!xn) {
4389 *prot |= PAGE_EXEC;
4392 *phys_ptr = phys_addr;
4393 return 0;
4394 do_fault:
4395 return code | (domain << 4);
4398 /* Fault type for long-descriptor MMU fault reporting; this corresponds
4399 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
4401 typedef enum {
4402 translation_fault = 1,
4403 access_fault = 2,
4404 permission_fault = 3,
4405 } MMUFaultType;
4407 static int get_phys_addr_lpae(CPUARMState *env, target_ulong address,
4408 int access_type, int is_user,
4409 hwaddr *phys_ptr, int *prot,
4410 target_ulong *page_size_ptr)
4412 CPUState *cs = CPU(arm_env_get_cpu(env));
4413 /* Read an LPAE long-descriptor translation table. */
4414 MMUFaultType fault_type = translation_fault;
4415 uint32_t level = 1;
4416 uint32_t epd;
4417 int32_t tsz;
4418 uint32_t tg;
4419 uint64_t ttbr;
4420 int ttbr_select;
4421 hwaddr descaddr, descmask;
4422 uint32_t tableattrs;
4423 target_ulong page_size;
4424 uint32_t attrs;
4425 int32_t granule_sz = 9;
4426 int32_t va_size = 32;
4427 int32_t tbi = 0;
4429 if (arm_el_is_aa64(env, 1)) {
4430 va_size = 64;
4431 if (extract64(address, 55, 1))
4432 tbi = extract64(env->cp15.c2_control, 38, 1);
4433 else
4434 tbi = extract64(env->cp15.c2_control, 37, 1);
4435 tbi *= 8;
4438 /* Determine whether this address is in the region controlled by
4439 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
4440 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
4441 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
4443 uint32_t t0sz = extract32(env->cp15.c2_control, 0, 6);
4444 if (arm_el_is_aa64(env, 1)) {
4445 t0sz = MIN(t0sz, 39);
4446 t0sz = MAX(t0sz, 16);
4448 uint32_t t1sz = extract32(env->cp15.c2_control, 16, 6);
4449 if (arm_el_is_aa64(env, 1)) {
4450 t1sz = MIN(t1sz, 39);
4451 t1sz = MAX(t1sz, 16);
4453 if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
4454 /* there is a ttbr0 region and we are in it (high bits all zero) */
4455 ttbr_select = 0;
4456 } else if (t1sz && !extract64(~address, va_size - t1sz, t1sz - tbi)) {
4457 /* there is a ttbr1 region and we are in it (high bits all one) */
4458 ttbr_select = 1;
4459 } else if (!t0sz) {
4460 /* ttbr0 region is "everything not in the ttbr1 region" */
4461 ttbr_select = 0;
4462 } else if (!t1sz) {
4463 /* ttbr1 region is "everything not in the ttbr0 region" */
4464 ttbr_select = 1;
4465 } else {
4466 /* in the gap between the two regions, this is a Translation fault */
4467 fault_type = translation_fault;
4468 goto do_fault;
4471 /* Note that QEMU ignores shareability and cacheability attributes,
4472 * so we don't need to do anything with the SH, ORGN, IRGN fields
4473 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
4474 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
4475 * implement any ASID-like capability so we can ignore it (instead
4476 * we will always flush the TLB any time the ASID is changed).
4478 if (ttbr_select == 0) {
4479 ttbr = env->cp15.ttbr0_el1;
4480 epd = extract32(env->cp15.c2_control, 7, 1);
4481 tsz = t0sz;
4483 tg = extract32(env->cp15.c2_control, 14, 2);
4484 if (tg == 1) { /* 64KB pages */
4485 granule_sz = 13;
4487 if (tg == 2) { /* 16KB pages */
4488 granule_sz = 11;
4490 } else {
4491 ttbr = env->cp15.ttbr1_el1;
4492 epd = extract32(env->cp15.c2_control, 23, 1);
4493 tsz = t1sz;
4495 tg = extract32(env->cp15.c2_control, 30, 2);
4496 if (tg == 3) { /* 64KB pages */
4497 granule_sz = 13;
4499 if (tg == 1) { /* 16KB pages */
4500 granule_sz = 11;
4504 if (epd) {
4505 /* Translation table walk disabled => Translation fault on TLB miss */
4506 goto do_fault;
4509 /* The starting level depends on the virtual address size which can be
4510 * up to 48-bits and the translation granule size.
4512 if ((va_size - tsz) > (granule_sz * 4 + 3)) {
4513 level = 0;
4514 } else if ((va_size - tsz) > (granule_sz * 3 + 3)) {
4515 level = 1;
4516 } else {
4517 level = 2;
4520 /* Clear the vaddr bits which aren't part of the within-region address,
4521 * so that we don't have to special case things when calculating the
4522 * first descriptor address.
4524 if (tsz) {
4525 address &= (1ULL << (va_size - tsz)) - 1;
4528 descmask = (1ULL << (granule_sz + 3)) - 1;
4530 /* Now we can extract the actual base address from the TTBR */
4531 descaddr = extract64(ttbr, 0, 48);
4532 descaddr &= ~((1ULL << (va_size - tsz - (granule_sz * (4 - level)))) - 1);
4534 tableattrs = 0;
4535 for (;;) {
4536 uint64_t descriptor;
4538 descaddr |= (address >> (granule_sz * (4 - level))) & descmask;
4539 descaddr &= ~7ULL;
4540 descriptor = ldq_phys(cs->as, descaddr);
4541 if (!(descriptor & 1) ||
4542 (!(descriptor & 2) && (level == 3))) {
4543 /* Invalid, or the Reserved level 3 encoding */
4544 goto do_fault;
4546 descaddr = descriptor & 0xfffffff000ULL;
4548 if ((descriptor & 2) && (level < 3)) {
4549 /* Table entry. The top five bits are attributes which may
4550 * propagate down through lower levels of the table (and
4551 * which are all arranged so that 0 means "no effect", so
4552 * we can gather them up by ORing in the bits at each level).
4554 tableattrs |= extract64(descriptor, 59, 5);
4555 level++;
4556 continue;
4558 /* Block entry at level 1 or 2, or page entry at level 3.
4559 * These are basically the same thing, although the number
4560 * of bits we pull in from the vaddr varies.
4562 page_size = (1ULL << ((granule_sz * (4 - level)) + 3));
4563 descaddr |= (address & (page_size - 1));
4564 /* Extract attributes from the descriptor and merge with table attrs */
4565 attrs = extract64(descriptor, 2, 10)
4566 | (extract64(descriptor, 52, 12) << 10);
4567 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
4568 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
4569 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
4570 * means "force PL1 access only", which means forcing AP[1] to 0.
4572 if (extract32(tableattrs, 2, 1)) {
4573 attrs &= ~(1 << 4);
4575 /* Since we're always in the Non-secure state, NSTable is ignored. */
4576 break;
4578 /* Here descaddr is the final physical address, and attributes
4579 * are all in attrs.
4581 fault_type = access_fault;
4582 if ((attrs & (1 << 8)) == 0) {
4583 /* Access flag */
4584 goto do_fault;
4586 fault_type = permission_fault;
4587 if (is_user && !(attrs & (1 << 4))) {
4588 /* Unprivileged access not enabled */
4589 goto do_fault;
4591 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
4592 if ((arm_feature(env, ARM_FEATURE_V8) && is_user && (attrs & (1 << 12))) ||
4593 (!arm_feature(env, ARM_FEATURE_V8) && (attrs & (1 << 12))) ||
4594 (!is_user && (attrs & (1 << 11)))) {
4595 /* XN/UXN or PXN. Since we only implement EL0/EL1 we unconditionally
4596 * treat XN/UXN as UXN for v8.
4598 if (access_type == 2) {
4599 goto do_fault;
4601 *prot &= ~PAGE_EXEC;
4603 if (attrs & (1 << 5)) {
4604 /* Write access forbidden */
4605 if (access_type == 1) {
4606 goto do_fault;
4608 *prot &= ~PAGE_WRITE;
4611 *phys_ptr = descaddr;
4612 *page_size_ptr = page_size;
4613 return 0;
4615 do_fault:
4616 /* Long-descriptor format IFSR/DFSR value */
4617 return (1 << 9) | (fault_type << 2) | level;
4620 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
4621 int access_type, int is_user,
4622 hwaddr *phys_ptr, int *prot)
4624 int n;
4625 uint32_t mask;
4626 uint32_t base;
4628 *phys_ptr = address;
4629 for (n = 7; n >= 0; n--) {
4630 base = env->cp15.c6_region[n];
4631 if ((base & 1) == 0)
4632 continue;
4633 mask = 1 << ((base >> 1) & 0x1f);
4634 /* Keep this shift separate from the above to avoid an
4635 (undefined) << 32. */
4636 mask = (mask << 1) - 1;
4637 if (((base ^ address) & ~mask) == 0)
4638 break;
4640 if (n < 0)
4641 return 2;
4643 if (access_type == 2) {
4644 mask = env->cp15.pmsav5_insn_ap;
4645 } else {
4646 mask = env->cp15.pmsav5_data_ap;
4648 mask = (mask >> (n * 4)) & 0xf;
4649 switch (mask) {
4650 case 0:
4651 return 1;
4652 case 1:
4653 if (is_user)
4654 return 1;
4655 *prot = PAGE_READ | PAGE_WRITE;
4656 break;
4657 case 2:
4658 *prot = PAGE_READ;
4659 if (!is_user)
4660 *prot |= PAGE_WRITE;
4661 break;
4662 case 3:
4663 *prot = PAGE_READ | PAGE_WRITE;
4664 break;
4665 case 5:
4666 if (is_user)
4667 return 1;
4668 *prot = PAGE_READ;
4669 break;
4670 case 6:
4671 *prot = PAGE_READ;
4672 break;
4673 default:
4674 /* Bad permission. */
4675 return 1;
4677 *prot |= PAGE_EXEC;
4678 return 0;
4681 /* get_phys_addr - get the physical address for this virtual address
4683 * Find the physical address corresponding to the given virtual address,
4684 * by doing a translation table walk on MMU based systems or using the
4685 * MPU state on MPU based systems.
4687 * Returns 0 if the translation was successful. Otherwise, phys_ptr,
4688 * prot and page_size are not filled in, and the return value provides
4689 * information on why the translation aborted, in the format of a
4690 * DFSR/IFSR fault register, with the following caveats:
4691 * * we honour the short vs long DFSR format differences.
4692 * * the WnR bit is never set (the caller must do this).
4693 * * for MPU based systems we don't bother to return a full FSR format
4694 * value.
4696 * @env: CPUARMState
4697 * @address: virtual address to get physical address for
4698 * @access_type: 0 for read, 1 for write, 2 for execute
4699 * @is_user: 0 for privileged access, 1 for user
4700 * @phys_ptr: set to the physical address corresponding to the virtual address
4701 * @prot: set to the permissions for the page containing phys_ptr
4702 * @page_size: set to the size of the page containing phys_ptr
4704 static inline int get_phys_addr(CPUARMState *env, target_ulong address,
4705 int access_type, int is_user,
4706 hwaddr *phys_ptr, int *prot,
4707 target_ulong *page_size)
4709 /* Fast Context Switch Extension. */
4710 if (address < 0x02000000)
4711 address += env->cp15.c13_fcse;
4713 if ((env->cp15.c1_sys & SCTLR_M) == 0) {
4714 /* MMU/MPU disabled. */
4715 *phys_ptr = address;
4716 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
4717 *page_size = TARGET_PAGE_SIZE;
4718 return 0;
4719 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
4720 *page_size = TARGET_PAGE_SIZE;
4721 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
4722 prot);
4723 } else if (extended_addresses_enabled(env)) {
4724 return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
4725 prot, page_size);
4726 } else if (env->cp15.c1_sys & SCTLR_XP) {
4727 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
4728 prot, page_size);
4729 } else {
4730 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
4731 prot, page_size);
4735 int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
4736 int access_type, int mmu_idx)
4738 ARMCPU *cpu = ARM_CPU(cs);
4739 CPUARMState *env = &cpu->env;
4740 hwaddr phys_addr;
4741 target_ulong page_size;
4742 int prot;
4743 int ret, is_user;
4744 uint32_t syn;
4745 bool same_el = (arm_current_pl(env) != 0);
4747 is_user = mmu_idx == MMU_USER_IDX;
4748 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
4749 &page_size);
4750 if (ret == 0) {
4751 /* Map a single [sub]page. */
4752 phys_addr &= TARGET_PAGE_MASK;
4753 address &= TARGET_PAGE_MASK;
4754 tlb_set_page(cs, address, phys_addr, prot, mmu_idx, page_size);
4755 return 0;
4758 /* AArch64 syndrome does not have an LPAE bit */
4759 syn = ret & ~(1 << 9);
4761 /* For insn and data aborts we assume there is no instruction syndrome
4762 * information; this is always true for exceptions reported to EL1.
4764 if (access_type == 2) {
4765 syn = syn_insn_abort(same_el, 0, 0, syn);
4766 cs->exception_index = EXCP_PREFETCH_ABORT;
4767 } else {
4768 syn = syn_data_abort(same_el, 0, 0, 0, access_type == 1, syn);
4769 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6)) {
4770 ret |= (1 << 11);
4772 cs->exception_index = EXCP_DATA_ABORT;
4775 env->exception.syndrome = syn;
4776 env->exception.vaddress = address;
4777 env->exception.fsr = ret;
4778 return 1;
4781 hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
4783 ARMCPU *cpu = ARM_CPU(cs);
4784 hwaddr phys_addr;
4785 target_ulong page_size;
4786 int prot;
4787 int ret;
4789 ret = get_phys_addr(&cpu->env, addr, 0, 0, &phys_addr, &prot, &page_size);
4791 if (ret != 0) {
4792 return -1;
4795 return phys_addr;
4798 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
4800 if ((env->uncached_cpsr & CPSR_M) == mode) {
4801 env->regs[13] = val;
4802 } else {
4803 env->banked_r13[bank_number(mode)] = val;
4807 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
4809 if ((env->uncached_cpsr & CPSR_M) == mode) {
4810 return env->regs[13];
4811 } else {
4812 return env->banked_r13[bank_number(mode)];
4816 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
4818 ARMCPU *cpu = arm_env_get_cpu(env);
4820 switch (reg) {
4821 case 0: /* APSR */
4822 return xpsr_read(env) & 0xf8000000;
4823 case 1: /* IAPSR */
4824 return xpsr_read(env) & 0xf80001ff;
4825 case 2: /* EAPSR */
4826 return xpsr_read(env) & 0xff00fc00;
4827 case 3: /* xPSR */
4828 return xpsr_read(env) & 0xff00fdff;
4829 case 5: /* IPSR */
4830 return xpsr_read(env) & 0x000001ff;
4831 case 6: /* EPSR */
4832 return xpsr_read(env) & 0x0700fc00;
4833 case 7: /* IEPSR */
4834 return xpsr_read(env) & 0x0700edff;
4835 case 8: /* MSP */
4836 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
4837 case 9: /* PSP */
4838 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
4839 case 16: /* PRIMASK */
4840 return (env->daif & PSTATE_I) != 0;
4841 case 17: /* BASEPRI */
4842 case 18: /* BASEPRI_MAX */
4843 return env->v7m.basepri;
4844 case 19: /* FAULTMASK */
4845 return (env->daif & PSTATE_F) != 0;
4846 case 20: /* CONTROL */
4847 return env->v7m.control;
4848 default:
4849 /* ??? For debugging only. */
4850 cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
4851 return 0;
4855 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
4857 ARMCPU *cpu = arm_env_get_cpu(env);
4859 switch (reg) {
4860 case 0: /* APSR */
4861 xpsr_write(env, val, 0xf8000000);
4862 break;
4863 case 1: /* IAPSR */
4864 xpsr_write(env, val, 0xf8000000);
4865 break;
4866 case 2: /* EAPSR */
4867 xpsr_write(env, val, 0xfe00fc00);
4868 break;
4869 case 3: /* xPSR */
4870 xpsr_write(env, val, 0xfe00fc00);
4871 break;
4872 case 5: /* IPSR */
4873 /* IPSR bits are readonly. */
4874 break;
4875 case 6: /* EPSR */
4876 xpsr_write(env, val, 0x0600fc00);
4877 break;
4878 case 7: /* IEPSR */
4879 xpsr_write(env, val, 0x0600fc00);
4880 break;
4881 case 8: /* MSP */
4882 if (env->v7m.current_sp)
4883 env->v7m.other_sp = val;
4884 else
4885 env->regs[13] = val;
4886 break;
4887 case 9: /* PSP */
4888 if (env->v7m.current_sp)
4889 env->regs[13] = val;
4890 else
4891 env->v7m.other_sp = val;
4892 break;
4893 case 16: /* PRIMASK */
4894 if (val & 1) {
4895 env->daif |= PSTATE_I;
4896 } else {
4897 env->daif &= ~PSTATE_I;
4899 break;
4900 case 17: /* BASEPRI */
4901 env->v7m.basepri = val & 0xff;
4902 break;
4903 case 18: /* BASEPRI_MAX */
4904 val &= 0xff;
4905 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
4906 env->v7m.basepri = val;
4907 break;
4908 case 19: /* FAULTMASK */
4909 if (val & 1) {
4910 env->daif |= PSTATE_F;
4911 } else {
4912 env->daif &= ~PSTATE_F;
4914 break;
4915 case 20: /* CONTROL */
4916 env->v7m.control = val & 3;
4917 switch_v7m_sp(env, (val & 2) != 0);
4918 break;
4919 default:
4920 /* ??? For debugging only. */
4921 cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
4922 return;
4926 #endif
4928 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
4930 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
4931 * Note that we do not implement the (architecturally mandated)
4932 * alignment fault for attempts to use this on Device memory
4933 * (which matches the usual QEMU behaviour of not implementing either
4934 * alignment faults or any memory attribute handling).
4937 ARMCPU *cpu = arm_env_get_cpu(env);
4938 uint64_t blocklen = 4 << cpu->dcz_blocksize;
4939 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
4941 #ifndef CONFIG_USER_ONLY
4943 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
4944 * the block size so we might have to do more than one TLB lookup.
4945 * We know that in fact for any v8 CPU the page size is at least 4K
4946 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
4947 * 1K as an artefact of legacy v5 subpage support being present in the
4948 * same QEMU executable.
4950 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
4951 void *hostaddr[maxidx];
4952 int try, i;
4954 for (try = 0; try < 2; try++) {
4956 for (i = 0; i < maxidx; i++) {
4957 hostaddr[i] = tlb_vaddr_to_host(env,
4958 vaddr + TARGET_PAGE_SIZE * i,
4959 1, cpu_mmu_index(env));
4960 if (!hostaddr[i]) {
4961 break;
4964 if (i == maxidx) {
4965 /* If it's all in the TLB it's fair game for just writing to;
4966 * we know we don't need to update dirty status, etc.
4968 for (i = 0; i < maxidx - 1; i++) {
4969 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
4971 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
4972 return;
4974 /* OK, try a store and see if we can populate the tlb. This
4975 * might cause an exception if the memory isn't writable,
4976 * in which case we will longjmp out of here. We must for
4977 * this purpose use the actual register value passed to us
4978 * so that we get the fault address right.
4980 helper_ret_stb_mmu(env, vaddr_in, 0, cpu_mmu_index(env), GETRA());
4981 /* Now we can populate the other TLB entries, if any */
4982 for (i = 0; i < maxidx; i++) {
4983 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
4984 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
4985 helper_ret_stb_mmu(env, va, 0, cpu_mmu_index(env), GETRA());
4990 /* Slow path (probably attempt to do this to an I/O device or
4991 * similar, or clearing of a block of code we have translations
4992 * cached for). Just do a series of byte writes as the architecture
4993 * demands. It's not worth trying to use a cpu_physical_memory_map(),
4994 * memset(), unmap() sequence here because:
4995 * + we'd need to account for the blocksize being larger than a page
4996 * + the direct-RAM access case is almost always going to be dealt
4997 * with in the fastpath code above, so there's no speed benefit
4998 * + we would have to deal with the map returning NULL because the
4999 * bounce buffer was in use
5001 for (i = 0; i < blocklen; i++) {
5002 helper_ret_stb_mmu(env, vaddr + i, 0, cpu_mmu_index(env), GETRA());
5005 #else
5006 memset(g2h(vaddr), 0, blocklen);
5007 #endif
5010 /* Note that signed overflow is undefined in C. The following routines are
5011 careful to use unsigned types where modulo arithmetic is required.
5012 Failure to do so _will_ break on newer gcc. */
5014 /* Signed saturating arithmetic. */
5016 /* Perform 16-bit signed saturating addition. */
5017 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
5019 uint16_t res;
5021 res = a + b;
5022 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
5023 if (a & 0x8000)
5024 res = 0x8000;
5025 else
5026 res = 0x7fff;
5028 return res;
5031 /* Perform 8-bit signed saturating addition. */
5032 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
5034 uint8_t res;
5036 res = a + b;
5037 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
5038 if (a & 0x80)
5039 res = 0x80;
5040 else
5041 res = 0x7f;
5043 return res;
5046 /* Perform 16-bit signed saturating subtraction. */
5047 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
5049 uint16_t res;
5051 res = a - b;
5052 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
5053 if (a & 0x8000)
5054 res = 0x8000;
5055 else
5056 res = 0x7fff;
5058 return res;
5061 /* Perform 8-bit signed saturating subtraction. */
5062 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
5064 uint8_t res;
5066 res = a - b;
5067 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
5068 if (a & 0x80)
5069 res = 0x80;
5070 else
5071 res = 0x7f;
5073 return res;
5076 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
5077 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
5078 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
5079 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
5080 #define PFX q
5082 #include "op_addsub.h"
5084 /* Unsigned saturating arithmetic. */
5085 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
5087 uint16_t res;
5088 res = a + b;
5089 if (res < a)
5090 res = 0xffff;
5091 return res;
5094 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
5096 if (a > b)
5097 return a - b;
5098 else
5099 return 0;
5102 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
5104 uint8_t res;
5105 res = a + b;
5106 if (res < a)
5107 res = 0xff;
5108 return res;
5111 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
5113 if (a > b)
5114 return a - b;
5115 else
5116 return 0;
5119 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
5120 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
5121 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
5122 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
5123 #define PFX uq
5125 #include "op_addsub.h"
5127 /* Signed modulo arithmetic. */
5128 #define SARITH16(a, b, n, op) do { \
5129 int32_t sum; \
5130 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
5131 RESULT(sum, n, 16); \
5132 if (sum >= 0) \
5133 ge |= 3 << (n * 2); \
5134 } while(0)
5136 #define SARITH8(a, b, n, op) do { \
5137 int32_t sum; \
5138 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
5139 RESULT(sum, n, 8); \
5140 if (sum >= 0) \
5141 ge |= 1 << n; \
5142 } while(0)
5145 #define ADD16(a, b, n) SARITH16(a, b, n, +)
5146 #define SUB16(a, b, n) SARITH16(a, b, n, -)
5147 #define ADD8(a, b, n) SARITH8(a, b, n, +)
5148 #define SUB8(a, b, n) SARITH8(a, b, n, -)
5149 #define PFX s
5150 #define ARITH_GE
5152 #include "op_addsub.h"
5154 /* Unsigned modulo arithmetic. */
5155 #define ADD16(a, b, n) do { \
5156 uint32_t sum; \
5157 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
5158 RESULT(sum, n, 16); \
5159 if ((sum >> 16) == 1) \
5160 ge |= 3 << (n * 2); \
5161 } while(0)
5163 #define ADD8(a, b, n) do { \
5164 uint32_t sum; \
5165 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
5166 RESULT(sum, n, 8); \
5167 if ((sum >> 8) == 1) \
5168 ge |= 1 << n; \
5169 } while(0)
5171 #define SUB16(a, b, n) do { \
5172 uint32_t sum; \
5173 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
5174 RESULT(sum, n, 16); \
5175 if ((sum >> 16) == 0) \
5176 ge |= 3 << (n * 2); \
5177 } while(0)
5179 #define SUB8(a, b, n) do { \
5180 uint32_t sum; \
5181 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
5182 RESULT(sum, n, 8); \
5183 if ((sum >> 8) == 0) \
5184 ge |= 1 << n; \
5185 } while(0)
5187 #define PFX u
5188 #define ARITH_GE
5190 #include "op_addsub.h"
5192 /* Halved signed arithmetic. */
5193 #define ADD16(a, b, n) \
5194 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
5195 #define SUB16(a, b, n) \
5196 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
5197 #define ADD8(a, b, n) \
5198 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
5199 #define SUB8(a, b, n) \
5200 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
5201 #define PFX sh
5203 #include "op_addsub.h"
5205 /* Halved unsigned arithmetic. */
5206 #define ADD16(a, b, n) \
5207 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
5208 #define SUB16(a, b, n) \
5209 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
5210 #define ADD8(a, b, n) \
5211 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
5212 #define SUB8(a, b, n) \
5213 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
5214 #define PFX uh
5216 #include "op_addsub.h"
5218 static inline uint8_t do_usad(uint8_t a, uint8_t b)
5220 if (a > b)
5221 return a - b;
5222 else
5223 return b - a;
5226 /* Unsigned sum of absolute byte differences. */
5227 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
5229 uint32_t sum;
5230 sum = do_usad(a, b);
5231 sum += do_usad(a >> 8, b >> 8);
5232 sum += do_usad(a >> 16, b >>16);
5233 sum += do_usad(a >> 24, b >> 24);
5234 return sum;
5237 /* For ARMv6 SEL instruction. */
5238 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
5240 uint32_t mask;
5242 mask = 0;
5243 if (flags & 1)
5244 mask |= 0xff;
5245 if (flags & 2)
5246 mask |= 0xff00;
5247 if (flags & 4)
5248 mask |= 0xff0000;
5249 if (flags & 8)
5250 mask |= 0xff000000;
5251 return (a & mask) | (b & ~mask);
5254 /* VFP support. We follow the convention used for VFP instructions:
5255 Single precision routines have a "s" suffix, double precision a
5256 "d" suffix. */
5258 /* Convert host exception flags to vfp form. */
5259 static inline int vfp_exceptbits_from_host(int host_bits)
5261 int target_bits = 0;
5263 if (host_bits & float_flag_invalid)
5264 target_bits |= 1;
5265 if (host_bits & float_flag_divbyzero)
5266 target_bits |= 2;
5267 if (host_bits & float_flag_overflow)
5268 target_bits |= 4;
5269 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
5270 target_bits |= 8;
5271 if (host_bits & float_flag_inexact)
5272 target_bits |= 0x10;
5273 if (host_bits & float_flag_input_denormal)
5274 target_bits |= 0x80;
5275 return target_bits;
5278 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
5280 int i;
5281 uint32_t fpscr;
5283 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
5284 | (env->vfp.vec_len << 16)
5285 | (env->vfp.vec_stride << 20);
5286 i = get_float_exception_flags(&env->vfp.fp_status);
5287 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
5288 fpscr |= vfp_exceptbits_from_host(i);
5289 return fpscr;
5292 uint32_t vfp_get_fpscr(CPUARMState *env)
5294 return HELPER(vfp_get_fpscr)(env);
5297 /* Convert vfp exception flags to target form. */
5298 static inline int vfp_exceptbits_to_host(int target_bits)
5300 int host_bits = 0;
5302 if (target_bits & 1)
5303 host_bits |= float_flag_invalid;
5304 if (target_bits & 2)
5305 host_bits |= float_flag_divbyzero;
5306 if (target_bits & 4)
5307 host_bits |= float_flag_overflow;
5308 if (target_bits & 8)
5309 host_bits |= float_flag_underflow;
5310 if (target_bits & 0x10)
5311 host_bits |= float_flag_inexact;
5312 if (target_bits & 0x80)
5313 host_bits |= float_flag_input_denormal;
5314 return host_bits;
5317 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
5319 int i;
5320 uint32_t changed;
5322 changed = env->vfp.xregs[ARM_VFP_FPSCR];
5323 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
5324 env->vfp.vec_len = (val >> 16) & 7;
5325 env->vfp.vec_stride = (val >> 20) & 3;
5327 changed ^= val;
5328 if (changed & (3 << 22)) {
5329 i = (val >> 22) & 3;
5330 switch (i) {
5331 case FPROUNDING_TIEEVEN:
5332 i = float_round_nearest_even;
5333 break;
5334 case FPROUNDING_POSINF:
5335 i = float_round_up;
5336 break;
5337 case FPROUNDING_NEGINF:
5338 i = float_round_down;
5339 break;
5340 case FPROUNDING_ZERO:
5341 i = float_round_to_zero;
5342 break;
5344 set_float_rounding_mode(i, &env->vfp.fp_status);
5346 if (changed & (1 << 24)) {
5347 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
5348 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
5350 if (changed & (1 << 25))
5351 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
5353 i = vfp_exceptbits_to_host(val);
5354 set_float_exception_flags(i, &env->vfp.fp_status);
5355 set_float_exception_flags(0, &env->vfp.standard_fp_status);
5358 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
5360 HELPER(vfp_set_fpscr)(env, val);
5363 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
5365 #define VFP_BINOP(name) \
5366 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
5368 float_status *fpst = fpstp; \
5369 return float32_ ## name(a, b, fpst); \
5371 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
5373 float_status *fpst = fpstp; \
5374 return float64_ ## name(a, b, fpst); \
5376 VFP_BINOP(add)
5377 VFP_BINOP(sub)
5378 VFP_BINOP(mul)
5379 VFP_BINOP(div)
5380 VFP_BINOP(min)
5381 VFP_BINOP(max)
5382 VFP_BINOP(minnum)
5383 VFP_BINOP(maxnum)
5384 #undef VFP_BINOP
5386 float32 VFP_HELPER(neg, s)(float32 a)
5388 return float32_chs(a);
5391 float64 VFP_HELPER(neg, d)(float64 a)
5393 return float64_chs(a);
5396 float32 VFP_HELPER(abs, s)(float32 a)
5398 return float32_abs(a);
5401 float64 VFP_HELPER(abs, d)(float64 a)
5403 return float64_abs(a);
5406 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
5408 return float32_sqrt(a, &env->vfp.fp_status);
5411 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
5413 return float64_sqrt(a, &env->vfp.fp_status);
5416 /* XXX: check quiet/signaling case */
5417 #define DO_VFP_cmp(p, type) \
5418 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
5420 uint32_t flags; \
5421 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
5422 case 0: flags = 0x6; break; \
5423 case -1: flags = 0x8; break; \
5424 case 1: flags = 0x2; break; \
5425 default: case 2: flags = 0x3; break; \
5427 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
5428 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
5430 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
5432 uint32_t flags; \
5433 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
5434 case 0: flags = 0x6; break; \
5435 case -1: flags = 0x8; break; \
5436 case 1: flags = 0x2; break; \
5437 default: case 2: flags = 0x3; break; \
5439 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
5440 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
5442 DO_VFP_cmp(s, float32)
5443 DO_VFP_cmp(d, float64)
5444 #undef DO_VFP_cmp
5446 /* Integer to float and float to integer conversions */
5448 #define CONV_ITOF(name, fsz, sign) \
5449 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
5451 float_status *fpst = fpstp; \
5452 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
5455 #define CONV_FTOI(name, fsz, sign, round) \
5456 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
5458 float_status *fpst = fpstp; \
5459 if (float##fsz##_is_any_nan(x)) { \
5460 float_raise(float_flag_invalid, fpst); \
5461 return 0; \
5463 return float##fsz##_to_##sign##int32##round(x, fpst); \
5466 #define FLOAT_CONVS(name, p, fsz, sign) \
5467 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
5468 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
5469 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
5471 FLOAT_CONVS(si, s, 32, )
5472 FLOAT_CONVS(si, d, 64, )
5473 FLOAT_CONVS(ui, s, 32, u)
5474 FLOAT_CONVS(ui, d, 64, u)
5476 #undef CONV_ITOF
5477 #undef CONV_FTOI
5478 #undef FLOAT_CONVS
5480 /* floating point conversion */
5481 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
5483 float64 r = float32_to_float64(x, &env->vfp.fp_status);
5484 /* ARM requires that S<->D conversion of any kind of NaN generates
5485 * a quiet NaN by forcing the most significant frac bit to 1.
5487 return float64_maybe_silence_nan(r);
5490 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
5492 float32 r = float64_to_float32(x, &env->vfp.fp_status);
5493 /* ARM requires that S<->D conversion of any kind of NaN generates
5494 * a quiet NaN by forcing the most significant frac bit to 1.
5496 return float32_maybe_silence_nan(r);
5499 /* VFP3 fixed point conversion. */
5500 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
5501 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
5502 void *fpstp) \
5504 float_status *fpst = fpstp; \
5505 float##fsz tmp; \
5506 tmp = itype##_to_##float##fsz(x, fpst); \
5507 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
5510 /* Notice that we want only input-denormal exception flags from the
5511 * scalbn operation: the other possible flags (overflow+inexact if
5512 * we overflow to infinity, output-denormal) aren't correct for the
5513 * complete scale-and-convert operation.
5515 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
5516 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
5517 uint32_t shift, \
5518 void *fpstp) \
5520 float_status *fpst = fpstp; \
5521 int old_exc_flags = get_float_exception_flags(fpst); \
5522 float##fsz tmp; \
5523 if (float##fsz##_is_any_nan(x)) { \
5524 float_raise(float_flag_invalid, fpst); \
5525 return 0; \
5527 tmp = float##fsz##_scalbn(x, shift, fpst); \
5528 old_exc_flags |= get_float_exception_flags(fpst) \
5529 & float_flag_input_denormal; \
5530 set_float_exception_flags(old_exc_flags, fpst); \
5531 return float##fsz##_to_##itype##round(tmp, fpst); \
5534 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \
5535 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
5536 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
5537 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
5539 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
5540 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
5541 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
5543 VFP_CONV_FIX(sh, d, 64, 64, int16)
5544 VFP_CONV_FIX(sl, d, 64, 64, int32)
5545 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
5546 VFP_CONV_FIX(uh, d, 64, 64, uint16)
5547 VFP_CONV_FIX(ul, d, 64, 64, uint32)
5548 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
5549 VFP_CONV_FIX(sh, s, 32, 32, int16)
5550 VFP_CONV_FIX(sl, s, 32, 32, int32)
5551 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
5552 VFP_CONV_FIX(uh, s, 32, 32, uint16)
5553 VFP_CONV_FIX(ul, s, 32, 32, uint32)
5554 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
5555 #undef VFP_CONV_FIX
5556 #undef VFP_CONV_FIX_FLOAT
5557 #undef VFP_CONV_FLOAT_FIX_ROUND
5559 /* Set the current fp rounding mode and return the old one.
5560 * The argument is a softfloat float_round_ value.
5562 uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
5564 float_status *fp_status = &env->vfp.fp_status;
5566 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
5567 set_float_rounding_mode(rmode, fp_status);
5569 return prev_rmode;
5572 /* Set the current fp rounding mode in the standard fp status and return
5573 * the old one. This is for NEON instructions that need to change the
5574 * rounding mode but wish to use the standard FPSCR values for everything
5575 * else. Always set the rounding mode back to the correct value after
5576 * modifying it.
5577 * The argument is a softfloat float_round_ value.
5579 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
5581 float_status *fp_status = &env->vfp.standard_fp_status;
5583 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
5584 set_float_rounding_mode(rmode, fp_status);
5586 return prev_rmode;
5589 /* Half precision conversions. */
5590 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
5592 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
5593 float32 r = float16_to_float32(make_float16(a), ieee, s);
5594 if (ieee) {
5595 return float32_maybe_silence_nan(r);
5597 return r;
5600 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
5602 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
5603 float16 r = float32_to_float16(a, ieee, s);
5604 if (ieee) {
5605 r = float16_maybe_silence_nan(r);
5607 return float16_val(r);
5610 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
5612 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
5615 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
5617 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
5620 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
5622 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
5625 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
5627 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
5630 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
5632 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
5633 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
5634 if (ieee) {
5635 return float64_maybe_silence_nan(r);
5637 return r;
5640 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
5642 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
5643 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
5644 if (ieee) {
5645 r = float16_maybe_silence_nan(r);
5647 return float16_val(r);
5650 #define float32_two make_float32(0x40000000)
5651 #define float32_three make_float32(0x40400000)
5652 #define float32_one_point_five make_float32(0x3fc00000)
5654 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
5656 float_status *s = &env->vfp.standard_fp_status;
5657 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
5658 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
5659 if (!(float32_is_zero(a) || float32_is_zero(b))) {
5660 float_raise(float_flag_input_denormal, s);
5662 return float32_two;
5664 return float32_sub(float32_two, float32_mul(a, b, s), s);
5667 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
5669 float_status *s = &env->vfp.standard_fp_status;
5670 float32 product;
5671 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
5672 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
5673 if (!(float32_is_zero(a) || float32_is_zero(b))) {
5674 float_raise(float_flag_input_denormal, s);
5676 return float32_one_point_five;
5678 product = float32_mul(a, b, s);
5679 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
5682 /* NEON helpers. */
5684 /* Constants 256 and 512 are used in some helpers; we avoid relying on
5685 * int->float conversions at run-time. */
5686 #define float64_256 make_float64(0x4070000000000000LL)
5687 #define float64_512 make_float64(0x4080000000000000LL)
5688 #define float32_maxnorm make_float32(0x7f7fffff)
5689 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
5691 /* Reciprocal functions
5693 * The algorithm that must be used to calculate the estimate
5694 * is specified by the ARM ARM, see FPRecipEstimate()
5697 static float64 recip_estimate(float64 a, float_status *real_fp_status)
5699 /* These calculations mustn't set any fp exception flags,
5700 * so we use a local copy of the fp_status.
5702 float_status dummy_status = *real_fp_status;
5703 float_status *s = &dummy_status;
5704 /* q = (int)(a * 512.0) */
5705 float64 q = float64_mul(float64_512, a, s);
5706 int64_t q_int = float64_to_int64_round_to_zero(q, s);
5708 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
5709 q = int64_to_float64(q_int, s);
5710 q = float64_add(q, float64_half, s);
5711 q = float64_div(q, float64_512, s);
5712 q = float64_div(float64_one, q, s);
5714 /* s = (int)(256.0 * r + 0.5) */
5715 q = float64_mul(q, float64_256, s);
5716 q = float64_add(q, float64_half, s);
5717 q_int = float64_to_int64_round_to_zero(q, s);
5719 /* return (double)s / 256.0 */
5720 return float64_div(int64_to_float64(q_int, s), float64_256, s);
5723 /* Common wrapper to call recip_estimate */
5724 static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
5726 uint64_t val64 = float64_val(num);
5727 uint64_t frac = extract64(val64, 0, 52);
5728 int64_t exp = extract64(val64, 52, 11);
5729 uint64_t sbit;
5730 float64 scaled, estimate;
5732 /* Generate the scaled number for the estimate function */
5733 if (exp == 0) {
5734 if (extract64(frac, 51, 1) == 0) {
5735 exp = -1;
5736 frac = extract64(frac, 0, 50) << 2;
5737 } else {
5738 frac = extract64(frac, 0, 51) << 1;
5742 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
5743 scaled = make_float64((0x3feULL << 52)
5744 | extract64(frac, 44, 8) << 44);
5746 estimate = recip_estimate(scaled, fpst);
5748 /* Build new result */
5749 val64 = float64_val(estimate);
5750 sbit = 0x8000000000000000ULL & val64;
5751 exp = off - exp;
5752 frac = extract64(val64, 0, 52);
5754 if (exp == 0) {
5755 frac = 1ULL << 51 | extract64(frac, 1, 51);
5756 } else if (exp == -1) {
5757 frac = 1ULL << 50 | extract64(frac, 2, 50);
5758 exp = 0;
5761 return make_float64(sbit | (exp << 52) | frac);
5764 static bool round_to_inf(float_status *fpst, bool sign_bit)
5766 switch (fpst->float_rounding_mode) {
5767 case float_round_nearest_even: /* Round to Nearest */
5768 return true;
5769 case float_round_up: /* Round to +Inf */
5770 return !sign_bit;
5771 case float_round_down: /* Round to -Inf */
5772 return sign_bit;
5773 case float_round_to_zero: /* Round to Zero */
5774 return false;
5777 g_assert_not_reached();
5780 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
5782 float_status *fpst = fpstp;
5783 float32 f32 = float32_squash_input_denormal(input, fpst);
5784 uint32_t f32_val = float32_val(f32);
5785 uint32_t f32_sbit = 0x80000000ULL & f32_val;
5786 int32_t f32_exp = extract32(f32_val, 23, 8);
5787 uint32_t f32_frac = extract32(f32_val, 0, 23);
5788 float64 f64, r64;
5789 uint64_t r64_val;
5790 int64_t r64_exp;
5791 uint64_t r64_frac;
5793 if (float32_is_any_nan(f32)) {
5794 float32 nan = f32;
5795 if (float32_is_signaling_nan(f32)) {
5796 float_raise(float_flag_invalid, fpst);
5797 nan = float32_maybe_silence_nan(f32);
5799 if (fpst->default_nan_mode) {
5800 nan = float32_default_nan;
5802 return nan;
5803 } else if (float32_is_infinity(f32)) {
5804 return float32_set_sign(float32_zero, float32_is_neg(f32));
5805 } else if (float32_is_zero(f32)) {
5806 float_raise(float_flag_divbyzero, fpst);
5807 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5808 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
5809 /* Abs(value) < 2.0^-128 */
5810 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5811 if (round_to_inf(fpst, f32_sbit)) {
5812 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5813 } else {
5814 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
5816 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
5817 float_raise(float_flag_underflow, fpst);
5818 return float32_set_sign(float32_zero, float32_is_neg(f32));
5822 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
5823 r64 = call_recip_estimate(f64, 253, fpst);
5824 r64_val = float64_val(r64);
5825 r64_exp = extract64(r64_val, 52, 11);
5826 r64_frac = extract64(r64_val, 0, 52);
5828 /* result = sign : result_exp<7:0> : fraction<51:29>; */
5829 return make_float32(f32_sbit |
5830 (r64_exp & 0xff) << 23 |
5831 extract64(r64_frac, 29, 24));
5834 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
5836 float_status *fpst = fpstp;
5837 float64 f64 = float64_squash_input_denormal(input, fpst);
5838 uint64_t f64_val = float64_val(f64);
5839 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
5840 int64_t f64_exp = extract64(f64_val, 52, 11);
5841 float64 r64;
5842 uint64_t r64_val;
5843 int64_t r64_exp;
5844 uint64_t r64_frac;
5846 /* Deal with any special cases */
5847 if (float64_is_any_nan(f64)) {
5848 float64 nan = f64;
5849 if (float64_is_signaling_nan(f64)) {
5850 float_raise(float_flag_invalid, fpst);
5851 nan = float64_maybe_silence_nan(f64);
5853 if (fpst->default_nan_mode) {
5854 nan = float64_default_nan;
5856 return nan;
5857 } else if (float64_is_infinity(f64)) {
5858 return float64_set_sign(float64_zero, float64_is_neg(f64));
5859 } else if (float64_is_zero(f64)) {
5860 float_raise(float_flag_divbyzero, fpst);
5861 return float64_set_sign(float64_infinity, float64_is_neg(f64));
5862 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
5863 /* Abs(value) < 2.0^-1024 */
5864 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5865 if (round_to_inf(fpst, f64_sbit)) {
5866 return float64_set_sign(float64_infinity, float64_is_neg(f64));
5867 } else {
5868 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
5870 } else if (f64_exp >= 1023 && fpst->flush_to_zero) {
5871 float_raise(float_flag_underflow, fpst);
5872 return float64_set_sign(float64_zero, float64_is_neg(f64));
5875 r64 = call_recip_estimate(f64, 2045, fpst);
5876 r64_val = float64_val(r64);
5877 r64_exp = extract64(r64_val, 52, 11);
5878 r64_frac = extract64(r64_val, 0, 52);
5880 /* result = sign : result_exp<10:0> : fraction<51:0> */
5881 return make_float64(f64_sbit |
5882 ((r64_exp & 0x7ff) << 52) |
5883 r64_frac);
5886 /* The algorithm that must be used to calculate the estimate
5887 * is specified by the ARM ARM.
5889 static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
5891 /* These calculations mustn't set any fp exception flags,
5892 * so we use a local copy of the fp_status.
5894 float_status dummy_status = *real_fp_status;
5895 float_status *s = &dummy_status;
5896 float64 q;
5897 int64_t q_int;
5899 if (float64_lt(a, float64_half, s)) {
5900 /* range 0.25 <= a < 0.5 */
5902 /* a in units of 1/512 rounded down */
5903 /* q0 = (int)(a * 512.0); */
5904 q = float64_mul(float64_512, a, s);
5905 q_int = float64_to_int64_round_to_zero(q, s);
5907 /* reciprocal root r */
5908 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
5909 q = int64_to_float64(q_int, s);
5910 q = float64_add(q, float64_half, s);
5911 q = float64_div(q, float64_512, s);
5912 q = float64_sqrt(q, s);
5913 q = float64_div(float64_one, q, s);
5914 } else {
5915 /* range 0.5 <= a < 1.0 */
5917 /* a in units of 1/256 rounded down */
5918 /* q1 = (int)(a * 256.0); */
5919 q = float64_mul(float64_256, a, s);
5920 int64_t q_int = float64_to_int64_round_to_zero(q, s);
5922 /* reciprocal root r */
5923 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
5924 q = int64_to_float64(q_int, s);
5925 q = float64_add(q, float64_half, s);
5926 q = float64_div(q, float64_256, s);
5927 q = float64_sqrt(q, s);
5928 q = float64_div(float64_one, q, s);
5930 /* r in units of 1/256 rounded to nearest */
5931 /* s = (int)(256.0 * r + 0.5); */
5933 q = float64_mul(q, float64_256,s );
5934 q = float64_add(q, float64_half, s);
5935 q_int = float64_to_int64_round_to_zero(q, s);
5937 /* return (double)s / 256.0;*/
5938 return float64_div(int64_to_float64(q_int, s), float64_256, s);
5941 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
5943 float_status *s = fpstp;
5944 float32 f32 = float32_squash_input_denormal(input, s);
5945 uint32_t val = float32_val(f32);
5946 uint32_t f32_sbit = 0x80000000 & val;
5947 int32_t f32_exp = extract32(val, 23, 8);
5948 uint32_t f32_frac = extract32(val, 0, 23);
5949 uint64_t f64_frac;
5950 uint64_t val64;
5951 int result_exp;
5952 float64 f64;
5954 if (float32_is_any_nan(f32)) {
5955 float32 nan = f32;
5956 if (float32_is_signaling_nan(f32)) {
5957 float_raise(float_flag_invalid, s);
5958 nan = float32_maybe_silence_nan(f32);
5960 if (s->default_nan_mode) {
5961 nan = float32_default_nan;
5963 return nan;
5964 } else if (float32_is_zero(f32)) {
5965 float_raise(float_flag_divbyzero, s);
5966 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5967 } else if (float32_is_neg(f32)) {
5968 float_raise(float_flag_invalid, s);
5969 return float32_default_nan;
5970 } else if (float32_is_infinity(f32)) {
5971 return float32_zero;
5974 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
5975 * preserving the parity of the exponent. */
5977 f64_frac = ((uint64_t) f32_frac) << 29;
5978 if (f32_exp == 0) {
5979 while (extract64(f64_frac, 51, 1) == 0) {
5980 f64_frac = f64_frac << 1;
5981 f32_exp = f32_exp-1;
5983 f64_frac = extract64(f64_frac, 0, 51) << 1;
5986 if (extract64(f32_exp, 0, 1) == 0) {
5987 f64 = make_float64(((uint64_t) f32_sbit) << 32
5988 | (0x3feULL << 52)
5989 | f64_frac);
5990 } else {
5991 f64 = make_float64(((uint64_t) f32_sbit) << 32
5992 | (0x3fdULL << 52)
5993 | f64_frac);
5996 result_exp = (380 - f32_exp) / 2;
5998 f64 = recip_sqrt_estimate(f64, s);
6000 val64 = float64_val(f64);
6002 val = ((result_exp & 0xff) << 23)
6003 | ((val64 >> 29) & 0x7fffff);
6004 return make_float32(val);
6007 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
6009 float_status *s = fpstp;
6010 float64 f64 = float64_squash_input_denormal(input, s);
6011 uint64_t val = float64_val(f64);
6012 uint64_t f64_sbit = 0x8000000000000000ULL & val;
6013 int64_t f64_exp = extract64(val, 52, 11);
6014 uint64_t f64_frac = extract64(val, 0, 52);
6015 int64_t result_exp;
6016 uint64_t result_frac;
6018 if (float64_is_any_nan(f64)) {
6019 float64 nan = f64;
6020 if (float64_is_signaling_nan(f64)) {
6021 float_raise(float_flag_invalid, s);
6022 nan = float64_maybe_silence_nan(f64);
6024 if (s->default_nan_mode) {
6025 nan = float64_default_nan;
6027 return nan;
6028 } else if (float64_is_zero(f64)) {
6029 float_raise(float_flag_divbyzero, s);
6030 return float64_set_sign(float64_infinity, float64_is_neg(f64));
6031 } else if (float64_is_neg(f64)) {
6032 float_raise(float_flag_invalid, s);
6033 return float64_default_nan;
6034 } else if (float64_is_infinity(f64)) {
6035 return float64_zero;
6038 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
6039 * preserving the parity of the exponent. */
6041 if (f64_exp == 0) {
6042 while (extract64(f64_frac, 51, 1) == 0) {
6043 f64_frac = f64_frac << 1;
6044 f64_exp = f64_exp - 1;
6046 f64_frac = extract64(f64_frac, 0, 51) << 1;
6049 if (extract64(f64_exp, 0, 1) == 0) {
6050 f64 = make_float64(f64_sbit
6051 | (0x3feULL << 52)
6052 | f64_frac);
6053 } else {
6054 f64 = make_float64(f64_sbit
6055 | (0x3fdULL << 52)
6056 | f64_frac);
6059 result_exp = (3068 - f64_exp) / 2;
6061 f64 = recip_sqrt_estimate(f64, s);
6063 result_frac = extract64(float64_val(f64), 0, 52);
6065 return make_float64(f64_sbit |
6066 ((result_exp & 0x7ff) << 52) |
6067 result_frac);
6070 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
6072 float_status *s = fpstp;
6073 float64 f64;
6075 if ((a & 0x80000000) == 0) {
6076 return 0xffffffff;
6079 f64 = make_float64((0x3feULL << 52)
6080 | ((int64_t)(a & 0x7fffffff) << 21));
6082 f64 = recip_estimate(f64, s);
6084 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
6087 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
6089 float_status *fpst = fpstp;
6090 float64 f64;
6092 if ((a & 0xc0000000) == 0) {
6093 return 0xffffffff;
6096 if (a & 0x80000000) {
6097 f64 = make_float64((0x3feULL << 52)
6098 | ((uint64_t)(a & 0x7fffffff) << 21));
6099 } else { /* bits 31-30 == '01' */
6100 f64 = make_float64((0x3fdULL << 52)
6101 | ((uint64_t)(a & 0x3fffffff) << 22));
6104 f64 = recip_sqrt_estimate(f64, fpst);
6106 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
6109 /* VFPv4 fused multiply-accumulate */
6110 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
6112 float_status *fpst = fpstp;
6113 return float32_muladd(a, b, c, 0, fpst);
6116 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
6118 float_status *fpst = fpstp;
6119 return float64_muladd(a, b, c, 0, fpst);
6122 /* ARMv8 round to integral */
6123 float32 HELPER(rints_exact)(float32 x, void *fp_status)
6125 return float32_round_to_int(x, fp_status);
6128 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
6130 return float64_round_to_int(x, fp_status);
6133 float32 HELPER(rints)(float32 x, void *fp_status)
6135 int old_flags = get_float_exception_flags(fp_status), new_flags;
6136 float32 ret;
6138 ret = float32_round_to_int(x, fp_status);
6140 /* Suppress any inexact exceptions the conversion produced */
6141 if (!(old_flags & float_flag_inexact)) {
6142 new_flags = get_float_exception_flags(fp_status);
6143 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
6146 return ret;
6149 float64 HELPER(rintd)(float64 x, void *fp_status)
6151 int old_flags = get_float_exception_flags(fp_status), new_flags;
6152 float64 ret;
6154 ret = float64_round_to_int(x, fp_status);
6156 new_flags = get_float_exception_flags(fp_status);
6158 /* Suppress any inexact exceptions the conversion produced */
6159 if (!(old_flags & float_flag_inexact)) {
6160 new_flags = get_float_exception_flags(fp_status);
6161 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
6164 return ret;
6167 /* Convert ARM rounding mode to softfloat */
6168 int arm_rmode_to_sf(int rmode)
6170 switch (rmode) {
6171 case FPROUNDING_TIEAWAY:
6172 rmode = float_round_ties_away;
6173 break;
6174 case FPROUNDING_ODD:
6175 /* FIXME: add support for TIEAWAY and ODD */
6176 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
6177 rmode);
6178 case FPROUNDING_TIEEVEN:
6179 default:
6180 rmode = float_round_nearest_even;
6181 break;
6182 case FPROUNDING_POSINF:
6183 rmode = float_round_up;
6184 break;
6185 case FPROUNDING_NEGINF:
6186 rmode = float_round_down;
6187 break;
6188 case FPROUNDING_ZERO:
6189 rmode = float_round_to_zero;
6190 break;
6192 return rmode;
6195 /* CRC helpers.
6196 * The upper bytes of val (above the number specified by 'bytes') must have
6197 * been zeroed out by the caller.
6199 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
6201 uint8_t buf[4];
6203 stl_le_p(buf, val);
6205 /* zlib crc32 converts the accumulator and output to one's complement. */
6206 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
6209 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
6211 uint8_t buf[4];
6213 stl_le_p(buf, val);
6215 /* Linux crc32c converts the output to one's complement. */
6216 return crc32c(acc, buf, bytes) ^ 0xffffffff;