4 #include "host-utils.h"
7 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
11 /* VFP data registers are always little-endian. */
12 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
14 stfq_le_p(buf, env->vfp.regs[reg]);
17 if (arm_feature(env, ARM_FEATURE_NEON)) {
18 /* Aliases for Q regs. */
21 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
22 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
26 switch (reg - nregs) {
27 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
28 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
29 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
34 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
38 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
40 env->vfp.regs[reg] = ldfq_le_p(buf);
43 if (arm_feature(env, ARM_FEATURE_NEON)) {
46 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
47 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
51 switch (reg - nregs) {
52 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
53 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
54 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
59 static int dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
62 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
66 static int fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
68 if (env->cp15.c13_fcse != value) {
69 /* Unlike real hardware the qemu TLB uses virtual addresses,
70 * not modified virtual addresses, so this causes a TLB flush.
73 env->cp15.c13_fcse = value;
77 static int contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
80 if (env->cp15.c13_context != value && !arm_feature(env, ARM_FEATURE_MPU)) {
81 /* For VMSA (when not using the LPAE long descriptor page table
82 * format) this register includes the ASID, so do a TLB flush.
83 * For PMSA it is purely a process ID and no action is needed.
87 env->cp15.c13_context = value;
91 static const ARMCPRegInfo cp_reginfo[] = {
92 /* DBGDIDR: just RAZ. In particular this means the "debug architecture
93 * version" bits will read as a reserved value, which should cause
94 * Linux to not try to use the debug hardware.
96 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
97 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
98 /* MMU Domain access control / MPU write buffer control */
99 { .name = "DACR", .cp = 15,
100 .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
101 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
102 .resetvalue = 0, .writefn = dacr_write },
103 { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0,
104 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
105 .resetvalue = 0, .writefn = fcse_write },
106 { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
107 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
108 .resetvalue = 0, .writefn = contextidr_write },
109 /* ??? This covers not just the impdef TLB lockdown registers but also
110 * some v7VMSA registers relating to TEX remap, so it is overly broad.
112 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
113 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
117 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
118 /* Not all pre-v6 cores implemented this WFI, so this is slightly
121 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
122 .access = PL1_W, .type = ARM_CP_WFI },
126 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
127 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
128 * is UNPREDICTABLE; we choose to NOP as most implementations do).
130 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
131 .access = PL1_W, .type = ARM_CP_WFI },
135 static const ARMCPRegInfo v6_cp_reginfo[] = {
136 /* prefetch by MVA in v6, NOP in v7 */
137 { .name = "MVA_prefetch",
138 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
139 .access = PL1_W, .type = ARM_CP_NOP },
140 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
141 .access = PL0_W, .type = ARM_CP_NOP },
142 { .name = "ISB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
143 .access = PL0_W, .type = ARM_CP_NOP },
144 { .name = "ISB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
145 .access = PL0_W, .type = ARM_CP_NOP },
149 static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
152 /* Generic performance monitor register read function for where
153 * user access may be allowed by PMUSERENR.
155 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
158 *value = CPREG_FIELD32(env, ri);
162 static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
165 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
168 /* only the DP, X, D and E bits are writable */
169 env->cp15.c9_pmcr &= ~0x39;
170 env->cp15.c9_pmcr |= (value & 0x39);
174 static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
177 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
181 env->cp15.c9_pmcnten |= value;
185 static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
188 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
192 env->cp15.c9_pmcnten &= ~value;
196 static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
199 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
202 env->cp15.c9_pmovsr &= ~value;
206 static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
209 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
212 env->cp15.c9_pmxevtyper = value & 0xff;
216 static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
219 env->cp15.c9_pmuserenr = value & 1;
223 static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
226 /* We have no event counters so only the C bit can be changed */
228 env->cp15.c9_pminten |= value;
232 static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
236 env->cp15.c9_pminten &= ~value;
240 static const ARMCPRegInfo v7_cp_reginfo[] = {
241 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
244 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
245 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
246 { .name = "DBGDRAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
247 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
248 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
249 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
250 .access = PL1_W, .type = ARM_CP_NOP },
251 /* Performance monitors are implementation defined in v7,
252 * but with an ARM recommended set of registers, which we
253 * follow (although we don't actually implement any counters)
255 * Performance registers fall into three categories:
256 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
257 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
258 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
259 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
260 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
262 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
263 .access = PL0_RW, .resetvalue = 0,
264 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
265 .readfn = pmreg_read, .writefn = pmcntenset_write },
266 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
267 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
268 .readfn = pmreg_read, .writefn = pmcntenclr_write },
269 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
270 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
271 .readfn = pmreg_read, .writefn = pmovsr_write },
272 /* Unimplemented so WI. Strictly speaking write accesses in PL0 should
275 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
276 .access = PL0_W, .type = ARM_CP_NOP },
277 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
278 * We choose to RAZ/WI. XXX should respect PMUSERENR.
280 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
281 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
282 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
283 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
284 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
285 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
287 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
288 .readfn = pmreg_read, .writefn = pmxevtyper_write },
289 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
290 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
291 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
292 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
293 .access = PL0_R | PL1_RW,
294 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
296 .writefn = pmuserenr_write },
297 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
299 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
301 .writefn = pmintenset_write },
302 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
304 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
306 .writefn = pmintenclr_write },
310 static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
317 static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
320 /* This is a helper function because the user access rights
321 * depend on the value of the TEECR.
323 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
326 *value = env->teehbr;
330 static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
333 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
340 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
341 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
342 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
344 .writefn = teecr_write },
345 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
346 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
348 .readfn = teehbr_read, .writefn = teehbr_write },
352 static const ARMCPRegInfo v6k_cp_reginfo[] = {
353 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
355 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
357 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
358 .access = PL0_R|PL1_W,
359 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
361 { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
363 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
368 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
369 /* Dummy implementation: RAZ/WI the whole crn=14 space */
370 { .name = "GENERIC_TIMER", .cp = 15, .crn = 14,
371 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
372 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
376 /* Return basic MPU access permission bits. */
377 static uint32_t simple_mpu_ap_bits(uint32_t val)
384 for (i = 0; i < 16; i += 2) {
385 ret |= (val >> i) & mask;
391 /* Pad basic MPU access permission bits to extended format. */
392 static uint32_t extended_mpu_ap_bits(uint32_t val)
399 for (i = 0; i < 16; i += 2) {
400 ret |= (val & mask) << i;
406 static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
409 env->cp15.c5_data = extended_mpu_ap_bits(value);
413 static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
416 *value = simple_mpu_ap_bits(env->cp15.c5_data);
420 static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
423 env->cp15.c5_insn = extended_mpu_ap_bits(value);
427 static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
430 *value = simple_mpu_ap_bits(env->cp15.c5_insn);
434 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
435 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
437 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
438 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
439 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
441 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
442 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
443 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
445 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
446 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
448 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
449 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
451 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
452 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
454 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
458 static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
462 env->cp15.c2_control = value;
463 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> value);
464 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> value);
468 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
470 env->cp15.c2_base_mask = 0xffffc000u;
471 env->cp15.c2_control = 0;
472 env->cp15.c2_mask = 0;
475 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
476 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
478 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
479 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
481 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
482 { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
484 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
485 { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
487 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
488 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
489 .access = PL1_RW, .writefn = vmsa_ttbcr_write,
490 .resetfn = vmsa_ttbcr_reset,
491 .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
495 static const ARMCPRegInfo omap_cp_reginfo[] = {
496 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
497 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
498 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
502 void register_cp_regs_for_features(ARMCPU *cpu)
504 /* Register all the coprocessor registers based on feature bits */
505 CPUARMState *env = &cpu->env;
506 if (arm_feature(env, ARM_FEATURE_M)) {
507 /* M profile has no coprocessor registers */
511 define_arm_cp_regs(cpu, cp_reginfo);
512 if (arm_feature(env, ARM_FEATURE_V6)) {
513 define_arm_cp_regs(cpu, v6_cp_reginfo);
515 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
517 if (arm_feature(env, ARM_FEATURE_V6K)) {
518 define_arm_cp_regs(cpu, v6k_cp_reginfo);
520 if (arm_feature(env, ARM_FEATURE_V7)) {
521 /* v7 performance monitor control register: same implementor
522 * field as main ID register, and we implement no event counters.
524 ARMCPRegInfo pmcr = {
525 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
526 .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
527 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
528 .readfn = pmreg_read, .writefn = pmcr_write
530 define_one_arm_cp_reg(cpu, &pmcr);
531 define_arm_cp_regs(cpu, v7_cp_reginfo);
533 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
535 if (arm_feature(env, ARM_FEATURE_MPU)) {
536 /* These are the MPU registers prior to PMSAv6. Any new
537 * PMSA core later than the ARM946 will require that we
538 * implement the PMSAv6 or PMSAv7 registers, which are
539 * completely different.
541 assert(!arm_feature(env, ARM_FEATURE_V6));
542 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
544 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
546 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
547 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
549 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
550 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
552 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
553 define_arm_cp_regs(cpu, omap_cp_reginfo);
557 ARMCPU *cpu_arm_init(const char *cpu_model)
561 static int inited = 0;
563 if (!object_class_by_name(cpu_model)) {
566 cpu = ARM_CPU(object_new(cpu_model));
568 env->cpu_model_str = cpu_model;
569 arm_cpu_realize(cpu);
571 if (tcg_enabled() && !inited) {
573 arm_translate_init();
577 if (arm_feature(env, ARM_FEATURE_NEON)) {
578 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
579 51, "arm-neon.xml", 0);
580 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
581 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
582 35, "arm-vfp3.xml", 0);
583 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
584 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
585 19, "arm-vfp.xml", 0);
591 typedef struct ARMCPUListState {
592 fprintf_function cpu_fprintf;
596 /* Sort alphabetically by type name, except for "any". */
597 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
599 ObjectClass *class_a = (ObjectClass *)a;
600 ObjectClass *class_b = (ObjectClass *)b;
601 const char *name_a, *name_b;
603 name_a = object_class_get_name(class_a);
604 name_b = object_class_get_name(class_b);
605 if (strcmp(name_a, "any") == 0) {
607 } else if (strcmp(name_b, "any") == 0) {
610 return strcmp(name_a, name_b);
614 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
616 ObjectClass *oc = data;
617 ARMCPUListState *s = user_data;
619 (*s->cpu_fprintf)(s->file, " %s\n",
620 object_class_get_name(oc));
623 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
625 ARMCPUListState s = {
627 .cpu_fprintf = cpu_fprintf,
631 list = object_class_get_list(TYPE_ARM_CPU, false);
632 list = g_slist_sort(list, arm_cpu_list_compare);
633 (*cpu_fprintf)(f, "Available CPUs:\n");
634 g_slist_foreach(list, arm_cpu_list_entry, &s);
638 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
639 const ARMCPRegInfo *r, void *opaque)
641 /* Define implementations of coprocessor registers.
642 * We store these in a hashtable because typically
643 * there are less than 150 registers in a space which
644 * is 16*16*16*8*8 = 262144 in size.
645 * Wildcarding is supported for the crm, opc1 and opc2 fields.
646 * If a register is defined twice then the second definition is
647 * used, so this can be used to define some generic registers and
648 * then override them with implementation specific variations.
649 * At least one of the original and the second definition should
650 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
651 * against accidental use.
654 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
655 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
656 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
657 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
658 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
659 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
660 /* 64 bit registers have only CRm and Opc1 fields */
661 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
662 /* Check that the register definition has enough info to handle
663 * reads and writes if they are permitted.
665 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
666 if (r->access & PL3_R) {
667 assert(r->fieldoffset || r->readfn);
669 if (r->access & PL3_W) {
670 assert(r->fieldoffset || r->writefn);
673 /* Bad type field probably means missing sentinel at end of reg list */
674 assert(cptype_valid(r->type));
675 for (crm = crmmin; crm <= crmmax; crm++) {
676 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
677 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
678 uint32_t *key = g_new(uint32_t, 1);
679 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
680 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
681 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
683 /* Make sure reginfo passed to helpers for wildcarded regs
684 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
689 /* Overriding of an existing definition must be explicitly
692 if (!(r->type & ARM_CP_OVERRIDE)) {
693 ARMCPRegInfo *oldreg;
694 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
695 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
696 fprintf(stderr, "Register redefined: cp=%d %d bit "
697 "crn=%d crm=%d opc1=%d opc2=%d, "
698 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
699 r2->crn, r2->crm, r2->opc1, r2->opc2,
700 oldreg->name, r2->name);
704 g_hash_table_insert(cpu->cp_regs, key, r2);
710 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
711 const ARMCPRegInfo *regs, void *opaque)
713 /* Define a whole list of registers */
714 const ARMCPRegInfo *r;
715 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
716 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
720 const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
722 return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
725 int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
728 /* Helper coprocessor write function for write-ignore registers */
732 int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
734 /* Helper coprocessor write function for read-as-zero registers */
739 static int bad_mode_switch(CPUARMState *env, int mode)
741 /* Return true if it is not valid for us to switch to
742 * this CPU mode (ie all the UNPREDICTABLE cases in
743 * the ARM ARM CPSRWriteByInstr pseudocode).
746 case ARM_CPU_MODE_USR:
747 case ARM_CPU_MODE_SYS:
748 case ARM_CPU_MODE_SVC:
749 case ARM_CPU_MODE_ABT:
750 case ARM_CPU_MODE_UND:
751 case ARM_CPU_MODE_IRQ:
752 case ARM_CPU_MODE_FIQ:
759 uint32_t cpsr_read(CPUARMState *env)
763 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
764 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
765 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
766 | ((env->condexec_bits & 0xfc) << 8)
770 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
772 if (mask & CPSR_NZCV) {
773 env->ZF = (~val) & CPSR_Z;
775 env->CF = (val >> 29) & 1;
776 env->VF = (val << 3) & 0x80000000;
779 env->QF = ((val & CPSR_Q) != 0);
781 env->thumb = ((val & CPSR_T) != 0);
782 if (mask & CPSR_IT_0_1) {
783 env->condexec_bits &= ~3;
784 env->condexec_bits |= (val >> 25) & 3;
786 if (mask & CPSR_IT_2_7) {
787 env->condexec_bits &= 3;
788 env->condexec_bits |= (val >> 8) & 0xfc;
790 if (mask & CPSR_GE) {
791 env->GE = (val >> 16) & 0xf;
794 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
795 if (bad_mode_switch(env, val & CPSR_M)) {
796 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
797 * We choose to ignore the attempt and leave the CPSR M field
802 switch_mode(env, val & CPSR_M);
805 mask &= ~CACHED_CPSR_BITS;
806 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
809 /* Sign/zero extend */
810 uint32_t HELPER(sxtb16)(uint32_t x)
813 res = (uint16_t)(int8_t)x;
814 res |= (uint32_t)(int8_t)(x >> 16) << 16;
818 uint32_t HELPER(uxtb16)(uint32_t x)
821 res = (uint16_t)(uint8_t)x;
822 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
826 uint32_t HELPER(clz)(uint32_t x)
831 int32_t HELPER(sdiv)(int32_t num, int32_t den)
835 if (num == INT_MIN && den == -1)
840 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
847 uint32_t HELPER(rbit)(uint32_t x)
849 x = ((x & 0xff000000) >> 24)
850 | ((x & 0x00ff0000) >> 8)
851 | ((x & 0x0000ff00) << 8)
852 | ((x & 0x000000ff) << 24);
853 x = ((x & 0xf0f0f0f0) >> 4)
854 | ((x & 0x0f0f0f0f) << 4);
855 x = ((x & 0x88888888) >> 3)
856 | ((x & 0x44444444) >> 1)
857 | ((x & 0x22222222) << 1)
858 | ((x & 0x11111111) << 3);
862 uint32_t HELPER(abs)(uint32_t x)
864 return ((int32_t)x < 0) ? -x : x;
867 #if defined(CONFIG_USER_ONLY)
869 void do_interrupt (CPUARMState *env)
871 env->exception_index = -1;
874 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
878 env->exception_index = EXCP_PREFETCH_ABORT;
879 env->cp15.c6_insn = address;
881 env->exception_index = EXCP_DATA_ABORT;
882 env->cp15.c6_data = address;
887 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
889 cpu_abort(env, "cp15 insn %08x\n", insn);
892 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
894 cpu_abort(env, "cp15 insn %08x\n", insn);
897 /* These should probably raise undefined insn exceptions. */
898 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
900 cpu_abort(env, "v7m_mrs %d\n", reg);
903 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
905 cpu_abort(env, "v7m_mrs %d\n", reg);
909 void switch_mode(CPUARMState *env, int mode)
911 if (mode != ARM_CPU_MODE_USR)
912 cpu_abort(env, "Tried to switch out of user mode\n");
915 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
917 cpu_abort(env, "banked r13 write\n");
920 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
922 cpu_abort(env, "banked r13 read\n");
928 /* Map CPU modes onto saved register banks. */
929 static inline int bank_number(CPUARMState *env, int mode)
932 case ARM_CPU_MODE_USR:
933 case ARM_CPU_MODE_SYS:
935 case ARM_CPU_MODE_SVC:
937 case ARM_CPU_MODE_ABT:
939 case ARM_CPU_MODE_UND:
941 case ARM_CPU_MODE_IRQ:
943 case ARM_CPU_MODE_FIQ:
946 cpu_abort(env, "Bad mode %x\n", mode);
950 void switch_mode(CPUARMState *env, int mode)
955 old_mode = env->uncached_cpsr & CPSR_M;
956 if (mode == old_mode)
959 if (old_mode == ARM_CPU_MODE_FIQ) {
960 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
961 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
962 } else if (mode == ARM_CPU_MODE_FIQ) {
963 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
964 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
967 i = bank_number(env, old_mode);
968 env->banked_r13[i] = env->regs[13];
969 env->banked_r14[i] = env->regs[14];
970 env->banked_spsr[i] = env->spsr;
972 i = bank_number(env, mode);
973 env->regs[13] = env->banked_r13[i];
974 env->regs[14] = env->banked_r14[i];
975 env->spsr = env->banked_spsr[i];
978 static void v7m_push(CPUARMState *env, uint32_t val)
981 stl_phys(env->regs[13], val);
984 static uint32_t v7m_pop(CPUARMState *env)
987 val = ldl_phys(env->regs[13]);
992 /* Switch to V7M main or process stack pointer. */
993 static void switch_v7m_sp(CPUARMState *env, int process)
996 if (env->v7m.current_sp != process) {
997 tmp = env->v7m.other_sp;
998 env->v7m.other_sp = env->regs[13];
1000 env->v7m.current_sp = process;
1004 static void do_v7m_exception_exit(CPUARMState *env)
1009 type = env->regs[15];
1010 if (env->v7m.exception != 0)
1011 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
1013 /* Switch to the target stack. */
1014 switch_v7m_sp(env, (type & 4) != 0);
1015 /* Pop registers. */
1016 env->regs[0] = v7m_pop(env);
1017 env->regs[1] = v7m_pop(env);
1018 env->regs[2] = v7m_pop(env);
1019 env->regs[3] = v7m_pop(env);
1020 env->regs[12] = v7m_pop(env);
1021 env->regs[14] = v7m_pop(env);
1022 env->regs[15] = v7m_pop(env);
1023 xpsr = v7m_pop(env);
1024 xpsr_write(env, xpsr, 0xfffffdff);
1025 /* Undo stack alignment. */
1028 /* ??? The exception return type specifies Thread/Handler mode. However
1029 this is also implied by the xPSR value. Not sure what to do
1030 if there is a mismatch. */
1031 /* ??? Likewise for mismatches between the CONTROL register and the stack
1035 static void do_interrupt_v7m(CPUARMState *env)
1037 uint32_t xpsr = xpsr_read(env);
1042 if (env->v7m.current_sp)
1044 if (env->v7m.exception == 0)
1047 /* For exceptions we just mark as pending on the NVIC, and let that
1049 /* TODO: Need to escalate if the current priority is higher than the
1050 one we're raising. */
1051 switch (env->exception_index) {
1053 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
1057 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
1059 case EXCP_PREFETCH_ABORT:
1060 case EXCP_DATA_ABORT:
1061 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
1064 if (semihosting_enabled) {
1066 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
1069 env->regs[0] = do_arm_semihosting(env);
1073 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
1076 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
1078 case EXCP_EXCEPTION_EXIT:
1079 do_v7m_exception_exit(env);
1082 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
1083 return; /* Never happens. Keep compiler happy. */
1086 /* Align stack pointer. */
1087 /* ??? Should only do this if Configuration Control Register
1088 STACKALIGN bit is set. */
1089 if (env->regs[13] & 4) {
1093 /* Switch to the handler mode. */
1094 v7m_push(env, xpsr);
1095 v7m_push(env, env->regs[15]);
1096 v7m_push(env, env->regs[14]);
1097 v7m_push(env, env->regs[12]);
1098 v7m_push(env, env->regs[3]);
1099 v7m_push(env, env->regs[2]);
1100 v7m_push(env, env->regs[1]);
1101 v7m_push(env, env->regs[0]);
1102 switch_v7m_sp(env, 0);
1104 env->condexec_bits = 0;
1106 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
1107 env->regs[15] = addr & 0xfffffffe;
1108 env->thumb = addr & 1;
1111 /* Handle a CPU exception. */
1112 void do_interrupt(CPUARMState *env)
1120 do_interrupt_v7m(env);
1123 /* TODO: Vectored interrupt controller. */
1124 switch (env->exception_index) {
1126 new_mode = ARM_CPU_MODE_UND;
1135 if (semihosting_enabled) {
1136 /* Check for semihosting interrupt. */
1138 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
1140 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
1143 /* Only intercept calls from privileged modes, to provide some
1144 semblance of security. */
1145 if (((mask == 0x123456 && !env->thumb)
1146 || (mask == 0xab && env->thumb))
1147 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
1148 env->regs[0] = do_arm_semihosting(env);
1152 new_mode = ARM_CPU_MODE_SVC;
1155 /* The PC already points to the next instruction. */
1159 /* See if this is a semihosting syscall. */
1160 if (env->thumb && semihosting_enabled) {
1161 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
1163 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
1165 env->regs[0] = do_arm_semihosting(env);
1169 env->cp15.c5_insn = 2;
1170 /* Fall through to prefetch abort. */
1171 case EXCP_PREFETCH_ABORT:
1172 new_mode = ARM_CPU_MODE_ABT;
1174 mask = CPSR_A | CPSR_I;
1177 case EXCP_DATA_ABORT:
1178 new_mode = ARM_CPU_MODE_ABT;
1180 mask = CPSR_A | CPSR_I;
1184 new_mode = ARM_CPU_MODE_IRQ;
1186 /* Disable IRQ and imprecise data aborts. */
1187 mask = CPSR_A | CPSR_I;
1191 new_mode = ARM_CPU_MODE_FIQ;
1193 /* Disable FIQ, IRQ and imprecise data aborts. */
1194 mask = CPSR_A | CPSR_I | CPSR_F;
1198 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
1199 return; /* Never happens. Keep compiler happy. */
1202 if (env->cp15.c1_sys & (1 << 13)) {
1205 switch_mode (env, new_mode);
1206 env->spsr = cpsr_read(env);
1207 /* Clear IT bits. */
1208 env->condexec_bits = 0;
1209 /* Switch to the new mode, and to the correct instruction set. */
1210 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
1211 env->uncached_cpsr |= mask;
1212 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
1213 * and we should just guard the thumb mode on V4 */
1214 if (arm_feature(env, ARM_FEATURE_V4T)) {
1215 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
1217 env->regs[14] = env->regs[15] + offset;
1218 env->regs[15] = addr;
1219 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1222 /* Check section/page access permissions.
1223 Returns the page protection flags, or zero if the access is not
1225 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
1226 int access_type, int is_user)
1230 if (domain_prot == 3) {
1231 return PAGE_READ | PAGE_WRITE;
1234 if (access_type == 1)
1237 prot_ro = PAGE_READ;
1241 if (access_type == 1)
1243 switch ((env->cp15.c1_sys >> 8) & 3) {
1245 return is_user ? 0 : PAGE_READ;
1252 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
1257 return PAGE_READ | PAGE_WRITE;
1259 return PAGE_READ | PAGE_WRITE;
1260 case 4: /* Reserved. */
1263 return is_user ? 0 : prot_ro;
1267 if (!arm_feature (env, ARM_FEATURE_V6K))
1275 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
1279 if (address & env->cp15.c2_mask)
1280 table = env->cp15.c2_base1 & 0xffffc000;
1282 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
1284 table |= (address >> 18) & 0x3ffc;
1288 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
1289 int is_user, uint32_t *phys_ptr, int *prot,
1290 target_ulong *page_size)
1301 /* Pagetable walk. */
1302 /* Lookup l1 descriptor. */
1303 table = get_level1_table_address(env, address);
1304 desc = ldl_phys(table);
1306 domain = (desc >> 5) & 0x0f;
1307 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1309 /* Section translation fault. */
1313 if (domain_prot == 0 || domain_prot == 2) {
1315 code = 9; /* Section domain fault. */
1317 code = 11; /* Page domain fault. */
1322 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1323 ap = (desc >> 10) & 3;
1325 *page_size = 1024 * 1024;
1327 /* Lookup l2 entry. */
1329 /* Coarse pagetable. */
1330 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1332 /* Fine pagetable. */
1333 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1335 desc = ldl_phys(table);
1337 case 0: /* Page translation fault. */
1340 case 1: /* 64k page. */
1341 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1342 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1343 *page_size = 0x10000;
1345 case 2: /* 4k page. */
1346 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1347 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1348 *page_size = 0x1000;
1350 case 3: /* 1k page. */
1352 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1353 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1355 /* Page translation fault. */
1360 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1362 ap = (desc >> 4) & 3;
1366 /* Never happens, but compiler isn't smart enough to tell. */
1371 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1373 /* Access permission fault. */
1377 *phys_ptr = phys_addr;
1380 return code | (domain << 4);
1383 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
1384 int is_user, uint32_t *phys_ptr, int *prot,
1385 target_ulong *page_size)
1397 /* Pagetable walk. */
1398 /* Lookup l1 descriptor. */
1399 table = get_level1_table_address(env, address);
1400 desc = ldl_phys(table);
1403 /* Section translation fault. */
1407 } else if (type == 2 && (desc & (1 << 18))) {
1411 /* Section or page. */
1412 domain = (desc >> 5) & 0x0f;
1414 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1415 if (domain_prot == 0 || domain_prot == 2) {
1417 code = 9; /* Section domain fault. */
1419 code = 11; /* Page domain fault. */
1423 if (desc & (1 << 18)) {
1425 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1426 *page_size = 0x1000000;
1429 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1430 *page_size = 0x100000;
1432 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1433 xn = desc & (1 << 4);
1436 /* Lookup l2 entry. */
1437 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1438 desc = ldl_phys(table);
1439 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1441 case 0: /* Page translation fault. */
1444 case 1: /* 64k page. */
1445 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1446 xn = desc & (1 << 15);
1447 *page_size = 0x10000;
1449 case 2: case 3: /* 4k page. */
1450 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1452 *page_size = 0x1000;
1455 /* Never happens, but compiler isn't smart enough to tell. */
1460 if (domain_prot == 3) {
1461 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1463 if (xn && access_type == 2)
1466 /* The simplified model uses AP[0] as an access control bit. */
1467 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1468 /* Access flag fault. */
1469 code = (code == 15) ? 6 : 3;
1472 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1474 /* Access permission fault. */
1481 *phys_ptr = phys_addr;
1484 return code | (domain << 4);
1487 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
1488 int is_user, uint32_t *phys_ptr, int *prot)
1494 *phys_ptr = address;
1495 for (n = 7; n >= 0; n--) {
1496 base = env->cp15.c6_region[n];
1497 if ((base & 1) == 0)
1499 mask = 1 << ((base >> 1) & 0x1f);
1500 /* Keep this shift separate from the above to avoid an
1501 (undefined) << 32. */
1502 mask = (mask << 1) - 1;
1503 if (((base ^ address) & ~mask) == 0)
1509 if (access_type == 2) {
1510 mask = env->cp15.c5_insn;
1512 mask = env->cp15.c5_data;
1514 mask = (mask >> (n * 4)) & 0xf;
1521 *prot = PAGE_READ | PAGE_WRITE;
1526 *prot |= PAGE_WRITE;
1529 *prot = PAGE_READ | PAGE_WRITE;
1540 /* Bad permission. */
1547 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
1548 int access_type, int is_user,
1549 uint32_t *phys_ptr, int *prot,
1550 target_ulong *page_size)
1552 /* Fast Context Switch Extension. */
1553 if (address < 0x02000000)
1554 address += env->cp15.c13_fcse;
1556 if ((env->cp15.c1_sys & 1) == 0) {
1557 /* MMU/MPU disabled. */
1558 *phys_ptr = address;
1559 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1560 *page_size = TARGET_PAGE_SIZE;
1562 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1563 *page_size = TARGET_PAGE_SIZE;
1564 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1566 } else if (env->cp15.c1_sys & (1 << 23)) {
1567 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1570 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1575 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
1576 int access_type, int mmu_idx)
1579 target_ulong page_size;
1583 is_user = mmu_idx == MMU_USER_IDX;
1584 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1587 /* Map a single [sub]page. */
1588 phys_addr &= ~(uint32_t)0x3ff;
1589 address &= ~(uint32_t)0x3ff;
1590 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1594 if (access_type == 2) {
1595 env->cp15.c5_insn = ret;
1596 env->cp15.c6_insn = address;
1597 env->exception_index = EXCP_PREFETCH_ABORT;
1599 env->cp15.c5_data = ret;
1600 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1601 env->cp15.c5_data |= (1 << 11);
1602 env->cp15.c6_data = address;
1603 env->exception_index = EXCP_DATA_ABORT;
1608 target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
1611 target_ulong page_size;
1615 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1623 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1629 op1 = (insn >> 21) & 7;
1630 op2 = (insn >> 5) & 7;
1632 switch ((insn >> 16) & 0xf) {
1635 if (arm_feature(env, ARM_FEATURE_XSCALE))
1637 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1639 if (arm_feature(env, ARM_FEATURE_V7)
1640 && op1 == 2 && crm == 0 && op2 == 0) {
1641 env->cp15.c0_cssel = val & 0xf;
1645 case 1: /* System configuration. */
1646 if (arm_feature(env, ARM_FEATURE_V7)
1647 && op1 == 0 && crm == 1 && op2 == 0) {
1648 env->cp15.c1_scr = val;
1651 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1655 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1656 env->cp15.c1_sys = val;
1657 /* ??? Lots of these bits are not implemented. */
1658 /* This may enable/disable the MMU, so do a TLB flush. */
1661 case 1: /* Auxiliary control register. */
1662 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1663 env->cp15.c1_xscaleauxcr = val;
1666 /* Not implemented. */
1669 if (arm_feature(env, ARM_FEATURE_XSCALE))
1671 if (env->cp15.c1_coproc != val) {
1672 env->cp15.c1_coproc = val;
1673 /* ??? Is this safe when called from within a TB? */
1681 case 4: /* Reserved. */
1683 case 6: /* MMU Fault address / MPU base/size. */
1684 if (arm_feature(env, ARM_FEATURE_MPU)) {
1687 env->cp15.c6_region[crm] = val;
1689 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1693 env->cp15.c6_data = val;
1695 case 1: /* ??? This is WFAR on armv6 */
1697 env->cp15.c6_insn = val;
1704 case 7: /* Cache control. */
1705 env->cp15.c15_i_max = 0x000;
1706 env->cp15.c15_i_min = 0xff0;
1710 /* No cache, so nothing to do except VA->PA translations. */
1711 if (arm_feature(env, ARM_FEATURE_VAPA)) {
1714 if (arm_feature(env, ARM_FEATURE_V7)) {
1715 env->cp15.c7_par = val & 0xfffff6ff;
1717 env->cp15.c7_par = val & 0xfffff1ff;
1722 target_ulong page_size;
1724 int ret, is_user = op2 & 2;
1725 int access_type = op2 & 1;
1728 /* Other states are only available with TrustZone */
1731 ret = get_phys_addr(env, val, access_type, is_user,
1732 &phys_addr, &prot, &page_size);
1734 /* We do not set any attribute bits in the PAR */
1735 if (page_size == (1 << 24)
1736 && arm_feature(env, ARM_FEATURE_V7)) {
1737 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1739 env->cp15.c7_par = phys_addr & 0xfffff000;
1742 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1743 ((ret & (12 << 1)) >> 6) |
1744 ((ret & 0xf) << 1) | 1;
1751 case 8: /* MMU TLB control. */
1753 case 0: /* Invalidate all (TLBIALL) */
1756 case 1: /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
1757 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1759 case 2: /* Invalidate by ASID (TLBIASID) */
1760 tlb_flush(env, val == 0);
1762 case 3: /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
1763 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1770 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1772 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1773 break; /* Ignore ReadBuffer access */
1775 case 0: /* Cache lockdown. */
1777 case 0: /* L1 cache. */
1780 env->cp15.c9_data = val;
1783 env->cp15.c9_insn = val;
1789 case 1: /* L2 cache. */
1790 /* Ignore writes to L2 lockdown/auxiliary registers. */
1796 case 1: /* TCM memory region registers. */
1797 /* Not implemented. */
1803 case 12: /* Reserved. */
1805 case 15: /* Implementation specific. */
1806 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1807 if (op2 == 0 && crm == 1) {
1808 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1809 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1811 env->cp15.c15_cpar = val & 0x3fff;
1817 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1821 case 1: /* Set TI925T configuration. */
1822 env->cp15.c15_ticonfig = val & 0xe7;
1823 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1824 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1826 case 2: /* Set I_max. */
1827 env->cp15.c15_i_max = val;
1829 case 3: /* Set I_min. */
1830 env->cp15.c15_i_min = val;
1832 case 4: /* Set thread-ID. */
1833 env->cp15.c15_threadid = val & 0xffff;
1835 case 8: /* Wait-for-interrupt (deprecated). */
1836 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1842 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1845 if ((op1 == 0) && (op2 == 0)) {
1846 env->cp15.c15_power_control = val;
1847 } else if ((op1 == 0) && (op2 == 1)) {
1848 env->cp15.c15_diagnostic = val;
1849 } else if ((op1 == 0) && (op2 == 2)) {
1850 env->cp15.c15_power_diagnostic = val;
1860 /* ??? For debugging only. Should raise illegal instruction exception. */
1861 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1862 (insn >> 16) & 0xf, crm, op1, op2);
1865 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
1871 op1 = (insn >> 21) & 7;
1872 op2 = (insn >> 5) & 7;
1874 switch ((insn >> 16) & 0xf) {
1875 case 0: /* ID codes. */
1881 case 0: /* Device ID. */
1882 return env->cp15.c0_cpuid;
1883 case 1: /* Cache Type. */
1884 return env->cp15.c0_cachetype;
1885 case 2: /* TCM status. */
1887 case 3: /* TLB type register. */
1888 return 0; /* No lockable TLB entries. */
1890 /* The MPIDR was standardised in v7; prior to
1891 * this it was implemented only in the 11MPCore.
1892 * For all other pre-v7 cores it does not exist.
1894 if (arm_feature(env, ARM_FEATURE_V7) ||
1895 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1896 int mpidr = env->cpu_index;
1897 /* We don't support setting cluster ID ([8..11])
1898 * so these bits always RAZ.
1900 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1902 /* Cores which are uniprocessor (non-coherent)
1903 * but still implement the MP extensions set
1904 * bit 30. (For instance, A9UP.) However we do
1905 * not currently model any of those cores.
1910 /* otherwise fall through to the unimplemented-reg case */
1915 if (!arm_feature(env, ARM_FEATURE_V6))
1917 return env->cp15.c0_c1[op2];
1919 if (!arm_feature(env, ARM_FEATURE_V6))
1921 return env->cp15.c0_c2[op2];
1922 case 3: case 4: case 5: case 6: case 7:
1928 /* These registers aren't documented on arm11 cores. However
1929 Linux looks at them anyway. */
1930 if (!arm_feature(env, ARM_FEATURE_V6))
1934 if (!arm_feature(env, ARM_FEATURE_V7))
1939 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1941 return env->cp15.c0_clid;
1947 if (op2 != 0 || crm != 0)
1949 return env->cp15.c0_cssel;
1953 case 1: /* System configuration. */
1954 if (arm_feature(env, ARM_FEATURE_V7)
1955 && op1 == 0 && crm == 1 && op2 == 0) {
1956 return env->cp15.c1_scr;
1958 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1961 case 0: /* Control register. */
1962 return env->cp15.c1_sys;
1963 case 1: /* Auxiliary control register. */
1964 if (arm_feature(env, ARM_FEATURE_XSCALE))
1965 return env->cp15.c1_xscaleauxcr;
1966 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1968 switch (ARM_CPUID(env)) {
1969 case ARM_CPUID_ARM1026:
1971 case ARM_CPUID_ARM1136:
1972 case ARM_CPUID_ARM1136_R2:
1973 case ARM_CPUID_ARM1176:
1975 case ARM_CPUID_ARM11MPCORE:
1977 case ARM_CPUID_CORTEXA8:
1979 case ARM_CPUID_CORTEXA9:
1980 case ARM_CPUID_CORTEXA15:
1985 case 2: /* Coprocessor access register. */
1986 if (arm_feature(env, ARM_FEATURE_XSCALE))
1988 return env->cp15.c1_coproc;
1992 case 4: /* Reserved. */
1994 case 6: /* MMU Fault address. */
1995 if (arm_feature(env, ARM_FEATURE_MPU)) {
1998 return env->cp15.c6_region[crm];
2000 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2004 return env->cp15.c6_data;
2006 if (arm_feature(env, ARM_FEATURE_V6)) {
2007 /* Watchpoint Fault Adrress. */
2008 return 0; /* Not implemented. */
2010 /* Instruction Fault Adrress. */
2011 /* Arm9 doesn't have an IFAR, but implementing it anyway
2012 shouldn't do any harm. */
2013 return env->cp15.c6_insn;
2016 if (arm_feature(env, ARM_FEATURE_V6)) {
2017 /* Instruction Fault Adrress. */
2018 return env->cp15.c6_insn;
2026 case 7: /* Cache control. */
2027 if (crm == 4 && op1 == 0 && op2 == 0) {
2028 return env->cp15.c7_par;
2030 /* FIXME: Should only clear Z flag if destination is r15. */
2033 case 8: /* MMU TLB control. */
2037 case 0: /* Cache lockdown */
2039 case 0: /* L1 cache. */
2040 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2045 return env->cp15.c9_data;
2047 return env->cp15.c9_insn;
2051 case 1: /* L2 cache */
2052 /* L2 Lockdown and Auxiliary control. */
2055 /* L2 cache lockdown (A8 only) */
2058 /* L2 cache auxiliary control (A8) or control (A15) */
2059 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
2060 /* Linux wants the number of processors from here.
2061 * Might as well set the interrupt-controller bit too.
2063 return ((smp_cpus - 1) << 24) | (1 << 23);
2067 /* L2 cache extended control (A15) */
2080 case 11: /* TCM DMA control. */
2081 case 12: /* Reserved. */
2083 case 15: /* Implementation specific. */
2084 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2085 if (op2 == 0 && crm == 1)
2086 return env->cp15.c15_cpar;
2090 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2094 case 1: /* Read TI925T configuration. */
2095 return env->cp15.c15_ticonfig;
2096 case 2: /* Read I_max. */
2097 return env->cp15.c15_i_max;
2098 case 3: /* Read I_min. */
2099 return env->cp15.c15_i_min;
2100 case 4: /* Read thread-ID. */
2101 return env->cp15.c15_threadid;
2102 case 8: /* TI925T_status */
2105 /* TODO: Peripheral port remap register:
2106 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2107 * controller base address at $rn & ~0xfff and map size of
2108 * 0x200 << ($rn & 0xfff), when MMU is off. */
2111 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
2114 if ((op1 == 4) && (op2 == 0)) {
2115 /* The config_base_address should hold the value of
2116 * the peripheral base. ARM should get this from a CPU
2117 * object property, but that support isn't available in
2118 * December 2011. Default to 0 for now and board models
2119 * that care can set it by a private hook */
2120 return env->cp15.c15_config_base_address;
2121 } else if ((op1 == 0) && (op2 == 0)) {
2122 /* power_control should be set to maximum latency. Again,
2123 default to 0 and set by private hook */
2124 return env->cp15.c15_power_control;
2125 } else if ((op1 == 0) && (op2 == 1)) {
2126 return env->cp15.c15_diagnostic;
2127 } else if ((op1 == 0) && (op2 == 2)) {
2128 return env->cp15.c15_power_diagnostic;
2131 case 1: /* NEON Busy */
2133 case 5: /* tlb lockdown */
2136 if ((op1 == 5) && (op2 == 2)) {
2148 /* ??? For debugging only. Should raise illegal instruction exception. */
2149 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2150 (insn >> 16) & 0xf, crm, op1, op2);
2154 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2156 if ((env->uncached_cpsr & CPSR_M) == mode) {
2157 env->regs[13] = val;
2159 env->banked_r13[bank_number(env, mode)] = val;
2163 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2165 if ((env->uncached_cpsr & CPSR_M) == mode) {
2166 return env->regs[13];
2168 return env->banked_r13[bank_number(env, mode)];
2172 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2176 return xpsr_read(env) & 0xf8000000;
2178 return xpsr_read(env) & 0xf80001ff;
2180 return xpsr_read(env) & 0xff00fc00;
2182 return xpsr_read(env) & 0xff00fdff;
2184 return xpsr_read(env) & 0x000001ff;
2186 return xpsr_read(env) & 0x0700fc00;
2188 return xpsr_read(env) & 0x0700edff;
2190 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2192 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2193 case 16: /* PRIMASK */
2194 return (env->uncached_cpsr & CPSR_I) != 0;
2195 case 17: /* BASEPRI */
2196 case 18: /* BASEPRI_MAX */
2197 return env->v7m.basepri;
2198 case 19: /* FAULTMASK */
2199 return (env->uncached_cpsr & CPSR_F) != 0;
2200 case 20: /* CONTROL */
2201 return env->v7m.control;
2203 /* ??? For debugging only. */
2204 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2209 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2213 xpsr_write(env, val, 0xf8000000);
2216 xpsr_write(env, val, 0xf8000000);
2219 xpsr_write(env, val, 0xfe00fc00);
2222 xpsr_write(env, val, 0xfe00fc00);
2225 /* IPSR bits are readonly. */
2228 xpsr_write(env, val, 0x0600fc00);
2231 xpsr_write(env, val, 0x0600fc00);
2234 if (env->v7m.current_sp)
2235 env->v7m.other_sp = val;
2237 env->regs[13] = val;
2240 if (env->v7m.current_sp)
2241 env->regs[13] = val;
2243 env->v7m.other_sp = val;
2245 case 16: /* PRIMASK */
2247 env->uncached_cpsr |= CPSR_I;
2249 env->uncached_cpsr &= ~CPSR_I;
2251 case 17: /* BASEPRI */
2252 env->v7m.basepri = val & 0xff;
2254 case 18: /* BASEPRI_MAX */
2256 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2257 env->v7m.basepri = val;
2259 case 19: /* FAULTMASK */
2261 env->uncached_cpsr |= CPSR_F;
2263 env->uncached_cpsr &= ~CPSR_F;
2265 case 20: /* CONTROL */
2266 env->v7m.control = val & 3;
2267 switch_v7m_sp(env, (val & 2) != 0);
2270 /* ??? For debugging only. */
2271 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2278 /* Note that signed overflow is undefined in C. The following routines are
2279 careful to use unsigned types where modulo arithmetic is required.
2280 Failure to do so _will_ break on newer gcc. */
2282 /* Signed saturating arithmetic. */
2284 /* Perform 16-bit signed saturating addition. */
2285 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2290 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2299 /* Perform 8-bit signed saturating addition. */
2300 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2305 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2314 /* Perform 16-bit signed saturating subtraction. */
2315 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2320 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2329 /* Perform 8-bit signed saturating subtraction. */
2330 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2335 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2344 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2345 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2346 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2347 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2350 #include "op_addsub.h"
2352 /* Unsigned saturating arithmetic. */
2353 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2362 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2370 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2379 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2387 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2388 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2389 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2390 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2393 #include "op_addsub.h"
2395 /* Signed modulo arithmetic. */
2396 #define SARITH16(a, b, n, op) do { \
2398 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2399 RESULT(sum, n, 16); \
2401 ge |= 3 << (n * 2); \
2404 #define SARITH8(a, b, n, op) do { \
2406 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2407 RESULT(sum, n, 8); \
2413 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2414 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2415 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2416 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2420 #include "op_addsub.h"
2422 /* Unsigned modulo arithmetic. */
2423 #define ADD16(a, b, n) do { \
2425 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2426 RESULT(sum, n, 16); \
2427 if ((sum >> 16) == 1) \
2428 ge |= 3 << (n * 2); \
2431 #define ADD8(a, b, n) do { \
2433 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2434 RESULT(sum, n, 8); \
2435 if ((sum >> 8) == 1) \
2439 #define SUB16(a, b, n) do { \
2441 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2442 RESULT(sum, n, 16); \
2443 if ((sum >> 16) == 0) \
2444 ge |= 3 << (n * 2); \
2447 #define SUB8(a, b, n) do { \
2449 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2450 RESULT(sum, n, 8); \
2451 if ((sum >> 8) == 0) \
2458 #include "op_addsub.h"
2460 /* Halved signed arithmetic. */
2461 #define ADD16(a, b, n) \
2462 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2463 #define SUB16(a, b, n) \
2464 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2465 #define ADD8(a, b, n) \
2466 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2467 #define SUB8(a, b, n) \
2468 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2471 #include "op_addsub.h"
2473 /* Halved unsigned arithmetic. */
2474 #define ADD16(a, b, n) \
2475 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2476 #define SUB16(a, b, n) \
2477 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2478 #define ADD8(a, b, n) \
2479 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2480 #define SUB8(a, b, n) \
2481 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2484 #include "op_addsub.h"
2486 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2494 /* Unsigned sum of absolute byte differences. */
2495 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2498 sum = do_usad(a, b);
2499 sum += do_usad(a >> 8, b >> 8);
2500 sum += do_usad(a >> 16, b >>16);
2501 sum += do_usad(a >> 24, b >> 24);
2505 /* For ARMv6 SEL instruction. */
2506 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2519 return (a & mask) | (b & ~mask);
2522 uint32_t HELPER(logicq_cc)(uint64_t val)
2524 return (val >> 32) | (val != 0);
2527 /* VFP support. We follow the convention used for VFP instrunctions:
2528 Single precition routines have a "s" suffix, double precision a
2531 /* Convert host exception flags to vfp form. */
2532 static inline int vfp_exceptbits_from_host(int host_bits)
2534 int target_bits = 0;
2536 if (host_bits & float_flag_invalid)
2538 if (host_bits & float_flag_divbyzero)
2540 if (host_bits & float_flag_overflow)
2542 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
2544 if (host_bits & float_flag_inexact)
2545 target_bits |= 0x10;
2546 if (host_bits & float_flag_input_denormal)
2547 target_bits |= 0x80;
2551 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2556 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2557 | (env->vfp.vec_len << 16)
2558 | (env->vfp.vec_stride << 20);
2559 i = get_float_exception_flags(&env->vfp.fp_status);
2560 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
2561 fpscr |= vfp_exceptbits_from_host(i);
2565 uint32_t vfp_get_fpscr(CPUARMState *env)
2567 return HELPER(vfp_get_fpscr)(env);
2570 /* Convert vfp exception flags to target form. */
2571 static inline int vfp_exceptbits_to_host(int target_bits)
2575 if (target_bits & 1)
2576 host_bits |= float_flag_invalid;
2577 if (target_bits & 2)
2578 host_bits |= float_flag_divbyzero;
2579 if (target_bits & 4)
2580 host_bits |= float_flag_overflow;
2581 if (target_bits & 8)
2582 host_bits |= float_flag_underflow;
2583 if (target_bits & 0x10)
2584 host_bits |= float_flag_inexact;
2585 if (target_bits & 0x80)
2586 host_bits |= float_flag_input_denormal;
2590 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
2595 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2596 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2597 env->vfp.vec_len = (val >> 16) & 7;
2598 env->vfp.vec_stride = (val >> 20) & 3;
2601 if (changed & (3 << 22)) {
2602 i = (val >> 22) & 3;
2605 i = float_round_nearest_even;
2611 i = float_round_down;
2614 i = float_round_to_zero;
2617 set_float_rounding_mode(i, &env->vfp.fp_status);
2619 if (changed & (1 << 24)) {
2620 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2621 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2623 if (changed & (1 << 25))
2624 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2626 i = vfp_exceptbits_to_host(val);
2627 set_float_exception_flags(i, &env->vfp.fp_status);
2628 set_float_exception_flags(0, &env->vfp.standard_fp_status);
2631 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
2633 HELPER(vfp_set_fpscr)(env, val);
2636 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2638 #define VFP_BINOP(name) \
2639 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2641 float_status *fpst = fpstp; \
2642 return float32_ ## name(a, b, fpst); \
2644 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2646 float_status *fpst = fpstp; \
2647 return float64_ ## name(a, b, fpst); \
2655 float32 VFP_HELPER(neg, s)(float32 a)
2657 return float32_chs(a);
2660 float64 VFP_HELPER(neg, d)(float64 a)
2662 return float64_chs(a);
2665 float32 VFP_HELPER(abs, s)(float32 a)
2667 return float32_abs(a);
2670 float64 VFP_HELPER(abs, d)(float64 a)
2672 return float64_abs(a);
2675 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
2677 return float32_sqrt(a, &env->vfp.fp_status);
2680 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
2682 return float64_sqrt(a, &env->vfp.fp_status);
2685 /* XXX: check quiet/signaling case */
2686 #define DO_VFP_cmp(p, type) \
2687 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
2690 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2691 case 0: flags = 0x6; break; \
2692 case -1: flags = 0x8; break; \
2693 case 1: flags = 0x2; break; \
2694 default: case 2: flags = 0x3; break; \
2696 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2697 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2699 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
2702 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2703 case 0: flags = 0x6; break; \
2704 case -1: flags = 0x8; break; \
2705 case 1: flags = 0x2; break; \
2706 default: case 2: flags = 0x3; break; \
2708 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2709 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2711 DO_VFP_cmp(s, float32)
2712 DO_VFP_cmp(d, float64)
2715 /* Integer to float and float to integer conversions */
2717 #define CONV_ITOF(name, fsz, sign) \
2718 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2720 float_status *fpst = fpstp; \
2721 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
2724 #define CONV_FTOI(name, fsz, sign, round) \
2725 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2727 float_status *fpst = fpstp; \
2728 if (float##fsz##_is_any_nan(x)) { \
2729 float_raise(float_flag_invalid, fpst); \
2732 return float##fsz##_to_##sign##int32##round(x, fpst); \
2735 #define FLOAT_CONVS(name, p, fsz, sign) \
2736 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2737 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2738 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2740 FLOAT_CONVS(si, s, 32, )
2741 FLOAT_CONVS(si, d, 64, )
2742 FLOAT_CONVS(ui, s, 32, u)
2743 FLOAT_CONVS(ui, d, 64, u)
2749 /* floating point conversion */
2750 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
2752 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2753 /* ARM requires that S<->D conversion of any kind of NaN generates
2754 * a quiet NaN by forcing the most significant frac bit to 1.
2756 return float64_maybe_silence_nan(r);
2759 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
2761 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2762 /* ARM requires that S<->D conversion of any kind of NaN generates
2763 * a quiet NaN by forcing the most significant frac bit to 1.
2765 return float32_maybe_silence_nan(r);
2768 /* VFP3 fixed point conversion. */
2769 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2770 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2773 float_status *fpst = fpstp; \
2775 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2776 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2778 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2781 float_status *fpst = fpstp; \
2783 if (float##fsz##_is_any_nan(x)) { \
2784 float_raise(float_flag_invalid, fpst); \
2787 tmp = float##fsz##_scalbn(x, shift, fpst); \
2788 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2791 VFP_CONV_FIX(sh, d, 64, int16, )
2792 VFP_CONV_FIX(sl, d, 64, int32, )
2793 VFP_CONV_FIX(uh, d, 64, uint16, u)
2794 VFP_CONV_FIX(ul, d, 64, uint32, u)
2795 VFP_CONV_FIX(sh, s, 32, int16, )
2796 VFP_CONV_FIX(sl, s, 32, int32, )
2797 VFP_CONV_FIX(uh, s, 32, uint16, u)
2798 VFP_CONV_FIX(ul, s, 32, uint32, u)
2801 /* Half precision conversions. */
2802 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
2804 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2805 float32 r = float16_to_float32(make_float16(a), ieee, s);
2807 return float32_maybe_silence_nan(r);
2812 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
2814 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2815 float16 r = float32_to_float16(a, ieee, s);
2817 r = float16_maybe_silence_nan(r);
2819 return float16_val(r);
2822 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2824 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2827 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2829 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2832 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2834 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2837 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2839 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2842 #define float32_two make_float32(0x40000000)
2843 #define float32_three make_float32(0x40400000)
2844 #define float32_one_point_five make_float32(0x3fc00000)
2846 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
2848 float_status *s = &env->vfp.standard_fp_status;
2849 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2850 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2851 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2852 float_raise(float_flag_input_denormal, s);
2856 return float32_sub(float32_two, float32_mul(a, b, s), s);
2859 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
2861 float_status *s = &env->vfp.standard_fp_status;
2863 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2864 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2865 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2866 float_raise(float_flag_input_denormal, s);
2868 return float32_one_point_five;
2870 product = float32_mul(a, b, s);
2871 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
2876 /* Constants 256 and 512 are used in some helpers; we avoid relying on
2877 * int->float conversions at run-time. */
2878 #define float64_256 make_float64(0x4070000000000000LL)
2879 #define float64_512 make_float64(0x4080000000000000LL)
2881 /* The algorithm that must be used to calculate the estimate
2882 * is specified by the ARM ARM.
2884 static float64 recip_estimate(float64 a, CPUARMState *env)
2886 /* These calculations mustn't set any fp exception flags,
2887 * so we use a local copy of the fp_status.
2889 float_status dummy_status = env->vfp.standard_fp_status;
2890 float_status *s = &dummy_status;
2891 /* q = (int)(a * 512.0) */
2892 float64 q = float64_mul(float64_512, a, s);
2893 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2895 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2896 q = int64_to_float64(q_int, s);
2897 q = float64_add(q, float64_half, s);
2898 q = float64_div(q, float64_512, s);
2899 q = float64_div(float64_one, q, s);
2901 /* s = (int)(256.0 * r + 0.5) */
2902 q = float64_mul(q, float64_256, s);
2903 q = float64_add(q, float64_half, s);
2904 q_int = float64_to_int64_round_to_zero(q, s);
2906 /* return (double)s / 256.0 */
2907 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2910 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
2912 float_status *s = &env->vfp.standard_fp_status;
2914 uint32_t val32 = float32_val(a);
2917 int a_exp = (val32 & 0x7f800000) >> 23;
2918 int sign = val32 & 0x80000000;
2920 if (float32_is_any_nan(a)) {
2921 if (float32_is_signaling_nan(a)) {
2922 float_raise(float_flag_invalid, s);
2924 return float32_default_nan;
2925 } else if (float32_is_infinity(a)) {
2926 return float32_set_sign(float32_zero, float32_is_neg(a));
2927 } else if (float32_is_zero_or_denormal(a)) {
2928 if (!float32_is_zero(a)) {
2929 float_raise(float_flag_input_denormal, s);
2931 float_raise(float_flag_divbyzero, s);
2932 return float32_set_sign(float32_infinity, float32_is_neg(a));
2933 } else if (a_exp >= 253) {
2934 float_raise(float_flag_underflow, s);
2935 return float32_set_sign(float32_zero, float32_is_neg(a));
2938 f64 = make_float64((0x3feULL << 52)
2939 | ((int64_t)(val32 & 0x7fffff) << 29));
2941 result_exp = 253 - a_exp;
2943 f64 = recip_estimate(f64, env);
2946 | ((result_exp & 0xff) << 23)
2947 | ((float64_val(f64) >> 29) & 0x7fffff);
2948 return make_float32(val32);
2951 /* The algorithm that must be used to calculate the estimate
2952 * is specified by the ARM ARM.
2954 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
2956 /* These calculations mustn't set any fp exception flags,
2957 * so we use a local copy of the fp_status.
2959 float_status dummy_status = env->vfp.standard_fp_status;
2960 float_status *s = &dummy_status;
2964 if (float64_lt(a, float64_half, s)) {
2965 /* range 0.25 <= a < 0.5 */
2967 /* a in units of 1/512 rounded down */
2968 /* q0 = (int)(a * 512.0); */
2969 q = float64_mul(float64_512, a, s);
2970 q_int = float64_to_int64_round_to_zero(q, s);
2972 /* reciprocal root r */
2973 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2974 q = int64_to_float64(q_int, s);
2975 q = float64_add(q, float64_half, s);
2976 q = float64_div(q, float64_512, s);
2977 q = float64_sqrt(q, s);
2978 q = float64_div(float64_one, q, s);
2980 /* range 0.5 <= a < 1.0 */
2982 /* a in units of 1/256 rounded down */
2983 /* q1 = (int)(a * 256.0); */
2984 q = float64_mul(float64_256, a, s);
2985 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2987 /* reciprocal root r */
2988 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2989 q = int64_to_float64(q_int, s);
2990 q = float64_add(q, float64_half, s);
2991 q = float64_div(q, float64_256, s);
2992 q = float64_sqrt(q, s);
2993 q = float64_div(float64_one, q, s);
2995 /* r in units of 1/256 rounded to nearest */
2996 /* s = (int)(256.0 * r + 0.5); */
2998 q = float64_mul(q, float64_256,s );
2999 q = float64_add(q, float64_half, s);
3000 q_int = float64_to_int64_round_to_zero(q, s);
3002 /* return (double)s / 256.0;*/
3003 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3006 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
3008 float_status *s = &env->vfp.standard_fp_status;
3014 val = float32_val(a);
3016 if (float32_is_any_nan(a)) {
3017 if (float32_is_signaling_nan(a)) {
3018 float_raise(float_flag_invalid, s);
3020 return float32_default_nan;
3021 } else if (float32_is_zero_or_denormal(a)) {
3022 if (!float32_is_zero(a)) {
3023 float_raise(float_flag_input_denormal, s);
3025 float_raise(float_flag_divbyzero, s);
3026 return float32_set_sign(float32_infinity, float32_is_neg(a));
3027 } else if (float32_is_neg(a)) {
3028 float_raise(float_flag_invalid, s);
3029 return float32_default_nan;
3030 } else if (float32_is_infinity(a)) {
3031 return float32_zero;
3034 /* Normalize to a double-precision value between 0.25 and 1.0,
3035 * preserving the parity of the exponent. */
3036 if ((val & 0x800000) == 0) {
3037 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3039 | ((uint64_t)(val & 0x7fffff) << 29));
3041 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3043 | ((uint64_t)(val & 0x7fffff) << 29));
3046 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3048 f64 = recip_sqrt_estimate(f64, env);
3050 val64 = float64_val(f64);
3052 val = ((result_exp & 0xff) << 23)
3053 | ((val64 >> 29) & 0x7fffff);
3054 return make_float32(val);
3057 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
3061 if ((a & 0x80000000) == 0) {
3065 f64 = make_float64((0x3feULL << 52)
3066 | ((int64_t)(a & 0x7fffffff) << 21));
3068 f64 = recip_estimate (f64, env);
3070 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3073 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
3077 if ((a & 0xc0000000) == 0) {
3081 if (a & 0x80000000) {
3082 f64 = make_float64((0x3feULL << 52)
3083 | ((uint64_t)(a & 0x7fffffff) << 21));
3084 } else { /* bits 31-30 == '01' */
3085 f64 = make_float64((0x3fdULL << 52)
3086 | ((uint64_t)(a & 0x3fffffff) << 22));
3089 f64 = recip_sqrt_estimate(f64, env);
3091 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3094 /* VFPv4 fused multiply-accumulate */
3095 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3097 float_status *fpst = fpstp;
3098 return float32_muladd(a, b, c, 0, fpst);
3101 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3103 float_status *fpst = fpstp;
3104 return float64_muladd(a, b, c, 0, fpst);