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