blob: 2c6e7ed7d583be07e9abf05e6aceccf4babc450f [file] [log] [blame]
bellardb5ff1b32005-11-26 10:38:39 +00001#include "cpu.h"
pbrook9ee6e8b2007-11-11 00:04:49 +00002#include "gdbstub.h"
Lluís7b592202011-04-13 18:38:24 +02003#include "helper.h"
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +02004#include "host-utils.h"
Peter Maydell0b03bdf2012-01-25 12:42:29 +00005#include "sysemu.h"
6
Peter Maydell4a501602012-06-20 11:57:16 +00007#ifndef CONFIG_USER_ONLY
8static 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
Andreas Färber0ecb72a2012-03-14 01:38:21 +010014static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +000015{
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
Andreas Färber0ecb72a2012-03-14 01:38:21 +010041static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +000042{
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;
Juha Riihimäki71b3c3d2009-10-26 11:46:42 +020061 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
pbrook56aebc82008-10-11 17:55:29 +000062 }
63 return 0;
64}
65
Peter Maydellc983fe62012-06-20 11:57:13 +000066static 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
Peter Maydell08de2072012-06-20 11:57:14 +000073static 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}
84static 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
Peter Maydelld9298232012-06-20 11:57:16 +000098static 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
106static 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
114static 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
122static 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
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000130static 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 },
Peter Maydellc983fe62012-06-20 11:57:13 +0000137 /* 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 },
Peter Maydell08de2072012-06-20 11:57:14 +0000142 { .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 },
Peter Maydell4fdd17d2012-06-20 11:57:15 +0000148 /* ??? 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 },
Peter Maydelld9298232012-06-20 11:57:16 +0000153 /* 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, },
Peter Maydellc4804212012-06-20 11:57:17 +0000164 /* 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 },
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000168 REGINFO_SENTINEL
169};
170
Peter Maydell7d57f402012-06-20 11:57:11 +0000171static 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
180static 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 },
Peter Maydell34f90522012-06-20 11:57:18 +0000186 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
187 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
188 * OMAPCP will override this space.
189 */
190 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
191 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
192 .resetvalue = 0 },
193 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
194 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
195 .resetvalue = 0 },
Peter Maydell7d57f402012-06-20 11:57:11 +0000196 REGINFO_SENTINEL
197};
198
199static const ARMCPRegInfo v6_cp_reginfo[] = {
200 /* prefetch by MVA in v6, NOP in v7 */
201 { .name = "MVA_prefetch",
202 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
203 .access = PL1_W, .type = ARM_CP_NOP },
204 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
205 .access = PL0_W, .type = ARM_CP_NOP },
206 { .name = "ISB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
207 .access = PL0_W, .type = ARM_CP_NOP },
208 { .name = "ISB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
209 .access = PL0_W, .type = ARM_CP_NOP },
Peter Maydell06d76f32012-06-20 11:57:17 +0000210 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
211 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_insn),
212 .resetvalue = 0, },
213 /* Watchpoint Fault Address Register : should actually only be present
214 * for 1136, 1176, 11MPCore.
215 */
216 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
217 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
Peter Maydell7d57f402012-06-20 11:57:11 +0000218 REGINFO_SENTINEL
219};
220
Peter Maydell200ac0e2012-06-20 11:57:12 +0000221static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri,
222 uint64_t *value)
223{
224 /* Generic performance monitor register read function for where
225 * user access may be allowed by PMUSERENR.
226 */
227 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
228 return EXCP_UDEF;
229 }
230 *value = CPREG_FIELD32(env, ri);
231 return 0;
232}
233
234static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
235 uint64_t value)
236{
237 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
238 return EXCP_UDEF;
239 }
240 /* only the DP, X, D and E bits are writable */
241 env->cp15.c9_pmcr &= ~0x39;
242 env->cp15.c9_pmcr |= (value & 0x39);
243 return 0;
244}
245
246static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
247 uint64_t value)
248{
249 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
250 return EXCP_UDEF;
251 }
252 value &= (1 << 31);
253 env->cp15.c9_pmcnten |= value;
254 return 0;
255}
256
257static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
258 uint64_t value)
259{
260 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
261 return EXCP_UDEF;
262 }
263 value &= (1 << 31);
264 env->cp15.c9_pmcnten &= ~value;
265 return 0;
266}
267
268static int pmovsr_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_pmovsr &= ~value;
275 return 0;
276}
277
278static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
279 uint64_t value)
280{
281 if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) {
282 return EXCP_UDEF;
283 }
284 env->cp15.c9_pmxevtyper = value & 0xff;
285 return 0;
286}
287
288static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
289 uint64_t value)
290{
291 env->cp15.c9_pmuserenr = value & 1;
292 return 0;
293}
294
295static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
296 uint64_t value)
297{
298 /* We have no event counters so only the C bit can be changed */
299 value &= (1 << 31);
300 env->cp15.c9_pminten |= value;
301 return 0;
302}
303
304static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
305 uint64_t value)
306{
307 value &= (1 << 31);
308 env->cp15.c9_pminten &= ~value;
309 return 0;
310}
311
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000312static const ARMCPRegInfo v7_cp_reginfo[] = {
313 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
314 * debug components
315 */
316 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
317 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
318 { .name = "DBGDRAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
319 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell7d57f402012-06-20 11:57:11 +0000320 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
321 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
322 .access = PL1_W, .type = ARM_CP_NOP },
Peter Maydell200ac0e2012-06-20 11:57:12 +0000323 /* Performance monitors are implementation defined in v7,
324 * but with an ARM recommended set of registers, which we
325 * follow (although we don't actually implement any counters)
326 *
327 * Performance registers fall into three categories:
328 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
329 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
330 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
331 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
332 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
333 */
334 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
335 .access = PL0_RW, .resetvalue = 0,
336 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
337 .readfn = pmreg_read, .writefn = pmcntenset_write },
338 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
339 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
340 .readfn = pmreg_read, .writefn = pmcntenclr_write },
341 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
342 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
343 .readfn = pmreg_read, .writefn = pmovsr_write },
344 /* Unimplemented so WI. Strictly speaking write accesses in PL0 should
345 * respect PMUSERENR.
346 */
347 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
348 .access = PL0_W, .type = ARM_CP_NOP },
349 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
350 * We choose to RAZ/WI. XXX should respect PMUSERENR.
351 */
352 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
353 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
354 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
355 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
356 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
357 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
358 .access = PL0_RW,
359 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
360 .readfn = pmreg_read, .writefn = pmxevtyper_write },
361 /* Unimplemented, RAZ/WI. XXX PMUSERENR */
362 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
363 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
364 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
365 .access = PL0_R | PL1_RW,
366 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
367 .resetvalue = 0,
368 .writefn = pmuserenr_write },
369 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
370 .access = PL1_RW,
371 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
372 .resetvalue = 0,
373 .writefn = pmintenset_write },
374 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
375 .access = PL1_RW,
376 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
377 .resetvalue = 0,
378 .writefn = pmintenclr_write },
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000379 REGINFO_SENTINEL
380};
381
Peter Maydellc326b972012-06-20 11:57:10 +0000382static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
383{
384 value &= 1;
385 env->teecr = value;
386 return 0;
387}
388
389static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
390 uint64_t *value)
391{
392 /* This is a helper function because the user access rights
393 * depend on the value of the TEECR.
394 */
395 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
396 return EXCP_UDEF;
397 }
398 *value = env->teehbr;
399 return 0;
400}
401
402static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
403 uint64_t value)
404{
405 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
406 return EXCP_UDEF;
407 }
408 env->teehbr = value;
409 return 0;
410}
411
412static const ARMCPRegInfo t2ee_cp_reginfo[] = {
413 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
414 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
415 .resetvalue = 0,
416 .writefn = teecr_write },
417 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
418 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
419 .resetvalue = 0,
420 .readfn = teehbr_read, .writefn = teehbr_write },
421 REGINFO_SENTINEL
422};
423
Peter Maydell4d31c592012-06-20 11:57:11 +0000424static const ARMCPRegInfo v6k_cp_reginfo[] = {
425 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
426 .access = PL0_RW,
427 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1),
428 .resetvalue = 0 },
429 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
430 .access = PL0_R|PL1_W,
431 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2),
432 .resetvalue = 0 },
433 { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4,
434 .access = PL1_RW,
435 .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3),
436 .resetvalue = 0 },
437 REGINFO_SENTINEL
438};
439
Peter Maydell6cc7a3a2012-06-20 11:57:12 +0000440static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
441 /* Dummy implementation: RAZ/WI the whole crn=14 space */
442 { .name = "GENERIC_TIMER", .cp = 15, .crn = 14,
443 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
444 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
445 REGINFO_SENTINEL
446};
447
Peter Maydell4a501602012-06-20 11:57:16 +0000448static int par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
449{
450 if (arm_feature(env, ARM_FEATURE_V7)) {
451 env->cp15.c7_par = value & 0xfffff6ff;
452 } else {
453 env->cp15.c7_par = value & 0xfffff1ff;
454 }
455 return 0;
456}
457
458#ifndef CONFIG_USER_ONLY
459/* get_phys_addr() isn't present for user-mode-only targets */
460static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
461{
462 uint32_t phys_addr;
463 target_ulong page_size;
464 int prot;
465 int ret, is_user = ri->opc2 & 2;
466 int access_type = ri->opc2 & 1;
467
468 if (ri->opc2 & 4) {
469 /* Other states are only available with TrustZone */
470 return EXCP_UDEF;
471 }
472 ret = get_phys_addr(env, value, access_type, is_user,
473 &phys_addr, &prot, &page_size);
474 if (ret == 0) {
475 /* We do not set any attribute bits in the PAR */
476 if (page_size == (1 << 24)
477 && arm_feature(env, ARM_FEATURE_V7)) {
478 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
479 } else {
480 env->cp15.c7_par = phys_addr & 0xfffff000;
481 }
482 } else {
483 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
484 ((ret & (12 << 1)) >> 6) |
485 ((ret & 0xf) << 1) | 1;
486 }
487 return 0;
488}
489#endif
490
491static const ARMCPRegInfo vapa_cp_reginfo[] = {
492 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
493 .access = PL1_RW, .resetvalue = 0,
494 .fieldoffset = offsetof(CPUARMState, cp15.c7_par),
495 .writefn = par_write },
496#ifndef CONFIG_USER_ONLY
497 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
498 .access = PL1_W, .writefn = ats_write },
499#endif
500 REGINFO_SENTINEL
501};
502
Peter Maydell18032be2012-06-20 11:57:13 +0000503/* Return basic MPU access permission bits. */
504static uint32_t simple_mpu_ap_bits(uint32_t val)
505{
506 uint32_t ret;
507 uint32_t mask;
508 int i;
509 ret = 0;
510 mask = 3;
511 for (i = 0; i < 16; i += 2) {
512 ret |= (val >> i) & mask;
513 mask <<= 2;
514 }
515 return ret;
516}
517
518/* Pad basic MPU access permission bits to extended format. */
519static uint32_t extended_mpu_ap_bits(uint32_t val)
520{
521 uint32_t ret;
522 uint32_t mask;
523 int i;
524 ret = 0;
525 mask = 3;
526 for (i = 0; i < 16; i += 2) {
527 ret |= (val & mask) << i;
528 mask <<= 2;
529 }
530 return ret;
531}
532
533static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
534 uint64_t value)
535{
536 env->cp15.c5_data = extended_mpu_ap_bits(value);
537 return 0;
538}
539
540static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
541 uint64_t *value)
542{
543 *value = simple_mpu_ap_bits(env->cp15.c5_data);
544 return 0;
545}
546
547static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
548 uint64_t value)
549{
550 env->cp15.c5_insn = extended_mpu_ap_bits(value);
551 return 0;
552}
553
554static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri,
555 uint64_t *value)
556{
557 *value = simple_mpu_ap_bits(env->cp15.c5_insn);
558 return 0;
559}
560
Peter Maydell06d76f32012-06-20 11:57:17 +0000561static int arm946_prbs_read(CPUARMState *env, const ARMCPRegInfo *ri,
562 uint64_t *value)
563{
564 if (ri->crm > 8) {
565 return EXCP_UDEF;
566 }
567 *value = env->cp15.c6_region[ri->crm];
568 return 0;
569}
570
571static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
572 uint64_t value)
573{
574 if (ri->crm > 8) {
575 return EXCP_UDEF;
576 }
577 env->cp15.c6_region[ri->crm] = value;
578 return 0;
579}
580
Peter Maydell18032be2012-06-20 11:57:13 +0000581static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
582 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
583 .access = PL1_RW,
584 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0,
585 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
586 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
587 .access = PL1_RW,
588 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0,
589 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
590 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
591 .access = PL1_RW,
592 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
593 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
594 .access = PL1_RW,
595 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
Peter Maydellecce5c32012-06-20 11:57:14 +0000596 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
597 .access = PL1_RW,
598 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
599 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
600 .access = PL1_RW,
601 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
Peter Maydell06d76f32012-06-20 11:57:17 +0000602 /* Protection region base and size registers */
603 { .name = "946_PRBS", .cp = 15, .crn = 6, .crm = CP_ANY, .opc1 = 0,
604 .opc2 = CP_ANY, .access = PL1_RW,
605 .readfn = arm946_prbs_read, .writefn = arm946_prbs_write, },
Peter Maydell18032be2012-06-20 11:57:13 +0000606 REGINFO_SENTINEL
607};
608
Peter Maydellecce5c32012-06-20 11:57:14 +0000609static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
610 uint64_t value)
611{
612 value &= 7;
613 env->cp15.c2_control = value;
614 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> value);
615 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> value);
616 return 0;
617}
618
619static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
620{
621 env->cp15.c2_base_mask = 0xffffc000u;
622 env->cp15.c2_control = 0;
623 env->cp15.c2_mask = 0;
624}
625
Peter Maydell18032be2012-06-20 11:57:13 +0000626static const ARMCPRegInfo vmsa_cp_reginfo[] = {
627 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
628 .access = PL1_RW,
629 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
630 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
631 .access = PL1_RW,
632 .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, },
Peter Maydellecce5c32012-06-20 11:57:14 +0000633 { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
634 .access = PL1_RW,
635 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
636 { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
637 .access = PL1_RW,
638 .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, },
639 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
640 .access = PL1_RW, .writefn = vmsa_ttbcr_write,
641 .resetfn = vmsa_ttbcr_reset,
642 .fieldoffset = offsetof(CPUARMState, cp15.c2_control) },
Peter Maydell06d76f32012-06-20 11:57:17 +0000643 { .name = "DFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
644 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_data),
645 .resetvalue = 0, },
Peter Maydell18032be2012-06-20 11:57:13 +0000646 REGINFO_SENTINEL
647};
648
Peter Maydell1047b9d2012-06-20 11:57:15 +0000649static int omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
650 uint64_t value)
651{
652 env->cp15.c15_ticonfig = value & 0xe7;
653 /* The OS_TYPE bit in this register changes the reported CPUID! */
654 env->cp15.c0_cpuid = (value & (1 << 5)) ?
655 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
656 return 0;
657}
658
659static int omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
660 uint64_t value)
661{
662 env->cp15.c15_threadid = value & 0xffff;
663 return 0;
664}
665
666static int omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
667 uint64_t value)
668{
669 /* Wait-for-interrupt (deprecated) */
670 cpu_interrupt(env, CPU_INTERRUPT_HALT);
671 return 0;
672}
673
Peter Maydellc4804212012-06-20 11:57:17 +0000674static int omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
675 uint64_t value)
676{
677 /* On OMAP there are registers indicating the max/min index of dcache lines
678 * containing a dirty line; cache flush operations have to reset these.
679 */
680 env->cp15.c15_i_max = 0x000;
681 env->cp15.c15_i_min = 0xff0;
682 return 0;
683}
684
Peter Maydell18032be2012-06-20 11:57:13 +0000685static const ARMCPRegInfo omap_cp_reginfo[] = {
686 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
687 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
688 .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, },
Peter Maydell1047b9d2012-06-20 11:57:15 +0000689 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
690 .access = PL1_RW, .type = ARM_CP_NOP },
691 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
692 .access = PL1_RW,
693 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
694 .writefn = omap_ticonfig_write },
695 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
696 .access = PL1_RW,
697 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
698 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
699 .access = PL1_RW, .resetvalue = 0xff0,
700 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
701 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
702 .access = PL1_RW,
703 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
704 .writefn = omap_threadid_write },
705 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
706 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
707 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
708 /* TODO: Peripheral port remap register:
709 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
710 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
711 * when MMU is off.
712 */
Peter Maydellc4804212012-06-20 11:57:17 +0000713 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
714 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, .type = ARM_CP_OVERRIDE,
715 .writefn = omap_cachemaint_write },
Peter Maydell34f90522012-06-20 11:57:18 +0000716 { .name = "C9", .cp = 15, .crn = 9,
717 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
718 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
Peter Maydell1047b9d2012-06-20 11:57:15 +0000719 REGINFO_SENTINEL
720};
721
722static int xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
723 uint64_t value)
724{
725 value &= 0x3fff;
726 if (env->cp15.c15_cpar != value) {
727 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
728 tb_flush(env);
729 env->cp15.c15_cpar = value;
730 }
731 return 0;
732}
733
734static const ARMCPRegInfo xscale_cp_reginfo[] = {
735 { .name = "XSCALE_CPAR",
736 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
737 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
738 .writefn = xscale_cpar_write, },
739 REGINFO_SENTINEL
740};
741
742static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
743 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
744 * implementation of this implementation-defined space.
745 * Ideally this should eventually disappear in favour of actually
746 * implementing the correct behaviour for all cores.
747 */
748 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
749 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
750 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell18032be2012-06-20 11:57:13 +0000751 REGINFO_SENTINEL
752};
753
Peter Maydellc4804212012-06-20 11:57:17 +0000754static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
755 /* Cache status: RAZ because we have no cache so it's always clean */
756 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
757 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
758 REGINFO_SENTINEL
759};
760
761static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
762 /* We never have a a block transfer operation in progress */
763 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
764 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
765 REGINFO_SENTINEL
766};
767
768static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
769 /* The cache test-and-clean instructions always return (1 << 30)
770 * to indicate that there are no dirty cache lines.
771 */
772 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
773 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = (1 << 30) },
774 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
775 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = (1 << 30) },
776 REGINFO_SENTINEL
777};
778
Peter Maydell34f90522012-06-20 11:57:18 +0000779static const ARMCPRegInfo strongarm_cp_reginfo[] = {
780 /* Ignore ReadBuffer accesses */
781 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
782 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
783 .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE,
784 .resetvalue = 0 },
785 REGINFO_SENTINEL
786};
787
Peter Maydell2ceb98c2012-06-20 11:57:09 +0000788void register_cp_regs_for_features(ARMCPU *cpu)
789{
790 /* Register all the coprocessor registers based on feature bits */
791 CPUARMState *env = &cpu->env;
792 if (arm_feature(env, ARM_FEATURE_M)) {
793 /* M profile has no coprocessor registers */
794 return;
795 }
796
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000797 define_arm_cp_regs(cpu, cp_reginfo);
Peter Maydell7d57f402012-06-20 11:57:11 +0000798 if (arm_feature(env, ARM_FEATURE_V6)) {
799 define_arm_cp_regs(cpu, v6_cp_reginfo);
800 } else {
801 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
802 }
Peter Maydell4d31c592012-06-20 11:57:11 +0000803 if (arm_feature(env, ARM_FEATURE_V6K)) {
804 define_arm_cp_regs(cpu, v6k_cp_reginfo);
805 }
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000806 if (arm_feature(env, ARM_FEATURE_V7)) {
Peter Maydell200ac0e2012-06-20 11:57:12 +0000807 /* v7 performance monitor control register: same implementor
808 * field as main ID register, and we implement no event counters.
809 */
810 ARMCPRegInfo pmcr = {
811 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
812 .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000,
813 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
814 .readfn = pmreg_read, .writefn = pmcr_write
815 };
816 define_one_arm_cp_reg(cpu, &pmcr);
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000817 define_arm_cp_regs(cpu, v7_cp_reginfo);
Peter Maydell7d57f402012-06-20 11:57:11 +0000818 } else {
819 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000820 }
Peter Maydell18032be2012-06-20 11:57:13 +0000821 if (arm_feature(env, ARM_FEATURE_MPU)) {
822 /* These are the MPU registers prior to PMSAv6. Any new
823 * PMSA core later than the ARM946 will require that we
824 * implement the PMSAv6 or PMSAv7 registers, which are
825 * completely different.
826 */
827 assert(!arm_feature(env, ARM_FEATURE_V6));
828 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
829 } else {
830 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
831 }
Peter Maydellc326b972012-06-20 11:57:10 +0000832 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
833 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
834 }
Peter Maydell6cc7a3a2012-06-20 11:57:12 +0000835 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
836 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
837 }
Peter Maydell4a501602012-06-20 11:57:16 +0000838 if (arm_feature(env, ARM_FEATURE_VAPA)) {
839 define_arm_cp_regs(cpu, vapa_cp_reginfo);
840 }
Peter Maydellc4804212012-06-20 11:57:17 +0000841 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
842 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
843 }
844 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
845 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
846 }
847 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
848 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
849 }
Peter Maydell18032be2012-06-20 11:57:13 +0000850 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
851 define_arm_cp_regs(cpu, omap_cp_reginfo);
852 }
Peter Maydell34f90522012-06-20 11:57:18 +0000853 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
854 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
855 }
Peter Maydell1047b9d2012-06-20 11:57:15 +0000856 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
857 define_arm_cp_regs(cpu, xscale_cp_reginfo);
858 }
859 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
860 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
861 }
Peter Maydell2ceb98c2012-06-20 11:57:09 +0000862}
863
Andreas Färber778c3a02012-04-20 07:39:14 +0000864ARMCPU *cpu_arm_init(const char *cpu_model)
pbrook40f137e2006-02-20 00:33:36 +0000865{
Andreas Färberdec9c2d2012-03-29 04:50:31 +0000866 ARMCPU *cpu;
pbrook40f137e2006-02-20 00:33:36 +0000867 CPUARMState *env;
pbrookb26eefb2008-03-31 03:44:26 +0000868 static int inited = 0;
pbrook40f137e2006-02-20 00:33:36 +0000869
Peter Maydell777dc782012-04-20 17:58:31 +0000870 if (!object_class_by_name(cpu_model)) {
bellardaaed9092007-11-10 15:15:54 +0000871 return NULL;
Peter Maydell777dc782012-04-20 17:58:31 +0000872 }
873 cpu = ARM_CPU(object_new(cpu_model));
Andreas Färberdec9c2d2012-03-29 04:50:31 +0000874 env = &cpu->env;
Peter Maydell777dc782012-04-20 17:58:31 +0000875 env->cpu_model_str = cpu_model;
Peter Maydell581be092012-04-20 17:58:31 +0000876 arm_cpu_realize(cpu);
Peter Maydell777dc782012-04-20 17:58:31 +0000877
Peter Maydellf4fc2472011-11-25 19:25:50 +0100878 if (tcg_enabled() && !inited) {
pbrookb26eefb2008-03-31 03:44:26 +0000879 inited = 1;
880 arm_translate_init();
881 }
882
Andreas Färberdf90dad2012-05-04 19:14:38 +0200883 cpu_reset(CPU(cpu));
pbrook56aebc82008-10-11 17:55:29 +0000884 if (arm_feature(env, ARM_FEATURE_NEON)) {
885 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
886 51, "arm-neon.xml", 0);
887 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
888 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
889 35, "arm-vfp3.xml", 0);
890 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
891 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
892 19, "arm-vfp.xml", 0);
893 }
aliguori0bf46a42009-04-24 18:03:41 +0000894 qemu_init_vcpu(env);
Andreas Färber778c3a02012-04-20 07:39:14 +0000895 return cpu;
pbrook40f137e2006-02-20 00:33:36 +0000896}
897
Peter Maydell777dc782012-04-20 17:58:31 +0000898typedef struct ARMCPUListState {
899 fprintf_function cpu_fprintf;
900 FILE *file;
901} ARMCPUListState;
pbrook3371d272007-03-08 03:04:12 +0000902
Peter Maydell777dc782012-04-20 17:58:31 +0000903/* Sort alphabetically by type name, except for "any". */
904static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
pbrook5adb4832007-03-08 03:15:18 +0000905{
Peter Maydell777dc782012-04-20 17:58:31 +0000906 ObjectClass *class_a = (ObjectClass *)a;
907 ObjectClass *class_b = (ObjectClass *)b;
908 const char *name_a, *name_b;
pbrook5adb4832007-03-08 03:15:18 +0000909
Peter Maydell777dc782012-04-20 17:58:31 +0000910 name_a = object_class_get_name(class_a);
911 name_b = object_class_get_name(class_b);
912 if (strcmp(name_a, "any") == 0) {
913 return 1;
914 } else if (strcmp(name_b, "any") == 0) {
915 return -1;
916 } else {
917 return strcmp(name_a, name_b);
pbrook5adb4832007-03-08 03:15:18 +0000918 }
919}
920
Peter Maydell777dc782012-04-20 17:58:31 +0000921static void arm_cpu_list_entry(gpointer data, gpointer user_data)
pbrook40f137e2006-02-20 00:33:36 +0000922{
Peter Maydell777dc782012-04-20 17:58:31 +0000923 ObjectClass *oc = data;
924 ARMCPUListState *s = user_data;
pbrook3371d272007-03-08 03:04:12 +0000925
Peter Maydell777dc782012-04-20 17:58:31 +0000926 (*s->cpu_fprintf)(s->file, " %s\n",
927 object_class_get_name(oc));
928}
929
930void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
931{
932 ARMCPUListState s = {
933 .file = f,
934 .cpu_fprintf = cpu_fprintf,
935 };
936 GSList *list;
937
938 list = object_class_get_list(TYPE_ARM_CPU, false);
939 list = g_slist_sort(list, arm_cpu_list_compare);
940 (*cpu_fprintf)(f, "Available CPUs:\n");
941 g_slist_foreach(list, arm_cpu_list_entry, &s);
942 g_slist_free(list);
pbrook40f137e2006-02-20 00:33:36 +0000943}
944
Peter Maydell4b6a83f2012-06-20 11:57:06 +0000945void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
946 const ARMCPRegInfo *r, void *opaque)
947{
948 /* Define implementations of coprocessor registers.
949 * We store these in a hashtable because typically
950 * there are less than 150 registers in a space which
951 * is 16*16*16*8*8 = 262144 in size.
952 * Wildcarding is supported for the crm, opc1 and opc2 fields.
953 * If a register is defined twice then the second definition is
954 * used, so this can be used to define some generic registers and
955 * then override them with implementation specific variations.
956 * At least one of the original and the second definition should
957 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
958 * against accidental use.
959 */
960 int crm, opc1, opc2;
961 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
962 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
963 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
964 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
965 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
966 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
967 /* 64 bit registers have only CRm and Opc1 fields */
968 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
969 /* Check that the register definition has enough info to handle
970 * reads and writes if they are permitted.
971 */
972 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
973 if (r->access & PL3_R) {
974 assert(r->fieldoffset || r->readfn);
975 }
976 if (r->access & PL3_W) {
977 assert(r->fieldoffset || r->writefn);
978 }
979 }
980 /* Bad type field probably means missing sentinel at end of reg list */
981 assert(cptype_valid(r->type));
982 for (crm = crmmin; crm <= crmmax; crm++) {
983 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
984 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
985 uint32_t *key = g_new(uint32_t, 1);
986 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
987 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
988 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
989 r2->opaque = opaque;
990 /* Make sure reginfo passed to helpers for wildcarded regs
991 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
992 */
993 r2->crm = crm;
994 r2->opc1 = opc1;
995 r2->opc2 = opc2;
996 /* Overriding of an existing definition must be explicitly
997 * requested.
998 */
999 if (!(r->type & ARM_CP_OVERRIDE)) {
1000 ARMCPRegInfo *oldreg;
1001 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
1002 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
1003 fprintf(stderr, "Register redefined: cp=%d %d bit "
1004 "crn=%d crm=%d opc1=%d opc2=%d, "
1005 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
1006 r2->crn, r2->crm, r2->opc1, r2->opc2,
1007 oldreg->name, r2->name);
1008 assert(0);
1009 }
1010 }
1011 g_hash_table_insert(cpu->cp_regs, key, r2);
1012 }
1013 }
1014 }
1015}
1016
1017void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
1018 const ARMCPRegInfo *regs, void *opaque)
1019{
1020 /* Define a whole list of registers */
1021 const ARMCPRegInfo *r;
1022 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
1023 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
1024 }
1025}
1026
1027const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
1028{
1029 return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
1030}
1031
1032int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
1033 uint64_t value)
1034{
1035 /* Helper coprocessor write function for write-ignore registers */
1036 return 0;
1037}
1038
1039int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
1040{
1041 /* Helper coprocessor write function for read-as-zero registers */
1042 *value = 0;
1043 return 0;
1044}
1045
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001046static int bad_mode_switch(CPUARMState *env, int mode)
Peter Maydell37064a82012-01-05 15:49:06 +00001047{
1048 /* Return true if it is not valid for us to switch to
1049 * this CPU mode (ie all the UNPREDICTABLE cases in
1050 * the ARM ARM CPSRWriteByInstr pseudocode).
1051 */
1052 switch (mode) {
1053 case ARM_CPU_MODE_USR:
1054 case ARM_CPU_MODE_SYS:
1055 case ARM_CPU_MODE_SVC:
1056 case ARM_CPU_MODE_ABT:
1057 case ARM_CPU_MODE_UND:
1058 case ARM_CPU_MODE_IRQ:
1059 case ARM_CPU_MODE_FIQ:
1060 return 0;
1061 default:
1062 return 1;
1063 }
1064}
1065
balrog2f4a40e2007-11-13 01:50:15 +00001066uint32_t cpsr_read(CPUARMState *env)
1067{
1068 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +00001069 ZF = (env->ZF == 0);
1070 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
balrog2f4a40e2007-11-13 01:50:15 +00001071 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
1072 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
1073 | ((env->condexec_bits & 0xfc) << 8)
1074 | (env->GE << 16);
1075}
1076
1077void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
1078{
balrog2f4a40e2007-11-13 01:50:15 +00001079 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +00001080 env->ZF = (~val) & CPSR_Z;
1081 env->NF = val;
balrog2f4a40e2007-11-13 01:50:15 +00001082 env->CF = (val >> 29) & 1;
1083 env->VF = (val << 3) & 0x80000000;
1084 }
1085 if (mask & CPSR_Q)
1086 env->QF = ((val & CPSR_Q) != 0);
1087 if (mask & CPSR_T)
1088 env->thumb = ((val & CPSR_T) != 0);
1089 if (mask & CPSR_IT_0_1) {
1090 env->condexec_bits &= ~3;
1091 env->condexec_bits |= (val >> 25) & 3;
1092 }
1093 if (mask & CPSR_IT_2_7) {
1094 env->condexec_bits &= 3;
1095 env->condexec_bits |= (val >> 8) & 0xfc;
1096 }
1097 if (mask & CPSR_GE) {
1098 env->GE = (val >> 16) & 0xf;
1099 }
1100
1101 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
Peter Maydell37064a82012-01-05 15:49:06 +00001102 if (bad_mode_switch(env, val & CPSR_M)) {
1103 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
1104 * We choose to ignore the attempt and leave the CPSR M field
1105 * untouched.
1106 */
1107 mask &= ~CPSR_M;
1108 } else {
1109 switch_mode(env, val & CPSR_M);
1110 }
balrog2f4a40e2007-11-13 01:50:15 +00001111 }
1112 mask &= ~CACHED_CPSR_BITS;
1113 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
1114}
1115
pbrookb26eefb2008-03-31 03:44:26 +00001116/* Sign/zero extend */
1117uint32_t HELPER(sxtb16)(uint32_t x)
1118{
1119 uint32_t res;
1120 res = (uint16_t)(int8_t)x;
1121 res |= (uint32_t)(int8_t)(x >> 16) << 16;
1122 return res;
1123}
1124
1125uint32_t HELPER(uxtb16)(uint32_t x)
1126{
1127 uint32_t res;
1128 res = (uint16_t)(uint8_t)x;
1129 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
1130 return res;
1131}
1132
pbrookf51bbbf2008-03-31 03:45:13 +00001133uint32_t HELPER(clz)(uint32_t x)
1134{
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +02001135 return clz32(x);
pbrookf51bbbf2008-03-31 03:45:13 +00001136}
1137
pbrook36706692008-03-31 03:46:19 +00001138int32_t HELPER(sdiv)(int32_t num, int32_t den)
1139{
1140 if (den == 0)
1141 return 0;
Aurelien Jarno686eeb92009-10-15 23:08:46 +02001142 if (num == INT_MIN && den == -1)
1143 return INT_MIN;
pbrook36706692008-03-31 03:46:19 +00001144 return num / den;
1145}
1146
1147uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
1148{
1149 if (den == 0)
1150 return 0;
1151 return num / den;
1152}
1153
1154uint32_t HELPER(rbit)(uint32_t x)
1155{
1156 x = ((x & 0xff000000) >> 24)
1157 | ((x & 0x00ff0000) >> 8)
1158 | ((x & 0x0000ff00) << 8)
1159 | ((x & 0x000000ff) << 24);
1160 x = ((x & 0xf0f0f0f0) >> 4)
1161 | ((x & 0x0f0f0f0f) << 4);
1162 x = ((x & 0x88888888) >> 3)
1163 | ((x & 0x44444444) >> 1)
1164 | ((x & 0x22222222) << 1)
1165 | ((x & 0x11111111) << 3);
1166 return x;
1167}
1168
pbrookad694712008-03-31 03:48:30 +00001169uint32_t HELPER(abs)(uint32_t x)
1170{
1171 return ((int32_t)x < 0) ? -x : x;
1172}
1173
ths5fafdf22007-09-16 21:08:06 +00001174#if defined(CONFIG_USER_ONLY)
bellardb5ff1b32005-11-26 10:38:39 +00001175
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001176void do_interrupt (CPUARMState *env)
bellardb5ff1b32005-11-26 10:38:39 +00001177{
1178 env->exception_index = -1;
1179}
1180
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001181int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
Blue Swirl97b348e2011-08-01 16:12:17 +00001182 int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +00001183{
1184 if (rw == 2) {
1185 env->exception_index = EXCP_PREFETCH_ABORT;
1186 env->cp15.c6_insn = address;
1187 } else {
1188 env->exception_index = EXCP_DATA_ABORT;
1189 env->cp15.c6_data = address;
1190 }
1191 return 1;
1192}
1193
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001194void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +00001195{
1196 cpu_abort(env, "cp15 insn %08x\n", insn);
1197}
1198
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001199uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +00001200{
1201 cpu_abort(env, "cp15 insn %08x\n", insn);
bellardb5ff1b32005-11-26 10:38:39 +00001202}
1203
pbrook9ee6e8b2007-11-11 00:04:49 +00001204/* These should probably raise undefined insn exceptions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001205void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00001206{
1207 cpu_abort(env, "v7m_mrs %d\n", reg);
1208}
1209
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001210uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00001211{
1212 cpu_abort(env, "v7m_mrs %d\n", reg);
1213 return 0;
1214}
1215
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001216void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +00001217{
1218 if (mode != ARM_CPU_MODE_USR)
1219 cpu_abort(env, "Tried to switch out of user mode\n");
1220}
1221
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001222void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00001223{
1224 cpu_abort(env, "banked r13 write\n");
1225}
1226
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001227uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00001228{
1229 cpu_abort(env, "banked r13 read\n");
1230 return 0;
1231}
1232
bellardb5ff1b32005-11-26 10:38:39 +00001233#else
1234
1235/* Map CPU modes onto saved register banks. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001236static inline int bank_number(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +00001237{
1238 switch (mode) {
1239 case ARM_CPU_MODE_USR:
1240 case ARM_CPU_MODE_SYS:
1241 return 0;
1242 case ARM_CPU_MODE_SVC:
1243 return 1;
1244 case ARM_CPU_MODE_ABT:
1245 return 2;
1246 case ARM_CPU_MODE_UND:
1247 return 3;
1248 case ARM_CPU_MODE_IRQ:
1249 return 4;
1250 case ARM_CPU_MODE_FIQ:
1251 return 5;
1252 }
Peter Maydell1b9e01c2012-01-05 15:49:06 +00001253 cpu_abort(env, "Bad mode %x\n", mode);
bellardb5ff1b32005-11-26 10:38:39 +00001254 return -1;
1255}
1256
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001257void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +00001258{
1259 int old_mode;
1260 int i;
1261
1262 old_mode = env->uncached_cpsr & CPSR_M;
1263 if (mode == old_mode)
1264 return;
1265
1266 if (old_mode == ARM_CPU_MODE_FIQ) {
1267 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +00001268 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +00001269 } else if (mode == ARM_CPU_MODE_FIQ) {
1270 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +00001271 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +00001272 }
1273
Peter Maydell1b9e01c2012-01-05 15:49:06 +00001274 i = bank_number(env, old_mode);
bellardb5ff1b32005-11-26 10:38:39 +00001275 env->banked_r13[i] = env->regs[13];
1276 env->banked_r14[i] = env->regs[14];
1277 env->banked_spsr[i] = env->spsr;
1278
Peter Maydell1b9e01c2012-01-05 15:49:06 +00001279 i = bank_number(env, mode);
bellardb5ff1b32005-11-26 10:38:39 +00001280 env->regs[13] = env->banked_r13[i];
1281 env->regs[14] = env->banked_r14[i];
1282 env->spsr = env->banked_spsr[i];
1283}
1284
pbrook9ee6e8b2007-11-11 00:04:49 +00001285static void v7m_push(CPUARMState *env, uint32_t val)
1286{
1287 env->regs[13] -= 4;
1288 stl_phys(env->regs[13], val);
1289}
1290
1291static uint32_t v7m_pop(CPUARMState *env)
1292{
1293 uint32_t val;
1294 val = ldl_phys(env->regs[13]);
1295 env->regs[13] += 4;
1296 return val;
1297}
1298
1299/* Switch to V7M main or process stack pointer. */
1300static void switch_v7m_sp(CPUARMState *env, int process)
1301{
1302 uint32_t tmp;
1303 if (env->v7m.current_sp != process) {
1304 tmp = env->v7m.other_sp;
1305 env->v7m.other_sp = env->regs[13];
1306 env->regs[13] = tmp;
1307 env->v7m.current_sp = process;
1308 }
1309}
1310
1311static void do_v7m_exception_exit(CPUARMState *env)
1312{
1313 uint32_t type;
1314 uint32_t xpsr;
1315
1316 type = env->regs[15];
1317 if (env->v7m.exception != 0)
Paul Brook983fe822010-04-05 19:34:51 +01001318 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
pbrook9ee6e8b2007-11-11 00:04:49 +00001319
1320 /* Switch to the target stack. */
1321 switch_v7m_sp(env, (type & 4) != 0);
1322 /* Pop registers. */
1323 env->regs[0] = v7m_pop(env);
1324 env->regs[1] = v7m_pop(env);
1325 env->regs[2] = v7m_pop(env);
1326 env->regs[3] = v7m_pop(env);
1327 env->regs[12] = v7m_pop(env);
1328 env->regs[14] = v7m_pop(env);
1329 env->regs[15] = v7m_pop(env);
1330 xpsr = v7m_pop(env);
1331 xpsr_write(env, xpsr, 0xfffffdff);
1332 /* Undo stack alignment. */
1333 if (xpsr & 0x200)
1334 env->regs[13] |= 4;
1335 /* ??? The exception return type specifies Thread/Handler mode. However
1336 this is also implied by the xPSR value. Not sure what to do
1337 if there is a mismatch. */
1338 /* ??? Likewise for mismatches between the CONTROL register and the stack
1339 pointer. */
1340}
1341
aurel322b3ea312009-03-07 21:48:00 +00001342static void do_interrupt_v7m(CPUARMState *env)
pbrook9ee6e8b2007-11-11 00:04:49 +00001343{
1344 uint32_t xpsr = xpsr_read(env);
1345 uint32_t lr;
1346 uint32_t addr;
1347
1348 lr = 0xfffffff1;
1349 if (env->v7m.current_sp)
1350 lr |= 4;
1351 if (env->v7m.exception == 0)
1352 lr |= 8;
1353
1354 /* For exceptions we just mark as pending on the NVIC, and let that
1355 handle it. */
1356 /* TODO: Need to escalate if the current priority is higher than the
1357 one we're raising. */
1358 switch (env->exception_index) {
1359 case EXCP_UDEF:
Paul Brook983fe822010-04-05 19:34:51 +01001360 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
pbrook9ee6e8b2007-11-11 00:04:49 +00001361 return;
1362 case EXCP_SWI:
1363 env->regs[15] += 2;
Paul Brook983fe822010-04-05 19:34:51 +01001364 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
pbrook9ee6e8b2007-11-11 00:04:49 +00001365 return;
1366 case EXCP_PREFETCH_ABORT:
1367 case EXCP_DATA_ABORT:
Paul Brook983fe822010-04-05 19:34:51 +01001368 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
pbrook9ee6e8b2007-11-11 00:04:49 +00001369 return;
1370 case EXCP_BKPT:
pbrook2ad207d2007-11-24 23:22:11 +00001371 if (semihosting_enabled) {
1372 int nr;
Paul Brookd8fd2952012-03-30 18:02:50 +01001373 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
pbrook2ad207d2007-11-24 23:22:11 +00001374 if (nr == 0xab) {
1375 env->regs[15] += 2;
1376 env->regs[0] = do_arm_semihosting(env);
1377 return;
1378 }
1379 }
Paul Brook983fe822010-04-05 19:34:51 +01001380 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
pbrook9ee6e8b2007-11-11 00:04:49 +00001381 return;
1382 case EXCP_IRQ:
Paul Brook983fe822010-04-05 19:34:51 +01001383 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
pbrook9ee6e8b2007-11-11 00:04:49 +00001384 break;
1385 case EXCP_EXCEPTION_EXIT:
1386 do_v7m_exception_exit(env);
1387 return;
1388 default:
1389 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
1390 return; /* Never happens. Keep compiler happy. */
1391 }
1392
1393 /* Align stack pointer. */
1394 /* ??? Should only do this if Configuration Control Register
1395 STACKALIGN bit is set. */
1396 if (env->regs[13] & 4) {
pbrookab19b0e2008-07-02 16:44:09 +00001397 env->regs[13] -= 4;
pbrook9ee6e8b2007-11-11 00:04:49 +00001398 xpsr |= 0x200;
1399 }
balrog6c956762008-04-13 00:57:49 +00001400 /* Switch to the handler mode. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001401 v7m_push(env, xpsr);
1402 v7m_push(env, env->regs[15]);
1403 v7m_push(env, env->regs[14]);
1404 v7m_push(env, env->regs[12]);
1405 v7m_push(env, env->regs[3]);
1406 v7m_push(env, env->regs[2]);
1407 v7m_push(env, env->regs[1]);
1408 v7m_push(env, env->regs[0]);
1409 switch_v7m_sp(env, 0);
Peter Maydellc98d1742012-03-14 12:26:10 +00001410 /* Clear IT bits */
1411 env->condexec_bits = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00001412 env->regs[14] = lr;
1413 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
1414 env->regs[15] = addr & 0xfffffffe;
1415 env->thumb = addr & 1;
1416}
1417
bellardb5ff1b32005-11-26 10:38:39 +00001418/* Handle a CPU exception. */
1419void do_interrupt(CPUARMState *env)
1420{
1421 uint32_t addr;
1422 uint32_t mask;
1423 int new_mode;
1424 uint32_t offset;
1425
pbrook9ee6e8b2007-11-11 00:04:49 +00001426 if (IS_M(env)) {
1427 do_interrupt_v7m(env);
1428 return;
1429 }
bellardb5ff1b32005-11-26 10:38:39 +00001430 /* TODO: Vectored interrupt controller. */
1431 switch (env->exception_index) {
1432 case EXCP_UDEF:
1433 new_mode = ARM_CPU_MODE_UND;
1434 addr = 0x04;
1435 mask = CPSR_I;
1436 if (env->thumb)
1437 offset = 2;
1438 else
1439 offset = 4;
1440 break;
1441 case EXCP_SWI:
pbrook8e716212007-01-20 17:12:09 +00001442 if (semihosting_enabled) {
1443 /* Check for semihosting interrupt. */
1444 if (env->thumb) {
Paul Brookd8fd2952012-03-30 18:02:50 +01001445 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
pbrook8e716212007-01-20 17:12:09 +00001446 } else {
Paul Brookd8fd2952012-03-30 18:02:50 +01001447 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
1448 & 0xffffff;
pbrook8e716212007-01-20 17:12:09 +00001449 }
1450 /* Only intercept calls from privileged modes, to provide some
1451 semblance of security. */
1452 if (((mask == 0x123456 && !env->thumb)
1453 || (mask == 0xab && env->thumb))
1454 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
1455 env->regs[0] = do_arm_semihosting(env);
1456 return;
1457 }
1458 }
bellardb5ff1b32005-11-26 10:38:39 +00001459 new_mode = ARM_CPU_MODE_SVC;
1460 addr = 0x08;
1461 mask = CPSR_I;
balrog601d70b2008-04-20 01:03:45 +00001462 /* The PC already points to the next instruction. */
bellardb5ff1b32005-11-26 10:38:39 +00001463 offset = 0;
1464 break;
pbrook06c949e2006-02-04 19:35:26 +00001465 case EXCP_BKPT:
pbrook9ee6e8b2007-11-11 00:04:49 +00001466 /* See if this is a semihosting syscall. */
pbrook2ad207d2007-11-24 23:22:11 +00001467 if (env->thumb && semihosting_enabled) {
Paul Brookd8fd2952012-03-30 18:02:50 +01001468 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
pbrook9ee6e8b2007-11-11 00:04:49 +00001469 if (mask == 0xab
1470 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
1471 env->regs[15] += 2;
1472 env->regs[0] = do_arm_semihosting(env);
1473 return;
1474 }
1475 }
Alex Zuepke81c05da2011-06-03 18:42:17 +02001476 env->cp15.c5_insn = 2;
pbrook9ee6e8b2007-11-11 00:04:49 +00001477 /* Fall through to prefetch abort. */
1478 case EXCP_PREFETCH_ABORT:
bellardb5ff1b32005-11-26 10:38:39 +00001479 new_mode = ARM_CPU_MODE_ABT;
1480 addr = 0x0c;
1481 mask = CPSR_A | CPSR_I;
1482 offset = 4;
1483 break;
1484 case EXCP_DATA_ABORT:
1485 new_mode = ARM_CPU_MODE_ABT;
1486 addr = 0x10;
1487 mask = CPSR_A | CPSR_I;
1488 offset = 8;
1489 break;
1490 case EXCP_IRQ:
1491 new_mode = ARM_CPU_MODE_IRQ;
1492 addr = 0x18;
1493 /* Disable IRQ and imprecise data aborts. */
1494 mask = CPSR_A | CPSR_I;
1495 offset = 4;
1496 break;
1497 case EXCP_FIQ:
1498 new_mode = ARM_CPU_MODE_FIQ;
1499 addr = 0x1c;
1500 /* Disable FIQ, IRQ and imprecise data aborts. */
1501 mask = CPSR_A | CPSR_I | CPSR_F;
1502 offset = 4;
1503 break;
1504 default:
1505 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
1506 return; /* Never happens. Keep compiler happy. */
1507 }
1508 /* High vectors. */
1509 if (env->cp15.c1_sys & (1 << 13)) {
1510 addr += 0xffff0000;
1511 }
1512 switch_mode (env, new_mode);
1513 env->spsr = cpsr_read(env);
pbrook9ee6e8b2007-11-11 00:04:49 +00001514 /* Clear IT bits. */
1515 env->condexec_bits = 0;
Rabin Vincent30a8cac2010-02-15 00:02:36 +05301516 /* Switch to the new mode, and to the correct instruction set. */
bellard6d7e6322005-12-18 16:54:08 +00001517 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
bellardb5ff1b32005-11-26 10:38:39 +00001518 env->uncached_cpsr |= mask;
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +04001519 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
1520 * and we should just guard the thumb mode on V4 */
1521 if (arm_feature(env, ARM_FEATURE_V4T)) {
1522 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
1523 }
bellardb5ff1b32005-11-26 10:38:39 +00001524 env->regs[14] = env->regs[15] + offset;
1525 env->regs[15] = addr;
1526 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
1527}
1528
1529/* Check section/page access permissions.
1530 Returns the page protection flags, or zero if the access is not
1531 permitted. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001532static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001533 int access_type, int is_user)
bellardb5ff1b32005-11-26 10:38:39 +00001534{
pbrook9ee6e8b2007-11-11 00:04:49 +00001535 int prot_ro;
1536
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001537 if (domain_prot == 3) {
bellardb5ff1b32005-11-26 10:38:39 +00001538 return PAGE_READ | PAGE_WRITE;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001539 }
bellardb5ff1b32005-11-26 10:38:39 +00001540
pbrook9ee6e8b2007-11-11 00:04:49 +00001541 if (access_type == 1)
1542 prot_ro = 0;
1543 else
1544 prot_ro = PAGE_READ;
1545
bellardb5ff1b32005-11-26 10:38:39 +00001546 switch (ap) {
1547 case 0:
pbrook78600322006-09-09 14:36:26 +00001548 if (access_type == 1)
bellardb5ff1b32005-11-26 10:38:39 +00001549 return 0;
1550 switch ((env->cp15.c1_sys >> 8) & 3) {
1551 case 1:
1552 return is_user ? 0 : PAGE_READ;
1553 case 2:
1554 return PAGE_READ;
1555 default:
1556 return 0;
1557 }
1558 case 1:
1559 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
1560 case 2:
1561 if (is_user)
pbrook9ee6e8b2007-11-11 00:04:49 +00001562 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +00001563 else
1564 return PAGE_READ | PAGE_WRITE;
1565 case 3:
1566 return PAGE_READ | PAGE_WRITE;
pbrookd4934d12008-12-19 12:39:00 +00001567 case 4: /* Reserved. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001568 return 0;
1569 case 5:
1570 return is_user ? 0 : prot_ro;
1571 case 6:
1572 return prot_ro;
pbrookd4934d12008-12-19 12:39:00 +00001573 case 7:
Jamie Iles0ab06d82011-06-23 01:12:59 +00001574 if (!arm_feature (env, ARM_FEATURE_V6K))
pbrookd4934d12008-12-19 12:39:00 +00001575 return 0;
1576 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +00001577 default:
1578 abort();
1579 }
1580}
1581
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001582static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
pbrookb2fa1792008-10-22 19:22:30 +00001583{
1584 uint32_t table;
1585
1586 if (address & env->cp15.c2_mask)
1587 table = env->cp15.c2_base1 & 0xffffc000;
1588 else
1589 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
1590
1591 table |= (address >> 18) & 0x3ffc;
1592 return table;
1593}
1594
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001595static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +00001596 int is_user, uint32_t *phys_ptr, int *prot,
1597 target_ulong *page_size)
bellardb5ff1b32005-11-26 10:38:39 +00001598{
1599 int code;
1600 uint32_t table;
1601 uint32_t desc;
1602 int type;
1603 int ap;
1604 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001605 int domain_prot;
bellardb5ff1b32005-11-26 10:38:39 +00001606 uint32_t phys_addr;
1607
pbrook9ee6e8b2007-11-11 00:04:49 +00001608 /* Pagetable walk. */
1609 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00001610 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00001611 desc = ldl_phys(table);
1612 type = (desc & 3);
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001613 domain = (desc >> 5) & 0x0f;
1614 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
pbrook9ee6e8b2007-11-11 00:04:49 +00001615 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00001616 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001617 code = 5;
1618 goto do_fault;
1619 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001620 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001621 if (type == 2)
1622 code = 9; /* Section domain fault. */
1623 else
1624 code = 11; /* Page domain fault. */
1625 goto do_fault;
1626 }
1627 if (type == 2) {
1628 /* 1Mb section. */
1629 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1630 ap = (desc >> 10) & 3;
1631 code = 13;
Paul Brookd4c430a2010-03-17 02:14:28 +00001632 *page_size = 1024 * 1024;
pbrook9ee6e8b2007-11-11 00:04:49 +00001633 } else {
1634 /* Lookup l2 entry. */
1635 if (type == 1) {
1636 /* Coarse pagetable. */
1637 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1638 } else {
1639 /* Fine pagetable. */
1640 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1641 }
1642 desc = ldl_phys(table);
1643 switch (desc & 3) {
1644 case 0: /* Page translation fault. */
1645 code = 7;
1646 goto do_fault;
1647 case 1: /* 64k page. */
1648 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1649 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001650 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001651 break;
1652 case 2: /* 4k page. */
1653 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1654 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001655 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001656 break;
1657 case 3: /* 1k page. */
1658 if (type == 1) {
1659 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1660 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1661 } else {
1662 /* Page translation fault. */
1663 code = 7;
1664 goto do_fault;
1665 }
1666 } else {
1667 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1668 }
1669 ap = (desc >> 4) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001670 *page_size = 0x400;
pbrook9ee6e8b2007-11-11 00:04:49 +00001671 break;
1672 default:
1673 /* Never happens, but compiler isn't smart enough to tell. */
1674 abort();
1675 }
1676 code = 15;
1677 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001678 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
pbrook9ee6e8b2007-11-11 00:04:49 +00001679 if (!*prot) {
1680 /* Access permission fault. */
1681 goto do_fault;
1682 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301683 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00001684 *phys_ptr = phys_addr;
1685 return 0;
1686do_fault:
1687 return code | (domain << 4);
1688}
1689
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001690static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +00001691 int is_user, uint32_t *phys_ptr, int *prot,
1692 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00001693{
1694 int code;
1695 uint32_t table;
1696 uint32_t desc;
1697 uint32_t xn;
1698 int type;
1699 int ap;
1700 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001701 int domain_prot;
pbrook9ee6e8b2007-11-11 00:04:49 +00001702 uint32_t phys_addr;
1703
1704 /* Pagetable walk. */
1705 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00001706 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00001707 desc = ldl_phys(table);
1708 type = (desc & 3);
1709 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00001710 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001711 code = 5;
1712 domain = 0;
1713 goto do_fault;
1714 } else if (type == 2 && (desc & (1 << 18))) {
1715 /* Supersection. */
1716 domain = 0;
1717 } else {
1718 /* Section or page. */
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001719 domain = (desc >> 5) & 0x0f;
pbrook9ee6e8b2007-11-11 00:04:49 +00001720 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001721 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1722 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001723 if (type == 2)
1724 code = 9; /* Section domain fault. */
1725 else
1726 code = 11; /* Page domain fault. */
1727 goto do_fault;
1728 }
1729 if (type == 2) {
1730 if (desc & (1 << 18)) {
1731 /* Supersection. */
1732 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001733 *page_size = 0x1000000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001734 } else {
1735 /* Section. */
1736 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001737 *page_size = 0x100000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001738 }
1739 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1740 xn = desc & (1 << 4);
1741 code = 13;
1742 } else {
1743 /* Lookup l2 entry. */
1744 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1745 desc = ldl_phys(table);
1746 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1747 switch (desc & 3) {
1748 case 0: /* Page translation fault. */
1749 code = 7;
1750 goto do_fault;
1751 case 1: /* 64k page. */
1752 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1753 xn = desc & (1 << 15);
Paul Brookd4c430a2010-03-17 02:14:28 +00001754 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001755 break;
1756 case 2: case 3: /* 4k page. */
1757 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1758 xn = desc & 1;
Paul Brookd4c430a2010-03-17 02:14:28 +00001759 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001760 break;
1761 default:
1762 /* Never happens, but compiler isn't smart enough to tell. */
1763 abort();
1764 }
1765 code = 15;
1766 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001767 if (domain_prot == 3) {
Juha Riihimäkic0034322010-12-08 13:15:16 +02001768 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1769 } else {
1770 if (xn && access_type == 2)
1771 goto do_fault;
pbrook9ee6e8b2007-11-11 00:04:49 +00001772
Juha Riihimäkic0034322010-12-08 13:15:16 +02001773 /* The simplified model uses AP[0] as an access control bit. */
1774 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1775 /* Access flag fault. */
1776 code = (code == 15) ? 6 : 3;
1777 goto do_fault;
1778 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001779 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
Juha Riihimäkic0034322010-12-08 13:15:16 +02001780 if (!*prot) {
1781 /* Access permission fault. */
1782 goto do_fault;
1783 }
1784 if (!xn) {
1785 *prot |= PAGE_EXEC;
1786 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301787 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001788 *phys_ptr = phys_addr;
1789 return 0;
1790do_fault:
1791 return code | (domain << 4);
1792}
1793
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001794static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
pbrook9ee6e8b2007-11-11 00:04:49 +00001795 int is_user, uint32_t *phys_ptr, int *prot)
1796{
1797 int n;
1798 uint32_t mask;
1799 uint32_t base;
1800
1801 *phys_ptr = address;
1802 for (n = 7; n >= 0; n--) {
1803 base = env->cp15.c6_region[n];
1804 if ((base & 1) == 0)
1805 continue;
1806 mask = 1 << ((base >> 1) & 0x1f);
1807 /* Keep this shift separate from the above to avoid an
1808 (undefined) << 32. */
1809 mask = (mask << 1) - 1;
1810 if (((base ^ address) & ~mask) == 0)
1811 break;
1812 }
1813 if (n < 0)
1814 return 2;
1815
1816 if (access_type == 2) {
1817 mask = env->cp15.c5_insn;
1818 } else {
1819 mask = env->cp15.c5_data;
1820 }
1821 mask = (mask >> (n * 4)) & 0xf;
1822 switch (mask) {
1823 case 0:
1824 return 1;
1825 case 1:
1826 if (is_user)
1827 return 1;
1828 *prot = PAGE_READ | PAGE_WRITE;
1829 break;
1830 case 2:
1831 *prot = PAGE_READ;
1832 if (!is_user)
1833 *prot |= PAGE_WRITE;
1834 break;
1835 case 3:
1836 *prot = PAGE_READ | PAGE_WRITE;
1837 break;
1838 case 5:
1839 if (is_user)
1840 return 1;
1841 *prot = PAGE_READ;
1842 break;
1843 case 6:
1844 *prot = PAGE_READ;
1845 break;
1846 default:
1847 /* Bad permission. */
1848 return 1;
1849 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301850 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00001851 return 0;
1852}
1853
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001854static inline int get_phys_addr(CPUARMState *env, uint32_t address,
pbrook9ee6e8b2007-11-11 00:04:49 +00001855 int access_type, int is_user,
Paul Brookd4c430a2010-03-17 02:14:28 +00001856 uint32_t *phys_ptr, int *prot,
1857 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00001858{
bellardb5ff1b32005-11-26 10:38:39 +00001859 /* Fast Context Switch Extension. */
1860 if (address < 0x02000000)
1861 address += env->cp15.c13_fcse;
1862
1863 if ((env->cp15.c1_sys & 1) == 0) {
pbrookce819862007-05-08 02:30:40 +00001864 /* MMU/MPU disabled. */
bellardb5ff1b32005-11-26 10:38:39 +00001865 *phys_ptr = address;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301866 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
Paul Brookd4c430a2010-03-17 02:14:28 +00001867 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001868 return 0;
pbrookce819862007-05-08 02:30:40 +00001869 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
Paul Brookd4c430a2010-03-17 02:14:28 +00001870 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001871 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1872 prot);
1873 } else if (env->cp15.c1_sys & (1 << 23)) {
1874 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001875 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001876 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001877 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001878 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001879 }
bellardb5ff1b32005-11-26 10:38:39 +00001880}
1881
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001882int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
Blue Swirl97b348e2011-08-01 16:12:17 +00001883 int access_type, int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +00001884{
1885 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001886 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001887 int prot;
j_mayer6ebbf392007-10-14 07:07:08 +00001888 int ret, is_user;
bellardb5ff1b32005-11-26 10:38:39 +00001889
j_mayer6ebbf392007-10-14 07:07:08 +00001890 is_user = mmu_idx == MMU_USER_IDX;
Paul Brookd4c430a2010-03-17 02:14:28 +00001891 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1892 &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001893 if (ret == 0) {
1894 /* Map a single [sub]page. */
1895 phys_addr &= ~(uint32_t)0x3ff;
1896 address &= ~(uint32_t)0x3ff;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301897 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
Paul Brookd4c430a2010-03-17 02:14:28 +00001898 return 0;
bellardb5ff1b32005-11-26 10:38:39 +00001899 }
1900
1901 if (access_type == 2) {
1902 env->cp15.c5_insn = ret;
1903 env->cp15.c6_insn = address;
1904 env->exception_index = EXCP_PREFETCH_ABORT;
1905 } else {
1906 env->cp15.c5_data = ret;
pbrook9ee6e8b2007-11-11 00:04:49 +00001907 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1908 env->cp15.c5_data |= (1 << 11);
bellardb5ff1b32005-11-26 10:38:39 +00001909 env->cp15.c6_data = address;
1910 env->exception_index = EXCP_DATA_ABORT;
1911 }
1912 return 1;
1913}
1914
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001915target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
bellardb5ff1b32005-11-26 10:38:39 +00001916{
1917 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001918 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001919 int prot;
1920 int ret;
1921
Paul Brookd4c430a2010-03-17 02:14:28 +00001922 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001923
1924 if (ret != 0)
1925 return -1;
1926
1927 return phys_addr;
1928}
1929
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001930void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +00001931{
pbrook9ee6e8b2007-11-11 00:04:49 +00001932 int op1;
1933 int op2;
1934 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001935
pbrook9ee6e8b2007-11-11 00:04:49 +00001936 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001937 op2 = (insn >> 5) & 7;
pbrookce819862007-05-08 02:30:40 +00001938 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001939 switch ((insn >> 16) & 0xf) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001940 case 0:
pbrook9ee6e8b2007-11-11 00:04:49 +00001941 /* ID codes. */
balrog610c3c82007-06-24 12:09:48 +00001942 if (arm_feature(env, ARM_FEATURE_XSCALE))
1943 break;
balrogc3d26892007-07-29 17:57:26 +00001944 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1945 break;
pbrooka49ea272008-12-19 13:37:53 +00001946 if (arm_feature(env, ARM_FEATURE_V7)
1947 && op1 == 2 && crm == 0 && op2 == 0) {
1948 env->cp15.c0_cssel = val & 0xf;
1949 break;
1950 }
bellardb5ff1b32005-11-26 10:38:39 +00001951 goto bad_reg;
1952 case 1: /* System configuration. */
Rob Herring2be27622012-01-13 17:25:08 +00001953 if (arm_feature(env, ARM_FEATURE_V7)
1954 && op1 == 0 && crm == 1 && op2 == 0) {
1955 env->cp15.c1_scr = val;
1956 break;
1957 }
balrogc3d26892007-07-29 17:57:26 +00001958 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1959 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001960 switch (op2) {
1961 case 0:
pbrookce819862007-05-08 02:30:40 +00001962 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
balrogc1713132007-04-30 01:26:42 +00001963 env->cp15.c1_sys = val;
bellardb5ff1b32005-11-26 10:38:39 +00001964 /* ??? Lots of these bits are not implemented. */
1965 /* This may enable/disable the MMU, so do a TLB flush. */
1966 tlb_flush(env, 1);
1967 break;
Stefan Weil61cc8702011-04-13 22:45:22 +02001968 case 1: /* Auxiliary control register. */
balrog610c3c82007-06-24 12:09:48 +00001969 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1970 env->cp15.c1_xscaleauxcr = val;
balrogc1713132007-04-30 01:26:42 +00001971 break;
balrog610c3c82007-06-24 12:09:48 +00001972 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001973 /* Not implemented. */
1974 break;
bellardb5ff1b32005-11-26 10:38:39 +00001975 case 2:
balrog610c3c82007-06-24 12:09:48 +00001976 if (arm_feature(env, ARM_FEATURE_XSCALE))
1977 goto bad_reg;
pbrook4be27db2008-10-22 16:14:08 +00001978 if (env->cp15.c1_coproc != val) {
1979 env->cp15.c1_coproc = val;
1980 /* ??? Is this safe when called from within a TB? */
1981 tb_flush(env);
1982 }
balrogc1713132007-04-30 01:26:42 +00001983 break;
bellardb5ff1b32005-11-26 10:38:39 +00001984 default:
1985 goto bad_reg;
1986 }
1987 break;
bellardb5ff1b32005-11-26 10:38:39 +00001988 case 4: /* Reserved. */
1989 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001990 case 12: /* Reserved. */
1991 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001992 }
1993 return;
1994bad_reg:
1995 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001996 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1997 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00001998}
1999
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002000uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +00002001{
pbrook9ee6e8b2007-11-11 00:04:49 +00002002 int op1;
2003 int op2;
2004 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00002005
pbrook9ee6e8b2007-11-11 00:04:49 +00002006 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00002007 op2 = (insn >> 5) & 7;
balrogc3d26892007-07-29 17:57:26 +00002008 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00002009 switch ((insn >> 16) & 0xf) {
2010 case 0: /* ID codes. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002011 switch (op1) {
2012 case 0:
2013 switch (crm) {
2014 case 0:
2015 switch (op2) {
2016 case 0: /* Device ID. */
2017 return env->cp15.c0_cpuid;
2018 case 1: /* Cache Type. */
2019 return env->cp15.c0_cachetype;
2020 case 2: /* TCM status. */
2021 return 0;
2022 case 3: /* TLB type register. */
2023 return 0; /* No lockable TLB entries. */
Peter Maydell607b4b02011-02-03 19:43:23 +00002024 case 5: /* MPIDR */
2025 /* The MPIDR was standardised in v7; prior to
2026 * this it was implemented only in the 11MPCore.
2027 * For all other pre-v7 cores it does not exist.
2028 */
2029 if (arm_feature(env, ARM_FEATURE_V7) ||
2030 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
2031 int mpidr = env->cpu_index;
2032 /* We don't support setting cluster ID ([8..11])
2033 * so these bits always RAZ.
2034 */
2035 if (arm_feature(env, ARM_FEATURE_V7MP)) {
2036 mpidr |= (1 << 31);
2037 /* Cores which are uniprocessor (non-coherent)
2038 * but still implement the MP extensions set
2039 * bit 30. (For instance, A9UP.) However we do
2040 * not currently model any of those cores.
2041 */
2042 }
2043 return mpidr;
Paul Brook10055562009-11-19 16:45:20 +00002044 }
Peter Maydell607b4b02011-02-03 19:43:23 +00002045 /* otherwise fall through to the unimplemented-reg case */
pbrook9ee6e8b2007-11-11 00:04:49 +00002046 default:
2047 goto bad_reg;
2048 }
2049 case 1:
2050 if (!arm_feature(env, ARM_FEATURE_V6))
2051 goto bad_reg;
2052 return env->cp15.c0_c1[op2];
2053 case 2:
2054 if (!arm_feature(env, ARM_FEATURE_V6))
2055 goto bad_reg;
2056 return env->cp15.c0_c2[op2];
2057 case 3: case 4: case 5: case 6: case 7:
2058 return 0;
2059 default:
2060 goto bad_reg;
2061 }
2062 case 1:
2063 /* These registers aren't documented on arm11 cores. However
2064 Linux looks at them anyway. */
2065 if (!arm_feature(env, ARM_FEATURE_V6))
2066 goto bad_reg;
2067 if (crm != 0)
2068 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00002069 if (!arm_feature(env, ARM_FEATURE_V7))
2070 return 0;
2071
2072 switch (op2) {
2073 case 0:
2074 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
2075 case 1:
2076 return env->cp15.c0_clid;
2077 case 7:
2078 return 0;
2079 }
2080 goto bad_reg;
2081 case 2:
2082 if (op2 != 0 || crm != 0)
balrog610c3c82007-06-24 12:09:48 +00002083 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00002084 return env->cp15.c0_cssel;
pbrook9ee6e8b2007-11-11 00:04:49 +00002085 default:
2086 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00002087 }
2088 case 1: /* System configuration. */
Rob Herring2be27622012-01-13 17:25:08 +00002089 if (arm_feature(env, ARM_FEATURE_V7)
2090 && op1 == 0 && crm == 1 && op2 == 0) {
2091 return env->cp15.c1_scr;
2092 }
balrogc3d26892007-07-29 17:57:26 +00002093 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2094 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00002095 switch (op2) {
2096 case 0: /* Control register. */
2097 return env->cp15.c1_sys;
2098 case 1: /* Auxiliary control register. */
balrogc1713132007-04-30 01:26:42 +00002099 if (arm_feature(env, ARM_FEATURE_XSCALE))
balrog610c3c82007-06-24 12:09:48 +00002100 return env->cp15.c1_xscaleauxcr;
pbrook9ee6e8b2007-11-11 00:04:49 +00002101 if (!arm_feature(env, ARM_FEATURE_AUXCR))
2102 goto bad_reg;
2103 switch (ARM_CPUID(env)) {
2104 case ARM_CPUID_ARM1026:
2105 return 1;
2106 case ARM_CPUID_ARM1136:
balrog827df9f2008-04-14 21:05:22 +00002107 case ARM_CPUID_ARM1136_R2:
Jamie Iles7807eed2011-07-20 10:32:54 +00002108 case ARM_CPUID_ARM1176:
pbrook9ee6e8b2007-11-11 00:04:49 +00002109 return 7;
2110 case ARM_CPUID_ARM11MPCORE:
2111 return 1;
2112 case ARM_CPUID_CORTEXA8:
aurel32533d1772009-03-07 22:10:28 +00002113 return 2;
Paul Brook10055562009-11-19 16:45:20 +00002114 case ARM_CPUID_CORTEXA9:
Peter Maydell0b03bdf2012-01-25 12:42:29 +00002115 case ARM_CPUID_CORTEXA15:
Paul Brook10055562009-11-19 16:45:20 +00002116 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002117 default:
2118 goto bad_reg;
2119 }
bellardb5ff1b32005-11-26 10:38:39 +00002120 case 2: /* Coprocessor access register. */
balrog610c3c82007-06-24 12:09:48 +00002121 if (arm_feature(env, ARM_FEATURE_XSCALE))
2122 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00002123 return env->cp15.c1_coproc;
2124 default:
2125 goto bad_reg;
2126 }
bellardb5ff1b32005-11-26 10:38:39 +00002127 case 4: /* Reserved. */
2128 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00002129 case 11: /* TCM DMA control. */
2130 case 12: /* Reserved. */
2131 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00002132 }
2133bad_reg:
2134 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002135 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2136 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00002137 return 0;
2138}
2139
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002140void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002141{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002142 if ((env->uncached_cpsr & CPSR_M) == mode) {
2143 env->regs[13] = val;
2144 } else {
Peter Maydell1b9e01c2012-01-05 15:49:06 +00002145 env->banked_r13[bank_number(env, mode)] = val;
Peter Maydell39ea3d42011-01-14 20:39:18 +01002146 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002147}
2148
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002149uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00002150{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002151 if ((env->uncached_cpsr & CPSR_M) == mode) {
2152 return env->regs[13];
2153 } else {
Peter Maydell1b9e01c2012-01-05 15:49:06 +00002154 return env->banked_r13[bank_number(env, mode)];
Peter Maydell39ea3d42011-01-14 20:39:18 +01002155 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002156}
2157
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002158uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00002159{
2160 switch (reg) {
2161 case 0: /* APSR */
2162 return xpsr_read(env) & 0xf8000000;
2163 case 1: /* IAPSR */
2164 return xpsr_read(env) & 0xf80001ff;
2165 case 2: /* EAPSR */
2166 return xpsr_read(env) & 0xff00fc00;
2167 case 3: /* xPSR */
2168 return xpsr_read(env) & 0xff00fdff;
2169 case 5: /* IPSR */
2170 return xpsr_read(env) & 0x000001ff;
2171 case 6: /* EPSR */
2172 return xpsr_read(env) & 0x0700fc00;
2173 case 7: /* IEPSR */
2174 return xpsr_read(env) & 0x0700edff;
2175 case 8: /* MSP */
2176 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2177 case 9: /* PSP */
2178 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2179 case 16: /* PRIMASK */
2180 return (env->uncached_cpsr & CPSR_I) != 0;
Sebastian Huber82845822011-05-29 02:58:41 +00002181 case 17: /* BASEPRI */
2182 case 18: /* BASEPRI_MAX */
pbrook9ee6e8b2007-11-11 00:04:49 +00002183 return env->v7m.basepri;
Sebastian Huber82845822011-05-29 02:58:41 +00002184 case 19: /* FAULTMASK */
2185 return (env->uncached_cpsr & CPSR_F) != 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002186 case 20: /* CONTROL */
2187 return env->v7m.control;
2188 default:
2189 /* ??? For debugging only. */
2190 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2191 return 0;
2192 }
2193}
2194
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002195void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002196{
2197 switch (reg) {
2198 case 0: /* APSR */
2199 xpsr_write(env, val, 0xf8000000);
2200 break;
2201 case 1: /* IAPSR */
2202 xpsr_write(env, val, 0xf8000000);
2203 break;
2204 case 2: /* EAPSR */
2205 xpsr_write(env, val, 0xfe00fc00);
2206 break;
2207 case 3: /* xPSR */
2208 xpsr_write(env, val, 0xfe00fc00);
2209 break;
2210 case 5: /* IPSR */
2211 /* IPSR bits are readonly. */
2212 break;
2213 case 6: /* EPSR */
2214 xpsr_write(env, val, 0x0600fc00);
2215 break;
2216 case 7: /* IEPSR */
2217 xpsr_write(env, val, 0x0600fc00);
2218 break;
2219 case 8: /* MSP */
2220 if (env->v7m.current_sp)
2221 env->v7m.other_sp = val;
2222 else
2223 env->regs[13] = val;
2224 break;
2225 case 9: /* PSP */
2226 if (env->v7m.current_sp)
2227 env->regs[13] = val;
2228 else
2229 env->v7m.other_sp = val;
2230 break;
2231 case 16: /* PRIMASK */
2232 if (val & 1)
2233 env->uncached_cpsr |= CPSR_I;
2234 else
2235 env->uncached_cpsr &= ~CPSR_I;
2236 break;
Sebastian Huber82845822011-05-29 02:58:41 +00002237 case 17: /* BASEPRI */
2238 env->v7m.basepri = val & 0xff;
2239 break;
2240 case 18: /* BASEPRI_MAX */
2241 val &= 0xff;
2242 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2243 env->v7m.basepri = val;
2244 break;
2245 case 19: /* FAULTMASK */
pbrook9ee6e8b2007-11-11 00:04:49 +00002246 if (val & 1)
2247 env->uncached_cpsr |= CPSR_F;
2248 else
2249 env->uncached_cpsr &= ~CPSR_F;
2250 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00002251 case 20: /* CONTROL */
2252 env->v7m.control = val & 3;
2253 switch_v7m_sp(env, (val & 2) != 0);
2254 break;
2255 default:
2256 /* ??? For debugging only. */
2257 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2258 return;
2259 }
2260}
2261
bellardb5ff1b32005-11-26 10:38:39 +00002262#endif
pbrook6ddbc6e2008-03-31 03:46:33 +00002263
2264/* Note that signed overflow is undefined in C. The following routines are
2265 careful to use unsigned types where modulo arithmetic is required.
2266 Failure to do so _will_ break on newer gcc. */
2267
2268/* Signed saturating arithmetic. */
2269
aurel321654b2d2008-04-11 04:55:07 +00002270/* Perform 16-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002271static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2272{
2273 uint16_t res;
2274
2275 res = a + b;
2276 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2277 if (a & 0x8000)
2278 res = 0x8000;
2279 else
2280 res = 0x7fff;
2281 }
2282 return res;
2283}
2284
aurel321654b2d2008-04-11 04:55:07 +00002285/* Perform 8-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002286static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2287{
2288 uint8_t res;
2289
2290 res = a + b;
2291 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2292 if (a & 0x80)
2293 res = 0x80;
2294 else
2295 res = 0x7f;
2296 }
2297 return res;
2298}
2299
aurel321654b2d2008-04-11 04:55:07 +00002300/* Perform 16-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002301static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2302{
2303 uint16_t res;
2304
2305 res = a - b;
2306 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2307 if (a & 0x8000)
2308 res = 0x8000;
2309 else
2310 res = 0x7fff;
2311 }
2312 return res;
2313}
2314
aurel321654b2d2008-04-11 04:55:07 +00002315/* Perform 8-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002316static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2317{
2318 uint8_t res;
2319
2320 res = a - b;
2321 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2322 if (a & 0x80)
2323 res = 0x80;
2324 else
2325 res = 0x7f;
2326 }
2327 return res;
2328}
2329
2330#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2331#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2332#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2333#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2334#define PFX q
2335
2336#include "op_addsub.h"
2337
2338/* Unsigned saturating arithmetic. */
pbrook460a09c2008-05-01 12:04:35 +00002339static inline uint16_t add16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002340{
2341 uint16_t res;
2342 res = a + b;
2343 if (res < a)
2344 res = 0xffff;
2345 return res;
2346}
2347
pbrook460a09c2008-05-01 12:04:35 +00002348static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002349{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002350 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002351 return a - b;
2352 else
2353 return 0;
2354}
2355
2356static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2357{
2358 uint8_t res;
2359 res = a + b;
2360 if (res < a)
2361 res = 0xff;
2362 return res;
2363}
2364
2365static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2366{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002367 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002368 return a - b;
2369 else
2370 return 0;
2371}
2372
2373#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2374#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2375#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2376#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2377#define PFX uq
2378
2379#include "op_addsub.h"
2380
2381/* Signed modulo arithmetic. */
2382#define SARITH16(a, b, n, op) do { \
2383 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002384 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002385 RESULT(sum, n, 16); \
2386 if (sum >= 0) \
2387 ge |= 3 << (n * 2); \
2388 } while(0)
2389
2390#define SARITH8(a, b, n, op) do { \
2391 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002392 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002393 RESULT(sum, n, 8); \
2394 if (sum >= 0) \
2395 ge |= 1 << n; \
2396 } while(0)
2397
2398
2399#define ADD16(a, b, n) SARITH16(a, b, n, +)
2400#define SUB16(a, b, n) SARITH16(a, b, n, -)
2401#define ADD8(a, b, n) SARITH8(a, b, n, +)
2402#define SUB8(a, b, n) SARITH8(a, b, n, -)
2403#define PFX s
2404#define ARITH_GE
2405
2406#include "op_addsub.h"
2407
2408/* Unsigned modulo arithmetic. */
2409#define ADD16(a, b, n) do { \
2410 uint32_t sum; \
2411 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2412 RESULT(sum, n, 16); \
balroga87aa102008-07-19 10:46:13 +00002413 if ((sum >> 16) == 1) \
pbrook6ddbc6e2008-03-31 03:46:33 +00002414 ge |= 3 << (n * 2); \
2415 } while(0)
2416
2417#define ADD8(a, b, n) do { \
2418 uint32_t sum; \
2419 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2420 RESULT(sum, n, 8); \
balroga87aa102008-07-19 10:46:13 +00002421 if ((sum >> 8) == 1) \
2422 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002423 } while(0)
2424
2425#define SUB16(a, b, n) do { \
2426 uint32_t sum; \
2427 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2428 RESULT(sum, n, 16); \
2429 if ((sum >> 16) == 0) \
2430 ge |= 3 << (n * 2); \
2431 } while(0)
2432
2433#define SUB8(a, b, n) do { \
2434 uint32_t sum; \
2435 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2436 RESULT(sum, n, 8); \
2437 if ((sum >> 8) == 0) \
balroga87aa102008-07-19 10:46:13 +00002438 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002439 } while(0)
2440
2441#define PFX u
2442#define ARITH_GE
2443
2444#include "op_addsub.h"
2445
2446/* Halved signed arithmetic. */
2447#define ADD16(a, b, n) \
2448 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2449#define SUB16(a, b, n) \
2450 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2451#define ADD8(a, b, n) \
2452 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2453#define SUB8(a, b, n) \
2454 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2455#define PFX sh
2456
2457#include "op_addsub.h"
2458
2459/* Halved unsigned arithmetic. */
2460#define ADD16(a, b, n) \
2461 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2462#define SUB16(a, b, n) \
2463 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2464#define ADD8(a, b, n) \
2465 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2466#define SUB8(a, b, n) \
2467 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2468#define PFX uh
2469
2470#include "op_addsub.h"
2471
2472static inline uint8_t do_usad(uint8_t a, uint8_t b)
2473{
2474 if (a > b)
2475 return a - b;
2476 else
2477 return b - a;
2478}
2479
2480/* Unsigned sum of absolute byte differences. */
2481uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2482{
2483 uint32_t sum;
2484 sum = do_usad(a, b);
2485 sum += do_usad(a >> 8, b >> 8);
2486 sum += do_usad(a >> 16, b >>16);
2487 sum += do_usad(a >> 24, b >> 24);
2488 return sum;
2489}
2490
2491/* For ARMv6 SEL instruction. */
2492uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2493{
2494 uint32_t mask;
2495
2496 mask = 0;
2497 if (flags & 1)
2498 mask |= 0xff;
2499 if (flags & 2)
2500 mask |= 0xff00;
2501 if (flags & 4)
2502 mask |= 0xff0000;
2503 if (flags & 8)
2504 mask |= 0xff000000;
2505 return (a & mask) | (b & ~mask);
2506}
2507
pbrook5e3f8782008-03-31 03:47:34 +00002508uint32_t HELPER(logicq_cc)(uint64_t val)
2509{
2510 return (val >> 32) | (val != 0);
2511}
pbrook4373f3c2008-03-31 03:47:19 +00002512
2513/* VFP support. We follow the convention used for VFP instrunctions:
2514 Single precition routines have a "s" suffix, double precision a
2515 "d" suffix. */
2516
2517/* Convert host exception flags to vfp form. */
2518static inline int vfp_exceptbits_from_host(int host_bits)
2519{
2520 int target_bits = 0;
2521
2522 if (host_bits & float_flag_invalid)
2523 target_bits |= 1;
2524 if (host_bits & float_flag_divbyzero)
2525 target_bits |= 2;
2526 if (host_bits & float_flag_overflow)
2527 target_bits |= 4;
Peter Maydell36802b62011-05-19 14:46:18 +01002528 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
pbrook4373f3c2008-03-31 03:47:19 +00002529 target_bits |= 8;
2530 if (host_bits & float_flag_inexact)
2531 target_bits |= 0x10;
Peter Maydellcecd8502011-01-06 19:37:55 +00002532 if (host_bits & float_flag_input_denormal)
2533 target_bits |= 0x80;
pbrook4373f3c2008-03-31 03:47:19 +00002534 return target_bits;
2535}
2536
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002537uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002538{
2539 int i;
2540 uint32_t fpscr;
2541
2542 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2543 | (env->vfp.vec_len << 16)
2544 | (env->vfp.vec_stride << 20);
2545 i = get_float_exception_flags(&env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002546 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002547 fpscr |= vfp_exceptbits_from_host(i);
2548 return fpscr;
2549}
2550
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002551uint32_t vfp_get_fpscr(CPUARMState *env)
Peter Maydell01653292010-11-24 15:20:04 +00002552{
2553 return HELPER(vfp_get_fpscr)(env);
2554}
2555
pbrook4373f3c2008-03-31 03:47:19 +00002556/* Convert vfp exception flags to target form. */
2557static inline int vfp_exceptbits_to_host(int target_bits)
2558{
2559 int host_bits = 0;
2560
2561 if (target_bits & 1)
2562 host_bits |= float_flag_invalid;
2563 if (target_bits & 2)
2564 host_bits |= float_flag_divbyzero;
2565 if (target_bits & 4)
2566 host_bits |= float_flag_overflow;
2567 if (target_bits & 8)
2568 host_bits |= float_flag_underflow;
2569 if (target_bits & 0x10)
2570 host_bits |= float_flag_inexact;
Peter Maydellcecd8502011-01-06 19:37:55 +00002571 if (target_bits & 0x80)
2572 host_bits |= float_flag_input_denormal;
pbrook4373f3c2008-03-31 03:47:19 +00002573 return host_bits;
2574}
2575
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002576void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
pbrook4373f3c2008-03-31 03:47:19 +00002577{
2578 int i;
2579 uint32_t changed;
2580
2581 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2582 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2583 env->vfp.vec_len = (val >> 16) & 7;
2584 env->vfp.vec_stride = (val >> 20) & 3;
2585
2586 changed ^= val;
2587 if (changed & (3 << 22)) {
2588 i = (val >> 22) & 3;
2589 switch (i) {
2590 case 0:
2591 i = float_round_nearest_even;
2592 break;
2593 case 1:
2594 i = float_round_up;
2595 break;
2596 case 2:
2597 i = float_round_down;
2598 break;
2599 case 3:
2600 i = float_round_to_zero;
2601 break;
2602 }
2603 set_float_rounding_mode(i, &env->vfp.fp_status);
2604 }
Peter Maydellcecd8502011-01-06 19:37:55 +00002605 if (changed & (1 << 24)) {
pbrookfe76d972008-12-19 14:33:59 +00002606 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
Peter Maydellcecd8502011-01-06 19:37:55 +00002607 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2608 }
pbrook5c7908e2008-12-19 13:53:37 +00002609 if (changed & (1 << 25))
2610 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002611
Peter Maydellb12c3902011-01-06 19:37:54 +00002612 i = vfp_exceptbits_to_host(val);
pbrook4373f3c2008-03-31 03:47:19 +00002613 set_float_exception_flags(i, &env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002614 set_float_exception_flags(0, &env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002615}
2616
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002617void vfp_set_fpscr(CPUARMState *env, uint32_t val)
Peter Maydell01653292010-11-24 15:20:04 +00002618{
2619 HELPER(vfp_set_fpscr)(env, val);
2620}
2621
pbrook4373f3c2008-03-31 03:47:19 +00002622#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2623
2624#define VFP_BINOP(name) \
Peter Maydellae1857e2011-05-25 14:51:48 +00002625float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002626{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002627 float_status *fpst = fpstp; \
2628 return float32_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002629} \
Peter Maydellae1857e2011-05-25 14:51:48 +00002630float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002631{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002632 float_status *fpst = fpstp; \
2633 return float64_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002634}
2635VFP_BINOP(add)
2636VFP_BINOP(sub)
2637VFP_BINOP(mul)
2638VFP_BINOP(div)
2639#undef VFP_BINOP
2640
2641float32 VFP_HELPER(neg, s)(float32 a)
2642{
2643 return float32_chs(a);
2644}
2645
2646float64 VFP_HELPER(neg, d)(float64 a)
2647{
balrog66230e02008-04-20 00:58:01 +00002648 return float64_chs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002649}
2650
2651float32 VFP_HELPER(abs, s)(float32 a)
2652{
2653 return float32_abs(a);
2654}
2655
2656float64 VFP_HELPER(abs, d)(float64 a)
2657{
balrog66230e02008-04-20 00:58:01 +00002658 return float64_abs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002659}
2660
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002661float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002662{
2663 return float32_sqrt(a, &env->vfp.fp_status);
2664}
2665
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002666float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002667{
2668 return float64_sqrt(a, &env->vfp.fp_status);
2669}
2670
2671/* XXX: check quiet/signaling case */
2672#define DO_VFP_cmp(p, type) \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002673void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00002674{ \
2675 uint32_t flags; \
2676 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2677 case 0: flags = 0x6; break; \
2678 case -1: flags = 0x8; break; \
2679 case 1: flags = 0x2; break; \
2680 default: case 2: flags = 0x3; break; \
2681 } \
2682 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2683 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2684} \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002685void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00002686{ \
2687 uint32_t flags; \
2688 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2689 case 0: flags = 0x6; break; \
2690 case -1: flags = 0x8; break; \
2691 case 1: flags = 0x2; break; \
2692 default: case 2: flags = 0x3; break; \
2693 } \
2694 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2695 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2696}
2697DO_VFP_cmp(s, float32)
2698DO_VFP_cmp(d, float64)
2699#undef DO_VFP_cmp
2700
Peter Maydell5500b062011-05-19 14:46:19 +01002701/* Integer to float and float to integer conversions */
2702
2703#define CONV_ITOF(name, fsz, sign) \
2704 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2705{ \
2706 float_status *fpst = fpstp; \
Peter Maydell85836972012-01-25 11:49:46 +00002707 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002708}
2709
Peter Maydell5500b062011-05-19 14:46:19 +01002710#define CONV_FTOI(name, fsz, sign, round) \
2711uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2712{ \
2713 float_status *fpst = fpstp; \
2714 if (float##fsz##_is_any_nan(x)) { \
2715 float_raise(float_flag_invalid, fpst); \
2716 return 0; \
2717 } \
2718 return float##fsz##_to_##sign##int32##round(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002719}
2720
Peter Maydell5500b062011-05-19 14:46:19 +01002721#define FLOAT_CONVS(name, p, fsz, sign) \
2722CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2723CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2724CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
pbrook4373f3c2008-03-31 03:47:19 +00002725
Peter Maydell5500b062011-05-19 14:46:19 +01002726FLOAT_CONVS(si, s, 32, )
2727FLOAT_CONVS(si, d, 64, )
2728FLOAT_CONVS(ui, s, 32, u)
2729FLOAT_CONVS(ui, d, 64, u)
pbrook4373f3c2008-03-31 03:47:19 +00002730
Peter Maydell5500b062011-05-19 14:46:19 +01002731#undef CONV_ITOF
2732#undef CONV_FTOI
2733#undef FLOAT_CONVS
pbrook4373f3c2008-03-31 03:47:19 +00002734
2735/* floating point conversion */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002736float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002737{
Peter Maydell2d627732010-12-07 15:37:34 +00002738 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2739 /* ARM requires that S<->D conversion of any kind of NaN generates
2740 * a quiet NaN by forcing the most significant frac bit to 1.
2741 */
2742 return float64_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002743}
2744
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002745float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002746{
Peter Maydell2d627732010-12-07 15:37:34 +00002747 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2748 /* ARM requires that S<->D conversion of any kind of NaN generates
2749 * a quiet NaN by forcing the most significant frac bit to 1.
2750 */
2751 return float32_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002752}
2753
2754/* VFP3 fixed point conversion. */
Peter Maydell622465e2011-03-14 07:23:11 +00002755#define VFP_CONV_FIX(name, p, fsz, itype, sign) \
Peter Maydell5500b062011-05-19 14:46:19 +01002756float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2757 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002758{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002759 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002760 float##fsz tmp; \
Peter Maydell5500b062011-05-19 14:46:19 +01002761 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2762 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002763} \
Peter Maydell5500b062011-05-19 14:46:19 +01002764uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2765 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002766{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002767 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002768 float##fsz tmp; \
2769 if (float##fsz##_is_any_nan(x)) { \
Peter Maydell5500b062011-05-19 14:46:19 +01002770 float_raise(float_flag_invalid, fpst); \
Peter Maydell622465e2011-03-14 07:23:11 +00002771 return 0; \
Peter Maydell09d94872010-12-07 15:37:34 +00002772 } \
Peter Maydell5500b062011-05-19 14:46:19 +01002773 tmp = float##fsz##_scalbn(x, shift, fpst); \
2774 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002775}
2776
Peter Maydell622465e2011-03-14 07:23:11 +00002777VFP_CONV_FIX(sh, d, 64, int16, )
2778VFP_CONV_FIX(sl, d, 64, int32, )
2779VFP_CONV_FIX(uh, d, 64, uint16, u)
2780VFP_CONV_FIX(ul, d, 64, uint32, u)
2781VFP_CONV_FIX(sh, s, 32, int16, )
2782VFP_CONV_FIX(sl, s, 32, int32, )
2783VFP_CONV_FIX(uh, s, 32, uint16, u)
2784VFP_CONV_FIX(ul, s, 32, uint32, u)
pbrook4373f3c2008-03-31 03:47:19 +00002785#undef VFP_CONV_FIX
2786
Paul Brook60011492009-11-19 16:45:20 +00002787/* Half precision conversions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002788static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002789{
Paul Brook60011492009-11-19 16:45:20 +00002790 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002791 float32 r = float16_to_float32(make_float16(a), ieee, s);
2792 if (ieee) {
2793 return float32_maybe_silence_nan(r);
2794 }
2795 return r;
Paul Brook60011492009-11-19 16:45:20 +00002796}
2797
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002798static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002799{
Paul Brook60011492009-11-19 16:45:20 +00002800 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002801 float16 r = float32_to_float16(a, ieee, s);
2802 if (ieee) {
2803 r = float16_maybe_silence_nan(r);
2804 }
2805 return float16_val(r);
Paul Brook60011492009-11-19 16:45:20 +00002806}
2807
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002808float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002809{
2810 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2811}
2812
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002813uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002814{
2815 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2816}
2817
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002818float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002819{
2820 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2821}
2822
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002823uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002824{
2825 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2826}
2827
Peter Maydelldda3ec42011-03-14 15:37:12 +00002828#define float32_two make_float32(0x40000000)
Peter Maydell6aae3df2011-03-14 15:37:13 +00002829#define float32_three make_float32(0x40400000)
2830#define float32_one_point_five make_float32(0x3fc00000)
Peter Maydelldda3ec42011-03-14 15:37:12 +00002831
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002832float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002833{
Peter Maydelldda3ec42011-03-14 15:37:12 +00002834 float_status *s = &env->vfp.standard_fp_status;
2835 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2836 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002837 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2838 float_raise(float_flag_input_denormal, s);
2839 }
Peter Maydelldda3ec42011-03-14 15:37:12 +00002840 return float32_two;
2841 }
2842 return float32_sub(float32_two, float32_mul(a, b, s), s);
pbrook4373f3c2008-03-31 03:47:19 +00002843}
2844
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002845float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002846{
Peter Maydell71826962011-01-14 20:39:18 +01002847 float_status *s = &env->vfp.standard_fp_status;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002848 float32 product;
2849 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2850 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002851 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2852 float_raise(float_flag_input_denormal, s);
2853 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002854 return float32_one_point_five;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002855 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002856 product = float32_mul(a, b, s);
2857 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
pbrook4373f3c2008-03-31 03:47:19 +00002858}
2859
pbrook8f8e3aa2008-03-31 03:48:01 +00002860/* NEON helpers. */
2861
Christophe Lyon56bf4fe2011-02-21 17:38:46 +01002862/* Constants 256 and 512 are used in some helpers; we avoid relying on
2863 * int->float conversions at run-time. */
2864#define float64_256 make_float64(0x4070000000000000LL)
2865#define float64_512 make_float64(0x4080000000000000LL)
2866
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002867/* The algorithm that must be used to calculate the estimate
2868 * is specified by the ARM ARM.
2869 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002870static float64 recip_estimate(float64 a, CPUARMState *env)
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002871{
Peter Maydell1146a812011-05-19 14:46:14 +01002872 /* These calculations mustn't set any fp exception flags,
2873 * so we use a local copy of the fp_status.
2874 */
2875 float_status dummy_status = env->vfp.standard_fp_status;
2876 float_status *s = &dummy_status;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002877 /* q = (int)(a * 512.0) */
2878 float64 q = float64_mul(float64_512, a, s);
2879 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2880
2881 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2882 q = int64_to_float64(q_int, s);
2883 q = float64_add(q, float64_half, s);
2884 q = float64_div(q, float64_512, s);
2885 q = float64_div(float64_one, q, s);
2886
2887 /* s = (int)(256.0 * r + 0.5) */
2888 q = float64_mul(q, float64_256, s);
2889 q = float64_add(q, float64_half, s);
2890 q_int = float64_to_int64_round_to_zero(q, s);
2891
2892 /* return (double)s / 256.0 */
2893 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2894}
2895
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002896float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002897{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002898 float_status *s = &env->vfp.standard_fp_status;
2899 float64 f64;
2900 uint32_t val32 = float32_val(a);
2901
2902 int result_exp;
2903 int a_exp = (val32 & 0x7f800000) >> 23;
2904 int sign = val32 & 0x80000000;
2905
2906 if (float32_is_any_nan(a)) {
2907 if (float32_is_signaling_nan(a)) {
2908 float_raise(float_flag_invalid, s);
2909 }
2910 return float32_default_nan;
2911 } else if (float32_is_infinity(a)) {
2912 return float32_set_sign(float32_zero, float32_is_neg(a));
2913 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002914 if (!float32_is_zero(a)) {
2915 float_raise(float_flag_input_denormal, s);
2916 }
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002917 float_raise(float_flag_divbyzero, s);
2918 return float32_set_sign(float32_infinity, float32_is_neg(a));
2919 } else if (a_exp >= 253) {
2920 float_raise(float_flag_underflow, s);
2921 return float32_set_sign(float32_zero, float32_is_neg(a));
2922 }
2923
2924 f64 = make_float64((0x3feULL << 52)
2925 | ((int64_t)(val32 & 0x7fffff) << 29));
2926
2927 result_exp = 253 - a_exp;
2928
2929 f64 = recip_estimate(f64, env);
2930
2931 val32 = sign
2932 | ((result_exp & 0xff) << 23)
2933 | ((float64_val(f64) >> 29) & 0x7fffff);
2934 return make_float32(val32);
pbrook4373f3c2008-03-31 03:47:19 +00002935}
2936
Christophe Lyone07be5d2011-02-21 17:38:48 +01002937/* The algorithm that must be used to calculate the estimate
2938 * is specified by the ARM ARM.
2939 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002940static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
Christophe Lyone07be5d2011-02-21 17:38:48 +01002941{
Peter Maydell1146a812011-05-19 14:46:14 +01002942 /* These calculations mustn't set any fp exception flags,
2943 * so we use a local copy of the fp_status.
2944 */
2945 float_status dummy_status = env->vfp.standard_fp_status;
2946 float_status *s = &dummy_status;
Christophe Lyone07be5d2011-02-21 17:38:48 +01002947 float64 q;
2948 int64_t q_int;
2949
2950 if (float64_lt(a, float64_half, s)) {
2951 /* range 0.25 <= a < 0.5 */
2952
2953 /* a in units of 1/512 rounded down */
2954 /* q0 = (int)(a * 512.0); */
2955 q = float64_mul(float64_512, a, s);
2956 q_int = float64_to_int64_round_to_zero(q, s);
2957
2958 /* reciprocal root r */
2959 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2960 q = int64_to_float64(q_int, s);
2961 q = float64_add(q, float64_half, s);
2962 q = float64_div(q, float64_512, s);
2963 q = float64_sqrt(q, s);
2964 q = float64_div(float64_one, q, s);
2965 } else {
2966 /* range 0.5 <= a < 1.0 */
2967
2968 /* a in units of 1/256 rounded down */
2969 /* q1 = (int)(a * 256.0); */
2970 q = float64_mul(float64_256, a, s);
2971 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2972
2973 /* reciprocal root r */
2974 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2975 q = int64_to_float64(q_int, s);
2976 q = float64_add(q, float64_half, s);
2977 q = float64_div(q, float64_256, s);
2978 q = float64_sqrt(q, s);
2979 q = float64_div(float64_one, q, s);
2980 }
2981 /* r in units of 1/256 rounded to nearest */
2982 /* s = (int)(256.0 * r + 0.5); */
2983
2984 q = float64_mul(q, float64_256,s );
2985 q = float64_add(q, float64_half, s);
2986 q_int = float64_to_int64_round_to_zero(q, s);
2987
2988 /* return (double)s / 256.0;*/
2989 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2990}
2991
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002992float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002993{
Christophe Lyone07be5d2011-02-21 17:38:48 +01002994 float_status *s = &env->vfp.standard_fp_status;
2995 int result_exp;
2996 float64 f64;
2997 uint32_t val;
2998 uint64_t val64;
2999
3000 val = float32_val(a);
3001
3002 if (float32_is_any_nan(a)) {
3003 if (float32_is_signaling_nan(a)) {
3004 float_raise(float_flag_invalid, s);
3005 }
3006 return float32_default_nan;
3007 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01003008 if (!float32_is_zero(a)) {
3009 float_raise(float_flag_input_denormal, s);
3010 }
Christophe Lyone07be5d2011-02-21 17:38:48 +01003011 float_raise(float_flag_divbyzero, s);
3012 return float32_set_sign(float32_infinity, float32_is_neg(a));
3013 } else if (float32_is_neg(a)) {
3014 float_raise(float_flag_invalid, s);
3015 return float32_default_nan;
3016 } else if (float32_is_infinity(a)) {
3017 return float32_zero;
3018 }
3019
3020 /* Normalize to a double-precision value between 0.25 and 1.0,
3021 * preserving the parity of the exponent. */
3022 if ((val & 0x800000) == 0) {
3023 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3024 | (0x3feULL << 52)
3025 | ((uint64_t)(val & 0x7fffff) << 29));
3026 } else {
3027 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3028 | (0x3fdULL << 52)
3029 | ((uint64_t)(val & 0x7fffff) << 29));
3030 }
3031
3032 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3033
3034 f64 = recip_sqrt_estimate(f64, env);
3035
3036 val64 = float64_val(f64);
3037
Christophe LYON26cc6ab2011-10-19 16:14:05 +00003038 val = ((result_exp & 0xff) << 23)
Christophe Lyone07be5d2011-02-21 17:38:48 +01003039 | ((val64 >> 29) & 0x7fffff);
3040 return make_float32(val);
pbrook4373f3c2008-03-31 03:47:19 +00003041}
3042
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003043uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003044{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003045 float64 f64;
3046
3047 if ((a & 0x80000000) == 0) {
3048 return 0xffffffff;
3049 }
3050
3051 f64 = make_float64((0x3feULL << 52)
3052 | ((int64_t)(a & 0x7fffffff) << 21));
3053
3054 f64 = recip_estimate (f64, env);
3055
3056 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00003057}
3058
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003059uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003060{
Christophe Lyone07be5d2011-02-21 17:38:48 +01003061 float64 f64;
3062
3063 if ((a & 0xc0000000) == 0) {
3064 return 0xffffffff;
3065 }
3066
3067 if (a & 0x80000000) {
3068 f64 = make_float64((0x3feULL << 52)
3069 | ((uint64_t)(a & 0x7fffffff) << 21));
3070 } else { /* bits 31-30 == '01' */
3071 f64 = make_float64((0x3fdULL << 52)
3072 | ((uint64_t)(a & 0x3fffffff) << 22));
3073 }
3074
3075 f64 = recip_sqrt_estimate(f64, env);
3076
3077 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00003078}
pbrookfe1479c2008-12-19 13:18:36 +00003079
Peter Maydellda97f522011-10-19 16:14:07 +00003080/* VFPv4 fused multiply-accumulate */
3081float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3082{
3083 float_status *fpst = fpstp;
3084 return float32_muladd(a, b, c, 0, fpst);
3085}
3086
3087float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3088{
3089 float_status *fpst = fpstp;
3090 return float64_muladd(a, b, c, 0, fpst);
3091}