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