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