blob: c003fe0fd1164250e5c50ecc48cf20cc51541d4a [file] [log] [blame]
bellardb5ff1b32005-11-26 10:38:39 +00001#include "cpu.h"
pbrook9ee6e8b2007-11-11 00:04:49 +00002#include "gdbstub.h"
Lluís7b592202011-04-13 18:38:24 +02003#include "helper.h"
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +02004#include "host-utils.h"
Peter Maydell0b03bdf2012-01-25 12:42:29 +00005#include "sysemu.h"
6
Andreas Färber0ecb72a2012-03-14 01:38:21 +01007static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +00008{
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
Andreas Färber0ecb72a2012-03-14 01:38:21 +010034static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +000035{
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;
Juha Riihimäki71b3c3d2009-10-26 11:46:42 +020054 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
pbrook56aebc82008-10-11 17:55:29 +000055 }
56 return 0;
57}
58
Peter Maydelle9aa6c22012-06-20 11:57:09 +000059static 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
69static const ARMCPRegInfo v7_cp_reginfo[] = {
70 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
71 * debug components
72 */
73 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
74 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
75 { .name = "DBGDRAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
76 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
77 REGINFO_SENTINEL
78};
79
Peter Maydellc326b972012-06-20 11:57:10 +000080static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
81{
82 value &= 1;
83 env->teecr = value;
84 return 0;
85}
86
87static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri,
88 uint64_t *value)
89{
90 /* This is a helper function because the user access rights
91 * depend on the value of the TEECR.
92 */
93 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
94 return EXCP_UDEF;
95 }
96 *value = env->teehbr;
97 return 0;
98}
99
100static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
101 uint64_t value)
102{
103 if (arm_current_pl(env) == 0 && (env->teecr & 1)) {
104 return EXCP_UDEF;
105 }
106 env->teehbr = value;
107 return 0;
108}
109
110static const ARMCPRegInfo t2ee_cp_reginfo[] = {
111 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
112 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
113 .resetvalue = 0,
114 .writefn = teecr_write },
115 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
116 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
117 .resetvalue = 0,
118 .readfn = teehbr_read, .writefn = teehbr_write },
119 REGINFO_SENTINEL
120};
121
Peter Maydell2ceb98c2012-06-20 11:57:09 +0000122void register_cp_regs_for_features(ARMCPU *cpu)
123{
124 /* Register all the coprocessor registers based on feature bits */
125 CPUARMState *env = &cpu->env;
126 if (arm_feature(env, ARM_FEATURE_M)) {
127 /* M profile has no coprocessor registers */
128 return;
129 }
130
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000131 define_arm_cp_regs(cpu, cp_reginfo);
132 if (arm_feature(env, ARM_FEATURE_V7)) {
133 define_arm_cp_regs(cpu, v7_cp_reginfo);
134 }
Peter Maydellc326b972012-06-20 11:57:10 +0000135 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
136 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
137 }
Peter Maydell2ceb98c2012-06-20 11:57:09 +0000138}
139
Andreas Färber778c3a02012-04-20 07:39:14 +0000140ARMCPU *cpu_arm_init(const char *cpu_model)
pbrook40f137e2006-02-20 00:33:36 +0000141{
Andreas Färberdec9c2d2012-03-29 04:50:31 +0000142 ARMCPU *cpu;
pbrook40f137e2006-02-20 00:33:36 +0000143 CPUARMState *env;
pbrookb26eefb2008-03-31 03:44:26 +0000144 static int inited = 0;
pbrook40f137e2006-02-20 00:33:36 +0000145
Peter Maydell777dc782012-04-20 17:58:31 +0000146 if (!object_class_by_name(cpu_model)) {
bellardaaed9092007-11-10 15:15:54 +0000147 return NULL;
Peter Maydell777dc782012-04-20 17:58:31 +0000148 }
149 cpu = ARM_CPU(object_new(cpu_model));
Andreas Färberdec9c2d2012-03-29 04:50:31 +0000150 env = &cpu->env;
Peter Maydell777dc782012-04-20 17:58:31 +0000151 env->cpu_model_str = cpu_model;
Peter Maydell581be092012-04-20 17:58:31 +0000152 arm_cpu_realize(cpu);
Peter Maydell777dc782012-04-20 17:58:31 +0000153
Peter Maydellf4fc2472011-11-25 19:25:50 +0100154 if (tcg_enabled() && !inited) {
pbrookb26eefb2008-03-31 03:44:26 +0000155 inited = 1;
156 arm_translate_init();
157 }
158
Andreas Färberdf90dad2012-05-04 19:14:38 +0200159 cpu_reset(CPU(cpu));
pbrook56aebc82008-10-11 17:55:29 +0000160 if (arm_feature(env, ARM_FEATURE_NEON)) {
161 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
162 51, "arm-neon.xml", 0);
163 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
164 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
165 35, "arm-vfp3.xml", 0);
166 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
167 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
168 19, "arm-vfp.xml", 0);
169 }
aliguori0bf46a42009-04-24 18:03:41 +0000170 qemu_init_vcpu(env);
Andreas Färber778c3a02012-04-20 07:39:14 +0000171 return cpu;
pbrook40f137e2006-02-20 00:33:36 +0000172}
173
Peter Maydell777dc782012-04-20 17:58:31 +0000174typedef struct ARMCPUListState {
175 fprintf_function cpu_fprintf;
176 FILE *file;
177} ARMCPUListState;
pbrook3371d272007-03-08 03:04:12 +0000178
Peter Maydell777dc782012-04-20 17:58:31 +0000179/* Sort alphabetically by type name, except for "any". */
180static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
pbrook5adb4832007-03-08 03:15:18 +0000181{
Peter Maydell777dc782012-04-20 17:58:31 +0000182 ObjectClass *class_a = (ObjectClass *)a;
183 ObjectClass *class_b = (ObjectClass *)b;
184 const char *name_a, *name_b;
pbrook5adb4832007-03-08 03:15:18 +0000185
Peter Maydell777dc782012-04-20 17:58:31 +0000186 name_a = object_class_get_name(class_a);
187 name_b = object_class_get_name(class_b);
188 if (strcmp(name_a, "any") == 0) {
189 return 1;
190 } else if (strcmp(name_b, "any") == 0) {
191 return -1;
192 } else {
193 return strcmp(name_a, name_b);
pbrook5adb4832007-03-08 03:15:18 +0000194 }
195}
196
Peter Maydell777dc782012-04-20 17:58:31 +0000197static void arm_cpu_list_entry(gpointer data, gpointer user_data)
pbrook40f137e2006-02-20 00:33:36 +0000198{
Peter Maydell777dc782012-04-20 17:58:31 +0000199 ObjectClass *oc = data;
200 ARMCPUListState *s = user_data;
pbrook3371d272007-03-08 03:04:12 +0000201
Peter Maydell777dc782012-04-20 17:58:31 +0000202 (*s->cpu_fprintf)(s->file, " %s\n",
203 object_class_get_name(oc));
204}
205
206void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
207{
208 ARMCPUListState s = {
209 .file = f,
210 .cpu_fprintf = cpu_fprintf,
211 };
212 GSList *list;
213
214 list = object_class_get_list(TYPE_ARM_CPU, false);
215 list = g_slist_sort(list, arm_cpu_list_compare);
216 (*cpu_fprintf)(f, "Available CPUs:\n");
217 g_slist_foreach(list, arm_cpu_list_entry, &s);
218 g_slist_free(list);
pbrook40f137e2006-02-20 00:33:36 +0000219}
220
Peter Maydell4b6a83f2012-06-20 11:57:06 +0000221void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
222 const ARMCPRegInfo *r, void *opaque)
223{
224 /* Define implementations of coprocessor registers.
225 * We store these in a hashtable because typically
226 * there are less than 150 registers in a space which
227 * is 16*16*16*8*8 = 262144 in size.
228 * Wildcarding is supported for the crm, opc1 and opc2 fields.
229 * If a register is defined twice then the second definition is
230 * used, so this can be used to define some generic registers and
231 * then override them with implementation specific variations.
232 * At least one of the original and the second definition should
233 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
234 * against accidental use.
235 */
236 int crm, opc1, opc2;
237 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
238 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
239 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
240 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
241 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
242 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
243 /* 64 bit registers have only CRm and Opc1 fields */
244 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
245 /* Check that the register definition has enough info to handle
246 * reads and writes if they are permitted.
247 */
248 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
249 if (r->access & PL3_R) {
250 assert(r->fieldoffset || r->readfn);
251 }
252 if (r->access & PL3_W) {
253 assert(r->fieldoffset || r->writefn);
254 }
255 }
256 /* Bad type field probably means missing sentinel at end of reg list */
257 assert(cptype_valid(r->type));
258 for (crm = crmmin; crm <= crmmax; crm++) {
259 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
260 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
261 uint32_t *key = g_new(uint32_t, 1);
262 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
263 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
264 *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2);
265 r2->opaque = opaque;
266 /* Make sure reginfo passed to helpers for wildcarded regs
267 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
268 */
269 r2->crm = crm;
270 r2->opc1 = opc1;
271 r2->opc2 = opc2;
272 /* Overriding of an existing definition must be explicitly
273 * requested.
274 */
275 if (!(r->type & ARM_CP_OVERRIDE)) {
276 ARMCPRegInfo *oldreg;
277 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
278 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
279 fprintf(stderr, "Register redefined: cp=%d %d bit "
280 "crn=%d crm=%d opc1=%d opc2=%d, "
281 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
282 r2->crn, r2->crm, r2->opc1, r2->opc2,
283 oldreg->name, r2->name);
284 assert(0);
285 }
286 }
287 g_hash_table_insert(cpu->cp_regs, key, r2);
288 }
289 }
290 }
291}
292
293void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
294 const ARMCPRegInfo *regs, void *opaque)
295{
296 /* Define a whole list of registers */
297 const ARMCPRegInfo *r;
298 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
299 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
300 }
301}
302
303const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp)
304{
305 return g_hash_table_lookup(cpu->cp_regs, &encoded_cp);
306}
307
308int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
309 uint64_t value)
310{
311 /* Helper coprocessor write function for write-ignore registers */
312 return 0;
313}
314
315int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value)
316{
317 /* Helper coprocessor write function for read-as-zero registers */
318 *value = 0;
319 return 0;
320}
321
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100322static int bad_mode_switch(CPUARMState *env, int mode)
Peter Maydell37064a82012-01-05 15:49:06 +0000323{
324 /* Return true if it is not valid for us to switch to
325 * this CPU mode (ie all the UNPREDICTABLE cases in
326 * the ARM ARM CPSRWriteByInstr pseudocode).
327 */
328 switch (mode) {
329 case ARM_CPU_MODE_USR:
330 case ARM_CPU_MODE_SYS:
331 case ARM_CPU_MODE_SVC:
332 case ARM_CPU_MODE_ABT:
333 case ARM_CPU_MODE_UND:
334 case ARM_CPU_MODE_IRQ:
335 case ARM_CPU_MODE_FIQ:
336 return 0;
337 default:
338 return 1;
339 }
340}
341
balrog2f4a40e2007-11-13 01:50:15 +0000342uint32_t cpsr_read(CPUARMState *env)
343{
344 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +0000345 ZF = (env->ZF == 0);
346 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
balrog2f4a40e2007-11-13 01:50:15 +0000347 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
348 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
349 | ((env->condexec_bits & 0xfc) << 8)
350 | (env->GE << 16);
351}
352
353void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
354{
balrog2f4a40e2007-11-13 01:50:15 +0000355 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +0000356 env->ZF = (~val) & CPSR_Z;
357 env->NF = val;
balrog2f4a40e2007-11-13 01:50:15 +0000358 env->CF = (val >> 29) & 1;
359 env->VF = (val << 3) & 0x80000000;
360 }
361 if (mask & CPSR_Q)
362 env->QF = ((val & CPSR_Q) != 0);
363 if (mask & CPSR_T)
364 env->thumb = ((val & CPSR_T) != 0);
365 if (mask & CPSR_IT_0_1) {
366 env->condexec_bits &= ~3;
367 env->condexec_bits |= (val >> 25) & 3;
368 }
369 if (mask & CPSR_IT_2_7) {
370 env->condexec_bits &= 3;
371 env->condexec_bits |= (val >> 8) & 0xfc;
372 }
373 if (mask & CPSR_GE) {
374 env->GE = (val >> 16) & 0xf;
375 }
376
377 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
Peter Maydell37064a82012-01-05 15:49:06 +0000378 if (bad_mode_switch(env, val & CPSR_M)) {
379 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
380 * We choose to ignore the attempt and leave the CPSR M field
381 * untouched.
382 */
383 mask &= ~CPSR_M;
384 } else {
385 switch_mode(env, val & CPSR_M);
386 }
balrog2f4a40e2007-11-13 01:50:15 +0000387 }
388 mask &= ~CACHED_CPSR_BITS;
389 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
390}
391
pbrookb26eefb2008-03-31 03:44:26 +0000392/* Sign/zero extend */
393uint32_t HELPER(sxtb16)(uint32_t x)
394{
395 uint32_t res;
396 res = (uint16_t)(int8_t)x;
397 res |= (uint32_t)(int8_t)(x >> 16) << 16;
398 return res;
399}
400
401uint32_t HELPER(uxtb16)(uint32_t x)
402{
403 uint32_t res;
404 res = (uint16_t)(uint8_t)x;
405 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
406 return res;
407}
408
pbrookf51bbbf2008-03-31 03:45:13 +0000409uint32_t HELPER(clz)(uint32_t x)
410{
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +0200411 return clz32(x);
pbrookf51bbbf2008-03-31 03:45:13 +0000412}
413
pbrook36706692008-03-31 03:46:19 +0000414int32_t HELPER(sdiv)(int32_t num, int32_t den)
415{
416 if (den == 0)
417 return 0;
Aurelien Jarno686eeb92009-10-15 23:08:46 +0200418 if (num == INT_MIN && den == -1)
419 return INT_MIN;
pbrook36706692008-03-31 03:46:19 +0000420 return num / den;
421}
422
423uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
424{
425 if (den == 0)
426 return 0;
427 return num / den;
428}
429
430uint32_t HELPER(rbit)(uint32_t x)
431{
432 x = ((x & 0xff000000) >> 24)
433 | ((x & 0x00ff0000) >> 8)
434 | ((x & 0x0000ff00) << 8)
435 | ((x & 0x000000ff) << 24);
436 x = ((x & 0xf0f0f0f0) >> 4)
437 | ((x & 0x0f0f0f0f) << 4);
438 x = ((x & 0x88888888) >> 3)
439 | ((x & 0x44444444) >> 1)
440 | ((x & 0x22222222) << 1)
441 | ((x & 0x11111111) << 3);
442 return x;
443}
444
pbrookad694712008-03-31 03:48:30 +0000445uint32_t HELPER(abs)(uint32_t x)
446{
447 return ((int32_t)x < 0) ? -x : x;
448}
449
ths5fafdf22007-09-16 21:08:06 +0000450#if defined(CONFIG_USER_ONLY)
bellardb5ff1b32005-11-26 10:38:39 +0000451
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100452void do_interrupt (CPUARMState *env)
bellardb5ff1b32005-11-26 10:38:39 +0000453{
454 env->exception_index = -1;
455}
456
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100457int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
Blue Swirl97b348e2011-08-01 16:12:17 +0000458 int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +0000459{
460 if (rw == 2) {
461 env->exception_index = EXCP_PREFETCH_ABORT;
462 env->cp15.c6_insn = address;
463 } else {
464 env->exception_index = EXCP_DATA_ABORT;
465 env->cp15.c6_data = address;
466 }
467 return 1;
468}
469
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100470void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +0000471{
472 cpu_abort(env, "cp15 insn %08x\n", insn);
473}
474
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100475uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +0000476{
477 cpu_abort(env, "cp15 insn %08x\n", insn);
bellardb5ff1b32005-11-26 10:38:39 +0000478}
479
pbrook9ee6e8b2007-11-11 00:04:49 +0000480/* These should probably raise undefined insn exceptions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100481void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000482{
483 cpu_abort(env, "v7m_mrs %d\n", reg);
484}
485
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100486uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +0000487{
488 cpu_abort(env, "v7m_mrs %d\n", reg);
489 return 0;
490}
491
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100492void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +0000493{
494 if (mode != ARM_CPU_MODE_USR)
495 cpu_abort(env, "Tried to switch out of user mode\n");
496}
497
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100498void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000499{
500 cpu_abort(env, "banked r13 write\n");
501}
502
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100503uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +0000504{
505 cpu_abort(env, "banked r13 read\n");
506 return 0;
507}
508
bellardb5ff1b32005-11-26 10:38:39 +0000509#else
510
511/* Map CPU modes onto saved register banks. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100512static inline int bank_number(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +0000513{
514 switch (mode) {
515 case ARM_CPU_MODE_USR:
516 case ARM_CPU_MODE_SYS:
517 return 0;
518 case ARM_CPU_MODE_SVC:
519 return 1;
520 case ARM_CPU_MODE_ABT:
521 return 2;
522 case ARM_CPU_MODE_UND:
523 return 3;
524 case ARM_CPU_MODE_IRQ:
525 return 4;
526 case ARM_CPU_MODE_FIQ:
527 return 5;
528 }
Peter Maydell1b9e01c2012-01-05 15:49:06 +0000529 cpu_abort(env, "Bad mode %x\n", mode);
bellardb5ff1b32005-11-26 10:38:39 +0000530 return -1;
531}
532
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100533void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +0000534{
535 int old_mode;
536 int i;
537
538 old_mode = env->uncached_cpsr & CPSR_M;
539 if (mode == old_mode)
540 return;
541
542 if (old_mode == ARM_CPU_MODE_FIQ) {
543 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000544 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000545 } else if (mode == ARM_CPU_MODE_FIQ) {
546 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000547 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000548 }
549
Peter Maydell1b9e01c2012-01-05 15:49:06 +0000550 i = bank_number(env, old_mode);
bellardb5ff1b32005-11-26 10:38:39 +0000551 env->banked_r13[i] = env->regs[13];
552 env->banked_r14[i] = env->regs[14];
553 env->banked_spsr[i] = env->spsr;
554
Peter Maydell1b9e01c2012-01-05 15:49:06 +0000555 i = bank_number(env, mode);
bellardb5ff1b32005-11-26 10:38:39 +0000556 env->regs[13] = env->banked_r13[i];
557 env->regs[14] = env->banked_r14[i];
558 env->spsr = env->banked_spsr[i];
559}
560
pbrook9ee6e8b2007-11-11 00:04:49 +0000561static void v7m_push(CPUARMState *env, uint32_t val)
562{
563 env->regs[13] -= 4;
564 stl_phys(env->regs[13], val);
565}
566
567static uint32_t v7m_pop(CPUARMState *env)
568{
569 uint32_t val;
570 val = ldl_phys(env->regs[13]);
571 env->regs[13] += 4;
572 return val;
573}
574
575/* Switch to V7M main or process stack pointer. */
576static void switch_v7m_sp(CPUARMState *env, int process)
577{
578 uint32_t tmp;
579 if (env->v7m.current_sp != process) {
580 tmp = env->v7m.other_sp;
581 env->v7m.other_sp = env->regs[13];
582 env->regs[13] = tmp;
583 env->v7m.current_sp = process;
584 }
585}
586
587static void do_v7m_exception_exit(CPUARMState *env)
588{
589 uint32_t type;
590 uint32_t xpsr;
591
592 type = env->regs[15];
593 if (env->v7m.exception != 0)
Paul Brook983fe822010-04-05 19:34:51 +0100594 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
pbrook9ee6e8b2007-11-11 00:04:49 +0000595
596 /* Switch to the target stack. */
597 switch_v7m_sp(env, (type & 4) != 0);
598 /* Pop registers. */
599 env->regs[0] = v7m_pop(env);
600 env->regs[1] = v7m_pop(env);
601 env->regs[2] = v7m_pop(env);
602 env->regs[3] = v7m_pop(env);
603 env->regs[12] = v7m_pop(env);
604 env->regs[14] = v7m_pop(env);
605 env->regs[15] = v7m_pop(env);
606 xpsr = v7m_pop(env);
607 xpsr_write(env, xpsr, 0xfffffdff);
608 /* Undo stack alignment. */
609 if (xpsr & 0x200)
610 env->regs[13] |= 4;
611 /* ??? The exception return type specifies Thread/Handler mode. However
612 this is also implied by the xPSR value. Not sure what to do
613 if there is a mismatch. */
614 /* ??? Likewise for mismatches between the CONTROL register and the stack
615 pointer. */
616}
617
aurel322b3ea312009-03-07 21:48:00 +0000618static void do_interrupt_v7m(CPUARMState *env)
pbrook9ee6e8b2007-11-11 00:04:49 +0000619{
620 uint32_t xpsr = xpsr_read(env);
621 uint32_t lr;
622 uint32_t addr;
623
624 lr = 0xfffffff1;
625 if (env->v7m.current_sp)
626 lr |= 4;
627 if (env->v7m.exception == 0)
628 lr |= 8;
629
630 /* For exceptions we just mark as pending on the NVIC, and let that
631 handle it. */
632 /* TODO: Need to escalate if the current priority is higher than the
633 one we're raising. */
634 switch (env->exception_index) {
635 case EXCP_UDEF:
Paul Brook983fe822010-04-05 19:34:51 +0100636 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
pbrook9ee6e8b2007-11-11 00:04:49 +0000637 return;
638 case EXCP_SWI:
639 env->regs[15] += 2;
Paul Brook983fe822010-04-05 19:34:51 +0100640 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
pbrook9ee6e8b2007-11-11 00:04:49 +0000641 return;
642 case EXCP_PREFETCH_ABORT:
643 case EXCP_DATA_ABORT:
Paul Brook983fe822010-04-05 19:34:51 +0100644 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
pbrook9ee6e8b2007-11-11 00:04:49 +0000645 return;
646 case EXCP_BKPT:
pbrook2ad207d2007-11-24 23:22:11 +0000647 if (semihosting_enabled) {
648 int nr;
Paul Brookd8fd2952012-03-30 18:02:50 +0100649 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
pbrook2ad207d2007-11-24 23:22:11 +0000650 if (nr == 0xab) {
651 env->regs[15] += 2;
652 env->regs[0] = do_arm_semihosting(env);
653 return;
654 }
655 }
Paul Brook983fe822010-04-05 19:34:51 +0100656 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
pbrook9ee6e8b2007-11-11 00:04:49 +0000657 return;
658 case EXCP_IRQ:
Paul Brook983fe822010-04-05 19:34:51 +0100659 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
pbrook9ee6e8b2007-11-11 00:04:49 +0000660 break;
661 case EXCP_EXCEPTION_EXIT:
662 do_v7m_exception_exit(env);
663 return;
664 default:
665 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
666 return; /* Never happens. Keep compiler happy. */
667 }
668
669 /* Align stack pointer. */
670 /* ??? Should only do this if Configuration Control Register
671 STACKALIGN bit is set. */
672 if (env->regs[13] & 4) {
pbrookab19b0e2008-07-02 16:44:09 +0000673 env->regs[13] -= 4;
pbrook9ee6e8b2007-11-11 00:04:49 +0000674 xpsr |= 0x200;
675 }
balrog6c956762008-04-13 00:57:49 +0000676 /* Switch to the handler mode. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000677 v7m_push(env, xpsr);
678 v7m_push(env, env->regs[15]);
679 v7m_push(env, env->regs[14]);
680 v7m_push(env, env->regs[12]);
681 v7m_push(env, env->regs[3]);
682 v7m_push(env, env->regs[2]);
683 v7m_push(env, env->regs[1]);
684 v7m_push(env, env->regs[0]);
685 switch_v7m_sp(env, 0);
Peter Maydellc98d1742012-03-14 12:26:10 +0000686 /* Clear IT bits */
687 env->condexec_bits = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +0000688 env->regs[14] = lr;
689 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
690 env->regs[15] = addr & 0xfffffffe;
691 env->thumb = addr & 1;
692}
693
bellardb5ff1b32005-11-26 10:38:39 +0000694/* Handle a CPU exception. */
695void do_interrupt(CPUARMState *env)
696{
697 uint32_t addr;
698 uint32_t mask;
699 int new_mode;
700 uint32_t offset;
701
pbrook9ee6e8b2007-11-11 00:04:49 +0000702 if (IS_M(env)) {
703 do_interrupt_v7m(env);
704 return;
705 }
bellardb5ff1b32005-11-26 10:38:39 +0000706 /* TODO: Vectored interrupt controller. */
707 switch (env->exception_index) {
708 case EXCP_UDEF:
709 new_mode = ARM_CPU_MODE_UND;
710 addr = 0x04;
711 mask = CPSR_I;
712 if (env->thumb)
713 offset = 2;
714 else
715 offset = 4;
716 break;
717 case EXCP_SWI:
pbrook8e716212007-01-20 17:12:09 +0000718 if (semihosting_enabled) {
719 /* Check for semihosting interrupt. */
720 if (env->thumb) {
Paul Brookd8fd2952012-03-30 18:02:50 +0100721 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
pbrook8e716212007-01-20 17:12:09 +0000722 } else {
Paul Brookd8fd2952012-03-30 18:02:50 +0100723 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
724 & 0xffffff;
pbrook8e716212007-01-20 17:12:09 +0000725 }
726 /* Only intercept calls from privileged modes, to provide some
727 semblance of security. */
728 if (((mask == 0x123456 && !env->thumb)
729 || (mask == 0xab && env->thumb))
730 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
731 env->regs[0] = do_arm_semihosting(env);
732 return;
733 }
734 }
bellardb5ff1b32005-11-26 10:38:39 +0000735 new_mode = ARM_CPU_MODE_SVC;
736 addr = 0x08;
737 mask = CPSR_I;
balrog601d70b2008-04-20 01:03:45 +0000738 /* The PC already points to the next instruction. */
bellardb5ff1b32005-11-26 10:38:39 +0000739 offset = 0;
740 break;
pbrook06c949e2006-02-04 19:35:26 +0000741 case EXCP_BKPT:
pbrook9ee6e8b2007-11-11 00:04:49 +0000742 /* See if this is a semihosting syscall. */
pbrook2ad207d2007-11-24 23:22:11 +0000743 if (env->thumb && semihosting_enabled) {
Paul Brookd8fd2952012-03-30 18:02:50 +0100744 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
pbrook9ee6e8b2007-11-11 00:04:49 +0000745 if (mask == 0xab
746 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
747 env->regs[15] += 2;
748 env->regs[0] = do_arm_semihosting(env);
749 return;
750 }
751 }
Alex Zuepke81c05da2011-06-03 18:42:17 +0200752 env->cp15.c5_insn = 2;
pbrook9ee6e8b2007-11-11 00:04:49 +0000753 /* Fall through to prefetch abort. */
754 case EXCP_PREFETCH_ABORT:
bellardb5ff1b32005-11-26 10:38:39 +0000755 new_mode = ARM_CPU_MODE_ABT;
756 addr = 0x0c;
757 mask = CPSR_A | CPSR_I;
758 offset = 4;
759 break;
760 case EXCP_DATA_ABORT:
761 new_mode = ARM_CPU_MODE_ABT;
762 addr = 0x10;
763 mask = CPSR_A | CPSR_I;
764 offset = 8;
765 break;
766 case EXCP_IRQ:
767 new_mode = ARM_CPU_MODE_IRQ;
768 addr = 0x18;
769 /* Disable IRQ and imprecise data aborts. */
770 mask = CPSR_A | CPSR_I;
771 offset = 4;
772 break;
773 case EXCP_FIQ:
774 new_mode = ARM_CPU_MODE_FIQ;
775 addr = 0x1c;
776 /* Disable FIQ, IRQ and imprecise data aborts. */
777 mask = CPSR_A | CPSR_I | CPSR_F;
778 offset = 4;
779 break;
780 default:
781 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
782 return; /* Never happens. Keep compiler happy. */
783 }
784 /* High vectors. */
785 if (env->cp15.c1_sys & (1 << 13)) {
786 addr += 0xffff0000;
787 }
788 switch_mode (env, new_mode);
789 env->spsr = cpsr_read(env);
pbrook9ee6e8b2007-11-11 00:04:49 +0000790 /* Clear IT bits. */
791 env->condexec_bits = 0;
Rabin Vincent30a8cac2010-02-15 00:02:36 +0530792 /* Switch to the new mode, and to the correct instruction set. */
bellard6d7e6322005-12-18 16:54:08 +0000793 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
bellardb5ff1b32005-11-26 10:38:39 +0000794 env->uncached_cpsr |= mask;
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400795 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
796 * and we should just guard the thumb mode on V4 */
797 if (arm_feature(env, ARM_FEATURE_V4T)) {
798 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
799 }
bellardb5ff1b32005-11-26 10:38:39 +0000800 env->regs[14] = env->regs[15] + offset;
801 env->regs[15] = addr;
802 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
803}
804
805/* Check section/page access permissions.
806 Returns the page protection flags, or zero if the access is not
807 permitted. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100808static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000809 int access_type, int is_user)
bellardb5ff1b32005-11-26 10:38:39 +0000810{
pbrook9ee6e8b2007-11-11 00:04:49 +0000811 int prot_ro;
812
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000813 if (domain_prot == 3) {
bellardb5ff1b32005-11-26 10:38:39 +0000814 return PAGE_READ | PAGE_WRITE;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000815 }
bellardb5ff1b32005-11-26 10:38:39 +0000816
pbrook9ee6e8b2007-11-11 00:04:49 +0000817 if (access_type == 1)
818 prot_ro = 0;
819 else
820 prot_ro = PAGE_READ;
821
bellardb5ff1b32005-11-26 10:38:39 +0000822 switch (ap) {
823 case 0:
pbrook78600322006-09-09 14:36:26 +0000824 if (access_type == 1)
bellardb5ff1b32005-11-26 10:38:39 +0000825 return 0;
826 switch ((env->cp15.c1_sys >> 8) & 3) {
827 case 1:
828 return is_user ? 0 : PAGE_READ;
829 case 2:
830 return PAGE_READ;
831 default:
832 return 0;
833 }
834 case 1:
835 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
836 case 2:
837 if (is_user)
pbrook9ee6e8b2007-11-11 00:04:49 +0000838 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +0000839 else
840 return PAGE_READ | PAGE_WRITE;
841 case 3:
842 return PAGE_READ | PAGE_WRITE;
pbrookd4934d12008-12-19 12:39:00 +0000843 case 4: /* Reserved. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000844 return 0;
845 case 5:
846 return is_user ? 0 : prot_ro;
847 case 6:
848 return prot_ro;
pbrookd4934d12008-12-19 12:39:00 +0000849 case 7:
Jamie Iles0ab06d82011-06-23 01:12:59 +0000850 if (!arm_feature (env, ARM_FEATURE_V6K))
pbrookd4934d12008-12-19 12:39:00 +0000851 return 0;
852 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +0000853 default:
854 abort();
855 }
856}
857
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100858static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
pbrookb2fa1792008-10-22 19:22:30 +0000859{
860 uint32_t table;
861
862 if (address & env->cp15.c2_mask)
863 table = env->cp15.c2_base1 & 0xffffc000;
864 else
865 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
866
867 table |= (address >> 18) & 0x3ffc;
868 return table;
869}
870
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100871static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +0000872 int is_user, uint32_t *phys_ptr, int *prot,
873 target_ulong *page_size)
bellardb5ff1b32005-11-26 10:38:39 +0000874{
875 int code;
876 uint32_t table;
877 uint32_t desc;
878 int type;
879 int ap;
880 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000881 int domain_prot;
bellardb5ff1b32005-11-26 10:38:39 +0000882 uint32_t phys_addr;
883
pbrook9ee6e8b2007-11-11 00:04:49 +0000884 /* Pagetable walk. */
885 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +0000886 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +0000887 desc = ldl_phys(table);
888 type = (desc & 3);
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000889 domain = (desc >> 5) & 0x0f;
890 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
pbrook9ee6e8b2007-11-11 00:04:49 +0000891 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +0000892 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000893 code = 5;
894 goto do_fault;
895 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000896 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +0000897 if (type == 2)
898 code = 9; /* Section domain fault. */
899 else
900 code = 11; /* Page domain fault. */
901 goto do_fault;
902 }
903 if (type == 2) {
904 /* 1Mb section. */
905 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
906 ap = (desc >> 10) & 3;
907 code = 13;
Paul Brookd4c430a2010-03-17 02:14:28 +0000908 *page_size = 1024 * 1024;
pbrook9ee6e8b2007-11-11 00:04:49 +0000909 } else {
910 /* Lookup l2 entry. */
911 if (type == 1) {
912 /* Coarse pagetable. */
913 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
914 } else {
915 /* Fine pagetable. */
916 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
917 }
918 desc = ldl_phys(table);
919 switch (desc & 3) {
920 case 0: /* Page translation fault. */
921 code = 7;
922 goto do_fault;
923 case 1: /* 64k page. */
924 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
925 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +0000926 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +0000927 break;
928 case 2: /* 4k page. */
929 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
930 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +0000931 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +0000932 break;
933 case 3: /* 1k page. */
934 if (type == 1) {
935 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
936 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
937 } else {
938 /* Page translation fault. */
939 code = 7;
940 goto do_fault;
941 }
942 } else {
943 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
944 }
945 ap = (desc >> 4) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +0000946 *page_size = 0x400;
pbrook9ee6e8b2007-11-11 00:04:49 +0000947 break;
948 default:
949 /* Never happens, but compiler isn't smart enough to tell. */
950 abort();
951 }
952 code = 15;
953 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000954 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
pbrook9ee6e8b2007-11-11 00:04:49 +0000955 if (!*prot) {
956 /* Access permission fault. */
957 goto do_fault;
958 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +0530959 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +0000960 *phys_ptr = phys_addr;
961 return 0;
962do_fault:
963 return code | (domain << 4);
964}
965
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100966static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +0000967 int is_user, uint32_t *phys_ptr, int *prot,
968 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +0000969{
970 int code;
971 uint32_t table;
972 uint32_t desc;
973 uint32_t xn;
974 int type;
975 int ap;
976 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000977 int domain_prot;
pbrook9ee6e8b2007-11-11 00:04:49 +0000978 uint32_t phys_addr;
979
980 /* Pagetable walk. */
981 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +0000982 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +0000983 desc = ldl_phys(table);
984 type = (desc & 3);
985 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +0000986 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000987 code = 5;
988 domain = 0;
989 goto do_fault;
990 } else if (type == 2 && (desc & (1 << 18))) {
991 /* Supersection. */
992 domain = 0;
993 } else {
994 /* Section or page. */
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000995 domain = (desc >> 5) & 0x0f;
pbrook9ee6e8b2007-11-11 00:04:49 +0000996 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000997 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
998 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +0000999 if (type == 2)
1000 code = 9; /* Section domain fault. */
1001 else
1002 code = 11; /* Page domain fault. */
1003 goto do_fault;
1004 }
1005 if (type == 2) {
1006 if (desc & (1 << 18)) {
1007 /* Supersection. */
1008 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001009 *page_size = 0x1000000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001010 } else {
1011 /* Section. */
1012 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001013 *page_size = 0x100000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001014 }
1015 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1016 xn = desc & (1 << 4);
1017 code = 13;
1018 } else {
1019 /* Lookup l2 entry. */
1020 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1021 desc = ldl_phys(table);
1022 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1023 switch (desc & 3) {
1024 case 0: /* Page translation fault. */
1025 code = 7;
1026 goto do_fault;
1027 case 1: /* 64k page. */
1028 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1029 xn = desc & (1 << 15);
Paul Brookd4c430a2010-03-17 02:14:28 +00001030 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001031 break;
1032 case 2: case 3: /* 4k page. */
1033 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1034 xn = desc & 1;
Paul Brookd4c430a2010-03-17 02:14:28 +00001035 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001036 break;
1037 default:
1038 /* Never happens, but compiler isn't smart enough to tell. */
1039 abort();
1040 }
1041 code = 15;
1042 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001043 if (domain_prot == 3) {
Juha Riihimäkic0034322010-12-08 13:15:16 +02001044 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1045 } else {
1046 if (xn && access_type == 2)
1047 goto do_fault;
pbrook9ee6e8b2007-11-11 00:04:49 +00001048
Juha Riihimäkic0034322010-12-08 13:15:16 +02001049 /* The simplified model uses AP[0] as an access control bit. */
1050 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1051 /* Access flag fault. */
1052 code = (code == 15) ? 6 : 3;
1053 goto do_fault;
1054 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001055 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
Juha Riihimäkic0034322010-12-08 13:15:16 +02001056 if (!*prot) {
1057 /* Access permission fault. */
1058 goto do_fault;
1059 }
1060 if (!xn) {
1061 *prot |= PAGE_EXEC;
1062 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301063 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001064 *phys_ptr = phys_addr;
1065 return 0;
1066do_fault:
1067 return code | (domain << 4);
1068}
1069
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001070static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
pbrook9ee6e8b2007-11-11 00:04:49 +00001071 int is_user, uint32_t *phys_ptr, int *prot)
1072{
1073 int n;
1074 uint32_t mask;
1075 uint32_t base;
1076
1077 *phys_ptr = address;
1078 for (n = 7; n >= 0; n--) {
1079 base = env->cp15.c6_region[n];
1080 if ((base & 1) == 0)
1081 continue;
1082 mask = 1 << ((base >> 1) & 0x1f);
1083 /* Keep this shift separate from the above to avoid an
1084 (undefined) << 32. */
1085 mask = (mask << 1) - 1;
1086 if (((base ^ address) & ~mask) == 0)
1087 break;
1088 }
1089 if (n < 0)
1090 return 2;
1091
1092 if (access_type == 2) {
1093 mask = env->cp15.c5_insn;
1094 } else {
1095 mask = env->cp15.c5_data;
1096 }
1097 mask = (mask >> (n * 4)) & 0xf;
1098 switch (mask) {
1099 case 0:
1100 return 1;
1101 case 1:
1102 if (is_user)
1103 return 1;
1104 *prot = PAGE_READ | PAGE_WRITE;
1105 break;
1106 case 2:
1107 *prot = PAGE_READ;
1108 if (!is_user)
1109 *prot |= PAGE_WRITE;
1110 break;
1111 case 3:
1112 *prot = PAGE_READ | PAGE_WRITE;
1113 break;
1114 case 5:
1115 if (is_user)
1116 return 1;
1117 *prot = PAGE_READ;
1118 break;
1119 case 6:
1120 *prot = PAGE_READ;
1121 break;
1122 default:
1123 /* Bad permission. */
1124 return 1;
1125 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301126 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00001127 return 0;
1128}
1129
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001130static inline int get_phys_addr(CPUARMState *env, uint32_t address,
pbrook9ee6e8b2007-11-11 00:04:49 +00001131 int access_type, int is_user,
Paul Brookd4c430a2010-03-17 02:14:28 +00001132 uint32_t *phys_ptr, int *prot,
1133 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00001134{
bellardb5ff1b32005-11-26 10:38:39 +00001135 /* Fast Context Switch Extension. */
1136 if (address < 0x02000000)
1137 address += env->cp15.c13_fcse;
1138
1139 if ((env->cp15.c1_sys & 1) == 0) {
pbrookce819862007-05-08 02:30:40 +00001140 /* MMU/MPU disabled. */
bellardb5ff1b32005-11-26 10:38:39 +00001141 *phys_ptr = address;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301142 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
Paul Brookd4c430a2010-03-17 02:14:28 +00001143 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001144 return 0;
pbrookce819862007-05-08 02:30:40 +00001145 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
Paul Brookd4c430a2010-03-17 02:14:28 +00001146 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001147 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1148 prot);
1149 } else if (env->cp15.c1_sys & (1 << 23)) {
1150 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001151 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001152 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001153 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001154 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001155 }
bellardb5ff1b32005-11-26 10:38:39 +00001156}
1157
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001158int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
Blue Swirl97b348e2011-08-01 16:12:17 +00001159 int access_type, int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +00001160{
1161 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001162 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001163 int prot;
j_mayer6ebbf392007-10-14 07:07:08 +00001164 int ret, is_user;
bellardb5ff1b32005-11-26 10:38:39 +00001165
j_mayer6ebbf392007-10-14 07:07:08 +00001166 is_user = mmu_idx == MMU_USER_IDX;
Paul Brookd4c430a2010-03-17 02:14:28 +00001167 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1168 &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001169 if (ret == 0) {
1170 /* Map a single [sub]page. */
1171 phys_addr &= ~(uint32_t)0x3ff;
1172 address &= ~(uint32_t)0x3ff;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301173 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
Paul Brookd4c430a2010-03-17 02:14:28 +00001174 return 0;
bellardb5ff1b32005-11-26 10:38:39 +00001175 }
1176
1177 if (access_type == 2) {
1178 env->cp15.c5_insn = ret;
1179 env->cp15.c6_insn = address;
1180 env->exception_index = EXCP_PREFETCH_ABORT;
1181 } else {
1182 env->cp15.c5_data = ret;
pbrook9ee6e8b2007-11-11 00:04:49 +00001183 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1184 env->cp15.c5_data |= (1 << 11);
bellardb5ff1b32005-11-26 10:38:39 +00001185 env->cp15.c6_data = address;
1186 env->exception_index = EXCP_DATA_ABORT;
1187 }
1188 return 1;
1189}
1190
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001191target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
bellardb5ff1b32005-11-26 10:38:39 +00001192{
1193 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001194 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001195 int prot;
1196 int ret;
1197
Paul Brookd4c430a2010-03-17 02:14:28 +00001198 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001199
1200 if (ret != 0)
1201 return -1;
1202
1203 return phys_addr;
1204}
1205
pbrookce819862007-05-08 02:30:40 +00001206/* Return basic MPU access permission bits. */
1207static uint32_t simple_mpu_ap_bits(uint32_t val)
1208{
1209 uint32_t ret;
1210 uint32_t mask;
1211 int i;
1212 ret = 0;
1213 mask = 3;
1214 for (i = 0; i < 16; i += 2) {
1215 ret |= (val >> i) & mask;
1216 mask <<= 2;
1217 }
1218 return ret;
1219}
1220
1221/* Pad basic MPU access permission bits to extended format. */
1222static uint32_t extended_mpu_ap_bits(uint32_t val)
1223{
1224 uint32_t ret;
1225 uint32_t mask;
1226 int i;
1227 ret = 0;
1228 mask = 3;
1229 for (i = 0; i < 16; i += 2) {
1230 ret |= (val & mask) << i;
1231 mask <<= 2;
1232 }
1233 return ret;
1234}
1235
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001236void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +00001237{
pbrook9ee6e8b2007-11-11 00:04:49 +00001238 int op1;
1239 int op2;
1240 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001241
pbrook9ee6e8b2007-11-11 00:04:49 +00001242 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001243 op2 = (insn >> 5) & 7;
pbrookce819862007-05-08 02:30:40 +00001244 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001245 switch ((insn >> 16) & 0xf) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001246 case 0:
pbrook9ee6e8b2007-11-11 00:04:49 +00001247 /* ID codes. */
balrog610c3c82007-06-24 12:09:48 +00001248 if (arm_feature(env, ARM_FEATURE_XSCALE))
1249 break;
balrogc3d26892007-07-29 17:57:26 +00001250 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1251 break;
pbrooka49ea272008-12-19 13:37:53 +00001252 if (arm_feature(env, ARM_FEATURE_V7)
1253 && op1 == 2 && crm == 0 && op2 == 0) {
1254 env->cp15.c0_cssel = val & 0xf;
1255 break;
1256 }
bellardb5ff1b32005-11-26 10:38:39 +00001257 goto bad_reg;
1258 case 1: /* System configuration. */
Rob Herring2be27622012-01-13 17:25:08 +00001259 if (arm_feature(env, ARM_FEATURE_V7)
1260 && op1 == 0 && crm == 1 && op2 == 0) {
1261 env->cp15.c1_scr = val;
1262 break;
1263 }
balrogc3d26892007-07-29 17:57:26 +00001264 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1265 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001266 switch (op2) {
1267 case 0:
pbrookce819862007-05-08 02:30:40 +00001268 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
balrogc1713132007-04-30 01:26:42 +00001269 env->cp15.c1_sys = val;
bellardb5ff1b32005-11-26 10:38:39 +00001270 /* ??? Lots of these bits are not implemented. */
1271 /* This may enable/disable the MMU, so do a TLB flush. */
1272 tlb_flush(env, 1);
1273 break;
Stefan Weil61cc8702011-04-13 22:45:22 +02001274 case 1: /* Auxiliary control register. */
balrog610c3c82007-06-24 12:09:48 +00001275 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1276 env->cp15.c1_xscaleauxcr = val;
balrogc1713132007-04-30 01:26:42 +00001277 break;
balrog610c3c82007-06-24 12:09:48 +00001278 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001279 /* Not implemented. */
1280 break;
bellardb5ff1b32005-11-26 10:38:39 +00001281 case 2:
balrog610c3c82007-06-24 12:09:48 +00001282 if (arm_feature(env, ARM_FEATURE_XSCALE))
1283 goto bad_reg;
pbrook4be27db2008-10-22 16:14:08 +00001284 if (env->cp15.c1_coproc != val) {
1285 env->cp15.c1_coproc = val;
1286 /* ??? Is this safe when called from within a TB? */
1287 tb_flush(env);
1288 }
balrogc1713132007-04-30 01:26:42 +00001289 break;
bellardb5ff1b32005-11-26 10:38:39 +00001290 default:
1291 goto bad_reg;
1292 }
1293 break;
pbrookce819862007-05-08 02:30:40 +00001294 case 2: /* MMU Page table control / MPU cache control. */
1295 if (arm_feature(env, ARM_FEATURE_MPU)) {
1296 switch (op2) {
1297 case 0:
1298 env->cp15.c2_data = val;
1299 break;
1300 case 1:
1301 env->cp15.c2_insn = val;
1302 break;
1303 default:
1304 goto bad_reg;
1305 }
1306 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001307 switch (op2) {
1308 case 0:
1309 env->cp15.c2_base0 = val;
1310 break;
1311 case 1:
1312 env->cp15.c2_base1 = val;
1313 break;
1314 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00001315 val &= 7;
1316 env->cp15.c2_control = val;
pbrook9ee6e8b2007-11-11 00:04:49 +00001317 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
pbrookb2fa1792008-10-22 19:22:30 +00001318 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
pbrook9ee6e8b2007-11-11 00:04:49 +00001319 break;
1320 default:
1321 goto bad_reg;
1322 }
pbrookce819862007-05-08 02:30:40 +00001323 }
bellardb5ff1b32005-11-26 10:38:39 +00001324 break;
pbrookce819862007-05-08 02:30:40 +00001325 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00001326 env->cp15.c3 = val;
balrog405ee3a2007-10-31 00:47:13 +00001327 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
bellardb5ff1b32005-11-26 10:38:39 +00001328 break;
1329 case 4: /* Reserved. */
1330 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00001331 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00001332 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1333 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001334 switch (op2) {
1335 case 0:
pbrookce819862007-05-08 02:30:40 +00001336 if (arm_feature(env, ARM_FEATURE_MPU))
1337 val = extended_mpu_ap_bits(val);
bellardb5ff1b32005-11-26 10:38:39 +00001338 env->cp15.c5_data = val;
1339 break;
1340 case 1:
pbrookce819862007-05-08 02:30:40 +00001341 if (arm_feature(env, ARM_FEATURE_MPU))
1342 val = extended_mpu_ap_bits(val);
1343 env->cp15.c5_insn = val;
1344 break;
1345 case 2:
1346 if (!arm_feature(env, ARM_FEATURE_MPU))
1347 goto bad_reg;
1348 env->cp15.c5_data = val;
1349 break;
1350 case 3:
1351 if (!arm_feature(env, ARM_FEATURE_MPU))
1352 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001353 env->cp15.c5_insn = val;
1354 break;
1355 default:
1356 goto bad_reg;
1357 }
1358 break;
pbrookce819862007-05-08 02:30:40 +00001359 case 6: /* MMU Fault address / MPU base/size. */
1360 if (arm_feature(env, ARM_FEATURE_MPU)) {
1361 if (crm >= 8)
1362 goto bad_reg;
1363 env->cp15.c6_region[crm] = val;
1364 } else {
balrogc3d26892007-07-29 17:57:26 +00001365 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1366 op2 = 0;
pbrookce819862007-05-08 02:30:40 +00001367 switch (op2) {
1368 case 0:
1369 env->cp15.c6_data = val;
1370 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001371 case 1: /* ??? This is WFAR on armv6 */
1372 case 2:
pbrookce819862007-05-08 02:30:40 +00001373 env->cp15.c6_insn = val;
1374 break;
1375 default:
1376 goto bad_reg;
1377 }
bellardb5ff1b32005-11-26 10:38:39 +00001378 }
1379 break;
1380 case 7: /* Cache control. */
balrogc3d26892007-07-29 17:57:26 +00001381 env->cp15.c15_i_max = 0x000;
1382 env->cp15.c15_i_min = 0xff0;
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01001383 if (op1 != 0) {
1384 goto bad_reg;
1385 }
1386 /* No cache, so nothing to do except VA->PA translations. */
Peter Maydell906879a2011-07-20 10:32:55 +00001387 if (arm_feature(env, ARM_FEATURE_VAPA)) {
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01001388 switch (crm) {
1389 case 4:
1390 if (arm_feature(env, ARM_FEATURE_V7)) {
1391 env->cp15.c7_par = val & 0xfffff6ff;
1392 } else {
1393 env->cp15.c7_par = val & 0xfffff1ff;
1394 }
1395 break;
1396 case 8: {
1397 uint32_t phys_addr;
1398 target_ulong page_size;
1399 int prot;
1400 int ret, is_user = op2 & 2;
1401 int access_type = op2 & 1;
1402
1403 if (op2 & 4) {
1404 /* Other states are only available with TrustZone */
1405 goto bad_reg;
1406 }
1407 ret = get_phys_addr(env, val, access_type, is_user,
1408 &phys_addr, &prot, &page_size);
1409 if (ret == 0) {
1410 /* We do not set any attribute bits in the PAR */
1411 if (page_size == (1 << 24)
1412 && arm_feature(env, ARM_FEATURE_V7)) {
1413 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1414 } else {
1415 env->cp15.c7_par = phys_addr & 0xfffff000;
1416 }
1417 } else {
1418 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1419 ((ret & (12 << 1)) >> 6) |
1420 ((ret & 0xf) << 1) | 1;
1421 }
1422 break;
1423 }
1424 }
1425 }
bellardb5ff1b32005-11-26 10:38:39 +00001426 break;
1427 case 8: /* MMU TLB control. */
1428 switch (op2) {
Peter Maydelldc8714c2012-01-25 11:49:46 +00001429 case 0: /* Invalidate all (TLBIALL) */
1430 tlb_flush(env, 1);
bellardb5ff1b32005-11-26 10:38:39 +00001431 break;
Peter Maydelldc8714c2012-01-25 11:49:46 +00001432 case 1: /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
Paul Brookd4c430a2010-03-17 02:14:28 +00001433 tlb_flush_page(env, val & TARGET_PAGE_MASK);
bellardb5ff1b32005-11-26 10:38:39 +00001434 break;
Peter Maydelldc8714c2012-01-25 11:49:46 +00001435 case 2: /* Invalidate by ASID (TLBIASID) */
pbrook9ee6e8b2007-11-11 00:04:49 +00001436 tlb_flush(env, val == 0);
1437 break;
Peter Maydelldc8714c2012-01-25 11:49:46 +00001438 case 3: /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
1439 tlb_flush_page(env, val & TARGET_PAGE_MASK);
pbrook9ee6e8b2007-11-11 00:04:49 +00001440 break;
bellardb5ff1b32005-11-26 10:38:39 +00001441 default:
1442 goto bad_reg;
1443 }
1444 break;
pbrookce819862007-05-08 02:30:40 +00001445 case 9:
balrogc3d26892007-07-29 17:57:26 +00001446 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1447 break;
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +04001448 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1449 break; /* Ignore ReadBuffer access */
pbrookce819862007-05-08 02:30:40 +00001450 switch (crm) {
1451 case 0: /* Cache lockdown. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001452 switch (op1) {
1453 case 0: /* L1 cache. */
1454 switch (op2) {
1455 case 0:
1456 env->cp15.c9_data = val;
1457 break;
1458 case 1:
1459 env->cp15.c9_insn = val;
1460 break;
1461 default:
1462 goto bad_reg;
1463 }
1464 break;
1465 case 1: /* L2 cache. */
1466 /* Ignore writes to L2 lockdown/auxiliary registers. */
1467 break;
1468 default:
1469 goto bad_reg;
1470 }
1471 break;
pbrookce819862007-05-08 02:30:40 +00001472 case 1: /* TCM memory region registers. */
1473 /* Not implemented. */
1474 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00001475 case 12: /* Performance monitor control */
1476 /* Performance monitors are implementation defined in v7,
1477 * but with an ARM recommended set of registers, which we
1478 * follow (although we don't actually implement any counters)
1479 */
1480 if (!arm_feature(env, ARM_FEATURE_V7)) {
1481 goto bad_reg;
1482 }
1483 switch (op2) {
1484 case 0: /* performance monitor control register */
1485 /* only the DP, X, D and E bits are writable */
1486 env->cp15.c9_pmcr &= ~0x39;
1487 env->cp15.c9_pmcr |= (val & 0x39);
1488 break;
1489 case 1: /* Count enable set register */
1490 val &= (1 << 31);
1491 env->cp15.c9_pmcnten |= val;
1492 break;
1493 case 2: /* Count enable clear */
1494 val &= (1 << 31);
1495 env->cp15.c9_pmcnten &= ~val;
1496 break;
1497 case 3: /* Overflow flag status */
1498 env->cp15.c9_pmovsr &= ~val;
1499 break;
1500 case 4: /* Software increment */
1501 /* RAZ/WI since we don't implement the software-count event */
1502 break;
1503 case 5: /* Event counter selection register */
1504 /* Since we don't implement any events, writing to this register
1505 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1506 */
1507 break;
1508 default:
1509 goto bad_reg;
1510 }
1511 break;
1512 case 13: /* Performance counters */
1513 if (!arm_feature(env, ARM_FEATURE_V7)) {
1514 goto bad_reg;
1515 }
1516 switch (op2) {
1517 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1518 break;
1519 case 1: /* Event type select */
1520 env->cp15.c9_pmxevtyper = val & 0xff;
1521 break;
1522 case 2: /* Event count register */
1523 /* Unimplemented (we have no events), RAZ/WI */
1524 break;
1525 default:
1526 goto bad_reg;
1527 }
1528 break;
1529 case 14: /* Performance monitor control */
1530 if (!arm_feature(env, ARM_FEATURE_V7)) {
1531 goto bad_reg;
1532 }
1533 switch (op2) {
1534 case 0: /* user enable */
1535 env->cp15.c9_pmuserenr = val & 1;
1536 /* changes access rights for cp registers, so flush tbs */
1537 tb_flush(env);
1538 break;
1539 case 1: /* interrupt enable set */
1540 /* We have no event counters so only the C bit can be changed */
1541 val &= (1 << 31);
1542 env->cp15.c9_pminten |= val;
1543 break;
1544 case 2: /* interrupt enable clear */
1545 val &= (1 << 31);
1546 env->cp15.c9_pminten &= ~val;
1547 break;
1548 }
1549 break;
bellardb5ff1b32005-11-26 10:38:39 +00001550 default:
1551 goto bad_reg;
1552 }
1553 break;
1554 case 10: /* MMU TLB lockdown. */
1555 /* ??? TLB lockdown not implemented. */
1556 break;
bellardb5ff1b32005-11-26 10:38:39 +00001557 case 12: /* Reserved. */
1558 goto bad_reg;
1559 case 13: /* Process ID. */
1560 switch (op2) {
1561 case 0:
pbrookd07edbf2006-07-21 22:39:57 +00001562 /* Unlike real hardware the qemu TLB uses virtual addresses,
1563 not modified virtual addresses, so this causes a TLB flush.
1564 */
1565 if (env->cp15.c13_fcse != val)
1566 tlb_flush(env, 1);
1567 env->cp15.c13_fcse = val;
bellardb5ff1b32005-11-26 10:38:39 +00001568 break;
1569 case 1:
pbrookd07edbf2006-07-21 22:39:57 +00001570 /* This changes the ASID, so do a TLB flush. */
pbrookce819862007-05-08 02:30:40 +00001571 if (env->cp15.c13_context != val
1572 && !arm_feature(env, ARM_FEATURE_MPU))
pbrookd07edbf2006-07-21 22:39:57 +00001573 tlb_flush(env, 0);
1574 env->cp15.c13_context = val;
bellardb5ff1b32005-11-26 10:38:39 +00001575 break;
1576 default:
1577 goto bad_reg;
1578 }
1579 break;
Peter Maydell0383ac02012-01-25 12:42:29 +00001580 case 14: /* Generic timer */
1581 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1582 /* Dummy implementation: RAZ/WI for all */
1583 break;
1584 }
bellardb5ff1b32005-11-26 10:38:39 +00001585 goto bad_reg;
1586 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00001587 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
pbrookce819862007-05-08 02:30:40 +00001588 if (op2 == 0 && crm == 1) {
balrog2e232132007-08-01 02:31:54 +00001589 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1590 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1591 tb_flush(env);
1592 env->cp15.c15_cpar = val & 0x3fff;
1593 }
balrogc1713132007-04-30 01:26:42 +00001594 break;
1595 }
1596 goto bad_reg;
1597 }
balrogc3d26892007-07-29 17:57:26 +00001598 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1599 switch (crm) {
1600 case 0:
1601 break;
1602 case 1: /* Set TI925T configuration. */
1603 env->cp15.c15_ticonfig = val & 0xe7;
1604 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1605 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1606 break;
1607 case 2: /* Set I_max. */
1608 env->cp15.c15_i_max = val;
1609 break;
1610 case 3: /* Set I_min. */
1611 env->cp15.c15_i_min = val;
1612 break;
1613 case 4: /* Set thread-ID. */
1614 env->cp15.c15_threadid = val & 0xffff;
1615 break;
1616 case 8: /* Wait-for-interrupt (deprecated). */
1617 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1618 break;
1619 default:
1620 goto bad_reg;
1621 }
1622 }
Mark Langsdorf7da362d2012-01-05 15:49:06 +00001623 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1624 switch (crm) {
1625 case 0:
1626 if ((op1 == 0) && (op2 == 0)) {
1627 env->cp15.c15_power_control = val;
1628 } else if ((op1 == 0) && (op2 == 1)) {
1629 env->cp15.c15_diagnostic = val;
1630 } else if ((op1 == 0) && (op2 == 2)) {
1631 env->cp15.c15_power_diagnostic = val;
1632 }
1633 default:
1634 break;
1635 }
1636 }
bellardb5ff1b32005-11-26 10:38:39 +00001637 break;
1638 }
1639 return;
1640bad_reg:
1641 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001642 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1643 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00001644}
1645
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001646uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +00001647{
pbrook9ee6e8b2007-11-11 00:04:49 +00001648 int op1;
1649 int op2;
1650 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001651
pbrook9ee6e8b2007-11-11 00:04:49 +00001652 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001653 op2 = (insn >> 5) & 7;
balrogc3d26892007-07-29 17:57:26 +00001654 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001655 switch ((insn >> 16) & 0xf) {
1656 case 0: /* ID codes. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001657 switch (op1) {
1658 case 0:
1659 switch (crm) {
1660 case 0:
1661 switch (op2) {
1662 case 0: /* Device ID. */
1663 return env->cp15.c0_cpuid;
1664 case 1: /* Cache Type. */
1665 return env->cp15.c0_cachetype;
1666 case 2: /* TCM status. */
1667 return 0;
1668 case 3: /* TLB type register. */
1669 return 0; /* No lockable TLB entries. */
Peter Maydell607b4b02011-02-03 19:43:23 +00001670 case 5: /* MPIDR */
1671 /* The MPIDR was standardised in v7; prior to
1672 * this it was implemented only in the 11MPCore.
1673 * For all other pre-v7 cores it does not exist.
1674 */
1675 if (arm_feature(env, ARM_FEATURE_V7) ||
1676 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1677 int mpidr = env->cpu_index;
1678 /* We don't support setting cluster ID ([8..11])
1679 * so these bits always RAZ.
1680 */
1681 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1682 mpidr |= (1 << 31);
1683 /* Cores which are uniprocessor (non-coherent)
1684 * but still implement the MP extensions set
1685 * bit 30. (For instance, A9UP.) However we do
1686 * not currently model any of those cores.
1687 */
1688 }
1689 return mpidr;
Paul Brook10055562009-11-19 16:45:20 +00001690 }
Peter Maydell607b4b02011-02-03 19:43:23 +00001691 /* otherwise fall through to the unimplemented-reg case */
pbrook9ee6e8b2007-11-11 00:04:49 +00001692 default:
1693 goto bad_reg;
1694 }
1695 case 1:
1696 if (!arm_feature(env, ARM_FEATURE_V6))
1697 goto bad_reg;
1698 return env->cp15.c0_c1[op2];
1699 case 2:
1700 if (!arm_feature(env, ARM_FEATURE_V6))
1701 goto bad_reg;
1702 return env->cp15.c0_c2[op2];
1703 case 3: case 4: case 5: case 6: case 7:
1704 return 0;
1705 default:
1706 goto bad_reg;
1707 }
1708 case 1:
1709 /* These registers aren't documented on arm11 cores. However
1710 Linux looks at them anyway. */
1711 if (!arm_feature(env, ARM_FEATURE_V6))
1712 goto bad_reg;
1713 if (crm != 0)
1714 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001715 if (!arm_feature(env, ARM_FEATURE_V7))
1716 return 0;
1717
1718 switch (op2) {
1719 case 0:
1720 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1721 case 1:
1722 return env->cp15.c0_clid;
1723 case 7:
1724 return 0;
1725 }
1726 goto bad_reg;
1727 case 2:
1728 if (op2 != 0 || crm != 0)
balrog610c3c82007-06-24 12:09:48 +00001729 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001730 return env->cp15.c0_cssel;
pbrook9ee6e8b2007-11-11 00:04:49 +00001731 default:
1732 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001733 }
1734 case 1: /* System configuration. */
Rob Herring2be27622012-01-13 17:25:08 +00001735 if (arm_feature(env, ARM_FEATURE_V7)
1736 && op1 == 0 && crm == 1 && op2 == 0) {
1737 return env->cp15.c1_scr;
1738 }
balrogc3d26892007-07-29 17:57:26 +00001739 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1740 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001741 switch (op2) {
1742 case 0: /* Control register. */
1743 return env->cp15.c1_sys;
1744 case 1: /* Auxiliary control register. */
balrogc1713132007-04-30 01:26:42 +00001745 if (arm_feature(env, ARM_FEATURE_XSCALE))
balrog610c3c82007-06-24 12:09:48 +00001746 return env->cp15.c1_xscaleauxcr;
pbrook9ee6e8b2007-11-11 00:04:49 +00001747 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1748 goto bad_reg;
1749 switch (ARM_CPUID(env)) {
1750 case ARM_CPUID_ARM1026:
1751 return 1;
1752 case ARM_CPUID_ARM1136:
balrog827df9f2008-04-14 21:05:22 +00001753 case ARM_CPUID_ARM1136_R2:
Jamie Iles7807eed2011-07-20 10:32:54 +00001754 case ARM_CPUID_ARM1176:
pbrook9ee6e8b2007-11-11 00:04:49 +00001755 return 7;
1756 case ARM_CPUID_ARM11MPCORE:
1757 return 1;
1758 case ARM_CPUID_CORTEXA8:
aurel32533d1772009-03-07 22:10:28 +00001759 return 2;
Paul Brook10055562009-11-19 16:45:20 +00001760 case ARM_CPUID_CORTEXA9:
Peter Maydell0b03bdf2012-01-25 12:42:29 +00001761 case ARM_CPUID_CORTEXA15:
Paul Brook10055562009-11-19 16:45:20 +00001762 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00001763 default:
1764 goto bad_reg;
1765 }
bellardb5ff1b32005-11-26 10:38:39 +00001766 case 2: /* Coprocessor access register. */
balrog610c3c82007-06-24 12:09:48 +00001767 if (arm_feature(env, ARM_FEATURE_XSCALE))
1768 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001769 return env->cp15.c1_coproc;
1770 default:
1771 goto bad_reg;
1772 }
pbrookce819862007-05-08 02:30:40 +00001773 case 2: /* MMU Page table control / MPU cache control. */
1774 if (arm_feature(env, ARM_FEATURE_MPU)) {
1775 switch (op2) {
1776 case 0:
1777 return env->cp15.c2_data;
1778 break;
1779 case 1:
1780 return env->cp15.c2_insn;
1781 break;
1782 default:
1783 goto bad_reg;
1784 }
1785 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001786 switch (op2) {
1787 case 0:
1788 return env->cp15.c2_base0;
1789 case 1:
1790 return env->cp15.c2_base1;
1791 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00001792 return env->cp15.c2_control;
pbrook9ee6e8b2007-11-11 00:04:49 +00001793 default:
1794 goto bad_reg;
1795 }
1796 }
pbrookce819862007-05-08 02:30:40 +00001797 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00001798 return env->cp15.c3;
1799 case 4: /* Reserved. */
1800 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00001801 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00001802 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1803 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001804 switch (op2) {
1805 case 0:
pbrookce819862007-05-08 02:30:40 +00001806 if (arm_feature(env, ARM_FEATURE_MPU))
1807 return simple_mpu_ap_bits(env->cp15.c5_data);
bellardb5ff1b32005-11-26 10:38:39 +00001808 return env->cp15.c5_data;
1809 case 1:
pbrookce819862007-05-08 02:30:40 +00001810 if (arm_feature(env, ARM_FEATURE_MPU))
Peter Maydell4de47792012-03-14 12:26:10 +00001811 return simple_mpu_ap_bits(env->cp15.c5_insn);
pbrookce819862007-05-08 02:30:40 +00001812 return env->cp15.c5_insn;
1813 case 2:
1814 if (!arm_feature(env, ARM_FEATURE_MPU))
1815 goto bad_reg;
1816 return env->cp15.c5_data;
1817 case 3:
1818 if (!arm_feature(env, ARM_FEATURE_MPU))
1819 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001820 return env->cp15.c5_insn;
1821 default:
1822 goto bad_reg;
1823 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001824 case 6: /* MMU Fault address. */
pbrookce819862007-05-08 02:30:40 +00001825 if (arm_feature(env, ARM_FEATURE_MPU)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001826 if (crm >= 8)
pbrookce819862007-05-08 02:30:40 +00001827 goto bad_reg;
pbrook9ee6e8b2007-11-11 00:04:49 +00001828 return env->cp15.c6_region[crm];
pbrookce819862007-05-08 02:30:40 +00001829 } else {
balrogc3d26892007-07-29 17:57:26 +00001830 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1831 op2 = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00001832 switch (op2) {
1833 case 0:
1834 return env->cp15.c6_data;
1835 case 1:
1836 if (arm_feature(env, ARM_FEATURE_V6)) {
1837 /* Watchpoint Fault Adrress. */
1838 return 0; /* Not implemented. */
1839 } else {
1840 /* Instruction Fault Adrress. */
1841 /* Arm9 doesn't have an IFAR, but implementing it anyway
1842 shouldn't do any harm. */
1843 return env->cp15.c6_insn;
1844 }
1845 case 2:
1846 if (arm_feature(env, ARM_FEATURE_V6)) {
1847 /* Instruction Fault Adrress. */
1848 return env->cp15.c6_insn;
1849 } else {
1850 goto bad_reg;
1851 }
1852 default:
1853 goto bad_reg;
1854 }
bellardb5ff1b32005-11-26 10:38:39 +00001855 }
1856 case 7: /* Cache control. */
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01001857 if (crm == 4 && op1 == 0 && op2 == 0) {
1858 return env->cp15.c7_par;
1859 }
pbrook6fbe23d2008-04-01 17:19:11 +00001860 /* FIXME: Should only clear Z flag if destination is r15. */
1861 env->ZF = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001862 return 0;
1863 case 8: /* MMU TLB control. */
1864 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00001865 case 9:
1866 switch (crm) {
1867 case 0: /* Cache lockdown */
1868 switch (op1) {
1869 case 0: /* L1 cache. */
1870 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1871 return 0;
1872 }
1873 switch (op2) {
1874 case 0:
1875 return env->cp15.c9_data;
1876 case 1:
1877 return env->cp15.c9_insn;
1878 default:
1879 goto bad_reg;
1880 }
1881 case 1: /* L2 cache */
Peter Maydell0b03bdf2012-01-25 12:42:29 +00001882 /* L2 Lockdown and Auxiliary control. */
1883 switch (op2) {
1884 case 0:
1885 /* L2 cache lockdown (A8 only) */
1886 return 0;
1887 case 2:
1888 /* L2 cache auxiliary control (A8) or control (A15) */
1889 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
1890 /* Linux wants the number of processors from here.
1891 * Might as well set the interrupt-controller bit too.
1892 */
1893 return ((smp_cpus - 1) << 24) | (1 << 23);
1894 }
1895 return 0;
1896 case 3:
1897 /* L2 cache extended control (A15) */
1898 return 0;
1899 default:
Peter Maydell74594c92011-03-22 12:16:16 +00001900 goto bad_reg;
1901 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001902 default:
1903 goto bad_reg;
1904 }
Peter Maydell74594c92011-03-22 12:16:16 +00001905 break;
1906 case 12: /* Performance monitor control */
1907 if (!arm_feature(env, ARM_FEATURE_V7)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001908 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00001909 }
1910 switch (op2) {
1911 case 0: /* performance monitor control register */
1912 return env->cp15.c9_pmcr;
1913 case 1: /* count enable set */
1914 case 2: /* count enable clear */
1915 return env->cp15.c9_pmcnten;
1916 case 3: /* overflow flag status */
1917 return env->cp15.c9_pmovsr;
1918 case 4: /* software increment */
1919 case 5: /* event counter selection register */
1920 return 0; /* Unimplemented, RAZ/WI */
1921 default:
1922 goto bad_reg;
1923 }
1924 case 13: /* Performance counters */
1925 if (!arm_feature(env, ARM_FEATURE_V7)) {
1926 goto bad_reg;
1927 }
1928 switch (op2) {
1929 case 1: /* Event type select */
1930 return env->cp15.c9_pmxevtyper;
1931 case 0: /* Cycle count register */
1932 case 2: /* Event count register */
1933 /* Unimplemented, so RAZ/WI */
1934 return 0;
1935 default:
1936 goto bad_reg;
1937 }
1938 case 14: /* Performance monitor control */
1939 if (!arm_feature(env, ARM_FEATURE_V7)) {
1940 goto bad_reg;
1941 }
1942 switch (op2) {
1943 case 0: /* user enable */
1944 return env->cp15.c9_pmuserenr;
1945 case 1: /* interrupt enable set */
1946 case 2: /* interrupt enable clear */
1947 return env->cp15.c9_pminten;
1948 default:
1949 goto bad_reg;
1950 }
bellardb5ff1b32005-11-26 10:38:39 +00001951 default:
1952 goto bad_reg;
1953 }
Peter Maydell74594c92011-03-22 12:16:16 +00001954 break;
bellardb5ff1b32005-11-26 10:38:39 +00001955 case 10: /* MMU TLB lockdown. */
1956 /* ??? TLB lockdown not implemented. */
1957 return 0;
1958 case 11: /* TCM DMA control. */
1959 case 12: /* Reserved. */
1960 goto bad_reg;
1961 case 13: /* Process ID. */
1962 switch (op2) {
1963 case 0:
1964 return env->cp15.c13_fcse;
1965 case 1:
1966 return env->cp15.c13_context;
1967 default:
1968 goto bad_reg;
1969 }
Peter Maydell0383ac02012-01-25 12:42:29 +00001970 case 14: /* Generic timer */
1971 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1972 /* Dummy implementation: RAZ/WI for all */
1973 return 0;
1974 }
bellardb5ff1b32005-11-26 10:38:39 +00001975 goto bad_reg;
1976 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00001977 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
balrogc3d26892007-07-29 17:57:26 +00001978 if (op2 == 0 && crm == 1)
balrogc1713132007-04-30 01:26:42 +00001979 return env->cp15.c15_cpar;
1980
1981 goto bad_reg;
1982 }
balrogc3d26892007-07-29 17:57:26 +00001983 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1984 switch (crm) {
1985 case 0:
1986 return 0;
1987 case 1: /* Read TI925T configuration. */
1988 return env->cp15.c15_ticonfig;
1989 case 2: /* Read I_max. */
1990 return env->cp15.c15_i_max;
1991 case 3: /* Read I_min. */
1992 return env->cp15.c15_i_min;
1993 case 4: /* Read thread-ID. */
1994 return env->cp15.c15_threadid;
1995 case 8: /* TI925T_status */
1996 return 0;
1997 }
balrog827df9f2008-04-14 21:05:22 +00001998 /* TODO: Peripheral port remap register:
1999 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2000 * controller base address at $rn & ~0xfff and map size of
2001 * 0x200 << ($rn & 0xfff), when MMU is off. */
balrogc3d26892007-07-29 17:57:26 +00002002 goto bad_reg;
2003 }
Mark Langsdorf7da362d2012-01-05 15:49:06 +00002004 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
2005 switch (crm) {
2006 case 0:
2007 if ((op1 == 4) && (op2 == 0)) {
2008 /* The config_base_address should hold the value of
2009 * the peripheral base. ARM should get this from a CPU
2010 * object property, but that support isn't available in
2011 * December 2011. Default to 0 for now and board models
2012 * that care can set it by a private hook */
2013 return env->cp15.c15_config_base_address;
2014 } else if ((op1 == 0) && (op2 == 0)) {
2015 /* power_control should be set to maximum latency. Again,
2016 default to 0 and set by private hook */
2017 return env->cp15.c15_power_control;
2018 } else if ((op1 == 0) && (op2 == 1)) {
2019 return env->cp15.c15_diagnostic;
2020 } else if ((op1 == 0) && (op2 == 2)) {
2021 return env->cp15.c15_power_diagnostic;
2022 }
2023 break;
2024 case 1: /* NEON Busy */
2025 return 0;
2026 case 5: /* tlb lockdown */
2027 case 6:
2028 case 7:
2029 if ((op1 == 5) && (op2 == 2)) {
2030 return 0;
2031 }
2032 break;
2033 default:
2034 break;
2035 }
2036 goto bad_reg;
2037 }
bellardb5ff1b32005-11-26 10:38:39 +00002038 return 0;
2039 }
2040bad_reg:
2041 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002042 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2043 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00002044 return 0;
2045}
2046
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002047void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002048{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002049 if ((env->uncached_cpsr & CPSR_M) == mode) {
2050 env->regs[13] = val;
2051 } else {
Peter Maydell1b9e01c2012-01-05 15:49:06 +00002052 env->banked_r13[bank_number(env, mode)] = val;
Peter Maydell39ea3d42011-01-14 20:39:18 +01002053 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002054}
2055
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002056uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00002057{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002058 if ((env->uncached_cpsr & CPSR_M) == mode) {
2059 return env->regs[13];
2060 } else {
Peter Maydell1b9e01c2012-01-05 15:49:06 +00002061 return env->banked_r13[bank_number(env, mode)];
Peter Maydell39ea3d42011-01-14 20:39:18 +01002062 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002063}
2064
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002065uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00002066{
2067 switch (reg) {
2068 case 0: /* APSR */
2069 return xpsr_read(env) & 0xf8000000;
2070 case 1: /* IAPSR */
2071 return xpsr_read(env) & 0xf80001ff;
2072 case 2: /* EAPSR */
2073 return xpsr_read(env) & 0xff00fc00;
2074 case 3: /* xPSR */
2075 return xpsr_read(env) & 0xff00fdff;
2076 case 5: /* IPSR */
2077 return xpsr_read(env) & 0x000001ff;
2078 case 6: /* EPSR */
2079 return xpsr_read(env) & 0x0700fc00;
2080 case 7: /* IEPSR */
2081 return xpsr_read(env) & 0x0700edff;
2082 case 8: /* MSP */
2083 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2084 case 9: /* PSP */
2085 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2086 case 16: /* PRIMASK */
2087 return (env->uncached_cpsr & CPSR_I) != 0;
Sebastian Huber82845822011-05-29 02:58:41 +00002088 case 17: /* BASEPRI */
2089 case 18: /* BASEPRI_MAX */
pbrook9ee6e8b2007-11-11 00:04:49 +00002090 return env->v7m.basepri;
Sebastian Huber82845822011-05-29 02:58:41 +00002091 case 19: /* FAULTMASK */
2092 return (env->uncached_cpsr & CPSR_F) != 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002093 case 20: /* CONTROL */
2094 return env->v7m.control;
2095 default:
2096 /* ??? For debugging only. */
2097 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2098 return 0;
2099 }
2100}
2101
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002102void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002103{
2104 switch (reg) {
2105 case 0: /* APSR */
2106 xpsr_write(env, val, 0xf8000000);
2107 break;
2108 case 1: /* IAPSR */
2109 xpsr_write(env, val, 0xf8000000);
2110 break;
2111 case 2: /* EAPSR */
2112 xpsr_write(env, val, 0xfe00fc00);
2113 break;
2114 case 3: /* xPSR */
2115 xpsr_write(env, val, 0xfe00fc00);
2116 break;
2117 case 5: /* IPSR */
2118 /* IPSR bits are readonly. */
2119 break;
2120 case 6: /* EPSR */
2121 xpsr_write(env, val, 0x0600fc00);
2122 break;
2123 case 7: /* IEPSR */
2124 xpsr_write(env, val, 0x0600fc00);
2125 break;
2126 case 8: /* MSP */
2127 if (env->v7m.current_sp)
2128 env->v7m.other_sp = val;
2129 else
2130 env->regs[13] = val;
2131 break;
2132 case 9: /* PSP */
2133 if (env->v7m.current_sp)
2134 env->regs[13] = val;
2135 else
2136 env->v7m.other_sp = val;
2137 break;
2138 case 16: /* PRIMASK */
2139 if (val & 1)
2140 env->uncached_cpsr |= CPSR_I;
2141 else
2142 env->uncached_cpsr &= ~CPSR_I;
2143 break;
Sebastian Huber82845822011-05-29 02:58:41 +00002144 case 17: /* BASEPRI */
2145 env->v7m.basepri = val & 0xff;
2146 break;
2147 case 18: /* BASEPRI_MAX */
2148 val &= 0xff;
2149 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2150 env->v7m.basepri = val;
2151 break;
2152 case 19: /* FAULTMASK */
pbrook9ee6e8b2007-11-11 00:04:49 +00002153 if (val & 1)
2154 env->uncached_cpsr |= CPSR_F;
2155 else
2156 env->uncached_cpsr &= ~CPSR_F;
2157 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00002158 case 20: /* CONTROL */
2159 env->v7m.control = val & 3;
2160 switch_v7m_sp(env, (val & 2) != 0);
2161 break;
2162 default:
2163 /* ??? For debugging only. */
2164 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2165 return;
2166 }
2167}
2168
bellardb5ff1b32005-11-26 10:38:39 +00002169#endif
pbrook6ddbc6e2008-03-31 03:46:33 +00002170
2171/* Note that signed overflow is undefined in C. The following routines are
2172 careful to use unsigned types where modulo arithmetic is required.
2173 Failure to do so _will_ break on newer gcc. */
2174
2175/* Signed saturating arithmetic. */
2176
aurel321654b2d2008-04-11 04:55:07 +00002177/* Perform 16-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002178static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2179{
2180 uint16_t res;
2181
2182 res = a + b;
2183 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2184 if (a & 0x8000)
2185 res = 0x8000;
2186 else
2187 res = 0x7fff;
2188 }
2189 return res;
2190}
2191
aurel321654b2d2008-04-11 04:55:07 +00002192/* Perform 8-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002193static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2194{
2195 uint8_t res;
2196
2197 res = a + b;
2198 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2199 if (a & 0x80)
2200 res = 0x80;
2201 else
2202 res = 0x7f;
2203 }
2204 return res;
2205}
2206
aurel321654b2d2008-04-11 04:55:07 +00002207/* Perform 16-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002208static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2209{
2210 uint16_t res;
2211
2212 res = a - b;
2213 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2214 if (a & 0x8000)
2215 res = 0x8000;
2216 else
2217 res = 0x7fff;
2218 }
2219 return res;
2220}
2221
aurel321654b2d2008-04-11 04:55:07 +00002222/* Perform 8-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002223static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2224{
2225 uint8_t res;
2226
2227 res = a - b;
2228 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2229 if (a & 0x80)
2230 res = 0x80;
2231 else
2232 res = 0x7f;
2233 }
2234 return res;
2235}
2236
2237#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2238#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2239#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2240#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2241#define PFX q
2242
2243#include "op_addsub.h"
2244
2245/* Unsigned saturating arithmetic. */
pbrook460a09c2008-05-01 12:04:35 +00002246static inline uint16_t add16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002247{
2248 uint16_t res;
2249 res = a + b;
2250 if (res < a)
2251 res = 0xffff;
2252 return res;
2253}
2254
pbrook460a09c2008-05-01 12:04:35 +00002255static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002256{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002257 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002258 return a - b;
2259 else
2260 return 0;
2261}
2262
2263static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2264{
2265 uint8_t res;
2266 res = a + b;
2267 if (res < a)
2268 res = 0xff;
2269 return res;
2270}
2271
2272static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2273{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002274 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002275 return a - b;
2276 else
2277 return 0;
2278}
2279
2280#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2281#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2282#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2283#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2284#define PFX uq
2285
2286#include "op_addsub.h"
2287
2288/* Signed modulo arithmetic. */
2289#define SARITH16(a, b, n, op) do { \
2290 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002291 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002292 RESULT(sum, n, 16); \
2293 if (sum >= 0) \
2294 ge |= 3 << (n * 2); \
2295 } while(0)
2296
2297#define SARITH8(a, b, n, op) do { \
2298 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002299 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002300 RESULT(sum, n, 8); \
2301 if (sum >= 0) \
2302 ge |= 1 << n; \
2303 } while(0)
2304
2305
2306#define ADD16(a, b, n) SARITH16(a, b, n, +)
2307#define SUB16(a, b, n) SARITH16(a, b, n, -)
2308#define ADD8(a, b, n) SARITH8(a, b, n, +)
2309#define SUB8(a, b, n) SARITH8(a, b, n, -)
2310#define PFX s
2311#define ARITH_GE
2312
2313#include "op_addsub.h"
2314
2315/* Unsigned modulo arithmetic. */
2316#define ADD16(a, b, n) do { \
2317 uint32_t sum; \
2318 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2319 RESULT(sum, n, 16); \
balroga87aa102008-07-19 10:46:13 +00002320 if ((sum >> 16) == 1) \
pbrook6ddbc6e2008-03-31 03:46:33 +00002321 ge |= 3 << (n * 2); \
2322 } while(0)
2323
2324#define ADD8(a, b, n) do { \
2325 uint32_t sum; \
2326 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2327 RESULT(sum, n, 8); \
balroga87aa102008-07-19 10:46:13 +00002328 if ((sum >> 8) == 1) \
2329 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002330 } while(0)
2331
2332#define SUB16(a, b, n) do { \
2333 uint32_t sum; \
2334 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2335 RESULT(sum, n, 16); \
2336 if ((sum >> 16) == 0) \
2337 ge |= 3 << (n * 2); \
2338 } while(0)
2339
2340#define SUB8(a, b, n) do { \
2341 uint32_t sum; \
2342 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2343 RESULT(sum, n, 8); \
2344 if ((sum >> 8) == 0) \
balroga87aa102008-07-19 10:46:13 +00002345 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002346 } while(0)
2347
2348#define PFX u
2349#define ARITH_GE
2350
2351#include "op_addsub.h"
2352
2353/* Halved signed arithmetic. */
2354#define ADD16(a, b, n) \
2355 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2356#define SUB16(a, b, n) \
2357 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2358#define ADD8(a, b, n) \
2359 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2360#define SUB8(a, b, n) \
2361 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2362#define PFX sh
2363
2364#include "op_addsub.h"
2365
2366/* Halved unsigned arithmetic. */
2367#define ADD16(a, b, n) \
2368 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2369#define SUB16(a, b, n) \
2370 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2371#define ADD8(a, b, n) \
2372 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2373#define SUB8(a, b, n) \
2374 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2375#define PFX uh
2376
2377#include "op_addsub.h"
2378
2379static inline uint8_t do_usad(uint8_t a, uint8_t b)
2380{
2381 if (a > b)
2382 return a - b;
2383 else
2384 return b - a;
2385}
2386
2387/* Unsigned sum of absolute byte differences. */
2388uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2389{
2390 uint32_t sum;
2391 sum = do_usad(a, b);
2392 sum += do_usad(a >> 8, b >> 8);
2393 sum += do_usad(a >> 16, b >>16);
2394 sum += do_usad(a >> 24, b >> 24);
2395 return sum;
2396}
2397
2398/* For ARMv6 SEL instruction. */
2399uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2400{
2401 uint32_t mask;
2402
2403 mask = 0;
2404 if (flags & 1)
2405 mask |= 0xff;
2406 if (flags & 2)
2407 mask |= 0xff00;
2408 if (flags & 4)
2409 mask |= 0xff0000;
2410 if (flags & 8)
2411 mask |= 0xff000000;
2412 return (a & mask) | (b & ~mask);
2413}
2414
pbrook5e3f8782008-03-31 03:47:34 +00002415uint32_t HELPER(logicq_cc)(uint64_t val)
2416{
2417 return (val >> 32) | (val != 0);
2418}
pbrook4373f3c2008-03-31 03:47:19 +00002419
2420/* VFP support. We follow the convention used for VFP instrunctions:
2421 Single precition routines have a "s" suffix, double precision a
2422 "d" suffix. */
2423
2424/* Convert host exception flags to vfp form. */
2425static inline int vfp_exceptbits_from_host(int host_bits)
2426{
2427 int target_bits = 0;
2428
2429 if (host_bits & float_flag_invalid)
2430 target_bits |= 1;
2431 if (host_bits & float_flag_divbyzero)
2432 target_bits |= 2;
2433 if (host_bits & float_flag_overflow)
2434 target_bits |= 4;
Peter Maydell36802b62011-05-19 14:46:18 +01002435 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
pbrook4373f3c2008-03-31 03:47:19 +00002436 target_bits |= 8;
2437 if (host_bits & float_flag_inexact)
2438 target_bits |= 0x10;
Peter Maydellcecd8502011-01-06 19:37:55 +00002439 if (host_bits & float_flag_input_denormal)
2440 target_bits |= 0x80;
pbrook4373f3c2008-03-31 03:47:19 +00002441 return target_bits;
2442}
2443
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002444uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002445{
2446 int i;
2447 uint32_t fpscr;
2448
2449 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2450 | (env->vfp.vec_len << 16)
2451 | (env->vfp.vec_stride << 20);
2452 i = get_float_exception_flags(&env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002453 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002454 fpscr |= vfp_exceptbits_from_host(i);
2455 return fpscr;
2456}
2457
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002458uint32_t vfp_get_fpscr(CPUARMState *env)
Peter Maydell01653292010-11-24 15:20:04 +00002459{
2460 return HELPER(vfp_get_fpscr)(env);
2461}
2462
pbrook4373f3c2008-03-31 03:47:19 +00002463/* Convert vfp exception flags to target form. */
2464static inline int vfp_exceptbits_to_host(int target_bits)
2465{
2466 int host_bits = 0;
2467
2468 if (target_bits & 1)
2469 host_bits |= float_flag_invalid;
2470 if (target_bits & 2)
2471 host_bits |= float_flag_divbyzero;
2472 if (target_bits & 4)
2473 host_bits |= float_flag_overflow;
2474 if (target_bits & 8)
2475 host_bits |= float_flag_underflow;
2476 if (target_bits & 0x10)
2477 host_bits |= float_flag_inexact;
Peter Maydellcecd8502011-01-06 19:37:55 +00002478 if (target_bits & 0x80)
2479 host_bits |= float_flag_input_denormal;
pbrook4373f3c2008-03-31 03:47:19 +00002480 return host_bits;
2481}
2482
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002483void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
pbrook4373f3c2008-03-31 03:47:19 +00002484{
2485 int i;
2486 uint32_t changed;
2487
2488 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2489 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2490 env->vfp.vec_len = (val >> 16) & 7;
2491 env->vfp.vec_stride = (val >> 20) & 3;
2492
2493 changed ^= val;
2494 if (changed & (3 << 22)) {
2495 i = (val >> 22) & 3;
2496 switch (i) {
2497 case 0:
2498 i = float_round_nearest_even;
2499 break;
2500 case 1:
2501 i = float_round_up;
2502 break;
2503 case 2:
2504 i = float_round_down;
2505 break;
2506 case 3:
2507 i = float_round_to_zero;
2508 break;
2509 }
2510 set_float_rounding_mode(i, &env->vfp.fp_status);
2511 }
Peter Maydellcecd8502011-01-06 19:37:55 +00002512 if (changed & (1 << 24)) {
pbrookfe76d972008-12-19 14:33:59 +00002513 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
Peter Maydellcecd8502011-01-06 19:37:55 +00002514 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2515 }
pbrook5c7908e2008-12-19 13:53:37 +00002516 if (changed & (1 << 25))
2517 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002518
Peter Maydellb12c3902011-01-06 19:37:54 +00002519 i = vfp_exceptbits_to_host(val);
pbrook4373f3c2008-03-31 03:47:19 +00002520 set_float_exception_flags(i, &env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002521 set_float_exception_flags(0, &env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002522}
2523
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002524void vfp_set_fpscr(CPUARMState *env, uint32_t val)
Peter Maydell01653292010-11-24 15:20:04 +00002525{
2526 HELPER(vfp_set_fpscr)(env, val);
2527}
2528
pbrook4373f3c2008-03-31 03:47:19 +00002529#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2530
2531#define VFP_BINOP(name) \
Peter Maydellae1857e2011-05-25 14:51:48 +00002532float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002533{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002534 float_status *fpst = fpstp; \
2535 return float32_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002536} \
Peter Maydellae1857e2011-05-25 14:51:48 +00002537float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002538{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002539 float_status *fpst = fpstp; \
2540 return float64_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002541}
2542VFP_BINOP(add)
2543VFP_BINOP(sub)
2544VFP_BINOP(mul)
2545VFP_BINOP(div)
2546#undef VFP_BINOP
2547
2548float32 VFP_HELPER(neg, s)(float32 a)
2549{
2550 return float32_chs(a);
2551}
2552
2553float64 VFP_HELPER(neg, d)(float64 a)
2554{
balrog66230e02008-04-20 00:58:01 +00002555 return float64_chs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002556}
2557
2558float32 VFP_HELPER(abs, s)(float32 a)
2559{
2560 return float32_abs(a);
2561}
2562
2563float64 VFP_HELPER(abs, d)(float64 a)
2564{
balrog66230e02008-04-20 00:58:01 +00002565 return float64_abs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002566}
2567
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002568float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002569{
2570 return float32_sqrt(a, &env->vfp.fp_status);
2571}
2572
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002573float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002574{
2575 return float64_sqrt(a, &env->vfp.fp_status);
2576}
2577
2578/* XXX: check quiet/signaling case */
2579#define DO_VFP_cmp(p, type) \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002580void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00002581{ \
2582 uint32_t flags; \
2583 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2584 case 0: flags = 0x6; break; \
2585 case -1: flags = 0x8; break; \
2586 case 1: flags = 0x2; break; \
2587 default: case 2: flags = 0x3; break; \
2588 } \
2589 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2590 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2591} \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002592void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00002593{ \
2594 uint32_t flags; \
2595 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2596 case 0: flags = 0x6; break; \
2597 case -1: flags = 0x8; break; \
2598 case 1: flags = 0x2; break; \
2599 default: case 2: flags = 0x3; break; \
2600 } \
2601 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2602 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2603}
2604DO_VFP_cmp(s, float32)
2605DO_VFP_cmp(d, float64)
2606#undef DO_VFP_cmp
2607
Peter Maydell5500b062011-05-19 14:46:19 +01002608/* Integer to float and float to integer conversions */
2609
2610#define CONV_ITOF(name, fsz, sign) \
2611 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2612{ \
2613 float_status *fpst = fpstp; \
Peter Maydell85836972012-01-25 11:49:46 +00002614 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002615}
2616
Peter Maydell5500b062011-05-19 14:46:19 +01002617#define CONV_FTOI(name, fsz, sign, round) \
2618uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2619{ \
2620 float_status *fpst = fpstp; \
2621 if (float##fsz##_is_any_nan(x)) { \
2622 float_raise(float_flag_invalid, fpst); \
2623 return 0; \
2624 } \
2625 return float##fsz##_to_##sign##int32##round(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002626}
2627
Peter Maydell5500b062011-05-19 14:46:19 +01002628#define FLOAT_CONVS(name, p, fsz, sign) \
2629CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2630CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2631CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
pbrook4373f3c2008-03-31 03:47:19 +00002632
Peter Maydell5500b062011-05-19 14:46:19 +01002633FLOAT_CONVS(si, s, 32, )
2634FLOAT_CONVS(si, d, 64, )
2635FLOAT_CONVS(ui, s, 32, u)
2636FLOAT_CONVS(ui, d, 64, u)
pbrook4373f3c2008-03-31 03:47:19 +00002637
Peter Maydell5500b062011-05-19 14:46:19 +01002638#undef CONV_ITOF
2639#undef CONV_FTOI
2640#undef FLOAT_CONVS
pbrook4373f3c2008-03-31 03:47:19 +00002641
2642/* floating point conversion */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002643float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002644{
Peter Maydell2d627732010-12-07 15:37:34 +00002645 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2646 /* ARM requires that S<->D conversion of any kind of NaN generates
2647 * a quiet NaN by forcing the most significant frac bit to 1.
2648 */
2649 return float64_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002650}
2651
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002652float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002653{
Peter Maydell2d627732010-12-07 15:37:34 +00002654 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2655 /* ARM requires that S<->D conversion of any kind of NaN generates
2656 * a quiet NaN by forcing the most significant frac bit to 1.
2657 */
2658 return float32_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002659}
2660
2661/* VFP3 fixed point conversion. */
Peter Maydell622465e2011-03-14 07:23:11 +00002662#define VFP_CONV_FIX(name, p, fsz, itype, sign) \
Peter Maydell5500b062011-05-19 14:46:19 +01002663float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2664 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002665{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002666 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002667 float##fsz tmp; \
Peter Maydell5500b062011-05-19 14:46:19 +01002668 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2669 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002670} \
Peter Maydell5500b062011-05-19 14:46:19 +01002671uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2672 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002673{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002674 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002675 float##fsz tmp; \
2676 if (float##fsz##_is_any_nan(x)) { \
Peter Maydell5500b062011-05-19 14:46:19 +01002677 float_raise(float_flag_invalid, fpst); \
Peter Maydell622465e2011-03-14 07:23:11 +00002678 return 0; \
Peter Maydell09d94872010-12-07 15:37:34 +00002679 } \
Peter Maydell5500b062011-05-19 14:46:19 +01002680 tmp = float##fsz##_scalbn(x, shift, fpst); \
2681 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002682}
2683
Peter Maydell622465e2011-03-14 07:23:11 +00002684VFP_CONV_FIX(sh, d, 64, int16, )
2685VFP_CONV_FIX(sl, d, 64, int32, )
2686VFP_CONV_FIX(uh, d, 64, uint16, u)
2687VFP_CONV_FIX(ul, d, 64, uint32, u)
2688VFP_CONV_FIX(sh, s, 32, int16, )
2689VFP_CONV_FIX(sl, s, 32, int32, )
2690VFP_CONV_FIX(uh, s, 32, uint16, u)
2691VFP_CONV_FIX(ul, s, 32, uint32, u)
pbrook4373f3c2008-03-31 03:47:19 +00002692#undef VFP_CONV_FIX
2693
Paul Brook60011492009-11-19 16:45:20 +00002694/* Half precision conversions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002695static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002696{
Paul Brook60011492009-11-19 16:45:20 +00002697 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002698 float32 r = float16_to_float32(make_float16(a), ieee, s);
2699 if (ieee) {
2700 return float32_maybe_silence_nan(r);
2701 }
2702 return r;
Paul Brook60011492009-11-19 16:45:20 +00002703}
2704
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002705static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002706{
Paul Brook60011492009-11-19 16:45:20 +00002707 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002708 float16 r = float32_to_float16(a, ieee, s);
2709 if (ieee) {
2710 r = float16_maybe_silence_nan(r);
2711 }
2712 return float16_val(r);
Paul Brook60011492009-11-19 16:45:20 +00002713}
2714
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002715float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002716{
2717 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2718}
2719
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002720uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002721{
2722 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2723}
2724
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002725float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002726{
2727 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2728}
2729
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002730uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002731{
2732 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2733}
2734
Peter Maydelldda3ec42011-03-14 15:37:12 +00002735#define float32_two make_float32(0x40000000)
Peter Maydell6aae3df2011-03-14 15:37:13 +00002736#define float32_three make_float32(0x40400000)
2737#define float32_one_point_five make_float32(0x3fc00000)
Peter Maydelldda3ec42011-03-14 15:37:12 +00002738
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002739float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002740{
Peter Maydelldda3ec42011-03-14 15:37:12 +00002741 float_status *s = &env->vfp.standard_fp_status;
2742 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2743 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002744 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2745 float_raise(float_flag_input_denormal, s);
2746 }
Peter Maydelldda3ec42011-03-14 15:37:12 +00002747 return float32_two;
2748 }
2749 return float32_sub(float32_two, float32_mul(a, b, s), s);
pbrook4373f3c2008-03-31 03:47:19 +00002750}
2751
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002752float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002753{
Peter Maydell71826962011-01-14 20:39:18 +01002754 float_status *s = &env->vfp.standard_fp_status;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002755 float32 product;
2756 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2757 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002758 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2759 float_raise(float_flag_input_denormal, s);
2760 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002761 return float32_one_point_five;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002762 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002763 product = float32_mul(a, b, s);
2764 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
pbrook4373f3c2008-03-31 03:47:19 +00002765}
2766
pbrook8f8e3aa2008-03-31 03:48:01 +00002767/* NEON helpers. */
2768
Christophe Lyon56bf4fe2011-02-21 17:38:46 +01002769/* Constants 256 and 512 are used in some helpers; we avoid relying on
2770 * int->float conversions at run-time. */
2771#define float64_256 make_float64(0x4070000000000000LL)
2772#define float64_512 make_float64(0x4080000000000000LL)
2773
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002774/* The algorithm that must be used to calculate the estimate
2775 * is specified by the ARM ARM.
2776 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002777static float64 recip_estimate(float64 a, CPUARMState *env)
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002778{
Peter Maydell1146a812011-05-19 14:46:14 +01002779 /* These calculations mustn't set any fp exception flags,
2780 * so we use a local copy of the fp_status.
2781 */
2782 float_status dummy_status = env->vfp.standard_fp_status;
2783 float_status *s = &dummy_status;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002784 /* q = (int)(a * 512.0) */
2785 float64 q = float64_mul(float64_512, a, s);
2786 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2787
2788 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2789 q = int64_to_float64(q_int, s);
2790 q = float64_add(q, float64_half, s);
2791 q = float64_div(q, float64_512, s);
2792 q = float64_div(float64_one, q, s);
2793
2794 /* s = (int)(256.0 * r + 0.5) */
2795 q = float64_mul(q, float64_256, s);
2796 q = float64_add(q, float64_half, s);
2797 q_int = float64_to_int64_round_to_zero(q, s);
2798
2799 /* return (double)s / 256.0 */
2800 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2801}
2802
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002803float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002804{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002805 float_status *s = &env->vfp.standard_fp_status;
2806 float64 f64;
2807 uint32_t val32 = float32_val(a);
2808
2809 int result_exp;
2810 int a_exp = (val32 & 0x7f800000) >> 23;
2811 int sign = val32 & 0x80000000;
2812
2813 if (float32_is_any_nan(a)) {
2814 if (float32_is_signaling_nan(a)) {
2815 float_raise(float_flag_invalid, s);
2816 }
2817 return float32_default_nan;
2818 } else if (float32_is_infinity(a)) {
2819 return float32_set_sign(float32_zero, float32_is_neg(a));
2820 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002821 if (!float32_is_zero(a)) {
2822 float_raise(float_flag_input_denormal, s);
2823 }
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002824 float_raise(float_flag_divbyzero, s);
2825 return float32_set_sign(float32_infinity, float32_is_neg(a));
2826 } else if (a_exp >= 253) {
2827 float_raise(float_flag_underflow, s);
2828 return float32_set_sign(float32_zero, float32_is_neg(a));
2829 }
2830
2831 f64 = make_float64((0x3feULL << 52)
2832 | ((int64_t)(val32 & 0x7fffff) << 29));
2833
2834 result_exp = 253 - a_exp;
2835
2836 f64 = recip_estimate(f64, env);
2837
2838 val32 = sign
2839 | ((result_exp & 0xff) << 23)
2840 | ((float64_val(f64) >> 29) & 0x7fffff);
2841 return make_float32(val32);
pbrook4373f3c2008-03-31 03:47:19 +00002842}
2843
Christophe Lyone07be5d2011-02-21 17:38:48 +01002844/* The algorithm that must be used to calculate the estimate
2845 * is specified by the ARM ARM.
2846 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002847static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
Christophe Lyone07be5d2011-02-21 17:38:48 +01002848{
Peter Maydell1146a812011-05-19 14:46:14 +01002849 /* These calculations mustn't set any fp exception flags,
2850 * so we use a local copy of the fp_status.
2851 */
2852 float_status dummy_status = env->vfp.standard_fp_status;
2853 float_status *s = &dummy_status;
Christophe Lyone07be5d2011-02-21 17:38:48 +01002854 float64 q;
2855 int64_t q_int;
2856
2857 if (float64_lt(a, float64_half, s)) {
2858 /* range 0.25 <= a < 0.5 */
2859
2860 /* a in units of 1/512 rounded down */
2861 /* q0 = (int)(a * 512.0); */
2862 q = float64_mul(float64_512, a, s);
2863 q_int = float64_to_int64_round_to_zero(q, s);
2864
2865 /* reciprocal root r */
2866 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2867 q = int64_to_float64(q_int, s);
2868 q = float64_add(q, float64_half, s);
2869 q = float64_div(q, float64_512, s);
2870 q = float64_sqrt(q, s);
2871 q = float64_div(float64_one, q, s);
2872 } else {
2873 /* range 0.5 <= a < 1.0 */
2874
2875 /* a in units of 1/256 rounded down */
2876 /* q1 = (int)(a * 256.0); */
2877 q = float64_mul(float64_256, a, s);
2878 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2879
2880 /* reciprocal root r */
2881 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2882 q = int64_to_float64(q_int, s);
2883 q = float64_add(q, float64_half, s);
2884 q = float64_div(q, float64_256, s);
2885 q = float64_sqrt(q, s);
2886 q = float64_div(float64_one, q, s);
2887 }
2888 /* r in units of 1/256 rounded to nearest */
2889 /* s = (int)(256.0 * r + 0.5); */
2890
2891 q = float64_mul(q, float64_256,s );
2892 q = float64_add(q, float64_half, s);
2893 q_int = float64_to_int64_round_to_zero(q, s);
2894
2895 /* return (double)s / 256.0;*/
2896 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2897}
2898
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002899float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002900{
Christophe Lyone07be5d2011-02-21 17:38:48 +01002901 float_status *s = &env->vfp.standard_fp_status;
2902 int result_exp;
2903 float64 f64;
2904 uint32_t val;
2905 uint64_t val64;
2906
2907 val = float32_val(a);
2908
2909 if (float32_is_any_nan(a)) {
2910 if (float32_is_signaling_nan(a)) {
2911 float_raise(float_flag_invalid, s);
2912 }
2913 return float32_default_nan;
2914 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002915 if (!float32_is_zero(a)) {
2916 float_raise(float_flag_input_denormal, s);
2917 }
Christophe Lyone07be5d2011-02-21 17:38:48 +01002918 float_raise(float_flag_divbyzero, s);
2919 return float32_set_sign(float32_infinity, float32_is_neg(a));
2920 } else if (float32_is_neg(a)) {
2921 float_raise(float_flag_invalid, s);
2922 return float32_default_nan;
2923 } else if (float32_is_infinity(a)) {
2924 return float32_zero;
2925 }
2926
2927 /* Normalize to a double-precision value between 0.25 and 1.0,
2928 * preserving the parity of the exponent. */
2929 if ((val & 0x800000) == 0) {
2930 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
2931 | (0x3feULL << 52)
2932 | ((uint64_t)(val & 0x7fffff) << 29));
2933 } else {
2934 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
2935 | (0x3fdULL << 52)
2936 | ((uint64_t)(val & 0x7fffff) << 29));
2937 }
2938
2939 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
2940
2941 f64 = recip_sqrt_estimate(f64, env);
2942
2943 val64 = float64_val(f64);
2944
Christophe LYON26cc6ab2011-10-19 16:14:05 +00002945 val = ((result_exp & 0xff) << 23)
Christophe Lyone07be5d2011-02-21 17:38:48 +01002946 | ((val64 >> 29) & 0x7fffff);
2947 return make_float32(val);
pbrook4373f3c2008-03-31 03:47:19 +00002948}
2949
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002950uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002951{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002952 float64 f64;
2953
2954 if ((a & 0x80000000) == 0) {
2955 return 0xffffffff;
2956 }
2957
2958 f64 = make_float64((0x3feULL << 52)
2959 | ((int64_t)(a & 0x7fffffff) << 21));
2960
2961 f64 = recip_estimate (f64, env);
2962
2963 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00002964}
2965
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002966uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002967{
Christophe Lyone07be5d2011-02-21 17:38:48 +01002968 float64 f64;
2969
2970 if ((a & 0xc0000000) == 0) {
2971 return 0xffffffff;
2972 }
2973
2974 if (a & 0x80000000) {
2975 f64 = make_float64((0x3feULL << 52)
2976 | ((uint64_t)(a & 0x7fffffff) << 21));
2977 } else { /* bits 31-30 == '01' */
2978 f64 = make_float64((0x3fdULL << 52)
2979 | ((uint64_t)(a & 0x3fffffff) << 22));
2980 }
2981
2982 f64 = recip_sqrt_estimate(f64, env);
2983
2984 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00002985}
pbrookfe1479c2008-12-19 13:18:36 +00002986
Peter Maydellda97f522011-10-19 16:14:07 +00002987/* VFPv4 fused multiply-accumulate */
2988float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
2989{
2990 float_status *fpst = fpstp;
2991 return float32_muladd(a, b, c, 0, fpst);
2992}
2993
2994float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
2995{
2996 float_status *fpst = fpstp;
2997 return float64_muladd(a, b, c, 0, fpst);
2998}