]> git.proxmox.com Git - qemu.git/blame - target-arm/helper.c
softfloat: Add float32_is_zero_or_denormal() function
[qemu.git] / target-arm / helper.c
CommitLineData
b5ff1b31
FB
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "cpu.h"
6#include "exec-all.h"
9ee6e8bb 7#include "gdbstub.h"
b26eefb6 8#include "helpers.h"
ca10f867 9#include "qemu-common.h"
7bbcb0af 10#include "host-utils.h"
4f78c9ad 11#if !defined(CONFIG_USER_ONLY)
983fe826 12#include "hw/loader.h"
4f78c9ad 13#endif
9ee6e8bb 14
10055562
PB
15static uint32_t cortexa9_cp15_c0_c1[8] =
16{ 0x1031, 0x11, 0x000, 0, 0x00100103, 0x20000000, 0x01230000, 0x00002111 };
17
18static uint32_t cortexa9_cp15_c0_c2[8] =
19{ 0x00101111, 0x13112111, 0x21232041, 0x11112131, 0x00111142, 0, 0, 0 };
20
9ee6e8bb
PB
21static uint32_t cortexa8_cp15_c0_c1[8] =
22{ 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 };
23
24static uint32_t cortexa8_cp15_c0_c2[8] =
25{ 0x00101111, 0x12112111, 0x21232031, 0x11112131, 0x00111142, 0, 0, 0 };
26
27static uint32_t mpcore_cp15_c0_c1[8] =
28{ 0x111, 0x1, 0, 0x2, 0x01100103, 0x10020302, 0x01222000, 0 };
29
30static uint32_t mpcore_cp15_c0_c2[8] =
31{ 0x00100011, 0x12002111, 0x11221011, 0x01102131, 0x141, 0, 0, 0 };
32
33static uint32_t arm1136_cp15_c0_c1[8] =
34{ 0x111, 0x1, 0x2, 0x3, 0x01130003, 0x10030302, 0x01222110, 0 };
35
36static uint32_t arm1136_cp15_c0_c2[8] =
37{ 0x00140011, 0x12002111, 0x11231111, 0x01102131, 0x141, 0, 0, 0 };
b5ff1b31 38
aaed909a
FB
39static uint32_t cpu_arm_find_by_name(const char *name);
40
f3d6b95e
PB
41static inline void set_feature(CPUARMState *env, int feature)
42{
43 env->features |= 1u << feature;
44}
45
46static void cpu_reset_model_id(CPUARMState *env, uint32_t id)
47{
48 env->cp15.c0_cpuid = id;
49 switch (id) {
50 case ARM_CPUID_ARM926:
51 set_feature(env, ARM_FEATURE_VFP);
52 env->vfp.xregs[ARM_VFP_FPSID] = 0x41011090;
c1713132 53 env->cp15.c0_cachetype = 0x1dd20d2;
610c3c8a 54 env->cp15.c1_sys = 0x00090078;
f3d6b95e 55 break;
ce819861
PB
56 case ARM_CPUID_ARM946:
57 set_feature(env, ARM_FEATURE_MPU);
58 env->cp15.c0_cachetype = 0x0f004006;
610c3c8a 59 env->cp15.c1_sys = 0x00000078;
ce819861 60 break;
f3d6b95e
PB
61 case ARM_CPUID_ARM1026:
62 set_feature(env, ARM_FEATURE_VFP);
63 set_feature(env, ARM_FEATURE_AUXCR);
64 env->vfp.xregs[ARM_VFP_FPSID] = 0x410110a0;
c1713132 65 env->cp15.c0_cachetype = 0x1dd20d2;
610c3c8a 66 env->cp15.c1_sys = 0x00090078;
c1713132 67 break;
827df9f3 68 case ARM_CPUID_ARM1136_R2:
9ee6e8bb
PB
69 case ARM_CPUID_ARM1136:
70 set_feature(env, ARM_FEATURE_V6);
71 set_feature(env, ARM_FEATURE_VFP);
72 set_feature(env, ARM_FEATURE_AUXCR);
73 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
74 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
75 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
76 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c1, 8 * sizeof(uint32_t));
22478e79 77 memcpy(env->cp15.c0_c2, arm1136_cp15_c0_c2, 8 * sizeof(uint32_t));
9ee6e8bb 78 env->cp15.c0_cachetype = 0x1dd20d2;
16440c5f 79 env->cp15.c1_sys = 0x00050078;
9ee6e8bb
PB
80 break;
81 case ARM_CPUID_ARM11MPCORE:
82 set_feature(env, ARM_FEATURE_V6);
83 set_feature(env, ARM_FEATURE_V6K);
84 set_feature(env, ARM_FEATURE_VFP);
85 set_feature(env, ARM_FEATURE_AUXCR);
86 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
87 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
88 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
89 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c1, 8 * sizeof(uint32_t));
22478e79 90 memcpy(env->cp15.c0_c2, mpcore_cp15_c0_c2, 8 * sizeof(uint32_t));
9ee6e8bb
PB
91 env->cp15.c0_cachetype = 0x1dd20d2;
92 break;
93 case ARM_CPUID_CORTEXA8:
94 set_feature(env, ARM_FEATURE_V6);
95 set_feature(env, ARM_FEATURE_V6K);
96 set_feature(env, ARM_FEATURE_V7);
97 set_feature(env, ARM_FEATURE_AUXCR);
98 set_feature(env, ARM_FEATURE_THUMB2);
99 set_feature(env, ARM_FEATURE_VFP);
100 set_feature(env, ARM_FEATURE_VFP3);
101 set_feature(env, ARM_FEATURE_NEON);
fe1479c3 102 set_feature(env, ARM_FEATURE_THUMB2EE);
9ee6e8bb
PB
103 env->vfp.xregs[ARM_VFP_FPSID] = 0x410330c0;
104 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
105 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00011100;
106 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c1, 8 * sizeof(uint32_t));
22478e79 107 memcpy(env->cp15.c0_c2, cortexa8_cp15_c0_c2, 8 * sizeof(uint32_t));
a49ea279
PB
108 env->cp15.c0_cachetype = 0x82048004;
109 env->cp15.c0_clid = (1 << 27) | (2 << 24) | 3;
110 env->cp15.c0_ccsid[0] = 0xe007e01a; /* 16k L1 dcache. */
111 env->cp15.c0_ccsid[1] = 0x2007e01a; /* 16k L1 icache. */
112 env->cp15.c0_ccsid[2] = 0xf0000000; /* No L2 icache. */
9c486ad6 113 env->cp15.c1_sys = 0x00c50078;
9ee6e8bb 114 break;
10055562
PB
115 case ARM_CPUID_CORTEXA9:
116 set_feature(env, ARM_FEATURE_V6);
117 set_feature(env, ARM_FEATURE_V6K);
118 set_feature(env, ARM_FEATURE_V7);
119 set_feature(env, ARM_FEATURE_AUXCR);
120 set_feature(env, ARM_FEATURE_THUMB2);
121 set_feature(env, ARM_FEATURE_VFP);
122 set_feature(env, ARM_FEATURE_VFP3);
123 set_feature(env, ARM_FEATURE_VFP_FP16);
124 set_feature(env, ARM_FEATURE_NEON);
125 set_feature(env, ARM_FEATURE_THUMB2EE);
126 env->vfp.xregs[ARM_VFP_FPSID] = 0x41034000; /* Guess */
127 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
128 env->vfp.xregs[ARM_VFP_MVFR1] = 0x01111111;
129 memcpy(env->cp15.c0_c1, cortexa9_cp15_c0_c1, 8 * sizeof(uint32_t));
130 memcpy(env->cp15.c0_c2, cortexa9_cp15_c0_c2, 8 * sizeof(uint32_t));
131 env->cp15.c0_cachetype = 0x80038003;
132 env->cp15.c0_clid = (1 << 27) | (1 << 24) | 3;
133 env->cp15.c0_ccsid[0] = 0xe00fe015; /* 16k L1 dcache. */
134 env->cp15.c0_ccsid[1] = 0x200fe015; /* 16k L1 icache. */
16440c5f 135 env->cp15.c1_sys = 0x00c50078;
10055562 136 break;
9ee6e8bb
PB
137 case ARM_CPUID_CORTEXM3:
138 set_feature(env, ARM_FEATURE_V6);
139 set_feature(env, ARM_FEATURE_THUMB2);
140 set_feature(env, ARM_FEATURE_V7);
141 set_feature(env, ARM_FEATURE_M);
142 set_feature(env, ARM_FEATURE_DIV);
143 break;
144 case ARM_CPUID_ANY: /* For userspace emulation. */
145 set_feature(env, ARM_FEATURE_V6);
146 set_feature(env, ARM_FEATURE_V6K);
147 set_feature(env, ARM_FEATURE_V7);
148 set_feature(env, ARM_FEATURE_THUMB2);
149 set_feature(env, ARM_FEATURE_VFP);
150 set_feature(env, ARM_FEATURE_VFP3);
60011498 151 set_feature(env, ARM_FEATURE_VFP_FP16);
9ee6e8bb 152 set_feature(env, ARM_FEATURE_NEON);
fe1479c3 153 set_feature(env, ARM_FEATURE_THUMB2EE);
9ee6e8bb
PB
154 set_feature(env, ARM_FEATURE_DIV);
155 break;
c3d2689d
AZ
156 case ARM_CPUID_TI915T:
157 case ARM_CPUID_TI925T:
158 set_feature(env, ARM_FEATURE_OMAPCP);
159 env->cp15.c0_cpuid = ARM_CPUID_TI925T; /* Depends on wiring. */
160 env->cp15.c0_cachetype = 0x5109149;
161 env->cp15.c1_sys = 0x00000070;
162 env->cp15.c15_i_max = 0x000;
163 env->cp15.c15_i_min = 0xff0;
164 break;
c1713132
AZ
165 case ARM_CPUID_PXA250:
166 case ARM_CPUID_PXA255:
167 case ARM_CPUID_PXA260:
168 case ARM_CPUID_PXA261:
169 case ARM_CPUID_PXA262:
170 set_feature(env, ARM_FEATURE_XSCALE);
171 /* JTAG_ID is ((id << 28) | 0x09265013) */
172 env->cp15.c0_cachetype = 0xd172172;
610c3c8a 173 env->cp15.c1_sys = 0x00000078;
c1713132
AZ
174 break;
175 case ARM_CPUID_PXA270_A0:
176 case ARM_CPUID_PXA270_A1:
177 case ARM_CPUID_PXA270_B0:
178 case ARM_CPUID_PXA270_B1:
179 case ARM_CPUID_PXA270_C0:
180 case ARM_CPUID_PXA270_C5:
181 set_feature(env, ARM_FEATURE_XSCALE);
182 /* JTAG_ID is ((id << 28) | 0x09265013) */
18c9b560
AZ
183 set_feature(env, ARM_FEATURE_IWMMXT);
184 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
c1713132 185 env->cp15.c0_cachetype = 0xd172172;
610c3c8a 186 env->cp15.c1_sys = 0x00000078;
f3d6b95e
PB
187 break;
188 default:
189 cpu_abort(env, "Bad CPU ID: %x\n", id);
190 break;
191 }
192}
193
40f137e1
PB
194void cpu_reset(CPUARMState *env)
195{
f3d6b95e 196 uint32_t id;
eca1bdf4
AL
197
198 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
199 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
200 log_cpu_state(env, 0);
201 }
202
f3d6b95e
PB
203 id = env->cp15.c0_cpuid;
204 memset(env, 0, offsetof(CPUARMState, breakpoints));
205 if (id)
206 cpu_reset_model_id(env, id);
40f137e1
PB
207#if defined (CONFIG_USER_ONLY)
208 env->uncached_cpsr = ARM_CPU_MODE_USR;
3a807dec 209 /* For user mode we must enable access to coprocessors */
40f137e1 210 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
3a807dec
PM
211 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
212 env->cp15.c15_cpar = 3;
213 } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
214 env->cp15.c15_cpar = 1;
215 }
40f137e1
PB
216#else
217 /* SVC mode with interrupts disabled. */
218 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
9ee6e8bb 219 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
983fe826
PB
220 clear at reset. Initial SP and PC are loaded from ROM. */
221 if (IS_M(env)) {
222 uint32_t pc;
223 uint8_t *rom;
9ee6e8bb 224 env->uncached_cpsr &= ~CPSR_I;
983fe826
PB
225 rom = rom_ptr(0);
226 if (rom) {
227 /* We should really use ldl_phys here, in case the guest
228 modified flash and reset itself. However images
229 loaded via -kenrel have not been copied yet, so load the
230 values directly from there. */
231 env->regs[13] = ldl_p(rom);
232 pc = ldl_p(rom + 4);
233 env->thumb = pc & 1;
234 env->regs[15] = pc & ~1;
235 }
236 }
40f137e1 237 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
b2fa1797 238 env->cp15.c2_base_mask = 0xffffc000u;
40f137e1 239#endif
f3d6b95e 240 tlb_flush(env, 1);
40f137e1
PB
241}
242
56aebc89
PB
243static int vfp_gdb_get_reg(CPUState *env, uint8_t *buf, int reg)
244{
245 int nregs;
246
247 /* VFP data registers are always little-endian. */
248 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
249 if (reg < nregs) {
250 stfq_le_p(buf, env->vfp.regs[reg]);
251 return 8;
252 }
253 if (arm_feature(env, ARM_FEATURE_NEON)) {
254 /* Aliases for Q regs. */
255 nregs += 16;
256 if (reg < nregs) {
257 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
258 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
259 return 16;
260 }
261 }
262 switch (reg - nregs) {
263 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
264 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
265 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
266 }
267 return 0;
268}
269
270static int vfp_gdb_set_reg(CPUState *env, uint8_t *buf, int reg)
271{
272 int nregs;
273
274 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
275 if (reg < nregs) {
276 env->vfp.regs[reg] = ldfq_le_p(buf);
277 return 8;
278 }
279 if (arm_feature(env, ARM_FEATURE_NEON)) {
280 nregs += 16;
281 if (reg < nregs) {
282 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
283 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
284 return 16;
285 }
286 }
287 switch (reg - nregs) {
288 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
289 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
71b3c3de 290 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
56aebc89
PB
291 }
292 return 0;
293}
294
aaed909a 295CPUARMState *cpu_arm_init(const char *cpu_model)
40f137e1
PB
296{
297 CPUARMState *env;
aaed909a 298 uint32_t id;
b26eefb6 299 static int inited = 0;
40f137e1 300
aaed909a
FB
301 id = cpu_arm_find_by_name(cpu_model);
302 if (id == 0)
303 return NULL;
40f137e1 304 env = qemu_mallocz(sizeof(CPUARMState));
40f137e1 305 cpu_exec_init(env);
b26eefb6
PB
306 if (!inited) {
307 inited = 1;
308 arm_translate_init();
309 }
310
01ba9816 311 env->cpu_model_str = cpu_model;
aaed909a 312 env->cp15.c0_cpuid = id;
40f137e1 313 cpu_reset(env);
56aebc89
PB
314 if (arm_feature(env, ARM_FEATURE_NEON)) {
315 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
316 51, "arm-neon.xml", 0);
317 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
318 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
319 35, "arm-vfp3.xml", 0);
320 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
321 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
322 19, "arm-vfp.xml", 0);
323 }
0bf46a40 324 qemu_init_vcpu(env);
40f137e1
PB
325 return env;
326}
327
3371d272
PB
328struct arm_cpu_t {
329 uint32_t id;
330 const char *name;
331};
332
333static const struct arm_cpu_t arm_cpu_names[] = {
334 { ARM_CPUID_ARM926, "arm926"},
ce819861 335 { ARM_CPUID_ARM946, "arm946"},
3371d272 336 { ARM_CPUID_ARM1026, "arm1026"},
9ee6e8bb 337 { ARM_CPUID_ARM1136, "arm1136"},
827df9f3 338 { ARM_CPUID_ARM1136_R2, "arm1136-r2"},
9ee6e8bb
PB
339 { ARM_CPUID_ARM11MPCORE, "arm11mpcore"},
340 { ARM_CPUID_CORTEXM3, "cortex-m3"},
341 { ARM_CPUID_CORTEXA8, "cortex-a8"},
10055562 342 { ARM_CPUID_CORTEXA9, "cortex-a9"},
c3d2689d 343 { ARM_CPUID_TI925T, "ti925t" },
c1713132
AZ
344 { ARM_CPUID_PXA250, "pxa250" },
345 { ARM_CPUID_PXA255, "pxa255" },
346 { ARM_CPUID_PXA260, "pxa260" },
347 { ARM_CPUID_PXA261, "pxa261" },
348 { ARM_CPUID_PXA262, "pxa262" },
349 { ARM_CPUID_PXA270, "pxa270" },
350 { ARM_CPUID_PXA270_A0, "pxa270-a0" },
351 { ARM_CPUID_PXA270_A1, "pxa270-a1" },
352 { ARM_CPUID_PXA270_B0, "pxa270-b0" },
353 { ARM_CPUID_PXA270_B1, "pxa270-b1" },
354 { ARM_CPUID_PXA270_C0, "pxa270-c0" },
355 { ARM_CPUID_PXA270_C5, "pxa270-c5" },
9ee6e8bb 356 { ARM_CPUID_ANY, "any"},
3371d272
PB
357 { 0, NULL}
358};
359
9a78eead 360void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5adb4839
PB
361{
362 int i;
363
c732abe2 364 (*cpu_fprintf)(f, "Available CPUs:\n");
5adb4839 365 for (i = 0; arm_cpu_names[i].name; i++) {
c732abe2 366 (*cpu_fprintf)(f, " %s\n", arm_cpu_names[i].name);
5adb4839
PB
367 }
368}
369
aaed909a
FB
370/* return 0 if not found */
371static uint32_t cpu_arm_find_by_name(const char *name)
40f137e1 372{
3371d272
PB
373 int i;
374 uint32_t id;
375
376 id = 0;
3371d272
PB
377 for (i = 0; arm_cpu_names[i].name; i++) {
378 if (strcmp(name, arm_cpu_names[i].name) == 0) {
379 id = arm_cpu_names[i].id;
380 break;
381 }
382 }
aaed909a 383 return id;
40f137e1
PB
384}
385
386void cpu_arm_close(CPUARMState *env)
387{
388 free(env);
389}
390
2f4a40e5
AZ
391uint32_t cpsr_read(CPUARMState *env)
392{
393 int ZF;
6fbe23d5
PB
394 ZF = (env->ZF == 0);
395 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
396 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
397 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
398 | ((env->condexec_bits & 0xfc) << 8)
399 | (env->GE << 16);
400}
401
402void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
403{
2f4a40e5 404 if (mask & CPSR_NZCV) {
6fbe23d5
PB
405 env->ZF = (~val) & CPSR_Z;
406 env->NF = val;
2f4a40e5
AZ
407 env->CF = (val >> 29) & 1;
408 env->VF = (val << 3) & 0x80000000;
409 }
410 if (mask & CPSR_Q)
411 env->QF = ((val & CPSR_Q) != 0);
412 if (mask & CPSR_T)
413 env->thumb = ((val & CPSR_T) != 0);
414 if (mask & CPSR_IT_0_1) {
415 env->condexec_bits &= ~3;
416 env->condexec_bits |= (val >> 25) & 3;
417 }
418 if (mask & CPSR_IT_2_7) {
419 env->condexec_bits &= 3;
420 env->condexec_bits |= (val >> 8) & 0xfc;
421 }
422 if (mask & CPSR_GE) {
423 env->GE = (val >> 16) & 0xf;
424 }
425
426 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
427 switch_mode(env, val & CPSR_M);
428 }
429 mask &= ~CACHED_CPSR_BITS;
430 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
431}
432
b26eefb6
PB
433/* Sign/zero extend */
434uint32_t HELPER(sxtb16)(uint32_t x)
435{
436 uint32_t res;
437 res = (uint16_t)(int8_t)x;
438 res |= (uint32_t)(int8_t)(x >> 16) << 16;
439 return res;
440}
441
442uint32_t HELPER(uxtb16)(uint32_t x)
443{
444 uint32_t res;
445 res = (uint16_t)(uint8_t)x;
446 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
447 return res;
448}
449
f51bbbfe
PB
450uint32_t HELPER(clz)(uint32_t x)
451{
7bbcb0af 452 return clz32(x);
f51bbbfe
PB
453}
454
3670669c
PB
455int32_t HELPER(sdiv)(int32_t num, int32_t den)
456{
457 if (den == 0)
458 return 0;
686eeb93
AJ
459 if (num == INT_MIN && den == -1)
460 return INT_MIN;
3670669c
PB
461 return num / den;
462}
463
464uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
465{
466 if (den == 0)
467 return 0;
468 return num / den;
469}
470
471uint32_t HELPER(rbit)(uint32_t x)
472{
473 x = ((x & 0xff000000) >> 24)
474 | ((x & 0x00ff0000) >> 8)
475 | ((x & 0x0000ff00) << 8)
476 | ((x & 0x000000ff) << 24);
477 x = ((x & 0xf0f0f0f0) >> 4)
478 | ((x & 0x0f0f0f0f) << 4);
479 x = ((x & 0x88888888) >> 3)
480 | ((x & 0x44444444) >> 1)
481 | ((x & 0x22222222) << 1)
482 | ((x & 0x11111111) << 3);
483 return x;
484}
485
ad69471c
PB
486uint32_t HELPER(abs)(uint32_t x)
487{
488 return ((int32_t)x < 0) ? -x : x;
489}
490
5fafdf24 491#if defined(CONFIG_USER_ONLY)
b5ff1b31
FB
492
493void do_interrupt (CPUState *env)
494{
495 env->exception_index = -1;
496}
497
498int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
6ebbf390 499 int mmu_idx, int is_softmmu)
b5ff1b31
FB
500{
501 if (rw == 2) {
502 env->exception_index = EXCP_PREFETCH_ABORT;
503 env->cp15.c6_insn = address;
504 } else {
505 env->exception_index = EXCP_DATA_ABORT;
506 env->cp15.c6_data = address;
507 }
508 return 1;
509}
510
b5ff1b31 511/* These should probably raise undefined insn exceptions. */
8984bd2e 512void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
c1713132
AZ
513{
514 int op1 = (insn >> 8) & 0xf;
515 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
516 return;
517}
518
8984bd2e 519uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
c1713132
AZ
520{
521 int op1 = (insn >> 8) & 0xf;
522 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
523 return 0;
524}
525
8984bd2e 526void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
b5ff1b31
FB
527{
528 cpu_abort(env, "cp15 insn %08x\n", insn);
529}
530
8984bd2e 531uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
b5ff1b31
FB
532{
533 cpu_abort(env, "cp15 insn %08x\n", insn);
b5ff1b31
FB
534}
535
9ee6e8bb 536/* These should probably raise undefined insn exceptions. */
8984bd2e 537void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
9ee6e8bb
PB
538{
539 cpu_abort(env, "v7m_mrs %d\n", reg);
540}
541
8984bd2e 542uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
9ee6e8bb
PB
543{
544 cpu_abort(env, "v7m_mrs %d\n", reg);
545 return 0;
546}
547
b5ff1b31
FB
548void switch_mode(CPUState *env, int mode)
549{
550 if (mode != ARM_CPU_MODE_USR)
551 cpu_abort(env, "Tried to switch out of user mode\n");
552}
553
b0109805 554void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
9ee6e8bb
PB
555{
556 cpu_abort(env, "banked r13 write\n");
557}
558
b0109805 559uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
9ee6e8bb
PB
560{
561 cpu_abort(env, "banked r13 read\n");
562 return 0;
563}
564
b5ff1b31
FB
565#else
566
8e71621f
PB
567extern int semihosting_enabled;
568
b5ff1b31
FB
569/* Map CPU modes onto saved register banks. */
570static inline int bank_number (int mode)
571{
572 switch (mode) {
573 case ARM_CPU_MODE_USR:
574 case ARM_CPU_MODE_SYS:
575 return 0;
576 case ARM_CPU_MODE_SVC:
577 return 1;
578 case ARM_CPU_MODE_ABT:
579 return 2;
580 case ARM_CPU_MODE_UND:
581 return 3;
582 case ARM_CPU_MODE_IRQ:
583 return 4;
584 case ARM_CPU_MODE_FIQ:
585 return 5;
586 }
587 cpu_abort(cpu_single_env, "Bad mode %x\n", mode);
588 return -1;
589}
590
591void switch_mode(CPUState *env, int mode)
592{
593 int old_mode;
594 int i;
595
596 old_mode = env->uncached_cpsr & CPSR_M;
597 if (mode == old_mode)
598 return;
599
600 if (old_mode == ARM_CPU_MODE_FIQ) {
601 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 602 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
603 } else if (mode == ARM_CPU_MODE_FIQ) {
604 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 605 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
606 }
607
608 i = bank_number(old_mode);
609 env->banked_r13[i] = env->regs[13];
610 env->banked_r14[i] = env->regs[14];
611 env->banked_spsr[i] = env->spsr;
612
613 i = bank_number(mode);
614 env->regs[13] = env->banked_r13[i];
615 env->regs[14] = env->banked_r14[i];
616 env->spsr = env->banked_spsr[i];
617}
618
9ee6e8bb
PB
619static void v7m_push(CPUARMState *env, uint32_t val)
620{
621 env->regs[13] -= 4;
622 stl_phys(env->regs[13], val);
623}
624
625static uint32_t v7m_pop(CPUARMState *env)
626{
627 uint32_t val;
628 val = ldl_phys(env->regs[13]);
629 env->regs[13] += 4;
630 return val;
631}
632
633/* Switch to V7M main or process stack pointer. */
634static void switch_v7m_sp(CPUARMState *env, int process)
635{
636 uint32_t tmp;
637 if (env->v7m.current_sp != process) {
638 tmp = env->v7m.other_sp;
639 env->v7m.other_sp = env->regs[13];
640 env->regs[13] = tmp;
641 env->v7m.current_sp = process;
642 }
643}
644
645static void do_v7m_exception_exit(CPUARMState *env)
646{
647 uint32_t type;
648 uint32_t xpsr;
649
650 type = env->regs[15];
651 if (env->v7m.exception != 0)
983fe826 652 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
9ee6e8bb
PB
653
654 /* Switch to the target stack. */
655 switch_v7m_sp(env, (type & 4) != 0);
656 /* Pop registers. */
657 env->regs[0] = v7m_pop(env);
658 env->regs[1] = v7m_pop(env);
659 env->regs[2] = v7m_pop(env);
660 env->regs[3] = v7m_pop(env);
661 env->regs[12] = v7m_pop(env);
662 env->regs[14] = v7m_pop(env);
663 env->regs[15] = v7m_pop(env);
664 xpsr = v7m_pop(env);
665 xpsr_write(env, xpsr, 0xfffffdff);
666 /* Undo stack alignment. */
667 if (xpsr & 0x200)
668 env->regs[13] |= 4;
669 /* ??? The exception return type specifies Thread/Handler mode. However
670 this is also implied by the xPSR value. Not sure what to do
671 if there is a mismatch. */
672 /* ??? Likewise for mismatches between the CONTROL register and the stack
673 pointer. */
674}
675
2b3ea315 676static void do_interrupt_v7m(CPUARMState *env)
9ee6e8bb
PB
677{
678 uint32_t xpsr = xpsr_read(env);
679 uint32_t lr;
680 uint32_t addr;
681
682 lr = 0xfffffff1;
683 if (env->v7m.current_sp)
684 lr |= 4;
685 if (env->v7m.exception == 0)
686 lr |= 8;
687
688 /* For exceptions we just mark as pending on the NVIC, and let that
689 handle it. */
690 /* TODO: Need to escalate if the current priority is higher than the
691 one we're raising. */
692 switch (env->exception_index) {
693 case EXCP_UDEF:
983fe826 694 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
9ee6e8bb
PB
695 return;
696 case EXCP_SWI:
697 env->regs[15] += 2;
983fe826 698 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
9ee6e8bb
PB
699 return;
700 case EXCP_PREFETCH_ABORT:
701 case EXCP_DATA_ABORT:
983fe826 702 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
9ee6e8bb
PB
703 return;
704 case EXCP_BKPT:
2ad207d4
PB
705 if (semihosting_enabled) {
706 int nr;
707 nr = lduw_code(env->regs[15]) & 0xff;
708 if (nr == 0xab) {
709 env->regs[15] += 2;
710 env->regs[0] = do_arm_semihosting(env);
711 return;
712 }
713 }
983fe826 714 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
9ee6e8bb
PB
715 return;
716 case EXCP_IRQ:
983fe826 717 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
9ee6e8bb
PB
718 break;
719 case EXCP_EXCEPTION_EXIT:
720 do_v7m_exception_exit(env);
721 return;
722 default:
723 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
724 return; /* Never happens. Keep compiler happy. */
725 }
726
727 /* Align stack pointer. */
728 /* ??? Should only do this if Configuration Control Register
729 STACKALIGN bit is set. */
730 if (env->regs[13] & 4) {
ab19b0ec 731 env->regs[13] -= 4;
9ee6e8bb
PB
732 xpsr |= 0x200;
733 }
6c95676b 734 /* Switch to the handler mode. */
9ee6e8bb
PB
735 v7m_push(env, xpsr);
736 v7m_push(env, env->regs[15]);
737 v7m_push(env, env->regs[14]);
738 v7m_push(env, env->regs[12]);
739 v7m_push(env, env->regs[3]);
740 v7m_push(env, env->regs[2]);
741 v7m_push(env, env->regs[1]);
742 v7m_push(env, env->regs[0]);
743 switch_v7m_sp(env, 0);
744 env->uncached_cpsr &= ~CPSR_IT;
745 env->regs[14] = lr;
746 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
747 env->regs[15] = addr & 0xfffffffe;
748 env->thumb = addr & 1;
749}
750
b5ff1b31
FB
751/* Handle a CPU exception. */
752void do_interrupt(CPUARMState *env)
753{
754 uint32_t addr;
755 uint32_t mask;
756 int new_mode;
757 uint32_t offset;
758
9ee6e8bb
PB
759 if (IS_M(env)) {
760 do_interrupt_v7m(env);
761 return;
762 }
b5ff1b31
FB
763 /* TODO: Vectored interrupt controller. */
764 switch (env->exception_index) {
765 case EXCP_UDEF:
766 new_mode = ARM_CPU_MODE_UND;
767 addr = 0x04;
768 mask = CPSR_I;
769 if (env->thumb)
770 offset = 2;
771 else
772 offset = 4;
773 break;
774 case EXCP_SWI:
8e71621f
PB
775 if (semihosting_enabled) {
776 /* Check for semihosting interrupt. */
777 if (env->thumb) {
778 mask = lduw_code(env->regs[15] - 2) & 0xff;
779 } else {
780 mask = ldl_code(env->regs[15] - 4) & 0xffffff;
781 }
782 /* Only intercept calls from privileged modes, to provide some
783 semblance of security. */
784 if (((mask == 0x123456 && !env->thumb)
785 || (mask == 0xab && env->thumb))
786 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
787 env->regs[0] = do_arm_semihosting(env);
788 return;
789 }
790 }
b5ff1b31
FB
791 new_mode = ARM_CPU_MODE_SVC;
792 addr = 0x08;
793 mask = CPSR_I;
601d70b9 794 /* The PC already points to the next instruction. */
b5ff1b31
FB
795 offset = 0;
796 break;
06c949e6 797 case EXCP_BKPT:
9ee6e8bb 798 /* See if this is a semihosting syscall. */
2ad207d4 799 if (env->thumb && semihosting_enabled) {
9ee6e8bb
PB
800 mask = lduw_code(env->regs[15]) & 0xff;
801 if (mask == 0xab
802 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
803 env->regs[15] += 2;
804 env->regs[0] = do_arm_semihosting(env);
805 return;
806 }
807 }
808 /* Fall through to prefetch abort. */
809 case EXCP_PREFETCH_ABORT:
b5ff1b31
FB
810 new_mode = ARM_CPU_MODE_ABT;
811 addr = 0x0c;
812 mask = CPSR_A | CPSR_I;
813 offset = 4;
814 break;
815 case EXCP_DATA_ABORT:
816 new_mode = ARM_CPU_MODE_ABT;
817 addr = 0x10;
818 mask = CPSR_A | CPSR_I;
819 offset = 8;
820 break;
821 case EXCP_IRQ:
822 new_mode = ARM_CPU_MODE_IRQ;
823 addr = 0x18;
824 /* Disable IRQ and imprecise data aborts. */
825 mask = CPSR_A | CPSR_I;
826 offset = 4;
827 break;
828 case EXCP_FIQ:
829 new_mode = ARM_CPU_MODE_FIQ;
830 addr = 0x1c;
831 /* Disable FIQ, IRQ and imprecise data aborts. */
832 mask = CPSR_A | CPSR_I | CPSR_F;
833 offset = 4;
834 break;
835 default:
836 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
837 return; /* Never happens. Keep compiler happy. */
838 }
839 /* High vectors. */
840 if (env->cp15.c1_sys & (1 << 13)) {
841 addr += 0xffff0000;
842 }
843 switch_mode (env, new_mode);
844 env->spsr = cpsr_read(env);
9ee6e8bb
PB
845 /* Clear IT bits. */
846 env->condexec_bits = 0;
30a8cac1 847 /* Switch to the new mode, and to the correct instruction set. */
6d7e6326 848 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
b5ff1b31 849 env->uncached_cpsr |= mask;
30a8cac1 850 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
b5ff1b31
FB
851 env->regs[14] = env->regs[15] + offset;
852 env->regs[15] = addr;
853 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
854}
855
856/* Check section/page access permissions.
857 Returns the page protection flags, or zero if the access is not
858 permitted. */
859static inline int check_ap(CPUState *env, int ap, int domain, int access_type,
860 int is_user)
861{
9ee6e8bb
PB
862 int prot_ro;
863
b5ff1b31
FB
864 if (domain == 3)
865 return PAGE_READ | PAGE_WRITE;
866
9ee6e8bb
PB
867 if (access_type == 1)
868 prot_ro = 0;
869 else
870 prot_ro = PAGE_READ;
871
b5ff1b31
FB
872 switch (ap) {
873 case 0:
78600320 874 if (access_type == 1)
b5ff1b31
FB
875 return 0;
876 switch ((env->cp15.c1_sys >> 8) & 3) {
877 case 1:
878 return is_user ? 0 : PAGE_READ;
879 case 2:
880 return PAGE_READ;
881 default:
882 return 0;
883 }
884 case 1:
885 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
886 case 2:
887 if (is_user)
9ee6e8bb 888 return prot_ro;
b5ff1b31
FB
889 else
890 return PAGE_READ | PAGE_WRITE;
891 case 3:
892 return PAGE_READ | PAGE_WRITE;
d4934d18 893 case 4: /* Reserved. */
9ee6e8bb
PB
894 return 0;
895 case 5:
896 return is_user ? 0 : prot_ro;
897 case 6:
898 return prot_ro;
d4934d18
PB
899 case 7:
900 if (!arm_feature (env, ARM_FEATURE_V7))
901 return 0;
902 return prot_ro;
b5ff1b31
FB
903 default:
904 abort();
905 }
906}
907
b2fa1797
PB
908static uint32_t get_level1_table_address(CPUState *env, uint32_t address)
909{
910 uint32_t table;
911
912 if (address & env->cp15.c2_mask)
913 table = env->cp15.c2_base1 & 0xffffc000;
914 else
915 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
916
917 table |= (address >> 18) & 0x3ffc;
918 return table;
919}
920
9ee6e8bb 921static int get_phys_addr_v5(CPUState *env, uint32_t address, int access_type,
d4c430a8
PB
922 int is_user, uint32_t *phys_ptr, int *prot,
923 target_ulong *page_size)
b5ff1b31
FB
924{
925 int code;
926 uint32_t table;
927 uint32_t desc;
928 int type;
929 int ap;
930 int domain;
931 uint32_t phys_addr;
932
9ee6e8bb
PB
933 /* Pagetable walk. */
934 /* Lookup l1 descriptor. */
b2fa1797 935 table = get_level1_table_address(env, address);
9ee6e8bb
PB
936 desc = ldl_phys(table);
937 type = (desc & 3);
938 domain = (env->cp15.c3 >> ((desc >> 4) & 0x1e)) & 3;
939 if (type == 0) {
601d70b9 940 /* Section translation fault. */
9ee6e8bb
PB
941 code = 5;
942 goto do_fault;
943 }
944 if (domain == 0 || domain == 2) {
945 if (type == 2)
946 code = 9; /* Section domain fault. */
947 else
948 code = 11; /* Page domain fault. */
949 goto do_fault;
950 }
951 if (type == 2) {
952 /* 1Mb section. */
953 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
954 ap = (desc >> 10) & 3;
955 code = 13;
d4c430a8 956 *page_size = 1024 * 1024;
9ee6e8bb
PB
957 } else {
958 /* Lookup l2 entry. */
959 if (type == 1) {
960 /* Coarse pagetable. */
961 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
962 } else {
963 /* Fine pagetable. */
964 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
965 }
966 desc = ldl_phys(table);
967 switch (desc & 3) {
968 case 0: /* Page translation fault. */
969 code = 7;
970 goto do_fault;
971 case 1: /* 64k page. */
972 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
973 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 974 *page_size = 0x10000;
ce819861 975 break;
9ee6e8bb
PB
976 case 2: /* 4k page. */
977 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
978 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 979 *page_size = 0x1000;
ce819861 980 break;
9ee6e8bb
PB
981 case 3: /* 1k page. */
982 if (type == 1) {
983 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
984 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
985 } else {
986 /* Page translation fault. */
987 code = 7;
988 goto do_fault;
989 }
990 } else {
991 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
992 }
993 ap = (desc >> 4) & 3;
d4c430a8 994 *page_size = 0x400;
ce819861
PB
995 break;
996 default:
9ee6e8bb
PB
997 /* Never happens, but compiler isn't smart enough to tell. */
998 abort();
ce819861 999 }
9ee6e8bb
PB
1000 code = 15;
1001 }
1002 *prot = check_ap(env, ap, domain, access_type, is_user);
1003 if (!*prot) {
1004 /* Access permission fault. */
1005 goto do_fault;
1006 }
3ad493fc 1007 *prot |= PAGE_EXEC;
9ee6e8bb
PB
1008 *phys_ptr = phys_addr;
1009 return 0;
1010do_fault:
1011 return code | (domain << 4);
1012}
1013
1014static int get_phys_addr_v6(CPUState *env, uint32_t address, int access_type,
d4c430a8
PB
1015 int is_user, uint32_t *phys_ptr, int *prot,
1016 target_ulong *page_size)
9ee6e8bb
PB
1017{
1018 int code;
1019 uint32_t table;
1020 uint32_t desc;
1021 uint32_t xn;
1022 int type;
1023 int ap;
1024 int domain;
1025 uint32_t phys_addr;
1026
1027 /* Pagetable walk. */
1028 /* Lookup l1 descriptor. */
b2fa1797 1029 table = get_level1_table_address(env, address);
9ee6e8bb
PB
1030 desc = ldl_phys(table);
1031 type = (desc & 3);
1032 if (type == 0) {
601d70b9 1033 /* Section translation fault. */
9ee6e8bb
PB
1034 code = 5;
1035 domain = 0;
1036 goto do_fault;
1037 } else if (type == 2 && (desc & (1 << 18))) {
1038 /* Supersection. */
1039 domain = 0;
b5ff1b31 1040 } else {
9ee6e8bb
PB
1041 /* Section or page. */
1042 domain = (desc >> 4) & 0x1e;
1043 }
1044 domain = (env->cp15.c3 >> domain) & 3;
1045 if (domain == 0 || domain == 2) {
1046 if (type == 2)
1047 code = 9; /* Section domain fault. */
1048 else
1049 code = 11; /* Page domain fault. */
1050 goto do_fault;
1051 }
1052 if (type == 2) {
1053 if (desc & (1 << 18)) {
1054 /* Supersection. */
1055 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
d4c430a8 1056 *page_size = 0x1000000;
b5ff1b31 1057 } else {
9ee6e8bb
PB
1058 /* Section. */
1059 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 1060 *page_size = 0x100000;
b5ff1b31 1061 }
9ee6e8bb
PB
1062 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1063 xn = desc & (1 << 4);
1064 code = 13;
1065 } else {
1066 /* Lookup l2 entry. */
1067 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1068 desc = ldl_phys(table);
1069 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1070 switch (desc & 3) {
1071 case 0: /* Page translation fault. */
1072 code = 7;
b5ff1b31 1073 goto do_fault;
9ee6e8bb
PB
1074 case 1: /* 64k page. */
1075 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1076 xn = desc & (1 << 15);
d4c430a8 1077 *page_size = 0x10000;
9ee6e8bb
PB
1078 break;
1079 case 2: case 3: /* 4k page. */
1080 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1081 xn = desc & 1;
d4c430a8 1082 *page_size = 0x1000;
9ee6e8bb
PB
1083 break;
1084 default:
1085 /* Never happens, but compiler isn't smart enough to tell. */
1086 abort();
b5ff1b31 1087 }
9ee6e8bb
PB
1088 code = 15;
1089 }
c0034328
JR
1090 if (domain == 3) {
1091 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1092 } else {
1093 if (xn && access_type == 2)
1094 goto do_fault;
9ee6e8bb 1095
c0034328
JR
1096 /* The simplified model uses AP[0] as an access control bit. */
1097 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1098 /* Access flag fault. */
1099 code = (code == 15) ? 6 : 3;
1100 goto do_fault;
1101 }
1102 *prot = check_ap(env, ap, domain, access_type, is_user);
1103 if (!*prot) {
1104 /* Access permission fault. */
1105 goto do_fault;
1106 }
1107 if (!xn) {
1108 *prot |= PAGE_EXEC;
1109 }
3ad493fc 1110 }
9ee6e8bb 1111 *phys_ptr = phys_addr;
b5ff1b31
FB
1112 return 0;
1113do_fault:
1114 return code | (domain << 4);
1115}
1116
9ee6e8bb
PB
1117static int get_phys_addr_mpu(CPUState *env, uint32_t address, int access_type,
1118 int is_user, uint32_t *phys_ptr, int *prot)
1119{
1120 int n;
1121 uint32_t mask;
1122 uint32_t base;
1123
1124 *phys_ptr = address;
1125 for (n = 7; n >= 0; n--) {
1126 base = env->cp15.c6_region[n];
1127 if ((base & 1) == 0)
1128 continue;
1129 mask = 1 << ((base >> 1) & 0x1f);
1130 /* Keep this shift separate from the above to avoid an
1131 (undefined) << 32. */
1132 mask = (mask << 1) - 1;
1133 if (((base ^ address) & ~mask) == 0)
1134 break;
1135 }
1136 if (n < 0)
1137 return 2;
1138
1139 if (access_type == 2) {
1140 mask = env->cp15.c5_insn;
1141 } else {
1142 mask = env->cp15.c5_data;
1143 }
1144 mask = (mask >> (n * 4)) & 0xf;
1145 switch (mask) {
1146 case 0:
1147 return 1;
1148 case 1:
1149 if (is_user)
1150 return 1;
1151 *prot = PAGE_READ | PAGE_WRITE;
1152 break;
1153 case 2:
1154 *prot = PAGE_READ;
1155 if (!is_user)
1156 *prot |= PAGE_WRITE;
1157 break;
1158 case 3:
1159 *prot = PAGE_READ | PAGE_WRITE;
1160 break;
1161 case 5:
1162 if (is_user)
1163 return 1;
1164 *prot = PAGE_READ;
1165 break;
1166 case 6:
1167 *prot = PAGE_READ;
1168 break;
1169 default:
1170 /* Bad permission. */
1171 return 1;
1172 }
3ad493fc 1173 *prot |= PAGE_EXEC;
9ee6e8bb
PB
1174 return 0;
1175}
1176
1177static inline int get_phys_addr(CPUState *env, uint32_t address,
1178 int access_type, int is_user,
d4c430a8
PB
1179 uint32_t *phys_ptr, int *prot,
1180 target_ulong *page_size)
9ee6e8bb
PB
1181{
1182 /* Fast Context Switch Extension. */
1183 if (address < 0x02000000)
1184 address += env->cp15.c13_fcse;
1185
1186 if ((env->cp15.c1_sys & 1) == 0) {
1187 /* MMU/MPU disabled. */
1188 *phys_ptr = address;
3ad493fc 1189 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 1190 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb
PB
1191 return 0;
1192 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
d4c430a8 1193 *page_size = TARGET_PAGE_SIZE;
9ee6e8bb
PB
1194 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1195 prot);
1196 } else if (env->cp15.c1_sys & (1 << 23)) {
1197 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
d4c430a8 1198 prot, page_size);
9ee6e8bb
PB
1199 } else {
1200 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
d4c430a8 1201 prot, page_size);
9ee6e8bb
PB
1202 }
1203}
1204
b5ff1b31 1205int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address,
6ebbf390 1206 int access_type, int mmu_idx, int is_softmmu)
b5ff1b31
FB
1207{
1208 uint32_t phys_addr;
d4c430a8 1209 target_ulong page_size;
b5ff1b31 1210 int prot;
6ebbf390 1211 int ret, is_user;
b5ff1b31 1212
6ebbf390 1213 is_user = mmu_idx == MMU_USER_IDX;
d4c430a8
PB
1214 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1215 &page_size);
b5ff1b31
FB
1216 if (ret == 0) {
1217 /* Map a single [sub]page. */
1218 phys_addr &= ~(uint32_t)0x3ff;
1219 address &= ~(uint32_t)0x3ff;
3ad493fc 1220 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
d4c430a8 1221 return 0;
b5ff1b31
FB
1222 }
1223
1224 if (access_type == 2) {
1225 env->cp15.c5_insn = ret;
1226 env->cp15.c6_insn = address;
1227 env->exception_index = EXCP_PREFETCH_ABORT;
1228 } else {
1229 env->cp15.c5_data = ret;
9ee6e8bb
PB
1230 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1231 env->cp15.c5_data |= (1 << 11);
b5ff1b31
FB
1232 env->cp15.c6_data = address;
1233 env->exception_index = EXCP_DATA_ABORT;
1234 }
1235 return 1;
1236}
1237
c227f099 1238target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
b5ff1b31
FB
1239{
1240 uint32_t phys_addr;
d4c430a8 1241 target_ulong page_size;
b5ff1b31
FB
1242 int prot;
1243 int ret;
1244
d4c430a8 1245 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
b5ff1b31
FB
1246
1247 if (ret != 0)
1248 return -1;
1249
1250 return phys_addr;
1251}
1252
8984bd2e 1253void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
c1713132
AZ
1254{
1255 int cp_num = (insn >> 8) & 0xf;
1256 int cp_info = (insn >> 5) & 7;
1257 int src = (insn >> 16) & 0xf;
1258 int operand = insn & 0xf;
1259
1260 if (env->cp[cp_num].cp_write)
1261 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1262 cp_info, src, operand, val);
1263}
1264
8984bd2e 1265uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
c1713132
AZ
1266{
1267 int cp_num = (insn >> 8) & 0xf;
1268 int cp_info = (insn >> 5) & 7;
1269 int dest = (insn >> 16) & 0xf;
1270 int operand = insn & 0xf;
1271
1272 if (env->cp[cp_num].cp_read)
1273 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1274 cp_info, dest, operand);
1275 return 0;
1276}
1277
ce819861
PB
1278/* Return basic MPU access permission bits. */
1279static uint32_t simple_mpu_ap_bits(uint32_t val)
1280{
1281 uint32_t ret;
1282 uint32_t mask;
1283 int i;
1284 ret = 0;
1285 mask = 3;
1286 for (i = 0; i < 16; i += 2) {
1287 ret |= (val >> i) & mask;
1288 mask <<= 2;
1289 }
1290 return ret;
1291}
1292
1293/* Pad basic MPU access permission bits to extended format. */
1294static uint32_t extended_mpu_ap_bits(uint32_t val)
1295{
1296 uint32_t ret;
1297 uint32_t mask;
1298 int i;
1299 ret = 0;
1300 mask = 3;
1301 for (i = 0; i < 16; i += 2) {
1302 ret |= (val & mask) << i;
1303 mask <<= 2;
1304 }
1305 return ret;
1306}
1307
8984bd2e 1308void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
b5ff1b31 1309{
9ee6e8bb
PB
1310 int op1;
1311 int op2;
1312 int crm;
b5ff1b31 1313
9ee6e8bb 1314 op1 = (insn >> 21) & 7;
b5ff1b31 1315 op2 = (insn >> 5) & 7;
ce819861 1316 crm = insn & 0xf;
b5ff1b31 1317 switch ((insn >> 16) & 0xf) {
9ee6e8bb 1318 case 0:
9ee6e8bb 1319 /* ID codes. */
610c3c8a
AZ
1320 if (arm_feature(env, ARM_FEATURE_XSCALE))
1321 break;
c3d2689d
AZ
1322 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1323 break;
a49ea279
PB
1324 if (arm_feature(env, ARM_FEATURE_V7)
1325 && op1 == 2 && crm == 0 && op2 == 0) {
1326 env->cp15.c0_cssel = val & 0xf;
1327 break;
1328 }
b5ff1b31
FB
1329 goto bad_reg;
1330 case 1: /* System configuration. */
c3d2689d
AZ
1331 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1332 op2 = 0;
b5ff1b31
FB
1333 switch (op2) {
1334 case 0:
ce819861 1335 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
c1713132 1336 env->cp15.c1_sys = val;
b5ff1b31
FB
1337 /* ??? Lots of these bits are not implemented. */
1338 /* This may enable/disable the MMU, so do a TLB flush. */
1339 tlb_flush(env, 1);
1340 break;
9ee6e8bb 1341 case 1: /* Auxiliary cotrol register. */
610c3c8a
AZ
1342 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1343 env->cp15.c1_xscaleauxcr = val;
c1713132 1344 break;
610c3c8a 1345 }
9ee6e8bb
PB
1346 /* Not implemented. */
1347 break;
b5ff1b31 1348 case 2:
610c3c8a
AZ
1349 if (arm_feature(env, ARM_FEATURE_XSCALE))
1350 goto bad_reg;
4be27dbb
PB
1351 if (env->cp15.c1_coproc != val) {
1352 env->cp15.c1_coproc = val;
1353 /* ??? Is this safe when called from within a TB? */
1354 tb_flush(env);
1355 }
c1713132 1356 break;
b5ff1b31
FB
1357 default:
1358 goto bad_reg;
1359 }
1360 break;
ce819861
PB
1361 case 2: /* MMU Page table control / MPU cache control. */
1362 if (arm_feature(env, ARM_FEATURE_MPU)) {
1363 switch (op2) {
1364 case 0:
1365 env->cp15.c2_data = val;
1366 break;
1367 case 1:
1368 env->cp15.c2_insn = val;
1369 break;
1370 default:
1371 goto bad_reg;
1372 }
1373 } else {
9ee6e8bb
PB
1374 switch (op2) {
1375 case 0:
1376 env->cp15.c2_base0 = val;
1377 break;
1378 case 1:
1379 env->cp15.c2_base1 = val;
1380 break;
1381 case 2:
b2fa1797
PB
1382 val &= 7;
1383 env->cp15.c2_control = val;
9ee6e8bb 1384 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
b2fa1797 1385 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
9ee6e8bb
PB
1386 break;
1387 default:
1388 goto bad_reg;
1389 }
ce819861 1390 }
b5ff1b31 1391 break;
ce819861 1392 case 3: /* MMU Domain access control / MPU write buffer control. */
b5ff1b31 1393 env->cp15.c3 = val;
405ee3ad 1394 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
b5ff1b31
FB
1395 break;
1396 case 4: /* Reserved. */
1397 goto bad_reg;
ce819861 1398 case 5: /* MMU Fault status / MPU access permission. */
c3d2689d
AZ
1399 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1400 op2 = 0;
b5ff1b31
FB
1401 switch (op2) {
1402 case 0:
ce819861
PB
1403 if (arm_feature(env, ARM_FEATURE_MPU))
1404 val = extended_mpu_ap_bits(val);
b5ff1b31
FB
1405 env->cp15.c5_data = val;
1406 break;
1407 case 1:
ce819861
PB
1408 if (arm_feature(env, ARM_FEATURE_MPU))
1409 val = extended_mpu_ap_bits(val);
b5ff1b31
FB
1410 env->cp15.c5_insn = val;
1411 break;
ce819861
PB
1412 case 2:
1413 if (!arm_feature(env, ARM_FEATURE_MPU))
1414 goto bad_reg;
1415 env->cp15.c5_data = val;
b5ff1b31 1416 break;
ce819861
PB
1417 case 3:
1418 if (!arm_feature(env, ARM_FEATURE_MPU))
1419 goto bad_reg;
1420 env->cp15.c5_insn = val;
b5ff1b31
FB
1421 break;
1422 default:
1423 goto bad_reg;
1424 }
1425 break;
ce819861
PB
1426 case 6: /* MMU Fault address / MPU base/size. */
1427 if (arm_feature(env, ARM_FEATURE_MPU)) {
1428 if (crm >= 8)
1429 goto bad_reg;
1430 env->cp15.c6_region[crm] = val;
1431 } else {
c3d2689d
AZ
1432 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1433 op2 = 0;
ce819861
PB
1434 switch (op2) {
1435 case 0:
1436 env->cp15.c6_data = val;
1437 break;
9ee6e8bb
PB
1438 case 1: /* ??? This is WFAR on armv6 */
1439 case 2:
ce819861
PB
1440 env->cp15.c6_insn = val;
1441 break;
1442 default:
1443 goto bad_reg;
1444 }
1445 }
1446 break;
b5ff1b31 1447 case 7: /* Cache control. */
c3d2689d
AZ
1448 env->cp15.c15_i_max = 0x000;
1449 env->cp15.c15_i_min = 0xff0;
b5ff1b31 1450 /* No cache, so nothing to do. */
9ee6e8bb 1451 /* ??? MPCore has VA to PA translation functions. */
b5ff1b31
FB
1452 break;
1453 case 8: /* MMU TLB control. */
1454 switch (op2) {
1455 case 0: /* Invalidate all. */
1456 tlb_flush(env, 0);
1457 break;
1458 case 1: /* Invalidate single TLB entry. */
d4c430a8 1459 tlb_flush_page(env, val & TARGET_PAGE_MASK);
b5ff1b31 1460 break;
9ee6e8bb
PB
1461 case 2: /* Invalidate on ASID. */
1462 tlb_flush(env, val == 0);
1463 break;
1464 case 3: /* Invalidate single entry on MVA. */
1465 /* ??? This is like case 1, but ignores ASID. */
1466 tlb_flush(env, 1);
1467 break;
b5ff1b31
FB
1468 default:
1469 goto bad_reg;
1470 }
1471 break;
ce819861 1472 case 9:
c3d2689d
AZ
1473 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1474 break;
ce819861
PB
1475 switch (crm) {
1476 case 0: /* Cache lockdown. */
9ee6e8bb
PB
1477 switch (op1) {
1478 case 0: /* L1 cache. */
1479 switch (op2) {
1480 case 0:
1481 env->cp15.c9_data = val;
1482 break;
1483 case 1:
1484 env->cp15.c9_insn = val;
1485 break;
1486 default:
1487 goto bad_reg;
1488 }
1489 break;
1490 case 1: /* L2 cache. */
1491 /* Ignore writes to L2 lockdown/auxiliary registers. */
1492 break;
1493 default:
1494 goto bad_reg;
1495 }
1496 break;
ce819861
PB
1497 case 1: /* TCM memory region registers. */
1498 /* Not implemented. */
1499 goto bad_reg;
b5ff1b31
FB
1500 default:
1501 goto bad_reg;
1502 }
1503 break;
1504 case 10: /* MMU TLB lockdown. */
1505 /* ??? TLB lockdown not implemented. */
1506 break;
b5ff1b31
FB
1507 case 12: /* Reserved. */
1508 goto bad_reg;
1509 case 13: /* Process ID. */
1510 switch (op2) {
1511 case 0:
d07edbfa
PB
1512 /* Unlike real hardware the qemu TLB uses virtual addresses,
1513 not modified virtual addresses, so this causes a TLB flush.
1514 */
1515 if (env->cp15.c13_fcse != val)
1516 tlb_flush(env, 1);
1517 env->cp15.c13_fcse = val;
b5ff1b31
FB
1518 break;
1519 case 1:
d07edbfa 1520 /* This changes the ASID, so do a TLB flush. */
ce819861
PB
1521 if (env->cp15.c13_context != val
1522 && !arm_feature(env, ARM_FEATURE_MPU))
d07edbfa
PB
1523 tlb_flush(env, 0);
1524 env->cp15.c13_context = val;
b5ff1b31
FB
1525 break;
1526 default:
1527 goto bad_reg;
1528 }
1529 break;
1530 case 14: /* Reserved. */
1531 goto bad_reg;
1532 case 15: /* Implementation specific. */
c1713132 1533 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
ce819861 1534 if (op2 == 0 && crm == 1) {
2e23213f
AZ
1535 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1536 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1537 tb_flush(env);
1538 env->cp15.c15_cpar = val & 0x3fff;
1539 }
c1713132
AZ
1540 break;
1541 }
1542 goto bad_reg;
1543 }
c3d2689d
AZ
1544 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1545 switch (crm) {
1546 case 0:
1547 break;
1548 case 1: /* Set TI925T configuration. */
1549 env->cp15.c15_ticonfig = val & 0xe7;
1550 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1551 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1552 break;
1553 case 2: /* Set I_max. */
1554 env->cp15.c15_i_max = val;
1555 break;
1556 case 3: /* Set I_min. */
1557 env->cp15.c15_i_min = val;
1558 break;
1559 case 4: /* Set thread-ID. */
1560 env->cp15.c15_threadid = val & 0xffff;
1561 break;
1562 case 8: /* Wait-for-interrupt (deprecated). */
1563 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1564 break;
1565 default:
1566 goto bad_reg;
1567 }
1568 }
b5ff1b31
FB
1569 break;
1570 }
1571 return;
1572bad_reg:
1573 /* ??? For debugging only. Should raise illegal instruction exception. */
9ee6e8bb
PB
1574 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1575 (insn >> 16) & 0xf, crm, op1, op2);
b5ff1b31
FB
1576}
1577
8984bd2e 1578uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
b5ff1b31 1579{
9ee6e8bb
PB
1580 int op1;
1581 int op2;
1582 int crm;
b5ff1b31 1583
9ee6e8bb 1584 op1 = (insn >> 21) & 7;
b5ff1b31 1585 op2 = (insn >> 5) & 7;
c3d2689d 1586 crm = insn & 0xf;
b5ff1b31
FB
1587 switch ((insn >> 16) & 0xf) {
1588 case 0: /* ID codes. */
9ee6e8bb
PB
1589 switch (op1) {
1590 case 0:
1591 switch (crm) {
1592 case 0:
1593 switch (op2) {
1594 case 0: /* Device ID. */
1595 return env->cp15.c0_cpuid;
1596 case 1: /* Cache Type. */
1597 return env->cp15.c0_cachetype;
1598 case 2: /* TCM status. */
1599 return 0;
1600 case 3: /* TLB type register. */
1601 return 0; /* No lockable TLB entries. */
1602 case 5: /* CPU ID */
10055562
PB
1603 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1604 return env->cpu_index | 0x80000900;
1605 } else {
1606 return env->cpu_index;
1607 }
9ee6e8bb
PB
1608 default:
1609 goto bad_reg;
1610 }
1611 case 1:
1612 if (!arm_feature(env, ARM_FEATURE_V6))
1613 goto bad_reg;
1614 return env->cp15.c0_c1[op2];
1615 case 2:
1616 if (!arm_feature(env, ARM_FEATURE_V6))
1617 goto bad_reg;
1618 return env->cp15.c0_c2[op2];
1619 case 3: case 4: case 5: case 6: case 7:
1620 return 0;
1621 default:
1622 goto bad_reg;
1623 }
1624 case 1:
1625 /* These registers aren't documented on arm11 cores. However
1626 Linux looks at them anyway. */
1627 if (!arm_feature(env, ARM_FEATURE_V6))
1628 goto bad_reg;
1629 if (crm != 0)
1630 goto bad_reg;
a49ea279
PB
1631 if (!arm_feature(env, ARM_FEATURE_V7))
1632 return 0;
1633
1634 switch (op2) {
1635 case 0:
1636 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1637 case 1:
1638 return env->cp15.c0_clid;
1639 case 7:
1640 return 0;
1641 }
1642 goto bad_reg;
1643 case 2:
1644 if (op2 != 0 || crm != 0)
610c3c8a 1645 goto bad_reg;
a49ea279 1646 return env->cp15.c0_cssel;
9ee6e8bb
PB
1647 default:
1648 goto bad_reg;
b5ff1b31
FB
1649 }
1650 case 1: /* System configuration. */
c3d2689d
AZ
1651 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1652 op2 = 0;
b5ff1b31
FB
1653 switch (op2) {
1654 case 0: /* Control register. */
1655 return env->cp15.c1_sys;
1656 case 1: /* Auxiliary control register. */
c1713132 1657 if (arm_feature(env, ARM_FEATURE_XSCALE))
610c3c8a 1658 return env->cp15.c1_xscaleauxcr;
9ee6e8bb
PB
1659 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1660 goto bad_reg;
1661 switch (ARM_CPUID(env)) {
1662 case ARM_CPUID_ARM1026:
1663 return 1;
1664 case ARM_CPUID_ARM1136:
827df9f3 1665 case ARM_CPUID_ARM1136_R2:
9ee6e8bb
PB
1666 return 7;
1667 case ARM_CPUID_ARM11MPCORE:
1668 return 1;
1669 case ARM_CPUID_CORTEXA8:
533d177a 1670 return 2;
10055562
PB
1671 case ARM_CPUID_CORTEXA9:
1672 return 0;
9ee6e8bb
PB
1673 default:
1674 goto bad_reg;
1675 }
b5ff1b31 1676 case 2: /* Coprocessor access register. */
610c3c8a
AZ
1677 if (arm_feature(env, ARM_FEATURE_XSCALE))
1678 goto bad_reg;
b5ff1b31
FB
1679 return env->cp15.c1_coproc;
1680 default:
1681 goto bad_reg;
1682 }
ce819861
PB
1683 case 2: /* MMU Page table control / MPU cache control. */
1684 if (arm_feature(env, ARM_FEATURE_MPU)) {
1685 switch (op2) {
1686 case 0:
1687 return env->cp15.c2_data;
1688 break;
1689 case 1:
1690 return env->cp15.c2_insn;
1691 break;
1692 default:
1693 goto bad_reg;
1694 }
1695 } else {
9ee6e8bb
PB
1696 switch (op2) {
1697 case 0:
1698 return env->cp15.c2_base0;
1699 case 1:
1700 return env->cp15.c2_base1;
1701 case 2:
b2fa1797 1702 return env->cp15.c2_control;
9ee6e8bb
PB
1703 default:
1704 goto bad_reg;
1705 }
1706 }
ce819861 1707 case 3: /* MMU Domain access control / MPU write buffer control. */
b5ff1b31
FB
1708 return env->cp15.c3;
1709 case 4: /* Reserved. */
1710 goto bad_reg;
ce819861 1711 case 5: /* MMU Fault status / MPU access permission. */
c3d2689d
AZ
1712 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1713 op2 = 0;
b5ff1b31
FB
1714 switch (op2) {
1715 case 0:
ce819861
PB
1716 if (arm_feature(env, ARM_FEATURE_MPU))
1717 return simple_mpu_ap_bits(env->cp15.c5_data);
b5ff1b31
FB
1718 return env->cp15.c5_data;
1719 case 1:
ce819861
PB
1720 if (arm_feature(env, ARM_FEATURE_MPU))
1721 return simple_mpu_ap_bits(env->cp15.c5_data);
1722 return env->cp15.c5_insn;
1723 case 2:
1724 if (!arm_feature(env, ARM_FEATURE_MPU))
1725 goto bad_reg;
1726 return env->cp15.c5_data;
1727 case 3:
1728 if (!arm_feature(env, ARM_FEATURE_MPU))
1729 goto bad_reg;
b5ff1b31
FB
1730 return env->cp15.c5_insn;
1731 default:
1732 goto bad_reg;
1733 }
9ee6e8bb 1734 case 6: /* MMU Fault address. */
ce819861 1735 if (arm_feature(env, ARM_FEATURE_MPU)) {
9ee6e8bb 1736 if (crm >= 8)
ce819861 1737 goto bad_reg;
9ee6e8bb 1738 return env->cp15.c6_region[crm];
ce819861 1739 } else {
c3d2689d
AZ
1740 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1741 op2 = 0;
9ee6e8bb
PB
1742 switch (op2) {
1743 case 0:
1744 return env->cp15.c6_data;
1745 case 1:
1746 if (arm_feature(env, ARM_FEATURE_V6)) {
1747 /* Watchpoint Fault Adrress. */
1748 return 0; /* Not implemented. */
1749 } else {
1750 /* Instruction Fault Adrress. */
1751 /* Arm9 doesn't have an IFAR, but implementing it anyway
1752 shouldn't do any harm. */
1753 return env->cp15.c6_insn;
1754 }
1755 case 2:
1756 if (arm_feature(env, ARM_FEATURE_V6)) {
1757 /* Instruction Fault Adrress. */
1758 return env->cp15.c6_insn;
1759 } else {
1760 goto bad_reg;
1761 }
1762 default:
1763 goto bad_reg;
1764 }
b5ff1b31
FB
1765 }
1766 case 7: /* Cache control. */
6fbe23d5
PB
1767 /* FIXME: Should only clear Z flag if destination is r15. */
1768 env->ZF = 0;
b5ff1b31
FB
1769 return 0;
1770 case 8: /* MMU TLB control. */
1771 goto bad_reg;
1772 case 9: /* Cache lockdown. */
9ee6e8bb
PB
1773 switch (op1) {
1774 case 0: /* L1 cache. */
1775 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1776 return 0;
1777 switch (op2) {
1778 case 0:
1779 return env->cp15.c9_data;
1780 case 1:
1781 return env->cp15.c9_insn;
1782 default:
1783 goto bad_reg;
1784 }
1785 case 1: /* L2 cache */
1786 if (crm != 0)
1787 goto bad_reg;
1788 /* L2 Lockdown and Auxiliary control. */
c3d2689d 1789 return 0;
b5ff1b31
FB
1790 default:
1791 goto bad_reg;
1792 }
1793 case 10: /* MMU TLB lockdown. */
1794 /* ??? TLB lockdown not implemented. */
1795 return 0;
1796 case 11: /* TCM DMA control. */
1797 case 12: /* Reserved. */
1798 goto bad_reg;
1799 case 13: /* Process ID. */
1800 switch (op2) {
1801 case 0:
1802 return env->cp15.c13_fcse;
1803 case 1:
1804 return env->cp15.c13_context;
1805 default:
1806 goto bad_reg;
1807 }
1808 case 14: /* Reserved. */
1809 goto bad_reg;
1810 case 15: /* Implementation specific. */
c1713132 1811 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
c3d2689d 1812 if (op2 == 0 && crm == 1)
c1713132
AZ
1813 return env->cp15.c15_cpar;
1814
1815 goto bad_reg;
1816 }
c3d2689d
AZ
1817 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1818 switch (crm) {
1819 case 0:
1820 return 0;
1821 case 1: /* Read TI925T configuration. */
1822 return env->cp15.c15_ticonfig;
1823 case 2: /* Read I_max. */
1824 return env->cp15.c15_i_max;
1825 case 3: /* Read I_min. */
1826 return env->cp15.c15_i_min;
1827 case 4: /* Read thread-ID. */
1828 return env->cp15.c15_threadid;
1829 case 8: /* TI925T_status */
1830 return 0;
1831 }
827df9f3
AZ
1832 /* TODO: Peripheral port remap register:
1833 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
1834 * controller base address at $rn & ~0xfff and map size of
1835 * 0x200 << ($rn & 0xfff), when MMU is off. */
c3d2689d
AZ
1836 goto bad_reg;
1837 }
b5ff1b31
FB
1838 return 0;
1839 }
1840bad_reg:
1841 /* ??? For debugging only. Should raise illegal instruction exception. */
9ee6e8bb
PB
1842 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
1843 (insn >> 16) & 0xf, crm, op1, op2);
b5ff1b31
FB
1844 return 0;
1845}
1846
b0109805 1847void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
9ee6e8bb
PB
1848{
1849 env->banked_r13[bank_number(mode)] = val;
1850}
1851
b0109805 1852uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
9ee6e8bb
PB
1853{
1854 return env->banked_r13[bank_number(mode)];
1855}
1856
8984bd2e 1857uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
9ee6e8bb
PB
1858{
1859 switch (reg) {
1860 case 0: /* APSR */
1861 return xpsr_read(env) & 0xf8000000;
1862 case 1: /* IAPSR */
1863 return xpsr_read(env) & 0xf80001ff;
1864 case 2: /* EAPSR */
1865 return xpsr_read(env) & 0xff00fc00;
1866 case 3: /* xPSR */
1867 return xpsr_read(env) & 0xff00fdff;
1868 case 5: /* IPSR */
1869 return xpsr_read(env) & 0x000001ff;
1870 case 6: /* EPSR */
1871 return xpsr_read(env) & 0x0700fc00;
1872 case 7: /* IEPSR */
1873 return xpsr_read(env) & 0x0700edff;
1874 case 8: /* MSP */
1875 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
1876 case 9: /* PSP */
1877 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
1878 case 16: /* PRIMASK */
1879 return (env->uncached_cpsr & CPSR_I) != 0;
1880 case 17: /* FAULTMASK */
1881 return (env->uncached_cpsr & CPSR_F) != 0;
1882 case 18: /* BASEPRI */
1883 case 19: /* BASEPRI_MAX */
1884 return env->v7m.basepri;
1885 case 20: /* CONTROL */
1886 return env->v7m.control;
1887 default:
1888 /* ??? For debugging only. */
1889 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
1890 return 0;
1891 }
1892}
1893
8984bd2e 1894void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
9ee6e8bb
PB
1895{
1896 switch (reg) {
1897 case 0: /* APSR */
1898 xpsr_write(env, val, 0xf8000000);
1899 break;
1900 case 1: /* IAPSR */
1901 xpsr_write(env, val, 0xf8000000);
1902 break;
1903 case 2: /* EAPSR */
1904 xpsr_write(env, val, 0xfe00fc00);
1905 break;
1906 case 3: /* xPSR */
1907 xpsr_write(env, val, 0xfe00fc00);
1908 break;
1909 case 5: /* IPSR */
1910 /* IPSR bits are readonly. */
1911 break;
1912 case 6: /* EPSR */
1913 xpsr_write(env, val, 0x0600fc00);
1914 break;
1915 case 7: /* IEPSR */
1916 xpsr_write(env, val, 0x0600fc00);
1917 break;
1918 case 8: /* MSP */
1919 if (env->v7m.current_sp)
1920 env->v7m.other_sp = val;
1921 else
1922 env->regs[13] = val;
1923 break;
1924 case 9: /* PSP */
1925 if (env->v7m.current_sp)
1926 env->regs[13] = val;
1927 else
1928 env->v7m.other_sp = val;
1929 break;
1930 case 16: /* PRIMASK */
1931 if (val & 1)
1932 env->uncached_cpsr |= CPSR_I;
1933 else
1934 env->uncached_cpsr &= ~CPSR_I;
1935 break;
1936 case 17: /* FAULTMASK */
1937 if (val & 1)
1938 env->uncached_cpsr |= CPSR_F;
1939 else
1940 env->uncached_cpsr &= ~CPSR_F;
1941 break;
1942 case 18: /* BASEPRI */
1943 env->v7m.basepri = val & 0xff;
1944 break;
1945 case 19: /* BASEPRI_MAX */
1946 val &= 0xff;
1947 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
1948 env->v7m.basepri = val;
1949 break;
1950 case 20: /* CONTROL */
1951 env->v7m.control = val & 3;
1952 switch_v7m_sp(env, (val & 2) != 0);
1953 break;
1954 default:
1955 /* ??? For debugging only. */
1956 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
1957 return;
1958 }
1959}
1960
c1713132
AZ
1961void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
1962 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
1963 void *opaque)
1964{
1965 if (cpnum < 0 || cpnum > 14) {
1966 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
1967 return;
1968 }
1969
1970 env->cp[cpnum].cp_read = cp_read;
1971 env->cp[cpnum].cp_write = cp_write;
1972 env->cp[cpnum].opaque = opaque;
1973}
1974
b5ff1b31 1975#endif
6ddbc6e4
PB
1976
1977/* Note that signed overflow is undefined in C. The following routines are
1978 careful to use unsigned types where modulo arithmetic is required.
1979 Failure to do so _will_ break on newer gcc. */
1980
1981/* Signed saturating arithmetic. */
1982
1654b2d6 1983/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
1984static inline uint16_t add16_sat(uint16_t a, uint16_t b)
1985{
1986 uint16_t res;
1987
1988 res = a + b;
1989 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
1990 if (a & 0x8000)
1991 res = 0x8000;
1992 else
1993 res = 0x7fff;
1994 }
1995 return res;
1996}
1997
1654b2d6 1998/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
1999static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2000{
2001 uint8_t res;
2002
2003 res = a + b;
2004 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2005 if (a & 0x80)
2006 res = 0x80;
2007 else
2008 res = 0x7f;
2009 }
2010 return res;
2011}
2012
1654b2d6 2013/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
2014static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2015{
2016 uint16_t res;
2017
2018 res = a - b;
2019 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2020 if (a & 0x8000)
2021 res = 0x8000;
2022 else
2023 res = 0x7fff;
2024 }
2025 return res;
2026}
2027
1654b2d6 2028/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
2029static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2030{
2031 uint8_t res;
2032
2033 res = a - b;
2034 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2035 if (a & 0x80)
2036 res = 0x80;
2037 else
2038 res = 0x7f;
2039 }
2040 return res;
2041}
2042
2043#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2044#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2045#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2046#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2047#define PFX q
2048
2049#include "op_addsub.h"
2050
2051/* Unsigned saturating arithmetic. */
460a09c1 2052static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
2053{
2054 uint16_t res;
2055 res = a + b;
2056 if (res < a)
2057 res = 0xffff;
2058 return res;
2059}
2060
460a09c1 2061static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 2062{
4c4fd3f8 2063 if (a > b)
6ddbc6e4
PB
2064 return a - b;
2065 else
2066 return 0;
2067}
2068
2069static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2070{
2071 uint8_t res;
2072 res = a + b;
2073 if (res < a)
2074 res = 0xff;
2075 return res;
2076}
2077
2078static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2079{
4c4fd3f8 2080 if (a > b)
6ddbc6e4
PB
2081 return a - b;
2082 else
2083 return 0;
2084}
2085
2086#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2087#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2088#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2089#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2090#define PFX uq
2091
2092#include "op_addsub.h"
2093
2094/* Signed modulo arithmetic. */
2095#define SARITH16(a, b, n, op) do { \
2096 int32_t sum; \
2097 sum = (int16_t)((uint16_t)(a) op (uint16_t)(b)); \
2098 RESULT(sum, n, 16); \
2099 if (sum >= 0) \
2100 ge |= 3 << (n * 2); \
2101 } while(0)
2102
2103#define SARITH8(a, b, n, op) do { \
2104 int32_t sum; \
2105 sum = (int8_t)((uint8_t)(a) op (uint8_t)(b)); \
2106 RESULT(sum, n, 8); \
2107 if (sum >= 0) \
2108 ge |= 1 << n; \
2109 } while(0)
2110
2111
2112#define ADD16(a, b, n) SARITH16(a, b, n, +)
2113#define SUB16(a, b, n) SARITH16(a, b, n, -)
2114#define ADD8(a, b, n) SARITH8(a, b, n, +)
2115#define SUB8(a, b, n) SARITH8(a, b, n, -)
2116#define PFX s
2117#define ARITH_GE
2118
2119#include "op_addsub.h"
2120
2121/* Unsigned modulo arithmetic. */
2122#define ADD16(a, b, n) do { \
2123 uint32_t sum; \
2124 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2125 RESULT(sum, n, 16); \
a87aa10b 2126 if ((sum >> 16) == 1) \
6ddbc6e4
PB
2127 ge |= 3 << (n * 2); \
2128 } while(0)
2129
2130#define ADD8(a, b, n) do { \
2131 uint32_t sum; \
2132 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2133 RESULT(sum, n, 8); \
a87aa10b
AZ
2134 if ((sum >> 8) == 1) \
2135 ge |= 1 << n; \
6ddbc6e4
PB
2136 } while(0)
2137
2138#define SUB16(a, b, n) do { \
2139 uint32_t sum; \
2140 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2141 RESULT(sum, n, 16); \
2142 if ((sum >> 16) == 0) \
2143 ge |= 3 << (n * 2); \
2144 } while(0)
2145
2146#define SUB8(a, b, n) do { \
2147 uint32_t sum; \
2148 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2149 RESULT(sum, n, 8); \
2150 if ((sum >> 8) == 0) \
a87aa10b 2151 ge |= 1 << n; \
6ddbc6e4
PB
2152 } while(0)
2153
2154#define PFX u
2155#define ARITH_GE
2156
2157#include "op_addsub.h"
2158
2159/* Halved signed arithmetic. */
2160#define ADD16(a, b, n) \
2161 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2162#define SUB16(a, b, n) \
2163 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2164#define ADD8(a, b, n) \
2165 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2166#define SUB8(a, b, n) \
2167 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2168#define PFX sh
2169
2170#include "op_addsub.h"
2171
2172/* Halved unsigned arithmetic. */
2173#define ADD16(a, b, n) \
2174 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2175#define SUB16(a, b, n) \
2176 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2177#define ADD8(a, b, n) \
2178 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2179#define SUB8(a, b, n) \
2180 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2181#define PFX uh
2182
2183#include "op_addsub.h"
2184
2185static inline uint8_t do_usad(uint8_t a, uint8_t b)
2186{
2187 if (a > b)
2188 return a - b;
2189 else
2190 return b - a;
2191}
2192
2193/* Unsigned sum of absolute byte differences. */
2194uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2195{
2196 uint32_t sum;
2197 sum = do_usad(a, b);
2198 sum += do_usad(a >> 8, b >> 8);
2199 sum += do_usad(a >> 16, b >>16);
2200 sum += do_usad(a >> 24, b >> 24);
2201 return sum;
2202}
2203
2204/* For ARMv6 SEL instruction. */
2205uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2206{
2207 uint32_t mask;
2208
2209 mask = 0;
2210 if (flags & 1)
2211 mask |= 0xff;
2212 if (flags & 2)
2213 mask |= 0xff00;
2214 if (flags & 4)
2215 mask |= 0xff0000;
2216 if (flags & 8)
2217 mask |= 0xff000000;
2218 return (a & mask) | (b & ~mask);
2219}
2220
5e3f878a
PB
2221uint32_t HELPER(logicq_cc)(uint64_t val)
2222{
2223 return (val >> 32) | (val != 0);
2224}
4373f3ce
PB
2225
2226/* VFP support. We follow the convention used for VFP instrunctions:
2227 Single precition routines have a "s" suffix, double precision a
2228 "d" suffix. */
2229
2230/* Convert host exception flags to vfp form. */
2231static inline int vfp_exceptbits_from_host(int host_bits)
2232{
2233 int target_bits = 0;
2234
2235 if (host_bits & float_flag_invalid)
2236 target_bits |= 1;
2237 if (host_bits & float_flag_divbyzero)
2238 target_bits |= 2;
2239 if (host_bits & float_flag_overflow)
2240 target_bits |= 4;
2241 if (host_bits & float_flag_underflow)
2242 target_bits |= 8;
2243 if (host_bits & float_flag_inexact)
2244 target_bits |= 0x10;
cecd8504
PM
2245 if (host_bits & float_flag_input_denormal)
2246 target_bits |= 0x80;
4373f3ce
PB
2247 return target_bits;
2248}
2249
2250uint32_t HELPER(vfp_get_fpscr)(CPUState *env)
2251{
2252 int i;
2253 uint32_t fpscr;
2254
2255 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2256 | (env->vfp.vec_len << 16)
2257 | (env->vfp.vec_stride << 20);
2258 i = get_float_exception_flags(&env->vfp.fp_status);
2259 fpscr |= vfp_exceptbits_from_host(i);
2260 return fpscr;
2261}
2262
01653295
PM
2263uint32_t vfp_get_fpscr(CPUState *env)
2264{
2265 return HELPER(vfp_get_fpscr)(env);
2266}
2267
4373f3ce
PB
2268/* Convert vfp exception flags to target form. */
2269static inline int vfp_exceptbits_to_host(int target_bits)
2270{
2271 int host_bits = 0;
2272
2273 if (target_bits & 1)
2274 host_bits |= float_flag_invalid;
2275 if (target_bits & 2)
2276 host_bits |= float_flag_divbyzero;
2277 if (target_bits & 4)
2278 host_bits |= float_flag_overflow;
2279 if (target_bits & 8)
2280 host_bits |= float_flag_underflow;
2281 if (target_bits & 0x10)
2282 host_bits |= float_flag_inexact;
cecd8504
PM
2283 if (target_bits & 0x80)
2284 host_bits |= float_flag_input_denormal;
4373f3ce
PB
2285 return host_bits;
2286}
2287
2288void HELPER(vfp_set_fpscr)(CPUState *env, uint32_t val)
2289{
2290 int i;
2291 uint32_t changed;
2292
2293 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2294 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2295 env->vfp.vec_len = (val >> 16) & 7;
2296 env->vfp.vec_stride = (val >> 20) & 3;
2297
2298 changed ^= val;
2299 if (changed & (3 << 22)) {
2300 i = (val >> 22) & 3;
2301 switch (i) {
2302 case 0:
2303 i = float_round_nearest_even;
2304 break;
2305 case 1:
2306 i = float_round_up;
2307 break;
2308 case 2:
2309 i = float_round_down;
2310 break;
2311 case 3:
2312 i = float_round_to_zero;
2313 break;
2314 }
2315 set_float_rounding_mode(i, &env->vfp.fp_status);
2316 }
cecd8504 2317 if (changed & (1 << 24)) {
fe76d976 2318 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
cecd8504
PM
2319 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2320 }
5c7908ed
PB
2321 if (changed & (1 << 25))
2322 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
4373f3ce 2323
b12c390b 2324 i = vfp_exceptbits_to_host(val);
4373f3ce 2325 set_float_exception_flags(i, &env->vfp.fp_status);
4373f3ce
PB
2326}
2327
01653295
PM
2328void vfp_set_fpscr(CPUState *env, uint32_t val)
2329{
2330 HELPER(vfp_set_fpscr)(env, val);
2331}
2332
4373f3ce
PB
2333#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2334
2335#define VFP_BINOP(name) \
2336float32 VFP_HELPER(name, s)(float32 a, float32 b, CPUState *env) \
2337{ \
2338 return float32_ ## name (a, b, &env->vfp.fp_status); \
2339} \
2340float64 VFP_HELPER(name, d)(float64 a, float64 b, CPUState *env) \
2341{ \
2342 return float64_ ## name (a, b, &env->vfp.fp_status); \
2343}
2344VFP_BINOP(add)
2345VFP_BINOP(sub)
2346VFP_BINOP(mul)
2347VFP_BINOP(div)
2348#undef VFP_BINOP
2349
2350float32 VFP_HELPER(neg, s)(float32 a)
2351{
2352 return float32_chs(a);
2353}
2354
2355float64 VFP_HELPER(neg, d)(float64 a)
2356{
66230e0d 2357 return float64_chs(a);
4373f3ce
PB
2358}
2359
2360float32 VFP_HELPER(abs, s)(float32 a)
2361{
2362 return float32_abs(a);
2363}
2364
2365float64 VFP_HELPER(abs, d)(float64 a)
2366{
66230e0d 2367 return float64_abs(a);
4373f3ce
PB
2368}
2369
2370float32 VFP_HELPER(sqrt, s)(float32 a, CPUState *env)
2371{
2372 return float32_sqrt(a, &env->vfp.fp_status);
2373}
2374
2375float64 VFP_HELPER(sqrt, d)(float64 a, CPUState *env)
2376{
2377 return float64_sqrt(a, &env->vfp.fp_status);
2378}
2379
2380/* XXX: check quiet/signaling case */
2381#define DO_VFP_cmp(p, type) \
2382void VFP_HELPER(cmp, p)(type a, type b, CPUState *env) \
2383{ \
2384 uint32_t flags; \
2385 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2386 case 0: flags = 0x6; break; \
2387 case -1: flags = 0x8; break; \
2388 case 1: flags = 0x2; break; \
2389 default: case 2: flags = 0x3; break; \
2390 } \
2391 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2392 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2393} \
2394void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2395{ \
2396 uint32_t flags; \
2397 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2398 case 0: flags = 0x6; break; \
2399 case -1: flags = 0x8; break; \
2400 case 1: flags = 0x2; break; \
2401 default: case 2: flags = 0x3; break; \
2402 } \
2403 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2404 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2405}
2406DO_VFP_cmp(s, float32)
2407DO_VFP_cmp(d, float64)
2408#undef DO_VFP_cmp
2409
2410/* Helper routines to perform bitwise copies between float and int. */
2411static inline float32 vfp_itos(uint32_t i)
2412{
2413 union {
2414 uint32_t i;
2415 float32 s;
2416 } v;
2417
2418 v.i = i;
2419 return v.s;
2420}
2421
2422static inline uint32_t vfp_stoi(float32 s)
2423{
2424 union {
2425 uint32_t i;
2426 float32 s;
2427 } v;
2428
2429 v.s = s;
2430 return v.i;
2431}
2432
2433static inline float64 vfp_itod(uint64_t i)
2434{
2435 union {
2436 uint64_t i;
2437 float64 d;
2438 } v;
2439
2440 v.i = i;
2441 return v.d;
2442}
2443
2444static inline uint64_t vfp_dtoi(float64 d)
2445{
2446 union {
2447 uint64_t i;
2448 float64 d;
2449 } v;
2450
2451 v.d = d;
2452 return v.i;
2453}
2454
2455/* Integer to float conversion. */
2456float32 VFP_HELPER(uito, s)(float32 x, CPUState *env)
2457{
2458 return uint32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2459}
2460
2461float64 VFP_HELPER(uito, d)(float32 x, CPUState *env)
2462{
2463 return uint32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2464}
2465
2466float32 VFP_HELPER(sito, s)(float32 x, CPUState *env)
2467{
2468 return int32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2469}
2470
2471float64 VFP_HELPER(sito, d)(float32 x, CPUState *env)
2472{
2473 return int32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2474}
2475
2476/* Float to integer conversion. */
2477float32 VFP_HELPER(toui, s)(float32 x, CPUState *env)
2478{
09d9487f
PM
2479 if (float32_is_any_nan(x)) {
2480 return float32_zero;
2481 }
4373f3ce
PB
2482 return vfp_itos(float32_to_uint32(x, &env->vfp.fp_status));
2483}
2484
2485float32 VFP_HELPER(toui, d)(float64 x, CPUState *env)
2486{
09d9487f
PM
2487 if (float64_is_any_nan(x)) {
2488 return float32_zero;
2489 }
4373f3ce
PB
2490 return vfp_itos(float64_to_uint32(x, &env->vfp.fp_status));
2491}
2492
2493float32 VFP_HELPER(tosi, s)(float32 x, CPUState *env)
2494{
09d9487f
PM
2495 if (float32_is_any_nan(x)) {
2496 return float32_zero;
2497 }
4373f3ce
PB
2498 return vfp_itos(float32_to_int32(x, &env->vfp.fp_status));
2499}
2500
2501float32 VFP_HELPER(tosi, d)(float64 x, CPUState *env)
2502{
09d9487f
PM
2503 if (float64_is_any_nan(x)) {
2504 return float32_zero;
2505 }
4373f3ce
PB
2506 return vfp_itos(float64_to_int32(x, &env->vfp.fp_status));
2507}
2508
2509float32 VFP_HELPER(touiz, s)(float32 x, CPUState *env)
2510{
09d9487f
PM
2511 if (float32_is_any_nan(x)) {
2512 return float32_zero;
2513 }
4373f3ce
PB
2514 return vfp_itos(float32_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2515}
2516
2517float32 VFP_HELPER(touiz, d)(float64 x, CPUState *env)
2518{
09d9487f
PM
2519 if (float64_is_any_nan(x)) {
2520 return float32_zero;
2521 }
4373f3ce
PB
2522 return vfp_itos(float64_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2523}
2524
2525float32 VFP_HELPER(tosiz, s)(float32 x, CPUState *env)
2526{
09d9487f
PM
2527 if (float32_is_any_nan(x)) {
2528 return float32_zero;
2529 }
4373f3ce
PB
2530 return vfp_itos(float32_to_int32_round_to_zero(x, &env->vfp.fp_status));
2531}
2532
2533float32 VFP_HELPER(tosiz, d)(float64 x, CPUState *env)
2534{
09d9487f
PM
2535 if (float64_is_any_nan(x)) {
2536 return float32_zero;
2537 }
4373f3ce
PB
2538 return vfp_itos(float64_to_int32_round_to_zero(x, &env->vfp.fp_status));
2539}
2540
2541/* floating point conversion */
2542float64 VFP_HELPER(fcvtd, s)(float32 x, CPUState *env)
2543{
2d627737
PM
2544 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2545 /* ARM requires that S<->D conversion of any kind of NaN generates
2546 * a quiet NaN by forcing the most significant frac bit to 1.
2547 */
2548 return float64_maybe_silence_nan(r);
4373f3ce
PB
2549}
2550
2551float32 VFP_HELPER(fcvts, d)(float64 x, CPUState *env)
2552{
2d627737
PM
2553 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2554 /* ARM requires that S<->D conversion of any kind of NaN generates
2555 * a quiet NaN by forcing the most significant frac bit to 1.
2556 */
2557 return float32_maybe_silence_nan(r);
4373f3ce
PB
2558}
2559
2560/* VFP3 fixed point conversion. */
2561#define VFP_CONV_FIX(name, p, ftype, itype, sign) \
2562ftype VFP_HELPER(name##to, p)(ftype x, uint32_t shift, CPUState *env) \
2563{ \
2564 ftype tmp; \
26a5e69a 2565 tmp = sign##int32_to_##ftype ((itype##_t)vfp_##p##toi(x), \
4373f3ce 2566 &env->vfp.fp_status); \
644ad806 2567 return ftype##_scalbn(tmp, -(int)shift, &env->vfp.fp_status); \
4373f3ce
PB
2568} \
2569ftype VFP_HELPER(to##name, p)(ftype x, uint32_t shift, CPUState *env) \
2570{ \
2571 ftype tmp; \
09d9487f
PM
2572 if (ftype##_is_any_nan(x)) { \
2573 return ftype##_zero; \
2574 } \
4373f3ce 2575 tmp = ftype##_scalbn(x, shift, &env->vfp.fp_status); \
72f24d15 2576 return vfp_ito##p(ftype##_to_##itype##_round_to_zero(tmp, \
4373f3ce
PB
2577 &env->vfp.fp_status)); \
2578}
2579
2580VFP_CONV_FIX(sh, d, float64, int16, )
2581VFP_CONV_FIX(sl, d, float64, int32, )
2582VFP_CONV_FIX(uh, d, float64, uint16, u)
2583VFP_CONV_FIX(ul, d, float64, uint32, u)
2584VFP_CONV_FIX(sh, s, float32, int16, )
2585VFP_CONV_FIX(sl, s, float32, int32, )
2586VFP_CONV_FIX(uh, s, float32, uint16, u)
2587VFP_CONV_FIX(ul, s, float32, uint32, u)
2588#undef VFP_CONV_FIX
2589
60011498
PB
2590/* Half precision conversions. */
2591float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUState *env)
2592{
2593 float_status *s = &env->vfp.fp_status;
2594 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2595 return float16_to_float32(a, ieee, s);
2596}
2597
2598uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUState *env)
2599{
2600 float_status *s = &env->vfp.fp_status;
2601 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2602 return float32_to_float16(a, ieee, s);
2603}
2604
4373f3ce
PB
2605float32 HELPER(recps_f32)(float32 a, float32 b, CPUState *env)
2606{
2607 float_status *s = &env->vfp.fp_status;
2608 float32 two = int32_to_float32(2, s);
2609 return float32_sub(two, float32_mul(a, b, s), s);
2610}
2611
2612float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUState *env)
2613{
2614 float_status *s = &env->vfp.fp_status;
2615 float32 three = int32_to_float32(3, s);
2616 return float32_sub(three, float32_mul(a, b, s), s);
2617}
2618
8f8e3aa4
PB
2619/* NEON helpers. */
2620
4373f3ce
PB
2621/* TODO: The architecture specifies the value that the estimate functions
2622 should return. We return the exact reciprocal/root instead. */
2623float32 HELPER(recpe_f32)(float32 a, CPUState *env)
2624{
2625 float_status *s = &env->vfp.fp_status;
2626 float32 one = int32_to_float32(1, s);
2627 return float32_div(one, a, s);
2628}
2629
2630float32 HELPER(rsqrte_f32)(float32 a, CPUState *env)
2631{
2632 float_status *s = &env->vfp.fp_status;
2633 float32 one = int32_to_float32(1, s);
2634 return float32_div(one, float32_sqrt(a, s), s);
2635}
2636
2637uint32_t HELPER(recpe_u32)(uint32_t a, CPUState *env)
2638{
2639 float_status *s = &env->vfp.fp_status;
2640 float32 tmp;
2641 tmp = int32_to_float32(a, s);
2642 tmp = float32_scalbn(tmp, -32, s);
2643 tmp = helper_recpe_f32(tmp, env);
2644 tmp = float32_scalbn(tmp, 31, s);
2645 return float32_to_int32(tmp, s);
2646}
2647
2648uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUState *env)
2649{
2650 float_status *s = &env->vfp.fp_status;
2651 float32 tmp;
2652 tmp = int32_to_float32(a, s);
2653 tmp = float32_scalbn(tmp, -32, s);
2654 tmp = helper_rsqrte_f32(tmp, env);
2655 tmp = float32_scalbn(tmp, 31, s);
2656 return float32_to_int32(tmp, s);
2657}
fe1479c3
PB
2658
2659void HELPER(set_teecr)(CPUState *env, uint32_t val)
2660{
2661 val &= 1;
2662 if (env->teecr != val) {
2663 env->teecr = val;
2664 tb_flush(env);
2665 }
2666}