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