4 #include "host-utils.h"
7 #ifndef CONFIG_USER_ONLY
8 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
9 int access_type, int is_user,
10 uint32_t *phys_ptr, int *prot,
11 target_ulong *page_size);
14 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
18 /* VFP data registers are always little-endian. */
19 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
21 stfq_le_p(buf, env->vfp.regs[reg]);
24 if (arm_feature(env, ARM_FEATURE_NEON)) {
25 /* Aliases for Q regs. */
28 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
29 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
33 switch (reg - nregs) {
34 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
35 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
36 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
41 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
45 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
47 env->vfp.regs[reg] = ldfq_le_p(buf);
50 if (arm_feature(env, ARM_FEATURE_NEON)) {
53 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
54 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
58 switch (reg - nregs) {
59 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
60 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
61 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
66 static int dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
69 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
73 static int fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
75 if (env->cp15.c13_fcse != value) {
76 /* Unlike real hardware the qemu TLB uses virtual addresses,
77 * not modified virtual addresses, so this causes a TLB flush.
80 env->cp15.c13_fcse = value;
84 static int contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
87 if (env->cp15.c13_context != value && !arm_feature(env, ARM_FEATURE_MPU)) {
88 /* For VMSA (when not using the LPAE long descriptor page table
89 * format) this register includes the ASID, so do a TLB flush.
90 * For PMSA it is purely a process ID and no action is needed.
94 env->cp15.c13_context = value;
98 static int tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
101 /* Invalidate all (TLBIALL) */
106 static int tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
109 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
110 tlb_flush_page(env, value & TARGET_PAGE_MASK);
114 static int tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
117 /* Invalidate by ASID (TLBIASID) */
118 tlb_flush(env, value == 0);
122 static int tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
125 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
126 tlb_flush_page(env, value & TARGET_PAGE_MASK);
130 static const ARMCPRegInfo cp_reginfo[] = {
131 /* DBGDIDR: just RAZ. In particular this means the "debug architecture
132 * version" bits will read as a reserved value, which should cause
133 * Linux to not try to use the debug hardware.
135 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
136 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
137 /* MMU Domain access control / MPU write buffer control */
138 { .name = "DACR", .cp = 15,
139 .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
140 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3),
141 .resetvalue = 0, .writefn = dacr_write },
142 { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0,
143 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
144 .resetvalue = 0, .writefn = fcse_write },
145 { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1,
146 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse),
147 .resetvalue = 0, .writefn = contextidr_write },
148 /* ??? This covers not just the impdef TLB lockdown registers but also
149 * some v7VMSA registers relating to TEX remap, so it is overly broad.
151 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY,
152 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
153 /* MMU TLB control. Note that the wildcarding means we cover not just
154 * the unified TLB ops but also the dside/iside/inner-shareable variants.
156 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
157 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, },
158 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
159 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, },
160 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
161 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, },
162 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
163 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, },
164 /* Cache maintenance ops; some of this space may be overridden later. */
165 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
166 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
167 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
171 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
172 /* Not all pre-v6 cores implemented this WFI, so this is slightly
175 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
176 .access = PL1_W, .type = ARM_CP_WFI },
180 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
181 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
182 * is UNPREDICTABLE; we choose to NOP as most implementations do).
184 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
185 .access = PL1_W, .type = ARM_CP_WFI },
186 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
187 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
188 * OMAPCP will override this space.
190 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
191 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
193 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
194 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
199 static int cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
201 if (env->cp15.c1_coproc != value) {
202 env->cp15.c1_coproc = value;
203 /* ??? Is this safe when called from within a TB? */
209 static const ARMCPRegInfo v6_cp_reginfo[] = {
210 /* prefetch by MVA in v6, NOP in v7 */
211 { .name = "MVA_prefetch",
212 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
213 .access = PL1_W, .type = ARM_CP_NOP },
214 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
215 .access = PL0_W, .type = ARM_CP_NOP },
216 { .name = "ISB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
217 .access = PL0_W, .type = ARM_CP_NOP },
218 { .name = "ISB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
219 .access = PL0_W, .type = ARM_CP_NOP },
220 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
221 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_insn),
223 /* Watchpoint Fault Address Register : should actually only be present
224 * for 1136, 1176, 11MPCore.
226 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
227 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
228 { .name = "CPACR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2,
229 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc),
230 .resetvalue = 0, .writefn = cpacr_write },
234 static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
237 /* Generic performance monitor register read function for where
238 * user access may be allowed by PMUSERENR.
240 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
243 *value = CPREG_FIELD32(env, ri);
247 static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
250 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
253 /* only the DP, X, D and E bits are writable */
254 env->cp15.c9_pmcr &= ~0x39;
255 env->cp15.c9_pmcr |= (value & 0x39);
259 static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
262 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
266 env->cp15.c9_pmcnten |= value;
270 static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
273 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
277 env->cp15.c9_pmcnten &= ~value;
281 static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
284 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
287 env->cp15.c9_pmovsr &= ~value;
291 static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
294 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
297 env->cp15.c9_pmxevtyper = value & 0xff;
301 static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
304 env->cp15.c9_pmuserenr = value & 1;
308 static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
311 /* We have no event counters so only the C bit can be changed */
313 env->cp15.c9_pminten |= value;
317 static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
321 env->cp15.c9_pminten &= ~value;
325 static const ARMCPRegInfo v7_cp_reginfo[] = {
326 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
329 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
330 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
331 { .name = "DBGDRAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
332 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
333 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
334 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
335 .access = PL1_W, .type = ARM_CP_NOP },
336 /* Performance monitors are implementation defined in v7,
337 * but with an ARM recommended set of registers, which we
338 * follow (although we don't actually implement any counters)
340 * Performance registers fall into three categories:
341 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
342 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
343 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
344 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
345 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
347 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
348 .access = PL0_RW, .resetvalue = 0,
349 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
350 .readfn = pmreg_read, .writefn = pmcntenset_write },
351 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
352 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
353 .readfn = pmreg_read, .writefn = pmcntenclr_write },
354 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
355 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
356 .readfn = pmreg_read, .writefn = pmovsr_write },
357 /* Unimplemented so WI. Strictly speaking write accesses in PL0 should
360 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
361 .access = PL0_W, .type = ARM_CP_NOP },
362 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
363 * We choose to RAZ/WI. XXX should respect PMUSERENR.
365 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
366 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
367 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
368 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
369 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
370 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
372 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
373 .readfn = pmreg_read, .writefn = pmxevtyper_write },
374 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
375 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
376 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
377 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
378 .access = PL0_R | PL1_RW,
379 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
381 .writefn = pmuserenr_write },
382 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
384 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
386 .writefn = pmintenset_write },
387 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
389 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
391 .writefn = pmintenclr_write },
392 { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0,
393 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr),
398 static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
405 static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
408 /* This is a helper function because the user access rights
409 * depend on the value of the TEECR.
411 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
414 *value = env->teehbr;
418 static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
421 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
428 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
429 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
430 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
432 .writefn = teecr_write },
433 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
434 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
436 .readfn = teehbr_read, .writefn = teehbr_write },
440 static const ARMCPRegInfo v6k_cp_reginfo[] = {
441 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
443 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
445 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
446 .access = PL0_R|PL1_W,
447 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
449 { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
451 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
456 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
457 /* Dummy implementation: RAZ/WI the whole crn=14 space */
458 { .name = "GENERIC_TIMER", .cp = 15, .crn = 14,
459 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
460 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
464 static int par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
466 if (arm_feature(env, ARM_FEATURE_V7)) {
467 env->cp15.c7_par = value & 0xfffff6ff;
469 env->cp15.c7_par = value & 0xfffff1ff;
474 #ifndef CONFIG_USER_ONLY
475 /* get_phys_addr() isn't present for user-mode-only targets */
476 static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
479 target_ulong page_size;
481 int ret, is_user = ri->opc2 & 2;
482 int access_type = ri->opc2 & 1;
485 /* Other states are only available with TrustZone */
488 ret = get_phys_addr(env, value, access_type, is_user,
489 &phys_addr, &prot, &page_size);
491 /* We do not set any attribute bits in the PAR */
492 if (page_size == (1 << 24)
493 && arm_feature(env, ARM_FEATURE_V7)) {
494 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
496 env->cp15.c7_par = phys_addr & 0xfffff000;
499 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
500 ((ret & (12 << 1)) >> 6) |
501 ((ret & 0xf) << 1) | 1;
507 static const ARMCPRegInfo vapa_cp_reginfo[] = {
508 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
509 .access = PL1_RW, .resetvalue = 0,
510 .fieldoffset = offsetof(CPUARMState, cp15.c7_par),
511 .writefn = par_write },
512 #ifndef CONFIG_USER_ONLY
513 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
514 .access = PL1_W, .writefn = ats_write },
519 /* Return basic MPU access permission bits. */
520 static uint32_t simple_mpu_ap_bits(uint32_t val)
527 for (i = 0; i < 16; i += 2) {
528 ret |= (val >> i) & mask;
534 /* Pad basic MPU access permission bits to extended format. */
535 static uint32_t extended_mpu_ap_bits(uint32_t val)
542 for (i = 0; i < 16; i += 2) {
543 ret |= (val & mask) << i;
549 static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
552 env->cp15.c5_data = extended_mpu_ap_bits(value);
556 static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
559 *value = simple_mpu_ap_bits(env->cp15.c5_data);
563 static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 env->cp15.c5_insn = extended_mpu_ap_bits(value);
570 static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
573 *value = simple_mpu_ap_bits(env->cp15.c5_insn);
577 static int arm946_prbs_read(CPUARMState *env, const ARMCPRegInfo *ri,
583 *value = env->cp15.c6_region[ri->crm];
587 static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
593 env->cp15.c6_region[ri->crm] = value;
597 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
598 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
600 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
601 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
602 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
604 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
605 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
606 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
608 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
609 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
611 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
612 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
614 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
615 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
617 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
618 /* Protection region base and size registers */
619 { .name = "946_PRBS", .cp = 15, .crn = 6, .crm = CP_ANY, .opc1 = 0,
620 .opc2 = CP_ANY, .access = PL1_RW,
621 .readfn = arm946_prbs_read, .writefn = arm946_prbs_write, },
625 static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
629 env->cp15.c2_control = value;
630 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> value);
631 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> value);
635 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
637 env->cp15.c2_base_mask = 0xffffc000u;
638 env->cp15.c2_control = 0;
639 env->cp15.c2_mask = 0;
642 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
643 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
645 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
646 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
648 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
649 { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
651 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
652 { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
654 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
655 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
656 .access = PL1_RW, .writefn = vmsa_ttbcr_write,
657 .resetfn = vmsa_ttbcr_reset,
658 .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
659 { .name = "DFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
660 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_data),
665 static int omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
668 env->cp15.c15_ticonfig = value & 0xe7;
669 /* The OS_TYPE bit in this register changes the reported CPUID! */
670 env->cp15.c0_cpuid = (value & (1 << 5)) ?
671 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
675 static int omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
678 env->cp15.c15_threadid = value & 0xffff;
682 static int omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
685 /* Wait-for-interrupt (deprecated) */
686 cpu_interrupt(env, CPU_INTERRUPT_HALT);
690 static int omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
693 /* On OMAP there are registers indicating the max/min index of dcache lines
694 * containing a dirty line; cache flush operations have to reset these.
696 env->cp15.c15_i_max = 0x000;
697 env->cp15.c15_i_min = 0xff0;
701 static const ARMCPRegInfo omap_cp_reginfo[] = {
702 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
703 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
704 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
705 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
706 .access = PL1_RW, .type = ARM_CP_NOP },
707 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
709 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
710 .writefn = omap_ticonfig_write },
711 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
713 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
714 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
715 .access = PL1_RW, .resetvalue = 0xff0,
716 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
717 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
719 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
720 .writefn = omap_threadid_write },
721 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
722 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
723 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
724 /* TODO: Peripheral port remap register:
725 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
726 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
729 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
730 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, .type = ARM_CP_OVERRIDE,
731 .writefn = omap_cachemaint_write },
732 { .name = "C9", .cp = 15, .crn = 9,
733 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
734 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
738 static int xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
742 if (env->cp15.c15_cpar != value) {
743 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
745 env->cp15.c15_cpar = value;
750 static const ARMCPRegInfo xscale_cp_reginfo[] = {
751 { .name = "XSCALE_CPAR",
752 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
753 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
754 .writefn = xscale_cpar_write, },
755 { .name = "XSCALE_AUXCR",
756 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
757 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
762 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
763 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
764 * implementation of this implementation-defined space.
765 * Ideally this should eventually disappear in favour of actually
766 * implementing the correct behaviour for all cores.
768 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
769 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
770 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
774 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
775 /* Cache status: RAZ because we have no cache so it's always clean */
776 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
777 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
781 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
782 /* We never have a a block transfer operation in progress */
783 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
784 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
788 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
789 /* The cache test-and-clean instructions always return (1 << 30)
790 * to indicate that there are no dirty cache lines.
792 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
793 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = (1 << 30) },
794 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
795 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = (1 << 30) },
799 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
800 /* Ignore ReadBuffer accesses */
801 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
802 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
803 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
808 static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
810 env->cp15.c1_sys = value;
811 /* ??? Lots of these bits are not implemented. */
812 /* This may enable/disable the MMU, so do a TLB flush. */
817 void register_cp_regs_for_features(ARMCPU *cpu)
819 /* Register all the coprocessor registers based on feature bits */
820 CPUARMState *env = &cpu->env;
821 if (arm_feature(env, ARM_FEATURE_M)) {
822 /* M profile has no coprocessor registers */
826 define_arm_cp_regs(cpu, cp_reginfo);
827 if (arm_feature(env, ARM_FEATURE_V6)) {
828 define_arm_cp_regs(cpu, v6_cp_reginfo);
830 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
832 if (arm_feature(env, ARM_FEATURE_V6K)) {
833 define_arm_cp_regs(cpu, v6k_cp_reginfo);
835 if (arm_feature(env, ARM_FEATURE_V7)) {
836 /* v7 performance monitor control register: same implementor
837 * field as main ID register, and we implement no event counters.
839 ARMCPRegInfo pmcr = {
840 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
841 .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
842 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
843 .readfn = pmreg_read, .writefn = pmcr_write
845 define_one_arm_cp_reg(cpu, &pmcr);
846 define_arm_cp_regs(cpu, v7_cp_reginfo);
848 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
850 if (arm_feature(env, ARM_FEATURE_MPU)) {
851 /* These are the MPU registers prior to PMSAv6. Any new
852 * PMSA core later than the ARM946 will require that we
853 * implement the PMSAv6 or PMSAv7 registers, which are
854 * completely different.
856 assert(!arm_feature(env, ARM_FEATURE_V6));
857 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
859 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
861 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
862 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
864 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
865 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
867 if (arm_feature(env, ARM_FEATURE_VAPA)) {
868 define_arm_cp_regs(cpu, vapa_cp_reginfo);
870 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
871 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
873 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
874 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
876 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
877 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
879 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
880 define_arm_cp_regs(cpu, omap_cp_reginfo);
882 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
883 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
885 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
886 define_arm_cp_regs(cpu, xscale_cp_reginfo);
888 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
889 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
891 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
892 ARMCPRegInfo auxcr = {
893 .name = "AUXCR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1,
894 .access = PL1_RW, .type = ARM_CP_CONST,
895 .resetvalue = cpu->reset_auxcr
897 define_one_arm_cp_reg(cpu, &auxcr);
900 /* Generic registers whose values depend on the implementation */
902 ARMCPRegInfo sctlr = {
903 .name = "SCTLR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
904 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys),
905 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr
907 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
908 /* Normally we would always end the TB on an SCTLR write, but Linux
909 * arch/arm/mach-pxa/sleep.S expects two instructions following
910 * an MMU enable to execute from cache. Imitate this behaviour.
912 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
914 define_one_arm_cp_reg(cpu, &sctlr);
918 ARMCPU *cpu_arm_init(const char *cpu_model)
922 static int inited = 0;
924 if (!object_class_by_name(cpu_model)) {
927 cpu = ARM_CPU(object_new(cpu_model));
929 env->cpu_model_str = cpu_model;
930 arm_cpu_realize(cpu);
932 if (tcg_enabled() && !inited) {
934 arm_translate_init();
938 if (arm_feature(env, ARM_FEATURE_NEON)) {
939 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
940 51, "arm-neon.xml", 0);
941 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
942 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
943 35, "arm-vfp3.xml", 0);
944 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
945 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
946 19, "arm-vfp.xml", 0);
952 typedef struct ARMCPUListState {
953 fprintf_function cpu_fprintf;
957 /* Sort alphabetically by type name, except for "any". */
958 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
960 ObjectClass *class_a = (ObjectClass *)a;
961 ObjectClass *class_b = (ObjectClass *)b;
962 const char *name_a, *name_b;
964 name_a = object_class_get_name(class_a);
965 name_b = object_class_get_name(class_b);
966 if (strcmp(name_a, "any") == 0) {
968 } else if (strcmp(name_b, "any") == 0) {
971 return strcmp(name_a, name_b);
975 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
977 ObjectClass *oc = data;
978 ARMCPUListState *s = user_data;
980 (*s->cpu_fprintf)(s->file, " %s\n",
981 object_class_get_name(oc));
984 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
986 ARMCPUListState s = {
988 .cpu_fprintf = cpu_fprintf,
992 list = object_class_get_list(TYPE_ARM_CPU, false);
993 list = g_slist_sort(list, arm_cpu_list_compare);
994 (*cpu_fprintf)(f, "Available CPUs:\n");
995 g_slist_foreach(list, arm_cpu_list_entry, &s);
999 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
1000 const ARMCPRegInfo *r, void *opaque)
1002 /* Define implementations of coprocessor registers.
1003 * We store these in a hashtable because typically
1004 * there are less than 150 registers in a space which
1005 * is 16*16*16*8*8 = 262144 in size.
1006 * Wildcarding is supported for the crm, opc1 and opc2 fields.
1007 * If a register is defined twice then the second definition is
1008 * used, so this can be used to define some generic registers and
1009 * then override them with implementation specific variations.
1010 * At least one of the original and the second definition should
1011 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
1012 * against accidental use.
1014 int crm, opc1, opc2;
1015 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
1016 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
1017 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
1018 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
1019 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
1020 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
1021 /* 64 bit registers have only CRm and Opc1 fields */
1022 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
1023 /* Check that the register definition has enough info to handle
1024 * reads and writes if they are permitted.
1026 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
1027 if (r->access & PL3_R) {
1028 assert(r->fieldoffset || r->readfn);
1030 if (r->access & PL3_W) {
1031 assert(r->fieldoffset || r->writefn);
1034 /* Bad type field probably means missing sentinel at end of reg list */
1035 assert(cptype_valid(r->type));
1036 for (crm = crmmin; crm <= crmmax; crm++) {
1037 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
1038 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
1039 uint32_t *key = g_new(uint32_t, 1);
1040 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
1041 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
1042 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
1043 r2->opaque = opaque;
1044 /* Make sure reginfo passed to helpers for wildcarded regs
1045 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
1050 /* Overriding of an existing definition must be explicitly
1053 if (!(r->type & ARM_CP_OVERRIDE)) {
1054 ARMCPRegInfo *oldreg;
1055 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
1056 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
1057 fprintf(stderr, "Register redefined: cp=%d %d bit "
1058 "crn=%d crm=%d opc1=%d opc2=%d, "
1059 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
1060 r2->crn, r2->crm, r2->opc1, r2->opc2,
1061 oldreg->name, r2->name);
1065 g_hash_table_insert(cpu->cp_regs, key, r2);
1071 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
1072 const ARMCPRegInfo *regs, void *opaque)
1074 /* Define a whole list of registers */
1075 const ARMCPRegInfo *r;
1076 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
1077 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
1081 const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
1083 return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
1086 int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
1089 /* Helper coprocessor write function for write-ignore registers */
1093 int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1095 /* Helper coprocessor write function for read-as-zero registers */
1100 static int bad_mode_switch(CPUARMState *env, int mode)
1102 /* Return true if it is not valid for us to switch to
1103 * this CPU mode (ie all the UNPREDICTABLE cases in
1104 * the ARM ARM CPSRWriteByInstr pseudocode).
1107 case ARM_CPU_MODE_USR:
1108 case ARM_CPU_MODE_SYS:
1109 case ARM_CPU_MODE_SVC:
1110 case ARM_CPU_MODE_ABT:
1111 case ARM_CPU_MODE_UND:
1112 case ARM_CPU_MODE_IRQ:
1113 case ARM_CPU_MODE_FIQ:
1120 uint32_t cpsr_read(CPUARMState *env)
1123 ZF = (env->ZF == 0);
1124 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
1125 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
1126 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
1127 | ((env->condexec_bits & 0xfc) << 8)
1131 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
1133 if (mask & CPSR_NZCV) {
1134 env->ZF = (~val) & CPSR_Z;
1136 env->CF = (val >> 29) & 1;
1137 env->VF = (val << 3) & 0x80000000;
1140 env->QF = ((val & CPSR_Q) != 0);
1142 env->thumb = ((val & CPSR_T) != 0);
1143 if (mask & CPSR_IT_0_1) {
1144 env->condexec_bits &= ~3;
1145 env->condexec_bits |= (val >> 25) & 3;
1147 if (mask & CPSR_IT_2_7) {
1148 env->condexec_bits &= 3;
1149 env->condexec_bits |= (val >> 8) & 0xfc;
1151 if (mask & CPSR_GE) {
1152 env->GE = (val >> 16) & 0xf;
1155 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
1156 if (bad_mode_switch(env, val & CPSR_M)) {
1157 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
1158 * We choose to ignore the attempt and leave the CPSR M field
1163 switch_mode(env, val & CPSR_M);
1166 mask &= ~CACHED_CPSR_BITS;
1167 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
1170 /* Sign/zero extend */
1171 uint32_t HELPER(sxtb16)(uint32_t x)
1174 res = (uint16_t)(int8_t)x;
1175 res |= (uint32_t)(int8_t)(x >> 16) << 16;
1179 uint32_t HELPER(uxtb16)(uint32_t x)
1182 res = (uint16_t)(uint8_t)x;
1183 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
1187 uint32_t HELPER(clz)(uint32_t x)
1192 int32_t HELPER(sdiv)(int32_t num, int32_t den)
1196 if (num == INT_MIN && den == -1)
1201 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
1208 uint32_t HELPER(rbit)(uint32_t x)
1210 x = ((x & 0xff000000) >> 24)
1211 | ((x & 0x00ff0000) >> 8)
1212 | ((x & 0x0000ff00) << 8)
1213 | ((x & 0x000000ff) << 24);
1214 x = ((x & 0xf0f0f0f0) >> 4)
1215 | ((x & 0x0f0f0f0f) << 4);
1216 x = ((x & 0x88888888) >> 3)
1217 | ((x & 0x44444444) >> 1)
1218 | ((x & 0x22222222) << 1)
1219 | ((x & 0x11111111) << 3);
1223 uint32_t HELPER(abs)(uint32_t x)
1225 return ((int32_t)x < 0) ? -x : x;
1228 #if defined(CONFIG_USER_ONLY)
1230 void do_interrupt (CPUARMState *env)
1232 env->exception_index = -1;
1235 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
1239 env->exception_index = EXCP_PREFETCH_ABORT;
1240 env->cp15.c6_insn = address;
1242 env->exception_index = EXCP_DATA_ABORT;
1243 env->cp15.c6_data = address;
1248 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1250 cpu_abort(env, "cp15 insn %08x\n", insn);
1253 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
1255 cpu_abort(env, "cp15 insn %08x\n", insn);
1258 /* These should probably raise undefined insn exceptions. */
1259 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
1261 cpu_abort(env, "v7m_mrs %d\n", reg);
1264 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
1266 cpu_abort(env, "v7m_mrs %d\n", reg);
1270 void switch_mode(CPUARMState *env, int mode)
1272 if (mode != ARM_CPU_MODE_USR)
1273 cpu_abort(env, "Tried to switch out of user mode\n");
1276 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
1278 cpu_abort(env, "banked r13 write\n");
1281 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
1283 cpu_abort(env, "banked r13 read\n");
1289 /* Map CPU modes onto saved register banks. */
1290 static inline int bank_number(CPUARMState *env, int mode)
1293 case ARM_CPU_MODE_USR:
1294 case ARM_CPU_MODE_SYS:
1296 case ARM_CPU_MODE_SVC:
1298 case ARM_CPU_MODE_ABT:
1300 case ARM_CPU_MODE_UND:
1302 case ARM_CPU_MODE_IRQ:
1304 case ARM_CPU_MODE_FIQ:
1307 cpu_abort(env, "Bad mode %x\n", mode);
1311 void switch_mode(CPUARMState *env, int mode)
1316 old_mode = env->uncached_cpsr & CPSR_M;
1317 if (mode == old_mode)
1320 if (old_mode == ARM_CPU_MODE_FIQ) {
1321 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
1322 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
1323 } else if (mode == ARM_CPU_MODE_FIQ) {
1324 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
1325 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
1328 i = bank_number(env, old_mode);
1329 env->banked_r13[i] = env->regs[13];
1330 env->banked_r14[i] = env->regs[14];
1331 env->banked_spsr[i] = env->spsr;
1333 i = bank_number(env, mode);
1334 env->regs[13] = env->banked_r13[i];
1335 env->regs[14] = env->banked_r14[i];
1336 env->spsr = env->banked_spsr[i];
1339 static void v7m_push(CPUARMState *env, uint32_t val)
1342 stl_phys(env->regs[13], val);
1345 static uint32_t v7m_pop(CPUARMState *env)
1348 val = ldl_phys(env->regs[13]);
1353 /* Switch to V7M main or process stack pointer. */
1354 static void switch_v7m_sp(CPUARMState *env, int process)
1357 if (env->v7m.current_sp != process) {
1358 tmp = env->v7m.other_sp;
1359 env->v7m.other_sp = env->regs[13];
1360 env->regs[13] = tmp;
1361 env->v7m.current_sp = process;
1365 static void do_v7m_exception_exit(CPUARMState *env)
1370 type = env->regs[15];
1371 if (env->v7m.exception != 0)
1372 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
1374 /* Switch to the target stack. */
1375 switch_v7m_sp(env, (type & 4) != 0);
1376 /* Pop registers. */
1377 env->regs[0] = v7m_pop(env);
1378 env->regs[1] = v7m_pop(env);
1379 env->regs[2] = v7m_pop(env);
1380 env->regs[3] = v7m_pop(env);
1381 env->regs[12] = v7m_pop(env);
1382 env->regs[14] = v7m_pop(env);
1383 env->regs[15] = v7m_pop(env);
1384 xpsr = v7m_pop(env);
1385 xpsr_write(env, xpsr, 0xfffffdff);
1386 /* Undo stack alignment. */
1389 /* ??? The exception return type specifies Thread/Handler mode. However
1390 this is also implied by the xPSR value. Not sure what to do
1391 if there is a mismatch. */
1392 /* ??? Likewise for mismatches between the CONTROL register and the stack
1396 static void do_interrupt_v7m(CPUARMState *env)
1398 uint32_t xpsr = xpsr_read(env);
1403 if (env->v7m.current_sp)
1405 if (env->v7m.exception == 0)
1408 /* For exceptions we just mark as pending on the NVIC, and let that
1410 /* TODO: Need to escalate if the current priority is higher than the
1411 one we're raising. */
1412 switch (env->exception_index) {
1414 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
1418 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
1420 case EXCP_PREFETCH_ABORT:
1421 case EXCP_DATA_ABORT:
1422 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
1425 if (semihosting_enabled) {
1427 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
1430 env->regs[0] = do_arm_semihosting(env);
1434 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
1437 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
1439 case EXCP_EXCEPTION_EXIT:
1440 do_v7m_exception_exit(env);
1443 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
1444 return; /* Never happens. Keep compiler happy. */
1447 /* Align stack pointer. */
1448 /* ??? Should only do this if Configuration Control Register
1449 STACKALIGN bit is set. */
1450 if (env->regs[13] & 4) {
1454 /* Switch to the handler mode. */
1455 v7m_push(env, xpsr);
1456 v7m_push(env, env->regs[15]);
1457 v7m_push(env, env->regs[14]);
1458 v7m_push(env, env->regs[12]);
1459 v7m_push(env, env->regs[3]);
1460 v7m_push(env, env->regs[2]);
1461 v7m_push(env, env->regs[1]);
1462 v7m_push(env, env->regs[0]);
1463 switch_v7m_sp(env, 0);
1465 env->condexec_bits = 0;
1467 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
1468 env->regs[15] = addr & 0xfffffffe;
1469 env->thumb = addr & 1;
1472 /* Handle a CPU exception. */
1473 void do_interrupt(CPUARMState *env)
1481 do_interrupt_v7m(env);
1484 /* TODO: Vectored interrupt controller. */
1485 switch (env->exception_index) {
1487 new_mode = ARM_CPU_MODE_UND;
1496 if (semihosting_enabled) {
1497 /* Check for semihosting interrupt. */
1499 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
1501 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
1504 /* Only intercept calls from privileged modes, to provide some
1505 semblance of security. */
1506 if (((mask == 0x123456 && !env->thumb)
1507 || (mask == 0xab && env->thumb))
1508 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
1509 env->regs[0] = do_arm_semihosting(env);
1513 new_mode = ARM_CPU_MODE_SVC;
1516 /* The PC already points to the next instruction. */
1520 /* See if this is a semihosting syscall. */
1521 if (env->thumb && semihosting_enabled) {
1522 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
1524 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
1526 env->regs[0] = do_arm_semihosting(env);
1530 env->cp15.c5_insn = 2;
1531 /* Fall through to prefetch abort. */
1532 case EXCP_PREFETCH_ABORT:
1533 new_mode = ARM_CPU_MODE_ABT;
1535 mask = CPSR_A | CPSR_I;
1538 case EXCP_DATA_ABORT:
1539 new_mode = ARM_CPU_MODE_ABT;
1541 mask = CPSR_A | CPSR_I;
1545 new_mode = ARM_CPU_MODE_IRQ;
1547 /* Disable IRQ and imprecise data aborts. */
1548 mask = CPSR_A | CPSR_I;
1552 new_mode = ARM_CPU_MODE_FIQ;
1554 /* Disable FIQ, IRQ and imprecise data aborts. */
1555 mask = CPSR_A | CPSR_I | CPSR_F;
1559 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
1560 return; /* Never happens. Keep compiler happy. */
1563 if (env->cp15.c1_sys & (1 << 13)) {
1566 switch_mode (env, new_mode);
1567 env->spsr = cpsr_read(env);
1568 /* Clear IT bits. */
1569 env->condexec_bits = 0;
1570 /* Switch to the new mode, and to the correct instruction set. */
1571 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
1572 env->uncached_cpsr |= mask;
1573 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
1574 * and we should just guard the thumb mode on V4 */
1575 if (arm_feature(env, ARM_FEATURE_V4T)) {
1576 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
1578 env->regs[14] = env->regs[15] + offset;
1579 env->regs[15] = addr;
1580 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1583 /* Check section/page access permissions.
1584 Returns the page protection flags, or zero if the access is not
1586 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
1587 int access_type, int is_user)
1591 if (domain_prot == 3) {
1592 return PAGE_READ | PAGE_WRITE;
1595 if (access_type == 1)
1598 prot_ro = PAGE_READ;
1602 if (access_type == 1)
1604 switch ((env->cp15.c1_sys >> 8) & 3) {
1606 return is_user ? 0 : PAGE_READ;
1613 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
1618 return PAGE_READ | PAGE_WRITE;
1620 return PAGE_READ | PAGE_WRITE;
1621 case 4: /* Reserved. */
1624 return is_user ? 0 : prot_ro;
1628 if (!arm_feature (env, ARM_FEATURE_V6K))
1636 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
1640 if (address & env->cp15.c2_mask)
1641 table = env->cp15.c2_base1 & 0xffffc000;
1643 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
1645 table |= (address >> 18) & 0x3ffc;
1649 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
1650 int is_user, uint32_t *phys_ptr, int *prot,
1651 target_ulong *page_size)
1662 /* Pagetable walk. */
1663 /* Lookup l1 descriptor. */
1664 table = get_level1_table_address(env, address);
1665 desc = ldl_phys(table);
1667 domain = (desc >> 5) & 0x0f;
1668 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1670 /* Section translation fault. */
1674 if (domain_prot == 0 || domain_prot == 2) {
1676 code = 9; /* Section domain fault. */
1678 code = 11; /* Page domain fault. */
1683 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1684 ap = (desc >> 10) & 3;
1686 *page_size = 1024 * 1024;
1688 /* Lookup l2 entry. */
1690 /* Coarse pagetable. */
1691 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1693 /* Fine pagetable. */
1694 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1696 desc = ldl_phys(table);
1698 case 0: /* Page translation fault. */
1701 case 1: /* 64k page. */
1702 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1703 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1704 *page_size = 0x10000;
1706 case 2: /* 4k page. */
1707 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1708 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1709 *page_size = 0x1000;
1711 case 3: /* 1k page. */
1713 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1714 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1716 /* Page translation fault. */
1721 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1723 ap = (desc >> 4) & 3;
1727 /* Never happens, but compiler isn't smart enough to tell. */
1732 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1734 /* Access permission fault. */
1738 *phys_ptr = phys_addr;
1741 return code | (domain << 4);
1744 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
1745 int is_user, uint32_t *phys_ptr, int *prot,
1746 target_ulong *page_size)
1758 /* Pagetable walk. */
1759 /* Lookup l1 descriptor. */
1760 table = get_level1_table_address(env, address);
1761 desc = ldl_phys(table);
1764 /* Section translation fault. */
1768 } else if (type == 2 && (desc & (1 << 18))) {
1772 /* Section or page. */
1773 domain = (desc >> 5) & 0x0f;
1775 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1776 if (domain_prot == 0 || domain_prot == 2) {
1778 code = 9; /* Section domain fault. */
1780 code = 11; /* Page domain fault. */
1784 if (desc & (1 << 18)) {
1786 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1787 *page_size = 0x1000000;
1790 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1791 *page_size = 0x100000;
1793 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1794 xn = desc & (1 << 4);
1797 /* Lookup l2 entry. */
1798 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1799 desc = ldl_phys(table);
1800 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1802 case 0: /* Page translation fault. */
1805 case 1: /* 64k page. */
1806 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1807 xn = desc & (1 << 15);
1808 *page_size = 0x10000;
1810 case 2: case 3: /* 4k page. */
1811 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1813 *page_size = 0x1000;
1816 /* Never happens, but compiler isn't smart enough to tell. */
1821 if (domain_prot == 3) {
1822 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1824 if (xn && access_type == 2)
1827 /* The simplified model uses AP[0] as an access control bit. */
1828 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1829 /* Access flag fault. */
1830 code = (code == 15) ? 6 : 3;
1833 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1835 /* Access permission fault. */
1842 *phys_ptr = phys_addr;
1845 return code | (domain << 4);
1848 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
1849 int is_user, uint32_t *phys_ptr, int *prot)
1855 *phys_ptr = address;
1856 for (n = 7; n >= 0; n--) {
1857 base = env->cp15.c6_region[n];
1858 if ((base & 1) == 0)
1860 mask = 1 << ((base >> 1) & 0x1f);
1861 /* Keep this shift separate from the above to avoid an
1862 (undefined) << 32. */
1863 mask = (mask << 1) - 1;
1864 if (((base ^ address) & ~mask) == 0)
1870 if (access_type == 2) {
1871 mask = env->cp15.c5_insn;
1873 mask = env->cp15.c5_data;
1875 mask = (mask >> (n * 4)) & 0xf;
1882 *prot = PAGE_READ | PAGE_WRITE;
1887 *prot |= PAGE_WRITE;
1890 *prot = PAGE_READ | PAGE_WRITE;
1901 /* Bad permission. */
1908 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
1909 int access_type, int is_user,
1910 uint32_t *phys_ptr, int *prot,
1911 target_ulong *page_size)
1913 /* Fast Context Switch Extension. */
1914 if (address < 0x02000000)
1915 address += env->cp15.c13_fcse;
1917 if ((env->cp15.c1_sys & 1) == 0) {
1918 /* MMU/MPU disabled. */
1919 *phys_ptr = address;
1920 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1921 *page_size = TARGET_PAGE_SIZE;
1923 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1924 *page_size = TARGET_PAGE_SIZE;
1925 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1927 } else if (env->cp15.c1_sys & (1 << 23)) {
1928 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1931 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1936 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
1937 int access_type, int mmu_idx)
1940 target_ulong page_size;
1944 is_user = mmu_idx == MMU_USER_IDX;
1945 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1948 /* Map a single [sub]page. */
1949 phys_addr &= ~(uint32_t)0x3ff;
1950 address &= ~(uint32_t)0x3ff;
1951 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1955 if (access_type == 2) {
1956 env->cp15.c5_insn = ret;
1957 env->cp15.c6_insn = address;
1958 env->exception_index = EXCP_PREFETCH_ABORT;
1960 env->cp15.c5_data = ret;
1961 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1962 env->cp15.c5_data |= (1 << 11);
1963 env->cp15.c6_data = address;
1964 env->exception_index = EXCP_DATA_ABORT;
1969 target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
1972 target_ulong page_size;
1976 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1984 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1990 op1 = (insn >> 21) & 7;
1991 op2 = (insn >> 5) & 7;
1993 switch ((insn >> 16) & 0xf) {
1996 if (arm_feature(env, ARM_FEATURE_XSCALE))
1998 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2000 if (arm_feature(env, ARM_FEATURE_V7)
2001 && op1 == 2 && crm == 0 && op2 == 0) {
2002 env->cp15.c0_cssel = val & 0xf;
2006 case 4: /* Reserved. */
2008 case 12: /* Reserved. */
2013 /* ??? For debugging only. Should raise illegal instruction exception. */
2014 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
2015 (insn >> 16) & 0xf, crm, op1, op2);
2018 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
2024 op1 = (insn >> 21) & 7;
2025 op2 = (insn >> 5) & 7;
2027 switch ((insn >> 16) & 0xf) {
2028 case 0: /* ID codes. */
2034 case 0: /* Device ID. */
2035 return env->cp15.c0_cpuid;
2036 case 1: /* Cache Type. */
2037 return env->cp15.c0_cachetype;
2038 case 2: /* TCM status. */
2040 case 3: /* TLB type register. */
2041 return 0; /* No lockable TLB entries. */
2043 /* The MPIDR was standardised in v7; prior to
2044 * this it was implemented only in the 11MPCore.
2045 * For all other pre-v7 cores it does not exist.
2047 if (arm_feature(env, ARM_FEATURE_V7) ||
2048 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
2049 int mpidr = env->cpu_index;
2050 /* We don't support setting cluster ID ([8..11])
2051 * so these bits always RAZ.
2053 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2055 /* Cores which are uniprocessor (non-coherent)
2056 * but still implement the MP extensions set
2057 * bit 30. (For instance, A9UP.) However we do
2058 * not currently model any of those cores.
2063 /* otherwise fall through to the unimplemented-reg case */
2068 if (!arm_feature(env, ARM_FEATURE_V6))
2070 return env->cp15.c0_c1[op2];
2072 if (!arm_feature(env, ARM_FEATURE_V6))
2074 return env->cp15.c0_c2[op2];
2075 case 3: case 4: case 5: case 6: case 7:
2081 /* These registers aren't documented on arm11 cores. However
2082 Linux looks at them anyway. */
2083 if (!arm_feature(env, ARM_FEATURE_V6))
2087 if (!arm_feature(env, ARM_FEATURE_V7))
2092 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
2094 return env->cp15.c0_clid;
2100 if (op2 != 0 || crm != 0)
2102 return env->cp15.c0_cssel;
2106 case 4: /* Reserved. */
2108 case 11: /* TCM DMA control. */
2109 case 12: /* Reserved. */
2113 /* ??? For debugging only. Should raise illegal instruction exception. */
2114 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2115 (insn >> 16) & 0xf, crm, op1, op2);
2119 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2121 if ((env->uncached_cpsr & CPSR_M) == mode) {
2122 env->regs[13] = val;
2124 env->banked_r13[bank_number(env, mode)] = val;
2128 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2130 if ((env->uncached_cpsr & CPSR_M) == mode) {
2131 return env->regs[13];
2133 return env->banked_r13[bank_number(env, mode)];
2137 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2141 return xpsr_read(env) & 0xf8000000;
2143 return xpsr_read(env) & 0xf80001ff;
2145 return xpsr_read(env) & 0xff00fc00;
2147 return xpsr_read(env) & 0xff00fdff;
2149 return xpsr_read(env) & 0x000001ff;
2151 return xpsr_read(env) & 0x0700fc00;
2153 return xpsr_read(env) & 0x0700edff;
2155 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2157 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2158 case 16: /* PRIMASK */
2159 return (env->uncached_cpsr & CPSR_I) != 0;
2160 case 17: /* BASEPRI */
2161 case 18: /* BASEPRI_MAX */
2162 return env->v7m.basepri;
2163 case 19: /* FAULTMASK */
2164 return (env->uncached_cpsr & CPSR_F) != 0;
2165 case 20: /* CONTROL */
2166 return env->v7m.control;
2168 /* ??? For debugging only. */
2169 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2174 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2178 xpsr_write(env, val, 0xf8000000);
2181 xpsr_write(env, val, 0xf8000000);
2184 xpsr_write(env, val, 0xfe00fc00);
2187 xpsr_write(env, val, 0xfe00fc00);
2190 /* IPSR bits are readonly. */
2193 xpsr_write(env, val, 0x0600fc00);
2196 xpsr_write(env, val, 0x0600fc00);
2199 if (env->v7m.current_sp)
2200 env->v7m.other_sp = val;
2202 env->regs[13] = val;
2205 if (env->v7m.current_sp)
2206 env->regs[13] = val;
2208 env->v7m.other_sp = val;
2210 case 16: /* PRIMASK */
2212 env->uncached_cpsr |= CPSR_I;
2214 env->uncached_cpsr &= ~CPSR_I;
2216 case 17: /* BASEPRI */
2217 env->v7m.basepri = val & 0xff;
2219 case 18: /* BASEPRI_MAX */
2221 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2222 env->v7m.basepri = val;
2224 case 19: /* FAULTMASK */
2226 env->uncached_cpsr |= CPSR_F;
2228 env->uncached_cpsr &= ~CPSR_F;
2230 case 20: /* CONTROL */
2231 env->v7m.control = val & 3;
2232 switch_v7m_sp(env, (val & 2) != 0);
2235 /* ??? For debugging only. */
2236 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2243 /* Note that signed overflow is undefined in C. The following routines are
2244 careful to use unsigned types where modulo arithmetic is required.
2245 Failure to do so _will_ break on newer gcc. */
2247 /* Signed saturating arithmetic. */
2249 /* Perform 16-bit signed saturating addition. */
2250 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2255 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2264 /* Perform 8-bit signed saturating addition. */
2265 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2270 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2279 /* Perform 16-bit signed saturating subtraction. */
2280 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2285 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2294 /* Perform 8-bit signed saturating subtraction. */
2295 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2300 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2309 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2310 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2311 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2312 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2315 #include "op_addsub.h"
2317 /* Unsigned saturating arithmetic. */
2318 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2327 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2335 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2344 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2352 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2353 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2354 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2355 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2358 #include "op_addsub.h"
2360 /* Signed modulo arithmetic. */
2361 #define SARITH16(a, b, n, op) do { \
2363 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2364 RESULT(sum, n, 16); \
2366 ge |= 3 << (n * 2); \
2369 #define SARITH8(a, b, n, op) do { \
2371 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2372 RESULT(sum, n, 8); \
2378 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2379 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2380 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2381 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2385 #include "op_addsub.h"
2387 /* Unsigned modulo arithmetic. */
2388 #define ADD16(a, b, n) do { \
2390 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2391 RESULT(sum, n, 16); \
2392 if ((sum >> 16) == 1) \
2393 ge |= 3 << (n * 2); \
2396 #define ADD8(a, b, n) do { \
2398 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2399 RESULT(sum, n, 8); \
2400 if ((sum >> 8) == 1) \
2404 #define SUB16(a, b, n) do { \
2406 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2407 RESULT(sum, n, 16); \
2408 if ((sum >> 16) == 0) \
2409 ge |= 3 << (n * 2); \
2412 #define SUB8(a, b, n) do { \
2414 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2415 RESULT(sum, n, 8); \
2416 if ((sum >> 8) == 0) \
2423 #include "op_addsub.h"
2425 /* Halved signed arithmetic. */
2426 #define ADD16(a, b, n) \
2427 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2428 #define SUB16(a, b, n) \
2429 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2430 #define ADD8(a, b, n) \
2431 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2432 #define SUB8(a, b, n) \
2433 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2436 #include "op_addsub.h"
2438 /* Halved unsigned arithmetic. */
2439 #define ADD16(a, b, n) \
2440 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2441 #define SUB16(a, b, n) \
2442 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2443 #define ADD8(a, b, n) \
2444 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2445 #define SUB8(a, b, n) \
2446 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2449 #include "op_addsub.h"
2451 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2459 /* Unsigned sum of absolute byte differences. */
2460 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2463 sum = do_usad(a, b);
2464 sum += do_usad(a >> 8, b >> 8);
2465 sum += do_usad(a >> 16, b >>16);
2466 sum += do_usad(a >> 24, b >> 24);
2470 /* For ARMv6 SEL instruction. */
2471 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2484 return (a & mask) | (b & ~mask);
2487 uint32_t HELPER(logicq_cc)(uint64_t val)
2489 return (val >> 32) | (val != 0);
2492 /* VFP support. We follow the convention used for VFP instrunctions:
2493 Single precition routines have a "s" suffix, double precision a
2496 /* Convert host exception flags to vfp form. */
2497 static inline int vfp_exceptbits_from_host(int host_bits)
2499 int target_bits = 0;
2501 if (host_bits & float_flag_invalid)
2503 if (host_bits & float_flag_divbyzero)
2505 if (host_bits & float_flag_overflow)
2507 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
2509 if (host_bits & float_flag_inexact)
2510 target_bits |= 0x10;
2511 if (host_bits & float_flag_input_denormal)
2512 target_bits |= 0x80;
2516 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2521 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2522 | (env->vfp.vec_len << 16)
2523 | (env->vfp.vec_stride << 20);
2524 i = get_float_exception_flags(&env->vfp.fp_status);
2525 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
2526 fpscr |= vfp_exceptbits_from_host(i);
2530 uint32_t vfp_get_fpscr(CPUARMState *env)
2532 return HELPER(vfp_get_fpscr)(env);
2535 /* Convert vfp exception flags to target form. */
2536 static inline int vfp_exceptbits_to_host(int target_bits)
2540 if (target_bits & 1)
2541 host_bits |= float_flag_invalid;
2542 if (target_bits & 2)
2543 host_bits |= float_flag_divbyzero;
2544 if (target_bits & 4)
2545 host_bits |= float_flag_overflow;
2546 if (target_bits & 8)
2547 host_bits |= float_flag_underflow;
2548 if (target_bits & 0x10)
2549 host_bits |= float_flag_inexact;
2550 if (target_bits & 0x80)
2551 host_bits |= float_flag_input_denormal;
2555 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
2560 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2561 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2562 env->vfp.vec_len = (val >> 16) & 7;
2563 env->vfp.vec_stride = (val >> 20) & 3;
2566 if (changed & (3 << 22)) {
2567 i = (val >> 22) & 3;
2570 i = float_round_nearest_even;
2576 i = float_round_down;
2579 i = float_round_to_zero;
2582 set_float_rounding_mode(i, &env->vfp.fp_status);
2584 if (changed & (1 << 24)) {
2585 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2586 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2588 if (changed & (1 << 25))
2589 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2591 i = vfp_exceptbits_to_host(val);
2592 set_float_exception_flags(i, &env->vfp.fp_status);
2593 set_float_exception_flags(0, &env->vfp.standard_fp_status);
2596 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
2598 HELPER(vfp_set_fpscr)(env, val);
2601 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2603 #define VFP_BINOP(name) \
2604 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2606 float_status *fpst = fpstp; \
2607 return float32_ ## name(a, b, fpst); \
2609 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2611 float_status *fpst = fpstp; \
2612 return float64_ ## name(a, b, fpst); \
2620 float32 VFP_HELPER(neg, s)(float32 a)
2622 return float32_chs(a);
2625 float64 VFP_HELPER(neg, d)(float64 a)
2627 return float64_chs(a);
2630 float32 VFP_HELPER(abs, s)(float32 a)
2632 return float32_abs(a);
2635 float64 VFP_HELPER(abs, d)(float64 a)
2637 return float64_abs(a);
2640 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
2642 return float32_sqrt(a, &env->vfp.fp_status);
2645 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
2647 return float64_sqrt(a, &env->vfp.fp_status);
2650 /* XXX: check quiet/signaling case */
2651 #define DO_VFP_cmp(p, type) \
2652 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
2655 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2656 case 0: flags = 0x6; break; \
2657 case -1: flags = 0x8; break; \
2658 case 1: flags = 0x2; break; \
2659 default: case 2: flags = 0x3; break; \
2661 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2662 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2664 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
2667 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2668 case 0: flags = 0x6; break; \
2669 case -1: flags = 0x8; break; \
2670 case 1: flags = 0x2; break; \
2671 default: case 2: flags = 0x3; break; \
2673 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2674 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2676 DO_VFP_cmp(s, float32)
2677 DO_VFP_cmp(d, float64)
2680 /* Integer to float and float to integer conversions */
2682 #define CONV_ITOF(name, fsz, sign) \
2683 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2685 float_status *fpst = fpstp; \
2686 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
2689 #define CONV_FTOI(name, fsz, sign, round) \
2690 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2692 float_status *fpst = fpstp; \
2693 if (float##fsz##_is_any_nan(x)) { \
2694 float_raise(float_flag_invalid, fpst); \
2697 return float##fsz##_to_##sign##int32##round(x, fpst); \
2700 #define FLOAT_CONVS(name, p, fsz, sign) \
2701 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2702 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2703 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2705 FLOAT_CONVS(si, s, 32, )
2706 FLOAT_CONVS(si, d, 64, )
2707 FLOAT_CONVS(ui, s, 32, u)
2708 FLOAT_CONVS(ui, d, 64, u)
2714 /* floating point conversion */
2715 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
2717 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2718 /* ARM requires that S<->D conversion of any kind of NaN generates
2719 * a quiet NaN by forcing the most significant frac bit to 1.
2721 return float64_maybe_silence_nan(r);
2724 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
2726 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2727 /* ARM requires that S<->D conversion of any kind of NaN generates
2728 * a quiet NaN by forcing the most significant frac bit to 1.
2730 return float32_maybe_silence_nan(r);
2733 /* VFP3 fixed point conversion. */
2734 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2735 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2738 float_status *fpst = fpstp; \
2740 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2741 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2743 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2746 float_status *fpst = fpstp; \
2748 if (float##fsz##_is_any_nan(x)) { \
2749 float_raise(float_flag_invalid, fpst); \
2752 tmp = float##fsz##_scalbn(x, shift, fpst); \
2753 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2756 VFP_CONV_FIX(sh, d, 64, int16, )
2757 VFP_CONV_FIX(sl, d, 64, int32, )
2758 VFP_CONV_FIX(uh, d, 64, uint16, u)
2759 VFP_CONV_FIX(ul, d, 64, uint32, u)
2760 VFP_CONV_FIX(sh, s, 32, int16, )
2761 VFP_CONV_FIX(sl, s, 32, int32, )
2762 VFP_CONV_FIX(uh, s, 32, uint16, u)
2763 VFP_CONV_FIX(ul, s, 32, uint32, u)
2766 /* Half precision conversions. */
2767 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
2769 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2770 float32 r = float16_to_float32(make_float16(a), ieee, s);
2772 return float32_maybe_silence_nan(r);
2777 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
2779 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2780 float16 r = float32_to_float16(a, ieee, s);
2782 r = float16_maybe_silence_nan(r);
2784 return float16_val(r);
2787 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2789 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2792 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2794 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2797 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2799 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2802 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2804 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2807 #define float32_two make_float32(0x40000000)
2808 #define float32_three make_float32(0x40400000)
2809 #define float32_one_point_five make_float32(0x3fc00000)
2811 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
2813 float_status *s = &env->vfp.standard_fp_status;
2814 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2815 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2816 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2817 float_raise(float_flag_input_denormal, s);
2821 return float32_sub(float32_two, float32_mul(a, b, s), s);
2824 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
2826 float_status *s = &env->vfp.standard_fp_status;
2828 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2829 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2830 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2831 float_raise(float_flag_input_denormal, s);
2833 return float32_one_point_five;
2835 product = float32_mul(a, b, s);
2836 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
2841 /* Constants 256 and 512 are used in some helpers; we avoid relying on
2842 * int->float conversions at run-time. */
2843 #define float64_256 make_float64(0x4070000000000000LL)
2844 #define float64_512 make_float64(0x4080000000000000LL)
2846 /* The algorithm that must be used to calculate the estimate
2847 * is specified by the ARM ARM.
2849 static float64 recip_estimate(float64 a, CPUARMState *env)
2851 /* These calculations mustn't set any fp exception flags,
2852 * so we use a local copy of the fp_status.
2854 float_status dummy_status = env->vfp.standard_fp_status;
2855 float_status *s = &dummy_status;
2856 /* q = (int)(a * 512.0) */
2857 float64 q = float64_mul(float64_512, a, s);
2858 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2860 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2861 q = int64_to_float64(q_int, s);
2862 q = float64_add(q, float64_half, s);
2863 q = float64_div(q, float64_512, s);
2864 q = float64_div(float64_one, q, s);
2866 /* s = (int)(256.0 * r + 0.5) */
2867 q = float64_mul(q, float64_256, s);
2868 q = float64_add(q, float64_half, s);
2869 q_int = float64_to_int64_round_to_zero(q, s);
2871 /* return (double)s / 256.0 */
2872 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2875 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
2877 float_status *s = &env->vfp.standard_fp_status;
2879 uint32_t val32 = float32_val(a);
2882 int a_exp = (val32 & 0x7f800000) >> 23;
2883 int sign = val32 & 0x80000000;
2885 if (float32_is_any_nan(a)) {
2886 if (float32_is_signaling_nan(a)) {
2887 float_raise(float_flag_invalid, s);
2889 return float32_default_nan;
2890 } else if (float32_is_infinity(a)) {
2891 return float32_set_sign(float32_zero, float32_is_neg(a));
2892 } else if (float32_is_zero_or_denormal(a)) {
2893 if (!float32_is_zero(a)) {
2894 float_raise(float_flag_input_denormal, s);
2896 float_raise(float_flag_divbyzero, s);
2897 return float32_set_sign(float32_infinity, float32_is_neg(a));
2898 } else if (a_exp >= 253) {
2899 float_raise(float_flag_underflow, s);
2900 return float32_set_sign(float32_zero, float32_is_neg(a));
2903 f64 = make_float64((0x3feULL << 52)
2904 | ((int64_t)(val32 & 0x7fffff) << 29));
2906 result_exp = 253 - a_exp;
2908 f64 = recip_estimate(f64, env);
2911 | ((result_exp & 0xff) << 23)
2912 | ((float64_val(f64) >> 29) & 0x7fffff);
2913 return make_float32(val32);
2916 /* The algorithm that must be used to calculate the estimate
2917 * is specified by the ARM ARM.
2919 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
2921 /* These calculations mustn't set any fp exception flags,
2922 * so we use a local copy of the fp_status.
2924 float_status dummy_status = env->vfp.standard_fp_status;
2925 float_status *s = &dummy_status;
2929 if (float64_lt(a, float64_half, s)) {
2930 /* range 0.25 <= a < 0.5 */
2932 /* a in units of 1/512 rounded down */
2933 /* q0 = (int)(a * 512.0); */
2934 q = float64_mul(float64_512, a, s);
2935 q_int = float64_to_int64_round_to_zero(q, s);
2937 /* reciprocal root r */
2938 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2939 q = int64_to_float64(q_int, s);
2940 q = float64_add(q, float64_half, s);
2941 q = float64_div(q, float64_512, s);
2942 q = float64_sqrt(q, s);
2943 q = float64_div(float64_one, q, s);
2945 /* range 0.5 <= a < 1.0 */
2947 /* a in units of 1/256 rounded down */
2948 /* q1 = (int)(a * 256.0); */
2949 q = float64_mul(float64_256, a, s);
2950 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2952 /* reciprocal root r */
2953 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2954 q = int64_to_float64(q_int, s);
2955 q = float64_add(q, float64_half, s);
2956 q = float64_div(q, float64_256, s);
2957 q = float64_sqrt(q, s);
2958 q = float64_div(float64_one, q, s);
2960 /* r in units of 1/256 rounded to nearest */
2961 /* s = (int)(256.0 * r + 0.5); */
2963 q = float64_mul(q, float64_256,s );
2964 q = float64_add(q, float64_half, s);
2965 q_int = float64_to_int64_round_to_zero(q, s);
2967 /* return (double)s / 256.0;*/
2968 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2971 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
2973 float_status *s = &env->vfp.standard_fp_status;
2979 val = float32_val(a);
2981 if (float32_is_any_nan(a)) {
2982 if (float32_is_signaling_nan(a)) {
2983 float_raise(float_flag_invalid, s);
2985 return float32_default_nan;
2986 } else if (float32_is_zero_or_denormal(a)) {
2987 if (!float32_is_zero(a)) {
2988 float_raise(float_flag_input_denormal, s);
2990 float_raise(float_flag_divbyzero, s);
2991 return float32_set_sign(float32_infinity, float32_is_neg(a));
2992 } else if (float32_is_neg(a)) {
2993 float_raise(float_flag_invalid, s);
2994 return float32_default_nan;
2995 } else if (float32_is_infinity(a)) {
2996 return float32_zero;
2999 /* Normalize to a double-precision value between 0.25 and 1.0,
3000 * preserving the parity of the exponent. */
3001 if ((val & 0x800000) == 0) {
3002 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3004 | ((uint64_t)(val & 0x7fffff) << 29));
3006 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3008 | ((uint64_t)(val & 0x7fffff) << 29));
3011 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3013 f64 = recip_sqrt_estimate(f64, env);
3015 val64 = float64_val(f64);
3017 val = ((result_exp & 0xff) << 23)
3018 | ((val64 >> 29) & 0x7fffff);
3019 return make_float32(val);
3022 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
3026 if ((a & 0x80000000) == 0) {
3030 f64 = make_float64((0x3feULL << 52)
3031 | ((int64_t)(a & 0x7fffffff) << 21));
3033 f64 = recip_estimate (f64, env);
3035 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3038 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
3042 if ((a & 0xc0000000) == 0) {
3046 if (a & 0x80000000) {
3047 f64 = make_float64((0x3feULL << 52)
3048 | ((uint64_t)(a & 0x7fffffff) << 21));
3049 } else { /* bits 31-30 == '01' */
3050 f64 = make_float64((0x3fdULL << 52)
3051 | ((uint64_t)(a & 0x3fffffff) << 22));
3054 f64 = recip_sqrt_estimate(f64, env);
3056 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3059 /* VFPv4 fused multiply-accumulate */
3060 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3062 float_status *fpst = fpstp;
3063 return float32_muladd(a, b, c, 0, fpst);
3066 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3068 float_status *fpst = fpstp;
3069 return float64_muladd(a, b, c, 0, fpst);