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