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"
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 */
26 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
30 /* VFP data registers are always little-endian. */
31 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
33 stfq_le_p(buf, env->vfp.regs[reg]);
36 if (arm_feature(env, ARM_FEATURE_NEON)) {
37 /* Aliases for Q regs. */
40 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
41 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
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;
53 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
57 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
59 env->vfp.regs[reg] = ldfq_le_p(buf);
62 if (arm_feature(env, ARM_FEATURE_NEON)) {
65 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
66 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
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;
78 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
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]);
88 stl_p(buf, vfp_get_fpsr(env));
92 stl_p(buf, vfp_get_fpcr(env));
99 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
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);
109 vfp_set_fpsr(env, ldl_p(buf));
113 vfp_set_fpcr(env, ldl_p(buf));
120 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
122 if (cpreg_field_is_64bit(ri)) {
123 return CPREG_FIELD64(env, ri);
125 return CPREG_FIELD32(env, ri);
129 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
132 if (cpreg_field_is_64bit(ri)) {
133 CPREG_FIELD64(env, ri) = value;
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);
149 return raw_read(env, ri);
153 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
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
161 if (ri->type & ARM_CP_CONST) {
163 } else if (ri->raw_writefn) {
164 ri->raw_writefn(env, ri, v);
165 } else if (ri->writefn) {
166 ri->writefn(env, ri, v);
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. */
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);
187 if (ri->type & ARM_CP_NO_MIGRATE) {
190 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
195 bool write_list_to_cpustate(ARMCPU *cpu)
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);
210 if (ri->type & ARM_CP_NO_MIGRATE) {
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) {
225 static void add_cpreg_to_list(gpointer key, gpointer opaque)
227 ARMCPU *cpu = opaque;
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;
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);
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.
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);
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,
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,
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,
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,
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,
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 static const ARMCPRegInfo cp_reginfo[] = {
381 { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0,
382 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
383 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
384 { .name = "CONTEXTIDR", .state = ARM_CP_STATE_BOTH,
385 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
387 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el1),
388 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
392 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
393 /* NB: Some of these registers exist in v8 but with more precise
394 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
396 /* MMU Domain access control / MPU write buffer control */
397 { .name = "DACR", .cp = 15,
398 .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
399 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
400 .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
401 /* ??? This covers not just the impdef TLB lockdown registers but also
402 * some v7VMSA registers relating to TEX remap, so it is overly broad.
404 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
405 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
406 /* MMU TLB control. Note that the wildcarding means we cover not just
407 * the unified TLB ops but also the dside/iside/inner-shareable variants.
409 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
410 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
411 .type = ARM_CP_NO_MIGRATE },
412 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
413 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
414 .type = ARM_CP_NO_MIGRATE },
415 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
416 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
417 .type = ARM_CP_NO_MIGRATE },
418 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
419 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
420 .type = ARM_CP_NO_MIGRATE },
421 /* Cache maintenance ops; some of this space may be overridden later. */
422 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
423 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
424 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
428 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
429 /* Not all pre-v6 cores implemented this WFI, so this is slightly
432 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
433 .access = PL1_W, .type = ARM_CP_WFI },
437 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
438 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
439 * is UNPREDICTABLE; we choose to NOP as most implementations do).
441 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
442 .access = PL1_W, .type = ARM_CP_WFI },
443 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
444 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
445 * OMAPCP will override this space.
447 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
448 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
450 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
451 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
453 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
454 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
455 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
457 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
458 * implementing it as RAZ means the "debug architecture version" bits
459 * will read as a reserved value, which should cause Linux to not try
460 * to use the debug hardware.
462 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
463 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
467 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
472 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
473 if (!arm_feature(env, ARM_FEATURE_V8)) {
474 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
475 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
476 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
478 if (arm_feature(env, ARM_FEATURE_VFP)) {
479 /* VFP coprocessor: cp10 & cp11 [23:20] */
480 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
482 if (!arm_feature(env, ARM_FEATURE_NEON)) {
483 /* ASEDIS [31] bit is RAO/WI */
487 /* VFPv3 and upwards with NEON implement 32 double precision
488 * registers (D0-D31).
490 if (!arm_feature(env, ARM_FEATURE_NEON) ||
491 !arm_feature(env, ARM_FEATURE_VFP3)) {
492 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
498 env->cp15.c1_coproc = value;
501 static const ARMCPRegInfo v6_cp_reginfo[] = {
502 /* prefetch by MVA in v6, NOP in v7 */
503 { .name = "MVA_prefetch",
504 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
505 .access = PL1_W, .type = ARM_CP_NOP },
506 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
507 .access = PL0_W, .type = ARM_CP_NOP },
508 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
509 .access = PL0_W, .type = ARM_CP_NOP },
510 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
511 .access = PL0_W, .type = ARM_CP_NOP },
512 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
514 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[1]),
516 /* Watchpoint Fault Address Register : should actually only be present
517 * for 1136, 1176, 11MPCore.
519 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
520 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
521 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
522 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
523 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
524 .resetvalue = 0, .writefn = cpacr_write },
528 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri)
530 /* Performance monitor registers user accessibility is controlled
533 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
534 return CP_ACCESS_TRAP;
539 #ifndef CONFIG_USER_ONLY
541 static inline bool arm_ccnt_enabled(CPUARMState *env)
543 /* This does not support checking PMCCFILTR_EL0 register */
545 if (!(env->cp15.c9_pmcr & PMCRE)) {
552 void pmccntr_sync(CPUARMState *env)
556 temp_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
557 get_ticks_per_sec(), 1000000);
559 if (env->cp15.c9_pmcr & PMCRD) {
560 /* Increment once every 64 processor clock cycles */
564 if (arm_ccnt_enabled(env)) {
565 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
569 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
575 /* The counter has been reset */
576 env->cp15.c15_ccnt = 0;
579 /* only the DP, X, D and E bits are writable */
580 env->cp15.c9_pmcr &= ~0x39;
581 env->cp15.c9_pmcr |= (value & 0x39);
586 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
588 uint64_t total_ticks;
590 if (!arm_ccnt_enabled(env)) {
591 /* Counter is disabled, do not change value */
592 return env->cp15.c15_ccnt;
595 total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
596 get_ticks_per_sec(), 1000000);
598 if (env->cp15.c9_pmcr & PMCRD) {
599 /* Increment once every 64 processor clock cycles */
602 return total_ticks - env->cp15.c15_ccnt;
605 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
608 uint64_t total_ticks;
610 if (!arm_ccnt_enabled(env)) {
611 /* Counter is disabled, set the absolute value */
612 env->cp15.c15_ccnt = value;
616 total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL),
617 get_ticks_per_sec(), 1000000);
619 if (env->cp15.c9_pmcr & PMCRD) {
620 /* Increment once every 64 processor clock cycles */
623 env->cp15.c15_ccnt = total_ticks - value;
626 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
629 uint64_t cur_val = pmccntr_read(env, NULL);
631 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
634 #else /* CONFIG_USER_ONLY */
636 void pmccntr_sync(CPUARMState *env)
642 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
646 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
650 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
654 env->cp15.c9_pmcnten |= value;
657 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
661 env->cp15.c9_pmcnten &= ~value;
664 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
667 env->cp15.c9_pmovsr &= ~value;
670 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
673 env->cp15.c9_pmxevtyper = value & 0xff;
676 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
679 env->cp15.c9_pmuserenr = value & 1;
682 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
685 /* We have no event counters so only the C bit can be changed */
687 env->cp15.c9_pminten |= value;
690 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
694 env->cp15.c9_pminten &= ~value;
697 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
700 /* Note that even though the AArch64 view of this register has bits
701 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
702 * architectural requirements for bits which are RES0 only in some
703 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
704 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
706 raw_write(env, ri, value & ~0x1FULL);
709 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
711 ARMCPU *cpu = arm_env_get_cpu(env);
712 return cpu->ccsidr[env->cp15.c0_cssel];
715 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
718 raw_write(env, ri, value & 0xf);
721 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
723 CPUState *cs = ENV_GET_CPU(env);
726 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
729 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
732 /* External aborts are not possible in QEMU so A bit is always clear */
736 static const ARMCPRegInfo v7_cp_reginfo[] = {
737 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
738 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
739 .access = PL1_W, .type = ARM_CP_NOP },
740 /* Performance monitors are implementation defined in v7,
741 * but with an ARM recommended set of registers, which we
742 * follow (although we don't actually implement any counters)
744 * Performance registers fall into three categories:
745 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
746 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
747 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
748 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
749 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
751 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
752 .access = PL0_RW, .type = ARM_CP_NO_MIGRATE,
753 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
754 .writefn = pmcntenset_write,
755 .accessfn = pmreg_access,
756 .raw_writefn = raw_write },
757 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
758 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
759 .access = PL0_RW, .accessfn = pmreg_access,
760 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
761 .writefn = pmcntenset_write, .raw_writefn = raw_write },
762 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
764 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
765 .accessfn = pmreg_access,
766 .writefn = pmcntenclr_write,
767 .type = ARM_CP_NO_MIGRATE },
768 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
769 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
770 .access = PL0_RW, .accessfn = pmreg_access,
771 .type = ARM_CP_NO_MIGRATE,
772 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
773 .writefn = pmcntenclr_write },
774 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
775 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
776 .accessfn = pmreg_access,
777 .writefn = pmovsr_write,
778 .raw_writefn = raw_write },
779 /* Unimplemented so WI. */
780 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
781 .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
782 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
783 * We choose to RAZ/WI.
785 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
786 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
787 .accessfn = pmreg_access },
788 #ifndef CONFIG_USER_ONLY
789 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
790 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
791 .readfn = pmccntr_read, .writefn = pmccntr_write32,
792 .accessfn = pmreg_access },
793 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
794 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
795 .access = PL0_RW, .accessfn = pmreg_access,
797 .readfn = pmccntr_read, .writefn = pmccntr_write, },
799 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
800 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
801 .writefn = pmccfiltr_write,
802 .access = PL0_RW, .accessfn = pmreg_access,
804 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
806 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
808 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
809 .accessfn = pmreg_access, .writefn = pmxevtyper_write,
810 .raw_writefn = raw_write },
811 /* Unimplemented, RAZ/WI. */
812 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
813 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
814 .accessfn = pmreg_access },
815 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
816 .access = PL0_R | PL1_RW,
817 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
819 .writefn = pmuserenr_write, .raw_writefn = raw_write },
820 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
822 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
824 .writefn = pmintenset_write, .raw_writefn = raw_write },
825 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
826 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
827 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
828 .resetvalue = 0, .writefn = pmintenclr_write, },
829 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
830 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
831 .access = PL1_RW, .writefn = vbar_write,
832 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[1]),
834 { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
835 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
837 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
838 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
839 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE },
840 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
841 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
842 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c0_cssel),
843 .writefn = csselr_write, .resetvalue = 0 },
844 /* Auxiliary ID register: this actually has an IMPDEF value but for now
845 * just RAZ for all cores:
847 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
848 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
849 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
850 /* Auxiliary fault status registers: these also are IMPDEF, and we
851 * choose to RAZ/WI for all cores.
853 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
854 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
855 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
856 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
857 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
858 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
859 /* MAIR can just read-as-written because we don't implement caches
860 * and so don't need to care about memory attributes.
862 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
863 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
864 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el1),
866 /* For non-long-descriptor page tables these are PRRR and NMRR;
867 * regardless they still act as reads-as-written for QEMU.
868 * The override is necessary because of the overly-broad TLB_LOCKDOWN
871 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
872 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
873 .fieldoffset = offsetoflow32(CPUARMState, cp15.mair_el1),
874 .resetfn = arm_cp_reset_ignore },
875 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE,
876 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
877 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el1),
878 .resetfn = arm_cp_reset_ignore },
879 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
880 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
881 .type = ARM_CP_NO_MIGRATE, .access = PL1_R, .readfn = isr_read },
885 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
892 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri)
894 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
895 return CP_ACCESS_TRAP;
900 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
901 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
902 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
904 .writefn = teecr_write },
905 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
906 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
907 .accessfn = teehbr_access, .resetvalue = 0 },
911 static const ARMCPRegInfo v6k_cp_reginfo[] = {
912 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
913 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
915 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el0), .resetvalue = 0 },
916 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
918 .fieldoffset = offsetoflow32(CPUARMState, cp15.tpidr_el0),
919 .resetfn = arm_cp_reset_ignore },
920 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
921 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
922 .access = PL0_R|PL1_W,
923 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el0), .resetvalue = 0 },
924 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
925 .access = PL0_R|PL1_W,
926 .fieldoffset = offsetoflow32(CPUARMState, cp15.tpidrro_el0),
927 .resetfn = arm_cp_reset_ignore },
928 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_BOTH,
929 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
931 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el1), .resetvalue = 0 },
935 #ifndef CONFIG_USER_ONLY
937 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri)
939 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */
940 if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) {
941 return CP_ACCESS_TRAP;
946 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx)
948 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
949 if (arm_current_pl(env) == 0 &&
950 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
951 return CP_ACCESS_TRAP;
956 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx)
958 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
959 * EL0[PV]TEN is zero.
961 if (arm_current_pl(env) == 0 &&
962 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
963 return CP_ACCESS_TRAP;
968 static CPAccessResult gt_pct_access(CPUARMState *env,
969 const ARMCPRegInfo *ri)
971 return gt_counter_access(env, GTIMER_PHYS);
974 static CPAccessResult gt_vct_access(CPUARMState *env,
975 const ARMCPRegInfo *ri)
977 return gt_counter_access(env, GTIMER_VIRT);
980 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
982 return gt_timer_access(env, GTIMER_PHYS);
985 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri)
987 return gt_timer_access(env, GTIMER_VIRT);
990 static uint64_t gt_get_countervalue(CPUARMState *env)
992 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
995 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
997 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1000 /* Timer enabled: calculate and set current ISTATUS, irq, and
1001 * reset timer to when ISTATUS next has to change
1003 uint64_t count = gt_get_countervalue(&cpu->env);
1004 /* Note that this must be unsigned 64 bit arithmetic: */
1005 int istatus = count >= gt->cval;
1008 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1009 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1010 (istatus && !(gt->ctl & 2)));
1012 /* Next transition is when count rolls back over to zero */
1013 nexttick = UINT64_MAX;
1015 /* Next transition is when we hit cval */
1016 nexttick = gt->cval;
1018 /* Note that the desired next expiry time might be beyond the
1019 * signed-64-bit range of a QEMUTimer -- in this case we just
1020 * set the timer for as far in the future as possible. When the
1021 * timer expires we will reset the timer for any remaining period.
1023 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1024 nexttick = INT64_MAX / GTIMER_SCALE;
1026 timer_mod(cpu->gt_timer[timeridx], nexttick);
1028 /* Timer disabled: ISTATUS and timer output always clear */
1030 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
1031 timer_del(cpu->gt_timer[timeridx]);
1035 static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1037 ARMCPU *cpu = arm_env_get_cpu(env);
1038 int timeridx = ri->opc1 & 1;
1040 timer_del(cpu->gt_timer[timeridx]);
1043 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1045 return gt_get_countervalue(env);
1048 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1051 int timeridx = ri->opc1 & 1;
1053 env->cp15.c14_timer[timeridx].cval = value;
1054 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1057 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1059 int timeridx = ri->crm & 1;
1061 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
1062 gt_get_countervalue(env));
1065 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1068 int timeridx = ri->crm & 1;
1070 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) +
1071 + sextract64(value, 0, 32);
1072 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
1075 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1078 ARMCPU *cpu = arm_env_get_cpu(env);
1079 int timeridx = ri->crm & 1;
1080 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1082 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
1083 if ((oldval ^ value) & 1) {
1084 /* Enable toggled */
1085 gt_recalc_timer(cpu, timeridx);
1086 } else if ((oldval ^ value) & 2) {
1087 /* IMASK toggled: don't need to recalculate,
1088 * just set the interrupt line based on ISTATUS
1090 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1091 (oldval & 4) && !(value & 2));
1095 void arm_gt_ptimer_cb(void *opaque)
1097 ARMCPU *cpu = opaque;
1099 gt_recalc_timer(cpu, GTIMER_PHYS);
1102 void arm_gt_vtimer_cb(void *opaque)
1104 ARMCPU *cpu = opaque;
1106 gt_recalc_timer(cpu, GTIMER_VIRT);
1109 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1110 /* Note that CNTFRQ is purely reads-as-written for the benefit
1111 * of software; writing it doesn't actually change the timer frequency.
1112 * Our reset value matches the fixed frequency we implement the timer at.
1114 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
1115 .type = ARM_CP_NO_MIGRATE,
1116 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1117 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
1118 .resetfn = arm_cp_reset_ignore,
1120 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1121 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1122 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1123 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1124 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
1126 /* overall control: mostly access permissions */
1127 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1128 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
1130 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1133 /* per-timer control */
1134 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1135 .type = ARM_CP_IO | ARM_CP_NO_MIGRATE, .access = PL1_RW | PL0_R,
1136 .accessfn = gt_ptimer_access,
1137 .fieldoffset = offsetoflow32(CPUARMState,
1138 cp15.c14_timer[GTIMER_PHYS].ctl),
1139 .resetfn = arm_cp_reset_ignore,
1140 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1142 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1143 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
1144 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1145 .accessfn = gt_ptimer_access,
1146 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1148 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1150 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
1151 .type = ARM_CP_IO | ARM_CP_NO_MIGRATE, .access = PL1_RW | PL0_R,
1152 .accessfn = gt_vtimer_access,
1153 .fieldoffset = offsetoflow32(CPUARMState,
1154 cp15.c14_timer[GTIMER_VIRT].ctl),
1155 .resetfn = arm_cp_reset_ignore,
1156 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1158 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1159 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
1160 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
1161 .accessfn = gt_vtimer_access,
1162 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1164 .writefn = gt_ctl_write, .raw_writefn = raw_write,
1166 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1167 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1168 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
1169 .accessfn = gt_ptimer_access,
1170 .readfn = gt_tval_read, .writefn = gt_tval_write,
1172 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1173 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
1174 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
1175 .readfn = gt_tval_read, .writefn = gt_tval_write,
1177 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
1178 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
1179 .accessfn = gt_vtimer_access,
1180 .readfn = gt_tval_read, .writefn = gt_tval_write,
1182 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1183 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
1184 .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R,
1185 .readfn = gt_tval_read, .writefn = gt_tval_write,
1187 /* The counter itself */
1188 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
1189 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
1190 .accessfn = gt_pct_access,
1191 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1193 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1194 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
1195 .access = PL0_R, .type = ARM_CP_NO_MIGRATE | ARM_CP_IO,
1196 .accessfn = gt_pct_access,
1197 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
1199 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
1200 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO,
1201 .accessfn = gt_vct_access,
1202 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1204 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1205 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
1206 .access = PL0_R, .type = ARM_CP_NO_MIGRATE | ARM_CP_IO,
1207 .accessfn = gt_vct_access,
1208 .readfn = gt_cnt_read, .resetfn = gt_cnt_reset,
1210 /* Comparison value, indicating when the timer goes off */
1211 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
1212 .access = PL1_RW | PL0_R,
1213 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_MIGRATE,
1214 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1215 .accessfn = gt_ptimer_access, .resetfn = arm_cp_reset_ignore,
1216 .writefn = gt_cval_write, .raw_writefn = raw_write,
1218 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1219 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1220 .access = PL1_RW | PL0_R,
1222 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
1223 .resetvalue = 0, .accessfn = gt_vtimer_access,
1224 .writefn = gt_cval_write, .raw_writefn = raw_write,
1226 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1227 .access = PL1_RW | PL0_R,
1228 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_MIGRATE,
1229 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1230 .accessfn = gt_vtimer_access, .resetfn = arm_cp_reset_ignore,
1231 .writefn = gt_cval_write, .raw_writefn = raw_write,
1233 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1234 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1235 .access = PL1_RW | PL0_R,
1237 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1238 .resetvalue = 0, .accessfn = gt_vtimer_access,
1239 .writefn = gt_cval_write, .raw_writefn = raw_write,
1245 /* In user-mode none of the generic timer registers are accessible,
1246 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
1247 * so instead just don't register any of them.
1249 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1255 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1257 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1258 raw_write(env, ri, value);
1259 } else if (arm_feature(env, ARM_FEATURE_V7)) {
1260 raw_write(env, ri, value & 0xfffff6ff);
1262 raw_write(env, ri, value & 0xfffff1ff);
1266 #ifndef CONFIG_USER_ONLY
1267 /* get_phys_addr() isn't present for user-mode-only targets */
1269 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri)
1272 /* Other states are only available with TrustZone; in
1273 * a non-TZ implementation these registers don't exist
1274 * at all, which is an Uncategorized trap. This underdecoding
1275 * is safe because the reginfo is NO_MIGRATE.
1277 return CP_ACCESS_TRAP_UNCATEGORIZED;
1279 return CP_ACCESS_OK;
1282 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1285 target_ulong page_size;
1287 int ret, is_user = ri->opc2 & 2;
1288 int access_type = ri->opc2 & 1;
1290 ret = get_phys_addr(env, value, access_type, is_user,
1291 &phys_addr, &prot, &page_size);
1292 if (extended_addresses_enabled(env)) {
1293 /* ret is a DFSR/IFSR value for the long descriptor
1294 * translation table format, but with WnR always clear.
1295 * Convert it to a 64-bit PAR.
1297 uint64_t par64 = (1 << 11); /* LPAE bit always set */
1299 par64 |= phys_addr & ~0xfffULL;
1300 /* We don't set the ATTR or SH fields in the PAR. */
1303 par64 |= (ret & 0x3f) << 1; /* FS */
1304 /* Note that S2WLK and FSTAGE are always zero, because we don't
1305 * implement virtualization and therefore there can't be a stage 2
1309 env->cp15.par_el1 = par64;
1311 /* ret is a DFSR/IFSR value for the short descriptor
1312 * translation table format (with WnR always clear).
1313 * Convert it to a 32-bit PAR.
1316 /* We do not set any attribute bits in the PAR */
1317 if (page_size == (1 << 24)
1318 && arm_feature(env, ARM_FEATURE_V7)) {
1319 env->cp15.par_el1 = (phys_addr & 0xff000000) | 1 << 1;
1321 env->cp15.par_el1 = phys_addr & 0xfffff000;
1324 env->cp15.par_el1 = ((ret & (1 << 10)) >> 5) |
1325 ((ret & (1 << 12)) >> 6) |
1326 ((ret & 0xf) << 1) | 1;
1332 static const ARMCPRegInfo vapa_cp_reginfo[] = {
1333 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
1334 .access = PL1_RW, .resetvalue = 0,
1335 .fieldoffset = offsetoflow32(CPUARMState, cp15.par_el1),
1336 .writefn = par_write },
1337 #ifndef CONFIG_USER_ONLY
1338 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
1339 .access = PL1_W, .accessfn = ats_access,
1340 .writefn = ats_write, .type = ARM_CP_NO_MIGRATE },
1345 /* Return basic MPU access permission bits. */
1346 static uint32_t simple_mpu_ap_bits(uint32_t val)
1353 for (i = 0; i < 16; i += 2) {
1354 ret |= (val >> i) & mask;
1360 /* Pad basic MPU access permission bits to extended format. */
1361 static uint32_t extended_mpu_ap_bits(uint32_t val)
1368 for (i = 0; i < 16; i += 2) {
1369 ret |= (val & mask) << i;
1375 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1378 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
1381 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1383 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
1386 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1389 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
1392 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1394 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
1397 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
1398 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1399 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1400 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
1402 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
1403 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1404 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1405 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
1407 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
1408 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
1410 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
1412 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
1414 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
1416 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1418 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
1419 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1421 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
1422 /* Protection region base and size registers */
1423 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
1424 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1425 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
1426 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
1427 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1428 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
1429 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
1430 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1431 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
1432 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
1433 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1434 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
1435 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
1436 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1437 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
1438 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
1439 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1440 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
1441 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
1442 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1443 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
1444 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
1445 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
1446 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
1450 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
1453 int maskshift = extract32(value, 0, 3);
1455 if (!arm_feature(env, ARM_FEATURE_V8)) {
1456 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
1457 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
1458 * using Long-desciptor translation table format */
1459 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
1460 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
1461 /* In an implementation that includes the Security Extensions
1462 * TTBCR has additional fields PD0 [4] and PD1 [5] for
1463 * Short-descriptor translation table format.
1465 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
1471 /* Note that we always calculate c2_mask and c2_base_mask, but
1472 * they are only used for short-descriptor tables (ie if EAE is 0);
1473 * for long-descriptor tables the TTBCR fields are used differently
1474 * and the c2_mask and c2_base_mask values are meaningless.
1476 raw_write(env, ri, value);
1477 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift);
1478 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift);
1481 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1484 ARMCPU *cpu = arm_env_get_cpu(env);
1486 if (arm_feature(env, ARM_FEATURE_LPAE)) {
1487 /* With LPAE the TTBCR could result in a change of ASID
1488 * via the TTBCR.A1 bit, so do a TLB flush.
1490 tlb_flush(CPU(cpu), 1);
1492 vmsa_ttbcr_raw_write(env, ri, value);
1495 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1497 env->cp15.c2_base_mask = 0xffffc000u;
1498 raw_write(env, ri, 0);
1499 env->cp15.c2_mask = 0;
1502 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
1505 ARMCPU *cpu = arm_env_get_cpu(env);
1507 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
1508 tlb_flush(CPU(cpu), 1);
1509 raw_write(env, ri, value);
1512 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1515 /* 64 bit accesses to the TTBRs can change the ASID and so we
1516 * must flush the TLB.
1518 if (cpreg_field_is_64bit(ri)) {
1519 ARMCPU *cpu = arm_env_get_cpu(env);
1521 tlb_flush(CPU(cpu), 1);
1523 raw_write(env, ri, value);
1526 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
1527 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
1528 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE,
1529 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
1530 .resetfn = arm_cp_reset_ignore, },
1531 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
1533 .fieldoffset = offsetof(CPUARMState, cp15.ifsr_el2), .resetvalue = 0, },
1534 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
1535 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
1537 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
1538 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
1539 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
1540 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el1),
1541 .writefn = vmsa_ttbr_write, .resetvalue = 0 },
1542 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
1543 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
1544 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el1),
1545 .writefn = vmsa_ttbr_write, .resetvalue = 0 },
1546 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
1547 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1548 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
1549 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
1550 .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
1551 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
1552 .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, .writefn = vmsa_ttbcr_write,
1553 .resetfn = arm_cp_reset_ignore, .raw_writefn = vmsa_ttbcr_raw_write,
1554 .fieldoffset = offsetoflow32(CPUARMState, cp15.c2_control) },
1555 /* 64-bit FAR; this entry also gives us the AArch32 DFAR */
1556 { .name = "FAR_EL1", .state = ARM_CP_STATE_BOTH,
1557 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
1558 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
1563 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
1566 env->cp15.c15_ticonfig = value & 0xe7;
1567 /* The OS_TYPE bit in this register changes the reported CPUID! */
1568 env->cp15.c0_cpuid = (value & (1 << 5)) ?
1569 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1572 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1575 env->cp15.c15_threadid = value & 0xffff;
1578 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
1581 /* Wait-for-interrupt (deprecated) */
1582 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
1585 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
1588 /* On OMAP there are registers indicating the max/min index of dcache lines
1589 * containing a dirty line; cache flush operations have to reset these.
1591 env->cp15.c15_i_max = 0x000;
1592 env->cp15.c15_i_min = 0xff0;
1595 static const ARMCPRegInfo omap_cp_reginfo[] = {
1596 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
1597 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
1598 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
1600 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
1601 .access = PL1_RW, .type = ARM_CP_NOP },
1602 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
1604 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
1605 .writefn = omap_ticonfig_write },
1606 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
1608 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
1609 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
1610 .access = PL1_RW, .resetvalue = 0xff0,
1611 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
1612 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
1614 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
1615 .writefn = omap_threadid_write },
1616 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
1617 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1618 .type = ARM_CP_NO_MIGRATE,
1619 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
1620 /* TODO: Peripheral port remap register:
1621 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
1622 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
1625 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
1626 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
1627 .type = ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE,
1628 .writefn = omap_cachemaint_write },
1629 { .name = "C9", .cp = 15, .crn = 9,
1630 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
1631 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1635 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1639 if (env->cp15.c15_cpar != value) {
1640 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1642 env->cp15.c15_cpar = value;
1646 static const ARMCPRegInfo xscale_cp_reginfo[] = {
1647 { .name = "XSCALE_CPAR",
1648 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
1649 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
1650 .writefn = xscale_cpar_write, },
1651 { .name = "XSCALE_AUXCR",
1652 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
1653 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
1655 /* XScale specific cache-lockdown: since we have no cache we NOP these
1656 * and hope the guest does not really rely on cache behaviour.
1658 { .name = "XSCALE_LOCK_ICACHE_LINE",
1659 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
1660 .access = PL1_W, .type = ARM_CP_NOP },
1661 { .name = "XSCALE_UNLOCK_ICACHE",
1662 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
1663 .access = PL1_W, .type = ARM_CP_NOP },
1664 { .name = "XSCALE_DCACHE_LOCK",
1665 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
1666 .access = PL1_RW, .type = ARM_CP_NOP },
1667 { .name = "XSCALE_UNLOCK_DCACHE",
1668 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
1669 .access = PL1_W, .type = ARM_CP_NOP },
1673 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
1674 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
1675 * implementation of this implementation-defined space.
1676 * Ideally this should eventually disappear in favour of actually
1677 * implementing the correct behaviour for all cores.
1679 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
1680 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1682 .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE | ARM_CP_OVERRIDE,
1687 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
1688 /* Cache status: RAZ because we have no cache so it's always clean */
1689 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
1690 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1695 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
1696 /* We never have a a block transfer operation in progress */
1697 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
1698 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1700 /* The cache ops themselves: these all NOP for QEMU */
1701 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
1702 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1703 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
1704 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1705 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
1706 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1707 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
1708 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1709 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
1710 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1711 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
1712 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
1716 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
1717 /* The cache test-and-clean instructions always return (1 << 30)
1718 * to indicate that there are no dirty cache lines.
1720 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
1721 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1722 .resetvalue = (1 << 30) },
1723 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
1724 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE,
1725 .resetvalue = (1 << 30) },
1729 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
1730 /* Ignore ReadBuffer accesses */
1731 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
1732 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
1733 .access = PL1_RW, .resetvalue = 0,
1734 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE },
1738 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1740 CPUState *cs = CPU(arm_env_get_cpu(env));
1741 uint32_t mpidr = cs->cpu_index;
1742 /* We don't support setting cluster ID ([8..11]) (known as Aff1
1743 * in later ARM ARM versions), or any of the higher affinity level fields,
1744 * so these bits always RAZ.
1746 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1747 mpidr |= (1U << 31);
1748 /* Cores which are uniprocessor (non-coherent)
1749 * but still implement the MP extensions set
1750 * bit 30. (For instance, A9UP.) However we do
1751 * not currently model any of those cores.
1757 static const ARMCPRegInfo mpidr_cp_reginfo[] = {
1758 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
1759 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
1760 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE },
1764 static const ARMCPRegInfo lpae_cp_reginfo[] = {
1765 /* NOP AMAIR0/1: the override is because these clash with the rather
1766 * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo.
1768 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
1769 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
1770 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1772 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
1773 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
1774 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
1776 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
1777 .access = PL1_RW, .type = ARM_CP_64BIT,
1778 .fieldoffset = offsetof(CPUARMState, cp15.par_el1), .resetvalue = 0 },
1779 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
1780 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE,
1781 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el1),
1782 .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
1783 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
1784 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE,
1785 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el1),
1786 .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore },
1790 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1792 return vfp_get_fpcr(env);
1795 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1798 vfp_set_fpcr(env, value);
1801 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1803 return vfp_get_fpsr(env);
1806 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1809 vfp_set_fpsr(env, value);
1812 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri)
1814 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UMA)) {
1815 return CP_ACCESS_TRAP;
1817 return CP_ACCESS_OK;
1820 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
1823 env->daif = value & PSTATE_DAIF;
1826 static CPAccessResult aa64_cacheop_access(CPUARMState *env,
1827 const ARMCPRegInfo *ri)
1829 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
1830 * SCTLR_EL1.UCI is set.
1832 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UCI)) {
1833 return CP_ACCESS_TRAP;
1835 return CP_ACCESS_OK;
1838 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
1839 * Page D4-1736 (DDI0487A.b)
1842 static void tlbi_aa64_va_write(CPUARMState *env, const ARMCPRegInfo *ri,
1845 /* Invalidate by VA (AArch64 version) */
1846 ARMCPU *cpu = arm_env_get_cpu(env);
1847 uint64_t pageaddr = sextract64(value << 12, 0, 56);
1849 tlb_flush_page(CPU(cpu), pageaddr);
1852 static void tlbi_aa64_vaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
1855 /* Invalidate by VA, all ASIDs (AArch64 version) */
1856 ARMCPU *cpu = arm_env_get_cpu(env);
1857 uint64_t pageaddr = sextract64(value << 12, 0, 56);
1859 tlb_flush_page(CPU(cpu), pageaddr);
1862 static void tlbi_aa64_asid_write(CPUARMState *env, const ARMCPRegInfo *ri,
1865 /* Invalidate by ASID (AArch64 version) */
1866 ARMCPU *cpu = arm_env_get_cpu(env);
1867 int asid = extract64(value, 48, 16);
1868 tlb_flush(CPU(cpu), asid == 0);
1871 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri)
1873 /* We don't implement EL2, so the only control on DC ZVA is the
1874 * bit in the SCTLR which can prohibit access for EL0.
1876 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_DZE)) {
1877 return CP_ACCESS_TRAP;
1879 return CP_ACCESS_OK;
1882 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
1884 ARMCPU *cpu = arm_env_get_cpu(env);
1885 int dzp_bit = 1 << 4;
1887 /* DZP indicates whether DC ZVA access is allowed */
1888 if (aa64_zva_access(env, NULL) != CP_ACCESS_OK) {
1891 return cpu->dcz_blocksize | dzp_bit;
1894 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
1896 if (!(env->pstate & PSTATE_SP)) {
1897 /* Access to SP_EL0 is undefined if it's being used as
1898 * the stack pointer.
1900 return CP_ACCESS_TRAP_UNCATEGORIZED;
1902 return CP_ACCESS_OK;
1905 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
1907 return env->pstate & PSTATE_SP;
1910 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
1912 update_spsel(env, val);
1915 static const ARMCPRegInfo v8_cp_reginfo[] = {
1916 /* Minimal set of EL0-visible registers. This will need to be expanded
1917 * significantly for system emulation of AArch64 CPUs.
1919 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
1920 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
1921 .access = PL0_RW, .type = ARM_CP_NZCV },
1922 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
1923 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
1924 .type = ARM_CP_NO_MIGRATE,
1925 .access = PL0_RW, .accessfn = aa64_daif_access,
1926 .fieldoffset = offsetof(CPUARMState, daif),
1927 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
1928 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
1929 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
1930 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
1931 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
1932 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
1933 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
1934 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
1935 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
1936 .access = PL0_R, .type = ARM_CP_NO_MIGRATE,
1937 .readfn = aa64_dczid_read },
1938 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
1939 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
1940 .access = PL0_W, .type = ARM_CP_DC_ZVA,
1941 #ifndef CONFIG_USER_ONLY
1942 /* Avoid overhead of an access check that always passes in user-mode */
1943 .accessfn = aa64_zva_access,
1946 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
1947 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
1948 .access = PL1_R, .type = ARM_CP_CURRENTEL },
1949 /* Cache ops: all NOPs since we don't emulate caches */
1950 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
1951 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
1952 .access = PL1_W, .type = ARM_CP_NOP },
1953 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
1954 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
1955 .access = PL1_W, .type = ARM_CP_NOP },
1956 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
1957 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
1958 .access = PL0_W, .type = ARM_CP_NOP,
1959 .accessfn = aa64_cacheop_access },
1960 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
1961 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1962 .access = PL1_W, .type = ARM_CP_NOP },
1963 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
1964 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1965 .access = PL1_W, .type = ARM_CP_NOP },
1966 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
1967 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
1968 .access = PL0_W, .type = ARM_CP_NOP,
1969 .accessfn = aa64_cacheop_access },
1970 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
1971 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1972 .access = PL1_W, .type = ARM_CP_NOP },
1973 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
1974 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
1975 .access = PL0_W, .type = ARM_CP_NOP,
1976 .accessfn = aa64_cacheop_access },
1977 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
1978 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
1979 .access = PL0_W, .type = ARM_CP_NOP,
1980 .accessfn = aa64_cacheop_access },
1981 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
1982 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1983 .access = PL1_W, .type = ARM_CP_NOP },
1984 /* TLBI operations */
1985 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
1986 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
1987 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1988 .writefn = tlbiall_write },
1989 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
1990 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
1991 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1992 .writefn = tlbi_aa64_va_write },
1993 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
1994 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
1995 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
1996 .writefn = tlbi_aa64_asid_write },
1997 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
1998 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
1999 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2000 .writefn = tlbi_aa64_vaa_write },
2001 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
2002 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
2003 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2004 .writefn = tlbi_aa64_va_write },
2005 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
2006 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
2007 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2008 .writefn = tlbi_aa64_vaa_write },
2009 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
2010 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2011 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2012 .writefn = tlbiall_write },
2013 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
2014 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2015 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2016 .writefn = tlbi_aa64_va_write },
2017 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
2018 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2019 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2020 .writefn = tlbi_aa64_asid_write },
2021 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
2022 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2023 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2024 .writefn = tlbi_aa64_vaa_write },
2025 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
2026 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
2027 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2028 .writefn = tlbi_aa64_va_write },
2029 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
2030 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
2031 .access = PL1_W, .type = ARM_CP_NO_MIGRATE,
2032 .writefn = tlbi_aa64_vaa_write },
2033 #ifndef CONFIG_USER_ONLY
2034 /* 64 bit address translation operations */
2035 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
2036 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
2037 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
2038 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
2039 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
2040 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
2041 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
2042 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
2043 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
2044 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
2045 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
2046 .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write },
2048 /* 32 bit TLB invalidates, Inner Shareable */
2049 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2050 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write },
2051 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2052 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2053 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2054 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write },
2055 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2056 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write },
2057 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
2058 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2059 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
2060 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write },
2061 /* 32 bit ITLB invalidates */
2062 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2063 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write },
2064 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2065 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2066 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2067 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write },
2068 /* 32 bit DTLB invalidates */
2069 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2070 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write },
2071 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2072 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2073 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2074 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write },
2075 /* 32 bit TLB invalidates */
2076 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2077 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write },
2078 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2079 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2080 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2081 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write },
2082 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2083 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write },
2084 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
2085 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write },
2086 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
2087 .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write },
2088 /* 32 bit cache operations */
2089 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2090 .type = ARM_CP_NOP, .access = PL1_W },
2091 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
2092 .type = ARM_CP_NOP, .access = PL1_W },
2093 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2094 .type = ARM_CP_NOP, .access = PL1_W },
2095 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
2096 .type = ARM_CP_NOP, .access = PL1_W },
2097 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
2098 .type = ARM_CP_NOP, .access = PL1_W },
2099 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
2100 .type = ARM_CP_NOP, .access = PL1_W },
2101 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
2102 .type = ARM_CP_NOP, .access = PL1_W },
2103 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
2104 .type = ARM_CP_NOP, .access = PL1_W },
2105 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
2106 .type = ARM_CP_NOP, .access = PL1_W },
2107 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
2108 .type = ARM_CP_NOP, .access = PL1_W },
2109 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
2110 .type = ARM_CP_NOP, .access = PL1_W },
2111 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
2112 .type = ARM_CP_NOP, .access = PL1_W },
2113 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
2114 .type = ARM_CP_NOP, .access = PL1_W },
2115 /* MMU Domain access control / MPU write buffer control */
2116 { .name = "DACR", .cp = 15,
2117 .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
2118 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
2119 .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, },
2120 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
2121 .type = ARM_CP_NO_MIGRATE,
2122 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
2124 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
2125 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
2126 .type = ARM_CP_NO_MIGRATE,
2127 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
2128 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[0]) },
2129 /* We rely on the access checks not allowing the guest to write to the
2130 * state field when SPSel indicates that it's being used as the stack
2133 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
2134 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
2135 .access = PL1_RW, .accessfn = sp_el0_access,
2136 .type = ARM_CP_NO_MIGRATE,
2137 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
2138 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
2139 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
2140 .type = ARM_CP_NO_MIGRATE,
2141 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
2145 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
2146 static const ARMCPRegInfo v8_el3_no_el2_cp_reginfo[] = {
2147 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
2148 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
2150 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
2154 static const ARMCPRegInfo v8_el2_cp_reginfo[] = {
2155 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
2156 .type = ARM_CP_NO_MIGRATE,
2157 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
2159 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
2160 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
2161 .type = ARM_CP_NO_MIGRATE,
2162 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
2163 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
2164 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
2165 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
2166 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
2167 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
2168 .type = ARM_CP_NO_MIGRATE,
2169 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
2170 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[6]) },
2171 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
2172 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
2173 .access = PL2_RW, .writefn = vbar_write,
2174 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
2179 static const ARMCPRegInfo v8_el3_cp_reginfo[] = {
2180 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
2181 .type = ARM_CP_NO_MIGRATE,
2182 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
2184 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
2185 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
2186 .type = ARM_CP_NO_MIGRATE,
2187 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
2188 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
2189 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
2190 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
2191 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
2192 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
2193 .type = ARM_CP_NO_MIGRATE,
2194 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
2195 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[7]) },
2196 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
2197 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
2198 .access = PL3_RW, .writefn = vbar_write,
2199 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
2204 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2207 ARMCPU *cpu = arm_env_get_cpu(env);
2209 if (raw_read(env, ri) == value) {
2210 /* Skip the TLB flush if nothing actually changed; Linux likes
2211 * to do a lot of pointless SCTLR writes.
2216 raw_write(env, ri, value);
2217 /* ??? Lots of these bits are not implemented. */
2218 /* This may enable/disable the MMU, so do a TLB flush. */
2219 tlb_flush(CPU(cpu), 1);
2222 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri)
2224 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
2225 * but the AArch32 CTR has its own reginfo struct)
2227 if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UCT)) {
2228 return CP_ACCESS_TRAP;
2230 return CP_ACCESS_OK;
2233 static const ARMCPRegInfo debug_cp_reginfo[] = {
2234 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
2235 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
2236 * unlike DBGDRAR it is never accessible from EL0.
2237 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
2240 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
2241 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2242 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
2243 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
2244 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2245 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2246 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2247 /* Dummy implementation of monitor debug system control register:
2248 * we don't support debug. (The 32-bit alias is DBGDSCRext.)
2250 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
2251 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
2253 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
2255 /* We define a dummy WI OSLAR_EL1, because Linux writes to it. */
2256 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
2257 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
2258 .access = PL1_W, .type = ARM_CP_NOP },
2262 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
2263 /* 64 bit access versions of the (dummy) debug registers */
2264 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
2265 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
2266 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
2267 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
2271 void hw_watchpoint_update(ARMCPU *cpu, int n)
2273 CPUARMState *env = &cpu->env;
2275 vaddr wvr = env->cp15.dbgwvr[n];
2276 uint64_t wcr = env->cp15.dbgwcr[n];
2278 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
2280 if (env->cpu_watchpoint[n]) {
2281 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
2282 env->cpu_watchpoint[n] = NULL;
2285 if (!extract64(wcr, 0, 1)) {
2286 /* E bit clear : watchpoint disabled */
2290 switch (extract64(wcr, 3, 2)) {
2292 /* LSC 00 is reserved and must behave as if the wp is disabled */
2295 flags |= BP_MEM_READ;
2298 flags |= BP_MEM_WRITE;
2301 flags |= BP_MEM_ACCESS;
2305 /* Attempts to use both MASK and BAS fields simultaneously are
2306 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
2307 * thus generating a watchpoint for every byte in the masked region.
2309 mask = extract64(wcr, 24, 4);
2310 if (mask == 1 || mask == 2) {
2311 /* Reserved values of MASK; we must act as if the mask value was
2312 * some non-reserved value, or as if the watchpoint were disabled.
2313 * We choose the latter.
2317 /* Watchpoint covers an aligned area up to 2GB in size */
2319 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
2320 * whether the watchpoint fires when the unmasked bits match; we opt
2321 * to generate the exceptions.
2325 /* Watchpoint covers bytes defined by the byte address select bits */
2326 int bas = extract64(wcr, 5, 8);
2330 /* This must act as if the watchpoint is disabled */
2334 if (extract64(wvr, 2, 1)) {
2335 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
2336 * ignored, and BAS[3:0] define which bytes to watch.
2340 /* The BAS bits are supposed to be programmed to indicate a contiguous
2341 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
2342 * we fire for each byte in the word/doubleword addressed by the WVR.
2343 * We choose to ignore any non-zero bits after the first range of 1s.
2345 basstart = ctz32(bas);
2346 len = cto32(bas >> basstart);
2350 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
2351 &env->cpu_watchpoint[n]);
2354 void hw_watchpoint_update_all(ARMCPU *cpu)
2357 CPUARMState *env = &cpu->env;
2359 /* Completely clear out existing QEMU watchpoints and our array, to
2360 * avoid possible stale entries following migration load.
2362 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
2363 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
2365 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
2366 hw_watchpoint_update(cpu, i);
2370 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2373 ARMCPU *cpu = arm_env_get_cpu(env);
2376 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
2377 * register reads and behaves as if values written are sign extended.
2378 * Bits [1:0] are RES0.
2380 value = sextract64(value, 0, 49) & ~3ULL;
2382 raw_write(env, ri, value);
2383 hw_watchpoint_update(cpu, i);
2386 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2389 ARMCPU *cpu = arm_env_get_cpu(env);
2392 raw_write(env, ri, value);
2393 hw_watchpoint_update(cpu, i);
2396 static void define_debug_regs(ARMCPU *cpu)
2398 /* Define v7 and v8 architectural debug registers.
2399 * These are just dummy implementations for now.
2402 int wrps, brps, ctx_cmps;
2403 ARMCPRegInfo dbgdidr = {
2404 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
2405 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
2408 /* Note that all these register fields hold "number of Xs minus 1". */
2409 brps = extract32(cpu->dbgdidr, 24, 4);
2410 wrps = extract32(cpu->dbgdidr, 28, 4);
2411 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
2413 assert(ctx_cmps <= brps);
2415 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
2416 * of the debug registers such as number of breakpoints;
2417 * check that if they both exist then they agree.
2419 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
2420 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
2421 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
2422 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
2425 define_one_arm_cp_reg(cpu, &dbgdidr);
2426 define_arm_cp_regs(cpu, debug_cp_reginfo);
2428 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
2429 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
2432 for (i = 0; i < brps + 1; i++) {
2433 ARMCPRegInfo dbgregs[] = {
2434 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
2435 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
2437 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]) },
2438 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
2439 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
2441 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]) },
2444 define_arm_cp_regs(cpu, dbgregs);
2447 for (i = 0; i < wrps + 1; i++) {
2448 ARMCPRegInfo dbgregs[] = {
2449 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
2450 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
2452 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
2453 .writefn = dbgwvr_write, .raw_writefn = raw_write
2455 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
2456 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
2458 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
2459 .writefn = dbgwcr_write, .raw_writefn = raw_write
2463 define_arm_cp_regs(cpu, dbgregs);
2467 void register_cp_regs_for_features(ARMCPU *cpu)
2469 /* Register all the coprocessor registers based on feature bits */
2470 CPUARMState *env = &cpu->env;
2471 if (arm_feature(env, ARM_FEATURE_M)) {
2472 /* M profile has no coprocessor registers */
2476 define_arm_cp_regs(cpu, cp_reginfo);
2477 if (!arm_feature(env, ARM_FEATURE_V8)) {
2478 /* Must go early as it is full of wildcards that may be
2479 * overridden by later definitions.
2481 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
2484 if (arm_feature(env, ARM_FEATURE_V6)) {
2485 /* The ID registers all have impdef reset values */
2486 ARMCPRegInfo v6_idregs[] = {
2487 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
2488 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
2489 .access = PL1_R, .type = ARM_CP_CONST,
2490 .resetvalue = cpu->id_pfr0 },
2491 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
2492 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
2493 .access = PL1_R, .type = ARM_CP_CONST,
2494 .resetvalue = cpu->id_pfr1 },
2495 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
2496 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
2497 .access = PL1_R, .type = ARM_CP_CONST,
2498 .resetvalue = cpu->id_dfr0 },
2499 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
2500 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
2501 .access = PL1_R, .type = ARM_CP_CONST,
2502 .resetvalue = cpu->id_afr0 },
2503 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
2504 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
2505 .access = PL1_R, .type = ARM_CP_CONST,
2506 .resetvalue = cpu->id_mmfr0 },
2507 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
2508 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
2509 .access = PL1_R, .type = ARM_CP_CONST,
2510 .resetvalue = cpu->id_mmfr1 },
2511 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
2512 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
2513 .access = PL1_R, .type = ARM_CP_CONST,
2514 .resetvalue = cpu->id_mmfr2 },
2515 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
2516 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
2517 .access = PL1_R, .type = ARM_CP_CONST,
2518 .resetvalue = cpu->id_mmfr3 },
2519 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
2520 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
2521 .access = PL1_R, .type = ARM_CP_CONST,
2522 .resetvalue = cpu->id_isar0 },
2523 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
2524 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
2525 .access = PL1_R, .type = ARM_CP_CONST,
2526 .resetvalue = cpu->id_isar1 },
2527 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
2528 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
2529 .access = PL1_R, .type = ARM_CP_CONST,
2530 .resetvalue = cpu->id_isar2 },
2531 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
2532 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
2533 .access = PL1_R, .type = ARM_CP_CONST,
2534 .resetvalue = cpu->id_isar3 },
2535 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
2536 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
2537 .access = PL1_R, .type = ARM_CP_CONST,
2538 .resetvalue = cpu->id_isar4 },
2539 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
2540 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
2541 .access = PL1_R, .type = ARM_CP_CONST,
2542 .resetvalue = cpu->id_isar5 },
2543 /* 6..7 are as yet unallocated and must RAZ */
2544 { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2,
2545 .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST,
2547 { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2,
2548 .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST,
2552 define_arm_cp_regs(cpu, v6_idregs);
2553 define_arm_cp_regs(cpu, v6_cp_reginfo);
2555 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
2557 if (arm_feature(env, ARM_FEATURE_V6K)) {
2558 define_arm_cp_regs(cpu, v6k_cp_reginfo);
2560 if (arm_feature(env, ARM_FEATURE_V7)) {
2561 /* v7 performance monitor control register: same implementor
2562 * field as main ID register, and we implement only the cycle
2565 #ifndef CONFIG_USER_ONLY
2566 ARMCPRegInfo pmcr = {
2567 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
2569 .type = ARM_CP_IO | ARM_CP_NO_MIGRATE,
2570 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
2571 .accessfn = pmreg_access, .writefn = pmcr_write,
2572 .raw_writefn = raw_write,
2574 ARMCPRegInfo pmcr64 = {
2575 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
2576 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
2577 .access = PL0_RW, .accessfn = pmreg_access,
2579 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
2580 .resetvalue = cpu->midr & 0xff000000,
2581 .writefn = pmcr_write, .raw_writefn = raw_write,
2583 define_one_arm_cp_reg(cpu, &pmcr);
2584 define_one_arm_cp_reg(cpu, &pmcr64);
2586 ARMCPRegInfo clidr = {
2587 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
2588 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
2589 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
2591 define_one_arm_cp_reg(cpu, &clidr);
2592 define_arm_cp_regs(cpu, v7_cp_reginfo);
2593 define_debug_regs(cpu);
2595 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
2597 if (arm_feature(env, ARM_FEATURE_V8)) {
2598 /* AArch64 ID registers, which all have impdef reset values */
2599 ARMCPRegInfo v8_idregs[] = {
2600 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
2601 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
2602 .access = PL1_R, .type = ARM_CP_CONST,
2603 .resetvalue = cpu->id_aa64pfr0 },
2604 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
2605 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
2606 .access = PL1_R, .type = ARM_CP_CONST,
2607 .resetvalue = cpu->id_aa64pfr1},
2608 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
2609 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
2610 .access = PL1_R, .type = ARM_CP_CONST,
2611 /* We mask out the PMUVer field, because we don't currently
2612 * implement the PMU. Not advertising it prevents the guest
2613 * from trying to use it and getting UNDEFs on registers we
2616 .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
2617 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
2618 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
2619 .access = PL1_R, .type = ARM_CP_CONST,
2620 .resetvalue = cpu->id_aa64dfr1 },
2621 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
2622 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
2623 .access = PL1_R, .type = ARM_CP_CONST,
2624 .resetvalue = cpu->id_aa64afr0 },
2625 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
2626 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
2627 .access = PL1_R, .type = ARM_CP_CONST,
2628 .resetvalue = cpu->id_aa64afr1 },
2629 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
2630 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
2631 .access = PL1_R, .type = ARM_CP_CONST,
2632 .resetvalue = cpu->id_aa64isar0 },
2633 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
2634 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
2635 .access = PL1_R, .type = ARM_CP_CONST,
2636 .resetvalue = cpu->id_aa64isar1 },
2637 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
2638 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
2639 .access = PL1_R, .type = ARM_CP_CONST,
2640 .resetvalue = cpu->id_aa64mmfr0 },
2641 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
2642 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
2643 .access = PL1_R, .type = ARM_CP_CONST,
2644 .resetvalue = cpu->id_aa64mmfr1 },
2645 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
2646 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
2647 .access = PL1_R, .type = ARM_CP_CONST,
2648 .resetvalue = cpu->mvfr0 },
2649 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
2650 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
2651 .access = PL1_R, .type = ARM_CP_CONST,
2652 .resetvalue = cpu->mvfr1 },
2653 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
2654 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
2655 .access = PL1_R, .type = ARM_CP_CONST,
2656 .resetvalue = cpu->mvfr2 },
2659 ARMCPRegInfo rvbar = {
2660 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
2661 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
2662 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
2664 define_one_arm_cp_reg(cpu, &rvbar);
2665 define_arm_cp_regs(cpu, v8_idregs);
2666 define_arm_cp_regs(cpu, v8_cp_reginfo);
2668 if (arm_feature(env, ARM_FEATURE_EL2)) {
2669 define_arm_cp_regs(cpu, v8_el2_cp_reginfo);
2671 /* If EL2 is missing but higher ELs are enabled, we need to
2672 * register the no_el2 reginfos.
2674 if (arm_feature(env, ARM_FEATURE_EL3)) {
2675 define_arm_cp_regs(cpu, v8_el3_no_el2_cp_reginfo);
2678 if (arm_feature(env, ARM_FEATURE_EL3)) {
2679 define_arm_cp_regs(cpu, v8_el3_cp_reginfo);
2681 if (arm_feature(env, ARM_FEATURE_MPU)) {
2682 /* These are the MPU registers prior to PMSAv6. Any new
2683 * PMSA core later than the ARM946 will require that we
2684 * implement the PMSAv6 or PMSAv7 registers, which are
2685 * completely different.
2687 assert(!arm_feature(env, ARM_FEATURE_V6));
2688 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
2690 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
2692 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
2693 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
2695 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
2696 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
2698 if (arm_feature(env, ARM_FEATURE_VAPA)) {
2699 define_arm_cp_regs(cpu, vapa_cp_reginfo);
2701 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
2702 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
2704 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
2705 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
2707 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
2708 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
2710 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2711 define_arm_cp_regs(cpu, omap_cp_reginfo);
2713 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
2714 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
2716 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2717 define_arm_cp_regs(cpu, xscale_cp_reginfo);
2719 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
2720 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
2722 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2723 define_arm_cp_regs(cpu, lpae_cp_reginfo);
2725 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
2726 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
2727 * be read-only (ie write causes UNDEF exception).
2730 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
2731 /* Pre-v8 MIDR space.
2732 * Note that the MIDR isn't a simple constant register because
2733 * of the TI925 behaviour where writes to another register can
2734 * cause the MIDR value to change.
2736 * Unimplemented registers in the c15 0 0 0 space default to
2737 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
2738 * and friends override accordingly.
2741 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
2742 .access = PL1_R, .resetvalue = cpu->midr,
2743 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
2744 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
2745 .type = ARM_CP_OVERRIDE },
2746 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
2748 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
2749 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2751 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
2752 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2754 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
2755 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2757 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
2758 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2760 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
2761 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2764 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
2765 /* v8 MIDR -- the wildcard isn't necessary, and nor is the
2766 * variable-MIDR TI925 behaviour. Instead we have a single
2767 * (strictly speaking IMPDEF) alias of the MIDR, REVIDR.
2769 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
2770 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
2771 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr },
2772 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
2773 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
2774 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr },
2777 ARMCPRegInfo id_cp_reginfo[] = {
2778 /* These are common to v8 and pre-v8 */
2780 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
2781 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
2782 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
2783 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
2784 .access = PL0_R, .accessfn = ctr_el0_access,
2785 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
2786 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
2788 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
2789 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2791 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
2792 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
2795 ARMCPRegInfo crn0_wi_reginfo = {
2796 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
2797 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
2798 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
2800 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
2801 arm_feature(env, ARM_FEATURE_STRONGARM)) {
2803 /* Register the blanket "writes ignored" value first to cover the
2804 * whole space. Then update the specific ID registers to allow write
2805 * access, so that they ignore writes rather than causing them to
2808 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
2809 for (r = id_pre_v8_midr_cp_reginfo;
2810 r->type != ARM_CP_SENTINEL; r++) {
2813 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
2817 if (arm_feature(env, ARM_FEATURE_V8)) {
2818 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
2820 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
2822 define_arm_cp_regs(cpu, id_cp_reginfo);
2825 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
2826 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
2829 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
2830 ARMCPRegInfo auxcr = {
2831 .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
2832 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
2833 .access = PL1_RW, .type = ARM_CP_CONST,
2834 .resetvalue = cpu->reset_auxcr
2836 define_one_arm_cp_reg(cpu, &auxcr);
2839 if (arm_feature(env, ARM_FEATURE_CBAR)) {
2840 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
2841 /* 32 bit view is [31:18] 0...0 [43:32]. */
2842 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
2843 | extract64(cpu->reset_cbar, 32, 12);
2844 ARMCPRegInfo cbar_reginfo[] = {
2846 .type = ARM_CP_CONST,
2847 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
2848 .access = PL1_R, .resetvalue = cpu->reset_cbar },
2849 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
2850 .type = ARM_CP_CONST,
2851 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
2852 .access = PL1_R, .resetvalue = cbar32 },
2855 /* We don't implement a r/w 64 bit CBAR currently */
2856 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
2857 define_arm_cp_regs(cpu, cbar_reginfo);
2859 ARMCPRegInfo cbar = {
2861 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
2862 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
2863 .fieldoffset = offsetof(CPUARMState,
2864 cp15.c15_config_base_address)
2866 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
2867 cbar.access = PL1_R;
2868 cbar.fieldoffset = 0;
2869 cbar.type = ARM_CP_CONST;
2871 define_one_arm_cp_reg(cpu, &cbar);
2875 /* Generic registers whose values depend on the implementation */
2877 ARMCPRegInfo sctlr = {
2878 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
2879 .opc0 = 3, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
2880 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys),
2881 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
2882 .raw_writefn = raw_write,
2884 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2885 /* Normally we would always end the TB on an SCTLR write, but Linux
2886 * arch/arm/mach-pxa/sleep.S expects two instructions following
2887 * an MMU enable to execute from cache. Imitate this behaviour.
2889 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
2891 define_one_arm_cp_reg(cpu, &sctlr);
2895 ARMCPU *cpu_arm_init(const char *cpu_model)
2897 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
2900 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
2902 CPUState *cs = CPU(cpu);
2903 CPUARMState *env = &cpu->env;
2905 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
2906 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
2907 aarch64_fpu_gdb_set_reg,
2908 34, "aarch64-fpu.xml", 0);
2909 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
2910 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
2911 51, "arm-neon.xml", 0);
2912 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
2913 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
2914 35, "arm-vfp3.xml", 0);
2915 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
2916 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
2917 19, "arm-vfp.xml", 0);
2921 /* Sort alphabetically by type name, except for "any". */
2922 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
2924 ObjectClass *class_a = (ObjectClass *)a;
2925 ObjectClass *class_b = (ObjectClass *)b;
2926 const char *name_a, *name_b;
2928 name_a = object_class_get_name(class_a);
2929 name_b = object_class_get_name(class_b);
2930 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
2932 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
2935 return strcmp(name_a, name_b);
2939 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
2941 ObjectClass *oc = data;
2942 CPUListState *s = user_data;
2943 const char *typename;
2946 typename = object_class_get_name(oc);
2947 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
2948 (*s->cpu_fprintf)(s->file, " %s\n",
2953 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
2957 .cpu_fprintf = cpu_fprintf,
2961 list = object_class_get_list(TYPE_ARM_CPU, false);
2962 list = g_slist_sort(list, arm_cpu_list_compare);
2963 (*cpu_fprintf)(f, "Available CPUs:\n");
2964 g_slist_foreach(list, arm_cpu_list_entry, &s);
2967 /* The 'host' CPU type is dynamically registered only if KVM is
2968 * enabled, so we have to special-case it here:
2970 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
2974 static void arm_cpu_add_definition(gpointer data, gpointer user_data)
2976 ObjectClass *oc = data;
2977 CpuDefinitionInfoList **cpu_list = user_data;
2978 CpuDefinitionInfoList *entry;
2979 CpuDefinitionInfo *info;
2980 const char *typename;
2982 typename = object_class_get_name(oc);
2983 info = g_malloc0(sizeof(*info));
2984 info->name = g_strndup(typename,
2985 strlen(typename) - strlen("-" TYPE_ARM_CPU));
2987 entry = g_malloc0(sizeof(*entry));
2988 entry->value = info;
2989 entry->next = *cpu_list;
2993 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
2995 CpuDefinitionInfoList *cpu_list = NULL;
2998 list = object_class_get_list(TYPE_ARM_CPU, false);
2999 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
3005 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
3006 void *opaque, int state,
3007 int crm, int opc1, int opc2)
3009 /* Private utility function for define_one_arm_cp_reg_with_opaque():
3010 * add a single reginfo struct to the hash table.
3012 uint32_t *key = g_new(uint32_t, 1);
3013 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
3014 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3015 if (r->state == ARM_CP_STATE_BOTH && state == ARM_CP_STATE_AA32) {
3016 /* The AArch32 view of a shared register sees the lower 32 bits
3017 * of a 64 bit backing field. It is not migratable as the AArch64
3018 * view handles that. AArch64 also handles reset.
3019 * We assume it is a cp15 register if the .cp field is left unset.
3024 r2->type |= ARM_CP_NO_MIGRATE;
3025 r2->resetfn = arm_cp_reset_ignore;
3026 #ifdef HOST_WORDS_BIGENDIAN
3027 if (r2->fieldoffset) {
3028 r2->fieldoffset += sizeof(uint32_t);
3032 if (state == ARM_CP_STATE_AA64) {
3033 /* To allow abbreviation of ARMCPRegInfo
3034 * definitions, we treat cp == 0 as equivalent to
3035 * the value for "standard guest-visible sysreg".
3036 * STATE_BOTH definitions are also always "standard
3037 * sysreg" in their AArch64 view (the .cp value may
3038 * be non-zero for the benefit of the AArch32 view).
3040 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
3041 r2->cp = CP_REG_ARM64_SYSREG_CP;
3043 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
3044 r2->opc0, opc1, opc2);
3046 *key = ENCODE_CP_REG(r2->cp, is64, r2->crn, crm, opc1, opc2);
3049 r2->opaque = opaque;
3051 /* reginfo passed to helpers is correct for the actual access,
3052 * and is never ARM_CP_STATE_BOTH:
3055 /* Make sure reginfo passed to helpers for wildcarded regs
3056 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
3061 /* By convention, for wildcarded registers only the first
3062 * entry is used for migration; the others are marked as
3063 * NO_MIGRATE so we don't try to transfer the register
3064 * multiple times. Special registers (ie NOP/WFI) are
3067 if ((r->type & ARM_CP_SPECIAL) ||
3068 ((r->crm == CP_ANY) && crm != 0) ||
3069 ((r->opc1 == CP_ANY) && opc1 != 0) ||
3070 ((r->opc2 == CP_ANY) && opc2 != 0)) {
3071 r2->type |= ARM_CP_NO_MIGRATE;
3074 /* Overriding of an existing definition must be explicitly
3077 if (!(r->type & ARM_CP_OVERRIDE)) {
3078 ARMCPRegInfo *oldreg;
3079 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
3080 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
3081 fprintf(stderr, "Register redefined: cp=%d %d bit "
3082 "crn=%d crm=%d opc1=%d opc2=%d, "
3083 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
3084 r2->crn, r2->crm, r2->opc1, r2->opc2,
3085 oldreg->name, r2->name);
3086 g_assert_not_reached();
3089 g_hash_table_insert(cpu->cp_regs, key, r2);
3093 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
3094 const ARMCPRegInfo *r, void *opaque)
3096 /* Define implementations of coprocessor registers.
3097 * We store these in a hashtable because typically
3098 * there are less than 150 registers in a space which
3099 * is 16*16*16*8*8 = 262144 in size.
3100 * Wildcarding is supported for the crm, opc1 and opc2 fields.
3101 * If a register is defined twice then the second definition is
3102 * used, so this can be used to define some generic registers and
3103 * then override them with implementation specific variations.
3104 * At least one of the original and the second definition should
3105 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
3106 * against accidental use.
3108 * The state field defines whether the register is to be
3109 * visible in the AArch32 or AArch64 execution state. If the
3110 * state is set to ARM_CP_STATE_BOTH then we synthesise a
3111 * reginfo structure for the AArch32 view, which sees the lower
3112 * 32 bits of the 64 bit register.
3114 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
3115 * be wildcarded. AArch64 registers are always considered to be 64
3116 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
3117 * the register, if any.
3119 int crm, opc1, opc2, state;
3120 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
3121 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
3122 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
3123 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
3124 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
3125 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
3126 /* 64 bit registers have only CRm and Opc1 fields */
3127 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
3128 /* op0 only exists in the AArch64 encodings */
3129 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
3130 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
3131 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
3132 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
3133 * encodes a minimum access level for the register. We roll this
3134 * runtime check into our general permission check code, so check
3135 * here that the reginfo's specified permissions are strict enough
3136 * to encompass the generic architectural permission check.
3138 if (r->state != ARM_CP_STATE_AA32) {
3141 case 0: case 1: case 2:
3154 /* unallocated encoding, so not possible */
3162 /* min_EL EL1, secure mode only (we don't check the latter) */
3166 /* broken reginfo with out-of-range opc1 */
3170 /* assert our permissions are not too lax (stricter is fine) */
3171 assert((r->access & ~mask) == 0);
3174 /* Check that the register definition has enough info to handle
3175 * reads and writes if they are permitted.
3177 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
3178 if (r->access & PL3_R) {
3179 assert(r->fieldoffset || r->readfn);
3181 if (r->access & PL3_W) {
3182 assert(r->fieldoffset || r->writefn);
3185 /* Bad type field probably means missing sentinel at end of reg list */
3186 assert(cptype_valid(r->type));
3187 for (crm = crmmin; crm <= crmmax; crm++) {
3188 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
3189 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
3190 for (state = ARM_CP_STATE_AA32;
3191 state <= ARM_CP_STATE_AA64; state++) {
3192 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
3195 add_cpreg_to_hashtable(cpu, r, opaque, state,
3203 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
3204 const ARMCPRegInfo *regs, void *opaque)
3206 /* Define a whole list of registers */
3207 const ARMCPRegInfo *r;
3208 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
3209 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
3213 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
3215 return g_hash_table_lookup(cpregs, &encoded_cp);
3218 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
3221 /* Helper coprocessor write function for write-ignore registers */
3224 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
3226 /* Helper coprocessor write function for read-as-zero registers */
3230 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
3232 /* Helper coprocessor reset function for do-nothing-on-reset registers */
3235 static int bad_mode_switch(CPUARMState *env, int mode)
3237 /* Return true if it is not valid for us to switch to
3238 * this CPU mode (ie all the UNPREDICTABLE cases in
3239 * the ARM ARM CPSRWriteByInstr pseudocode).
3242 case ARM_CPU_MODE_USR:
3243 case ARM_CPU_MODE_SYS:
3244 case ARM_CPU_MODE_SVC:
3245 case ARM_CPU_MODE_ABT:
3246 case ARM_CPU_MODE_UND:
3247 case ARM_CPU_MODE_IRQ:
3248 case ARM_CPU_MODE_FIQ:
3255 uint32_t cpsr_read(CPUARMState *env)
3258 ZF = (env->ZF == 0);
3259 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
3260 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
3261 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
3262 | ((env->condexec_bits & 0xfc) << 8)
3263 | (env->GE << 16) | (env->daif & CPSR_AIF);
3266 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
3268 if (mask & CPSR_NZCV) {
3269 env->ZF = (~val) & CPSR_Z;
3271 env->CF = (val >> 29) & 1;
3272 env->VF = (val << 3) & 0x80000000;
3275 env->QF = ((val & CPSR_Q) != 0);
3277 env->thumb = ((val & CPSR_T) != 0);
3278 if (mask & CPSR_IT_0_1) {
3279 env->condexec_bits &= ~3;
3280 env->condexec_bits |= (val >> 25) & 3;
3282 if (mask & CPSR_IT_2_7) {
3283 env->condexec_bits &= 3;
3284 env->condexec_bits |= (val >> 8) & 0xfc;
3286 if (mask & CPSR_GE) {
3287 env->GE = (val >> 16) & 0xf;
3290 env->daif &= ~(CPSR_AIF & mask);
3291 env->daif |= val & CPSR_AIF & mask;
3293 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
3294 if (bad_mode_switch(env, val & CPSR_M)) {
3295 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
3296 * We choose to ignore the attempt and leave the CPSR M field
3301 switch_mode(env, val & CPSR_M);
3304 mask &= ~CACHED_CPSR_BITS;
3305 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
3308 /* Sign/zero extend */
3309 uint32_t HELPER(sxtb16)(uint32_t x)
3312 res = (uint16_t)(int8_t)x;
3313 res |= (uint32_t)(int8_t)(x >> 16) << 16;
3317 uint32_t HELPER(uxtb16)(uint32_t x)
3320 res = (uint16_t)(uint8_t)x;
3321 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
3325 uint32_t HELPER(clz)(uint32_t x)
3330 int32_t HELPER(sdiv)(int32_t num, int32_t den)
3334 if (num == INT_MIN && den == -1)
3339 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
3346 uint32_t HELPER(rbit)(uint32_t x)
3348 x = ((x & 0xff000000) >> 24)
3349 | ((x & 0x00ff0000) >> 8)
3350 | ((x & 0x0000ff00) << 8)
3351 | ((x & 0x000000ff) << 24);
3352 x = ((x & 0xf0f0f0f0) >> 4)
3353 | ((x & 0x0f0f0f0f) << 4);
3354 x = ((x & 0x88888888) >> 3)
3355 | ((x & 0x44444444) >> 1)
3356 | ((x & 0x22222222) << 1)
3357 | ((x & 0x11111111) << 3);
3361 #if defined(CONFIG_USER_ONLY)
3363 void arm_cpu_do_interrupt(CPUState *cs)
3365 cs->exception_index = -1;
3368 int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
3371 ARMCPU *cpu = ARM_CPU(cs);
3372 CPUARMState *env = &cpu->env;
3374 env->exception.vaddress = address;
3376 cs->exception_index = EXCP_PREFETCH_ABORT;
3378 cs->exception_index = EXCP_DATA_ABORT;
3383 /* These should probably raise undefined insn exceptions. */
3384 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
3386 ARMCPU *cpu = arm_env_get_cpu(env);
3388 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
3391 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
3393 ARMCPU *cpu = arm_env_get_cpu(env);
3395 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
3399 void switch_mode(CPUARMState *env, int mode)
3401 ARMCPU *cpu = arm_env_get_cpu(env);
3403 if (mode != ARM_CPU_MODE_USR) {
3404 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
3408 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
3410 ARMCPU *cpu = arm_env_get_cpu(env);
3412 cpu_abort(CPU(cpu), "banked r13 write\n");
3415 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
3417 ARMCPU *cpu = arm_env_get_cpu(env);
3419 cpu_abort(CPU(cpu), "banked r13 read\n");
3425 /* Map CPU modes onto saved register banks. */
3426 int bank_number(int mode)
3429 case ARM_CPU_MODE_USR:
3430 case ARM_CPU_MODE_SYS:
3432 case ARM_CPU_MODE_SVC:
3434 case ARM_CPU_MODE_ABT:
3436 case ARM_CPU_MODE_UND:
3438 case ARM_CPU_MODE_IRQ:
3440 case ARM_CPU_MODE_FIQ:
3442 case ARM_CPU_MODE_HYP:
3444 case ARM_CPU_MODE_MON:
3447 hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode);
3450 void switch_mode(CPUARMState *env, int mode)
3455 old_mode = env->uncached_cpsr & CPSR_M;
3456 if (mode == old_mode)
3459 if (old_mode == ARM_CPU_MODE_FIQ) {
3460 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
3461 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
3462 } else if (mode == ARM_CPU_MODE_FIQ) {
3463 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
3464 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
3467 i = bank_number(old_mode);
3468 env->banked_r13[i] = env->regs[13];
3469 env->banked_r14[i] = env->regs[14];
3470 env->banked_spsr[i] = env->spsr;
3472 i = bank_number(mode);
3473 env->regs[13] = env->banked_r13[i];
3474 env->regs[14] = env->banked_r14[i];
3475 env->spsr = env->banked_spsr[i];
3478 static void v7m_push(CPUARMState *env, uint32_t val)
3480 CPUState *cs = CPU(arm_env_get_cpu(env));
3483 stl_phys(cs->as, env->regs[13], val);
3486 static uint32_t v7m_pop(CPUARMState *env)
3488 CPUState *cs = CPU(arm_env_get_cpu(env));
3491 val = ldl_phys(cs->as, env->regs[13]);
3496 /* Switch to V7M main or process stack pointer. */
3497 static void switch_v7m_sp(CPUARMState *env, int process)
3500 if (env->v7m.current_sp != process) {
3501 tmp = env->v7m.other_sp;
3502 env->v7m.other_sp = env->regs[13];
3503 env->regs[13] = tmp;
3504 env->v7m.current_sp = process;
3508 static void do_v7m_exception_exit(CPUARMState *env)
3513 type = env->regs[15];
3514 if (env->v7m.exception != 0)
3515 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
3517 /* Switch to the target stack. */
3518 switch_v7m_sp(env, (type & 4) != 0);
3519 /* Pop registers. */
3520 env->regs[0] = v7m_pop(env);
3521 env->regs[1] = v7m_pop(env);
3522 env->regs[2] = v7m_pop(env);
3523 env->regs[3] = v7m_pop(env);
3524 env->regs[12] = v7m_pop(env);
3525 env->regs[14] = v7m_pop(env);
3526 env->regs[15] = v7m_pop(env);
3527 xpsr = v7m_pop(env);
3528 xpsr_write(env, xpsr, 0xfffffdff);
3529 /* Undo stack alignment. */
3532 /* ??? The exception return type specifies Thread/Handler mode. However
3533 this is also implied by the xPSR value. Not sure what to do
3534 if there is a mismatch. */
3535 /* ??? Likewise for mismatches between the CONTROL register and the stack
3539 void arm_v7m_cpu_do_interrupt(CPUState *cs)
3541 ARMCPU *cpu = ARM_CPU(cs);
3542 CPUARMState *env = &cpu->env;
3543 uint32_t xpsr = xpsr_read(env);
3547 arm_log_exception(cs->exception_index);
3550 if (env->v7m.current_sp)
3552 if (env->v7m.exception == 0)
3555 /* For exceptions we just mark as pending on the NVIC, and let that
3557 /* TODO: Need to escalate if the current priority is higher than the
3558 one we're raising. */
3559 switch (cs->exception_index) {
3561 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
3564 /* The PC already points to the next instruction. */
3565 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
3567 case EXCP_PREFETCH_ABORT:
3568 case EXCP_DATA_ABORT:
3569 /* TODO: if we implemented the MPU registers, this is where we
3570 * should set the MMFAR, etc from exception.fsr and exception.vaddress.
3572 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
3575 if (semihosting_enabled) {
3577 nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
3580 env->regs[0] = do_arm_semihosting(env);
3581 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
3585 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
3588 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
3590 case EXCP_EXCEPTION_EXIT:
3591 do_v7m_exception_exit(env);
3594 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
3595 return; /* Never happens. Keep compiler happy. */
3598 /* Align stack pointer. */
3599 /* ??? Should only do this if Configuration Control Register
3600 STACKALIGN bit is set. */
3601 if (env->regs[13] & 4) {
3605 /* Switch to the handler mode. */
3606 v7m_push(env, xpsr);
3607 v7m_push(env, env->regs[15]);
3608 v7m_push(env, env->regs[14]);
3609 v7m_push(env, env->regs[12]);
3610 v7m_push(env, env->regs[3]);
3611 v7m_push(env, env->regs[2]);
3612 v7m_push(env, env->regs[1]);
3613 v7m_push(env, env->regs[0]);
3614 switch_v7m_sp(env, 0);
3616 env->condexec_bits = 0;
3618 addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
3619 env->regs[15] = addr & 0xfffffffe;
3620 env->thumb = addr & 1;
3623 /* Handle a CPU exception. */
3624 void arm_cpu_do_interrupt(CPUState *cs)
3626 ARMCPU *cpu = ARM_CPU(cs);
3627 CPUARMState *env = &cpu->env;
3635 arm_log_exception(cs->exception_index);
3637 /* TODO: Vectored interrupt controller. */
3638 switch (cs->exception_index) {
3640 new_mode = ARM_CPU_MODE_UND;
3649 if (semihosting_enabled) {
3650 /* Check for semihosting interrupt. */
3652 mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code)
3655 mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code)
3658 /* Only intercept calls from privileged modes, to provide some
3659 semblance of security. */
3660 if (((mask == 0x123456 && !env->thumb)
3661 || (mask == 0xab && env->thumb))
3662 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
3663 env->regs[0] = do_arm_semihosting(env);
3664 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
3668 new_mode = ARM_CPU_MODE_SVC;
3671 /* The PC already points to the next instruction. */
3675 /* See if this is a semihosting syscall. */
3676 if (env->thumb && semihosting_enabled) {
3677 mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff;
3679 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
3681 env->regs[0] = do_arm_semihosting(env);
3682 qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n");
3686 env->exception.fsr = 2;
3687 /* Fall through to prefetch abort. */
3688 case EXCP_PREFETCH_ABORT:
3689 env->cp15.ifsr_el2 = env->exception.fsr;
3690 env->cp15.far_el[1] = deposit64(env->cp15.far_el[1], 32, 32,
3691 env->exception.vaddress);
3692 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
3693 env->cp15.ifsr_el2, (uint32_t)env->exception.vaddress);
3694 new_mode = ARM_CPU_MODE_ABT;
3696 mask = CPSR_A | CPSR_I;
3699 case EXCP_DATA_ABORT:
3700 env->cp15.esr_el[1] = env->exception.fsr;
3701 env->cp15.far_el[1] = deposit64(env->cp15.far_el[1], 0, 32,
3702 env->exception.vaddress);
3703 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
3704 (uint32_t)env->cp15.esr_el[1],
3705 (uint32_t)env->exception.vaddress);
3706 new_mode = ARM_CPU_MODE_ABT;
3708 mask = CPSR_A | CPSR_I;
3712 new_mode = ARM_CPU_MODE_IRQ;
3714 /* Disable IRQ and imprecise data aborts. */
3715 mask = CPSR_A | CPSR_I;
3719 new_mode = ARM_CPU_MODE_FIQ;
3721 /* Disable FIQ, IRQ and imprecise data aborts. */
3722 mask = CPSR_A | CPSR_I | CPSR_F;
3726 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
3727 return; /* Never happens. Keep compiler happy. */
3730 if (env->cp15.c1_sys & SCTLR_V) {
3731 /* when enabled, base address cannot be remapped. */
3734 /* ARM v7 architectures provide a vector base address register to remap
3735 * the interrupt vector table.
3736 * This register is only followed in non-monitor mode, and has a secure
3737 * and un-secure copy. Since the cpu is always in a un-secure operation
3738 * and is never in monitor mode this feature is always active.
3739 * Note: only bits 31:5 are valid.
3741 addr += env->cp15.vbar_el[1];
3743 switch_mode (env, new_mode);
3744 /* For exceptions taken to AArch32 we must clear the SS bit in both
3745 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
3747 env->uncached_cpsr &= ~PSTATE_SS;
3748 env->spsr = cpsr_read(env);
3749 /* Clear IT bits. */
3750 env->condexec_bits = 0;
3751 /* Switch to the new mode, and to the correct instruction set. */
3752 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
3754 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
3755 * and we should just guard the thumb mode on V4 */
3756 if (arm_feature(env, ARM_FEATURE_V4T)) {
3757 env->thumb = (env->cp15.c1_sys & SCTLR_TE) != 0;
3759 env->regs[14] = env->regs[15] + offset;
3760 env->regs[15] = addr;
3761 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
3764 /* Check section/page access permissions.
3765 Returns the page protection flags, or zero if the access is not
3767 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
3768 int access_type, int is_user)
3772 if (domain_prot == 3) {
3773 return PAGE_READ | PAGE_WRITE;
3776 if (access_type == 1)
3779 prot_ro = PAGE_READ;
3783 if (arm_feature(env, ARM_FEATURE_V7)) {
3786 if (access_type == 1)
3788 switch (env->cp15.c1_sys & (SCTLR_S | SCTLR_R)) {
3790 return is_user ? 0 : PAGE_READ;
3797 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
3802 return PAGE_READ | PAGE_WRITE;
3804 return PAGE_READ | PAGE_WRITE;
3805 case 4: /* Reserved. */
3808 return is_user ? 0 : prot_ro;
3812 if (!arm_feature (env, ARM_FEATURE_V6K))
3820 static bool get_level1_table_address(CPUARMState *env, uint32_t *table,
3823 if (address & env->cp15.c2_mask) {
3824 if ((env->cp15.c2_control & TTBCR_PD1)) {
3825 /* Translation table walk disabled for TTBR1 */
3828 *table = env->cp15.ttbr1_el1 & 0xffffc000;
3830 if ((env->cp15.c2_control & TTBCR_PD0)) {
3831 /* Translation table walk disabled for TTBR0 */
3834 *table = env->cp15.ttbr0_el1 & env->cp15.c2_base_mask;
3836 *table |= (address >> 18) & 0x3ffc;
3840 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
3841 int is_user, hwaddr *phys_ptr,
3842 int *prot, target_ulong *page_size)
3844 CPUState *cs = CPU(arm_env_get_cpu(env));
3854 /* Pagetable walk. */
3855 /* Lookup l1 descriptor. */
3856 if (!get_level1_table_address(env, &table, address)) {
3857 /* Section translation fault if page walk is disabled by PD0 or PD1 */
3861 desc = ldl_phys(cs->as, table);
3863 domain = (desc >> 5) & 0x0f;
3864 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
3866 /* Section translation fault. */
3870 if (domain_prot == 0 || domain_prot == 2) {
3872 code = 9; /* Section domain fault. */
3874 code = 11; /* Page domain fault. */
3879 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
3880 ap = (desc >> 10) & 3;
3882 *page_size = 1024 * 1024;
3884 /* Lookup l2 entry. */
3886 /* Coarse pagetable. */
3887 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
3889 /* Fine pagetable. */
3890 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
3892 desc = ldl_phys(cs->as, table);
3894 case 0: /* Page translation fault. */
3897 case 1: /* 64k page. */
3898 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
3899 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
3900 *page_size = 0x10000;
3902 case 2: /* 4k page. */
3903 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
3904 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
3905 *page_size = 0x1000;
3907 case 3: /* 1k page. */
3909 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
3910 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
3912 /* Page translation fault. */
3917 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
3919 ap = (desc >> 4) & 3;
3923 /* Never happens, but compiler isn't smart enough to tell. */
3928 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
3930 /* Access permission fault. */
3934 *phys_ptr = phys_addr;
3937 return code | (domain << 4);
3940 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
3941 int is_user, hwaddr *phys_ptr,
3942 int *prot, target_ulong *page_size)
3944 CPUState *cs = CPU(arm_env_get_cpu(env));
3956 /* Pagetable walk. */
3957 /* Lookup l1 descriptor. */
3958 if (!get_level1_table_address(env, &table, address)) {
3959 /* Section translation fault if page walk is disabled by PD0 or PD1 */
3963 desc = ldl_phys(cs->as, table);
3965 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
3966 /* Section translation fault, or attempt to use the encoding
3967 * which is Reserved on implementations without PXN.
3972 if ((type == 1) || !(desc & (1 << 18))) {
3973 /* Page or Section. */
3974 domain = (desc >> 5) & 0x0f;
3976 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
3977 if (domain_prot == 0 || domain_prot == 2) {
3979 code = 9; /* Section domain fault. */
3981 code = 11; /* Page domain fault. */
3986 if (desc & (1 << 18)) {
3988 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
3989 *page_size = 0x1000000;
3992 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
3993 *page_size = 0x100000;
3995 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
3996 xn = desc & (1 << 4);
4000 if (arm_feature(env, ARM_FEATURE_PXN)) {
4001 pxn = (desc >> 2) & 1;
4003 /* Lookup l2 entry. */
4004 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
4005 desc = ldl_phys(cs->as, table);
4006 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
4008 case 0: /* Page translation fault. */
4011 case 1: /* 64k page. */
4012 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
4013 xn = desc & (1 << 15);
4014 *page_size = 0x10000;
4016 case 2: case 3: /* 4k page. */
4017 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
4019 *page_size = 0x1000;
4022 /* Never happens, but compiler isn't smart enough to tell. */
4027 if (domain_prot == 3) {
4028 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
4030 if (pxn && !is_user) {
4033 if (xn && access_type == 2)
4036 /* The simplified model uses AP[0] as an access control bit. */
4037 if ((env->cp15.c1_sys & SCTLR_AFE) && (ap & 1) == 0) {
4038 /* Access flag fault. */
4039 code = (code == 15) ? 6 : 3;
4042 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
4044 /* Access permission fault. */
4051 *phys_ptr = phys_addr;
4054 return code | (domain << 4);
4057 /* Fault type for long-descriptor MMU fault reporting; this corresponds
4058 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
4061 translation_fault = 1,
4063 permission_fault = 3,
4066 static int get_phys_addr_lpae(CPUARMState *env, target_ulong address,
4067 int access_type, int is_user,
4068 hwaddr *phys_ptr, int *prot,
4069 target_ulong *page_size_ptr)
4071 CPUState *cs = CPU(arm_env_get_cpu(env));
4072 /* Read an LPAE long-descriptor translation table. */
4073 MMUFaultType fault_type = translation_fault;
4080 hwaddr descaddr, descmask;
4081 uint32_t tableattrs;
4082 target_ulong page_size;
4084 int32_t granule_sz = 9;
4085 int32_t va_size = 32;
4088 if (arm_el_is_aa64(env, 1)) {
4090 if (extract64(address, 55, 1))
4091 tbi = extract64(env->cp15.c2_control, 38, 1);
4093 tbi = extract64(env->cp15.c2_control, 37, 1);
4097 /* Determine whether this address is in the region controlled by
4098 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
4099 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
4100 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
4102 uint32_t t0sz = extract32(env->cp15.c2_control, 0, 6);
4103 if (arm_el_is_aa64(env, 1)) {
4104 t0sz = MIN(t0sz, 39);
4105 t0sz = MAX(t0sz, 16);
4107 uint32_t t1sz = extract32(env->cp15.c2_control, 16, 6);
4108 if (arm_el_is_aa64(env, 1)) {
4109 t1sz = MIN(t1sz, 39);
4110 t1sz = MAX(t1sz, 16);
4112 if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
4113 /* there is a ttbr0 region and we are in it (high bits all zero) */
4115 } else if (t1sz && !extract64(~address, va_size - t1sz, t1sz - tbi)) {
4116 /* there is a ttbr1 region and we are in it (high bits all one) */
4119 /* ttbr0 region is "everything not in the ttbr1 region" */
4122 /* ttbr1 region is "everything not in the ttbr0 region" */
4125 /* in the gap between the two regions, this is a Translation fault */
4126 fault_type = translation_fault;
4130 /* Note that QEMU ignores shareability and cacheability attributes,
4131 * so we don't need to do anything with the SH, ORGN, IRGN fields
4132 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
4133 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
4134 * implement any ASID-like capability so we can ignore it (instead
4135 * we will always flush the TLB any time the ASID is changed).
4137 if (ttbr_select == 0) {
4138 ttbr = env->cp15.ttbr0_el1;
4139 epd = extract32(env->cp15.c2_control, 7, 1);
4142 tg = extract32(env->cp15.c2_control, 14, 2);
4143 if (tg == 1) { /* 64KB pages */
4146 if (tg == 2) { /* 16KB pages */
4150 ttbr = env->cp15.ttbr1_el1;
4151 epd = extract32(env->cp15.c2_control, 23, 1);
4154 tg = extract32(env->cp15.c2_control, 30, 2);
4155 if (tg == 3) { /* 64KB pages */
4158 if (tg == 1) { /* 16KB pages */
4164 /* Translation table walk disabled => Translation fault on TLB miss */
4168 /* The starting level depends on the virtual address size which can be
4169 * up to 48-bits and the translation granule size.
4171 if ((va_size - tsz) > (granule_sz * 4 + 3)) {
4173 } else if ((va_size - tsz) > (granule_sz * 3 + 3)) {
4179 /* Clear the vaddr bits which aren't part of the within-region address,
4180 * so that we don't have to special case things when calculating the
4181 * first descriptor address.
4184 address &= (1ULL << (va_size - tsz)) - 1;
4187 descmask = (1ULL << (granule_sz + 3)) - 1;
4189 /* Now we can extract the actual base address from the TTBR */
4190 descaddr = extract64(ttbr, 0, 48);
4191 descaddr &= ~((1ULL << (va_size - tsz - (granule_sz * (4 - level)))) - 1);
4195 uint64_t descriptor;
4197 descaddr |= (address >> (granule_sz * (4 - level))) & descmask;
4199 descriptor = ldq_phys(cs->as, descaddr);
4200 if (!(descriptor & 1) ||
4201 (!(descriptor & 2) && (level == 3))) {
4202 /* Invalid, or the Reserved level 3 encoding */
4205 descaddr = descriptor & 0xfffffff000ULL;
4207 if ((descriptor & 2) && (level < 3)) {
4208 /* Table entry. The top five bits are attributes which may
4209 * propagate down through lower levels of the table (and
4210 * which are all arranged so that 0 means "no effect", so
4211 * we can gather them up by ORing in the bits at each level).
4213 tableattrs |= extract64(descriptor, 59, 5);
4217 /* Block entry at level 1 or 2, or page entry at level 3.
4218 * These are basically the same thing, although the number
4219 * of bits we pull in from the vaddr varies.
4221 page_size = (1ULL << ((granule_sz * (4 - level)) + 3));
4222 descaddr |= (address & (page_size - 1));
4223 /* Extract attributes from the descriptor and merge with table attrs */
4224 attrs = extract64(descriptor, 2, 10)
4225 | (extract64(descriptor, 52, 12) << 10);
4226 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
4227 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
4228 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
4229 * means "force PL1 access only", which means forcing AP[1] to 0.
4231 if (extract32(tableattrs, 2, 1)) {
4234 /* Since we're always in the Non-secure state, NSTable is ignored. */
4237 /* Here descaddr is the final physical address, and attributes
4240 fault_type = access_fault;
4241 if ((attrs & (1 << 8)) == 0) {
4245 fault_type = permission_fault;
4246 if (is_user && !(attrs & (1 << 4))) {
4247 /* Unprivileged access not enabled */
4250 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
4251 if ((arm_feature(env, ARM_FEATURE_V8) && is_user && (attrs & (1 << 12))) ||
4252 (!arm_feature(env, ARM_FEATURE_V8) && (attrs & (1 << 12))) ||
4253 (!is_user && (attrs & (1 << 11)))) {
4254 /* XN/UXN or PXN. Since we only implement EL0/EL1 we unconditionally
4255 * treat XN/UXN as UXN for v8.
4257 if (access_type == 2) {
4260 *prot &= ~PAGE_EXEC;
4262 if (attrs & (1 << 5)) {
4263 /* Write access forbidden */
4264 if (access_type == 1) {
4267 *prot &= ~PAGE_WRITE;
4270 *phys_ptr = descaddr;
4271 *page_size_ptr = page_size;
4275 /* Long-descriptor format IFSR/DFSR value */
4276 return (1 << 9) | (fault_type << 2) | level;
4279 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address,
4280 int access_type, int is_user,
4281 hwaddr *phys_ptr, int *prot)
4287 *phys_ptr = address;
4288 for (n = 7; n >= 0; n--) {
4289 base = env->cp15.c6_region[n];
4290 if ((base & 1) == 0)
4292 mask = 1 << ((base >> 1) & 0x1f);
4293 /* Keep this shift separate from the above to avoid an
4294 (undefined) << 32. */
4295 mask = (mask << 1) - 1;
4296 if (((base ^ address) & ~mask) == 0)
4302 if (access_type == 2) {
4303 mask = env->cp15.pmsav5_insn_ap;
4305 mask = env->cp15.pmsav5_data_ap;
4307 mask = (mask >> (n * 4)) & 0xf;
4314 *prot = PAGE_READ | PAGE_WRITE;
4319 *prot |= PAGE_WRITE;
4322 *prot = PAGE_READ | PAGE_WRITE;
4333 /* Bad permission. */
4340 /* get_phys_addr - get the physical address for this virtual address
4342 * Find the physical address corresponding to the given virtual address,
4343 * by doing a translation table walk on MMU based systems or using the
4344 * MPU state on MPU based systems.
4346 * Returns 0 if the translation was successful. Otherwise, phys_ptr,
4347 * prot and page_size are not filled in, and the return value provides
4348 * information on why the translation aborted, in the format of a
4349 * DFSR/IFSR fault register, with the following caveats:
4350 * * we honour the short vs long DFSR format differences.
4351 * * the WnR bit is never set (the caller must do this).
4352 * * for MPU based systems we don't bother to return a full FSR format
4356 * @address: virtual address to get physical address for
4357 * @access_type: 0 for read, 1 for write, 2 for execute
4358 * @is_user: 0 for privileged access, 1 for user
4359 * @phys_ptr: set to the physical address corresponding to the virtual address
4360 * @prot: set to the permissions for the page containing phys_ptr
4361 * @page_size: set to the size of the page containing phys_ptr
4363 static inline int get_phys_addr(CPUARMState *env, target_ulong address,
4364 int access_type, int is_user,
4365 hwaddr *phys_ptr, int *prot,
4366 target_ulong *page_size)
4368 /* Fast Context Switch Extension. */
4369 if (address < 0x02000000)
4370 address += env->cp15.c13_fcse;
4372 if ((env->cp15.c1_sys & SCTLR_M) == 0) {
4373 /* MMU/MPU disabled. */
4374 *phys_ptr = address;
4375 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
4376 *page_size = TARGET_PAGE_SIZE;
4378 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
4379 *page_size = TARGET_PAGE_SIZE;
4380 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
4382 } else if (extended_addresses_enabled(env)) {
4383 return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr,
4385 } else if (env->cp15.c1_sys & SCTLR_XP) {
4386 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
4389 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
4394 int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
4395 int access_type, int mmu_idx)
4397 ARMCPU *cpu = ARM_CPU(cs);
4398 CPUARMState *env = &cpu->env;
4400 target_ulong page_size;
4404 bool same_el = (arm_current_pl(env) != 0);
4406 is_user = mmu_idx == MMU_USER_IDX;
4407 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
4410 /* Map a single [sub]page. */
4411 phys_addr &= TARGET_PAGE_MASK;
4412 address &= TARGET_PAGE_MASK;
4413 tlb_set_page(cs, address, phys_addr, prot, mmu_idx, page_size);
4417 /* AArch64 syndrome does not have an LPAE bit */
4418 syn = ret & ~(1 << 9);
4420 /* For insn and data aborts we assume there is no instruction syndrome
4421 * information; this is always true for exceptions reported to EL1.
4423 if (access_type == 2) {
4424 syn = syn_insn_abort(same_el, 0, 0, syn);
4425 cs->exception_index = EXCP_PREFETCH_ABORT;
4427 syn = syn_data_abort(same_el, 0, 0, 0, access_type == 1, syn);
4428 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6)) {
4431 cs->exception_index = EXCP_DATA_ABORT;
4434 env->exception.syndrome = syn;
4435 env->exception.vaddress = address;
4436 env->exception.fsr = ret;
4440 hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
4442 ARMCPU *cpu = ARM_CPU(cs);
4444 target_ulong page_size;
4448 ret = get_phys_addr(&cpu->env, addr, 0, 0, &phys_addr, &prot, &page_size);
4457 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
4459 if ((env->uncached_cpsr & CPSR_M) == mode) {
4460 env->regs[13] = val;
4462 env->banked_r13[bank_number(mode)] = val;
4466 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
4468 if ((env->uncached_cpsr & CPSR_M) == mode) {
4469 return env->regs[13];
4471 return env->banked_r13[bank_number(mode)];
4475 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
4477 ARMCPU *cpu = arm_env_get_cpu(env);
4481 return xpsr_read(env) & 0xf8000000;
4483 return xpsr_read(env) & 0xf80001ff;
4485 return xpsr_read(env) & 0xff00fc00;
4487 return xpsr_read(env) & 0xff00fdff;
4489 return xpsr_read(env) & 0x000001ff;
4491 return xpsr_read(env) & 0x0700fc00;
4493 return xpsr_read(env) & 0x0700edff;
4495 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
4497 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
4498 case 16: /* PRIMASK */
4499 return (env->daif & PSTATE_I) != 0;
4500 case 17: /* BASEPRI */
4501 case 18: /* BASEPRI_MAX */
4502 return env->v7m.basepri;
4503 case 19: /* FAULTMASK */
4504 return (env->daif & PSTATE_F) != 0;
4505 case 20: /* CONTROL */
4506 return env->v7m.control;
4508 /* ??? For debugging only. */
4509 cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
4514 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
4516 ARMCPU *cpu = arm_env_get_cpu(env);
4520 xpsr_write(env, val, 0xf8000000);
4523 xpsr_write(env, val, 0xf8000000);
4526 xpsr_write(env, val, 0xfe00fc00);
4529 xpsr_write(env, val, 0xfe00fc00);
4532 /* IPSR bits are readonly. */
4535 xpsr_write(env, val, 0x0600fc00);
4538 xpsr_write(env, val, 0x0600fc00);
4541 if (env->v7m.current_sp)
4542 env->v7m.other_sp = val;
4544 env->regs[13] = val;
4547 if (env->v7m.current_sp)
4548 env->regs[13] = val;
4550 env->v7m.other_sp = val;
4552 case 16: /* PRIMASK */
4554 env->daif |= PSTATE_I;
4556 env->daif &= ~PSTATE_I;
4559 case 17: /* BASEPRI */
4560 env->v7m.basepri = val & 0xff;
4562 case 18: /* BASEPRI_MAX */
4564 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
4565 env->v7m.basepri = val;
4567 case 19: /* FAULTMASK */
4569 env->daif |= PSTATE_F;
4571 env->daif &= ~PSTATE_F;
4574 case 20: /* CONTROL */
4575 env->v7m.control = val & 3;
4576 switch_v7m_sp(env, (val & 2) != 0);
4579 /* ??? For debugging only. */
4580 cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
4587 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
4589 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
4590 * Note that we do not implement the (architecturally mandated)
4591 * alignment fault for attempts to use this on Device memory
4592 * (which matches the usual QEMU behaviour of not implementing either
4593 * alignment faults or any memory attribute handling).
4596 ARMCPU *cpu = arm_env_get_cpu(env);
4597 uint64_t blocklen = 4 << cpu->dcz_blocksize;
4598 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
4600 #ifndef CONFIG_USER_ONLY
4602 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
4603 * the block size so we might have to do more than one TLB lookup.
4604 * We know that in fact for any v8 CPU the page size is at least 4K
4605 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
4606 * 1K as an artefact of legacy v5 subpage support being present in the
4607 * same QEMU executable.
4609 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
4610 void *hostaddr[maxidx];
4613 for (try = 0; try < 2; try++) {
4615 for (i = 0; i < maxidx; i++) {
4616 hostaddr[i] = tlb_vaddr_to_host(env,
4617 vaddr + TARGET_PAGE_SIZE * i,
4618 1, cpu_mmu_index(env));
4624 /* If it's all in the TLB it's fair game for just writing to;
4625 * we know we don't need to update dirty status, etc.
4627 for (i = 0; i < maxidx - 1; i++) {
4628 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
4630 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
4633 /* OK, try a store and see if we can populate the tlb. This
4634 * might cause an exception if the memory isn't writable,
4635 * in which case we will longjmp out of here. We must for
4636 * this purpose use the actual register value passed to us
4637 * so that we get the fault address right.
4639 helper_ret_stb_mmu(env, vaddr_in, 0, cpu_mmu_index(env), GETRA());
4640 /* Now we can populate the other TLB entries, if any */
4641 for (i = 0; i < maxidx; i++) {
4642 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
4643 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
4644 helper_ret_stb_mmu(env, va, 0, cpu_mmu_index(env), GETRA());
4649 /* Slow path (probably attempt to do this to an I/O device or
4650 * similar, or clearing of a block of code we have translations
4651 * cached for). Just do a series of byte writes as the architecture
4652 * demands. It's not worth trying to use a cpu_physical_memory_map(),
4653 * memset(), unmap() sequence here because:
4654 * + we'd need to account for the blocksize being larger than a page
4655 * + the direct-RAM access case is almost always going to be dealt
4656 * with in the fastpath code above, so there's no speed benefit
4657 * + we would have to deal with the map returning NULL because the
4658 * bounce buffer was in use
4660 for (i = 0; i < blocklen; i++) {
4661 helper_ret_stb_mmu(env, vaddr + i, 0, cpu_mmu_index(env), GETRA());
4665 memset(g2h(vaddr), 0, blocklen);
4669 /* Note that signed overflow is undefined in C. The following routines are
4670 careful to use unsigned types where modulo arithmetic is required.
4671 Failure to do so _will_ break on newer gcc. */
4673 /* Signed saturating arithmetic. */
4675 /* Perform 16-bit signed saturating addition. */
4676 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
4681 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
4690 /* Perform 8-bit signed saturating addition. */
4691 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
4696 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
4705 /* Perform 16-bit signed saturating subtraction. */
4706 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
4711 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
4720 /* Perform 8-bit signed saturating subtraction. */
4721 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
4726 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
4735 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
4736 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
4737 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
4738 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
4741 #include "op_addsub.h"
4743 /* Unsigned saturating arithmetic. */
4744 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
4753 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
4761 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
4770 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
4778 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
4779 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
4780 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
4781 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
4784 #include "op_addsub.h"
4786 /* Signed modulo arithmetic. */
4787 #define SARITH16(a, b, n, op) do { \
4789 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
4790 RESULT(sum, n, 16); \
4792 ge |= 3 << (n * 2); \
4795 #define SARITH8(a, b, n, op) do { \
4797 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
4798 RESULT(sum, n, 8); \
4804 #define ADD16(a, b, n) SARITH16(a, b, n, +)
4805 #define SUB16(a, b, n) SARITH16(a, b, n, -)
4806 #define ADD8(a, b, n) SARITH8(a, b, n, +)
4807 #define SUB8(a, b, n) SARITH8(a, b, n, -)
4811 #include "op_addsub.h"
4813 /* Unsigned modulo arithmetic. */
4814 #define ADD16(a, b, n) do { \
4816 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
4817 RESULT(sum, n, 16); \
4818 if ((sum >> 16) == 1) \
4819 ge |= 3 << (n * 2); \
4822 #define ADD8(a, b, n) do { \
4824 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
4825 RESULT(sum, n, 8); \
4826 if ((sum >> 8) == 1) \
4830 #define SUB16(a, b, n) do { \
4832 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
4833 RESULT(sum, n, 16); \
4834 if ((sum >> 16) == 0) \
4835 ge |= 3 << (n * 2); \
4838 #define SUB8(a, b, n) do { \
4840 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
4841 RESULT(sum, n, 8); \
4842 if ((sum >> 8) == 0) \
4849 #include "op_addsub.h"
4851 /* Halved signed arithmetic. */
4852 #define ADD16(a, b, n) \
4853 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
4854 #define SUB16(a, b, n) \
4855 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
4856 #define ADD8(a, b, n) \
4857 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
4858 #define SUB8(a, b, n) \
4859 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
4862 #include "op_addsub.h"
4864 /* Halved unsigned arithmetic. */
4865 #define ADD16(a, b, n) \
4866 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
4867 #define SUB16(a, b, n) \
4868 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
4869 #define ADD8(a, b, n) \
4870 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
4871 #define SUB8(a, b, n) \
4872 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
4875 #include "op_addsub.h"
4877 static inline uint8_t do_usad(uint8_t a, uint8_t b)
4885 /* Unsigned sum of absolute byte differences. */
4886 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
4889 sum = do_usad(a, b);
4890 sum += do_usad(a >> 8, b >> 8);
4891 sum += do_usad(a >> 16, b >>16);
4892 sum += do_usad(a >> 24, b >> 24);
4896 /* For ARMv6 SEL instruction. */
4897 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
4910 return (a & mask) | (b & ~mask);
4913 /* VFP support. We follow the convention used for VFP instructions:
4914 Single precision routines have a "s" suffix, double precision a
4917 /* Convert host exception flags to vfp form. */
4918 static inline int vfp_exceptbits_from_host(int host_bits)
4920 int target_bits = 0;
4922 if (host_bits & float_flag_invalid)
4924 if (host_bits & float_flag_divbyzero)
4926 if (host_bits & float_flag_overflow)
4928 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
4930 if (host_bits & float_flag_inexact)
4931 target_bits |= 0x10;
4932 if (host_bits & float_flag_input_denormal)
4933 target_bits |= 0x80;
4937 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
4942 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
4943 | (env->vfp.vec_len << 16)
4944 | (env->vfp.vec_stride << 20);
4945 i = get_float_exception_flags(&env->vfp.fp_status);
4946 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
4947 fpscr |= vfp_exceptbits_from_host(i);
4951 uint32_t vfp_get_fpscr(CPUARMState *env)
4953 return HELPER(vfp_get_fpscr)(env);
4956 /* Convert vfp exception flags to target form. */
4957 static inline int vfp_exceptbits_to_host(int target_bits)
4961 if (target_bits & 1)
4962 host_bits |= float_flag_invalid;
4963 if (target_bits & 2)
4964 host_bits |= float_flag_divbyzero;
4965 if (target_bits & 4)
4966 host_bits |= float_flag_overflow;
4967 if (target_bits & 8)
4968 host_bits |= float_flag_underflow;
4969 if (target_bits & 0x10)
4970 host_bits |= float_flag_inexact;
4971 if (target_bits & 0x80)
4972 host_bits |= float_flag_input_denormal;
4976 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
4981 changed = env->vfp.xregs[ARM_VFP_FPSCR];
4982 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
4983 env->vfp.vec_len = (val >> 16) & 7;
4984 env->vfp.vec_stride = (val >> 20) & 3;
4987 if (changed & (3 << 22)) {
4988 i = (val >> 22) & 3;
4990 case FPROUNDING_TIEEVEN:
4991 i = float_round_nearest_even;
4993 case FPROUNDING_POSINF:
4996 case FPROUNDING_NEGINF:
4997 i = float_round_down;
4999 case FPROUNDING_ZERO:
5000 i = float_round_to_zero;
5003 set_float_rounding_mode(i, &env->vfp.fp_status);
5005 if (changed & (1 << 24)) {
5006 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
5007 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
5009 if (changed & (1 << 25))
5010 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
5012 i = vfp_exceptbits_to_host(val);
5013 set_float_exception_flags(i, &env->vfp.fp_status);
5014 set_float_exception_flags(0, &env->vfp.standard_fp_status);
5017 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
5019 HELPER(vfp_set_fpscr)(env, val);
5022 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
5024 #define VFP_BINOP(name) \
5025 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
5027 float_status *fpst = fpstp; \
5028 return float32_ ## name(a, b, fpst); \
5030 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
5032 float_status *fpst = fpstp; \
5033 return float64_ ## name(a, b, fpst); \
5045 float32 VFP_HELPER(neg, s)(float32 a)
5047 return float32_chs(a);
5050 float64 VFP_HELPER(neg, d)(float64 a)
5052 return float64_chs(a);
5055 float32 VFP_HELPER(abs, s)(float32 a)
5057 return float32_abs(a);
5060 float64 VFP_HELPER(abs, d)(float64 a)
5062 return float64_abs(a);
5065 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
5067 return float32_sqrt(a, &env->vfp.fp_status);
5070 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
5072 return float64_sqrt(a, &env->vfp.fp_status);
5075 /* XXX: check quiet/signaling case */
5076 #define DO_VFP_cmp(p, type) \
5077 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
5080 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
5081 case 0: flags = 0x6; break; \
5082 case -1: flags = 0x8; break; \
5083 case 1: flags = 0x2; break; \
5084 default: case 2: flags = 0x3; break; \
5086 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
5087 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
5089 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
5092 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
5093 case 0: flags = 0x6; break; \
5094 case -1: flags = 0x8; break; \
5095 case 1: flags = 0x2; break; \
5096 default: case 2: flags = 0x3; break; \
5098 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
5099 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
5101 DO_VFP_cmp(s, float32)
5102 DO_VFP_cmp(d, float64)
5105 /* Integer to float and float to integer conversions */
5107 #define CONV_ITOF(name, fsz, sign) \
5108 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
5110 float_status *fpst = fpstp; \
5111 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
5114 #define CONV_FTOI(name, fsz, sign, round) \
5115 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
5117 float_status *fpst = fpstp; \
5118 if (float##fsz##_is_any_nan(x)) { \
5119 float_raise(float_flag_invalid, fpst); \
5122 return float##fsz##_to_##sign##int32##round(x, fpst); \
5125 #define FLOAT_CONVS(name, p, fsz, sign) \
5126 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
5127 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
5128 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
5130 FLOAT_CONVS(si, s, 32, )
5131 FLOAT_CONVS(si, d, 64, )
5132 FLOAT_CONVS(ui, s, 32, u)
5133 FLOAT_CONVS(ui, d, 64, u)
5139 /* floating point conversion */
5140 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
5142 float64 r = float32_to_float64(x, &env->vfp.fp_status);
5143 /* ARM requires that S<->D conversion of any kind of NaN generates
5144 * a quiet NaN by forcing the most significant frac bit to 1.
5146 return float64_maybe_silence_nan(r);
5149 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
5151 float32 r = float64_to_float32(x, &env->vfp.fp_status);
5152 /* ARM requires that S<->D conversion of any kind of NaN generates
5153 * a quiet NaN by forcing the most significant frac bit to 1.
5155 return float32_maybe_silence_nan(r);
5158 /* VFP3 fixed point conversion. */
5159 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
5160 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
5163 float_status *fpst = fpstp; \
5165 tmp = itype##_to_##float##fsz(x, fpst); \
5166 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
5169 /* Notice that we want only input-denormal exception flags from the
5170 * scalbn operation: the other possible flags (overflow+inexact if
5171 * we overflow to infinity, output-denormal) aren't correct for the
5172 * complete scale-and-convert operation.
5174 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
5175 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
5179 float_status *fpst = fpstp; \
5180 int old_exc_flags = get_float_exception_flags(fpst); \
5182 if (float##fsz##_is_any_nan(x)) { \
5183 float_raise(float_flag_invalid, fpst); \
5186 tmp = float##fsz##_scalbn(x, shift, fpst); \
5187 old_exc_flags |= get_float_exception_flags(fpst) \
5188 & float_flag_input_denormal; \
5189 set_float_exception_flags(old_exc_flags, fpst); \
5190 return float##fsz##_to_##itype##round(tmp, fpst); \
5193 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \
5194 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
5195 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
5196 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
5198 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
5199 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
5200 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
5202 VFP_CONV_FIX(sh, d, 64, 64, int16)
5203 VFP_CONV_FIX(sl, d, 64, 64, int32)
5204 VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
5205 VFP_CONV_FIX(uh, d, 64, 64, uint16)
5206 VFP_CONV_FIX(ul, d, 64, 64, uint32)
5207 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
5208 VFP_CONV_FIX(sh, s, 32, 32, int16)
5209 VFP_CONV_FIX(sl, s, 32, 32, int32)
5210 VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
5211 VFP_CONV_FIX(uh, s, 32, 32, uint16)
5212 VFP_CONV_FIX(ul, s, 32, 32, uint32)
5213 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
5215 #undef VFP_CONV_FIX_FLOAT
5216 #undef VFP_CONV_FLOAT_FIX_ROUND
5218 /* Set the current fp rounding mode and return the old one.
5219 * The argument is a softfloat float_round_ value.
5221 uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
5223 float_status *fp_status = &env->vfp.fp_status;
5225 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
5226 set_float_rounding_mode(rmode, fp_status);
5231 /* Set the current fp rounding mode in the standard fp status and return
5232 * the old one. This is for NEON instructions that need to change the
5233 * rounding mode but wish to use the standard FPSCR values for everything
5234 * else. Always set the rounding mode back to the correct value after
5236 * The argument is a softfloat float_round_ value.
5238 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
5240 float_status *fp_status = &env->vfp.standard_fp_status;
5242 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
5243 set_float_rounding_mode(rmode, fp_status);
5248 /* Half precision conversions. */
5249 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
5251 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
5252 float32 r = float16_to_float32(make_float16(a), ieee, s);
5254 return float32_maybe_silence_nan(r);
5259 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
5261 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
5262 float16 r = float32_to_float16(a, ieee, s);
5264 r = float16_maybe_silence_nan(r);
5266 return float16_val(r);
5269 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
5271 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
5274 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
5276 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
5279 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
5281 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
5284 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
5286 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
5289 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
5291 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
5292 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
5294 return float64_maybe_silence_nan(r);
5299 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
5301 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
5302 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
5304 r = float16_maybe_silence_nan(r);
5306 return float16_val(r);
5309 #define float32_two make_float32(0x40000000)
5310 #define float32_three make_float32(0x40400000)
5311 #define float32_one_point_five make_float32(0x3fc00000)
5313 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
5315 float_status *s = &env->vfp.standard_fp_status;
5316 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
5317 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
5318 if (!(float32_is_zero(a) || float32_is_zero(b))) {
5319 float_raise(float_flag_input_denormal, s);
5323 return float32_sub(float32_two, float32_mul(a, b, s), s);
5326 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
5328 float_status *s = &env->vfp.standard_fp_status;
5330 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
5331 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
5332 if (!(float32_is_zero(a) || float32_is_zero(b))) {
5333 float_raise(float_flag_input_denormal, s);
5335 return float32_one_point_five;
5337 product = float32_mul(a, b, s);
5338 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
5343 /* Constants 256 and 512 are used in some helpers; we avoid relying on
5344 * int->float conversions at run-time. */
5345 #define float64_256 make_float64(0x4070000000000000LL)
5346 #define float64_512 make_float64(0x4080000000000000LL)
5347 #define float32_maxnorm make_float32(0x7f7fffff)
5348 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
5350 /* Reciprocal functions
5352 * The algorithm that must be used to calculate the estimate
5353 * is specified by the ARM ARM, see FPRecipEstimate()
5356 static float64 recip_estimate(float64 a, float_status *real_fp_status)
5358 /* These calculations mustn't set any fp exception flags,
5359 * so we use a local copy of the fp_status.
5361 float_status dummy_status = *real_fp_status;
5362 float_status *s = &dummy_status;
5363 /* q = (int)(a * 512.0) */
5364 float64 q = float64_mul(float64_512, a, s);
5365 int64_t q_int = float64_to_int64_round_to_zero(q, s);
5367 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
5368 q = int64_to_float64(q_int, s);
5369 q = float64_add(q, float64_half, s);
5370 q = float64_div(q, float64_512, s);
5371 q = float64_div(float64_one, q, s);
5373 /* s = (int)(256.0 * r + 0.5) */
5374 q = float64_mul(q, float64_256, s);
5375 q = float64_add(q, float64_half, s);
5376 q_int = float64_to_int64_round_to_zero(q, s);
5378 /* return (double)s / 256.0 */
5379 return float64_div(int64_to_float64(q_int, s), float64_256, s);
5382 /* Common wrapper to call recip_estimate */
5383 static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
5385 uint64_t val64 = float64_val(num);
5386 uint64_t frac = extract64(val64, 0, 52);
5387 int64_t exp = extract64(val64, 52, 11);
5389 float64 scaled, estimate;
5391 /* Generate the scaled number for the estimate function */
5393 if (extract64(frac, 51, 1) == 0) {
5395 frac = extract64(frac, 0, 50) << 2;
5397 frac = extract64(frac, 0, 51) << 1;
5401 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
5402 scaled = make_float64((0x3feULL << 52)
5403 | extract64(frac, 44, 8) << 44);
5405 estimate = recip_estimate(scaled, fpst);
5407 /* Build new result */
5408 val64 = float64_val(estimate);
5409 sbit = 0x8000000000000000ULL & val64;
5411 frac = extract64(val64, 0, 52);
5414 frac = 1ULL << 51 | extract64(frac, 1, 51);
5415 } else if (exp == -1) {
5416 frac = 1ULL << 50 | extract64(frac, 2, 50);
5420 return make_float64(sbit | (exp << 52) | frac);
5423 static bool round_to_inf(float_status *fpst, bool sign_bit)
5425 switch (fpst->float_rounding_mode) {
5426 case float_round_nearest_even: /* Round to Nearest */
5428 case float_round_up: /* Round to +Inf */
5430 case float_round_down: /* Round to -Inf */
5432 case float_round_to_zero: /* Round to Zero */
5436 g_assert_not_reached();
5439 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
5441 float_status *fpst = fpstp;
5442 float32 f32 = float32_squash_input_denormal(input, fpst);
5443 uint32_t f32_val = float32_val(f32);
5444 uint32_t f32_sbit = 0x80000000ULL & f32_val;
5445 int32_t f32_exp = extract32(f32_val, 23, 8);
5446 uint32_t f32_frac = extract32(f32_val, 0, 23);
5452 if (float32_is_any_nan(f32)) {
5454 if (float32_is_signaling_nan(f32)) {
5455 float_raise(float_flag_invalid, fpst);
5456 nan = float32_maybe_silence_nan(f32);
5458 if (fpst->default_nan_mode) {
5459 nan = float32_default_nan;
5462 } else if (float32_is_infinity(f32)) {
5463 return float32_set_sign(float32_zero, float32_is_neg(f32));
5464 } else if (float32_is_zero(f32)) {
5465 float_raise(float_flag_divbyzero, fpst);
5466 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5467 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
5468 /* Abs(value) < 2.0^-128 */
5469 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5470 if (round_to_inf(fpst, f32_sbit)) {
5471 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5473 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
5475 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
5476 float_raise(float_flag_underflow, fpst);
5477 return float32_set_sign(float32_zero, float32_is_neg(f32));
5481 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
5482 r64 = call_recip_estimate(f64, 253, fpst);
5483 r64_val = float64_val(r64);
5484 r64_exp = extract64(r64_val, 52, 11);
5485 r64_frac = extract64(r64_val, 0, 52);
5487 /* result = sign : result_exp<7:0> : fraction<51:29>; */
5488 return make_float32(f32_sbit |
5489 (r64_exp & 0xff) << 23 |
5490 extract64(r64_frac, 29, 24));
5493 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
5495 float_status *fpst = fpstp;
5496 float64 f64 = float64_squash_input_denormal(input, fpst);
5497 uint64_t f64_val = float64_val(f64);
5498 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
5499 int64_t f64_exp = extract64(f64_val, 52, 11);
5505 /* Deal with any special cases */
5506 if (float64_is_any_nan(f64)) {
5508 if (float64_is_signaling_nan(f64)) {
5509 float_raise(float_flag_invalid, fpst);
5510 nan = float64_maybe_silence_nan(f64);
5512 if (fpst->default_nan_mode) {
5513 nan = float64_default_nan;
5516 } else if (float64_is_infinity(f64)) {
5517 return float64_set_sign(float64_zero, float64_is_neg(f64));
5518 } else if (float64_is_zero(f64)) {
5519 float_raise(float_flag_divbyzero, fpst);
5520 return float64_set_sign(float64_infinity, float64_is_neg(f64));
5521 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
5522 /* Abs(value) < 2.0^-1024 */
5523 float_raise(float_flag_overflow | float_flag_inexact, fpst);
5524 if (round_to_inf(fpst, f64_sbit)) {
5525 return float64_set_sign(float64_infinity, float64_is_neg(f64));
5527 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
5529 } else if (f64_exp >= 1023 && fpst->flush_to_zero) {
5530 float_raise(float_flag_underflow, fpst);
5531 return float64_set_sign(float64_zero, float64_is_neg(f64));
5534 r64 = call_recip_estimate(f64, 2045, fpst);
5535 r64_val = float64_val(r64);
5536 r64_exp = extract64(r64_val, 52, 11);
5537 r64_frac = extract64(r64_val, 0, 52);
5539 /* result = sign : result_exp<10:0> : fraction<51:0> */
5540 return make_float64(f64_sbit |
5541 ((r64_exp & 0x7ff) << 52) |
5545 /* The algorithm that must be used to calculate the estimate
5546 * is specified by the ARM ARM.
5548 static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
5550 /* These calculations mustn't set any fp exception flags,
5551 * so we use a local copy of the fp_status.
5553 float_status dummy_status = *real_fp_status;
5554 float_status *s = &dummy_status;
5558 if (float64_lt(a, float64_half, s)) {
5559 /* range 0.25 <= a < 0.5 */
5561 /* a in units of 1/512 rounded down */
5562 /* q0 = (int)(a * 512.0); */
5563 q = float64_mul(float64_512, a, s);
5564 q_int = float64_to_int64_round_to_zero(q, s);
5566 /* reciprocal root r */
5567 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
5568 q = int64_to_float64(q_int, s);
5569 q = float64_add(q, float64_half, s);
5570 q = float64_div(q, float64_512, s);
5571 q = float64_sqrt(q, s);
5572 q = float64_div(float64_one, q, s);
5574 /* range 0.5 <= a < 1.0 */
5576 /* a in units of 1/256 rounded down */
5577 /* q1 = (int)(a * 256.0); */
5578 q = float64_mul(float64_256, a, s);
5579 int64_t q_int = float64_to_int64_round_to_zero(q, s);
5581 /* reciprocal root r */
5582 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
5583 q = int64_to_float64(q_int, s);
5584 q = float64_add(q, float64_half, s);
5585 q = float64_div(q, float64_256, s);
5586 q = float64_sqrt(q, s);
5587 q = float64_div(float64_one, q, s);
5589 /* r in units of 1/256 rounded to nearest */
5590 /* s = (int)(256.0 * r + 0.5); */
5592 q = float64_mul(q, float64_256,s );
5593 q = float64_add(q, float64_half, s);
5594 q_int = float64_to_int64_round_to_zero(q, s);
5596 /* return (double)s / 256.0;*/
5597 return float64_div(int64_to_float64(q_int, s), float64_256, s);
5600 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
5602 float_status *s = fpstp;
5603 float32 f32 = float32_squash_input_denormal(input, s);
5604 uint32_t val = float32_val(f32);
5605 uint32_t f32_sbit = 0x80000000 & val;
5606 int32_t f32_exp = extract32(val, 23, 8);
5607 uint32_t f32_frac = extract32(val, 0, 23);
5613 if (float32_is_any_nan(f32)) {
5615 if (float32_is_signaling_nan(f32)) {
5616 float_raise(float_flag_invalid, s);
5617 nan = float32_maybe_silence_nan(f32);
5619 if (s->default_nan_mode) {
5620 nan = float32_default_nan;
5623 } else if (float32_is_zero(f32)) {
5624 float_raise(float_flag_divbyzero, s);
5625 return float32_set_sign(float32_infinity, float32_is_neg(f32));
5626 } else if (float32_is_neg(f32)) {
5627 float_raise(float_flag_invalid, s);
5628 return float32_default_nan;
5629 } else if (float32_is_infinity(f32)) {
5630 return float32_zero;
5633 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
5634 * preserving the parity of the exponent. */
5636 f64_frac = ((uint64_t) f32_frac) << 29;
5638 while (extract64(f64_frac, 51, 1) == 0) {
5639 f64_frac = f64_frac << 1;
5640 f32_exp = f32_exp-1;
5642 f64_frac = extract64(f64_frac, 0, 51) << 1;
5645 if (extract64(f32_exp, 0, 1) == 0) {
5646 f64 = make_float64(((uint64_t) f32_sbit) << 32
5650 f64 = make_float64(((uint64_t) f32_sbit) << 32
5655 result_exp = (380 - f32_exp) / 2;
5657 f64 = recip_sqrt_estimate(f64, s);
5659 val64 = float64_val(f64);
5661 val = ((result_exp & 0xff) << 23)
5662 | ((val64 >> 29) & 0x7fffff);
5663 return make_float32(val);
5666 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
5668 float_status *s = fpstp;
5669 float64 f64 = float64_squash_input_denormal(input, s);
5670 uint64_t val = float64_val(f64);
5671 uint64_t f64_sbit = 0x8000000000000000ULL & val;
5672 int64_t f64_exp = extract64(val, 52, 11);
5673 uint64_t f64_frac = extract64(val, 0, 52);
5675 uint64_t result_frac;
5677 if (float64_is_any_nan(f64)) {
5679 if (float64_is_signaling_nan(f64)) {
5680 float_raise(float_flag_invalid, s);
5681 nan = float64_maybe_silence_nan(f64);
5683 if (s->default_nan_mode) {
5684 nan = float64_default_nan;
5687 } else if (float64_is_zero(f64)) {
5688 float_raise(float_flag_divbyzero, s);
5689 return float64_set_sign(float64_infinity, float64_is_neg(f64));
5690 } else if (float64_is_neg(f64)) {
5691 float_raise(float_flag_invalid, s);
5692 return float64_default_nan;
5693 } else if (float64_is_infinity(f64)) {
5694 return float64_zero;
5697 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
5698 * preserving the parity of the exponent. */
5701 while (extract64(f64_frac, 51, 1) == 0) {
5702 f64_frac = f64_frac << 1;
5703 f64_exp = f64_exp - 1;
5705 f64_frac = extract64(f64_frac, 0, 51) << 1;
5708 if (extract64(f64_exp, 0, 1) == 0) {
5709 f64 = make_float64(f64_sbit
5713 f64 = make_float64(f64_sbit
5718 result_exp = (3068 - f64_exp) / 2;
5720 f64 = recip_sqrt_estimate(f64, s);
5722 result_frac = extract64(float64_val(f64), 0, 52);
5724 return make_float64(f64_sbit |
5725 ((result_exp & 0x7ff) << 52) |
5729 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
5731 float_status *s = fpstp;
5734 if ((a & 0x80000000) == 0) {
5738 f64 = make_float64((0x3feULL << 52)
5739 | ((int64_t)(a & 0x7fffffff) << 21));
5741 f64 = recip_estimate(f64, s);
5743 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
5746 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
5748 float_status *fpst = fpstp;
5751 if ((a & 0xc0000000) == 0) {
5755 if (a & 0x80000000) {
5756 f64 = make_float64((0x3feULL << 52)
5757 | ((uint64_t)(a & 0x7fffffff) << 21));
5758 } else { /* bits 31-30 == '01' */
5759 f64 = make_float64((0x3fdULL << 52)
5760 | ((uint64_t)(a & 0x3fffffff) << 22));
5763 f64 = recip_sqrt_estimate(f64, fpst);
5765 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
5768 /* VFPv4 fused multiply-accumulate */
5769 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
5771 float_status *fpst = fpstp;
5772 return float32_muladd(a, b, c, 0, fpst);
5775 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
5777 float_status *fpst = fpstp;
5778 return float64_muladd(a, b, c, 0, fpst);
5781 /* ARMv8 round to integral */
5782 float32 HELPER(rints_exact)(float32 x, void *fp_status)
5784 return float32_round_to_int(x, fp_status);
5787 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
5789 return float64_round_to_int(x, fp_status);
5792 float32 HELPER(rints)(float32 x, void *fp_status)
5794 int old_flags = get_float_exception_flags(fp_status), new_flags;
5797 ret = float32_round_to_int(x, fp_status);
5799 /* Suppress any inexact exceptions the conversion produced */
5800 if (!(old_flags & float_flag_inexact)) {
5801 new_flags = get_float_exception_flags(fp_status);
5802 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
5808 float64 HELPER(rintd)(float64 x, void *fp_status)
5810 int old_flags = get_float_exception_flags(fp_status), new_flags;
5813 ret = float64_round_to_int(x, fp_status);
5815 new_flags = get_float_exception_flags(fp_status);
5817 /* Suppress any inexact exceptions the conversion produced */
5818 if (!(old_flags & float_flag_inexact)) {
5819 new_flags = get_float_exception_flags(fp_status);
5820 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
5826 /* Convert ARM rounding mode to softfloat */
5827 int arm_rmode_to_sf(int rmode)
5830 case FPROUNDING_TIEAWAY:
5831 rmode = float_round_ties_away;
5833 case FPROUNDING_ODD:
5834 /* FIXME: add support for TIEAWAY and ODD */
5835 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
5837 case FPROUNDING_TIEEVEN:
5839 rmode = float_round_nearest_even;
5841 case FPROUNDING_POSINF:
5842 rmode = float_round_up;
5844 case FPROUNDING_NEGINF:
5845 rmode = float_round_down;
5847 case FPROUNDING_ZERO:
5848 rmode = float_round_to_zero;
5855 * The upper bytes of val (above the number specified by 'bytes') must have
5856 * been zeroed out by the caller.
5858 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
5864 /* zlib crc32 converts the accumulator and output to one's complement. */
5865 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
5868 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
5874 /* Linux crc32c converts the output to one's complement. */
5875 return crc32c(acc, buf, bytes) ^ 0xffffffff;