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 },
189 static const ARMCPRegInfo v6_cp_reginfo[] = {
190 /* prefetch by MVA in v6, NOP in v7 */
191 { .name = "MVA_prefetch",
192 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
193 .access = PL1_W, .type = ARM_CP_NOP },
194 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
195 .access = PL0_W, .type = ARM_CP_NOP },
196 { .name = "ISB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
197 .access = PL0_W, .type = ARM_CP_NOP },
198 { .name = "ISB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
199 .access = PL0_W, .type = ARM_CP_NOP },
203 static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
206 /* Generic performance monitor register read function for where
207 * user access may be allowed by PMUSERENR.
209 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
212 *value = CPREG_FIELD32(env, ri);
216 static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
219 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
222 /* only the DP, X, D and E bits are writable */
223 env->cp15.c9_pmcr &= ~0x39;
224 env->cp15.c9_pmcr |= (value & 0x39);
228 static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
231 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
235 env->cp15.c9_pmcnten |= value;
239 static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
242 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
246 env->cp15.c9_pmcnten &= ~value;
250 static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
253 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
256 env->cp15.c9_pmovsr &= ~value;
260 static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
263 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
266 env->cp15.c9_pmxevtyper = value & 0xff;
270 static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
273 env->cp15.c9_pmuserenr = value & 1;
277 static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
280 /* We have no event counters so only the C bit can be changed */
282 env->cp15.c9_pminten |= value;
286 static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
290 env->cp15.c9_pminten &= ~value;
294 static const ARMCPRegInfo v7_cp_reginfo[] = {
295 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
298 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
299 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
300 { .name = "DBGDRAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
301 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
302 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
303 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
304 .access = PL1_W, .type = ARM_CP_NOP },
305 /* Performance monitors are implementation defined in v7,
306 * but with an ARM recommended set of registers, which we
307 * follow (although we don't actually implement any counters)
309 * Performance registers fall into three categories:
310 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
311 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
312 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
313 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
314 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
316 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
317 .access = PL0_RW, .resetvalue = 0,
318 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
319 .readfn = pmreg_read, .writefn = pmcntenset_write },
320 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
321 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
322 .readfn = pmreg_read, .writefn = pmcntenclr_write },
323 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
324 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
325 .readfn = pmreg_read, .writefn = pmovsr_write },
326 /* Unimplemented so WI. Strictly speaking write accesses in PL0 should
329 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
330 .access = PL0_W, .type = ARM_CP_NOP },
331 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
332 * We choose to RAZ/WI. XXX should respect PMUSERENR.
334 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
335 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
336 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
337 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
338 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
339 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
341 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
342 .readfn = pmreg_read, .writefn = pmxevtyper_write },
343 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
344 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
345 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
346 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
347 .access = PL0_R | PL1_RW,
348 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
350 .writefn = pmuserenr_write },
351 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
353 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
355 .writefn = pmintenset_write },
356 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
358 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
360 .writefn = pmintenclr_write },
364 static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
371 static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
374 /* This is a helper function because the user access rights
375 * depend on the value of the TEECR.
377 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
380 *value = env->teehbr;
384 static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
387 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
394 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
395 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
396 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
398 .writefn = teecr_write },
399 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
400 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
402 .readfn = teehbr_read, .writefn = teehbr_write },
406 static const ARMCPRegInfo v6k_cp_reginfo[] = {
407 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
409 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
411 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
412 .access = PL0_R|PL1_W,
413 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
415 { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
417 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
422 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
423 /* Dummy implementation: RAZ/WI the whole crn=14 space */
424 { .name = "GENERIC_TIMER", .cp = 15, .crn = 14,
425 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
426 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
430 static int par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
432 if (arm_feature(env, ARM_FEATURE_V7)) {
433 env->cp15.c7_par = value & 0xfffff6ff;
435 env->cp15.c7_par = value & 0xfffff1ff;
440 #ifndef CONFIG_USER_ONLY
441 /* get_phys_addr() isn't present for user-mode-only targets */
442 static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
445 target_ulong page_size;
447 int ret, is_user = ri->opc2 & 2;
448 int access_type = ri->opc2 & 1;
451 /* Other states are only available with TrustZone */
454 ret = get_phys_addr(env, value, access_type, is_user,
455 &phys_addr, &prot, &page_size);
457 /* We do not set any attribute bits in the PAR */
458 if (page_size == (1 << 24)
459 && arm_feature(env, ARM_FEATURE_V7)) {
460 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
462 env->cp15.c7_par = phys_addr & 0xfffff000;
465 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
466 ((ret & (12 << 1)) >> 6) |
467 ((ret & 0xf) << 1) | 1;
473 static const ARMCPRegInfo vapa_cp_reginfo[] = {
474 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
475 .access = PL1_RW, .resetvalue = 0,
476 .fieldoffset = offsetof(CPUARMState, cp15.c7_par),
477 .writefn = par_write },
478 #ifndef CONFIG_USER_ONLY
479 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
480 .access = PL1_W, .writefn = ats_write },
485 /* Return basic MPU access permission bits. */
486 static uint32_t simple_mpu_ap_bits(uint32_t val)
493 for (i = 0; i < 16; i += 2) {
494 ret |= (val >> i) & mask;
500 /* Pad basic MPU access permission bits to extended format. */
501 static uint32_t extended_mpu_ap_bits(uint32_t val)
508 for (i = 0; i < 16; i += 2) {
509 ret |= (val & mask) << i;
515 static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
518 env->cp15.c5_data = extended_mpu_ap_bits(value);
522 static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
525 *value = simple_mpu_ap_bits(env->cp15.c5_data);
529 static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
532 env->cp15.c5_insn = extended_mpu_ap_bits(value);
536 static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
539 *value = simple_mpu_ap_bits(env->cp15.c5_insn);
543 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
544 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
546 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
547 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
548 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
550 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
551 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
552 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
554 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
555 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
557 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
558 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
560 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
561 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
563 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
567 static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
571 env->cp15.c2_control = value;
572 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> value);
573 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> value);
577 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
579 env->cp15.c2_base_mask = 0xffffc000u;
580 env->cp15.c2_control = 0;
581 env->cp15.c2_mask = 0;
584 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
585 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
587 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
588 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
590 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
591 { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
593 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
594 { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
596 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
597 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
598 .access = PL1_RW, .writefn = vmsa_ttbcr_write,
599 .resetfn = vmsa_ttbcr_reset,
600 .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
604 static int omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
607 env->cp15.c15_ticonfig = value & 0xe7;
608 /* The OS_TYPE bit in this register changes the reported CPUID! */
609 env->cp15.c0_cpuid = (value & (1 << 5)) ?
610 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
614 static int omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
617 env->cp15.c15_threadid = value & 0xffff;
621 static int omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
624 /* Wait-for-interrupt (deprecated) */
625 cpu_interrupt(env, CPU_INTERRUPT_HALT);
629 static int omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
632 /* On OMAP there are registers indicating the max/min index of dcache lines
633 * containing a dirty line; cache flush operations have to reset these.
635 env->cp15.c15_i_max = 0x000;
636 env->cp15.c15_i_min = 0xff0;
640 static const ARMCPRegInfo omap_cp_reginfo[] = {
641 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
642 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
643 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
644 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
645 .access = PL1_RW, .type = ARM_CP_NOP },
646 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
648 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
649 .writefn = omap_ticonfig_write },
650 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
652 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
653 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
654 .access = PL1_RW, .resetvalue = 0xff0,
655 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
656 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
658 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
659 .writefn = omap_threadid_write },
660 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
661 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
662 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
663 /* TODO: Peripheral port remap register:
664 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
665 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
668 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
669 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, .type = ARM_CP_OVERRIDE,
670 .writefn = omap_cachemaint_write },
674 static int xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
678 if (env->cp15.c15_cpar != value) {
679 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
681 env->cp15.c15_cpar = value;
686 static const ARMCPRegInfo xscale_cp_reginfo[] = {
687 { .name = "XSCALE_CPAR",
688 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
689 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
690 .writefn = xscale_cpar_write, },
694 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
695 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
696 * implementation of this implementation-defined space.
697 * Ideally this should eventually disappear in favour of actually
698 * implementing the correct behaviour for all cores.
700 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
701 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
702 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
706 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
707 /* Cache status: RAZ because we have no cache so it's always clean */
708 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
709 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
713 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
714 /* We never have a a block transfer operation in progress */
715 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
716 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
720 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
721 /* The cache test-and-clean instructions always return (1 << 30)
722 * to indicate that there are no dirty cache lines.
724 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
725 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = (1 << 30) },
726 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
727 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = (1 << 30) },
731 void register_cp_regs_for_features(ARMCPU *cpu)
733 /* Register all the coprocessor registers based on feature bits */
734 CPUARMState *env = &cpu->env;
735 if (arm_feature(env, ARM_FEATURE_M)) {
736 /* M profile has no coprocessor registers */
740 define_arm_cp_regs(cpu, cp_reginfo);
741 if (arm_feature(env, ARM_FEATURE_V6)) {
742 define_arm_cp_regs(cpu, v6_cp_reginfo);
744 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
746 if (arm_feature(env, ARM_FEATURE_V6K)) {
747 define_arm_cp_regs(cpu, v6k_cp_reginfo);
749 if (arm_feature(env, ARM_FEATURE_V7)) {
750 /* v7 performance monitor control register: same implementor
751 * field as main ID register, and we implement no event counters.
753 ARMCPRegInfo pmcr = {
754 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
755 .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
756 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
757 .readfn = pmreg_read, .writefn = pmcr_write
759 define_one_arm_cp_reg(cpu, &pmcr);
760 define_arm_cp_regs(cpu, v7_cp_reginfo);
762 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
764 if (arm_feature(env, ARM_FEATURE_MPU)) {
765 /* These are the MPU registers prior to PMSAv6. Any new
766 * PMSA core later than the ARM946 will require that we
767 * implement the PMSAv6 or PMSAv7 registers, which are
768 * completely different.
770 assert(!arm_feature(env, ARM_FEATURE_V6));
771 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
773 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
775 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
776 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
778 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
779 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
781 if (arm_feature(env, ARM_FEATURE_VAPA)) {
782 define_arm_cp_regs(cpu, vapa_cp_reginfo);
784 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
785 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
787 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
788 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
790 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
791 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
793 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
794 define_arm_cp_regs(cpu, omap_cp_reginfo);
796 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
797 define_arm_cp_regs(cpu, xscale_cp_reginfo);
799 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
800 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
804 ARMCPU *cpu_arm_init(const char *cpu_model)
808 static int inited = 0;
810 if (!object_class_by_name(cpu_model)) {
813 cpu = ARM_CPU(object_new(cpu_model));
815 env->cpu_model_str = cpu_model;
816 arm_cpu_realize(cpu);
818 if (tcg_enabled() && !inited) {
820 arm_translate_init();
824 if (arm_feature(env, ARM_FEATURE_NEON)) {
825 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
826 51, "arm-neon.xml", 0);
827 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
828 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
829 35, "arm-vfp3.xml", 0);
830 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
831 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
832 19, "arm-vfp.xml", 0);
838 typedef struct ARMCPUListState {
839 fprintf_function cpu_fprintf;
843 /* Sort alphabetically by type name, except for "any". */
844 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
846 ObjectClass *class_a = (ObjectClass *)a;
847 ObjectClass *class_b = (ObjectClass *)b;
848 const char *name_a, *name_b;
850 name_a = object_class_get_name(class_a);
851 name_b = object_class_get_name(class_b);
852 if (strcmp(name_a, "any") == 0) {
854 } else if (strcmp(name_b, "any") == 0) {
857 return strcmp(name_a, name_b);
861 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
863 ObjectClass *oc = data;
864 ARMCPUListState *s = user_data;
866 (*s->cpu_fprintf)(s->file, " %s\n",
867 object_class_get_name(oc));
870 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
872 ARMCPUListState s = {
874 .cpu_fprintf = cpu_fprintf,
878 list = object_class_get_list(TYPE_ARM_CPU, false);
879 list = g_slist_sort(list, arm_cpu_list_compare);
880 (*cpu_fprintf)(f, "Available CPUs:\n");
881 g_slist_foreach(list, arm_cpu_list_entry, &s);
885 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
886 const ARMCPRegInfo *r, void *opaque)
888 /* Define implementations of coprocessor registers.
889 * We store these in a hashtable because typically
890 * there are less than 150 registers in a space which
891 * is 16*16*16*8*8 = 262144 in size.
892 * Wildcarding is supported for the crm, opc1 and opc2 fields.
893 * If a register is defined twice then the second definition is
894 * used, so this can be used to define some generic registers and
895 * then override them with implementation specific variations.
896 * At least one of the original and the second definition should
897 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
898 * against accidental use.
901 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
902 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
903 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
904 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
905 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
906 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
907 /* 64 bit registers have only CRm and Opc1 fields */
908 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
909 /* Check that the register definition has enough info to handle
910 * reads and writes if they are permitted.
912 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
913 if (r->access & PL3_R) {
914 assert(r->fieldoffset || r->readfn);
916 if (r->access & PL3_W) {
917 assert(r->fieldoffset || r->writefn);
920 /* Bad type field probably means missing sentinel at end of reg list */
921 assert(cptype_valid(r->type));
922 for (crm = crmmin; crm <= crmmax; crm++) {
923 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
924 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
925 uint32_t *key = g_new(uint32_t, 1);
926 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
927 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
928 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
930 /* Make sure reginfo passed to helpers for wildcarded regs
931 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
936 /* Overriding of an existing definition must be explicitly
939 if (!(r->type & ARM_CP_OVERRIDE)) {
940 ARMCPRegInfo *oldreg;
941 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
942 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
943 fprintf(stderr, "Register redefined: cp=%d %d bit "
944 "crn=%d crm=%d opc1=%d opc2=%d, "
945 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
946 r2->crn, r2->crm, r2->opc1, r2->opc2,
947 oldreg->name, r2->name);
951 g_hash_table_insert(cpu->cp_regs, key, r2);
957 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
958 const ARMCPRegInfo *regs, void *opaque)
960 /* Define a whole list of registers */
961 const ARMCPRegInfo *r;
962 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
963 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
967 const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
969 return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
972 int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
975 /* Helper coprocessor write function for write-ignore registers */
979 int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
981 /* Helper coprocessor write function for read-as-zero registers */
986 static int bad_mode_switch(CPUARMState *env, int mode)
988 /* Return true if it is not valid for us to switch to
989 * this CPU mode (ie all the UNPREDICTABLE cases in
990 * the ARM ARM CPSRWriteByInstr pseudocode).
993 case ARM_CPU_MODE_USR:
994 case ARM_CPU_MODE_SYS:
995 case ARM_CPU_MODE_SVC:
996 case ARM_CPU_MODE_ABT:
997 case ARM_CPU_MODE_UND:
998 case ARM_CPU_MODE_IRQ:
999 case ARM_CPU_MODE_FIQ:
1006 uint32_t cpsr_read(CPUARMState *env)
1009 ZF = (env->ZF == 0);
1010 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
1011 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
1012 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
1013 | ((env->condexec_bits & 0xfc) << 8)
1017 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
1019 if (mask & CPSR_NZCV) {
1020 env->ZF = (~val) & CPSR_Z;
1022 env->CF = (val >> 29) & 1;
1023 env->VF = (val << 3) & 0x80000000;
1026 env->QF = ((val & CPSR_Q) != 0);
1028 env->thumb = ((val & CPSR_T) != 0);
1029 if (mask & CPSR_IT_0_1) {
1030 env->condexec_bits &= ~3;
1031 env->condexec_bits |= (val >> 25) & 3;
1033 if (mask & CPSR_IT_2_7) {
1034 env->condexec_bits &= 3;
1035 env->condexec_bits |= (val >> 8) & 0xfc;
1037 if (mask & CPSR_GE) {
1038 env->GE = (val >> 16) & 0xf;
1041 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
1042 if (bad_mode_switch(env, val & CPSR_M)) {
1043 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
1044 * We choose to ignore the attempt and leave the CPSR M field
1049 switch_mode(env, val & CPSR_M);
1052 mask &= ~CACHED_CPSR_BITS;
1053 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
1056 /* Sign/zero extend */
1057 uint32_t HELPER(sxtb16)(uint32_t x)
1060 res = (uint16_t)(int8_t)x;
1061 res |= (uint32_t)(int8_t)(x >> 16) << 16;
1065 uint32_t HELPER(uxtb16)(uint32_t x)
1068 res = (uint16_t)(uint8_t)x;
1069 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
1073 uint32_t HELPER(clz)(uint32_t x)
1078 int32_t HELPER(sdiv)(int32_t num, int32_t den)
1082 if (num == INT_MIN && den == -1)
1087 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
1094 uint32_t HELPER(rbit)(uint32_t x)
1096 x = ((x & 0xff000000) >> 24)
1097 | ((x & 0x00ff0000) >> 8)
1098 | ((x & 0x0000ff00) << 8)
1099 | ((x & 0x000000ff) << 24);
1100 x = ((x & 0xf0f0f0f0) >> 4)
1101 | ((x & 0x0f0f0f0f) << 4);
1102 x = ((x & 0x88888888) >> 3)
1103 | ((x & 0x44444444) >> 1)
1104 | ((x & 0x22222222) << 1)
1105 | ((x & 0x11111111) << 3);
1109 uint32_t HELPER(abs)(uint32_t x)
1111 return ((int32_t)x < 0) ? -x : x;
1114 #if defined(CONFIG_USER_ONLY)
1116 void do_interrupt (CPUARMState *env)
1118 env->exception_index = -1;
1121 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
1125 env->exception_index = EXCP_PREFETCH_ABORT;
1126 env->cp15.c6_insn = address;
1128 env->exception_index = EXCP_DATA_ABORT;
1129 env->cp15.c6_data = address;
1134 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1136 cpu_abort(env, "cp15 insn %08x\n", insn);
1139 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
1141 cpu_abort(env, "cp15 insn %08x\n", insn);
1144 /* These should probably raise undefined insn exceptions. */
1145 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
1147 cpu_abort(env, "v7m_mrs %d\n", reg);
1150 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
1152 cpu_abort(env, "v7m_mrs %d\n", reg);
1156 void switch_mode(CPUARMState *env, int mode)
1158 if (mode != ARM_CPU_MODE_USR)
1159 cpu_abort(env, "Tried to switch out of user mode\n");
1162 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
1164 cpu_abort(env, "banked r13 write\n");
1167 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
1169 cpu_abort(env, "banked r13 read\n");
1175 /* Map CPU modes onto saved register banks. */
1176 static inline int bank_number(CPUARMState *env, int mode)
1179 case ARM_CPU_MODE_USR:
1180 case ARM_CPU_MODE_SYS:
1182 case ARM_CPU_MODE_SVC:
1184 case ARM_CPU_MODE_ABT:
1186 case ARM_CPU_MODE_UND:
1188 case ARM_CPU_MODE_IRQ:
1190 case ARM_CPU_MODE_FIQ:
1193 cpu_abort(env, "Bad mode %x\n", mode);
1197 void switch_mode(CPUARMState *env, int mode)
1202 old_mode = env->uncached_cpsr & CPSR_M;
1203 if (mode == old_mode)
1206 if (old_mode == ARM_CPU_MODE_FIQ) {
1207 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
1208 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
1209 } else if (mode == ARM_CPU_MODE_FIQ) {
1210 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
1211 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
1214 i = bank_number(env, old_mode);
1215 env->banked_r13[i] = env->regs[13];
1216 env->banked_r14[i] = env->regs[14];
1217 env->banked_spsr[i] = env->spsr;
1219 i = bank_number(env, mode);
1220 env->regs[13] = env->banked_r13[i];
1221 env->regs[14] = env->banked_r14[i];
1222 env->spsr = env->banked_spsr[i];
1225 static void v7m_push(CPUARMState *env, uint32_t val)
1228 stl_phys(env->regs[13], val);
1231 static uint32_t v7m_pop(CPUARMState *env)
1234 val = ldl_phys(env->regs[13]);
1239 /* Switch to V7M main or process stack pointer. */
1240 static void switch_v7m_sp(CPUARMState *env, int process)
1243 if (env->v7m.current_sp != process) {
1244 tmp = env->v7m.other_sp;
1245 env->v7m.other_sp = env->regs[13];
1246 env->regs[13] = tmp;
1247 env->v7m.current_sp = process;
1251 static void do_v7m_exception_exit(CPUARMState *env)
1256 type = env->regs[15];
1257 if (env->v7m.exception != 0)
1258 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
1260 /* Switch to the target stack. */
1261 switch_v7m_sp(env, (type & 4) != 0);
1262 /* Pop registers. */
1263 env->regs[0] = v7m_pop(env);
1264 env->regs[1] = v7m_pop(env);
1265 env->regs[2] = v7m_pop(env);
1266 env->regs[3] = v7m_pop(env);
1267 env->regs[12] = v7m_pop(env);
1268 env->regs[14] = v7m_pop(env);
1269 env->regs[15] = v7m_pop(env);
1270 xpsr = v7m_pop(env);
1271 xpsr_write(env, xpsr, 0xfffffdff);
1272 /* Undo stack alignment. */
1275 /* ??? The exception return type specifies Thread/Handler mode. However
1276 this is also implied by the xPSR value. Not sure what to do
1277 if there is a mismatch. */
1278 /* ??? Likewise for mismatches between the CONTROL register and the stack
1282 static void do_interrupt_v7m(CPUARMState *env)
1284 uint32_t xpsr = xpsr_read(env);
1289 if (env->v7m.current_sp)
1291 if (env->v7m.exception == 0)
1294 /* For exceptions we just mark as pending on the NVIC, and let that
1296 /* TODO: Need to escalate if the current priority is higher than the
1297 one we're raising. */
1298 switch (env->exception_index) {
1300 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
1304 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
1306 case EXCP_PREFETCH_ABORT:
1307 case EXCP_DATA_ABORT:
1308 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
1311 if (semihosting_enabled) {
1313 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
1316 env->regs[0] = do_arm_semihosting(env);
1320 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
1323 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
1325 case EXCP_EXCEPTION_EXIT:
1326 do_v7m_exception_exit(env);
1329 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
1330 return; /* Never happens. Keep compiler happy. */
1333 /* Align stack pointer. */
1334 /* ??? Should only do this if Configuration Control Register
1335 STACKALIGN bit is set. */
1336 if (env->regs[13] & 4) {
1340 /* Switch to the handler mode. */
1341 v7m_push(env, xpsr);
1342 v7m_push(env, env->regs[15]);
1343 v7m_push(env, env->regs[14]);
1344 v7m_push(env, env->regs[12]);
1345 v7m_push(env, env->regs[3]);
1346 v7m_push(env, env->regs[2]);
1347 v7m_push(env, env->regs[1]);
1348 v7m_push(env, env->regs[0]);
1349 switch_v7m_sp(env, 0);
1351 env->condexec_bits = 0;
1353 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
1354 env->regs[15] = addr & 0xfffffffe;
1355 env->thumb = addr & 1;
1358 /* Handle a CPU exception. */
1359 void do_interrupt(CPUARMState *env)
1367 do_interrupt_v7m(env);
1370 /* TODO: Vectored interrupt controller. */
1371 switch (env->exception_index) {
1373 new_mode = ARM_CPU_MODE_UND;
1382 if (semihosting_enabled) {
1383 /* Check for semihosting interrupt. */
1385 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
1387 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
1390 /* Only intercept calls from privileged modes, to provide some
1391 semblance of security. */
1392 if (((mask == 0x123456 && !env->thumb)
1393 || (mask == 0xab && env->thumb))
1394 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
1395 env->regs[0] = do_arm_semihosting(env);
1399 new_mode = ARM_CPU_MODE_SVC;
1402 /* The PC already points to the next instruction. */
1406 /* See if this is a semihosting syscall. */
1407 if (env->thumb && semihosting_enabled) {
1408 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
1410 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
1412 env->regs[0] = do_arm_semihosting(env);
1416 env->cp15.c5_insn = 2;
1417 /* Fall through to prefetch abort. */
1418 case EXCP_PREFETCH_ABORT:
1419 new_mode = ARM_CPU_MODE_ABT;
1421 mask = CPSR_A | CPSR_I;
1424 case EXCP_DATA_ABORT:
1425 new_mode = ARM_CPU_MODE_ABT;
1427 mask = CPSR_A | CPSR_I;
1431 new_mode = ARM_CPU_MODE_IRQ;
1433 /* Disable IRQ and imprecise data aborts. */
1434 mask = CPSR_A | CPSR_I;
1438 new_mode = ARM_CPU_MODE_FIQ;
1440 /* Disable FIQ, IRQ and imprecise data aborts. */
1441 mask = CPSR_A | CPSR_I | CPSR_F;
1445 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
1446 return; /* Never happens. Keep compiler happy. */
1449 if (env->cp15.c1_sys & (1 << 13)) {
1452 switch_mode (env, new_mode);
1453 env->spsr = cpsr_read(env);
1454 /* Clear IT bits. */
1455 env->condexec_bits = 0;
1456 /* Switch to the new mode, and to the correct instruction set. */
1457 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
1458 env->uncached_cpsr |= mask;
1459 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
1460 * and we should just guard the thumb mode on V4 */
1461 if (arm_feature(env, ARM_FEATURE_V4T)) {
1462 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
1464 env->regs[14] = env->regs[15] + offset;
1465 env->regs[15] = addr;
1466 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1469 /* Check section/page access permissions.
1470 Returns the page protection flags, or zero if the access is not
1472 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
1473 int access_type, int is_user)
1477 if (domain_prot == 3) {
1478 return PAGE_READ | PAGE_WRITE;
1481 if (access_type == 1)
1484 prot_ro = PAGE_READ;
1488 if (access_type == 1)
1490 switch ((env->cp15.c1_sys >> 8) & 3) {
1492 return is_user ? 0 : PAGE_READ;
1499 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
1504 return PAGE_READ | PAGE_WRITE;
1506 return PAGE_READ | PAGE_WRITE;
1507 case 4: /* Reserved. */
1510 return is_user ? 0 : prot_ro;
1514 if (!arm_feature (env, ARM_FEATURE_V6K))
1522 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
1526 if (address & env->cp15.c2_mask)
1527 table = env->cp15.c2_base1 & 0xffffc000;
1529 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
1531 table |= (address >> 18) & 0x3ffc;
1535 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
1536 int is_user, uint32_t *phys_ptr, int *prot,
1537 target_ulong *page_size)
1548 /* Pagetable walk. */
1549 /* Lookup l1 descriptor. */
1550 table = get_level1_table_address(env, address);
1551 desc = ldl_phys(table);
1553 domain = (desc >> 5) & 0x0f;
1554 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1556 /* Section translation fault. */
1560 if (domain_prot == 0 || domain_prot == 2) {
1562 code = 9; /* Section domain fault. */
1564 code = 11; /* Page domain fault. */
1569 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1570 ap = (desc >> 10) & 3;
1572 *page_size = 1024 * 1024;
1574 /* Lookup l2 entry. */
1576 /* Coarse pagetable. */
1577 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1579 /* Fine pagetable. */
1580 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1582 desc = ldl_phys(table);
1584 case 0: /* Page translation fault. */
1587 case 1: /* 64k page. */
1588 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1589 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1590 *page_size = 0x10000;
1592 case 2: /* 4k page. */
1593 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1594 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1595 *page_size = 0x1000;
1597 case 3: /* 1k page. */
1599 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1600 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1602 /* Page translation fault. */
1607 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1609 ap = (desc >> 4) & 3;
1613 /* Never happens, but compiler isn't smart enough to tell. */
1618 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1620 /* Access permission fault. */
1624 *phys_ptr = phys_addr;
1627 return code | (domain << 4);
1630 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
1631 int is_user, uint32_t *phys_ptr, int *prot,
1632 target_ulong *page_size)
1644 /* Pagetable walk. */
1645 /* Lookup l1 descriptor. */
1646 table = get_level1_table_address(env, address);
1647 desc = ldl_phys(table);
1650 /* Section translation fault. */
1654 } else if (type == 2 && (desc & (1 << 18))) {
1658 /* Section or page. */
1659 domain = (desc >> 5) & 0x0f;
1661 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1662 if (domain_prot == 0 || domain_prot == 2) {
1664 code = 9; /* Section domain fault. */
1666 code = 11; /* Page domain fault. */
1670 if (desc & (1 << 18)) {
1672 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1673 *page_size = 0x1000000;
1676 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1677 *page_size = 0x100000;
1679 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1680 xn = desc & (1 << 4);
1683 /* Lookup l2 entry. */
1684 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1685 desc = ldl_phys(table);
1686 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1688 case 0: /* Page translation fault. */
1691 case 1: /* 64k page. */
1692 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1693 xn = desc & (1 << 15);
1694 *page_size = 0x10000;
1696 case 2: case 3: /* 4k page. */
1697 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1699 *page_size = 0x1000;
1702 /* Never happens, but compiler isn't smart enough to tell. */
1707 if (domain_prot == 3) {
1708 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1710 if (xn && access_type == 2)
1713 /* The simplified model uses AP[0] as an access control bit. */
1714 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1715 /* Access flag fault. */
1716 code = (code == 15) ? 6 : 3;
1719 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1721 /* Access permission fault. */
1728 *phys_ptr = phys_addr;
1731 return code | (domain << 4);
1734 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
1735 int is_user, uint32_t *phys_ptr, int *prot)
1741 *phys_ptr = address;
1742 for (n = 7; n >= 0; n--) {
1743 base = env->cp15.c6_region[n];
1744 if ((base & 1) == 0)
1746 mask = 1 << ((base >> 1) & 0x1f);
1747 /* Keep this shift separate from the above to avoid an
1748 (undefined) << 32. */
1749 mask = (mask << 1) - 1;
1750 if (((base ^ address) & ~mask) == 0)
1756 if (access_type == 2) {
1757 mask = env->cp15.c5_insn;
1759 mask = env->cp15.c5_data;
1761 mask = (mask >> (n * 4)) & 0xf;
1768 *prot = PAGE_READ | PAGE_WRITE;
1773 *prot |= PAGE_WRITE;
1776 *prot = PAGE_READ | PAGE_WRITE;
1787 /* Bad permission. */
1794 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
1795 int access_type, int is_user,
1796 uint32_t *phys_ptr, int *prot,
1797 target_ulong *page_size)
1799 /* Fast Context Switch Extension. */
1800 if (address < 0x02000000)
1801 address += env->cp15.c13_fcse;
1803 if ((env->cp15.c1_sys & 1) == 0) {
1804 /* MMU/MPU disabled. */
1805 *phys_ptr = address;
1806 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1807 *page_size = TARGET_PAGE_SIZE;
1809 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1810 *page_size = TARGET_PAGE_SIZE;
1811 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1813 } else if (env->cp15.c1_sys & (1 << 23)) {
1814 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1817 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1822 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
1823 int access_type, int mmu_idx)
1826 target_ulong page_size;
1830 is_user = mmu_idx == MMU_USER_IDX;
1831 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1834 /* Map a single [sub]page. */
1835 phys_addr &= ~(uint32_t)0x3ff;
1836 address &= ~(uint32_t)0x3ff;
1837 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1841 if (access_type == 2) {
1842 env->cp15.c5_insn = ret;
1843 env->cp15.c6_insn = address;
1844 env->exception_index = EXCP_PREFETCH_ABORT;
1846 env->cp15.c5_data = ret;
1847 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1848 env->cp15.c5_data |= (1 << 11);
1849 env->cp15.c6_data = address;
1850 env->exception_index = EXCP_DATA_ABORT;
1855 target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
1858 target_ulong page_size;
1862 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1870 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1876 op1 = (insn >> 21) & 7;
1877 op2 = (insn >> 5) & 7;
1879 switch ((insn >> 16) & 0xf) {
1882 if (arm_feature(env, ARM_FEATURE_XSCALE))
1884 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1886 if (arm_feature(env, ARM_FEATURE_V7)
1887 && op1 == 2 && crm == 0 && op2 == 0) {
1888 env->cp15.c0_cssel = val & 0xf;
1892 case 1: /* System configuration. */
1893 if (arm_feature(env, ARM_FEATURE_V7)
1894 && op1 == 0 && crm == 1 && op2 == 0) {
1895 env->cp15.c1_scr = val;
1898 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1902 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1903 env->cp15.c1_sys = val;
1904 /* ??? Lots of these bits are not implemented. */
1905 /* This may enable/disable the MMU, so do a TLB flush. */
1908 case 1: /* Auxiliary control register. */
1909 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1910 env->cp15.c1_xscaleauxcr = val;
1913 /* Not implemented. */
1916 if (arm_feature(env, ARM_FEATURE_XSCALE))
1918 if (env->cp15.c1_coproc != val) {
1919 env->cp15.c1_coproc = val;
1920 /* ??? Is this safe when called from within a TB? */
1928 case 4: /* Reserved. */
1930 case 6: /* MMU Fault address / MPU base/size. */
1931 if (arm_feature(env, ARM_FEATURE_MPU)) {
1934 env->cp15.c6_region[crm] = val;
1936 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1940 env->cp15.c6_data = val;
1942 case 1: /* ??? This is WFAR on armv6 */
1944 env->cp15.c6_insn = val;
1952 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1954 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1955 break; /* Ignore ReadBuffer access */
1957 case 0: /* Cache lockdown. */
1959 case 0: /* L1 cache. */
1962 env->cp15.c9_data = val;
1965 env->cp15.c9_insn = val;
1971 case 1: /* L2 cache. */
1972 /* Ignore writes to L2 lockdown/auxiliary registers. */
1978 case 1: /* TCM memory region registers. */
1979 /* Not implemented. */
1985 case 12: /* Reserved. */
1990 /* ??? For debugging only. Should raise illegal instruction exception. */
1991 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1992 (insn >> 16) & 0xf, crm, op1, op2);
1995 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
2001 op1 = (insn >> 21) & 7;
2002 op2 = (insn >> 5) & 7;
2004 switch ((insn >> 16) & 0xf) {
2005 case 0: /* ID codes. */
2011 case 0: /* Device ID. */
2012 return env->cp15.c0_cpuid;
2013 case 1: /* Cache Type. */
2014 return env->cp15.c0_cachetype;
2015 case 2: /* TCM status. */
2017 case 3: /* TLB type register. */
2018 return 0; /* No lockable TLB entries. */
2020 /* The MPIDR was standardised in v7; prior to
2021 * this it was implemented only in the 11MPCore.
2022 * For all other pre-v7 cores it does not exist.
2024 if (arm_feature(env, ARM_FEATURE_V7) ||
2025 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
2026 int mpidr = env->cpu_index;
2027 /* We don't support setting cluster ID ([8..11])
2028 * so these bits always RAZ.
2030 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2032 /* Cores which are uniprocessor (non-coherent)
2033 * but still implement the MP extensions set
2034 * bit 30. (For instance, A9UP.) However we do
2035 * not currently model any of those cores.
2040 /* otherwise fall through to the unimplemented-reg case */
2045 if (!arm_feature(env, ARM_FEATURE_V6))
2047 return env->cp15.c0_c1[op2];
2049 if (!arm_feature(env, ARM_FEATURE_V6))
2051 return env->cp15.c0_c2[op2];
2052 case 3: case 4: case 5: case 6: case 7:
2058 /* These registers aren't documented on arm11 cores. However
2059 Linux looks at them anyway. */
2060 if (!arm_feature(env, ARM_FEATURE_V6))
2064 if (!arm_feature(env, ARM_FEATURE_V7))
2069 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
2071 return env->cp15.c0_clid;
2077 if (op2 != 0 || crm != 0)
2079 return env->cp15.c0_cssel;
2083 case 1: /* System configuration. */
2084 if (arm_feature(env, ARM_FEATURE_V7)
2085 && op1 == 0 && crm == 1 && op2 == 0) {
2086 return env->cp15.c1_scr;
2088 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2091 case 0: /* Control register. */
2092 return env->cp15.c1_sys;
2093 case 1: /* Auxiliary control register. */
2094 if (arm_feature(env, ARM_FEATURE_XSCALE))
2095 return env->cp15.c1_xscaleauxcr;
2096 if (!arm_feature(env, ARM_FEATURE_AUXCR))
2098 switch (ARM_CPUID(env)) {
2099 case ARM_CPUID_ARM1026:
2101 case ARM_CPUID_ARM1136:
2102 case ARM_CPUID_ARM1136_R2:
2103 case ARM_CPUID_ARM1176:
2105 case ARM_CPUID_ARM11MPCORE:
2107 case ARM_CPUID_CORTEXA8:
2109 case ARM_CPUID_CORTEXA9:
2110 case ARM_CPUID_CORTEXA15:
2115 case 2: /* Coprocessor access register. */
2116 if (arm_feature(env, ARM_FEATURE_XSCALE))
2118 return env->cp15.c1_coproc;
2122 case 4: /* Reserved. */
2124 case 6: /* MMU Fault address. */
2125 if (arm_feature(env, ARM_FEATURE_MPU)) {
2128 return env->cp15.c6_region[crm];
2130 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2134 return env->cp15.c6_data;
2136 if (arm_feature(env, ARM_FEATURE_V6)) {
2137 /* Watchpoint Fault Adrress. */
2138 return 0; /* Not implemented. */
2140 /* Instruction Fault Adrress. */
2141 /* Arm9 doesn't have an IFAR, but implementing it anyway
2142 shouldn't do any harm. */
2143 return env->cp15.c6_insn;
2146 if (arm_feature(env, ARM_FEATURE_V6)) {
2147 /* Instruction Fault Adrress. */
2148 return env->cp15.c6_insn;
2158 case 0: /* Cache lockdown */
2160 case 0: /* L1 cache. */
2161 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2166 return env->cp15.c9_data;
2168 return env->cp15.c9_insn;
2172 case 1: /* L2 cache */
2173 /* L2 Lockdown and Auxiliary control. */
2176 /* L2 cache lockdown (A8 only) */
2179 /* L2 cache auxiliary control (A8) or control (A15) */
2180 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
2181 /* Linux wants the number of processors from here.
2182 * Might as well set the interrupt-controller bit too.
2184 return ((smp_cpus - 1) << 24) | (1 << 23);
2188 /* L2 cache extended control (A15) */
2201 case 11: /* TCM DMA control. */
2202 case 12: /* Reserved. */
2206 /* ??? For debugging only. Should raise illegal instruction exception. */
2207 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2208 (insn >> 16) & 0xf, crm, op1, op2);
2212 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2214 if ((env->uncached_cpsr & CPSR_M) == mode) {
2215 env->regs[13] = val;
2217 env->banked_r13[bank_number(env, mode)] = val;
2221 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2223 if ((env->uncached_cpsr & CPSR_M) == mode) {
2224 return env->regs[13];
2226 return env->banked_r13[bank_number(env, mode)];
2230 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2234 return xpsr_read(env) & 0xf8000000;
2236 return xpsr_read(env) & 0xf80001ff;
2238 return xpsr_read(env) & 0xff00fc00;
2240 return xpsr_read(env) & 0xff00fdff;
2242 return xpsr_read(env) & 0x000001ff;
2244 return xpsr_read(env) & 0x0700fc00;
2246 return xpsr_read(env) & 0x0700edff;
2248 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2250 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2251 case 16: /* PRIMASK */
2252 return (env->uncached_cpsr & CPSR_I) != 0;
2253 case 17: /* BASEPRI */
2254 case 18: /* BASEPRI_MAX */
2255 return env->v7m.basepri;
2256 case 19: /* FAULTMASK */
2257 return (env->uncached_cpsr & CPSR_F) != 0;
2258 case 20: /* CONTROL */
2259 return env->v7m.control;
2261 /* ??? For debugging only. */
2262 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2267 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2271 xpsr_write(env, val, 0xf8000000);
2274 xpsr_write(env, val, 0xf8000000);
2277 xpsr_write(env, val, 0xfe00fc00);
2280 xpsr_write(env, val, 0xfe00fc00);
2283 /* IPSR bits are readonly. */
2286 xpsr_write(env, val, 0x0600fc00);
2289 xpsr_write(env, val, 0x0600fc00);
2292 if (env->v7m.current_sp)
2293 env->v7m.other_sp = val;
2295 env->regs[13] = val;
2298 if (env->v7m.current_sp)
2299 env->regs[13] = val;
2301 env->v7m.other_sp = val;
2303 case 16: /* PRIMASK */
2305 env->uncached_cpsr |= CPSR_I;
2307 env->uncached_cpsr &= ~CPSR_I;
2309 case 17: /* BASEPRI */
2310 env->v7m.basepri = val & 0xff;
2312 case 18: /* BASEPRI_MAX */
2314 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2315 env->v7m.basepri = val;
2317 case 19: /* FAULTMASK */
2319 env->uncached_cpsr |= CPSR_F;
2321 env->uncached_cpsr &= ~CPSR_F;
2323 case 20: /* CONTROL */
2324 env->v7m.control = val & 3;
2325 switch_v7m_sp(env, (val & 2) != 0);
2328 /* ??? For debugging only. */
2329 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2336 /* Note that signed overflow is undefined in C. The following routines are
2337 careful to use unsigned types where modulo arithmetic is required.
2338 Failure to do so _will_ break on newer gcc. */
2340 /* Signed saturating arithmetic. */
2342 /* Perform 16-bit signed saturating addition. */
2343 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2348 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2357 /* Perform 8-bit signed saturating addition. */
2358 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2363 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2372 /* Perform 16-bit signed saturating subtraction. */
2373 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2378 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2387 /* Perform 8-bit signed saturating subtraction. */
2388 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2393 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2402 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2403 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2404 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2405 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2408 #include "op_addsub.h"
2410 /* Unsigned saturating arithmetic. */
2411 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2420 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2428 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2437 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2445 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2446 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2447 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2448 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2451 #include "op_addsub.h"
2453 /* Signed modulo arithmetic. */
2454 #define SARITH16(a, b, n, op) do { \
2456 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2457 RESULT(sum, n, 16); \
2459 ge |= 3 << (n * 2); \
2462 #define SARITH8(a, b, n, op) do { \
2464 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2465 RESULT(sum, n, 8); \
2471 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2472 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2473 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2474 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2478 #include "op_addsub.h"
2480 /* Unsigned modulo arithmetic. */
2481 #define ADD16(a, b, n) do { \
2483 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2484 RESULT(sum, n, 16); \
2485 if ((sum >> 16) == 1) \
2486 ge |= 3 << (n * 2); \
2489 #define ADD8(a, b, n) do { \
2491 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2492 RESULT(sum, n, 8); \
2493 if ((sum >> 8) == 1) \
2497 #define SUB16(a, b, n) do { \
2499 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2500 RESULT(sum, n, 16); \
2501 if ((sum >> 16) == 0) \
2502 ge |= 3 << (n * 2); \
2505 #define SUB8(a, b, n) do { \
2507 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2508 RESULT(sum, n, 8); \
2509 if ((sum >> 8) == 0) \
2516 #include "op_addsub.h"
2518 /* Halved signed arithmetic. */
2519 #define ADD16(a, b, n) \
2520 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2521 #define SUB16(a, b, n) \
2522 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2523 #define ADD8(a, b, n) \
2524 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2525 #define SUB8(a, b, n) \
2526 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2529 #include "op_addsub.h"
2531 /* Halved unsigned arithmetic. */
2532 #define ADD16(a, b, n) \
2533 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2534 #define SUB16(a, b, n) \
2535 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2536 #define ADD8(a, b, n) \
2537 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2538 #define SUB8(a, b, n) \
2539 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2542 #include "op_addsub.h"
2544 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2552 /* Unsigned sum of absolute byte differences. */
2553 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2556 sum = do_usad(a, b);
2557 sum += do_usad(a >> 8, b >> 8);
2558 sum += do_usad(a >> 16, b >>16);
2559 sum += do_usad(a >> 24, b >> 24);
2563 /* For ARMv6 SEL instruction. */
2564 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2577 return (a & mask) | (b & ~mask);
2580 uint32_t HELPER(logicq_cc)(uint64_t val)
2582 return (val >> 32) | (val != 0);
2585 /* VFP support. We follow the convention used for VFP instrunctions:
2586 Single precition routines have a "s" suffix, double precision a
2589 /* Convert host exception flags to vfp form. */
2590 static inline int vfp_exceptbits_from_host(int host_bits)
2592 int target_bits = 0;
2594 if (host_bits & float_flag_invalid)
2596 if (host_bits & float_flag_divbyzero)
2598 if (host_bits & float_flag_overflow)
2600 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
2602 if (host_bits & float_flag_inexact)
2603 target_bits |= 0x10;
2604 if (host_bits & float_flag_input_denormal)
2605 target_bits |= 0x80;
2609 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2614 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2615 | (env->vfp.vec_len << 16)
2616 | (env->vfp.vec_stride << 20);
2617 i = get_float_exception_flags(&env->vfp.fp_status);
2618 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
2619 fpscr |= vfp_exceptbits_from_host(i);
2623 uint32_t vfp_get_fpscr(CPUARMState *env)
2625 return HELPER(vfp_get_fpscr)(env);
2628 /* Convert vfp exception flags to target form. */
2629 static inline int vfp_exceptbits_to_host(int target_bits)
2633 if (target_bits & 1)
2634 host_bits |= float_flag_invalid;
2635 if (target_bits & 2)
2636 host_bits |= float_flag_divbyzero;
2637 if (target_bits & 4)
2638 host_bits |= float_flag_overflow;
2639 if (target_bits & 8)
2640 host_bits |= float_flag_underflow;
2641 if (target_bits & 0x10)
2642 host_bits |= float_flag_inexact;
2643 if (target_bits & 0x80)
2644 host_bits |= float_flag_input_denormal;
2648 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
2653 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2654 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2655 env->vfp.vec_len = (val >> 16) & 7;
2656 env->vfp.vec_stride = (val >> 20) & 3;
2659 if (changed & (3 << 22)) {
2660 i = (val >> 22) & 3;
2663 i = float_round_nearest_even;
2669 i = float_round_down;
2672 i = float_round_to_zero;
2675 set_float_rounding_mode(i, &env->vfp.fp_status);
2677 if (changed & (1 << 24)) {
2678 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2679 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2681 if (changed & (1 << 25))
2682 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2684 i = vfp_exceptbits_to_host(val);
2685 set_float_exception_flags(i, &env->vfp.fp_status);
2686 set_float_exception_flags(0, &env->vfp.standard_fp_status);
2689 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
2691 HELPER(vfp_set_fpscr)(env, val);
2694 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2696 #define VFP_BINOP(name) \
2697 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2699 float_status *fpst = fpstp; \
2700 return float32_ ## name(a, b, fpst); \
2702 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2704 float_status *fpst = fpstp; \
2705 return float64_ ## name(a, b, fpst); \
2713 float32 VFP_HELPER(neg, s)(float32 a)
2715 return float32_chs(a);
2718 float64 VFP_HELPER(neg, d)(float64 a)
2720 return float64_chs(a);
2723 float32 VFP_HELPER(abs, s)(float32 a)
2725 return float32_abs(a);
2728 float64 VFP_HELPER(abs, d)(float64 a)
2730 return float64_abs(a);
2733 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
2735 return float32_sqrt(a, &env->vfp.fp_status);
2738 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
2740 return float64_sqrt(a, &env->vfp.fp_status);
2743 /* XXX: check quiet/signaling case */
2744 #define DO_VFP_cmp(p, type) \
2745 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
2748 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2749 case 0: flags = 0x6; break; \
2750 case -1: flags = 0x8; break; \
2751 case 1: flags = 0x2; break; \
2752 default: case 2: flags = 0x3; break; \
2754 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2755 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2757 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
2760 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2761 case 0: flags = 0x6; break; \
2762 case -1: flags = 0x8; break; \
2763 case 1: flags = 0x2; break; \
2764 default: case 2: flags = 0x3; break; \
2766 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2767 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2769 DO_VFP_cmp(s, float32)
2770 DO_VFP_cmp(d, float64)
2773 /* Integer to float and float to integer conversions */
2775 #define CONV_ITOF(name, fsz, sign) \
2776 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2778 float_status *fpst = fpstp; \
2779 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
2782 #define CONV_FTOI(name, fsz, sign, round) \
2783 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2785 float_status *fpst = fpstp; \
2786 if (float##fsz##_is_any_nan(x)) { \
2787 float_raise(float_flag_invalid, fpst); \
2790 return float##fsz##_to_##sign##int32##round(x, fpst); \
2793 #define FLOAT_CONVS(name, p, fsz, sign) \
2794 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2795 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2796 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2798 FLOAT_CONVS(si, s, 32, )
2799 FLOAT_CONVS(si, d, 64, )
2800 FLOAT_CONVS(ui, s, 32, u)
2801 FLOAT_CONVS(ui, d, 64, u)
2807 /* floating point conversion */
2808 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
2810 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2811 /* ARM requires that S<->D conversion of any kind of NaN generates
2812 * a quiet NaN by forcing the most significant frac bit to 1.
2814 return float64_maybe_silence_nan(r);
2817 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
2819 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2820 /* ARM requires that S<->D conversion of any kind of NaN generates
2821 * a quiet NaN by forcing the most significant frac bit to 1.
2823 return float32_maybe_silence_nan(r);
2826 /* VFP3 fixed point conversion. */
2827 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2828 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2831 float_status *fpst = fpstp; \
2833 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2834 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2836 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2839 float_status *fpst = fpstp; \
2841 if (float##fsz##_is_any_nan(x)) { \
2842 float_raise(float_flag_invalid, fpst); \
2845 tmp = float##fsz##_scalbn(x, shift, fpst); \
2846 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2849 VFP_CONV_FIX(sh, d, 64, int16, )
2850 VFP_CONV_FIX(sl, d, 64, int32, )
2851 VFP_CONV_FIX(uh, d, 64, uint16, u)
2852 VFP_CONV_FIX(ul, d, 64, uint32, u)
2853 VFP_CONV_FIX(sh, s, 32, int16, )
2854 VFP_CONV_FIX(sl, s, 32, int32, )
2855 VFP_CONV_FIX(uh, s, 32, uint16, u)
2856 VFP_CONV_FIX(ul, s, 32, uint32, u)
2859 /* Half precision conversions. */
2860 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
2862 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2863 float32 r = float16_to_float32(make_float16(a), ieee, s);
2865 return float32_maybe_silence_nan(r);
2870 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
2872 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2873 float16 r = float32_to_float16(a, ieee, s);
2875 r = float16_maybe_silence_nan(r);
2877 return float16_val(r);
2880 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2882 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2885 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2887 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2890 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2892 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2895 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2897 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2900 #define float32_two make_float32(0x40000000)
2901 #define float32_three make_float32(0x40400000)
2902 #define float32_one_point_five make_float32(0x3fc00000)
2904 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
2906 float_status *s = &env->vfp.standard_fp_status;
2907 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2908 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2909 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2910 float_raise(float_flag_input_denormal, s);
2914 return float32_sub(float32_two, float32_mul(a, b, s), s);
2917 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
2919 float_status *s = &env->vfp.standard_fp_status;
2921 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2922 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2923 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2924 float_raise(float_flag_input_denormal, s);
2926 return float32_one_point_five;
2928 product = float32_mul(a, b, s);
2929 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
2934 /* Constants 256 and 512 are used in some helpers; we avoid relying on
2935 * int->float conversions at run-time. */
2936 #define float64_256 make_float64(0x4070000000000000LL)
2937 #define float64_512 make_float64(0x4080000000000000LL)
2939 /* The algorithm that must be used to calculate the estimate
2940 * is specified by the ARM ARM.
2942 static float64 recip_estimate(float64 a, CPUARMState *env)
2944 /* These calculations mustn't set any fp exception flags,
2945 * so we use a local copy of the fp_status.
2947 float_status dummy_status = env->vfp.standard_fp_status;
2948 float_status *s = &dummy_status;
2949 /* q = (int)(a * 512.0) */
2950 float64 q = float64_mul(float64_512, a, s);
2951 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2953 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2954 q = int64_to_float64(q_int, s);
2955 q = float64_add(q, float64_half, s);
2956 q = float64_div(q, float64_512, s);
2957 q = float64_div(float64_one, q, s);
2959 /* s = (int)(256.0 * r + 0.5) */
2960 q = float64_mul(q, float64_256, s);
2961 q = float64_add(q, float64_half, s);
2962 q_int = float64_to_int64_round_to_zero(q, s);
2964 /* return (double)s / 256.0 */
2965 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2968 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
2970 float_status *s = &env->vfp.standard_fp_status;
2972 uint32_t val32 = float32_val(a);
2975 int a_exp = (val32 & 0x7f800000) >> 23;
2976 int sign = val32 & 0x80000000;
2978 if (float32_is_any_nan(a)) {
2979 if (float32_is_signaling_nan(a)) {
2980 float_raise(float_flag_invalid, s);
2982 return float32_default_nan;
2983 } else if (float32_is_infinity(a)) {
2984 return float32_set_sign(float32_zero, float32_is_neg(a));
2985 } else if (float32_is_zero_or_denormal(a)) {
2986 if (!float32_is_zero(a)) {
2987 float_raise(float_flag_input_denormal, s);
2989 float_raise(float_flag_divbyzero, s);
2990 return float32_set_sign(float32_infinity, float32_is_neg(a));
2991 } else if (a_exp >= 253) {
2992 float_raise(float_flag_underflow, s);
2993 return float32_set_sign(float32_zero, float32_is_neg(a));
2996 f64 = make_float64((0x3feULL << 52)
2997 | ((int64_t)(val32 & 0x7fffff) << 29));
2999 result_exp = 253 - a_exp;
3001 f64 = recip_estimate(f64, env);
3004 | ((result_exp & 0xff) << 23)
3005 | ((float64_val(f64) >> 29) & 0x7fffff);
3006 return make_float32(val32);
3009 /* The algorithm that must be used to calculate the estimate
3010 * is specified by the ARM ARM.
3012 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
3014 /* These calculations mustn't set any fp exception flags,
3015 * so we use a local copy of the fp_status.
3017 float_status dummy_status = env->vfp.standard_fp_status;
3018 float_status *s = &dummy_status;
3022 if (float64_lt(a, float64_half, s)) {
3023 /* range 0.25 <= a < 0.5 */
3025 /* a in units of 1/512 rounded down */
3026 /* q0 = (int)(a * 512.0); */
3027 q = float64_mul(float64_512, a, s);
3028 q_int = float64_to_int64_round_to_zero(q, s);
3030 /* reciprocal root r */
3031 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
3032 q = int64_to_float64(q_int, s);
3033 q = float64_add(q, float64_half, s);
3034 q = float64_div(q, float64_512, s);
3035 q = float64_sqrt(q, s);
3036 q = float64_div(float64_one, q, s);
3038 /* range 0.5 <= a < 1.0 */
3040 /* a in units of 1/256 rounded down */
3041 /* q1 = (int)(a * 256.0); */
3042 q = float64_mul(float64_256, a, s);
3043 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3045 /* reciprocal root r */
3046 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
3047 q = int64_to_float64(q_int, s);
3048 q = float64_add(q, float64_half, s);
3049 q = float64_div(q, float64_256, s);
3050 q = float64_sqrt(q, s);
3051 q = float64_div(float64_one, q, s);
3053 /* r in units of 1/256 rounded to nearest */
3054 /* s = (int)(256.0 * r + 0.5); */
3056 q = float64_mul(q, float64_256,s );
3057 q = float64_add(q, float64_half, s);
3058 q_int = float64_to_int64_round_to_zero(q, s);
3060 /* return (double)s / 256.0;*/
3061 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3064 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
3066 float_status *s = &env->vfp.standard_fp_status;
3072 val = float32_val(a);
3074 if (float32_is_any_nan(a)) {
3075 if (float32_is_signaling_nan(a)) {
3076 float_raise(float_flag_invalid, s);
3078 return float32_default_nan;
3079 } else if (float32_is_zero_or_denormal(a)) {
3080 if (!float32_is_zero(a)) {
3081 float_raise(float_flag_input_denormal, s);
3083 float_raise(float_flag_divbyzero, s);
3084 return float32_set_sign(float32_infinity, float32_is_neg(a));
3085 } else if (float32_is_neg(a)) {
3086 float_raise(float_flag_invalid, s);
3087 return float32_default_nan;
3088 } else if (float32_is_infinity(a)) {
3089 return float32_zero;
3092 /* Normalize to a double-precision value between 0.25 and 1.0,
3093 * preserving the parity of the exponent. */
3094 if ((val & 0x800000) == 0) {
3095 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3097 | ((uint64_t)(val & 0x7fffff) << 29));
3099 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3101 | ((uint64_t)(val & 0x7fffff) << 29));
3104 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3106 f64 = recip_sqrt_estimate(f64, env);
3108 val64 = float64_val(f64);
3110 val = ((result_exp & 0xff) << 23)
3111 | ((val64 >> 29) & 0x7fffff);
3112 return make_float32(val);
3115 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
3119 if ((a & 0x80000000) == 0) {
3123 f64 = make_float64((0x3feULL << 52)
3124 | ((int64_t)(a & 0x7fffffff) << 21));
3126 f64 = recip_estimate (f64, env);
3128 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3131 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
3135 if ((a & 0xc0000000) == 0) {
3139 if (a & 0x80000000) {
3140 f64 = make_float64((0x3feULL << 52)
3141 | ((uint64_t)(a & 0x7fffffff) << 21));
3142 } else { /* bits 31-30 == '01' */
3143 f64 = make_float64((0x3fdULL << 52)
3144 | ((uint64_t)(a & 0x3fffffff) << 22));
3147 f64 = recip_sqrt_estimate(f64, env);
3149 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3152 /* VFPv4 fused multiply-accumulate */
3153 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3155 float_status *fpst = fpstp;
3156 return float32_muladd(a, b, c, 0, fpst);
3159 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3161 float_status *fpst = fpstp;
3162 return float64_muladd(a, b, c, 0, fpst);