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