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