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