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