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