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