]> git.proxmox.com Git - qemu.git/blob - target-arm/helper.c
target-arm: Convert cp15 crn=6 registers
[qemu.git] / target-arm / helper.c
1 #include "cpu.h"
2 #include "gdbstub.h"
3 #include "helper.h"
4 #include "host-utils.h"
5 #include "sysemu.h"
6
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);
12 #endif
13
14 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
15 {
16 int nregs;
17
18 /* VFP data registers are always little-endian. */
19 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
20 if (reg < nregs) {
21 stfq_le_p(buf, env->vfp.regs[reg]);
22 return 8;
23 }
24 if (arm_feature(env, ARM_FEATURE_NEON)) {
25 /* Aliases for Q regs. */
26 nregs += 16;
27 if (reg < nregs) {
28 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
29 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
30 return 16;
31 }
32 }
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;
37 }
38 return 0;
39 }
40
41 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
42 {
43 int nregs;
44
45 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
46 if (reg < nregs) {
47 env->vfp.regs[reg] = ldfq_le_p(buf);
48 return 8;
49 }
50 if (arm_feature(env, ARM_FEATURE_NEON)) {
51 nregs += 16;
52 if (reg < nregs) {
53 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
54 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
55 return 16;
56 }
57 }
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;
62 }
63 return 0;
64 }
65
66 static int dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
67 {
68 env->cp15.c3 = value;
69 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
70 return 0;
71 }
72
73 static int fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
74 {
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.
78 */
79 tlb_flush(env, 1);
80 env->cp15.c13_fcse = value;
81 }
82 return 0;
83 }
84 static int contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
85 uint64_t value)
86 {
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.
91 */
92 tlb_flush(env, 1);
93 }
94 env->cp15.c13_context = value;
95 return 0;
96 }
97
98 static int tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
99 uint64_t value)
100 {
101 /* Invalidate all (TLBIALL) */
102 tlb_flush(env, 1);
103 return 0;
104 }
105
106 static int tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
107 uint64_t value)
108 {
109 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
110 tlb_flush_page(env, value & TARGET_PAGE_MASK);
111 return 0;
112 }
113
114 static int tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
115 uint64_t value)
116 {
117 /* Invalidate by ASID (TLBIASID) */
118 tlb_flush(env, value == 0);
119 return 0;
120 }
121
122 static int tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
123 uint64_t value)
124 {
125 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
126 tlb_flush_page(env, value & TARGET_PAGE_MASK);
127 return 0;
128 }
129
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.
134 */
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.
150 */
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.
155 */
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 },
168 REGINFO_SENTINEL
169 };
170
171 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
172 /* Not all pre-v6 cores implemented this WFI, so this is slightly
173 * over-broad.
174 */
175 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
176 .access = PL1_W, .type = ARM_CP_WFI },
177 REGINFO_SENTINEL
178 };
179
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).
183 */
184 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
185 .access = PL1_W, .type = ARM_CP_WFI },
186 REGINFO_SENTINEL
187 };
188
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 },
200 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
201 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_insn),
202 .resetvalue = 0, },
203 /* Watchpoint Fault Address Register : should actually only be present
204 * for 1136, 1176, 11MPCore.
205 */
206 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
207 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
208 REGINFO_SENTINEL
209 };
210
211 static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
212 uint64_t *value)
213 {
214 /* Generic performance monitor register read function for where
215 * user access may be allowed by PMUSERENR.
216 */
217 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
218 return EXCP_UDEF;
219 }
220 *value = CPREG_FIELD32(env, ri);
221 return 0;
222 }
223
224 static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
225 uint64_t value)
226 {
227 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
228 return EXCP_UDEF;
229 }
230 /* only the DP, X, D and E bits are writable */
231 env->cp15.c9_pmcr &= ~0x39;
232 env->cp15.c9_pmcr |= (value & 0x39);
233 return 0;
234 }
235
236 static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
237 uint64_t value)
238 {
239 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
240 return EXCP_UDEF;
241 }
242 value &= (1 << 31);
243 env->cp15.c9_pmcnten |= value;
244 return 0;
245 }
246
247 static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
248 uint64_t value)
249 {
250 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
251 return EXCP_UDEF;
252 }
253 value &= (1 << 31);
254 env->cp15.c9_pmcnten &= ~value;
255 return 0;
256 }
257
258 static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
259 uint64_t value)
260 {
261 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
262 return EXCP_UDEF;
263 }
264 env->cp15.c9_pmovsr &= ~value;
265 return 0;
266 }
267
268 static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
269 uint64_t value)
270 {
271 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
272 return EXCP_UDEF;
273 }
274 env->cp15.c9_pmxevtyper = value & 0xff;
275 return 0;
276 }
277
278 static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
279 uint64_t value)
280 {
281 env->cp15.c9_pmuserenr = value & 1;
282 return 0;
283 }
284
285 static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
286 uint64_t value)
287 {
288 /* We have no event counters so only the C bit can be changed */
289 value &= (1 << 31);
290 env->cp15.c9_pminten |= value;
291 return 0;
292 }
293
294 static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
295 uint64_t value)
296 {
297 value &= (1 << 31);
298 env->cp15.c9_pminten &= ~value;
299 return 0;
300 }
301
302 static const ARMCPRegInfo v7_cp_reginfo[] = {
303 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
304 * debug components
305 */
306 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
307 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
308 { .name = "DBGDRAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
309 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
310 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
311 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
312 .access = PL1_W, .type = ARM_CP_NOP },
313 /* Performance monitors are implementation defined in v7,
314 * but with an ARM recommended set of registers, which we
315 * follow (although we don't actually implement any counters)
316 *
317 * Performance registers fall into three categories:
318 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
319 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
320 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
321 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
322 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
323 */
324 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
325 .access = PL0_RW, .resetvalue = 0,
326 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
327 .readfn = pmreg_read, .writefn = pmcntenset_write },
328 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
329 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
330 .readfn = pmreg_read, .writefn = pmcntenclr_write },
331 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
332 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
333 .readfn = pmreg_read, .writefn = pmovsr_write },
334 /* Unimplemented so WI. Strictly speaking write accesses in PL0 should
335 * respect PMUSERENR.
336 */
337 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
338 .access = PL0_W, .type = ARM_CP_NOP },
339 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
340 * We choose to RAZ/WI. XXX should respect PMUSERENR.
341 */
342 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
343 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
344 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
345 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
346 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
347 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
348 .access = PL0_RW,
349 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
350 .readfn = pmreg_read, .writefn = pmxevtyper_write },
351 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
352 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
353 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
354 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
355 .access = PL0_R | PL1_RW,
356 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
357 .resetvalue = 0,
358 .writefn = pmuserenr_write },
359 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
360 .access = PL1_RW,
361 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
362 .resetvalue = 0,
363 .writefn = pmintenset_write },
364 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
365 .access = PL1_RW,
366 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
367 .resetvalue = 0,
368 .writefn = pmintenclr_write },
369 REGINFO_SENTINEL
370 };
371
372 static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
373 {
374 value &= 1;
375 env->teecr = value;
376 return 0;
377 }
378
379 static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
380 uint64_t *value)
381 {
382 /* This is a helper function because the user access rights
383 * depend on the value of the TEECR.
384 */
385 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
386 return EXCP_UDEF;
387 }
388 *value = env->teehbr;
389 return 0;
390 }
391
392 static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
393 uint64_t value)
394 {
395 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
396 return EXCP_UDEF;
397 }
398 env->teehbr = value;
399 return 0;
400 }
401
402 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
403 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
404 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
405 .resetvalue = 0,
406 .writefn = teecr_write },
407 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
408 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
409 .resetvalue = 0,
410 .readfn = teehbr_read, .writefn = teehbr_write },
411 REGINFO_SENTINEL
412 };
413
414 static const ARMCPRegInfo v6k_cp_reginfo[] = {
415 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
416 .access = PL0_RW,
417 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
418 .resetvalue = 0 },
419 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
420 .access = PL0_R|PL1_W,
421 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
422 .resetvalue = 0 },
423 { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
424 .access = PL1_RW,
425 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
426 .resetvalue = 0 },
427 REGINFO_SENTINEL
428 };
429
430 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
431 /* Dummy implementation: RAZ/WI the whole crn=14 space */
432 { .name = "GENERIC_TIMER", .cp = 15, .crn = 14,
433 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
434 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
435 REGINFO_SENTINEL
436 };
437
438 static int par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
439 {
440 if (arm_feature(env, ARM_FEATURE_V7)) {
441 env->cp15.c7_par = value & 0xfffff6ff;
442 } else {
443 env->cp15.c7_par = value & 0xfffff1ff;
444 }
445 return 0;
446 }
447
448 #ifndef CONFIG_USER_ONLY
449 /* get_phys_addr() isn't present for user-mode-only targets */
450 static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
451 {
452 uint32_t phys_addr;
453 target_ulong page_size;
454 int prot;
455 int ret, is_user = ri->opc2 & 2;
456 int access_type = ri->opc2 & 1;
457
458 if (ri->opc2 & 4) {
459 /* Other states are only available with TrustZone */
460 return EXCP_UDEF;
461 }
462 ret = get_phys_addr(env, value, access_type, is_user,
463 &phys_addr, &prot, &page_size);
464 if (ret == 0) {
465 /* We do not set any attribute bits in the PAR */
466 if (page_size == (1 << 24)
467 && arm_feature(env, ARM_FEATURE_V7)) {
468 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
469 } else {
470 env->cp15.c7_par = phys_addr & 0xfffff000;
471 }
472 } else {
473 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
474 ((ret & (12 << 1)) >> 6) |
475 ((ret & 0xf) << 1) | 1;
476 }
477 return 0;
478 }
479 #endif
480
481 static const ARMCPRegInfo vapa_cp_reginfo[] = {
482 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
483 .access = PL1_RW, .resetvalue = 0,
484 .fieldoffset = offsetof(CPUARMState, cp15.c7_par),
485 .writefn = par_write },
486 #ifndef CONFIG_USER_ONLY
487 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
488 .access = PL1_W, .writefn = ats_write },
489 #endif
490 REGINFO_SENTINEL
491 };
492
493 /* Return basic MPU access permission bits. */
494 static uint32_t simple_mpu_ap_bits(uint32_t val)
495 {
496 uint32_t ret;
497 uint32_t mask;
498 int i;
499 ret = 0;
500 mask = 3;
501 for (i = 0; i < 16; i += 2) {
502 ret |= (val >> i) & mask;
503 mask <<= 2;
504 }
505 return ret;
506 }
507
508 /* Pad basic MPU access permission bits to extended format. */
509 static uint32_t extended_mpu_ap_bits(uint32_t val)
510 {
511 uint32_t ret;
512 uint32_t mask;
513 int i;
514 ret = 0;
515 mask = 3;
516 for (i = 0; i < 16; i += 2) {
517 ret |= (val & mask) << i;
518 mask <<= 2;
519 }
520 return ret;
521 }
522
523 static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
524 uint64_t value)
525 {
526 env->cp15.c5_data = extended_mpu_ap_bits(value);
527 return 0;
528 }
529
530 static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
531 uint64_t *value)
532 {
533 *value = simple_mpu_ap_bits(env->cp15.c5_data);
534 return 0;
535 }
536
537 static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
538 uint64_t value)
539 {
540 env->cp15.c5_insn = extended_mpu_ap_bits(value);
541 return 0;
542 }
543
544 static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
545 uint64_t *value)
546 {
547 *value = simple_mpu_ap_bits(env->cp15.c5_insn);
548 return 0;
549 }
550
551 static int arm946_prbs_read(CPUARMState *env, const ARMCPRegInfo *ri,
552 uint64_t *value)
553 {
554 if (ri->crm > 8) {
555 return EXCP_UDEF;
556 }
557 *value = env->cp15.c6_region[ri->crm];
558 return 0;
559 }
560
561 static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
562 uint64_t value)
563 {
564 if (ri->crm > 8) {
565 return EXCP_UDEF;
566 }
567 env->cp15.c6_region[ri->crm] = value;
568 return 0;
569 }
570
571 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
572 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
573 .access = PL1_RW,
574 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
575 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
576 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
577 .access = PL1_RW,
578 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
579 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
580 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
581 .access = PL1_RW,
582 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
583 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
584 .access = PL1_RW,
585 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
586 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
587 .access = PL1_RW,
588 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
589 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
590 .access = PL1_RW,
591 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
592 /* Protection region base and size registers */
593 { .name = "946_PRBS", .cp = 15, .crn = 6, .crm = CP_ANY, .opc1 = 0,
594 .opc2 = CP_ANY, .access = PL1_RW,
595 .readfn = arm946_prbs_read, .writefn = arm946_prbs_write, },
596 REGINFO_SENTINEL
597 };
598
599 static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
600 uint64_t value)
601 {
602 value &= 7;
603 env->cp15.c2_control = value;
604 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> value);
605 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> value);
606 return 0;
607 }
608
609 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
610 {
611 env->cp15.c2_base_mask = 0xffffc000u;
612 env->cp15.c2_control = 0;
613 env->cp15.c2_mask = 0;
614 }
615
616 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
617 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
618 .access = PL1_RW,
619 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
620 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
621 .access = PL1_RW,
622 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
623 { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
624 .access = PL1_RW,
625 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
626 { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
627 .access = PL1_RW,
628 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
629 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
630 .access = PL1_RW, .writefn = vmsa_ttbcr_write,
631 .resetfn = vmsa_ttbcr_reset,
632 .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
633 { .name = "DFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
634 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_data),
635 .resetvalue = 0, },
636 REGINFO_SENTINEL
637 };
638
639 static int omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
640 uint64_t value)
641 {
642 env->cp15.c15_ticonfig = value & 0xe7;
643 /* The OS_TYPE bit in this register changes the reported CPUID! */
644 env->cp15.c0_cpuid = (value & (1 << 5)) ?
645 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
646 return 0;
647 }
648
649 static int omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
650 uint64_t value)
651 {
652 env->cp15.c15_threadid = value & 0xffff;
653 return 0;
654 }
655
656 static int omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
657 uint64_t value)
658 {
659 /* Wait-for-interrupt (deprecated) */
660 cpu_interrupt(env, CPU_INTERRUPT_HALT);
661 return 0;
662 }
663
664 static int omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
665 uint64_t value)
666 {
667 /* On OMAP there are registers indicating the max/min index of dcache lines
668 * containing a dirty line; cache flush operations have to reset these.
669 */
670 env->cp15.c15_i_max = 0x000;
671 env->cp15.c15_i_min = 0xff0;
672 return 0;
673 }
674
675 static const ARMCPRegInfo omap_cp_reginfo[] = {
676 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
677 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
678 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
679 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
680 .access = PL1_RW, .type = ARM_CP_NOP },
681 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
682 .access = PL1_RW,
683 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
684 .writefn = omap_ticonfig_write },
685 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
686 .access = PL1_RW,
687 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
688 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
689 .access = PL1_RW, .resetvalue = 0xff0,
690 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
691 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
692 .access = PL1_RW,
693 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
694 .writefn = omap_threadid_write },
695 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
696 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
697 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
698 /* TODO: Peripheral port remap register:
699 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
700 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
701 * when MMU is off.
702 */
703 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
704 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, .type = ARM_CP_OVERRIDE,
705 .writefn = omap_cachemaint_write },
706 REGINFO_SENTINEL
707 };
708
709 static int xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
710 uint64_t value)
711 {
712 value &= 0x3fff;
713 if (env->cp15.c15_cpar != value) {
714 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
715 tb_flush(env);
716 env->cp15.c15_cpar = value;
717 }
718 return 0;
719 }
720
721 static const ARMCPRegInfo xscale_cp_reginfo[] = {
722 { .name = "XSCALE_CPAR",
723 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
724 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
725 .writefn = xscale_cpar_write, },
726 REGINFO_SENTINEL
727 };
728
729 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
730 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
731 * implementation of this implementation-defined space.
732 * Ideally this should eventually disappear in favour of actually
733 * implementing the correct behaviour for all cores.
734 */
735 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
736 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
737 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
738 REGINFO_SENTINEL
739 };
740
741 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
742 /* Cache status: RAZ because we have no cache so it's always clean */
743 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
744 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
745 REGINFO_SENTINEL
746 };
747
748 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
749 /* We never have a a block transfer operation in progress */
750 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
751 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
752 REGINFO_SENTINEL
753 };
754
755 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
756 /* The cache test-and-clean instructions always return (1 << 30)
757 * to indicate that there are no dirty cache lines.
758 */
759 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
760 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = (1 << 30) },
761 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
762 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = (1 << 30) },
763 REGINFO_SENTINEL
764 };
765
766 void register_cp_regs_for_features(ARMCPU *cpu)
767 {
768 /* Register all the coprocessor registers based on feature bits */
769 CPUARMState *env = &cpu->env;
770 if (arm_feature(env, ARM_FEATURE_M)) {
771 /* M profile has no coprocessor registers */
772 return;
773 }
774
775 define_arm_cp_regs(cpu, cp_reginfo);
776 if (arm_feature(env, ARM_FEATURE_V6)) {
777 define_arm_cp_regs(cpu, v6_cp_reginfo);
778 } else {
779 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
780 }
781 if (arm_feature(env, ARM_FEATURE_V6K)) {
782 define_arm_cp_regs(cpu, v6k_cp_reginfo);
783 }
784 if (arm_feature(env, ARM_FEATURE_V7)) {
785 /* v7 performance monitor control register: same implementor
786 * field as main ID register, and we implement no event counters.
787 */
788 ARMCPRegInfo pmcr = {
789 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
790 .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
791 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
792 .readfn = pmreg_read, .writefn = pmcr_write
793 };
794 define_one_arm_cp_reg(cpu, &pmcr);
795 define_arm_cp_regs(cpu, v7_cp_reginfo);
796 } else {
797 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
798 }
799 if (arm_feature(env, ARM_FEATURE_MPU)) {
800 /* These are the MPU registers prior to PMSAv6. Any new
801 * PMSA core later than the ARM946 will require that we
802 * implement the PMSAv6 or PMSAv7 registers, which are
803 * completely different.
804 */
805 assert(!arm_feature(env, ARM_FEATURE_V6));
806 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
807 } else {
808 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
809 }
810 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
811 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
812 }
813 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
814 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
815 }
816 if (arm_feature(env, ARM_FEATURE_VAPA)) {
817 define_arm_cp_regs(cpu, vapa_cp_reginfo);
818 }
819 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
820 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
821 }
822 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
823 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
824 }
825 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
826 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
827 }
828 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
829 define_arm_cp_regs(cpu, omap_cp_reginfo);
830 }
831 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
832 define_arm_cp_regs(cpu, xscale_cp_reginfo);
833 }
834 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
835 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
836 }
837 }
838
839 ARMCPU *cpu_arm_init(const char *cpu_model)
840 {
841 ARMCPU *cpu;
842 CPUARMState *env;
843 static int inited = 0;
844
845 if (!object_class_by_name(cpu_model)) {
846 return NULL;
847 }
848 cpu = ARM_CPU(object_new(cpu_model));
849 env = &cpu->env;
850 env->cpu_model_str = cpu_model;
851 arm_cpu_realize(cpu);
852
853 if (tcg_enabled() && !inited) {
854 inited = 1;
855 arm_translate_init();
856 }
857
858 cpu_reset(CPU(cpu));
859 if (arm_feature(env, ARM_FEATURE_NEON)) {
860 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
861 51, "arm-neon.xml", 0);
862 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
863 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
864 35, "arm-vfp3.xml", 0);
865 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
866 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
867 19, "arm-vfp.xml", 0);
868 }
869 qemu_init_vcpu(env);
870 return cpu;
871 }
872
873 typedef struct ARMCPUListState {
874 fprintf_function cpu_fprintf;
875 FILE *file;
876 } ARMCPUListState;
877
878 /* Sort alphabetically by type name, except for "any". */
879 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
880 {
881 ObjectClass *class_a = (ObjectClass *)a;
882 ObjectClass *class_b = (ObjectClass *)b;
883 const char *name_a, *name_b;
884
885 name_a = object_class_get_name(class_a);
886 name_b = object_class_get_name(class_b);
887 if (strcmp(name_a, "any") == 0) {
888 return 1;
889 } else if (strcmp(name_b, "any") == 0) {
890 return -1;
891 } else {
892 return strcmp(name_a, name_b);
893 }
894 }
895
896 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
897 {
898 ObjectClass *oc = data;
899 ARMCPUListState *s = user_data;
900
901 (*s->cpu_fprintf)(s->file, " %s\n",
902 object_class_get_name(oc));
903 }
904
905 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
906 {
907 ARMCPUListState s = {
908 .file = f,
909 .cpu_fprintf = cpu_fprintf,
910 };
911 GSList *list;
912
913 list = object_class_get_list(TYPE_ARM_CPU, false);
914 list = g_slist_sort(list, arm_cpu_list_compare);
915 (*cpu_fprintf)(f, "Available CPUs:\n");
916 g_slist_foreach(list, arm_cpu_list_entry, &s);
917 g_slist_free(list);
918 }
919
920 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
921 const ARMCPRegInfo *r, void *opaque)
922 {
923 /* Define implementations of coprocessor registers.
924 * We store these in a hashtable because typically
925 * there are less than 150 registers in a space which
926 * is 16*16*16*8*8 = 262144 in size.
927 * Wildcarding is supported for the crm, opc1 and opc2 fields.
928 * If a register is defined twice then the second definition is
929 * used, so this can be used to define some generic registers and
930 * then override them with implementation specific variations.
931 * At least one of the original and the second definition should
932 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
933 * against accidental use.
934 */
935 int crm, opc1, opc2;
936 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
937 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
938 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
939 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
940 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
941 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
942 /* 64 bit registers have only CRm and Opc1 fields */
943 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
944 /* Check that the register definition has enough info to handle
945 * reads and writes if they are permitted.
946 */
947 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
948 if (r->access & PL3_R) {
949 assert(r->fieldoffset || r->readfn);
950 }
951 if (r->access & PL3_W) {
952 assert(r->fieldoffset || r->writefn);
953 }
954 }
955 /* Bad type field probably means missing sentinel at end of reg list */
956 assert(cptype_valid(r->type));
957 for (crm = crmmin; crm <= crmmax; crm++) {
958 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
959 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
960 uint32_t *key = g_new(uint32_t, 1);
961 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
962 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
963 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
964 r2->opaque = opaque;
965 /* Make sure reginfo passed to helpers for wildcarded regs
966 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
967 */
968 r2->crm = crm;
969 r2->opc1 = opc1;
970 r2->opc2 = opc2;
971 /* Overriding of an existing definition must be explicitly
972 * requested.
973 */
974 if (!(r->type & ARM_CP_OVERRIDE)) {
975 ARMCPRegInfo *oldreg;
976 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
977 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
978 fprintf(stderr, "Register redefined: cp=%d %d bit "
979 "crn=%d crm=%d opc1=%d opc2=%d, "
980 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
981 r2->crn, r2->crm, r2->opc1, r2->opc2,
982 oldreg->name, r2->name);
983 assert(0);
984 }
985 }
986 g_hash_table_insert(cpu->cp_regs, key, r2);
987 }
988 }
989 }
990 }
991
992 void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
993 const ARMCPRegInfo *regs, void *opaque)
994 {
995 /* Define a whole list of registers */
996 const ARMCPRegInfo *r;
997 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
998 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
999 }
1000 }
1001
1002 const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
1003 {
1004 return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
1005 }
1006
1007 int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
1008 uint64_t value)
1009 {
1010 /* Helper coprocessor write function for write-ignore registers */
1011 return 0;
1012 }
1013
1014 int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1015 {
1016 /* Helper coprocessor write function for read-as-zero registers */
1017 *value = 0;
1018 return 0;
1019 }
1020
1021 static int bad_mode_switch(CPUARMState *env, int mode)
1022 {
1023 /* Return true if it is not valid for us to switch to
1024 * this CPU mode (ie all the UNPREDICTABLE cases in
1025 * the ARM ARM CPSRWriteByInstr pseudocode).
1026 */
1027 switch (mode) {
1028 case ARM_CPU_MODE_USR:
1029 case ARM_CPU_MODE_SYS:
1030 case ARM_CPU_MODE_SVC:
1031 case ARM_CPU_MODE_ABT:
1032 case ARM_CPU_MODE_UND:
1033 case ARM_CPU_MODE_IRQ:
1034 case ARM_CPU_MODE_FIQ:
1035 return 0;
1036 default:
1037 return 1;
1038 }
1039 }
1040
1041 uint32_t cpsr_read(CPUARMState *env)
1042 {
1043 int ZF;
1044 ZF = (env->ZF == 0);
1045 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
1046 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
1047 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
1048 | ((env->condexec_bits & 0xfc) << 8)
1049 | (env->GE << 16);
1050 }
1051
1052 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
1053 {
1054 if (mask & CPSR_NZCV) {
1055 env->ZF = (~val) & CPSR_Z;
1056 env->NF = val;
1057 env->CF = (val >> 29) & 1;
1058 env->VF = (val << 3) & 0x80000000;
1059 }
1060 if (mask & CPSR_Q)
1061 env->QF = ((val & CPSR_Q) != 0);
1062 if (mask & CPSR_T)
1063 env->thumb = ((val & CPSR_T) != 0);
1064 if (mask & CPSR_IT_0_1) {
1065 env->condexec_bits &= ~3;
1066 env->condexec_bits |= (val >> 25) & 3;
1067 }
1068 if (mask & CPSR_IT_2_7) {
1069 env->condexec_bits &= 3;
1070 env->condexec_bits |= (val >> 8) & 0xfc;
1071 }
1072 if (mask & CPSR_GE) {
1073 env->GE = (val >> 16) & 0xf;
1074 }
1075
1076 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
1077 if (bad_mode_switch(env, val & CPSR_M)) {
1078 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
1079 * We choose to ignore the attempt and leave the CPSR M field
1080 * untouched.
1081 */
1082 mask &= ~CPSR_M;
1083 } else {
1084 switch_mode(env, val & CPSR_M);
1085 }
1086 }
1087 mask &= ~CACHED_CPSR_BITS;
1088 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
1089 }
1090
1091 /* Sign/zero extend */
1092 uint32_t HELPER(sxtb16)(uint32_t x)
1093 {
1094 uint32_t res;
1095 res = (uint16_t)(int8_t)x;
1096 res |= (uint32_t)(int8_t)(x >> 16) << 16;
1097 return res;
1098 }
1099
1100 uint32_t HELPER(uxtb16)(uint32_t x)
1101 {
1102 uint32_t res;
1103 res = (uint16_t)(uint8_t)x;
1104 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
1105 return res;
1106 }
1107
1108 uint32_t HELPER(clz)(uint32_t x)
1109 {
1110 return clz32(x);
1111 }
1112
1113 int32_t HELPER(sdiv)(int32_t num, int32_t den)
1114 {
1115 if (den == 0)
1116 return 0;
1117 if (num == INT_MIN && den == -1)
1118 return INT_MIN;
1119 return num / den;
1120 }
1121
1122 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
1123 {
1124 if (den == 0)
1125 return 0;
1126 return num / den;
1127 }
1128
1129 uint32_t HELPER(rbit)(uint32_t x)
1130 {
1131 x = ((x & 0xff000000) >> 24)
1132 | ((x & 0x00ff0000) >> 8)
1133 | ((x & 0x0000ff00) << 8)
1134 | ((x & 0x000000ff) << 24);
1135 x = ((x & 0xf0f0f0f0) >> 4)
1136 | ((x & 0x0f0f0f0f) << 4);
1137 x = ((x & 0x88888888) >> 3)
1138 | ((x & 0x44444444) >> 1)
1139 | ((x & 0x22222222) << 1)
1140 | ((x & 0x11111111) << 3);
1141 return x;
1142 }
1143
1144 uint32_t HELPER(abs)(uint32_t x)
1145 {
1146 return ((int32_t)x < 0) ? -x : x;
1147 }
1148
1149 #if defined(CONFIG_USER_ONLY)
1150
1151 void do_interrupt (CPUARMState *env)
1152 {
1153 env->exception_index = -1;
1154 }
1155
1156 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
1157 int mmu_idx)
1158 {
1159 if (rw == 2) {
1160 env->exception_index = EXCP_PREFETCH_ABORT;
1161 env->cp15.c6_insn = address;
1162 } else {
1163 env->exception_index = EXCP_DATA_ABORT;
1164 env->cp15.c6_data = address;
1165 }
1166 return 1;
1167 }
1168
1169 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1170 {
1171 cpu_abort(env, "cp15 insn %08x\n", insn);
1172 }
1173
1174 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
1175 {
1176 cpu_abort(env, "cp15 insn %08x\n", insn);
1177 }
1178
1179 /* These should probably raise undefined insn exceptions. */
1180 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
1181 {
1182 cpu_abort(env, "v7m_mrs %d\n", reg);
1183 }
1184
1185 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
1186 {
1187 cpu_abort(env, "v7m_mrs %d\n", reg);
1188 return 0;
1189 }
1190
1191 void switch_mode(CPUARMState *env, int mode)
1192 {
1193 if (mode != ARM_CPU_MODE_USR)
1194 cpu_abort(env, "Tried to switch out of user mode\n");
1195 }
1196
1197 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
1198 {
1199 cpu_abort(env, "banked r13 write\n");
1200 }
1201
1202 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
1203 {
1204 cpu_abort(env, "banked r13 read\n");
1205 return 0;
1206 }
1207
1208 #else
1209
1210 /* Map CPU modes onto saved register banks. */
1211 static inline int bank_number(CPUARMState *env, int mode)
1212 {
1213 switch (mode) {
1214 case ARM_CPU_MODE_USR:
1215 case ARM_CPU_MODE_SYS:
1216 return 0;
1217 case ARM_CPU_MODE_SVC:
1218 return 1;
1219 case ARM_CPU_MODE_ABT:
1220 return 2;
1221 case ARM_CPU_MODE_UND:
1222 return 3;
1223 case ARM_CPU_MODE_IRQ:
1224 return 4;
1225 case ARM_CPU_MODE_FIQ:
1226 return 5;
1227 }
1228 cpu_abort(env, "Bad mode %x\n", mode);
1229 return -1;
1230 }
1231
1232 void switch_mode(CPUARMState *env, int mode)
1233 {
1234 int old_mode;
1235 int i;
1236
1237 old_mode = env->uncached_cpsr & CPSR_M;
1238 if (mode == old_mode)
1239 return;
1240
1241 if (old_mode == ARM_CPU_MODE_FIQ) {
1242 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
1243 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
1244 } else if (mode == ARM_CPU_MODE_FIQ) {
1245 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
1246 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
1247 }
1248
1249 i = bank_number(env, old_mode);
1250 env->banked_r13[i] = env->regs[13];
1251 env->banked_r14[i] = env->regs[14];
1252 env->banked_spsr[i] = env->spsr;
1253
1254 i = bank_number(env, mode);
1255 env->regs[13] = env->banked_r13[i];
1256 env->regs[14] = env->banked_r14[i];
1257 env->spsr = env->banked_spsr[i];
1258 }
1259
1260 static void v7m_push(CPUARMState *env, uint32_t val)
1261 {
1262 env->regs[13] -= 4;
1263 stl_phys(env->regs[13], val);
1264 }
1265
1266 static uint32_t v7m_pop(CPUARMState *env)
1267 {
1268 uint32_t val;
1269 val = ldl_phys(env->regs[13]);
1270 env->regs[13] += 4;
1271 return val;
1272 }
1273
1274 /* Switch to V7M main or process stack pointer. */
1275 static void switch_v7m_sp(CPUARMState *env, int process)
1276 {
1277 uint32_t tmp;
1278 if (env->v7m.current_sp != process) {
1279 tmp = env->v7m.other_sp;
1280 env->v7m.other_sp = env->regs[13];
1281 env->regs[13] = tmp;
1282 env->v7m.current_sp = process;
1283 }
1284 }
1285
1286 static void do_v7m_exception_exit(CPUARMState *env)
1287 {
1288 uint32_t type;
1289 uint32_t xpsr;
1290
1291 type = env->regs[15];
1292 if (env->v7m.exception != 0)
1293 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
1294
1295 /* Switch to the target stack. */
1296 switch_v7m_sp(env, (type & 4) != 0);
1297 /* Pop registers. */
1298 env->regs[0] = v7m_pop(env);
1299 env->regs[1] = v7m_pop(env);
1300 env->regs[2] = v7m_pop(env);
1301 env->regs[3] = v7m_pop(env);
1302 env->regs[12] = v7m_pop(env);
1303 env->regs[14] = v7m_pop(env);
1304 env->regs[15] = v7m_pop(env);
1305 xpsr = v7m_pop(env);
1306 xpsr_write(env, xpsr, 0xfffffdff);
1307 /* Undo stack alignment. */
1308 if (xpsr & 0x200)
1309 env->regs[13] |= 4;
1310 /* ??? The exception return type specifies Thread/Handler mode. However
1311 this is also implied by the xPSR value. Not sure what to do
1312 if there is a mismatch. */
1313 /* ??? Likewise for mismatches between the CONTROL register and the stack
1314 pointer. */
1315 }
1316
1317 static void do_interrupt_v7m(CPUARMState *env)
1318 {
1319 uint32_t xpsr = xpsr_read(env);
1320 uint32_t lr;
1321 uint32_t addr;
1322
1323 lr = 0xfffffff1;
1324 if (env->v7m.current_sp)
1325 lr |= 4;
1326 if (env->v7m.exception == 0)
1327 lr |= 8;
1328
1329 /* For exceptions we just mark as pending on the NVIC, and let that
1330 handle it. */
1331 /* TODO: Need to escalate if the current priority is higher than the
1332 one we're raising. */
1333 switch (env->exception_index) {
1334 case EXCP_UDEF:
1335 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
1336 return;
1337 case EXCP_SWI:
1338 env->regs[15] += 2;
1339 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
1340 return;
1341 case EXCP_PREFETCH_ABORT:
1342 case EXCP_DATA_ABORT:
1343 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
1344 return;
1345 case EXCP_BKPT:
1346 if (semihosting_enabled) {
1347 int nr;
1348 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
1349 if (nr == 0xab) {
1350 env->regs[15] += 2;
1351 env->regs[0] = do_arm_semihosting(env);
1352 return;
1353 }
1354 }
1355 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
1356 return;
1357 case EXCP_IRQ:
1358 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
1359 break;
1360 case EXCP_EXCEPTION_EXIT:
1361 do_v7m_exception_exit(env);
1362 return;
1363 default:
1364 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
1365 return; /* Never happens. Keep compiler happy. */
1366 }
1367
1368 /* Align stack pointer. */
1369 /* ??? Should only do this if Configuration Control Register
1370 STACKALIGN bit is set. */
1371 if (env->regs[13] & 4) {
1372 env->regs[13] -= 4;
1373 xpsr |= 0x200;
1374 }
1375 /* Switch to the handler mode. */
1376 v7m_push(env, xpsr);
1377 v7m_push(env, env->regs[15]);
1378 v7m_push(env, env->regs[14]);
1379 v7m_push(env, env->regs[12]);
1380 v7m_push(env, env->regs[3]);
1381 v7m_push(env, env->regs[2]);
1382 v7m_push(env, env->regs[1]);
1383 v7m_push(env, env->regs[0]);
1384 switch_v7m_sp(env, 0);
1385 /* Clear IT bits */
1386 env->condexec_bits = 0;
1387 env->regs[14] = lr;
1388 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
1389 env->regs[15] = addr & 0xfffffffe;
1390 env->thumb = addr & 1;
1391 }
1392
1393 /* Handle a CPU exception. */
1394 void do_interrupt(CPUARMState *env)
1395 {
1396 uint32_t addr;
1397 uint32_t mask;
1398 int new_mode;
1399 uint32_t offset;
1400
1401 if (IS_M(env)) {
1402 do_interrupt_v7m(env);
1403 return;
1404 }
1405 /* TODO: Vectored interrupt controller. */
1406 switch (env->exception_index) {
1407 case EXCP_UDEF:
1408 new_mode = ARM_CPU_MODE_UND;
1409 addr = 0x04;
1410 mask = CPSR_I;
1411 if (env->thumb)
1412 offset = 2;
1413 else
1414 offset = 4;
1415 break;
1416 case EXCP_SWI:
1417 if (semihosting_enabled) {
1418 /* Check for semihosting interrupt. */
1419 if (env->thumb) {
1420 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
1421 } else {
1422 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
1423 & 0xffffff;
1424 }
1425 /* Only intercept calls from privileged modes, to provide some
1426 semblance of security. */
1427 if (((mask == 0x123456 && !env->thumb)
1428 || (mask == 0xab && env->thumb))
1429 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
1430 env->regs[0] = do_arm_semihosting(env);
1431 return;
1432 }
1433 }
1434 new_mode = ARM_CPU_MODE_SVC;
1435 addr = 0x08;
1436 mask = CPSR_I;
1437 /* The PC already points to the next instruction. */
1438 offset = 0;
1439 break;
1440 case EXCP_BKPT:
1441 /* See if this is a semihosting syscall. */
1442 if (env->thumb && semihosting_enabled) {
1443 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
1444 if (mask == 0xab
1445 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
1446 env->regs[15] += 2;
1447 env->regs[0] = do_arm_semihosting(env);
1448 return;
1449 }
1450 }
1451 env->cp15.c5_insn = 2;
1452 /* Fall through to prefetch abort. */
1453 case EXCP_PREFETCH_ABORT:
1454 new_mode = ARM_CPU_MODE_ABT;
1455 addr = 0x0c;
1456 mask = CPSR_A | CPSR_I;
1457 offset = 4;
1458 break;
1459 case EXCP_DATA_ABORT:
1460 new_mode = ARM_CPU_MODE_ABT;
1461 addr = 0x10;
1462 mask = CPSR_A | CPSR_I;
1463 offset = 8;
1464 break;
1465 case EXCP_IRQ:
1466 new_mode = ARM_CPU_MODE_IRQ;
1467 addr = 0x18;
1468 /* Disable IRQ and imprecise data aborts. */
1469 mask = CPSR_A | CPSR_I;
1470 offset = 4;
1471 break;
1472 case EXCP_FIQ:
1473 new_mode = ARM_CPU_MODE_FIQ;
1474 addr = 0x1c;
1475 /* Disable FIQ, IRQ and imprecise data aborts. */
1476 mask = CPSR_A | CPSR_I | CPSR_F;
1477 offset = 4;
1478 break;
1479 default:
1480 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
1481 return; /* Never happens. Keep compiler happy. */
1482 }
1483 /* High vectors. */
1484 if (env->cp15.c1_sys & (1 << 13)) {
1485 addr += 0xffff0000;
1486 }
1487 switch_mode (env, new_mode);
1488 env->spsr = cpsr_read(env);
1489 /* Clear IT bits. */
1490 env->condexec_bits = 0;
1491 /* Switch to the new mode, and to the correct instruction set. */
1492 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
1493 env->uncached_cpsr |= mask;
1494 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
1495 * and we should just guard the thumb mode on V4 */
1496 if (arm_feature(env, ARM_FEATURE_V4T)) {
1497 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
1498 }
1499 env->regs[14] = env->regs[15] + offset;
1500 env->regs[15] = addr;
1501 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1502 }
1503
1504 /* Check section/page access permissions.
1505 Returns the page protection flags, or zero if the access is not
1506 permitted. */
1507 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
1508 int access_type, int is_user)
1509 {
1510 int prot_ro;
1511
1512 if (domain_prot == 3) {
1513 return PAGE_READ | PAGE_WRITE;
1514 }
1515
1516 if (access_type == 1)
1517 prot_ro = 0;
1518 else
1519 prot_ro = PAGE_READ;
1520
1521 switch (ap) {
1522 case 0:
1523 if (access_type == 1)
1524 return 0;
1525 switch ((env->cp15.c1_sys >> 8) & 3) {
1526 case 1:
1527 return is_user ? 0 : PAGE_READ;
1528 case 2:
1529 return PAGE_READ;
1530 default:
1531 return 0;
1532 }
1533 case 1:
1534 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
1535 case 2:
1536 if (is_user)
1537 return prot_ro;
1538 else
1539 return PAGE_READ | PAGE_WRITE;
1540 case 3:
1541 return PAGE_READ | PAGE_WRITE;
1542 case 4: /* Reserved. */
1543 return 0;
1544 case 5:
1545 return is_user ? 0 : prot_ro;
1546 case 6:
1547 return prot_ro;
1548 case 7:
1549 if (!arm_feature (env, ARM_FEATURE_V6K))
1550 return 0;
1551 return prot_ro;
1552 default:
1553 abort();
1554 }
1555 }
1556
1557 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
1558 {
1559 uint32_t table;
1560
1561 if (address & env->cp15.c2_mask)
1562 table = env->cp15.c2_base1 & 0xffffc000;
1563 else
1564 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
1565
1566 table |= (address >> 18) & 0x3ffc;
1567 return table;
1568 }
1569
1570 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
1571 int is_user, uint32_t *phys_ptr, int *prot,
1572 target_ulong *page_size)
1573 {
1574 int code;
1575 uint32_t table;
1576 uint32_t desc;
1577 int type;
1578 int ap;
1579 int domain;
1580 int domain_prot;
1581 uint32_t phys_addr;
1582
1583 /* Pagetable walk. */
1584 /* Lookup l1 descriptor. */
1585 table = get_level1_table_address(env, address);
1586 desc = ldl_phys(table);
1587 type = (desc & 3);
1588 domain = (desc >> 5) & 0x0f;
1589 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1590 if (type == 0) {
1591 /* Section translation fault. */
1592 code = 5;
1593 goto do_fault;
1594 }
1595 if (domain_prot == 0 || domain_prot == 2) {
1596 if (type == 2)
1597 code = 9; /* Section domain fault. */
1598 else
1599 code = 11; /* Page domain fault. */
1600 goto do_fault;
1601 }
1602 if (type == 2) {
1603 /* 1Mb section. */
1604 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1605 ap = (desc >> 10) & 3;
1606 code = 13;
1607 *page_size = 1024 * 1024;
1608 } else {
1609 /* Lookup l2 entry. */
1610 if (type == 1) {
1611 /* Coarse pagetable. */
1612 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1613 } else {
1614 /* Fine pagetable. */
1615 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1616 }
1617 desc = ldl_phys(table);
1618 switch (desc & 3) {
1619 case 0: /* Page translation fault. */
1620 code = 7;
1621 goto do_fault;
1622 case 1: /* 64k page. */
1623 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1624 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1625 *page_size = 0x10000;
1626 break;
1627 case 2: /* 4k page. */
1628 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1629 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1630 *page_size = 0x1000;
1631 break;
1632 case 3: /* 1k page. */
1633 if (type == 1) {
1634 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1635 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1636 } else {
1637 /* Page translation fault. */
1638 code = 7;
1639 goto do_fault;
1640 }
1641 } else {
1642 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1643 }
1644 ap = (desc >> 4) & 3;
1645 *page_size = 0x400;
1646 break;
1647 default:
1648 /* Never happens, but compiler isn't smart enough to tell. */
1649 abort();
1650 }
1651 code = 15;
1652 }
1653 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1654 if (!*prot) {
1655 /* Access permission fault. */
1656 goto do_fault;
1657 }
1658 *prot |= PAGE_EXEC;
1659 *phys_ptr = phys_addr;
1660 return 0;
1661 do_fault:
1662 return code | (domain << 4);
1663 }
1664
1665 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
1666 int is_user, uint32_t *phys_ptr, int *prot,
1667 target_ulong *page_size)
1668 {
1669 int code;
1670 uint32_t table;
1671 uint32_t desc;
1672 uint32_t xn;
1673 int type;
1674 int ap;
1675 int domain;
1676 int domain_prot;
1677 uint32_t phys_addr;
1678
1679 /* Pagetable walk. */
1680 /* Lookup l1 descriptor. */
1681 table = get_level1_table_address(env, address);
1682 desc = ldl_phys(table);
1683 type = (desc & 3);
1684 if (type == 0) {
1685 /* Section translation fault. */
1686 code = 5;
1687 domain = 0;
1688 goto do_fault;
1689 } else if (type == 2 && (desc & (1 << 18))) {
1690 /* Supersection. */
1691 domain = 0;
1692 } else {
1693 /* Section or page. */
1694 domain = (desc >> 5) & 0x0f;
1695 }
1696 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1697 if (domain_prot == 0 || domain_prot == 2) {
1698 if (type == 2)
1699 code = 9; /* Section domain fault. */
1700 else
1701 code = 11; /* Page domain fault. */
1702 goto do_fault;
1703 }
1704 if (type == 2) {
1705 if (desc & (1 << 18)) {
1706 /* Supersection. */
1707 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1708 *page_size = 0x1000000;
1709 } else {
1710 /* Section. */
1711 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1712 *page_size = 0x100000;
1713 }
1714 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1715 xn = desc & (1 << 4);
1716 code = 13;
1717 } else {
1718 /* Lookup l2 entry. */
1719 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1720 desc = ldl_phys(table);
1721 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1722 switch (desc & 3) {
1723 case 0: /* Page translation fault. */
1724 code = 7;
1725 goto do_fault;
1726 case 1: /* 64k page. */
1727 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1728 xn = desc & (1 << 15);
1729 *page_size = 0x10000;
1730 break;
1731 case 2: case 3: /* 4k page. */
1732 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1733 xn = desc & 1;
1734 *page_size = 0x1000;
1735 break;
1736 default:
1737 /* Never happens, but compiler isn't smart enough to tell. */
1738 abort();
1739 }
1740 code = 15;
1741 }
1742 if (domain_prot == 3) {
1743 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1744 } else {
1745 if (xn && access_type == 2)
1746 goto do_fault;
1747
1748 /* The simplified model uses AP[0] as an access control bit. */
1749 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1750 /* Access flag fault. */
1751 code = (code == 15) ? 6 : 3;
1752 goto do_fault;
1753 }
1754 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1755 if (!*prot) {
1756 /* Access permission fault. */
1757 goto do_fault;
1758 }
1759 if (!xn) {
1760 *prot |= PAGE_EXEC;
1761 }
1762 }
1763 *phys_ptr = phys_addr;
1764 return 0;
1765 do_fault:
1766 return code | (domain << 4);
1767 }
1768
1769 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
1770 int is_user, uint32_t *phys_ptr, int *prot)
1771 {
1772 int n;
1773 uint32_t mask;
1774 uint32_t base;
1775
1776 *phys_ptr = address;
1777 for (n = 7; n >= 0; n--) {
1778 base = env->cp15.c6_region[n];
1779 if ((base & 1) == 0)
1780 continue;
1781 mask = 1 << ((base >> 1) & 0x1f);
1782 /* Keep this shift separate from the above to avoid an
1783 (undefined) << 32. */
1784 mask = (mask << 1) - 1;
1785 if (((base ^ address) & ~mask) == 0)
1786 break;
1787 }
1788 if (n < 0)
1789 return 2;
1790
1791 if (access_type == 2) {
1792 mask = env->cp15.c5_insn;
1793 } else {
1794 mask = env->cp15.c5_data;
1795 }
1796 mask = (mask >> (n * 4)) & 0xf;
1797 switch (mask) {
1798 case 0:
1799 return 1;
1800 case 1:
1801 if (is_user)
1802 return 1;
1803 *prot = PAGE_READ | PAGE_WRITE;
1804 break;
1805 case 2:
1806 *prot = PAGE_READ;
1807 if (!is_user)
1808 *prot |= PAGE_WRITE;
1809 break;
1810 case 3:
1811 *prot = PAGE_READ | PAGE_WRITE;
1812 break;
1813 case 5:
1814 if (is_user)
1815 return 1;
1816 *prot = PAGE_READ;
1817 break;
1818 case 6:
1819 *prot = PAGE_READ;
1820 break;
1821 default:
1822 /* Bad permission. */
1823 return 1;
1824 }
1825 *prot |= PAGE_EXEC;
1826 return 0;
1827 }
1828
1829 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
1830 int access_type, int is_user,
1831 uint32_t *phys_ptr, int *prot,
1832 target_ulong *page_size)
1833 {
1834 /* Fast Context Switch Extension. */
1835 if (address < 0x02000000)
1836 address += env->cp15.c13_fcse;
1837
1838 if ((env->cp15.c1_sys & 1) == 0) {
1839 /* MMU/MPU disabled. */
1840 *phys_ptr = address;
1841 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1842 *page_size = TARGET_PAGE_SIZE;
1843 return 0;
1844 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1845 *page_size = TARGET_PAGE_SIZE;
1846 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1847 prot);
1848 } else if (env->cp15.c1_sys & (1 << 23)) {
1849 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1850 prot, page_size);
1851 } else {
1852 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1853 prot, page_size);
1854 }
1855 }
1856
1857 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
1858 int access_type, int mmu_idx)
1859 {
1860 uint32_t phys_addr;
1861 target_ulong page_size;
1862 int prot;
1863 int ret, is_user;
1864
1865 is_user = mmu_idx == MMU_USER_IDX;
1866 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1867 &page_size);
1868 if (ret == 0) {
1869 /* Map a single [sub]page. */
1870 phys_addr &= ~(uint32_t)0x3ff;
1871 address &= ~(uint32_t)0x3ff;
1872 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1873 return 0;
1874 }
1875
1876 if (access_type == 2) {
1877 env->cp15.c5_insn = ret;
1878 env->cp15.c6_insn = address;
1879 env->exception_index = EXCP_PREFETCH_ABORT;
1880 } else {
1881 env->cp15.c5_data = ret;
1882 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1883 env->cp15.c5_data |= (1 << 11);
1884 env->cp15.c6_data = address;
1885 env->exception_index = EXCP_DATA_ABORT;
1886 }
1887 return 1;
1888 }
1889
1890 target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
1891 {
1892 uint32_t phys_addr;
1893 target_ulong page_size;
1894 int prot;
1895 int ret;
1896
1897 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1898
1899 if (ret != 0)
1900 return -1;
1901
1902 return phys_addr;
1903 }
1904
1905 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1906 {
1907 int op1;
1908 int op2;
1909 int crm;
1910
1911 op1 = (insn >> 21) & 7;
1912 op2 = (insn >> 5) & 7;
1913 crm = insn & 0xf;
1914 switch ((insn >> 16) & 0xf) {
1915 case 0:
1916 /* ID codes. */
1917 if (arm_feature(env, ARM_FEATURE_XSCALE))
1918 break;
1919 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1920 break;
1921 if (arm_feature(env, ARM_FEATURE_V7)
1922 && op1 == 2 && crm == 0 && op2 == 0) {
1923 env->cp15.c0_cssel = val & 0xf;
1924 break;
1925 }
1926 goto bad_reg;
1927 case 1: /* System configuration. */
1928 if (arm_feature(env, ARM_FEATURE_V7)
1929 && op1 == 0 && crm == 1 && op2 == 0) {
1930 env->cp15.c1_scr = val;
1931 break;
1932 }
1933 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1934 op2 = 0;
1935 switch (op2) {
1936 case 0:
1937 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1938 env->cp15.c1_sys = val;
1939 /* ??? Lots of these bits are not implemented. */
1940 /* This may enable/disable the MMU, so do a TLB flush. */
1941 tlb_flush(env, 1);
1942 break;
1943 case 1: /* Auxiliary control register. */
1944 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1945 env->cp15.c1_xscaleauxcr = val;
1946 break;
1947 }
1948 /* Not implemented. */
1949 break;
1950 case 2:
1951 if (arm_feature(env, ARM_FEATURE_XSCALE))
1952 goto bad_reg;
1953 if (env->cp15.c1_coproc != val) {
1954 env->cp15.c1_coproc = val;
1955 /* ??? Is this safe when called from within a TB? */
1956 tb_flush(env);
1957 }
1958 break;
1959 default:
1960 goto bad_reg;
1961 }
1962 break;
1963 case 4: /* Reserved. */
1964 goto bad_reg;
1965 case 9:
1966 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1967 break;
1968 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1969 break; /* Ignore ReadBuffer access */
1970 switch (crm) {
1971 case 0: /* Cache lockdown. */
1972 switch (op1) {
1973 case 0: /* L1 cache. */
1974 switch (op2) {
1975 case 0:
1976 env->cp15.c9_data = val;
1977 break;
1978 case 1:
1979 env->cp15.c9_insn = val;
1980 break;
1981 default:
1982 goto bad_reg;
1983 }
1984 break;
1985 case 1: /* L2 cache. */
1986 /* Ignore writes to L2 lockdown/auxiliary registers. */
1987 break;
1988 default:
1989 goto bad_reg;
1990 }
1991 break;
1992 case 1: /* TCM memory region registers. */
1993 /* Not implemented. */
1994 goto bad_reg;
1995 default:
1996 goto bad_reg;
1997 }
1998 break;
1999 case 12: /* Reserved. */
2000 goto bad_reg;
2001 }
2002 return;
2003 bad_reg:
2004 /* ??? For debugging only. Should raise illegal instruction exception. */
2005 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
2006 (insn >> 16) & 0xf, crm, op1, op2);
2007 }
2008
2009 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
2010 {
2011 int op1;
2012 int op2;
2013 int crm;
2014
2015 op1 = (insn >> 21) & 7;
2016 op2 = (insn >> 5) & 7;
2017 crm = insn & 0xf;
2018 switch ((insn >> 16) & 0xf) {
2019 case 0: /* ID codes. */
2020 switch (op1) {
2021 case 0:
2022 switch (crm) {
2023 case 0:
2024 switch (op2) {
2025 case 0: /* Device ID. */
2026 return env->cp15.c0_cpuid;
2027 case 1: /* Cache Type. */
2028 return env->cp15.c0_cachetype;
2029 case 2: /* TCM status. */
2030 return 0;
2031 case 3: /* TLB type register. */
2032 return 0; /* No lockable TLB entries. */
2033 case 5: /* MPIDR */
2034 /* The MPIDR was standardised in v7; prior to
2035 * this it was implemented only in the 11MPCore.
2036 * For all other pre-v7 cores it does not exist.
2037 */
2038 if (arm_feature(env, ARM_FEATURE_V7) ||
2039 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
2040 int mpidr = env->cpu_index;
2041 /* We don't support setting cluster ID ([8..11])
2042 * so these bits always RAZ.
2043 */
2044 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2045 mpidr |= (1 << 31);
2046 /* Cores which are uniprocessor (non-coherent)
2047 * but still implement the MP extensions set
2048 * bit 30. (For instance, A9UP.) However we do
2049 * not currently model any of those cores.
2050 */
2051 }
2052 return mpidr;
2053 }
2054 /* otherwise fall through to the unimplemented-reg case */
2055 default:
2056 goto bad_reg;
2057 }
2058 case 1:
2059 if (!arm_feature(env, ARM_FEATURE_V6))
2060 goto bad_reg;
2061 return env->cp15.c0_c1[op2];
2062 case 2:
2063 if (!arm_feature(env, ARM_FEATURE_V6))
2064 goto bad_reg;
2065 return env->cp15.c0_c2[op2];
2066 case 3: case 4: case 5: case 6: case 7:
2067 return 0;
2068 default:
2069 goto bad_reg;
2070 }
2071 case 1:
2072 /* These registers aren't documented on arm11 cores. However
2073 Linux looks at them anyway. */
2074 if (!arm_feature(env, ARM_FEATURE_V6))
2075 goto bad_reg;
2076 if (crm != 0)
2077 goto bad_reg;
2078 if (!arm_feature(env, ARM_FEATURE_V7))
2079 return 0;
2080
2081 switch (op2) {
2082 case 0:
2083 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
2084 case 1:
2085 return env->cp15.c0_clid;
2086 case 7:
2087 return 0;
2088 }
2089 goto bad_reg;
2090 case 2:
2091 if (op2 != 0 || crm != 0)
2092 goto bad_reg;
2093 return env->cp15.c0_cssel;
2094 default:
2095 goto bad_reg;
2096 }
2097 case 1: /* System configuration. */
2098 if (arm_feature(env, ARM_FEATURE_V7)
2099 && op1 == 0 && crm == 1 && op2 == 0) {
2100 return env->cp15.c1_scr;
2101 }
2102 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2103 op2 = 0;
2104 switch (op2) {
2105 case 0: /* Control register. */
2106 return env->cp15.c1_sys;
2107 case 1: /* Auxiliary control register. */
2108 if (arm_feature(env, ARM_FEATURE_XSCALE))
2109 return env->cp15.c1_xscaleauxcr;
2110 if (!arm_feature(env, ARM_FEATURE_AUXCR))
2111 goto bad_reg;
2112 switch (ARM_CPUID(env)) {
2113 case ARM_CPUID_ARM1026:
2114 return 1;
2115 case ARM_CPUID_ARM1136:
2116 case ARM_CPUID_ARM1136_R2:
2117 case ARM_CPUID_ARM1176:
2118 return 7;
2119 case ARM_CPUID_ARM11MPCORE:
2120 return 1;
2121 case ARM_CPUID_CORTEXA8:
2122 return 2;
2123 case ARM_CPUID_CORTEXA9:
2124 case ARM_CPUID_CORTEXA15:
2125 return 0;
2126 default:
2127 goto bad_reg;
2128 }
2129 case 2: /* Coprocessor access register. */
2130 if (arm_feature(env, ARM_FEATURE_XSCALE))
2131 goto bad_reg;
2132 return env->cp15.c1_coproc;
2133 default:
2134 goto bad_reg;
2135 }
2136 case 4: /* Reserved. */
2137 goto bad_reg;
2138 case 9:
2139 switch (crm) {
2140 case 0: /* Cache lockdown */
2141 switch (op1) {
2142 case 0: /* L1 cache. */
2143 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2144 return 0;
2145 }
2146 switch (op2) {
2147 case 0:
2148 return env->cp15.c9_data;
2149 case 1:
2150 return env->cp15.c9_insn;
2151 default:
2152 goto bad_reg;
2153 }
2154 case 1: /* L2 cache */
2155 /* L2 Lockdown and Auxiliary control. */
2156 switch (op2) {
2157 case 0:
2158 /* L2 cache lockdown (A8 only) */
2159 return 0;
2160 case 2:
2161 /* L2 cache auxiliary control (A8) or control (A15) */
2162 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
2163 /* Linux wants the number of processors from here.
2164 * Might as well set the interrupt-controller bit too.
2165 */
2166 return ((smp_cpus - 1) << 24) | (1 << 23);
2167 }
2168 return 0;
2169 case 3:
2170 /* L2 cache extended control (A15) */
2171 return 0;
2172 default:
2173 goto bad_reg;
2174 }
2175 default:
2176 goto bad_reg;
2177 }
2178 break;
2179 default:
2180 goto bad_reg;
2181 }
2182 break;
2183 case 11: /* TCM DMA control. */
2184 case 12: /* Reserved. */
2185 goto bad_reg;
2186 }
2187 bad_reg:
2188 /* ??? For debugging only. Should raise illegal instruction exception. */
2189 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2190 (insn >> 16) & 0xf, crm, op1, op2);
2191 return 0;
2192 }
2193
2194 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2195 {
2196 if ((env->uncached_cpsr & CPSR_M) == mode) {
2197 env->regs[13] = val;
2198 } else {
2199 env->banked_r13[bank_number(env, mode)] = val;
2200 }
2201 }
2202
2203 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2204 {
2205 if ((env->uncached_cpsr & CPSR_M) == mode) {
2206 return env->regs[13];
2207 } else {
2208 return env->banked_r13[bank_number(env, mode)];
2209 }
2210 }
2211
2212 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2213 {
2214 switch (reg) {
2215 case 0: /* APSR */
2216 return xpsr_read(env) & 0xf8000000;
2217 case 1: /* IAPSR */
2218 return xpsr_read(env) & 0xf80001ff;
2219 case 2: /* EAPSR */
2220 return xpsr_read(env) & 0xff00fc00;
2221 case 3: /* xPSR */
2222 return xpsr_read(env) & 0xff00fdff;
2223 case 5: /* IPSR */
2224 return xpsr_read(env) & 0x000001ff;
2225 case 6: /* EPSR */
2226 return xpsr_read(env) & 0x0700fc00;
2227 case 7: /* IEPSR */
2228 return xpsr_read(env) & 0x0700edff;
2229 case 8: /* MSP */
2230 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2231 case 9: /* PSP */
2232 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2233 case 16: /* PRIMASK */
2234 return (env->uncached_cpsr & CPSR_I) != 0;
2235 case 17: /* BASEPRI */
2236 case 18: /* BASEPRI_MAX */
2237 return env->v7m.basepri;
2238 case 19: /* FAULTMASK */
2239 return (env->uncached_cpsr & CPSR_F) != 0;
2240 case 20: /* CONTROL */
2241 return env->v7m.control;
2242 default:
2243 /* ??? For debugging only. */
2244 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2245 return 0;
2246 }
2247 }
2248
2249 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2250 {
2251 switch (reg) {
2252 case 0: /* APSR */
2253 xpsr_write(env, val, 0xf8000000);
2254 break;
2255 case 1: /* IAPSR */
2256 xpsr_write(env, val, 0xf8000000);
2257 break;
2258 case 2: /* EAPSR */
2259 xpsr_write(env, val, 0xfe00fc00);
2260 break;
2261 case 3: /* xPSR */
2262 xpsr_write(env, val, 0xfe00fc00);
2263 break;
2264 case 5: /* IPSR */
2265 /* IPSR bits are readonly. */
2266 break;
2267 case 6: /* EPSR */
2268 xpsr_write(env, val, 0x0600fc00);
2269 break;
2270 case 7: /* IEPSR */
2271 xpsr_write(env, val, 0x0600fc00);
2272 break;
2273 case 8: /* MSP */
2274 if (env->v7m.current_sp)
2275 env->v7m.other_sp = val;
2276 else
2277 env->regs[13] = val;
2278 break;
2279 case 9: /* PSP */
2280 if (env->v7m.current_sp)
2281 env->regs[13] = val;
2282 else
2283 env->v7m.other_sp = val;
2284 break;
2285 case 16: /* PRIMASK */
2286 if (val & 1)
2287 env->uncached_cpsr |= CPSR_I;
2288 else
2289 env->uncached_cpsr &= ~CPSR_I;
2290 break;
2291 case 17: /* BASEPRI */
2292 env->v7m.basepri = val & 0xff;
2293 break;
2294 case 18: /* BASEPRI_MAX */
2295 val &= 0xff;
2296 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2297 env->v7m.basepri = val;
2298 break;
2299 case 19: /* FAULTMASK */
2300 if (val & 1)
2301 env->uncached_cpsr |= CPSR_F;
2302 else
2303 env->uncached_cpsr &= ~CPSR_F;
2304 break;
2305 case 20: /* CONTROL */
2306 env->v7m.control = val & 3;
2307 switch_v7m_sp(env, (val & 2) != 0);
2308 break;
2309 default:
2310 /* ??? For debugging only. */
2311 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2312 return;
2313 }
2314 }
2315
2316 #endif
2317
2318 /* Note that signed overflow is undefined in C. The following routines are
2319 careful to use unsigned types where modulo arithmetic is required.
2320 Failure to do so _will_ break on newer gcc. */
2321
2322 /* Signed saturating arithmetic. */
2323
2324 /* Perform 16-bit signed saturating addition. */
2325 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2326 {
2327 uint16_t res;
2328
2329 res = a + b;
2330 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2331 if (a & 0x8000)
2332 res = 0x8000;
2333 else
2334 res = 0x7fff;
2335 }
2336 return res;
2337 }
2338
2339 /* Perform 8-bit signed saturating addition. */
2340 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2341 {
2342 uint8_t res;
2343
2344 res = a + b;
2345 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2346 if (a & 0x80)
2347 res = 0x80;
2348 else
2349 res = 0x7f;
2350 }
2351 return res;
2352 }
2353
2354 /* Perform 16-bit signed saturating subtraction. */
2355 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2356 {
2357 uint16_t res;
2358
2359 res = a - b;
2360 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2361 if (a & 0x8000)
2362 res = 0x8000;
2363 else
2364 res = 0x7fff;
2365 }
2366 return res;
2367 }
2368
2369 /* Perform 8-bit signed saturating subtraction. */
2370 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2371 {
2372 uint8_t res;
2373
2374 res = a - b;
2375 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2376 if (a & 0x80)
2377 res = 0x80;
2378 else
2379 res = 0x7f;
2380 }
2381 return res;
2382 }
2383
2384 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2385 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2386 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2387 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2388 #define PFX q
2389
2390 #include "op_addsub.h"
2391
2392 /* Unsigned saturating arithmetic. */
2393 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2394 {
2395 uint16_t res;
2396 res = a + b;
2397 if (res < a)
2398 res = 0xffff;
2399 return res;
2400 }
2401
2402 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2403 {
2404 if (a > b)
2405 return a - b;
2406 else
2407 return 0;
2408 }
2409
2410 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2411 {
2412 uint8_t res;
2413 res = a + b;
2414 if (res < a)
2415 res = 0xff;
2416 return res;
2417 }
2418
2419 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2420 {
2421 if (a > b)
2422 return a - b;
2423 else
2424 return 0;
2425 }
2426
2427 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2428 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2429 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2430 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2431 #define PFX uq
2432
2433 #include "op_addsub.h"
2434
2435 /* Signed modulo arithmetic. */
2436 #define SARITH16(a, b, n, op) do { \
2437 int32_t sum; \
2438 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2439 RESULT(sum, n, 16); \
2440 if (sum >= 0) \
2441 ge |= 3 << (n * 2); \
2442 } while(0)
2443
2444 #define SARITH8(a, b, n, op) do { \
2445 int32_t sum; \
2446 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2447 RESULT(sum, n, 8); \
2448 if (sum >= 0) \
2449 ge |= 1 << n; \
2450 } while(0)
2451
2452
2453 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2454 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2455 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2456 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2457 #define PFX s
2458 #define ARITH_GE
2459
2460 #include "op_addsub.h"
2461
2462 /* Unsigned modulo arithmetic. */
2463 #define ADD16(a, b, n) do { \
2464 uint32_t sum; \
2465 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2466 RESULT(sum, n, 16); \
2467 if ((sum >> 16) == 1) \
2468 ge |= 3 << (n * 2); \
2469 } while(0)
2470
2471 #define ADD8(a, b, n) do { \
2472 uint32_t sum; \
2473 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2474 RESULT(sum, n, 8); \
2475 if ((sum >> 8) == 1) \
2476 ge |= 1 << n; \
2477 } while(0)
2478
2479 #define SUB16(a, b, n) do { \
2480 uint32_t sum; \
2481 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2482 RESULT(sum, n, 16); \
2483 if ((sum >> 16) == 0) \
2484 ge |= 3 << (n * 2); \
2485 } while(0)
2486
2487 #define SUB8(a, b, n) do { \
2488 uint32_t sum; \
2489 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2490 RESULT(sum, n, 8); \
2491 if ((sum >> 8) == 0) \
2492 ge |= 1 << n; \
2493 } while(0)
2494
2495 #define PFX u
2496 #define ARITH_GE
2497
2498 #include "op_addsub.h"
2499
2500 /* Halved signed arithmetic. */
2501 #define ADD16(a, b, n) \
2502 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2503 #define SUB16(a, b, n) \
2504 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2505 #define ADD8(a, b, n) \
2506 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2507 #define SUB8(a, b, n) \
2508 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2509 #define PFX sh
2510
2511 #include "op_addsub.h"
2512
2513 /* Halved unsigned arithmetic. */
2514 #define ADD16(a, b, n) \
2515 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2516 #define SUB16(a, b, n) \
2517 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2518 #define ADD8(a, b, n) \
2519 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2520 #define SUB8(a, b, n) \
2521 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2522 #define PFX uh
2523
2524 #include "op_addsub.h"
2525
2526 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2527 {
2528 if (a > b)
2529 return a - b;
2530 else
2531 return b - a;
2532 }
2533
2534 /* Unsigned sum of absolute byte differences. */
2535 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2536 {
2537 uint32_t sum;
2538 sum = do_usad(a, b);
2539 sum += do_usad(a >> 8, b >> 8);
2540 sum += do_usad(a >> 16, b >>16);
2541 sum += do_usad(a >> 24, b >> 24);
2542 return sum;
2543 }
2544
2545 /* For ARMv6 SEL instruction. */
2546 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2547 {
2548 uint32_t mask;
2549
2550 mask = 0;
2551 if (flags & 1)
2552 mask |= 0xff;
2553 if (flags & 2)
2554 mask |= 0xff00;
2555 if (flags & 4)
2556 mask |= 0xff0000;
2557 if (flags & 8)
2558 mask |= 0xff000000;
2559 return (a & mask) | (b & ~mask);
2560 }
2561
2562 uint32_t HELPER(logicq_cc)(uint64_t val)
2563 {
2564 return (val >> 32) | (val != 0);
2565 }
2566
2567 /* VFP support. We follow the convention used for VFP instrunctions:
2568 Single precition routines have a "s" suffix, double precision a
2569 "d" suffix. */
2570
2571 /* Convert host exception flags to vfp form. */
2572 static inline int vfp_exceptbits_from_host(int host_bits)
2573 {
2574 int target_bits = 0;
2575
2576 if (host_bits & float_flag_invalid)
2577 target_bits |= 1;
2578 if (host_bits & float_flag_divbyzero)
2579 target_bits |= 2;
2580 if (host_bits & float_flag_overflow)
2581 target_bits |= 4;
2582 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
2583 target_bits |= 8;
2584 if (host_bits & float_flag_inexact)
2585 target_bits |= 0x10;
2586 if (host_bits & float_flag_input_denormal)
2587 target_bits |= 0x80;
2588 return target_bits;
2589 }
2590
2591 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2592 {
2593 int i;
2594 uint32_t fpscr;
2595
2596 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2597 | (env->vfp.vec_len << 16)
2598 | (env->vfp.vec_stride << 20);
2599 i = get_float_exception_flags(&env->vfp.fp_status);
2600 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
2601 fpscr |= vfp_exceptbits_from_host(i);
2602 return fpscr;
2603 }
2604
2605 uint32_t vfp_get_fpscr(CPUARMState *env)
2606 {
2607 return HELPER(vfp_get_fpscr)(env);
2608 }
2609
2610 /* Convert vfp exception flags to target form. */
2611 static inline int vfp_exceptbits_to_host(int target_bits)
2612 {
2613 int host_bits = 0;
2614
2615 if (target_bits & 1)
2616 host_bits |= float_flag_invalid;
2617 if (target_bits & 2)
2618 host_bits |= float_flag_divbyzero;
2619 if (target_bits & 4)
2620 host_bits |= float_flag_overflow;
2621 if (target_bits & 8)
2622 host_bits |= float_flag_underflow;
2623 if (target_bits & 0x10)
2624 host_bits |= float_flag_inexact;
2625 if (target_bits & 0x80)
2626 host_bits |= float_flag_input_denormal;
2627 return host_bits;
2628 }
2629
2630 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
2631 {
2632 int i;
2633 uint32_t changed;
2634
2635 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2636 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2637 env->vfp.vec_len = (val >> 16) & 7;
2638 env->vfp.vec_stride = (val >> 20) & 3;
2639
2640 changed ^= val;
2641 if (changed & (3 << 22)) {
2642 i = (val >> 22) & 3;
2643 switch (i) {
2644 case 0:
2645 i = float_round_nearest_even;
2646 break;
2647 case 1:
2648 i = float_round_up;
2649 break;
2650 case 2:
2651 i = float_round_down;
2652 break;
2653 case 3:
2654 i = float_round_to_zero;
2655 break;
2656 }
2657 set_float_rounding_mode(i, &env->vfp.fp_status);
2658 }
2659 if (changed & (1 << 24)) {
2660 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2661 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2662 }
2663 if (changed & (1 << 25))
2664 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2665
2666 i = vfp_exceptbits_to_host(val);
2667 set_float_exception_flags(i, &env->vfp.fp_status);
2668 set_float_exception_flags(0, &env->vfp.standard_fp_status);
2669 }
2670
2671 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
2672 {
2673 HELPER(vfp_set_fpscr)(env, val);
2674 }
2675
2676 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2677
2678 #define VFP_BINOP(name) \
2679 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2680 { \
2681 float_status *fpst = fpstp; \
2682 return float32_ ## name(a, b, fpst); \
2683 } \
2684 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2685 { \
2686 float_status *fpst = fpstp; \
2687 return float64_ ## name(a, b, fpst); \
2688 }
2689 VFP_BINOP(add)
2690 VFP_BINOP(sub)
2691 VFP_BINOP(mul)
2692 VFP_BINOP(div)
2693 #undef VFP_BINOP
2694
2695 float32 VFP_HELPER(neg, s)(float32 a)
2696 {
2697 return float32_chs(a);
2698 }
2699
2700 float64 VFP_HELPER(neg, d)(float64 a)
2701 {
2702 return float64_chs(a);
2703 }
2704
2705 float32 VFP_HELPER(abs, s)(float32 a)
2706 {
2707 return float32_abs(a);
2708 }
2709
2710 float64 VFP_HELPER(abs, d)(float64 a)
2711 {
2712 return float64_abs(a);
2713 }
2714
2715 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
2716 {
2717 return float32_sqrt(a, &env->vfp.fp_status);
2718 }
2719
2720 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
2721 {
2722 return float64_sqrt(a, &env->vfp.fp_status);
2723 }
2724
2725 /* XXX: check quiet/signaling case */
2726 #define DO_VFP_cmp(p, type) \
2727 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
2728 { \
2729 uint32_t flags; \
2730 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2731 case 0: flags = 0x6; break; \
2732 case -1: flags = 0x8; break; \
2733 case 1: flags = 0x2; break; \
2734 default: case 2: flags = 0x3; break; \
2735 } \
2736 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2737 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2738 } \
2739 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
2740 { \
2741 uint32_t flags; \
2742 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2743 case 0: flags = 0x6; break; \
2744 case -1: flags = 0x8; break; \
2745 case 1: flags = 0x2; break; \
2746 default: case 2: flags = 0x3; break; \
2747 } \
2748 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2749 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2750 }
2751 DO_VFP_cmp(s, float32)
2752 DO_VFP_cmp(d, float64)
2753 #undef DO_VFP_cmp
2754
2755 /* Integer to float and float to integer conversions */
2756
2757 #define CONV_ITOF(name, fsz, sign) \
2758 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2759 { \
2760 float_status *fpst = fpstp; \
2761 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
2762 }
2763
2764 #define CONV_FTOI(name, fsz, sign, round) \
2765 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2766 { \
2767 float_status *fpst = fpstp; \
2768 if (float##fsz##_is_any_nan(x)) { \
2769 float_raise(float_flag_invalid, fpst); \
2770 return 0; \
2771 } \
2772 return float##fsz##_to_##sign##int32##round(x, fpst); \
2773 }
2774
2775 #define FLOAT_CONVS(name, p, fsz, sign) \
2776 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2777 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2778 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2779
2780 FLOAT_CONVS(si, s, 32, )
2781 FLOAT_CONVS(si, d, 64, )
2782 FLOAT_CONVS(ui, s, 32, u)
2783 FLOAT_CONVS(ui, d, 64, u)
2784
2785 #undef CONV_ITOF
2786 #undef CONV_FTOI
2787 #undef FLOAT_CONVS
2788
2789 /* floating point conversion */
2790 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
2791 {
2792 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2793 /* ARM requires that S<->D conversion of any kind of NaN generates
2794 * a quiet NaN by forcing the most significant frac bit to 1.
2795 */
2796 return float64_maybe_silence_nan(r);
2797 }
2798
2799 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
2800 {
2801 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2802 /* ARM requires that S<->D conversion of any kind of NaN generates
2803 * a quiet NaN by forcing the most significant frac bit to 1.
2804 */
2805 return float32_maybe_silence_nan(r);
2806 }
2807
2808 /* VFP3 fixed point conversion. */
2809 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2810 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2811 void *fpstp) \
2812 { \
2813 float_status *fpst = fpstp; \
2814 float##fsz tmp; \
2815 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2816 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2817 } \
2818 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2819 void *fpstp) \
2820 { \
2821 float_status *fpst = fpstp; \
2822 float##fsz tmp; \
2823 if (float##fsz##_is_any_nan(x)) { \
2824 float_raise(float_flag_invalid, fpst); \
2825 return 0; \
2826 } \
2827 tmp = float##fsz##_scalbn(x, shift, fpst); \
2828 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2829 }
2830
2831 VFP_CONV_FIX(sh, d, 64, int16, )
2832 VFP_CONV_FIX(sl, d, 64, int32, )
2833 VFP_CONV_FIX(uh, d, 64, uint16, u)
2834 VFP_CONV_FIX(ul, d, 64, uint32, u)
2835 VFP_CONV_FIX(sh, s, 32, int16, )
2836 VFP_CONV_FIX(sl, s, 32, int32, )
2837 VFP_CONV_FIX(uh, s, 32, uint16, u)
2838 VFP_CONV_FIX(ul, s, 32, uint32, u)
2839 #undef VFP_CONV_FIX
2840
2841 /* Half precision conversions. */
2842 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
2843 {
2844 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2845 float32 r = float16_to_float32(make_float16(a), ieee, s);
2846 if (ieee) {
2847 return float32_maybe_silence_nan(r);
2848 }
2849 return r;
2850 }
2851
2852 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
2853 {
2854 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2855 float16 r = float32_to_float16(a, ieee, s);
2856 if (ieee) {
2857 r = float16_maybe_silence_nan(r);
2858 }
2859 return float16_val(r);
2860 }
2861
2862 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2863 {
2864 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2865 }
2866
2867 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2868 {
2869 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2870 }
2871
2872 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2873 {
2874 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2875 }
2876
2877 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2878 {
2879 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2880 }
2881
2882 #define float32_two make_float32(0x40000000)
2883 #define float32_three make_float32(0x40400000)
2884 #define float32_one_point_five make_float32(0x3fc00000)
2885
2886 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
2887 {
2888 float_status *s = &env->vfp.standard_fp_status;
2889 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2890 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2891 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2892 float_raise(float_flag_input_denormal, s);
2893 }
2894 return float32_two;
2895 }
2896 return float32_sub(float32_two, float32_mul(a, b, s), s);
2897 }
2898
2899 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
2900 {
2901 float_status *s = &env->vfp.standard_fp_status;
2902 float32 product;
2903 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2904 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2905 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2906 float_raise(float_flag_input_denormal, s);
2907 }
2908 return float32_one_point_five;
2909 }
2910 product = float32_mul(a, b, s);
2911 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
2912 }
2913
2914 /* NEON helpers. */
2915
2916 /* Constants 256 and 512 are used in some helpers; we avoid relying on
2917 * int->float conversions at run-time. */
2918 #define float64_256 make_float64(0x4070000000000000LL)
2919 #define float64_512 make_float64(0x4080000000000000LL)
2920
2921 /* The algorithm that must be used to calculate the estimate
2922 * is specified by the ARM ARM.
2923 */
2924 static float64 recip_estimate(float64 a, CPUARMState *env)
2925 {
2926 /* These calculations mustn't set any fp exception flags,
2927 * so we use a local copy of the fp_status.
2928 */
2929 float_status dummy_status = env->vfp.standard_fp_status;
2930 float_status *s = &dummy_status;
2931 /* q = (int)(a * 512.0) */
2932 float64 q = float64_mul(float64_512, a, s);
2933 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2934
2935 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2936 q = int64_to_float64(q_int, s);
2937 q = float64_add(q, float64_half, s);
2938 q = float64_div(q, float64_512, s);
2939 q = float64_div(float64_one, q, s);
2940
2941 /* s = (int)(256.0 * r + 0.5) */
2942 q = float64_mul(q, float64_256, s);
2943 q = float64_add(q, float64_half, s);
2944 q_int = float64_to_int64_round_to_zero(q, s);
2945
2946 /* return (double)s / 256.0 */
2947 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2948 }
2949
2950 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
2951 {
2952 float_status *s = &env->vfp.standard_fp_status;
2953 float64 f64;
2954 uint32_t val32 = float32_val(a);
2955
2956 int result_exp;
2957 int a_exp = (val32 & 0x7f800000) >> 23;
2958 int sign = val32 & 0x80000000;
2959
2960 if (float32_is_any_nan(a)) {
2961 if (float32_is_signaling_nan(a)) {
2962 float_raise(float_flag_invalid, s);
2963 }
2964 return float32_default_nan;
2965 } else if (float32_is_infinity(a)) {
2966 return float32_set_sign(float32_zero, float32_is_neg(a));
2967 } else if (float32_is_zero_or_denormal(a)) {
2968 if (!float32_is_zero(a)) {
2969 float_raise(float_flag_input_denormal, s);
2970 }
2971 float_raise(float_flag_divbyzero, s);
2972 return float32_set_sign(float32_infinity, float32_is_neg(a));
2973 } else if (a_exp >= 253) {
2974 float_raise(float_flag_underflow, s);
2975 return float32_set_sign(float32_zero, float32_is_neg(a));
2976 }
2977
2978 f64 = make_float64((0x3feULL << 52)
2979 | ((int64_t)(val32 & 0x7fffff) << 29));
2980
2981 result_exp = 253 - a_exp;
2982
2983 f64 = recip_estimate(f64, env);
2984
2985 val32 = sign
2986 | ((result_exp & 0xff) << 23)
2987 | ((float64_val(f64) >> 29) & 0x7fffff);
2988 return make_float32(val32);
2989 }
2990
2991 /* The algorithm that must be used to calculate the estimate
2992 * is specified by the ARM ARM.
2993 */
2994 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
2995 {
2996 /* These calculations mustn't set any fp exception flags,
2997 * so we use a local copy of the fp_status.
2998 */
2999 float_status dummy_status = env->vfp.standard_fp_status;
3000 float_status *s = &dummy_status;
3001 float64 q;
3002 int64_t q_int;
3003
3004 if (float64_lt(a, float64_half, s)) {
3005 /* range 0.25 <= a < 0.5 */
3006
3007 /* a in units of 1/512 rounded down */
3008 /* q0 = (int)(a * 512.0); */
3009 q = float64_mul(float64_512, a, s);
3010 q_int = float64_to_int64_round_to_zero(q, s);
3011
3012 /* reciprocal root r */
3013 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
3014 q = int64_to_float64(q_int, s);
3015 q = float64_add(q, float64_half, s);
3016 q = float64_div(q, float64_512, s);
3017 q = float64_sqrt(q, s);
3018 q = float64_div(float64_one, q, s);
3019 } else {
3020 /* range 0.5 <= a < 1.0 */
3021
3022 /* a in units of 1/256 rounded down */
3023 /* q1 = (int)(a * 256.0); */
3024 q = float64_mul(float64_256, a, s);
3025 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3026
3027 /* reciprocal root r */
3028 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
3029 q = int64_to_float64(q_int, s);
3030 q = float64_add(q, float64_half, s);
3031 q = float64_div(q, float64_256, s);
3032 q = float64_sqrt(q, s);
3033 q = float64_div(float64_one, q, s);
3034 }
3035 /* r in units of 1/256 rounded to nearest */
3036 /* s = (int)(256.0 * r + 0.5); */
3037
3038 q = float64_mul(q, float64_256,s );
3039 q = float64_add(q, float64_half, s);
3040 q_int = float64_to_int64_round_to_zero(q, s);
3041
3042 /* return (double)s / 256.0;*/
3043 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3044 }
3045
3046 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
3047 {
3048 float_status *s = &env->vfp.standard_fp_status;
3049 int result_exp;
3050 float64 f64;
3051 uint32_t val;
3052 uint64_t val64;
3053
3054 val = float32_val(a);
3055
3056 if (float32_is_any_nan(a)) {
3057 if (float32_is_signaling_nan(a)) {
3058 float_raise(float_flag_invalid, s);
3059 }
3060 return float32_default_nan;
3061 } else if (float32_is_zero_or_denormal(a)) {
3062 if (!float32_is_zero(a)) {
3063 float_raise(float_flag_input_denormal, s);
3064 }
3065 float_raise(float_flag_divbyzero, s);
3066 return float32_set_sign(float32_infinity, float32_is_neg(a));
3067 } else if (float32_is_neg(a)) {
3068 float_raise(float_flag_invalid, s);
3069 return float32_default_nan;
3070 } else if (float32_is_infinity(a)) {
3071 return float32_zero;
3072 }
3073
3074 /* Normalize to a double-precision value between 0.25 and 1.0,
3075 * preserving the parity of the exponent. */
3076 if ((val & 0x800000) == 0) {
3077 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3078 | (0x3feULL << 52)
3079 | ((uint64_t)(val & 0x7fffff) << 29));
3080 } else {
3081 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3082 | (0x3fdULL << 52)
3083 | ((uint64_t)(val & 0x7fffff) << 29));
3084 }
3085
3086 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3087
3088 f64 = recip_sqrt_estimate(f64, env);
3089
3090 val64 = float64_val(f64);
3091
3092 val = ((result_exp & 0xff) << 23)
3093 | ((val64 >> 29) & 0x7fffff);
3094 return make_float32(val);
3095 }
3096
3097 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
3098 {
3099 float64 f64;
3100
3101 if ((a & 0x80000000) == 0) {
3102 return 0xffffffff;
3103 }
3104
3105 f64 = make_float64((0x3feULL << 52)
3106 | ((int64_t)(a & 0x7fffffff) << 21));
3107
3108 f64 = recip_estimate (f64, env);
3109
3110 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3111 }
3112
3113 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
3114 {
3115 float64 f64;
3116
3117 if ((a & 0xc0000000) == 0) {
3118 return 0xffffffff;
3119 }
3120
3121 if (a & 0x80000000) {
3122 f64 = make_float64((0x3feULL << 52)
3123 | ((uint64_t)(a & 0x7fffffff) << 21));
3124 } else { /* bits 31-30 == '01' */
3125 f64 = make_float64((0x3fdULL << 52)
3126 | ((uint64_t)(a & 0x3fffffff) << 22));
3127 }
3128
3129 f64 = recip_sqrt_estimate(f64, env);
3130
3131 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3132 }
3133
3134 /* VFPv4 fused multiply-accumulate */
3135 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3136 {
3137 float_status *fpst = fpstp;
3138 return float32_muladd(a, b, c, 0, fpst);
3139 }
3140
3141 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3142 {
3143 float_status *fpst = fpstp;
3144 return float64_muladd(a, b, c, 0, fpst);
3145 }